@@ -295,20 +295,22 @@ class CoverageMappingBuilder {
295
295
}
296
296
297
297
// / Find out where a macro is expanded. If the immediate result is a
298
- // / <scratch space>, keep looking until the result isn't. Return the source
299
- // / range of the found result, or std::nullopt if the while loop didn't get
300
- // / executed, which means the location wasn't changed.
301
- std::optional<SourceRange> getNonScratchExpansion (SourceLocation Loc) {
298
+ // / <scratch space>, keep looking until the result isn't. Return a pair of
299
+ // / \c SourceLocation. The first object is always the begin sloc of found
300
+ // / result. The second should be checked by the caller: if it has value, it's
301
+ // / the end sloc of the found result. Otherwise the while loop didn't get
302
+ // / executed, which means the location wasn't changed and the caller has to
303
+ // / learn the end sloc from somewhere else.
304
+ std::pair<SourceLocation, std::optional<SourceLocation>>
305
+ getNonScratchExpansionLoc (SourceLocation Loc) {
302
306
std::optional<SourceLocation> EndLoc = std::nullopt;
303
307
while (Loc.isMacroID () &&
304
308
SM.isWrittenInScratchSpace (SM.getSpellingLoc (Loc))) {
305
309
auto ExpansionRange = SM.getImmediateExpansionRange (Loc);
306
310
Loc = ExpansionRange.getBegin ();
307
311
EndLoc = ExpansionRange.getEnd ();
308
312
}
309
- if (EndLoc.has_value ())
310
- return SourceRange (Loc, EndLoc.value ());
311
- return std::nullopt;
313
+ return std::make_pair (Loc, EndLoc);
312
314
}
313
315
314
316
// / Find out where the current file is included or macro is expanded. If
@@ -321,9 +323,7 @@ class CoverageMappingBuilder {
321
323
Loc = SM.getImmediateExpansionRange (Loc).getBegin ();
322
324
if (AcceptScratch)
323
325
return Loc;
324
- auto NonScratchExpansion = getNonScratchExpansion (Loc);
325
- return NonScratchExpansion.has_value () ? NonScratchExpansion->getBegin ()
326
- : Loc;
326
+ return getNonScratchExpansionLoc (Loc).first ;
327
327
}
328
328
329
329
// / Return true if \c Loc is a location in a built-in macro.
@@ -371,12 +371,12 @@ class CoverageMappingBuilder {
371
371
SourceLocation Loc = Region.getBeginLoc ();
372
372
373
373
// Replace Region with its definition if it is in <scratch space>.
374
- auto NonScratchExpansion = getNonScratchExpansion (Loc);
375
- if (NonScratchExpansion. has_value ()) {
376
- Loc = NonScratchExpansion-> getBegin () ;
377
- Region.setStartLoc (Loc);
378
- Region.setEndLoc (NonScratchExpansion-> getEnd ());
379
- }
374
+ auto NonScratchExpansionLoc = getNonScratchExpansionLoc (Loc);
375
+ Loc = NonScratchExpansionLoc. first ;
376
+ auto EndLoc = NonScratchExpansionLoc. second ;
377
+ Region.setStartLoc (Loc);
378
+ Region.setEndLoc (EndLoc. has_value () ? EndLoc. value ()
379
+ : Region. getEndLoc ());
380
380
381
381
// Replace Loc with FileLoc if it is expanded with system headers.
382
382
if (!SystemHeadersCoverage && SM.isInSystemMacro (Loc)) {
0 commit comments