Fixed #9415 (C++11: `alignas` not handled -> wrong code -> false negatives)

This commit is contained in:
Daniel Marjamäki 2021-04-15 20:26:53 +02:00
parent f62d9d5853
commit 11f828a669
3 changed files with 26 additions and 0 deletions

View File

@ -4781,6 +4781,8 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
// Remove extra "template" tokens that are not used by cppcheck // Remove extra "template" tokens that are not used by cppcheck
removeExtraTemplateKeywords(); removeExtraTemplateKeywords();
removeAlignas();
// Bail out if code is garbage // Bail out if code is garbage
if (mTimerResults) { if (mTimerResults) {
Timer t("Tokenizer::tokenize::findGarbageCode", mSettings->showtime, mTimerResults); Timer t("Tokenizer::tokenize::findGarbageCode", mSettings->showtime, mTimerResults);
@ -10905,6 +10907,19 @@ void Tokenizer::simplifyCPPAttribute()
} }
} }
void Tokenizer::removeAlignas()
{
if (!isCPP() || mSettings->standards.cpp < Standards::CPP11)
return;
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "alignas|alignof (")) {
Token::eraseTokens(tok, tok->linkAt(1)->next());
tok->deleteThis();
}
}
}
static const std::unordered_set<std::string> keywords = { static const std::unordered_set<std::string> keywords = {
"inline" "inline"
, "_inline" , "_inline"

View File

@ -692,6 +692,9 @@ private:
*/ */
void simplifyCppcheckAttribute(); void simplifyCppcheckAttribute();
/** Remove alignas */
void removeAlignas();
/** /**
* Remove keywords "volatile", "inline", "register", and "restrict" * Remove keywords "volatile", "inline", "register", and "restrict"
*/ */

View File

@ -414,6 +414,8 @@ private:
TEST_CASE(checkHeader1); TEST_CASE(checkHeader1);
TEST_CASE(removeExtraTemplateKeywords); TEST_CASE(removeExtraTemplateKeywords);
TEST_CASE(removeAlignas);
} }
std::string tokenizeAndStringify(const char code[], bool expand = true, Settings::PlatformType platform = Settings::Native, const char* filename = "test.cpp", bool cpp11 = true) { std::string tokenizeAndStringify(const char code[], bool expand = true, Settings::PlatformType platform = Settings::Native, const char* filename = "test.cpp", bool cpp11 = true) {
@ -6497,6 +6499,12 @@ private:
const char expected2[] = "GridView :: Codim < 0 > :: Iterator it ; it = gv . begin < 0 > ( ) ;"; const char expected2[] = "GridView :: Codim < 0 > :: Iterator it ; it = gv . begin < 0 > ( ) ;";
ASSERT_EQUALS(expected2, tokenizeAndStringify(code2)); ASSERT_EQUALS(expected2, tokenizeAndStringify(code2));
} }
void removeAlignas() {
const char code[] = "alignas(float) unsigned char c[sizeof(float)];";
const char expected[] = "unsigned char c [ sizeof ( float ) ] ;";
ASSERT_EQUALS(expected, tokenizeAndStringify(code));
}
}; };
REGISTER_TEST(TestTokenizer) REGISTER_TEST(TestTokenizer)