@@ -3209,6 +3209,54 @@ static char *go_receiver_type_name(CBMArena *a, TSNode recv, const char *source)
32093209 return NULL ;
32103210}
32113211
3212+ /* C++/CUDA: true when name is a GoogleTest test-definition macro whose
3213+ * invocations parse as function definitions with bare-identifier parameters.
3214+ * Multiple such macros in one file all share the extracted name (e.g. "TEST"),
3215+ * causing qualified-name collisions and node loss (#1266). */
3216+ static bool is_cpp_test_macro (const char * name ) {
3217+ return strcmp (name , "TEST" ) == 0 || strcmp (name , "TEST_F" ) == 0 ||
3218+ strcmp (name , "TEST_P" ) == 0 || strcmp (name , "TYPED_TEST" ) == 0 ||
3219+ strcmp (name , "TYPED_TEST_P" ) == 0 ;
3220+ }
3221+
3222+ /* Compose a unique name from a GoogleTest-style macro invocation by appending
3223+ * the macro arguments: TEST(Suite, Case) -> "TEST_Suite_Case".
3224+ * Returns an arena-allocated string, or NULL when the parameters cannot be
3225+ * resolved (caller keeps the original name in that case). */
3226+ static char * resolve_cpp_test_macro_name (CBMArena * a , const char * macro , TSNode node ,
3227+ const char * source ) {
3228+ TSNode params = ts_node_child_by_field_name (node , TS_FIELD ("parameters" ));
3229+ if (ts_node_is_null (params )) {
3230+ params = find_c_params (node );
3231+ }
3232+ if (ts_node_is_null (params )) {
3233+ return NULL ;
3234+ }
3235+
3236+ const char * args [2 ] = {NULL , NULL };
3237+ int count = 0 ;
3238+ uint32_t nc = ts_node_named_child_count (params );
3239+ for (uint32_t i = 0 ; i < nc && count < 2 ; i ++ ) {
3240+ TSNode child = ts_node_named_child (params , i );
3241+ if (ts_node_is_null (child )) {
3242+ continue ;
3243+ }
3244+ TSNode type_node = ts_node_child_by_field_name (child , TS_FIELD ("type" ));
3245+ char * text = cbm_node_text (a , ts_node_is_null (type_node ) ? child : type_node , source );
3246+ if (text && text [0 ]) {
3247+ args [count ++ ] = text ;
3248+ }
3249+ }
3250+
3251+ if (count == 2 ) {
3252+ return cbm_arena_sprintf (a , "%s_%s_%s" , macro , args [0 ], args [1 ]);
3253+ }
3254+ if (count == 1 ) {
3255+ return cbm_arena_sprintf (a , "%s_%s" , macro , args [0 ]);
3256+ }
3257+ return NULL ;
3258+ }
3259+
32123260static void extract_func_def (CBMExtractCtx * ctx , TSNode node , const CBMLangSpec * spec ) {
32133261 CBMArena * a = ctx -> arena ;
32143262
@@ -3230,6 +3278,20 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
32303278 return ;
32313279 }
32323280
3281+ /* C++/CUDA: GoogleTest macros (TEST, TEST_F, TEST_P, ...) parse as
3282+ * function definitions whose name resolves to the bare macro identifier.
3283+ * Multiple test cases per file collide on qualified name; derive a unique
3284+ * name from the macro arguments so each gets its own graph node (#1266). */
3285+ bool is_gtest = false;
3286+ if ((ctx -> language == CBM_LANG_CPP || ctx -> language == CBM_LANG_CUDA ) &&
3287+ is_cpp_test_macro (name )) {
3288+ char * gtest_name = resolve_cpp_test_macro_name (a , name , node , ctx -> source );
3289+ if (gtest_name ) {
3290+ name = gtest_name ;
3291+ is_gtest = true;
3292+ }
3293+ }
3294+
32333295 TSNode func_node = unwrap_template_inner (node , ctx -> language );
32343296
32353297 CBMDefinition def ;
@@ -3357,6 +3419,11 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
33573419 def .is_test = rust_def_is_test (def .decorators );
33583420 }
33593421
3422+ // C++/CUDA: GoogleTest macros are test functions (#1266).
3423+ if (is_gtest ) {
3424+ def .is_test = true;
3425+ }
3426+
33603427 // Docstring
33613428 def .docstring = extract_docstring (a , node , ctx -> source , ctx -> language );
33623429
0 commit comments