2014-05-23 00:07:11 +08:00
|
|
|
//===--- ClangTidyOptions.cpp - clang-tidy ----------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ClangTidyOptions.h"
|
2014-10-16 19:27:57 +08:00
|
|
|
#include "ClangTidyModuleRegistry.h"
|
2014-09-04 22:23:36 +08:00
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2015-01-14 19:24:38 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2014-09-04 22:23:36 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
2014-05-23 00:07:11 +08:00
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
2015-01-14 19:24:38 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-09-04 22:23:36 +08:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "clang-tidy-options"
|
2014-05-23 00:07:11 +08:00
|
|
|
|
2014-06-05 21:31:45 +08:00
|
|
|
using clang::tidy::ClangTidyOptions;
|
2014-05-23 00:07:11 +08:00
|
|
|
using clang::tidy::FileFilter;
|
|
|
|
|
|
|
|
LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FileFilter)
|
|
|
|
LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FileFilter::LineRange)
|
2014-09-23 22:46:55 +08:00
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(ClangTidyOptions::StringPair)
|
2015-11-10 00:28:11 +08:00
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(std::string)
|
2014-05-23 00:07:11 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace yaml {
|
|
|
|
|
|
|
|
// Map std::pair<int, int> to a JSON array of size 2.
|
|
|
|
template <> struct SequenceTraits<FileFilter::LineRange> {
|
|
|
|
static size_t size(IO &IO, FileFilter::LineRange &Range) {
|
|
|
|
return Range.first == 0 ? 0 : Range.second == 0 ? 1 : 2;
|
|
|
|
}
|
|
|
|
static unsigned &element(IO &IO, FileFilter::LineRange &Range, size_t Index) {
|
|
|
|
if (Index > 1)
|
|
|
|
IO.setError("Too many elements in line range.");
|
|
|
|
return Index == 0 ? Range.first : Range.second;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct MappingTraits<FileFilter> {
|
|
|
|
static void mapping(IO &IO, FileFilter &File) {
|
|
|
|
IO.mapRequired("name", File.Name);
|
|
|
|
IO.mapOptional("lines", File.LineRanges);
|
|
|
|
}
|
|
|
|
static StringRef validate(IO &io, FileFilter &File) {
|
|
|
|
if (File.Name.empty())
|
|
|
|
return "No file name specified";
|
|
|
|
for (const FileFilter::LineRange &Range : File.LineRanges) {
|
|
|
|
if (Range.first <= 0 || Range.second <= 0)
|
|
|
|
return "Invalid line range";
|
|
|
|
}
|
|
|
|
return StringRef();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-12 16:53:36 +08:00
|
|
|
template <> struct MappingTraits<ClangTidyOptions::StringPair> {
|
|
|
|
static void mapping(IO &IO, ClangTidyOptions::StringPair &KeyValue) {
|
|
|
|
IO.mapRequired("key", KeyValue.first);
|
|
|
|
IO.mapRequired("value", KeyValue.second);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NOptionMap {
|
|
|
|
NOptionMap(IO &) {}
|
|
|
|
NOptionMap(IO &, const ClangTidyOptions::OptionMap &OptionMap)
|
|
|
|
: Options(OptionMap.begin(), OptionMap.end()) {}
|
|
|
|
ClangTidyOptions::OptionMap denormalize(IO &) {
|
|
|
|
ClangTidyOptions::OptionMap Map;
|
|
|
|
for (const auto &KeyValue : Options)
|
|
|
|
Map[KeyValue.first] = KeyValue.second;
|
|
|
|
return Map;
|
|
|
|
}
|
|
|
|
std::vector<ClangTidyOptions::StringPair> Options;
|
|
|
|
};
|
|
|
|
|
2014-06-05 21:31:45 +08:00
|
|
|
template <> struct MappingTraits<ClangTidyOptions> {
|
|
|
|
static void mapping(IO &IO, ClangTidyOptions &Options) {
|
2014-09-12 16:53:36 +08:00
|
|
|
MappingNormalization<NOptionMap, ClangTidyOptions::OptionMap> NOpts(
|
|
|
|
IO, Options.CheckOptions);
|
2014-06-05 21:31:45 +08:00
|
|
|
IO.mapOptional("Checks", Options.Checks);
|
|
|
|
IO.mapOptional("HeaderFilterRegex", Options.HeaderFilterRegex);
|
|
|
|
IO.mapOptional("AnalyzeTemporaryDtors", Options.AnalyzeTemporaryDtors);
|
2014-09-25 02:36:03 +08:00
|
|
|
IO.mapOptional("User", Options.User);
|
2014-09-12 16:53:36 +08:00
|
|
|
IO.mapOptional("CheckOptions", NOpts->Options);
|
2015-11-10 00:28:11 +08:00
|
|
|
IO.mapOptional("ExtraArgs", Options.ExtraArgs);
|
|
|
|
IO.mapOptional("ExtraArgsBefore", Options.ExtraArgsBefore);
|
2014-06-05 21:31:45 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-23 00:07:11 +08:00
|
|
|
} // namespace yaml
|
|
|
|
} // namespace llvm
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
|
2014-10-16 19:27:57 +08:00
|
|
|
ClangTidyOptions ClangTidyOptions::getDefaults() {
|
|
|
|
ClangTidyOptions Options;
|
|
|
|
Options.Checks = "";
|
|
|
|
Options.HeaderFilterRegex = "";
|
2014-10-29 06:16:13 +08:00
|
|
|
Options.SystemHeaders = false;
|
2014-10-16 19:27:57 +08:00
|
|
|
Options.AnalyzeTemporaryDtors = false;
|
|
|
|
Options.User = llvm::None;
|
|
|
|
for (ClangTidyModuleRegistry::iterator I = ClangTidyModuleRegistry::begin(),
|
|
|
|
E = ClangTidyModuleRegistry::end();
|
|
|
|
I != E; ++I)
|
|
|
|
Options = Options.mergeWith(I->instantiate()->getModuleOptions());
|
|
|
|
return Options;
|
|
|
|
}
|
|
|
|
|
2014-09-04 22:23:36 +08:00
|
|
|
ClangTidyOptions
|
|
|
|
ClangTidyOptions::mergeWith(const ClangTidyOptions &Other) const {
|
|
|
|
ClangTidyOptions Result = *this;
|
|
|
|
|
|
|
|
// Merge comma-separated glob lists by appending the new value after a comma.
|
|
|
|
if (Other.Checks)
|
|
|
|
Result.Checks =
|
|
|
|
(Result.Checks && !Result.Checks->empty() ? *Result.Checks + "," : "") +
|
|
|
|
*Other.Checks;
|
|
|
|
|
|
|
|
if (Other.HeaderFilterRegex)
|
|
|
|
Result.HeaderFilterRegex = Other.HeaderFilterRegex;
|
2014-10-29 06:16:13 +08:00
|
|
|
if (Other.SystemHeaders)
|
|
|
|
Result.SystemHeaders = Other.SystemHeaders;
|
2014-09-04 22:23:36 +08:00
|
|
|
if (Other.AnalyzeTemporaryDtors)
|
|
|
|
Result.AnalyzeTemporaryDtors = Other.AnalyzeTemporaryDtors;
|
2014-09-25 02:36:03 +08:00
|
|
|
if (Other.User)
|
|
|
|
Result.User = Other.User;
|
2015-11-10 00:28:11 +08:00
|
|
|
if (Other.ExtraArgs)
|
|
|
|
Result.ExtraArgs = Other.ExtraArgs;
|
|
|
|
if (Other.ExtraArgsBefore)
|
|
|
|
Result.ExtraArgsBefore = Other.ExtraArgsBefore;
|
2014-09-12 16:53:36 +08:00
|
|
|
|
|
|
|
for (const auto &KeyValue : Other.CheckOptions)
|
|
|
|
Result.CheckOptions[KeyValue.first] = KeyValue.second;
|
|
|
|
|
2014-09-04 22:23:36 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileOptionsProvider::FileOptionsProvider(
|
|
|
|
const ClangTidyGlobalOptions &GlobalOptions,
|
2014-09-25 02:36:03 +08:00
|
|
|
const ClangTidyOptions &DefaultOptions,
|
2014-09-04 22:23:36 +08:00
|
|
|
const ClangTidyOptions &OverrideOptions)
|
2014-09-25 02:36:03 +08:00
|
|
|
: DefaultOptionsProvider(GlobalOptions, DefaultOptions),
|
2014-09-04 22:23:36 +08:00
|
|
|
OverrideOptions(OverrideOptions) {
|
2014-10-20 20:29:15 +08:00
|
|
|
ConfigHandlers.emplace_back(".clang-tidy", parseConfiguration);
|
2014-09-25 02:36:03 +08:00
|
|
|
CachedOptions[""] = DefaultOptions.mergeWith(OverrideOptions);
|
2014-09-04 22:23:36 +08:00
|
|
|
}
|
|
|
|
|
2014-10-20 20:29:15 +08:00
|
|
|
FileOptionsProvider::FileOptionsProvider(
|
|
|
|
const ClangTidyGlobalOptions &GlobalOptions,
|
|
|
|
const ClangTidyOptions &DefaultOptions,
|
|
|
|
const ClangTidyOptions &OverrideOptions,
|
|
|
|
const FileOptionsProvider::ConfigFileHandlers &ConfigHandlers)
|
|
|
|
: DefaultOptionsProvider(GlobalOptions, DefaultOptions),
|
|
|
|
OverrideOptions(OverrideOptions), ConfigHandlers(ConfigHandlers) {
|
|
|
|
CachedOptions[""] = DefaultOptions.mergeWith(OverrideOptions);
|
|
|
|
}
|
2014-09-04 22:23:36 +08:00
|
|
|
|
|
|
|
// FIXME: This method has some common logic with clang::format::getStyle().
|
|
|
|
// Consider pulling out common bits to a findParentFileWithName function or
|
|
|
|
// similar.
|
2015-01-20 17:48:51 +08:00
|
|
|
ClangTidyOptions FileOptionsProvider::getOptions(StringRef FileName) {
|
2014-09-04 22:23:36 +08:00
|
|
|
DEBUG(llvm::dbgs() << "Getting options for file " << FileName << "...\n");
|
|
|
|
SmallString<256> FilePath(FileName);
|
|
|
|
|
|
|
|
if (std::error_code EC = llvm::sys::fs::make_absolute(FilePath)) {
|
|
|
|
llvm::errs() << "Can't make absolute path from " << FileName << ": "
|
|
|
|
<< EC.message() << "\n";
|
|
|
|
// FIXME: Figure out what to do.
|
|
|
|
} else {
|
|
|
|
FileName = FilePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
for (StringRef CurrentPath = Path;;
|
|
|
|
CurrentPath = llvm::sys::path::parent_path(CurrentPath)) {
|
2014-10-20 20:29:15 +08:00
|
|
|
llvm::Optional<ClangTidyOptions> Result;
|
2014-09-04 22:23:36 +08:00
|
|
|
|
|
|
|
auto Iter = CachedOptions.find(CurrentPath);
|
|
|
|
if (Iter != CachedOptions.end())
|
|
|
|
Result = Iter->second;
|
|
|
|
|
|
|
|
if (!Result)
|
|
|
|
Result = TryReadConfigFile(CurrentPath);
|
|
|
|
|
|
|
|
if (Result) {
|
|
|
|
// Store cached value for all intermediate directories.
|
|
|
|
while (Path != CurrentPath) {
|
|
|
|
DEBUG(llvm::dbgs() << "Caching configuration for path " << Path
|
|
|
|
<< ".\n");
|
2014-11-19 11:05:07 +08:00
|
|
|
CachedOptions[Path] = *Result;
|
2014-09-04 22:23:36 +08:00
|
|
|
Path = llvm::sys::path::parent_path(Path);
|
|
|
|
}
|
2014-11-19 11:05:07 +08:00
|
|
|
return CachedOptions[Path] = *Result;
|
2014-09-04 22:23:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-20 20:29:15 +08:00
|
|
|
llvm::Optional<ClangTidyOptions>
|
2014-09-04 22:23:36 +08:00
|
|
|
FileOptionsProvider::TryReadConfigFile(StringRef Directory) {
|
|
|
|
assert(!Directory.empty());
|
|
|
|
|
2014-10-20 20:29:15 +08:00
|
|
|
if (!llvm::sys::fs::is_directory(Directory)) {
|
|
|
|
llvm::errs() << "Error reading configuration from " << Directory
|
|
|
|
<< ": directory doesn't exist.\n";
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const ConfigFileHandler &ConfigHandler : ConfigHandlers) {
|
|
|
|
SmallString<128> ConfigFile(Directory);
|
|
|
|
llvm::sys::path::append(ConfigFile, ConfigHandler.first);
|
|
|
|
DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
|
|
|
|
|
|
|
|
bool IsFile = false;
|
|
|
|
// Ignore errors from is_regular_file: we only need to know if we can read
|
|
|
|
// the file or not.
|
|
|
|
llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
|
|
|
|
if (!IsFile)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
|
|
|
|
llvm::MemoryBuffer::getFile(ConfigFile.c_str());
|
|
|
|
if (std::error_code EC = Text.getError()) {
|
|
|
|
llvm::errs() << "Can't read " << ConfigFile << ": " << EC.message()
|
|
|
|
<< "\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip empty files, e.g. files opened for writing via shell output
|
|
|
|
// redirection.
|
|
|
|
if ((*Text)->getBuffer().empty())
|
|
|
|
continue;
|
|
|
|
llvm::ErrorOr<ClangTidyOptions> ParsedOptions =
|
|
|
|
ConfigHandler.second((*Text)->getBuffer());
|
|
|
|
if (!ParsedOptions) {
|
2014-11-04 23:25:22 +08:00
|
|
|
if (ParsedOptions.getError())
|
|
|
|
llvm::errs() << "Error parsing " << ConfigFile << ": "
|
|
|
|
<< ParsedOptions.getError().message() << "\n";
|
2014-10-20 20:29:15 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-05 22:50:17 +08:00
|
|
|
return DefaultOptionsProvider::getOptions(Directory)
|
|
|
|
.mergeWith(*ParsedOptions)
|
|
|
|
.mergeWith(OverrideOptions);
|
2014-09-25 02:36:03 +08:00
|
|
|
}
|
2014-10-20 20:29:15 +08:00
|
|
|
return llvm::None;
|
2014-09-04 22:23:36 +08:00
|
|
|
}
|
|
|
|
|
2014-05-23 00:07:11 +08:00
|
|
|
/// \brief Parses -line-filter option and stores it to the \c Options.
|
2014-09-04 22:23:36 +08:00
|
|
|
std::error_code parseLineFilter(StringRef LineFilter,
|
2014-06-12 21:32:11 +08:00
|
|
|
clang::tidy::ClangTidyGlobalOptions &Options) {
|
2014-05-23 00:07:11 +08:00
|
|
|
llvm::yaml::Input Input(LineFilter);
|
|
|
|
Input >> Options.LineFilter;
|
|
|
|
return Input.error();
|
|
|
|
}
|
|
|
|
|
2014-09-25 02:36:03 +08:00
|
|
|
llvm::ErrorOr<ClangTidyOptions> parseConfiguration(StringRef Config) {
|
2014-06-05 21:31:45 +08:00
|
|
|
llvm::yaml::Input Input(Config);
|
2014-09-25 02:36:03 +08:00
|
|
|
ClangTidyOptions Options;
|
2014-06-05 21:31:45 +08:00
|
|
|
Input >> Options;
|
2014-09-25 02:36:03 +08:00
|
|
|
if (Input.error())
|
|
|
|
return Input.error();
|
|
|
|
return Options;
|
2014-06-05 21:31:45 +08:00
|
|
|
}
|
|
|
|
|
2014-09-04 22:23:36 +08:00
|
|
|
std::string configurationAsText(const ClangTidyOptions &Options) {
|
|
|
|
std::string Text;
|
|
|
|
llvm::raw_string_ostream Stream(Text);
|
|
|
|
llvm::yaml::Output Output(Stream);
|
|
|
|
// We use the same mapping method for input and output, so we need a non-const
|
|
|
|
// reference here.
|
|
|
|
ClangTidyOptions NonConstValue = Options;
|
|
|
|
Output << NonConstValue;
|
|
|
|
return Stream.str();
|
|
|
|
}
|
|
|
|
|
2014-05-23 00:07:11 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|