diff options
Diffstat (limited to 'contrib/llvm-project/clang/lib/Tooling/InterpolatingCompilationDatabase.cpp')
-rw-r--r-- | contrib/llvm-project/clang/lib/Tooling/InterpolatingCompilationDatabase.cpp | 60 |
1 files changed, 30 insertions, 30 deletions
diff --git a/contrib/llvm-project/clang/lib/Tooling/InterpolatingCompilationDatabase.cpp b/contrib/llvm-project/clang/lib/Tooling/InterpolatingCompilationDatabase.cpp index fa61560e5123..995019ca5a4d 100644 --- a/contrib/llvm-project/clang/lib/Tooling/InterpolatingCompilationDatabase.cpp +++ b/contrib/llvm-project/clang/lib/Tooling/InterpolatingCompilationDatabase.cpp @@ -43,13 +43,13 @@ //===----------------------------------------------------------------------===// #include "clang/Basic/LangStandard.h" +#include "clang/Driver/Driver.h" #include "clang/Driver/Options.h" #include "clang/Driver/Types.h" #include "clang/Tooling/CompilationDatabase.h" +#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" -#include "llvm/ADT/Optional.h" #include "llvm/ADT/StringExtras.h" -#include "llvm/ADT/StringSwitch.h" #include "llvm/Option/ArgList.h" #include "llvm/Option/OptTable.h" #include "llvm/Support/Debug.h" @@ -57,6 +57,7 @@ #include "llvm/Support/StringSaver.h" #include "llvm/Support/raw_ostream.h" #include <memory> +#include <optional> namespace clang { namespace tooling { @@ -127,15 +128,14 @@ struct TransferableCommand { // Flags that should not apply to all files are stripped from CommandLine. CompileCommand Cmd; // Language detected from -x or the filename. Never TY_INVALID. - Optional<types::ID> Type; + std::optional<types::ID> Type; // Standard specified by -std. LangStandard::Kind Std = LangStandard::lang_unspecified; // Whether the command line is for the cl-compatible driver. bool ClangCLMode; TransferableCommand(CompileCommand C) - : Cmd(std::move(C)), Type(guessType(Cmd.Filename)), - ClangCLMode(checkIsCLMode(Cmd.CommandLine)) { + : Cmd(std::move(C)), Type(guessType(Cmd.Filename)) { std::vector<std::string> OldArgs = std::move(Cmd.CommandLine); Cmd.CommandLine.clear(); @@ -145,6 +145,9 @@ struct TransferableCommand { SmallVector<const char *, 16> TmpArgv; for (const std::string &S : OldArgs) TmpArgv.push_back(S.c_str()); + ClangCLMode = !TmpArgv.empty() && + driver::IsClangCL(driver::getDriverMode( + TmpArgv.front(), llvm::ArrayRef(TmpArgv).slice(1))); ArgList = {TmpArgv.begin(), TmpArgv.end()}; } @@ -161,8 +164,7 @@ struct TransferableCommand { const unsigned OldPos = Pos; std::unique_ptr<llvm::opt::Arg> Arg(OptTable.ParseOneArg( ArgList, Pos, - /* Include */ ClangCLMode ? CoreOption | CLOption : 0, - /* Exclude */ ClangCLMode ? 0 : CLOption)); + llvm::opt::Visibility(ClangCLMode ? CLOption : ClangOption))); if (!Arg) continue; @@ -177,6 +179,10 @@ struct TransferableCommand { Opt.matches(OPT__SLASH_Fo)))) continue; + // ...including when the inputs are passed after --. + if (Opt.matches(OPT__DASH_DASH)) + break; + // Strip -x, but record the overridden language. if (const auto GivenType = tryParseTypeArg(*Arg)) { Type = *GivenType; @@ -200,12 +206,14 @@ struct TransferableCommand { Type = foldType(*Type); // The contract is to store None instead of TY_INVALID. if (Type == types::TY_INVALID) - Type = llvm::None; + Type = std::nullopt; } // Produce a CompileCommand for \p filename, based on this one. - CompileCommand transferTo(StringRef Filename) const { - CompileCommand Result = Cmd; + // (This consumes the TransferableCommand just to avoid copying Cmd). + CompileCommand transferTo(StringRef Filename) && { + CompileCommand Result = std::move(Cmd); + Result.Heuristic = "inferred from " + Result.Filename; Result.Filename = std::string(Filename); bool TypeCertain; auto TargetType = guessType(Filename, &TypeCertain); @@ -233,25 +241,12 @@ struct TransferableCommand { llvm::Twine(ClangCLMode ? "/std:" : "-std=") + LangStandard::getLangStandardForKind(Std).getName()).str()); } + Result.CommandLine.push_back("--"); Result.CommandLine.push_back(std::string(Filename)); - Result.Heuristic = "inferred from " + Cmd.Filename; return Result; } private: - // Determine whether the given command line is intended for the CL driver. - static bool checkIsCLMode(ArrayRef<std::string> CmdLine) { - // First look for --driver-mode. - for (StringRef S : llvm::reverse(CmdLine)) { - if (S.consume_front("--driver-mode=")) - return S == "cl"; - } - - // Otherwise just check the clang executable file name. - return !CmdLine.empty() && - llvm::sys::path::stem(CmdLine.front()).endswith_lower("cl"); - } - // Map the language from the --std flag to that of the -x flag. static types::ID toType(Language Lang) { switch (Lang) { @@ -283,7 +278,7 @@ private: } // Try to interpret the argument as a type specifier, e.g. '-x'. - Optional<types::ID> tryParseTypeArg(const llvm::opt::Arg &Arg) { + std::optional<types::ID> tryParseTypeArg(const llvm::opt::Arg &Arg) { const llvm::opt::Option &Opt = Arg.getOption(); using namespace driver::options; if (ClangCLMode) { @@ -295,15 +290,15 @@ private: if (Opt.matches(driver::options::OPT_x)) return types::lookupTypeForTypeSpecifier(Arg.getValue()); } - return None; + return std::nullopt; } // Try to interpret the argument as '-std='. - Optional<LangStandard::Kind> tryParseStdArg(const llvm::opt::Arg &Arg) { + std::optional<LangStandard::Kind> tryParseStdArg(const llvm::opt::Arg &Arg) { using namespace driver::options; if (Arg.getOption().matches(ClangCLMode ? OPT__SLASH_std : OPT_std_EQ)) return LangStandard::getLangKind(Arg.getValue()); - return None; + return std::nullopt; } }; @@ -331,7 +326,7 @@ public: StringRef Path = Strings.save(StringRef(OriginalPaths[I]).lower()); Paths.emplace_back(Path, I); - Types.push_back(foldType(guessType(Path))); + Types.push_back(foldType(guessType(OriginalPaths[I]))); Stems.emplace_back(sys::path::stem(Path), I); auto Dir = ++sys::path::rbegin(Path), DirEnd = sys::path::rend(Path); for (int J = 0; J < DirectorySegmentsIndexed && Dir != DirEnd; ++J, ++Dir) @@ -521,7 +516,7 @@ public: Inner->getCompileCommands(Index.chooseProxy(Filename, foldType(Lang))); if (ProxyCommands.empty()) return {}; - return {TransferableCommand(ProxyCommands[0]).transferTo(Filename)}; + return {transferCompileCommand(std::move(ProxyCommands.front()), Filename)}; } std::vector<std::string> getAllFiles() const override { @@ -544,5 +539,10 @@ inferMissingCompileCommands(std::unique_ptr<CompilationDatabase> Inner) { return std::make_unique<InterpolatingCompilationDatabase>(std::move(Inner)); } +tooling::CompileCommand transferCompileCommand(CompileCommand Cmd, + StringRef Filename) { + return TransferableCommand(std::move(Cmd)).transferTo(Filename); +} + } // namespace tooling } // namespace clang |