mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Handle var initialization. (#628)
This commit is contained in:
@@ -116,11 +116,19 @@ void VarDecl::Run() {
|
||||
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());
|
||||
clang::SourceLocation replace_start = !comma_source_text.trim().empty()
|
||||
? decl.getLocation()
|
||||
: decl.getBeginLoc();
|
||||
|
||||
AddReplacement(replace_range, after);
|
||||
// Figure out where the replacement ends and initialization begins. For
|
||||
// example, `int i` the end is the identifier, `int i[4]` the end is the `[4]`
|
||||
// type qualifier.
|
||||
clang::SourceLocation identifier_end = clang::Lexer::getLocForEndOfToken(
|
||||
decl.getLocation(), 0, GetSourceManager(), GetLangOpts());
|
||||
clang::SourceLocation replace_end = std::max(identifier_end, after_type_loc);
|
||||
|
||||
AddReplacement(
|
||||
clang::CharSourceRange::getCharRange(replace_start, replace_end), after);
|
||||
}
|
||||
|
||||
auto VarDeclFactory::GetAstMatcher() -> cam::DeclarationMatcher {
|
||||
|
||||
@@ -25,12 +25,11 @@ TEST_F(VarDeclTest, DeclarationArray) {
|
||||
|
||||
TEST_F(VarDeclTest, DeclarationConstArray) {
|
||||
constexpr char Before[] = "const int i[] = {0, 1};";
|
||||
constexpr char After[] = "let i: const int[];";
|
||||
constexpr char After[] = "let i: const int[] = {0, 1};";
|
||||
ExpectReplacement(Before, After);
|
||||
}
|
||||
|
||||
TEST_F(VarDeclTest, DeclarationConstPointer) {
|
||||
// TODO: Include init.
|
||||
// TODO: Fix j replacement location.
|
||||
constexpr char Before[] = R"cpp(
|
||||
int i = 0;
|
||||
@@ -38,9 +37,9 @@ TEST_F(VarDeclTest, DeclarationConstPointer) {
|
||||
const int* k = &i;
|
||||
)cpp";
|
||||
constexpr char After[] = R"(
|
||||
var i: int;
|
||||
int* const let j: int* const;
|
||||
var k: const int*;
|
||||
var i: int = 0;
|
||||
int* const let j: int* const = &i;
|
||||
var k: const int* = &i;
|
||||
)";
|
||||
ExpectReplacement(Before, After);
|
||||
}
|
||||
@@ -52,6 +51,13 @@ TEST_F(VarDeclTest, DeclarationComma) {
|
||||
ExpectReplacement(Before, After);
|
||||
}
|
||||
|
||||
TEST_F(VarDeclTest, DeclarationCommaAssignment) {
|
||||
// TODO: Maybe replace the comma with a `;`.
|
||||
constexpr char Before[] = "int i = 0, j = 0;";
|
||||
constexpr char After[] = "var i: int = 0, var j: int = 0;";
|
||||
ExpectReplacement(Before, After);
|
||||
}
|
||||
|
||||
TEST_F(VarDeclTest, DeclarationCommaArray) {
|
||||
// TODO: Maybe replace the comma with a `;`.
|
||||
// TODO: Need to handle j's array.
|
||||
@@ -60,6 +66,14 @@ TEST_F(VarDeclTest, DeclarationCommaArray) {
|
||||
ExpectReplacement(Before, After);
|
||||
}
|
||||
|
||||
TEST_F(VarDeclTest, DeclarationCommaArrayAssignment) {
|
||||
// TODO: Maybe replace the comma with a `;`.
|
||||
// TODO: Need to handle j's array.
|
||||
constexpr char Before[] = "int i[] = {0}, j[] = {1};";
|
||||
constexpr char After[] = "var i: int[] = {0}, j[] = {1};";
|
||||
ExpectReplacement(Before, After);
|
||||
}
|
||||
|
||||
TEST_F(VarDeclTest, DeclarationCommaPointers) {
|
||||
// TODO: Maybe replace the comma with a `;`.
|
||||
// TODO: Need to handle j's pointer.
|
||||
@@ -69,36 +83,42 @@ TEST_F(VarDeclTest, DeclarationCommaPointers) {
|
||||
ExpectReplacement(Before, After);
|
||||
}
|
||||
|
||||
TEST_F(VarDeclTest, DeclarationCommaPointersAssignment) {
|
||||
// TODO: Maybe replace the comma with a `;`.
|
||||
// TODO: Need to handle j's pointer.
|
||||
// constexpr char After[] = "var i: int *, var j: int *;";
|
||||
constexpr char Before[] = "int *i = nullptr, *j = i;";
|
||||
constexpr char After[] = "var i: int* = nullptr, *j = i;";
|
||||
ExpectReplacement(Before, After);
|
||||
}
|
||||
|
||||
TEST_F(VarDeclTest, Assignment) {
|
||||
constexpr char Before[] = "int i = 0;";
|
||||
// TODO: Include init.
|
||||
constexpr char After[] = "var i: int;";
|
||||
constexpr char After[] = "var i: int = 0;";
|
||||
ExpectReplacement(Before, After);
|
||||
}
|
||||
|
||||
TEST_F(VarDeclTest, Auto) {
|
||||
constexpr char Before[] = "auto i = 0;";
|
||||
constexpr char After[] = "var i: auto;";
|
||||
constexpr char After[] = "var i: auto = 0;";
|
||||
ExpectReplacement(Before, After);
|
||||
}
|
||||
|
||||
TEST_F(VarDeclTest, AutoRef) {
|
||||
// TODO: Include init.
|
||||
constexpr char Before[] = R"cpp(
|
||||
auto i = 0;
|
||||
const auto& j = i;
|
||||
)cpp";
|
||||
constexpr char After[] = R"(
|
||||
var i: auto;
|
||||
var j: const auto&;
|
||||
var i: auto = 0;
|
||||
var j: const auto& = i;
|
||||
)";
|
||||
ExpectReplacement(Before, After);
|
||||
}
|
||||
|
||||
TEST_F(VarDeclTest, Const) {
|
||||
// TODO: Include init.
|
||||
constexpr char Before[] = "const int i = 0;";
|
||||
constexpr char After[] = "let i: const int;";
|
||||
constexpr char After[] = "let i: const int = 0;";
|
||||
ExpectReplacement(Before, After);
|
||||
}
|
||||
|
||||
@@ -132,9 +152,8 @@ TEST_F(VarDeclTest, Params) {
|
||||
}
|
||||
|
||||
TEST_F(VarDeclTest, ParamsDefault) {
|
||||
// TODO: Include init.
|
||||
constexpr char Before[] = "auto Foo(int i = 0) -> int;";
|
||||
constexpr char After[] = "auto Foo(i: int) -> int;";
|
||||
constexpr char After[] = "auto Foo(i: int = 0) -> int;";
|
||||
ExpectReplacement(Before, After);
|
||||
}
|
||||
|
||||
@@ -172,6 +191,26 @@ TEST_F(VarDeclTest, Member) {
|
||||
ExpectReplacement(Before, Before);
|
||||
}
|
||||
|
||||
TEST_F(VarDeclTest, Constructor) {
|
||||
constexpr char Before[] = R"cpp(
|
||||
struct Index {
|
||||
Index(int i) : i(i) {}
|
||||
|
||||
int i;
|
||||
};
|
||||
Index x(0);
|
||||
)cpp";
|
||||
constexpr char After[] = R"(
|
||||
struct Index {
|
||||
Index(i: int) : i(i) {}
|
||||
|
||||
int i;
|
||||
};
|
||||
var x: Index(0);
|
||||
)";
|
||||
ExpectReplacement(Before, After);
|
||||
}
|
||||
|
||||
TEST_F(VarDeclTest, RangeFor) {
|
||||
constexpr char Before[] = R"cpp(
|
||||
void Foo() {
|
||||
@@ -183,7 +222,7 @@ TEST_F(VarDeclTest, RangeFor) {
|
||||
)cpp";
|
||||
constexpr char After[] = R"(
|
||||
void Foo() {
|
||||
var items: int[];
|
||||
var items: int[] = {1};
|
||||
for (int i : items) {
|
||||
var j: int;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user