Skip to content

fix #13378: syntax error for guessed macro value #7218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
17 changes: 15 additions & 2 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8113,6 +8113,19 @@ void Tokenizer::unknownMacroError(const Token *tok1) const
throw InternalError(tok1, "There is an unknown macro here somewhere. Configuration is required. If " + tok1->str() + " is a macro then please configure it.", InternalError::UNKNOWN_MACRO);
}

void Tokenizer::unknownMacroSyntaxError(const Token *tok, const std::string &description,
const Token *errTok, const std::string &code) const
{
if (tok->isExpandedMacro() && mSettings.userDefines.empty()) {
throw InternalError(tok, description + ". Macro '" + tok->getMacroName() +
"' expands to '" + tok->str() + +"'. Use -D" + tok->getMacroName() +
"=... to specify a value, or -U" + tok->getMacroName()
+ " to undefine it.", InternalError::UNKNOWN_MACRO);
}
errTok = errTok ? errTok : tok;
syntaxError(errTok, code);
}

void Tokenizer::unhandled_macro_class_x_y(const Token *tok, const std::string& type, const std::string& x, const std::string& y, const std::string& bracket) const
{
reportError(tok,
Expand Down Expand Up @@ -8730,7 +8743,7 @@ void Tokenizer::findGarbageCode() const
syntaxError(tok);
if (Token::Match(tok, "%num%|%bool%|%char%|%str% {|(")) {
if (tok->strAt(1) == "(")
syntaxError(tok);
unknownMacroSyntaxError(tok, "literal used as function");
else if (!(tok->tokType() == Token::Type::eString && Token::simpleMatch(tok->tokAt(-1), "extern")) &&
!(tok->tokType() == Token::Type::eBoolean && cpp && Token::simpleMatch(tok->tokAt(-1), "requires")))
syntaxError(tok);
Expand Down Expand Up @@ -8768,7 +8781,7 @@ void Tokenizer::findGarbageCode() const
!Token::Match(tok->previous(), "{|, . %name% =|.|[|{") &&
!Token::Match(tok->previous(), ", . %name%")) {
if (!Token::Match(tok->previous(), "%name%|)|]|>|}"))
syntaxError(tok, tok->strAt(-1) + " " + tok->str() + " " + tok->strAt(1));
unknownMacroSyntaxError(tok->previous(), "member access on literal", tok, tok->strAt(-1) + " " + tok->str() + " " + tok->strAt(1));
if (!Token::Match(tok->next(), "%name%|*|~"))
syntaxError(tok, tok->strAt(-1) + " " + tok->str() + " " + tok->strAt(1));
}
Expand Down
4 changes: 4 additions & 0 deletions lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,10 @@ class CPPCHECKLIB Tokenizer {
/** Warn about unknown macro(s), configuration is recommended */
NORETURN void unknownMacroError(const Token *tok1) const;

/** Syntax error that may be due to an unknown macro, for example START() where START=1 */
NORETURN void unknownMacroSyntaxError(const Token *tok, const std::string &description,
const Token *errTok = nullptr, const std::string &code = emptyString) const;

void unhandledCharLiteral(const Token *tok, const std::string& msg) const;

private:
Expand Down
84 changes: 84 additions & 0 deletions test/testcppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class TestCppcheck : public TestFixture {
TEST_CASE(getDumpFileContentsRawTokens);
TEST_CASE(getDumpFileContentsLibrary);
TEST_CASE(getClangFlagsIncludeFile);
TEST_CASE(invalidSyntaxUnknownMacro1); // #13378
TEST_CASE(invalidSyntaxUnknownMacro2); // #13378
TEST_CASE(invalidSyntaxUnknownMacro3);
TEST_CASE(invalidSyntaxUnknownMacro4);
}

void getErrorMessages() const {
Expand Down Expand Up @@ -251,6 +255,86 @@ class TestCppcheck : public TestFixture {
ASSERT_EQUALS("-x c --include 1.h ", cppcheck.getClangFlags(Standards::Language::C));
}

void invalidSyntaxUnknownMacro1() { // #13378
ScopedFile file("test.c",
"void foo(void) {\n"
"#ifdef START\n"
" START();\n"
"#endif\n"
"}\n");
ErrorLogger2 errorLogger;
CppCheck cppcheck(errorLogger, false, {});

ASSERT_EQUALS(1, cppcheck.check(FileWithDetails(file.path())));
errorLogger.errmsgs.erase(std::remove_if(errorLogger.errmsgs.begin(), errorLogger.errmsgs.end(), [](const ErrorMessage& msg) {
return msg.id == "logChecker";
}), errorLogger.errmsgs.end());
ASSERT_EQUALS(1, errorLogger.errmsgs.size());
ASSERT_EQUALS("literal used as function. Macro 'START' expands to '1'. Use -DSTART=... to specify a value, or -USTART to undefine it.",
errorLogger.errmsgs.cbegin()->shortMessage());
ASSERT_EQUALS("unknownMacro", errorLogger.errmsgs.cbegin()->id);
}

void invalidSyntaxUnknownMacro2() { // #13378
ScopedFile file("test.c",
"void foo(void) {\n"
"#ifdef START\n"
" START();\n"
"#endif\n"
"}\n");
ErrorLogger2 errorLogger;
CppCheck cppcheck(errorLogger, false, {});
cppcheck.settings().userDefines = "START=1"; // cppcheck -DSTART ...

ASSERT_EQUALS(1, cppcheck.check(FileWithDetails(file.path())));
errorLogger.errmsgs.erase(std::remove_if(errorLogger.errmsgs.begin(), errorLogger.errmsgs.end(), [](const ErrorMessage& msg) {
return msg.id == "logChecker";
}), errorLogger.errmsgs.end());
ASSERT_EQUALS(1, errorLogger.errmsgs.size());
ASSERT_EQUALS("syntax error", errorLogger.errmsgs.cbegin()->shortMessage());
ASSERT_EQUALS("syntaxError", errorLogger.errmsgs.cbegin()->id);
}

void invalidSyntaxUnknownMacro3() {
ScopedFile file("test.c",
"void foo(void) {\n"
"#ifdef START\n"
" int k = START.f;\n"
"#endif\n"
"}\n");
ErrorLogger2 errorLogger;
CppCheck cppcheck(errorLogger, false, {});

ASSERT_EQUALS(1, cppcheck.check(FileWithDetails(file.path())));
errorLogger.errmsgs.erase(std::remove_if(errorLogger.errmsgs.begin(), errorLogger.errmsgs.end(), [](const ErrorMessage& msg) {
return msg.id == "logChecker";
}), errorLogger.errmsgs.end());
ASSERT_EQUALS(1, errorLogger.errmsgs.size());
ASSERT_EQUALS("member access on literal. Macro 'START' expands to '1'. Use -DSTART=... to specify a value, or -USTART to undefine it.",
errorLogger.errmsgs.cbegin()->shortMessage());
ASSERT_EQUALS("unknownMacro", errorLogger.errmsgs.cbegin()->id);
}

void invalidSyntaxUnknownMacro4() {
ScopedFile file("test.c",
"void foo(void) {\n"
"#ifdef START\n"
" int k = START.f;\n"
"#endif\n"
"}\n");
ErrorLogger2 errorLogger;
CppCheck cppcheck(errorLogger, false, {});
cppcheck.settings().userDefines = "START=1"; // cppcheck -DSTART ...

ASSERT_EQUALS(1, cppcheck.check(FileWithDetails(file.path())));
errorLogger.errmsgs.erase(std::remove_if(errorLogger.errmsgs.begin(), errorLogger.errmsgs.end(), [](const ErrorMessage& msg) {
return msg.id == "logChecker";
}), errorLogger.errmsgs.end());
ASSERT_EQUALS(1, errorLogger.errmsgs.size());
ASSERT_EQUALS("syntax error: 1 . f", errorLogger.errmsgs.cbegin()->shortMessage());
ASSERT_EQUALS("syntaxError", errorLogger.errmsgs.cbegin()->id);
}

// TODO: test suppressions
// TODO: test all with FS
};
Expand Down
Loading