Skip to content

Commit

Permalink
fixed some modernize-use-emplace false negatives with std::stack
Browse files Browse the repository at this point in the history
…/ removed `internalStlUsage` check (#4346)
  • Loading branch information
firewave committed Aug 17, 2022
1 parent 5b4c6c1 commit 5f171b9
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 28 deletions.
4 changes: 2 additions & 2 deletions gui/test/data/benchmark/simple.cpp
Expand Up @@ -357,7 +357,7 @@ void CheckOther::checkSwitchCaseFallThrough()
reportError(_tokenizer->tokens(), Severity::debug, "debug", errmsg.str());
break;
}
ifnest.push(std::make_pair(tok2->link(), false));
ifnest.emplace(tok2->link(), false);
justbreak = false;
} else if (Token::simpleMatch(tok2, "while (")) {
tok2 = tok2->tokAt(1)->link()->next();
Expand Down Expand Up @@ -420,7 +420,7 @@ void CheckOther::checkSwitchCaseFallThrough()
reportError(_tokenizer->tokens(), Severity::debug, "debug", errmsg.str());
break;
}
ifnest.push(std::make_pair(tok2->link(), justbreak));
ifnest.emplace(tok2->link(), justbreak);
justbreak = false;
} else {
justbreak &= ifnest.top().second;
Expand Down
13 changes: 0 additions & 13 deletions lib/checkinternal.cpp
Expand Up @@ -343,19 +343,6 @@ void CheckInternal::checkExtraWhitespace()
}
}

void CheckInternal::checkStlUsage()
{
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
for (const Scope *scope : symbolDatabase->functionScopes) {
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
if (Token::simpleMatch(tok, ". emplace ("))
reportError(tok, Severity::error, "internalStlUsage", "The 'emplace' function shall be avoided for now. It is not available e.g. in Slackware 14.0. 'emplace_back' is fine.");
//if (Token::simpleMatch(tok, ". back ( )") && tok->astOperand1() && tok->astOperand1()->valueType() && tok->astOperand1()->valueType()->container && Token::simpleMatch(tok->astOperand1()->valueType()->container, "std :: string"))
// reportError(tok, Severity::error, "internalStlUsage", "The 'std::string::back()' function shall be avoided for now.");
}
}
}

void CheckInternal::multiComparePatternError(const Token* tok, const std::string& pattern, const std::string &funcname)
{
reportError(tok, Severity::error, "multiComparePatternError",
Expand Down
4 changes: 0 additions & 4 deletions lib/checkinternal.h
Expand Up @@ -55,7 +55,6 @@ class CPPCHECKLIB CheckInternal : public Check {
checkInternal.checkRedundantNextPrevious();
checkInternal.checkExtraWhitespace();
checkInternal.checkRedundantTokCheck();
checkInternal.checkStlUsage();
}

/** @brief %Check if a simple pattern is used inside Token::Match or Token::findmatch */
Expand All @@ -78,9 +77,6 @@ class CPPCHECKLIB CheckInternal : public Check {

/** @brief %Check if there is a redundant check for none-nullness of parameter before Match functions, such as (tok && Token::Match(tok, "foo")) */
void checkRedundantTokCheck();

/** @brief Try to avoid some new functions that are not fully supported in Linux */
void checkStlUsage();
private:
void multiComparePatternError(const Token *tok, const std::string &pattern, const std::string &funcname);
void simplePatternError(const Token *tok, const std::string &pattern, const std::string &funcname);
Expand Down
6 changes: 3 additions & 3 deletions lib/symboldatabase.cpp
Expand Up @@ -699,7 +699,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
scope->checkVariable(tok->tokAt(2), AccessControl::Throw, mSettings); // check for variable declaration and add it to new scope if found
tok = scopeStartTok;
} else if (Token::Match(tok, "%var% {")) {
endInitList.push(std::make_pair(tok->next()->link(), scope));
endInitList.emplace(tok->next()->link(), scope);
tok = tok->next();
} else if (const Token *lambdaEndToken = findLambdaEndToken(tok)) {
const Token *lambdaStartToken = lambdaEndToken->link();
Expand All @@ -711,13 +711,13 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
tok = lambdaStartToken;
} else if (tok->str() == "{") {
if (inInitList()) {
endInitList.push(std::make_pair(tok->link(), scope));
endInitList.emplace(tok->link(), scope);
} else if (isExecutableScope(tok)) {
scopeList.emplace_back(this, tok, scope, Scope::eUnconditional, tok);
scope->nestedList.push_back(&scopeList.back());
scope = &scopeList.back();
} else if (scope->isExecutable()) {
endInitList.push(std::make_pair(tok->link(), scope));
endInitList.emplace(tok->link(), scope);
} else {
tok = tok->link();
}
Expand Down
10 changes: 5 additions & 5 deletions lib/tokenize.cpp
Expand Up @@ -3466,7 +3466,7 @@ class VariableMap {

void VariableMap::enterScope()
{
mScopeInfo.push(std::vector<std::pair<std::string, nonneg int>>());
mScopeInfo.emplace(/*std::vector<std::pair<std::string, nonneg int>>()*/);
}

bool VariableMap::leaveScope()
Expand Down Expand Up @@ -3840,7 +3840,7 @@ void Tokenizer::setVarIdPass1()

std::stack<VarIdScopeInfo> scopeStack;

scopeStack.push(VarIdScopeInfo());
scopeStack.emplace(/*VarIdScopeInfo()*/);
std::stack<const Token *> functionDeclEndStack;
const Token *functionDeclEndToken = nullptr;
bool initlist = false;
Expand All @@ -3864,7 +3864,7 @@ void Tokenizer::setVarIdPass1()
if (!variableMap.leaveScope())
cppcheckError(tok);
} else if (tok->str() == "{") {
scopeStack.push(VarIdScopeInfo(true, scopeStack.top().isStructInit || tok->strAt(-1) == "=", /*isEnum=*/ false, *variableMap.getVarId()));
scopeStack.emplace(true, scopeStack.top().isStructInit || tok->strAt(-1) == "=", /*isEnum=*/ false, *variableMap.getVarId());

// check if this '{' is a start of an "if" body
const Token * ifToken = tok->previous();
Expand Down Expand Up @@ -3918,7 +3918,7 @@ void Tokenizer::setVarIdPass1()
variableMap.enterScope();
}
initlist = false;
scopeStack.push(VarIdScopeInfo(isExecutable, scopeStack.top().isStructInit || tok->strAt(-1) == "=", isEnumStart(tok), *variableMap.getVarId()));
scopeStack.emplace(isExecutable, scopeStack.top().isStructInit || tok->strAt(-1) == "=", isEnumStart(tok), *variableMap.getVarId());
} else { /* if (tok->str() == "}") */
bool isNamespace = false;
for (const Token *tok1 = tok->link()->previous(); tok1 && tok1->isName(); tok1 = tok1->previous()) {
Expand Down Expand Up @@ -3952,7 +3952,7 @@ void Tokenizer::setVarIdPass1()

scopeStack.pop();
if (scopeStack.empty()) { // should be impossible
scopeStack.push(VarIdScopeInfo());
scopeStack.emplace(/*VarIdScopeInfo()*/);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.cpp
Expand Up @@ -69,7 +69,7 @@ bool matchglob(const std::string& pattern, const std::string& name)
}
if (*n != '\0') {
// If this isn't the last possibility, save it for later
backtrack.push(std::make_pair(p, n));
backtrack.emplace(p, n);
}
break;
case '?':
Expand Down

0 comments on commit 5f171b9

Please sign in to comment.