From 2e22edc25132d8ca3ce085083780788b15876714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Sat, 7 Mar 2026 15:15:09 +0100 Subject: [PATCH 1/2] Add test --- test/testtokenize.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index e59f7b3be62..8291c539b29 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -284,6 +284,8 @@ class TestTokenizer : public TestFixture { TEST_CASE(cppMaybeUnusedAfter2); TEST_CASE(cppMaybeUnusedStructuredBinding); + TEST_CASE(simplifyAlignedStorage); + TEST_CASE(splitTemplateRightAngleBrackets); TEST_CASE(cpp03template1); @@ -4332,6 +4334,20 @@ class TestTokenizer : public TestFixture { ASSERT(var2 && var2->isAttributeMaybeUnused()); } + void simplifyAlignedStorage() { + const char code[] = "std::aligned_storage::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() { { From a8b3275c71649e7b5dc6eea59df9979f63de229c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Sat, 7 Mar 2026 15:15:18 +0100 Subject: [PATCH 2/2] Fix #14576 (aligned_storage) --- lib/tokenize.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/tokenize.h | 5 +++++ 2 files changed, 53 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 193fa5bbb4d..8be9cc88e8d 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5983,6 +5983,9 @@ bool Tokenizer::simplifyTokenList1(const char FileName[]) // Link < with > createLinks2(); + // Handle std::aligned_storage<...> + simplifyAlignedStorage(); + // Mark C++ casts markCppCasts(); @@ -11133,6 +11136,51 @@ 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()) { + 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); + tok->addAttributeAlignas(str); + } +} + void Tokenizer::setDirectives(std::list directives) { mDirectives = std::move(directives); diff --git a/lib/tokenize.h b/lib/tokenize.h index 23669be7225..ff14930c1cb 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -528,6 +528,11 @@ class CPPCHECKLIB Tokenizer { */ void simplifyNamespaceAliases(); + /** + * Handle std::aligned_storage<...> + */ + void simplifyAlignedStorage(); + /** * Convert C++17 style nested namespace to older style */