blob: b3d956f84ffac74d02875e6f9e7b8ef72c10c366 [file] [log] [blame]
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +00001//===--- 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
Alexander Kornienko66580552015-03-09 16:52:33 +000010#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H
11#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +000012
Alexander Kornienkod53d2682014-09-04 14:23:36 +000013#include "llvm/ADT/Optional.h"
14#include "llvm/ADT/StringMap.h"
Alexander Kornienkoa4695222014-06-05 13:31:45 +000015#include "llvm/ADT/StringRef.h"
Alexander Kornienkod53d2682014-09-04 14:23:36 +000016#include "llvm/Support/ErrorOr.h"
Alexander Kornienko4f6b0662014-10-20 12:29:15 +000017#include <functional>
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000018#include <map>
Alexander Kornienko9ff5b6f2014-05-05 14:54:47 +000019#include <string>
Rafael Espindolafd85bb32014-06-12 16:53:02 +000020#include <system_error>
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000021#include <utility>
22#include <vector>
Alexander Kornienko9ff5b6f2014-05-05 14:54:47 +000023
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +000024namespace clang {
25namespace tidy {
26
Alexander Kornienkoa4695222014-06-05 13:31:45 +000027/// \brief Contains a list of line ranges in a single file.
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000028struct FileFilter {
Alexander Kornienkoa4695222014-06-05 13:31:45 +000029 /// \brief File name.
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000030 std::string Name;
Alexander Kornienkoa4695222014-06-05 13:31:45 +000031
32 /// \brief LineRange is a pair<start, end> (inclusive).
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000033 typedef std::pair<unsigned, unsigned> LineRange;
Alexander Kornienkoa4695222014-06-05 13:31:45 +000034
35 /// \brief A list of line ranges in this file, for which we show warnings.
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000036 std::vector<LineRange> LineRanges;
37};
38
Alexander Kornienkoa4695222014-06-05 13:31:45 +000039/// \brief Global options. These options are neither stored nor read from
40/// configuration files.
41struct ClangTidyGlobalOptions {
Alexander Kornienko3095b422014-06-12 11:25:45 +000042 /// \brief Output warnings from certain line ranges of certain files only.
43 /// If empty, no warnings will be filtered.
Alexander Kornienkoa4695222014-06-05 13:31:45 +000044 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.
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +000049struct ClangTidyOptions {
Alexander Kornienkod53d2682014-09-04 14:23:36 +000050 /// \brief These options are used for all settings that haven't been
51 /// overridden by the \c OptionsProvider.
52 ///
Alexander Kornienko1efc4252014-10-16 11:27:57 +000053 /// 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();
Alexander Kornienkod53d2682014-09-04 14:23:36 +000057
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;
Alexander Kornienkoa4695222014-06-05 13:31:45 +000061
62 /// \brief Checks filter.
Alexander Kornienkod53d2682014-09-04 14:23:36 +000063 llvm::Optional<std::string> Checks;
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000064
Alexander Kornienkoa4695222014-06-05 13:31:45 +000065 /// \brief Output warnings from headers matching this filter. Warnings from
66 /// main files will always be displayed.
Alexander Kornienkod53d2682014-09-04 14:23:36 +000067 llvm::Optional<std::string> HeaderFilterRegex;
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000068
Alexander Kornienko37f7abe2014-10-28 22:16:13 +000069 /// \brief Output warnings from system headers matching \c HeaderFilterRegex.
70 llvm::Optional<bool> SystemHeaders;
71
Alexander Kornienkoa4695222014-06-05 13:31:45 +000072 /// \brief Turns on temporary destructor-based analysis.
Alexander Kornienkod53d2682014-09-04 14:23:36 +000073 llvm::Optional<bool> AnalyzeTemporaryDtors;
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000074
Alexander Kornienkoe9951542014-09-24 18:36:03 +000075 /// \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
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000081 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;
Alexander Kornienko64956b52015-11-09 16:28:11 +000086
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;
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +000094};
95
Alexander Kornienkoa4695222014-06-05 13:31:45 +000096/// \brief Abstract interface for retrieving various ClangTidy options.
97class ClangTidyOptionsProvider {
98public:
David Blaikiee04a3da2015-10-20 21:45:52 +000099 virtual ~ClangTidyOptionsProvider() {}
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000100
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.
Alexander Kornienko9115a3a2015-01-20 09:48:51 +0000106 virtual ClangTidyOptions getOptions(llvm::StringRef FileName) = 0;
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000107};
108
109/// \brief Implementation of the \c ClangTidyOptionsProvider interface, which
110/// returns the same options for all files.
111class DefaultOptionsProvider : public ClangTidyOptionsProvider {
112public:
113 DefaultOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
114 const ClangTidyOptions &Options)
115 : GlobalOptions(GlobalOptions), DefaultOptions(Options) {}
116 const ClangTidyGlobalOptions &getGlobalOptions() override {
117 return GlobalOptions;
118 }
Alexander Kornienko9115a3a2015-01-20 09:48:51 +0000119 ClangTidyOptions getOptions(llvm::StringRef /*FileName*/) override {
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000120 return DefaultOptions;
121 }
122
123private:
124 ClangTidyGlobalOptions GlobalOptions;
125 ClangTidyOptions DefaultOptions;
126};
127
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000128/// \brief Implementation of the \c ClangTidyOptionsProvider interface, which
Alexander Kornienko4f6b0662014-10-20 12:29:15 +0000129/// 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.
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000136class FileOptionsProvider : public DefaultOptionsProvider {
137public:
Alexander Kornienko4f6b0662014-10-20 12:29:15 +0000138 // \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
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000162 /// \brief Initializes the \c FileOptionsProvider instance.
163 ///
164 /// \param GlobalOptions are just stored and returned to the caller of
165 /// \c getGlobalOptions.
166 ///
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000167 /// \param DefaultOptions are used for all settings not specified in a
Alexander Kornienko4f6b0662014-10-20 12:29:15 +0000168 /// configuration file.
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000169 ///
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,
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000173 const ClangTidyOptions &DefaultOptions,
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000174 const ClangTidyOptions &OverrideOptions);
Alexander Kornienko4f6b0662014-10-20 12:29:15 +0000175
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
Alexander Kornienko9115a3a2015-01-20 09:48:51 +0000198 ClangTidyOptions getOptions(llvm::StringRef FileName) override;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000199
Alexander Kornienkob7029d02015-08-12 19:29:57 +0000200protected:
Alexander Kornienko4f6b0662014-10-20 12:29:15 +0000201 /// \brief Try to read configuration files from \p Directory using registered
202 /// \c ConfigHandlers.
203 llvm::Optional<ClangTidyOptions> TryReadConfigFile(llvm::StringRef Directory);
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000204
205 llvm::StringMap<ClangTidyOptions> CachedOptions;
206 ClangTidyOptions OverrideOptions;
Alexander Kornienko4f6b0662014-10-20 12:29:15 +0000207 ConfigFileHandlers ConfigHandlers;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000208};
209
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000210/// \brief Parses LineFilter from JSON and stores it to the \p Options.
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000211std::error_code parseLineFilter(llvm::StringRef LineFilter,
212 ClangTidyGlobalOptions &Options);
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000213
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000214/// \brief Parses configuration from JSON and returns \c ClangTidyOptions or an
215/// error.
216llvm::ErrorOr<ClangTidyOptions> parseConfiguration(llvm::StringRef Config);
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000217
218/// \brief Serializes configuration to a YAML-encoded string.
219std::string configurationAsText(const ClangTidyOptions &Options);
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000220
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +0000221} // end namespace tidy
222} // end namespace clang
223
Alexander Kornienko66580552015-03-09 16:52:33 +0000224#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H