summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang-tools-extra/clang-tidy/ClangTidyOptions.cpp16
-rw-r--r--clang-tools-extra/clang-tidy/ClangTidyOptions.h6
-rw-r--r--clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp18
-rw-r--r--clang-tools-extra/test/clang-tidy/read_file_config.cpp12
4 files changed, 38 insertions, 14 deletions
diff --git a/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp b/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
index 7cbe6ac3621..63b2b5eb650 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
@@ -204,9 +204,12 @@ ConfigOptionsProvider::getRawOptions(llvm::StringRef FileName) {
FileOptionsProvider::FileOptionsProvider(
const ClangTidyGlobalOptions &GlobalOptions,
const ClangTidyOptions &DefaultOptions,
- const ClangTidyOptions &OverrideOptions)
+ const ClangTidyOptions &OverrideOptions,
+ llvm::IntrusiveRefCntPtr<vfs::FileSystem> VFS)
: DefaultOptionsProvider(GlobalOptions, DefaultOptions),
- OverrideOptions(OverrideOptions) {
+ OverrideOptions(OverrideOptions), FS(std::move(VFS)) {
+ if (!FS)
+ FS = vfs::getRealFileSystem();
ConfigHandlers.emplace_back(".clang-tidy", parseConfiguration);
}
@@ -224,14 +227,19 @@ FileOptionsProvider::FileOptionsProvider(
std::vector<OptionsSource>
FileOptionsProvider::getRawOptions(StringRef FileName) {
DEBUG(llvm::dbgs() << "Getting options for file " << FileName << "...\n");
+ assert(FS && "FS must be set.");
+
+ llvm::SmallString<128> AbsoluteFilePath(FileName);
+ if (std::error_code ec = FS->makeAbsolute(AbsoluteFilePath))
+ return {};
std::vector<OptionsSource> RawOptions =
- DefaultOptionsProvider::getRawOptions(FileName);
+ DefaultOptionsProvider::getRawOptions(AbsoluteFilePath.str());
OptionsSource CommandLineOptions(OverrideOptions,
OptionsSourceTypeCheckCommandLineOption);
// Look for a suitable configuration file in all parent directories of the
// file. Start with the immediate parent directory and move up.
- StringRef Path = llvm::sys::path::parent_path(FileName);
+ StringRef Path = llvm::sys::path::parent_path(AbsoluteFilePath.str());
for (StringRef CurrentPath = Path; !CurrentPath.empty();
CurrentPath = llvm::sys::path::parent_path(CurrentPath)) {
llvm::Optional<OptionsSource> Result;
diff --git a/clang-tools-extra/clang-tidy/ClangTidyOptions.h b/clang-tools-extra/clang-tidy/ClangTidyOptions.h
index d3f4de2070d..f5363bf8f42 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyOptions.h
+++ b/clang-tools-extra/clang-tidy/ClangTidyOptions.h
@@ -13,7 +13,9 @@
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/Support/ErrorOr.h"
+#include "clang/Basic/VirtualFileSystem.h"
#include <functional>
#include <map>
#include <string>
@@ -221,7 +223,8 @@ public:
/// whatever options are read from the configuration file.
FileOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
const ClangTidyOptions &DefaultOptions,
- const ClangTidyOptions &OverrideOptions);
+ const ClangTidyOptions &OverrideOptions,
+ llvm::IntrusiveRefCntPtr<vfs::FileSystem> FS = nullptr);
/// \brief Initializes the \c FileOptionsProvider instance with a custom set
/// of configuration file handlers.
@@ -255,6 +258,7 @@ protected:
llvm::StringMap<OptionsSource> CachedOptions;
ClangTidyOptions OverrideOptions;
ConfigFileHandlers ConfigHandlers;
+ llvm::IntrusiveRefCntPtr<vfs::FileSystem> FS;
};
/// \brief Parses LineFilter from JSON and stores it to the \p Options.
diff --git a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
index 273e4b97b73..e5d043c5ecb 100644
--- a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
+++ b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
@@ -286,7 +286,8 @@ static void printProfileData(const ProfileData &Profile,
OS.flush();
}
-static std::unique_ptr<ClangTidyOptionsProvider> createOptionsProvider() {
+static std::unique_ptr<ClangTidyOptionsProvider> createOptionsProvider(
+ llvm::IntrusiveRefCntPtr<vfs::FileSystem> FS) {
ClangTidyGlobalOptions GlobalOptions;
if (std::error_code Err = parseLineFilter(LineFilter, GlobalOptions)) {
llvm::errs() << "Invalid LineFilter: " << Err.message() << "\n\nUsage:\n";
@@ -334,7 +335,7 @@ static std::unique_ptr<ClangTidyOptionsProvider> createOptionsProvider() {
}
}
return llvm::make_unique<FileOptionsProvider>(GlobalOptions, DefaultOptions,
- OverrideOptions);
+ OverrideOptions, std::move(FS));
}
llvm::IntrusiveRefCntPtr<vfs::FileSystem>
@@ -364,8 +365,13 @@ getVfsOverlayFromFile(const std::string &OverlayFile) {
static int clangTidyMain(int argc, const char **argv) {
CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory,
cl::ZeroOrMore);
+ llvm::IntrusiveRefCntPtr<vfs::FileSystem> BaseFS(
+ VfsOverlay.empty() ? vfs::getRealFileSystem()
+ : getVfsOverlayFromFile(VfsOverlay));
+ if (!BaseFS)
+ return 1;
- auto OwningOptionsProvider = createOptionsProvider();
+ auto OwningOptionsProvider = createOptionsProvider(BaseFS);
auto *OptionsProvider = OwningOptionsProvider.get();
if (!OptionsProvider)
return 1;
@@ -432,12 +438,6 @@ static int clangTidyMain(int argc, const char **argv) {
llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
return 1;
}
- llvm::IntrusiveRefCntPtr<vfs::FileSystem> BaseFS(
- VfsOverlay.empty() ? vfs::getRealFileSystem()
- : getVfsOverlayFromFile(VfsOverlay));
- if (!BaseFS)
- return 1;
-
ProfileData Profile;
llvm::InitializeAllTargetInfos();
diff --git a/clang-tools-extra/test/clang-tidy/read_file_config.cpp b/clang-tools-extra/test/clang-tidy/read_file_config.cpp
new file mode 100644
index 00000000000..aacdad1cc0a
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/read_file_config.cpp
@@ -0,0 +1,12 @@
+// RUN: mkdir -p %T/read-file-config/
+// RUN: cp %s %T/read-file-config/test.cpp
+// RUN: echo 'Checks: "-*,modernize-use-nullptr"' > %T/read-file-config/.clang-tidy
+// RUN: echo '[{"command": "cc -c -o test.o test.cpp", "directory": "%T/read-file-config", "file": "%T/read-file-config/test.cpp"}]' > %T/read-file-config/compile_commands.json
+// RUN: clang-tidy %T/read-file-config/test.cpp | not grep "warning: .*\[clang-analyzer-deadcode.DeadStores\]$"
+// RUN: clang-tidy -checks="-*,clang-analyzer-*" %T/read-file-config/test.cpp | grep "warning: .*\[clang-analyzer-deadcode.DeadStores\]$"
+
+void f() {
+ int x;
+ x = 1;
+ x = 2;
+}
OpenPOWER on IntegriCloud