]> Savannah Git Hosting - gnulib.git/commitdiff
string-buffer, string-buffer-reversed: Allow for better static analysis.
authorBruno Haible <bruno@clisp.org>
Sun, 9 Feb 2025 21:02:27 +0000 (22:02 +0100)
committerBruno Haible <bruno@clisp.org>
Sun, 9 Feb 2025 21:02:27 +0000 (22:02 +0100)
* 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.

ChangeLog
lib/string-buffer-reversed.h
lib/string-buffer.h

index 1ab751f289afc4672d26e2c1f3cf5e2d74666e49..77d52ffe384de913c6d345dab2b31af25660c4f4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2025-02-09  Bruno Haible  <bruno@clisp.org>
+
+       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  <bruno@clisp.org>
 
        xstring-desc: Allow for better static analysis.
index 4915aa529789946d5bc7848a992341b21742323d..021508c2e9e389612acd730605b3998d4fc0753a 100644 (file)
 
 /* Written by Bruno Haible <bruno@clisp.org>, 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 */
index 19b54ea6e7b10cf47d6b4f0b5c02048e0562606c..eefdd195d7f961cf0611394357c02fee1c707268 100644 (file)
 
 /* Written by Bruno Haible <bruno@clisp.org>, 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 */