File: ForwardCalls.cpp

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (190 lines) | stat: -rw-r--r-- 7,723 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Clang tool to change accesses to forward calls to a member function of a
// class through another member function of that class.
// This can be useful to update callsites when splitting a large class into
// subclasses.
// In particular, this is used to split the `autofill::PersonalDataManager`.

#include <cassert>
#include <memory>
#include <string>

#include "clang/AST/ASTContext.h"
#include "clang/AST/ExprCXX.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchersMacros.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Lex/Lexer.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Core/Replacement.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/TargetSelect.h"

namespace {

llvm::cl::extrahelp common_help(
    clang::tooling::CommonOptionsParser::HelpMessage);
llvm::cl::extrahelp more_help(
    "The rewriter forwards calls to functions-of-interest on objects of type "
    "class-of-interest through forward-through.");
llvm::cl::OptionCategory rewriter_category("Rewriter Options");
llvm::cl::opt<std::string> class_of_interest_option(
    "class-of-interest",
    llvm::cl::desc("Fully qualified names of the class whose "
                   "calls are to be forwarded"),
    llvm::cl::init("Foo"),
    llvm::cl::cat(rewriter_category));
llvm::cl::opt<std::string> functions_of_interest_option(
    "functions-of-interest",
    llvm::cl::desc("Comma-separated function names of the class-of-interest "
                   "that are to be forwarded"),
    llvm::cl::init("bar"),
    llvm::cl::cat(rewriter_category));
llvm::cl::opt<std::string> forward_through_option(
    "forward-through",
    llvm::cl::desc("Name of the function to forward calls through"),
    llvm::cl::init("baz"),
    llvm::cl::cat(rewriter_category));
llvm::cl::opt<std::string> include_header_option(
    "header",
    llvm::cl::desc("Name of the header to include in every touched file"),
    llvm::cl::init("some/file.h"),
    llvm::cl::cat(rewriter_category));

// Generates substitution directives according to the format documented in
// tools/clang/scripts/run_tool.py.
//
// We do not use `clang::tooling::Replacements` because we don't need any
// buffering, and we'd need to implement the serialization of
// `clang::tooling::Replacement` anyway.
class OutputHelper : public clang::tooling::SourceFileCallbacks {
 public:
  // Replaces `replacement_range` with `replacement_text`.
  void Replace(const clang::CharSourceRange& replacement_range,
               std::string replacement_text,
               const clang::SourceManager& source_manager,
               const clang::LangOptions& lang_opts) {
    clang::tooling::Replacement replacement(source_manager, replacement_range,
                                            replacement_text, lang_opts);
    llvm::StringRef file_path = replacement.getFilePath();
    if (file_path.empty()) {
      return;
    }
    std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0');
    Add(file_path, replacement.getOffset(), replacement.getLength(),
        replacement_text);
  }

 private:
  // clang::tooling::SourceFileCallbacks:
  bool handleBeginSource(clang::CompilerInstance& compiler) override {
    const clang::FrontendOptions& frontend_options = compiler.getFrontendOpts();

    assert((frontend_options.Inputs.size() == 1) &&
           "run_tool.py should invoke the rewriter one file at a time");
    const clang::FrontendInputFile& input_file = frontend_options.Inputs[0];
    assert(input_file.isFile() &&
           "run_tool.py should invoke the rewriter on actual files");

    llvm::outs() << "==== BEGIN EDITS ====\n";
    return true;
  }

  void handleEndSource() override { llvm::outs() << "==== END EDITS ====\n"; }

  void Add(llvm::StringRef file_path,
           unsigned offset,
           unsigned length,
           llvm::StringRef replacement_text) {
    llvm::outs() << "r:::" << file_path << ":::" << offset << ":::" << length
                 << ":::" << replacement_text << "\n";
    llvm::outs() << "include-user-header:::" << file_path
                 << ":::-1:::-1:::" << include_header_option << "\n";
  }
};

// Matches `foo.bar()` and `foo->bar()` calls, independently of the parameters
// to bar, where:
// - The type of `foo` is `class_name` or derived from it.
// - `bar` is one of `function_names`.
auto IsCallOfInterest(llvm::StringRef class_name,
                      llvm::SmallVector<llvm::StringRef> function_names) {
  using namespace clang::ast_matchers;
  return cxxMemberCallExpr(
             on(anyOf(hasType(cxxRecordDecl(
                          isSameOrDerivedFrom(hasName(class_name)))),
                      hasType(pointsTo(cxxRecordDecl(
                          isSameOrDerivedFrom(hasName(class_name))))))),
             callee(cxxMethodDecl(hasAnyName(function_names))))
      .bind("call");
}

// Rewrites calls of interest (as per `IsCallOfInterest()`) to go through
// `forward_through_option`. E.g.:
// - `foo.bar()` -> `foo.baz().bar()`
// - `foo->bar()` -> `foo->baz().bar()`
class ForwardCallRewriter
    : public clang::ast_matchers::MatchFinder::MatchCallback {
 public:
  explicit ForwardCallRewriter(OutputHelper* output_helper)
      : output_helper_(*output_helper) {}

  void AddMatchers(clang::ast_matchers::MatchFinder& match_finder) {
    llvm::SmallVector<llvm::StringRef> function_names;
    // `functions_of_interest_option` outlives `ForwardCallRewriter`, so the
    // `llvm::StringRef`s returned by `split()` remain valid.
    llvm::StringRef(functions_of_interest_option).split(function_names, ",");
    match_finder.addMatcher(
        IsCallOfInterest(class_of_interest_option, std::move(function_names)),
        this);
  }

 private:
  void run(
      const clang::ast_matchers::MatchFinder::MatchResult& result) override {
    const auto* call = result.Nodes.getNodeAs<clang::CXXMemberCallExpr>("call");
    assert(call);
    clang::CharSourceRange range = clang::CharSourceRange::getTokenRange(
        clang::SourceRange(call->getCallee()->getExprLoc()));
    auto source_text = clang::Lexer::getSourceText(
        range, *result.SourceManager, result.Context->getLangOpts());
    std::string replacement_text =
        (forward_through_option + "()." + source_text).str();
    output_helper_.Replace(range, replacement_text, *result.SourceManager,
                           result.Context->getLangOpts());
  }

  OutputHelper& output_helper_;
};

}  // namespace

int main(int argc, const char* argv[]) {
  llvm::InitializeNativeTarget();
  llvm::InitializeNativeTargetAsmParser();

  llvm::Expected<clang::tooling::CommonOptionsParser> options =
      clang::tooling::CommonOptionsParser::create(argc, argv,
                                                  rewriter_category);
  assert(static_cast<bool>(options));
  clang::tooling::ClangTool tool(options->getCompilations(),
                                 options->getSourcePathList());

  OutputHelper output_helper;
  ForwardCallRewriter rewriter(&output_helper);
  clang::ast_matchers::MatchFinder match_finder;
  rewriter.AddMatchers(match_finder);

  std::unique_ptr<clang::tooling::FrontendActionFactory> factory =
      clang::tooling::newFrontendActionFactory(&match_finder, &output_helper);
  return tool.run(factory.get());
}