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

Method simplifyStaticConst

lib/tokenize.cpp:7700–7768  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7698}
7699
7700void Tokenizer::simplifyStaticConst()
7701{
7702 // This function will simplify the token list so that the qualifiers "extern", "static"
7703 // and "const" appear in the same order as in the array below.
7704 static const std::array<std::string, 3> qualifiers = {"extern", "static", "const"};
7705
7706 // Move 'const' before all other qualifiers and types and then
7707 // move 'static' before all other qualifiers and types, ...
7708 for (Token *tok = list.front(); tok; tok = tok->next()) {
7709 bool continue2 = false;
7710 for (std::size_t i = 0; i < qualifiers.size(); i++) {
7711
7712 // Keep searching for a qualifier
7713 if (!tok->next() || tok->strAt(1) != qualifiers[i])
7714 continue;
7715
7716 // Look backwards to find the beginning of the declaration
7717 Token* leftTok = tok;
7718 bool behindOther = false;
7719 for (; leftTok; leftTok = leftTok->previous()) {
7720 behindOther = std::any_of(qualifiers.cbegin(), qualifiers.cbegin() + i + 1, [&](const std::string& q) {
7721 return q == leftTok->str();
7722 });
7723 if (behindOther)
7724 break;
7725 if (isCPP() && Token::simpleMatch(leftTok, ">")) {
7726 Token* opening = leftTok->findOpeningBracket();
7727 if (opening) {
7728 leftTok = opening;
7729 continue;
7730 }
7731 }
7732 if (!Token::Match(leftTok, "%type%|struct|::") ||
7733 (isCPP() && Token::Match(leftTok, "private:|protected:|public:|operator|template"))) {
7734 break;
7735 }
7736 }
7737
7738 // The token preceding the declaration should indicate the start of a declaration
7739 if (leftTok == tok)
7740 continue;
7741
7742 if (leftTok && !behindOther && !Token::Match(leftTok, ";|{|}|(|,|private:|protected:|public:")) {
7743 continue2 = true;
7744 break;
7745 }
7746
7747 // Move the qualifier to the left-most position in the declaration
7748 const int column = tok->next()->column();
7749 tok->deleteNext();
7750 if (!leftTok) {
7751 list.front()->insertToken(qualifiers[i]);
7752 list.front()->swapWithNext();
7753 list.front()->column(column);
7754 tok = list.front();
7755 } else if (leftTok->next()) {
7756 leftTok->next()->insertTokenBefore(qualifiers[i]);
7757 leftTok->next()->column(column);

Callers

nothing calls this directly

Calls 11

isCPPFunction · 0.85
frontMethod · 0.80
nextMethod · 0.80
findOpeningBracketMethod · 0.80
columnMethod · 0.80
deleteNextMethod · 0.80
insertTokenMethod · 0.80
swapWithNextMethod · 0.80
simpleMatchFunction · 0.70
sizeMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected