MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / findGarbageCode

Method findGarbageCode

lib/tokenize.cpp:8707–9201  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8705}
8706
8707void Tokenizer::findGarbageCode() const
8708{
8709 const bool cpp = isCPP();
8710 const bool isCPP11 = cpp && mSettings.standards.cpp >= Standards::CPP11;
8711
8712 static const std::unordered_set<std::string> nonConsecutiveKeywords{ "break",
8713 "continue",
8714 "for",
8715 "goto",
8716 "if",
8717 "return",
8718 "switch",
8719 "throw",
8720 "typedef",
8721 "while" };
8722
8723 for (const Token *tok = tokens(); tok; tok = tok->next()) {
8724 // initialization: = {
8725 if (Token::simpleMatch(tok, "= {") && Token::simpleMatch(tok->linkAt(1), "} ("))
8726 syntaxError(tok->linkAt(1));
8727
8728 // Inside [] there can't be ; or various keywords
8729 else if (tok->str() == "[") {
8730 for (const Token* inner = tok->next(); inner != tok->link(); inner = inner->next()) {
8731 if (Token::Match(inner, "(|[|{"))
8732 inner = inner->link();
8733 else if (Token::Match(inner, ";|goto|return|typedef"))
8734 syntaxError(inner);
8735 }
8736 }
8737
8738 // array assignment
8739 else if (Token::Match(tok, "%assign% [") && Token::simpleMatch(tok->linkAt(1), "] ;"))
8740 syntaxError(tok, tok->str() + "[...];");
8741
8742 else if (Token::Match(tok, "[({<] %assign%"))
8743 syntaxError(tok);
8744
8745 else if (Token::Match(tok, "%assign% >"))
8746 syntaxError(tok);
8747
8748 else if (Token::Match(tok, "[`\\@]"))
8749 syntaxError(tok);
8750
8751 // UNKNOWN_MACRO(return)
8752 if (tok->isKeyword() && Token::Match(tok, "throw|return )") && Token::Match(tok->linkAt(1)->previous(), "%name% ("))
8753 unknownMacroError(tok->linkAt(1)->previous());
8754
8755 // UNKNOWN_MACRO(return)
8756 else if (!tok->isKeyword() && (Token::Match(tok, "%name% return") || (isCPP() && Token::Match(tok, "%name% throw"))))
8757 unknownMacroError(tok);
8758
8759 // Assign/increment/decrement literal
8760 else if (Token::Match(tok, "!!) %num%|%str%|%char% %assign%|++|--")) {
8761 if (!cpp || mSettings.standards.cpp < Standards::CPP20 || !Token::Match(tok->previous(), "%name% : %num% ="))
8762 syntaxError(tok, tok->strAt(1) + " " + tok->strAt(2));
8763 }
8764 else if (Token::simpleMatch(tok, ") return") && !Token::Match(tok->link()->previous(), "if|while|for (")) {

Callers

nothing calls this directly

Calls 15

isCPPFunction · 0.85
tokensFunction · 0.85
isCPPAttributeFunction · 0.85
isAlignAttributeFunction · 0.85
skipCPPOrAlignAttributeFunction · 0.85
isUnevaluatedFunction · 0.85
findUnmatchedTernaryOpFunction · 0.85
nextMethod · 0.80
linkAtMethod · 0.80
isUpperCaseNameMethod · 0.80

Tested by

no test coverage detected