Go to the documentation of this file.
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.");
47 IO.mapRequired(
"name", File.Name);
48 IO.mapOptional(
"lines", File.LineRanges);
51 if (File.Name.empty())
52 return "No file name specified";
53 for (
const FileFilter::LineRange &
Range : File.LineRanges) {
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.reserve(OptionMap.size());
72 for (
const auto &KeyValue : OptionMap)
73 Options.emplace_back(KeyValue.first, KeyValue.second.Value);
76 ClangTidyOptions::OptionMap Map;
77 for (
const auto &KeyValue :
Options)
78 Map[KeyValue.first] = ClangTidyOptions::ClangTidyValue(KeyValue.second);
81 std::vector<ClangTidyOptions::StringPair>
Options;
86 MappingNormalization<NOptionMap, ClangTidyOptions::OptionMap> NOpts(
89 IO.mapOptional(
"Checks", Options.
Checks);
92 IO.mapOptional(
"AnalyzeTemporaryDtors", Ignored);
94 IO.mapOptional(
"User", Options.
User);
95 IO.mapOptional(
"CheckOptions", NOpts->Options);
96 IO.mapOptional(
"ExtraArgs", Options.
ExtraArgs);
99 IO.mapOptional(
"UseColor", Options.
UseColor);
116 Options.
User = llvm::None;
117 unsigned Priority = 0;
118 for (
const ClangTidyModuleRegistry::entry &Module :
119 ClangTidyModuleRegistry::entries())
121 Options.
mergeWith(Module.instantiate()->getModuleOptions(), ++Priority);
125 template <
typename T>
129 Dest->insert(Dest->end(),
Src->begin(),
Src->end());
136 const Optional<std::string> &
Src) {
138 Dest = (Dest && !Dest->empty() ? *Dest +
"," :
"") + *
Src;
141 template <
typename T>
148 unsigned Priority)
const {
163 KeyValue.second.Value, KeyValue.second.Priority + Priority);
172 "command-line option '-checks'";
175 "command-line option '-config'";
180 unsigned Priority = 0;
182 Result = Result.mergeWith(Source.first, ++Priority);
186 std::vector<OptionsSource>
188 std::vector<OptionsSource> Result;
198 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
FS)
201 ConfigOptions(ConfigOptions) {}
203 std::vector<OptionsSource>
205 std::vector<OptionsSource> RawOptions =
208 LLVM_DEBUG(llvm::dbgs()
209 <<
"Getting options for file " <<
FileName <<
"...\n");
210 assert(
FS &&
"FS must be set.");
212 llvm::SmallString<128> AbsoluteFilePath(
FileName);
214 if (!
FS->makeAbsolute(AbsoluteFilePath)) {
218 RawOptions.emplace_back(ConfigOptions,
229 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS)
231 OverrideOptions(OverrideOptions),
FS(std::move(VFS)) {
233 FS = llvm::vfs::getRealFileSystem();
243 OverrideOptions(OverrideOptions), ConfigHandlers(ConfigHandlers) {}
246 llvm::StringRef AbsolutePath, std::vector<OptionsSource> &CurOptions) {
247 auto CurSize = CurOptions.size();
251 StringRef
Path = llvm::sys::path::parent_path(AbsolutePath);
252 for (StringRef CurrentPath =
Path; !CurrentPath.empty();
253 CurrentPath = llvm::sys::path::parent_path(CurrentPath)) {
254 llvm::Optional<OptionsSource> Result;
258 Result = Iter->second;
265 while (
Path != CurrentPath) {
266 LLVM_DEBUG(llvm::dbgs()
267 <<
"Caching configuration for path " <<
Path <<
".\n");
270 Path = llvm::sys::path::parent_path(
Path);
274 CurOptions.push_back(*Result);
275 if (!Result->first.InheritParentConfig.getValueOr(
false))
281 std::reverse(CurOptions.begin() + CurSize, CurOptions.end());
288 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS)
303 std::vector<OptionsSource>
305 LLVM_DEBUG(llvm::dbgs() <<
"Getting options for file " <<
FileName
307 assert(
FS &&
"FS must be set.");
309 llvm::SmallString<128> AbsoluteFilePath(
FileName);
311 if (
FS->makeAbsolute(AbsoluteFilePath))
314 std::vector<OptionsSource> RawOptions =
320 RawOptions.push_back(CommandLineOptions);
324 llvm::Optional<OptionsSource>
328 if (!llvm::sys::fs::is_directory(
Directory)) {
329 llvm::errs() <<
"Error reading configuration from " <<
Directory
330 <<
": directory doesn't exist.\n";
336 llvm::sys::path::append(ConfigFile, ConfigHandler.first);
337 LLVM_DEBUG(llvm::dbgs() <<
"Trying " << ConfigFile <<
"...\n");
342 llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
346 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
Text =
347 llvm::MemoryBuffer::getFile(ConfigFile.c_str());
348 if (std::error_code EC =
Text.getError()) {
349 llvm::errs() <<
"Can't read " << ConfigFile <<
": " << EC.message()
356 if ((*Text)->getBuffer().empty())
358 llvm::ErrorOr<ClangTidyOptions> ParsedOptions =
359 ConfigHandler.second((*Text)->getBuffer());
360 if (!ParsedOptions) {
361 if (ParsedOptions.getError())
362 llvm::errs() <<
"Error parsing " << ConfigFile <<
": "
363 << ParsedOptions.getError().message() <<
"\n";
376 return Input.error();
380 llvm::yaml::Input Input(
Config);
384 return Input.error();
390 llvm::raw_string_ostream Stream(
Text);
CharSourceRange Range
SourceRange for the file name.
ConfigOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions, const ClangTidyOptions &DefaultOptions, const ClangTidyOptions &ConfigOptions, const ClangTidyOptions &OverrideOptions, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS=nullptr)
std::vector< OptionsSource > getRawOptions(llvm::StringRef FileName) override
Returns an ordered vector of OptionsSources, in order of increasing priority.
Some operations such as code completion produce a set of candidates.
Implementation of the ClangTidyOptionsProvider interface, which returns the same options for all file...
llvm::Optional< std::string > Checks
Checks filter.
ConfigFileHandlers ConfigHandlers
std::error_code parseLineFilter(StringRef LineFilter, clang::tidy::ClangTidyGlobalOptions &Options)
Parses -line-filter option and stores it to the Options.
static unsigned & element(IO &IO, FileFilter::LineRange &Range, size_t Index)
static const char OptionsSourceTypeCheckCommandLineOption[]
void addRawFileOptions(llvm::StringRef AbsolutePath, std::vector< OptionsSource > &CurOptions)
Helper structure for storing option value with priority of the value.
Contains a list of line ranges in a single file.
llvm::Optional< ArgList > ExtraArgsBefore
Add extra compilation arguments to the start of the list.
static void mapping(IO &IO, ClangTidyOptions::StringPair &KeyValue)
llvm::Optional< std::string > User
Specifies the name or e-mail of the user running clang-tidy.
OptionMap CheckOptions
Key-value mapping used to store check-specific options.
static size_t size(IO &IO, FileFilter::LineRange &Range)
static void mergeVectors(Optional< T > &Dest, const Optional< T > &Src)
Contains options for clang-tidy.
static const char OptionsSourceTypeDefaultBinary[]
llvm::Optional< bool > InheritParentConfig
Only used in the FileOptionsProvider and ConfigOptionsProvider.
llvm::Optional< std::string > FormatStyle
Format code around applied fixes with clang-format using this style.
llvm::Optional< bool > UseColor
Use colors in diagnostics. If missing, it will be auto detected.
clang::tidy::ClangTidyOptionsProvider::OptionsSource OptionsSource
std::pair< ClangTidyOptions, std::string > OptionsSource
ClangTidyOptions and its source.
llvm::Optional< std::string > HeaderFilterRegex
Output warnings from headers matching this filter.
llvm::Optional< bool > SystemHeaders
Output warnings from system headers matching HeaderFilterRegex.
FileOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions, const ClangTidyOptions &DefaultOptions, const ClangTidyOptions &OverrideOptions, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS=nullptr)
Initializes the FileOptionsProvider instance.
std::vector< FileFilter > LineFilter
Output warnings from certain line ranges of certain files only.
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS
static StringRef validate(IO &io, FileFilter &File)
std::vector< ClangTidyOptions::StringPair > Options
std::vector< OptionsSource > getRawOptions(llvm::StringRef FileName) override
Returns an ordered vector of OptionsSources, in order of increasing priority.
llvm::ErrorOr< ClangTidyOptions > parseConfiguration(StringRef Config)
llvm::StringRef Directory
llvm::StringMap< OptionsSource > CachedOptions
std::string configurationAsText(const ClangTidyOptions &Options)
Serializes configuration to a YAML-encoded string.
llvm::Optional< OptionsSource > tryReadConfigFile(llvm::StringRef Directory)
Try to read configuration files from Directory using registered ConfigHandlers.
static void mapping(IO &IO, FileFilter &File)
static const char OptionsSourceTypeConfigCommandLineOption[]
FileOptionsBaseProvider(const ClangTidyGlobalOptions &GlobalOptions, const ClangTidyOptions &DefaultOptions, const ClangTidyOptions &OverrideOptions, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS=nullptr)
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))
const SymbolIndex * Index
static void mergeCommaSeparatedLists(Optional< std::string > &Dest, const Optional< std::string > &Src)
llvm::Optional< std::string > WarningsAsErrors
WarningsAsErrors filter.
static void overrideValue(Optional< T > &Dest, const Optional< T > &Src)
ClangTidyOptions getOptions(llvm::StringRef FileName)
Returns options applying to a specific translation unit with the specified FileName.
std::vector< ConfigFileHandler > ConfigFileHandlers
Configuration file handlers listed in the order of priority.
std::pair< std::string, std::function< llvm::ErrorOr< ClangTidyOptions > llvm::StringRef)> > ConfigFileHandler
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))
ClangTidyOptions::OptionMap denormalize(IO &)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
virtual std::vector< OptionsSource > getRawOptions(llvm::StringRef FileName)=0
Returns an ordered vector of OptionsSources, in order of increasing priority.
ClangTidyOptions OverrideOptions
ClangTidyOptions mergeWith(const ClangTidyOptions &Other, unsigned Order) const
Creates a new ClangTidyOptions instance combined from all fields of this instance overridden by the f...
static ClangTidyOptions getDefaults()
These options are used for all settings that haven't been overridden by the OptionsProvider.
static void mapping(IO &IO, ClangTidyOptions &Options)
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.
NOptionMap(IO &, const ClangTidyOptions::OptionMap &OptionMap)
std::vector< HeaderHandle > Path