Skip to content

Commit 0e0f9de

Browse files
Refs #13771, #13776: No AST for function declarations (danmar#8222)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent da110ba commit 0e0f9de

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

lib/symboldatabase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
713713
}
714714
// function prototype?
715715
else if (declEnd && declEnd->str() == ";") {
716-
if (tok->astParent() && tok->astParent()->str() == "::" &&
716+
if ((Token::simpleMatch(tok->tokAt(-1), "::") || (tok->tokAt(-1) && Token::simpleMatch(tok->tokAt(-2), ":: ~"))) &&
717717
Token::Match(declEnd->previous(), "default|delete")) {
718718
addClassFunction(scope, tok, argStart);
719719
continue;

lib/tokenlist.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,8 +1787,9 @@ static Token * createAstAtToken(Token *tok)
17871787
}
17881788
}
17891789

1790-
if (Token::Match(tok, "%type% %name%|*|&|&&|::") && !Token::Match(tok, "return|new|delete")) {
1791-
int typecount = 0;
1790+
if ((Token::Match(tok, "%type% %name%|*|&|&&|::") && !Token::Match(tok, "return|new|delete")) ||
1791+
(Token::Match(tok, ":: %type%") && !tok->next()->isKeyword())) {
1792+
int typecount = tok->str() == "::" ? 1 : 0;
17921793
Token *typetok = tok;
17931794
while (Token::Match(typetok, "%type%|::|*|&|&&|<")) {
17941795
if (typetok->isName() && !Token::simpleMatch(typetok->previous(), "::"))
@@ -1811,7 +1812,7 @@ static Token * createAstAtToken(Token *tok)
18111812
!Token::Match(tok, "return|throw") &&
18121813
Token::Match(typetok->previous(), "%name% ( !!*") &&
18131814
typetok->previous()->varId() == 0 &&
1814-
!typetok->previous()->isKeyword() &&
1815+
(!typetok->previous()->isKeyword() || typetok->previous()->isOperatorKeyword()) &&
18151816
(skipMethodDeclEnding(typetok->link()) || Token::Match(typetok->link(), ") ;|{")))
18161817
return typetok;
18171818
}

test/testtokenize.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ class TestTokenizer : public TestFixture {
427427
TEST_CASE(astorkeyword);
428428
TEST_CASE(astenumdecl);
429429
TEST_CASE(astcompound);
430+
TEST_CASE(astfuncdecl);
430431

431432
TEST_CASE(startOfExecutableScope);
432433

@@ -6415,22 +6416,31 @@ class TestTokenizer : public TestFixture {
64156416
Z3
64166417
};
64176418

6419+
enum class ListSimplification : std::uint8_t {
6420+
Partial,
6421+
Full
6422+
};
6423+
64186424
template<size_t size>
6419-
std::string testAst(const char (&data)[size], AstStyle style = AstStyle::Simple) {
6425+
std::string testAst(const char (&data)[size], AstStyle style = AstStyle::Simple, ListSimplification ls = ListSimplification::Partial) {
64206426
// tokenize given code..
64216427
TokenList tokenlist{settings0, Standards::Language::CPP};
64226428
tokenlist.appendFileIfNew("test.cpp");
64236429
if (!tokenlist.createTokensFromString(data))
64246430
return "ERROR";
64256431

64266432
TokenizerTest tokenizer(std::move(tokenlist), *this);
6427-
tokenizer.combineStringAndCharLiterals();
6428-
tokenizer.combineOperators();
6429-
tokenizer.simplifySpaceshipOperator();
6430-
tokenizer.createLinks();
6431-
tokenizer.createLinks2();
6432-
tokenizer.simplifyCAlternativeTokens();
6433-
tokenizer.list.front()->assignIndexes();
6433+
if (ls == ListSimplification::Partial) {
6434+
tokenizer.combineStringAndCharLiterals();
6435+
tokenizer.combineOperators();
6436+
tokenizer.simplifySpaceshipOperator();
6437+
tokenizer.createLinks();
6438+
tokenizer.createLinks2();
6439+
tokenizer.simplifyCAlternativeTokens();
6440+
tokenizer.list.front()->assignIndexes();
6441+
} else { // Full
6442+
tokenizer.simplifyTokens1("");
6443+
}
64346444

64356445
// set varid..
64366446
for (Token *tok = tokenizer.list.front(); tok; tok = tok->next()) {
@@ -7428,6 +7438,11 @@ class TestTokenizer : public TestFixture {
74287438
ASSERT_EQUALS("s(sstrlens(0:?,{(return", testAst("return (struct Str) { (unsigned char*)s, s ? strlen(s) : 0 };"));
74297439
}
74307440

7441+
void astfuncdecl() {
7442+
ASSERT_EQUALS("", testAst("bool operator==(const S& a, const S& b);", AstStyle::Simple, ListSimplification::Full));
7443+
ASSERT_EQUALS("", testAst("::int32_t f();"));
7444+
}
7445+
74317446
#define isStartOfExecutableScope(offset, code) isStartOfExecutableScope_(offset, code, __FILE__, __LINE__)
74327447
template<size_t size>
74337448
bool isStartOfExecutableScope_(int offset, const char (&code)[size], const char* file, int line) {

0 commit comments

Comments
 (0)