Skip to content

Commit a993303

Browse files
committed
Fix loc comparision and get end-of-token
1 parent 20093c6 commit a993303

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

clang/lib/Tooling/Transformer/RangeSelector.cpp

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -187,43 +187,50 @@ RangeSelector transformer::merge(RangeSelector First, RangeSelector Second) {
187187
Expected<CharSourceRange> SecondRange = Second(Result);
188188
if (!SecondRange)
189189
return SecondRange.takeError();
190+
191+
SourceLocation FirstB = FirstRange->getBegin();
192+
SourceLocation FirstE = FirstRange->getEnd();
193+
SourceLocation SecondB = SecondRange->getBegin();
194+
SourceLocation SecondE = SecondRange->getEnd();
190195
// Result begin loc is the minimum of the begin locs of the two ranges.
191-
SourceLocation B = FirstRange->getBegin() < SecondRange->getBegin()
192-
? FirstRange->getBegin()
193-
: SecondRange->getBegin();
196+
SourceLocation B =
197+
Result.SourceManager->isBeforeInTranslationUnit(FirstB, SecondB)
198+
? FirstB
199+
: SecondB;
194200
if (FirstRange->isTokenRange() && SecondRange->isTokenRange()) {
195201
// Both ranges are token ranges. Just take the maximum of their end locs.
196-
SourceLocation E = FirstRange->getEnd() > SecondRange->getEnd()
197-
? FirstRange->getEnd()
198-
: SecondRange->getEnd();
202+
SourceLocation E =
203+
Result.SourceManager->isBeforeInTranslationUnit(FirstE, SecondE)
204+
? SecondE
205+
: FirstE;
199206
return CharSourceRange::getTokenRange(B, E);
200207
}
201-
SourceLocation FirstE = FirstRange->getEnd();
208+
202209
if (FirstRange->isTokenRange()) {
203210
// The end of the first range is a token. Need to resolve the token to a
204211
// char range.
205-
CharSourceRange EndRange = Lexer::makeFileCharRange(
206-
CharSourceRange::getTokenRange(FirstRange->getEnd()),
207-
*Result.SourceManager, Result.Context->getLangOpts());
208-
if (EndRange.isInvalid())
212+
FirstE = Lexer::getLocForEndOfToken(FirstE, /*Offset=*/0,
213+
*Result.SourceManager,
214+
Result.Context->getLangOpts());
215+
if (FirstE.isInvalid())
209216
return invalidArgumentError(
210217
"merge: can't resolve first token range to valid source range");
211-
FirstE = EndRange.getEnd();
212218
}
213-
SourceLocation SecondE = SecondRange->getEnd();
214219
if (SecondRange->isTokenRange()) {
215220
// The end of the second range is a token. Need to resolve the token to a
216221
// char range.
217-
CharSourceRange EndRange = Lexer::makeFileCharRange(
218-
CharSourceRange::getTokenRange(SecondRange->getEnd()),
219-
*Result.SourceManager, Result.Context->getLangOpts());
220-
if (EndRange.isInvalid())
222+
SecondE = Lexer::getLocForEndOfToken(SecondE, /*Offset=*/0,
223+
*Result.SourceManager,
224+
Result.Context->getLangOpts());
225+
if (SecondE.isInvalid())
221226
return invalidArgumentError(
222227
"merge: can't resolve second token range to valid source range");
223-
SecondE = EndRange.getEnd();
224228
}
225229
// Result end loc is the maximum of the end locs of the two ranges.
226-
SourceLocation E = FirstE > SecondE ? FirstE : SecondE;
230+
SourceLocation E =
231+
Result.SourceManager->isBeforeInTranslationUnit(FirstE, SecondE)
232+
? SecondE
233+
: FirstE;
227234
return CharSourceRange::getCharRange(B, E);
228235
};
229236
}

0 commit comments

Comments
 (0)