Index: lib/CodeGen/CoverageMappingGen.cpp =================================================================== --- lib/CodeGen/CoverageMappingGen.cpp +++ lib/CodeGen/CoverageMappingGen.cpp @@ -807,6 +807,14 @@ // counter for the body when looking at the coverage. propagateCounts(ParentCount, S->getCond()); + // The region containing the condition may be spanned by an expansion. + // Make sure we handle a file exit out of this expansion before handling + // the consequent branch. + if (SM.isBeforeInTranslationUnit(getStart(S->getCond()), + S->getCond()->getLocStart())) { + MostRecentLocation = getEnd(S->getCond()); + } + extendRegion(S->getThen()); Counter OutCount = propagateCounts(ThenCount, S->getThen()); Index: test/CoverageMapping/macro-expressions.cpp =================================================================== --- test/CoverageMapping/macro-expressions.cpp +++ test/CoverageMapping/macro-expressions.cpp @@ -12,6 +12,38 @@ #define PRIo64 PRI_64_LENGTH_MODIFIER "o" #define PRIu64 PRI_64_LENGTH_MODIFIER "u" +#define STMT(s) s + +void fn1() { + STMT(if (1)); + STMT(while (1)); + STMT(for (;;)); + if (1) + STMT(if (1)) 0; + if (1) + STMT(while (1)) 0; + if (1) + STMT(for (;;)) 0; + while (1) + STMT(if (1)) 0; + while (1) + STMT(while (1)) 0; + while (1) + STMT(for (;;)) 0; + for (;;) + STMT(if (1)) 0; + for (;;) + STMT(while (1)) 0; + for (;;) + STMT(for (;;)) 0; +} + +void STMT(fn2()) { +} + +void STMT(fn3)() { +} + // CHECK: foo // CHECK-NEXT: File 0, [[@LINE+1]]:17 -> {{[0-9]+}}:2 = #0 void foo(int i) {