Skip to content

Commit

Permalink
Cleanup in createSymbolDatabaseIncompleteVars() (#6238)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Apr 6, 2024
1 parent 78ef618 commit a16f360
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 78 deletions.
75 changes: 5 additions & 70 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1451,67 +1451,6 @@ void SymbolDatabase::createSymbolDatabaseEnums()

void SymbolDatabase::createSymbolDatabaseIncompleteVars()
{
// TODO: replace with Keywords::getX()
static const std::unordered_set<std::string> cpp20keywords = {
"alignas",
"alignof",
"axiom",
"co_await",
"co_return",
"co_yield",
"concept",
"synchronized",
"consteval",
"reflexpr",
"requires",
};
static const std::unordered_set<std::string> cppkeywords = {
"asm",
"auto",
"catch",
"char",
"class",
"const",
"constexpr",
"decltype",
"default",
"do",
"enum",
"explicit",
"export",
"extern",
"final",
"friend",
"inline",
"mutable",
"namespace",
"new",
"noexcept",
"nullptr",
"override",
"private",
"protected",
"public",
"register",
"sizeof",
"static",
"static_assert",
"struct",
"template",
"this",
"thread_local",
"throw",
"try",
"typedef",
"typeid",
"typename",
"union",
"using",
"virtual",
"void",
"volatile",
"NULL",
};
for (Token* tok = mTokenizer.list.front(); tok != mTokenizer.list.back(); tok = tok->next()) {
const Scope * scope = tok->scope();
if (!scope)
Expand All @@ -1524,11 +1463,14 @@ void SymbolDatabase::createSymbolDatabaseIncompleteVars()
tok = tok->link();
continue;
}
if (Token::Match(tok, "catch|typeid (")) {
if (tok->isCpp() && (Token::Match(tok, "catch|typeid (") ||
Token::Match(tok, "static_cast|dynamic_cast|const_cast|reinterpret_cast"))) {
tok = tok->linkAt(1);
continue;
}
if (!(tok->isNameOnly() || tok->isKeyword()))
if (tok->str() == "NULL")
continue;
if (tok->isKeyword() || !tok->isNameOnly())
continue;
if (tok->type())
continue;
Expand All @@ -1544,16 +1486,9 @@ void SymbolDatabase::createSymbolDatabaseIncompleteVars()
tok = tok->linkAt(1);
continue;
}
if (tok->isKeyword())
continue;
// Skip goto labels
if (Token::simpleMatch(tok->previous(), "goto"))
continue;
// TODO: handle all C/C++ standards
if (cppkeywords.count(tok->str()) > 0)
continue;
if (tok->isCpp() && (mSettings.standards.cpp >= Standards::CPP20) && cpp20keywords.count(tok->str()) > 0)
continue;
std::string fstr = tok->str();
const Token* ftok = tok->previous();
while (Token::simpleMatch(ftok, "::")) {
Expand Down
22 changes: 16 additions & 6 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5414,6 +5414,21 @@ void Tokenizer::createLinks2()
}
}

void Tokenizer::markCppCasts()
{
if (isC())
return;
for (Token* tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "const_cast|dynamic_cast|reinterpret_cast|static_cast")) {
if (!Token::simpleMatch(tok->next(), "<") || !Token::simpleMatch(tok->linkAt(1), "> ("))
syntaxError(tok);
tok = tok->linkAt(1)->next();
tok->isCast(true);
}
}

}

void Tokenizer::sizeofAddParentheses()
{
for (Token *tok = list.front(); tok; tok = tok->next()) {
Expand Down Expand Up @@ -5807,12 +5822,7 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
createLinks2();

// Mark C++ casts
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "const_cast|dynamic_cast|reinterpret_cast|static_cast <") && Token::simpleMatch(tok->linkAt(1), "> (")) {
tok = tok->linkAt(1)->next();
tok->isCast(true);
}
}
markCppCasts();

// specify array size
arraySize();
Expand Down
5 changes: 5 additions & 0 deletions lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ class CPPCHECKLIB Tokenizer {
*/
void createLinks2();

/**
* Set isCast() for C++ casts
*/
void markCppCasts();

public:

/** Syntax error */
Expand Down
4 changes: 2 additions & 2 deletions test/testgarbage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1744,8 +1744,8 @@ class TestGarbage : public TestFixture {
ASSERT_THROW(checkCode("struct{}*"), InternalError); // don't crash
}
void garbageCode224() {
checkCode("void f(){ auto* b = dynamic_cast<const }"); // don't crash
(void)errout_str(); // we are not interested in the output
ASSERT_THROW(checkCode("void f(){ auto* b = dynamic_cast<const }"), InternalError); // don't crash
ASSERT_THROW(checkCode("void f(){ auto* b = dynamic_cast x; }"), InternalError);
}
void garbageCode225() {
ASSERT_THROW(checkCode("int n() { c * s0, 0 s0 = c(sizeof = ) }"), InternalError);
Expand Down

0 comments on commit a16f360

Please sign in to comment.