@@ -105,6 +105,26 @@ static CharSourceRange toCharSourceRange(SourceManager &SM, SourceLoc Start,
105
105
return CharSourceRange (SM, Start, End);
106
106
}
107
107
108
+ // / \brief Extract a character at \p Loc. If \p Loc is the end of the buffer,
109
+ // / return '\f'.
110
+ static char extractCharAfter (SourceManager &SM, SourceLoc Loc) {
111
+ auto chars = SM.extractText ({Loc, 1 });
112
+ return chars.empty () ? ' \f ' : chars[0 ];
113
+ }
114
+
115
+ // / \brief Extract a character immediately before \p Loc. If \p Loc is the
116
+ // / start of the buffer, return '\f'.
117
+ static char extractCharBefore (SourceManager &SM, SourceLoc Loc) {
118
+ // We have to be careful not to go off the front of the buffer.
119
+ auto bufferID = SM.findBufferContainingLoc (Loc);
120
+ auto bufferRange = SM.getRangeForBuffer (bufferID);
121
+ if (bufferRange.getStart () == Loc)
122
+ return ' \f ' ;
123
+ auto chars = SM.extractText ({Loc.getAdvancedLoc (-1 ), 1 }, bufferID);
124
+ assert (!chars.empty () && " Couldn't extractText with valid range" );
125
+ return chars[0 ];
126
+ }
127
+
108
128
InFlightDiagnostic &InFlightDiagnostic::highlight (SourceRange R) {
109
129
assert (IsActive && " Cannot modify an inactive diagnostic" );
110
130
if (Engine && R.isValid ())
@@ -147,23 +167,10 @@ InFlightDiagnostic &InFlightDiagnostic::fixItRemove(SourceRange R) {
147
167
// space around its hole. Specifically, check to see there is whitespace
148
168
// before and after the end of range. If so, nuke the space afterward to keep
149
169
// things consistent.
150
- if (SM.extractText ({charRange.getEnd (), 1 }) == " " ) {
151
- // Check before the string, we have to be careful not to go off the front of
152
- // the buffer.
153
- auto bufferRange =
154
- SM.getRangeForBuffer (SM.findBufferContainingLoc (charRange.getStart ()));
155
- bool ShouldRemove = false ;
156
- if (bufferRange.getStart () == charRange.getStart ())
157
- ShouldRemove = true ;
158
- else {
159
- auto beforeChars =
160
- SM.extractText ({charRange.getStart ().getAdvancedLoc (-1 ), 1 });
161
- ShouldRemove = !beforeChars.empty () && isspace (beforeChars[0 ]);
162
- }
163
- if (ShouldRemove) {
164
- charRange = CharSourceRange (charRange.getStart (),
165
- charRange.getByteLength ()+1 );
166
- }
170
+ if (extractCharAfter (SM, charRange.getEnd ()) == ' ' &&
171
+ isspace (extractCharBefore (SM, charRange.getStart ()))) {
172
+ charRange = CharSourceRange (charRange.getStart (),
173
+ charRange.getByteLength ()+1 );
167
174
}
168
175
Engine->getActiveDiagnostic ().addFixIt (Diagnostic::FixIt (charRange, {}));
169
176
return *this ;
@@ -179,29 +186,17 @@ InFlightDiagnostic &InFlightDiagnostic::fixItReplace(SourceRange R,
179
186
if (R.isInvalid () || !Engine) return *this ;
180
187
181
188
auto &SM = Engine->SourceMgr ;
182
- auto charRange = toCharSourceRange (Engine-> SourceMgr , R);
189
+ auto charRange = toCharSourceRange (SM , R);
183
190
184
191
// If we're replacing with something that wants spaces around it, do a bit of
185
192
// extra work so that we don't suggest extra spaces.
186
193
if (Str.back () == ' ' ) {
187
- auto afterChars = SM.extractText ({charRange.getEnd (), 1 });
188
- if (!afterChars.empty () && isspace (afterChars[0 ])) {
194
+ if (isspace (extractCharAfter (SM, charRange.getEnd ())))
189
195
Str = Str.drop_back ();
190
- }
191
196
}
192
197
if (!Str.empty () && Str.front () == ' ' ) {
193
- bool ShouldRemove = false ;
194
- auto buffer = SM.findBufferContainingLoc (charRange.getStart ());
195
- if (SM.getLocForBufferStart (buffer) == charRange.getStart ()) {
196
- ShouldRemove = true ;
197
- } else {
198
- auto beforeChars =
199
- SM.extractText ({charRange.getStart ().getAdvancedLoc (-1 ), 1 });
200
- ShouldRemove = !beforeChars.empty () && isspace (beforeChars[0 ]);
201
- }
202
- if (ShouldRemove) {
198
+ if (isspace (extractCharBefore (SM, charRange.getStart ())))
203
199
Str = Str.drop_front ();
204
- }
205
200
}
206
201
207
202
Engine->getActiveDiagnostic ().addFixIt (Diagnostic::FixIt (charRange, Str));
0 commit comments