[cfe-commits] r159998 - in /cfe/trunk: include/clang/Tooling/CompilationDatabase.h lib/Tooling/CompilationDatabase.cpp test/Tooling/clang-check-autodetect-dir.cpp tools/clang-check/ClangCheck.cpp

Arnaud A. de Grandmaison arnaud.adegm at gmail.com
Tue Jul 10 09:56:35 PDT 2012


Author: aadg
Date: Tue Jul 10 11:56:35 2012
New Revision: 159998

URL: http://llvm.org/viewvc/llvm-project?rev=159998&view=rev
Log:
Adds support for auto-detection of compilation databases, looking in a directory and all its parents.

Added:
    cfe/trunk/test/Tooling/clang-check-autodetect-dir.cpp
Modified:
    cfe/trunk/include/clang/Tooling/CompilationDatabase.h
    cfe/trunk/lib/Tooling/CompilationDatabase.cpp
    cfe/trunk/tools/clang-check/ClangCheck.cpp

Modified: cfe/trunk/include/clang/Tooling/CompilationDatabase.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/CompilationDatabase.h?rev=159998&r1=159997&r2=159998&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/CompilationDatabase.h (original)
+++ cfe/trunk/include/clang/Tooling/CompilationDatabase.h Tue Jul 10 11:56:35 2012
@@ -83,11 +83,18 @@
 
   /// \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.
   ///

Modified: cfe/trunk/lib/Tooling/CompilationDatabase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/CompilationDatabase.cpp?rev=159998&r1=159997&r2=159998&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/CompilationDatabase.cpp (original)
+++ cfe/trunk/lib/Tooling/CompilationDatabase.cpp Tue Jul 10 11:56:35 2012
@@ -122,23 +122,47 @@
   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,

Added: cfe/trunk/test/Tooling/clang-check-autodetect-dir.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Tooling/clang-check-autodetect-dir.cpp?rev=159998&view=auto
==============================================================================
--- cfe/trunk/test/Tooling/clang-check-autodetect-dir.cpp (added)
+++ cfe/trunk/test/Tooling/clang-check-autodetect-dir.cpp Tue Jul 10 11:56:35 2012
@@ -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

Modified: cfe/trunk/tools/clang-check/ClangCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-check/ClangCheck.cpp?rev=159998&r1=159997&r2=159998&view=diff
==============================================================================
--- cfe/trunk/tools/clang-check/ClangCheck.cpp (original)
+++ cfe/trunk/tools/clang-check/ClangCheck.cpp Tue Jul 10 11:56:35 2012
@@ -58,8 +58,9 @@
   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));





More information about the cfe-commits mailing list