From b60736ec1405bb0a8dd40989f67ef4c93da068ab Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Tue, 16 Feb 2021 21:13:02 +0100 Subject: Vendor import of llvm-project main 8e464dd76bef, the last commit before the upstream release/12.x branch was created. --- clang/lib/Tooling/CompilationDatabase.cpp | 58 +++++++++++++------------------ 1 file changed, 24 insertions(+), 34 deletions(-) (limited to 'clang/lib/Tooling/CompilationDatabase.cpp') 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 UnusedInputs; }; -// Unary functor for asking "Given a StringRef S1, does there exist a string -// S2 in Arr where S1 == S2?" -struct MatchesAny { - MatchesAny(ArrayRef 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 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 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 Compilation( NewDriver->BuildCompilation(Args)); @@ -320,15 +303,14 @@ static bool stripPositionalArgs(std::vector 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::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::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::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::loadFromBuffer(StringRef Directory, StringRef Data, + std::string &ErrorMsg) { + ErrorMsg.clear(); std::vector 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( - llvm::sys::path::parent_path(Path), std::move(Args)); + return std::make_unique(Directory, std::move(Args)); } -FixedCompilationDatabase:: -FixedCompilationDatabase(Twine Directory, ArrayRef CommandLine) { +FixedCompilationDatabase::FixedCompilationDatabase( + const Twine &Directory, ArrayRef CommandLine) { std::vector ToolCommandLine(1, GetClangToolCommand()); ToolCommandLine.insert(ToolCommandLine.end(), CommandLine.begin(), CommandLine.end()); -- cgit v1.3