diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2021-02-16 20:13:02 +0000 |
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2021-02-16 20:13:02 +0000 |
| commit | b60736ec1405bb0a8dd40989f67ef4c93da068ab (patch) | |
| tree | 5c43fbb7c9fc45f0f87e0e6795a86267dbd12f9d /clang/lib/Tooling/CompilationDatabase.cpp | |
| parent | cfca06d7963fa0909f90483b42a6d7d194d01e08 (diff) | |
Diffstat (limited to 'clang/lib/Tooling/CompilationDatabase.cpp')
| -rw-r--r-- | clang/lib/Tooling/CompilationDatabase.cpp | 58 |
1 files changed, 24 insertions, 34 deletions
diff --git a/clang/lib/Tooling/CompilationDatabase.cpp b/clang/lib/Tooling/CompilationDatabase.cpp index 2b4c26dab96f..1e19e68633d2 100644 --- a/clang/lib/Tooling/CompilationDatabase.cpp +++ b/clang/lib/Tooling/CompilationDatabase.cpp @@ -199,22 +199,6 @@ public: SmallVector<std::string, 2> UnusedInputs; }; -// Unary functor for asking "Given a StringRef S1, does there exist a string -// S2 in Arr where S1 == S2?" -struct MatchesAny { - MatchesAny(ArrayRef<std::string> Arr) : Arr(Arr) {} - - bool operator() (StringRef S) { - for (const std::string *I = Arr.begin(), *E = Arr.end(); I != E; ++I) - if (*I == S) - return true; - return false; - } - -private: - ArrayRef<std::string> Arr; -}; - // Filter of tools unused flags such as -no-integrated-as and -Wa,*. // They are not used for syntax checking, and could confuse targets // which don't support these options. @@ -292,8 +276,7 @@ static bool stripPositionalArgs(std::vector<const char *> Args, // up with no jobs but then this is the user's fault. Args.push_back("placeholder.cpp"); - Args.erase(std::remove_if(Args.begin(), Args.end(), FilterUnusedFlags()), - Args.end()); + llvm::erase_if(Args, FilterUnusedFlags()); const std::unique_ptr<driver::Compilation> Compilation( NewDriver->BuildCompilation(Args)); @@ -320,15 +303,14 @@ static bool stripPositionalArgs(std::vector<const char *> Args, return false; } - // Remove all compilation input files from the command line. This is - // necessary so that getCompileCommands() can construct a command line for - // each file. - std::vector<const char *>::iterator End = std::remove_if( - Args.begin(), Args.end(), MatchesAny(CompileAnalyzer.Inputs)); - - // Remove all inputs deemed unused for compilation. - End = std::remove_if(Args.begin(), End, MatchesAny(DiagClient.UnusedInputs)); - + // Remove all compilation input files from the command line and inputs deemed + // unused for compilation. This is necessary so that getCompileCommands() can + // construct a command line for each file. + std::vector<const char *>::iterator End = + llvm::remove_if(Args, [&](StringRef S) { + return llvm::is_contained(CompileAnalyzer.Inputs, S) || + llvm::is_contained(DiagClient.UnusedInputs, S); + }); // Remove the -c add above as well. It will be at the end right now. assert(strcmp(*(End - 1), "-c") == 0); --End; @@ -341,7 +323,7 @@ std::unique_ptr<FixedCompilationDatabase> FixedCompilationDatabase::loadFromCommandLine(int &Argc, const char *const *Argv, std::string &ErrorMsg, - Twine Directory) { + const Twine &Directory) { ErrorMsg.clear(); if (Argc == 0) return nullptr; @@ -366,20 +348,28 @@ FixedCompilationDatabase::loadFromFile(StringRef Path, std::string &ErrorMsg) { ErrorMsg = "Error while opening fixed database: " + Result.message(); return nullptr; } + return loadFromBuffer(llvm::sys::path::parent_path(Path), + (*File)->getBuffer(), ErrorMsg); +} + +std::unique_ptr<FixedCompilationDatabase> +FixedCompilationDatabase::loadFromBuffer(StringRef Directory, StringRef Data, + std::string &ErrorMsg) { + ErrorMsg.clear(); std::vector<std::string> Args; - for (llvm::StringRef Line : - llvm::make_range(llvm::line_iterator(**File), llvm::line_iterator())) { + StringRef Line; + while (!Data.empty()) { + std::tie(Line, Data) = Data.split('\n'); // Stray whitespace is almost certainly unintended. Line = Line.trim(); if (!Line.empty()) Args.push_back(Line.str()); } - return std::make_unique<FixedCompilationDatabase>( - llvm::sys::path::parent_path(Path), std::move(Args)); + return std::make_unique<FixedCompilationDatabase>(Directory, std::move(Args)); } -FixedCompilationDatabase:: -FixedCompilationDatabase(Twine Directory, ArrayRef<std::string> CommandLine) { +FixedCompilationDatabase::FixedCompilationDatabase( + const Twine &Directory, ArrayRef<std::string> CommandLine) { std::vector<std::string> ToolCommandLine(1, GetClangToolCommand()); ToolCommandLine.insert(ToolCommandLine.end(), CommandLine.begin(), CommandLine.end()); |
