Skip to content

Commit a6f32d0

Browse files
fix(extract): derive unique names for GoogleTest macros to prevent same-file node collisions (DeusData#1266)
Signed-off-by: Harshita Joshi <j.harshitaa06@gmail.com>
1 parent 560ad40 commit a6f32d0

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
31993247
static 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

‎tests/test_extraction.c‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,41 @@ TEST(cpp_function) {
12851285
PASS();
12861286
}
12871287

1288+
/* #1266: GoogleTest TEST() macros with the same name collapse into a single
1289+
* node when multiple tests share a file. Each must mint a distinct Function
1290+
* node whose name encodes the suite and case arguments. */
1291+
TEST(cpp_gtest_same_name_collision_issue1266) {
1292+
CBMFileResult *r = extract(
1293+
"namespace demo { int assembleWidget(int s) { return s * 2; } }\n"
1294+
"TEST(WidgetSuite, DoublesSmallSize) { demo::assembleWidget(1); }\n"
1295+
"TEST(WidgetSuite, DoublesZero) { demo::assembleWidget(0); }\n"
1296+
"TEST(WidgetSuite, DoublesLargeSize) {\n"
1297+
" demo::assembleWidget(1000);\n"
1298+
"}\n",
1299+
CBM_LANG_CPP, "t", "direct_test.cpp");
1300+
ASSERT_NOT_NULL(r);
1301+
ASSERT(has_def(r, "Function", "TEST_WidgetSuite_DoublesSmallSize"));
1302+
ASSERT(has_def(r, "Function", "TEST_WidgetSuite_DoublesZero"));
1303+
ASSERT(has_def(r, "Function", "TEST_WidgetSuite_DoublesLargeSize"));
1304+
ASSERT(!has_def(r, "Function", "TEST"));
1305+
cbm_free_result(r);
1306+
PASS();
1307+
}
1308+
1309+
/* #1266: TEST_F fixture macro also produces unique names. */
1310+
TEST(cpp_gtest_f_unique_name_issue1266) {
1311+
CBMFileResult *r = extract(
1312+
"TEST_F(MyFixture, FirstTest) { doStuff(); }\n"
1313+
"TEST_F(MyFixture, SecondTest) { doOtherStuff(); }\n",
1314+
CBM_LANG_CPP, "t", "fixture_test.cpp");
1315+
ASSERT_NOT_NULL(r);
1316+
ASSERT(has_def(r, "Function", "TEST_F_MyFixture_FirstTest"));
1317+
ASSERT(has_def(r, "Function", "TEST_F_MyFixture_SecondTest"));
1318+
ASSERT(!has_def(r, "Function", "TEST_F"));
1319+
cbm_free_result(r);
1320+
PASS();
1321+
}
1322+
12881323
/* --- C++ out-of-line method definitions (#428) ---
12891324
* A .cpp defining methods of a class declared elsewhere (not in this TU).
12901325
* Pre-fix these were recorded as free Functions (label "Function", no
@@ -4816,6 +4851,8 @@ SUITE(extraction) {
48164851
RUN_TEST(rust_enum);
48174852
RUN_TEST(zig_struct);
48184853
RUN_TEST(cpp_function);
4854+
RUN_TEST(cpp_gtest_same_name_collision_issue1266);
4855+
RUN_TEST(cpp_gtest_f_unique_name_issue1266);
48194856
RUN_TEST(cpp_out_of_line_method_issue428);
48204857
RUN_TEST(cobol_paragraph);
48214858
RUN_TEST(verilog_module);

0 commit comments

Comments
 (0)