1 //===--- Transformer.cpp - Transformer library implementation ---*- 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 "clang/Tooling/Transformer/RewriteRule.h" 10 #include "clang/ASTMatchers/ASTMatchFinder.h" 11 #include "clang/ASTMatchers/ASTMatchers.h" 12 #include "clang/Basic/SourceLocation.h" 13 #include "clang/Tooling/Transformer/SourceCode.h" 14 #include "llvm/ADT/Optional.h" 15 #include "llvm/ADT/StringRef.h" 16 #include "llvm/Support/Errc.h" 17 #include "llvm/Support/Error.h" 18 #include <map> 19 #include <string> 20 #include <utility> 21 #include <vector> 22 23 using namespace clang; 24 using namespace transformer; 25 26 using ast_matchers::MatchFinder; 27 using ast_matchers::internal::DynTypedMatcher; 28 29 using MatchResult = MatchFinder::MatchResult; 30 31 static Expected<SmallVector<transformer::Edit, 1>> 32 translateEdits(const MatchResult &Result, ArrayRef<ASTEdit> ASTEdits) { 33 SmallVector<transformer::Edit, 1> Edits; 34 for (const auto &E : ASTEdits) { 35 Expected<CharSourceRange> Range = E.TargetRange(Result); 36 if (!Range) 37 return Range.takeError(); 38 llvm::Optional<CharSourceRange> EditRange = 39 tooling::getRangeForEdit(*Range, *Result.Context); 40 // FIXME: let user specify whether to treat this case as an error or ignore 41 // it as is currently done. 42 if (!EditRange) 43 return SmallVector<Edit, 0>(); 44 auto Replacement = E.Replacement->eval(Result); 45 if (!Replacement) 46 return Replacement.takeError(); 47 auto Metadata = E.Metadata(Result); 48 if (!Metadata) 49 return Metadata.takeError(); 50 transformer::Edit T; 51 T.Range = *EditRange; 52 T.Replacement = std::move(*Replacement); 53 T.Metadata = std::move(*Metadata); 54 Edits.push_back(std::move(T)); 55 } 56 return Edits; 57 } 58 59 EditGenerator transformer::editList(SmallVector<ASTEdit, 1> Edits) { 60 return [Edits = std::move(Edits)](const MatchResult &Result) { 61 return translateEdits(Result, Edits); 62 }; 63 } 64 65 EditGenerator transformer::edit(ASTEdit Edit) { 66 return [Edit = std::move(Edit)](const MatchResult &Result) { 67 return translateEdits(Result, {Edit}); 68 }; 69 } 70 71 ASTEdit transformer::changeTo(RangeSelector Target, TextGenerator Replacement) { 72 ASTEdit E; 73 E.TargetRange = std::move(Target); 74 E.Replacement = std::move(Replacement); 75 return E; 76 } 77 78 namespace { 79 /// A \c TextGenerator that always returns a fixed string. 80 class SimpleTextGenerator : public MatchComputation<std::string> { 81 std::string S; 82 83 public: 84 SimpleTextGenerator(std::string S) : S(std::move(S)) {} 85 llvm::Error eval(const ast_matchers::MatchFinder::MatchResult &, 86 std::string *Result) const override { 87 Result->append(S); 88 return llvm::Error::success(); 89 } 90 std::string toString() const override { 91 return (llvm::Twine("text(\"") + S + "\")").str(); 92 } 93 }; 94 } // namespace 95 96 ASTEdit transformer::remove(RangeSelector S) { 97 return change(std::move(S), std::make_shared<SimpleTextGenerator>("")); 98 } 99 100 RewriteRule transformer::makeRule(ast_matchers::internal::DynTypedMatcher M, 101 EditGenerator Edits, 102 TextGenerator Explanation) { 103 return RewriteRule{{RewriteRule::Case{ 104 std::move(M), std::move(Edits), std::move(Explanation), {}}}}; 105 } 106 107 void transformer::addInclude(RewriteRule &Rule, StringRef Header, 108 IncludeFormat Format) { 109 for (auto &Case : Rule.Cases) 110 Case.AddedIncludes.emplace_back(Header.str(), Format); 111 } 112 113 #ifndef NDEBUG 114 // Filters for supported matcher kinds. FIXME: Explicitly list the allowed kinds 115 // (all node matcher types except for `QualType` and `Type`), rather than just 116 // banning `QualType` and `Type`. 117 static bool hasValidKind(const DynTypedMatcher &M) { 118 return !M.canConvertTo<QualType>(); 119 } 120 #endif 121 122 // Binds each rule's matcher to a unique (and deterministic) tag based on 123 // `TagBase` and the id paired with the case. All of the returned matchers have 124 // their traversal kind explicitly set, either based on a pre-set kind or to the 125 // provided `DefaultTraversalKind`. 126 static std::vector<DynTypedMatcher> taggedMatchers( 127 StringRef TagBase, 128 const SmallVectorImpl<std::pair<size_t, RewriteRule::Case>> &Cases, 129 ast_type_traits::TraversalKind DefaultTraversalKind) { 130 std::vector<DynTypedMatcher> Matchers; 131 Matchers.reserve(Cases.size()); 132 for (const auto &Case : Cases) { 133 std::string Tag = (TagBase + Twine(Case.first)).str(); 134 // HACK: Many matchers are not bindable, so ensure that tryBind will work. 135 DynTypedMatcher BoundMatcher(Case.second.Matcher); 136 BoundMatcher.setAllowBind(true); 137 auto M = *BoundMatcher.tryBind(Tag); 138 Matchers.push_back(!M.getTraversalKind() 139 ? M.withTraversalKind(DefaultTraversalKind) 140 : std::move(M)); 141 } 142 return Matchers; 143 } 144 145 // Simply gathers the contents of the various rules into a single rule. The 146 // actual work to combine these into an ordered choice is deferred to matcher 147 // registration. 148 RewriteRule transformer::applyFirst(ArrayRef<RewriteRule> Rules) { 149 RewriteRule R; 150 for (auto &Rule : Rules) 151 R.Cases.append(Rule.Cases.begin(), Rule.Cases.end()); 152 return R; 153 } 154 155 std::vector<DynTypedMatcher> 156 transformer::detail::buildMatchers(const RewriteRule &Rule) { 157 // Map the cases into buckets of matchers -- one for each "root" AST kind, 158 // which guarantees that they can be combined in a single anyOf matcher. Each 159 // case is paired with an identifying number that is converted to a string id 160 // in `taggedMatchers`. 161 std::map<ASTNodeKind, SmallVector<std::pair<size_t, RewriteRule::Case>, 1>> 162 Buckets; 163 const SmallVectorImpl<RewriteRule::Case> &Cases = Rule.Cases; 164 for (int I = 0, N = Cases.size(); I < N; ++I) { 165 assert(hasValidKind(Cases[I].Matcher) && 166 "Matcher must be non-(Qual)Type node matcher"); 167 Buckets[Cases[I].Matcher.getSupportedKind()].emplace_back(I, Cases[I]); 168 } 169 170 // Each anyOf explicitly controls the traversal kind. The anyOf itself is set 171 // to `TK_AsIs` to ensure no nodes are skipped, thereby deferring to the kind 172 // of the branches. Then, each branch is either left as is, if the kind is 173 // already set, or explicitly set to `TK_IgnoreUnlessSpelledInSource`. We 174 // choose this setting, because we think it is the one most friendly to 175 // beginners, who are (largely) the target audience of Transformer. 176 std::vector<DynTypedMatcher> Matchers; 177 for (const auto &Bucket : Buckets) { 178 DynTypedMatcher M = DynTypedMatcher::constructVariadic( 179 DynTypedMatcher::VO_AnyOf, Bucket.first, 180 taggedMatchers("Tag", Bucket.second, TK_IgnoreUnlessSpelledInSource)); 181 M.setAllowBind(true); 182 // `tryBind` is guaranteed to succeed, because `AllowBind` was set to true. 183 Matchers.push_back( 184 M.tryBind(RewriteRule::RootID)->withTraversalKind(TK_AsIs)); 185 } 186 return Matchers; 187 } 188 189 DynTypedMatcher transformer::detail::buildMatcher(const RewriteRule &Rule) { 190 std::vector<DynTypedMatcher> Ms = buildMatchers(Rule); 191 assert(Ms.size() == 1 && "Cases must have compatible matchers."); 192 return Ms[0]; 193 } 194 195 SourceLocation transformer::detail::getRuleMatchLoc(const MatchResult &Result) { 196 auto &NodesMap = Result.Nodes.getMap(); 197 auto Root = NodesMap.find(RewriteRule::RootID); 198 assert(Root != NodesMap.end() && "Transformation failed: missing root node."); 199 llvm::Optional<CharSourceRange> RootRange = tooling::getRangeForEdit( 200 CharSourceRange::getTokenRange(Root->second.getSourceRange()), 201 *Result.Context); 202 if (RootRange) 203 return RootRange->getBegin(); 204 // The match doesn't have a coherent range, so fall back to the expansion 205 // location as the "beginning" of the match. 206 return Result.SourceManager->getExpansionLoc( 207 Root->second.getSourceRange().getBegin()); 208 } 209 210 // Finds the case that was "selected" -- that is, whose matcher triggered the 211 // `MatchResult`. 212 const RewriteRule::Case & 213 transformer::detail::findSelectedCase(const MatchResult &Result, 214 const RewriteRule &Rule) { 215 if (Rule.Cases.size() == 1) 216 return Rule.Cases[0]; 217 218 auto &NodesMap = Result.Nodes.getMap(); 219 for (size_t i = 0, N = Rule.Cases.size(); i < N; ++i) { 220 std::string Tag = ("Tag" + Twine(i)).str(); 221 if (NodesMap.find(Tag) != NodesMap.end()) 222 return Rule.Cases[i]; 223 } 224 llvm_unreachable("No tag found for this rule."); 225 } 226 227 constexpr llvm::StringLiteral RewriteRule::RootID; 228 229 TextGenerator tooling::text(std::string M) { 230 return std::make_shared<SimpleTextGenerator>(std::move(M)); 231 } 232