clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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/Optional.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/ErrorOr.h"
17 #include <functional>
18 #include <map>
19 #include <string>
20 #include <system_error>
21 #include <utility>
22 #include <vector>
23
24 namespace clang {
25 namespace tidy {
26
27 /// \brief Contains a list of line ranges in a single file.
28 struct FileFilter {
29 /// \brief File name.
30 std::string Name;
31
32 /// \brief LineRange is a pair<start, end> (inclusive).
33 typedef std::pair<unsigned, unsigned> LineRange;
34
35 /// \brief A list of line ranges in this file, for which we show warnings.
36 std::vector<LineRange> LineRanges;
37 };
38
39 /// \brief Global options. These options are neither stored nor read from
40 /// configuration files.
41 struct ClangTidyGlobalOptions {
42 /// \brief Output warnings from certain line ranges of certain files only.
43 /// If empty, no warnings will be filtered.
44 std::vector<FileFilter> LineFilter;
45 };
46
47 /// \brief Contains options for clang-tidy. These options may be read from
48 /// configuration files, and may be different for different translation units.
49 struct ClangTidyOptions {
50 /// \brief These options are used for all settings that haven't been
51 /// overridden by the \c OptionsProvider.
52 ///
53 /// Allow no checks and no headers by default. This method initializes
54 /// check-specific options by calling \c ClangTidyModule::getModuleOptions()
55 /// of each registered \c ClangTidyModule.
56 static ClangTidyOptions getDefaults();
57
58 /// \brief Creates a new \c ClangTidyOptions instance combined from all fields
59 /// of this instance overridden by the fields of \p Other that have a value.
60 ClangTidyOptions mergeWith(const ClangTidyOptions &Other) const;
61
62 /// \brief Checks filter.
63 llvm::Optional<std::string> Checks;
64
65 /// \brief Output warnings from headers matching this filter. Warnings from
66 /// main files will always be displayed.
67 llvm::Optional<std::string> HeaderFilterRegex;
68
69 /// \brief Output warnings from system headers matching \c HeaderFilterRegex.
70 llvm::Optional<bool> SystemHeaders;
71
72 /// \brief Turns on temporary destructor-based analysis.
73 llvm::Optional<bool> AnalyzeTemporaryDtors;
74
75 /// \brief Specifies the name or e-mail of the user running clang-tidy.
76 ///
77 /// This option is used, for example, to place the correct user name in TODO()
78 /// comments in the relevant check.
79 llvm::Optional<std::string> User;
80
81 typedef std::pair<std::string, std::string> StringPair;
82 typedef std::map<std::string, std::string> OptionMap;
83
84 /// \brief Key-value mapping used to store check-specific options.
85 OptionMap CheckOptions;
86
87 typedef std::vector<std::string> ArgList;
88
89 /// \brief Add extra compilation arguments to the end of the list.
90 llvm::Optional<ArgList> ExtraArgs;
91
92 /// \brief Add extra compilation arguments to the start of the list.
93 llvm::Optional<ArgList> ExtraArgsBefore;
94 };
95
96 /// \brief Abstract interface for retrieving various ClangTidy options.
97 class ClangTidyOptionsProvider {
98 public:
99 virtual ~ClangTidyOptionsProvider() {}
100
101 /// \brief Returns global options, which are independent of the file.
102 virtual const ClangTidyGlobalOptions &getGlobalOptions() = 0;
103
104 /// \brief Returns options applying to a specific translation unit with the
105 /// specified \p FileName.
106 virtual ClangTidyOptions getOptions(llvm::StringRef FileName) = 0;
107 };
108
109 /// \brief Implementation of the \c ClangTidyOptionsProvider interface, which
110 /// returns the same options for all files.
111 class DefaultOptionsProvider : public ClangTidyOptionsProvider {
112 public:
113 DefaultOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
114 const ClangTidyOptions &Options)
115 : GlobalOptions(GlobalOptions), DefaultOptions(Options) {}
116 const ClangTidyGlobalOptions &getGlobalOptions() override {
117 return GlobalOptions;
118 }
119 ClangTidyOptions getOptions(llvm::StringRef /*FileName*/) override {
120 return DefaultOptions;
121 }
122
123 private:
124 ClangTidyGlobalOptions GlobalOptions;
125 ClangTidyOptions DefaultOptions;
126 };
127
128 /// \brief Implementation of the \c ClangTidyOptionsProvider interface, which
129 /// tries to find a configuration file in the closest parent directory of each
130 /// source file.
131 ///
132 /// By default, files named ".clang-tidy" will be considered, and the
133 /// \c clang::tidy::parseConfiguration function will be used for parsing, but a
134 /// custom set of configuration file names and parsing functions can be
135 /// specified using the appropriate constructor.
136 class FileOptionsProvider : public DefaultOptionsProvider {
137 public:
138 // \brief A pair of configuration file base name and a function parsing
139 // configuration from text in the corresponding format.
140 typedef std::pair<std::string, std::function<llvm::ErrorOr<ClangTidyOptions>(
141 llvm::StringRef)>> ConfigFileHandler;
142
143 /// \brief Configuration file handlers listed in the order of priority.
144 ///
145 /// Custom configuration file formats can be supported by constructing the
146 /// list of handlers and passing it to the appropriate \c FileOptionsProvider
147 /// constructor. E.g. initialization of a \c FileOptionsProvider with support
148 /// of a custom configuration file format for files named ".my-tidy-config"
149 /// could look similar to this:
150 /// \code
151 /// FileOptionsProvider::ConfigFileHandlers ConfigHandlers;
152 /// ConfigHandlers.emplace_back(".my-tidy-config", parseMyConfigFormat);
153 /// ConfigHandlers.emplace_back(".clang-tidy", parseConfiguration);
154 /// return llvm::make_unique<FileOptionsProvider>(
155 /// GlobalOptions, DefaultOptions, OverrideOptions, ConfigHandlers);
156 /// \endcode
157 ///
158 /// With the order of handlers shown above, the ".my-tidy-config" file would
159 /// take precedence over ".clang-tidy" if both reside in the same directory.
160 typedef std::vector<ConfigFileHandler> ConfigFileHandlers;
161
162 /// \brief Initializes the \c FileOptionsProvider instance.
163 ///
164 /// \param GlobalOptions are just stored and returned to the caller of
165 /// \c getGlobalOptions.
166 ///
167 /// \param DefaultOptions are used for all settings not specified in a
168 /// configuration file.
169 ///
170 /// If any of the \param OverrideOptions fields are set, they will override
171 /// whatever options are read from the configuration file.
172 FileOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
173 const ClangTidyOptions &DefaultOptions,
174 const ClangTidyOptions &OverrideOptions);
175
176 /// \brief Initializes the \c FileOptionsProvider instance with a custom set
177 /// of configuration file handlers.
178 ///
179 /// \param GlobalOptions are just stored and returned to the caller of
180 /// \c getGlobalOptions.
181 ///
182 /// \param DefaultOptions are used for all settings not specified in a
183 /// configuration file.
184 ///
185 /// If any of the \param OverrideOptions fields are set, they will override
186 /// whatever options are read from the configuration file.
187 ///
188 /// \param ConfigHandlers specifies a custom set of configuration file
189 /// handlers. Each handler is a pair of configuration file name and a function
190 /// that can parse configuration from this file type. The configuration files
191 /// in each directory are searched for in the order of appearance in
192 /// \p ConfigHandlers.
193 FileOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
194 const ClangTidyOptions &DefaultOptions,
195 const ClangTidyOptions &OverrideOptions,
196 const ConfigFileHandlers &ConfigHandlers);
197
198 ClangTidyOptions getOptions(llvm::StringRef FileName) override;
199
200 protected:
201 /// \brief Try to read configuration files from \p Directory using registered
202 /// \c ConfigHandlers.
203 llvm::Optional<ClangTidyOptions> TryReadConfigFile(llvm::StringRef Directory);
204
205 llvm::StringMap<ClangTidyOptions> CachedOptions;
206 ClangTidyOptions OverrideOptions;
207 ConfigFileHandlers ConfigHandlers;
208 };
209
210 /// \brief Parses LineFilter from JSON and stores it to the \p Options.
211 std::error_code parseLineFilter(llvm::StringRef LineFilter,
212 ClangTidyGlobalOptions &Options);
213
214 /// \brief Parses configuration from JSON and returns \c ClangTidyOptions or an
215 /// error.
216 llvm::ErrorOr<ClangTidyOptions> parseConfiguration(llvm::StringRef Config);
217
218 /// \brief Serializes configuration to a YAML-encoded string.
219 std::string configurationAsText(const ClangTidyOptions &Options);
220
221 } // end namespace tidy
222 } // end namespace clang
223
224 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H
clang::tidy::ClangTidyOptions::Checks
llvm::Optional< std::string > Checks
Checks filter.
Definition: ClangTidyOptions.h:63
clang::tidy::ClangTidyOptions::ExtraArgs
llvm::Optional< ArgList > ExtraArgs
Add extra compilation arguments to the end of the list.
Definition: ClangTidyOptions.h:90
clang::tidy::FileOptionsProvider
Implementation of the ClangTidyOptionsProvider interface, which tries to find a configuration file in...
Definition: ClangTidyOptions.h:136
clang::tidy::ClangTidyOptions::User
llvm::Optional< std::string > User
Specifies the name or e-mail of the user running clang-tidy.
Definition: ClangTidyOptions.h:79
clang::tidy::ClangTidyOptions::ArgList
std::vector< std::string > ArgList
Definition: ClangTidyOptions.h:87
clang::tidy::ClangTidyOptions::StringPair
std::pair< std::string, std::string > StringPair
Definition: ClangTidyOptions.h:81
clang::tidy::ClangTidyOptions::HeaderFilterRegex
llvm::Optional< std::string > HeaderFilterRegex
Output warnings from headers matching this filter.
Definition: ClangTidyOptions.h:67
clang::tidy::ClangTidyOptions
Contains options for clang-tidy.
Definition: ClangTidyOptions.h:49
clang::tidy::ClangTidyGlobalOptions
Global options.
Definition: ClangTidyOptions.h:41
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:259
clang::tidy::parseConfiguration
llvm::ErrorOr< ClangTidyOptions > parseConfiguration(StringRef Config)
Definition: ClangTidyOptions.cpp:266
clang::tidy::ClangTidyOptions::CheckOptions
OptionMap CheckOptions
Key-value mapping used to store check-specific options.
Definition: ClangTidyOptions.h:85
clang::tidy::ClangTidyOptions::SystemHeaders
llvm::Optional< bool > SystemHeaders
Output warnings from system headers matching HeaderFilterRegex.
Definition: ClangTidyOptions.h:70
clang::tidy::ClangTidyOptionsProvider::getOptions
virtual ClangTidyOptions getOptions(llvm::StringRef FileName)=0
Returns options applying to a specific translation unit with the specified FileName.
clang::tidy::FileOptionsProvider::OverrideOptions
ClangTidyOptions OverrideOptions
Definition: ClangTidyOptions.h:206
clang::tidy::ClangTidyOptionsProvider::~ClangTidyOptionsProvider
virtual ~ClangTidyOptionsProvider()
Definition: ClangTidyOptions.h:99
clang::tidy::FileOptionsProvider::ConfigHandlers
ConfigFileHandlers ConfigHandlers
Definition: ClangTidyOptions.h:207
clang::tidy::ClangTidyOptions::ExtraArgsBefore
llvm::Optional< ArgList > ExtraArgsBefore
Add extra compilation arguments to the start of the list.
Definition: ClangTidyOptions.h:93
LineFilter
static cl::opt< std::string > LineFilter("line-filter", cl::desc("List of files with line ranges to filter the\n""warnings. Can be used together with\n""-header-filter. The format of the list is a JSON\n""array of objects:\n"" [\n"" {\"name\":\"file1.cpp\",\"lines\":[[1,3],[5,7]]},\n"" {\"name\":\"file2.h\"}\n"" ]"), cl::init(""), cl::cat(ClangTidyCategory))
Directory
static cl::opt< std::string > Directory(cl::Positional, cl::Required, cl::desc("<Search Root Directory>"))
clang::tidy::FileFilter::LineRange
std::pair< unsigned, unsigned > LineRange
LineRange is a pair<start, end> (inclusive).
Definition: ClangTidyOptions.h:33
clang::tidy::configurationAsText
std::string configurationAsText(const ClangTidyOptions &Options)
Serializes configuration to a YAML-encoded string.
Definition: ClangTidyOptions.cpp:275
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:82
clang::tidy::ClangTidyGlobalOptions::LineFilter
std::vector< FileFilter > LineFilter
Output warnings from certain line ranges of certain files only.
Definition: ClangTidyOptions.h:44
clang::tidy::DefaultOptionsProvider::getOptions
ClangTidyOptions getOptions(llvm::StringRef) override
Returns options applying to a specific translation unit with the specified FileName.
Definition: ClangTidyOptions.h:119
clang::tidy::ClangTidyOptions::mergeWith
ClangTidyOptions mergeWith(const ClangTidyOptions &Other) const
Creates a new ClangTidyOptions instance combined from all fields of this instance overridden by the f...
Definition: ClangTidyOptions.cpp:118
clang::tidy::FileOptionsProvider::ConfigFileHandler
std::pair< std::string, std::function< llvm::ErrorOr< ClangTidyOptions > llvm::StringRef)> > ConfigFileHandler
Definition: ClangTidyOptions.h:141
clang::tidy::ClangTidyOptionsProvider::getGlobalOptions
virtual const ClangTidyGlobalOptions & getGlobalOptions()=0
Returns global options, which are independent of the file.
clang::tidy::FileOptionsProvider::ConfigFileHandlers
std::vector< ConfigFileHandler > ConfigFileHandlers
Configuration file handlers listed in the order of priority.
Definition: ClangTidyOptions.h:160
clang::tidy::FileOptionsProvider::TryReadConfigFile
llvm::Optional< ClangTidyOptions > TryReadConfigFile(llvm::StringRef Directory)
Try to read configuration files from Directory using registered ConfigHandlers.
Definition: ClangTidyOptions.cpp:209
clang::tidy::FileFilter
Contains a list of line ranges in a single file.
Definition: ClangTidyOptions.h:28
Config
static cl::opt< std::string > Config("config", cl::desc("Specifies a configuration in YAML/JSON format:\n"" -config=\"{Checks: '*', CheckOptions: [{key: x, value: y}]}\"\n""When the value is empty, clang-tidy will attempt to find\n""a file named .clang-tidy for each source file in its parent\n""directories."), cl::init(""), cl::cat(ClangTidyCategory))
clang::tidy::ClangTidyOptions::AnalyzeTemporaryDtors
llvm::Optional< bool > AnalyzeTemporaryDtors
Turns on temporary destructor-based analysis.
Definition: ClangTidyOptions.h:73
clang::tidy::FileOptionsProvider::CachedOptions
llvm::StringMap< ClangTidyOptions > CachedOptions
Definition: ClangTidyOptions.h:205
clang::tidy::DefaultOptionsProvider::DefaultOptionsProvider
DefaultOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions, const ClangTidyOptions &Options)
Definition: ClangTidyOptions.h:113
clang::tidy::FileOptionsProvider::getOptions
ClangTidyOptions getOptions(llvm::StringRef FileName) override
Returns options applying to a specific translation unit with the specified FileName.
Definition: ClangTidyOptions.cpp:169
clang::tidy::FileFilter::LineRanges
std::vector< LineRange > LineRanges
A list of line ranges in this file, for which we show warnings.
Definition: ClangTidyOptions.h:36
clang::tidy::ClangTidyOptions::getDefaults
static ClangTidyOptions getDefaults()
These options are used for all settings that haven't been overridden by the OptionsProvider.
Definition: ClangTidyOptions.cpp:103
clang::tidy::ClangTidyOptionsProvider
Abstract interface for retrieving various ClangTidy options.
Definition: ClangTidyOptions.h:97
clang::tidy::DefaultOptionsProvider::getGlobalOptions
const ClangTidyGlobalOptions & getGlobalOptions() override
Returns global options, which are independent of the file.
Definition: ClangTidyOptions.h:116
clang::tidy::FileOptionsProvider::FileOptionsProvider
FileOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions, const ClangTidyOptions &DefaultOptions, const ClangTidyOptions &OverrideOptions)
Initializes the FileOptionsProvider instance.
Definition: ClangTidyOptions.cpp:146
clang::tidy::FileFilter::Name
std::string Name
File name.
Definition: ClangTidyOptions.h:30
clang::tidy::DefaultOptionsProvider
Implementation of the ClangTidyOptionsProvider interface, which returns the same options for all file...
Definition: ClangTidyOptions.h:111