Index: include/clang/Driver/Driver.h =================================================================== --- include/clang/Driver/Driver.h +++ include/clang/Driver/Driver.h @@ -155,6 +155,9 @@ /// Whether the driver is just the preprocessor. bool CCCIsCPP() const { return Mode == CPPMode; } + /// Whether the driver should follow gcc like behavior. + bool CCCIsCC() const { return Mode == GCCMode; } + /// Whether the driver should follow cl.exe like behavior. bool IsCLMode() const { return Mode == CLMode; } @@ -291,7 +294,7 @@ /// @{ /// ParseDriverMode - Look for and handle the driver mode option in Args. - void ParseDriverMode(ArrayRef Args); + void ParseDriverMode(StringRef ProgramName, ArrayRef Args); /// ParseArgStrings - Parse the given list of strings into an /// ArgList. @@ -440,6 +443,10 @@ LTOKind getLTOMode() const { return LTOMode; } private: + /// Set the driver mode (cl, gcc, etc) from an option string of the form + /// --driver-mode=. + void setDriverModeFromOption(StringRef Opt); + /// Parse the \p Args list for LTO options and record the type of LTO /// compilation based on which -f(no-)?lto(=.*)? option occurs last. void setLTOMode(const llvm::opt::ArgList &Args); Index: lib/Driver/Driver.cpp =================================================================== --- lib/Driver/Driver.cpp +++ lib/Driver/Driver.cpp @@ -88,31 +88,39 @@ llvm::DeleteContainerSeconds(ToolChains); } -void Driver::ParseDriverMode(ArrayRef Args) { - const std::string OptName = - getOpts().getOption(options::OPT_driver_mode).getPrefixedName(); +void Driver::ParseDriverMode(StringRef ProgramName, + ArrayRef Args) { + auto Default = ToolChain::getTargetAndModeFromProgramName(ProgramName); + StringRef DefaultMode(Default.second); + setDriverModeFromOption(DefaultMode); for (const char *ArgPtr : Args) { // Ingore nullptrs, they are response file's EOL markers if (ArgPtr == nullptr) continue; const StringRef Arg = ArgPtr; - if (!Arg.startswith(OptName)) - continue; + setDriverModeFromOption(Arg); + } +} + +void Driver::setDriverModeFromOption(StringRef Opt) { + const std::string OptName = + getOpts().getOption(options::OPT_driver_mode).getPrefixedName(); + if (!Opt.startswith(OptName)) + return; + StringRef Value = Opt.drop_front(OptName.size()); - const StringRef Value = Arg.drop_front(OptName.size()); - const unsigned M = llvm::StringSwitch(Value) - .Case("gcc", GCCMode) - .Case("g++", GXXMode) - .Case("cpp", CPPMode) - .Case("cl", CLMode) - .Default(~0U); + const unsigned M = llvm::StringSwitch(Value) + .Case("gcc", GCCMode) + .Case("g++", GXXMode) + .Case("cpp", CPPMode) + .Case("cl", CLMode) + .Default(~0U); - if (M != ~0U) - Mode = static_cast(M); - else - Diag(diag::err_drv_unsupported_option_argument) << OptName << Value; - } + if (M != ~0U) + Mode = static_cast(M); + else + Diag(diag::err_drv_unsupported_option_argument) << OptName << Value; } InputArgList Driver::ParseArgStrings(ArrayRef ArgStrings) { @@ -468,7 +476,7 @@ // We look for the driver mode option early, because the mode can affect // how other options are parsed. - ParseDriverMode(ArgList.slice(1)); + ParseDriverMode(ClangExecutable, ArgList.slice(1)); // FIXME: What are we going to do with -V and -b? Index: lib/Tooling/JSONCompilationDatabase.cpp =================================================================== --- lib/Tooling/JSONCompilationDatabase.cpp +++ lib/Tooling/JSONCompilationDatabase.cpp @@ -16,7 +16,10 @@ #include "clang/Tooling/CompilationDatabasePluginRegistry.h" #include "clang/Tooling/Tooling.h" #include "llvm/ADT/SmallString.h" +#include "llvm/Support/Allocator.h" +#include "llvm/Support/CommandLine.h" #include "llvm/Support/Path.h" +#include "llvm/Support/StringSaver.h" #include namespace clang { @@ -113,8 +116,17 @@ std::vector unescapeCommandLine( StringRef EscapedCommandLine) { +#if defined(LLVM_ON_WIN32) + llvm::BumpPtrAllocator Alloc; + llvm::StringSaver Saver(Alloc); + llvm::SmallVector T; + llvm::cl::TokenizeWindowsCommandLine(EscapedCommandLine, Saver, T); + std::vector Result(T.begin(), T.end()); + return Result; +#else CommandLineArgumentParser parser(EscapedCommandLine); return parser.parse(); +#endif } class JSONCompilationDatabasePlugin : public CompilationDatabasePlugin { Index: unittests/Driver/ToolChainTest.cpp =================================================================== --- unittests/Driver/ToolChainTest.cpp +++ unittests/Driver/ToolChainTest.cpp @@ -117,4 +117,29 @@ S); } +TEST(ToolChainTest, DefaultDriverMode) { + IntrusiveRefCntPtr DiagOpts = new DiagnosticOptions(); + + IntrusiveRefCntPtr DiagID(new DiagnosticIDs()); + struct TestDiagnosticConsumer : public DiagnosticConsumer {}; + DiagnosticsEngine Diags(DiagID, &*DiagOpts, new TestDiagnosticConsumer); + IntrusiveRefCntPtr InMemoryFileSystem( + new vfs::InMemoryFileSystem); + + Driver CCDriver("/home/test/bin/clang", "arm-linux-gnueabi", Diags, + InMemoryFileSystem); + Driver CXXDriver("/home/test/bin/clang++", "arm-linux-gnueabi", Diags, + InMemoryFileSystem); + Driver CLDriver("/home/test/bin/clang-cl", "arm-linux-gnueabi", Diags, + InMemoryFileSystem); + + std::unique_ptr CC(CCDriver.BuildCompilation({"foo.cpp"})); + std::unique_ptr CXX(CXXDriver.BuildCompilation({"foo.cpp"})); + std::unique_ptr CL(CLDriver.BuildCompilation({"foo.cpp"})); + + EXPECT_TRUE(CCDriver.CCCIsCC()); + EXPECT_TRUE(CXXDriver.CCCIsCXX()); + EXPECT_TRUE(CLDriver.IsCLMode()); +} + } // end anonymous namespace