clang-tools  3.8.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/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.
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.
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.
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.
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.
98 public:
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.
112 public:
114  const ClangTidyOptions &Options)
115  : GlobalOptions(GlobalOptions), DefaultOptions(Options) {}
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.
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,
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,
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;
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
llvm::Optional< std::string > Checks
Checks filter.
llvm::Optional< ArgList > ExtraArgs
Add extra compilation arguments to the end of the list.
Implementation of the ClangTidyOptionsProvider interface, which tries to find a configuration file in...
llvm::Optional< std::string > User
Specifies the name or e-mail of the user running clang-tidy.
std::vector< std::string > ArgList
std::pair< std::string, std::string > StringPair
llvm::Optional< std::string > HeaderFilterRegex
Output warnings from headers matching this filter.
Contains options for clang-tidy.
std::error_code parseLineFilter(StringRef LineFilter, clang::tidy::ClangTidyGlobalOptions &Options)
Parses -line-filter option and stores it to the Options.
llvm::ErrorOr< ClangTidyOptions > parseConfiguration(StringRef Config)
OptionMap CheckOptions
Key-value mapping used to store check-specific options.
llvm::Optional< bool > SystemHeaders
Output warnings from system headers matching HeaderFilterRegex.
virtual ClangTidyOptions getOptions(llvm::StringRef FileName)=0
Returns options applying to a specific translation unit with the specified FileName.
llvm::Optional< ArgList > ExtraArgsBefore
Add extra compilation arguments to the start of the list.
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))
static cl::opt< std::string > Directory(cl::Positional, cl::Required, cl::desc("<Search Root Directory>"))
std::pair< unsigned, unsigned > LineRange
LineRange is a pair<start, end> (inclusive).
std::string configurationAsText(const ClangTidyOptions &Options)
Serializes configuration to a YAML-encoded string.
std::map< std::string, std::string > OptionMap
std::vector< FileFilter > LineFilter
Output warnings from certain line ranges of certain files only.
ClangTidyOptions getOptions(llvm::StringRef) override
Returns options applying to a specific translation unit with the specified FileName.
ClangTidyOptions mergeWith(const ClangTidyOptions &Other) const
Creates a new ClangTidyOptions instance combined from all fields of this instance overridden by the f...
std::pair< std::string, std::function< llvm::ErrorOr< ClangTidyOptions > llvm::StringRef)> > ConfigFileHandler
virtual const ClangTidyGlobalOptions & getGlobalOptions()=0
Returns global options, which are independent of the file.
std::vector< ConfigFileHandler > ConfigFileHandlers
Configuration file handlers listed in the order of priority.
llvm::Optional< ClangTidyOptions > TryReadConfigFile(llvm::StringRef Directory)
Try to read configuration files from Directory using registered ConfigHandlers.
Contains a list of line ranges in a single file.
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))
llvm::Optional< bool > AnalyzeTemporaryDtors
Turns on temporary destructor-based analysis.
llvm::StringMap< ClangTidyOptions > CachedOptions
DefaultOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions, const ClangTidyOptions &Options)
ClangTidyOptions getOptions(llvm::StringRef FileName) override
Returns options applying to a specific translation unit with the specified FileName.
std::vector< LineRange > LineRanges
A list of line ranges in this file, for which we show warnings.
static ClangTidyOptions getDefaults()
These options are used for all settings that haven't been overridden by the OptionsProvider.
Abstract interface for retrieving various ClangTidy options.
const ClangTidyGlobalOptions & getGlobalOptions() override
Returns global options, which are independent of the file.
FileOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions, const ClangTidyOptions &DefaultOptions, const ClangTidyOptions &OverrideOptions)
Initializes the FileOptionsProvider instance.
std::string Name
File name.
Implementation of the ClangTidyOptionsProvider interface, which returns the same options for all file...