From b60d6d19c5f116b4550b5296f6bf9968f252fa1c Mon Sep 17 00:00:00 2001 From: Ken-Patrick Lehrmann Date: Wed, 20 May 2020 12:15:03 +0200 Subject: [PATCH 1/2] Pass defines flags when calling clang --- lib/cppcheck.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 03b95f86eb5..0c4d89eeb14 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -233,6 +233,20 @@ static std::string executeAddon(const AddonInfo &addonInfo, return result; } +static std::string getDefinesFlags(const std::string &semicolonSeparatedString) +{ + std::string allDefines; + if (!semicolonSeparatedString.empty()) { + allDefines = "-D"+semicolonSeparatedString + " "; + std::string::size_type pos = 0u; + while ((pos = allDefines.find(";", pos)) != std::string::npos) { + allDefines.replace(pos, 1, " -D"); + pos += 3; + } + } + return allDefines; +} + CppCheck::CppCheck(ErrorLogger &errorLogger, bool useGlobalSuppressions, std::function,std::string,std::string*)> executeCommand) @@ -350,6 +364,9 @@ unsigned int CppCheck::check(const std::string &path) for (const std::string &i: mSettings.includePaths) flags += "-I" + i + " "; + flags += getDefinesFlags(mSettings.userDefines); + + const std::string args2 = "-cc1 -ast-dump " + flags + path; const std::string redirect2 = analyzerInfo.empty() ? std::string("2>&1") : ("2> " + clangStderr); if (!mSettings.buildDir.empty()) { @@ -1407,16 +1424,11 @@ void CppCheck::getErrorMessages() void CppCheck::analyseClangTidy(const ImportProject::FileSettings &fileSettings) { std::string allIncludes = ""; - std::string allDefines = "-D"+fileSettings.defines; for (const std::string &inc : fileSettings.includePaths) { allIncludes = allIncludes + "-I\"" + inc + "\" "; } - std::string::size_type pos = 0u; - while ((pos = allDefines.find(";", pos)) != std::string::npos) { - allDefines.replace(pos, 1, " -D"); - pos += 3; - } + const std::string allDefines = getDefinesFlags(fileSettings.defines); #ifdef _WIN32 const char exe[] = "clang-tidy.exe"; From baf16254a2a6e4ee30b7e39e465c7c6833e048a2 Mon Sep 17 00:00:00 2001 From: Ken-Patrick Lehrmann Date: Wed, 20 May 2020 12:42:22 +0200 Subject: [PATCH 2/2] Use split instead of manual parsing --- lib/cppcheck.cpp | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 0c4d89eeb14..1fa6cf46dce 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -235,16 +235,10 @@ static std::string executeAddon(const AddonInfo &addonInfo, static std::string getDefinesFlags(const std::string &semicolonSeparatedString) { - std::string allDefines; - if (!semicolonSeparatedString.empty()) { - allDefines = "-D"+semicolonSeparatedString + " "; - std::string::size_type pos = 0u; - while ((pos = allDefines.find(";", pos)) != std::string::npos) { - allDefines.replace(pos, 1, " -D"); - pos += 3; - } - } - return allDefines; + std::string flags; + for (const std::string &d: split(semicolonSeparatedString, ";")) + flags += "-D" + d + " "; + return flags; } CppCheck::CppCheck(ErrorLogger &errorLogger,