Skip to content

Fix #760 (record argument for integer constant macros) - #761

Open
danmar wants to merge 3 commits into
masterfrom
fix-760
Open

danmar wants to merge 3 commits into
masterfrom
fix-760

Conversation

@danmar

@danmar danmar commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

dmcppcheck and others added 2 commits November 16, 2026 09:18
Fixes clang-tidy readability-convert-member-functions-to-static.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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 crash, hang or regression. All test suites pass. I have two remarks:

  • INTMAX_C / UINTMAX_C are not handled. isIntegerConstantMacro() only accepts n = 8/16/32/64. The C standard (C11 7.20.4.2) also lists INTMAX_C and UINTMAX_C among the integer constant macros, and for those arg stays empty.
  • arg is not always the text as written. If the call is inside a macro body (for example #define X(a) UINT32_C(a)), arg holds the substituted text with macros already expanded. With X(R(7) /* c */ + 8) it is 7 + 8, not R ( 7 ) + 8. The comment in simplecpp.h says "as written in the call". Either the code or that comment should change.

This is the first review of the PR; comments.md is empty.

Testing

Build and test suites

  • make -j8 testrunner simplecpp CXXOPTS=-Werror in pr/ built cleanly.
  • ./testrunner passed with no failures.
  • python3 run-tests.py: 176 tests, 0 failed (21 skipped, 15 todo, the same as before).
  • python3 -m pytest integration_test.py: 128 passed.
  • ./selfcheck.sh exited with 0.

Own driver
I wrote a small driver, scratch/drv.cpp. It calls simplecpp::preprocess with a macroUsage list and prints name line:col [arg] for each entry. I ran it on these cases:

  • calls inside a define (#define X(a) UINT32_C(a), #define Q UINT32_C, #define Y UINT32_C(7))
  • a call nested in another macro's arguments (F(UINT32_C(9)))
  • a macro passed as an argument (V(UINT32_C))
  • arguments that span lines, both at top level and inside a multi-line outer call (R(\nUINT32_C(5\n+6)))
  • comments, with comments both removed and kept
  • an unterminated UINT32_C( at end of file

The arguments came out as expected and there was no crash or hang. The preprocessed output matched gcc -E.

Problems found

  1. INTMAX_C / UINTMAX_C get no argument:
#define INTMAX_C(x) x
#define UINTMAX_C(x) x
INTMAX_C(300) UINTMAX_C(1)

./drv t6.c prints:

INTMAX_C 3:1 []
UINTMAX_C 3:15 []
  1. Inside a macro body, the argument is the expanded text, not the written text:
#define UINT32_C(x) x
#define X(a) UINT32_C(a)
#define R(a) a
X(R(7) /* c */ + 8)

The driver prints UINT32_C 4:1 [7 + 8].


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

Comment thread simplecpp.cpp Outdated
if (name.compare(pos, 3, "INT") != 0)
return false;
const std::string bits = name.substr(pos + 3);
return bits == "8_C" || bits == "16_C" || bits == "32_C" || bits == "64_C";

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

This misses INTMAX_C and UINTMAX_C. C11 7.20.4.2 lists them as integer constant macros next to INTn_C/UINTn_C, and they have the same kind of argument restrictions. Today INTMAX_C(300) gets an empty arg. If they should be covered, the check could also accept bits == "MAX_C", and the comments in simplecpp.h and in the tests would need updating.

Comment thread simplecpp.h Outdated
Location useLocation;
bool macroValueKnown;
/** The argument of a call of INTn_C / UINTn_C (n = 8, 16, 32, 64) as written in the call: the tokens
* between the parentheses joined with a single space, comments are skipped. Empty for all other macros. */

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

"as written in the call" does not hold when the call is inside a macro body. With #define X(a) UINT32_C(a) and #define R(a) a, the code X(R(7) /* c */ + 8) gives arg == "7 + 8": the parameter has been substituted and the macros in it expanded. That may be what cppcheck wants, but then this comment should say so, and a test for the call-inside-define case should be added. None of macroUsageArg1–5 covers it.

@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 new problems. The earlier review raised two points, and both are fixed in the current code. isIntegerConstantMacro() now accepts INTMAX_C and UINTMAX_C, which macroUsageArg7 covers. The doc comment of MacroUsage::arg in simplecpp.h now says that for a call inside a macro body the argument is the substituted and expanded text, and macroUsageArg6 tests that case. The build is clean with -Werror, all test suites pass, and my own cases gave the expected arg with no crash and no change to the preprocessed output compared with base.

Comments

# Comment By Where Status
1 INTMAX_C / UINTMAX_C are not handled AI review general ✅ Fixed
2 arg is not the written text when the call is in a macro body; the doc comment is wrong AI review general ✅ Fixed
3 isIntegerConstantMacro misses INTMAX_C / UINTMAX_C AI review simplecpp.cpp:2280 ✅ Fixed
4 the 'as written' doc comment is wrong for calls in a macro body; no test for it AI review simplecpp.h:468 ✅ Fixed

Testing

Build and test suites

  • make -j8 testrunner simplecpp CXXOPTS=-Werror in pr/: built cleanly.
  • ./testrunner: 0 failed (21 skipped, 15 todo).
  • python3 run-tests.py: no failures.
  • python3 -m pytest integration_test.py: 128 passed.
  • ./selfcheck.sh: exit code 0.

Own driver
I wrote scratch/drv.cpp, which calls simplecpp::preprocess with a macroUsage list and prints name line:col [arg] for each entry. I ran it on these cases:

  • a macro passed as an argument and then called inside the body (#define F(m) m(5) / F(UINT32_C)): gives [5]
  • a call formed after expansion (G(UINT32_C)(7+1)): gives [7 + 1]
  • an object-like macro that opens the call (#define H UINT32_C( / H 3)): gives [3]
  • the name and ( on different lines: gives [4]
  • calls in a macro body whose arguments span several lines (X(1\n+2), Y(\n3\n), Z(1,\n\n2)): gives [1 + 2], [3] and [9], [1 2]
  • nested integer constant macros together with a // comment: the outer call gives [INT8_C ( 1 ) + 2] and the inner one [1]
  • a call inside another macro's argument (W(UINT32_C(W(4)))): gives [W ( 4 )]
  • a stringified call (S(UINT32_C(3))): no usage is recorded
  • two calls in one object-like body: gives [1] and [2]
  • unterminated calls inside a define, including UINTMAX_C

There was no crash or hang. diff <(../base/simplecpp d.c) <(../pr/simplecpp d.c) showed no difference, so the PR does not change the preprocessed output.


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

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