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