From de1e088012efd7839c6912f3f7f5d5787646405e Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 8 Nov 2013 18:14:31 +0100 Subject: [PATCH] *: add "pure" attributes --- Makefile.am | 1 + src/Compiler.h | 159 ++++++++++++++++++++++++++++++++++++++++++++++++ src/charset.h | 7 +-- src/colors.h | 2 + src/command.c | 4 +- src/command.h | 21 ++++++- src/filelist.c | 4 +- src/filelist.h | 10 ++- src/match.h | 3 + src/mpdclient.h | 8 ++- src/playlist.h | 4 ++ 11 files changed, 208 insertions(+), 15 deletions(-) create mode 100644 src/Compiler.h diff --git a/Makefile.am b/Makefile.am index 1f8e8fe..72e93a1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -72,6 +72,7 @@ src_ncmpc_LDADD = \ $(LIBLIRCCLIENT_LIBS) src_ncmpc_SOURCES = \ + src/Compiler.h \ $(ncmpc_headers) \ src/main.c \ src/gidle.c \ diff --git a/src/Compiler.h b/src/Compiler.h new file mode 100644 index 0000000..9756e1f --- /dev/null +++ b/src/Compiler.h @@ -0,0 +1,159 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef COMPILER_H +#define COMPILER_H + +#define GCC_CHECK_VERSION(major, minor) \ + (defined(__GNUC__) && \ + (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))) + +#ifdef __GNUC__ +#define GCC_VERSION (__GNUC__ * 10000 \ + + __GNUC_MINOR__ * 100 \ + + __GNUC_PATCHLEVEL__) +#else +#define GCC_VERSION 0 +#endif + +#if GCC_CHECK_VERSION(4,0) + +/* GCC 4.x */ + +#define gcc_const __attribute__((const)) +#define gcc_deprecated __attribute__((deprecated)) +#define gcc_may_alias __attribute__((may_alias)) +#define gcc_malloc __attribute__((malloc)) +#define gcc_noreturn __attribute__((noreturn)) +#define gcc_packed __attribute__((packed)) +#define gcc_printf(a,b) __attribute__((format(printf, a, b))) +#define gcc_pure __attribute__((pure)) +#define gcc_sentinel __attribute__((sentinel)) +#define gcc_unused __attribute__((unused)) +#define gcc_warn_unused_result __attribute__((warn_unused_result)) + +#define gcc_nonnull(...) __attribute__((nonnull(__VA_ARGS__))) +#define gcc_nonnull_all __attribute__((nonnull)) + +#define gcc_likely(x) __builtin_expect (!!(x), 1) +#define gcc_unlikely(x) __builtin_expect (!!(x), 0) + +#define gcc_aligned(n) __attribute__((aligned(n))) + +#define gcc_visibility_hidden __attribute__((visibility("hidden"))) +#define gcc_visibility_default __attribute__((visibility("default"))) + +#define gcc_always_inline __attribute__((always_inline)) + +#else + +/* generic C compiler */ + +#define gcc_const +#define gcc_deprecated +#define gcc_may_alias +#define gcc_malloc +#define gcc_noreturn +#define gcc_packed +#define gcc_printf(a,b) +#define gcc_pure +#define gcc_sentinel +#define gcc_unused +#define gcc_warn_unused_result + +#define gcc_nonnull(...) +#define gcc_nonnull_all + +#define gcc_likely(x) (x) +#define gcc_unlikely(x) (x) + +#define gcc_aligned(n) + +#define gcc_visibility_hidden +#define gcc_visibility_default + +#define gcc_always_inline inline + +#endif + +#if GCC_CHECK_VERSION(4,3) + +#define gcc_hot __attribute__((hot)) +#define gcc_cold __attribute__((cold)) + +#else /* ! GCC_UNUSED >= 40300 */ + +#define gcc_hot +#define gcc_cold + +#endif /* ! GCC_UNUSED >= 40300 */ + +#if GCC_CHECK_VERSION(4,6) && !defined(__clang__) +#define gcc_flatten __attribute__((flatten)) +#else +#define gcc_flatten +#endif + +#ifndef __cplusplus +/* plain C99 has "restrict" */ +#define gcc_restrict restrict +#elif GCC_CHECK_VERSION(4,0) +/* "__restrict__" is a GCC extension for C++ */ +#define gcc_restrict __restrict__ +#else +/* disable it on other compilers */ +#define gcc_restrict +#endif + +/* C++11 features */ + +#if defined(__cplusplus) + +/* support for C++11 "override" was added in gcc 4.7 */ +#if !defined(__clang__) && !GCC_CHECK_VERSION(4,7) +#define override +#define final +#endif + +#if defined(__clang__) || GCC_CHECK_VERSION(4,8) +#define gcc_alignas(T, fallback) alignas(T) +#else +#define gcc_alignas(T, fallback) gcc_aligned(fallback) +#endif + +#endif + +#ifndef __has_feature + // define dummy macro for non-clang compilers + #define __has_feature(x) 0 +#endif + +#if __has_feature(attribute_unused_on_fields) +#define gcc_unused_field gcc_unused +#else +#define gcc_unused_field +#endif + +#if defined(__GNUC__) || defined(__clang__) +#define gcc_unreachable() __builtin_unreachable() +#else +#define gcc_unreachable() +#endif + +#endif diff --git a/src/charset.h b/src/charset.h index 1564f3f..1fa5ec4 100644 --- a/src/charset.h +++ b/src/charset.h @@ -21,8 +21,7 @@ #define CHARSET_H #include "config.h" - -#include +#include "Compiler.h" #include @@ -34,14 +33,14 @@ charset_init(void); /** * Returns the number of terminal cells occupied by this string. */ -G_GNUC_PURE +gcc_pure unsigned utf8_width(const char *str); /** * Returns the number of terminal cells occupied by this string. */ -G_GNUC_PURE +gcc_pure unsigned locale_width(const char *p); diff --git a/src/colors.h b/src/colors.h index f049df8..4d8e876 100644 --- a/src/colors.h +++ b/src/colors.h @@ -22,6 +22,7 @@ #include "config.h" #include "ncmpc_curses.h" +#include "Compiler.h" enum color { COLOR_TITLE = 1, @@ -41,6 +42,7 @@ enum color { COLOR_END }; +gcc_pure int colors_str2color(const char *str); #ifdef ENABLE_COLORS diff --git a/src/command.c b/src/command.c index b9a909f..5621b16 100644 --- a/src/command.c +++ b/src/command.c @@ -414,7 +414,7 @@ get_key_command_name(command_t command) } command_t -get_key_command_from_name(char *name) +get_key_command_from_name(const char *name) { for (int i = 0; cmds[i].name; i++) if (strcmp(name, cmds[i].name) == 0) @@ -424,7 +424,7 @@ get_key_command_from_name(char *name) } command_t -find_key_command(int key, command_definition_t *c) +find_key_command(int key, const command_definition_t *c) { assert(key != 0); assert(c != NULL); diff --git a/src/command.h b/src/command.h index 9e8b166..307bba1 100644 --- a/src/command.h +++ b/src/command.h @@ -21,6 +21,7 @@ #define COMMAND_H #include "config.h" +#include "Compiler.h" #include #include @@ -131,7 +132,9 @@ command_definition_t *get_command_definitions(void); size_t get_cmds_max_name_width(command_definition_t *cmds); #endif -command_t find_key_command(int key, command_definition_t *cmds); +gcc_pure +command_t +find_key_command(int key, const command_definition_t *cmds); void command_dump_keys(void); @@ -142,14 +145,28 @@ int write_key_bindings(FILE *f, int all); #endif +gcc_pure const char *key2str(int key); + +gcc_pure const char *get_key_description(command_t command); + +gcc_pure const char *get_key_command_name(command_t command); + +gcc_pure const char *get_key_names(command_t command, bool all); + +gcc_pure command_t get_key_command(int key); -command_t get_key_command_from_name(char *name); + +gcc_pure +command_t +get_key_command_from_name(const char *name); + int assign_keys(command_t command, int keys[MAX_COMMAND_KEYS]); +gcc_pure command_t get_keyboard_command(void); #endif diff --git a/src/filelist.c b/src/filelist.c index 5138fdb..6a5ade3 100644 --- a/src/filelist.c +++ b/src/filelist.c @@ -197,7 +197,7 @@ same_song(const struct mpd_song *a, const struct mpd_song *b) } int -filelist_find_song(struct filelist *fl, const struct mpd_song *song) +filelist_find_song(const struct filelist *fl, const struct mpd_song *song) { guint i; @@ -221,7 +221,7 @@ filelist_find_song(struct filelist *fl, const struct mpd_song *song) } int -filelist_find_directory(struct filelist *filelist, const char *name) +filelist_find_directory(const struct filelist *filelist, const char *name) { guint i; diff --git a/src/filelist.h b/src/filelist.h index ad4cbf3..11faab6 100644 --- a/src/filelist.h +++ b/src/filelist.h @@ -20,6 +20,8 @@ #ifndef FILELIST_H #define FILELIST_H +#include "Compiler.h" + #include struct mpd_connection; @@ -53,6 +55,7 @@ filelist_is_empty(const struct filelist *filelist) return filelist_length(filelist) == 0; } +gcc_pure static inline struct filelist_entry * filelist_get(const struct filelist *filelist, guint i) { @@ -65,6 +68,7 @@ filelist_append(struct filelist *filelist, struct mpd_entity *entity); void filelist_move(struct filelist *filelist, struct filelist *from); +gcc_pure gint compare_filelist_entry_path(gconstpointer filelist_entry1, gconstpointer filelist_entry2); @@ -84,11 +88,13 @@ filelist_sort_dir_play(struct filelist *filelist, GCompareFunc compare_func); void filelist_no_duplicates(struct filelist *filelist); +gcc_pure int -filelist_find_song(struct filelist *flist, const struct mpd_song *song); +filelist_find_song(const struct filelist *flist, const struct mpd_song *song); +gcc_pure int -filelist_find_directory(struct filelist *filelist, const char *name); +filelist_find_directory(const struct filelist *filelist, const char *name); /** * Receives entities from the connection, and appends them to the diff --git a/src/match.h b/src/match.h index 5948ead..d4771ec 100644 --- a/src/match.h +++ b/src/match.h @@ -21,6 +21,7 @@ #define MATCH_H #include "config.h" +#include "Compiler.h" #include @@ -40,6 +41,7 @@ match_line(const char *line, const char *needle) GRegex * compile_regex(const char *src, bool anchor); +gcc_pure bool match_regex(GRegex *regex, const char *line); @@ -47,6 +49,7 @@ match_regex(GRegex *regex, const char *line); * Checks whether the specified line matches the search string. Case * is ignored. */ +gcc_pure bool match_line(const char *line, const char *needle); diff --git a/src/mpdclient.h b/src/mpdclient.h index c2aa377..8dab093 100644 --- a/src/mpdclient.h +++ b/src/mpdclient.h @@ -2,6 +2,7 @@ #define MPDCLIENT_H #include "playlist.h" +#include "Compiler.h" #include @@ -84,14 +85,14 @@ mpdclient_new(void); void mpdclient_free(struct mpdclient *c); -G_GNUC_PURE +gcc_pure static inline bool mpdclient_is_connected(const struct mpdclient *c) { return c->connection != NULL; } -G_GNUC_PURE +gcc_pure static inline bool mpdclient_is_playing(const struct mpdclient *c) { @@ -100,7 +101,7 @@ mpdclient_is_playing(const struct mpdclient *c) mpd_status_get_state(c->status) == MPD_STATE_PAUSE); } -G_GNUC_PURE +gcc_pure static inline const struct mpd_song * mpdclient_get_current_song(const struct mpdclient *c) { @@ -197,6 +198,7 @@ bool mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl); /* sort by list-format */ +gcc_pure gint compare_filelistentry_format(gconstpointer filelist_entry1, gconstpointer filelist_entry2); #endif diff --git a/src/playlist.h b/src/playlist.h index 0043496..2754afa 100644 --- a/src/playlist.h +++ b/src/playlist.h @@ -20,6 +20,8 @@ #ifndef MPDCLIENT_PLAYLIST_H #define MPDCLIENT_PLAYLIST_H +#include "Compiler.h" + #include #include @@ -131,10 +133,12 @@ playlist_get_index_from_same_song(const struct mpdclient_playlist *playlist, return playlist_get_index_from_file(playlist, mpd_song_get_uri(song)); } +gcc_pure gint playlist_get_id_from_uri(const struct mpdclient_playlist *playlist, const gchar *uri); +gcc_pure static inline gint playlist_get_id_from_same_song(const struct mpdclient_playlist *playlist, const struct mpd_song *song) -- 2.30.2