diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 4dc75ba8c1f..3bde7a71e19 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -713,7 +713,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes() } // function prototype? else if (declEnd && declEnd->str() == ";") { - if (tok->astParent() && tok->astParent()->str() == "::" && + if ((Token::simpleMatch(tok->tokAt(-1), "::") || (tok->tokAt(-1) && Token::simpleMatch(tok->tokAt(-2), ":: ~"))) && Token::Match(declEnd->previous(), "default|delete")) { addClassFunction(scope, tok, argStart); continue; diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index d5b48ceab22..0ffcf7e37e4 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -1787,8 +1787,9 @@ static Token * createAstAtToken(Token *tok) } } - if (Token::Match(tok, "%type% %name%|*|&|&&|::") && !Token::Match(tok, "return|new|delete")) { - int typecount = 0; + if ((Token::Match(tok, "%type% %name%|*|&|&&|::") && !Token::Match(tok, "return|new|delete")) || + (Token::Match(tok, ":: %type%") && !tok->next()->isKeyword())) { + int typecount = tok->str() == "::" ? 1 : 0; Token *typetok = tok; while (Token::Match(typetok, "%type%|::|*|&|&&|<")) { if (typetok->isName() && !Token::simpleMatch(typetok->previous(), "::")) @@ -1811,7 +1812,7 @@ static Token * createAstAtToken(Token *tok) !Token::Match(tok, "return|throw") && Token::Match(typetok->previous(), "%name% ( !!*") && typetok->previous()->varId() == 0 && - !typetok->previous()->isKeyword() && + (!typetok->previous()->isKeyword() || typetok->previous()->isOperatorKeyword()) && (skipMethodDeclEnding(typetok->link()) || Token::Match(typetok->link(), ") ;|{"))) return typetok; } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index a698a3c20bf..e59f7b3be62 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -427,6 +427,7 @@ class TestTokenizer : public TestFixture { TEST_CASE(astorkeyword); TEST_CASE(astenumdecl); TEST_CASE(astcompound); + TEST_CASE(astfuncdecl); TEST_CASE(startOfExecutableScope); @@ -6415,8 +6416,13 @@ class TestTokenizer : public TestFixture { Z3 }; + enum class ListSimplification : std::uint8_t { + Partial, + Full + }; + template - std::string testAst(const char (&data)[size], AstStyle style = AstStyle::Simple) { + std::string testAst(const char (&data)[size], AstStyle style = AstStyle::Simple, ListSimplification ls = ListSimplification::Partial) { // tokenize given code.. TokenList tokenlist{settings0, Standards::Language::CPP}; tokenlist.appendFileIfNew("test.cpp"); @@ -6424,13 +6430,17 @@ class TestTokenizer : public TestFixture { return "ERROR"; TokenizerTest tokenizer(std::move(tokenlist), *this); - tokenizer.combineStringAndCharLiterals(); - tokenizer.combineOperators(); - tokenizer.simplifySpaceshipOperator(); - tokenizer.createLinks(); - tokenizer.createLinks2(); - tokenizer.simplifyCAlternativeTokens(); - tokenizer.list.front()->assignIndexes(); + if (ls == ListSimplification::Partial) { + tokenizer.combineStringAndCharLiterals(); + tokenizer.combineOperators(); + tokenizer.simplifySpaceshipOperator(); + tokenizer.createLinks(); + tokenizer.createLinks2(); + tokenizer.simplifyCAlternativeTokens(); + tokenizer.list.front()->assignIndexes(); + } else { // Full + tokenizer.simplifyTokens1(""); + } // set varid.. for (Token *tok = tokenizer.list.front(); tok; tok = tok->next()) { @@ -7428,6 +7438,11 @@ class TestTokenizer : public TestFixture { ASSERT_EQUALS("s(sstrlens(0:?,{(return", testAst("return (struct Str) { (unsigned char*)s, s ? strlen(s) : 0 };")); } + void astfuncdecl() { + ASSERT_EQUALS("", testAst("bool operator==(const S& a, const S& b);", AstStyle::Simple, ListSimplification::Full)); + ASSERT_EQUALS("", testAst("::int32_t f();")); + } + #define isStartOfExecutableScope(offset, code) isStartOfExecutableScope_(offset, code, __FILE__, __LINE__) template bool isStartOfExecutableScope_(int offset, const char (&code)[size], const char* file, int line) {