blob: 3c2251c4bd34ec276bddabf5649278e0af517cb6 [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 ///
52 /// Allow no checks and no headers by default.
53 static ClangTidyOptions getDefaults() {
54 ClangTidyOptions Options;
55 Options.Checks = "";
56 Options.HeaderFilterRegex = "";
57 Options.AnalyzeTemporaryDtors = false;
58 return Options;
59 }
60
61 /// \brief Creates a new \c ClangTidyOptions instance combined from all fields
62 /// of this instance overridden by the fields of \p Other that have a value.
63 ClangTidyOptions mergeWith(const ClangTidyOptions &Other) const;
Alexander Kornienkoa4695222014-06-05 13:31:45 +000064
65 /// \brief Checks filter.
Alexander Kornienkod53d2682014-09-04 14:23:36 +000066 llvm::Optional<std::string> Checks;
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000067
Alexander Kornienkoa4695222014-06-05 13:31:45 +000068 /// \brief Output warnings from headers matching this filter. Warnings from
69 /// main files will always be displayed.
Alexander Kornienkod53d2682014-09-04 14:23:36 +000070 llvm::Optional<std::string> HeaderFilterRegex;
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000071
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
75 typedef std::pair<std::string, std::string> StringPair;
76 typedef std::map<std::string, std::string> OptionMap;
77
78 /// \brief Key-value mapping used to store check-specific options.
79 OptionMap CheckOptions;
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +000080};
81
Alexander Kornienkoa4695222014-06-05 13:31:45 +000082/// \brief Abstract interface for retrieving various ClangTidy options.
83class ClangTidyOptionsProvider {
84public:
85 virtual ~ClangTidyOptionsProvider() {}
86
87 /// \brief Returns global options, which are independent of the file.
88 virtual const ClangTidyGlobalOptions &getGlobalOptions() = 0;
89
90 /// \brief Returns options applying to a specific translation unit with the
91 /// specified \p FileName.
92 virtual const ClangTidyOptions &getOptions(llvm::StringRef FileName) = 0;
93};
94
95/// \brief Implementation of the \c ClangTidyOptionsProvider interface, which
96/// returns the same options for all files.
97class DefaultOptionsProvider : public ClangTidyOptionsProvider {
98public:
99 DefaultOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
100 const ClangTidyOptions &Options)
101 : GlobalOptions(GlobalOptions), DefaultOptions(Options) {}
102 const ClangTidyGlobalOptions &getGlobalOptions() override {
103 return GlobalOptions;
104 }
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000105 const ClangTidyOptions &getOptions(llvm::StringRef /*FileName*/) override {
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000106 return DefaultOptions;
107 }
108
109private:
110 ClangTidyGlobalOptions GlobalOptions;
111 ClangTidyOptions DefaultOptions;
112};
113
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000114/// \brief Implementation of the \c ClangTidyOptionsProvider interface, which
115/// tries to find a .clang-tidy file in the closest parent directory of each
116/// file.
117class FileOptionsProvider : public DefaultOptionsProvider {
118public:
119 /// \brief Initializes the \c FileOptionsProvider instance.
120 ///
121 /// \param GlobalOptions are just stored and returned to the caller of
122 /// \c getGlobalOptions.
123 ///
124 /// \param FallbackOptions are used in case there's no corresponding
125 /// .clang-tidy file.
126 ///
127 /// If any of the \param OverrideOptions fields are set, they will override
128 /// whatever options are read from the configuration file.
129 FileOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
130 const ClangTidyOptions &FallbackOptions,
131 const ClangTidyOptions &OverrideOptions);
132 const ClangTidyOptions &getOptions(llvm::StringRef FileName) override;
133
134private:
135 /// \brief Try to read configuration file from \p Directory. If \p Directory
136 /// is empty, use the fallback value.
137 llvm::ErrorOr<ClangTidyOptions> TryReadConfigFile(llvm::StringRef Directory);
138
139 llvm::StringMap<ClangTidyOptions> CachedOptions;
140 ClangTidyOptions OverrideOptions;
141};
142
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000143/// \brief Parses LineFilter from JSON and stores it to the \p Options.
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000144std::error_code parseLineFilter(llvm::StringRef LineFilter,
145 ClangTidyGlobalOptions &Options);
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000146
147/// \brief Parses configuration from JSON and stores it to the \p Options.
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000148std::error_code parseConfiguration(llvm::StringRef Config,
149 ClangTidyOptions &Options);
150
151/// \brief Serializes configuration to a YAML-encoded string.
152std::string configurationAsText(const ClangTidyOptions &Options);
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000153
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +0000154} // end namespace tidy
155} // end namespace clang
156
157#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_OPTIONS_H