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
51 changes: 51 additions & 0 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5983,6 +5983,9 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
// Link < with >
createLinks2();

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

// Mark C++ casts
markCppCasts();

Expand Down Expand Up @@ -11133,6 +11136,54 @@ void Tokenizer::simplifyNamespaceAliases()
}
}

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()) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if tok becomes null in the loop body then yes there will be null pointer dereference when tok->next() is executed here. I am not sure if tok can become null but we do have some code like tok && .. that suggest that tok can become null..

if (!Token::simpleMatch(tok, "aligned_storage <"))
continue;

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is redundant as far as I see and in theory tok will become null if "<" is the last token and then there will be a crash.


if (!tok)
break;

if (!end)
continue;

for (; 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 != end; tok = tok->next()) {
str += " " + tok->str();
}

str = str.substr(1);

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

tok = tok->tokAt(3);
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