[cfe-commits] r159990 - in /cfe/trunk: include/clang/Tooling/ lib/Tooling/ test/Tooling/ tools/clang-check/
Manuel Klimek
klimek at google.com
Tue Jul 10 06:10:52 PDT 2012
Author: klimek
Date: Tue Jul 10 08:10:51 2012
New Revision: 159990
URL: http://llvm.org/viewvc/llvm-project?rev=159990&view=rev
Log:
Adds support for auto-detection of compilation databases
from a source file and changes clang-check to make use of this.
This makes clang-check just work on in-tree builds, and allows
easy setup via a symlink per source directory to make clang-check
work without any extra configuration.
Added:
cfe/trunk/test/Tooling/auto-detect-from-source-parent-of-cwd.cpp
cfe/trunk/test/Tooling/auto-detect-from-source-parent.cpp
cfe/trunk/test/Tooling/auto-detect-from-source.cpp
Modified:
cfe/trunk/include/clang/Tooling/CompilationDatabase.h
cfe/trunk/include/clang/Tooling/Tooling.h
cfe/trunk/lib/Tooling/CompilationDatabase.cpp
cfe/trunk/lib/Tooling/Tooling.cpp
cfe/trunk/test/Tooling/clang-check-args.cpp
cfe/trunk/test/Tooling/clang-check-builtin-headers.cpp
cfe/trunk/test/Tooling/clang-check-chdir.cpp
cfe/trunk/test/Tooling/clang-check-pwd.cpp
cfe/trunk/test/Tooling/clang-check.cpp
cfe/trunk/test/Tooling/multi-jobs.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=159990&r1=159989&r2=159990&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/CompilationDatabase.h (original)
+++ cfe/trunk/include/clang/Tooling/CompilationDatabase.h Tue Jul 10 08:10:51 2012
@@ -81,6 +81,13 @@
static CompilationDatabase *loadFromDirectory(StringRef BuildDirectory,
std::string &ErrorMessage);
+ /// \brief Tries to detect a compilation database location and load it.
+ ///
+ /// Looks for a compilation database in all parent paths by calling
+ /// loadFromDirectory.
+ static CompilationDatabase *autoDetectFromSource(StringRef SourceFile,
+ std::string &ErrorMessage);
+
/// \brief Returns all compile commands in which the specified file was
/// compiled.
///
@@ -215,4 +222,3 @@
} // end namespace clang
#endif // LLVM_CLANG_TOOLING_COMPILATION_DATABASE_H
-
Modified: cfe/trunk/include/clang/Tooling/Tooling.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Tooling.h?rev=159990&r1=159989&r2=159990&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/Tooling.h (original)
+++ cfe/trunk/include/clang/Tooling/Tooling.h Tue Jul 10 08:10:51 2012
@@ -232,6 +232,22 @@
return new FrontendActionFactoryAdapter(ConsumerFactory);
}
+/// \brief Returns the absolute path of \c File, by prepending it with
+/// the current directory if \c File is not absolute.
+///
+/// Otherwise returns \c File.
+/// If 'File' starts with "./", the returned path will not contain the "./".
+/// Otherwise, the returned path will contain the literal path-concatenation of
+/// the current directory and \c File.
+///
+/// The difference to llvm::sys::fs::make_absolute is that we prefer
+/// ::getenv("PWD") if available.
+/// FIXME: Make this functionality available from llvm::sys::fs and delete
+/// this function.
+///
+/// \param File Either an absolute or relative path.
+std::string getAbsolutePath(StringRef File);
+
} // end namespace tooling
} // end namespace clang
Modified: cfe/trunk/lib/Tooling/CompilationDatabase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/CompilationDatabase.cpp?rev=159990&r1=159989&r2=159990&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/CompilationDatabase.cpp (original)
+++ cfe/trunk/lib/Tooling/CompilationDatabase.cpp Tue Jul 10 08:10:51 2012
@@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//
#include "clang/Tooling/CompilationDatabase.h"
+#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/YAMLParser.h"
#include "llvm/Support/Path.h"
@@ -121,6 +122,23 @@
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);
+ while (!Directory.empty()) {
+ std::string LoadErrorMessage;
+ if (CompilationDatabase *DB = 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;
+}
+
FixedCompilationDatabase *
FixedCompilationDatabase::loadFromCommandLine(int &Argc,
const char **Argv,
@@ -283,4 +301,3 @@
} // end namespace tooling
} // end namespace clang
-
Modified: cfe/trunk/lib/Tooling/Tooling.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Tooling.cpp?rev=159990&r1=159989&r2=159990&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/Tooling.cpp (original)
+++ cfe/trunk/lib/Tooling/Tooling.cpp Tue Jul 10 08:10:51 2012
@@ -115,20 +115,13 @@
return Invocation.run();
}
-/// \brief Returns the absolute path of 'File', by prepending it with
-/// 'BaseDirectory' if 'File' is not absolute.
-///
-/// Otherwise returns 'File'.
-/// If 'File' starts with "./", the returned path will not contain the "./".
-/// Otherwise, the returned path will contain the literal path-concatenation of
-/// 'BaseDirectory' and 'File'.
-///
-/// \param File Either an absolute or relative path.
-/// \param BaseDirectory An absolute path.
-static std::string getAbsolutePath(
- StringRef File, StringRef BaseDirectory) {
+std::string getAbsolutePath(StringRef File) {
+ llvm::SmallString<1024> BaseDirectory;
+ if (const char *PWD = ::getenv("PWD"))
+ BaseDirectory = PWD;
+ else
+ llvm::sys::fs::current_path(BaseDirectory);
SmallString<1024> PathStorage;
- assert(llvm::sys::path::is_absolute(BaseDirectory));
if (llvm::sys::path::is_absolute(File)) {
llvm::sys::path::native(File, PathStorage);
return PathStorage.str();
@@ -240,14 +233,8 @@
ArrayRef<std::string> SourcePaths)
: Files((FileSystemOptions())),
ArgsAdjuster(new ClangSyntaxOnlyAdjuster()) {
- llvm::SmallString<1024> BaseDirectory;
- if (const char *PWD = ::getenv("PWD"))
- BaseDirectory = PWD;
- else
- llvm::sys::fs::current_path(BaseDirectory);
for (unsigned I = 0, E = SourcePaths.size(); I != E; ++I) {
- llvm::SmallString<1024> File(getAbsolutePath(
- SourcePaths[I], BaseDirectory));
+ llvm::SmallString<1024> File(getAbsolutePath(SourcePaths[I]));
std::vector<CompileCommand> CompileCommandsForFile =
Compilations.getCompileCommands(File.str());
Added: cfe/trunk/test/Tooling/auto-detect-from-source-parent-of-cwd.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Tooling/auto-detect-from-source-parent-of-cwd.cpp?rev=159990&view=auto
==============================================================================
--- cfe/trunk/test/Tooling/auto-detect-from-source-parent-of-cwd.cpp (added)
+++ cfe/trunk/test/Tooling/auto-detect-from-source-parent-of-cwd.cpp Tue Jul 10 08:10:51 2012
@@ -0,0 +1,8 @@
+// RUN: rm -rf %t
+// RUN: mkdir -p %t/abc/def/ijk/qwe
+// RUN: echo "[{\"directory\":\".\",\"command\":\"clang++ -c %t/abc/def/ijk/qwe/test.cpp\",\"file\":\"%t/abc/def/ijk/qwe/test.cpp\"}]" | sed -e 's/\\/\\\\/g' > %t/compile_commands.json
+// RUN: cp "%s" "%t/abc/def/ijk/qwe/test.cpp"
+// RUN: PWD="%t/abc/def" clang-check "ijk/qwe/test.cpp" 2>&1 | FileCheck %s
+
+// CHECK: C++ requires
+invalid;
Added: cfe/trunk/test/Tooling/auto-detect-from-source-parent.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Tooling/auto-detect-from-source-parent.cpp?rev=159990&view=auto
==============================================================================
--- cfe/trunk/test/Tooling/auto-detect-from-source-parent.cpp (added)
+++ cfe/trunk/test/Tooling/auto-detect-from-source-parent.cpp Tue Jul 10 08:10:51 2012
@@ -0,0 +1,8 @@
+// RUN: rm -rf %t
+// RUN: mkdir -p %t/abc/def/ijk/qwe
+// RUN: echo "[{\"directory\":\".\",\"command\":\"clang++ -c %t/abc/def/ijk/qwe/test.cpp\",\"file\":\"%t/abc/def/ijk/qwe/test.cpp\"}]" | sed -e 's/\\/\\\\/g' > %t/compile_commands.json
+// RUN: cp "%s" "%t/abc/def/ijk/qwe/test.cpp"
+// RUN: clang-check "%t/abc/def/ijk/qwe/test.cpp" 2>&1 | FileCheck %s
+
+// CHECK: C++ requires
+invalid;
Added: cfe/trunk/test/Tooling/auto-detect-from-source.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Tooling/auto-detect-from-source.cpp?rev=159990&view=auto
==============================================================================
--- cfe/trunk/test/Tooling/auto-detect-from-source.cpp (added)
+++ cfe/trunk/test/Tooling/auto-detect-from-source.cpp Tue Jul 10 08:10:51 2012
@@ -0,0 +1,8 @@
+// RUN: rm -rf %t
+// RUN: mkdir %t
+// 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 "%t/test.cpp" 2>&1 | FileCheck %s
+
+// CHECK: C++ requires
+invalid;
Modified: cfe/trunk/test/Tooling/clang-check-args.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Tooling/clang-check-args.cpp?rev=159990&r1=159989&r2=159990&view=diff
==============================================================================
--- cfe/trunk/test/Tooling/clang-check-args.cpp (original)
+++ cfe/trunk/test/Tooling/clang-check-args.cpp Tue Jul 10 08:10:51 2012
@@ -1,4 +1,4 @@
-// RUN: clang-check . "%s" -- -c 2>&1 | FileCheck %s
+// RUN: clang-check "%s" -- -c 2>&1 | FileCheck %s
// CHECK: C++ requires
invalid;
Modified: cfe/trunk/test/Tooling/clang-check-builtin-headers.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Tooling/clang-check-builtin-headers.cpp?rev=159990&r1=159989&r2=159990&view=diff
==============================================================================
--- cfe/trunk/test/Tooling/clang-check-builtin-headers.cpp (original)
+++ cfe/trunk/test/Tooling/clang-check-builtin-headers.cpp Tue Jul 10 08:10:51 2012
@@ -3,7 +3,7 @@
// Add a path that doesn't exist as argv[0] for the compile command line:
// RUN: echo '[{"directory":".","command":"/random/tool -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 "%t" "%t/test.cpp" 2>&1|FileCheck %s
+// RUN: clang-check -p "%t" "%t/test.cpp" 2>&1|FileCheck %s
// FIXME: Make the above easier.
#include <stddef.h>
Modified: cfe/trunk/test/Tooling/clang-check-chdir.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Tooling/clang-check-chdir.cpp?rev=159990&r1=159989&r2=159990&view=diff
==============================================================================
--- cfe/trunk/test/Tooling/clang-check-chdir.cpp (original)
+++ cfe/trunk/test/Tooling/clang-check-chdir.cpp Tue Jul 10 08:10:51 2012
@@ -5,7 +5,7 @@
// RUN: echo "[{\"directory\":\"%t\",\"command\":\"clang -c test.cpp -I.\",\"file\":\"%t/test.cpp\"}]" | sed -e 's/\\/\//g' > %t/compile_commands.json
// RUN: cp "%s" "%t/test.cpp"
// RUN: touch "%t/clang-check-test.h"
-// RUN: clang-check "%t" "%t/test.cpp" 2>&1|FileCheck %s
+// RUN: clang-check -p "%t" "%t/test.cpp" 2>&1|FileCheck %s
// FIXME: Make the above easier.
#include "clang-check-test.h"
Modified: cfe/trunk/test/Tooling/clang-check-pwd.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Tooling/clang-check-pwd.cpp?rev=159990&r1=159989&r2=159990&view=diff
==============================================================================
--- cfe/trunk/test/Tooling/clang-check-pwd.cpp (original)
+++ cfe/trunk/test/Tooling/clang-check-pwd.cpp Tue Jul 10 08:10:51 2012
@@ -2,7 +2,7 @@
// RUN: mkdir %t
// 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: PWD="%t" clang-check "%t" "test.cpp" 2>&1|FileCheck %s
+// RUN: PWD="%t" clang-check -p "%t" "test.cpp" 2>&1|FileCheck %s
// FIXME: Make the above easier.
// CHECK: C++ requires
Modified: cfe/trunk/test/Tooling/clang-check.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Tooling/clang-check.cpp?rev=159990&r1=159989&r2=159990&view=diff
==============================================================================
--- cfe/trunk/test/Tooling/clang-check.cpp (original)
+++ cfe/trunk/test/Tooling/clang-check.cpp Tue Jul 10 08:10:51 2012
@@ -2,7 +2,7 @@
// RUN: mkdir %t
// 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 "%t" "%t/test.cpp" 2>&1|FileCheck %s
+// RUN: clang-check -p "%t" "%t/test.cpp" 2>&1|FileCheck %s
// FIXME: Make the above easier.
// CHECK: C++ requires
Modified: cfe/trunk/test/Tooling/multi-jobs.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Tooling/multi-jobs.cpp?rev=159990&r1=159989&r2=159990&view=diff
==============================================================================
--- cfe/trunk/test/Tooling/multi-jobs.cpp (original)
+++ cfe/trunk/test/Tooling/multi-jobs.cpp Tue Jul 10 08:10:51 2012
@@ -1,4 +1,4 @@
-// RUN: clang-check . "%s" -- -no-integrated-as -c 2>&1 | FileCheck %s
+// RUN: clang-check "%s" -- -no-integrated-as -c 2>&1 | FileCheck %s
// CHECK: C++ requires
invalid;
Modified: cfe/trunk/tools/clang-check/ClangCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-check/ClangCheck.cpp?rev=159990&r1=159989&r2=159990&view=diff
==============================================================================
--- cfe/trunk/tools/clang-check/ClangCheck.cpp (original)
+++ cfe/trunk/tools/clang-check/ClangCheck.cpp Tue Jul 10 08:10:51 2012
@@ -42,8 +42,9 @@
using namespace llvm;
cl::opt<std::string> BuildPath(
- cl::Positional,
- cl::desc("<build-path>"));
+ "p",
+ cl::desc("<build-path>"),
+ cl::Optional);
cl::list<std::string> SourcePaths(
cl::Positional,
@@ -56,8 +57,13 @@
cl::ParseCommandLineOptions(argc, argv);
if (!Compilations) {
std::string ErrorMessage;
- Compilations.reset(CompilationDatabase::loadFromDirectory(BuildPath,
- ErrorMessage));
+ if (!BuildPath.empty()) {
+ Compilations.reset(CompilationDatabase::loadFromDirectory(BuildPath,
+ ErrorMessage));
+ } else {
+ Compilations.reset(CompilationDatabase::autoDetectFromSource(
+ SourcePaths[0], ErrorMessage));
+ }
if (!Compilations)
llvm::report_fatal_error(ErrorMessage);
}
More information about the cfe-commits
mailing list