⚡ Sangsakala Proxy 🏠 Home
Skip to content

Fix #709 Do not evaluate #elif after a taken group - #710

Merged
danmar merged 1 commit into
masterfrom
fix-709
Nov 4, 2026
Merged

danmar merged 1 commit into
masterfrom
fix-709

Conversation

@danmar

@danmar danmar commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

The change to the #if/#elif condition is correct. An #if is still evaluated only when the enclosing group is active (as before), and an #elif is now evaluated only when its state is ElseIsTrue. The state handling after the evaluation still gives the right result. I found no wrong preprocessor output, and the reported case now matches gcc and clang.

New findings:

  1. Side effect on the macroUsage and ifCond outputs, which cppcheck uses. An #elif after a taken group is no longer evaluated, so macros in its condition are no longer reported in macroUsage, and the #elif no longer gets an IfCond entry. Take #define A 1 / #if 1 / #elif A / #endif. Before the PR, A was reported as used at line 4. With the PR, A is not reported at all. Tools built on macro usage (for example the unused-macro check / MISRA 2.5 through the dump) may now see such macros as unused. If this is intended, it should be a deliberate decision with a test. Otherwise, record maybeUsedMacros for the skipped #elif.
  2. The fix is incomplete for an empty condition. The check !sameline(rawtok,rawtok->next) ("Syntax error in #elif") runs before the new skip logic. So #if 1 / #elif / #endif still aborts with an error and empty output, while gcc and clang accept it. This is not a regression; the base version behaves the same.

Testing

  • I built /work/pr with make -j8 testrunner simplecpp CXXOPTS=-Werror without warnings. ./testrunner exits with 0. run-tests.py shows 0 failed (21 skipped, 16 todos). pytest integration_test.py shows 128 passed. ./selfcheck.sh exits with 0.
  • I also built /work/base for comparison.
  • Combined input (scratch/a.c) with these cases:
    • #elif 1/0 and #elif garbage( after a taken group
    • #elif 1/0 after a taken #elif
    • a nested #if/#elif 1/0 inside a skipped group
    • #elif defined( after a false #ifdef
    • Results: gcc and clang print a f i j (and report errors for the malformed defined(, which is in a group that must be evaluated). Base aborts with division/modulo by zero. The PR stops at an empty #elifdef with no name (line 7).
  • Empty #elif after a taken group (not a regression; base is the same):
    #if 1
    a
    #elif
    b
    #endif
    /work/pr/simplecpp b.c prints b.c:3: syntax error: Syntax error in #elif and no code. gcc -E -P b.c and clang -E -P b.c print a without diagnostics. The same happens when the #if 1 / #elif is nested inside #if 0 (c.c); gcc and clang print x.
  • Macro usage and IfCond through the API. I wrote a small driver, scratch/dm.cpp, that calls simplecpp::preprocess(out,raw,files,cache,dui,&ol,&mu,&ic) and prints mu and ic. Input d.c:
    #define A 1
    #define B 1
    #if 1
    #elif A
    #elifdef B
    #endif
    Base output:
    usage A line 4
    usage A line 4
    ifcond line 3 1=1
    ifcond line 4 1=1
    
    PR output:
    ifcond line 3 1=1
    

Comments

simplecpp.cpp:3829

This check runs before the new skip logic, so an empty #elif after a taken group still aborts preprocessing:

#if 1
a
#elif
b
#endif

simplecpp prints Syntax error in #elif and gives no output. gcc and clang print a. The same happens when the #if 1/#elif is inside an #if 0 group. Since the PR is about not evaluating such #elif lines, consider applying the check only when the condition will actually be evaluated.


Automated review of a3e956c by Claude. The pull request was built and tested locally.

Comment thread simplecpp.cpp Resolved
@danmar
danmar merged commit 54b71fe into master Nov 4, 2026
49 checks passed
@danmar
danmar deleted the fix-709 branch November 4, 2026 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants