diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a95840453..9d1ffdd61 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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()) { diff --git a/lib/tokenize.h b/lib/tokenize.h index 11526883e..2ad4eb391 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -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 { }; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index ae4c5418a..4d644f386 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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); } }";