Skip to content

Commit 96600fe

Browse files
committed
[Diag] Factor-out some common logic
1 parent ba982bc commit 96600fe

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

lib/AST/DiagnosticEngine.cpp

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,26 @@ static CharSourceRange toCharSourceRange(SourceManager &SM, SourceLoc Start,
105105
return CharSourceRange(SM, Start, End);
106106
}
107107

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+
108128
InFlightDiagnostic &InFlightDiagnostic::highlight(SourceRange R) {
109129
assert(IsActive && "Cannot modify an inactive diagnostic");
110130
if (Engine && R.isValid())
@@ -147,23 +167,10 @@ InFlightDiagnostic &InFlightDiagnostic::fixItRemove(SourceRange R) {
147167
// space around its hole. Specifically, check to see there is whitespace
148168
// before and after the end of range. If so, nuke the space afterward to keep
149169
// 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);
167174
}
168175
Engine->getActiveDiagnostic().addFixIt(Diagnostic::FixIt(charRange, {}));
169176
return *this;
@@ -179,29 +186,17 @@ InFlightDiagnostic &InFlightDiagnostic::fixItReplace(SourceRange R,
179186
if (R.isInvalid() || !Engine) return *this;
180187

181188
auto &SM = Engine->SourceMgr;
182-
auto charRange = toCharSourceRange(Engine->SourceMgr, R);
189+
auto charRange = toCharSourceRange(SM, R);
183190

184191
// If we're replacing with something that wants spaces around it, do a bit of
185192
// extra work so that we don't suggest extra spaces.
186193
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())))
189195
Str = Str.drop_back();
190-
}
191196
}
192197
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())))
203199
Str = Str.drop_front();
204-
}
205200
}
206201

207202
Engine->getActiveDiagnostic().addFixIt(Diagnostic::FixIt(charRange, Str));

0 commit comments

Comments
 (0)