Skip to content

Commit 1721485

Browse files
authored
Partial fix for #14576: Missing attribute alignas (aligned_storage case) (danmar#8307)
1 parent 79ca30a commit 1721485

3 files changed

Lines changed: 77 additions & 0 deletions

File tree

lib/tokenize.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6011,6 +6011,9 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
60116011
// Link < with >
60126012
createLinks2();
60136013

6014+
// Handle std::aligned_storage<...>
6015+
simplifyAlignedStorage();
6016+
60146017
// Mark C++ casts
60156018
markCppCasts();
60166019

@@ -10361,6 +10364,8 @@ void Tokenizer::simplifyNamespaceStd()
1036110364
mSettings.library.podtype("std::" + tok->str()) ||
1036210365
isStdContainerOrIterator(tok, mSettings))
1036310366
insert = true;
10367+
else if (Token::simpleMatch(tok, "aligned_storage"))
10368+
insert = true;
1036410369

1036510370
if (insert) {
1036610371
tok->previous()->insertToken("std");
@@ -11176,6 +11181,57 @@ void Tokenizer::simplifyNamespaceAliases()
1117611181
}
1117711182
}
1117811183

11184+
void Tokenizer::simplifyAlignedStorage()
11185+
{
11186+
if (!isCPP())
11187+
return;
11188+
11189+
const Standards::cppstd_t std = mSettings.standards.cpp;
11190+
if (std < Standards::CPP11 || std >= Standards::CPP23)
11191+
return;
11192+
11193+
for (Token *tok = list.front(); tok; tok = tok->next()) {
11194+
if (!Token::simpleMatch(tok, "std :: aligned_storage <"))
11195+
continue;
11196+
11197+
tok = tok->tokAt(3);
11198+
const Token *end = tok->link();
11199+
tok = tok->next();
11200+
11201+
if (!tok)
11202+
break;
11203+
11204+
if (!end)
11205+
continue;
11206+
11207+
for (; tok != end; tok = tok->next()) {
11208+
if (Token::simpleMatch(tok, ",")) {
11209+
tok = tok->next();
11210+
break;
11211+
}
11212+
11213+
if (Token::Match(tok, "(|<"))
11214+
tok = tok->link();
11215+
}
11216+
11217+
std::string str;
11218+
for (; tok != end; tok = tok->next()) {
11219+
str += " " + tok->str();
11220+
}
11221+
11222+
if (str.empty())
11223+
continue;
11224+
11225+
if (!Token::Match(tok, "> :: type %name%"))
11226+
continue;
11227+
11228+
str = str.substr(1);
11229+
11230+
tok = tok->tokAt(3);
11231+
tok->addAttributeAlignas(str);
11232+
}
11233+
}
11234+
1117911235
void Tokenizer::setDirectives(std::list<Directive> directives)
1118011236
{
1118111237
mDirectives = std::move(directives);

lib/tokenize.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,11 @@ class CPPCHECKLIB Tokenizer {
528528
*/
529529
void simplifyNamespaceAliases();
530530

531+
/**
532+
* Handle std::aligned_storage<...>
533+
*/
534+
void simplifyAlignedStorage();
535+
531536
/**
532537
* Convert C++17 style nested namespace to older style
533538
*/

test/testtokenize.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ class TestTokenizer : public TestFixture {
292292

293293
TEST_CASE(attributeAlignasBefore);
294294
TEST_CASE(attributeAlignasAfter);
295+
TEST_CASE(simplifyAlignedStorage);
295296

296297
TEST_CASE(splitTemplateRightAngleBrackets);
297298

@@ -4380,6 +4381,21 @@ class TestTokenizer : public TestFixture {
43804381
ASSERT_EQUALS(var->getAttributeAlignas()[0], "long");
43814382
}
43824383

4384+
void simplifyAlignedStorage() {
4385+
const char code[] = "std::aligned_storage<sizeof(long), alignof(long)>::type buffer;";
4386+
const char expected[] = "std :: aligned_storage < sizeof ( long ) , alignof ( long ) > :: type buffer ;";
4387+
4388+
SimpleTokenizer tokenizer(settings2, *this);
4389+
ASSERT(tokenizer.tokenize(code));
4390+
4391+
ASSERT_EQUALS(expected, tokenizer.tokens()->stringifyList(nullptr, false));
4392+
4393+
const Token *buffer = Token::findsimplematch(tokenizer.tokens(), "buffer");
4394+
ASSERT(buffer);
4395+
ASSERT(buffer->hasAttributeAlignas());
4396+
ASSERT_EQUALS("alignof ( long )", buffer->getAttributeAlignas()[0]);
4397+
}
4398+
43834399
void splitTemplateRightAngleBrackets() {
43844400
{
43854401
const char code[] = "; z = x < 0 ? x >> y : x >> y;";

0 commit comments

Comments
 (0)