Skip to content

Commit 71dcef8

Browse files
authored
Fix #12807 (dump file: provide alignas expressions) (#6485)
cherry pick b84bc8d from main branch
1 parent 466909f commit 71dcef8

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

lib/token.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ struct TokenImpl {
121121
};
122122
CppcheckAttributes* mCppcheckAttributes{};
123123

124+
// alignas expressions
125+
std::unique_ptr<std::vector<std::string>> mAttributeAlignas;
126+
void addAttributeAlignas(const std::string& a) {
127+
if (!mAttributeAlignas)
128+
mAttributeAlignas = std::unique_ptr<std::vector<std::string>>(new std::vector<std::string>());
129+
if (std::find(mAttributeAlignas->cbegin(), mAttributeAlignas->cend(), a) == mAttributeAlignas->cend())
130+
mAttributeAlignas->push_back(a);
131+
}
132+
124133
// For memoization, to speed up parsing of huge arrays #8897
125134
enum class Cpp11init { UNKNOWN, CPP11INIT, NOINIT } mCpp11init = Cpp11init::UNKNOWN;
126135

@@ -533,6 +542,15 @@ class CPPCHECKLIB Token {
533542
void isAttributeMaybeUnused(const bool value) {
534543
setFlag(fIsAttributeMaybeUnused, value);
535544
}
545+
std::vector<std::string> getAttributeAlignas() const {
546+
return mImpl->mAttributeAlignas ? *mImpl->mAttributeAlignas : std::vector<std::string>();
547+
}
548+
bool hasAttributeAlignas() const {
549+
return !!mImpl->mAttributeAlignas;
550+
}
551+
void addAttributeAlignas(const std::string& a) {
552+
mImpl->addAttributeAlignas(a);
553+
}
536554
void setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, MathLib::bigint value) {
537555
mImpl->setCppcheckAttribute(type, value);
538556
}

lib/tokenize.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6026,6 +6026,13 @@ void Tokenizer::dump(std::ostream &out) const
60266026
outs += " isAttributeMaybeUnused=\"true\"";
60276027
if (tok->isAttributeUnused())
60286028
outs += " isAttributeUnused=\"true\"";
6029+
if (tok->hasAttributeAlignas()) {
6030+
const std::vector<std::string>& a = tok->getAttributeAlignas();
6031+
outs += " alignas=\"" + ErrorLogger::toxml(a[0]) + "\"";
6032+
if (a.size() > 1)
6033+
// we could write all alignas expressions but currently we only need 2
6034+
outs += " alignas2=\"" + ErrorLogger::toxml(a[1]) + "\"";
6035+
}
60296036
if (tok->link()) {
60306037
outs += " link=\"";
60316038
outs += id_string(tok->link());
@@ -9277,6 +9284,21 @@ void Tokenizer::simplifyCPPAttribute()
92779284
}
92789285
} else {
92799286
if (Token::simpleMatch(tok, "alignas (")) {
9287+
Token* atok = nullptr;
9288+
if (Token::Match(tok->previous(), "%name%"))
9289+
atok = tok->previous();
9290+
else {
9291+
atok = tok;
9292+
while (isCPPAttribute(atok) || isAlignAttribute(atok))
9293+
atok = skipCPPOrAlignAttribute(atok)->next();
9294+
}
9295+
if (atok) {
9296+
std::string a;
9297+
for (const Token* t = tok->tokAt(2); t && t->str() != ")"; t = t->next())
9298+
a += " " + t->str();
9299+
if (a.size() > 1)
9300+
atok->addAttributeAlignas(a.substr(1));
9301+
}
92809302
// alignment requirements could be checked here
92819303
}
92829304
}

test/testtokenize.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ class TestTokenizer : public TestFixture {
438438

439439
TEST_CASE(removeAlignas1);
440440
TEST_CASE(removeAlignas2); // Do not remove alignof in the same way
441+
TEST_CASE(dumpAlignas);
441442

442443
TEST_CASE(simplifyCoroutines);
443444

@@ -7764,6 +7765,17 @@ class TestTokenizer : public TestFixture {
77647765
ASSERT_EQUALS(expected, tokenizeAndStringify(code));
77657766
}
77667767

7768+
void dumpAlignas() {
7769+
Settings settings;
7770+
SimpleTokenizer tokenizer(settings, *this);
7771+
tokenizer.tokenize("int alignas(8) alignas(16) x;", false);
7772+
ASSERT(Token::simpleMatch(tokenizer.tokens(), "int x ;"));
7773+
std::ostringstream ostr;
7774+
tokenizer.dump(ostr);
7775+
const std::string dump = ostr.str();
7776+
ASSERT(dump.find(" alignas=\"8\" alignas2=\"16\"") != std::string::npos);
7777+
}
7778+
77677779
void simplifyCoroutines() {
77687780
const Settings settings = settingsBuilder().cpp(Standards::CPP20).build();
77697781

0 commit comments

Comments
 (0)