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
3 changes: 3 additions & 0 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4761,6 +4761,9 @@ bool Scope::isVariableDeclaration(const Token* const tok, const Token*& vartok,
const Token* localTypeTok = skipScopeIdentifiers(tok);
const Token* localVarTok = nullptr;

while (Token::simpleMatch(localTypeTok, "alignas (") && Token::Match(localTypeTok->linkAt(1), ") %name%"))
localTypeTok = localTypeTok->linkAt(1)->next();

if (Token::Match(localTypeTok, "%type% <")) {
if (Token::Match(tok, "const_cast|dynamic_cast|reinterpret_cast|static_cast <"))
return false;
Expand Down
19 changes: 5 additions & 14 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4094,6 +4094,10 @@ static bool setVarIdParseDeclaration(Token** tok, const VariableMap& variableMap
bool ref = false;
while (tok2) {
if (tok2->isName()) {
if (Token::simpleMatch(tok2, "alignas (")) {
tok2 = tok2->linkAt(1)->next();
continue;
}
if (cpp && Token::Match(tok2, "namespace|public|private|protected"))
return false;
if (cpp && Token::simpleMatch(tok2, "decltype (")) {
Expand Down Expand Up @@ -5364,8 +5368,6 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
// Remove extra "template" tokens that are not used by cppcheck
removeExtraTemplateKeywords();

removeAlignas();

simplifySpaceshipOperator();

// Bail out if code is garbage
Expand Down Expand Up @@ -5402,7 +5404,7 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
// simplify namespace aliases
simplifyNamespaceAliases();

// Remove [[attribute]] and alignas(?)
// Remove [[attribute]]
simplifyCPPAttribute();

// remove __attribute__((?))
Expand Down Expand Up @@ -8961,17 +8963,6 @@ void Tokenizer::simplifyCPPAttribute()
}
}

void Tokenizer::removeAlignas()
{
if (!isCPP() || mSettings->standards.cpp < Standards::CPP11)
return;

for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "[;{}] alignas (") && Token::Match(tok->linkAt(2), ") %name%"))
Token::eraseTokens(tok, tok->linkAt(2)->next());
}
}

void Tokenizer::simplifySpaceshipOperator()
{
if (isCPP() && mSettings->standards.cpp >= Standards::CPP20) {
Expand Down
3 changes: 0 additions & 3 deletions lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,6 @@ class CPPCHECKLIB Tokenizer {
*/
void simplifyCppcheckAttribute();

/** Remove alignas */
void removeAlignas();

/** Simplify c++20 spaceship operator */
void simplifySpaceshipOperator();

Expand Down
10 changes: 10 additions & 0 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class TestSymbolDatabase : public TestFixture {
TEST_CASE(variableVolatile);
TEST_CASE(variableConstexpr);
TEST_CASE(isVariableDecltype);
TEST_CASE(isVariableAlignas);

TEST_CASE(VariableValueType1);
TEST_CASE(VariableValueType2);
Expand Down Expand Up @@ -1475,6 +1476,15 @@ class TestSymbolDatabase : public TestFixture {
ASSERT_EQUALS("signed int *", c->valueType()->str());
}

void isVariableAlignas() {
GET_SYMBOL_DB_C("extern alignas(16) int x;\n"
"alignas(16) int x;\n");
ASSERT(db);
ASSERT_EQUALS(2, db->scopeList.front().varlist.size());
const Variable *x1 = Token::findsimplematch(tokenizer.tokens(), "x")->variable();
ASSERT(x1 && Token::simpleMatch(x1->typeStartToken(), "alignas ( 16 ) int x ;"));
}

void memberVar1() {
GET_SYMBOL_DB("struct Foo {\n"
" int x;\n"
Expand Down
9 changes: 9 additions & 0 deletions test/testvarid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ class TestVarID : public TestFixture {
TEST_CASE(varid_declInIfCondition);
TEST_CASE(varid_globalScope);
TEST_CASE(varid_function_pointer_args);
TEST_CASE(varid_alignas);

TEST_CASE(varidclass1);
TEST_CASE(varidclass2);
Expand Down Expand Up @@ -3045,6 +3046,14 @@ class TestVarID : public TestFixture {
ASSERT_EQUALS("1: void f ( void ( * g@1 ) ( int , IN int ) ) { }\n", tokenize(code3));
}

void varid_alignas() {
const char code[] = "extern alignas(16) int x;\n"
"alignas(16) int x;";
const char expected[] = "1: extern alignas ( 16 ) int x@1 ;\n"
"2: alignas ( 16 ) int x@2 ;\n";
ASSERT_EQUALS(expected, tokenize(code, "test.c"));
}

void varidclass1() {
const std::string actual = tokenize(
"class Fred\n"
Expand Down