mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-27 22:02:33 +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,21 +5,12 @@
|
||||
#include "migrate_cpp/cpp_refactoring/var_decl.h"
|
||||
|
||||
#include "clang/ASTMatchers/ASTMatchers.h"
|
||||
#include "clang/Lex/Lexer.h"
|
||||
|
||||
namespace cam = ::clang::ast_matchers;
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
VarDecl::VarDecl(std::map<std::string, Replacements>& in_replacements,
|
||||
cam::MatchFinder* finder)
|
||||
: Matcher(in_replacements) {
|
||||
// Skip range-based for loops in this var-inserter.
|
||||
finder->addMatcher(cam::varDecl(cam::unless(cam::hasParent(cam::declStmt(
|
||||
cam::hasParent(cam::cxxForRangeStmt())))))
|
||||
.bind(Label),
|
||||
this);
|
||||
}
|
||||
static constexpr char Label[] = "VarDecl";
|
||||
|
||||
// Helper function for printing TypeLocClass. Useful for debugging.
|
||||
LLVM_ATTRIBUTE_UNUSED
|
||||
@@ -38,12 +29,10 @@ static auto TypeLocClassToString(clang::TypeLoc::TypeLocClass c)
|
||||
}
|
||||
|
||||
// Returns a string for the type.
|
||||
static auto GetTypeStr(const clang::VarDecl* decl,
|
||||
const clang::SourceManager& sm,
|
||||
const clang::LangOptions& lang_opts) -> std::string {
|
||||
auto VarDecl::GetTypeStr(const clang::VarDecl& decl) -> std::string {
|
||||
// Built a vector of class information, because we'll be traversing reverse
|
||||
// order to construct the final type.
|
||||
auto type_loc = decl->getTypeSourceInfo()->getTypeLoc();
|
||||
auto type_loc = decl.getTypeSourceInfo()->getTypeLoc();
|
||||
std::vector<std::pair<clang::TypeLoc::TypeLocClass, std::string>> segments;
|
||||
while (!type_loc.isNull()) {
|
||||
std::string text;
|
||||
@@ -54,8 +43,7 @@ static auto GetTypeStr(const clang::VarDecl* decl,
|
||||
}
|
||||
auto range =
|
||||
clang::CharSourceRange::getTokenRange(type_loc.getLocalSourceRange());
|
||||
std::string range_str =
|
||||
clang::Lexer::getSourceText(range, sm, lang_opts).str();
|
||||
std::string range_str = GetSourceText(range).str();
|
||||
|
||||
// Make a list of segments with their TypeLocClass for reconstruction of the
|
||||
// string. Locally, we will have a qualifier (such as `const`) and a type
|
||||
@@ -101,46 +89,44 @@ static auto GetTypeStr(const clang::VarDecl* decl,
|
||||
return type_str;
|
||||
}
|
||||
|
||||
void VarDecl::run(const cam::MatchFinder::MatchResult& result) {
|
||||
const auto* decl = result.Nodes.getNodeAs<clang::VarDecl>(Label);
|
||||
if (!decl) {
|
||||
llvm::report_fatal_error(std::string("getNodeAs failed for ") + Label);
|
||||
}
|
||||
|
||||
if (decl->getTypeSourceInfo() == nullptr) {
|
||||
void VarDecl::Run() {
|
||||
const auto& decl = GetNodeAsOrDie<clang::VarDecl>(Label);
|
||||
if (decl.getTypeSourceInfo() == nullptr) {
|
||||
// TODO: Need to understand what's happening in this case. Not sure if we
|
||||
// need to address it.
|
||||
return;
|
||||
}
|
||||
|
||||
auto& sm = *(result.SourceManager);
|
||||
auto lang_opts = result.Context->getLangOpts();
|
||||
|
||||
std::string after;
|
||||
if (decl->getType().isConstQualified()) {
|
||||
if (decl.getType().isConstQualified()) {
|
||||
after = "let ";
|
||||
} else if (result.Nodes.getNodeAs<clang::ParmVarDecl>(Label) == nullptr) {
|
||||
} else if (!llvm::isa<clang::ParmVarDecl>(&decl)) {
|
||||
// Start the replacement with "var" unless it's a parameter.
|
||||
after = "var ";
|
||||
}
|
||||
// Add "identifier: type" to the replacement.
|
||||
after += decl->getNameAsString() + ": " + GetTypeStr(decl, sm, lang_opts);
|
||||
after += decl.getNameAsString() + ": " + GetTypeStr(decl);
|
||||
|
||||
// This decides the range to replace. Normally the entire decl is replaced,
|
||||
// but for code like `int i, j` we need to detect the comma between the
|
||||
// declared names. That case currently results in `var i: int, var j: int`.
|
||||
// If there's a comma, this range will be non-empty.
|
||||
auto type_loc = decl->getTypeSourceInfo()->getTypeLoc();
|
||||
auto after_type_loc =
|
||||
clang::Lexer::getLocForEndOfToken(type_loc.getEndLoc(), 0, sm, lang_opts);
|
||||
auto comma_source_text = clang::Lexer::getSourceText(
|
||||
clang::CharSourceRange::getCharRange(after_type_loc, decl->getLocation()),
|
||||
sm, lang_opts);
|
||||
auto type_loc = decl.getTypeSourceInfo()->getTypeLoc();
|
||||
clang::SourceLocation after_type_loc = clang::Lexer::getLocForEndOfToken(
|
||||
type_loc.getEndLoc(), 0, GetSourceManager(), GetLangOpts());
|
||||
llvm::StringRef comma_source_text = GetSourceText(
|
||||
clang::CharSourceRange::getCharRange(after_type_loc, decl.getLocation()));
|
||||
bool has_comma = !comma_source_text.trim().empty();
|
||||
clang::CharSourceRange replace_range = clang::CharSourceRange::getTokenRange(
|
||||
has_comma ? decl->getLocation() : decl->getBeginLoc(), decl->getEndLoc());
|
||||
has_comma ? decl.getLocation() : decl.getBeginLoc(), decl.getEndLoc());
|
||||
|
||||
AddReplacement(sm, replace_range, after);
|
||||
AddReplacement(replace_range, after);
|
||||
}
|
||||
|
||||
auto VarDeclFactory::GetAstMatcher() -> cam::DeclarationMatcher {
|
||||
return cam::varDecl(cam::unless(cam::hasParent(cam::declStmt(
|
||||
cam::hasParent(cam::cxxForRangeStmt())))))
|
||||
.bind(Label);
|
||||
}
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
Reference in New Issue
Block a user