Fixed #7979 (Tokenizer: Detect and reject c++ code in .c file)

This commit is contained in:
Daniel Marjamäki 2017-04-17 22:15:29 +02:00
parent bb015c6a2b
commit 1a95dc9bd6
2 changed files with 9 additions and 0 deletions

View File

@ -8046,6 +8046,14 @@ void Tokenizer::validateC() const
if (!isC()) if (!isC())
return; return;
for (const Token *tok = tokens(); tok; tok = tok->next()) { for (const Token *tok = tokens(); tok; tok = tok->next()) {
// Template function..
if (Token::Match(tok, "%name% < %name% > (")) {
const Token *tok2 = tok->tokAt(5);
while (tok2 && !Token::Match(tok2, "[()]"))
tok2 = tok2->next();
if (Token::simpleMatch(tok2, ") {"))
syntaxErrorC(tok, tok->str() + '<' + tok->strAt(2) + ">() {}");
}
if (tok->previous() && !Token::Match(tok->previous(), "[;{}]")) if (tok->previous() && !Token::Match(tok->previous(), "[;{}]"))
continue; continue;
if (Token::simpleMatch(tok, "using namespace std ;")) if (Token::simpleMatch(tok, "using namespace std ;"))

View File

@ -790,6 +790,7 @@ private:
ASSERT_THROW(tokenizeAndStringify(";using namespace std;",false,false,Settings::Native,"test.c"), InternalError); ASSERT_THROW(tokenizeAndStringify(";using namespace std;",false,false,Settings::Native,"test.c"), InternalError);
ASSERT_THROW(tokenizeAndStringify(";std::map<int,int> m;",false,false,Settings::Native,"test.c"), InternalError); ASSERT_THROW(tokenizeAndStringify(";std::map<int,int> m;",false,false,Settings::Native,"test.c"), InternalError);
ASSERT_THROW(tokenizeAndStringify(";template<class T> class X { };",false,false,Settings::Native,"test.c"), InternalError); ASSERT_THROW(tokenizeAndStringify(";template<class T> class X { };",false,false,Settings::Native,"test.c"), InternalError);
ASSERT_THROW(tokenizeAndStringify("int X<Y>() {};",false,false,Settings::Native,"test.c"), InternalError);
} }
void syntax_case_default() { // correct syntax void syntax_case_default() { // correct syntax