@@ -9299,7 +9299,11 @@ void Tokenizer::simplifyCppcheckAttribute()
9299
9299
9300
9300
void Tokenizer::simplifyCPPAttribute()
9301
9301
{
9302
- if ((isCPP() && mSettings.standards.cpp < Standards::CPP11) || (isC() && mSettings.standards.c < Standards::C23))
9302
+ // According to cppreference alignas is a c21 feature however the macro is often available when compiling c11
9303
+ const bool hasAlignas = ((isCPP() && mSettings.standards.cpp >= Standards::CPP11) || (isC() && mSettings.standards.c >= Standards::C11));
9304
+ const bool hasCppAttribute = ((isCPP() && mSettings.standards.cpp >= Standards::CPP11) || (isC() && mSettings.standards.c >= Standards::C23));
9305
+
9306
+ if (!hasAlignas && !hasCppAttribute)
9303
9307
return;
9304
9308
9305
9309
for (Token *tok = list.front(); tok;) {
@@ -9308,6 +9312,10 @@ void Tokenizer::simplifyCPPAttribute()
9308
9312
continue;
9309
9313
}
9310
9314
if (isCPPAttribute(tok)) {
9315
+ if (!hasCppAttribute) {
9316
+ tok = skipCPPOrAlignAttribute(tok)->next();
9317
+ continue;
9318
+ }
9311
9319
if (Token::findsimplematch(tok->tokAt(2), "noreturn", tok->link())) {
9312
9320
Token * head = skipCPPOrAlignAttribute(tok)->next();
9313
9321
while (isCPPAttribute(head) || isAlignAttribute(head))
@@ -9359,23 +9367,29 @@ void Tokenizer::simplifyCPPAttribute()
9359
9367
}
9360
9368
}
9361
9369
} else {
9362
- if (Token::simpleMatch(tok, "alignas (")) {
9363
- Token* atok = nullptr;
9364
- if (Token::Match(tok->previous(), "%name%"))
9365
- atok = tok->previous();
9366
- else {
9367
- atok = tok;
9368
- while (isCPPAttribute(atok) || isAlignAttribute(atok))
9369
- atok = skipCPPOrAlignAttribute(atok)->next();
9370
- }
9371
- if (atok) {
9372
- std::string a;
9373
- for (const Token* t = tok->tokAt(2); t && t->str() != ")"; t = t->next())
9374
- a += " " + t->str();
9375
- if (a.size() > 1)
9376
- atok->addAttributeAlignas(a.substr(1));
9377
- }
9378
- // alignment requirements could be checked here
9370
+ // alignas(expr)
9371
+
9372
+ if (!hasAlignas) {
9373
+ tok = skipCPPOrAlignAttribute(tok)->next();
9374
+ continue;
9375
+ }
9376
+
9377
+ // alignment requirements could be checked here
9378
+
9379
+ Token* atok = nullptr;
9380
+ if (Token::Match(tok->previous(), "%name%"))
9381
+ atok = tok->previous();
9382
+ else {
9383
+ atok = tok;
9384
+ while (isCPPAttribute(atok) || isAlignAttribute(atok))
9385
+ atok = skipCPPOrAlignAttribute(atok)->next();
9386
+ }
9387
+ if (atok) {
9388
+ std::string a;
9389
+ for (const Token* t = tok->tokAt(2); t && t->str() != ")"; t = t->next())
9390
+ a += " " + t->str();
9391
+ if (a.size() > 1)
9392
+ atok->addAttributeAlignas(a.substr(1));
9379
9393
}
9380
9394
}
9381
9395
Token::eraseTokens(tok, skipCPPOrAlignAttribute(tok)->next());
0 commit comments