Skip to content

Commit 7cd441f

Browse files
committed
[clang][NFC] Wrap TYPE_SWITCH in "do while (0)" in the interpreter
Wraps the expansions of TYPE_SWITCH and COMPOSITE_TYPE_SWITCH in the constexpr interpreter with "do { ... } while (0)" so that these macros can be used like this: if (llvm::Optional<PrimType> T = Ctx.classify(FieldTy)) TYPE_SWITCH(*T, Ok &= ReturnValue<T>(FP.deref<T>(), Value)); else Ok &= Composite(FieldTy, FP, Value); This bug was found while testing D116316. See also review comment: https://reviews.llvm.org/D64146?id=208520#inline-584131 Also cleaned up the macro definitions by removing the superfluous do-while statements and removed the unused INT_TPYE_SWITCH macro. Differential Revision: https://reviews.llvm.org/D117301
1 parent 6be7756 commit 7cd441f

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

clang/lib/AST/Interp/PrimType.h

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -81,35 +81,27 @@ inline bool isPrimitiveIntegral(PrimType Type) {
8181
/// Helper macro to simplify type switches.
8282
/// The macro implicitly exposes a type T in the scope of the inner block.
8383
#define TYPE_SWITCH_CASE(Name, B) \
84-
case Name: { using T = PrimConv<Name>::T; do {B;} while(0); break; }
84+
case Name: { using T = PrimConv<Name>::T; B; break; }
8585
#define TYPE_SWITCH(Expr, B) \
86-
switch (Expr) { \
87-
TYPE_SWITCH_CASE(PT_Sint8, B) \
88-
TYPE_SWITCH_CASE(PT_Uint8, B) \
89-
TYPE_SWITCH_CASE(PT_Sint16, B) \
90-
TYPE_SWITCH_CASE(PT_Uint16, B) \
91-
TYPE_SWITCH_CASE(PT_Sint32, B) \
92-
TYPE_SWITCH_CASE(PT_Uint32, B) \
93-
TYPE_SWITCH_CASE(PT_Sint64, B) \
94-
TYPE_SWITCH_CASE(PT_Uint64, B) \
95-
TYPE_SWITCH_CASE(PT_Bool, B) \
96-
TYPE_SWITCH_CASE(PT_Ptr, B) \
97-
}
86+
do { \
87+
switch (Expr) { \
88+
TYPE_SWITCH_CASE(PT_Sint8, B) \
89+
TYPE_SWITCH_CASE(PT_Uint8, B) \
90+
TYPE_SWITCH_CASE(PT_Sint16, B) \
91+
TYPE_SWITCH_CASE(PT_Uint16, B) \
92+
TYPE_SWITCH_CASE(PT_Sint32, B) \
93+
TYPE_SWITCH_CASE(PT_Uint32, B) \
94+
TYPE_SWITCH_CASE(PT_Sint64, B) \
95+
TYPE_SWITCH_CASE(PT_Uint64, B) \
96+
TYPE_SWITCH_CASE(PT_Bool, B) \
97+
TYPE_SWITCH_CASE(PT_Ptr, B) \
98+
} \
99+
} while (0)
98100
#define COMPOSITE_TYPE_SWITCH(Expr, B, D) \
99-
switch (Expr) { \
100-
TYPE_SWITCH_CASE(PT_Ptr, B) \
101-
default: do { D; } while(0); break; \
102-
}
103-
#define INT_TYPE_SWITCH(Expr, B) \
104-
switch (Expr) { \
105-
TYPE_SWITCH_CASE(PT_Sint8, B) \
106-
TYPE_SWITCH_CASE(PT_Uint8, B) \
107-
TYPE_SWITCH_CASE(PT_Sint16, B) \
108-
TYPE_SWITCH_CASE(PT_Uint16, B) \
109-
TYPE_SWITCH_CASE(PT_Sint32, B) \
110-
TYPE_SWITCH_CASE(PT_Uint32, B) \
111-
TYPE_SWITCH_CASE(PT_Sint64, B) \
112-
TYPE_SWITCH_CASE(PT_Uint64, B) \
113-
default: llvm_unreachable("not an integer"); \
114-
}
101+
do { \
102+
switch (Expr) { \
103+
TYPE_SWITCH_CASE(PT_Ptr, B) \
104+
default: { D; break; } \
105+
} \
106+
} while (0)
115107
#endif

0 commit comments

Comments
 (0)