Skip to content
Open
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
48 changes: 48 additions & 0 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5983,6 +5983,9 @@
// Link < with >
createLinks2();

// Handle std::aligned_storage<...>
simplifyAlignedStorage();

// Mark C++ casts
markCppCasts();

Expand Down Expand Up @@ -11133,6 +11136,51 @@
}
}

void Tokenizer::simplifyAlignedStorage()
{
if (!isCPP())
return;

const Standards::cppstd_t std = mSettings.standards.cpp;
if (std < Standards::CPP11 || std >= Standards::CPP23)
return;

for (Token *tok = list.front(); tok; tok = tok->next()) {

Check failure on line 11148 in lib/tokenize.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Called C++ object pointer is null

See more on https://sonarcloud.io/project/issues?id=danmar_cppcheck&issues=AZzIsO0ATBjk2HgQdhUO&open=AZzIsO0ATBjk2HgQdhUO&pullRequest=8307
if (!Token::simpleMatch(tok, "aligned_storage <"))
continue;

tok = tok->next();
const Token *end = tok->link();
tok = tok->next();

if (!end)
continue;

for (; tok && tok != end; tok = tok->next()) {
if (Token::simpleMatch(tok, ",")) {
tok = tok->next();
break;
}

if (Token::Match(tok, "(|<"))
tok = tok->link();
}

std::string str = "";
for (; tok && tok != end; tok = tok->next()) {
str += " " + tok->str();
}

str = str.substr(1);

if (!Token::Match(tok, "> :: type %name%"))
continue;

tok = tok->tokAt(3);

Check failure on line 11179 in lib/tokenize.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Called C++ object pointer is null

See more on https://sonarcloud.io/project/issues?id=danmar_cppcheck&issues=AZzIsO0ATBjk2HgQdhUP&open=AZzIsO0ATBjk2HgQdhUP&pullRequest=8307
tok->addAttributeAlignas(str);
}
}

void Tokenizer::setDirectives(std::list<Directive> directives)
{
mDirectives = std::move(directives);
Expand Down
5 changes: 5 additions & 0 deletions lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,11 @@ class CPPCHECKLIB Tokenizer {
*/
void simplifyNamespaceAliases();

/**
* Handle std::aligned_storage<...>
*/
void simplifyAlignedStorage();

/**
* Convert C++17 style nested namespace to older style
*/
Expand Down
16 changes: 16 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ class TestTokenizer : public TestFixture {
TEST_CASE(cppMaybeUnusedAfter2);
TEST_CASE(cppMaybeUnusedStructuredBinding);

TEST_CASE(simplifyAlignedStorage);

TEST_CASE(splitTemplateRightAngleBrackets);

TEST_CASE(cpp03template1);
Expand Down Expand Up @@ -4332,6 +4334,20 @@ class TestTokenizer : public TestFixture {
ASSERT(var2 && var2->isAttributeMaybeUnused());
}

void simplifyAlignedStorage() {
const char code[] = "std::aligned_storage<sizeof(long), alignof(long)>::type buffer;";
const char expected[] = "std :: aligned_storage < sizeof ( long ) , alignof ( long ) > :: type buffer ;";

SimpleTokenizer tokenizer(settings2, *this);
ASSERT(tokenizer.tokenize(code));

ASSERT_EQUALS(expected, tokenizer.tokens()->stringifyList(nullptr, false));

const Token *buffer = Token::findsimplematch(tokenizer.tokens(), "buffer");
ASSERT(buffer);
ASSERT(buffer->hasAttributeAlignas());
ASSERT_EQUALS("alignof ( long )", buffer->getAttributeAlignas()[0]);
}

void splitTemplateRightAngleBrackets() {
{
Expand Down
Loading