|
| 1 | +//===- examples/Tooling/ClangCheck.cpp - Clang check tool -----------------===// |
| 2 | +// |
| 3 | +// The LLVM Compiler Infrastructure |
| 4 | +// |
| 5 | +// This file is distributed under the University of Illinois Open Source |
| 6 | +// License. See LICENSE.TXT for details. |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | +// |
| 10 | +// This file implements a clang-check tool that runs the |
| 11 | +// clang::SyntaxOnlyAction over a number of translation units. |
| 12 | +// |
| 13 | +// Usage: |
| 14 | +// clang-check <cmake-output-dir> <file1> <file2> ... |
| 15 | +// |
| 16 | +// Where <cmake-output-dir> is a CMake build directory in which a file named |
| 17 | +// compile_commands.json exists (enable -DCMAKE_EXPORT_COMPILE_COMMANDS in |
| 18 | +// CMake to get this output). |
| 19 | +// |
| 20 | +// <file1> ... specify the paths of files in the CMake source tree. This path |
| 21 | +// is looked up in the compile command database. If the path of a file is |
| 22 | +// absolute, it needs to point into CMake's source tree. If the path is |
| 23 | +// relative, the current working directory needs to be in the CMake source |
| 24 | +// tree and the file must be in a subdirectory of the current working |
| 25 | +// directory. "./" prefixes in the relative files will be automatically |
| 26 | +// removed, but the rest of a relative path must be a suffix of a path in |
| 27 | +// the compile command line database. |
| 28 | +// |
| 29 | +// For example, to use clang-check on all files in a subtree of the source |
| 30 | +// tree, use: |
| 31 | +// /path/to/cmake/sources $ find . -name '*.cpp' \ |
| 32 | +// |xargs clang-check /path/to/cmake/build |
| 33 | +// |
| 34 | +//===----------------------------------------------------------------------===// |
| 35 | + |
| 36 | +#include "clang/Frontend/FrontendActions.h" |
| 37 | +#include "clang/Tooling/Tooling.h" |
| 38 | +#include "llvm/ADT/OwningPtr.h" |
| 39 | +#include "llvm/ADT/Twine.h" |
| 40 | +#include "llvm/Support/MemoryBuffer.h" |
| 41 | +#include "llvm/Support/Path.h" |
| 42 | +#include "llvm/Support/raw_ostream.h" |
| 43 | +#include "llvm/Support/system_error.h" |
| 44 | + |
| 45 | +/// \brief Returns the absolute path of 'File', by prepending it with |
| 46 | +/// 'BaseDirectory' if 'File' is not absolute. Otherwise returns 'File'. |
| 47 | +/// If 'File' starts with "./", the returned path will not contain the "./". |
| 48 | +/// Otherwise, the returned path will contain the literal path-concatenation of |
| 49 | +/// 'BaseDirectory' and 'File'. |
| 50 | +/// |
| 51 | +/// \param File Either an absolute or relative path. |
| 52 | +/// \param BaseDirectory An absolute path. |
| 53 | +/// |
| 54 | +/// FIXME: Put this somewhere where it is more generally available. |
| 55 | +static std::string GetAbsolutePath( |
| 56 | + llvm::StringRef File, llvm::StringRef BaseDirectory) { |
| 57 | + assert(llvm::sys::path::is_absolute(BaseDirectory)); |
| 58 | + if (llvm::sys::path::is_absolute(File)) { |
| 59 | + return File; |
| 60 | + } |
| 61 | + llvm::StringRef RelativePath(File); |
| 62 | + if (RelativePath.startswith("./")) { |
| 63 | + RelativePath = RelativePath.substr(strlen("./")); |
| 64 | + } |
| 65 | + llvm::SmallString<1024> AbsolutePath(BaseDirectory); |
| 66 | + llvm::sys::path::append(AbsolutePath, RelativePath); |
| 67 | + return AbsolutePath.str(); |
| 68 | +} |
| 69 | + |
| 70 | +int main(int argc, char **argv) { |
| 71 | + if (argc < 3) { |
| 72 | + llvm::outs() << "Usage: " << argv[0] << " <cmake-output-dir> " |
| 73 | + << "<file1> <file2> ...\n"; |
| 74 | + return 1; |
| 75 | + } |
| 76 | + // FIXME: We should pull how to find the database into the Tooling package. |
| 77 | + llvm::OwningPtr<llvm::MemoryBuffer> JsonDatabase; |
| 78 | + llvm::SmallString<1024> JsonDatabasePath(argv[1]); |
| 79 | + llvm::sys::path::append(JsonDatabasePath, "compile_commands.json"); |
| 80 | + llvm::error_code Result = |
| 81 | + llvm::MemoryBuffer::getFile(JsonDatabasePath, JsonDatabase); |
| 82 | + if (Result != 0) { |
| 83 | + llvm::outs() << "Error while opening JSON database: " << Result.message() |
| 84 | + << "\n"; |
| 85 | + return 1; |
| 86 | + } |
| 87 | + llvm::StringRef BaseDirectory(::getenv("PWD")); |
| 88 | + for (int I = 2; I < argc; ++I) { |
| 89 | + llvm::SmallString<1024> File(GetAbsolutePath(argv[I], BaseDirectory)); |
| 90 | + llvm::outs() << "Processing " << File << ".\n"; |
| 91 | + std::string ErrorMessage; |
| 92 | + clang::tooling::CompileCommand LookupResult = |
| 93 | + clang::tooling::FindCompileArgsInJsonDatabase( |
| 94 | + File.str(), JsonDatabase->getBuffer(), ErrorMessage); |
| 95 | + if (!LookupResult.CommandLine.empty()) { |
| 96 | + if (!clang::tooling::RunToolWithFlags( |
| 97 | + new clang::SyntaxOnlyAction, |
| 98 | + LookupResult.CommandLine.size(), |
| 99 | + clang::tooling::CommandLineToArgv( |
| 100 | + &LookupResult.CommandLine).data())) { |
| 101 | + llvm::outs() << "Error while processing " << File << ".\n"; |
| 102 | + } |
| 103 | + } else { |
| 104 | + llvm::outs() << "Skipping " << File << ". Command line not found.\n"; |
| 105 | + } |
| 106 | + } |
| 107 | + return 0; |
| 108 | +} |
0 commit comments