Fixed #9452 (FP syntaxError - _Pragma before struct with two constructors)

This commit is contained in:
Daniel Marjamäki 2020-11-15 14:57:18 +01:00
parent 6236beeb3f
commit 54a93c4374
3 changed files with 40 additions and 2 deletions

View File

@ -4352,6 +4352,8 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
createLinks();
removePragma();
reportUnknownMacros();
simplifyHeaders();
@ -5214,6 +5216,22 @@ void Tokenizer::removeMacrosInGlobalScope()
//---------------------------------------------------------------------------
void Tokenizer::removePragma()
{
if (isC() && mSettings->standards.c == Standards::C89)
return;
if (isCPP() && mSettings->standards.cpp == Standards::CPP03)
return;
for (Token *tok = list.front(); tok; tok = tok->next()) {
while (Token::simpleMatch(tok, "_Pragma (")) {
Token::eraseTokens(tok, tok->linkAt(1)->next());
tok->deleteThis();
}
}
}
//---------------------------------------------------------------------------
void Tokenizer::removeMacroInClassDef()
{
for (Token *tok = list.front(); tok; tok = tok->next()) {

View File

@ -252,6 +252,9 @@ public:
void addSemicolonAfterUnknownMacro();
// Remove C99 and CPP11 _Pragma(str)
void removePragma();
/** Remove undefined macro in class definition:
* class DLLEXPORT Fred { };
* class Fred FINAL : Base { };

View File

@ -84,6 +84,8 @@ private:
TEST_CASE(syntax_case_default);
TEST_CASE(removePragma);
TEST_CASE(foreach); // #3690
TEST_CASE(ifconstexpr);
@ -569,13 +571,13 @@ private:
return "";
}
std::string tokenizeAndStringify(const char code[], const Settings &settings) {
std::string tokenizeAndStringify(const char code[], const Settings &settings, const char filename[] = "test.cpp") {
errout.str("");
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.tokenize(istr, filename);
if (!tokenizer.tokens())
return "";
return tokenizer.tokens()->stringifyList(false, true, false, true, false, nullptr, nullptr);
@ -973,6 +975,21 @@ private:
ASSERT_EQUALS("", errout.str());
}
void removePragma() {
const char code[] = "_Pragma(\"abc\") int x;";
Settings s;
s.standards.c = Standards::C89;
ASSERT_EQUALS("_Pragma ( \"abc\" ) int x ;", tokenizeAndStringify(code, s, "test.c"));
s.standards.c = Standards::CLatest;
ASSERT_EQUALS("int x ;", tokenizeAndStringify(code, s, "test.c"));
s.standards.cpp = Standards::CPP03;
ASSERT_EQUALS("_Pragma ( \"abc\" ) int x ;", tokenizeAndStringify(code, s, "test.cpp"));
s.standards.cpp = Standards::CPPLatest;
ASSERT_EQUALS("int x ;", tokenizeAndStringify(code, s, "test.cpp"));
}
void foreach () {
// #3690,#5154
const char code[] ="void f() { for each ( char c in MyString ) { Console::Write(c); } }";