clang-tools 8.0.0

ClangTidyOptions.h
Go to the documentation of this file.
1 //===--- ClangTidyOptions.h - clang-tidy ------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H
12
13 #include "llvm/ADT/IntrusiveRefCntPtr.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/ErrorOr.h"
18 #include "llvm/Support/VirtualFileSystem.h"
19 #include <functional>
20 #include <map>
21 #include <string>
22 #include <system_error>
23 #include <utility>
24 #include <vector>
25
26 namespace clang {
27 namespace tidy {
28
29 /// \brief Contains a list of line ranges in a single file.
30 struct FileFilter {
31 /// \brief File name.
32 std::string Name;
33
34 /// \brief LineRange is a pair<start, end> (inclusive).
35 typedef std::pair<unsigned, unsigned> LineRange;
36
37 /// \brief A list of line ranges in this file, for which we show warnings.
38 std::vector<LineRange> LineRanges;
39 };
40
41 /// \brief Global options. These options are neither stored nor read from
42 /// configuration files.
43 struct ClangTidyGlobalOptions {
44 /// \brief Output warnings from certain line ranges of certain files only.
45 /// If empty, no warnings will be filtered.
46 std::vector<FileFilter> LineFilter;
47 };
48
49 /// \brief Contains options for clang-tidy. These options may be read from
50 /// configuration files, and may be different for different translation units.
51 struct ClangTidyOptions {
52 /// \brief These options are used for all settings that haven't been
53 /// overridden by the \c OptionsProvider.
54 ///
55 /// Allow no checks and no headers by default. This method initializes
56 /// check-specific options by calling \c ClangTidyModule::getModuleOptions()
57 /// of each registered \c ClangTidyModule.
58 static ClangTidyOptions getDefaults();
59
60 /// \brief Creates a new \c ClangTidyOptions instance combined from all fields
61 /// of this instance overridden by the fields of \p Other that have a value.
62 ClangTidyOptions mergeWith(const ClangTidyOptions &Other) const;
63
64 /// \brief Checks filter.
65 llvm::Optional<std::string> Checks;
66
67 /// \brief WarningsAsErrors filter.
68 llvm::Optional<std::string> WarningsAsErrors;
69
70 /// \brief Output warnings from headers matching this filter. Warnings from
71 /// main files will always be displayed.
72 llvm::Optional<std::string> HeaderFilterRegex;
73
74 /// \brief Output warnings from system headers matching \c HeaderFilterRegex.
75 llvm::Optional<bool> SystemHeaders;
76
77 /// \brief Format code around applied fixes with clang-format using this
78 /// style.
79 ///
80 /// Can be one of:
81 /// * 'none' - don't format code around applied fixes;
82 /// * 'llvm', 'google', 'mozilla' or other predefined clang-format style
83 /// names;
84 /// * 'file' - use the .clang-format file in the closest parent directory of
85 /// each source file;
86 /// * '{inline-formatting-style-in-yaml-format}'.
87 ///
88 /// See clang-format documentation for more about configuring format style.
89 llvm::Optional<std::string> FormatStyle;
90
91 /// \brief Specifies the name or e-mail of the user running clang-tidy.
92 ///
93 /// This option is used, for example, to place the correct user name in TODO()
94 /// comments in the relevant check.
95 llvm::Optional<std::string> User;
96
97 typedef std::pair<std::string, std::string> StringPair;
98 typedef std::map<std::string, std::string> OptionMap;
99
100 /// \brief Key-value mapping used to store check-specific options.
101 OptionMap CheckOptions;
102
103 typedef std::vector<std::string> ArgList;
104
105 /// \brief Add extra compilation arguments to the end of the list.
106 llvm::Optional<ArgList> ExtraArgs;
107
108 /// \brief Add extra compilation arguments to the start of the list.
109 llvm::Optional<ArgList> ExtraArgsBefore;
110 };
111
112 /// \brief Abstract interface for retrieving various ClangTidy options.
113 class ClangTidyOptionsProvider {
114 public:
115 static const char OptionsSourceTypeDefaultBinary[];
116 static const char OptionsSourceTypeCheckCommandLineOption[];
117 static const char OptionsSourceTypeConfigCommandLineOption[];
118
119 virtual ~ClangTidyOptionsProvider() {}
120
121 /// \brief Returns global options, which are independent of the file.
122 virtual const ClangTidyGlobalOptions &getGlobalOptions() = 0;
123
124 /// \brief ClangTidyOptions and its source.
125 //
126 /// clang-tidy has 3 types of the sources in order of increasing priority:
127 /// * clang-tidy binary.
128 /// * '-config' commandline option or a specific configuration file. If the
129 /// commandline option is specified, clang-tidy will ignore the
130 /// configuration file.
131 /// * '-checks' commandline option.
132 typedef std::pair<ClangTidyOptions, std::string> OptionsSource;
133
134 /// \brief Returns an ordered vector of OptionsSources, in order of increasing
135 /// priority.
136 virtual std::vector<OptionsSource>
137 getRawOptions(llvm::StringRef FileName) = 0;
138
139 /// \brief Returns options applying to a specific translation unit with the
140 /// specified \p FileName.
141 ClangTidyOptions getOptions(llvm::StringRef FileName);
142 };
143
144 /// \brief Implementation of the \c ClangTidyOptionsProvider interface, which
145 /// returns the same options for all files.
146 class DefaultOptionsProvider : public ClangTidyOptionsProvider {
147 public:
148 DefaultOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
149 const ClangTidyOptions &Options)
150 : GlobalOptions(GlobalOptions), DefaultOptions(Options) {}
151 const ClangTidyGlobalOptions &getGlobalOptions() override {
152 return GlobalOptions;
153 }
154 std::vector<OptionsSource> getRawOptions(llvm::StringRef FileName) override;
155
156 private:
157 ClangTidyGlobalOptions GlobalOptions;
158 ClangTidyOptions DefaultOptions;
159 };
160
161 /// \brief Implementation of ClangTidyOptions interface, which is used for
162 /// '-config' command-line option.
163 class ConfigOptionsProvider : public DefaultOptionsProvider {
164 public:
165 ConfigOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
166 const ClangTidyOptions &DefaultOptions,
167 const ClangTidyOptions &ConfigOptions,
168 const ClangTidyOptions &OverrideOptions);
169 std::vector<OptionsSource> getRawOptions(llvm::StringRef FileName) override;
170
171 private:
172 ClangTidyOptions ConfigOptions;
173 ClangTidyOptions OverrideOptions;
174 };
175
176 /// \brief Implementation of the \c ClangTidyOptionsProvider interface, which
177 /// tries to find a configuration file in the closest parent directory of each
178 /// source file.
179 ///
180 /// By default, files named ".clang-tidy" will be considered, and the
181 /// \c clang::tidy::parseConfiguration function will be used for parsing, but a
182 /// custom set of configuration file names and parsing functions can be
183 /// specified using the appropriate constructor.
184 class FileOptionsProvider : public DefaultOptionsProvider {
185 public:
186 // \brief A pair of configuration file base name and a function parsing
187 // configuration from text in the corresponding format.
188 typedef std::pair<std::string, std::function<llvm::ErrorOr<ClangTidyOptions>(
189 llvm::StringRef)>>
190 ConfigFileHandler;
191
192 /// \brief Configuration file handlers listed in the order of priority.
193 ///
194 /// Custom configuration file formats can be supported by constructing the
195 /// list of handlers and passing it to the appropriate \c FileOptionsProvider
196 /// constructor. E.g. initialization of a \c FileOptionsProvider with support
197 /// of a custom configuration file format for files named ".my-tidy-config"
198 /// could look similar to this:
199 /// \code
200 /// FileOptionsProvider::ConfigFileHandlers ConfigHandlers;
201 /// ConfigHandlers.emplace_back(".my-tidy-config", parseMyConfigFormat);
202 /// ConfigHandlers.emplace_back(".clang-tidy", parseConfiguration);
203 /// return llvm::make_unique<FileOptionsProvider>(
204 /// GlobalOptions, DefaultOptions, OverrideOptions, ConfigHandlers);
205 /// \endcode
206 ///
207 /// With the order of handlers shown above, the ".my-tidy-config" file would
208 /// take precedence over ".clang-tidy" if both reside in the same directory.
209 typedef std::vector<ConfigFileHandler> ConfigFileHandlers;
210
211 /// \brief Initializes the \c FileOptionsProvider instance.
212 ///
213 /// \param GlobalOptions are just stored and returned to the caller of
214 /// \c getGlobalOptions.
215 ///
216 /// \param DefaultOptions are used for all settings not specified in a
217 /// configuration file.
218 ///
219 /// If any of the \param OverrideOptions fields are set, they will override
220 /// whatever options are read from the configuration file.
221 FileOptionsProvider(
222 const ClangTidyGlobalOptions &GlobalOptions,
223 const ClangTidyOptions &DefaultOptions,
224 const ClangTidyOptions &OverrideOptions,
225 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS = nullptr);
226
227 /// \brief Initializes the \c FileOptionsProvider instance with a custom set
228 /// of configuration file handlers.
229 ///
230 /// \param GlobalOptions are just stored and returned to the caller of
231 /// \c getGlobalOptions.
232 ///
233 /// \param DefaultOptions are used for all settings not specified in a
234 /// configuration file.
235 ///
236 /// If any of the \param OverrideOptions fields are set, they will override
237 /// whatever options are read from the configuration file.
238 ///
239 /// \param ConfigHandlers specifies a custom set of configuration file
240 /// handlers. Each handler is a pair of configuration file name and a function
241 /// that can parse configuration from this file type. The configuration files
242 /// in each directory are searched for in the order of appearance in
243 /// \p ConfigHandlers.
244 FileOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
245 const ClangTidyOptions &DefaultOptions,
246 const ClangTidyOptions &OverrideOptions,
247 const ConfigFileHandlers &ConfigHandlers);
248
249 std::vector<OptionsSource> getRawOptions(llvm::StringRef FileName) override;
250
251 protected:
252 /// \brief Try to read configuration files from \p Directory using registered
253 /// \c ConfigHandlers.
254 llvm::Optional<OptionsSource> tryReadConfigFile(llvm::StringRef Directory);
255
256 llvm::StringMap<OptionsSource> CachedOptions;
257 ClangTidyOptions OverrideOptions;
258 ConfigFileHandlers ConfigHandlers;
259 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
260 };
261
262 /// \brief Parses LineFilter from JSON and stores it to the \p Options.
263 std::error_code parseLineFilter(llvm::StringRef LineFilter,
264 ClangTidyGlobalOptions &Options);
265
266 /// \brief Parses configuration from JSON and returns \c ClangTidyOptions or an
267 /// error.
268 llvm::ErrorOr<ClangTidyOptions> parseConfiguration(llvm::StringRef Config);
269
270 /// \brief Serializes configuration to a YAML-encoded string.
271 std::string configurationAsText(const ClangTidyOptions &Options);
272
273 } // end namespace tidy
274 } // end namespace clang
275
276 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H
clang::tidy::ClangTidyOptions::Checks
llvm::Optional< std::string > Checks
Checks filter.
Definition: ClangTidyOptions.h:65
clang::tidy::ClangTidyOptions::ExtraArgs
llvm::Optional< ArgList > ExtraArgs
Add extra compilation arguments to the end of the list.
Definition: ClangTidyOptions.h:106
clang::tidy::FileOptionsProvider
Implementation of the ClangTidyOptionsProvider interface, which tries to find a configuration file in...
Definition: ClangTidyOptions.h:184
clang::tidy::ClangTidyOptions::User
llvm::Optional< std::string > User
Specifies the name or e-mail of the user running clang-tidy.
Definition: ClangTidyOptions.h:95
clang::tidy::ClangTidyOptions::ArgList
std::vector< std::string > ArgList
Definition: ClangTidyOptions.h:103
clang::tidy::ClangTidyOptions::StringPair
std::pair< std::string, std::string > StringPair
Definition: ClangTidyOptions.h:97
clang::tidy::ConfigOptionsProvider
Implementation of ClangTidyOptions interface, which is used for &#39;-config&#39; command-line option...
Definition: ClangTidyOptions.h:163
Config
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))
clang::tidy::ClangTidyOptions::HeaderFilterRegex
llvm::Optional< std::string > HeaderFilterRegex
Output warnings from headers matching this filter.
Definition: ClangTidyOptions.h:72
clang::tidy::ClangTidyOptions
Contains options for clang-tidy.
Definition: ClangTidyOptions.h:51
clang::tidy::ClangTidyGlobalOptions
Global options.
Definition: ClangTidyOptions.h:43
clang::tidy::parseLineFilter
std::error_code parseLineFilter(StringRef LineFilter, clang::tidy::ClangTidyGlobalOptions &Options)
Parses -line-filter option and stores it to the Options.
Definition: ClangTidyOptions.cpp:321
clang::tidy::parseConfiguration
llvm::ErrorOr< ClangTidyOptions > parseConfiguration(StringRef Config)
Definition: ClangTidyOptions.cpp:328
clang::tidy::ClangTidyOptions::CheckOptions
OptionMap CheckOptions
Key-value mapping used to store check-specific options.
Definition: ClangTidyOptions.h:101
clang::tidy::ClangTidyOptions::SystemHeaders
llvm::Optional< bool > SystemHeaders
Output warnings from system headers matching HeaderFilterRegex.
Definition: ClangTidyOptions.h:75
clang::tidy::FileOptionsProvider::OverrideOptions
ClangTidyOptions OverrideOptions
Definition: ClangTidyOptions.h:257
clang::tidy::ClangTidyOptionsProvider::~ClangTidyOptionsProvider
virtual ~ClangTidyOptionsProvider()
Definition: ClangTidyOptions.h:119
clang::tidy::FileOptionsProvider::ConfigHandlers
ConfigFileHandlers ConfigHandlers
Definition: ClangTidyOptions.h:258
clang::tidy::ClangTidyOptions::ExtraArgsBefore
llvm::Optional< ArgList > ExtraArgsBefore
Add extra compilation arguments to the start of the list.
Definition: ClangTidyOptions.h:109
clang::tidy::ClangTidyOptions::FormatStyle
llvm::Optional< std::string > FormatStyle
Format code around applied fixes with clang-format using this style.
Definition: ClangTidyOptions.h:89
Directory
static cl::opt< std::string > Directory(cl::Positional, cl::Required, cl::desc("<Search Root Directory>"))
LineFilter
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))
clang::tidy::FileFilter::LineRange
std::pair< unsigned, unsigned > LineRange
LineRange is a pair<start, end> (inclusive).
Definition: ClangTidyOptions.h:35
clang::tidy::FileOptionsProvider::FS
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS
Definition: ClangTidyOptions.h:259
clang::tidy::configurationAsText
std::string configurationAsText(const ClangTidyOptions &Options)
Serializes configuration to a YAML-encoded string.
Definition: ClangTidyOptions.cpp:337
clang::tidy::FileOptionsProvider::CachedOptions
llvm::StringMap< OptionsSource > CachedOptions
Definition: ClangTidyOptions.h:256
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:98
clang::tidy::ClangTidyGlobalOptions::LineFilter
std::vector< FileFilter > LineFilter
Output warnings from certain line ranges of certain files only.
Definition: ClangTidyOptions.h:46
FileName
PathRef FileName
Definition: CodeComplete.cpp:1004
clang::tidy::FileOptionsProvider::ConfigFileHandler
std::pair< std::string, std::function< llvm::ErrorOr< ClangTidyOptions > llvm::StringRef)> > ConfigFileHandler
Definition: ClangTidyOptions.h:190
clang::tidy::ClangTidyOptions::WarningsAsErrors
llvm::Optional< std::string > WarningsAsErrors
WarningsAsErrors filter.
Definition: ClangTidyOptions.h:68
clang::tidy::FileOptionsProvider::ConfigFileHandlers
std::vector< ConfigFileHandler > ConfigFileHandlers
Configuration file handlers listed in the order of priority.
Definition: ClangTidyOptions.h:209
clang::tidy::FileFilter
Contains a list of line ranges in a single file.
Definition: ClangTidyOptions.h:30
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::tidy::DefaultOptionsProvider::DefaultOptionsProvider
DefaultOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions, const ClangTidyOptions &Options)
Definition: ClangTidyOptions.h:148
clang::tidy::ClangTidyOptionsProvider::OptionsSource
std::pair< ClangTidyOptions, std::string > OptionsSource
ClangTidyOptions and its source.
Definition: ClangTidyOptions.h:132
clang::tidy::FileFilter::LineRanges
std::vector< LineRange > LineRanges
A list of line ranges in this file, for which we show warnings.
Definition: ClangTidyOptions.h:38
clang::tidy::ClangTidyOptionsProvider
Abstract interface for retrieving various ClangTidy options.
Definition: ClangTidyOptions.h:113
clang::tidy::DefaultOptionsProvider::getGlobalOptions
const ClangTidyGlobalOptions & getGlobalOptions() override
Returns global options, which are independent of the file.
Definition: ClangTidyOptions.h:151
clang::tidy::FileFilter::Name
std::string Name
File name.
Definition: ClangTidyOptions.h:32
clang::tidy::DefaultOptionsProvider
Implementation of the ClangTidyOptionsProvider interface, which returns the same options for all file...
Definition: ClangTidyOptions.h:146