Skip to content

Commit

Permalink
switchStmt support single caseStmt and defaultStmt
Browse files Browse the repository at this point in the history
  • Loading branch information
wenpen committed Apr 1, 2024
1 parent 330c983 commit 8cba98d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
18 changes: 16 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -980,11 +980,25 @@ mlir::LogicalResult CIRGenFunction::buildSwitchStmt(const SwitchStmt &S) {
getLoc(S.getBeginLoc()), condV,
/*switchBuilder=*/
[&](mlir::OpBuilder &b, mlir::Location loc, mlir::OperationState &os) {
SmallVector<mlir::Attribute, 4> caseAttrs;
currLexScope->setAsSwitch();

if (auto *caseStmt = dyn_cast<CaseStmt>(S.getBody())) {
res = buildCaseStmt(*caseStmt, condV.getType(), caseAttrs, os);
os.addAttribute("cases", builder.getArrayAttr(caseAttrs));
return;
}

if (auto *defaultStmt = dyn_cast<DefaultStmt>(S.getBody())) {
res =
buildDefaultStmt(*defaultStmt, condV.getType(), caseAttrs, os);
os.addAttribute("cases", builder.getArrayAttr(caseAttrs));
return;
}

auto *cs = dyn_cast<CompoundStmt>(S.getBody());
assert(cs && "expected compound stmt");
SmallVector<mlir::Attribute, 4> caseAttrs;

currLexScope->setAsSwitch();
mlir::Block *lastCaseBlock = nullptr;
for (auto *c : cs->body()) {
bool caseLike = isa<CaseStmt, DefaultStmt>(c);
Expand Down
33 changes: 33 additions & 0 deletions clang/test/CIR/CodeGen/switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ void sw12(int a) {
break;
}
}

// CHECK: cir.func @_Z4sw12i
// CHECK: cir.scope {
// CHECK: cir.switch
Expand All @@ -275,6 +276,38 @@ void sw12(int a) {
// CHECK-NEXT: cir.break
// CHECK-NEXT: }

int sw13(int a) {
switch (a)
case 1:
return -1;
return a;
}

// CHECK: cir.func @_Z4sw13i
// CHECK: cir.scope {
// CHECK: cir.switch
// CHECK-NEXT: case (equal, 1) {
// CHECK: cir.return
// CHECK: }
// CHECK: }
// CHECK: cir.return

int sw14(int a) {
switch (a)
default:
return -1;
return a;
}

// CHECK: cir.func @_Z4sw14i
// CHECK: cir.scope {
// CHECK: cir.switch
// CHECK-NEXT: case (default) {
// CHECK: cir.return
// CHECK: }
// CHECK: }
// CHECK: cir.return

void fallthrough(int x) {
switch (x) {
case 1:
Expand Down

0 comments on commit 8cba98d

Please sign in to comment.