@@ -3196,6 +3196,54 @@ static char *go_receiver_type_name(CBMArena *a, TSNode recv, const char *source)
31963196 return NULL ;
31973197}
31983198
3199+ /* C++/CUDA: true when name is a GoogleTest test-definition macro whose
3200+ * invocations parse as function definitions with bare-identifier parameters.
3201+ * Multiple such macros in one file all share the extracted name (e.g. "TEST"),
3202+ * causing qualified-name collisions and node loss (#1266). */
3203+ static bool is_cpp_test_macro (const char * name ) {
3204+ return strcmp (name , "TEST" ) == 0 || strcmp (name , "TEST_F" ) == 0 ||
3205+ strcmp (name , "TEST_P" ) == 0 || strcmp (name , "TYPED_TEST" ) == 0 ||
3206+ strcmp (name , "TYPED_TEST_P" ) == 0 ;
3207+ }
3208+
3209+ /* Compose a unique name from a GoogleTest-style macro invocation by appending
3210+ * the macro arguments: TEST(Suite, Case) -> "TEST_Suite_Case".
3211+ * Returns an arena-allocated string, or NULL when the parameters cannot be
3212+ * resolved (caller keeps the original name in that case). */
3213+ static char * resolve_cpp_test_macro_name (CBMArena * a , const char * macro , TSNode node ,
3214+ const char * source ) {
3215+ TSNode params = ts_node_child_by_field_name (node , TS_FIELD ("parameters" ));
3216+ if (ts_node_is_null (params )) {
3217+ params = find_c_params (node );
3218+ }
3219+ if (ts_node_is_null (params )) {
3220+ return NULL ;
3221+ }
3222+
3223+ const char * args [2 ] = {NULL , NULL };
3224+ int count = 0 ;
3225+ uint32_t nc = ts_node_named_child_count (params );
3226+ for (uint32_t i = 0 ; i < nc && count < 2 ; i ++ ) {
3227+ TSNode child = ts_node_named_child (params , i );
3228+ if (ts_node_is_null (child )) {
3229+ continue ;
3230+ }
3231+ TSNode type_node = ts_node_child_by_field_name (child , TS_FIELD ("type" ));
3232+ char * text = cbm_node_text (a , ts_node_is_null (type_node ) ? child : type_node , source );
3233+ if (text && text [0 ]) {
3234+ args [count ++ ] = text ;
3235+ }
3236+ }
3237+
3238+ if (count == 2 ) {
3239+ return cbm_arena_sprintf (a , "%s_%s_%s" , macro , args [0 ], args [1 ]);
3240+ }
3241+ if (count == 1 ) {
3242+ return cbm_arena_sprintf (a , "%s_%s" , macro , args [0 ]);
3243+ }
3244+ return NULL ;
3245+ }
3246+
31993247static void extract_func_def (CBMExtractCtx * ctx , TSNode node , const CBMLangSpec * spec ) {
32003248 CBMArena * a = ctx -> arena ;
32013249
@@ -3217,6 +3265,20 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
32173265 return ;
32183266 }
32193267
3268+ /* C++/CUDA: GoogleTest macros (TEST, TEST_F, TEST_P, ...) parse as
3269+ * function definitions whose name resolves to the bare macro identifier.
3270+ * Multiple test cases per file collide on qualified name; derive a unique
3271+ * name from the macro arguments so each gets its own graph node (#1266). */
3272+ bool is_gtest = false;
3273+ if ((ctx -> language == CBM_LANG_CPP || ctx -> language == CBM_LANG_CUDA ) &&
3274+ is_cpp_test_macro (name )) {
3275+ char * gtest_name = resolve_cpp_test_macro_name (a , name , node , ctx -> source );
3276+ if (gtest_name ) {
3277+ name = gtest_name ;
3278+ is_gtest = true;
3279+ }
3280+ }
3281+
32203282 TSNode func_node = unwrap_template_inner (node , ctx -> language );
32213283
32223284 CBMDefinition def ;
@@ -3344,6 +3406,11 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
33443406 def .is_test = rust_def_is_test (def .decorators );
33453407 }
33463408
3409+ // C++/CUDA: GoogleTest macros are test functions (#1266).
3410+ if (is_gtest ) {
3411+ def .is_test = true;
3412+ }
3413+
33473414 // Docstring
33483415 def .docstring = extract_docstring (a , node , ctx -> source , ctx -> language );
33493416
0 commit comments