]> granicus.if.org Git - clang/commitdiff
Adds support for auto-detection of compilation databases, looking in a directory...
authorArnaud A. de Grandmaison <arnaud.adegm@gmail.com>
Tue, 10 Jul 2012 16:56:35 +0000 (16:56 +0000)
committerArnaud A. de Grandmaison <arnaud.adegm@gmail.com>
Tue, 10 Jul 2012 16:56:35 +0000 (16:56 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@159998 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Tooling/CompilationDatabase.h
lib/Tooling/CompilationDatabase.cpp
test/Tooling/clang-check-autodetect-dir.cpp [new file with mode: 0644]
tools/clang-check/ClangCheck.cpp

index 0dec6f8f87deef208bc4d1518eeaa40d67636c67..143c65e313a45968b709a3ad17beea8bcdd21835 100644 (file)
@@ -83,11 +83,18 @@ public:
 
   /// \brief Tries to detect a compilation database location and load it.
   ///
-  /// Looks for a compilation database in all parent paths by calling
-  /// loadFromDirectory.
+  /// Looks for a compilation database in all parent paths of file 'SourceFile'
+  /// by calling loadFromDirectory.
   static CompilationDatabase *autoDetectFromSource(StringRef SourceFile,
                                                    std::string &ErrorMessage);
 
+  /// \brief Tries to detect a compilation database location and load it.
+  ///
+  /// Looks for a compilation database in directory 'SourceDir' and all
+  /// its parent paths by calling loadFromDirectory.
+  static CompilationDatabase *autoDetectFromDirectory(StringRef SourceDir,
+                                                      std::string &ErrorMessage);
+
   /// \brief Returns all compile commands in which the specified file was
   /// compiled.
   ///
index 8e911123bc800e45cb68c24d1cea9f64223f1d84..a06343ddab6ee9994aa41ef136a3a6af5c9a21c3 100644 (file)
@@ -122,23 +122,47 @@ CompilationDatabase::loadFromDirectory(StringRef BuildDirectory,
   return Database.take();
 }
 
-CompilationDatabase *
-CompilationDatabase::autoDetectFromSource(StringRef SourceFile,
-                                          std::string &ErrorMessage) {
-  llvm::SmallString<1024> AbsolutePath(getAbsolutePath(SourceFile));
-  StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
+static CompilationDatabase *
+findCompilationDatabaseFromDirectory(StringRef Directory) {
   while (!Directory.empty()) {
     std::string LoadErrorMessage;
-    if (CompilationDatabase *DB = loadFromDirectory(Directory,
-                                                    LoadErrorMessage))
+
+    if (CompilationDatabase *DB =
+           CompilationDatabase::loadFromDirectory(Directory, LoadErrorMessage))
       return DB;
+
     Directory = llvm::sys::path::parent_path(Directory);
   }
-  ErrorMessage = ("Could not auto-detect compilation database for file \"" +
-                  SourceFile + "\"").str();
   return NULL;
 }
 
+CompilationDatabase *
+CompilationDatabase::autoDetectFromSource(StringRef SourceFile,
+                                          std::string &ErrorMessage) {
+  llvm::SmallString<1024> AbsolutePath(getAbsolutePath(SourceFile));
+  StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
+
+  CompilationDatabase *DB = findCompilationDatabaseFromDirectory(Directory);
+
+  if (!DB)
+    ErrorMessage = ("Could not auto-detect compilation database for file \"" +
+                   SourceFile + "\"").str();
+  return DB;
+}
+
+CompilationDatabase *
+CompilationDatabase::autoDetectFromDirectory(StringRef SourceDir,
+                                             std::string &ErrorMessage) {
+  llvm::SmallString<1024> AbsolutePath(getAbsolutePath(SourceDir));
+
+  CompilationDatabase *DB = findCompilationDatabaseFromDirectory(AbsolutePath);
+
+  if (!DB)
+    ErrorMessage = ("Could not auto-detect compilation database from directory \"" +
+                   SourceDir + "\"").str();
+  return DB;
+}
+
 FixedCompilationDatabase *
 FixedCompilationDatabase::loadFromCommandLine(int &Argc,
                                               const char **Argv,
diff --git a/test/Tooling/clang-check-autodetect-dir.cpp b/test/Tooling/clang-check-autodetect-dir.cpp
new file mode 100644 (file)
index 0000000..2c39504
--- /dev/null
@@ -0,0 +1,11 @@
+// RUN: rm -rf %t
+// RUN: mkdir -p %t/abc/def
+// RUN: echo "[{\"directory\":\".\",\"command\":\"clang++ -c %t/test.cpp\",\"file\":\"%t/test.cpp\"}]" | sed -e 's/\\/\\\\/g' > %t/compile_commands.json
+// RUN: cp "%s" "%t/test.cpp"
+// RUN: clang-check -p "%t/abc/def" "%t/test.cpp" 2>&1|FileCheck %s
+// FIXME: Make the above easier.
+
+// CHECK: C++ requires
+invalid;
+
+// REQUIRES: shell
index ef4a3ace0c3a6326b75906c5d59419d316bcd552..d51d90e90652c9da0ed46fc8f9f9c7e33ceaa9c2 100644 (file)
@@ -58,8 +58,9 @@ int main(int argc, const char **argv) {
   if (!Compilations) {
     std::string ErrorMessage;
     if (!BuildPath.empty()) {
-      Compilations.reset(CompilationDatabase::loadFromDirectory(BuildPath,
-                                                                ErrorMessage));
+      Compilations.reset(
+         CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage));
+
     } else {
       Compilations.reset(CompilationDatabase::autoDetectFromSource(
           SourcePaths[0], ErrorMessage));