From 4963bf31d61a4293294de0f008226ed5d21cafd0 Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Fri, 11 Sep 2015 20:43:05 +0000 Subject: [PATCH] [tooling] In CompileCommand, Expose the 'file' that was associated with the command. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@247468 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang-c/CXCompilationDatabase.h | 6 ++++++ include/clang/Tooling/CompilationDatabase.h | 10 ++++++++-- include/clang/Tooling/JSONCompilationDatabase.h | 5 +++-- lib/Tooling/CompilationDatabase.cpp | 4 +++- lib/Tooling/JSONCompilationDatabase.cpp | 9 ++++++--- tools/libclang/CXCompilationDatabase.cpp | 10 ++++++++++ tools/libclang/libclang.exports | 1 + 7 files changed, 37 insertions(+), 8 deletions(-) diff --git a/include/clang-c/CXCompilationDatabase.h b/include/clang-c/CXCompilationDatabase.h index 068a677a95..9359abfebf 100644 --- a/include/clang-c/CXCompilationDatabase.h +++ b/include/clang-c/CXCompilationDatabase.h @@ -125,6 +125,12 @@ clang_CompileCommands_getCommand(CXCompileCommands, unsigned I); CINDEX_LINKAGE CXString clang_CompileCommand_getDirectory(CXCompileCommand); +/** + * \brief Get the filename associated with the CompileCommand. + */ +CINDEX_LINKAGE CXString +clang_CompileCommand_getFilename(CXCompileCommand); + /** * \brief Get the number of arguments in the compiler invocation. * diff --git a/include/clang/Tooling/CompilationDatabase.h b/include/clang/Tooling/CompilationDatabase.h index e5b95af3ae..08a0ffec9d 100644 --- a/include/clang/Tooling/CompilationDatabase.h +++ b/include/clang/Tooling/CompilationDatabase.h @@ -42,12 +42,18 @@ namespace tooling { /// \brief Specifies the working directory and command of a compilation. struct CompileCommand { CompileCommand() {} - CompileCommand(Twine Directory, std::vector CommandLine) - : Directory(Directory.str()), CommandLine(std::move(CommandLine)) {} + CompileCommand(Twine Directory, Twine Filename, + std::vector CommandLine) + : Directory(Directory.str()), + Filename(Filename.str()), + CommandLine(std::move(CommandLine)) {} /// \brief The working directory the command was executed from. std::string Directory; + /// The source file associated with the command. + std::string Filename; + /// \brief The command line that was executed. std::vector CommandLine; diff --git a/include/clang/Tooling/JSONCompilationDatabase.h b/include/clang/Tooling/JSONCompilationDatabase.h index ec1a3ab296..0e6c893969 100644 --- a/include/clang/Tooling/JSONCompilationDatabase.h +++ b/include/clang/Tooling/JSONCompilationDatabase.h @@ -99,13 +99,14 @@ private: /// failed. bool parse(std::string &ErrorMessage); - // Tuple (directory, commandline) where 'commandline' points to the + // Tuple (directory, filename, commandline) where 'commandline' points to the // corresponding scalar nodes in the YAML stream. // If the command line contains a single argument, it is a shell-escaped // command line. // Otherwise, each entry in the command line vector is a literal // argument to the compiler. - typedef std::pair> CompileCommandRef; /// \brief Converts the given array of CompileCommandRefs to CompileCommands. diff --git a/lib/Tooling/CompilationDatabase.cpp b/lib/Tooling/CompilationDatabase.cpp index c1817b7afc..957e40137e 100644 --- a/lib/Tooling/CompilationDatabase.cpp +++ b/lib/Tooling/CompilationDatabase.cpp @@ -299,13 +299,15 @@ FixedCompilationDatabase(Twine Directory, ArrayRef CommandLine) { std::vector ToolCommandLine(1, "clang-tool"); ToolCommandLine.insert(ToolCommandLine.end(), CommandLine.begin(), CommandLine.end()); - CompileCommands.emplace_back(Directory, std::move(ToolCommandLine)); + CompileCommands.emplace_back(Directory, StringRef(), + std::move(ToolCommandLine)); } std::vector FixedCompilationDatabase::getCompileCommands(StringRef FilePath) const { std::vector Result(CompileCommands); Result[0].CommandLine.push_back(FilePath); + Result[0].Filename = FilePath; return Result; } diff --git a/lib/Tooling/JSONCompilationDatabase.cpp b/lib/Tooling/JSONCompilationDatabase.cpp index 3ac6f697e5..dd4d7a8f08 100644 --- a/lib/Tooling/JSONCompilationDatabase.cpp +++ b/lib/Tooling/JSONCompilationDatabase.cpp @@ -232,8 +232,11 @@ void JSONCompilationDatabase::getCommands( std::vector &Commands) const { for (int I = 0, E = CommandsRef.size(); I != E; ++I) { SmallString<8> DirectoryStorage; - Commands.emplace_back(CommandsRef[I].first->getValue(DirectoryStorage), - nodeToCommandLine(CommandsRef[I].second)); + SmallString<32> FilenameStorage; + Commands.emplace_back( + std::get<0>(CommandsRef[I])->getValue(DirectoryStorage), + std::get<1>(CommandsRef[I])->getValue(FilenameStorage), + nodeToCommandLine(std::get<2>(CommandsRef[I]))); } } @@ -335,7 +338,7 @@ bool JSONCompilationDatabase::parse(std::string &ErrorMessage) { llvm::sys::path::native(FileName, NativeFilePath); } IndexByFile[NativeFilePath].push_back( - CompileCommandRef(Directory, *Command)); + CompileCommandRef(Directory, File, *Command)); MatchTrie.insert(NativeFilePath); } return true; diff --git a/tools/libclang/CXCompilationDatabase.cpp b/tools/libclang/CXCompilationDatabase.cpp index 1e4a2cd44a..82498bf54c 100644 --- a/tools/libclang/CXCompilationDatabase.cpp +++ b/tools/libclang/CXCompilationDatabase.cpp @@ -111,6 +111,16 @@ clang_CompileCommand_getDirectory(CXCompileCommand CCmd) return cxstring::createRef(cmd->Directory.c_str()); } +CXString +clang_CompileCommand_getFilename(CXCompileCommand CCmd) +{ + if (!CCmd) + return cxstring::createNull(); + + CompileCommand *cmd = static_cast(CCmd); + return cxstring::createRef(cmd->Filename.c_str()); +} + unsigned clang_CompileCommand_getNumArgs(CXCompileCommand CCmd) { diff --git a/tools/libclang/libclang.exports b/tools/libclang/libclang.exports index 44f3836a11..5179b96fc6 100644 --- a/tools/libclang/libclang.exports +++ b/tools/libclang/libclang.exports @@ -297,6 +297,7 @@ clang_CompileCommands_dispose clang_CompileCommands_getSize clang_CompileCommands_getCommand clang_CompileCommand_getDirectory +clang_CompileCommand_getFilename clang_CompileCommand_getMappedSourceContent clang_CompileCommand_getMappedSourcePath clang_CompileCommand_getNumArgs -- 2.40.0