39
39
using namespace swift ;
40
40
using namespace ast_scope ;
41
41
42
- #pragma mark source range utilities
43
- static bool rangeableIsIgnored (const Decl *d) { return d->isImplicit (); }
44
- static bool rangeableIsIgnored (const Expr *d) {
45
- return false ; // implicit expr may contain closures
46
- }
47
- static bool rangeableIsIgnored (const Stmt *d) {
48
- return false ; // ??
49
- }
50
- static bool rangeableIsIgnored (const ASTNode n) {
51
- return (n.is <Decl *>() && rangeableIsIgnored (n.get <Decl *>())) ||
52
- (n.is <Stmt *>() && rangeableIsIgnored (n.get <Stmt *>())) ||
53
- (n.is <Expr *>() && rangeableIsIgnored (n.get <Expr *>()));
54
- }
55
-
56
- template <typename Rangeable>
57
- static SourceRange getRangeableSourceRange (const Rangeable *const p) {
58
- return p->getSourceRange ();
59
- }
60
- static SourceRange getRangeableSourceRange (const ASTNode n) {
61
- return n.getSourceRange ();
62
- }
63
-
64
- template <typename Rangeable>
65
- static bool isLocalizable (const Rangeable astElement) {
66
- return !rangeableIsIgnored (astElement) &&
67
- getRangeableSourceRange (astElement).isValid ();
68
- }
69
-
70
- template <typename Rangeable>
71
- static void dumpRangeable (const Rangeable r, llvm::raw_ostream &f) {
72
- r.dump (f);
73
- }
74
- template <typename Rangeable>
75
- static void dumpRangeable (const Rangeable *r, llvm::raw_ostream &f) {
76
- r->dump (f);
77
- }
78
- template <typename Rangeable>
79
- static void dumpRangeable (Rangeable *r, llvm::raw_ostream &f) {
80
- r->dump (f);
81
- }
82
-
83
- static void dumpRangeable (const SpecializeAttr *r,
84
- llvm::raw_ostream &f) LLVM_ATTRIBUTE_USED;
85
- static void dumpRangeable (const SpecializeAttr *r, llvm::raw_ostream &f) {
86
- llvm::errs () << " SpecializeAttr\n " ;
87
- }
88
- static void dumpRangeable (SpecializeAttr *r,
89
- llvm::raw_ostream &f) LLVM_ATTRIBUTE_USED;
90
- static void dumpRangeable (SpecializeAttr *r, llvm::raw_ostream &f) {
91
- llvm::errs () << " SpecializeAttr\n " ;
92
- }
93
-
94
- static void dumpRangeable (const DifferentiableAttr *a,
95
- llvm::raw_ostream &f) LLVM_ATTRIBUTE_USED;
96
- static void dumpRangeable (const DifferentiableAttr *a, llvm::raw_ostream &f) {
97
- llvm::errs () << " DifferentiableAttr\n " ;
98
- }
99
- static void dumpRangeable (DifferentiableAttr *a,
100
- llvm::raw_ostream &f) LLVM_ATTRIBUTE_USED;
101
- static void dumpRangeable (DifferentiableAttr *a, llvm::raw_ostream &f) {
102
- llvm::errs () << " DifferentiableAttr\n " ;
103
- }
104
-
105
- // / For Debugging
106
- template <typename T>
107
- bool doesRangeableRangeMatch (const T *x, const SourceManager &SM,
108
- unsigned start, unsigned end,
109
- StringRef file = " " ) {
110
- auto const r = getRangeableSourceRange (x);
111
- if (r.isInvalid ())
112
- return false ;
113
- if (start && SM.getLineAndColumnInBuffer (r.Start ).first != start)
114
- return false ;
115
- if (end && SM.getLineAndColumnInBuffer (r.End ).first != end)
116
- return false ;
117
- if (file.empty ())
118
- return true ;
119
- const auto buf = SM.findBufferContainingLoc (r.Start );
120
- return SM.getIdentifierForBuffer (buf).endswith (file);
121
- }
122
-
123
- #pragma mark end of rangeable
124
-
125
42
static std::vector<ASTNode> asNodeVector (DeclRange dr) {
126
43
std::vector<ASTNode> nodes;
127
44
llvm::transform (dr, std::back_inserter (nodes),
@@ -832,8 +749,10 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint(
832
749
// so compute it ourselves.
833
750
// Even if this predicate fails, there may be an initContext but
834
751
// we cannot make a scope for it, since no source range.
835
- if (patternEntry.getOriginalInit () &&
836
- isLocalizable (patternEntry.getOriginalInit ())) {
752
+ if (patternEntry.getOriginalInit ()) {
753
+ ASTScopeAssert (
754
+ patternEntry.getOriginalInit ()->getSourceRange ().isValid (),
755
+ " pattern initializer has invalid source range" );
837
756
ASTScopeAssert (
838
757
!getSourceManager ().isBeforeInBuffer (
839
758
patternEntry.getOriginalInit ()->getStartLoc (), decl->getStartLoc ()),
@@ -1053,8 +972,10 @@ void SwitchStmtScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
1053
972
scopeCreator.addToScopeTree (stmt->getSubjectExpr (), this );
1054
973
1055
974
for (auto caseStmt : stmt->getCases ()) {
1056
- if (isLocalizable (caseStmt))
1057
- scopeCreator.constructExpandAndInsert <CaseStmtScope>(this , caseStmt);
975
+ ASTScopeAssert (
976
+ caseStmt->getSourceRange ().isValid (),
977
+ " pattern initializer has invalid source range" );
978
+ scopeCreator.constructExpandAndInsert <CaseStmtScope>(this , caseStmt);
1058
979
}
1059
980
}
1060
981
@@ -1068,8 +989,10 @@ void ForEachStmtScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
1068
989
// the body is implicit and it would overlap the source range of the expr
1069
990
// above.
1070
991
if (!stmt->getBody ()->isImplicit ()) {
1071
- if (isLocalizable (stmt->getBody ()))
1072
- scopeCreator.constructExpandAndInsert <ForEachPatternScope>(this , stmt);
992
+ ASTScopeAssert (
993
+ stmt->getBody ()->getSourceRange ().isValid (),
994
+ " pattern initializer has invalid source range" );
995
+ scopeCreator.constructExpandAndInsert <ForEachPatternScope>(this , stmt);
1073
996
}
1074
997
}
1075
998
0 commit comments