Skip to content

Commit 55095ef

Browse files
authored
Merge pull request #4792 from jrose-apple/AssignExpr-SourceRange
Fix the SourceRange of an AssignExpr with an implicit source.
2 parents d1b02d0 + 4ec661d commit 55095ef

File tree

6 files changed

+222
-73
lines changed

6 files changed

+222
-73
lines changed

include/swift/AST/Expr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4270,7 +4270,7 @@ class AssignExpr : public Expr {
42704270
}
42714271
SourceLoc getEndLoc() const {
42724272
if (!isFolded()) return EqualLoc;
4273-
return Src->getEndLoc();
4273+
return (Src->isImplicit() ? Dest->getEndLoc() : Src->getEndLoc());
42744274
}
42754275

42764276
/// True if the node has been processed by binary expression folding.

unittests/AST/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
add_swift_unittest(SwiftASTTests
22
OverrideTests.cpp
3+
SourceLocTests.cpp
4+
TestContext.cpp
35
VersionRangeLattice.cpp
46
)
57

unittests/AST/OverrideTests.cpp

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "TestContext.h"
1314
#include "swift/AST/ASTContext.h"
1415
#include "swift/AST/DiagnosticEngine.h"
1516
#include "swift/AST/Module.h"
@@ -24,78 +25,7 @@
2425
#include "gtest/gtest.h"
2526

2627
using namespace swift;
27-
28-
namespace {
29-
/// Helper class used to set the LangOpts target before initializing the
30-
/// ASTContext.
31-
///
32-
/// \see TestContext
33-
class TestContextBase {
34-
public:
35-
LangOptions LangOpts;
36-
SearchPathOptions SearchPathOpts;
37-
SourceManager SourceMgr;
38-
DiagnosticEngine Diags;
39-
40-
TestContextBase() : Diags(SourceMgr) {
41-
LangOpts.Target = llvm::Triple(llvm::sys::getProcessTriple());
42-
}
43-
};
44-
45-
enum ShouldDeclareOptionalTypes : bool {
46-
DoNotDeclareOptionalTypes,
47-
DeclareOptionalTypes
48-
};
49-
50-
/// Owns an ASTContext and the associated types.
51-
class TestContext : public TestContextBase {
52-
SourceFile *FileForLookups;
53-
54-
void declareOptionalType(Identifier name) {
55-
auto wrapped = new (Ctx) GenericTypeParamDecl(FileForLookups,
56-
Ctx.getIdentifier("Wrapped"),
57-
SourceLoc(), /*depth*/0,
58-
/*index*/0);
59-
auto params = GenericParamList::create(Ctx, SourceLoc(), wrapped,
60-
SourceLoc());
61-
auto decl = new (Ctx) EnumDecl(SourceLoc(), name, SourceLoc(),
62-
/*inherited*/{}, params, FileForLookups);
63-
wrapped->setDeclContext(decl);
64-
FileForLookups->Decls.push_back(decl);
65-
}
66-
67-
public:
68-
ASTContext Ctx;
69-
70-
TestContext(ShouldDeclareOptionalTypes optionals = DoNotDeclareOptionalTypes)
71-
: Ctx(LangOpts, SearchPathOpts, SourceMgr, Diags) {
72-
auto stdlibID = Ctx.getIdentifier(STDLIB_NAME);
73-
auto *module = ModuleDecl::create(stdlibID, Ctx);
74-
Ctx.LoadedModules[stdlibID] = module;
75-
76-
using ImplicitModuleImportKind = SourceFile::ImplicitModuleImportKind;
77-
FileForLookups = new (Ctx) SourceFile(*module, SourceFileKind::Library,
78-
/*buffer*/None,
79-
ImplicitModuleImportKind::None);
80-
module->addFile(*FileForLookups);
81-
82-
if (optionals == DeclareOptionalTypes) {
83-
declareOptionalType(Ctx.getIdentifier("Optional"));
84-
declareOptionalType(Ctx.getIdentifier("ImplicitlyUnwrappedOptional"));
85-
}
86-
}
87-
88-
template <typename Nominal>
89-
Nominal *makeNominal(StringRef name,
90-
GenericParamList *genericParams = nullptr) {
91-
auto result = new (Ctx) Nominal(SourceLoc(), Ctx.getIdentifier(name),
92-
SourceLoc(), /*inherited*/{},
93-
genericParams, FileForLookups);
94-
result->setAccessibility(Accessibility::Internal);
95-
return result;
96-
}
97-
};
98-
} // end anonymous namespace
28+
using namespace swift::unittest;
9929

10030
TEST(Override, IdenticalTypes) {
10131
TestContext C;

unittests/AST/SourceLocTests.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//===--- SourceLocTests.cpp - Tests for source locations of AST nodes -----===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "TestContext.h"
14+
#include "swift/AST/Expr.h"
15+
#include "gtest/gtest.h"
16+
17+
using namespace swift;
18+
using namespace swift::unittest;
19+
20+
namespace swift {
21+
void PrintTo(SourceLoc loc, std::ostream *os) {
22+
*os << loc.getOpaquePointerValue();
23+
if (loc.isValid())
24+
*os << " '" << *(char *)loc.getOpaquePointerValue() << "'";
25+
}
26+
27+
void PrintTo(SourceRange range, std::ostream *os) {
28+
PrintTo(range.Start, os);
29+
*os << " - ";
30+
PrintTo(range.End, os);
31+
}
32+
} // end namespace swift
33+
34+
TEST(SourceLoc, AssignExpr) {
35+
TestContext C;
36+
37+
// 0123456789012
38+
auto bufferID = C.Ctx.SourceMgr.addMemBufferCopy("aa.bb = cc.dd");
39+
SourceLoc start = C.Ctx.SourceMgr.getLocForBufferStart(bufferID);
40+
41+
auto destBase = new (C.Ctx) UnresolvedDeclRefExpr(
42+
C.Ctx.getIdentifier("aa"),
43+
DeclRefKind::Ordinary,
44+
DeclNameLoc(start));
45+
auto dest = new (C.Ctx) UnresolvedDotExpr(
46+
destBase,
47+
start.getAdvancedLoc(2),
48+
C.Ctx.getIdentifier("bb"),
49+
DeclNameLoc(start.getAdvancedLoc(3)),
50+
/*implicit*/false);
51+
52+
auto sourceBase = new (C.Ctx) UnresolvedDeclRefExpr(
53+
C.Ctx.getIdentifier("cc"),
54+
DeclRefKind::Ordinary,
55+
DeclNameLoc(start.getAdvancedLoc(8)));
56+
auto source = new (C.Ctx) UnresolvedDotExpr(
57+
sourceBase,
58+
start.getAdvancedLoc(10),
59+
C.Ctx.getIdentifier("dd"),
60+
DeclNameLoc(start.getAdvancedLoc(11)),
61+
/*implicit*/false);
62+
63+
auto invalid = new (C.Ctx) UnresolvedDeclRefExpr(
64+
C.Ctx.getIdentifier("invalid"),
65+
DeclRefKind::Ordinary,
66+
DeclNameLoc());
67+
68+
auto complete = new (C.Ctx) AssignExpr(dest, start.getAdvancedLoc(6), source,
69+
/*implicit*/false);
70+
EXPECT_EQ(start, complete->getStartLoc());
71+
EXPECT_EQ(start.getAdvancedLoc(6), complete->getEqualLoc());
72+
EXPECT_EQ(start.getAdvancedLoc(6), complete->getLoc());
73+
EXPECT_EQ(start.getAdvancedLoc(11), complete->getEndLoc());
74+
EXPECT_EQ(SourceRange(start, start.getAdvancedLoc(11)),
75+
complete->getSourceRange());
76+
77+
auto invalidSource = new (C.Ctx) AssignExpr(dest, SourceLoc(), invalid,
78+
/*implicit*/true);
79+
EXPECT_EQ(start, invalidSource->getStartLoc());
80+
EXPECT_EQ(SourceLoc(), invalidSource->getEqualLoc());
81+
EXPECT_EQ(SourceLoc(), invalidSource->getLoc());
82+
EXPECT_EQ(start.getAdvancedLoc(3), invalidSource->getEndLoc());
83+
EXPECT_EQ(SourceRange(start, start.getAdvancedLoc(3)),
84+
invalidSource->getSourceRange());
85+
86+
auto invalidDest = new (C.Ctx) AssignExpr(invalid, SourceLoc(), source,
87+
/*implicit*/true);
88+
EXPECT_EQ(start.getAdvancedLoc(8), invalidDest->getStartLoc());
89+
EXPECT_EQ(SourceLoc(), invalidDest->getEqualLoc());
90+
EXPECT_EQ(SourceLoc(), invalidDest->getLoc());
91+
EXPECT_EQ(start.getAdvancedLoc(11), invalidDest->getEndLoc());
92+
EXPECT_EQ(SourceRange(start.getAdvancedLoc(8), start.getAdvancedLoc(11)),
93+
invalidDest->getSourceRange());
94+
95+
auto invalidAll = new (C.Ctx) AssignExpr(invalid, SourceLoc(), invalid,
96+
/*implicit*/true);
97+
EXPECT_EQ(SourceLoc(), invalidAll->getStartLoc());
98+
EXPECT_EQ(SourceLoc(), invalidAll->getEqualLoc());
99+
EXPECT_EQ(SourceLoc(), invalidAll->getLoc());
100+
EXPECT_EQ(SourceLoc(), invalidAll->getEndLoc());
101+
EXPECT_EQ(SourceRange(), invalidAll->getSourceRange());
102+
}

unittests/AST/TestContext.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===--- TestContext.cpp - Helper for setting up ASTContexts --------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "TestContext.h"
14+
#include "swift/AST/Module.h"
15+
#include "swift/Strings.h"
16+
17+
using namespace swift;
18+
using namespace swift::unittest;
19+
20+
21+
static void declareOptionalType(ASTContext &ctx, SourceFile *fileForLookups,
22+
Identifier name) {
23+
auto wrapped = new (ctx) GenericTypeParamDecl(fileForLookups,
24+
ctx.getIdentifier("Wrapped"),
25+
SourceLoc(), /*depth*/0,
26+
/*index*/0);
27+
auto params = GenericParamList::create(ctx, SourceLoc(), wrapped,
28+
SourceLoc());
29+
auto decl = new (ctx) EnumDecl(SourceLoc(), name, SourceLoc(),
30+
/*inherited*/{}, params, fileForLookups);
31+
wrapped->setDeclContext(decl);
32+
fileForLookups->Decls.push_back(decl);
33+
}
34+
35+
TestContext::TestContext(ShouldDeclareOptionalTypes optionals)
36+
: Ctx(LangOpts, SearchPathOpts, SourceMgr, Diags) {
37+
auto stdlibID = Ctx.getIdentifier(STDLIB_NAME);
38+
auto *module = ModuleDecl::create(stdlibID, Ctx);
39+
Ctx.LoadedModules[stdlibID] = module;
40+
41+
using ImplicitModuleImportKind = SourceFile::ImplicitModuleImportKind;
42+
FileForLookups = new (Ctx) SourceFile(*module, SourceFileKind::Library,
43+
/*buffer*/None,
44+
ImplicitModuleImportKind::None);
45+
module->addFile(*FileForLookups);
46+
47+
if (optionals == DeclareOptionalTypes) {
48+
declareOptionalType(Ctx, FileForLookups, Ctx.getIdentifier("Optional"));
49+
declareOptionalType(Ctx, FileForLookups,
50+
Ctx.getIdentifier("ImplicitlyUnwrappedOptional"));
51+
}
52+
}

unittests/AST/TestContext.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===--- TestContext.h - Helper for setting up ASTContexts ------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/AST/ASTContext.h"
14+
#include "swift/AST/DiagnosticEngine.h"
15+
#include "swift/Basic/LangOptions.h"
16+
#include "swift/Basic/SourceManager.h"
17+
18+
namespace swift {
19+
namespace unittest {
20+
21+
/// Helper class used to set the LangOpts target before initializing the
22+
/// ASTContext.
23+
///
24+
/// \see TestContext
25+
class TestContextBase {
26+
public:
27+
LangOptions LangOpts;
28+
SearchPathOptions SearchPathOpts;
29+
SourceManager SourceMgr;
30+
DiagnosticEngine Diags;
31+
32+
TestContextBase() : Diags(SourceMgr) {
33+
LangOpts.Target = llvm::Triple(llvm::sys::getProcessTriple());
34+
}
35+
};
36+
37+
enum ShouldDeclareOptionalTypes : bool {
38+
DoNotDeclareOptionalTypes,
39+
DeclareOptionalTypes
40+
};
41+
42+
/// Owns an ASTContext and the associated types.
43+
class TestContext : public TestContextBase {
44+
SourceFile *FileForLookups;
45+
46+
public:
47+
ASTContext Ctx;
48+
49+
TestContext(ShouldDeclareOptionalTypes optionals = DoNotDeclareOptionalTypes);
50+
51+
template <typename Nominal>
52+
Nominal *makeNominal(StringRef name,
53+
GenericParamList *genericParams = nullptr) {
54+
auto result = new (Ctx) Nominal(SourceLoc(), Ctx.getIdentifier(name),
55+
SourceLoc(), /*inherited*/{},
56+
genericParams, FileForLookups);
57+
result->setAccessibility(Accessibility::Internal);
58+
return result;
59+
}
60+
};
61+
62+
} // end namespace unittest
63+
} // end namespace swift

0 commit comments

Comments
 (0)