Tokenizer: refactoring garbage code handling
This commit is contained in:
parent
6fd8edd6e5
commit
383b815cca
|
@ -3317,6 +3317,10 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
|
||||||
|
|
||||||
createLinks();
|
createLinks();
|
||||||
|
|
||||||
|
// Bail out if code is garbage
|
||||||
|
if (const Token *garbage = findGarbageCode())
|
||||||
|
syntaxError(garbage);
|
||||||
|
|
||||||
// if (x) MACRO() ..
|
// if (x) MACRO() ..
|
||||||
for (const Token *tok = list.front(); tok; tok = tok->next()) {
|
for (const Token *tok = list.front(); tok; tok = tok->next()) {
|
||||||
if (Token::simpleMatch(tok, "if (")) {
|
if (Token::simpleMatch(tok, "if (")) {
|
||||||
|
@ -3372,20 +3376,6 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
|
||||||
Token::simpleMatch(tok->linkAt(2), "} ;")) {
|
Token::simpleMatch(tok->linkAt(2), "} ;")) {
|
||||||
syntaxError(tok);
|
syntaxError(tok);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tok->str() == "switch") { // switch (EXPR) { ... }
|
|
||||||
if (!Token::Match(tok->next(), "( !!)"))
|
|
||||||
syntaxError(tok->next());
|
|
||||||
if (!Token::simpleMatch(tok->linkAt(1),") {"))
|
|
||||||
syntaxError(tok->linkAt(1));
|
|
||||||
validateExpr(tok->next(), tok->linkAt(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Token::Match(tok, "if|while")) {
|
|
||||||
if (!Token::Match(tok->next(), "( !!)"))
|
|
||||||
syntaxError(tok->next());
|
|
||||||
validateExpr(tok->next(), tok->linkAt(1));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!simplifyAddBraces())
|
if (!simplifyAddBraces())
|
||||||
|
@ -8092,7 +8082,27 @@ void Tokenizer::validate() const
|
||||||
cppcheckError(lastTok);
|
cppcheckError(lastTok);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tokenizer::validateExpr(const Token *start, const Token *end)
|
|
||||||
|
const Token * Tokenizer::findGarbageCode() const
|
||||||
|
{
|
||||||
|
for (const Token *tok = tokens(); tok; tok = tok->next()) {
|
||||||
|
if (Token::Match(tok, "if|while|for|switch")) { // switch (EXPR) { ... }
|
||||||
|
if (!Token::Match(tok->previous(), ";|{|}|(|)|,|else"))
|
||||||
|
return tok;
|
||||||
|
if (!Token::Match(tok->next(), "( !!)"))
|
||||||
|
return tok;
|
||||||
|
if (tok->str() == "switch" && !Token::simpleMatch(tok->linkAt(1),") {"))
|
||||||
|
return tok->linkAt(1);
|
||||||
|
if (tok->str() != "for") {
|
||||||
|
if (isGarbageExpr(tok->next(), tok->linkAt(1)))
|
||||||
|
return tok;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Tokenizer::isGarbageExpr(const Token *start, const Token *end) const
|
||||||
{
|
{
|
||||||
std::set<std::string> controlFlowKeywords;
|
std::set<std::string> controlFlowKeywords;
|
||||||
controlFlowKeywords.insert("goto");
|
controlFlowKeywords.insert("goto");
|
||||||
|
@ -8107,12 +8117,13 @@ void Tokenizer::validateExpr(const Token *start, const Token *end)
|
||||||
controlFlowKeywords.insert("return");
|
controlFlowKeywords.insert("return");
|
||||||
for (const Token *tok = start; tok != end; tok = tok->next()) {
|
for (const Token *tok = start; tok != end; tok = tok->next()) {
|
||||||
if (controlFlowKeywords.find(tok->str()) != controlFlowKeywords.end())
|
if (controlFlowKeywords.find(tok->str()) != controlFlowKeywords.end())
|
||||||
syntaxError(tok);
|
return true;
|
||||||
if (tok->str() == ";")
|
if (tok->str() == ";")
|
||||||
syntaxError(tok);
|
return true;
|
||||||
if (tok->str() == "{")
|
if (tok->str() == "{")
|
||||||
tok = tok->link();
|
tok = tok->link();
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Tokenizer::simplifyString(const std::string &source)
|
std::string Tokenizer::simplifyString(const std::string &source)
|
||||||
|
|
|
@ -580,8 +580,11 @@ private:
|
||||||
*/
|
*/
|
||||||
void validate() const;
|
void validate() const;
|
||||||
|
|
||||||
/** Validate that expression is valid. If it's invalid a syntax error is reported. */
|
/** Detect garbage code */
|
||||||
void validateExpr(const Token *start, const Token *end);
|
const Token * findGarbageCode() const;
|
||||||
|
|
||||||
|
/** Detect garbage expression */
|
||||||
|
bool isGarbageExpr(const Token *start, const Token *end) const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove __declspec()
|
* Remove __declspec()
|
||||||
|
|
Loading…
Reference in New Issue