Skip to content

Fix: simplecpp ## fails to expand function-like macro when '(' is not adjacent#8578

Open
paulafy6 wants to merge 1 commit into
cppcheck-opensource:mainfrom
paulafy6:fix-preprocessor-concat
Open

Fix: simplecpp ## fails to expand function-like macro when '(' is not adjacent#8578
paulafy6 wants to merge 1 commit into
cppcheck-opensource:mainfrom
paulafy6:fix-preprocessor-concat

Conversation

@paulafy6
Copy link
Copy Markdown

When the ## operator concatenates two tokens to form a function-like macro
name (e.g. PREFIX_ ## kind → PREFIX_SCALAR), simplecpp looked for the
argument list '(...)' only at B->next. In PAR-style indirection patterns
the '(' is separated from B by a comma or a variadic parameter token:

#define PAR(a, ...) a VA_ARGS
#define PREFIX_SCALAR(T, N) T N
#define DISPATCH(kind, ...) PAR(PREFIX_ ## kind, (VA_ARGS))
DISPATCH(SCALAR, int, x) // was: [unknownMacro] — now: int x

Because '(' was not found, expansion was aborted and the macro was
reported as unknownMacro, causing cppcheck to skip the entire translation
unit.

Fix: when B->next is not '(' and we are in the appendTokens context
(expandResult==false), walk forward on the same line skipping ',' separators
and resolving named parameter tokens via expandArg(). The first '(' found
(literally or as the head of an expanded argument) is used as lpar and
passed to appendTokens() as before. The forwardScan flag ensures
expandToken() is called on the result even when expandResult is false.

The forward scan is restricted to expandResult==false to avoid unintended
side-effects in the main expansion loop.

Add test preprocessor_doublesharp_funclike_forward_scan covering:

  • single DISPATCH call (PAR pattern with VA_ARGS indirection)
  • chained calls with two different PREFIX_ specialisations

@chrchr-github
Copy link
Copy Markdown
Collaborator

Thanks for your contribution. Please open a PR at the upstream repo https://github.com/cppcheck-opensource/simplecpp

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