authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2020-04-01 12:47:50-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-01 15:56:00-04:00
log212e2354b8ab631995b0c25f7cf1d9a3e01fac57
treeb5d42acd5308378b995bc97b191c8a4efd240a43
parent0f1f56bb69610ea424ac311db72510b474249095

stage1: make C++ switch fallthrough an error

Make fallthrough an error when compiler supports it. This requires a new macro that is defined with such compilers to be used as a statement, at all fallthrough sites: switch (...) { case 0: ... ZIG_FALLTHROUGH; case 1: ... break; default: ... break; } If we ever move to C++17 as minimal requirement, then the macro can be replaced with `[[fallthrough]];` at statement sites.

8 files changed, 28 insertions(+), 7 deletions(-)

CMakeLists.txt+1-1
......@@ -325,7 +325,7 @@ if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
325325 if(MSVC)
326326 set(EXE_CFLAGS "${EXE_CFLAGS} /w")
327327 else()
328 set(EXE_CFLAGS "${EXE_CFLAGS} -Werror -Wall")
328 set(EXE_CFLAGS "${EXE_CFLAGS} -Werror -Wall -Werror=implicit-fallthrough")
329329 endif()
330330endif()
331331
src/analyze.cpp+2
......@@ -363,6 +363,7 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
363363 case ResolveStatusLLVMFull:
364364 return type_entry->llvm_type != nullptr;
365365 }
366 zig_unreachable();
366367 case ZigTypeIdOpaque:
367368 return status < ResolveStatusSizeKnown;
368369 case ZigTypeIdPointer:
......@@ -381,6 +382,7 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
381382 case ResolveStatusLLVMFull:
382383 return type_entry->llvm_type != nullptr;
383384 }
385 zig_unreachable();
384386 case ZigTypeIdMetaType:
385387 case ZigTypeIdVoid:
386388 case ZigTypeIdBool:
src/codegen.cpp+2-1
......@@ -3954,8 +3954,9 @@ static void render_async_var_decls(CodeGen *g, Scope *scope) {
39543954 if (var->did_the_decl_codegen) {
39553955 render_decl_var(g, var);
39563956 }
3957 // fallthrough
39583957 }
3958 ZIG_FALLTHROUGH;
3959
39593960 case ScopeIdDecls:
39603961 case ScopeIdBlock:
39613962 case ScopeIdDefer:
src/dump_analysis.cpp+2-2
......@@ -80,7 +80,7 @@ static void jw_array_elem(JsonWriter *jw) {
8080 zig_unreachable();
8181 case JsonWriterStateArray:
8282 fprintf(jw->f, ",");
83 // fallthrough
83 ZIG_FALLTHROUGH;
8484 case JsonWriterStateArrayStart:
8585 jw->state[jw->state_index] = JsonWriterStateArray;
8686 jw_push_state(jw, JsonWriterStateValue);
......@@ -134,7 +134,7 @@ static void jw_object_field(JsonWriter *jw, const char *name) {
134134 zig_unreachable();
135135 case JsonWriterStateObject:
136136 fprintf(jw->f, ",");
137 // fallthrough
137 ZIG_FALLTHROUGH;
138138 case JsonWriterStateObjectStart:
139139 jw->state[jw->state_index] = JsonWriterStateObject;
140140 jw_push_state(jw, JsonWriterStateValue);
src/ir.cpp+2
......@@ -19949,6 +19949,7 @@ static IrInstGen *ir_analyze_call_extra(IrAnalyze *ira, IrInst* source_instr,
1994919949 buf_sprintf("the specified modifier requires a comptime-known function"));
1995019950 return ira->codegen->invalid_inst_gen;
1995119951 }
19952 ZIG_FALLTHROUGH;
1995219953 default:
1995319954 break;
1995419955 }
......@@ -28169,6 +28170,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ZigValue *val)
2816928170 return;
2817028171 }
2817128172 }
28173 zig_unreachable();
2817228174 case ZigTypeIdOptional:
2817328175 zig_panic("TODO buf_write_value_bytes maybe type");
2817428176 case ZigTypeIdFn:
src/target.cpp+5
......@@ -624,6 +624,7 @@ uint32_t target_c_type_size_in_bits(const ZigTarget *target, CIntType id) {
624624 case CIntTypeCount:
625625 zig_unreachable();
626626 }
627 zig_unreachable();
627628 default:
628629 switch (id) {
629630 case CIntTypeShort:
......@@ -642,6 +643,7 @@ uint32_t target_c_type_size_in_bits(const ZigTarget *target, CIntType id) {
642643 zig_unreachable();
643644 }
644645 }
646 zig_unreachable();
645647 case OsLinux:
646648 case OsMacOSX:
647649 case OsFreeBSD:
......@@ -666,6 +668,7 @@ uint32_t target_c_type_size_in_bits(const ZigTarget *target, CIntType id) {
666668 case CIntTypeCount:
667669 zig_unreachable();
668670 }
671 zig_unreachable();
669672 case OsUefi:
670673 case OsWindows:
671674 switch (id) {
......@@ -683,6 +686,7 @@ uint32_t target_c_type_size_in_bits(const ZigTarget *target, CIntType id) {
683686 case CIntTypeCount:
684687 zig_unreachable();
685688 }
689 zig_unreachable();
686690 case OsIOS:
687691 switch (id) {
688692 case CIntTypeShort:
......@@ -699,6 +703,7 @@ uint32_t target_c_type_size_in_bits(const ZigTarget *target, CIntType id) {
699703 case CIntTypeCount:
700704 zig_unreachable();
701705 }
706 zig_unreachable();
702707 case OsAnanas:
703708 case OsCloudABI:
704709 case OsKFreeBSD:
src/tokenizer.cpp+4-3
......@@ -840,6 +840,7 @@ void tokenize(Buf *buf, Tokenization *out) {
840840 t.state = TokenizeStateStart;
841841 continue;
842842 }
843 break;
843844 case TokenizeStateSawSlash:
844845 switch (c) {
845846 case '/':
......@@ -1209,7 +1210,7 @@ void tokenize(Buf *buf, Tokenization *out) {
12091210 t.is_trailing_underscore = false;
12101211 t.state = TokenizeStateNumber;
12111212 }
1212 // fall through
1213 ZIG_FALLTHROUGH;
12131214 case TokenizeStateNumber:
12141215 {
12151216 if (c == '_') {
......@@ -1291,7 +1292,7 @@ void tokenize(Buf *buf, Tokenization *out) {
12911292 t.is_trailing_underscore = false;
12921293 t.state = TokenizeStateFloatFraction;
12931294 }
1294 // fall through
1295 ZIG_FALLTHROUGH;
12951296 case TokenizeStateFloatFraction:
12961297 {
12971298 if (c == '_') {
......@@ -1350,7 +1351,7 @@ void tokenize(Buf *buf, Tokenization *out) {
13501351 t.is_trailing_underscore = false;
13511352 t.state = TokenizeStateFloatExponentNumber;
13521353 }
1353 // fall through
1354 ZIG_FALLTHROUGH;
13541355 case TokenizeStateFloatExponentNumber:
13551356 {
13561357 if (c == '_') {
src/util_base.hpp+10
......@@ -64,4 +64,14 @@ static inline void zig_assert(bool ok, const char *file, int line, const char *f
6464#undef assert
6565#define assert(ok) zig_assert(ok, __FILE__, __LINE__, __func__)
6666
67#if defined(_MSC_VER)
68#define ZIG_FALLTHROUGH
69#elif defined(__clang__)
70#define ZIG_FALLTHROUGH [[clang::fallthrough]]
71#elif defined(__GNUC__)
72#define ZIG_FALLTHROUGH __attribute__((fallthrough))
73#else
74#define ZIG_FALLTHROUGH
75#endif
76
6777#endif