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

Method reportUnknownMacros

lib/tokenize.cpp:8566–8705  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8564}
8565
8566void Tokenizer::reportUnknownMacros() const
8567{
8568 // Report unknown macros used in expressions "%name% %num%"
8569 for (const Token *tok = tokens(); tok; tok = tok->next()) {
8570 if (Token::Match(tok, "%name% %num%")) {
8571 // A keyword is not an unknown macro
8572 if (tok->isKeyword())
8573 continue;
8574
8575 if (Token::Match(tok->previous(), "%op%|("))
8576 unknownMacroError(tok);
8577 }
8578 }
8579
8580 // Report unknown macros before } "{ .. if (x) MACRO }"
8581 for (const Token *tok = tokens(); tok; tok = tok->next()) {
8582 if (Token::Match(tok, ")|; %name% } !!)")) {
8583 if (tok->link() && !Token::simpleMatch(tok->link()->tokAt(-1), "if"))
8584 continue;
8585 const Token* prev = tok->linkAt(2);
8586 while (Token::simpleMatch(prev, "{"))
8587 prev = prev->previous();
8588 if (Token::Match(prev, ";|)"))
8589 unknownMacroError(tok->next());
8590 }
8591 }
8592
8593 // Report unknown macros that contain several statements "MACRO(a;b;c)"
8594 for (const Token *tok = tokens(); tok; tok = tok->next()) {
8595 if (!Token::Match(tok, "%name% ("))
8596 continue;
8597 if (!tok->isUpperCaseName())
8598 continue;
8599 const Token *endTok = tok->linkAt(1);
8600 for (const Token *inner = tok->tokAt(2); inner != endTok; inner = inner->next()) {
8601 if (Token::Match(inner, "[[({]"))
8602 inner = inner->link();
8603 else if (inner->str() == ";")
8604 unknownMacroError(tok);
8605 }
8606 }
8607
8608 // Report unknown macros that contain struct initialization "MACRO(a, .b=3)"
8609 for (const Token *tok = tokens(); tok; tok = tok->next()) {
8610 if (!Token::Match(tok, "%name% ("))
8611 continue;
8612 const Token *endTok = tok->linkAt(1);
8613 for (const Token *inner = tok->tokAt(2); inner != endTok; inner = inner->next()) {
8614 if (Token::Match(inner, "[[({]"))
8615 inner = inner->link();
8616 else if (Token::Match(inner->previous(), "[,(] . %name% =|{"))
8617 unknownMacroError(tok);
8618 }
8619 }
8620
8621 const bool cpp = isCPP();
8622
8623 // Report unknown macros in non-executable scopes..

Callers

nothing calls this directly

Calls 13

tokensFunction · 0.85
isCPPFunction · 0.85
isNonMacroFunction · 0.85
nextMethod · 0.80
linkAtMethod · 0.80
isUpperCaseNameMethod · 0.80
simpleMatchFunction · 0.70
startsWithFunction · 0.70
isKeywordMethod · 0.45
tokAtMethod · 0.45
strMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected