@@ -44,7 +44,8 @@ CoverageMappingModuleGen::setUpCoverageCallbacks(Preprocessor &PP) {
44
44
PP.setTokenWatcher ([CoverageInfo](clang::Token Tok) {
45
45
// Update previous token location.
46
46
CoverageInfo->PrevTokLoc = Tok.getLocation ();
47
- CoverageInfo->updateNextTokLoc (Tok.getLocation ());
47
+ if (Tok.getKind () != clang::tok::eod)
48
+ CoverageInfo->updateNextTokLoc (Tok.getLocation ());
48
49
});
49
50
return CoverageInfo;
50
51
}
@@ -305,20 +306,24 @@ class CoverageMappingBuilder {
305
306
// / non-comment token. If shrinking the skipped range would make it empty,
306
307
// / this returns None.
307
308
Optional<SpellingRegion> adjustSkippedRange (SourceManager &SM,
308
- SpellingRegion SR,
309
+ SourceLocation LocStart,
310
+ SourceLocation LocEnd,
309
311
SourceLocation PrevTokLoc,
310
312
SourceLocation NextTokLoc) {
313
+ SpellingRegion SR{SM, LocStart, LocEnd};
311
314
// If Range begin location is invalid, it's not a comment region.
312
315
if (PrevTokLoc.isInvalid ())
313
316
return SR;
314
317
unsigned PrevTokLine = SM.getSpellingLineNumber (PrevTokLoc);
315
318
unsigned NextTokLine = SM.getSpellingLineNumber (NextTokLoc);
316
319
SpellingRegion newSR (SR);
317
- if (SR.LineStart == PrevTokLine) {
320
+ if (SM.isWrittenInSameFile (LocStart, PrevTokLoc) &&
321
+ SR.LineStart == PrevTokLine) {
318
322
newSR.LineStart = SR.LineStart + 1 ;
319
323
newSR.ColumnStart = 1 ;
320
324
}
321
- if (SR.LineEnd == NextTokLine) {
325
+ if (SM.isWrittenInSameFile (LocEnd, NextTokLoc) &&
326
+ SR.LineEnd == NextTokLine) {
322
327
newSR.LineEnd = SR.LineEnd - 1 ;
323
328
newSR.ColumnEnd = SR.ColumnStart + 1 ;
324
329
}
@@ -354,14 +359,13 @@ class CoverageMappingBuilder {
354
359
auto CovFileID = getCoverageFileID (LocStart);
355
360
if (!CovFileID)
356
361
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 ())
362
365
continue ;
363
366
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 );
365
369
// Make sure that we only collect the regions that are inside
366
370
// the source code of this function.
367
371
if (Region.LineStart >= FileLineRanges[*CovFileID].first &&
0 commit comments