26
26
#include < libsolutil/Assertions.h>
27
27
#include < libsolutil/Exceptions.h>
28
28
29
- #include < liblangutil/CharStream.h>
30
-
31
29
#include < limits>
32
30
#include < memory>
33
31
#include < string>
@@ -44,51 +42,44 @@ struct SourceLocation
44
42
{
45
43
bool operator ==(SourceLocation const & _other) const
46
44
{
47
- return source. get () == _other.source . get () && start == _other.start && end == _other. end ;
45
+ return start == _other.start && end == _other.end && equalSources ( _other) ;
48
46
}
49
47
bool operator !=(SourceLocation const & _other) const { return !operator ==(_other); }
50
48
51
- inline bool operator <(SourceLocation const & _other) const
49
+ bool operator <(SourceLocation const & _other) const
52
50
{
53
- if (!source || !_other.source )
54
- return std::make_tuple (int (!!source ), start, end) < std::make_tuple (int (!!_other.source ), _other.start , _other.end );
51
+ if (!sourceName || !_other.sourceName )
52
+ return std::make_tuple (int (!!sourceName ), start, end) < std::make_tuple (int (!!_other.sourceName ), _other.start , _other.end );
55
53
else
56
- return std::make_tuple (source-> name () , start, end) < std::make_tuple (_other.source -> name () , _other.start , _other.end );
54
+ return std::make_tuple (*sourceName , start, end) < std::make_tuple (* _other.sourceName , _other.start , _other.end );
57
55
}
58
56
59
- inline bool contains (SourceLocation const & _other) const
57
+ bool contains (SourceLocation const & _other) const
60
58
{
61
- if (!hasText () || !_other.hasText () || source. get () != _other. source . get ( ))
59
+ if (!hasText () || !_other.hasText () || ! equalSources (_other ))
62
60
return false ;
63
61
return start <= _other.start && _other.end <= end;
64
62
}
65
63
66
- inline bool intersects (SourceLocation const & _other) const
64
+ bool intersects (SourceLocation const & _other) const
67
65
{
68
- if (!hasText () || !_other.hasText () || source. get () != _other. source . get ( ))
66
+ if (!hasText () || !_other.hasText () || ! equalSources (_other ))
69
67
return false ;
70
68
return _other.start < end && start < _other.end ;
71
69
}
72
70
73
- bool isValid () const { return source || start != -1 || end != -1 ; }
74
-
75
- bool hasText () const
71
+ bool equalSources (SourceLocation const & _other) const
76
72
{
77
- return
78
- source &&
79
- 0 <= start &&
80
- start <= end &&
81
- end <= int (source-> source (). length ()) ;
73
+ if (!!sourceName != !!_other. sourceName )
74
+ return false ;
75
+ if (sourceName && *sourceName != *_other. sourceName )
76
+ return false ;
77
+ return true ;
82
78
}
83
79
84
- std::string text () const
85
- {
86
- assertThrow (source, SourceLocationError, " Requested text from null source." );
87
- assertThrow (0 <= start, SourceLocationError, " Invalid source location." );
88
- assertThrow (start <= end, SourceLocationError, " Invalid source location." );
89
- assertThrow (end <= int (source->source ().length ()), SourceLocationError, " Invalid source location." );
90
- return source->source ().substr (size_t (start), size_t (end - start));
91
- }
80
+ bool isValid () const { return sourceName || start != -1 || end != -1 ; }
81
+
82
+ bool hasText () const { return sourceName && 0 <= start && start <= end; }
92
83
93
84
// / @returns the smallest SourceLocation that contains both @param _a and @param _b.
94
85
// / Assumes that @param _a and @param _b refer to the same source (exception: if the source of either one
@@ -97,8 +88,8 @@ struct SourceLocation
97
88
// / @param _b, then start resp. end of the result will be -1 as well).
98
89
static SourceLocation smallestCovering (SourceLocation _a, SourceLocation const & _b)
99
90
{
100
- if (!_a.source )
101
- _a.source = _b.source ;
91
+ if (!_a.sourceName )
92
+ _a.sourceName = _b.sourceName ;
102
93
103
94
if (_a.start < 0 )
104
95
_a.start = _b.start ;
@@ -112,7 +103,7 @@ struct SourceLocation
112
103
113
104
int start = -1 ;
114
105
int end = -1 ;
115
- std::shared_ptr<CharStream> source ;
106
+ std::shared_ptr<std::string const > sourceName ;
116
107
};
117
108
118
109
SourceLocation const parseSourceLocation (
@@ -127,8 +118,8 @@ inline std::ostream& operator<<(std::ostream& _out, SourceLocation const& _locat
127
118
if (!_location.isValid ())
128
119
return _out << " NO_LOCATION_SPECIFIED" ;
129
120
130
- if (_location.source )
131
- _out << _location.source -> name () ;
121
+ if (_location.sourceName )
122
+ _out << * _location.sourceName ;
132
123
133
124
_out << " [" << _location.start << " ," << _location.end << " ]" ;
134
125
0 commit comments