Skip to content
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

fixed #13420 - Tokenizer did not contribute to --errorlist #7107

Merged
merged 1 commit into from
Jan 11, 2025
Merged
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
1 change: 1 addition & 0 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1893,6 +1893,7 @@ void CppCheck::getErrorMessages(ErrorLogger &errorlogger)

CheckUnusedFunctions::getErrorMessages(errorlogger);
Preprocessor::getErrorMessages(errorlogger, s);
Tokenizer::getErrorMessages(errorlogger, s);
}

void CppCheck::analyseClangTidy(const FileSettings &fileSettings)
Expand Down
33 changes: 25 additions & 8 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1126,8 +1126,7 @@ void Tokenizer::simplifyTypedef()
if (!ts.fail() && numberOfTypedefs[ts.name()] == 1 &&
(numberOfTypedefs.find(ts.getTypedefToken()->strAt(1)) == numberOfTypedefs.end() || ts.getTypedefToken()->strAt(2) == "(")) {
if (mSettings.severity.isEnabled(Severity::portability) && ts.isInvalidConstFunctionType(typedefs))
reportError(tok->next(), Severity::portability, "invalidConstFunctionType",
"It is unspecified behavior to const qualify a function type.");
invalidConstFunctionTypeError(tok->next());
typedefs.emplace(ts.name(), ts);
if (!ts.isStructEtc())
tok = ts.endToken();
Expand Down Expand Up @@ -5743,7 +5742,7 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
if (isCPP() && mSettings.severity.isEnabled(Severity::information)) {
for (const Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "class %type% %type% [:{]")) {
unhandled_macro_class_x_y(tok);
unhandled_macro_class_x_y(tok, tok->str(), tok->strAt(1), tok->strAt(2), tok->strAt(3));
}
}
}
Expand Down Expand Up @@ -8114,16 +8113,16 @@ 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::unhandled_macro_class_x_y(const Token *tok) const
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,
Severity::information,
"class_X_Y",
"The code '" +
tok->str() + " " +
tok->strAt(1) + " " +
tok->strAt(2) + " " +
tok->strAt(3) + "' is not handled. You can use -I or --include to add handling of this code.");
type + " " +
x + " " +
y + " " +
bracket + "' is not handled. You can use -I or --include to add handling of this code.");
}

void Tokenizer::macroWithSemicolonError(const Token *tok, const std::string &macroName) const
Expand All @@ -8134,6 +8133,14 @@ void Tokenizer::macroWithSemicolonError(const Token *tok, const std::string &mac
"Ensure that '" + macroName + "' is defined either using -I, --include or -D.");
}

void Tokenizer::invalidConstFunctionTypeError(const Token *tok) const
{
reportError(tok,
Severity::portability,
"invalidConstFunctionType",
"It is unspecified behavior to const qualify a function type.");
}

void Tokenizer::cppcheckError(const Token *tok) const
{
printDebugOutput(0, std::cout);
Expand Down Expand Up @@ -10908,3 +10915,13 @@ bool Tokenizer::isPacked(const Token * bodyStart) const
return d.linenr < bodyStart->linenr() && d.str == "#pragma pack(1)" && d.file == list.getFiles().front();
});
}

void Tokenizer::getErrorMessages(ErrorLogger& errorLogger, const Settings& settings)
{
Tokenizer tokenizer(settings, errorLogger);
tokenizer.invalidConstFunctionTypeError(nullptr);
// checkLibraryNoReturn
tokenizer.unhandled_macro_class_x_y(nullptr, emptyString, emptyString, emptyString, emptyString);
tokenizer.macroWithSemicolonError(nullptr, emptyString);
tokenizer.unhandledCharLiteral(nullptr, emptyString);
}
6 changes: 5 additions & 1 deletion lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,14 @@ class CPPCHECKLIB Tokenizer {
private:

/** Report that there is an unhandled "class x y {" code */
void unhandled_macro_class_x_y(const Token *tok) const;
void 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;

/** Check configuration (unknown macros etc) */
void checkConfiguration() const;
void macroWithSemicolonError(const Token *tok, const std::string &macroName) const;

void invalidConstFunctionTypeError(const Token *tok) const;

/**
* Is there C++ code in C file?
*/
Expand Down Expand Up @@ -628,6 +630,8 @@ class CPPCHECKLIB Tokenizer {
void setDirectives(std::list<Directive> directives);

std::string dumpTypedefInfo() const;

static void getErrorMessages(ErrorLogger& errorLogger, const Settings& settings);
private:
const Token *processFunc(const Token *tok2, bool inOperator) const;
Token *processFunc(Token *tok2, bool inOperator);
Expand Down
Loading