Skip to content

Commit 7515422

Browse files
dmcppcheckclaude
andcommitted
HideSetExpander: handle __VA_OPT__
As in C23 6.10.5.1: __VA_OPT__(content) is replaced by nothing when the variable arguments are macro expanded to nothing, otherwise by its content, which is substituted like the replacement list. #__VA_OPT__(...) gives a string literal, and __VA_OPT__ can be an operand of ##. These give the same result as gcc with the hide-set expansion, but not with the old one: F(E) where E expands to nothing, #__VA_OPT__(a b), and a ## __VA_OPT__(b c) ## d with no variable arguments. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 25e79f2 commit 7515422

2 files changed

Lines changed: 130 additions & 11 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2988,7 +2988,7 @@ namespace simplecpp {
29882988
* The result of an expansion is pushed back onto the input and scanned again, so a
29892989
* function-like macro whose arguments follow the expansion in the source is found by
29902990
* normal rescanning.
2991-
* Work in progress: __VA_OPT__ is not handled yet.
2991+
* Work in progress: the old Token::macro and location rules are not followed yet.
29922992
*/
29932993
class HideSetExpander {
29942994
public:
@@ -3073,10 +3073,6 @@ namespace simplecpp {
30733073
bool whitespaceAfter{};
30743074
};
30753075

3076-
static Macro::Error unsupported(const Location &loc, const std::string &what) {
3077-
return {loc, what + " is not supported by the hide-set expansion yet"};
3078-
}
3079-
30803076
/** Expand the pending tokens of the input. Source tokens are only read for macro arguments. */
30813077
void expandInput(TokenList &output, Input &input) {
30823078
while (!input.pending.empty()) {
@@ -3234,6 +3230,37 @@ namespace simplecpp {
32343230
result.takeTokens(operand);
32353231
}
32363232

3233+
/** tok is __VA_OPT__ in the replacement list of a variadic macro */
3234+
static bool isVaOpt(const Macro &macro, const Token *tok) {
3235+
return macro.variadicOpt && tok && tok != macro.endToken && tok->str() == "__VA_OPT__";
3236+
}
3237+
3238+
/**
3239+
* __VA_OPT__(content) is replaced by the content, or by nothing (a placemarker)
3240+
* when the variable arguments are macro expanded to nothing (C23 6.10.5.1).
3241+
* @param operand destination tokenlist
3242+
* @param macro the macro
3243+
* @param tok the __VA_OPT__ token, it is moved to the closing )
3244+
* @param call the macro call
3245+
*/
3246+
void getVaOpt(TokenList &operand, const Macro &macro, const Token *&tok, const MacroCall &call) const {
3247+
// the parentheses were checked when the macro was defined
3248+
const Token * const lpar = tok->next;
3249+
const Token *rpar = lpar->next;
3250+
for (int par = 1; rpar != macro.endToken; rpar = rpar->next) {
3251+
if (rpar->op == '(')
3252+
++par;
3253+
else if (rpar->op == ')' && --par == 0)
3254+
break;
3255+
}
3256+
tok = rpar;
3257+
const bool noVariableArguments = call.expandedArguments.empty() || call.expandedArguments.back().empty();
3258+
if (!noVariableArguments)
3259+
substituteRange(operand, macro, call, lpar->next, rpar);
3260+
if (operand.back())
3261+
operand.back()->whitespaceahead = rpar->whitespaceahead;
3262+
}
3263+
32373264
/** A new token from the replacement list of macro, Token::macro is the macro name */
32383265
Token *newToken(const TokenString &str, const Macro &macro, bool whitespaceahead) const {
32393266
auto *tok = new Token(str, loc, whitespaceahead);
@@ -3267,7 +3294,19 @@ namespace simplecpp {
32673294
tok = last;
32683295
return;
32693296
}
3297+
if (isVaOpt(macro, tok)) {
3298+
getVaOpt(operand, macro, tok, call);
3299+
return;
3300+
}
32703301
const Token * const next = (tok->next != macro.endToken) ? tok->next : nullptr;
3302+
if (tok->op == '#' && isVaOpt(macro, next)) {
3303+
// #__VA_OPT__(content): the content as a string literal
3304+
tok = next;
3305+
TokenList content(files);
3306+
getVaOpt(content, macro, tok, call);
3307+
operand.push_back(newToken(stringify(content, loc, macro.name(), outputList), macro, tok->whitespaceahead));
3308+
return;
3309+
}
32713310
// # is an operator in a function-like macro when it is followed by a parameter
32723311
const unsigned int hashArg = (tok->op == '#') ? getParameter(macro, next) : ~0U;
32733312
if (hashArg < call.arguments.size()) {
@@ -3333,8 +3372,6 @@ namespace simplecpp {
33333372
* @param call the macro call
33343373
*/
33353374
void substitute(TokenList &result, const Macro &macro, const MacroCall &call) const {
3336-
if (macro.variadicOpt)
3337-
throw unsupported(loc, "__VA_OPT__");
33383375
substituteRange(result, macro, call, macro.valueToken, macro.endToken);
33393376
for (Token *tok = result.front(); tok; tok = tok->next) {
33403377
tok->location = loc;

‎test.cpp‎

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,7 +1514,6 @@ static void define_va_args_4() // cppcheck trac #9754
15141514

15151515
static void define_va_opt_1()
15161516
{
1517-
HIDESET_EXPANSION_TODO(); // __VA_OPT__
15181517
const char code[] = "#define p1(fmt, args...) printf(fmt __VA_OPT__(,) args)\n"
15191518
"p1(\"hello\");\n"
15201519
"p1(\"%s\", \"hello\");\n";
@@ -1526,7 +1525,6 @@ static void define_va_opt_1()
15261525

15271526
static void define_va_opt_2()
15281527
{
1529-
HIDESET_EXPANSION_TODO(); // __VA_OPT__
15301528
const char code[] = "#define err(...)\\\n"
15311529
"__VA_OPT__(\\\n"
15321530
"printf(__VA_ARGS__);\\\n"
@@ -1637,7 +1635,6 @@ static void define_va_opt_7()
16371635

16381636
static void define_va_opt_8()
16391637
{
1640-
HIDESET_EXPANSION_TODO(); // __VA_OPT__
16411638
const char code[] = "#define f(...) #__VA_OPT__(x)\n"
16421639
"const char* v1 = f();\n";
16431640

@@ -2540,7 +2537,6 @@ static void macro_c11_example7() // C11 6.10.3.5 EXAMPLE 7
25402537

25412538
static void macro_c23_va_opt_examples() // C23 6.10.5.1 EXAMPLE
25422539
{
2543-
HIDESET_EXPANSION_TODO(); // __VA_OPT__
25442540
const char code[] =
25452541
"#define F(...) f(0 __VA_OPT__(,) __VA_ARGS__)\n"
25462542
"#define G(X, ...) f(0, X __VA_OPT__(,) __VA_ARGS__)\n"
@@ -5005,6 +5001,85 @@ static void hideSetExpansion_empty()
50055001
"< > < >", preprocess(code));
50065002
}
50075003

5004+
// __VA_OPT__ (C23 6.10.5.1). The results are the same as gcc gives.
5005+
static void hideSetExpansion_va_opt_empty_after_expansion()
5006+
{
5007+
const char code[] = "#define F(...) <__VA_OPT__(x)>\n"
5008+
"#define E\n"
5009+
"F() F(E) F(1)\n";
5010+
// TODO: the old macro expansion is wrong
5011+
TODO_OLD_EXPANSION_ASSERT_EQUALS("\n"
5012+
"\n"
5013+
"< > < > < x >",
5014+
"\n"
5015+
"\n"
5016+
"< > < x > < x >",
5017+
preprocess(code));
5018+
}
5019+
5020+
static void hideSetExpansion_va_opt_hash()
5021+
{
5022+
const char code[] = "#define X 1\n"
5023+
"#define F(a,...) #__VA_OPT__(a b)\n"
5024+
"F(X,2) F(X)\n";
5025+
// TODO: the old macro expansion is wrong
5026+
TODO_OLD_EXPANSION_ASSERT_EQUALS("\n"
5027+
"\n"
5028+
"\"1 b\" \"\"",
5029+
"\n"
5030+
"\n"
5031+
"\"X\" b \"\"",
5032+
preprocess(code));
5033+
}
5034+
5035+
static void hideSetExpansion_va_opt_hashhash()
5036+
{
5037+
const char code[] = "#define F(a,...) a ## __VA_OPT__(b c) ## d\n"
5038+
"F(x,1) F(x)\n";
5039+
// TODO: the old macro expansion is wrong
5040+
TODO_OLD_EXPANSION_ASSERT_EQUALS("\n"
5041+
"xb cd xd",
5042+
"\n"
5043+
"xb cd x# \"d\"",
5044+
preprocess(code));
5045+
}
5046+
5047+
static void hideSetExpansion_va_opt_hash_in_content()
5048+
{
5049+
const char code[] = "#define F(a,...) __VA_OPT__(a##a) __VA_OPT__(#a)\n"
5050+
"F(x,1)\n";
5051+
ASSERT_EQUALS("\n"
5052+
"xx \"x\"", preprocess(code));
5053+
}
5054+
5055+
static void hideSetExpansion_va_opt_rescan()
5056+
{
5057+
const char code[] = "#define LP (\n"
5058+
"#define F(...) __VA_OPT__(LP)\n"
5059+
"F(1)\n";
5060+
ASSERT_EQUALS("\n"
5061+
"\n"
5062+
"(", preprocess(code));
5063+
}
5064+
5065+
static void hideSetExpansion_va_opt_comma()
5066+
{
5067+
const char code[] = "#define F(a,...) f(a __VA_OPT__(,) __VA_ARGS__)\n"
5068+
"#define X 1\n"
5069+
"F(X) F(X,X)\n";
5070+
ASSERT_EQUALS("\n"
5071+
"\n"
5072+
"f ( 1 ) f ( 1 , 1 )", preprocess(code));
5073+
}
5074+
5075+
static void hideSetExpansion_va_opt_parentheses()
5076+
{
5077+
const char code[] = "#define F(...) __VA_OPT__((__VA_ARGS__))\n"
5078+
"F(a,(b)) F()\n";
5079+
ASSERT_EQUALS("\n"
5080+
"( a , ( b ) )", preprocess(code));
5081+
}
5082+
50085083
static void undef()
50095084
{
50105085
const char code[] = "#define A\n"
@@ -6099,6 +6174,13 @@ static void runTests(int argc, char **argv, Input input)
60996174
TEST_CASE(hideSetExpansion_deep);
61006175
TEST_CASE(hideSetExpansion_q_example);
61016176
TEST_CASE(hideSetExpansion_empty);
6177+
TEST_CASE(hideSetExpansion_va_opt_empty_after_expansion);
6178+
TEST_CASE(hideSetExpansion_va_opt_hash);
6179+
TEST_CASE(hideSetExpansion_va_opt_hashhash);
6180+
TEST_CASE(hideSetExpansion_va_opt_hash_in_content);
6181+
TEST_CASE(hideSetExpansion_va_opt_rescan);
6182+
TEST_CASE(hideSetExpansion_va_opt_comma);
6183+
TEST_CASE(hideSetExpansion_va_opt_parentheses);
61026184

61036185
TEST_CASE(undef);
61046186

0 commit comments

Comments
 (0)