Skip to content

Commit 1b79cd9

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

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
@@ -9004,19 +9004,6 @@ void Tokenizer::findGarbageCode() const
90049004
syntaxError(tok);
90059005
}
90069006

9007-
// Invalid integer literal as argument of a possible macro
9008-
if (tok->isNumber() && MathLib::isInt(tok->str()) && isTooLargeInt(tok)) {
9009-
const Token *prev = tok->previous();
9010-
while (prev && !Token::Match(prev, "(|;|{|}")) {
9011-
if (Token::Match(prev, ")|]"))
9012-
prev = prev->link();
9013-
prev = prev->previous();
9014-
}
9015-
if (prev && prev->str() == "(" && Token::Match(prev->previous(), "%name% (") && !prev->previous()->isKeyword())
9016-
unknownMacroError(prev->previous());
9017-
syntaxError(tok, "integer literal too large");
9018-
}
9019-
90209007
if (tok->isControlFlowKeyword()) {
90219008
if (Token::Match(tok, "if|while|for|switch")) { // if|while|for|switch (EXPR) { ... }
90229009
if (tok->previous() && !Token::Match(tok->previous(), "%name%|:|;|{|}|)")) {
@@ -9466,6 +9453,21 @@ void Tokenizer::findGarbageCode() const
94669453
if (Token::Match(tok, "[;{}] [ %name% %name% ] ;"))
94679454
syntaxError(tok->next());
94689455
}
9456+
9457+
// Integer literal too large: argument of a possible macro, or invalid code.
9458+
for (const Token *tok = tokens(); tok; tok = tok->next()) {
9459+
if (!tok->isNumber() || !MathLib::isInt(tok->str()) || !isTooLargeInt(tok))
9460+
continue;
9461+
const Token *prev = tok->previous();
9462+
while (prev && prev->str() != "(") {
9463+
if (Token::Match(prev, ")|]|}"))
9464+
prev = prev->link();
9465+
prev = prev->previous();
9466+
}
9467+
if (prev && Token::Match(prev->previous(), "%name% (") && !prev->previous()->isKeyword())
9468+
unknownMacroError(prev->previous());
9469+
syntaxError(tok, "integer literal too large");
9470+
}
94699471
}
94709472

94719473

‎test/testtokenize.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8861,6 +8861,14 @@ class TestTokenizer : public TestFixture {
88618861
ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("int x = 123456789012345678901234567890123456789;\n"),
88628862
SYNTAX,
88638863
"syntax error: integer literal too large");
8864+
// more specific errors take priority
8865+
ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("struct S s = MAC(.a = { 123456789012345678901234567890123456789 });\n", dinit(TokenizeOptions, $.cpp = false)),
8866+
SYNTAX,
8867+
"syntax error: ( . a");
8868+
// macro argument with braces
8869+
ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("int x = STR({ 123456789012345678901234567890123456789 });\n"),
8870+
UNKNOWN_MACRO,
8871+
"There is an unknown macro here somewhere. Configuration is required. If STR is a macro then please configure it.");
88648872
}
88658873

88668874
void cppKeywordInCSource() {

0 commit comments

Comments
 (0)