Skip to content

Commit 84fffa6

Browse files
committed
[Coverage] Adjust skipped regions only if {Prev,Next}TokLoc is in the same file as regions' {start, end}Loc
Fix a bug if {Prev, Next}TokLoc is in different file from skipped regions' {start, end}Loc Differential Revision: https://reviews.llvm.org/D86116
1 parent 08748d1 commit 84fffa6

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

clang/lib/CodeGen/CoverageMappingGen.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ CoverageMappingModuleGen::setUpCoverageCallbacks(Preprocessor &PP) {
4444
PP.setTokenWatcher([CoverageInfo](clang::Token Tok) {
4545
// Update previous token location.
4646
CoverageInfo->PrevTokLoc = Tok.getLocation();
47-
CoverageInfo->updateNextTokLoc(Tok.getLocation());
47+
if (Tok.getKind() != clang::tok::eod)
48+
CoverageInfo->updateNextTokLoc(Tok.getLocation());
4849
});
4950
return CoverageInfo;
5051
}
@@ -305,20 +306,24 @@ class CoverageMappingBuilder {
305306
/// non-comment token. If shrinking the skipped range would make it empty,
306307
/// this returns None.
307308
Optional<SpellingRegion> adjustSkippedRange(SourceManager &SM,
308-
SpellingRegion SR,
309+
SourceLocation LocStart,
310+
SourceLocation LocEnd,
309311
SourceLocation PrevTokLoc,
310312
SourceLocation NextTokLoc) {
313+
SpellingRegion SR{SM, LocStart, LocEnd};
311314
// If Range begin location is invalid, it's not a comment region.
312315
if (PrevTokLoc.isInvalid())
313316
return SR;
314317
unsigned PrevTokLine = SM.getSpellingLineNumber(PrevTokLoc);
315318
unsigned NextTokLine = SM.getSpellingLineNumber(NextTokLoc);
316319
SpellingRegion newSR(SR);
317-
if (SR.LineStart == PrevTokLine) {
320+
if (SM.isWrittenInSameFile(LocStart, PrevTokLoc) &&
321+
SR.LineStart == PrevTokLine) {
318322
newSR.LineStart = SR.LineStart + 1;
319323
newSR.ColumnStart = 1;
320324
}
321-
if (SR.LineEnd == NextTokLine) {
325+
if (SM.isWrittenInSameFile(LocEnd, NextTokLoc) &&
326+
SR.LineEnd == NextTokLine) {
322327
newSR.LineEnd = SR.LineEnd - 1;
323328
newSR.ColumnEnd = SR.ColumnStart + 1;
324329
}
@@ -354,14 +359,13 @@ class CoverageMappingBuilder {
354359
auto CovFileID = getCoverageFileID(LocStart);
355360
if (!CovFileID)
356361
continue;
357-
SpellingRegion SR{SM, LocStart, LocEnd};
358-
if (Optional<SpellingRegion> res =
359-
adjustSkippedRange(SM, SR, I.PrevTokLoc, I.NextTokLoc))
360-
SR = res.getValue();
361-
else
362+
Optional<SpellingRegion> SR =
363+
adjustSkippedRange(SM, LocStart, LocEnd, I.PrevTokLoc, I.NextTokLoc);
364+
if (!SR.hasValue())
362365
continue;
363366
auto Region = CounterMappingRegion::makeSkipped(
364-
*CovFileID, SR.LineStart, SR.ColumnStart, SR.LineEnd, SR.ColumnEnd);
367+
*CovFileID, SR->LineStart, SR->ColumnStart, SR->LineEnd,
368+
SR->ColumnEnd);
365369
// Make sure that we only collect the regions that are inside
366370
// the source code of this function.
367371
if (Region.LineStart >= FileLineRanges[*CovFileID].first &&
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
4+
5+
6+
x = 0;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -emit-llvm-only %s | FileCheck %s
2+
3+
int f() {
4+
int x = 0;
5+
#include "Inputs/comment.h" /*
6+
*/
7+
return x;
8+
}
9+
10+
// CHECK: File 0, 3:9 -> 8:2 = #0
11+
// CHECK-NEXT: Expansion,File 0, 5:10 -> 5:28 = #0
12+
// CHECK-NEXT: Skipped,File 0, 6:1 -> 6:7 = 0
13+
// CHECK-NEXT: File 1, 1:1 -> 7:1 = #0

0 commit comments

Comments
 (0)