@@ -868,13 +868,22 @@ struct CounterCoverageMappingBuilder
868
868
// / Find a valid gap range between \p AfterLoc and \p BeforeLoc.
869
869
Optional<SourceRange> findGapAreaBetween (SourceLocation AfterLoc,
870
870
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;
873
875
if (!SM.isWrittenInSameFile (AfterLoc, BeforeLoc))
874
876
return None;
875
877
return {{AfterLoc, BeforeLoc}};
876
878
}
877
879
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
+
878
887
// / Emit a gap region between \p StartLoc and \p EndLoc with the given count.
879
888
void fillGapAreaWithCount (SourceLocation StartLoc, SourceLocation EndLoc,
880
889
Counter Count) {
@@ -1039,8 +1048,7 @@ struct CounterCoverageMappingBuilder
1039
1048
adjustForOutOfOrderTraversal (getEnd (S));
1040
1049
1041
1050
// 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 ());
1044
1052
if (Gap)
1045
1053
fillGapAreaWithCount (Gap->getBegin (), Gap->getEnd (), BodyCount);
1046
1054
@@ -1257,8 +1265,7 @@ struct CounterCoverageMappingBuilder
1257
1265
propagateCounts (ParentCount, S->getCond ());
1258
1266
1259
1267
// 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 ());
1262
1269
if (Gap)
1263
1270
fillGapAreaWithCount (Gap->getBegin (), Gap->getEnd (), ThenCount);
1264
1271
@@ -1268,8 +1275,7 @@ struct CounterCoverageMappingBuilder
1268
1275
Counter ElseCount = subtractCounters (ParentCount, ThenCount);
1269
1276
if (const Stmt *Else = S->getElse ()) {
1270
1277
// 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);
1273
1279
if (Gap)
1274
1280
fillGapAreaWithCount (Gap->getBegin (), Gap->getEnd (), ElseCount);
1275
1281
extendRegion (Else);
0 commit comments