@@ -868,13 +868,22 @@ struct CounterCoverageMappingBuilder
868868 // / Find a valid gap range between \p AfterLoc and \p BeforeLoc.
869869 Optional<SourceRange> findGapAreaBetween (SourceLocation AfterLoc,
870870 SourceLocation BeforeLoc) {
871- AfterLoc = SM .getExpansionLoc (AfterLoc);
872- BeforeLoc = SM .getExpansionLoc (BeforeLoc);
871+ // If the start and end locations of the gap are both within the same macro
872+ // file, the range may not be in source order.
873+ if (AfterLoc.isMacroID () || BeforeLoc.isMacroID ())
874+ return None;
873875 if (!SM .isWrittenInSameFile (AfterLoc, BeforeLoc))
874876 return None;
875877 return {{AfterLoc, BeforeLoc}};
876878 }
877879
880+ // / Find the source range after \p AfterStmt and before \p BeforeStmt.
881+ Optional<SourceRange> findGapAreaBetween (const Stmt *AfterStmt,
882+ const Stmt *BeforeStmt) {
883+ return findGapAreaBetween (getPreciseTokenLocEnd (getEnd (AfterStmt)),
884+ getStart (BeforeStmt));
885+ }
886+
878887 // / Emit a gap region between \p StartLoc and \p EndLoc with the given count.
879888 void fillGapAreaWithCount (SourceLocation StartLoc, SourceLocation EndLoc,
880889 Counter Count) {
@@ -1039,8 +1048,7 @@ struct CounterCoverageMappingBuilder
10391048 adjustForOutOfOrderTraversal (getEnd (S));
10401049
10411050 // The body count applies to the area immediately after the increment.
1042- auto Gap = findGapAreaBetween (getPreciseTokenLocEnd (S->getRParenLoc ()),
1043- getStart (S->getBody ()));
1051+ auto Gap = findGapAreaBetween (S->getCond (), S->getBody ());
10441052 if (Gap)
10451053 fillGapAreaWithCount (Gap->getBegin (), Gap->getEnd (), BodyCount);
10461054
@@ -1257,8 +1265,7 @@ struct CounterCoverageMappingBuilder
12571265 propagateCounts (ParentCount, S->getCond ());
12581266
12591267 // The 'then' count applies to the area immediately after the condition.
1260- auto Gap = findGapAreaBetween (getPreciseTokenLocEnd (S->getRParenLoc ()),
1261- getStart (S->getThen ()));
1268+ auto Gap = findGapAreaBetween (S->getCond (), S->getThen ());
12621269 if (Gap)
12631270 fillGapAreaWithCount (Gap->getBegin (), Gap->getEnd (), ThenCount);
12641271
@@ -1268,8 +1275,7 @@ struct CounterCoverageMappingBuilder
12681275 Counter ElseCount = subtractCounters (ParentCount, ThenCount);
12691276 if (const Stmt *Else = S->getElse ()) {
12701277 // The 'else' count applies to the area immediately after the 'then'.
1271- Gap = findGapAreaBetween (getPreciseTokenLocEnd (getEnd (S->getThen ())),
1272- getStart (Else));
1278+ Gap = findGapAreaBetween (S->getThen (), Else);
12731279 if (Gap)
12741280 fillGapAreaWithCount (Gap->getBegin (), Gap->getEnd (), ElseCount);
12751281 extendRegion (Else);
0 commit comments