This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Apply attribute returns_nonnull in libiberty


On Fri, 11 Oct 2013, DJ Delorie wrote:

Your patch changes the rule that the application can override
xmalloc() and get functions that return NULL...

How about this more limited version? I'll see about following Jakub's advice to apply the attributes to the other functions only in gcc.

2013-10-11  Marc Glisse  <marc.glisse@inria.fr>

	PR tree-optimization/58689
	* ansidecl.h (ATTRIBUTE_RETURNS_NONNULL): New macro.
	* libiberty.h (basename, lbasename, dos_lbasename, unix_lbasename,
	concat_copy): Mark with attributes nonnull(1) and returns_nonnull.
	(concat_copy2, xstrerror): Mark with attribute returns_nonnull.

--
Marc Glisse
Index: ansidecl.h
===================================================================
--- ansidecl.h	(revision 203451)
+++ ansidecl.h	(working copy)
@@ -304,20 +304,29 @@ So instead we use the macro below and te
 
 /* Attribute `nonnull' was valid as of gcc 3.3.  */
 #ifndef ATTRIBUTE_NONNULL
 # if (GCC_VERSION >= 3003)
 #  define ATTRIBUTE_NONNULL(m) __attribute__ ((__nonnull__ (m)))
 # else
 #  define ATTRIBUTE_NONNULL(m)
 # endif /* GNUC >= 3.3 */
 #endif /* ATTRIBUTE_NONNULL */
 
+/* Attribute `returns_nonnull' was valid as of gcc 4.9.  */
+#ifndef ATTRIBUTE_RETURNS_NONNULL
+# if (GCC_VERSION >= 4009)
+#  define ATTRIBUTE_RETURNS_NONNULL __attribute__ ((__returns_nonnull__))
+# else
+#  define ATTRIBUTE_RETURNS_NONNULL
+# endif /* GNUC >= 4.9 */
+#endif /* ATTRIBUTE_RETURNS_NONNULL */
+
 /* Attribute `pure' was valid as of gcc 3.0.  */
 #ifndef ATTRIBUTE_PURE
 # if (GCC_VERSION >= 3000)
 #  define ATTRIBUTE_PURE __attribute__ ((__pure__))
 # else
 #  define ATTRIBUTE_PURE
 # endif /* GNUC >= 3.0 */
 #endif /* ATTRIBUTE_PURE */
 
 /* Use ATTRIBUTE_PRINTF when the format specifier must not be NULL.
Index: libiberty.h
===================================================================
--- libiberty.h	(revision 203451)
+++ libiberty.h	(working copy)
@@ -100,43 +100,43 @@ extern int countargv (char**);
    across different systems, sometimes as "char *" and sometimes as
    "const char *" */
 
 /* HAVE_DECL_* is a three-state macro: undefined, 0 or 1.  If it is
    undefined, we haven't run the autoconf check so provide the
    declaration without arguments.  If it is 0, we checked and failed
    to find the declaration so provide a fully prototyped one.  If it
    is 1, we found it so don't provide any declaration at all.  */
 #if !HAVE_DECL_BASENAME
 #if defined (__GNU_LIBRARY__ ) || defined (__linux__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__) || defined (__CYGWIN__) || defined (__CYGWIN32__) || defined (__MINGW32__) || defined (HAVE_DECL_BASENAME)
-extern char *basename (const char *);
+extern char *basename (const char *) ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURNS_NONNULL;
 #else
 /* Do not allow basename to be used if there is no prototype seen.  We
    either need to use the above prototype or have one from
    autoconf which would result in HAVE_DECL_BASENAME being set.  */
 #define basename basename_cannot_be_used_without_a_prototype
 #endif
 #endif
 
 /* A well-defined basename () that is always compiled in.  */
 
-extern const char *lbasename (const char *);
+extern const char *lbasename (const char *) ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURNS_NONNULL;
 
 /* Same, but assumes DOS semantics (drive name, backslash is also a
    dir separator) regardless of host.  */
 
-extern const char *dos_lbasename (const char *);
+extern const char *dos_lbasename (const char *) ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURNS_NONNULL;
 
 /* Same, but assumes Unix semantics (absolute paths always start with
    a slash, only forward slash is accepted as dir separator)
    regardless of host.  */
 
-extern const char *unix_lbasename (const char *);
+extern const char *unix_lbasename (const char *) ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURNS_NONNULL;
 
 /* A well-defined realpath () that is always compiled in.  */
 
 extern char *lrealpath (const char *);
 
 /* Concatenate an arbitrary number of strings.  You must pass NULL as
    the last argument of this function, to terminate the list of
    strings.  Allocates memory using xmalloc.  */
 
 extern char *concat (const char *, ...) ATTRIBUTE_MALLOC ATTRIBUTE_SENTINEL;
@@ -154,28 +154,28 @@ extern char *reconcat (char *, const cha
    strings.  You must pass NULL as the last argument of this function,
    to terminate the list of strings.  */
 
 extern unsigned long concat_length (const char *, ...) ATTRIBUTE_SENTINEL;
 
 /* Concatenate an arbitrary number of strings into a SUPPLIED area of
    memory.  You must pass NULL as the last argument of this function,
    to terminate the list of strings.  The supplied memory is assumed
    to be large enough.  */
 
-extern char *concat_copy (char *, const char *, ...) ATTRIBUTE_SENTINEL;
+extern char *concat_copy (char *, const char *, ...) ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_SENTINEL;
 
 /* Concatenate an arbitrary number of strings into a GLOBAL area of
    memory.  You must pass NULL as the last argument of this function,
    to terminate the list of strings.  The supplied memory is assumed
    to be large enough.  */
 
-extern char *concat_copy2 (const char *, ...) ATTRIBUTE_SENTINEL;
+extern char *concat_copy2 (const char *, ...) ATTRIBUTE_RETURNS_NONNULL ATTRIBUTE_SENTINEL;
 
 /* This is the global area used by concat_copy2.  */
 
 extern char *libiberty_concat_ptr;
 
 /* Concatenate an arbitrary number of strings.  You must pass NULL as
    the last argument of this function, to terminate the list of
    strings.  Allocates memory using alloca.  The arguments are
    evaluated twice!  */
 #define ACONCAT(ACONCAT_PARAMS) \
@@ -249,21 +249,21 @@ extern int errno_max (void);
    "EINVAL").  */
 
 extern const char *strerrno (int);
 
 /* Given the name of an errno value, return the value.  */
 
 extern int strtoerrno (const char *);
 
 /* ANSI's strerror(), but more robust.  */
 
-extern char *xstrerror (int);
+extern char *xstrerror (int) ATTRIBUTE_RETURNS_NONNULL;
 
 /* Return the maximum signal number for which strsignal will return a
    string.  */
 
 extern int signo_max (void);
 
 /* Return a signal message string for a signal number
    (e.g., strsignal (SIGHUP) returns something like "Hangup").  */
 /* This is commented out as it can conflict with one in system headers.
    We still document its existence though.  */

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]