mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Replace int lines with file-associated SourceLocations (#779)
Most interesting changes should be ast/source_location.h and syntax/parse_and_lex_context.h
This commit is contained in:
@@ -25,11 +25,15 @@ MATCHER_P(AutoFieldNamed, name, "") {
|
||||
return arg.name == std::string(name) && isa<AutoPattern>(arg.pattern);
|
||||
}
|
||||
|
||||
static auto FakeSourceLoc(int line_num) -> SourceLocation {
|
||||
return SourceLocation("<test>", line_num);
|
||||
}
|
||||
|
||||
TEST(PatternTest, EmptyAsPattern) {
|
||||
ParenContents<Pattern> contents = {.elements = {},
|
||||
.has_trailing_comma = false};
|
||||
const Pattern* pattern = PatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(pattern->LineNumber(), 1);
|
||||
const Pattern* pattern = PatternFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(pattern->SourceLoc(), FakeSourceLoc(1));
|
||||
ASSERT_TRUE(isa<TuplePattern>(pattern));
|
||||
EXPECT_THAT(cast<TuplePattern>(pattern)->Fields(), IsEmpty());
|
||||
}
|
||||
@@ -38,8 +42,8 @@ TEST(PatternTest, EmptyAsTuplePattern) {
|
||||
ParenContents<Pattern> contents = {.elements = {},
|
||||
.has_trailing_comma = false};
|
||||
const TuplePattern* tuple =
|
||||
TuplePatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(tuple->LineNumber(), 1);
|
||||
TuplePatternFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1));
|
||||
EXPECT_THAT(tuple->Fields(), IsEmpty());
|
||||
}
|
||||
|
||||
@@ -52,34 +56,37 @@ TEST(PatternTest, UnaryNoCommaAsPattern) {
|
||||
// ```
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/2)}},
|
||||
.term =
|
||||
global_arena->RawNew<AutoPattern>(FakeSourceLoc(2))}},
|
||||
.has_trailing_comma = false};
|
||||
|
||||
const Pattern* pattern = PatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(pattern->LineNumber(), 2);
|
||||
const Pattern* pattern = PatternFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(pattern->SourceLoc(), FakeSourceLoc(2));
|
||||
ASSERT_TRUE(isa<AutoPattern>(pattern));
|
||||
}
|
||||
|
||||
TEST(PatternTest, UnaryNoCommaAsTuplePattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/2)}},
|
||||
.term =
|
||||
global_arena->RawNew<AutoPattern>(FakeSourceLoc(2))}},
|
||||
.has_trailing_comma = false};
|
||||
|
||||
const TuplePattern* tuple =
|
||||
TuplePatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(tuple->LineNumber(), 1);
|
||||
TuplePatternFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1));
|
||||
EXPECT_THAT(tuple->Fields(), ElementsAre(AutoFieldNamed("0")));
|
||||
}
|
||||
|
||||
TEST(PatternTest, UnaryWithCommaAsPattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/2)}},
|
||||
.term =
|
||||
global_arena->RawNew<AutoPattern>(FakeSourceLoc(2))}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const Pattern* pattern = PatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(pattern->LineNumber(), 1);
|
||||
const Pattern* pattern = PatternFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(pattern->SourceLoc(), FakeSourceLoc(1));
|
||||
ASSERT_TRUE(isa<TuplePattern>(pattern));
|
||||
EXPECT_THAT(cast<TuplePattern>(pattern)->Fields(),
|
||||
ElementsAre(AutoFieldNamed("0")));
|
||||
@@ -88,25 +95,28 @@ TEST(PatternTest, UnaryWithCommaAsPattern) {
|
||||
TEST(PatternTest, UnaryWithCommaAsTuplePattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/2)}},
|
||||
.term =
|
||||
global_arena->RawNew<AutoPattern>(FakeSourceLoc(2))}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const TuplePattern* tuple =
|
||||
TuplePatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(tuple->LineNumber(), 1);
|
||||
TuplePatternFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1));
|
||||
EXPECT_THAT(tuple->Fields(), ElementsAre(AutoFieldNamed("0")));
|
||||
}
|
||||
|
||||
TEST(PatternTest, BinaryAsPattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/2)},
|
||||
.term =
|
||||
global_arena->RawNew<AutoPattern>(FakeSourceLoc(2))},
|
||||
{.name = std::nullopt,
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/3)}},
|
||||
.term =
|
||||
global_arena->RawNew<AutoPattern>(FakeSourceLoc(2))}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const Pattern* pattern = PatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(pattern->LineNumber(), 1);
|
||||
const Pattern* pattern = PatternFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(pattern->SourceLoc(), FakeSourceLoc(1));
|
||||
ASSERT_TRUE(isa<TuplePattern>(pattern));
|
||||
EXPECT_THAT(cast<TuplePattern>(pattern)->Fields(),
|
||||
ElementsAre(AutoFieldNamed("0"), AutoFieldNamed("1")));
|
||||
@@ -115,14 +125,16 @@ TEST(PatternTest, BinaryAsPattern) {
|
||||
TEST(PatternTest, BinaryAsTuplePattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/2)},
|
||||
.term =
|
||||
global_arena->RawNew<AutoPattern>(FakeSourceLoc(2))},
|
||||
{.name = std::nullopt,
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/3)}},
|
||||
.term =
|
||||
global_arena->RawNew<AutoPattern>(FakeSourceLoc(2))}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const TuplePattern* tuple =
|
||||
TuplePatternFromParenContents(/*line_num=*/1, contents);
|
||||
EXPECT_EQ(tuple->LineNumber(), 1);
|
||||
TuplePatternFromParenContents(FakeSourceLoc(1), contents);
|
||||
EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1));
|
||||
EXPECT_THAT(tuple->Fields(),
|
||||
ElementsAre(AutoFieldNamed("0"), AutoFieldNamed("1")));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user