Index: bindings/python/clang/cindex.py =================================================================== --- bindings/python/clang/cindex.py +++ bindings/python/clang/cindex.py @@ -3223,14 +3223,6 @@ build filename. Returns None if filename is not found in the database. """ return conf.lib.clang_CompilationDatabase_getCompileCommands(self, - filename) - - def getAllCompileCommands(self): - """ - Get an iterable object providing all the CompileCommands available from - the database. - """ - return conf.lib.clang_CompilationDatabase_getAllCompileCommands(self) class Token(Structure): @@ -3301,11 +3293,6 @@ c_object_p, CompilationDatabase.from_result), - ("clang_CompilationDatabase_getAllCompileCommands", - [c_object_p], - c_object_p, - CompileCommands.from_result), - ("clang_CompilationDatabase_getCompileCommands", [c_object_p, c_interop_string], c_object_p, Index: include/clang-c/CXCompilationDatabase.h =================================================================== --- include/clang-c/CXCompilationDatabase.h +++ include/clang-c/CXCompilationDatabase.h @@ -96,6 +96,7 @@ /** * \brief Get all the compile commands in the given compilation database. + * This is no longer supported, the returned list is empty. */ CINDEX_LINKAGE CXCompileCommands clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase); Index: include/clang/Tooling/CommonOptionsParser.h =================================================================== --- include/clang/Tooling/CommonOptionsParser.h +++ include/clang/Tooling/CommonOptionsParser.h @@ -139,16 +139,9 @@ std::vector getCompileCommands(StringRef FilePath) const override; - std::vector getAllFiles() const override; - - std::vector getAllCompileCommands() const override; - private: std::unique_ptr Compilations; std::vector Adjusters; - - std::vector - adjustCommands(std::vector Commands) const; }; } // namespace tooling Index: include/clang/Tooling/CompilationDatabase.h =================================================================== --- include/clang/Tooling/CompilationDatabase.h +++ include/clang/Tooling/CompilationDatabase.h @@ -66,10 +66,6 @@ /// /// A compilation database allows the user to retrieve compile command lines /// for the files in a project. -/// -/// Many implementations are enumerable, allowing all command lines to be -/// retrieved. These can be used to run clang tools over a subset of the files -/// in a project. class CompilationDatabase { public: virtual ~CompilationDatabase(); @@ -83,9 +79,10 @@ /// Returns NULL and sets ErrorMessage if we were not able to build up a /// compilation database for the build directory. /// - /// FIXME: Currently only supports JSON compilation databases, which - /// are named 'compile_commands.json' in the given directory. Extend this - /// for other build types (like ninja build files). + /// Currently supports: + /// - JSON compilation databases (compile_commands.json) + /// - Fixed compilation databases (compile_flags.txt) + /// FIXME: Extend this for other build types (like ninja build files). static std::unique_ptr loadFromDirectory(StringRef BuildDirectory, std::string &ErrorMessage); @@ -103,8 +100,7 @@ static std::unique_ptr autoDetectFromDirectory(StringRef SourceDir, std::string &ErrorMessage); - /// \brief Returns all compile commands in which the specified file was - /// compiled. + /// \brief Returns the compile commands to build a file. /// /// This includes compile commands that span multiple source files. /// For example, consider a project with the following compilations: @@ -114,23 +110,6 @@ /// lines for a.cc and b.cc and only the first command line for t.cc. virtual std::vector getCompileCommands( StringRef FilePath) const = 0; - - /// \brief Returns the list of all files available in the compilation database. - /// - /// By default, returns nothing. Implementations should override this if they - /// can enumerate their source files. - virtual std::vector getAllFiles() const { return {}; } - - /// \brief Returns all compile commands for all the files in the compilation - /// database. - /// - /// FIXME: Add a layer in Tooling that provides an interface to run a tool - /// over all files in a compilation database. Not all build systems have the - /// ability to provide a feasible implementation for \c getAllCompileCommands. - /// - /// By default, this is implemented in terms of getAllFiles() and - /// getCompileCommands(). Subclasses may override this for efficiency. - virtual std::vector getAllCompileCommands() const; }; /// \brief Interface for compilation database plugins. Index: include/clang/Tooling/JSONCompilationDatabase.h =================================================================== --- include/clang/Tooling/JSONCompilationDatabase.h +++ include/clang/Tooling/JSONCompilationDatabase.h @@ -81,15 +81,6 @@ std::vector getCompileCommands(StringRef FilePath) const override; - /// \brief Returns the list of all files available in the compilation database. - /// - /// These are the 'file' entries of the JSON objects. - std::vector getAllFiles() const override; - - /// \brief Returns all compile commands for all the files in the compilation - /// database. - std::vector getAllCompileCommands() const override; - private: /// \brief Constructs a JSON compilation database on a memory buffer. JSONCompilationDatabase(std::unique_ptr Database, @@ -115,10 +106,6 @@ std::vector, llvm::yaml::ScalarNode *> CompileCommandRef; - /// \brief Converts the given array of CompileCommandRefs to CompileCommands. - void getCommands(ArrayRef CommandsRef, - std::vector &Commands) const; - // Maps file paths to the compile command lines for that file. llvm::StringMap> IndexByFile; Index: lib/Tooling/CommonOptionsParser.cpp =================================================================== --- lib/Tooling/CommonOptionsParser.cpp +++ lib/Tooling/CommonOptionsParser.cpp @@ -58,23 +58,9 @@ Adjusters.push_back(std::move(Adjuster)); } -std::vector ArgumentsAdjustingCompilations::getCompileCommands( - StringRef FilePath) const { - return adjustCommands(Compilations->getCompileCommands(FilePath)); -} - -std::vector -ArgumentsAdjustingCompilations::getAllFiles() const { - return Compilations->getAllFiles(); -} - std::vector -ArgumentsAdjustingCompilations::getAllCompileCommands() const { - return adjustCommands(Compilations->getAllCompileCommands()); -} - -std::vector ArgumentsAdjustingCompilations::adjustCommands( - std::vector Commands) const { +ArgumentsAdjustingCompilations::getCompileCommands(StringRef FilePath) const { + auto Commands = Compilations->getCompileCommands(FilePath); for (CompileCommand &Command : Commands) for (const auto &Adjuster : Adjusters) Command.CommandLine = Adjuster(Command.CommandLine, Command.Filename); Index: lib/Tooling/CompilationDatabase.cpp =================================================================== --- lib/Tooling/CompilationDatabase.cpp +++ lib/Tooling/CompilationDatabase.cpp @@ -112,15 +112,6 @@ return DB; } -std::vector CompilationDatabase::getAllCompileCommands() const { - std::vector Result; - for (const auto &File : getAllFiles()) { - auto C = getCompileCommands(File); - std::move(C.begin(), C.end(), std::back_inserter(Result)); - } - return Result; -} - CompilationDatabasePlugin::~CompilationDatabasePlugin() {} namespace { Index: lib/Tooling/JSONCompilationDatabase.cpp =================================================================== --- lib/Tooling/JSONCompilationDatabase.cpp +++ lib/Tooling/JSONCompilationDatabase.cpp @@ -192,6 +192,20 @@ return Database; } +static std::vector +nodeToCommandLine(JSONCommandLineSyntax Syntax, + const std::vector &Nodes) { + SmallString<1024> Storage; + if (Nodes.size() == 1) { + return unescapeCommandLine(Syntax, Nodes[0]->getValue(Storage)); + } + std::vector Arguments; + for (auto *Node : Nodes) { + Arguments.push_back(Node->getValue(Storage)); + } + return Arguments; +} + std::vector JSONCompilationDatabase::getCompileCommands(StringRef FilePath) const { SmallString<128> NativeFilePath; @@ -207,49 +221,7 @@ if (CommandsRefI == IndexByFile.end()) return std::vector(); std::vector Commands; - getCommands(CommandsRefI->getValue(), Commands); - return Commands; -} - -std::vector -JSONCompilationDatabase::getAllFiles() const { - std::vector Result; - - llvm::StringMap< std::vector >::const_iterator - CommandsRefI = IndexByFile.begin(); - const llvm::StringMap< std::vector >::const_iterator - CommandsRefEnd = IndexByFile.end(); - for (; CommandsRefI != CommandsRefEnd; ++CommandsRefI) { - Result.push_back(CommandsRefI->first().str()); - } - - return Result; -} - -std::vector -JSONCompilationDatabase::getAllCompileCommands() const { - std::vector Commands; - getCommands(AllCommands, Commands); - return Commands; -} - -static std::vector -nodeToCommandLine(JSONCommandLineSyntax Syntax, - const std::vector &Nodes) { - SmallString<1024> Storage; - if (Nodes.size() == 1) { - return unescapeCommandLine(Syntax, Nodes[0]->getValue(Storage)); - } - std::vector Arguments; - for (auto *Node : Nodes) { - Arguments.push_back(Node->getValue(Storage)); - } - return Arguments; -} - -void JSONCompilationDatabase::getCommands( - ArrayRef CommandsRef, - std::vector &Commands) const { + ArrayRef CommandsRef = CommandsRefI->getValue(); for (int I = 0, E = CommandsRef.size(); I != E; ++I) { SmallString<8> DirectoryStorage; SmallString<32> FilenameStorage; @@ -261,6 +233,7 @@ nodeToCommandLine(Syntax, std::get<2>(CommandsRef[I])), Output ? Output->getValue(OutputStorage) : ""); } + return Commands; } bool JSONCompilationDatabase::parse(std::string &ErrorMessage) { Index: test/Index/skip-parsed-bodies/compile_commands.json =================================================================== --- test/Index/skip-parsed-bodies/compile_commands.json +++ test/Index/skip-parsed-bodies/compile_commands.json @@ -1,22 +1,23 @@ [ { - "directory": ".", + "directory": "WORKDIR", "command": "/usr/bin/clang++ -fsyntax-only -fno-ms-compatibility -fno-delayed-template-parsing t1.cpp", "file": "t1.cpp" }, { - "directory": ".", + "directory": "WORKDIR", "command": "/usr/bin/clang++ -fsyntax-only -fno-ms-compatibility -fno-delayed-template-parsing t2.cpp -DBLAH", "file": "t2.cpp" }, { - "directory": ".", + "directory": "WORKDIR", "command": "/usr/bin/clang++ -fsyntax-only -fno-ms-compatibility -fno-delayed-template-parsing t3.cpp -DBLAH", "file": "t2.cpp" } ] -// RUN: c-index-test -index-compile-db %s | FileCheck %s +// RUN: sed "s|WORKDIR|%S|" < %s > %T/compile_commands.json +// RUN: c-index-test -index-compile-db %T/compile_commands.json %S/t1.cpp %S/t2.cpp | FileCheck %s // CHECK: [startedTranslationUnit] // CHECK-NEXT: [enteredMainFile]: t1.cpp Index: tools/c-index-test/c-index-test.c =================================================================== --- tools/c-index-test/c-index-test.c +++ tools/c-index-test/c-index-test.c @@ -3612,6 +3612,7 @@ static int index_compile_db(int argc, const char **argv) { const char *check_prefix; + const char *database; CXIndex Idx; CXIndexAction idxAction; int errorCode = 0; @@ -3629,6 +3630,9 @@ fprintf(stderr, "no compilation database\n"); return -1; } + database = argv[0]; + argv++; + --argc; if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, /* displayDiagnostics=*/1))) { @@ -3638,7 +3642,6 @@ idxAction = clang_IndexAction_create(Idx); { - const char *database = argv[0]; CXCompilationDatabase db = 0; CXCompileCommands CCmds = 0; CXCompileCommand CCmd; @@ -3673,48 +3676,44 @@ goto cdb_end; } - CCmds = clang_CompilationDatabase_getAllCompileCommands(db); - if (!CCmds) { - printf("compilation db is empty\n"); - errorCode = -1; - goto cdb_end; - } - - numCmds = clang_CompileCommands_getSize(CCmds); - - if (numCmds==0) { - fprintf(stderr, "should not get an empty compileCommand set\n"); - errorCode = -1; - goto cdb_end; - } - - for (i=0; i 0 && errorCode == 0; ++argv, --argc) { + CCmds = clang_CompilationDatabase_getCompileCommands(db, argv[0]); + if (!CCmds) { + printf("no compile commands for %s\n", argv[0]); errorCode = -1; - goto cdb_end; + goto file_end; } - clang_disposeString(wd); + numCmds = clang_CompileCommands_getSize(CCmds); + for (i = 0; i < numCmds && errorCode == 0; ++i) { + CCmd = clang_CompileCommands_getCommand(CCmds, i); - numArgs = clang_CompileCommand_getNumArgs(CCmd); - if (numArgs > MAX_COMPILE_ARGS){ - fprintf(stderr, "got more compile arguments than maximum\n"); - errorCode = -1; - goto cdb_end; - } - for (a=0; a MAX_COMPILE_ARGS){ + fprintf(stderr, "got more compile arguments than maximum\n"); + errorCode = -1; + goto file_end; + } + for (a=0; a] \n" " c-index-test -index-file-full [-check-prefix=] \n" " c-index-test -index-tu [-check-prefix=] \n" - " c-index-test -index-compile-db [-check-prefix=] \n" + " c-index-test -index-compile-db [-check-prefix=] \n" " c-index-test -test-file-scan " "[FileCheck prefix]\n"); fprintf(stderr, Index: tools/libclang/CXCompilationDatabase.cpp =================================================================== --- tools/libclang/CXCompilationDatabase.cpp +++ tools/libclang/CXCompilationDatabase.cpp @@ -57,12 +57,8 @@ CXCompileCommands clang_CompilationDatabase_getAllCompileCommands(CXCompilationDatabase CDb) { - if (CompilationDatabase *db = static_cast(CDb)) { - std::vector CCmd(db->getAllCompileCommands()); - if (!CCmd.empty()) - return new AllocatedCXCompileCommands(std::move(CCmd)); - } - + // Left here for backwards compatibility. + // Enumerating the commands is not supported anymore. return nullptr; } Index: unittests/Tooling/CompilationDatabaseTest.cpp =================================================================== --- unittests/Tooling/CompilationDatabaseTest.cpp +++ unittests/Tooling/CompilationDatabaseTest.cpp @@ -47,120 +47,6 @@ expectFailure("[{\"output\":[]}]", "Expected strings as value."); } -static std::vector getAllFiles(StringRef JSONDatabase, - std::string &ErrorMessage, - JSONCommandLineSyntax Syntax) { - std::unique_ptr Database( - JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage, - Syntax)); - if (!Database) { - ADD_FAILURE() << ErrorMessage; - return std::vector(); - } - return Database->getAllFiles(); -} - -static std::vector -getAllCompileCommands(JSONCommandLineSyntax Syntax, StringRef JSONDatabase, - std::string &ErrorMessage) { - std::unique_ptr Database( - JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage, - Syntax)); - if (!Database) { - ADD_FAILURE() << ErrorMessage; - return std::vector(); - } - return Database->getAllCompileCommands(); -} - -TEST(JSONCompilationDatabase, GetAllFiles) { - std::string ErrorMessage; - EXPECT_EQ(std::vector(), - getAllFiles("[]", ErrorMessage, JSONCommandLineSyntax::Gnu)) - << ErrorMessage; - - std::vector expected_files; - SmallString<16> PathStorage; - llvm::sys::path::native("//net/dir/file1", PathStorage); - expected_files.push_back(PathStorage.str()); - llvm::sys::path::native("//net/dir/file2", PathStorage); - expected_files.push_back(PathStorage.str()); - EXPECT_EQ(expected_files, - getAllFiles("[{\"directory\":\"//net/dir\"," - "\"command\":\"command\"," - "\"file\":\"file1\"}," - " {\"directory\":\"//net/dir\"," - "\"command\":\"command\"," - "\"file\":\"file2\"}]", - ErrorMessage, JSONCommandLineSyntax::Gnu)) - << ErrorMessage; -} - -TEST(JSONCompilationDatabase, GetAllCompileCommands) { - std::string ErrorMessage; - EXPECT_EQ( - 0u, getAllCompileCommands(JSONCommandLineSyntax::Gnu, "[]", ErrorMessage) - .size()) - << ErrorMessage; - - StringRef Directory1("//net/dir1"); - StringRef FileName1("file1"); - StringRef Command1("command1"); - StringRef Output1("file1.o"); - StringRef Directory2("//net/dir2"); - StringRef FileName2("file2"); - StringRef Command2("command2"); - StringRef Output2(""); - - std::vector Commands = getAllCompileCommands( - JSONCommandLineSyntax::Gnu, - ("[{\"directory\":\"" + Directory1 + "\"," + "\"command\":\"" + Command1 + - "\"," - "\"file\":\"" + - FileName1 + "\", \"output\":\"" + - Output1 + "\"}," - " {\"directory\":\"" + - Directory2 + "\"," + "\"command\":\"" + Command2 + "\"," - "\"file\":\"" + - FileName2 + "\"}]") - .str(), - ErrorMessage); - EXPECT_EQ(2U, Commands.size()) << ErrorMessage; - EXPECT_EQ(Directory1, Commands[0].Directory) << ErrorMessage; - EXPECT_EQ(FileName1, Commands[0].Filename) << ErrorMessage; - EXPECT_EQ(Output1, Commands[0].Output) << ErrorMessage; - ASSERT_EQ(1u, Commands[0].CommandLine.size()); - EXPECT_EQ(Command1, Commands[0].CommandLine[0]) << ErrorMessage; - EXPECT_EQ(Directory2, Commands[1].Directory) << ErrorMessage; - EXPECT_EQ(FileName2, Commands[1].Filename) << ErrorMessage; - EXPECT_EQ(Output2, Commands[1].Output) << ErrorMessage; - ASSERT_EQ(1u, Commands[1].CommandLine.size()); - EXPECT_EQ(Command2, Commands[1].CommandLine[0]) << ErrorMessage; - - // Check that order is preserved. - Commands = getAllCompileCommands( - JSONCommandLineSyntax::Gnu, - ("[{\"directory\":\"" + Directory2 + "\"," + "\"command\":\"" + Command2 + - "\"," - "\"file\":\"" + - FileName2 + "\"}," - " {\"directory\":\"" + - Directory1 + "\"," + "\"command\":\"" + Command1 + "\"," - "\"file\":\"" + - FileName1 + "\"}]") - .str(), - ErrorMessage); - EXPECT_EQ(2U, Commands.size()) << ErrorMessage; - EXPECT_EQ(Directory2, Commands[0].Directory) << ErrorMessage; - EXPECT_EQ(FileName2, Commands[0].Filename) << ErrorMessage; - ASSERT_EQ(1u, Commands[0].CommandLine.size()); - EXPECT_EQ(Command2, Commands[0].CommandLine[0]) << ErrorMessage; - EXPECT_EQ(Directory1, Commands[1].Directory) << ErrorMessage; - EXPECT_EQ(FileName1, Commands[1].Filename) << ErrorMessage; - ASSERT_EQ(1u, Commands[1].CommandLine.size()); - EXPECT_EQ(Command1, Commands[1].CommandLine[0]) << ErrorMessage; -} - static CompileCommand findCompileArgsInJsonDatabase(StringRef FileName, StringRef JSONDatabase, std::string &ErrorMessage) { @@ -484,24 +370,6 @@ EXPECT_EQ(ExpectedCommandLine, Result[0].CommandLine); } -TEST(FixedCompilationDatabase, GetAllFiles) { - std::vector CommandLine; - CommandLine.push_back("one"); - CommandLine.push_back("two"); - FixedCompilationDatabase Database(".", CommandLine); - - EXPECT_EQ(0ul, Database.getAllFiles().size()); -} - -TEST(FixedCompilationDatabase, GetAllCompileCommands) { - std::vector CommandLine; - CommandLine.push_back("one"); - CommandLine.push_back("two"); - FixedCompilationDatabase Database(".", CommandLine); - - EXPECT_EQ(0ul, Database.getAllCompileCommands().size()); -} - TEST(ParseFixedCompilationDatabase, ReturnsNullOnEmptyArgumentList) { int Argc = 0; std::string ErrorMsg;