[clang] [clang][CoverageMapping] do not emit gap when either end is an `ImplicitValueInitExpr` (PR #89564)

2024-04-27 Thread via cfe-commits

llvmbot wrote:

/pull-request llvm/llvm-project#90369

https://github.com/llvm/llvm-project/pull/89564
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CoverageMapping] do not emit gap when either end is an `ImplicitValueInitExpr` (PR #89564)

2024-04-27 Thread Wentao Zhang via cfe-commits

https://github.com/whentojump milestoned 
https://github.com/llvm/llvm-project/pull/89564
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CoverageMapping] do not emit gap when either end is an `ImplicitValueInitExpr` (PR #89564)

2024-04-27 Thread Wentao Zhang via cfe-commits

whentojump wrote:

/cherry-pick c1b6cca1214e7a9c14a30b81585dd8b81baeaa77


https://github.com/llvm/llvm-project/pull/89564
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CoverageMapping] do not emit gap when either end is an `ImplicitValueInitExpr` (PR #89564)

2024-04-22 Thread Wentao Zhang via cfe-commits

https://github.com/whentojump closed 
https://github.com/llvm/llvm-project/pull/89564
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CoverageMapping] do not emit gap when either end is an `ImplicitValueInitExpr` (PR #89564)

2024-04-22 Thread Wentao Zhang via cfe-commits

whentojump wrote:

Fwiw, I uncovered #86998 in the first place when I was compiling this file in 
Linux kernel: 
https://elixir.bootlin.com/linux/v6.8.1/source/fs/coredump.c#L545. I can 
confirm on my local side, with this patch, `fs/coredump.o` can be compiled and 
instrumented successfully.

I am going to squash merge this PR.

https://github.com/llvm/llvm-project/pull/89564
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CoverageMapping] do not emit gap when either end is an `ImplicitValueInitExpr` (PR #89564)

2024-04-22 Thread Zequan Wu via cfe-commits

https://github.com/ZequanWu approved this pull request.


https://github.com/llvm/llvm-project/pull/89564
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CoverageMapping] do not emit gap when either end is an `ImplicitValueInitExpr` (PR #89564)

2024-04-22 Thread Wentao Zhang via cfe-commits

whentojump wrote:

Thanks for your suggestion! @ZequanWu 

Would you please take another look?

https://github.com/llvm/llvm-project/pull/89564
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CoverageMapping] do not emit gap when either end is an `ImplicitValueInitExpr` (PR #89564)

2024-04-22 Thread Wentao Zhang via cfe-commits

https://github.com/whentojump ready_for_review 
https://github.com/llvm/llvm-project/pull/89564
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CoverageMapping] do not emit gap when either end is an `ImplicitValueInitExpr` (PR #89564)

2024-04-22 Thread Wentao Zhang via cfe-commits

https://github.com/whentojump updated 
https://github.com/llvm/llvm-project/pull/89564

>From abbdb318d62bb2e5ab6f07e7d0fe11f4a06b5a11 Mon Sep 17 00:00:00 2001
From: Wentao Zhang 
Date: Sun, 21 Apr 2024 21:27:01 -0500
Subject: [PATCH 1/4] [clang][CoverageMapping] do not emit gap when either end
 is an ImplicitValueInitExpr

---
 clang/lib/CodeGen/CoverageMappingGen.cpp | 9 ++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp 
b/clang/lib/CodeGen/CoverageMappingGen.cpp
index 64c39c5de351c7..dd8c0577d758ca 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -1368,9 +1368,12 @@ struct CounterCoverageMappingBuilder
 for (const Stmt *Child : S->children())
   if (Child) {
 // If last statement contains terminate statements, add a gap area
-// between the two statements. Skipping attributed statements, because
-// they don't have valid start location.
-if (LastStmt && HasTerminateStmt && !isa(Child)) {
+// between the two statements. Skipping attributed statements and
+// implicit initializations, because they don't have valid source
+// location.
+if (LastStmt && HasTerminateStmt && !isa(Child) &&
+!isa(Child) &&
+!isa(LastStmt)) {
   auto Gap = findGapAreaBetween(getEnd(LastStmt), getStart(Child));
   if (Gap)
 fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(),

>From f2931efcc97ce418ab724e2cb052b47ec5c87986 Mon Sep 17 00:00:00 2001
From: Wentao Zhang 
Date: Mon, 22 Apr 2024 11:17:49 -0500
Subject: [PATCH 2/4] add tests

---
 .../CoverageMapping/statement-expression.c| 36 +++
 1 file changed, 36 insertions(+)
 create mode 100644 clang/test/CoverageMapping/statement-expression.c

diff --git a/clang/test/CoverageMapping/statement-expression.c 
b/clang/test/CoverageMapping/statement-expression.c
new file mode 100644
index 00..5f9ab5838af342
--- /dev/null
+++ b/clang/test/CoverageMapping/statement-expression.c
@@ -0,0 +1,36 @@
+// RUN: %clang_cc1 -mllvm -emptyline-comment-coverage=false 
-fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping 
-emit-llvm-only -main-file-name statement-expression.c %s
+
+// No crash for the following examples, where GNU Statement Expression 
extension
+// could introduce region terminators (break, goto etc) before implicit
+// initializers in a struct or an array.
+// See https://github.com/llvm/llvm-project/pull/89564
+
+struct Foo {
+  int field1;
+  int field2;
+};
+
+void f1(void) {
+  struct Foo foo = {
+.field1 = ({
+  switch (0) {
+  case 0:
+break; // A region terminator
+  }
+  0;
+}),
+// ImplicitValueInitExpr introduced here for .field2
+  };
+}
+
+void f2(void) {
+  int arr[3] = {
+[0] = ({
+goto L0; // A region terminator
+L0:
+  0;
+}),
+// ImplicitValueInitExpr introduced here for subscript [1]
+[2] = 0,
+  };
+}

>From 6a00626a44b23815b99ad217b0ab9ed8567c19a2 Mon Sep 17 00:00:00 2001
From: Wentao Zhang 
Date: Mon, 22 Apr 2024 11:20:08 -0500
Subject: [PATCH 3/4] Revert "[clang][CoverageMapping] do not emit gap when
 either end is an ImplicitValueInitExpr"

This reverts commit abbdb318d62bb2e5ab6f07e7d0fe11f4a06b5a11.
---
 clang/lib/CodeGen/CoverageMappingGen.cpp | 9 +++--
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp 
b/clang/lib/CodeGen/CoverageMappingGen.cpp
index dd8c0577d758ca..64c39c5de351c7 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -1368,12 +1368,9 @@ struct CounterCoverageMappingBuilder
 for (const Stmt *Child : S->children())
   if (Child) {
 // If last statement contains terminate statements, add a gap area
-// between the two statements. Skipping attributed statements and
-// implicit initializations, because they don't have valid source
-// location.
-if (LastStmt && HasTerminateStmt && !isa(Child) &&
-!isa(Child) &&
-!isa(LastStmt)) {
+// between the two statements. Skipping attributed statements, because
+// they don't have valid start location.
+if (LastStmt && HasTerminateStmt && !isa(Child)) {
   auto Gap = findGapAreaBetween(getEnd(LastStmt), getStart(Child));
   if (Gap)
 fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(),

>From c7359ddf8ee689f12fa75ccc3ccff1bfecb2027a Mon Sep 17 00:00:00 2001
From: Wentao Zhang 
Date: Mon, 22 Apr 2024 11:33:41 -0500
Subject: [PATCH 4/4] move into findGapAreaBetween

---
 clang/lib/CodeGen/CoverageMappingGen.cpp | 11 ---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp 
b/clang/lib/CodeGen/CoverageMappingGen.cpp
index 64c39c5de351c7..733686d4946b3c 100644
--- 

[clang] [clang][CoverageMapping] do not emit gap when either end is an `ImplicitValueInitExpr` (PR #89564)

2024-04-22 Thread Wentao Zhang via cfe-commits

https://github.com/whentojump updated 
https://github.com/llvm/llvm-project/pull/89564

>From abbdb318d62bb2e5ab6f07e7d0fe11f4a06b5a11 Mon Sep 17 00:00:00 2001
From: Wentao Zhang 
Date: Sun, 21 Apr 2024 21:27:01 -0500
Subject: [PATCH 1/2] [clang][CoverageMapping] do not emit gap when either end
 is an ImplicitValueInitExpr

---
 clang/lib/CodeGen/CoverageMappingGen.cpp | 9 ++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp 
b/clang/lib/CodeGen/CoverageMappingGen.cpp
index 64c39c5de351c7..dd8c0577d758ca 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -1368,9 +1368,12 @@ struct CounterCoverageMappingBuilder
 for (const Stmt *Child : S->children())
   if (Child) {
 // If last statement contains terminate statements, add a gap area
-// between the two statements. Skipping attributed statements, because
-// they don't have valid start location.
-if (LastStmt && HasTerminateStmt && !isa(Child)) {
+// between the two statements. Skipping attributed statements and
+// implicit initializations, because they don't have valid source
+// location.
+if (LastStmt && HasTerminateStmt && !isa(Child) &&
+!isa(Child) &&
+!isa(LastStmt)) {
   auto Gap = findGapAreaBetween(getEnd(LastStmt), getStart(Child));
   if (Gap)
 fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(),

>From cc42595ea34f4f4b5b9dda968f323ec95ba712e9 Mon Sep 17 00:00:00 2001
From: Wentao Zhang 
Date: Mon, 22 Apr 2024 11:17:49 -0500
Subject: [PATCH 2/2] add tests

---
 .../CoverageMapping/statement-expression.c| 36 +++
 1 file changed, 36 insertions(+)
 create mode 100644 clang/test/CoverageMapping/statement-expression.c

diff --git a/clang/test/CoverageMapping/statement-expression.c 
b/clang/test/CoverageMapping/statement-expression.c
new file mode 100644
index 00..6006e3646ee943
--- /dev/null
+++ b/clang/test/CoverageMapping/statement-expression.c
@@ -0,0 +1,36 @@
+// RUN: %clang_cc1 -mllvm -emptyline-comment-coverage=false 
-fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping 
-emit-llvm-only -main-file-name statement-expression.c %s
+
+// No crash for the following examples, where GNU Statement Expression 
extension
+// could contain region terminators (break, goto etc) before implicit
+// initializers in a struct or an array.
+// See https://github.com/llvm/llvm-project/pull/89564
+
+struct Foo {
+  int field1;
+  int field2;
+};
+
+void test1(void) {
+  struct Foo foo = {
+.field1 = ({
+  switch (0) {
+  case 0:
+break; // A region terminator
+  }
+  0;
+}),
+// ImplicitValueInitExpr introduced here for .field2
+  };
+}
+
+void test2(void) {
+  int arr[3] = {
+[0] = ({
+goto L0; // A region terminator
+L0:
+  0;
+}),
+// ImplicitValueInitExpr introduced here for subscript [1]
+[2] = 0,
+  };
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CoverageMapping] do not emit gap when either end is an `ImplicitValueInitExpr` (PR #89564)

2024-04-22 Thread Wentao Zhang via cfe-commits

https://github.com/whentojump converted_to_draft 
https://github.com/llvm/llvm-project/pull/89564
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CoverageMapping] do not emit gap when either end is an `ImplicitValueInitExpr` (PR #89564)

2024-04-22 Thread Zequan Wu via cfe-commits

ZequanWu wrote:

There could be some other statements with invalid source locations (we have 
seen that before). I suggest validating both source locations inside 
`findGapAreaBetween` and returning `std::nullopt` if either of them is invalid.

https://github.com/llvm/llvm-project/pull/89564
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CoverageMapping] do not emit gap when either end is an `ImplicitValueInitExpr` (PR #89564)

2024-04-21 Thread Wentao Zhang via cfe-commits

https://github.com/whentojump updated 
https://github.com/llvm/llvm-project/pull/89564

>From abbdb318d62bb2e5ab6f07e7d0fe11f4a06b5a11 Mon Sep 17 00:00:00 2001
From: Wentao Zhang 
Date: Sun, 21 Apr 2024 21:27:01 -0500
Subject: [PATCH] [clang][CoverageMapping] do not emit gap when either end is
 an ImplicitValueInitExpr

---
 clang/lib/CodeGen/CoverageMappingGen.cpp | 9 ++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp 
b/clang/lib/CodeGen/CoverageMappingGen.cpp
index 64c39c5de351c7..dd8c0577d758ca 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -1368,9 +1368,12 @@ struct CounterCoverageMappingBuilder
 for (const Stmt *Child : S->children())
   if (Child) {
 // If last statement contains terminate statements, add a gap area
-// between the two statements. Skipping attributed statements, because
-// they don't have valid start location.
-if (LastStmt && HasTerminateStmt && !isa(Child)) {
+// between the two statements. Skipping attributed statements and
+// implicit initializations, because they don't have valid source
+// location.
+if (LastStmt && HasTerminateStmt && !isa(Child) &&
+!isa(Child) &&
+!isa(LastStmt)) {
   auto Gap = findGapAreaBetween(getEnd(LastStmt), getStart(Child));
   if (Gap)
 fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(),

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][CoverageMapping] do not emit gap when either end is an `ImplicitValueInitExpr` (PR #89564)

2024-04-21 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-codegen

Author: Wentao Zhang (whentojump)


Changes

Fixes #86998 

Two compiler explorer examples: 
[1](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,selection:(endColumn:21,endLineNumber:8,positionColumn:21,positionLineNumber:8,selectionStartColumn:21,selectionStartLineNumber:8,startColumn:21,startLineNumber:8),source:'struct+Foo+%7B%0Aint+field1%3B%0Aint+field2%3B%0A%7D%3B%0A%0Aint+main(void)+%7B%0Astruct+Foo+foo+%3D+%7B%0A.field1+%3D+(%7B%0Aswitch+(0)+%7B%0Acase+0:%0Abreak%3B%0A+//+%5E%5E%5E%5E%5E+HasTerminateStmt+set%0A%7D%0A0%3B%0A%7D),%0A//+%3C--+ImplicitValueInitExpr+introduced+for+.field2%0A%7D%3B%0A%7D'),l:'5',n:'0',o:'C+source+%231',t:'0')),k:61.44745998608211,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:compiler,i:(compiler:cclang_assertions_trunk,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'0',intel:'0',libraryCode:'1',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:2,lang:___c,libs:!(),options:'-fprofile-instr-generate+-fcoverage-mapping',overrides:!(),selection:(endColumn:45,endLineNumber:3,positionColumn:45,positionLineNumber:3,selectionStartColumn:45,selectionStartLineNumber:3,startColumn:45,startLineNumber:3),source:1),l:'5',n:'0',o:'+x86-64+clang+(assertions+trunk)+(Editor+%231)',t:'0')),k:34.5741843594503,l:'4',m:28.903654485049834,n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'x86-64+clang+(trunk)',editorid:1,fontScale:14,fontUsePx:'0',j:2,wrap:'1'),l:'5',n:'0',o:'Output+of+x86-64+clang+(assertions+trunk)+(Compiler+%232)',t:'0')),header:(),l:'4',m:71.09634551495017,n:'0',o:'',s:0,t:'0')),k:38.55254001391788,l:'3',n:'0',o:'',t:'0')),l:'2',m:100,n:'0',o:'',t:'0')),version:4),
 
[2](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,selection:(endColumn:2,endLineNumber:12,positionColumn:2,positionLineNumber:12,selectionStartColumn:2,selectionStartLineNumber:12,startColumn:2,startLineNumber:12),source:'int+main(void)+%7B%0Aint+arr%5B3%5D+%3D+%7B%0A%5B0%5D+%3D+(%7B%0Agoto+L0%3B%0A+//+%5E%5E%5E%5E+HasTerminateStmt+set%0AL0:%0A0%3B%0A%7D),%0A//+%3C--+ImplicitValueInitExpr+introduced+for+subscript+%5B1%5D%0A%5B2%5D+%3D+0,%0A%7D%3B%0A%7D'),l:'5',n:'0',o:'C+source+%231',t:'0')),k:61.44745998608211,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:compiler,i:(compiler:cclang_assertions_trunk,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'0',intel:'0',libraryCode:'1',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:2,lang:___c,libs:!(),options:'-fprofile-instr-generate+-fcoverage-mapping',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+clang+(assertions+trunk)+(Editor+%231)',t:'0')),k:34.5741843594503,l:'4',m:28.903654485049834,n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'x86-64+clang+(trunk)',editorid:1,fontScale:14,fontUsePx:'0',j:2,wrap:'1'),l:'5',n:'0',o:'Output+of+x86-64+clang+(assertions+trunk)+(Compiler+%232)',t:'0')),header:(),l:'4',m:71.09634551495017,n:'0',o:'',s:0,t:'0')),k:38.55254001391788,l:'3',n:'0',o:'',t:'0')),l:'2',m:100,n:'0',o:'',t:'0')),version:4)

Cause:

1. When visiting AST and generating mapping regions, a [region 
terminator](https://github.com/llvm/llvm-project/blob/llvmorg-18.1.4/clang/lib/CodeGen/CoverageMappingGen.cpp#L1197)
 (like `break` and `goto`) is likely followed by a stmt with `invalid 
sloc` (like `ImplicitValueInitExpr`).
2. Because a terminator is seen, the below branch will be executed when 
visiting the 2nd stmt:


https://github.com/llvm/llvm-project/blob/e6c3289804a67ea0bb6a86fadbe454dd93b8d855/clang/lib/CodeGen/CoverageMappingGen.cpp#L1375-L1376

3. However, the 2nd stmt doesn't have a valid source location and will fail 
some assertions in `findGapAreaBetween()`.







---
Full diff: https://github.com/llvm/llvm-project/pull/89564.diff


1 Files Affected:

- (modified) clang/lib/CodeGen/CoverageMappingGen.cpp (+3-1) 


``diff
diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp 
b/clang/lib/CodeGen/CoverageMappingGen.cpp
index 64c39c5de351c7..45296ff9cfb5e3 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -1370,7 +1370,9 @@ struct CounterCoverageMappingBuilder
 // If last statement contains terminate statements, add a gap area
 // between the two statements. Skipping attributed statements, because
 // they don't have valid start location.
-if 

[clang] [clang][CoverageMapping] do not emit gap when either end is an `ImplicitValueInitExpr` (PR #89564)

2024-04-21 Thread Wentao Zhang via cfe-commits

https://github.com/whentojump created 
https://github.com/llvm/llvm-project/pull/89564

Fixes #86998 

Two compiler explorer examples: 
[1](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,selection:(endColumn:21,endLineNumber:8,positionColumn:21,positionLineNumber:8,selectionStartColumn:21,selectionStartLineNumber:8,startColumn:21,startLineNumber:8),source:'struct+Foo+%7B%0Aint+field1%3B%0Aint+field2%3B%0A%7D%3B%0A%0Aint+main(void)+%7B%0Astruct+Foo+foo+%3D+%7B%0A.field1+%3D+(%7B%0Aswitch+(0)+%7B%0Acase+0:%0Abreak%3B%0A+//+%5E%5E%5E%5E%5E+HasTerminateStmt+set%0A%7D%0A0%3B%0A%7D),%0A//+%3C--+ImplicitValueInitExpr+introduced+for+.field2%0A%7D%3B%0A%7D'),l:'5',n:'0',o:'C+source+%231',t:'0')),k:61.44745998608211,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:compiler,i:(compiler:cclang_assertions_trunk,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'0',intel:'0',libraryCode:'1',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:2,lang:___c,libs:!(),options:'-fprofile-instr-generate+-fcoverage-mapping',overrides:!(),selection:(endColumn:45,endLineNumber:3,positionColumn:45,positionLineNumber:3,selectionStartColumn:45,selectionStartLineNumber:3,startColumn:45,startLineNumber:3),source:1),l:'5',n:'0',o:'+x86-64+clang+(assertions+trunk)+(Editor+%231)',t:'0')),k:34.5741843594503,l:'4',m:28.903654485049834,n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'x86-64+clang+(trunk)',editorid:1,fontScale:14,fontUsePx:'0',j:2,wrap:'1'),l:'5',n:'0',o:'Output+of+x86-64+clang+(assertions+trunk)+(Compiler+%232)',t:'0')),header:(),l:'4',m:71.09634551495017,n:'0',o:'',s:0,t:'0')),k:38.55254001391788,l:'3',n:'0',o:'',t:'0')),l:'2',m:100,n:'0',o:'',t:'0')),version:4),
 
[2](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,selection:(endColumn:2,endLineNumber:12,positionColumn:2,positionLineNumber:12,selectionStartColumn:2,selectionStartLineNumber:12,startColumn:2,startLineNumber:12),source:'int+main(void)+%7B%0Aint+arr%5B3%5D+%3D+%7B%0A%5B0%5D+%3D+(%7B%0Agoto+L0%3B%0A+//+%5E%5E%5E%5E+HasTerminateStmt+set%0AL0:%0A0%3B%0A%7D),%0A//+%3C--+ImplicitValueInitExpr+introduced+for+subscript+%5B1%5D%0A%5B2%5D+%3D+0,%0A%7D%3B%0A%7D'),l:'5',n:'0',o:'C+source+%231',t:'0')),k:61.44745998608211,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:compiler,i:(compiler:cclang_assertions_trunk,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'0',intel:'0',libraryCode:'1',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:2,lang:___c,libs:!(),options:'-fprofile-instr-generate+-fcoverage-mapping',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+clang+(assertions+trunk)+(Editor+%231)',t:'0')),k:34.5741843594503,l:'4',m:28.903654485049834,n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'x86-64+clang+(trunk)',editorid:1,fontScale:14,fontUsePx:'0',j:2,wrap:'1'),l:'5',n:'0',o:'Output+of+x86-64+clang+(assertions+trunk)+(Compiler+%232)',t:'0')),header:(),l:'4',m:71.09634551495017,n:'0',o:'',s:0,t:'0')),k:38.55254001391788,l:'3',n:'0',o:'',t:'0')),l:'2',m:100,n:'0',o:'',t:'0')),version:4)

Cause:

1. When visiting AST and generating mapping regions, a [region 
terminator](https://github.com/llvm/llvm-project/blob/llvmorg-18.1.4/clang/lib/CodeGen/CoverageMappingGen.cpp#L1197)
 (like `break` and `goto`) is likely followed by a stmt with `` 
(like `ImplicitValueInitExpr`).
2. Because a terminator is seen, the below branch will be executed when 
visiting the 2nd stmt:


https://github.com/llvm/llvm-project/blob/e6c3289804a67ea0bb6a86fadbe454dd93b8d855/clang/lib/CodeGen/CoverageMappingGen.cpp#L1375-L1376

3. However, the 2nd stmt doesn't have a valid source location and will fail 
some assertions in `findGapAreaBetween()`.







>From 73e29e5dc47bfdab0708476eb5961c70de59a6cd Mon Sep 17 00:00:00 2001
From: Wentao Zhang 
Date: Sun, 21 Apr 2024 21:27:01 -0500
Subject: [PATCH] [clang][CoverageMapping] do not emit gap when either end is
 an ImplicitValueInitExpr

---
 clang/lib/CodeGen/CoverageMappingGen.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp 
b/clang/lib/CodeGen/CoverageMappingGen.cpp
index 64c39c5de351c7..45296ff9cfb5e3 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -1370,7 +1370,9 @@ struct CounterCoverageMappingBuilder
 // If last statement contains terminate statements, add a gap area