11 #include "clang/Basic/LLVM.h" 12 #include "llvm/ADT/SmallString.h" 13 #include "llvm/Support/Debug.h" 14 #include "llvm/Support/Errc.h" 15 #include "llvm/Support/FileSystem.h" 16 #include "llvm/Support/Path.h" 17 #include "llvm/Support/YAMLTraits.h" 18 #include "llvm/Support/raw_ostream.h" 21 #define DEBUG_TYPE "clang-tidy-options" 28 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FileFilter::LineRange)
34 template <>
struct SequenceTraits<
FileFilter::LineRange> {
35 static size_t size(IO &IO, FileFilter::LineRange &
Range) {
36 return Range.first == 0 ? 0 : Range.second == 0 ? 1 : 2;
40 IO.setError(
"Too many elements in line range.");
41 return Index == 0 ? Range.first : Range.second;
47 IO.mapRequired(
"name", File.
Name);
51 if (File.
Name.empty())
52 return "No file name specified";
54 if (Range.first <= 0 || Range.second <= 0)
55 return "Invalid line range";
62 static void mapping(IO &IO, ClangTidyOptions::StringPair &KeyValue) {
63 IO.mapRequired(
"key", KeyValue.first);
64 IO.mapRequired(
"value", KeyValue.second);
70 NOptionMap(IO &,
const ClangTidyOptions::OptionMap &OptionMap)
71 : Options(OptionMap.begin(), OptionMap.end()) {}
73 ClangTidyOptions::OptionMap Map;
74 for (
const auto &KeyValue : Options)
75 Map[KeyValue.first] = KeyValue.second;
78 std::vector<ClangTidyOptions::StringPair>
Options;
83 MappingNormalization<NOptionMap, ClangTidyOptions::OptionMap> NOpts(
86 IO.mapOptional(
"Checks", Options.
Checks);
89 IO.mapOptional(
"AnalyzeTemporaryDtors", Ignored);
91 IO.mapOptional(
"User", Options.
User);
92 IO.mapOptional(
"CheckOptions", NOpts->Options);
93 IO.mapOptional(
"ExtraArgs", Options.
ExtraArgs);
111 Options.
User = llvm::None;
112 for (ClangTidyModuleRegistry::iterator I = ClangTidyModuleRegistry::begin(),
113 E = ClangTidyModuleRegistry::end();
115 Options = Options.
mergeWith(I->instantiate()->getModuleOptions());
119 template <
typename T>
123 Dest->insert(Dest->end(), Src->begin(), Src->end());
130 const Optional<std::string> &
Src) {
132 Dest = (Dest && !Dest->empty() ? *Dest +
"," :
"") + *Src;
135 template <
typename T>
160 const char ClangTidyOptionsProvider::OptionsSourceTypeDefaultBinary[] =
162 const char ClangTidyOptionsProvider::OptionsSourceTypeCheckCommandLineOption[] =
163 "command-line option '-checks'";
165 ClangTidyOptionsProvider::OptionsSourceTypeConfigCommandLineOption[] =
166 "command-line option '-config'";
169 ClangTidyOptionsProvider::getOptions(llvm::StringRef
FileName) {
171 for (
const auto &Source : getRawOptions(FileName))
176 std::vector<OptionsSource>
177 DefaultOptionsProvider::getRawOptions(llvm::StringRef
FileName) {
178 std::vector<OptionsSource> Result;
179 Result.emplace_back(DefaultOptions, OptionsSourceTypeDefaultBinary);
183 ConfigOptionsProvider::ConfigOptionsProvider(
189 ConfigOptions(ConfigOptions), OverrideOptions(OverrideOptions) {}
191 std::vector<OptionsSource>
193 std::vector<OptionsSource> RawOptions =
195 RawOptions.emplace_back(ConfigOptions,
197 RawOptions.emplace_back(OverrideOptions,
206 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
VFS)
208 OverrideOptions(OverrideOptions),
FS(std::move(VFS)) {
210 FS = llvm::vfs::getRealFileSystem();
220 OverrideOptions(OverrideOptions), ConfigHandlers(ConfigHandlers) {}
225 std::vector<OptionsSource>
227 LLVM_DEBUG(llvm::dbgs() <<
"Getting options for file " << FileName
229 assert(
FS &&
"FS must be set.");
231 llvm::SmallString<128> AbsoluteFilePath(FileName);
233 if (
FS->makeAbsolute(AbsoluteFilePath))
236 std::vector<OptionsSource> RawOptions =
242 StringRef
Path = llvm::sys::path::parent_path(AbsoluteFilePath.str());
243 for (StringRef CurrentPath = Path; !CurrentPath.empty();
244 CurrentPath = llvm::sys::path::parent_path(CurrentPath)) {
245 llvm::Optional<OptionsSource> Result;
249 Result = Iter->second;
256 while (Path != CurrentPath) {
257 LLVM_DEBUG(llvm::dbgs()
258 <<
"Caching configuration for path " << Path <<
".\n");
260 Path = llvm::sys::path::parent_path(Path);
264 RawOptions.push_back(*Result);
268 RawOptions.push_back(CommandLineOptions);
272 llvm::Optional<OptionsSource>
274 assert(!Directory.empty());
276 if (!llvm::sys::fs::is_directory(Directory)) {
277 llvm::errs() <<
"Error reading configuration from " << Directory
278 <<
": directory doesn't exist.\n";
283 SmallString<128> ConfigFile(Directory);
284 llvm::sys::path::append(ConfigFile, ConfigHandler.first);
285 LLVM_DEBUG(llvm::dbgs() <<
"Trying " << ConfigFile <<
"...\n");
290 llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
294 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
Text =
295 llvm::MemoryBuffer::getFile(ConfigFile.c_str());
296 if (std::error_code EC = Text.getError()) {
297 llvm::errs() <<
"Can't read " << ConfigFile <<
": " << EC.message()
304 if ((*Text)->getBuffer().empty())
306 llvm::ErrorOr<ClangTidyOptions> ParsedOptions =
307 ConfigHandler.second((*Text)->getBuffer());
308 if (!ParsedOptions) {
309 if (ParsedOptions.getError())
310 llvm::errs() <<
"Error parsing " << ConfigFile <<
": " 311 << ParsedOptions.getError().message() <<
"\n";
322 llvm::yaml::Input Input(LineFilter);
324 return Input.error();
328 llvm::yaml::Input Input(Config);
332 return Input.error();
338 llvm::raw_string_ostream Stream(Text);
339 llvm::yaml::Output Output(Stream);
343 Output << NonConstValue;
llvm::Optional< std::string > Checks
Checks filter.
ClangTidyOptions mergeWith(const ClangTidyOptions &Other) const
Creates a new ClangTidyOptions instance combined from all fields of this instance overridden by the f...
std::vector< OptionsSource > getRawOptions(llvm::StringRef FileName) override
Returns an ordered vector of OptionsSources, in order of increasing priority.
llvm::Optional< ArgList > ExtraArgs
Add extra compilation arguments to the end of the list.
Some operations such as code completion produce a set of candidates.
llvm::Optional< std::string > User
Specifies the name or e-mail of the user running clang-tidy.
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS
static void mergeVectors(Optional< T > &Dest, const Optional< T > &Src)
static cl::opt< std::string > Config("config", cl::desc(R"(
Specifies a configuration in YAML/JSON format:
-config="{Checks:' *', CheckOptions:[{key:x, value:y}]}"
When the value is empty, clang-tidy will
attempt to find a file named .clang-tidy for
each source file in its parent directories.
)"), cl::init(""), cl::cat(ClangTidyCategory))
llvm::Optional< std::string > HeaderFilterRegex
Output warnings from headers matching this filter.
static void mapping(IO &IO, FileFilter &File)
static const char OptionsSourceTypeCheckCommandLineOption[]
Contains options for clang-tidy.
static const char OptionsSourceTypeConfigCommandLineOption[]
std::vector< OptionsSource > getRawOptions(llvm::StringRef FileName) override
Returns an ordered vector of OptionsSources, in order of increasing priority.
std::error_code parseLineFilter(StringRef LineFilter, clang::tidy::ClangTidyGlobalOptions &Options)
Parses -line-filter option and stores it to the Options.
std::vector< HeaderHandle > Path
llvm::ErrorOr< ClangTidyOptions > parseConfiguration(StringRef Config)
static StringRef validate(IO &io, FileFilter &File)
NOptionMap(IO &, const ClangTidyOptions::OptionMap &OptionMap)
OptionMap CheckOptions
Key-value mapping used to store check-specific options.
llvm::Optional< bool > SystemHeaders
Output warnings from system headers matching HeaderFilterRegex.
ClangTidyOptions OverrideOptions
FileOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions, const ClangTidyOptions &DefaultOptions, const ClangTidyOptions &OverrideOptions, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS=nullptr)
Initializes the FileOptionsProvider instance.
ConfigFileHandlers ConfigHandlers
llvm::Optional< ArgList > ExtraArgsBefore
Add extra compilation arguments to the start of the list.
llvm::Optional< std::string > FormatStyle
Format code around applied fixes with clang-format using this style.
static cl::opt< std::string > LineFilter("line-filter", cl::desc(R"(
List of files with line ranges to filter the
warnings. Can be used together with
-header-filter. The format of the list is a
JSON array of objects:
[
{"name":"file1.cpp","lines":[[1,3],[5,7]]},
{"name":"file2.h"}
]
)"), cl::init(""), cl::cat(ClangTidyCategory))
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS
std::vector< OptionsSource > getRawOptions(llvm::StringRef FileName) override
Returns an ordered vector of OptionsSources, in order of increasing priority.
std::string configurationAsText(const ClangTidyOptions &Options)
Serializes configuration to a YAML-encoded string.
static unsigned & element(IO &IO, FileFilter::LineRange &Range, size_t Index)
llvm::StringMap< OptionsSource > CachedOptions
static void mapping(IO &IO, ClangTidyOptions &Options)
std::vector< FileFilter > LineFilter
Output warnings from certain line ranges of certain files only.
static void mapping(IO &IO, ClangTidyOptions::StringPair &KeyValue)
std::pair< std::string, std::function< llvm::ErrorOr< ClangTidyOptions > llvm::StringRef)> > ConfigFileHandler
llvm::Optional< std::string > WarningsAsErrors
WarningsAsErrors filter.
static void mergeCommaSeparatedLists(Optional< std::string > &Dest, const Optional< std::string > &Src)
std::vector< ConfigFileHandler > ConfigFileHandlers
Configuration file handlers listed in the order of priority.
llvm::StringRef Directory
Contains a list of line ranges in a single file.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
CharSourceRange Range
SourceRange for the file name.
std::vector< ClangTidyOptions::StringPair > Options
static size_t size(IO &IO, FileFilter::LineRange &Range)
static void overrideValue(Optional< T > &Dest, const Optional< T > &Src)
std::pair< ClangTidyOptions, std::string > OptionsSource
ClangTidyOptions and its source.
ClangTidyOptions::OptionMap denormalize(IO &)
std::vector< LineRange > LineRanges
A list of line ranges in this file, for which we show warnings.
clang::tidy::ClangTidyOptionsProvider::OptionsSource OptionsSource
std::string Name
File name.
llvm::Optional< OptionsSource > tryReadConfigFile(llvm::StringRef Directory)
Try to read configuration files from Directory using registered ConfigHandlers.
Implementation of the ClangTidyOptionsProvider interface, which returns the same options for all file...
const SymbolIndex * Index