summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIlya Biryukov <ibiryukov@google.com>2018-01-23 12:31:06 +0000
committerIlya Biryukov <ibiryukov@google.com>2018-01-23 12:31:06 +0000
commita67b3663de05b21af4bcbf2ae46c7230e9892c90 (patch)
tree6db9497d77661ecd532cbec3ecfa91dc56808407
parent5da21edb3537af70b8b5a87d1c3bed990742a632 (diff)
downloadbcm5719-llvm-a67b3663de05b21af4bcbf2ae46c7230e9892c90.tar.gz
bcm5719-llvm-a67b3663de05b21af4bcbf2ae46c7230e9892c90.zip
[clang-tidy] Add -vfsoverlay flag
Summary: It allows to remap and override files and directories on disk when running clang-tidy. The intended use case for the flag is running standalone clang-tidy binary for IDE and editor integration. Patch by Vladimir Plyashkun. Reviewers: alexfh, benlangmuir, ilya-biryukov Reviewed By: ilya-biryukov Subscribers: ilya-biryukov, cfe-commits Tags: #clang-tools-extra Differential Revision: https://reviews.llvm.org/D41535 llvm-svn: 323196
-rw-r--r--clang-tools-extra/clang-tidy/ClangTidy.cpp17
-rw-r--r--clang-tools-extra/clang-tidy/ClangTidy.h4
-rw-r--r--clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp41
-rw-r--r--clang-tools-extra/test/clang-tidy/Inputs/vfsoverlay/actual_header.h1
-rw-r--r--clang-tools-extra/test/clang-tidy/Inputs/vfsoverlay/vfsoverlay.yaml12
-rw-r--r--clang-tools-extra/test/clang-tidy/vfsoverlay.cpp8
6 files changed, 74 insertions, 9 deletions
diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp b/clang-tools-extra/clang-tidy/ClangTidy.cpp
index c3ddc458b89..a68111b4a95 100644
--- a/clang-tools-extra/clang-tidy/ClangTidy.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp
@@ -89,8 +89,9 @@ private:
class ErrorReporter {
public:
- ErrorReporter(ClangTidyContext &Context, bool ApplyFixes)
- : Files(FileSystemOptions()), DiagOpts(new DiagnosticOptions()),
+ ErrorReporter(ClangTidyContext &Context, bool ApplyFixes,
+ llvm::IntrusiveRefCntPtr<vfs::FileSystem> BaseFS)
+ : Files(FileSystemOptions(), BaseFS), DiagOpts(new DiagnosticOptions()),
DiagPrinter(new TextDiagnosticPrinter(llvm::outs(), &*DiagOpts)),
Diags(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts,
DiagPrinter),
@@ -474,8 +475,11 @@ ClangTidyOptions::OptionMap getCheckOptions(const ClangTidyOptions &Options) {
void runClangTidy(clang::tidy::ClangTidyContext &Context,
const CompilationDatabase &Compilations,
- ArrayRef<std::string> InputFiles, ProfileData *Profile) {
- ClangTool Tool(Compilations, InputFiles);
+ ArrayRef<std::string> InputFiles,
+ llvm::IntrusiveRefCntPtr<vfs::FileSystem> BaseFS,
+ ProfileData *Profile) {
+ ClangTool Tool(Compilations, InputFiles,
+ std::make_shared<PCHContainerOperations>(), BaseFS);
// Add extra arguments passed by the clang-tidy command-line.
ArgumentsAdjuster PerFileExtraArgumentsInserter =
@@ -546,8 +550,9 @@ void runClangTidy(clang::tidy::ClangTidyContext &Context,
}
void handleErrors(ClangTidyContext &Context, bool Fix,
- unsigned &WarningsAsErrorsCount) {
- ErrorReporter Reporter(Context, Fix);
+ unsigned &WarningsAsErrorsCount,
+ llvm::IntrusiveRefCntPtr<vfs::FileSystem> BaseFS) {
+ ErrorReporter Reporter(Context, Fix, BaseFS);
vfs::FileSystem &FileSystem =
*Reporter.getSourceManager().getFileManager().getVirtualFileSystem();
auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
diff --git a/clang-tools-extra/clang-tidy/ClangTidy.h b/clang-tools-extra/clang-tidy/ClangTidy.h
index 8d4d6c95a5a..7c9ef760fdc 100644
--- a/clang-tools-extra/clang-tidy/ClangTidy.h
+++ b/clang-tools-extra/clang-tidy/ClangTidy.h
@@ -227,6 +227,7 @@ ClangTidyOptions::OptionMap getCheckOptions(const ClangTidyOptions &Options);
void runClangTidy(clang::tidy::ClangTidyContext &Context,
const tooling::CompilationDatabase &Compilations,
ArrayRef<std::string> InputFiles,
+ llvm::IntrusiveRefCntPtr<vfs::FileSystem> BaseFS,
ProfileData *Profile = nullptr);
// FIXME: This interface will need to be significantly extended to be useful.
@@ -236,7 +237,8 @@ void runClangTidy(clang::tidy::ClangTidyContext &Context,
/// Errors containing fixes are automatically applied and reformatted. If no
/// clang-format configuration file is found, the given \P FormatStyle is used.
void handleErrors(ClangTidyContext &Context, bool Fix,
- unsigned &WarningsAsErrorsCount);
+ unsigned &WarningsAsErrorsCount,
+ llvm::IntrusiveRefCntPtr<vfs::FileSystem> BaseFS);
/// \brief Serializes replacements into YAML and writes them to the specified
/// output stream.
diff --git a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
index b5fdac4292b..e362b32e720 100644
--- a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
+++ b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
@@ -209,6 +209,13 @@ options are specified.
cl::init(false),
cl::cat(ClangTidyCategory));
+static cl::opt<std::string> VfsOverlay("vfsoverlay", cl::desc(R"(
+Overlay the virtual filesystem described by file
+over the real file system.
+)"),
+ cl::value_desc("filename"),
+ cl::cat(ClangTidyCategory));
+
namespace clang {
namespace tidy {
@@ -330,6 +337,30 @@ static std::unique_ptr<ClangTidyOptionsProvider> createOptionsProvider() {
OverrideOptions);
}
+llvm::IntrusiveRefCntPtr<vfs::FileSystem>
+getVfsOverlayFromFile(const std::string &OverlayFile) {
+ llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFS(
+ new vfs::OverlayFileSystem(vfs::getRealFileSystem()));
+ llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer =
+ OverlayFS->getBufferForFile(OverlayFile);
+ if (!Buffer) {
+ llvm::errs() << "Can't load virtual filesystem overlay file '"
+ << OverlayFile << "': " << Buffer.getError().message()
+ << ".\n";
+ return nullptr;
+ }
+
+ IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getVFSFromYAML(
+ std::move(Buffer.get()), /*DiagHandler*/ nullptr, OverlayFile);
+ if (!FS) {
+ llvm::errs() << "Error: invalid virtual filesystem overlay file '"
+ << OverlayFile << "'.\n";
+ return nullptr;
+ }
+ OverlayFS->pushOverlay(FS);
+ return OverlayFS;
+}
+
static int clangTidyMain(int argc, const char **argv) {
CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory,
cl::ZeroOrMore);
@@ -401,6 +432,11 @@ static int clangTidyMain(int argc, const char **argv) {
llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
return 0;
}
+ llvm::IntrusiveRefCntPtr<vfs::FileSystem> BaseFS(
+ VfsOverlay.empty() ? vfs::getRealFileSystem()
+ : getVfsOverlayFromFile(VfsOverlay));
+ if (!BaseFS)
+ return 1;
ProfileData Profile;
@@ -409,7 +445,7 @@ static int clangTidyMain(int argc, const char **argv) {
llvm::InitializeAllAsmParsers();
ClangTidyContext Context(std::move(OwningOptionsProvider));
- runClangTidy(Context, OptionsParser.getCompilations(), PathList,
+ runClangTidy(Context, OptionsParser.getCompilations(), PathList, BaseFS,
EnableCheckProfile ? &Profile : nullptr);
ArrayRef<ClangTidyError> Errors = Context.getErrors();
bool FoundErrors =
@@ -422,7 +458,8 @@ static int clangTidyMain(int argc, const char **argv) {
unsigned WErrorCount = 0;
// -fix-errors implies -fix.
- handleErrors(Context, (FixErrors || Fix) && !DisableFixes, WErrorCount);
+ handleErrors(Context, (FixErrors || Fix) && !DisableFixes, WErrorCount,
+ BaseFS);
if (!ExportFixes.empty() && !Errors.empty()) {
std::error_code EC;
diff --git a/clang-tools-extra/test/clang-tidy/Inputs/vfsoverlay/actual_header.h b/clang-tools-extra/test/clang-tidy/Inputs/vfsoverlay/actual_header.h
new file mode 100644
index 00000000000..8e1b9aef633
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/Inputs/vfsoverlay/actual_header.h
@@ -0,0 +1 @@
+struct X {};
diff --git a/clang-tools-extra/test/clang-tidy/Inputs/vfsoverlay/vfsoverlay.yaml b/clang-tools-extra/test/clang-tidy/Inputs/vfsoverlay/vfsoverlay.yaml
new file mode 100644
index 00000000000..80d8fbad562
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/Inputs/vfsoverlay/vfsoverlay.yaml
@@ -0,0 +1,12 @@
+{
+ 'version': 0,
+ 'roots': [
+ { 'name': 'OUT_DIR', 'type': 'directory',
+ 'contents': [
+ { 'name': 'not_real.h', 'type': 'file',
+ 'external-contents': 'INPUT_DIR/actual_header.h'
+ }
+ ]
+ }
+ ]
+}
diff --git a/clang-tools-extra/test/clang-tidy/vfsoverlay.cpp b/clang-tools-extra/test/clang-tidy/vfsoverlay.cpp
new file mode 100644
index 00000000000..5704b71fd10
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/vfsoverlay.cpp
@@ -0,0 +1,8 @@
+// RUN: sed -e "s:INPUT_DIR:%S/Inputs/vfsoverlay:g" -e "s:OUT_DIR:%t:g" %S/Inputs/vfsoverlay/vfsoverlay.yaml > %t.yaml
+// RUN: clang-tidy %s -checks='-*,modernize-use-nullptr' -vfsoverlay %t.yaml -- -I %t | FileCheck %s
+// REQUIRES: shell
+
+#include "not_real.h"
+
+X *ptr = 0;
+// CHECK: warning: use nullptr [modernize-use-nullptr]
OpenPOWER on IntegriCloud