Refactoring: Add Tokenizer::findGarbageCode to time report. The method now calls syntaxError instead of returning an invalid token

This commit is contained in:
Alexander Mai 2018-02-05 21:47:33 +01:00
parent 7a13e88170
commit 68eb6c4e6f
3 changed files with 30 additions and 28 deletions

View File

@ -128,7 +128,7 @@ public:
/** function arguments that are unconditionally dereferenced */ /** function arguments that are unconditionally dereferenced */
std::list<FunctionArg> dereferenced; std::list<FunctionArg> dereferenced;
/** function calls other function.. */ /** function calls other function */
std::list<FunctionArg> nestedCall; std::list<FunctionArg> nestedCall;
/** Convert MyFileInfo data into xml string */ /** Convert MyFileInfo data into xml string */

View File

@ -3559,8 +3559,12 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
simplifyAsm(); simplifyAsm();
// Bail out if code is garbage // Bail out if code is garbage
if (const Token *garbage = findGarbageCode()) if (m_timerResults) {
syntaxError(garbage); Timer t("Tokenizer::tokenize::findGarbageCode", _settings->showtime, m_timerResults);
findGarbageCode();
} else {
findGarbageCode();
}
checkConfiguration(); checkConfiguration();
@ -8327,19 +8331,19 @@ static const Token *findUnmatchedTernaryOp(const Token * const begin, const Toke
return ternaryOp.empty() ? nullptr : ternaryOp.top(); return ternaryOp.empty() ? nullptr : ternaryOp.top();
} }
const Token * Tokenizer::findGarbageCode() const void Tokenizer::findGarbageCode() const
{ {
for (const Token *tok = tokens(); tok; tok = tok->next()) { for (const Token *tok = tokens(); tok; tok = tok->next()) {
if (Token::Match(tok, "if|while|for|switch")) { // if|while|for|switch (EXPR) { ... } if (Token::Match(tok, "if|while|for|switch")) { // if|while|for|switch (EXPR) { ... }
if (tok->previous() && !Token::Match(tok->previous(), "%name%|:|;|{|}|(|)|,")) if (tok->previous() && !Token::Match(tok->previous(), "%name%|:|;|{|}|(|)|,"))
return tok; syntaxError(tok);
if (Token::Match(tok->previous(), "[(,]")) if (Token::Match(tok->previous(), "[(,]"))
continue; continue;
if (!Token::Match(tok->next(), "( !!)")) if (!Token::Match(tok->next(), "( !!)"))
return tok; syntaxError(tok);
if (tok->str() != "for") { if (tok->str() != "for") {
if (isGarbageExpr(tok->next(), tok->linkAt(1))) if (isGarbageExpr(tok->next(), tok->linkAt(1)))
return tok; syntaxError(tok);
} }
} }
} }
@ -8355,14 +8359,14 @@ const Token * Tokenizer::findGarbageCode() const
while (tok && !Token::Match(tok, "[;{}]")) while (tok && !Token::Match(tok, "[;{}]"))
tok = tok->next(); tok = tok->next();
if (!tok) if (!tok)
return switchToken; syntaxError(switchToken);
if (tok->str() != ";") if (tok->str() != ";")
return tok; syntaxError(tok);
} }
} else if (tok->str() == "(") { } else if (tok->str() == "(") {
tok = tok->link(); tok = tok->link();
} else if (tok->str() == "case") { } else if (tok->str() == "case") {
return tok; syntaxError(tok);
} }
} }
@ -8383,37 +8387,37 @@ const Token * Tokenizer::findGarbageCode() const
} }
// if we have an invalid number of semicolons inside for( ), assume syntax error // if we have an invalid number of semicolons inside for( ), assume syntax error
if ((semicolons == 1) || (semicolons > 2)) { if ((semicolons == 1) || (semicolons > 2)) {
return tok; syntaxError(tok);
} }
} }
// Operators without operands.. // Operators without operands..
for (const Token *tok = tokens(); tok; tok = tok->next()) { for (const Token *tok = tokens(); tok; tok = tok->next()) {
if (Token::Match(tok, "%cop%|=|,|[ %or%|%oror%|/|%")) if (Token::Match(tok, "%cop%|=|,|[ %or%|%oror%|/|%"))
return tok; syntaxError(tok);
if (Token::Match(tok, ";|(|[ %comp%")) if (Token::Match(tok, ";|(|[ %comp%"))
return tok; syntaxError(tok);
if (Token::Match(tok, "%cop%|= ]") && !(isCPP() && Token::Match(tok->previous(), "[|, &|= ]"))) if (Token::Match(tok, "%cop%|= ]") && !(isCPP() && Token::Match(tok->previous(), "[|, &|= ]")))
return tok; syntaxError(tok);
} }
// ternary operator without : // ternary operator without :
if (const Token *ternaryOp = findUnmatchedTernaryOp(tokens(), nullptr)) if (const Token *ternaryOp = findUnmatchedTernaryOp(tokens(), nullptr))
return ternaryOp; syntaxError(ternaryOp);
// Code must not start with an arithmetical operand // Code must not start with an arithmetical operand
if (Token::Match(list.front(), "%cop%")) if (Token::Match(list.front(), "%cop%"))
return list.front(); syntaxError(list.front());
// Code must end with } ; ) NAME // Code must end with } ; ) NAME
if (!Token::Match(list.back(), "%name%|;|}|)")) if (!Token::Match(list.back(), "%name%|;|}|)"))
return list.back(); syntaxError(list.back());
if (list.back()->str() == ")" && !Token::Match(list.back()->link()->previous(), "%name% (")) if (list.back()->str() == ")" && !Token::Match(list.back()->link()->previous(), "%name% ("))
return list.back(); syntaxError(list.back());
if (Token::Match(list.back(), "void|char|short|int|long|float|double|const|volatile|static|inline|struct|class|enum|union|template|sizeof|case|break|continue|typedef")) if (Token::Match(list.back(), "void|char|short|int|long|float|double|const|volatile|static|inline|struct|class|enum|union|template|sizeof|case|break|continue|typedef"))
return list.back(); syntaxError(list.back());
if ((list.back()->str()==")" || list.back()->str()=="}") && list.back()->previous() && list.back()->previous()->isControlFlowKeyword()) if ((list.back()->str()==")" || list.back()->str()=="}") && list.back()->previous() && list.back()->previous()->isControlFlowKeyword())
return list.back()->previous(); syntaxError(list.back()->previous());
// Garbage templates.. // Garbage templates..
if (isCPP()) { if (isCPP()) {
@ -8421,23 +8425,21 @@ const Token * Tokenizer::findGarbageCode() const
if (!Token::simpleMatch(tok, "template <")) if (!Token::simpleMatch(tok, "template <"))
continue; continue;
if (tok->previous() && !Token::Match(tok->previous(), "[:;{})>]")) if (tok->previous() && !Token::Match(tok->previous(), "[:;{})>]"))
return tok; syntaxError(tok);
const Token * const tok1 = tok; const Token * const tok1 = tok;
tok = tok->next()->findClosingBracket(); tok = tok->next()->findClosingBracket();
if (!tok) if (!tok)
return tok1; syntaxError(tok1);
if (!Token::Match(tok, ">|>> ::| %name%")) if (!Token::Match(tok, ">|>> ::| %name%"))
return tok->next() ? tok->next() : tok1; syntaxError(tok->next() ? tok->next() : tok1);
} }
} }
// Objective C/C++ // Objective C/C++
for (const Token *tok = tokens(); tok; tok = tok->next()) { for (const Token *tok = tokens(); tok; tok = tok->next()) {
if (Token::Match(tok, "[;{}] [ %name% %name% ] ;")) if (Token::Match(tok, "[;{}] [ %name% %name% ] ;"))
return tok->next(); syntaxError(tok->next());
} }
return nullptr;
} }

View File

@ -588,8 +588,8 @@ private:
*/ */
void validate() const; void validate() const;
/** Detect garbage code */ /** Detect garbage code and call syntaxError() if found. */
const Token * findGarbageCode() const; void findGarbageCode() const;
/** Detect garbage expression */ /** Detect garbage expression */
static bool isGarbageExpr(const Token *start, const Token *end); static bool isGarbageExpr(const Token *start, const Token *end);