Skip to content

Commit 4183336

Browse files
authored
fix skipping of cpp attributes. (#4971)
* fix skipping of cpp attributes. * fix simplifyCPPAttribute loop. When the first token was the start of a c++ attribute on a function, so that tok->previous() was nullptr at the bottom of the loop, an extra token would be skipped. This could result in the corresponding function being omitted from the symbol table. * fix alignas test failure, enhance cpp attr test. * uncrustify. * fix redundantNextPrevious * delete redundant code. * add some tokenizer tests for simplifyCPPAttribute. * enhance noreturn symbol test. The order of a noreturn attribute and another attribute used to matter. Test both orders.
1 parent c3002f1 commit 4183336

3 files changed

Lines changed: 40 additions & 19 deletions

File tree

lib/tokenize.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8825,36 +8825,35 @@ void Tokenizer::simplifyCPPAttribute()
88258825
if (mSettings->standards.cpp < Standards::CPP11 || isC())
88268826
return;
88278827

8828-
for (Token *tok = list.front(); tok; tok = tok->next()) {
8828+
for (Token *tok = list.front(); tok;) {
88298829
if (!isCPPAttribute(tok) && !isAlignAttribute(tok)) {
8830+
tok = tok->next();
88308831
continue;
88318832
}
88328833
if (isCPPAttribute(tok)) {
88338834
if (Token::findsimplematch(tok->tokAt(2), "noreturn", tok->link())) {
8834-
Token * head = skipCPPOrAlignAttribute(tok);
8835+
Token * head = skipCPPOrAlignAttribute(tok)->next();
88358836
while (isCPPAttribute(head) || isAlignAttribute(head))
8836-
head = skipCPPOrAlignAttribute(head);
8837-
head = head->next();
8837+
head = skipCPPOrAlignAttribute(head)->next();
88388838
while (Token::Match(head, "%name%|::|*|&|<|>|,")) // skip return type
88398839
head = head->next();
88408840
if (head && head->str() == "(" && isFunctionHead(head, "{|;")) {
88418841
head->previous()->isAttributeNoreturn(true);
88428842
}
88438843
} else if (Token::findsimplematch(tok->tokAt(2), "nodiscard", tok->link())) {
8844-
Token * head = skipCPPOrAlignAttribute(tok);
8844+
Token * head = skipCPPOrAlignAttribute(tok)->next();
88458845
while (isCPPAttribute(head) || isAlignAttribute(head))
8846-
head = skipCPPOrAlignAttribute(head);
8847-
head = head->next();
8846+
head = skipCPPOrAlignAttribute(head)->next();
88488847
while (Token::Match(head, "%name%|::|*|&|<|>|,"))
88498848
head = head->next();
88508849
if (head && head->str() == "(" && isFunctionHead(head, "{|;")) {
88518850
head->previous()->isAttributeNodiscard(true);
88528851
}
88538852
} else if (Token::findsimplematch(tok->tokAt(2), "maybe_unused", tok->link())) {
8854-
Token* head = skipCPPOrAlignAttribute(tok);
8853+
Token* head = skipCPPOrAlignAttribute(tok)->next();
88558854
while (isCPPAttribute(head) || isAlignAttribute(head))
8856-
head = skipCPPOrAlignAttribute(head);
8857-
head->next()->isAttributeMaybeUnused(true);
8855+
head = skipCPPOrAlignAttribute(head)->next();
8856+
head->isAttributeMaybeUnused(true);
88588857
} else if (Token::Match(tok->previous(), ") [ [ expects|ensures|assert default|audit|axiom| : %name% <|<=|>|>= %num% ] ]")) {
88598858
const Token *vartok = tok->tokAt(4);
88608859
if (vartok->str() == ":")
@@ -8884,14 +8883,7 @@ void Tokenizer::simplifyCPPAttribute()
88848883
}
88858884
}
88868885
Token::eraseTokens(tok, skipCPPOrAlignAttribute(tok)->next());
8887-
// fix iterator after removing
8888-
if (tok->previous()) {
8889-
tok = tok->previous();
8890-
tok->next()->deleteThis();
8891-
} else {
8892-
tok->deleteThis();
8893-
tok = list.front();
8894-
}
8886+
tok->deleteThis();
88958887
}
88968888
}
88978889

test/testsymboldatabase.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7408,7 +7408,10 @@ class TestSymbolDatabase : public TestFixture {
74087408
"void func1() { }\n"
74097409
"[[noreturn]] void func2();\n"
74107410
"[[noreturn]] void func3() { }\n"
7411-
"template <class T> [[noreturn]] void func4() { }");
7411+
"template <class T> [[noreturn]] void func4() { }\n"
7412+
"[[noreturn]] [[gnu::format(printf, 1, 2)]] void func5(const char*, ...);\n"
7413+
"[[gnu::format(printf, 1, 2)]] [[noreturn]] void func6(const char*, ...);\n"
7414+
);
74127415
ASSERT_EQUALS("", errout.str());
74137416
ASSERT_EQUALS(true, db != nullptr); // not null
74147417

@@ -7428,6 +7431,14 @@ class TestSymbolDatabase : public TestFixture {
74287431
ASSERT_EQUALS(true, func != nullptr);
74297432
ASSERT_EQUALS(true, func->isAttributeNoreturn());
74307433

7434+
func = findFunctionByName("func5", &db->scopeList.front());
7435+
ASSERT_EQUALS(true, func != nullptr);
7436+
ASSERT_EQUALS(true, func->isAttributeNoreturn());
7437+
7438+
func = findFunctionByName("func6", &db->scopeList.front());
7439+
ASSERT_EQUALS(true, func != nullptr);
7440+
ASSERT_EQUALS(true, func->isAttributeNoreturn());
7441+
74317442
}
74327443

74337444
void nodiscardAttributeFunction() {

test/testtokenize.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5691,6 +5691,24 @@ class TestTokenizer : public TestFixture {
56915691

56925692
ASSERT_EQUALS("struct a ;",
56935693
tokenizeAndStringify("struct [[deprecated,maybe_unused]] alignas(double) [[trivial_abi]] a;"));
5694+
5695+
ASSERT_EQUALS("void func5 ( const char * , ... ) ;",
5696+
tokenizeAndStringify("[[noreturn]] void func5(const char*, ...);"));
5697+
5698+
ASSERT_EQUALS("void func5 ( const char * , ... ) ;",
5699+
tokenizeAndStringify("[[noreturn]] [[gnu::format(printf, 1, 2)]] void func5(const char*, ...);"));
5700+
5701+
ASSERT_EQUALS("void func5 ( const char * , ... ) ;",
5702+
tokenizeAndStringify("[[gnu::format(printf, 1, 2)]] [[noreturn]] void func5(const char*, ...);"));
5703+
5704+
ASSERT_EQUALS("int func1 ( ) ;",
5705+
tokenizeAndStringify("[[nodiscard]] int func1();"));
5706+
5707+
ASSERT_EQUALS("int func1 ( ) ;",
5708+
tokenizeAndStringify("[[nodiscard]] [[clang::optnone]] int func1();"));
5709+
5710+
ASSERT_EQUALS("int func1 ( ) ;",
5711+
tokenizeAndStringify("[[clang::optnone]] [[nodiscard]] int func1();"));
56945712
}
56955713

56965714
void simplifyCaseRange() {

0 commit comments

Comments
 (0)