blob: c45baf3a0f050549b364f9b336a51b0b516c4aa8 [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 Kornienko6e0cbc82014-09-12 08:53:36 +000017#include <map>
Alexander Kornienko9ff5b6f2014-05-05 14:54:47 +000018#include <string>
Rafael Espindolafd85bb32014-06-12 16:53:02 +000019#include <system_error>
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000020#include <utility>
21#include <vector>
Alexander Kornienko9ff5b6f2014-05-05 14:54:47 +000022
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +000023namespace clang {
24namespace tidy {
25
Alexander Kornienkoa4695222014-06-05 13:31:45 +000026/// \brief Contains a list of line ranges in a single file.
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000027struct FileFilter {
Alexander Kornienkoa4695222014-06-05 13:31:45 +000028 /// \brief File name.
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000029 std::string Name;
Alexander Kornienkoa4695222014-06-05 13:31:45 +000030
31 /// \brief LineRange is a pair<start, end> (inclusive).
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000032 typedef std::pair<unsigned, unsigned> LineRange;
Alexander Kornienkoa4695222014-06-05 13:31:45 +000033
34 /// \brief A list of line ranges in this file, for which we show warnings.
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000035 std::vector<LineRange> LineRanges;
36};
37
Alexander Kornienkoa4695222014-06-05 13:31:45 +000038/// \brief Global options. These options are neither stored nor read from
39/// configuration files.
40struct ClangTidyGlobalOptions {
Alexander Kornienko3095b422014-06-12 11:25:45 +000041 /// \brief Output warnings from certain line ranges of certain files only.
42 /// If empty, no warnings will be filtered.
Alexander Kornienkoa4695222014-06-05 13:31:45 +000043 std::vector<FileFilter> LineFilter;
44};
45
46/// \brief Contains options for clang-tidy. These options may be read from
47/// configuration files, and may be different for different translation units.
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +000048struct ClangTidyOptions {
Alexander Kornienkod53d2682014-09-04 14:23:36 +000049 /// \brief These options are used for all settings that haven't been
50 /// overridden by the \c OptionsProvider.
51 ///
Alexander Kornienko1efc4252014-10-16 11:27:57 +000052 /// Allow no checks and no headers by default. This method initializes
53 /// check-specific options by calling \c ClangTidyModule::getModuleOptions()
54 /// of each registered \c ClangTidyModule.
55 static ClangTidyOptions getDefaults();
Alexander Kornienkod53d2682014-09-04 14:23:36 +000056
57 /// \brief Creates a new \c ClangTidyOptions instance combined from all fields
58 /// of this instance overridden by the fields of \p Other that have a value.
59 ClangTidyOptions mergeWith(const ClangTidyOptions &Other) const;
Alexander Kornienkoa4695222014-06-05 13:31:45 +000060
61 /// \brief Checks filter.
Alexander Kornienkod53d2682014-09-04 14:23:36 +000062 llvm::Optional<std::string> Checks;
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000063
Alexander Kornienkoa4695222014-06-05 13:31:45 +000064 /// \brief Output warnings from headers matching this filter. Warnings from
65 /// main files will always be displayed.
Alexander Kornienkod53d2682014-09-04 14:23:36 +000066 llvm::Optional<std::string> HeaderFilterRegex;
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000067
Alexander Kornienkoa4695222014-06-05 13:31:45 +000068 /// \brief Turns on temporary destructor-based analysis.
Alexander Kornienkod53d2682014-09-04 14:23:36 +000069 llvm::Optional<bool> AnalyzeTemporaryDtors;
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000070
Alexander Kornienkoe9951542014-09-24 18:36:03 +000071 /// \brief Specifies the name or e-mail of the user running clang-tidy.
72 ///
73 /// This option is used, for example, to place the correct user name in TODO()
74 /// comments in the relevant check.
75 llvm::Optional<std::string> User;
76
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000077 typedef std::pair<std::string, std::string> StringPair;
78 typedef std::map<std::string, std::string> OptionMap;
79
80 /// \brief Key-value mapping used to store check-specific options.
81 OptionMap CheckOptions;
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +000082};
83
Alexander Kornienkoa4695222014-06-05 13:31:45 +000084/// \brief Abstract interface for retrieving various ClangTidy options.
85class ClangTidyOptionsProvider {
86public:
87 virtual ~ClangTidyOptionsProvider() {}
88
89 /// \brief Returns global options, which are independent of the file.
90 virtual const ClangTidyGlobalOptions &getGlobalOptions() = 0;
91
92 /// \brief Returns options applying to a specific translation unit with the
93 /// specified \p FileName.
94 virtual const ClangTidyOptions &getOptions(llvm::StringRef FileName) = 0;
95};
96
97/// \brief Implementation of the \c ClangTidyOptionsProvider interface, which
98/// returns the same options for all files.
99class DefaultOptionsProvider : public ClangTidyOptionsProvider {
100public:
101 DefaultOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
102 const ClangTidyOptions &Options)
103 : GlobalOptions(GlobalOptions), DefaultOptions(Options) {}
104 const ClangTidyGlobalOptions &getGlobalOptions() override {
105 return GlobalOptions;
106 }
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000107 const ClangTidyOptions &getOptions(llvm::StringRef /*FileName*/) override {
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000108 return DefaultOptions;
109 }
110
111private:
112 ClangTidyGlobalOptions GlobalOptions;
113 ClangTidyOptions DefaultOptions;
114};
115
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000116/// \brief Implementation of the \c ClangTidyOptionsProvider interface, which
117/// tries to find a .clang-tidy file in the closest parent directory of each
118/// file.
119class FileOptionsProvider : public DefaultOptionsProvider {
120public:
121 /// \brief Initializes the \c FileOptionsProvider instance.
122 ///
123 /// \param GlobalOptions are just stored and returned to the caller of
124 /// \c getGlobalOptions.
125 ///
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000126 /// \param DefaultOptions are used for all settings not specified in a
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000127 /// .clang-tidy file.
128 ///
129 /// If any of the \param OverrideOptions fields are set, they will override
130 /// whatever options are read from the configuration file.
131 FileOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000132 const ClangTidyOptions &DefaultOptions,
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000133 const ClangTidyOptions &OverrideOptions);
134 const ClangTidyOptions &getOptions(llvm::StringRef FileName) override;
135
136private:
137 /// \brief Try to read configuration file from \p Directory. If \p Directory
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000138 /// is empty, use the default value.
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000139 llvm::ErrorOr<ClangTidyOptions> TryReadConfigFile(llvm::StringRef Directory);
140
141 llvm::StringMap<ClangTidyOptions> CachedOptions;
142 ClangTidyOptions OverrideOptions;
143};
144
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000145/// \brief Parses LineFilter from JSON and stores it to the \p Options.
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000146std::error_code parseLineFilter(llvm::StringRef LineFilter,
147 ClangTidyGlobalOptions &Options);
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000148
Alexander Kornienkoe9951542014-09-24 18:36:03 +0000149/// \brief Parses configuration from JSON and returns \c ClangTidyOptions or an
150/// error.
151llvm::ErrorOr<ClangTidyOptions> parseConfiguration(llvm::StringRef Config);
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000152
153/// \brief Serializes configuration to a YAML-encoded string.
154std::string configurationAsText(const ClangTidyOptions &Options);
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000155
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +0000156} // end namespace tidy
157} // end namespace clang
158
159#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_OPTIONS_H