diff options
| author | Manuel Klimek <klimek@google.com> | 2012-07-10 13:10:51 +0000 |
|---|---|---|
| committer | Manuel Klimek <klimek@google.com> | 2012-07-10 13:10:51 +0000 |
| commit | 65fd0e1fd3c7fe24bb09e654d228c9584c2fa6ff (patch) | |
| tree | c6a6969bd885beb99cae9776826065fdfda3f8bb /clang/lib/Tooling | |
| parent | 1dc44dcedd0bc93e267bcc5357fc2925942ca20e (diff) | |
| download | bcm5719-llvm-65fd0e1fd3c7fe24bb09e654d228c9584c2fa6ff.tar.gz bcm5719-llvm-65fd0e1fd3c7fe24bb09e654d228c9584c2fa6ff.zip | |
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.
llvm-svn: 159990
Diffstat (limited to 'clang/lib/Tooling')
| -rw-r--r-- | clang/lib/Tooling/CompilationDatabase.cpp | 19 | ||||
| -rw-r--r-- | clang/lib/Tooling/Tooling.cpp | 27 |
2 files changed, 25 insertions, 21 deletions
diff --git a/clang/lib/Tooling/CompilationDatabase.cpp b/clang/lib/Tooling/CompilationDatabase.cpp index 227fa82926c..8e911123bc8 100644 --- a/clang/lib/Tooling/CompilationDatabase.cpp +++ b/clang/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 - diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp index abac182b215..5d41172a919 100644 --- a/clang/lib/Tooling/Tooling.cpp +++ b/clang/lib/Tooling/Tooling.cpp @@ -115,20 +115,13 @@ bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code, 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 @@ ClangTool::ClangTool(const CompilationDatabase &Compilations, 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()); |

