blob: 889bc7246fdf6730cd557071f4e93f0001ba0fa5 [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 Kornienko33a9bcc2014-04-29 15:20:10 +000086};
87
Alexander Kornienkoa4695222014-06-05 13:31:45 +000088/// \brief Abstract interface for retrieving various ClangTidy options.
89class ClangTidyOptionsProvider {
90public:
91 virtual ~ClangTidyOptionsProvider() {}
92
93 /// \brief Returns global options, which are independent of the file.
94 virtual const ClangTidyGlobalOptions &getGlobalOptions() = 0;
95
96 /// \brief Returns options applying to a specific translation unit with the
97 /// specified \p FileName.
Alexander Kornienko9115a3a2015-01-20 09:48:51 +000098 virtual ClangTidyOptions getOptions(llvm::StringRef FileName) = 0;
Alexander Kornienkoa4695222014-06-05 13:31:45 +000099};
100
101/// \brief Implementation of the \c ClangTidyOptionsProvider interface, which
102/// returns the same options for all files.
103class DefaultOptionsProvider : public ClangTidyOptionsProvider {
104public:
105 DefaultOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
106 const ClangTidyOptions &Options)
107 : GlobalOptions(GlobalOptions), DefaultOptions(Options) {}
108 const ClangTidyGlobalOptions &getGlobalOptions() override {
109 return GlobalOptions;
110 }
Alexander Kornienko9115a3a2015-01-20 09:48:51 +0000111 ClangTidyOptions getOptions(llvm::StringRef /*FileName*/) override {
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000112 return DefaultOptions;
113 }
114
115private:
116 ClangTidyGlobalOptions GlobalOptions;
117 ClangTidyOptions DefaultOptions;
118};
119
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000120/// \brief Implementation of the \c ClangTidyOptionsProvider interface, which
Alexander Kornienko4f6b0662014-10-20 12:29:15 +0000121/// tries to find a configuration file in the closest parent directory of each
122/// source file.
123///
124/// By default, files named ".clang-tidy" will be considered, and the
125/// \c clang::tidy::parseConfiguration function will be used for parsing, but a
126/// custom set of configuration file names and parsing functions can be
127/// specified using the appropriate constructor.
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000128class FileOptionsProvider : public DefaultOptionsProvider {
129public:
Alexander Kornienko4f6b0662014-10-20 12:29:15 +0000130 // \brief A pair of configuration file base name and a function parsing
131 // configuration from text in the corresponding format.
132 typedef std::pair<std::string, std::function<llvm::ErrorOr<ClangTidyOptions>(
133 llvm::StringRef)>> ConfigFileHandler;
134
135 /// \brief Configuration file handlers listed in the order of priority.
136 ///
137 /// Custom configuration file formats can be supported by constructing the
138 /// list of handlers and passing it to the appropriate \c FileOptionsProvider
139 /// constructor. E.g. initialization of a \c FileOptionsProvider with support
140 /// of a custom configuration file format for files named ".my-tidy-config"
141 /// could look similar to this:
142 /// \code
143 /// FileOptionsProvider::ConfigFileHandlers ConfigHandlers;
144 /// ConfigHandlers.emplace_back(".my-tidy-config", parseMyConfigFormat);
145 /// ConfigHandlers.emplace_back(".clang-tidy", parseConfiguration);
146 /// return llvm::make_unique<FileOptionsProvider>(
147 /// GlobalOptions, DefaultOptions, OverrideOptions, ConfigHandlers);
148 /// \endcode
149 ///
150 /// With the order of handlers shown above, the ".my-tidy-config" file would
151 /// take precedence over ".clang-tidy" if both reside in the same directory.
152 typedef std::vector<ConfigFileHandler> ConfigFileHandlers;
153
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000154 /// \brief Initializes the \c FileOptionsProvider instance.
155 ///
156 /// \param GlobalOptions are just stored and returned to the caller of
157 /// \c getGlobalOptions.
158 ///
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000159 /// \param DefaultOptions are used for all settings not specified in a
Alexander Kornienko4f6b0662014-10-20 12:29:15 +0000160 /// configuration file.
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000161 ///
162 /// If any of the \param OverrideOptions fields are set, they will override
163 /// whatever options are read from the configuration file.
164 FileOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000165 const ClangTidyOptions &DefaultOptions,
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000166 const ClangTidyOptions &OverrideOptions);
Alexander Kornienko4f6b0662014-10-20 12:29:15 +0000167
168 /// \brief Initializes the \c FileOptionsProvider instance with a custom set
169 /// of configuration file handlers.
170 ///
171 /// \param GlobalOptions are just stored and returned to the caller of
172 /// \c getGlobalOptions.
173 ///
174 /// \param DefaultOptions are used for all settings not specified in a
175 /// configuration file.
176 ///
177 /// If any of the \param OverrideOptions fields are set, they will override
178 /// whatever options are read from the configuration file.
179 ///
180 /// \param ConfigHandlers specifies a custom set of configuration file
181 /// handlers. Each handler is a pair of configuration file name and a function
182 /// that can parse configuration from this file type. The configuration files
183 /// in each directory are searched for in the order of appearance in
184 /// \p ConfigHandlers.
185 FileOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
186 const ClangTidyOptions &DefaultOptions,
187 const ClangTidyOptions &OverrideOptions,
188 const ConfigFileHandlers &ConfigHandlers);
189
Alexander Kornienko9115a3a2015-01-20 09:48:51 +0000190 ClangTidyOptions getOptions(llvm::StringRef FileName) override;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000191
192private:
Alexander Kornienko4f6b0662014-10-20 12:29:15 +0000193 /// \brief Try to read configuration files from \p Directory using registered
194 /// \c ConfigHandlers.
195 llvm::Optional<ClangTidyOptions> TryReadConfigFile(llvm::StringRef Directory);
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000196
197 llvm::StringMap<ClangTidyOptions> CachedOptions;
198 ClangTidyOptions OverrideOptions;
Alexander Kornienko4f6b0662014-10-20 12:29:15 +0000199 ConfigFileHandlers ConfigHandlers;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000200};
201
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000202/// \brief Parses LineFilter from JSON and stores it to the \p Options.
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000203std::error_code parseLineFilter(llvm::StringRef LineFilter,
204 ClangTidyGlobalOptions &Options);
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000205
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000206/// \brief Parses configuration from JSON and returns \c ClangTidyOptions or an
207/// error.
208llvm::ErrorOr<ClangTidyOptions> parseConfiguration(llvm::StringRef Config);
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000209
210/// \brief Serializes configuration to a YAML-encoded string.
211std::string configurationAsText(const ClangTidyOptions &Options);
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000212
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +0000213} // end namespace tidy
214} // end namespace clang
215
Alexander Kornienko66580552015-03-09 16:52:33 +0000216#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H