clang 22.0.0git
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 <map>
16#include <utility>
17
18namespace clang {
19namespace tooling {
20
21using ::clang::ast_matchers::MatchFinder;
22
23namespace detail {
24
27 if (Result.Context->getDiagnostics().hasErrorOccurred())
28 return;
29
30 onMatchImpl(Result);
31}
32
37 // Group the transformations, by file, into AtomicChanges, each anchored by
38 // the location of the first change in that file.
39 std::map<FileID, AtomicChange> ChangesByFileID;
40 for (const auto &T : Edits) {
41 auto ID = Result.SourceManager->getFileID(T.Range.getBegin());
42 auto Iter = ChangesByFileID
43 .emplace(ID, AtomicChange(*Result.SourceManager,
44 T.Range.getBegin(), T.Metadata))
45 .first;
46 auto &AC = Iter->second;
47 switch (T.Kind) {
49 if (auto Err =
50 AC.replace(*Result.SourceManager, T.Range, T.Replacement)) {
51 return std::move(Err);
52 }
53 break;
55 AC.addHeader(T.Replacement);
56 break;
57 }
58 }
59
61 Changes.reserve(ChangesByFileID.size());
62 for (auto &IDChangePair : ChangesByFileID)
63 Changes.push_back(std::move(IDChangePair.second));
64
65 return Changes;
66}
67
68} // namespace detail
69
71 for (auto &Matcher : Impl->buildMatchers())
72 MatchFinder->addDynamicMatcher(Matcher, this);
73}
74
76 if (Result.Context->getDiagnostics().hasErrorOccurred())
77 return;
78
79 Impl->onMatch(Result);
80}
81
82} // namespace tooling
83} // namespace clang
static char ID
Definition: Arena.cpp:183
unsigned Iter
Definition: HTMLLogger.cpp:153
Defines the clang::SourceLocation class and associated facilities.
A class to allow finding matches over the Clang AST.
bool addDynamicMatcher(const internal::DynTypedMatcher &NodeMatch, MatchCallback *Action)
Adds a matcher to execute when running over the AST.
An atomic change is used to create and group a set of source edits, e.g.
Definition: AtomicChange.h:37
void registerMatchers(ast_matchers::MatchFinder *MatchFinder)
N.B.
Definition: Transformer.cpp:70
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:75
static llvm::Expected< llvm::SmallVector< AtomicChange, 1 > > convertToAtomicChanges(const llvm::SmallVectorImpl< transformer::Edit > &Edits, const ast_matchers::MatchFinder::MatchResult &Result)
Converts a set of Edit into a AtomicChange per file modified.
Definition: Transformer.cpp:34
void onMatch(const ast_matchers::MatchFinder::MatchResult &Result)
Definition: Transformer.cpp:25
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
const FunctionProtoType * T
Contains all information for a given match.