At Thu, 19 Sep 2019 10:07:40 +0900, Michael Paquier <mich...@paquier.xyz> wrote in <20190919010740.gc22...@paquier.xyz> > On Wed, Sep 18, 2019 at 04:46:30PM +0100, Dagfinn Ilmari Mannsåker wrote: > > Postgres doesn't seem to have it, but it would be possible to define a > > StaticAssertDecl macro that can be used at the file level, outside any > > function. See for example Perl's STATIC_ASSERT_DECL: > > > > https://github.com/Perl/perl5/blob/v5.30.0/perl.h#L3455-L3488 > > That sounds like a cleaner alternative. Thanks for the pointer.
The cause for StaticAssertStmt not being usable outside of functions is enclosing do-while, which is needed to avoid "mixed declaration" warnings, which we are inhibiting to use as of now. Therefore just defining another macro defined as just _Static_assert() works fine. I don't find an alternative way for the tool chains that don't have static assertion feature. In the attached diff the macro is defined as nothing. I don't find a way to warn that the assertion is ignored. regards.
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 90ffd89339..822b9846bf 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -4601,7 +4601,6 @@ static void write_auto_conf_file(int fd, const char *filename, ConfigVariable *h static void replace_auto_config_value(ConfigVariable **head_p, ConfigVariable **tail_p, const char *name, const char *value); - /* * Some infrastructure for checking malloc/strdup/realloc calls */ diff --git a/src/include/c.h b/src/include/c.h index f461628a24..a386a81a19 100644 --- a/src/include/c.h +++ b/src/include/c.h @@ -836,11 +836,14 @@ extern void ExceptionalCondition(const char *conditionName, #ifdef HAVE__STATIC_ASSERT #define StaticAssertStmt(condition, errmessage) \ do { _Static_assert(condition, errmessage); } while(0) +#define StaticAssertDecl(condition, errmessage) \ + _Static_assert(condition, errmessage) #define StaticAssertExpr(condition, errmessage) \ ((void) ({ StaticAssertStmt(condition, errmessage); true; })) #else /* !HAVE__STATIC_ASSERT */ #define StaticAssertStmt(condition, errmessage) \ ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; })) +#define StaticAssertDecl(condition, errmessage) /* no alternatives exist */ #define StaticAssertExpr(condition, errmessage) \ StaticAssertStmt(condition, errmessage) #endif /* HAVE__STATIC_ASSERT */ @@ -848,11 +851,14 @@ extern void ExceptionalCondition(const char *conditionName, #if defined(__cpp_static_assert) && __cpp_static_assert >= 200410 #define StaticAssertStmt(condition, errmessage) \ static_assert(condition, errmessage) +#define StaticAssertDecl(condition, errmessage) \ + static_assert(condition, errmessage) #define StaticAssertExpr(condition, errmessage) \ ({ static_assert(condition, errmessage); }) #else #define StaticAssertStmt(condition, errmessage) \ do { struct static_assert_struct { int static_assert_failure : (condition) ? 1 : -1; }; } while(0) +#define StaticAssertDecl(condition, errmessage) /* no alternatives exist */ #define StaticAssertExpr(condition, errmessage) \ ((void) ({ StaticAssertStmt(condition, errmessage); })) #endif