llvm / clangir

A new (MLIR based) high-level IR for clang.
https://clangir.org
Other
383 stars 100 forks source link

Assertion failure on switch statement with non-block substatement #520

Closed dkolsen-pgi closed 4 days ago

dkolsen-pgi commented 7 months ago

ClangIR hits an assertion failure when a switch statement has a non-block statement as its substatement.

int f(int x) {
  switch (x)
  case 1:
    return -1;
  return x;
}
clang++: .../clang/lib/CIR/CodeGen/CIRGenStmt.cpp:964: 
cir::CIRGenFunction::buildSwitchStmt(const clang::SwitchStmt&)::<lambda()>::<lambda(mlir::OpBuilder&, mlir::Location, mlir::OperationState&)>: 
Assertion `cs && "expected compound stmt"' failed.

While code like this should never appear in production and will only ever be found in test suites that try to break the compiler, it is legal code in both C and C++ and should not trigger an internal compiler error.

wenpen commented 7 months ago

I'd like to work on this, it looks like I only need to add some logic to handle CaseStmt and DefaultStmt.

smeenai commented 4 days ago

This works after #1006: https://godbolt.org/z/bK8cGhsvv