clang-tools  11.0.0
ClangTidyCheck.h
Go to the documentation of this file.
1 //===--- ClangTidyCheck.h - clang-tidy --------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYCHECK_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYCHECK_H
11 
13 #include "ClangTidyOptions.h"
14 #include "clang/ASTMatchers/ASTMatchFinder.h"
15 #include "clang/Basic/Diagnostic.h"
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/Support/Error.h"
18 #include <type_traits>
19 #include <utility>
20 #include <vector>
21 
22 namespace clang {
23 
24 class CompilerInstance;
25 class SourceManager;
26 
27 namespace tidy {
28 
29 /// This class should be specialized by any enum type that needs to be converted
30 /// to and from an \ref llvm::StringRef.
31 template <class T> struct OptionEnumMapping {
32  // Specializations of this struct must implement this function.
33  static ArrayRef<std::pair<T, StringRef>> getEnumMapping() = delete;
34 };
35 
36 template <typename T> class OptionError : public llvm::ErrorInfo<T> {
37  std::error_code convertToErrorCode() const override {
38  return llvm::inconvertibleErrorCode();
39  }
40 
41 public:
42  void log(raw_ostream &OS) const override { OS << this->message(); }
43 };
44 
45 class MissingOptionError : public OptionError<MissingOptionError> {
46 public:
47  explicit MissingOptionError(std::string OptionName)
48  : OptionName(OptionName) {}
49 
50  std::string message() const override;
51  static char ID;
52 private:
53  const std::string OptionName;
54 };
55 
57  : public OptionError<UnparseableEnumOptionError> {
58 public:
59  explicit UnparseableEnumOptionError(std::string LookupName,
60  std::string LookupValue)
61  : LookupName(LookupName), LookupValue(LookupValue) {}
62  explicit UnparseableEnumOptionError(std::string LookupName,
63  std::string LookupValue,
64  std::string SuggestedValue)
65  : LookupName(LookupName), LookupValue(LookupValue),
66  SuggestedValue(SuggestedValue) {}
67 
68  std::string message() const override;
69  static char ID;
70 
71 private:
72  const std::string LookupName;
73  const std::string LookupValue;
74  const llvm::Optional<std::string> SuggestedValue;
75 };
76 
78  : public OptionError<UnparseableIntegerOptionError> {
79 public:
80  explicit UnparseableIntegerOptionError(std::string LookupName,
81  std::string LookupValue,
82  bool IsBoolean = false)
83  : LookupName(LookupName), LookupValue(LookupValue), IsBoolean(IsBoolean) {
84  }
85 
86  std::string message() const override;
87  static char ID;
88 
89 private:
90  const std::string LookupName;
91  const std::string LookupValue;
92  const bool IsBoolean;
93 };
94 
95 /// Base class for all clang-tidy checks.
96 ///
97 /// To implement a ``ClangTidyCheck``, write a subclass and override some of the
98 /// base class's methods. E.g. to implement a check that validates namespace
99 /// declarations, override ``registerMatchers``:
100 ///
101 /// ~~~{.cpp}
102 /// void registerMatchers(ast_matchers::MatchFinder *Finder) override {
103 /// Finder->addMatcher(namespaceDecl().bind("namespace"), this);
104 /// }
105 /// ~~~
106 ///
107 /// and then override ``check(const MatchResult &Result)`` to do the actual
108 /// check for each match.
109 ///
110 /// A new ``ClangTidyCheck`` instance is created per translation unit.
111 ///
112 /// FIXME: Figure out whether carrying information from one TU to another is
113 /// useful/necessary.
114 class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
115 public:
116  /// Initializes the check with \p CheckName and \p Context.
117  ///
118  /// Derived classes must implement the constructor with this signature or
119  /// delegate it. If a check needs to read options, it can do this in the
120  /// constructor using the Options.get() methods below.
121  ClangTidyCheck(StringRef CheckName, ClangTidyContext *Context);
122 
123  /// Override this to disable registering matchers and PP callbacks if an
124  /// invalid language version is being used.
125  ///
126  /// For example if a check is examining overloaded functions then this should
127  /// be overridden to return false when the CPlusPlus flag is not set in
128  /// \p LangOpts.
129  virtual bool isLanguageVersionSupported(const LangOptions &LangOpts) const {
130  return true;
131  }
132 
133  /// Override this to register ``PPCallbacks`` in the preprocessor.
134  ///
135  /// This should be used for clang-tidy checks that analyze preprocessor-
136  /// dependent properties, e.g. include directives and macro definitions.
137  ///
138  /// This will only be executed if the function isLanguageVersionSupported
139  /// returns true.
140  ///
141  /// There are two Preprocessors to choose from that differ in how they handle
142  /// modular #includes:
143  /// - PP is the real Preprocessor. It doesn't walk into modular #includes and
144  /// thus doesn't generate PPCallbacks for their contents.
145  /// - ModuleExpanderPP preprocesses the whole translation unit in the
146  /// non-modular mode, which allows it to generate PPCallbacks not only for
147  /// the main file and textual headers, but also for all transitively
148  /// included modular headers when the analysis runs with modules enabled.
149  /// When modules are not enabled ModuleExpanderPP just points to the real
150  /// preprocessor.
151  virtual void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
152  Preprocessor *ModuleExpanderPP) {}
153 
154  /// Override this to register AST matchers with \p Finder.
155  ///
156  /// This should be used by clang-tidy checks that analyze code properties that
157  /// dependent on AST knowledge.
158  ///
159  /// You can register as many matchers as necessary with \p Finder. Usually,
160  /// "this" will be used as callback, but you can also specify other callback
161  /// classes. Thereby, different matchers can trigger different callbacks.
162  ///
163  /// This will only be executed if the function isLanguageVersionSupported
164  /// returns true.
165  ///
166  /// If you need to merge information between the different matchers, you can
167  /// store these as members of the derived class. However, note that all
168  /// matches occur in the order of the AST traversal.
169  virtual void registerMatchers(ast_matchers::MatchFinder *Finder) {}
170 
171  /// ``ClangTidyChecks`` that register ASTMatchers should do the actual
172  /// work in here.
173  virtual void check(const ast_matchers::MatchFinder::MatchResult &Result) {}
174 
175  /// Add a diagnostic with the check's name.
176  DiagnosticBuilder diag(SourceLocation Loc, StringRef Description,
177  DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
178 
179  /// Should store all options supported by this check with their
180  /// current values or default values for options that haven't been overridden.
181  ///
182  /// The check should use ``Options.store()`` to store each option it supports
183  /// whether it has the default value or it has been overridden.
185 
186  /// Provides access to the ``ClangTidyCheck`` options via check-local
187  /// names.
188  ///
189  /// Methods of this class prepend ``CheckName + "."`` to translate check-local
190  /// option names to global option names.
191  class OptionsView {
192  public:
193  /// Initializes the instance using \p CheckName + "." as a prefix.
194  OptionsView(StringRef CheckName,
195  const ClangTidyOptions::OptionMap &CheckOptions);
196 
197  /// Read a named option from the ``Context``.
198  ///
199  /// Reads the option with the check-local name \p LocalName from the
200  /// ``CheckOptions``. If the corresponding key is not present, returns
201  /// a ``MissingOptionError``.
202  llvm::Expected<std::string> get(StringRef LocalName) const;
203 
204  /// Read a named option from the ``Context``.
205  ///
206  /// Reads the option with the check-local name \p LocalName from the
207  /// ``CheckOptions``. If the corresponding key is not present, returns
208  /// \p Default.
209  std::string get(StringRef LocalName, StringRef Default) const {
210  if (llvm::Expected<std::string> Val = get(LocalName))
211  return *Val;
212  else
213  llvm::consumeError(Val.takeError());
214  return Default.str();
215  }
216 
217  /// Read a named option from the ``Context``.
218  ///
219  /// Reads the option with the check-local name \p LocalName from local or
220  /// global ``CheckOptions``. Gets local option first. If local is not
221  /// present, falls back to get global option. If global option is not
222  /// present either, returns a ``MissingOptionError``.
223  llvm::Expected<std::string> getLocalOrGlobal(StringRef LocalName) const;
224 
225  /// Read a named option from the ``Context``.
226  ///
227  /// Reads the option with the check-local name \p LocalName from local or
228  /// global ``CheckOptions``. Gets local option first. If local is not
229  /// present, falls back to get global option. If global option is not
230  /// present either, returns \p Default.
231  std::string getLocalOrGlobal(StringRef LocalName, StringRef Default) const {
232  if (llvm::Expected<std::string> Val = getLocalOrGlobal(LocalName))
233  return *Val;
234  else
235  llvm::consumeError(Val.takeError());
236  return Default.str();
237  }
238 
239  /// Read a named option from the ``Context`` and parse it as an
240  /// integral type ``T``.
241  ///
242  /// Reads the option with the check-local name \p LocalName from the
243  /// ``CheckOptions``. If the corresponding key is not present, returns
244  /// a ``MissingOptionError``. If the corresponding key can't be parsed as
245  /// a ``T``, return an ``UnparseableIntegerOptionError``.
246  template <typename T>
247  std::enable_if_t<std::is_integral<T>::value, llvm::Expected<T>>
248  get(StringRef LocalName) const {
249  if (llvm::Expected<std::string> Value = get(LocalName)) {
250  T Result{};
251  if (!StringRef(*Value).getAsInteger(10, Result))
252  return Result;
253  return llvm::make_error<UnparseableIntegerOptionError>(
254  (NamePrefix + LocalName).str(), *Value);
255  } else
256  return std::move(Value.takeError());
257  }
258 
259  /// Read a named option from the ``Context`` and parse it as an
260  /// integral type ``T``.
261  ///
262  /// Reads the option with the check-local name \p LocalName from the
263  /// ``CheckOptions``. If the corresponding key is not present or it can't be
264  /// parsed as a ``T``, returns \p Default.
265  template <typename T>
266  std::enable_if_t<std::is_integral<T>::value, T> get(StringRef LocalName,
267  T Default) const {
268  if (llvm::Expected<T> ValueOr = get<T>(LocalName))
269  return *ValueOr;
270  else
271  logErrToStdErr(ValueOr.takeError());
272  return Default;
273  }
274 
275  /// Read a named option from the ``Context`` and parse it as an
276  /// integral type ``T``.
277  ///
278  /// Reads the option with the check-local name \p LocalName from local or
279  /// global ``CheckOptions``. Gets local option first. If local is not
280  /// present, falls back to get global option. If global option is not
281  /// present either, returns a ``MissingOptionError``. If the corresponding
282  /// key can't be parsed as a ``T``, return an
283  /// ``UnparseableIntegerOptionError``.
284  template <typename T>
285  std::enable_if_t<std::is_integral<T>::value, llvm::Expected<T>>
286  getLocalOrGlobal(StringRef LocalName) const {
287  llvm::Expected<std::string> ValueOr = get(LocalName);
288  bool IsGlobal = false;
289  if (!ValueOr) {
290  IsGlobal = true;
291  llvm::consumeError(ValueOr.takeError());
292  ValueOr = getLocalOrGlobal(LocalName);
293  if (!ValueOr)
294  return std::move(ValueOr.takeError());
295  }
296  T Result{};
297  if (!StringRef(*ValueOr).getAsInteger(10, Result))
298  return Result;
299  return llvm::make_error<UnparseableIntegerOptionError>(
300  (IsGlobal ? LocalName.str() : (NamePrefix + LocalName).str()),
301  *ValueOr);
302  }
303 
304  /// Read a named option from the ``Context`` and parse it as an
305  /// integral type ``T``.
306  ///
307  /// Reads the option with the check-local name \p LocalName from local or
308  /// global ``CheckOptions``. Gets local option first. If local is not
309  /// present, falls back to get global option. If global option is not
310  /// present either or it can't be parsed as a ``T``, returns \p Default.
311  template <typename T>
312  std::enable_if_t<std::is_integral<T>::value, T>
313  getLocalOrGlobal(StringRef LocalName, T Default) const {
314  if (llvm::Expected<T> ValueOr = getLocalOrGlobal<T>(LocalName))
315  return *ValueOr;
316  else
317  logErrToStdErr(ValueOr.takeError());
318  return Default;
319  }
320 
321  /// Read a named option from the ``Context`` and parse it as an
322  /// enum type ``T``.
323  ///
324  /// Reads the option with the check-local name \p LocalName from the
325  /// ``CheckOptions``. If the corresponding key is not present, returns a
326  /// ``MissingOptionError``. If the key can't be parsed as a ``T`` returns a
327  /// ``UnparseableEnumOptionError``.
328  ///
329  /// \ref clang::tidy::OptionEnumMapping must be specialized for ``T`` to
330  /// supply the mapping required to convert between ``T`` and a string.
331  template <typename T>
332  std::enable_if_t<std::is_enum<T>::value, llvm::Expected<T>>
333  get(StringRef LocalName, bool IgnoreCase = false) {
334  if (llvm::Expected<int64_t> ValueOr =
335  getEnumInt(LocalName, typeEraseMapping<T>(), false, IgnoreCase))
336  return static_cast<T>(*ValueOr);
337  else
338  return std::move(ValueOr.takeError());
339  }
340 
341  /// Read a named option from the ``Context`` and parse it as an
342  /// enum type ``T``.
343  ///
344  /// Reads the option with the check-local name \p LocalName from the
345  /// ``CheckOptions``. If the corresponding key is not present or it can't be
346  /// parsed as a ``T``, returns \p Default.
347  ///
348  /// \ref clang::tidy::OptionEnumMapping must be specialized for ``T`` to
349  /// supply the mapping required to convert between ``T`` and a string.
350  template <typename T>
351  std::enable_if_t<std::is_enum<T>::value, T>
352  get(StringRef LocalName, T Default, bool IgnoreCase = false) {
353  if (auto ValueOr = get<T>(LocalName, IgnoreCase))
354  return *ValueOr;
355  else
356  logErrToStdErr(ValueOr.takeError());
357  return Default;
358  }
359 
360  /// Read a named option from the ``Context`` and parse it as an
361  /// enum type ``T``.
362  ///
363  /// Reads the option with the check-local name \p LocalName from local or
364  /// global ``CheckOptions``. Gets local option first. If local is not
365  /// present, falls back to get global option. If global option is not
366  /// present either, returns a ``MissingOptionError``. If the key can't be
367  /// parsed as a ``T`` returns a ``UnparseableEnumOptionError``.
368  ///
369  /// \ref clang::tidy::OptionEnumMapping must be specialized for ``T`` to
370  /// supply the mapping required to convert between ``T`` and a string.
371  template <typename T>
372  std::enable_if_t<std::is_enum<T>::value, llvm::Expected<T>>
373  getLocalOrGlobal(StringRef LocalName,
374  bool IgnoreCase = false) {
375  if (llvm::Expected<int64_t> ValueOr =
376  getEnumInt(LocalName, typeEraseMapping<T>(), true, IgnoreCase))
377  return static_cast<T>(*ValueOr);
378  else
379  return std::move(ValueOr.takeError());
380  }
381 
382  /// Read a named option from the ``Context`` and parse it as an
383  /// enum type ``T``.
384  ///
385  /// Reads the option with the check-local name \p LocalName from local or
386  /// global ``CheckOptions``. Gets local option first. If local is not
387  /// present, falls back to get global option. If global option is not
388  /// present either or it can't be parsed as a ``T``, returns \p Default.
389  ///
390  /// \ref clang::tidy::OptionEnumMapping must be specialized for ``T`` to
391  /// supply the mapping required to convert between ``T`` and a string.
392  template <typename T>
393  std::enable_if_t<std::is_enum<T>::value, T>
394  getLocalOrGlobal(StringRef LocalName, T Default, bool IgnoreCase = false) {
395  if (auto ValueOr = getLocalOrGlobal<T>(LocalName, IgnoreCase))
396  return *ValueOr;
397  else
398  logErrToStdErr(ValueOr.takeError());
399  return Default;
400  }
401 
402  /// Stores an option with the check-local name \p LocalName with
403  /// string value \p Value to \p Options.
404  void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
405  StringRef Value) const;
406 
407  /// Stores an option with the check-local name \p LocalName with
408  /// integer value \p Value to \p Options.
409  template <typename T>
410  std::enable_if_t<std::is_integral<T>::value>
412  T Value) const {
413  storeInt(Options, LocalName, Value);
414  }
415 
416  /// Stores an option with the check-local name \p LocalName as the string
417  /// representation of the Enum \p Value to \p Options.
418  ///
419  /// \ref clang::tidy::OptionEnumMapping must be specialized for ``T`` to
420  /// supply the mapping required to convert between ``T`` and a string.
421  template <typename T>
422  std::enable_if_t<std::is_enum<T>::value>
423  store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, T Value) {
424  ArrayRef<std::pair<T, StringRef>> Mapping =
426  auto Iter = llvm::find_if(
427  Mapping, [&](const std::pair<T, StringRef> &NameAndEnum) {
428  return NameAndEnum.first == Value;
429  });
430  assert(Iter != Mapping.end() && "Unknown Case Value");
431  store(Options, LocalName, Iter->second);
432  }
433 
434  private:
435  using NameAndValue = std::pair<int64_t, StringRef>;
436 
437  llvm::Expected<int64_t> getEnumInt(StringRef LocalName,
438  ArrayRef<NameAndValue> Mapping,
439  bool CheckGlobal, bool IgnoreCase);
440 
441  template <typename T>
442  std::enable_if_t<std::is_enum<T>::value, std::vector<NameAndValue>>
443  typeEraseMapping() {
444  ArrayRef<std::pair<T, StringRef>> Mapping =
446  std::vector<NameAndValue> Result;
447  Result.reserve(Mapping.size());
448  for (auto &MappedItem : Mapping) {
449  Result.emplace_back(static_cast<int64_t>(MappedItem.first),
450  MappedItem.second);
451  }
452  return Result;
453  }
454 
455  void storeInt(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
456  int64_t Value) const;
457 
458  static void logErrToStdErr(llvm::Error &&Err);
459 
460  std::string NamePrefix;
461  const ClangTidyOptions::OptionMap &CheckOptions;
462  };
463 
464 private:
465  void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
466  StringRef getID() const override { return CheckName; }
467  std::string CheckName;
468  ClangTidyContext *Context;
469 
470 protected:
472  /// Returns the main file name of the current translation unit.
473  StringRef getCurrentMainFile() const { return Context->getCurrentFile(); }
474  /// Returns the language options from the context.
475  const LangOptions &getLangOpts() const { return Context->getLangOpts(); }
476 };
477 
478 /// Read a named option from the ``Context`` and parse it as a bool.
479 ///
480 /// Reads the option with the check-local name \p LocalName from the
481 /// ``CheckOptions``. If the corresponding key is not present, returns
482 /// a ``MissingOptionError``. If the corresponding key can't be parsed as
483 /// a bool, return an ``UnparseableIntegerOptionError``.
484 template <>
485 llvm::Expected<bool>
486 ClangTidyCheck::OptionsView::get<bool>(StringRef LocalName) const;
487 
488 /// Read a named option from the ``Context`` and parse it as a bool.
489 ///
490 /// Reads the option with the check-local name \p LocalName from the
491 /// ``CheckOptions``. If the corresponding key is not present or it can't be
492 /// parsed as a bool, returns \p Default.
493 template <>
494 bool ClangTidyCheck::OptionsView::get<bool>(StringRef LocalName,
495  bool Default) const;
496 
497 /// Read a named option from the ``Context`` and parse it as a bool.
498 ///
499 /// Reads the option with the check-local name \p LocalName from local or
500 /// global ``CheckOptions``. Gets local option first. If local is not
501 /// present, falls back to get global option. If global option is not
502 /// present either, returns a ``MissingOptionError``. If the corresponding
503 /// key can't be parsed as a bool, return an
504 /// ``UnparseableIntegerOptionError``.
505 template <>
506 llvm::Expected<bool>
507 ClangTidyCheck::OptionsView::getLocalOrGlobal<bool>(StringRef LocalName) const;
508 
509 /// Read a named option from the ``Context`` and parse it as a bool.
510 ///
511 /// Reads the option with the check-local name \p LocalName from local or
512 /// global ``CheckOptions``. Gets local option first. If local is not
513 /// present, falls back to get global option. If global option is not
514 /// present either or it can't be parsed as a bool, returns \p Default.
515 template <>
516 bool ClangTidyCheck::OptionsView::getLocalOrGlobal<bool>(StringRef LocalName,
517  bool Default) const;
518 
519 /// Stores an option with the check-local name \p LocalName with
520 /// bool value \p Value to \p Options.
521 template <>
522 void ClangTidyCheck::OptionsView::store<bool>(
523  ClangTidyOptions::OptionMap &Options, StringRef LocalName,
524  bool Value) const;
525 
526 } // namespace tidy
527 } // namespace clang
528 
529 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYCHECK_H
ClangTidyDiagnosticConsumer.h
clang::tidy::OptionError::log
void log(raw_ostream &OS) const override
Definition: ClangTidyCheck.h:42
clang::tidy::ClangTidyCheck::OptionsView::OptionsView
OptionsView(StringRef CheckName, const ClangTidyOptions::OptionMap &CheckOptions)
Initializes the instance using CheckName + "." as a prefix.
Definition: ClangTidyCheck.cpp:67
clang::tidy::MissingOptionError
Definition: ClangTidyCheck.h:45
clang::tidy::ClangTidyCheck::storeOptions
virtual void storeOptions(ClangTidyOptions::OptionMap &Options)
Should store all options supported by this check with their current values or default values for opti...
Definition: ClangTidyCheck.h:184
clang::tidy::ClangTidyCheck::OptionsView::getLocalOrGlobal
std::enable_if_t< std::is_integral< T >::value, llvm::Expected< T > > getLocalOrGlobal(StringRef LocalName) const
Read a named option from the Context and parse it as an integral type T.
Definition: ClangTidyCheck.h:286
clang::tidy::ClangTidyCheck::ClangTidyCheck
ClangTidyCheck(StringRef CheckName, ClangTidyContext *Context)
Initializes the check with CheckName and Context.
Definition: ClangTidyCheck.cpp:48
clang::tidy::UnparseableIntegerOptionError::message
std::string message() const override
Definition: ClangTidyCheck.cpp:39
clang::tidy::ClangTidyCheck::OptionsView::get
std::enable_if_t< std::is_integral< T >::value, T > get(StringRef LocalName, T Default) const
Read a named option from the Context and parse it as an integral type T.
Definition: ClangTidyCheck.h:266
clang::tidy::MissingOptionError::message
std::string message() const override
Definition: ClangTidyCheck.cpp:22
clang::tidy::ClangTidyCheck::isLanguageVersionSupported
virtual bool isLanguageVersionSupported(const LangOptions &LangOpts) const
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
Definition: ClangTidyCheck.h:129
clang::tidy::OptionEnumMapping
This class should be specialized by any enum type that needs to be converted to and from an llvm::Str...
Definition: ClangTidyCheck.h:31
clang::tidy::ClangTidyCheck::OptionsView::getLocalOrGlobal
std::enable_if_t< std::is_enum< T >::value, T > getLocalOrGlobal(StringRef LocalName, T Default, bool IgnoreCase=false)
Read a named option from the Context and parse it as an enum type T.
Definition: ClangTidyCheck.h:394
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:114
clang::tidy::ClangTidyCheck::OptionsView::getLocalOrGlobal
llvm::Expected< std::string > getLocalOrGlobal(StringRef LocalName) const
Read a named option from the Context.
Definition: ClangTidyCheck.cpp:94
clang::tidy::ClangTidyCheck::OptionsView::getLocalOrGlobal
std::string getLocalOrGlobal(StringRef LocalName, StringRef Default) const
Read a named option from the Context.
Definition: ClangTidyCheck.h:231
clang::tidy::ClangTidyCheck::OptionsView::get
std::enable_if_t< std::is_enum< T >::value, T > get(StringRef LocalName, T Default, bool IgnoreCase=false)
Read a named option from the Context and parse it as an enum type T.
Definition: ClangTidyCheck.h:352
clang::tidy::ClangTidyCheck::OptionsView::get
std::enable_if_t< std::is_enum< T >::value, llvm::Expected< T > > get(StringRef LocalName, bool IgnoreCase=false)
Read a named option from the Context and parse it as an enum type T.
Definition: ClangTidyCheck.h:333
clang::tidy::ClangTidyCheck::getLangOpts
const LangOptions & getLangOpts() const
Returns the language options from the context.
Definition: ClangTidyCheck.h:475
clang::tidy::ClangTidyCheck::OptionsView::store
std::enable_if_t< std::is_enum< T >::value > store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, T Value)
Stores an option with the check-local name LocalName as the string representation of the Enum Value t...
Definition: ClangTidyCheck.h:423
clang::tidy::ClangTidyCheck::OptionsView::get
llvm::Expected< std::string > get(StringRef LocalName) const
Read a named option from the Context.
Definition: ClangTidyCheck.cpp:72
clang::tidy::UnparseableEnumOptionError::UnparseableEnumOptionError
UnparseableEnumOptionError(std::string LookupName, std::string LookupValue)
Definition: ClangTidyCheck.h:59
clang::tidy::UnparseableIntegerOptionError::ID
static char ID
Definition: ClangTidyCheck.h:87
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:471
clang::tidy::UnparseableEnumOptionError::ID
static char ID
Definition: ClangTidyCheck.h:69
clang::tidy::OptionError
Definition: ClangTidyCheck.h:36
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:76
Description
const char * Description
Definition: Dexp.cpp:320
clang::tidy::ClangTidyCheck::OptionsView::getLocalOrGlobal
std::enable_if_t< std::is_integral< T >::value, T > getLocalOrGlobal(StringRef LocalName, T Default) const
Read a named option from the Context and parse it as an integral type T.
Definition: ClangTidyCheck.h:313
clang::tidy::UnparseableIntegerOptionError::UnparseableIntegerOptionError
UnparseableIntegerOptionError(std::string LookupName, std::string LookupValue, bool IsBoolean=false)
Definition: ClangTidyCheck.h:80
clang::tidy::UnparseableEnumOptionError
Definition: ClangTidyCheck.h:56
clang::tidy::MissingOptionError::MissingOptionError
MissingOptionError(std::string OptionName)
Definition: ClangTidyCheck.h:47
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidyCheck.cpp:55
clang::tidy::MissingOptionError::ID
static char ID
Definition: ClangTidyCheck.h:51
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:29
clang::tidy::ClangTidyCheck::registerMatchers
virtual void registerMatchers(ast_matchers::MatchFinder *Finder)
Override this to register AST matchers with Finder.
Definition: ClangTidyCheck.h:169
clang::tidy::ClangTidyCheck::check
virtual void check(const ast_matchers::MatchFinder::MatchResult &Result)
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: ClangTidyCheck.h:173
clang::tidy::ClangTidyCheck::OptionsView::get
std::string get(StringRef LocalName, StringRef Default) const
Read a named option from the Context.
Definition: ClangTidyCheck.h:209
clang::tidy::UnparseableEnumOptionError::UnparseableEnumOptionError
UnparseableEnumOptionError(std::string LookupName, std::string LookupValue, std::string SuggestedValue)
Definition: ClangTidyCheck.h:62
clang::tidy::OptionEnumMapping::getEnumMapping
static ArrayRef< std::pair< T, StringRef > > getEnumMapping()=delete
clang::tidy::UnparseableEnumOptionError::message
std::string message() const override
Definition: ClangTidyCheck.cpp:29
clang::tidy::ClangTidyCheck::OptionsView::getLocalOrGlobal
std::enable_if_t< std::is_enum< T >::value, llvm::Expected< T > > getLocalOrGlobal(StringRef LocalName, bool IgnoreCase=false)
Read a named option from the Context and parse it as an enum type T.
Definition: ClangTidyCheck.h:373
clang::tidy::ClangTidyCheck::registerPPCallbacks
virtual void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP)
Override this to register PPCallbacks in the preprocessor.
Definition: ClangTidyCheck.h:151
clang::tidy::ClangTidyCheck::OptionsView::get
std::enable_if_t< std::is_integral< T >::value, llvm::Expected< T > > get(StringRef LocalName) const
Read a named option from the Context and parse it as an integral type T.
Definition: ClangTidyCheck.h:248
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:162
clang::tidy::ClangTidyCheck::getCurrentMainFile
StringRef getCurrentMainFile() const
Returns the main file name of the current translation unit.
Definition: ClangTidyCheck.h:473
clang::tidy::ClangTidyCheck::OptionsView::store
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidyCheck.cpp:152
Loc
SourceLocation Loc
'#' location in the include directive
Definition: IncludeOrderCheck.cpp:37
ClangTidyOptions.h
Warning
constexpr static llvm::SourceMgr::DiagKind Warning
Definition: ConfigCompile.cpp:212
clang::tidy::ClangTidyCheck::OptionsView
Provides access to the ClangTidyCheck options via check-local names.
Definition: ClangTidyCheck.h:191
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:111
clang::tidy::UnparseableIntegerOptionError
Definition: ClangTidyCheck.h:77
clang::tidy::ClangTidyCheck::OptionsView::store
std::enable_if_t< std::is_integral< T >::value > store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, T Value) const
Stores an option with the check-local name LocalName with integer value Value to Options.
Definition: ClangTidyCheck.h:411