From: Bruno Haible Date: Sun, 9 Feb 2025 21:02:27 +0000 (+0100) Subject: string-buffer, string-buffer-reversed: Allow for better static analysis. X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=6719cb7110ea4e6aad72d115740560007dd6b882;p=gnulib.git string-buffer, string-buffer-reversed: Allow for better static analysis. * lib/string-buffer.h (sb_appendvf, sb_appendf, sb_xappendvf, sb_xappendf): Don't declare if SB_NO_APPENDF is defined. (sb_xdupfree_c): Declare a non-NULL return value if SB_NO_APPENDF is defined. * lib/string-buffer-reversed.h (sbr_prependvf, sbr_prependf, sbr_xprependvf, sbr_xprependf): Don't declare if SBR_NO_PREPENDF is defined. (sbr_xdupfree_c): Declare a non-NULL return value if SBR_NO_PREPENDF is defined. --- diff --git a/ChangeLog b/ChangeLog index 1ab751f289..77d52ffe38 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2025-02-09 Bruno Haible + + string-buffer, string-buffer-reversed: Allow for better static analysis. + * lib/string-buffer.h (sb_appendvf, sb_appendf, sb_xappendvf, + sb_xappendf): Don't declare if SB_NO_APPENDF is defined. + (sb_xdupfree_c): Declare a non-NULL return value if SB_NO_APPENDF is + defined. + * lib/string-buffer-reversed.h (sbr_prependvf, sbr_prependf, + sbr_xprependvf, sbr_xprependf): Don't declare if SBR_NO_PREPENDF is + defined. + (sbr_xdupfree_c): Declare a non-NULL return value if SBR_NO_PREPENDF is + defined. + 2025-02-09 Bruno Haible xstring-desc: Allow for better static analysis. diff --git a/lib/string-buffer-reversed.h b/lib/string-buffer-reversed.h index 4915aa5297..021508c2e9 100644 --- a/lib/string-buffer-reversed.h +++ b/lib/string-buffer-reversed.h @@ -17,6 +17,13 @@ /* Written by Bruno Haible , 2025. */ +/* Before including this file, you may define: + SBR_NO_PREPENDF Provides for more efficient code, assuming that + sbr_prependvf and sbr_prependf will not be called + and that 'struct string_buffer_reversed' instances + are not used across different compilation units. + */ + #ifndef _STRING_BUFFER_REVERSED_H #define _STRING_BUFFER_REVERSED_H @@ -72,6 +79,8 @@ extern int sbr_prepend_desc (struct string_buffer_reversed *buffer, extern int sbr_prepend_c (struct string_buffer_reversed *buffer, const char *str); +#ifndef SBR_NO_PREPENDF + /* Prepends the result of the printf-compatible FORMATSTRING with the argument list LIST to BUFFER. Returns 0, or -1 with errno set in case of error. @@ -102,6 +111,8 @@ extern int sbr_prependf (struct string_buffer_reversed *buffer, #endif ; +#endif + /* Frees the memory held by BUFFER. */ extern void sbr_free (struct string_buffer_reversed *buffer) _GL_ATTRIBUTE_RELEASE_CAPABILITY (buffer->data); @@ -148,6 +159,8 @@ extern void sbr_xprepend_desc (struct string_buffer_reversed *buffer, extern void sbr_xprepend_c (struct string_buffer_reversed *buffer, const char *str); +# ifndef SBR_NO_PREPENDF + /* Prepends the result of the printf-compatible FORMATSTRING with the argument list LIST to BUFFER. Returns 0, or -1 in case of error other than out-of-memory error. @@ -178,6 +191,8 @@ extern int sbr_xprependf (struct string_buffer_reversed *buffer, #endif ; +# endif + /* Returns the contents of BUFFER and frees all other memory held by BUFFER. Returns (0, NULL) if there was an error earlier. It is the responsibility of the caller to sd_free() the result. */ @@ -190,6 +205,9 @@ extern string_desc_t sbr_xdupfree (struct string_buffer_reversed *buffer) It is the responsibility of the caller to free() the result. */ extern char * sbr_xdupfree_c (struct string_buffer_reversed *buffer) _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE +# ifdef SBR_NO_PREPENDF + _GL_ATTRIBUTE_RETURNS_NONNULL /* because buffer->error is always false */ +# endif _GL_ATTRIBUTE_RELEASE_CAPABILITY (buffer->data); #endif /* GNULIB_XSTRING_BUFFER_REVERSED */ diff --git a/lib/string-buffer.h b/lib/string-buffer.h index 19b54ea6e7..eefdd195d7 100644 --- a/lib/string-buffer.h +++ b/lib/string-buffer.h @@ -16,6 +16,13 @@ /* Written by Bruno Haible , 2021. */ +/* Before including this file, you may define: + SB_NO_APPENDF Provides for more efficient code, assuming that + sb_appendvf and sb_appendf will not be called and + that 'struct string_buffer' instances are not used + across different compilation units. + */ + #ifndef _STRING_BUFFER_H #define _STRING_BUFFER_H @@ -69,6 +76,8 @@ extern int sb_append_desc (struct string_buffer *buffer, string_desc_t s); Returns 0, or -1 in case of out-of-memory error. */ extern int sb_append_c (struct string_buffer *buffer, const char *str); +#ifndef SB_NO_APPENDF + /* Appends the result of the printf-compatible FORMATSTRING with the argument list LIST to BUFFER. Returns 0, or -1 with errno set in case of error. @@ -99,6 +108,8 @@ extern int sb_appendf (struct string_buffer *buffer, #endif ; +#endif + /* Frees the memory held by BUFFER. */ extern void sb_free (struct string_buffer *buffer) _GL_ATTRIBUTE_RELEASE_CAPABILITY (buffer->data); @@ -144,6 +155,8 @@ extern void sb_xappend_desc (struct string_buffer *buffer, string_desc_t s); /* Appends the contents of the C string STR to BUFFER. */ extern void sb_xappend_c (struct string_buffer *buffer, const char *str); +# ifndef SB_NO_APPENDF + /* Appends the result of the printf-compatible FORMATSTRING with the argument list LIST to BUFFER. Returns 0, or -1 in case of error other than out-of-memory error. @@ -174,6 +187,8 @@ extern int sb_xappendf (struct string_buffer *buffer, #endif ; +# endif + /* Ensures the contents of BUFFER is followed by a NUL byte (without incrementing the length of the contents). Then returns a read-only view of the current contents of BUFFER, @@ -194,6 +209,9 @@ extern string_desc_t sb_xdupfree (struct string_buffer *buffer) It is the responsibility of the caller to free() the result. */ extern char * sb_xdupfree_c (struct string_buffer *buffer) _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE +# ifdef SB_NO_APPENDF + _GL_ATTRIBUTE_RETURNS_NONNULL /* because buffer->error is always false */ +# endif _GL_ATTRIBUTE_RELEASE_CAPABILITY (buffer->data); #endif /* GNULIB_XSTRING_BUFFER */