Skip to content

Commit 8d87565

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 fd354e5 commit 8d87565

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
@@ -307,6 +307,8 @@ mlir::LogicalResult CIRGenFunction::buildSimpleStmt(const Stmt *S,
307307
return buildBreakStmt(cast<BreakStmt>(*S));
308308

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

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