Report unknownMacro in string concatenation (refs #11378) (#5558)

This commit is contained in:
chrchr-github 2023-10-18 18:46:47 +02:00 committed by GitHub
parent b60b374f9c
commit 285ef96b5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 9 deletions

View File

@ -8277,7 +8277,8 @@ void Tokenizer::reportUnknownMacros() const
// String concatenation with unknown macros
for (const Token *tok = tokens(); tok; tok = tok->next()) {
if (Token::Match(tok, "%str% %name% (") && Token::Match(tok->linkAt(2), ") %str%")) {
if ((Token::Match(tok, "%str% %name% (") && Token::Match(tok->linkAt(2), ") %str%")) ||
(Token::Match(tok, "%str% %name% %str%") && !(startsWith(tok->strAt(1), "PRI") || startsWith(tok->strAt(1), "SCN")))) { // TODO: implement macros in std.cfg
if (tok->next()->isKeyword())
continue;
unknownMacroError(tok->next());

View File

@ -6899,6 +6899,11 @@ private:
"void f(float a) {\n"
"S s = (S){ .i = (int)a };\n"
"}\n", /*expand*/ true, Platform::Type::Native, "test.c"));
ASSERT_THROW(tokenizeAndStringify("std::string g();\n"
"std::string f() {\n"
" return std::string{ g() + \"abc\" MACRO \"def\" };\n"
"}\n", /*expand*/ true, Platform::Type::Native, "test.cpp"), InternalError);
}
void findGarbageCode() { // Test Tokenizer::findGarbageCode()

View File

@ -3530,10 +3530,10 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: a\n", errout.str());
checkUninitVar("void f() {\n" // Don't crash
ASSERT_THROW(checkUninitVar("void f() {\n" // Don't crash
" int a;\n"
" dostuff(\"ab\" cd \"ef\", x?a:z);\n" // <- No AST is created for ?:
"}");
" dostuff(\"ab\" cd \"ef\", x?a:z);\n" // <- No AST is created for ?
"}"), InternalError);
// Unknown => bail out..
checkUninitVar("void f(int x) {\n"
@ -3652,10 +3652,10 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (warning) Uninitialized variable: a\n", errout.str());
valueFlowUninit("void f() {\n" // Don't crash
ASSERT_THROW(valueFlowUninit("void f() {\n" // Don't crash
" int a;\n"
" dostuff(\"ab\" cd \"ef\", x?a:z);\n" // <- No AST is created for ?:
"}");
" dostuff(\"ab\" cd \"ef\", x?a:z);\n" // <- No AST is created for ?
"}"), InternalError);
// Unknown => bail out..
valueFlowUninit("void f(int x) {\n"