Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:37:12

0001 //===--- RewriteRule.h - RewriteRule class ----------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 ///  \file
0010 ///  Defines the RewriteRule class and related functions for creating,
0011 ///  modifying and interpreting RewriteRules.
0012 ///
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_CLANG_TOOLING_TRANSFORMER_REWRITERULE_H
0016 #define LLVM_CLANG_TOOLING_TRANSFORMER_REWRITERULE_H
0017 
0018 #include "clang/ASTMatchers/ASTMatchFinder.h"
0019 #include "clang/ASTMatchers/ASTMatchers.h"
0020 #include "clang/ASTMatchers/ASTMatchersInternal.h"
0021 #include "clang/Tooling/Refactoring/AtomicChange.h"
0022 #include "clang/Tooling/Transformer/MatchConsumer.h"
0023 #include "clang/Tooling/Transformer/RangeSelector.h"
0024 #include "llvm/ADT/Any.h"
0025 #include "llvm/ADT/STLExtras.h"
0026 #include "llvm/ADT/SmallVector.h"
0027 #include "llvm/Support/Error.h"
0028 #include <functional>
0029 #include <string>
0030 #include <utility>
0031 
0032 namespace clang {
0033 namespace transformer {
0034 // Specifies how to interpret an edit.
0035 enum class EditKind {
0036   // Edits a source range in the file.
0037   Range,
0038   // Inserts an include in the file. The `Replacement` field is the name of the
0039   // newly included file.
0040   AddInclude,
0041 };
0042 
0043 /// A concrete description of a source edit, represented by a character range in
0044 /// the source to be replaced and a corresponding replacement string.
0045 struct Edit {
0046   EditKind Kind = EditKind::Range;
0047   CharSourceRange Range;
0048   std::string Replacement;
0049   std::string Note;
0050   llvm::Any Metadata;
0051 };
0052 
0053 /// Format of the path in an include directive -- angle brackets or quotes.
0054 enum class IncludeFormat {
0055   Quoted,
0056   Angled,
0057 };
0058 
0059 /// Maps a match result to a list of concrete edits (with possible
0060 /// failure). This type is a building block of rewrite rules, but users will
0061 /// generally work in terms of `ASTEdit`s (below) rather than directly in terms
0062 /// of `EditGenerator`.
0063 using EditGenerator = MatchConsumer<llvm::SmallVector<Edit, 1>>;
0064 
0065 template <typename T> using Generator = std::shared_ptr<MatchComputation<T>>;
0066 
0067 using TextGenerator = Generator<std::string>;
0068 
0069 using AnyGenerator = MatchConsumer<llvm::Any>;
0070 
0071 // Description of a source-code edit, expressed in terms of an AST node.
0072 // Includes: an ID for the (bound) node, a selector for source related to the
0073 // node, a replacement and, optionally, an explanation for the edit.
0074 //
0075 // * Target: the source code impacted by the rule. This identifies an AST node,
0076 //   or part thereof (\c Part), whose source range indicates the extent of the
0077 //   replacement applied by the replacement term.  By default, the extent is the
0078 //   node matched by the pattern term (\c NodePart::Node). Target's are typed
0079 //   (\c Kind), which guides the determination of the node extent.
0080 //
0081 // * Replacement: a function that produces a replacement string for the target,
0082 //   based on the match result.
0083 //
0084 // * Note: (optional) a note specifically for this edit, potentially referencing
0085 //   elements of the match.  This will be displayed to the user, where possible;
0086 //   for example, in clang-tidy diagnostics.  Use of notes should be rare --
0087 //   explanations of the entire rewrite should be set in the rule
0088 //   (`RewriteRule::Explanation`) instead.  Notes serve the rare cases wherein
0089 //   edit-specific diagnostics are required.
0090 //
0091 // `ASTEdit` should be built using the `change` convenience functions. For
0092 // example,
0093 // \code
0094 //   changeTo(name(fun), cat("Frodo"))
0095 // \endcode
0096 // Or, if we use Stencil for the TextGenerator:
0097 // \code
0098 //   using stencil::cat;
0099 //   changeTo(statement(thenNode), cat("{", thenNode, "}"))
0100 //   changeTo(callArgs(call), cat(x, ",", y))
0101 // \endcode
0102 // Or, if you are changing the node corresponding to the rule's matcher, you can
0103 // use the single-argument override of \c change:
0104 // \code
0105 //   changeTo(cat("different_expr"))
0106 // \endcode
0107 struct ASTEdit {
0108   EditKind Kind = EditKind::Range;
0109   RangeSelector TargetRange;
0110   TextGenerator Replacement;
0111   TextGenerator Note;
0112   // Not all transformations will want or need to attach metadata and therefore
0113   // should not be required to do so.
0114   AnyGenerator Metadata = [](const ast_matchers::MatchFinder::MatchResult &)
0115       -> llvm::Expected<llvm::Any> {
0116     return llvm::Expected<llvm::Any>(llvm::Any());
0117   };
0118 };
0119 
0120 /// Generates a single (specified) edit.
0121 EditGenerator edit(ASTEdit E);
0122 
0123 /// Lifts a list of `ASTEdit`s into an `EditGenerator`.
0124 ///
0125 /// The `EditGenerator` will return an empty vector if any of the edits apply to
0126 /// portions of the source that are ineligible for rewriting (certain
0127 /// interactions with macros, for example) and it will fail if any invariants
0128 /// are violated relating to bound nodes in the match.  However, it does not
0129 /// fail in the case of conflicting edits -- conflict handling is left to
0130 /// clients.  We recommend use of the \c AtomicChange or \c Replacements classes
0131 /// for assistance in detecting such conflicts.
0132 EditGenerator editList(llvm::SmallVector<ASTEdit, 1> Edits);
0133 
0134 /// Generates no edits.
0135 inline EditGenerator noEdits() { return editList({}); }
0136 
0137 /// Generates a single, no-op edit anchored at the start location of the
0138 /// specified range. A `noopEdit` may be preferred over `noEdits` to associate a
0139 /// diagnostic `Explanation` with the rule.
0140 EditGenerator noopEdit(RangeSelector Anchor);
0141 
0142 /// Generates a single, no-op edit with the associated note anchored at the
0143 /// start location of the specified range.
0144 ASTEdit note(RangeSelector Anchor, TextGenerator Note);
0145 
0146 /// Version of `ifBound` specialized to `ASTEdit`.
0147 inline EditGenerator ifBound(std::string ID, ASTEdit TrueEdit,
0148                              ASTEdit FalseEdit) {
0149   return ifBound(std::move(ID), edit(std::move(TrueEdit)),
0150                  edit(std::move(FalseEdit)));
0151 }
0152 
0153 /// Version of `ifBound` that has no "False" branch. If the node is not bound,
0154 /// then no edits are produced.
0155 inline EditGenerator ifBound(std::string ID, ASTEdit TrueEdit) {
0156   return ifBound(std::move(ID), edit(std::move(TrueEdit)), noEdits());
0157 }
0158 
0159 /// Flattens a list of generators into a single generator whose elements are the
0160 /// concatenation of the results of the argument generators.
0161 EditGenerator flattenVector(SmallVector<EditGenerator, 2> Generators);
0162 
0163 namespace detail {
0164 /// Helper function to construct an \c EditGenerator. Overloaded for common
0165 /// cases so that user doesn't need to specify which factory function to
0166 /// use. This pattern gives benefits similar to implicit constructors, while
0167 /// maintaing a higher degree of explicitness.
0168 inline EditGenerator injectEdits(ASTEdit E) { return edit(std::move(E)); }
0169 inline EditGenerator injectEdits(EditGenerator G) { return G; }
0170 } // namespace detail
0171 
0172 template <typename... Ts> EditGenerator flatten(Ts &&...Edits) {
0173   return flattenVector({detail::injectEdits(std::forward<Ts>(Edits))...});
0174 }
0175 
0176 // Every rewrite rule is triggered by a match against some AST node.
0177 // Transformer guarantees that this ID is bound to the triggering node whenever
0178 // a rewrite rule is applied.
0179 extern const char RootID[];
0180 
0181 /// Replaces a portion of the source text with \p Replacement.
0182 ASTEdit changeTo(RangeSelector Target, TextGenerator Replacement);
0183 /// DEPRECATED: use \c changeTo.
0184 inline ASTEdit change(RangeSelector Target, TextGenerator Replacement) {
0185   return changeTo(std::move(Target), std::move(Replacement));
0186 }
0187 
0188 /// Replaces the entirety of a RewriteRule's match with \p Replacement.  For
0189 /// example, to replace a function call, one could write:
0190 /// \code
0191 ///   makeRule(callExpr(callee(functionDecl(hasName("foo")))),
0192 ///            changeTo(cat("bar()")))
0193 /// \endcode
0194 inline ASTEdit changeTo(TextGenerator Replacement) {
0195   return changeTo(node(RootID), std::move(Replacement));
0196 }
0197 /// DEPRECATED: use \c changeTo.
0198 inline ASTEdit change(TextGenerator Replacement) {
0199   return changeTo(std::move(Replacement));
0200 }
0201 
0202 /// Inserts \p Replacement before \p S, leaving the source selected by \S
0203 /// unchanged.
0204 inline ASTEdit insertBefore(RangeSelector S, TextGenerator Replacement) {
0205   return changeTo(before(std::move(S)), std::move(Replacement));
0206 }
0207 
0208 /// Inserts \p Replacement after \p S, leaving the source selected by \S
0209 /// unchanged.
0210 inline ASTEdit insertAfter(RangeSelector S, TextGenerator Replacement) {
0211   return changeTo(after(std::move(S)), std::move(Replacement));
0212 }
0213 
0214 /// Removes the source selected by \p S.
0215 ASTEdit remove(RangeSelector S);
0216 
0217 /// Adds an include directive for the given header to the file of `Target`. The
0218 /// particular location specified by `Target` is ignored.
0219 ASTEdit addInclude(RangeSelector Target, StringRef Header,
0220                    IncludeFormat Format = IncludeFormat::Quoted);
0221 
0222 /// Adds an include directive for the given header to the file associated with
0223 /// `RootID`. If `RootID` matches inside a macro expansion, will add the
0224 /// directive to the file in which the macro was expanded (as opposed to the
0225 /// file in which the macro is defined).
0226 inline ASTEdit addInclude(StringRef Header,
0227                           IncludeFormat Format = IncludeFormat::Quoted) {
0228   return addInclude(expansion(node(RootID)), Header, Format);
0229 }
0230 
0231 // FIXME: If `Metadata` returns an `llvm::Expected<T>` the `AnyGenerator` will
0232 // construct an `llvm::Expected<llvm::Any>` where no error is present but the
0233 // `llvm::Any` holds the error. This is unlikely but potentially surprising.
0234 // Perhaps the `llvm::Expected` should be unwrapped, or perhaps this should be a
0235 // compile-time error. No solution here is perfect.
0236 //
0237 // Note: This function template accepts any type callable with a MatchResult
0238 // rather than a `std::function` because the return-type needs to be deduced. If
0239 // it accepted a `std::function<R(MatchResult)>`, lambdas or other callable
0240 // types would not be able to deduce `R`, and users would be forced to specify
0241 // explicitly the type they intended to return by wrapping the lambda at the
0242 // call-site.
0243 template <typename Callable>
0244 inline ASTEdit withMetadata(ASTEdit Edit, Callable Metadata) {
0245   Edit.Metadata =
0246       [Gen = std::move(Metadata)](
0247           const ast_matchers::MatchFinder::MatchResult &R) -> llvm::Any {
0248     return Gen(R);
0249   };
0250 
0251   return Edit;
0252 }
0253 
0254 /// Assuming that the inner range is enclosed by the outer range, creates
0255 /// precision edits to remove the parts of the outer range that are not included
0256 /// in the inner range.
0257 inline EditGenerator shrinkTo(RangeSelector outer, RangeSelector inner) {
0258   return editList({remove(enclose(before(outer), before(inner))),
0259                    remove(enclose(after(inner), after(outer)))});
0260 }
0261 
0262 /// Description of a source-code transformation.
0263 //
0264 // A *rewrite rule* describes a transformation of source code. A simple rule
0265 // contains each of the following components:
0266 //
0267 // * Matcher: the pattern term, expressed as clang matchers (with Transformer
0268 //   extensions).
0269 //
0270 // * Edits: a set of Edits to the source code, described with ASTEdits.
0271 //
0272 // However, rules can also consist of (sub)rules, where the first that matches
0273 // is applied and the rest are ignored.  So, the above components together form
0274 // a logical "case" and a rule is a sequence of cases.
0275 //
0276 // Rule cases have an additional, implicit, component: the parameters. These are
0277 // portions of the pattern which are left unspecified, yet bound in the pattern
0278 // so that we can reference them in the edits.
0279 //
0280 // The \c Transformer class can be used to apply the rewrite rule and obtain the
0281 // corresponding replacements.
0282 struct RewriteRuleBase {
0283   struct Case {
0284     ast_matchers::internal::DynTypedMatcher Matcher;
0285     EditGenerator Edits;
0286   };
0287   // We expect RewriteRules will most commonly include only one case.
0288   SmallVector<Case, 1> Cases;
0289 };
0290 
0291 /// A source-code transformation with accompanying metadata.
0292 ///
0293 /// When a case of the rule matches, the \c Transformer invokes the
0294 /// corresponding metadata generator and provides it alongside the edits.
0295 template <typename MetadataT> struct RewriteRuleWith : RewriteRuleBase {
0296   SmallVector<Generator<MetadataT>, 1> Metadata;
0297 };
0298 
0299 template <> struct RewriteRuleWith<void> : RewriteRuleBase {};
0300 
0301 using RewriteRule = RewriteRuleWith<void>;
0302 
0303 namespace detail {
0304 
0305 RewriteRule makeRule(ast_matchers::internal::DynTypedMatcher M,
0306                      EditGenerator Edits);
0307 
0308 template <typename MetadataT>
0309 RewriteRuleWith<MetadataT> makeRule(ast_matchers::internal::DynTypedMatcher M,
0310                                     EditGenerator Edits,
0311                                     Generator<MetadataT> Metadata) {
0312   RewriteRuleWith<MetadataT> R;
0313   R.Cases = {{std::move(M), std::move(Edits)}};
0314   R.Metadata = {std::move(Metadata)};
0315   return R;
0316 }
0317 
0318 inline EditGenerator makeEditGenerator(EditGenerator Edits) { return Edits; }
0319 EditGenerator makeEditGenerator(llvm::SmallVector<ASTEdit, 1> Edits);
0320 EditGenerator makeEditGenerator(ASTEdit Edit);
0321 
0322 } // namespace detail
0323 
0324 /// Constructs a simple \c RewriteRule. \c Edits can be an \c EditGenerator,
0325 /// multiple \c ASTEdits, or a single \c ASTEdit.
0326 /// @{
0327 template <int &..., typename EditsT>
0328 RewriteRule makeRule(ast_matchers::internal::DynTypedMatcher M,
0329                      EditsT &&Edits) {
0330   return detail::makeRule(
0331       std::move(M), detail::makeEditGenerator(std::forward<EditsT>(Edits)));
0332 }
0333 
0334 RewriteRule makeRule(ast_matchers::internal::DynTypedMatcher M,
0335                      std::initializer_list<ASTEdit> Edits);
0336 /// @}
0337 
0338 /// Overloads of \c makeRule that also generate metadata when matching.
0339 /// @{
0340 template <typename MetadataT, int &..., typename EditsT>
0341 RewriteRuleWith<MetadataT> makeRule(ast_matchers::internal::DynTypedMatcher M,
0342                                     EditsT &&Edits,
0343                                     Generator<MetadataT> Metadata) {
0344   return detail::makeRule(
0345       std::move(M), detail::makeEditGenerator(std::forward<EditsT>(Edits)),
0346       std::move(Metadata));
0347 }
0348 
0349 template <typename MetadataT>
0350 RewriteRuleWith<MetadataT> makeRule(ast_matchers::internal::DynTypedMatcher M,
0351                                     std::initializer_list<ASTEdit> Edits,
0352                                     Generator<MetadataT> Metadata) {
0353   return detail::makeRule(std::move(M),
0354                           detail::makeEditGenerator(std::move(Edits)),
0355                           std::move(Metadata));
0356 }
0357 /// @}
0358 
0359 /// For every case in Rule, adds an include directive for the given header. The
0360 /// common use is assumed to be a rule with only one case. For example, to
0361 /// replace a function call and add headers corresponding to the new code, one
0362 /// could write:
0363 /// \code
0364 ///   auto R = makeRule(callExpr(callee(functionDecl(hasName("foo")))),
0365 ///            changeTo(cat("bar()")));
0366 ///   addInclude(R, "path/to/bar_header.h");
0367 ///   addInclude(R, "vector", IncludeFormat::Angled);
0368 /// \endcode
0369 void addInclude(RewriteRuleBase &Rule, llvm::StringRef Header,
0370                 IncludeFormat Format = IncludeFormat::Quoted);
0371 
0372 /// Applies the first rule whose pattern matches; other rules are ignored.  If
0373 /// the matchers are independent then order doesn't matter. In that case,
0374 /// `applyFirst` is simply joining the set of rules into one.
0375 //
0376 // `applyFirst` is like an `anyOf` matcher with an edit action attached to each
0377 // of its cases. Anywhere you'd use `anyOf(m1.bind("id1"), m2.bind("id2"))` and
0378 // then dispatch on those ids in your code for control flow, `applyFirst` lifts
0379 // that behavior to the rule level.  So, you can write `applyFirst({makeRule(m1,
0380 // action1), makeRule(m2, action2), ...});`
0381 //
0382 // For example, consider a type `T` with a deterministic serialization function,
0383 // `serialize()`.  For performance reasons, we would like to make it
0384 // non-deterministic.  Therefore, we want to drop the expectation that
0385 // `a.serialize() = b.serialize() iff a = b` (although we'll maintain
0386 // `deserialize(a.serialize()) = a`).
0387 //
0388 // We have three cases to consider (for some equality function, `eq`):
0389 // ```
0390 // eq(a.serialize(), b.serialize()) --> eq(a,b)
0391 // eq(a, b.serialize())             --> eq(deserialize(a), b)
0392 // eq(a.serialize(), b)             --> eq(a, deserialize(b))
0393 // ```
0394 //
0395 // `applyFirst` allows us to specify each independently:
0396 // ```
0397 // auto eq_fun = functionDecl(...);
0398 // auto method_call = cxxMemberCallExpr(...);
0399 //
0400 // auto two_calls = callExpr(callee(eq_fun), hasArgument(0, method_call),
0401 //                           hasArgument(1, method_call));
0402 // auto left_call =
0403 //     callExpr(callee(eq_fun), callExpr(hasArgument(0, method_call)));
0404 // auto right_call =
0405 //     callExpr(callee(eq_fun), callExpr(hasArgument(1, method_call)));
0406 //
0407 // RewriteRule R = applyFirst({makeRule(two_calls, two_calls_action),
0408 //                             makeRule(left_call, left_call_action),
0409 //                             makeRule(right_call, right_call_action)});
0410 // ```
0411 /// @{
0412 template <typename MetadataT>
0413 RewriteRuleWith<MetadataT>
0414 applyFirst(ArrayRef<RewriteRuleWith<MetadataT>> Rules) {
0415   RewriteRuleWith<MetadataT> R;
0416   for (auto &Rule : Rules) {
0417     assert(Rule.Cases.size() == Rule.Metadata.size() &&
0418            "mis-match in case and metadata array size");
0419     R.Cases.append(Rule.Cases.begin(), Rule.Cases.end());
0420     R.Metadata.append(Rule.Metadata.begin(), Rule.Metadata.end());
0421   }
0422   return R;
0423 }
0424 
0425 template <>
0426 RewriteRuleWith<void> applyFirst(ArrayRef<RewriteRuleWith<void>> Rules);
0427 
0428 template <typename MetadataT>
0429 RewriteRuleWith<MetadataT>
0430 applyFirst(const std::vector<RewriteRuleWith<MetadataT>> &Rules) {
0431   return applyFirst(llvm::ArrayRef(Rules));
0432 }
0433 
0434 template <typename MetadataT>
0435 RewriteRuleWith<MetadataT>
0436 applyFirst(std::initializer_list<RewriteRuleWith<MetadataT>> Rules) {
0437   return applyFirst(llvm::ArrayRef(Rules.begin(), Rules.end()));
0438 }
0439 /// @}
0440 
0441 /// Converts a \c RewriteRuleWith<T> to a \c RewriteRule by stripping off the
0442 /// metadata generators.
0443 template <int &..., typename MetadataT>
0444 std::enable_if_t<!std::is_same<MetadataT, void>::value, RewriteRule>
0445 stripMetadata(RewriteRuleWith<MetadataT> Rule) {
0446   RewriteRule R;
0447   R.Cases = std::move(Rule.Cases);
0448   return R;
0449 }
0450 
0451 /// Applies `Rule` to all descendants of the node bound to `NodeId`. `Rule` can
0452 /// refer to nodes bound by the calling rule. `Rule` is not applied to the node
0453 /// itself.
0454 ///
0455 /// For example,
0456 /// ```
0457 /// auto InlineX =
0458 ///     makeRule(declRefExpr(to(varDecl(hasName("x")))), changeTo(cat("3")));
0459 /// makeRule(functionDecl(hasName("f"), hasBody(stmt().bind("body"))).bind("f"),
0460 ///          flatten(
0461 ///            changeTo(name("f"), cat("newName")),
0462 ///            rewriteDescendants("body", InlineX)));
0463 /// ```
0464 /// Here, we find the function `f`, change its name to `newName` and change all
0465 /// appearances of `x` in its body to `3`.
0466 EditGenerator rewriteDescendants(std::string NodeId, RewriteRule Rule);
0467 
0468 /// The following three functions are a low-level part of the RewriteRule
0469 /// API. We expose them for use in implementing the fixtures that interpret
0470 /// RewriteRule, like Transformer and TransfomerTidy, or for more advanced
0471 /// users.
0472 //
0473 // FIXME: These functions are really public, if advanced, elements of the
0474 // RewriteRule API.  Recast them as such.  Or, just declare these functions
0475 // public and well-supported and move them out of `detail`.
0476 namespace detail {
0477 /// The following overload set is a version of `rewriteDescendants` that
0478 /// operates directly on the AST, rather than generating a Transformer
0479 /// combinator. It applies `Rule` to all descendants of `Node`, although not
0480 /// `Node` itself. `Rule` can refer to nodes bound in `Result`.
0481 ///
0482 /// For example, assuming that "body" is bound to a function body in MatchResult
0483 /// `Results`, this will produce edits to change all appearances of `x` in that
0484 /// body to `3`.
0485 /// ```
0486 /// auto InlineX =
0487 ///     makeRule(declRefExpr(to(varDecl(hasName("x")))), changeTo(cat("3")));
0488 /// const auto *Node = Results.Nodes.getNodeAs<Stmt>("body");
0489 /// auto Edits = rewriteDescendants(*Node, InlineX, Results);
0490 /// ```
0491 /// @{
0492 llvm::Expected<SmallVector<Edit, 1>>
0493 rewriteDescendants(const Decl &Node, RewriteRule Rule,
0494                    const ast_matchers::MatchFinder::MatchResult &Result);
0495 
0496 llvm::Expected<SmallVector<Edit, 1>>
0497 rewriteDescendants(const Stmt &Node, RewriteRule Rule,
0498                    const ast_matchers::MatchFinder::MatchResult &Result);
0499 
0500 llvm::Expected<SmallVector<Edit, 1>>
0501 rewriteDescendants(const TypeLoc &Node, RewriteRule Rule,
0502                    const ast_matchers::MatchFinder::MatchResult &Result);
0503 
0504 llvm::Expected<SmallVector<Edit, 1>>
0505 rewriteDescendants(const DynTypedNode &Node, RewriteRule Rule,
0506                    const ast_matchers::MatchFinder::MatchResult &Result);
0507 /// @}
0508 
0509 /// Builds a single matcher for the rule, covering all of the rule's cases.
0510 /// Only supports Rules whose cases' matchers share the same base "kind"
0511 /// (`Stmt`, `Decl`, etc.)  Deprecated: use `buildMatchers` instead, which
0512 /// supports mixing matchers of different kinds.
0513 ast_matchers::internal::DynTypedMatcher
0514 buildMatcher(const RewriteRuleBase &Rule);
0515 
0516 /// Builds a set of matchers that cover the rule.
0517 ///
0518 /// One matcher is built for each distinct node matcher base kind: Stmt, Decl,
0519 /// etc. Node-matchers for `QualType` and `Type` are not permitted, since such
0520 /// nodes carry no source location information and are therefore not relevant
0521 /// for rewriting. If any such matchers are included, will return an empty
0522 /// vector.
0523 std::vector<ast_matchers::internal::DynTypedMatcher>
0524 buildMatchers(const RewriteRuleBase &Rule);
0525 
0526 /// Gets the beginning location of the source matched by a rewrite rule. If the
0527 /// match occurs within a macro expansion, returns the beginning of the
0528 /// expansion point. `Result` must come from the matching of a rewrite rule.
0529 SourceLocation
0530 getRuleMatchLoc(const ast_matchers::MatchFinder::MatchResult &Result);
0531 
0532 /// Returns the index of the \c Case of \c Rule that was selected in the match
0533 /// result. Assumes a matcher built with \c buildMatcher.
0534 size_t findSelectedCase(const ast_matchers::MatchFinder::MatchResult &Result,
0535                         const RewriteRuleBase &Rule);
0536 } // namespace detail
0537 } // namespace transformer
0538 } // namespace clang
0539 
0540 #endif // LLVM_CLANG_TOOLING_TRANSFORMER_REWRITERULE_H