refactor: add pure attribute to pure functions (#18165)

This will allow the compilers that support the pure attribute to make
further optimizations.
This commit is contained in:
dundargoc 2022-04-25 04:12:47 +02:00 committed by GitHub
parent 933274c438
commit c582194135
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 53 additions and 12 deletions

View File

@ -357,6 +357,7 @@ void set_bufref(bufref_T *bufref, buf_T *buf)
///
/// @param bufref Buffer reference to check for.
bool bufref_valid(bufref_T *bufref)
FUNC_ATTR_PURE
{
return bufref->br_buf_free_count == buf_free_count
? true
@ -2100,6 +2101,7 @@ buf_T *buflist_findname(char_u *ffname)
///
/// @return buffer or NULL if not found
static buf_T *buflist_findname_file_id(char_u *ffname, FileID *file_id, bool file_id_valid)
FUNC_ATTR_PURE
{
// Start at the last buffer, expect to find a match sooner.
FOR_ALL_BUFFERS_BACKWARDS(buf) {
@ -2531,7 +2533,7 @@ static bool wininfo_other_tab_diff(wininfo_T *wip)
///
/// @return NULL when there isn't any info.
static wininfo_T *find_wininfo(buf_T *buf, bool need_options, bool skip_diff_buffer)
FUNC_ATTR_NONNULL_ALL
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE
{
wininfo_T *wip;
@ -2609,6 +2611,7 @@ void get_winopts(buf_T *buf)
///
/// @return a pointer to no_position if no position is found.
pos_T *buflist_findfpos(buf_T *buf)
FUNC_ATTR_PURE
{
static pos_T no_position = { 1, 0, 0 };
@ -2618,6 +2621,7 @@ pos_T *buflist_findfpos(buf_T *buf)
/// Find the lnum for the buffer 'buf' for the current window.
linenr_T buflist_findlnum(buf_T *buf)
FUNC_ATTR_PURE
{
return buflist_findfpos(buf)->lnum;
}
@ -4928,6 +4932,7 @@ void do_arg_all(int count, int forceit, int keep_tabs)
/// @return true if "buf" is a prompt buffer.
bool bt_prompt(buf_T *buf)
FUNC_ATTR_PURE
{
return buf != NULL && buf->b_p_bt[0] == 'p';
}
@ -5339,6 +5344,7 @@ bool bt_dontwrite_msg(const buf_T *const buf)
/// @return true if the buffer should be hidden, according to 'hidden', ":hide"
/// and 'bufhidden'.
bool buf_hide(const buf_T *const buf)
FUNC_ATTR_PURE
{
// 'bufhidden' overrules 'hidden' and ":hide", check it first
switch (buf->b_p_bh[0]) {

View File

@ -654,6 +654,7 @@ static inline unsigned nr2hex(unsigned n)
///
/// @reeturn Number of display cells.
int byte2cells(int b)
FUNC_ATTR_PURE
{
if (b >= 0x80) {
return 0;
@ -1176,6 +1177,7 @@ intptr_t getwhitecols_curline(void)
}
intptr_t getwhitecols(const char_u *p)
FUNC_ATTR_PURE
{
return skipwhite(p) - p;
}
@ -1222,6 +1224,7 @@ const char *skipbin(const char *q)
/// @return Pointer to the character after the skipped digits and hex
/// characters.
char_u *skiphex(char_u *q)
FUNC_ATTR_PURE
{
char_u *p = q;
while (ascii_isxdigit(*p)) {
@ -1237,6 +1240,7 @@ char_u *skiphex(char_u *q)
///
/// @return Pointer to the digit or (NUL after the string).
char_u *skiptodigit(char_u *q)
FUNC_ATTR_PURE
{
char_u *p = q;
while (*p != NUL && !ascii_isdigit(*p)) {
@ -1270,6 +1274,7 @@ const char *skiptobin(const char *q)
///
/// @return Pointer to the hex character or (NUL after the string).
char_u *skiptohex(char_u *q)
FUNC_ATTR_PURE
{
char_u *p = q;
while (*p != NUL && !ascii_isxdigit(*p)) {
@ -1285,7 +1290,7 @@ char_u *skiptohex(char_u *q)
///
/// @return Pointer to the next whitespace or NUL character.
char_u *skiptowhite(const char_u *p)
FUNC_ATTR_NONNULL_ALL
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE
{
while (*p != ' ' && *p != '\t' && *p != NUL) {
p++;
@ -1299,6 +1304,7 @@ char_u *skiptowhite(const char_u *p)
///
/// @return Pointer to the next whitespace character.
char_u *skiptowhite_esc(char_u *p)
FUNC_ATTR_PURE
{
while (*p != ' ' && *p != '\t' && *p != NUL) {
if (((*p == '\\') || (*p == Ctrl_V)) && (*(p + 1) != NUL)) {
@ -1392,6 +1398,7 @@ long getdigits_long(char_u **pp, bool strict, long def)
///
/// @param lbuf line buffer to check
bool vim_isblankline(char_u *lbuf)
FUNC_ATTR_PURE
{
char_u *p = skipwhite(lbuf);
return *p == NUL || *p == '\r' || *p == '\n';
@ -1618,6 +1625,7 @@ vim_str2nr_proceed:
///
/// @return The value of the hex character.
int hex2nr(int c)
FUNC_ATTR_CONST
{
if ((c >= 'a') && (c <= 'f')) {
return c - 'a' + 10;
@ -1632,6 +1640,7 @@ int hex2nr(int c)
/// Convert two hex characters to a byte.
/// Return -1 if one of the characters is not hex.
int hexhex2nr(char_u *p)
FUNC_ATTR_PURE
{
if (!ascii_isxdigit(p[0]) || !ascii_isxdigit(p[1])) {
return -1;

View File

@ -846,6 +846,7 @@ static int insert_execute(VimState *state, int key)
/// Don't do this when still processing a command or a mapping.
/// Don't do this when inside a ":normal" command.
bool goto_im(void)
FUNC_ATTR_PURE
{
return p_im && stuff_empty() && typebuf_typed();
}
@ -1654,7 +1655,7 @@ void edit_putchar(int c, bool highlight)
/// Return the effective prompt for the specified buffer.
char_u *buf_prompt_text(const buf_T *const buf)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE
{
if (buf->b_prompt_text == NULL) {
return (char_u *)"% ";
@ -1663,7 +1664,8 @@ char_u *buf_prompt_text(const buf_T *const buf)
}
// Return the effective prompt for the current buffer.
char_u *prompt_text(void) FUNC_ATTR_WARN_UNUSED_RESULT
char_u *prompt_text(void)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE
{
return buf_prompt_text(curbuf);
}
@ -1711,6 +1713,7 @@ static void init_prompt(int cmdchar_todo)
/// @return true if the cursor is in the editable position of the prompt line.
bool prompt_curpos_editable(void)
FUNC_ATTR_PURE
{
return curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count
&& curwin->w_cursor.col >= (int)STRLEN(prompt_text());
@ -2101,6 +2104,7 @@ static void ins_ctrl_x(void)
// Whether other than default completion has been selected.
bool ctrl_x_mode_not_default(void)
FUNC_ATTR_PURE
{
return ctrl_x_mode != CTRL_X_NORMAL;
}
@ -2108,6 +2112,7 @@ bool ctrl_x_mode_not_default(void)
// Whether CTRL-X was typed without a following character,
// not including when in CTRL-X CTRL-V mode.
bool ctrl_x_mode_not_defined_yet(void)
FUNC_ATTR_PURE
{
return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
}
@ -3140,6 +3145,7 @@ static void ins_compl_files(int count, char_u **files, int thesaurus, int flags,
* Returns a pointer to the first char of the word. Also stops at a NUL.
*/
char_u *find_word_start(char_u *ptr)
FUNC_ATTR_PURE
{
while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1) {
ptr += utfc_ptr2len(ptr);
@ -3152,6 +3158,7 @@ char_u *find_word_start(char_u *ptr)
* Returns a pointer to just after the word.
*/
char_u *find_word_end(char_u *ptr)
FUNC_ATTR_PURE
{
const int start_class = mb_get_class(ptr);
if (start_class > 1) {
@ -7144,6 +7151,7 @@ int stuff_inserted(int c, long count, int no_esc)
}
char_u *get_last_insert(void)
FUNC_ATTR_PURE
{
if (last_insert == NULL) {
return NULL;
@ -7690,6 +7698,7 @@ bool in_cinkeys(int keytyped, int when, bool line_is_empty)
* Map Hebrew keyboard when in hkmap mode.
*/
int hkmap(int c)
FUNC_ATTR_PURE
{
if (p_hkmapp) { // phonetic mapping, by Ilya Dogolazky
enum {

View File

@ -5050,6 +5050,7 @@ static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
/// @return the function name of the partial.
char_u *partial_name(partial_T *pt)
FUNC_ATTR_PURE
{
if (pt->pt_name != NULL) {
return pt->pt_name;
@ -8525,6 +8526,7 @@ static void check_vars(const char *name, size_t len)
/// check if special v:lua value for calling lua functions
bool is_luafunc(partial_T *partial)
FUNC_ATTR_PURE
{
return partial == vvlua_partial;
}
@ -9922,6 +9924,7 @@ void func_line_end(void *cookie)
}
static var_flavour_T var_flavour(char_u *varname)
FUNC_ATTR_PURE
{
char_u *p = varname;

View File

@ -4969,6 +4969,7 @@ char_u *check_help_lang(char_u *arg)
///
/// @return a heuristic indicating how well the given string matches.
int help_heuristic(char_u *matched_string, int offset, int wrong_case)
FUNC_ATTR_PURE
{
int num_letters;
char_u *p;

View File

@ -201,6 +201,7 @@ static char *pexpand_cmds[] = {
/// Function given to ExpandGeneric() to obtain the profile command
/// specific expansion.
char_u *get_profile_name(expand_T *xp, int idx)
FUNC_ATTR_PURE
{
switch (pexpand_what) {
case PEXP_SUBCMD:
@ -439,6 +440,7 @@ static void script_dump_profile(FILE *fd)
/// @return true when a function defined in the current script should be
/// profiled.
bool prof_def_func(void)
FUNC_ATTR_PURE
{
if (current_sctx.sc_sid > 0) {
return SCRIPT_ITEM(current_sctx.sc_sid).sn_pr_force;
@ -1732,6 +1734,7 @@ int *source_dbg_tick(void *cookie)
/// @return the nesting level for a source cookie.
int source_level(void *cookie)
FUNC_ATTR_PURE
{
return ((struct source_cookie *)cookie)->level;
}
@ -2288,6 +2291,7 @@ void free_scriptnames(void)
#endif
linenr_T get_sourced_lnum(LineGetter fgetline, void *cookie)
FUNC_ATTR_PURE
{
return fgetline == getsourceline
? ((struct source_cookie *)cookie)->sourcing_lnum

View File

@ -129,6 +129,7 @@ int should_abort(int retcode)
/// to find finally clauses to be executed, and that some errors in skipped
/// commands are still reported.
int aborted_in_try(void)
FUNC_ATTR_PURE
{
// This function is only called after an error. In this case, "force_abort"
// determines whether searching for finally clauses is necessary.

View File

@ -397,6 +397,7 @@ static void start_stuff(void)
* Return TRUE if the stuff buffer is empty.
*/
int stuff_empty(void)
FUNC_ATTR_PURE
{
return (readbuf1.bh_first.b_next == NULL && readbuf2.bh_first.b_next == NULL);
}
@ -406,6 +407,7 @@ int stuff_empty(void)
* redbuf2.
*/
int readbuf1_empty(void)
FUNC_ATTR_PURE
{
return (readbuf1.bh_first.b_next == NULL);
}
@ -1025,10 +1027,10 @@ int ins_char_typebuf(int c, int modifier)
///
/// @param tb_change_cnt old value of typebuf.tb_change_cnt
bool typebuf_changed(int tb_change_cnt)
FUNC_ATTR_PURE
{
return tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
|| typebuf_was_filled
);
|| typebuf_was_filled);
}
/*
@ -1036,6 +1038,7 @@ bool typebuf_changed(int tb_change_cnt)
* not been typed (result from a mapping or come from ":normal").
*/
int typebuf_typed(void)
FUNC_ATTR_PURE
{
return typebuf.tb_maplen == 0;
}
@ -1044,6 +1047,7 @@ int typebuf_typed(void)
* Return the number of characters that are mapped (or not typed).
*/
int typebuf_maplen(void)
FUNC_ATTR_PURE
{
return typebuf.tb_maplen;
}
@ -1403,6 +1407,7 @@ void close_all_scripts(void)
* Return TRUE when reading keys from a script file.
*/
int using_script(void)
FUNC_ATTR_PURE
{
return scriptin[curscript] != NULL;
}

View File

@ -334,10 +334,9 @@ enc_alias_table[] =
* Returns -1 if not found.
*/
static int enc_canon_search(const char_u *name)
FUNC_ATTR_PURE
{
int i;
for (i = 0; i < IDX_COUNT; ++i) {
for (int i = 0; i < IDX_COUNT; i++) {
if (STRCMP(name, enc_canon_table[i].name) == 0) {
return i;
}
@ -351,10 +350,9 @@ static int enc_canon_search(const char_u *name)
* Returns 0 if not found.
*/
int enc_canon_props(const char_u *name)
FUNC_ATTR_PURE
{
int i;
i = enc_canon_search(name);
int i = enc_canon_search(name);
if (i >= 0) {
return enc_canon_table[i].prop;
} else if (STRNCMP(name, "2byte-", 6) == 0) {
@ -373,6 +371,7 @@ int enc_canon_props(const char_u *name)
* 3 - UTF-8 BOM
*/
int bomb_size(void)
FUNC_ATTR_PURE
{
int n = 0;
@ -414,11 +413,13 @@ void remove_bom(char_u *s)
* >2 for other word characters
*/
int mb_get_class(const char_u *p)
FUNC_ATTR_PURE
{
return mb_get_class_tab(p, curbuf->b_chartab);
}
int mb_get_class_tab(const char_u *p, const uint64_t *const chartab)
FUNC_ATTR_PURE
{
if (MB_BYTE2LEN(p[0]) == 1) {
if (p[0] == NUL || ascii_iswhite(p[0])) {
@ -436,6 +437,7 @@ int mb_get_class_tab(const char_u *p, const uint64_t *const chartab)
* Return true if "c" is in "table".
*/
static bool intable(const struct interval *table, size_t n_items, int c)
FUNC_ATTR_PURE
{
int mid, bot, top;
@ -1087,6 +1089,7 @@ int utf_class(const int c)
}
int utf_class_tab(const int c, const uint64_t *const chartab)
FUNC_ATTR_PURE
{
// sorted list of non-overlapping intervals
static struct clinterval {