Files
carbon-lang/migrate_cpp/cpp_refactoring/for_range.cpp
T
Jon Meow 6fa42a7f3c For-range in replacement logic (#631)
This replaces GetAstMatcher with AddMatcher because cxxForRangeStmt is a StatementMatcher. addMatcher has multiple definitions (https://clang.llvm.org/doxygen/classclang_1_1ast__matchers_1_1MatchFinder.html) and so this approach allows using the right addMatcher without writing per-call overloads.

To handle the `var`, I'm considering something like moving VarDecl logic into a VarMatcherBase so that I can just use CXXForRangeStmt's getLoopVariable. The problem is a for-range statement has multiple VarDecls, and getLoopVariable may be the easiest way to identify the real one.
2021-07-12 16:20:44 -07:00

30 lines
972 B
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "migrate_cpp/cpp_refactoring/for_range.h"
#include "clang/ASTMatchers/ASTMatchers.h"
namespace cam = ::clang::ast_matchers;
namespace Carbon {
static constexpr char Label[] = "ForRange";
void ForRange::Run() {
const auto& stmt = GetNodeAsOrDie<clang::CXXForRangeStmt>(Label);
// Wrap `in` with spaces so that `for (auto i:items)` has valid results.
AddReplacement(clang::CharSourceRange::getTokenRange(stmt.getColonLoc(),
stmt.getColonLoc()),
" in ");
}
void ForRangeFactory::AddMatcher(cam::MatchFinder* finder,
cam::MatchFinder::MatchCallback* callback) {
finder->addMatcher(cam::cxxForRangeStmt().bind(Label), callback);
}
} // namespace Carbon