Skip to content

Commit f79f472

Browse files
committed
Merging r347262:
------------------------------------------------------------------------ r347262 | vedantk | 2018-11-19 12:10:22 -0800 (Mon, 19 Nov 2018) | 8 lines [Coverage] Fix PR39258: support coverage regions that start deeper than they end popRegions used to assume that the start location of a region can't be nested deeper than the end location, which is not always true. Patch by Orivej Desh! Differential Revision: https://reviews.llvm.org/D53244 ------------------------------------------------------------------------ llvm-svn: 347798
1 parent b948f5f commit f79f472

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

clang/lib/CodeGen/CoverageMappingGen.cpp

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,15 @@ struct CounterCoverageMappingBuilder
553553
completeDeferred(Count, DeferredEndLoc);
554554
}
555555

556+
size_t locationDepth(SourceLocation Loc) {
557+
size_t Depth = 0;
558+
while (Loc.isValid()) {
559+
Loc = getIncludeOrExpansionLoc(Loc);
560+
Depth++;
561+
}
562+
return Depth;
563+
}
564+
556565
/// Pop regions from the stack into the function's list of regions.
557566
///
558567
/// Adds all regions from \c ParentIndex to the top of the stack to the
@@ -567,19 +576,41 @@ struct CounterCoverageMappingBuilder
567576
SourceLocation EndLoc = Region.hasEndLoc()
568577
? Region.getEndLoc()
569578
: RegionStack[ParentIndex].getEndLoc();
579+
size_t StartDepth = locationDepth(StartLoc);
580+
size_t EndDepth = locationDepth(EndLoc);
570581
while (!SM.isWrittenInSameFile(StartLoc, EndLoc)) {
571-
// The region ends in a nested file or macro expansion. Create a
572-
// separate region for each expansion.
573-
SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc);
574-
assert(SM.isWrittenInSameFile(NestedLoc, EndLoc));
575-
576-
if (!isRegionAlreadyAdded(NestedLoc, EndLoc))
577-
SourceRegions.emplace_back(Region.getCounter(), NestedLoc, EndLoc);
578-
579-
EndLoc = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(EndLoc));
580-
if (EndLoc.isInvalid())
581-
llvm::report_fatal_error("File exit not handled before popRegions");
582+
bool UnnestStart = StartDepth >= EndDepth;
583+
bool UnnestEnd = EndDepth >= StartDepth;
584+
if (UnnestEnd) {
585+
// The region ends in a nested file or macro expansion. Create a
586+
// separate region for each expansion.
587+
SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc);
588+
assert(SM.isWrittenInSameFile(NestedLoc, EndLoc));
589+
590+
if (!isRegionAlreadyAdded(NestedLoc, EndLoc))
591+
SourceRegions.emplace_back(Region.getCounter(), NestedLoc, EndLoc);
592+
593+
EndLoc = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(EndLoc));
594+
if (EndLoc.isInvalid())
595+
llvm::report_fatal_error("File exit not handled before popRegions");
596+
EndDepth--;
597+
}
598+
if (UnnestStart) {
599+
// The region begins in a nested file or macro expansion. Create a
600+
// separate region for each expansion.
601+
SourceLocation NestedLoc = getEndOfFileOrMacro(StartLoc);
602+
assert(SM.isWrittenInSameFile(StartLoc, NestedLoc));
603+
604+
if (!isRegionAlreadyAdded(StartLoc, NestedLoc))
605+
SourceRegions.emplace_back(Region.getCounter(), StartLoc, NestedLoc);
606+
607+
StartLoc = getIncludeOrExpansionLoc(StartLoc);
608+
if (StartLoc.isInvalid())
609+
llvm::report_fatal_error("File exit not handled before popRegions");
610+
StartDepth--;
611+
}
582612
}
613+
Region.setStartLoc(StartLoc);
583614
Region.setEndLoc(EndLoc);
584615

585616
MostRecentLocation = EndLoc;

clang/test/CoverageMapping/macros.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#define MACRO_2 bar()
55
#define MACRO_1 return; MACRO_2
66
#define MACRO_3 MACRO_2
7+
#define GOTO goto
78

89
void bar() {}
910

@@ -56,6 +57,15 @@ void func5() { // CHECK-NEXT: File 0, [[@LINE]]:14 -> [[@LINE+4]]:2 = #0
5657
// CHECK-NEXT: Expansion,File 1, 6:17 -> 6:24 = #1
5758
// CHECK-NEXT: File 2, 4:17 -> 4:22 = #1
5859

60+
// CHECK-NEXT: func6
61+
void func6(unsigned count) { // CHECK-NEXT: File 0, [[@LINE]]:28 -> [[@LINE+4]]:2 = #0
62+
begin: // CHECK-NEXT: File 0, [[@LINE]]:1 -> [[@LINE+3]]:2 = #1
63+
if (count--) // CHECK-NEXT: File 0, [[@LINE]]:9 -> [[@LINE]]:16 = #1
64+
GOTO begin; // CHECK-NEXT: File 0, [[@LINE]]:9 -> [[@LINE]]:19 = #2
65+
}
66+
// CHECK-NEXT: Expansion,File 0, [[@LINE-2]]:9 -> [[@LINE-2]]:13 = #2
67+
// CHECK-NEXT: File 1, 7:14 -> 7:18 = #2
68+
5969
int main(int argc, const char *argv[]) {
6070
func();
6171
func2();

0 commit comments

Comments
 (0)