150
|
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 std::string(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 std::string(Default);
|
|
56 }
|
|
57
|
|
58 void ClangTidyCheck::OptionsView::store(ClangTidyOptions::OptionMap &Options,
|
|
59 StringRef LocalName,
|
|
60 StringRef Value) const {
|
|
61 Options[NamePrefix + LocalName.str()] = std::string(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
|