Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8976,6 +8976,15 @@ void Tokenizer::findGarbageCode() const
return false;
};

const auto isTooLargeInt = [](const Token *tok) {
try {
(void)MathLib::toBigUNumber(tok);
return false;
} catch (const InternalError & /*e*/) {
return true;
}
};

for (const Token *tok = tokens(); tok; tok = tok->next()) {
// initialization: = {
if (Token::simpleMatch(tok, "= {") && Token::simpleMatch(tok->linkAt(1), "} ("))
Expand Down Expand Up @@ -9487,6 +9496,21 @@ void Tokenizer::findGarbageCode() const
if (Token::Match(tok, "[;{}] [ %name% %name% ] ;"))
syntaxError(tok->next());
}

// Integer literal too large: argument of a possible macro, or invalid code.
for (const Token *tok = tokens(); tok; tok = tok->next()) {
if (!tok->isNumber() || !MathLib::isInt(tok->str()) || !isTooLargeInt(tok))
continue;
const Token *prev = tok->previous();
while (prev && prev->str() != "(") {
if (Token::Match(prev, ")|]|}"))
prev = prev->link();
prev = prev->previous();
}
if (prev && Token::Match(prev->previous(), "%name% (") && !prev->previous()->isKeyword())
unknownMacroError(prev->previous());
syntaxError(tok, "integer literal too large");
}
}


Expand Down
4 changes: 2 additions & 2 deletions test/cli/other_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2928,7 +2928,7 @@ def test_outputfile(tmp_path): # #14051
assert out_text.splitlines() == out_exp


def test_internal_error_loc_int(tmp_path):
def test_syntax_error_loc_int(tmp_path):
test_file = tmp_path / 'test.c'
with open(test_file, 'wt') as f:
f.write(
Expand All @@ -2947,7 +2947,7 @@ def test_internal_error_loc_int(tmp_path):
assert exitcode == 0, stdout
assert stdout.splitlines() == []
assert stderr.splitlines() == [
'{}:3:13: error: Internal Error. MathLib::toBigUNumber: out_of_range: 0x10000000000000000 [internalError]'.format(test_file)
'{}:3:13: error: syntax error: integer literal too large [syntaxError]'.format(test_file)
Comment thread
ludviggunne marked this conversation as resolved.
Resolved
]


Expand Down
10 changes: 10 additions & 0 deletions test/testmathlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,16 @@ class TestMathLib : public TestFixture {
tok->str("invalid");
ASSERT_THROW_INTERNAL_EQUALS(MathLib::toBigUNumber(tok), INTERNAL, "Internal Error. MathLib::toBigUNumber: invalid_argument: invalid");
ASSERT_THROW_INTERNAL_EQUALS(MathLib::toBigUNumber("invalid", tok), INTERNAL, "Internal Error. MathLib::toBigUNumber: invalid_argument: invalid");

// the error location is the token (#13488)
tok->str("0x10000000000000000");
try {
(void)MathLib::toBigUNumber(tok);
assertThrowFail(__FILE__, __LINE__);
} catch (const InternalError &e) {
ASSERT_EQUALS("Internal Error. MathLib::toBigUNumber: out_of_range: 0x10000000000000000", e.errorMessage);
ASSERT(e.token == tok);
}
TokenList::deleteTokens(tok);
}

Expand Down
18 changes: 18 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ class TestTokenizer : public TestFixture {

TEST_CASE(unknownMacroBeforeReturn);
TEST_CASE(unknownMacroBracedArgument);
TEST_CASE(unknownMacroTooLargeInt);

TEST_CASE(cppKeywordInCSource);

Expand Down Expand Up @@ -8860,6 +8861,23 @@ class TestTokenizer : public TestFixture {
"There is an unknown macro here somewhere. Configuration is required. If F is a macro then please configure it.");
}

void unknownMacroTooLargeInt() {
ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("int x = STR(123456789012345678901234567890123456789);\n"),
UNKNOWN_MACRO,
"There is an unknown macro here somewhere. Configuration is required. If STR is a macro then please configure it.");
ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("int x = 123456789012345678901234567890123456789;\n"),
SYNTAX,
"syntax error: integer literal too large");
// more specific errors take priority
ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("struct S s = MAC(.a = { 123456789012345678901234567890123456789 });\n", dinit(TokenizeOptions, $.cpp = false)),
SYNTAX,
"syntax error: ( . a");
// macro argument with braces
ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("int x = STR({ 123456789012345678901234567890123456789 });\n"),
UNKNOWN_MACRO,
"There is an unknown macro here somewhere. Configuration is required. If STR is a macro then please configure it.");
}

void cppKeywordInCSource() {
ASSERT_NO_THROW(tokenizeAndStringify("int throw() {}\n", dinit(TokenizeOptions, $.cpp = false)));

Expand Down
Loading