From 8fa2fb859a4cb8e67d9763225281d9b0aa9cb59f Mon Sep 17 00:00:00 2001 From: Manuel Klimek Date: Tue, 10 Jul 2012 13:10:51 +0000 Subject: 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@159990 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Tooling/CompilationDatabase.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'lib/Tooling/CompilationDatabase.cpp') diff --git a/lib/Tooling/CompilationDatabase.cpp b/lib/Tooling/CompilationDatabase.cpp index 227fa82926..8e911123bc 100644 --- a/lib/Tooling/CompilationDatabase.cpp +++ b/lib/Tooling/CompilationDatabase.cpp @@ -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 @@ 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); + 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 @@ bool JSONCompilationDatabase::parse(std::string &ErrorMessage) { } // end namespace tooling } // end namespace clang - -- cgit v1.2.3-18-g5258