Skip to content

Commit 36b8eb5

Browse files
authored
Partial fix for #14576: Missing attribute alignas (cppcheck-opensource#8305)
1 parent 504b333 commit 36b8eb5

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

lib/tokenize.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9761,10 +9761,19 @@ void Tokenizer::simplifyCPPAttribute()
97619761
Token* atok = nullptr;
97629762
if (Token::Match(tok->previous(), "%name%"))
97639763
atok = tok->previous();
9764-
else {
9764+
else if (Token::simpleMatch(tok->previous(), "]")) {
9765+
atok = tok;
9766+
while (atok && Token::simpleMatch(atok->previous(), "]")) {
9767+
atok = atok->linkAt(-1);
9768+
atok = atok ? atok->previous() : nullptr;
9769+
}
9770+
if (!Token::Match(atok, "%name%"))
9771+
atok = nullptr;
9772+
} else {
97659773
atok = tok;
97669774
while (isCPPAttribute(atok) || isAlignAttribute(atok))
97679775
atok = skipCPPOrAlignAttribute(atok)->next();
9776+
atok = atok ? getVariableTokenAfterAttributes(atok) : atok;
97689777
}
97699778
if (atok) {
97709779
std::string a;

test/testtokenize.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ class TestTokenizer : public TestFixture {
284284
TEST_CASE(cppMaybeUnusedAfter2);
285285
TEST_CASE(cppMaybeUnusedStructuredBinding);
286286

287+
TEST_CASE(attributeAlignasBefore);
288+
TEST_CASE(attributeAlignasAfter);
289+
287290
TEST_CASE(splitTemplateRightAngleBrackets);
288291

289292
TEST_CASE(cpp03template1);
@@ -4332,6 +4335,35 @@ class TestTokenizer : public TestFixture {
43324335
ASSERT(var2 && var2->isAttributeMaybeUnused());
43334336
}
43344337

4338+
void attributeAlignasBefore() {
4339+
const char code[] = "alignas(long) unsigned char buffer[sizeof(long)];";
4340+
const char expected[] = "char buffer [ sizeof ( long ) ] ;";
4341+
SimpleTokenizer tokenizer(settings0, *this);
4342+
ASSERT(tokenizer.tokenize(code));
4343+
4344+
ASSERT_EQUALS(expected, tokenizer.tokens()->stringifyList(nullptr, false));
4345+
4346+
const Token *var = Token::findsimplematch(tokenizer.tokens(), "buffer");
4347+
ASSERT(var);
4348+
ASSERT(var->hasAttributeAlignas());
4349+
ASSERT(var->getAttributeAlignas().size() == 1);
4350+
ASSERT_EQUALS(var->getAttributeAlignas()[0], "long");
4351+
}
4352+
4353+
void attributeAlignasAfter() {
4354+
const char code[] = "unsigned char buffer[sizeof(long)] alignas(long);";
4355+
const char expected[] = "char buffer [ sizeof ( long ) ] ;";
4356+
SimpleTokenizer tokenizer(settings0, *this);
4357+
ASSERT(tokenizer.tokenize(code));
4358+
4359+
ASSERT_EQUALS(expected, tokenizer.tokens()->stringifyList(nullptr, false));
4360+
4361+
const Token *var = Token::findsimplematch(tokenizer.tokens(), "buffer");
4362+
ASSERT(var);
4363+
ASSERT(var->hasAttributeAlignas());
4364+
ASSERT(var->getAttributeAlignas().size() == 1);
4365+
ASSERT_EQUALS(var->getAttributeAlignas()[0], "long");
4366+
}
43354367

43364368
void splitTemplateRightAngleBrackets() {
43374369
{

0 commit comments

Comments
 (0)