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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
// 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 data members to calling getters or setters.
//
// For example, if `foo` and `bar` are of a type whose `member` is supposed to
// be rewritten, the rewriter replaces `foo.member = bar.member` with
// `foo.set_member(bar.member())`.
//
// In particular, this rewriter is used to convert the structs
// `autofill::FormData` and `autofill::FormFieldData` to classes.
#include <cassert>
#include <iostream>
#include <memory>
#include <regex>
#include <string>
#include "clang/AST/ASTContext.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/FormatVariadic.h"
#include "llvm/Support/TargetSelect.h"
// Prints a clang::SourceLocation or clang::SourceRange.
#define LOG(e) \
llvm::errs() << __FILE__ << ":" << __LINE__ << ": " << #e << " " \
<< (e).printToString(*result.SourceManager) << '\n';
namespace {
llvm::cl::extrahelp common_help(
clang::tooling::CommonOptionsParser::HelpMessage);
llvm::cl::extrahelp more_help(
"The rewriter turns references to ClassOfInterest::field_of_interest into\n"
"getter and setter calls.");
llvm::cl::OptionCategory rewriter_category("Rewriter Options");
llvm::cl::opt<std::string> classes_of_interest_option(
"classes-of-interest",
llvm::cl::desc("Comma-separated fully qualified names of the classes whose "
"field are to be rewritten"),
llvm::cl::init("::autofill::FormFieldData"),
llvm::cl::cat(rewriter_category));
llvm::cl::opt<std::string> fields_of_interest_option(
"fields-of-interest",
llvm::cl::desc("Comma-separated names of data members of any class of "
"interest that are to be rewritten"),
llvm::cl::init("value"),
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:
OutputHelper() = default;
~OutputHelper() = default;
OutputHelper(const OutputHelper&) = delete;
OutputHelper& operator=(const OutputHelper&) = delete;
// Deletes `replacement_range`.
void Delete(const clang::CharSourceRange& replacement_range,
const clang::SourceManager& source_manager,
const clang::LangOptions& lang_opts) {
Replace(replacement_range, "", source_manager, lang_opts);
}
// 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);
}
// Inserts `lhs` and `rhs` to the left and right of `replacement_range`.
void Wrap(const clang::CharSourceRange& replacement_range,
std::string_view lhs,
std::string_view rhs,
const clang::SourceManager& source_manager,
const clang::LangOptions& lang_opts) {
clang::tooling::Replacement replacement(source_manager, replacement_range,
"", lang_opts);
llvm::StringRef file_path = replacement.getFilePath();
if (file_path.empty()) {
return;
}
Add(file_path, replacement.getOffset(), 0, lhs);
Add(file_path, replacement.getOffset() + replacement.getLength(), 0, rhs);
}
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");
current_language_ = input_file.getKind().getLanguage();
if (ShouldOutput()) {
llvm::outs() << "==== BEGIN EDITS ====\n";
}
return true; // Report that |handleBeginSource| succeeded.
}
void handleEndSource() override {
if (ShouldOutput()) {
llvm::outs() << "==== END EDITS ====\n";
}
}
void Add(llvm::StringRef file_path,
unsigned offset,
unsigned length,
std::string_view replacement_text) {
if (ShouldOutput()) {
llvm::outs() << "r:::" << file_path << ":::" << offset << ":::" << length
<< ":::" << replacement_text << "\n";
}
}
bool ShouldOutput() {
switch (current_language_) {
case clang::Language::CXX:
case clang::Language::OpenCLCXX:
case clang::Language::ObjCXX:
return true;
case clang::Language::Unknown:
case clang::Language::Asm:
case clang::Language::LLVM_IR:
case clang::Language::CIR:
case clang::Language::OpenCL:
case clang::Language::CUDA:
case clang::Language::RenderScript:
case clang::Language::HIP:
case clang::Language::HLSL:
case clang::Language::C:
case clang::Language::ObjC:
return false;
}
assert(false && "Unrecognized clang::Language");
return false;
}
clang::Language current_language_ = clang::Language::Unknown;
};
namespace matchers {
auto IsClassOfInterest() {
using namespace clang::ast_matchers;
static auto names = [] {
llvm::SmallVector<llvm::StringRef> names;
llvm::StringRef(classes_of_interest_option).split(names, ",");
return names;
}();
return cxxRecordDecl(hasAnyName(names));
}
auto IsFieldOfInterest() {
using namespace clang::ast_matchers;
static auto names = [] {
llvm::SmallVector<llvm::StringRef> names;
llvm::StringRef(fields_of_interest_option).split(names, ",");
return names;
}();
return fieldDecl(hasAnyName(names));
}
// Matches `object.member` and `object->member` where `member` is a
// member-of-interest of a class-of-interest
auto IsMemberExprOfInterest() {
using namespace clang::ast_matchers;
return memberExpr(
allOf(member(allOf(IsFieldOfInterest(),
fieldDecl(hasParent(IsClassOfInterest())))),
// Avoids matching memberExprs in defaulted
// constructors and operators.
unless(hasAncestor(functionDecl(isDefaulted())))))
.bind("member_expr");
}
// Matches `f.member = foo`.
auto IsWriteExprOfInterest() {
using namespace clang::ast_matchers;
return expr(binaryOperation(
hasOperatorName("="),
hasLHS(ignoringParenImpCasts(IsMemberExprOfInterest())),
hasRHS(expr().bind("rhs"))))
.bind("assignment");
}
// Matches `f.member` and `f->member`, except on the left-hand side of
// assignments.
auto IsReadExprOfInterest() {
using namespace clang::ast_matchers;
return expr(
anyOf(allOf(IsMemberExprOfInterest(),
unless(hasAncestor(IsWriteExprOfInterest()))),
expr(binaryOperation(
hasOperatorName("="),
hasRHS(anyOf(IsMemberExprOfInterest(),
hasDescendant(IsMemberExprOfInterest())))))));
}
} // namespace matchers
class BaseRewriter {
public:
explicit BaseRewriter(OutputHelper* output_helper)
: output_helper_(*output_helper) {}
protected:
OutputHelper& output_helper_;
};
// Replaces `object.member` with `object.member()`.
class FieldReadAccessRewriter
: public BaseRewriter,
public clang::ast_matchers::MatchFinder::MatchCallback {
public:
using BaseRewriter::BaseRewriter;
void AddMatchers(clang::ast_matchers::MatchFinder& match_finder) {
match_finder.addMatcher(matchers::IsReadExprOfInterest(), this);
}
private:
void run(
const clang::ast_matchers::MatchFinder::MatchResult& result) override {
// object.member
// ^-----------^ "member_expr"
const auto* member_expr =
result.Nodes.getNodeAs<clang::MemberExpr>("member_expr");
assert(member_expr);
clang::CharSourceRange range = clang::CharSourceRange::getTokenRange(
clang::SourceRange(member_expr->getMemberLoc()));
auto source_text = clang::Lexer::getSourceText(
range, *result.SourceManager, result.Context->getLangOpts());
std::string replacement_text =
std::string(source_text.begin(), source_text.end()) + "()";
output_helper_.Replace(range, replacement_text, *result.SourceManager,
result.Context->getLangOpts());
}
};
// Replaces `object.member = something` with `object.set_value(something)`
// (modulo whitespace).
//
// More precisely, it inserts `set_` before `member`, removes `=`, and puts
// `something` into parentheses. We do it this way to avoid overlapping
// substitutions because `something` might need substitutions on its own.
class FieldWriteAccessRewriter
: public BaseRewriter,
public clang::ast_matchers::MatchFinder::MatchCallback {
public:
using BaseRewriter::BaseRewriter;
void AddMatchers(clang::ast_matchers::MatchFinder& match_finder) {
match_finder.addMatcher(matchers::IsWriteExprOfInterest(), this);
}
private:
void run(
const clang::ast_matchers::MatchFinder::MatchResult& result) override {
// object.member = something;
// ^-----------^ "member_expr"
// ^-------^ "rhs"
// ^-----------------------^ "assignment"
const auto* assignment = result.Nodes.getNodeAs<clang::Expr>("assignment");
const auto* member_expr =
result.Nodes.getNodeAs<clang::MemberExpr>("member_expr");
const auto* rhs = result.Nodes.getNodeAs<clang::Expr>("rhs");
assert(assignment);
assert(member_expr);
assert(rhs);
{
// Replace `member` with `set_member`.
clang::CharSourceRange range = clang::CharSourceRange::getTokenRange(
clang::SourceRange(member_expr->getMemberLoc()));
output_helper_.Wrap(range, "set_", "", *result.SourceManager,
result.Context->getLangOpts());
}
{
// Replace `=` with ``.
clang::CharSourceRange range =
clang::CharSourceRange::getTokenRange(assignment->getExprLoc());
output_helper_.Delete(range, *result.SourceManager,
result.Context->getLangOpts());
}
{
// Replace `something` with `(something)`.
clang::CharSourceRange range =
clang::CharSourceRange::getTokenRange(rhs->getSourceRange());
output_helper_.Wrap(range, "(", ")", *result.SourceManager,
result.Context->getLangOpts());
}
}
};
// Replaces `::testing::Field(..., &ClassOfInterest::field_of_interest, ...)`
// with `::testing::Property(..., &ClassOfInterest::field_of_interest, ...)`.
class TestingFieldRewriter
: public BaseRewriter,
public clang::ast_matchers::MatchFinder::MatchCallback {
public:
using BaseRewriter::BaseRewriter;
void AddMatchers(clang::ast_matchers::MatchFinder& match_finder) {
using namespace clang::ast_matchers;
using namespace matchers;
// Matches `Field(&ClassOfInterest::field_of_interest, ...)`.
auto testing_field =
callExpr(
callee(functionDecl(hasName("::testing::Field"))),
hasAnyArgument(expr(unaryOperator(
hasOperatorName("&"), hasUnaryOperand(declRefExpr(to(allOf(
hasParent(IsClassOfInterest()),
fieldDecl(IsFieldOfInterest())))))))))
.bind("call_expr");
match_finder.addMatcher(testing_field, this);
}
private:
void run(
const clang::ast_matchers::MatchFinder::MatchResult& result) override {
const auto* call_expr =
result.Nodes.getNodeAs<clang::CallExpr>("call_expr");
assert(call_expr);
// Replace `::testing::Field` with `::testing::Property`.
clang::CharSourceRange range = clang::CharSourceRange::getTokenRange(
call_expr->getCallee()->getSourceRange());
auto source_text = clang::Lexer::getSourceText(
range, *result.SourceManager, result.Context->getLangOpts());
std::string replacement_text =
std::string(source_text.begin(), source_text.end());
replacement_text =
std::regex_replace(replacement_text, std::regex("Field"), "Property");
output_helper_.Replace(range, replacement_text, *result.SourceManager,
result.Context->getLangOpts());
}
};
class FieldToFunctionRewriter : public BaseRewriter {
public:
using BaseRewriter::BaseRewriter;
void AddMatchers(clang::ast_matchers::MatchFinder& match_finder) {
frar_.AddMatchers(match_finder);
fwar_.AddMatchers(match_finder);
tfr_.AddMatchers(match_finder);
}
private:
FieldReadAccessRewriter frar_{&output_helper_};
FieldWriteAccessRewriter fwar_{&output_helper_};
TestingFieldRewriter tfr_{&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;
clang::ast_matchers::MatchFinder match_finder;
FieldToFunctionRewriter ftf_rewriter(&output_helper);
ftf_rewriter.AddMatchers(match_finder);
std::unique_ptr<clang::tooling::FrontendActionFactory> factory =
clang::tooling::newFrontendActionFactory(&match_finder, &output_helper);
return tool.run(factory.get());
}
|