Skip to content

Commit fb37137

Browse files
authored
extended the --clang command-line option so you can specify a custom … (cppcheck-opensource#2734)
1 parent ccdd5f0 commit fb37137

5 files changed

Lines changed: 23 additions & 9 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,12 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
227227
else if (std::strcmp(argv[i], "--check-library") == 0)
228228
mSettings->checkLibrary = true;
229229

230-
else if (std::strcmp(argv[i], "--clang") == 0)
230+
else if (std::strncmp(argv[i], "--clang", 7) == 0) {
231231
mSettings->clang = true;
232+
if (std::strncmp(argv[i], "--clang=", 8) == 0) {
233+
mSettings->clangExecutable = argv[i] + 8;
234+
}
235+
}
232236

233237
else if (std::strncmp(argv[i], "--config-exclude=",17) ==0) {
234238
mSettings->configExcludePaths.insert(Path::fromNativeSeparators(argv[i] + 17));
@@ -972,11 +976,13 @@ void CmdLineParser::printHelp()
972976
" analysis is disabled by this flag.\n"
973977
" --check-library Show information messages when library files have\n"
974978
" incomplete info.\n"
975-
" --clang Experimental: Use Clang parser instead of the builtin\n"
976-
" Cppcheck parser. Cppcheck executes `clang`. The Clang\n"
977-
" AST is imported and converted into Cppcheck data.\n"
978-
" After that the normal Cppcheck analysis is used. You\n"
979-
" must have `clang` in PATH.\n"
979+
" --clang=<path> Experimental: Use Clang parser instead of the builtin Cppcheck\n"
980+
" parser. Takes the executable as optional parameter and\n"
981+
" defaults to `clang`. Cppcheck will run the given Clang\n"
982+
" executable, import the Clang AST and convert it into\n"
983+
" Cppcheck data. After that the normal Cppcheck analysis is\n"
984+
" used. You must have the executable in PATH if no path is\n"
985+
" given.\n"
980986
" --config-exclude=<dir>\n"
981987
" Path (prefix) to be excluded from configuration\n"
982988
" checking. Preprocessor configurations defined in\n"

lib/cppcheck.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,12 @@ unsigned int CppCheck::check(const std::string &path)
328328
const std::string analyzerInfo = mSettings.buildDir.empty() ? std::string() : AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDir, path, "");
329329
const std::string clangcmd = analyzerInfo + ".clang-cmd";
330330
const std::string clangStderr = analyzerInfo + ".clang-stderr";
331+
std::string exe = mSettings.clangExecutable;
331332
#ifdef _WIN32
332-
const std::string exe = "clang.exe";
333-
#else
334-
const std::string exe = "clang";
333+
// append .exe if it is not a path
334+
if (Path::fromNativeSeparators(mSettings.clangExecutable).find('/') == std::string::npos) {
335+
exe += ".exe";
336+
}
335337
#endif
336338

337339
std::string flags(lang + " ");

lib/settings.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Settings::Settings()
3737
checkLibrary(false),
3838
checkUnusedTemplates(false),
3939
clang(false),
40+
clangExecutable("clang"),
4041
clangTidy(false),
4142
daca(false),
4243
debugBugHunting(false),

lib/settings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ class CPPCHECKLIB Settings : public cppcheck::Platform {
115115
/** Use Clang */
116116
bool clang;
117117

118+
/** Custom Clang executable */
119+
std::string clangExecutable;
120+
118121
/** Use clang-tidy */
119122
bool clangTidy;
120123

man/manual.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ Install `clang`. Then use Cppcheck option `--clang`.
116116

117117
Technically, Cppcheck will execute `clang` with its `-ast-dump` option. The Clang output is then imported and converted into our normal Cppcheck format. And then normal Cppcheck analysis is performed on that.
118118

119+
You can also pass a custom Clang executable to the option by using e.g. `--clang=clang-10`. You can also pass it with a path. On Windows it will append the `.exe` extension unless you use a path.
120+
119121
## Severities
120122

121123
The possible severities for messages are:

0 commit comments

Comments
 (0)