clang  10.0.0svn
Transformer.cpp
Go to the documentation of this file.
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 
14 #include "llvm/Support/Error.h"
15 #include <utility>
16 #include <vector>
17 
18 using namespace clang;
19 using namespace tooling;
20 
22 
24  for (auto &Matcher : transformer::detail::buildMatchers(Rule))
25  MatchFinder->addDynamicMatcher(Matcher, this);
26 }
27 
29  if (Result.Context->getDiagnostics().hasErrorOccurred())
30  return;
31 
34  auto Transformations = transformer::detail::translateEdits(Result, Case.Edits);
35  if (!Transformations) {
36  Consumer(Transformations.takeError());
37  return;
38  }
39 
40  if (Transformations->empty()) {
41  // No rewrite applied (but no error encountered either).
43  llvm::errs() << "note: skipping match at loc ", *Result.SourceManager);
44  llvm::errs() << "\n";
45  return;
46  }
47 
48  // Record the results in the AtomicChange, anchored at the location of the
49  // first change.
50  AtomicChange AC(*Result.SourceManager,
51  (*Transformations)[0].Range.getBegin());
52  for (const auto &T : *Transformations) {
53  if (auto Err = AC.replace(*Result.SourceManager, T.Range, T.Replacement)) {
54  Consumer(std::move(Err));
55  return;
56  }
57  }
58 
59  for (const auto &I : Case.AddedIncludes) {
60  auto &Header = I.first;
61  switch (I.second) {
63  AC.addHeader(Header);
64  break;
66  AC.addHeader((llvm::Twine("<") + Header + ">").str());
67  break;
68  }
69  }
70 
71  Consumer(std::move(AC));
72 }
A class to allow finding matches over the Clang AST.
bool hasErrorOccurred() const
Definition: Diagnostic.h:747
Expected< SmallVector< Transformation, 1 > > translateEdits(const ast_matchers::MatchFinder::MatchResult &Result, llvm::ArrayRef< ASTEdit > Edits)
Attempts to translate Edits, which are in terms of AST nodes bound in the match Result, into Transformations, which are in terms of the source code text.
Definition: RewriteRule.cpp:33
DiagnosticsEngine & getDiagnostics() const
void run(const ast_matchers::MatchFinder::MatchResult &Result) override
Not called directly by users – called by the framework, via base class pointer.
Definition: Transformer.cpp:28
void print(raw_ostream &OS, const SourceManager &SM) const
std::vector< std::pair< std::string, IncludeFormat > > AddedIncludes
Definition: RewriteRule.h:114
void registerMatchers(ast_matchers::MatchFinder *MatchFinder)
N.B.
Definition: Transformer.cpp:23
const RewriteRule::Case & findSelectedCase(const ast_matchers::MatchFinder::MatchResult &Result, const RewriteRule &Rule)
Returns the Case of Rule that was selected in the match result.
Contains all information for a given match.
std::vector< ast_matchers::internal::DynTypedMatcher > buildMatchers(const RewriteRule &Rule)
Builds a set of matchers that cover the rule (one for each distinct node matcher base kind: Stmt...
SmallVector< ASTEdit, 1 > Edits
Definition: RewriteRule.h:109
clang::ASTContext *const Context
Utilities for interpreting the matched AST structures.
Dataflow Directional Tag Classes.
SourceLocation getRuleMatchLoc(const ast_matchers::MatchFinder::MatchResult &Result)
Gets the beginning location of the source matched by a rewrite rule.
Defines the clang::SourceLocation class and associated facilities.
An atomic change is used to create and group a set of source edits, e.g.
Definition: AtomicChange.h:36
clang::SourceManager *const SourceManager
bool addDynamicMatcher(const internal::DynTypedMatcher &NodeMatch, MatchCallback *Action)
Adds a matcher to execute when running over the AST.