Skip to content

Commit dbc1540

Browse files
committed
Merge pull request #2081 from bettio/safe-macro
Use `do { ... } while (0)` for multi-statement macros These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents d184765 + 49c9af1 commit dbc1540

3 files changed

Lines changed: 12 additions & 20 deletions

File tree

src/libAtomVM/bif.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@
4343
#include "bifs_hash.h"
4444
#pragma GCC diagnostic pop
4545

46-
#define RAISE_ERROR(error_type_atom) \
47-
ctx->x[0] = ERROR_ATOM; \
48-
ctx->x[1] = (error_type_atom); \
49-
return term_invalid_term();
46+
#define RAISE_ERROR(error_type_atom) \
47+
do { \
48+
ctx->x[0] = ERROR_ATOM; \
49+
ctx->x[1] = (error_type_atom); \
50+
return term_invalid_term(); \
51+
} while (0);
5052

5153
#define RAISE_ERROR_BIF(fail_label, error_type_atom) \
5254
if (fail_label == 0) { \

src/libAtomVM/nifs.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ extern "C" {
4141
return term_invalid_term(); \
4242
}
4343

44-
#define RAISE_ERROR(error_type_atom) \
45-
ctx->x[0] = ERROR_ATOM; \
46-
ctx->x[1] = (error_type_atom); \
47-
return term_invalid_term();
44+
#define RAISE_ERROR(error_type_atom) \
45+
do { \
46+
ctx->x[0] = ERROR_ATOM; \
47+
ctx->x[1] = (error_type_atom); \
48+
return term_invalid_term(); \
49+
} while (0);
4850

4951
const struct Nif *nifs_get(const char *mfa);
5052

src/platforms/generic_unix/lib/platform_nifs.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,6 @@
3737
// #define ENABLE_TRACE
3838
#include "trace.h"
3939

40-
#define VALIDATE_VALUE(value, verify_function) \
41-
if (UNLIKELY(!verify_function((value)))) { \
42-
argv[0] = ERROR_ATOM; \
43-
argv[1] = BADARG_ATOM; \
44-
return term_invalid_term(); \
45-
}
46-
47-
#define RAISE_ERROR(error_type_atom) \
48-
ctx->x[0] = ERROR_ATOM; \
49-
ctx->x[1] = (error_type_atom); \
50-
return term_invalid_term();
51-
5240
#if ATOMVM_HAS_MBEDTLS
5341

5442
// declared in otp_crypt

0 commit comments

Comments
 (0)