Minor: add endsWith() template, empy() check (#3472)

This commit is contained in:
chrchr-github 2021-09-28 20:34:21 +02:00 committed by GitHub
parent d87c599f1e
commit 0c16e346f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 21 additions and 15 deletions

View File

@ -1872,7 +1872,7 @@ static bool isCPPCastKeyword(const Token* tok)
{ {
if (!tok) if (!tok)
return false; return false;
return endsWith(tok->str(), "_cast", 5); return endsWith(tok->str(), "_cast");
} }
static bool isTrivialConstructor(const Token* tok) static bool isTrivialConstructor(const Token* tok)

View File

@ -258,7 +258,7 @@ void CheckString::checkIncorrectStringCompare()
for (const Scope * scope : symbolDatabase->functionScopes) { for (const Scope * scope : symbolDatabase->functionScopes) {
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
// skip "assert(str && ..)" and "assert(.. && str)" // skip "assert(str && ..)" and "assert(.. && str)"
if ((endsWith(tok->str(), "assert", 6) || endsWith(tok->str(), "ASSERT", 6)) && if ((endsWith(tok->str(), "assert") || endsWith(tok->str(), "ASSERT")) &&
Token::Match(tok, "%name% (") && Token::Match(tok, "%name% (") &&
(Token::Match(tok->tokAt(2), "%str% &&") || Token::Match(tok->next()->link()->tokAt(-2), "&& %str% )"))) (Token::Match(tok->tokAt(2), "%str% &&") || Token::Match(tok->next()->link()->tokAt(-2), "&& %str% )")))
tok = tok->next()->link(); tok = tok->next()->link();

View File

@ -1576,7 +1576,7 @@ void clangimport::parseClangAstDump(Tokenizer *tokenizer, std::istream &f)
const std::string nodeType = line.substr(pos1+1, pos2 - pos1 - 1); const std::string nodeType = line.substr(pos1+1, pos2 - pos1 - 1);
const std::string ext = line.substr(pos2); const std::string ext = line.substr(pos2);
if (pos1 == 1 && endsWith(nodeType, "Decl", 4)) { if (pos1 == 1 && endsWith(nodeType, "Decl")) {
if (!tree.empty()) if (!tree.empty())
tree[0]->createTokens1(tokenList); tree[0]->createTokens1(tokenList);
tree.clear(); tree.clear();

View File

@ -140,7 +140,7 @@ namespace {
if (fileName.find(".") == std::string::npos) if (fileName.find(".") == std::string::npos)
return getAddonInfo(fileName + ".py", exename); return getAddonInfo(fileName + ".py", exename);
if (endsWith(fileName, ".py", 3)) { if (endsWith(fileName, ".py")) {
scriptFile = getFullPath(fileName, exename); scriptFile = getFullPath(fileName, exename);
if (scriptFile.empty()) if (scriptFile.empty())
return "Did not find addon " + fileName; return "Did not find addon " + fileName;
@ -160,7 +160,7 @@ namespace {
return ""; return "";
} }
if (!endsWith(fileName, ".json", 5)) if (!endsWith(fileName, ".json"))
return "Failed to open addon " + fileName; return "Failed to open addon " + fileName;
std::ifstream fin(fileName); std::ifstream fin(fileName);
@ -1312,7 +1312,7 @@ void CppCheck::executeAddons(const std::vector<std::string>& files)
std::string fileList; std::string fileList;
if (files.size() >= 2 || endsWith(files[0], ".ctu-info", 9)) { if (files.size() >= 2 || endsWith(files[0], ".ctu-info")) {
fileList = Path::getPathFromFilename(files[0]) + FILELIST; fileList = Path::getPathFromFilename(files[0]) + FILELIST;
std::ofstream fout(fileList); std::ofstream fout(fileList);
for (const std::string& f: files) for (const std::string& f: files)
@ -1327,7 +1327,7 @@ void CppCheck::executeAddons(const std::vector<std::string>& files)
mExitCode = 1; mExitCode = 1;
continue; continue;
} }
if (addon != "misra" && !addonInfo.ctu && endsWith(files.back(), ".ctu-info", 9)) if (addon != "misra" && !addonInfo.ctu && endsWith(files.back(), ".ctu-info"))
continue; continue;
const std::string results = const std::string results =

View File

@ -197,24 +197,24 @@ ImportProject::Type ImportProject::import(const std::string &filename, Settings
const std::string fileFilter = settings ? settings->fileFilter : std::string(); const std::string fileFilter = settings ? settings->fileFilter : std::string();
if (endsWith(filename, ".json", 5)) { if (endsWith(filename, ".json")) {
importCompileCommands(fin); importCompileCommands(fin);
setRelativePaths(filename); setRelativePaths(filename);
return ImportProject::Type::COMPILE_DB; return ImportProject::Type::COMPILE_DB;
} else if (endsWith(filename, ".sln", 4)) { } else if (endsWith(filename, ".sln")) {
importSln(fin, mPath, fileFilter); importSln(fin, mPath, fileFilter);
setRelativePaths(filename); setRelativePaths(filename);
return ImportProject::Type::VS_SLN; return ImportProject::Type::VS_SLN;
} else if (endsWith(filename, ".vcxproj", 8)) { } else if (endsWith(filename, ".vcxproj")) {
std::map<std::string, std::string, cppcheck::stricmp> variables; std::map<std::string, std::string, cppcheck::stricmp> variables;
importVcxproj(filename, variables, emptyString, fileFilter); importVcxproj(filename, variables, emptyString, fileFilter);
setRelativePaths(filename); setRelativePaths(filename);
return ImportProject::Type::VS_VCXPROJ; return ImportProject::Type::VS_VCXPROJ;
} else if (endsWith(filename, ".bpr", 4)) { } else if (endsWith(filename, ".bpr")) {
importBcb6Prj(filename); importBcb6Prj(filename);
setRelativePaths(filename); setRelativePaths(filename);
return ImportProject::Type::BORLAND; return ImportProject::Type::BORLAND;
} else if (settings && endsWith(filename, ".cppcheck", 9)) { } else if (settings && endsWith(filename, ".cppcheck")) {
const bool success = importCppcheckGuiProject(fin, settings); const bool success = importCppcheckGuiProject(fin, settings);
setRelativePaths(filename); setRelativePaths(filename);
return success ? ImportProject::Type::CPPCHECK_GUI : ImportProject::Type::MISSING; return success ? ImportProject::Type::CPPCHECK_GUI : ImportProject::Type::MISSING;
@ -294,7 +294,7 @@ void ImportProject::FileSettings::parseCommand(std::string command)
defs += fval; defs += fval;
if (defval.size() >= 3 && defval.compare(0,2,"=\"")==0 && defval.back()=='\"') if (defval.size() >= 3 && defval.compare(0,2,"=\"")==0 && defval.back()=='\"')
defval = "=" + unescape(defval.substr(2, defval.size() - 3)); defval = "=" + unescape(defval.substr(2, defval.size() - 3));
else if (defval.size() >= 5 && defval.compare(0,3,"=\\\"")==0 && endsWith(defval,"\\\"",2)) else if (defval.size() >= 5 && defval.compare(0, 3, "=\\\"") == 0 && endsWith(defval, "\\\""))
defval = "=\"" + unescape(defval.substr(3, defval.size() - 5)) + "\""; defval = "=\"" + unescape(defval.substr(3, defval.size() - 5)) + "\"";
if (!defval.empty()) if (!defval.empty())
defs += defval; defs += defval;

View File

@ -2279,7 +2279,7 @@ Function::Function(const Token *tokenDef, const std::string &clangType)
setFlags(tokenDef, tokenDef->scope()); setFlags(tokenDef, tokenDef->scope());
if (endsWith(clangType, " const", 6)) if (endsWith(clangType, " const"))
isConst(true); isConst(true);
} }

View File

@ -52,7 +52,7 @@ struct EnumClassHash {
inline bool endsWith(const std::string &str, char c) inline bool endsWith(const std::string &str, char c)
{ {
return str[str.size()-1U] == c; return !str.empty() && str.back() == c;
} }
inline bool endsWith(const std::string &str, const char end[], std::size_t endlen) inline bool endsWith(const std::string &str, const char end[], std::size_t endlen)
@ -60,6 +60,12 @@ inline bool endsWith(const std::string &str, const char end[], std::size_t endle
return (str.size() >= endlen) && (str.compare(str.size()-endlen, endlen, end)==0); return (str.size() >= endlen) && (str.compare(str.size()-endlen, endlen, end)==0);
} }
template<std::size_t N>
bool endsWith(const std::string& str, const char (&end)[N])
{
return endsWith(str, end, N - 1);
}
inline static bool isPrefixStringCharLiteral(const std::string &str, char q, const std::string& p) inline static bool isPrefixStringCharLiteral(const std::string &str, char q, const std::string& p)
{ {
if (!endsWith(str, q)) if (!endsWith(str, q))