Mercurial > hg > CbC > CbC_llvm
comparison clang-tools-extra/clang-tidy/utils/TransformerClangTidyCheck.cpp @ 173:0572611fdcc8 llvm10 llvm12
reorgnization done
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 25 May 2020 11:55:54 +0900 |
parents | 1d019706d866 |
children | 2e18cbf3894f |
comparison
equal
deleted
inserted
replaced
172:9fbae9c8bf63 | 173:0572611fdcc8 |
---|---|
28 TransformerClangTidyCheck::TransformerClangTidyCheck( | 28 TransformerClangTidyCheck::TransformerClangTidyCheck( |
29 std::function<Optional<RewriteRule>(const LangOptions &, | 29 std::function<Optional<RewriteRule>(const LangOptions &, |
30 const OptionsView &)> | 30 const OptionsView &)> |
31 MakeRule, | 31 MakeRule, |
32 StringRef Name, ClangTidyContext *Context) | 32 StringRef Name, ClangTidyContext *Context) |
33 : ClangTidyCheck(Name, Context), Rule(MakeRule(getLangOpts(), Options)) { | 33 : ClangTidyCheck(Name, Context), Rule(MakeRule(getLangOpts(), Options)), |
34 IncludeStyle(Options.getLocalOrGlobal("IncludeStyle", | |
35 IncludeSorter::getMapping(), | |
36 IncludeSorter::IS_LLVM)) { | |
34 if (Rule) | 37 if (Rule) |
35 assert(llvm::all_of(Rule->Cases, hasExplanation) && | 38 assert(llvm::all_of(Rule->Cases, hasExplanation) && |
36 "clang-tidy checks must have an explanation by default;" | 39 "clang-tidy checks must have an explanation by default;" |
37 " explicitly provide an empty explanation if none is desired"); | 40 " explicitly provide an empty explanation if none is desired"); |
38 } | 41 } |
39 | 42 |
40 TransformerClangTidyCheck::TransformerClangTidyCheck(RewriteRule R, | 43 TransformerClangTidyCheck::TransformerClangTidyCheck(RewriteRule R, |
41 StringRef Name, | 44 StringRef Name, |
42 ClangTidyContext *Context) | 45 ClangTidyContext *Context) |
43 : ClangTidyCheck(Name, Context), Rule(std::move(R)) { | 46 : ClangTidyCheck(Name, Context), Rule(std::move(R)), |
47 IncludeStyle(Options.getLocalOrGlobal("IncludeStyle", | |
48 IncludeSorter::getMapping(), | |
49 IncludeSorter::IS_LLVM)) { | |
44 assert(llvm::all_of(Rule->Cases, hasExplanation) && | 50 assert(llvm::all_of(Rule->Cases, hasExplanation) && |
45 "clang-tidy checks must have an explanation by default;" | 51 "clang-tidy checks must have an explanation by default;" |
46 " explicitly provide an empty explanation if none is desired"); | 52 " explicitly provide an empty explanation if none is desired"); |
47 } | 53 } |
48 | 54 |
51 // Only allocate and register the IncludeInsert when some `Case` will add | 57 // Only allocate and register the IncludeInsert when some `Case` will add |
52 // includes. | 58 // includes. |
53 if (Rule && llvm::any_of(Rule->Cases, [](const RewriteRule::Case &C) { | 59 if (Rule && llvm::any_of(Rule->Cases, [](const RewriteRule::Case &C) { |
54 return !C.AddedIncludes.empty(); | 60 return !C.AddedIncludes.empty(); |
55 })) { | 61 })) { |
56 Inserter = std::make_unique<IncludeInserter>( | 62 Inserter = |
57 SM, getLangOpts(), utils::IncludeSorter::IS_LLVM); | 63 std::make_unique<IncludeInserter>(SM, getLangOpts(), IncludeStyle); |
58 PP->addPPCallbacks(Inserter->CreatePPCallbacks()); | 64 PP->addPPCallbacks(Inserter->CreatePPCallbacks()); |
59 } | 65 } |
60 } | 66 } |
61 | 67 |
62 void TransformerClangTidyCheck::registerMatchers( | 68 void TransformerClangTidyCheck::registerMatchers( |
71 if (Result.Context->getDiagnostics().hasErrorOccurred()) | 77 if (Result.Context->getDiagnostics().hasErrorOccurred()) |
72 return; | 78 return; |
73 | 79 |
74 assert(Rule && "check() should not fire if Rule is None"); | 80 assert(Rule && "check() should not fire if Rule is None"); |
75 RewriteRule::Case Case = transformer::detail::findSelectedCase(Result, *Rule); | 81 RewriteRule::Case Case = transformer::detail::findSelectedCase(Result, *Rule); |
76 Expected<SmallVector<transformer::detail::Transformation, 1>> | 82 Expected<SmallVector<transformer::Edit, 1>> Edits = Case.Edits(Result); |
77 Transformations = transformer::detail::translateEdits(Result, Case.Edits); | 83 if (!Edits) { |
78 if (!Transformations) { | 84 llvm::errs() << "Rewrite failed: " << llvm::toString(Edits.takeError()) |
79 llvm::errs() << "Rewrite failed: " | 85 << "\n"; |
80 << llvm::toString(Transformations.takeError()) << "\n"; | |
81 return; | 86 return; |
82 } | 87 } |
83 | 88 |
84 // No rewrite applied, but no error encountered either. | 89 // No rewrite applied, but no error encountered either. |
85 if (Transformations->empty()) | 90 if (Edits->empty()) |
86 return; | 91 return; |
87 | 92 |
88 Expected<std::string> Explanation = Case.Explanation->eval(Result); | 93 Expected<std::string> Explanation = Case.Explanation->eval(Result); |
89 if (!Explanation) { | 94 if (!Explanation) { |
90 llvm::errs() << "Error in explanation: " | 95 llvm::errs() << "Error in explanation: " |
91 << llvm::toString(Explanation.takeError()) << "\n"; | 96 << llvm::toString(Explanation.takeError()) << "\n"; |
92 return; | 97 return; |
93 } | 98 } |
94 | 99 |
95 // Associate the diagnostic with the location of the first change. | 100 // Associate the diagnostic with the location of the first change. |
96 DiagnosticBuilder Diag = | 101 DiagnosticBuilder Diag = diag((*Edits)[0].Range.getBegin(), *Explanation); |
97 diag((*Transformations)[0].Range.getBegin(), *Explanation); | 102 for (const auto &T : *Edits) |
98 for (const auto &T : *Transformations) | |
99 Diag << FixItHint::CreateReplacement(T.Range, T.Replacement); | 103 Diag << FixItHint::CreateReplacement(T.Range, T.Replacement); |
100 | 104 |
101 for (const auto &I : Case.AddedIncludes) { | 105 for (const auto &I : Case.AddedIncludes) { |
102 auto &Header = I.first; | 106 auto &Header = I.first; |
103 if (Optional<FixItHint> Fix = Inserter->CreateIncludeInsertion( | 107 if (Optional<FixItHint> Fix = Inserter->CreateIncludeInsertion( |
106 Diag << *Fix; | 110 Diag << *Fix; |
107 } | 111 } |
108 } | 112 } |
109 } | 113 } |
110 | 114 |
115 void TransformerClangTidyCheck::storeOptions( | |
116 ClangTidyOptions::OptionMap &Opts) { | |
117 Options.store(Opts, "IncludeStyle", IncludeStyle, | |
118 IncludeSorter::getMapping()); | |
119 } | |
120 | |
111 } // namespace utils | 121 } // namespace utils |
112 } // namespace tidy | 122 } // namespace tidy |
113 } // namespace clang | 123 } // namespace clang |