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/AST/ASTTypeTraits.h"
11 #include "clang/AST/Stmt.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
14 #include "clang/Basic/SourceLocation.h"
15 #include "clang/Tooling/Transformer/SourceCode.h"
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/Errc.h"
19 #include "llvm/Support/Error.h"
20 #include <map>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 using namespace clang;
26 using namespace transformer;
27 
28 using ast_matchers::MatchFinder;
29 using ast_matchers::internal::DynTypedMatcher;
30 
31 using MatchResult = MatchFinder::MatchResult;
32 
33 const char transformer::RootID[] = "___root___";
34 
35 static Expected<SmallVector<transformer::Edit, 1>>
36 translateEdits(const MatchResult &Result, ArrayRef<ASTEdit> ASTEdits) {
37   SmallVector<transformer::Edit, 1> Edits;
38   for (const auto &E : ASTEdits) {
39     Expected<CharSourceRange> Range = E.TargetRange(Result);
40     if (!Range)
41       return Range.takeError();
42     llvm::Optional<CharSourceRange> EditRange =
43         tooling::getRangeForEdit(*Range, *Result.Context);
44     // FIXME: let user specify whether to treat this case as an error or ignore
45     // it as is currently done.
46     if (!EditRange)
47       return SmallVector<Edit, 0>();
48     auto Replacement = E.Replacement->eval(Result);
49     if (!Replacement)
50       return Replacement.takeError();
51     auto Metadata = E.Metadata(Result);
52     if (!Metadata)
53       return Metadata.takeError();
54     transformer::Edit T;
55     T.Kind = E.Kind;
56     T.Range = *EditRange;
57     T.Replacement = std::move(*Replacement);
58     T.Metadata = std::move(*Metadata);
59     Edits.push_back(std::move(T));
60   }
61   return Edits;
62 }
63 
64 EditGenerator transformer::editList(SmallVector<ASTEdit, 1> Edits) {
65   return [Edits = std::move(Edits)](const MatchResult &Result) {
66     return translateEdits(Result, Edits);
67   };
68 }
69 
70 EditGenerator transformer::edit(ASTEdit Edit) {
71   return [Edit = std::move(Edit)](const MatchResult &Result) {
72     return translateEdits(Result, {Edit});
73   };
74 }
75 
76 EditGenerator
77 transformer::flattenVector(SmallVector<EditGenerator, 2> Generators) {
78   if (Generators.size() == 1)
79     return std::move(Generators[0]);
80   return
81       [Gs = std::move(Generators)](
82           const MatchResult &Result) -> llvm::Expected<SmallVector<Edit, 1>> {
83         SmallVector<Edit, 1> AllEdits;
84         for (const auto &G : Gs) {
85           llvm::Expected<SmallVector<Edit, 1>> Edits = G(Result);
86           if (!Edits)
87             return Edits.takeError();
88           AllEdits.append(Edits->begin(), Edits->end());
89         }
90         return AllEdits;
91       };
92 }
93 
94 ASTEdit transformer::changeTo(RangeSelector Target, TextGenerator Replacement) {
95   ASTEdit E;
96   E.TargetRange = std::move(Target);
97   E.Replacement = std::move(Replacement);
98   return E;
99 }
100 
101 namespace {
102 /// A \c TextGenerator that always returns a fixed string.
103 class SimpleTextGenerator : public MatchComputation<std::string> {
104   std::string S;
105 
106 public:
107   SimpleTextGenerator(std::string S) : S(std::move(S)) {}
108   llvm::Error eval(const ast_matchers::MatchFinder::MatchResult &,
109                    std::string *Result) const override {
110     Result->append(S);
111     return llvm::Error::success();
112   }
113   std::string toString() const override {
114     return (llvm::Twine("text(\"") + S + "\")").str();
115   }
116 };
117 } // namespace
118 
119 static TextGenerator makeText(std::string S) {
120   return std::make_shared<SimpleTextGenerator>(std::move(S));
121 }
122 
123 ASTEdit transformer::remove(RangeSelector S) {
124   return change(std::move(S), makeText(""));
125 }
126 
127 static std::string formatHeaderPath(StringRef Header, IncludeFormat Format) {
128   switch (Format) {
129   case transformer::IncludeFormat::Quoted:
130     return Header.str();
131   case transformer::IncludeFormat::Angled:
132     return ("<" + Header + ">").str();
133   }
134   llvm_unreachable("Unknown transformer::IncludeFormat enum");
135 }
136 
137 ASTEdit transformer::addInclude(RangeSelector Target, StringRef Header,
138                                 IncludeFormat Format) {
139   ASTEdit E;
140   E.Kind = EditKind::AddInclude;
141   E.TargetRange = Target;
142   E.Replacement = makeText(formatHeaderPath(Header, Format));
143   return E;
144 }
145 
146 RewriteRule transformer::makeRule(DynTypedMatcher M, EditGenerator Edits,
147                                   TextGenerator Explanation) {
148   return RewriteRule{{RewriteRule::Case{std::move(M), std::move(Edits),
149                                         std::move(Explanation)}}};
150 }
151 
152 namespace {
153 
154 /// Unconditionally binds the given node set before trying `InnerMatcher` and
155 /// keeps the bound nodes on a successful match.
156 template <typename T>
157 class BindingsMatcher : public ast_matchers::internal::MatcherInterface<T> {
158   ast_matchers::BoundNodes Nodes;
159   const ast_matchers::internal::Matcher<T> InnerMatcher;
160 
161 public:
162   explicit BindingsMatcher(ast_matchers::BoundNodes Nodes,
163                            ast_matchers::internal::Matcher<T> InnerMatcher)
164       : Nodes(std::move(Nodes)), InnerMatcher(std::move(InnerMatcher)) {}
165 
166   bool matches(
167       const T &Node, ast_matchers::internal::ASTMatchFinder *Finder,
168       ast_matchers::internal::BoundNodesTreeBuilder *Builder) const override {
169     ast_matchers::internal::BoundNodesTreeBuilder Result(*Builder);
170     for (const auto &N : Nodes.getMap())
171       Result.setBinding(N.first, N.second);
172     if (InnerMatcher.matches(Node, Finder, &Result)) {
173       *Builder = std::move(Result);
174       return true;
175     }
176     return false;
177   }
178 };
179 
180 /// Matches nodes of type T that have at least one descendant node for which the
181 /// given inner matcher matches.  Will match for each descendant node that
182 /// matches.  Based on ForEachDescendantMatcher, but takes a dynamic matcher,
183 /// instead of a static one, because it is used by RewriteRule, which carries
184 /// (only top-level) dynamic matchers.
185 template <typename T>
186 class DynamicForEachDescendantMatcher
187     : public ast_matchers::internal::MatcherInterface<T> {
188   const DynTypedMatcher DescendantMatcher;
189 
190 public:
191   explicit DynamicForEachDescendantMatcher(DynTypedMatcher DescendantMatcher)
192       : DescendantMatcher(std::move(DescendantMatcher)) {}
193 
194   bool matches(
195       const T &Node, ast_matchers::internal::ASTMatchFinder *Finder,
196       ast_matchers::internal::BoundNodesTreeBuilder *Builder) const override {
197     return Finder->matchesDescendantOf(
198         Node, this->DescendantMatcher, Builder,
199         ast_matchers::internal::ASTMatchFinder::BK_All);
200   }
201 };
202 
203 template <typename T>
204 ast_matchers::internal::Matcher<T>
205 forEachDescendantDynamically(ast_matchers::BoundNodes Nodes,
206                              DynTypedMatcher M) {
207   return ast_matchers::internal::makeMatcher(new BindingsMatcher<T>(
208       std::move(Nodes),
209       ast_matchers::internal::makeMatcher(
210           new DynamicForEachDescendantMatcher<T>(std::move(M)))));
211 }
212 
213 class ApplyRuleCallback : public MatchFinder::MatchCallback {
214 public:
215   ApplyRuleCallback(RewriteRule Rule) : Rule(std::move(Rule)) {}
216 
217   template <typename T>
218   void registerMatchers(const ast_matchers::BoundNodes &Nodes,
219                         MatchFinder *MF) {
220     for (auto &Matcher : transformer::detail::buildMatchers(Rule))
221       MF->addMatcher(forEachDescendantDynamically<T>(Nodes, Matcher), this);
222   }
223 
224   void run(const MatchFinder::MatchResult &Result) override {
225     if (!Edits)
226       return;
227     transformer::RewriteRule::Case Case =
228         transformer::detail::findSelectedCase(Result, Rule);
229     auto Transformations = Case.Edits(Result);
230     if (!Transformations) {
231       Edits = Transformations.takeError();
232       return;
233     }
234     Edits->append(Transformations->begin(), Transformations->end());
235   }
236 
237   RewriteRule Rule;
238 
239   // Initialize to a non-error state.
240   Expected<SmallVector<Edit, 1>> Edits = SmallVector<Edit, 1>();
241 };
242 } // namespace
243 
244 template <typename T>
245 llvm::Expected<SmallVector<clang::transformer::Edit, 1>>
246 rewriteDescendantsImpl(const T &Node, RewriteRule Rule,
247                        const MatchResult &Result) {
248   ApplyRuleCallback Callback(std::move(Rule));
249   MatchFinder Finder;
250   Callback.registerMatchers<T>(Result.Nodes, &Finder);
251   Finder.match(Node, *Result.Context);
252   return std::move(Callback.Edits);
253 }
254 
255 llvm::Expected<SmallVector<clang::transformer::Edit, 1>>
256 transformer::detail::rewriteDescendants(const Decl &Node, RewriteRule Rule,
257                                         const MatchResult &Result) {
258   return rewriteDescendantsImpl(Node, std::move(Rule), Result);
259 }
260 
261 llvm::Expected<SmallVector<clang::transformer::Edit, 1>>
262 transformer::detail::rewriteDescendants(const Stmt &Node, RewriteRule Rule,
263                                         const MatchResult &Result) {
264   return rewriteDescendantsImpl(Node, std::move(Rule), Result);
265 }
266 
267 llvm::Expected<SmallVector<clang::transformer::Edit, 1>>
268 transformer::detail::rewriteDescendants(const TypeLoc &Node, RewriteRule Rule,
269                                         const MatchResult &Result) {
270   return rewriteDescendantsImpl(Node, std::move(Rule), Result);
271 }
272 
273 llvm::Expected<SmallVector<clang::transformer::Edit, 1>>
274 transformer::detail::rewriteDescendants(const DynTypedNode &DNode,
275                                         RewriteRule Rule,
276                                         const MatchResult &Result) {
277   if (const auto *Node = DNode.get<Decl>())
278     return rewriteDescendantsImpl(*Node, std::move(Rule), Result);
279   if (const auto *Node = DNode.get<Stmt>())
280     return rewriteDescendantsImpl(*Node, std::move(Rule), Result);
281   if (const auto *Node = DNode.get<TypeLoc>())
282     return rewriteDescendantsImpl(*Node, std::move(Rule), Result);
283 
284   return llvm::make_error<llvm::StringError>(
285       llvm::errc::invalid_argument,
286       "type unsupported for recursive rewriting, Kind=" +
287           DNode.getNodeKind().asStringRef());
288 }
289 
290 EditGenerator transformer::rewriteDescendants(std::string NodeId,
291                                               RewriteRule Rule) {
292   return [NodeId = std::move(NodeId),
293           Rule = std::move(Rule)](const MatchResult &Result)
294              -> llvm::Expected<SmallVector<clang::transformer::Edit, 1>> {
295     const ast_matchers::BoundNodes::IDToNodeMap &NodesMap =
296         Result.Nodes.getMap();
297     auto It = NodesMap.find(NodeId);
298     if (It == NodesMap.end())
299       return llvm::make_error<llvm::StringError>(llvm::errc::invalid_argument,
300                                                  "ID not bound: " + NodeId);
301     return detail::rewriteDescendants(It->second, std::move(Rule), Result);
302   };
303 }
304 
305 void transformer::addInclude(RewriteRule &Rule, StringRef Header,
306                              IncludeFormat Format) {
307   for (auto &Case : Rule.Cases)
308     Case.Edits = flatten(std::move(Case.Edits), addInclude(Header, Format));
309 }
310 
311 #ifndef NDEBUG
312 // Filters for supported matcher kinds. FIXME: Explicitly list the allowed kinds
313 // (all node matcher types except for `QualType` and `Type`), rather than just
314 // banning `QualType` and `Type`.
315 static bool hasValidKind(const DynTypedMatcher &M) {
316   return !M.canConvertTo<QualType>();
317 }
318 #endif
319 
320 // Binds each rule's matcher to a unique (and deterministic) tag based on
321 // `TagBase` and the id paired with the case. All of the returned matchers have
322 // their traversal kind explicitly set, either based on a pre-set kind or to the
323 // provided `DefaultTraversalKind`.
324 static std::vector<DynTypedMatcher> taggedMatchers(
325     StringRef TagBase,
326     const SmallVectorImpl<std::pair<size_t, RewriteRule::Case>> &Cases,
327     ast_type_traits::TraversalKind DefaultTraversalKind) {
328   std::vector<DynTypedMatcher> Matchers;
329   Matchers.reserve(Cases.size());
330   for (const auto &Case : Cases) {
331     std::string Tag = (TagBase + Twine(Case.first)).str();
332     // HACK: Many matchers are not bindable, so ensure that tryBind will work.
333     DynTypedMatcher BoundMatcher(Case.second.Matcher);
334     BoundMatcher.setAllowBind(true);
335     auto M = *BoundMatcher.tryBind(Tag);
336     Matchers.push_back(!M.getTraversalKind()
337                            ? M.withTraversalKind(DefaultTraversalKind)
338                            : std::move(M));
339   }
340   return Matchers;
341 }
342 
343 // Simply gathers the contents of the various rules into a single rule. The
344 // actual work to combine these into an ordered choice is deferred to matcher
345 // registration.
346 RewriteRule transformer::applyFirst(ArrayRef<RewriteRule> Rules) {
347   RewriteRule R;
348   for (auto &Rule : Rules)
349     R.Cases.append(Rule.Cases.begin(), Rule.Cases.end());
350   return R;
351 }
352 
353 std::vector<DynTypedMatcher>
354 transformer::detail::buildMatchers(const RewriteRule &Rule) {
355   // Map the cases into buckets of matchers -- one for each "root" AST kind,
356   // which guarantees that they can be combined in a single anyOf matcher. Each
357   // case is paired with an identifying number that is converted to a string id
358   // in `taggedMatchers`.
359   std::map<ASTNodeKind, SmallVector<std::pair<size_t, RewriteRule::Case>, 1>>
360       Buckets;
361   const SmallVectorImpl<RewriteRule::Case> &Cases = Rule.Cases;
362   for (int I = 0, N = Cases.size(); I < N; ++I) {
363     assert(hasValidKind(Cases[I].Matcher) &&
364            "Matcher must be non-(Qual)Type node matcher");
365     Buckets[Cases[I].Matcher.getSupportedKind()].emplace_back(I, Cases[I]);
366   }
367 
368   // Each anyOf explicitly controls the traversal kind. The anyOf itself is set
369   // to `TK_AsIs` to ensure no nodes are skipped, thereby deferring to the kind
370   // of the branches. Then, each branch is either left as is, if the kind is
371   // already set, or explicitly set to `TK_AsIs`. We choose this setting because
372   // it is the default interpretation of matchers.
373   std::vector<DynTypedMatcher> Matchers;
374   for (const auto &Bucket : Buckets) {
375     DynTypedMatcher M = DynTypedMatcher::constructVariadic(
376         DynTypedMatcher::VO_AnyOf, Bucket.first,
377         taggedMatchers("Tag", Bucket.second, TK_AsIs));
378     M.setAllowBind(true);
379     // `tryBind` is guaranteed to succeed, because `AllowBind` was set to true.
380     Matchers.push_back(M.tryBind(RootID)->withTraversalKind(TK_AsIs));
381   }
382   return Matchers;
383 }
384 
385 DynTypedMatcher transformer::detail::buildMatcher(const RewriteRule &Rule) {
386   std::vector<DynTypedMatcher> Ms = buildMatchers(Rule);
387   assert(Ms.size() == 1 && "Cases must have compatible matchers.");
388   return Ms[0];
389 }
390 
391 SourceLocation transformer::detail::getRuleMatchLoc(const MatchResult &Result) {
392   auto &NodesMap = Result.Nodes.getMap();
393   auto Root = NodesMap.find(RootID);
394   assert(Root != NodesMap.end() && "Transformation failed: missing root node.");
395   llvm::Optional<CharSourceRange> RootRange = tooling::getRangeForEdit(
396       CharSourceRange::getTokenRange(Root->second.getSourceRange()),
397       *Result.Context);
398   if (RootRange)
399     return RootRange->getBegin();
400   // The match doesn't have a coherent range, so fall back to the expansion
401   // location as the "beginning" of the match.
402   return Result.SourceManager->getExpansionLoc(
403       Root->second.getSourceRange().getBegin());
404 }
405 
406 // Finds the case that was "selected" -- that is, whose matcher triggered the
407 // `MatchResult`.
408 const RewriteRule::Case &
409 transformer::detail::findSelectedCase(const MatchResult &Result,
410                                   const RewriteRule &Rule) {
411   if (Rule.Cases.size() == 1)
412     return Rule.Cases[0];
413 
414   auto &NodesMap = Result.Nodes.getMap();
415   for (size_t i = 0, N = Rule.Cases.size(); i < N; ++i) {
416     std::string Tag = ("Tag" + Twine(i)).str();
417     if (NodesMap.find(Tag) != NodesMap.end())
418       return Rule.Cases[i];
419   }
420   llvm_unreachable("No tag found for this rule.");
421 }
422 
423 const llvm::StringRef RewriteRule::RootID = ::clang::transformer::RootID;
424 
425 TextGenerator tooling::text(std::string M) {
426   return std::make_shared<SimpleTextGenerator>(std::move(M));
427 }
428