[clang-tidy] Fix clang-tidy doesn't read .clangtidy configuration file.

Summary: Fix https://bugs.llvm.org/show_bug.cgi?id=34900.

Reviewers: alexfh

Reviewed By: alexfh

Subscribers: JonasToth, klimek, xazax.hun, cfe-commits

Differential Revision: https://reviews.llvm.org/D45697

llvm-svn: 330245
This commit is contained in:
Haojian Wu 2018-04-18 08:54:28 +00:00
parent deff753627
commit 10e50c8797
4 changed files with 38 additions and 14 deletions

View File

@ -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;

View File

@ -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.

View File

@ -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();

View File

@ -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;
}