Index: clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h =================================================================== --- clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h +++ clang-tools-extra/trunk/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 #include #include @@ -221,7 +223,8 @@ /// whatever options are read from the configuration file. FileOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions, const ClangTidyOptions &DefaultOptions, - const ClangTidyOptions &OverrideOptions); + const ClangTidyOptions &OverrideOptions, + llvm::IntrusiveRefCntPtr FS = nullptr); /// \brief Initializes the \c FileOptionsProvider instance with a custom set /// of configuration file handlers. @@ -255,6 +258,7 @@ llvm::StringMap CachedOptions; ClangTidyOptions OverrideOptions; ConfigFileHandlers ConfigHandlers; + llvm::IntrusiveRefCntPtr FS; }; /// \brief Parses LineFilter from JSON and stores it to the \p Options. Index: clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.cpp =================================================================== --- clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.cpp +++ clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.cpp @@ -204,9 +204,12 @@ FileOptionsProvider::FileOptionsProvider( const ClangTidyGlobalOptions &GlobalOptions, const ClangTidyOptions &DefaultOptions, - const ClangTidyOptions &OverrideOptions) + const ClangTidyOptions &OverrideOptions, + llvm::IntrusiveRefCntPtr 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 @@ std::vector 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 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 Result; Index: clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp =================================================================== --- clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp +++ clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp @@ -286,7 +286,8 @@ OS.flush(); } -static std::unique_ptr createOptionsProvider() { +static std::unique_ptr createOptionsProvider( + llvm::IntrusiveRefCntPtr FS) { ClangTidyGlobalOptions GlobalOptions; if (std::error_code Err = parseLineFilter(LineFilter, GlobalOptions)) { llvm::errs() << "Invalid LineFilter: " << Err.message() << "\n\nUsage:\n"; @@ -334,7 +335,7 @@ } } return llvm::make_unique(GlobalOptions, DefaultOptions, - OverrideOptions); + OverrideOptions, std::move(FS)); } llvm::IntrusiveRefCntPtr @@ -364,8 +365,13 @@ static int clangTidyMain(int argc, const char **argv) { CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory, cl::ZeroOrMore); + llvm::IntrusiveRefCntPtr 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 @@ llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true); return 1; } - llvm::IntrusiveRefCntPtr BaseFS( - VfsOverlay.empty() ? vfs::getRealFileSystem() - : getVfsOverlayFromFile(VfsOverlay)); - if (!BaseFS) - return 1; - ProfileData Profile; llvm::InitializeAllTargetInfos(); Index: clang-tools-extra/trunk/test/clang-tidy/read_file_config.cpp =================================================================== --- clang-tools-extra/trunk/test/clang-tidy/read_file_config.cpp +++ clang-tools-extra/trunk/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; +}