clang does not have the attributes __warning__ and __error__. But it has the attribute __diagnose_if__, that is a generalization of both. See <https://clang.llvm.org/docs/AttributeReference.html>.
2020-08-09 Bruno Haible <[email protected]> Add ability to emit user-defined warnings and errors with clang. * m4/gnulib-common.m4 (gl_COMMON_BODY): Define _GL_ATTRIBUTE_ERROR, _GL_ATTRIBUTE_WARNING using an attribute for clang. * lib/warn-on-use.h (_GL_WARN_ON_USE, _GL_WARN_ON_USE_ATTRIBUTE, _GL_WARN_ON_USE_CXX): Define using an attribute for clang. * lib/cdefs.h (__warndecl, __warnattr, __errordecl): Define using an attribute for clang. diff --git a/lib/cdefs.h b/lib/cdefs.h index b034c0b..801753c 100644 --- a/lib/cdefs.h +++ b/lib/cdefs.h @@ -129,6 +129,12 @@ # define __warnattr(msg) __attribute__((__warning__ (msg))) # define __errordecl(name, msg) \ extern void name (void) __attribute__((__error__ (msg))) +#elif __clang_major__ >= 4 +# define __warndecl(name, msg) \ + extern void name (void) __attribute__((__diagnose_if__ (1, msg, "warning"))) +# define __warnattr(msg) __attribute__((__diagnose_if__ (1, msg, "warning"))) +# define __errordecl(name, msg) \ + extern void name (void) __attribute__((__diagnose_if__ (1, msg, "error"))) #else # define __warndecl(name, msg) extern void name (void) # define __warnattr(msg) diff --git a/lib/warn-on-use.h b/lib/warn-on-use.h index 291e709..a39e3a3 100644 --- a/lib/warn-on-use.h +++ b/lib/warn-on-use.h @@ -87,6 +87,13 @@ extern __typeof__ (function) function __attribute__ ((__warning__ (message))) # define _GL_WARN_ON_USE_ATTRIBUTE(message) \ __attribute__ ((__warning__ (message))) +# elif __clang_major__ >= 4 +/* Another compiler attribute is available in clang. */ +# define _GL_WARN_ON_USE(function, message) \ +extern __typeof__ (function) function \ + __attribute__ ((__diagnose_if__ (1, message, "warning"))) +# define _GL_WARN_ON_USE_ATTRIBUTE(message) \ + __attribute__ ((__diagnose_if__ (1, message, "warning"))) # elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING /* Verify the existence of the function. */ # define _GL_WARN_ON_USE(function, message) \ @@ -111,9 +118,15 @@ _GL_WARN_EXTERN_C int _gl_warn_on_use _GL_WARN_ON_USE (function, msg) # else # if 4 < __GNUC__ || (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) +/* A compiler attribute is available in gcc versions 4.3.0 and later. */ +# define _GL_WARN_ON_USE_CXX(function,rettype,parameters_and_attributes,msg) \ +extern rettype function parameters_and_attributes \ + __attribute__ ((__warning__ (msg))) +# elif __clang_major__ >= 4 +/* Another compiler attribute is available in clang. */ # define _GL_WARN_ON_USE_CXX(function,rettype,parameters_and_attributes,msg) \ extern rettype function parameters_and_attributes \ - __attribute__ ((__warning__ (msg))) + __attribute__ ((__diagnose_if__ (1, msg, "warning"))) # elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING /* Verify the existence of the function. */ # define _GL_WARN_ON_USE_CXX(function,rettype,parameters_and_attributes,msg) \ diff --git a/m4/gnulib-common.m4 b/m4/gnulib-common.m4 index 50acc0a..e42b80c 100644 --- a/m4/gnulib-common.m4 +++ b/m4/gnulib-common.m4 @@ -1,4 +1,4 @@ -# gnulib-common.m4 serial 53 +# gnulib-common.m4 serial 54 dnl Copyright (C) 2007-2020 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -76,6 +76,7 @@ AC_DEFUN([gl_COMMON_BODY], [ # define _GL_ATTR_cold _GL_GNUC_PREREQ (4, 3) # define _GL_ATTR_const _GL_GNUC_PREREQ (2, 95) # define _GL_ATTR_deprecated _GL_GNUC_PREREQ (3, 1) +# define _GL_ATTR_diagnose_if 0 # define _GL_ATTR_error _GL_GNUC_PREREQ (4, 3) # define _GL_ATTR_externally_visible _GL_GNUC_PREREQ (4, 1) # define _GL_ATTR_fallthrough _GL_GNUC_PREREQ (7, 0) @@ -149,6 +150,9 @@ AC_DEFUN([gl_COMMON_BODY], [ #if _GL_HAS_ATTRIBUTE (error) # define _GL_ATTRIBUTE_ERROR(msg) __attribute__ ((__error__ (msg))) # define _GL_ATTRIBUTE_WARNING(msg) __attribute__ ((__warning__ (msg))) +#elif _GL_HAS_ATTRIBUTE (diagnose_if) +# define _GL_ATTRIBUTE_ERROR(msg) __attribute__ ((__diagnose_if__ (1, msg, "error"))) +# define _GL_ATTRIBUTE_WARNING(msg) __attribute__ ((__diagnose_if__ (1, msg, "warning"))) #else # define _GL_ATTRIBUTE_ERROR(msg) # define _GL_ATTRIBUTE_WARNING(msg)
