Skip to content

Commit 14e52c3

Browse files
committed
address comments: return optional<SourceRange>; update region only when needed
1 parent 819103d commit 14e52c3

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

clang/lib/CodeGen/CoverageMappingGen.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -295,22 +295,20 @@ class CoverageMappingBuilder {
295295
}
296296

297297
/// Find out where a macro is expanded. If the immediate result is a
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) {
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) {
306302
std::optional<SourceLocation> EndLoc = std::nullopt;
307303
while (Loc.isMacroID() &&
308304
SM.isWrittenInScratchSpace(SM.getSpellingLoc(Loc))) {
309305
auto ExpansionRange = SM.getImmediateExpansionRange(Loc);
310306
Loc = ExpansionRange.getBegin();
311307
EndLoc = ExpansionRange.getEnd();
312308
}
313-
return std::make_pair(Loc, EndLoc);
309+
if (EndLoc.has_value())
310+
return SourceRange(Loc, EndLoc.value());
311+
return std::nullopt;
314312
}
315313

316314
/// Find out where the current file is included or macro is expanded. If
@@ -323,7 +321,9 @@ class CoverageMappingBuilder {
323321
Loc = SM.getImmediateExpansionRange(Loc).getBegin();
324322
if (AcceptScratch)
325323
return Loc;
326-
return getNonScratchExpansionLoc(Loc).first;
324+
auto NonScratchExpansion = getNonScratchExpansion(Loc);
325+
return NonScratchExpansion.has_value() ? NonScratchExpansion->getBegin()
326+
: Loc;
327327
}
328328

329329
/// Return true if \c Loc is a location in a built-in macro.
@@ -371,12 +371,12 @@ class CoverageMappingBuilder {
371371
SourceLocation Loc = Region.getBeginLoc();
372372

373373
// Replace Region with its definition if it is in <scratch space>.
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());
374+
auto NonScratchExpansion = getNonScratchExpansion(Loc);
375+
if (NonScratchExpansion.has_value()) {
376+
Loc = NonScratchExpansion->getBegin();
377+
Region.setStartLoc(Loc);
378+
Region.setEndLoc(NonScratchExpansion->getEnd());
379+
}
380380

381381
// Replace Loc with FileLoc if it is expanded with system headers.
382382
if (!SystemHeadersCoverage && SM.isInSystemMacro(Loc)) {

0 commit comments

Comments
 (0)