mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 19:11:11 +01:00
Refactor Matcher code to reduce per-Matcher boilerplate (#615)
This is in part to eliminate the "sm" and "lang_opts" variables that received comment, but also cuts back on the use of auto.
This commit is contained in:
@@ -5,52 +5,42 @@
|
||||
#include "migrate_cpp/cpp_refactoring/fn_inserter.h"
|
||||
|
||||
#include "clang/ASTMatchers/ASTMatchers.h"
|
||||
#include "clang/Lex/Lexer.h"
|
||||
|
||||
namespace cam = ::clang::ast_matchers;
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
FnInserter::FnInserter(std::map<std::string, Replacements>& in_replacements,
|
||||
cam::MatchFinder* finder)
|
||||
: Matcher(in_replacements) {
|
||||
finder->addMatcher(
|
||||
cam::functionDecl(cam::anyOf(cam::hasTrailingReturn(),
|
||||
cam::returns(cam::asString("void"))),
|
||||
cam::unless(cam::anyOf(cam::cxxConstructorDecl(),
|
||||
cam::cxxDestructorDecl())))
|
||||
.bind(Label),
|
||||
this);
|
||||
}
|
||||
static constexpr char Label[] = "FnInserter";
|
||||
|
||||
void FnInserter::run(const cam::MatchFinder::MatchResult& result) {
|
||||
const auto* decl = result.Nodes.getNodeAs<clang::FunctionDecl>(Label);
|
||||
if (!decl) {
|
||||
llvm::report_fatal_error(std::string("getNodeAs failed for ") + Label);
|
||||
}
|
||||
|
||||
auto& sm = *(result.SourceManager);
|
||||
auto lang_opts = result.Context->getLangOpts();
|
||||
void FnInserter::Run() {
|
||||
const auto& decl = GetNodeAsOrDie<clang::FunctionDecl>(Label);
|
||||
|
||||
// For names like "Class::Method", replace up to "Class" not "Method".
|
||||
clang::NestedNameSpecifierLoc qual_loc = decl->getQualifierLoc();
|
||||
clang::NestedNameSpecifierLoc qual_loc = decl.getQualifierLoc();
|
||||
clang::SourceLocation name_begin_loc =
|
||||
qual_loc.hasQualifier() ? qual_loc.getBeginLoc() : decl->getLocation();
|
||||
qual_loc.hasQualifier() ? qual_loc.getBeginLoc() : decl.getLocation();
|
||||
auto range =
|
||||
clang::CharSourceRange::getCharRange(decl->getBeginLoc(), name_begin_loc);
|
||||
clang::CharSourceRange::getCharRange(decl.getBeginLoc(), name_begin_loc);
|
||||
|
||||
// In order to handle keywords like "virtual" in "virtual auto Foo() -> ...",
|
||||
// scan the replaced text and only drop auto/void entries.
|
||||
llvm::SmallVector<llvm::StringRef> split;
|
||||
clang::Lexer::getSourceText(range, sm, lang_opts)
|
||||
.split(split, ' ', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
|
||||
GetSourceText(range).split(split, ' ', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
|
||||
std::string new_text = "fn ";
|
||||
for (llvm::StringRef t : split) {
|
||||
if (t != "auto" && t != "void") {
|
||||
new_text += t.str() + " ";
|
||||
}
|
||||
}
|
||||
AddReplacement(*(result.SourceManager), range, new_text);
|
||||
AddReplacement(range, new_text);
|
||||
}
|
||||
|
||||
auto FnInserterFactory::GetAstMatcher() -> cam::DeclarationMatcher {
|
||||
return cam::functionDecl(cam::anyOf(cam::hasTrailingReturn(),
|
||||
cam::returns(cam::asString("void"))),
|
||||
cam::unless(cam::anyOf(cam::cxxConstructorDecl(),
|
||||
cam::cxxDestructorDecl())))
|
||||
.bind(Label);
|
||||
}
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
Reference in New Issue
Block a user