sammccall created this revision. Herald added a subscriber: klimek. In practice, these methods aren't used, and several databases don't implement them. They remain in libclang as a stub for binary compatibility. c-index-tests are modified not to rely on them.
https://reviews.llvm.org/D40439 Files: bindings/python/clang/cindex.py include/clang-c/CXCompilationDatabase.h include/clang/Tooling/CommonOptionsParser.h include/clang/Tooling/CompilationDatabase.h include/clang/Tooling/JSONCompilationDatabase.h lib/Tooling/CommonOptionsParser.cpp lib/Tooling/CompilationDatabase.cpp lib/Tooling/JSONCompilationDatabase.cpp test/Index/skip-parsed-bodies/compile_commands.json tools/c-index-test/c-index-test.c tools/libclang/CXCompilationDatabase.cpp unittests/Tooling/CompilationDatabaseTest.cpp
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<std::string> getAllFiles(StringRef JSONDatabase, - std::string &ErrorMessage, - JSONCommandLineSyntax Syntax) { - std::unique_ptr<CompilationDatabase> Database( - JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage, - Syntax)); - if (!Database) { - ADD_FAILURE() << ErrorMessage; - return std::vector<std::string>(); - } - return Database->getAllFiles(); -} - -static std::vector<CompileCommand> -getAllCompileCommands(JSONCommandLineSyntax Syntax, StringRef JSONDatabase, - std::string &ErrorMessage) { - std::unique_ptr<CompilationDatabase> Database( - JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage, - Syntax)); - if (!Database) { - ADD_FAILURE() << ErrorMessage; - return std::vector<CompileCommand>(); - } - return Database->getAllCompileCommands(); -} - -TEST(JSONCompilationDatabase, GetAllFiles) { - std::string ErrorMessage; - EXPECT_EQ(std::vector<std::string>(), - getAllFiles("[]", ErrorMessage, JSONCommandLineSyntax::Gnu)) - << ErrorMessage; - - std::vector<std::string> 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<CompileCommand> 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<std::string> CommandLine; - CommandLine.push_back("one"); - CommandLine.push_back("two"); - FixedCompilationDatabase Database(".", CommandLine); - - EXPECT_EQ(0ul, Database.getAllFiles().size()); -} - -TEST(FixedCompilationDatabase, GetAllCompileCommands) { - std::vector<std::string> 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; 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<CompilationDatabase *>(CDb)) { - std::vector<CompileCommand> 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: 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,56 +3676,51 @@ 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<numCmds && errorCode == 0; ++i) { - CCmd = clang_CompileCommands_getCommand(CCmds, i); - - wd = clang_CompileCommand_getDirectory(CCmd); - if (chdir(clang_getCString(wd)) != 0) { - printf("Could not chdir to %s\n", clang_getCString(wd)); + for (; argc > 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<numArgs; ++a) { - cxargs[a] = clang_CompileCommand_getArg(CCmd, a); - args[a] = clang_getCString(cxargs[a]); - } + wd = clang_CompileCommand_getDirectory(CCmd); + if (chdir(clang_getCString(wd)) != 0) { + printf("Could not chdir to %s\n", clang_getCString(wd)); + errorCode = -1; + goto file_end; + } + clang_disposeString(wd); + + numArgs = clang_CompileCommand_getNumArgs(CCmd); + if (numArgs > MAX_COMPILE_ARGS){ + fprintf(stderr, "got more compile arguments than maximum\n"); + errorCode = -1; + goto file_end; + } + for (a=0; a<numArgs; ++a) { + cxargs[a] = clang_CompileCommand_getArg(CCmd, a); + args[a] = clang_getCString(cxargs[a]); + } - errorCode = index_compile_args(numArgs, args, idxAction, - /*importedASTs=*/0, check_prefix); + errorCode = index_compile_args(numArgs, args, idxAction, + /*importedASTs=*/0, check_prefix); - for (a=0; a<numArgs; ++a) - clang_disposeString(cxargs[a]); + for (a=0; a<numArgs; ++a) + clang_disposeString(cxargs[a]); + } + file_end: + clang_CompileCommands_dispose(CCmds); } } else { printf("database loading failed with error code %d.\n", ec); errorCode = -1; } cdb_end: - clang_CompileCommands_dispose(CCmds); clang_CompilationDatabase_dispose(db); free(tmp); @@ -4405,7 +4403,7 @@ " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n" " c-index-test -index-file-full [-check-prefix=<FileCheck prefix>] <compiler arguments>\n" " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n" - " c-index-test -index-compile-db [-check-prefix=<FileCheck prefix>] <compilation database>\n" + " c-index-test -index-compile-db [-check-prefix=<FileCheck prefix>] <compilation database> <files>\n" " c-index-test -test-file-scan <AST file> <source file> " "[FileCheck prefix]\n"); fprintf(stderr, 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: lib/Tooling/JSONCompilationDatabase.cpp =================================================================== --- lib/Tooling/JSONCompilationDatabase.cpp +++ lib/Tooling/JSONCompilationDatabase.cpp @@ -192,6 +192,20 @@ return Database; } +static std::vector<std::string> +nodeToCommandLine(JSONCommandLineSyntax Syntax, + const std::vector<llvm::yaml::ScalarNode *> &Nodes) { + SmallString<1024> Storage; + if (Nodes.size() == 1) { + return unescapeCommandLine(Syntax, Nodes[0]->getValue(Storage)); + } + std::vector<std::string> Arguments; + for (auto *Node : Nodes) { + Arguments.push_back(Node->getValue(Storage)); + } + return Arguments; +} + std::vector<CompileCommand> JSONCompilationDatabase::getCompileCommands(StringRef FilePath) const { SmallString<128> NativeFilePath; @@ -207,49 +221,7 @@ if (CommandsRefI == IndexByFile.end()) return std::vector<CompileCommand>(); std::vector<CompileCommand> Commands; - getCommands(CommandsRefI->getValue(), Commands); - return Commands; -} - -std::vector<std::string> -JSONCompilationDatabase::getAllFiles() const { - std::vector<std::string> Result; - - llvm::StringMap< std::vector<CompileCommandRef> >::const_iterator - CommandsRefI = IndexByFile.begin(); - const llvm::StringMap< std::vector<CompileCommandRef> >::const_iterator - CommandsRefEnd = IndexByFile.end(); - for (; CommandsRefI != CommandsRefEnd; ++CommandsRefI) { - Result.push_back(CommandsRefI->first().str()); - } - - return Result; -} - -std::vector<CompileCommand> -JSONCompilationDatabase::getAllCompileCommands() const { - std::vector<CompileCommand> Commands; - getCommands(AllCommands, Commands); - return Commands; -} - -static std::vector<std::string> -nodeToCommandLine(JSONCommandLineSyntax Syntax, - const std::vector<llvm::yaml::ScalarNode *> &Nodes) { - SmallString<1024> Storage; - if (Nodes.size() == 1) { - return unescapeCommandLine(Syntax, Nodes[0]->getValue(Storage)); - } - std::vector<std::string> Arguments; - for (auto *Node : Nodes) { - Arguments.push_back(Node->getValue(Storage)); - } - return Arguments; -} - -void JSONCompilationDatabase::getCommands( - ArrayRef<CompileCommandRef> CommandsRef, - std::vector<CompileCommand> &Commands) const { + ArrayRef<CompileCommandRef> 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: lib/Tooling/CompilationDatabase.cpp =================================================================== --- lib/Tooling/CompilationDatabase.cpp +++ lib/Tooling/CompilationDatabase.cpp @@ -112,15 +112,6 @@ return DB; } -std::vector<CompileCommand> CompilationDatabase::getAllCompileCommands() const { - std::vector<CompileCommand> 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/CommonOptionsParser.cpp =================================================================== --- lib/Tooling/CommonOptionsParser.cpp +++ lib/Tooling/CommonOptionsParser.cpp @@ -58,23 +58,9 @@ Adjusters.push_back(std::move(Adjuster)); } -std::vector<CompileCommand> ArgumentsAdjustingCompilations::getCompileCommands( - StringRef FilePath) const { - return adjustCommands(Compilations->getCompileCommands(FilePath)); -} - -std::vector<std::string> -ArgumentsAdjustingCompilations::getAllFiles() const { - return Compilations->getAllFiles(); -} - std::vector<CompileCommand> -ArgumentsAdjustingCompilations::getAllCompileCommands() const { - return adjustCommands(Compilations->getAllCompileCommands()); -} - -std::vector<CompileCommand> ArgumentsAdjustingCompilations::adjustCommands( - std::vector<CompileCommand> 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: include/clang/Tooling/JSONCompilationDatabase.h =================================================================== --- include/clang/Tooling/JSONCompilationDatabase.h +++ include/clang/Tooling/JSONCompilationDatabase.h @@ -81,15 +81,6 @@ std::vector<CompileCommand> 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<std::string> getAllFiles() const override; - - /// \brief Returns all compile commands for all the files in the compilation - /// database. - std::vector<CompileCommand> getAllCompileCommands() const override; - private: /// \brief Constructs a JSON compilation database on a memory buffer. JSONCompilationDatabase(std::unique_ptr<llvm::MemoryBuffer> Database, @@ -115,10 +106,6 @@ std::vector<llvm::yaml::ScalarNode *>, llvm::yaml::ScalarNode *> CompileCommandRef; - /// \brief Converts the given array of CompileCommandRefs to CompileCommands. - void getCommands(ArrayRef<CompileCommandRef> CommandsRef, - std::vector<CompileCommand> &Commands) const; - // Maps file paths to the compile command lines for that file. llvm::StringMap<std::vector<CompileCommandRef>> IndexByFile; 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<CompilationDatabase> loadFromDirectory(StringRef BuildDirectory, std::string &ErrorMessage); @@ -103,8 +100,7 @@ static std::unique_ptr<CompilationDatabase> 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<CompileCommand> 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<std::string> 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<CompileCommand> getAllCompileCommands() const; }; /// \brief Interface for compilation database plugins. Index: include/clang/Tooling/CommonOptionsParser.h =================================================================== --- include/clang/Tooling/CommonOptionsParser.h +++ include/clang/Tooling/CommonOptionsParser.h @@ -139,16 +139,9 @@ std::vector<CompileCommand> getCompileCommands(StringRef FilePath) const override; - std::vector<std::string> getAllFiles() const override; - - std::vector<CompileCommand> getAllCompileCommands() const override; - private: std::unique_ptr<CompilationDatabase> Compilations; std::vector<ArgumentsAdjuster> Adjusters; - - std::vector<CompileCommand> - adjustCommands(std::vector<CompileCommand> Commands) const; }; } // namespace tooling 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: 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,
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits