mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-25 07:20:10 +01:00
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.
53 lines
1.1 KiB
C++
53 lines
1.1 KiB
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 "migrate_cpp/cpp_refactoring/matcher_test_base.h"
|
|
|
|
namespace Carbon {
|
|
namespace {
|
|
|
|
class ForRangeTest : public MatcherTestBase<ForRangeFactory> {};
|
|
|
|
TEST_F(ForRangeTest, Basic) {
|
|
constexpr char Before[] = R"cpp(
|
|
void Foo() {
|
|
int items[] = {1};
|
|
for (int i : items) {
|
|
}
|
|
}
|
|
)cpp";
|
|
constexpr char After[] = R"(
|
|
void Foo() {
|
|
int items[] = {1};
|
|
for (int i in items) {
|
|
}
|
|
}
|
|
)";
|
|
ExpectReplacement(Before, After);
|
|
}
|
|
|
|
TEST_F(ForRangeTest, NoSpace) {
|
|
// Do not mark `cpp` so that clang-format won't "fix" the `:` spacing.
|
|
constexpr char Before[] = R"(
|
|
void Foo() {
|
|
int items[] = {1};
|
|
for (int i:items) {
|
|
}
|
|
}
|
|
)";
|
|
constexpr char After[] = R"(
|
|
void Foo() {
|
|
int items[] = {1};
|
|
for (int i in items) {
|
|
}
|
|
}
|
|
)";
|
|
ExpectReplacement(Before, After);
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace Carbon
|