[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang / lib / Tooling / Transformer / Transformer.cpp
blobf95f2ab7d954d88cc2dc804c1960da3a31ac2db4
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 //===----------------------------------------------------------------------===//
9 #include "clang/Tooling/Transformer/Transformer.h"
10 #include "clang/ASTMatchers/ASTMatchFinder.h"
11 #include "clang/ASTMatchers/ASTMatchersInternal.h"
12 #include "clang/Basic/SourceLocation.h"
13 #include "clang/Tooling/Refactoring/AtomicChange.h"
14 #include "llvm/Support/Error.h"
15 #include <map>
16 #include <utility>
17 #include <vector>
19 namespace clang {
20 namespace tooling {
22 using ::clang::ast_matchers::MatchFinder;
24 namespace detail {
26 void TransformerImpl::onMatch(
27 const ast_matchers::MatchFinder::MatchResult &Result) {
28 if (Result.Context->getDiagnostics().hasErrorOccurred())
29 return;
31 onMatchImpl(Result);
34 llvm::Expected<llvm::SmallVector<AtomicChange, 1>>
35 TransformerImpl::convertToAtomicChanges(
36 const llvm::SmallVectorImpl<transformer::Edit> &Edits,
37 const MatchFinder::MatchResult &Result) {
38 // Group the transformations, by file, into AtomicChanges, each anchored by
39 // the location of the first change in that file.
40 std::map<FileID, AtomicChange> ChangesByFileID;
41 for (const auto &T : Edits) {
42 auto ID = Result.SourceManager->getFileID(T.Range.getBegin());
43 auto Iter = ChangesByFileID
44 .emplace(ID, AtomicChange(*Result.SourceManager,
45 T.Range.getBegin(), T.Metadata))
46 .first;
47 auto &AC = Iter->second;
48 switch (T.Kind) {
49 case transformer::EditKind::Range:
50 if (auto Err =
51 AC.replace(*Result.SourceManager, T.Range, T.Replacement)) {
52 return std::move(Err);
54 break;
55 case transformer::EditKind::AddInclude:
56 AC.addHeader(T.Replacement);
57 break;
61 llvm::SmallVector<AtomicChange, 1> Changes;
62 Changes.reserve(ChangesByFileID.size());
63 for (auto &IDChangePair : ChangesByFileID)
64 Changes.push_back(std::move(IDChangePair.second));
66 return Changes;
69 } // namespace detail
71 void Transformer::registerMatchers(MatchFinder *MatchFinder) {
72 for (auto &Matcher : Impl->buildMatchers())
73 MatchFinder->addDynamicMatcher(Matcher, this);
76 void Transformer::run(const MatchFinder::MatchResult &Result) {
77 if (Result.Context->getDiagnostics().hasErrorOccurred())
78 return;
80 Impl->onMatch(Result);
83 } // namespace tooling
84 } // namespace clang