Skip to content

Commit 92b451a

Browse files
dmcppcheckclaude
andcommitted
Move the argument count check into Macro::getMacroParameters()
It now returns the argument delimiters of a valid call, or throws wrongNumberOfParameters, so a new expander can use it. Tests are added for the argument count. No behaviour change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6a9acd6 commit 92b451a

2 files changed

Lines changed: 67 additions & 19 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,10 +2199,33 @@ namespace simplecpp {
21992199
return ~0U;
22002200
}
22012201

2202+
/**
2203+
* Find the arguments of a call of this macro.
2204+
* @param nameTokInst the macro name in the call
2205+
* @param calledInDefine the call is in a #define, so the arguments must be on the same line
2206+
* @return the tokens that delimit the arguments: the '(', each ',' between two arguments and the ')'.
2207+
* Empty when the macro is not function-like or the name is not followed by '('.
2208+
* @throws wrongNumberOfParameters if a function-like macro has the wrong number of arguments
2209+
*/
22022210
std::vector<const Token *> getMacroParameters(const Token *nameTokInst, bool calledInDefine) const {
2203-
if (!nameTokInst->next || nameTokInst->next->op != '(' || !functionLike())
2211+
if (!functionLike())
22042212
return {};
2213+
std::vector<const Token *> parametertokens;
2214+
if (nameTokInst->next && nameTokInst->next->op == '(')
2215+
parametertokens = findArgumentDelimiters(nameTokInst, calledInDefine);
2216+
2217+
if (variadic) {
2218+
if (parametertokens.size() < args.size())
2219+
throw wrongNumberOfParameters(nameTokInst->location, name());
2220+
} else {
2221+
if (parametertokens.size() != args.size() + (args.empty() ? 2U : 1U))
2222+
throw wrongNumberOfParameters(nameTokInst->location, name());
2223+
}
2224+
return parametertokens;
2225+
}
22052226

2227+
/** The '(', each ',' between two arguments and the ')' of the call. nameTokInst is followed by '('. */
2228+
std::vector<const Token *> findArgumentDelimiters(const Token *nameTokInst, bool calledInDefine) const {
22062229
std::vector<const Token *> parametertokens;
22072230
parametertokens.emplace_back(nameTokInst->next);
22082231
unsigned int par = 0U;
@@ -2291,26 +2314,14 @@ namespace simplecpp {
22912314
const bool calledInDefine = (loc.fileIndex != nameTokInst->location.fileIndex ||
22922315
loc.line < nameTokInst->location.line);
22932316

2294-
std::vector<const Token*> parametertokens1(getMacroParameters(nameTokInst, calledInDefine));
2295-
2296-
if (functionLike()) {
2297-
// No arguments => not macro expansion
2298-
if (nameTokInst->next && nameTokInst->next->op != '(') {
2299-
output.push_back(new Token(nameTokInst->str(), loc));
2300-
return nameTokInst->next;
2301-
}
2302-
2303-
// Parse macro-call
2304-
if (variadic) {
2305-
if (parametertokens1.size() < args.size()) {
2306-
throw wrongNumberOfParameters(nameTokInst->location, name());
2307-
}
2308-
} else {
2309-
if (parametertokens1.size() != args.size() + (args.empty() ? 2U : 1U))
2310-
throw wrongNumberOfParameters(nameTokInst->location, name());
2311-
}
2317+
// No arguments => not macro expansion
2318+
if (functionLike() && nameTokInst->next && nameTokInst->next->op != '(') {
2319+
output.push_back(new Token(nameTokInst->str(), loc));
2320+
return nameTokInst->next;
23122321
}
23132322

2323+
std::vector<const Token*> parametertokens1(getMacroParameters(nameTokInst, calledInDefine));
2324+
23142325
// If macro call uses __COUNTER__ then expand that first
23152326
TokenList tokensparams(files);
23162327
std::vector<const Token *> parametertokens2;

‎test.cpp‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,42 @@ static void define_invalid_3()
11541154
ASSERT_EQUALS("file0,2,syntax_error,failed to expand 'R', Wrong number of parameters for macro 'R'.\n", toString(outputList));
11551155
}
11561156

1157+
static void define_wrong_number_of_arguments()
1158+
{
1159+
{
1160+
simplecpp::OutputList outputList;
1161+
ASSERT_EQUALS("", preprocess("#define f(a,b) a+b\nf(1)\n", &outputList));
1162+
ASSERT_EQUALS("file0,2,syntax_error,failed to expand 'f', Wrong number of parameters for macro 'f'.\n", toString(outputList));
1163+
}
1164+
{
1165+
simplecpp::OutputList outputList;
1166+
ASSERT_EQUALS("", preprocess("#define f(a,b) a+b\nf(1,2,3)\n", &outputList));
1167+
ASSERT_EQUALS("file0,2,syntax_error,failed to expand 'f', Wrong number of parameters for macro 'f'.\n", toString(outputList));
1168+
}
1169+
{
1170+
simplecpp::OutputList outputList;
1171+
ASSERT_EQUALS("", preprocess("#define f(a,b,...) a+b\nf(1)\n", &outputList));
1172+
ASSERT_EQUALS("file0,2,syntax_error,failed to expand 'f', Wrong number of parameters for macro 'f'.\n", toString(outputList));
1173+
}
1174+
{
1175+
simplecpp::OutputList outputList;
1176+
ASSERT_EQUALS("\n1", preprocess("#define f(a,...) a __VA_ARGS__\nf(1)\n", &outputList));
1177+
ASSERT_EQUALS("", toString(outputList));
1178+
}
1179+
{
1180+
// a function-like macro name that is not followed by ( is not expanded
1181+
simplecpp::OutputList outputList;
1182+
ASSERT_EQUALS("\nf ;", preprocess("#define f(a) a\nf;\n", &outputList));
1183+
ASSERT_EQUALS("", toString(outputList));
1184+
}
1185+
{
1186+
// TODO: f takes no arguments, gcc reports an error
1187+
simplecpp::OutputList outputList;
1188+
TODO_ASSERT_EQUALS("", "\n1", preprocess("#define f() 1\nf(1)\n", &outputList));
1189+
TODO_ASSERT_EQUALS("file0,2,syntax_error,failed to expand 'f', Wrong number of parameters for macro 'f'.\n", "", toString(outputList));
1190+
}
1191+
}
1192+
11571193
static void define_invalid_4()
11581194
{
11591195
const char code[] = "#define X(...)\n"
@@ -5550,6 +5586,7 @@ static void runTests(int argc, char **argv, Input input)
55505586
TEST_CASE(define_invalid_2);
55515587
TEST_CASE(define_invalid_3);
55525588
TEST_CASE(define_invalid_4);
5589+
TEST_CASE(define_wrong_number_of_arguments);
55535590
TEST_CASE(define_define_1);
55545591
TEST_CASE(define_define_2);
55555592
TEST_CASE(define_define_3);

0 commit comments

Comments
 (0)