some minor optimizations (#4449)

* cppcheck.cpp: reduced scope of a variable

* cppcheck.cpp: removed unnecessary severity checks

* cppcheck.cpp: avoid unnecessary copy

* templatesimplifier.cpp: perform early exit in loop in `expandTemplate()`
This commit is contained in:
Oliver Stöneberg 2022-09-07 19:19:04 +02:00 committed by GitHub
parent 32d96104d6
commit 847391ea2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 12 deletions

View File

@ -911,20 +911,20 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
ErrorMessage::FileLocation loc(e.token, &tokenizer.list); ErrorMessage::FileLocation loc(e.token, &tokenizer.list);
locationList.push_back(loc); locationList.push_back(loc);
} else { } else {
ErrorMessage::FileLocation loc(tokenizer.list.getSourceFilePath(), 0, 0);
ErrorMessage::FileLocation loc2(filename, 0, 0); ErrorMessage::FileLocation loc2(filename, 0, 0);
locationList.push_back(loc2); locationList.push_back(loc2);
if (filename != tokenizer.list.getSourceFilePath()) if (filename != tokenizer.list.getSourceFilePath()) {
ErrorMessage::FileLocation loc(tokenizer.list.getSourceFilePath(), 0, 0);
locationList.push_back(loc); locationList.push_back(loc);
} }
ErrorMessage errmsg(locationList, }
ErrorMessage errmsg(std::move(locationList),
tokenizer.list.getSourceFilePath(), tokenizer.list.getSourceFilePath(),
Severity::error, Severity::error,
e.errorMessage, e.errorMessage,
e.id, e.id,
Certainty::normal); Certainty::normal);
if (errmsg.severity == Severity::error || mSettings.severity.isEnabled(errmsg.severity))
reportErr(errmsg); reportErr(errmsg);
} }
} }

View File

@ -2179,6 +2179,10 @@ void TemplateSimplifier::expandTemplate(
continue; continue;
} }
// don't add instantiations in template definitions
if (!templates.empty())
continue;
std::string scope; std::string scope;
const Token *prev = tok3; const Token *prev = tok3;
for (; Token::Match(prev->tokAt(-2), "%name% ::"); prev = prev->tokAt(-2)) { for (; Token::Match(prev->tokAt(-2), "%name% ::"); prev = prev->tokAt(-2)) {
@ -2202,14 +2206,11 @@ void TemplateSimplifier::expandTemplate(
} }
} }
// don't add instantiations in template definitions
if (templates.empty()) {
if (copy) if (copy)
newInstantiations.emplace_back(mTokenList.back(), scope); newInstantiations.emplace_back(mTokenList.back(), scope);
else if (!inTemplateDefinition) else if (!inTemplateDefinition)
newInstantiations.emplace_back(tok3, scope); newInstantiations.emplace_back(tok3, scope);
} }
}
// link() newly tokens manually // link() newly tokens manually
else if (copy) { else if (copy) {