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

Method simplifyFunctionParameters

lib/tokenize.cpp:7017–7186  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7015}
7016
7017void Tokenizer::simplifyFunctionParameters()
7018{
7019 for (Token *tok = list.front(); tok; tok = tok->next()) {
7020 if (tok->link() && Token::Match(tok, "{|[|(")) {
7021 tok = tok->link();
7022 }
7023
7024 // Find the function e.g. foo( x ) or foo( x, y )
7025 else if (Token::Match(tok, "%name% ( %name% [,)]") && !Token::Match(tok->tokAt(-1), ":|,|::|=")) {
7026 // We have found old style function, now we need to change it
7027
7028 // First step: Get list of argument names in parentheses
7029 std::map<std::string, Token *> argumentNames;
7030 bool bailOut = false;
7031 const Token * tokparam = nullptr;
7032
7033 //take count of the function name..
7034 const std::string& funcName(tok->str());
7035
7036 //floating token used to check for parameters
7037 Token *tok1 = tok;
7038
7039 while (nullptr != (tok1 = tok1->tokAt(2))) {
7040 if (!Token::Match(tok1, "%name% [,)]")) {
7041 bailOut = true;
7042 break;
7043 }
7044
7045 //same parameters: take note of the parameter
7046 if (argumentNames.find(tok1->str()) != argumentNames.end())
7047 tokparam = tok1;
7048 else if (tok1->str() != funcName)
7049 argumentNames[tok1->str()] = tok1;
7050 else {
7051 if (tok1->strAt(1) == ")") {
7052 if (tok1->strAt(-1) == ",") {
7053 tok1 = tok1->tokAt(-2);
7054 tok1->deleteNext(2);
7055 } else {
7056 tok1 = tok1->previous();
7057 tok1->deleteNext();
7058 bailOut = true;
7059 break;
7060 }
7061 } else {
7062 tok1 = tok1->tokAt(-2);
7063 tok1->next()->deleteNext(2);
7064 }
7065 }
7066
7067 if (tok1->strAt(1) == ")") {
7068 tok1 = tok1->tokAt(2);
7069 //expect at least a type name after round brace..
7070 if (!tok1 || !tok1->isName())
7071 bailOut = true;
7072 break;
7073 }
7074 }

Callers

nothing calls this directly

Calls 12

replaceFunction · 0.85
frontMethod · 0.80
nextMethod · 0.80
deleteNextMethod · 0.80
insertTokenMethod · 0.80
linkAtMethod · 0.80
simpleMatchFunction · 0.70
tokAtMethod · 0.45
findMethod · 0.45
strMethod · 0.45
endMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected