[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 2/2] maint: use "const" and "pure" function attributes where poss
From: |
Jim Meyering |
Subject: |
[PATCH 2/2] maint: use "const" and "pure" function attributes where possible |
Date: |
Sun, 26 Jun 2011 17:06:22 +0200 |
Following up on a month-old gnulib thread, and now
that gnulib provides _GL_ATTRIBUTE_CONST and _GL_ATTRIBUTE_PURE, ...
>From 4c45a7a71190231b542a90bc8a179ee17f1fe329 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Sat, 4 Jun 2011 10:41:08 +0200
Subject: [PATCH 1/2] buffer_lcm: declare with _GL_ATTRIBUTE_CONST
* lib/buffer-lcm.h (buffer_lcm): Use _GL_ATTRIBUTE_CONST.
* lib/buffer-lcm.c: Include <config.h>.
---
lib/buffer-lcm.c | 1 +
lib/buffer-lcm.h | 2 +-
2 files changed, 2 insertions(+), 1 deletions(-)
diff --git a/lib/buffer-lcm.c b/lib/buffer-lcm.c
index 022a731..87503af 100644
--- a/lib/buffer-lcm.c
+++ b/lib/buffer-lcm.c
@@ -17,6 +17,7 @@
/* Written by Paul Eggert. */
+#include <config.h>
#include "buffer-lcm.h"
/* Return a buffer size suitable for doing I/O with files whose block
diff --git a/lib/buffer-lcm.h b/lib/buffer-lcm.h
index 9a6cdf5..454c65b 100644
--- a/lib/buffer-lcm.h
+++ b/lib/buffer-lcm.h
@@ -1,2 +1,2 @@
#include <stddef.h>
-size_t buffer_lcm (size_t, size_t, size_t);
+size_t buffer_lcm (size_t, size_t, size_t) _GL_ATTRIBUTE_CONST;
--
1.7.6.rc2.302.gc2115
>From 16af8b95c0dc985e072ef6c22267c00c21637588 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Sun, 24 Apr 2011 19:06:39 +0200
Subject: [PATCH 2/2] maint: use "const" and "pure" function attributes where
possible
* configure.ac (WARN_CFLAGS): Add -Wsuggest-attribute=const,
-Wsuggest-attribute=pure and -Wsuggest-attribute=noreturn.
(GNULIB_WARN_CFLAGS): But do not add them here... yet.
* src/chown-core.h (chopt_free, uid_to_name): Add function attribute(s).
* src/copy.c (is_ancestor, valid_options): Likewise.
* src/copy.h (chown_failure_ok): Likewise.
* src/dd.c (operand_matches, operand_is): Likewise.
* src/df.c (selected_fstype, excluded_fstype): Likewise.
* src/expr.c (null looks_like_integer): Likewise.
* src/md5sum.c (hex_digits): Likewise.
* src/od.c (get_lcm): Likewise.
* src/pathchk.c (component_start, component_len): Likewise.
* src/pinky.c (count_ampersands): Likewise.
* src/pr.c (cols_ready_to_print): Likewise.
* src/ptx.c (search_table): Likewise.
* src/sort.c (find_unit_order): Likewise.
* src/stty.c (mode_type_flag, string_to_baud, baud_to_value): Likewise.
* src/system.h (gcd, lcm): Likewise.
* src/tr.c (is_char_class_member, look_up_char_class): Likewise.
(star_digits_closebracket): Likewise.
* src/uniq.c (find_field): Likewise.
* src/wc.c (compute_number_width): Likewise.
* lib/xfts.h (cycle_warning_required): Likewise.
* gl/lib/randint.h (randint_get_source): Likewise.
* gl/lib/randperm.c (ceil_lg): Likewise.
* gl/lib/randperm.h (randperm_bound): Likewise.
* lib/strnumcmp.h (strintcmp): Likewise.
---
configure.ac | 6 ++++++
gl/lib/randint.h | 3 ++-
gl/lib/randperm.c | 2 +-
gl/lib/randperm.h | 2 +-
gl/lib/tempname.c.diff | 2 +-
lib/strnumcmp-in.h | 4 ++--
lib/strnumcmp.c | 2 +-
lib/strnumcmp.h | 2 +-
lib/xfts.h | 3 ++-
src/chown-core.h | 4 ++--
src/copy.c | 4 ++--
src/copy.h | 2 +-
src/dd.c | 4 ++--
src/df.c | 4 ++--
src/expr.c | 4 ++--
src/md5sum.c | 2 +-
src/od.c | 2 +-
src/pathchk.c | 4 ++--
src/pinky.c | 2 +-
src/pr.c | 2 +-
src/ptx.c | 2 +-
src/sort.c | 2 +-
src/stty.c | 6 +++---
src/system.h | 4 ++--
src/tr.c | 6 +++---
src/uniq.c | 2 +-
src/wc.c | 2 +-
27 files changed, 46 insertions(+), 38 deletions(-)
diff --git a/configure.ac b/configure.ac
index b6a1007..41b2f00 100644
--- a/configure.ac
+++ b/configure.ac
@@ -101,6 +101,9 @@ if test "$gl_gcc_warnings" = yes; then
done
gl_WARN_ADD([-Wno-sign-compare]) # Too many warnings for now
gl_WARN_ADD([-Wno-unused-parameter]) # Too many warnings for now
+ gl_WARN_ADD([-Wsuggest-attribute=const])
+ gl_WARN_ADD([-Wsuggest-attribute=pure])
+ gl_WARN_ADD([-Wsuggest-attribute=noreturn])
# In spite of excluding -Wlogical-op above, it is enabled, as of
# gcc 4.5.0 20090517, and it provokes warnings in cat.c, dd.c, truncate.c
@@ -124,6 +127,9 @@ if test "$gl_gcc_warnings" = yes; then
nw="$nw -Wunused-macros"
nw="$nw -Wmissing-prototypes"
nw="$nw -Wold-style-definition"
+ # FIXME: remove/reenable the following two, once gnulib is adjusted.
+ nw="$nw -Wsuggest-attribute=const"
+ nw="$nw -Wsuggest-attribute=pure"
gl_MANYWARN_COMPLEMENT([GNULIB_WARN_CFLAGS], [$WARN_CFLAGS], [$nw])
AC_SUBST([GNULIB_WARN_CFLAGS])
diff --git a/gl/lib/randint.h b/gl/lib/randint.h
index edd6b19..80c9a0e 100644
--- a/gl/lib/randint.h
+++ b/gl/lib/randint.h
@@ -34,7 +34,8 @@ struct randint_source;
struct randint_source *randint_new (struct randread_source *);
struct randint_source *randint_all_new (char const *, size_t);
-struct randread_source *randint_get_source (struct randint_source const *);
+struct randread_source *randint_get_source (struct randint_source const *)
+ _GL_ATTRIBUTE_PURE;
randint randint_genmax (struct randint_source *, randint genmax);
/* Consume random data from *S to generate a random number in the range
diff --git a/gl/lib/randperm.c b/gl/lib/randperm.c
index 26181b8..362316e 100644
--- a/gl/lib/randperm.c
+++ b/gl/lib/randperm.c
@@ -30,7 +30,7 @@
/* Return the ceiling of the log base 2 of N. If N is zero, return
an unspecified value. */
-static size_t
+static size_t _GL_ATTRIBUTE_CONST
ceil_lg (size_t n)
{
size_t b = 0;
diff --git a/gl/lib/randperm.h b/gl/lib/randperm.h
index 79bbf9f..0a0837a 100644
--- a/gl/lib/randperm.h
+++ b/gl/lib/randperm.h
@@ -1,4 +1,4 @@
#include "randint.h"
#include <stddef.h>
-size_t randperm_bound (size_t, size_t);
+size_t randperm_bound (size_t, size_t) _GL_ATTRIBUTE_CONST;
size_t *randperm_new (struct randint_source *, size_t, size_t);
diff --git a/gl/lib/tempname.c.diff b/gl/lib/tempname.c.diff
index 1bf1119..fcacf53 100644
--- a/gl/lib/tempname.c.diff
+++ b/gl/lib/tempname.c.diff
@@ -22,7 +22,7 @@ index 2da5afe..562955a 100644
}
#endif /* _LIBC */
-+static inline bool
++static inline bool _GL_ATTRIBUTE_PURE
+check_x_suffix (char const *s, size_t len)
+{
+ return len <= strspn (s, "X");
diff --git a/lib/strnumcmp-in.h b/lib/strnumcmp-in.h
index dccbca5..7e963bc 100644
--- a/lib/strnumcmp-in.h
+++ b/lib/strnumcmp-in.h
@@ -71,7 +71,7 @@
if digit return -1, else 0
return 0 */
-static inline int
+static inline int _GL_ATTRIBUTE_PURE
fraccompare (char const *a, char const *b, char decimal_point)
{
if (*a == decimal_point && *b == decimal_point)
@@ -111,7 +111,7 @@ fraccompare (char const *a, char const *b, char
decimal_point)
causes comparisons to act as if there is no decimal point
character, and likewise for THOUSANDS_SEP. */
-static inline int
+static inline int _GL_ATTRIBUTE_PURE
numcompare (char const *a, char const *b,
int decimal_point, int thousands_sep)
{
diff --git a/lib/strnumcmp.c b/lib/strnumcmp.c
index f130b58..2dfc28c 100644
--- a/lib/strnumcmp.c
+++ b/lib/strnumcmp.c
@@ -23,7 +23,7 @@
/* Externally-visible name for numcompare. */
-int
+int _GL_ATTRIBUTE_PURE
strnumcmp (char const *a, char const *b,
int decimal_point, int thousands_sep)
{
diff --git a/lib/strnumcmp.h b/lib/strnumcmp.h
index 91ad351..4deef82 100644
--- a/lib/strnumcmp.h
+++ b/lib/strnumcmp.h
@@ -1,2 +1,2 @@
-int strintcmp (char const *, char const *);
+int strintcmp (char const *, char const *) _GL_ATTRIBUTE_PURE;
int strnumcmp (char const *, char const *, int, int);
diff --git a/lib/xfts.h b/lib/xfts.h
index fc3ba90..f903f48 100644
--- a/lib/xfts.h
+++ b/lib/xfts.h
@@ -6,4 +6,5 @@ xfts_open (char * const *, int options,
int (*) (const FTSENT **, const FTSENT **));
bool
-cycle_warning_required (FTS const *fts, FTSENT const *ent);
+cycle_warning_required (FTS const *fts, FTSENT const *ent)
+ _GL_ATTRIBUTE_PURE;
diff --git a/src/chown-core.h b/src/chown-core.h
index 3acb5e9..e7b3741 100644
--- a/src/chown-core.h
+++ b/src/chown-core.h
@@ -68,13 +68,13 @@ struct Chown_option
void
chopt_init (struct Chown_option *);
-void
+void _GL_ATTRIBUTE_PURE _GL_ATTRIBUTE_CONST
chopt_free (struct Chown_option *);
char *
gid_to_name (gid_t);
-char *
+char * _GL_ATTRIBUTE_PURE
uid_to_name (uid_t);
bool
diff --git a/src/copy.c b/src/copy.c
index 1025fe1..c17b942 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -468,7 +468,7 @@ extent_copy (int src_fd, int dest_fd, char *buf, size_t
buf_size,
performance hit that's probably noticeable only on trees deeper
than a few hundred levels. See use of active_dir_map in remove.c */
-static bool
+static bool _GL_ATTRIBUTE_PURE
is_ancestor (const struct stat *sb, const struct dir_list *ancestors)
{
while (ancestors != 0)
@@ -2535,7 +2535,7 @@ un_backup:
return false;
}
-static bool
+static bool _GL_ATTRIBUTE_PURE
valid_options (const struct cp_options *co)
{
assert (co != NULL);
diff --git a/src/copy.h b/src/copy.h
index 5014ea9..7332c7c 100644
--- a/src/copy.h
+++ b/src/copy.h
@@ -282,7 +282,7 @@ void dest_info_init (struct cp_options *);
void src_info_init (struct cp_options *);
void cp_options_default (struct cp_options *);
-bool chown_failure_ok (struct cp_options const *);
+bool chown_failure_ok (struct cp_options const *) _GL_ATTRIBUTE_PURE;
mode_t cached_umask (void);
#endif
diff --git a/src/dd.c b/src/dd.c
index 45aa9b9..3799d75 100644
--- a/src/dd.c
+++ b/src/dd.c
@@ -1028,7 +1028,7 @@ write_output (void)
/* Return true if STR is of the form "PATTERN" or "PATTERNDELIM...". */
-static bool
+static bool _GL_ATTRIBUTE_PURE
operand_matches (char const *str, char const *pattern, char delim)
{
while (*pattern)
@@ -1108,7 +1108,7 @@ parse_integer (const char *str, bool *invalid)
/* OPERAND is of the form "X=...". Return true if X is NAME. */
-static bool
+static bool _GL_ATTRIBUTE_PURE
operand_is (char const *operand, char const *name)
{
return operand_matches (operand, name, '=');
diff --git a/src/df.c b/src/df.c
index 1b27675..982d607 100644
--- a/src/df.c
+++ b/src/df.c
@@ -321,7 +321,7 @@ get_header (void)
/* Is FSTYPE a type of file system that should be listed? */
-static bool
+static bool _GL_ATTRIBUTE_PURE
selected_fstype (const char *fstype)
{
const struct fs_type_list *fsp;
@@ -336,7 +336,7 @@ selected_fstype (const char *fstype)
/* Is FSTYPE a type of file system that should be omitted? */
-static bool
+static bool _GL_ATTRIBUTE_PURE
excluded_fstype (const char *fstype)
{
const struct fs_type_list *fsp;
diff --git a/src/expr.c b/src/expr.c
index 2331f64..1f53507 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -393,7 +393,7 @@ printv (VALUE *v)
/* Return true if V is a null-string or zero-number. */
-static bool
+static bool _GL_ATTRIBUTE_PURE
null (VALUE *v)
{
switch (v->type)
@@ -424,7 +424,7 @@ null (VALUE *v)
/* Return true if CP takes the form of an integer. */
-static bool
+static bool _GL_ATTRIBUTE_PURE
looks_like_integer (char const *cp)
{
cp += (*cp == '-');
diff --git a/src/md5sum.c b/src/md5sum.c
index 6f6e637..9bbdc60 100644
--- a/src/md5sum.c
+++ b/src/md5sum.c
@@ -355,7 +355,7 @@ split_3 (char *s, size_t s_len,
/* Return true if S is a NUL-terminated string of DIGEST_HEX_BYTES hex digits.
Otherwise, return false. */
-static bool
+static bool _GL_ATTRIBUTE_PURE
hex_digits (unsigned char const *s)
{
unsigned int i;
diff --git a/src/od.c b/src/od.c
index 8604a8d..9ddd519 100644
--- a/src/od.c
+++ b/src/od.c
@@ -1257,7 +1257,7 @@ read_block (size_t n, char *block, size_t
*n_bytes_in_buffer)
/* Return the least common multiple of the sizes associated
with the format specs. */
-static int
+static int _GL_ATTRIBUTE_PURE
get_lcm (void)
{
size_t i;
diff --git a/src/pathchk.c b/src/pathchk.c
index 7f4e5df..8146528 100644
--- a/src/pathchk.c
+++ b/src/pathchk.c
@@ -211,7 +211,7 @@ portable_chars_only (char const *file, size_t filelen)
/* Return the address of the start of the next file name component in F. */
-static char *
+static char * _GL_ATTRIBUTE_PURE
component_start (char *f)
{
while (*f == '/')
@@ -221,7 +221,7 @@ component_start (char *f)
/* Return the size of the file name component F. F must be nonempty. */
-static size_t
+static size_t _GL_ATTRIBUTE_PURE
component_len (char const *f)
{
size_t len;
diff --git a/src/pinky.c b/src/pinky.c
index 5e279e9..00107b7 100644
--- a/src/pinky.c
+++ b/src/pinky.c
@@ -82,7 +82,7 @@ static struct option const longopts[] =
/* Count and return the number of ampersands in STR. */
-static size_t
+static size_t _GL_ATTRIBUTE_PURE
count_ampersands (const char *str)
{
size_t count = 0;
diff --git a/src/pr.c b/src/pr.c
index 3995c37..771418c 100644
--- a/src/pr.c
+++ b/src/pr.c
@@ -773,7 +773,7 @@ static struct option const long_options[] =
/* Return the number of columns that have either an open file or
stored lines. */
-static int
+static int _GL_ATTRIBUTE_PURE
cols_ready_to_print (void)
{
COLUMN *q;
diff --git a/src/ptx.c b/src/ptx.c
index 7ac6e8f..ff5e433 100644
--- a/src/ptx.c
+++ b/src/ptx.c
@@ -595,7 +595,7 @@ compare_occurs (const void *void_first, const void
*void_second)
| Return !0 if WORD appears in TABLE. Uses a binary search. |
`------------------------------------------------------------*/
-static int
+static int _GL_ATTRIBUTE_PURE
search_table (WORD *word, WORD_TABLE *table)
{
int lowest; /* current lowest possible index */
diff --git a/src/sort.c b/src/sort.c
index 084addf..3d3119d 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -1846,7 +1846,7 @@ static char const unit_order[UCHAR_LIM] =
separators and a decimal point, but it may not contain leading blanks.
Negative numbers get negative orders; zero numbers have a zero order. */
-static int
+static int _GL_ATTRIBUTE_PURE
find_unit_order (char const *number)
{
bool minus_sign = (*number == '-');
diff --git a/src/stty.c b/src/stty.c
index 6d54ece..5f5211b 100644
--- a/src/stty.c
+++ b/src/stty.c
@@ -1428,7 +1428,7 @@ screen_columns (void)
}
}
-static tcflag_t *
+static tcflag_t * _GL_ATTRIBUTE_PURE
mode_type_flag (enum mode_type type, struct termios *mode)
{
switch (type)
@@ -1789,7 +1789,7 @@ static struct speed_map const speeds[] =
{NULL, 0, 0}
};
-static speed_t
+static speed_t _GL_ATTRIBUTE_PURE
string_to_baud (const char *arg)
{
int i;
@@ -1800,7 +1800,7 @@ string_to_baud (const char *arg)
return (speed_t) -1;
}
-static unsigned long int
+static unsigned long int _GL_ATTRIBUTE_PURE
baud_to_value (speed_t speed)
{
int i;
diff --git a/src/system.h b/src/system.h
index d250d94..107dbd5 100644
--- a/src/system.h
+++ b/src/system.h
@@ -439,7 +439,7 @@ enum
/* Compute the greatest common divisor of U and V using Euclid's
algorithm. U and V must be nonzero. */
-static inline size_t
+static inline size_t _GL_ATTRIBUTE_CONST
gcd (size_t u, size_t v)
{
do
@@ -457,7 +457,7 @@ gcd (size_t u, size_t v)
nonzero. There is no overflow checking, so callers should not
specify outlandish sizes. */
-static inline size_t
+static inline size_t _GL_ATTRIBUTE_CONST
lcm (size_t u, size_t v)
{
return u * (v / gcd (u, v));
diff --git a/src/tr.c b/src/tr.c
index f7593d3..dfa37d0 100644
--- a/src/tr.c
+++ b/src/tr.c
@@ -364,7 +364,7 @@ is_equiv_class_member (unsigned char equiv_class, unsigned
char c)
/* Return true if the character C is a member of the
character class CHAR_CLASS. */
-static bool
+static bool _GL_ATTRIBUTE_PURE
is_char_class_member (enum Char_class char_class, unsigned char c)
{
int result;
@@ -542,7 +542,7 @@ unquote (char const *s, struct E_string *es)
/* If CLASS_STR is a valid character class string, return its index
in the global char_class_name array. Otherwise, return CC_NO_CLASS. */
-static enum Char_class
+static enum Char_class _GL_ATTRIBUTE_PURE
look_up_char_class (char const *class_str, size_t len)
{
enum Char_class i;
@@ -844,7 +844,7 @@ find_bracketed_repeat (const struct E_string *es, size_t
start_idx,
expression `\*[0-9]*\]', false otherwise. The string does not
match if any of its characters are escaped. */
-static bool
+static bool _GL_ATTRIBUTE_PURE
star_digits_closebracket (const struct E_string *es, size_t idx)
{
size_t i;
diff --git a/src/uniq.c b/src/uniq.c
index b35938a..db717b1 100644
--- a/src/uniq.c
+++ b/src/uniq.c
@@ -206,7 +206,7 @@ size_opt (char const *opt, char const *msgid)
/* Given a linebuffer LINE,
return a pointer to the beginning of the line's field to be compared. */
-static char *
+static char * _GL_ATTRIBUTE_PURE
find_field (struct linebuffer const *line)
{
size_t count;
diff --git a/src/wc.c b/src/wc.c
index d5a0afc..c4b5a91 100644
--- a/src/wc.c
+++ b/src/wc.c
@@ -556,7 +556,7 @@ get_input_fstatus (int nfiles, char *const *file)
recorded in FSTATUS. Optimize the same special case that
get_input_fstatus optimizes. */
-static int
+static int _GL_ATTRIBUTE_PURE
compute_number_width (int nfiles, struct fstatus const *fstatus)
{
int width = 1;
--
1.7.6.rc2.302.gc2115
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [PATCH 2/2] maint: use "const" and "pure" function attributes where possible,
Jim Meyering <=