Skip to content

Fix #774 (#define: stringizing a parameter named error or warning swallows the rest of the line) - #775

Open
wjakobsson wants to merge 2 commits into
cppcheck-opensource:masterfrom
wjakobsson:error-in-define
Open

wjakobsson wants to merge 2 commits into
cppcheck-opensource:masterfrom
wjakobsson:error-in-define

Conversation

@wjakobsson

Copy link
Copy Markdown
Collaborator

No description provided.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

I found no problems. The fix handles #774 correctly: the rest of the line after #error/#warning is now read as message text only when that # is the first token on the line. Comments are not counted when finding the first token. The check isLastLinePreprocessor(2) == ppTok looks back at most two tokens, so it costs almost nothing on the hot path of readfile. It does not change the API or C++11 compatibility. The new test error9 covers both #error and #warning used as stringizing operators. No earlier comments to check.

Testing

  • make -j8 testrunner simplecpp CXXOPTS=-Werror in pr/ built cleanly. I also built base/ to compare.
  • ./testrunner: 0 failed. python3 run-tests.py: 176 tests, 0 failed (21 skipped, 14 todo, same as before). python3 -m pytest integration_test.py: 128 passed. ./selfcheck.sh: exit code 0.
  • I compared these inputs between pr/, base/ and gcc -E:
    • #define A(error) f(#error); break / A(e1): base gives f ( "e1" ); break (the rest of the line is swallowed as one token, which is the bug). pr gives f ( "e1" ) ; break, which matches gcc.
    • #define S(warning) #warning, x y: base keeps x y as raw text. pr tokenizes it as gcc does.
    • The stringizing # at the start of a continuation line inside a #define (f(\ then #error); break on the next line, with and without a space after #): fixed, and the output matches gcc.
    • Real directives still produce the diagnostic as before: /*c*/ # /*d*/ error hello world, # warning hi there // c, and #\ followed by error ... on the next line.
    • #error inside #if 0, and int a; # error foo bar in the middle of a line: same output as base.

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

@ludviggunne

Copy link
Copy Markdown
Collaborator

What about other directives? E.g. #define F(define) #define.

@wjakobsson

wjakobsson commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator Author

What about other directives? E.g. #define F(define) #define.

They aren't affected. All other directives are tokenized as ordinary, only #warning and #error are exceptions, as their message is free text, and my not be valid tokens. (line1070)

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.

3 participants