Skip to content

Commit

Permalink
Add macros for recent GCC function attributes and use in various places
Browse files Browse the repository at this point in the history
These are used by sanitizer libraries and/or static analysis tools.
  • Loading branch information
craigbarnes committed Jan 27, 2025
1 parent 43a70d9 commit 1bb4b0f
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/change.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ void end_change_chain(View *view) NONNULL_ARGS;
bool undo(View *view) NONNULL_ARGS WARN_UNUSED_RESULT;
bool redo(View *view, unsigned long change_id) NONNULL_ARGS WARN_UNUSED_RESULT;
void free_changes(Change *c) NONNULL_ARGS;
void buffer_insert_bytes(View *view, const char *buf, size_t len) NONNULL_ARG(1);
void buffer_insert_bytes(View *view, const char *buf, size_t len) NONNULL_ARG(1) NONNULL_ARG_IF_NONZERO_LENGTH(2, 3);
void buffer_delete_bytes(View *view, size_t len) NONNULL_ARGS;
void buffer_erase_bytes(View *view, size_t len) NONNULL_ARGS;
void buffer_replace_bytes(View *view, size_t del_count, const char *ins, size_t ins_count) NONNULL_ARG(1);
void buffer_replace_bytes(View *view, size_t del_count, const char *ins, size_t ins_count) NONNULL_ARG(1) NONNULL_ARG_IF_NONZERO_LENGTH(3, 4);

#endif
2 changes: 1 addition & 1 deletion src/command/macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ bool macro_cancel(MacroRecorder *m) WARN_UNUSED_RESULT NONNULL_ARGS;
void macro_command_hook(MacroRecorder *m, const char *cmd_name, char **args) NONNULL_ARGS;
void macro_search_hook(MacroRecorder *m, const char *pattern, bool reverse, bool add_to_history) NONNULL_ARG(1);
void macro_insert_char_hook(MacroRecorder *m, CodePoint c) NONNULL_ARGS;
void macro_insert_text_hook(MacroRecorder *m, const char *text, size_t size) NONNULL_ARG(1);
void macro_insert_text_hook(MacroRecorder *m, const char *text, size_t size) NONNULL_ARG(1) NONNULL_ARG_IF_NONZERO_LENGTH(2, 3);
String dump_macro(const MacroRecorder *m) WARN_UNUSED_RESULT NONNULL_ARGS;
void free_macro(MacroRecorder *m) NONNULL_ARGS;

Expand Down
2 changes: 1 addition & 1 deletion src/command/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ typedef enum {
CMDERR_UNEXPECTED_EOF,
} CommandParseError;

char *parse_command_arg(const CommandRunner *runner, const char *cmd, size_t len) NONNULL_ARG(1) RETURNS_NONNULL;
char *parse_command_arg(const CommandRunner *runner, const char *cmd, size_t len) RETURNS_NONNULL NONNULL_ARG(1) NONNULL_ARG_IF_NONZERO_LENGTH(2, 3);
size_t find_end(const char *cmd, size_t pos, CommandParseError *err) NONNULL_ARGS;
CommandParseError parse_commands(const CommandRunner *runner, PointerArray *array, const char *cmd) NONNULL_ARGS WARN_UNUSED_RESULT;
const char *command_parse_error_to_string(CommandParseError err) RETURNS_NONNULL;
Expand Down
2 changes: 1 addition & 1 deletion src/ctags.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ typedef struct {
bool local; // Indicates if tag is local to file (e.g. "static" in C)
} Tag;

NONNULL_ARGS WARN_UNUSED_RESULT
NONNULL_ARGS WARN_UNUSED_RESULT READWRITE(3) WRITEONLY(6)
bool next_tag (
const char *buf,
size_t buf_len,
Expand Down
2 changes: 1 addition & 1 deletion src/insert.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "util/unicode.h"
#include "view.h"

void insert_text(View *view, const char *text, size_t size, bool move_after) NONNULL_ARG(1);
void insert_text(View *view, const char *text, size_t size, bool move_after) NONNULL_ARG(1) NONNULL_ARG_IF_NONZERO_LENGTH(2, 3);
void insert_ch(View *view, CodePoint ch) NONNULL_ARGS;
void new_line(View *view, bool above) NONNULL_ARGS;

Expand Down
2 changes: 1 addition & 1 deletion src/join.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
#include "view.h"
#include "util/macros.h"

void join_lines(View *view, const char *delim, size_t delim_len) NONNULL_ARG(1);
void join_lines(View *view, const char *delim, size_t delim_len) NONNULL_ARG(1) NONNULL_ARG_IF_NONZERO_LENGTH(2, 3);

#endif
2 changes: 1 addition & 1 deletion src/regexp.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void regexp_compile_or_fatal_error(regex_t *re, const char *pattern, int flags)
bool regexp_init_word_boundary_tokens(RegexpWordBoundaryTokens *rwbt) NONNULL_ARGS;
bool regexp_error_msg(ErrorBuffer *ebuf, const regex_t *re, const char *pattern, int err) NONNULL_ARG(2, 3);
char *regexp_escape(const char *pattern, size_t len) NONNULL_ARGS WARN_UNUSED_RESULT;
size_t regexp_escapeb(char *buf, size_t buflen, const char *pat, size_t plen) NONNULL_ARG(1);
size_t regexp_escapeb(char *buf, size_t buflen, const char *pat, size_t plen) NONNULL_ARG(1) NONNULL_ARG_IF_NONZERO_LENGTH(3, 4);

const InternedRegexp *regexp_intern(ErrorBuffer *ebuf, const char *pattern) NONNULL_ARG(2) WARN_UNUSED_RESULT;
bool regexp_is_interned(const char *pattern) NONNULL_ARGS;
Expand Down
25 changes: 25 additions & 0 deletions src/util/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,20 @@
#define NONSTRING
#endif

// https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-nonnull_005fif_005fnonzero-function-attribute
#if HAS_ATTRIBUTE(nonnull_if_nonzero)
#define NONNULL_ARG_IF_NONZERO_LENGTH(...) __attribute__((__nonnull_if_nonzero__, __VA_ARGS__))
#else
#define NONNULL_ARG_IF_NONZERO_LENGTH(...)
#endif

// https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-null_005fterminated_005fstring_005farg-function-attribute
#if HAS_ATTRIBUTE(null_terminated_string_arg)
#define CSTR_ARG(idx) __attribute__((__null_terminated_string_arg__(idx)))
#else
#define CSTR_ARG(idx)
#endif

#if HAS_ATTRIBUTE(counted_by)
#define COUNTED_BY(member) __attribute__((counted_by(member)))
#else
Expand All @@ -295,6 +309,17 @@
#define DIAGNOSE_IF(x)
#endif

// https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-access-function-attribute
#if HAS_ATTRIBUTE(access)
#define READONLY(...) __attribute__((__access__(read_only, __VA_ARGS__))) // in param
#define WRITEONLY(...) __attribute__((__access__(write_only, __VA_ARGS__))) // out param
#define READWRITE(...) __attribute__((__access__(read_write, __VA_ARGS__))) // in-out param
#else
#define READONLY(...)
#define WRITEONLY(...)
#define READWRITE(...)
#endif

#define UNUSED_ARG(x) unused__ ## x UNUSED
#define XMALLOC MALLOC RETURNS_NONNULL
#define XSTRDUP XMALLOC NONNULL_ARGS
Expand Down
4 changes: 2 additions & 2 deletions src/util/str-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ static inline void strn_replace_byte(char *str, size_t n, char byte, char rep)

// Extract a substring between `buf + pos` and either the next `delim`
// byte (if found) or `buf + size` (the remainder of the string). The
// substring is returned as a StringView and the `posp` in-out-param
// substring is returned as a StringView and the `posp` in-out param
// is set to the offset one byte after the found delimiter (or to the
// end of the size-bounded string, if no delimiter was found).
NONNULL_ARGS
NONNULL_ARGS READONLY(1, 3) READWRITE(2)
static inline StringView get_delim(const char *buf, size_t *posp, size_t size, int delim)
{
size_t pos = *posp;
Expand Down
4 changes: 2 additions & 2 deletions src/util/string-view.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ static inline bool strview_equal_cstring_icase(const StringView *sv, const char
return strview_equal_strn_icase(sv, str, strlen(str));
}

NONNULL_ARG(1)
NONNULL_ARG(1) NONNULL_ARG_IF_NONZERO_LENGTH(2, 3)
static inline bool strview_has_strn_prefix(const StringView *sv, const char *p, size_t n)
{
return sv->length >= n && mem_equal(sv->data, p, n);
}

NONNULL_ARG(1)
NONNULL_ARG(1) NONNULL_ARG_IF_NONZERO_LENGTH(2, 3)
static inline bool strview_has_strn_suffix(const StringView *sv, const char *suf, size_t suflen)
{
// See also: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3261.pdf
Expand Down
4 changes: 2 additions & 2 deletions src/util/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ typedef struct {

#define string_append_literal(s, x) string_append_buf(s, x, STRLEN(x))

void string_append_buf(String *s, const char *ptr, size_t len) NONNULL_ARG(1);
void string_append_buf(String *s, const char *ptr, size_t len) NONNULL_ARG(1) NONNULL_ARG_IF_NONZERO_LENGTH(2, 3);

static inline String string_new(size_t size)
{
Expand Down Expand Up @@ -73,7 +73,7 @@ char *string_reserve_space(String *s, size_t more) NONNULL_ARGS_AND_RETURN;
void string_append_byte(String *s, unsigned char byte) NONNULL_ARGS;
size_t string_append_codepoint(String *s, CodePoint u) NONNULL_ARGS;
size_t string_insert_codepoint(String *s, size_t pos, CodePoint u) NONNULL_ARGS;
void string_insert_buf(String *s, size_t pos, const char *buf, size_t len) NONNULL_ARG(1);
void string_insert_buf(String *s, size_t pos, const char *buf, size_t len) NONNULL_ARG(1) NONNULL_ARG_IF_NONZERO_LENGTH(3, 4);
void string_append_memset(String *s, unsigned char byte, size_t len) NONNULL_ARGS;
void string_sprintf(String *s, const char *fmt, ...) PRINTF(2) NONNULL_ARGS;
char *string_steal_cstring(String *s) NONNULL_ARGS_AND_RETURN;
Expand Down

0 comments on commit 1bb4b0f

Please sign in to comment.