From 00354796372407a06f532df2f45cc94e58f06386 Mon Sep 17 00:00:00 2001 From: Vedant Kumar Date: Mon, 18 Sep 2017 23:37:30 +0000 Subject: [PATCH] [Coverage] Use a new API to label gap areas This will make it possible for llvm-cov to pick better line execution counts, and is part of the fix for llvm.org/PR34612. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@313598 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CoverageMappingGen.cpp | 30 ++++++++++++++++---- test/CoverageMapping/deferred-region.cpp | 36 ++++++++++++------------ 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/lib/CodeGen/CoverageMappingGen.cpp b/lib/CodeGen/CoverageMappingGen.cpp index cf736a484e..943c7e0ccd 100644 --- a/lib/CodeGen/CoverageMappingGen.cpp +++ b/lib/CodeGen/CoverageMappingGen.cpp @@ -48,11 +48,16 @@ class SourceMappingRegion { /// Whether this region should be emitted after its parent is emitted. bool DeferRegion; + /// Whether this region is a gap region. The count from a gap region is set + /// as the line execution count if there are no other regions on the line. + bool GapRegion; + public: SourceMappingRegion(Counter Count, Optional LocStart, - Optional LocEnd, bool DeferRegion = false) + Optional LocEnd, bool DeferRegion = false, + bool GapRegion = false) : Count(Count), LocStart(LocStart), LocEnd(LocEnd), - DeferRegion(DeferRegion) {} + DeferRegion(DeferRegion), GapRegion(GapRegion) {} const Counter &getCounter() const { return Count; } @@ -79,6 +84,10 @@ public: bool isDeferred() const { return DeferRegion; } void setDeferred(bool Deferred) { DeferRegion = Deferred; } + + bool isGap() const { return GapRegion; } + + void setGap(bool Gap) { GapRegion = Gap; } }; /// Spelling locations for the start and end of a source region. @@ -322,9 +331,16 @@ public: // Find the spelling locations for the mapping region. SpellingRegion SR{SM, LocStart, LocEnd}; assert(SR.isInSourceOrder() && "region start and end out of order"); - MappingRegions.push_back(CounterMappingRegion::makeRegion( - Region.getCounter(), *CovFileID, SR.LineStart, SR.ColumnStart, - SR.LineEnd, SR.ColumnEnd)); + + if (Region.isGap()) { + MappingRegions.push_back(CounterMappingRegion::makeGapRegion( + Region.getCounter(), *CovFileID, SR.LineStart, SR.ColumnStart, + SR.LineEnd, SR.ColumnEnd)); + } else { + MappingRegions.push_back(CounterMappingRegion::makeRegion( + Region.getCounter(), *CovFileID, SR.LineStart, SR.ColumnStart, + SR.LineEnd, SR.ColumnEnd)); + } } } @@ -496,6 +512,7 @@ struct CounterCoverageMappingBuilder if (!SpellingRegion(SM, DR.getStartLoc(), DeferredEndLoc).isInSourceOrder()) return Index; + DR.setGap(true); DR.setCounter(Count); DR.setEndLoc(DeferredEndLoc); handleFileExit(DeferredEndLoc); @@ -1112,6 +1129,9 @@ static void dump(llvm::raw_ostream &OS, StringRef FunctionName, case CounterMappingRegion::SkippedRegion: OS << "Skipped,"; break; + case CounterMappingRegion::GapRegion: + OS << "Gap,"; + break; } OS << "File " << R.FileID << ", " << R.LineStart << ":" << R.ColumnStart diff --git a/test/CoverageMapping/deferred-region.cpp b/test/CoverageMapping/deferred-region.cpp index 282b6a1563..41b5e6be8c 100644 --- a/test/CoverageMapping/deferred-region.cpp +++ b/test/CoverageMapping/deferred-region.cpp @@ -7,7 +7,7 @@ void foo(int x) { if (x == 0) { return; - } // CHECK: [[@LINE]]:4 -> [[@LINE+2]]:2 = (#0 - #1) + } // CHECK: Gap,File 0, [[@LINE]]:4 -> [[@LINE+2]]:2 = (#0 - #1) } @@ -15,11 +15,11 @@ void foo(int x) { void fooo(int x) { if (x == 0) { return; - } // CHECK: [[@LINE]]:4 -> [[@LINE+2]]:3 = (#0 - #1) + } // CHECK: Gap,File 0, [[@LINE]]:4 -> [[@LINE+2]]:3 = (#0 - #1) if (x == 1) { return; - } // CHECK: [[@LINE]]:4 -> [[@LINE+2]]:2 = ((#0 - #1) - #2) + } // CHECK: Gap,File 0, [[@LINE]]:4 -> [[@LINE+2]]:2 = ((#0 - #1) - #2) } @@ -31,10 +31,10 @@ void baz() { // CHECK: [[@LINE]]:12 -> [[@LINE+2]]:2 // CHECK-LABEL: _Z3bari: void bar(int x) { IF (x) - return; // CHECK: [[@LINE]]:11 -> [[@LINE+2]]:3 = (#0 - #1) + return; // CHECK: Gap,File 0, [[@LINE]]:11 -> [[@LINE+2]]:3 = (#0 - #1) IF (!x) - return; // CHECK: [[@LINE]]:11 -> [[@LINE+2]]:3 = ((#0 - #1) - #2) + return; // CHECK: Gap,File 0, [[@LINE]]:11 -> [[@LINE+2]]:3 = ((#0 - #1) - #2) foo(x); } @@ -69,34 +69,34 @@ void weird_if() { int i = 0; if (false) - return; // CHECK: [[@LINE]]:11 -> [[@LINE+2]]:3 = (#0 - #1) + return; // CHECK: Gap,File 0, [[@LINE]]:11 -> [[@LINE+2]]:3 = (#0 - #1) if (false) i++; if (i + 100 > 0) { // CHECK: [[@LINE]]:20 -> [[@LINE+6]]:4 = #3 if (false) // CHECK: [[@LINE+1]]:7 -> [[@LINE+1]]:13 = #4 - return; // CHECK: [[@LINE]]:13 -> [[@LINE+2]]:5 = (#3 - #4) + return; // CHECK: Gap,File 0, [[@LINE]]:13 -> [[@LINE+2]]:5 = (#3 - #4) // CHECK: [[@LINE+1]]:5 -> [[@LINE+3]]:4 = (#3 - #4) - return; // CHECK: [[@LINE]]:5 -> [[@LINE+4]]:3 = ((#0 - #1) - #3) + return; // CHECK: Gap,File 0, [[@LINE]]:5 -> [[@LINE+4]]:3 = ((#0 - #1) - #3) } if (false) - return; + return; // CHECK: Gap,File 0, [[@LINE]]:11 -> [[@LINE+1]]:2 } // CHECK-LABEL: _Z8for_loopv: void for_loop() { if (false) - return; // CHECK: [[@LINE]]:11 -> [[@LINE+2]]:3 = (#0 - #1) + return; // CHECK: Gap,File 0, [[@LINE]]:11 -> [[@LINE+2]]:3 = (#0 - #1) for (int i = 0; i < 10; ++i) { if (i % 2 == 0) - continue; // CHECK: [[@LINE]]:15 -> [[@LINE+2]]:5 = (#2 - #3) + continue; // CHECK: Gap,File 0, [[@LINE]]:15 -> [[@LINE+2]]:5 = (#2 - #3) if (i % 5 == 0) - break; // CHECK: [[@LINE]]:12 -> [[@LINE+2]]:5 = ((#2 - #3) - #4) + break; // CHECK: Gap,File 0, [[@LINE]]:12 -> [[@LINE+2]]:5 = ((#2 - #3) - #4) int x = i; // CHECK: [[@LINE]]:5 -> [[@LINE+3]]:4 = ((#2 - #3) - #4) return; // CHECK-NOT: [[@LINE]]:11 -> [[@LINE+2]] @@ -109,22 +109,22 @@ struct Error {}; // CHECK-LABEL: _Z10while_loopv: void while_loop() { if (false) - return; // CHECK: [[@LINE]]:11 -> [[@LINE+2]]:3 = (#0 - #1) + return; // CHECK: Gap,File 0, [[@LINE]]:11 -> [[@LINE+2]]:3 = (#0 - #1) int x = 0; while (++x < 10) { if (x == 1) - continue; // CHECK: [[@LINE]]:15 -> [[@LINE+2]]:5 = (#2 - #3) + continue; // CHECK: Gap,File 0, [[@LINE]]:15 -> [[@LINE+2]]:5 = (#2 - #3) while (++x < 4) { if (x == 3) - break; // CHECK: [[@LINE]]:14 -> [[@LINE+2]]:7 = (#4 - #5) + break; // CHECK: Gap,File 0, [[@LINE]]:14 -> [[@LINE+2]]:7 = (#4 - #5) while (++x < 5) {} } if (x == 0) - throw Error(); // CHECK: [[@LINE]]:20 -> [[@LINE+2]]:5 = ((#2 - #3) - #7) + throw Error(); // CHECK: Gap,File 0, [[@LINE]]:20 -> [[@LINE+2]]:5 = ((#2 - #3) - #7) while (++x < 9) { if (x == 0) @@ -137,12 +137,12 @@ void while_loop() { // CHECK-LABEL: _Z5gotosv: void gotos() { if (false) - goto out; // CHECK: [[@LINE]]:13 -> [[@LINE+2]]:3 = (#0 - #1) + goto out; // CHECK: Gap,File 0, [[@LINE]]:13 -> [[@LINE+2]]:3 = (#0 - #1) return; // CHECK: [[@LINE]]:3 -> [[@LINE+4]]:2 = (#0 - #1) out: - return; // CHECK: [[@LINE]]:8 -> [[@LINE+1]]:2 = 0 + return; // CHECK: Gap,File 0, [[@LINE]]:8 -> [[@LINE+1]]:2 = 0 } int main() { -- 2.40.0