Fix #11302 debug: SymbolDatabase::isFunction found C function 'main' without a return type. (#5630)

This commit is contained in:
chrchr-github 2023-11-06 21:03:11 +01:00 committed by GitHub
parent a57fc9ace6
commit d26022ac9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 4 deletions

View File

@ -1997,7 +1997,7 @@ bool SymbolDatabase::isFunction(const Token *tok, const Scope* outerScope, const
Token::simpleMatch(tok->linkAt(1), ") {") &&
(!tok->previous() || Token::Match(tok->previous(), ";|}"))) {
if (mTokenizer.isC()) {
debugMessage(tok, "debug", "SymbolDatabase::isFunction found C function '" + tok->str() + "' without a return type.");
returnImplicitIntError(tok);
*funcStart = tok;
*argStart = tok->next();
*declEnd = tok->linkAt(1)->next();
@ -3522,6 +3522,19 @@ void SymbolDatabase::debugMessage(const Token *tok, const std::string &type, con
}
}
void SymbolDatabase::returnImplicitIntError(const Token *tok) const
{
if (tok && mSettings.severity.isEnabled(Severity::portability) && mSettings.standards.c != Standards::C89 && mErrorLogger) {
const std::list<const Token*> locationList(1, tok);
const ErrorMessage errmsg(locationList, &mTokenizer.list,
Severity::portability,
"returnImplicitInt",
"Omitted return type of function '" + tok->str() + "' defaults to int, this is not supported by ISO C99 and later standards.",
Certainty::normal);
mErrorLogger->reportErr(errmsg);
}
}
const Function* Type::getFunction(const std::string& funcName) const
{
if (classScope) {

View File

@ -1371,6 +1371,8 @@ public:
*/
void debugMessage(const Token *tok, const std::string &type, const std::string &msg) const;
void returnImplicitIntError(const Token *tok) const;
void printOut(const char * title = nullptr) const;
void printVariable(const Variable *var, const char *indent) const;
void printXml(std::ostream &out) const;

View File

@ -2380,12 +2380,12 @@ private:
}
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
void check_(const char* file, int line, const char code[], bool debug = true, const char filename[] = "test.cpp") {
void check_(const char* file, int line, const char code[], bool debug = true, const char filename[] = "test.cpp", const Settings* pSettings = nullptr) {
// Clear the error log
errout.str("");
// Check..
const Settings settings = settingsBuilder(settings1).debugwarnings(debug).build();
const Settings settings = settingsBuilder(pSettings ? *pSettings : settings1).debugwarnings(debug).build();
// Tokenize..
Tokenizer tokenizer(&settings, this);
@ -2986,7 +2986,12 @@ private:
ASSERT_EQUALS("", errout.str());
check("main(int argc, char *argv[]) { }", true, "test.c");
ASSERT_EQUALS("[test.c:1]: (debug) SymbolDatabase::isFunction found C function 'main' without a return type.\n", errout.str());
ASSERT_EQUALS("", errout.str());
const Settings s = settingsBuilder(settings1).severity(Severity::portability).build();
check("main(int argc, char *argv[]) { }", false, "test.c", &s);
ASSERT_EQUALS("[test.c:1]: (portability) Omitted return type of function 'main' defaults to int, this is not supported by ISO C99 and later standards.\n",
errout.str());
check("namespace boost {\n"
" std::locale generate_locale()\n"