From: Bruno Haible Date: Sun, 9 Aug 2020 12:51:56 +0000 (+0200) Subject: Add ability to emit user-defined warnings and errors with clang. X-Git-Tag: v1.0~3799 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=94e462dba2d39c73858d855d528f2d86253de498;p=gnulib.git 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/ChangeLog b/ChangeLog index 18de16e592..e1c48b2153 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2020-08-09 Bruno Haible + + 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. + 2020-08-09 Bruno Haible intprops: Fix typo in comment. diff --git a/lib/cdefs.h b/lib/cdefs.h index b034c0bdfd..801753c7f3 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 291e7091b8..a39e3a394c 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 50acc0a474..e42b80cf61 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)