Skip to content

Commit a7533c8

Browse files
committed
Add option "--driver-mode" to config driver mode used by clang-tidy.
clang-tidy can't specify driver mode from command line. It will cause problems when it is used with visual studio compiler (cl.exe). For example, option of cl.exe can be started with either '-' or '/'. But with default driver mode, you can only use '-' with clang-tidy. cl.exe a.cpp /Iinclude can be success, but clang-tidy.exe a.cpp /Iinclude may fail because /Iinclude can't be recongized by default driver mode of clang-tidy. You need change the above line to: clang-tidy.exe a.cpp -Iinclude One workaround is passing "--driver-mode=cl" after "--": clang-tidy.exe a.cpp -- --driver-mode But this is not ideal. Generally, the options follow "--" should be compiler options. "--driver-mode" is not the option of cl.exe. cl.exe will generate "unknown option" warning. This change adding an option "--driver-mode" to clang-tidy itself tries to solve this problem. User can specify "gcc", "g++", "cpp", and "cl" four modes. This option is optional. If this option is not specified, the default driver mode will be used.
1 parent 902f2f4 commit a7533c8

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

clang-tools-extra/docs/clang-tidy/index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ An overview of all the command-line options:
134134
When the value is empty, clang-tidy will
135135
attempt to find a file named .clang-tidy for
136136
each source file in its parent directories.
137+
--driver-mode -
138+
Specify the driver mode:
139+
- gcc: gcc mode
140+
- g++: g++ mode
141+
- cpp: cpp mode
142+
- cl: cl mode
137143
--dump-config -
138144
Dumps configuration in the YAML format to
139145
stdout. This option can be used along with a

clang/lib/Tooling/CommonOptionsParser.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
//
2424
//===----------------------------------------------------------------------===//
2525

26+
#include "clang/Driver/Options.h"
2627
#include "clang/Tooling/CommonOptionsParser.h"
2728
#include "clang/Tooling/Tooling.h"
2829
#include "llvm/Support/CommandLine.h"
@@ -92,6 +93,21 @@ llvm::Error CommonOptionsParser::init(
9293
cl::Positional, cl::desc("<source0> [... <sourceN>]"), OccurrencesFlag,
9394
cl::cat(Category), cl::sub(*cl::AllSubCommands));
9495

96+
static cl::list<std::string> DriverMode(
97+
"driver-mode", cl::desc(R"(
98+
Specify the driver mode:
99+
- gcc: gcc mode
100+
- g++: g++ mode
101+
- cpp: cpp mode
102+
- cl: cl mode
103+
)"),
104+
cl::Optional, cl::cat(Category),
105+
cl::sub(*cl::AllSubCommands));
106+
107+
assert(driver::getDriverOptTable()
108+
.getOption(driver::options::OPT_driver_mode)
109+
.getPrefixedName() == "--driver-mode=");
110+
95111
static cl::list<std::string> ArgsAfter(
96112
"extra-arg",
97113
cl::desc("Additional argument to append to the compiler command line"),

clang/lib/Tooling/CompilationDatabase.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ CompilationDatabase::loadFromDirectory(StringRef BuildDirectory,
6565
std::string &ErrorMessage) {
6666
llvm::raw_string_ostream ErrorStream(ErrorMessage);
6767
for (CompilationDatabasePluginRegistry::iterator
68-
It = CompilationDatabasePluginRegistry::begin(),
69-
Ie = CompilationDatabasePluginRegistry::end();
68+
It = CompilationDatabasePluginRegistry::begin(),
69+
Ie = CompilationDatabasePluginRegistry::end();
7070
It != Ie; ++It) {
7171
std::string DatabaseErrorMessage;
7272
std::unique_ptr<CompilationDatabasePlugin> Plugin(It->instantiate());
@@ -113,7 +113,7 @@ CompilationDatabase::autoDetectFromSource(StringRef SourceFile,
113113

114114
if (!DB)
115115
ErrorMessage = ("Could not auto-detect compilation database for file \"" +
116-
SourceFile + "\"\n" + ErrorMessage).str();
116+
SourceFile + "\"\n" + ErrorMessage).str();
117117
return DB;
118118
}
119119

@@ -353,6 +353,19 @@ FixedCompilationDatabase::loadFromCommandLine(int &Argc,
353353
std::vector<const char *> CommandLine(DoubleDash + 1, Argv + Argc);
354354
Argc = DoubleDash - Argv;
355355

356+
// If find "--driver-mode" option, we need insert this option to CommandLine,
357+
// so that Driver can be set to correct driver mode.
358+
const std::string DriverModePrefix =
359+
driver::getDriverOptTable()
360+
.getOption(driver::options::OPT_driver_mode)
361+
.getPrefixedName();
362+
for (int i = 0; i < Argc; ++i) {
363+
if (llvm::StringRef(Argv[i]).startswith(DriverModePrefix)) {
364+
CommandLine.insert(CommandLine.begin(), Argv[i]);
365+
break;
366+
}
367+
}
368+
356369
std::vector<std::string> StrippedArgs;
357370
if (!stripPositionalArgs(CommandLine, StrippedArgs, ErrorMsg))
358371
return nullptr;
@@ -406,7 +419,7 @@ class FixedCompilationDatabasePlugin : public CompilationDatabasePlugin {
406419
} // namespace
407420

408421
static CompilationDatabasePluginRegistry::Add<FixedCompilationDatabasePlugin>
409-
X("fixed-compilation-database", "Reads plain-text flags file");
422+
X("fixed-compilation-database", "Reads plain-text flags file");
410423

411424
namespace clang {
412425
namespace tooling {

0 commit comments

Comments
 (0)