Skip to content

Commit 53ccc79

Browse files
YazZz1klanza
authored andcommitted
[CIR][CIRGen] Support for __attribute__((fallthrough)) statement (#517)
This PR adds handling of AttributedStmt to support fallthrough attribute.
1 parent 38ca68d commit 53ccc79

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

clang/lib/CIR/CodeGen/CIRGenFunction.h

+2
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,8 @@ class CIRGenFunction : public CIRGenTypeCache {
986986
mlir::LogicalResult buildLabel(const clang::LabelDecl *D);
987987
mlir::LogicalResult buildLabelStmt(const clang::LabelStmt &S);
988988

989+
mlir::LogicalResult buildAttributedStmt(const AttributedStmt &S);
990+
989991
mlir::LogicalResult buildBreakStmt(const clang::BreakStmt &S);
990992
mlir::LogicalResult buildContinueStmt(const clang::ContinueStmt &S);
991993

clang/lib/CIR/CodeGen/CIRGenStmt.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ mlir::LogicalResult CIRGenFunction::buildSimpleStmt(const Stmt *S,
306306
return buildBreakStmt(cast<BreakStmt>(*S));
307307

308308
case Stmt::AttributedStmtClass:
309+
return buildAttributedStmt(cast<AttributedStmt>(*S));
310+
309311
case Stmt::SEHLeaveStmtClass:
310312
llvm::errs() << "CIR codegen for '" << S->getStmtClassName()
311313
<< "' not implemented\n";
@@ -325,6 +327,23 @@ mlir::LogicalResult CIRGenFunction::buildLabelStmt(const clang::LabelStmt &S) {
325327
return buildStmt(S.getSubStmt(), /* useCurrentScope */ true);
326328
}
327329

330+
mlir::LogicalResult
331+
CIRGenFunction::buildAttributedStmt(const AttributedStmt &S) {
332+
for (const auto *A : S.getAttrs()) {
333+
switch (A->getKind()) {
334+
case attr::NoMerge:
335+
case attr::NoInline:
336+
case attr::AlwaysInline:
337+
case attr::MustTail:
338+
llvm_unreachable("NIY attributes");
339+
default:
340+
break;
341+
}
342+
}
343+
344+
return buildStmt(S.getSubStmt(), true, S.getAttrs());
345+
}
346+
328347
// Add terminating yield on body regions (loops, ...) in case there are
329348
// not other terminators used.
330349
// FIXME: make terminateCaseRegion use this too.

clang/test/CIR/CodeGen/switch.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,29 @@ void sw12(int a) {
274274
// CHECK-NEXT: ^bb1: // no predecessors
275275
// CHECK-NEXT: cir.break
276276
// CHECK-NEXT: }
277+
278+
void fallthrough(int x) {
279+
switch (x) {
280+
case 1:
281+
__attribute__((fallthrough));
282+
case 2:
283+
break;
284+
default:
285+
break;
286+
}
287+
}
288+
289+
// CHECK: cir.func @_Z11fallthroughi
290+
// CHECK: cir.scope {
291+
// CHECK: cir.switch (%1 : !s32i) [
292+
// CHECK-NEXT: case (equal, 1) {
293+
// CHECK-NEXT: cir.yield
294+
// CHECK-NEXT: },
295+
// CHECK-NEXT: case (equal, 2) {
296+
// CHECK-NEXT: cir.break
297+
// CHECK-NEXT: },
298+
// CHECK-NEXT: case (default) {
299+
// CHECK-NEXT: cir.break
300+
// CHECK-NEXT: }
301+
// CHECK-NEXT: ]
302+
// CHECK-NEXT: }

0 commit comments

Comments
 (0)