clang-tools  10.0.0svn
ClangTidyCheck.cpp
Go to the documentation of this file.
1 //===--- ClangTidyCheck.cpp - 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 #include "ClangTidyCheck.h"
10 
11 namespace clang {
12 namespace tidy {
13 
14 ClangTidyCheck::ClangTidyCheck(StringRef CheckName, ClangTidyContext *Context)
15  : CheckName(CheckName), Context(Context),
16  Options(CheckName, Context->getOptions().CheckOptions) {
17  assert(Context != nullptr);
18  assert(!CheckName.empty());
19 }
20 
21 DiagnosticBuilder ClangTidyCheck::diag(SourceLocation Loc, StringRef Message,
22  DiagnosticIDs::Level Level) {
23  return Context->diag(CheckName, Loc, Message, Level);
24 }
25 
26 void ClangTidyCheck::run(const ast_matchers::MatchFinder::MatchResult &Result) {
27  // For historical reasons, checks don't implement the MatchFinder run()
28  // callback directly. We keep the run()/check() distinction to avoid interface
29  // churn, and to allow us to add cross-cutting logic in the future.
30  check(Result);
31 }
32 
34  const ClangTidyOptions::OptionMap &CheckOptions)
35  : NamePrefix(CheckName.str() + "."), CheckOptions(CheckOptions) {}
36 
37 std::string ClangTidyCheck::OptionsView::get(StringRef LocalName,
38  StringRef Default) const {
39  const auto &Iter = CheckOptions.find(NamePrefix + LocalName.str());
40  if (Iter != CheckOptions.end())
41  return Iter->second;
42  return Default;
43 }
44 
45 std::string
47  StringRef Default) const {
48  auto Iter = CheckOptions.find(NamePrefix + LocalName.str());
49  if (Iter != CheckOptions.end())
50  return Iter->second;
51  // Fallback to global setting, if present.
52  Iter = CheckOptions.find(LocalName.str());
53  if (Iter != CheckOptions.end())
54  return Iter->second;
55  return Default;
56 }
57 
59  StringRef LocalName,
60  StringRef Value) const {
61  Options[NamePrefix + LocalName.str()] = Value;
62 }
63 
65  StringRef LocalName,
66  int64_t Value) const {
67  store(Options, LocalName, llvm::itostr(Value));
68 }
69 
70 } // namespace tidy
71 } // namespace clang
SourceLocation Loc
'#' location in the include directive
constexpr llvm::StringLiteral Message
DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc, StringRef Message, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Report any errors detected using this method.
std::string getLocalOrGlobal(StringRef LocalName, StringRef Default) const
Read a named option from the Context.
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.
std::map< std::string, std::string > OptionMap
ClangTidyCheck(StringRef CheckName, ClangTidyContext *Context)
Initializes the check with CheckName and Context.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
OptionsView(StringRef CheckName, const ClangTidyOptions::OptionMap &CheckOptions)
Initializes the instance using CheckName + "." as a prefix.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
virtual void check(const ast_matchers::MatchFinder::MatchResult &Result)
ClangTidyChecks that register ASTMatchers should do the actual work in here.
std::string get(StringRef LocalName, StringRef Default) const
Read a named option from the Context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.