Skip to content

Commit 2a7b431

Browse files
committed
move check, handle ; and { in macro arg
1 parent f6b719b commit 2a7b431

2 files changed

Lines changed: 23 additions & 13 deletions

File tree

‎lib/tokenize.cpp‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9047,19 +9047,6 @@ void Tokenizer::findGarbageCode() const
90479047
unknownMacroError(par->previous());
90489048
}
90499049

9050-
// Invalid integer literal as argument of a possible macro
9051-
if (tok->isNumber() && MathLib::isInt(tok->str()) && isTooLargeInt(tok)) {
9052-
const Token *prev = tok->previous();
9053-
while (prev && !Token::Match(prev, "(|;|{|}")) {
9054-
if (Token::Match(prev, ")|]"))
9055-
prev = prev->link();
9056-
prev = prev->previous();
9057-
}
9058-
if (prev && prev->str() == "(" && Token::Match(prev->previous(), "%name% (") && !prev->previous()->isKeyword())
9059-
unknownMacroError(prev->previous());
9060-
syntaxError(tok, "integer literal too large");
9061-
}
9062-
90639050
if (tok->isControlFlowKeyword()) {
90649051
if (Token::Match(tok, "if|while|for|switch")) { // if|while|for|switch (EXPR) { ... }
90659052
if (tok->previous() && !Token::Match(tok->previous(), "%name%|:|;|{|}|)")) {
@@ -9509,6 +9496,21 @@ void Tokenizer::findGarbageCode() const
95099496
if (Token::Match(tok, "[;{}] [ %name% %name% ] ;"))
95109497
syntaxError(tok->next());
95119498
}
9499+
9500+
// Integer literal too large: argument of a possible macro, or invalid code.
9501+
for (const Token *tok = tokens(); tok; tok = tok->next()) {
9502+
if (!tok->isNumber() || !MathLib::isInt(tok->str()) || !isTooLargeInt(tok))
9503+
continue;
9504+
const Token *prev = tok->previous();
9505+
while (prev && prev->str() != "(") {
9506+
if (Token::Match(prev, ")|]|}"))
9507+
prev = prev->link();
9508+
prev = prev->previous();
9509+
}
9510+
if (prev && Token::Match(prev->previous(), "%name% (") && !prev->previous()->isKeyword())
9511+
unknownMacroError(prev->previous());
9512+
syntaxError(tok, "integer literal too large");
9513+
}
95129514
}
95139515

95149516

‎test/testtokenize.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8868,6 +8868,14 @@ class TestTokenizer : public TestFixture {
88688868
ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("int x = 123456789012345678901234567890123456789;\n"),
88698869
SYNTAX,
88708870
"syntax error: integer literal too large");
8871+
// more specific errors take priority
8872+
ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("struct S s = MAC(.a = { 123456789012345678901234567890123456789 });\n", dinit(TokenizeOptions, $.cpp = false)),
8873+
SYNTAX,
8874+
"syntax error: ( . a");
8875+
// macro argument with braces
8876+
ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("int x = STR({ 123456789012345678901234567890123456789 });\n"),
8877+
UNKNOWN_MACRO,
8878+
"There is an unknown macro here somewhere. Configuration is required. If STR is a macro then please configure it.");
88718879
}
88728880

88738881
void cppKeywordInCSource() {

0 commit comments

Comments
 (0)