blob: 8a89c59613c4b9deefcf518859a5cdf48b8090ac [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
10#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_OPTIONS_H
11#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_OPTIONS_H
12
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 Kornienkoa4695222014-06-05 13:31:45 +000069 /// \brief Turns on temporary destructor-based analysis.
Alexander Kornienkod53d2682014-09-04 14:23:36 +000070 llvm::Optional<bool> AnalyzeTemporaryDtors;
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000071
Alexander Kornienkoe9951542014-09-24 18:36:03 +000072 /// \brief Specifies the name or e-mail of the user running clang-tidy.
73 ///
74 /// This option is used, for example, to place the correct user name in TODO()
75 /// comments in the relevant check.
76 llvm::Optional<std::string> User;
77
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000078 typedef std::pair<std::string, std::string> StringPair;
79 typedef std::map<std::string, std::string> OptionMap;
80
81 /// \brief Key-value mapping used to store check-specific options.
82 OptionMap CheckOptions;
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +000083};
84
Alexander Kornienkoa4695222014-06-05 13:31:45 +000085/// \brief Abstract interface for retrieving various ClangTidy options.
86class ClangTidyOptionsProvider {
87public:
88 virtual ~ClangTidyOptionsProvider() {}
89
90 /// \brief Returns global options, which are independent of the file.
91 virtual const ClangTidyGlobalOptions &getGlobalOptions() = 0;
92
93 /// \brief Returns options applying to a specific translation unit with the
94 /// specified \p FileName.
95 virtual const ClangTidyOptions &getOptions(llvm::StringRef FileName) = 0;
96};
97
98/// \brief Implementation of the \c ClangTidyOptionsProvider interface, which
99/// returns the same options for all files.
100class DefaultOptionsProvider : public ClangTidyOptionsProvider {
101public:
102 DefaultOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
103 const ClangTidyOptions &Options)
104 : GlobalOptions(GlobalOptions), DefaultOptions(Options) {}
105 const ClangTidyGlobalOptions &getGlobalOptions() override {
106 return GlobalOptions;
107 }
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000108 const ClangTidyOptions &getOptions(llvm::StringRef /*FileName*/) override {
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000109 return DefaultOptions;
110 }
111
112private:
113 ClangTidyGlobalOptions GlobalOptions;
114 ClangTidyOptions DefaultOptions;
115};
116
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000117/// \brief Implementation of the \c ClangTidyOptionsProvider interface, which
Alexander Kornienko4f6b0662014-10-20 12:29:15 +0000118/// tries to find a configuration file in the closest parent directory of each
119/// source file.
120///
121/// By default, files named ".clang-tidy" will be considered, and the
122/// \c clang::tidy::parseConfiguration function will be used for parsing, but a
123/// custom set of configuration file names and parsing functions can be
124/// specified using the appropriate constructor.
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000125class FileOptionsProvider : public DefaultOptionsProvider {
126public:
Alexander Kornienko4f6b0662014-10-20 12:29:15 +0000127 // \brief A pair of configuration file base name and a function parsing
128 // configuration from text in the corresponding format.
129 typedef std::pair<std::string, std::function<llvm::ErrorOr<ClangTidyOptions>(
130 llvm::StringRef)>> ConfigFileHandler;
131
132 /// \brief Configuration file handlers listed in the order of priority.
133 ///
134 /// Custom configuration file formats can be supported by constructing the
135 /// list of handlers and passing it to the appropriate \c FileOptionsProvider
136 /// constructor. E.g. initialization of a \c FileOptionsProvider with support
137 /// of a custom configuration file format for files named ".my-tidy-config"
138 /// could look similar to this:
139 /// \code
140 /// FileOptionsProvider::ConfigFileHandlers ConfigHandlers;
141 /// ConfigHandlers.emplace_back(".my-tidy-config", parseMyConfigFormat);
142 /// ConfigHandlers.emplace_back(".clang-tidy", parseConfiguration);
143 /// return llvm::make_unique<FileOptionsProvider>(
144 /// GlobalOptions, DefaultOptions, OverrideOptions, ConfigHandlers);
145 /// \endcode
146 ///
147 /// With the order of handlers shown above, the ".my-tidy-config" file would
148 /// take precedence over ".clang-tidy" if both reside in the same directory.
149 typedef std::vector<ConfigFileHandler> ConfigFileHandlers;
150
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000151 /// \brief Initializes the \c FileOptionsProvider instance.
152 ///
153 /// \param GlobalOptions are just stored and returned to the caller of
154 /// \c getGlobalOptions.
155 ///
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000156 /// \param DefaultOptions are used for all settings not specified in a
Alexander Kornienko4f6b0662014-10-20 12:29:15 +0000157 /// configuration file.
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000158 ///
159 /// If any of the \param OverrideOptions fields are set, they will override
160 /// whatever options are read from the configuration file.
161 FileOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000162 const ClangTidyOptions &DefaultOptions,
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000163 const ClangTidyOptions &OverrideOptions);
Alexander Kornienko4f6b0662014-10-20 12:29:15 +0000164
165 /// \brief Initializes the \c FileOptionsProvider instance with a custom set
166 /// of configuration file handlers.
167 ///
168 /// \param GlobalOptions are just stored and returned to the caller of
169 /// \c getGlobalOptions.
170 ///
171 /// \param DefaultOptions are used for all settings not specified in a
172 /// configuration file.
173 ///
174 /// If any of the \param OverrideOptions fields are set, they will override
175 /// whatever options are read from the configuration file.
176 ///
177 /// \param ConfigHandlers specifies a custom set of configuration file
178 /// handlers. Each handler is a pair of configuration file name and a function
179 /// that can parse configuration from this file type. The configuration files
180 /// in each directory are searched for in the order of appearance in
181 /// \p ConfigHandlers.
182 FileOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
183 const ClangTidyOptions &DefaultOptions,
184 const ClangTidyOptions &OverrideOptions,
185 const ConfigFileHandlers &ConfigHandlers);
186
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000187 const ClangTidyOptions &getOptions(llvm::StringRef FileName) override;
188
189private:
Alexander Kornienko4f6b0662014-10-20 12:29:15 +0000190 /// \brief Try to read configuration files from \p Directory using registered
191 /// \c ConfigHandlers.
192 llvm::Optional<ClangTidyOptions> TryReadConfigFile(llvm::StringRef Directory);
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000193
194 llvm::StringMap<ClangTidyOptions> CachedOptions;
195 ClangTidyOptions OverrideOptions;
Alexander Kornienko4f6b0662014-10-20 12:29:15 +0000196 ConfigFileHandlers ConfigHandlers;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000197};
198
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000199/// \brief Parses LineFilter from JSON and stores it to the \p Options.
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000200std::error_code parseLineFilter(llvm::StringRef LineFilter,
201 ClangTidyGlobalOptions &Options);
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000202
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000203/// \brief Parses configuration from JSON and returns \c ClangTidyOptions or an
204/// error.
205llvm::ErrorOr<ClangTidyOptions> parseConfiguration(llvm::StringRef Config);
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000206
207/// \brief Serializes configuration to a YAML-encoded string.
208std::string configurationAsText(const ClangTidyOptions &Options);
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000209
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +0000210} // end namespace tidy
211} // end namespace clang
212
213#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_OPTIONS_H