]> granicus.if.org Git - clang/commitdiff
[tooling] In CompileCommand, Expose the 'file' that was associated with the command.
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Fri, 11 Sep 2015 20:43:05 +0000 (20:43 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Fri, 11 Sep 2015 20:43:05 +0000 (20:43 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@247468 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang-c/CXCompilationDatabase.h
include/clang/Tooling/CompilationDatabase.h
include/clang/Tooling/JSONCompilationDatabase.h
lib/Tooling/CompilationDatabase.cpp
lib/Tooling/JSONCompilationDatabase.cpp
tools/libclang/CXCompilationDatabase.cpp
tools/libclang/libclang.exports

index 068a677a95ed440e292c9e626a5ffe6298549cb7..9359abfebfe077c951c2555ae1434b7ba6dcaae2 100644 (file)
@@ -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.
  *
index e5b95af3aef4167e65719da97bccd28039d4477b..08a0ffec9d63adee77c10722914805c4957ba4c2 100644 (file)
@@ -42,12 +42,18 @@ namespace tooling {
 /// \brief Specifies the working directory and command of a compilation.
 struct CompileCommand {
   CompileCommand() {}
-  CompileCommand(Twine Directory, std::vector<std::string> CommandLine)
-      : Directory(Directory.str()), CommandLine(std::move(CommandLine)) {}
+  CompileCommand(Twine Directory, Twine Filename,
+                 std::vector<std::string> 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<std::string> CommandLine;
 
index ec1a3ab29601e531a56241de7f7a1ec0f2ef1b20..0e6c893969da002fdae203ffa1e3cbbfb1ea0b5c 100644 (file)
@@ -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<llvm::yaml::ScalarNode *,
+  typedef std::tuple<llvm::yaml::ScalarNode *,
+                     llvm::yaml::ScalarNode *,
                     std::vector<llvm::yaml::ScalarNode *>> CompileCommandRef;
 
   /// \brief Converts the given array of CompileCommandRefs to CompileCommands.
index c1817b7afcccf421ffbee625dd81fac661e33018..957e40137eacb4a1af505bc0a365eba928526a11 100644 (file)
@@ -299,13 +299,15 @@ FixedCompilationDatabase(Twine Directory, ArrayRef<std::string> CommandLine) {
   std::vector<std::string> 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<CompileCommand>
 FixedCompilationDatabase::getCompileCommands(StringRef FilePath) const {
   std::vector<CompileCommand> Result(CompileCommands);
   Result[0].CommandLine.push_back(FilePath);
+  Result[0].Filename = FilePath;
   return Result;
 }
 
index 3ac6f697e57179cdb32a2cb40321f3059633be66..dd4d7a8f08364e18fbc844ac9bc0f52cfc58cbc3 100644 (file)
@@ -232,8 +232,11 @@ void JSONCompilationDatabase::getCommands(
     std::vector<CompileCommand> &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;
index 1e4a2cd44aabbb29a3fa3664957ae316ce229b8c..82498bf54c749184e6fb8da420d68a05e0840d3c 100644 (file)
@@ -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<CompileCommand *>(CCmd);
+  return cxstring::createRef(cmd->Filename.c_str());
+}
+
 unsigned
 clang_CompileCommand_getNumArgs(CXCompileCommand CCmd)
 {
index 44f3836a1149f22fb0ea6d4e87e465bdcc94be6e..5179b96fc61badd087a9e95444c9c3292c0a174a 100644 (file)
@@ -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