clang-tools 10.0.0

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
33 ClangTidyCheck::OptionsView::OptionsView(StringRef CheckName,
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
46 ClangTidyCheck::OptionsView::getLocalOrGlobal(StringRef LocalName,
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
58 void ClangTidyCheck::OptionsView::store(ClangTidyOptions::OptionMap &Options,
59 StringRef LocalName,
60 StringRef Value) const {
61 Options[NamePrefix + LocalName.str()] = Value;
62 }
63
64 void ClangTidyCheck::OptionsView::store(ClangTidyOptions::OptionMap &Options,
65 StringRef LocalName,
66 int64_t Value) const {
67 store(Options, LocalName, llvm::itostr(Value));
68 }
69
70 } // namespace tidy
71 } // namespace clang
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:37
ClangTidyCheck.h
clang::tidy::modernize::Message
constexpr llvm::StringLiteral Message
Definition: UseTrailingReturnTypeCheck.cpp:101
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:184
clang::tidy::ClangTidyContext::diag
DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc, StringRef Message, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Report any errors detected using this method.
Definition: ClangTidyDiagnosticConsumer.cpp:160
clang::tidy::ClangTidyCheck::OptionsView::getLocalOrGlobal
std::string getLocalOrGlobal(StringRef LocalName, StringRef Default) const
Read a named option from the Context.
Definition: ClangTidyCheck.cpp:46
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:58
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
clang::tidy::ClangTidyCheck::ClangTidyCheck
ClangTidyCheck(StringRef CheckName, ClangTidyContext *Context)
Initializes the check with CheckName and Context.
Definition: ClangTidyCheck.cpp:14
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyCheck::OptionsView::OptionsView
OptionsView(StringRef CheckName, const ClangTidyOptions::OptionMap &CheckOptions)
Initializes the instance using CheckName + "." as a prefix.
Definition: ClangTidyCheck.cpp:33
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
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:90
clang::tidy::ClangTidyCheck::OptionsView::get
std::string get(StringRef LocalName, StringRef Default) const
Read a named option from the Context.
Definition: ClangTidyCheck.cpp:37
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidyCheck.cpp:21