Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1712,8 +1712,11 @@ void Tokenizer::simplifyTypedef()
tok2 = tok2->linkAt(1);

// skip over const/noexcept
while (Token::Match(tok2->next(), "const|noexcept"))
while (Token::Match(tok2->next(), "const|noexcept")) {
tok2 = tok2->next();
if (Token::Match(tok2->next(), "( true|false )"))
tok2 = tok2->tokAt(3);
}

tok2->insertToken(")");
tok2 = tok2->next();
Expand Down Expand Up @@ -6431,11 +6434,15 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co
tok2 = nullptr;
}

// parenthesis, functions can't be declared like:
// int f1(a,b), f2(c,d);
// so if there is a comma assume this is a variable declaration
else if (Token::Match(varName, "%name% (") && Token::simpleMatch(varName->linkAt(1), ") ,")) {
tok2 = varName->linkAt(1)->next();
// function declaration
else if (Token::Match(varName, "%name% (")) {
Token* commaTok = varName->linkAt(1)->next();
while (Token::Match(commaTok, "const|noexcept|override|final")) {
commaTok = commaTok->next();
if (Token::Match(commaTok, "( true|false )"))
commaTok = commaTok->link()->next();
}
tok2 = Token::simpleMatch(commaTok, ",") ? commaTok : nullptr;
}

else
Expand Down Expand Up @@ -8425,10 +8432,12 @@ void Tokenizer::simplifyKeyword()

// noexcept -> noexcept(true)
// 2) void f() noexcept; -> void f() noexcept(true);
else if (Token::Match(tok, ") noexcept :|{|;|const|override|final")) {
else if (Token::Match(tok, ") const|override|final| noexcept :|{|;|,|const|override|final")) {
// Insertion is done in inverse order
// The brackets are linked together accordingly afterwards
Token * tokNoExcept = tok->next();
Token* tokNoExcept = tok->next();
while (tokNoExcept->str() != "noexcept")
tokNoExcept = tokNoExcept->next();
tokNoExcept->insertToken(")");
Token * braceEnd = tokNoExcept->next();
tokNoExcept->insertToken("true");
Expand Down
10 changes: 5 additions & 5 deletions test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2622,7 +2622,7 @@ class TestSimplifyTypedef : public TestFixture {
" constexpr const foo &c_str() const noexcept { return _a; }\n"
"};";

const char exp[] = "class c { char _a [ 4 ] ; const constexpr char ( & c_str ( ) const noexcept ) [ 4 ] { return _a ; } } ;";
const char exp[] = "class c { char _a [ 4 ] ; const constexpr char ( & c_str ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;";
ASSERT_EQUALS(exp, tok(code, false));
}

Expand All @@ -2633,8 +2633,8 @@ class TestSimplifyTypedef : public TestFixture {
" constexpr operator foo &() const noexcept { return _a; }\n"
"};";

const char actual[] = "class c { char _a [ 4 ] ; constexpr operatorchar ( & ( ) const noexcept ) [ 4 ] { return _a ; } } ;";
const char exp[] = "class c { char _a [ 4 ] ; const operator char ( & ( ) const noexcept ) [ 4 ] { return _a ; } } ;";
const char actual[] = "class c { char _a [ 4 ] ; constexpr operatorchar ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;";
const char exp[] = "class c { char _a [ 4 ] ; const operator char ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;";
TODO_ASSERT_EQUALS(exp, actual, tok(code, false));
}

Expand All @@ -2645,8 +2645,8 @@ class TestSimplifyTypedef : public TestFixture {
" constexpr operator const foo &() const noexcept { return _a; }\n"
"};";

const char actual[] = "class c { char _a [ 4 ] ; constexpr operatorconstchar ( & ( ) const noexcept ) [ 4 ] { return _a ; } } ;";
const char exp[] = "class c { char _a [ 4 ] ; const operator const char ( & ( ) const noexcept ) [ 4 ] { return _a ; } } ;";
const char actual[] = "class c { char _a [ 4 ] ; constexpr operatorconstchar ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;";
const char exp[] = "class c { char _a [ 4 ] ; const operator const char ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;";
TODO_ASSERT_EQUALS(exp, actual, tok(code, false));
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4370,15 +4370,15 @@ class TestSymbolDatabase : public TestFixture {
"struct Foo : Bar {\n"
" virtual std::string get_endpoint_url() const noexcept override final;\n"
"};");
const Token *f = db ? Token::findsimplematch(tokenizer.tokens(), "get_endpoint_url ( ) const noexcept ;") : nullptr;
const Token *f = db ? Token::findsimplematch(tokenizer.tokens(), "get_endpoint_url ( ) const noexcept ( true ) ;") : nullptr;
ASSERT(f != nullptr);
ASSERT(f && f->function() && f->function()->token->linenr() == 2);
ASSERT(f && f->function() && f->function()->hasVirtualSpecifier());
ASSERT(f && f->function() && !f->function()->hasOverrideSpecifier());
ASSERT(f && f->function() && !f->function()->hasFinalSpecifier());
ASSERT(f && f->function() && f->function()->isConst());
ASSERT(f && f->function() && f->function()->isNoExcept());
f = db ? Token::findsimplematch(tokenizer.tokens(), "get_endpoint_url ( ) const noexcept override final ;") : nullptr;
f = db ? Token::findsimplematch(tokenizer.tokens(), "get_endpoint_url ( ) const noexcept ( true ) override final ;") : nullptr;
ASSERT(f != nullptr);
ASSERT(f && f->function() && f->function()->token->linenr() == 5);
ASSERT(f && f->function() && f->function()->hasVirtualSpecifier());
Expand Down Expand Up @@ -6731,7 +6731,7 @@ class TestSymbolDatabase : public TestFixture {
" return nullptr;\n"
"}");
ASSERT_EQUALS("", errout.str());
const Token *functok = Token::findsimplematch(tokenizer.tokens(), "what ( ) const noexcept {");
const Token *functok = Token::findsimplematch(tokenizer.tokens(), "what ( ) const noexcept ( true ) {");
ASSERT(functok);
ASSERT(functok->function());
ASSERT(functok->function()->name() == "what");
Expand Down
17 changes: 16 additions & 1 deletion test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ class TestTokenizer : public TestFixture {
TEST_CASE(vardecl26); // #5907 - incorrect handling of extern declarations
TEST_CASE(vardecl27); // #7850 - crash on valid C code
TEST_CASE(vardecl28);
TEST_CASE(vardecl29); // #9282
TEST_CASE(vardecl_stl_1);
TEST_CASE(vardecl_stl_2);
TEST_CASE(vardecl_stl_3);
Expand Down Expand Up @@ -2492,6 +2493,20 @@ class TestTokenizer : public TestFixture {
tokenizeAndStringify(code, /*expand=*/ true, Settings::Native, "test.c"));
}

void vardecl29() { // #9282
const char* code = "double f1() noexcept, f2(double) noexcept;";
ASSERT_EQUALS("double f1 ( ) noexcept ( true ) ; double f2 ( double ) noexcept ( true ) ;",
tokenizeAndStringify(code));

code = "class C {\n"
" double f1() const noexcept, f2 (double) const noexcept;\n"
"};\n";
ASSERT_EQUALS("class C {\n"
"double f1 ( ) const noexcept ( true ) ; double f2 ( double ) const noexcept ( true ) ;\n"
"} ;",
tokenizeAndStringify(code));
}

void volatile_variables() {
{
const char code[] = "volatile int a=0;\n"
Expand Down Expand Up @@ -4931,7 +4946,7 @@ class TestTokenizer : public TestFixture {
ASSERT_EQUALS(result3, tokenizeAndStringify(code3));

const char code4[] = "value_type * operator += (int) const noexcept ;";
const char result4[] = "value_type * operator+= ( int ) const noexcept ;";
const char result4[] = "value_type * operator+= ( int ) const noexcept ( true ) ;";
ASSERT_EQUALS(result4, tokenizeAndStringify(code4));

const char code5[] = "value_type * operator += (int) const noexcept ( true ) ;";
Expand Down
11 changes: 11 additions & 0 deletions test/testunusedprivfunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class TestUnusedPrivateFunction : public TestFixture {
TEST_CASE(test4);
TEST_CASE(test5);
TEST_CASE(test6); // ticket #2602
TEST_CASE(test7); // ticket #9282

// [ 2236547 ] False positive --style unused function, called via pointer
TEST_CASE(func_pointer1);
Expand Down Expand Up @@ -251,6 +252,16 @@ class TestUnusedPrivateFunction : public TestFixture {
ASSERT_EQUALS("", errout.str());
}

void test7() { // ticket #9282
check("class C {\n"
" double f1() const noexcept, f2(double) const noexcept;\n"
" void f3() const noexcept;\n"
"};\n"
"double C::f1() const noexcept { f3(); }\n"
"void C::f3() const noexcept {}\n");
ASSERT_EQUALS("", errout.str());
}




Expand Down