<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/86998>86998</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang][CoverageMapping] assertion failure during compiling `switch` within statement expressions
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          whentojump
      </td>
    </tr>
</table>

<pre>
    When a [statement expression](https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html) contains a `switch` statement during a partial struct initialization, source-based code coverage will crash. E.g.,

```c
    struct Foo foo = {
 .field1 = ({
            switch (123) {
            case 123:
 break;
            }
            456;
        }),
 };
```

Compiler explorer [link](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,selection:(endColumn:2,endLineNumber:33,positionColumn:2,positionLineNumber:33,selectionStartColumn:2,selectionStartLineNumber:33,startColumn:2,startLineNumber:33),source:'//+An+example+simplified+from+https://elixir.bootlin.com/linux/v6.8.1/source/fs/coredump.c.%0A//%0A//+Note+that+assertions+should+be+enabled,+therefore+prebuilt+packages+from%0A//+apt.llvm.org+etc+might+not+behave+the+same.%0A//%0A//+Assertion+failure:%0A//%0A//+++$+clang+-fprofile-instr-generate+-fcoverage-mapping+this_file.c%0A//%0A//+No+assertion+failure:%0A//%0A//+++$+clang+this_file.c%0A//%0A%0Astruct+Foo+%7B%0A++++int+field1%3B%0A++++int+field2%3B+//+.field2+must+be+defined+but+not+be+intialized%0A%7D%3B%0A%0Aint+main(int+argc,+char+**argv)+%7B%0A++++//+https://elixir.bootlin.com/linux/v6.8.1/source/fs/coredump.c%23L545%0A++++struct+Foo+foo+%3D+%7B%0A++++++++.field1+%3D+(%7B%0A++++++++++++switch+(123)+%7B%0A++++++++++++case+123:%0A++++++++++++++++break%3B%0A++++++++++++%7D%0A++++++++++++456%3B%0A++++++++%7D),%0A++++++++//+.field2+must+be+defined+but+not+be+intialized%0A++++%7D%3B%0A++++return+0%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'),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)

```text
clang: /root/llvm-project/clang/lib/CodeGen/CoverageMappingGen.cpp:1238: std::optional<clang::SourceRange> {anonymous}::CounterCoverageMappingBuilder::findGapAreaBetween(clang::SourceLocation, clang::SourceLocation): Assertion `AfterLoc.isValid()' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -fprofile-instr-generate -fcoverage-mapping <source>
1.      <eof> parser at end of file
2.      <source>:20:5: LLVM IR generation of declaration 'main'
3.      <source>:20:5: Generating code for declaration 'main'
 #0 0x0000000003922c78 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3922c78)
 #1 0x000000000392095c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x392095c)
 #2 0x00000000038671f8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007f5cbfc42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007f5cbfc969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x00007f5cbfc42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x00007f5cbfc287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x00007f5cbfc2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #8 0x00007f5cbfc39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #9 0x0000000003d79e01 (anonymous namespace)::CounterCoverageMappingBuilder::findGapAreaBetween(clang::SourceLocation, clang::SourceLocation) CoverageMappingGen.cpp:0:0
#10 0x0000000003d8327b (anonymous namespace)::CounterCoverageMappingBuilder::VisitStmt(clang::Stmt const*) CoverageMappingGen.cpp:0:0
#11 0x0000000003d81176 clang::StmtVisitorBase<llvm::make_const_ptr, (anonymous namespace)::CounterCoverageMappingBuilder, void>::Visit(clang::Stmt const*) CoverageMappingGen.cpp:0:0
#12 0x0000000003d832b3 (anonymous namespace)::CounterCoverageMappingBuilder::VisitStmt(clang::Stmt const*) CoverageMappingGen.cpp:0:0
#13 0x0000000003d81de0 clang::StmtVisitorBase<llvm::make_const_ptr, (anonymous namespace)::CounterCoverageMappingBuilder, void>::Visit(clang::Stmt const*) CoverageMappingGen.cpp:0:0
#14 0x0000000003d832b3 (anonymous namespace)::CounterCoverageMappingBuilder::VisitStmt(clang::Stmt const*) CoverageMappingGen.cpp:0:0
#15 0x0000000003d81e9f clang::StmtVisitorBase<llvm::make_const_ptr, (anonymous namespace)::CounterCoverageMappingBuilder, void>::Visit(clang::Stmt const*) CoverageMappingGen.cpp:0:0
#16 0x0000000003d88c7b clang::CodeGen::CoverageMappingGen::emitCounterMapping(clang::Decl const*, llvm::raw_ostream&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d88c7b)
#17 0x0000000003d324a8 clang::CodeGen::CodeGenPGO::emitCounterRegionMapping(clang::Decl const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d324a8)
#18 0x0000000003d67ddb clang::CodeGen::CodeGenPGO::assignRegionCounters(clang::GlobalDecl, llvm::Function*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d67ddb)
#19 0x0000000003cc5339 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3cc5339)
#20 0x0000000003d24952 clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d24952)
#21 0x0000000003d1f205 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d1f205)
#22 0x0000000003d201e0 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x3d201e0)
#23 0x0000000003d29d83 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#24 0x00000000041cee86 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#25 0x00000000041bf7a8 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x41bf7a8)
#26 0x00000000061c8fe4 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x61c8fe4)
#27 0x00000000041cc9c8 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x41cc9c8)
#28 0x000000000444c039 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x444c039)
#29 0x00000000043d572e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x43d572e)
#30 0x000000000452eece clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x452eece)
#31 0x0000000000c3d6dc cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xc3d6dc)
#32 0x0000000000c3696a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#33 0x000000000420f949 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#34 0x00000000038676a4 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x38676a4)
#35 0x000000000420ff3f clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#36 0x00000000041d62d5 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x41d62d5)
#37 0x00000000041d6d3d clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x41d6d3d)
#38 0x00000000041deaa5 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0x41deaa5)
#39 0x0000000000c3ab75 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xc3ab75)
#40 0x0000000000b1dd24 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xb1dd24)
#41 0x00007f5cbfc29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#42 0x00007f5cbfc29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#43 0x0000000000c3645e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+0xc3645e)
clang: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134
```




</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkW1uT46iS_jWqF8IOCXR9qAfbVe6diZ7pjq7Z3kcHQshmSgIFoOqq_fUbgGwL-dJVfby7Z89O-PQpQZL55YXMBMtYKbbllN4HyTJIHu5wr3dC3v_YUa7F333b3ZWierv_jx3lAIMgWSqNNW0p14C-dpIqxQQPkocA5jutOxWgRQDXAVxvCZlveT8XchvAteAN47QSRLmpAK6f9oxmj6-dVPOdbpsAFoAIrjHjyohLQ_WDabIL0hAcBVe9ZHwLMOiw1Aw3QGnZEw0YZ-aR_SfWBhRcASV6SeisxIpWgIiKAiJeqMRbCn6wpgFEYrWbg8f5dh7AVRA-BOFi-DcN3Ye4ZwDAXs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