Skip to content

Commit 06d3eec

Browse files
dmcppcheckclaude
andcommitted
HideSetExpander: handle #
# is an operator in a function-like macro when it is followed by a parameter; the argument as written in the call is turned into a string with stringify(). The whitespace after a parameter comes after its argument, and the expansion is followed by the whitespace after the macro call. Output: a # from a macro expansion at the start of a line gets a space before it, so it is not read as a directive, as gcc and clang do (clang tests hash_space.c and macro_not_define.c). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ec5d934 commit 06d3eec

2 files changed

Lines changed: 127 additions & 88 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,9 @@ std::string simplecpp::TokenList::stringify(bool linenrs) const
671671
ret << loc.line << ": ";
672672
}
673673

674-
if (sameline(tok->previous, tok))
674+
// a # from a macro expansion at the start of a line gets a space
675+
// before it, so the output is not read as a directive (as gcc and clang do)
676+
if (sameline(tok->previous, tok) || (tok->op == '#' && !tok->macro.empty()))
675677
ret << ' ';
676678

677679
ret << tok->str();
@@ -2950,12 +2952,12 @@ namespace simplecpp {
29502952
* The result of an expansion is pushed back onto the input and scanned again, so a
29512953
* function-like macro whose arguments follow the expansion in the source is found by
29522954
* normal rescanning.
2953-
* Work in progress: #, ##, __VA_OPT__ and "defined" in a replacement list are not handled yet.
2955+
* Work in progress: ##, __VA_OPT__ and "defined" in a replacement list are not handled yet.
29542956
*/
29552957
class HideSetExpander {
29562958
public:
2957-
HideSetExpander(const MacroMap &macros, std::vector<std::string> &files)
2958-
: macros(macros), files(files) {}
2959+
HideSetExpander(const MacroMap &macros, std::vector<std::string> &files, OutputList *outputList)
2960+
: macros(macros), files(files), outputList(outputList) {}
29592961

29602962
HideSetExpander(const HideSetExpander&) = delete;
29612963
HideSetExpander &operator=(const HideSetExpander&) = delete;
@@ -3054,6 +3056,8 @@ namespace simplecpp {
30543056

30553057
std::set<TokenString> hideSet;
30563058
std::vector<TokenList> arguments;
3059+
std::vector<TokenList> expandedArguments;
3060+
bool whitespaceAfter = tok->whitespaceahead;
30573061
if (macro.functionLike()) {
30583062
TokenList call(files);
30593063
call.push_back(tok);
@@ -3064,15 +3068,17 @@ namespace simplecpp {
30643068
const std::set<TokenString> &hs1 = call.cfront()->hideSet();
30653069
const std::set<TokenString> &hs2 = delimiters.back()->hideSet();
30663070
std::set_intersection(hs1.begin(), hs1.end(), hs2.begin(), hs2.end(), std::inserter(hideSet, hideSet.begin()));
3067-
arguments = expandArguments(getArguments(macro, delimiters));
3071+
arguments = getArguments(macro, delimiters);
3072+
expandedArguments = expandArguments(arguments);
3073+
whitespaceAfter = delimiters.back()->whitespaceahead;
30683074
} else {
30693075
hideSet = tok->hideSet();
30703076
delete tok;
30713077
}
30723078
hideSet.insert(macro.name());
30733079

30743080
TokenList result(files);
3075-
substitute(result, macro, arguments, hideSet);
3081+
substitute(result, macro, arguments, expandedArguments, hideSet, whitespaceAfter);
30763082
input.pushFront(result);
30773083
}
30783084
}
@@ -3115,19 +3121,44 @@ namespace simplecpp {
31153121
return expanded;
31163122
}
31173123

3118-
/** The replacement list of the macro, with the parameters replaced by the arguments */
3119-
void substitute(TokenList &result, const Macro &macro, const std::vector<TokenList> &arguments, const std::set<TokenString> &hideSet) const {
3124+
/** The parameter number of tok in the replacement list, or ~0U if it is not a parameter */
3125+
static unsigned int getParameter(const Macro &macro, const Token *tok) {
3126+
return (tok && tok != macro.endToken && tok->name) ? macro.getArgNum(tok->str()) : ~0U;
3127+
}
3128+
3129+
/**
3130+
* The replacement list of the macro, with the parameters replaced by the arguments.
3131+
* @param result destination tokenlist
3132+
* @param macro the macro
3133+
* @param arguments the arguments as written in the call, for #
3134+
* @param expandedArguments the arguments, macro expanded
3135+
* @param hideSet the hide set of the result
3136+
* @param whitespaceAfter there is whitespace after the macro call
3137+
*/
3138+
void substitute(TokenList &result, const Macro &macro, const std::vector<TokenList> &arguments, const std::vector<TokenList> &expandedArguments, const std::set<TokenString> &hideSet, bool whitespaceAfter) const {
31203139
if (macro.variadicOpt)
31213140
throw unsupported(loc, "__VA_OPT__");
31223141
for (const Token *tok = macro.valueToken; tok != macro.endToken; tok = tok->next) {
3123-
if (tok->op == '#')
3124-
throw unsupported(loc, "# and ##");
3142+
const Token * const next = (tok->next != macro.endToken) ? tok->next : nullptr;
3143+
if (tok->op == '#' && next && next->op == '#')
3144+
throw unsupported(loc, "##");
31253145
if (tok->str() == DEFINED)
31263146
throw unsupported(loc, "defined");
3127-
const unsigned int argnr = tok->name ? macro.getArgNum(tok->str()) : ~0U;
3128-
if (argnr < arguments.size()) {
3129-
for (const Token *argtok = arguments[argnr].cfront(); argtok; argtok = argtok->next)
3147+
// # is an operator in a function-like macro when it is followed by a parameter
3148+
const unsigned int hashArg = (tok->op == '#') ? getParameter(macro, next) : ~0U;
3149+
if (hashArg < arguments.size()) {
3150+
result.push_back(new Token(stringify(arguments[hashArg], loc, macro.name(), outputList), loc, next->whitespaceahead));
3151+
result.back()->macro = macro.name();
3152+
tok = next;
3153+
continue;
3154+
}
3155+
const unsigned int argnr = getParameter(macro, tok);
3156+
if (argnr < expandedArguments.size()) {
3157+
for (const Token *argtok = expandedArguments[argnr].cfront(); argtok; argtok = argtok->next)
31303158
result.push_back(new Token(*argtok));
3159+
// the whitespace after the parameter comes after the argument
3160+
if (tok->whitespaceahead && result.back())
3161+
result.back()->whitespaceahead = true;
31313162
} else {
31323163
result.push_back(new Token(tok->str(), loc, tok->whitespaceahead));
31333164
result.back()->macro = macro.name();
@@ -3138,10 +3169,15 @@ namespace simplecpp {
31383169
for (const TokenString &name : hideSet)
31393170
tok->addToHideSet(name);
31403171
}
3172+
// whitespace at the end of the replacement list does not count, the
3173+
// expansion is followed by the whitespace after the macro call
3174+
if (result.back())
3175+
result.back()->whitespaceahead = whitespaceAfter;
31413176
}
31423177

31433178
const MacroMap &macros;
31443179
std::vector<std::string> &files;
3180+
OutputList *outputList;
31453181
/** location of the macro name in the source, all tokens of the expansion get it */
31463182
Location loc;
31473183
};
@@ -4076,7 +4112,7 @@ static bool preprocessToken(simplecpp::TokenList &output, const simplecpp::Token
40764112
simplecpp::TokenList value(files);
40774113
try {
40784114
if (dui.hideSetExpansion) {
4079-
simplecpp::HideSetExpander expander(macros, files);
4115+
simplecpp::HideSetExpander expander(macros, files, outputList);
40804116
tok1 = expander.expand(value, tok);
40814117
} else {
40824118
tok1 = it->second.expand(value, tok, macros, files, outputList);

0 commit comments

Comments
 (0)