Skip to content

Commit 69e4255

Browse files
committed
fix(cpp): keep preprocessor recovery fill-only
Signed-off-by: Blank_Answer <97771966+blankanswer@users.noreply.github.com>
1 parent 4cf6c46 commit 69e4255

3 files changed

Lines changed: 12 additions & 81 deletions

File tree

internal/cbm/cbm.c

Lines changed: 3 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -429,75 +429,6 @@ static int result_find_same_def_identity(const CBMFileResult *result, const CBMD
429429
return -1;
430430
}
431431

432-
static bool cbm_pp_directive_matches(const char *p, int len, const char *word) {
433-
int word_len = (int)strlen(word);
434-
return len >= word_len && strncmp(p, word, (size_t)word_len) == 0 &&
435-
(len == word_len || !cbm_ident_char(p[word_len]));
436-
}
437-
438-
static bool cbm_line_is_preprocessor_branch_directive(const char *line, int len) {
439-
int p = 0;
440-
while (p < len && isspace((unsigned char)line[p])) {
441-
p++;
442-
}
443-
if (p >= len || line[p++] != '#') {
444-
return false;
445-
}
446-
while (p < len && isspace((unsigned char)line[p])) {
447-
p++;
448-
}
449-
int word_len = len - p;
450-
return cbm_pp_directive_matches(line + p, word_len, "if") ||
451-
cbm_pp_directive_matches(line + p, word_len, "ifdef") ||
452-
cbm_pp_directive_matches(line + p, word_len, "ifndef") ||
453-
cbm_pp_directive_matches(line + p, word_len, "elif") ||
454-
cbm_pp_directive_matches(line + p, word_len, "else") ||
455-
cbm_pp_directive_matches(line + p, word_len, "endif");
456-
}
457-
458-
static bool cbm_span_contains_preprocessor_branch(const char *source, int source_len,
459-
uint32_t start_line, uint32_t end_line) {
460-
int span_start = 0;
461-
int span_end = 0;
462-
if (!cbm_source_line_bounds(source, source_len, start_line, end_line, &span_start, &span_end)) {
463-
return false;
464-
}
465-
int line_start = span_start;
466-
while (line_start <= span_end) {
467-
int line_end = line_start;
468-
while (line_end < span_end && source[line_end] != '\n') {
469-
line_end++;
470-
}
471-
if (cbm_line_is_preprocessor_branch_directive(source + line_start, line_end - line_start)) {
472-
return true;
473-
}
474-
if (line_end >= span_end) {
475-
break;
476-
}
477-
line_start = line_end + 1;
478-
}
479-
return false;
480-
}
481-
482-
static bool cbm_should_replace_preprocessed_duplicate(const CBMDefinition *existing,
483-
const CBMDefinition *remapped,
484-
const char *source, int source_len) {
485-
if (!existing || !remapped || existing->start_line == 0 || remapped->start_line == 0) {
486-
return false;
487-
}
488-
if (existing->start_line == remapped->start_line && existing->end_line == remapped->end_line) {
489-
return false;
490-
}
491-
uint32_t start =
492-
existing->start_line < remapped->start_line ? existing->start_line : remapped->start_line;
493-
uint32_t existing_end =
494-
existing->end_line < existing->start_line ? existing->start_line : existing->end_line;
495-
uint32_t remapped_end =
496-
remapped->end_line < remapped->start_line ? remapped->start_line : remapped->end_line;
497-
uint32_t end = existing_end > remapped_end ? existing_end : remapped_end;
498-
return cbm_span_contains_preprocessor_branch(source, source_len, start, end);
499-
}
500-
501432
static void merge_missing_preprocessed_callables(CBMFileResult *dst, const CBMFileResult *src,
502433
CBMArena *arena,
503434
const CBMPreprocessedSource *preprocessed,
@@ -520,16 +451,13 @@ static void merge_missing_preprocessed_callables(CBMFileResult *dst, const CBMFi
520451
original_source_len)) {
521452
continue;
522453
}
454+
/* Successful expanded-source remaps are parse-coverage evidence. Raw
455+
* definitions remain primary; the final defs array is fill-only below. */
523456
if (!cbm_recovered_callables_push(arena, recovered, &remapped)) {
524457
continue;
525458
}
526-
int existing_idx = result_find_same_def_identity(dst, &remapped);
527-
if (existing_idx < 0) {
459+
if (result_find_same_def_identity(dst, &remapped) < 0) {
528460
cbm_defs_push(&dst->defs, arena, remapped);
529-
} else if (cbm_should_replace_preprocessed_duplicate(&dst->defs.items[existing_idx],
530-
&remapped, original_source,
531-
original_source_len)) {
532-
dst->defs.items[existing_idx] = remapped;
533461
}
534462
}
535463
}

internal/cbm/preprocessor.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ static bool has_preprocessor_work(const char *source, int source_len) {
3434
if (remaining >= 6 && strncmp(source + j, "ifndef", 6) == 0) {
3535
return true;
3636
}
37-
if (remaining >= 7 && strncmp(source + j, "include", 7) == 0) {
38-
return true;
39-
}
4037
if (remaining >= 3 && strncmp(source + j, "if ", 3) == 0) {
4138
return true;
4239
}

tests/test_extraction.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,9 +1357,11 @@ TEST(cpp_preproc_signature_gap_first_branch_define) {
13571357
ASSERT_FALSE(r->has_error);
13581358
const CBMDefinition *add = find_def_by_label_name(r, "Method", "addRegionSamplingListener");
13591359
ASSERT_NOT_NULL(add);
1360-
ASSERT_EQ(add->start_line, 17u);
1360+
/* Raw AST remains primary: the expanded active branch is coverage evidence,
1361+
* not a replacement for an existing raw definition. */
1362+
ASSERT_EQ(add->start_line, 22u);
13611363
ASSERT_EQ(add->end_line, 27u);
1362-
ASSERT_NEQ(add->end_line, 20u);
1364+
ASSERT_NEQ(add->start_line, 17u);
13631365
ASSERT(source_line_contains(src, add->start_line, "addRegionSamplingListener"));
13641366
ASSERT(source_line_contains(src, add->end_line, "}"));
13651367
const CBMDefinition *commit = find_def_by_label_name(r, "Method", "commit");
@@ -1396,7 +1398,8 @@ TEST(cpp_preproc_remap_failure_skips_macro_generated_callable) {
13961398
}
13971399

13981400
TEST(cpp_preproc_include_header_defs_not_main_owned) {
1399-
char tmpdir[256] = "/tmp/cbm_header_XXXXXX";
1401+
char tmpdir[512];
1402+
snprintf(tmpdir, sizeof(tmpdir), "%s/cbm_header_XXXXXX", cbm_tmpdir());
14001403
ASSERT_NOT_NULL(cbm_mkdtemp(tmpdir));
14011404

14021405
char header_path[512];
@@ -1408,6 +1411,9 @@ TEST(cpp_preproc_include_header_defs_not_main_owned) {
14081411

14091412
const char *includes[] = {tmpdir, NULL};
14101413
const char *src = "#include \"helper.h\"\n"
1414+
"#ifdef ENABLE_SECOND_PASS\n"
1415+
"int branch_value = 1;\n"
1416+
"#endif\n"
14111417
"int main_owned() { return 0; }\n";
14121418
CBMFileResult *r =
14131419
extract_with_preproc_options(src, CBM_LANG_CPP, "t", "main.cpp", NULL, includes);

0 commit comments

Comments
 (0)