refactor(indent_c.c): add const qualifiers

This commit is contained in:
zeertzjq
2022-01-31 15:44:54 +08:00
parent ae649650de
commit ba2bb6a81b

View File

@@ -135,7 +135,7 @@ static pos_T *find_start_rawstring(int ind_maxcomment) // XXX
* Skip to the end of a "string" and a 'c' character. * Skip to the end of a "string" and a 'c' character.
* If there is no string or character, return argument unmodified. * If there is no string or character, return argument unmodified.
*/ */
static char_u *skip_string(char_u *p) static const char_u *skip_string(const char_u *p)
{ {
int i; int i;
@@ -171,19 +171,19 @@ static char_u *skip_string(char_u *p)
} }
} else if (p[0] == 'R' && p[1] == '"') { } else if (p[0] == 'R' && p[1] == '"') {
// Raw string: R"[delim](...)[delim]" // Raw string: R"[delim](...)[delim]"
char_u *delim = p + 2; const char_u *delim = p + 2;
char_u *paren = vim_strchr(delim, '('); const char_u *paren = vim_strchr(delim, '(');
if (paren != NULL) { if (paren != NULL) {
const ptrdiff_t delim_len = paren - delim; const ptrdiff_t delim_len = paren - delim;
for (p += 3; *p; ++p) for (p += 3; *p; p++) {
if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
&& p[delim_len + 1] == '"') && p[delim_len + 1] == '"') {
{
p += delim_len + 1; p += delim_len + 1;
break; break;
} }
}
if (p[0] == '"') { if (p[0] == '"') {
continue; // continue for another string continue; // continue for another string
} }
@@ -198,9 +198,9 @@ static char_u *skip_string(char_u *p)
} }
/// @returns true if "line[col]" is inside a C string. /// @returns true if "line[col]" is inside a C string.
int is_pos_in_string(char_u *line, colnr_T col) int is_pos_in_string(const char_u *line, colnr_T col)
{ {
char_u *p; const char_u *p;
for (p = line; *p && (colnr_T)(p - line) < col; p++) { for (p = line; *p && (colnr_T)(p - line) < col; p++) {
p = skip_string(p); p = skip_string(p);
@@ -220,7 +220,7 @@ int is_pos_in_string(char_u *line, colnr_T col)
/* /*
* Return true if the string "line" starts with a word from 'cinwords'. * Return true if the string "line" starts with a word from 'cinwords'.
*/ */
bool cin_is_cinword(char_u *line) bool cin_is_cinword(const char_u *line)
{ {
bool retval = false; bool retval = false;
@@ -248,10 +248,10 @@ bool cin_is_cinword(char_u *line)
* Skip over white space and C comments within the line. * Skip over white space and C comments within the line.
* Also skip over Perl/shell comments if desired. * Also skip over Perl/shell comments if desired.
*/ */
static char_u *cin_skipcomment(char_u *s) static const char_u *cin_skipcomment(const char_u *s)
{ {
while (*s) { while (*s) {
char_u *prev_s = s; const char_u *prev_s = s;
s = skipwhite(s); s = skipwhite(s);
@@ -285,7 +285,7 @@ static char_u *cin_skipcomment(char_u *s)
* Return TRUE if there is no code at *s. White space and comments are * Return TRUE if there is no code at *s. White space and comments are
* not considered code. * not considered code.
*/ */
static int cin_nocode(char_u *s) static int cin_nocode(const char_u *s)
{ {
return *cin_skipcomment(s) == NUL; return *cin_skipcomment(s) == NUL;
} }
@@ -314,9 +314,9 @@ static pos_T *find_line_comment(void) // XXX
} }
/// Checks if `text` starts with "key:". /// Checks if `text` starts with "key:".
static bool cin_has_js_key(char_u *text) static bool cin_has_js_key(const char_u *text)
{ {
char_u *s = skipwhite(text); const char_u *s = skipwhite(text);
char_u quote = 0; char_u quote = 0;
if (*s == '\'' || *s == '"') { if (*s == '\'' || *s == '"') {
@@ -343,7 +343,7 @@ static bool cin_has_js_key(char_u *text)
/// Checks if string matches "label:"; move to character after ':' if true. /// Checks if string matches "label:"; move to character after ':' if true.
/// "*s" must point to the start of the label, if there is one. /// "*s" must point to the start of the label, if there is one.
static bool cin_islabel_skip(char_u **s) static bool cin_islabel_skip(const char_u **s)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ALL
{ {
if (!vim_isIDc(**s)) { // need at least one ID character if (!vim_isIDc(**s)) { // need at least one ID character
@@ -363,7 +363,7 @@ static bool cin_islabel_skip(char_u **s)
// Note: curwin->w_cursor must be where we are looking for the label. // Note: curwin->w_cursor must be where we are looking for the label.
bool cin_islabel(void) // XXX bool cin_islabel(void) // XXX
{ {
char_u *s = cin_skipcomment(get_cursor_line_ptr()); const char_u *s = cin_skipcomment(get_cursor_line_ptr());
// Exclude "default" from labels, since it should be indented // Exclude "default" from labels, since it should be indented
// like a switch label. Same for C++ scope declarations. // like a switch label. Same for C++ scope declarations.
@@ -383,7 +383,7 @@ bool cin_islabel(void) // XXX
*/ */
pos_T cursor_save; pos_T cursor_save;
pos_T *trypos; pos_T *trypos;
char_u *line; const char_u *line;
cursor_save = curwin->w_cursor; cursor_save = curwin->w_cursor;
while (curwin->w_cursor.lnum > 1) { while (curwin->w_cursor.lnum > 1) {
@@ -426,8 +426,8 @@ bool cin_islabel(void) // XXX
*/ */
static int cin_isinit(void) static int cin_isinit(void)
{ {
char_u *s; const char_u *s;
static char *skip[] = {"static", "public", "protected", "private"}; static char *skip[] = { "static", "public", "protected", "private" };
s = cin_skipcomment(get_cursor_line_ptr()); s = cin_skipcomment(get_cursor_line_ptr());
@@ -462,7 +462,7 @@ static int cin_isinit(void)
* Recognize a switch label: "case .*:" or "default:". * Recognize a switch label: "case .*:" or "default:".
*/ */
bool cin_iscase( bool cin_iscase(
char_u *s, const char_u *s,
bool strict // Allow relaxed check of case statement for JS bool strict // Allow relaxed check of case statement for JS
) )
{ {
@@ -505,7 +505,7 @@ bool cin_iscase(
/* /*
* Recognize a "default" switch label. * Recognize a "default" switch label.
*/ */
static int cin_isdefault(char_u *s) static int cin_isdefault(const char_u *s)
{ {
return STRNCMP(s, "default", 7) == 0 return STRNCMP(s, "default", 7) == 0
&& *(s = cin_skipcomment(s + 7)) == ':' && *(s = cin_skipcomment(s + 7)) == ':'
@@ -515,7 +515,7 @@ static int cin_isdefault(char_u *s)
/* /*
* Recognize a "public/private/protected" scope declaration label. * Recognize a "public/private/protected" scope declaration label.
*/ */
bool cin_isscopedecl(char_u *s) bool cin_isscopedecl(const char_u *s)
{ {
int i; int i;
@@ -536,9 +536,9 @@ bool cin_isscopedecl(char_u *s)
#define FIND_NAMESPACE_LIM 20 #define FIND_NAMESPACE_LIM 20
// Recognize a "namespace" scope declaration. // Recognize a "namespace" scope declaration.
static bool cin_is_cpp_namespace(char_u *s) static bool cin_is_cpp_namespace(const char_u *s)
{ {
char_u *p; const char_u *p;
bool has_name = false; bool has_name = false;
bool has_name_start = false; bool has_name_start = false;
@@ -583,7 +583,7 @@ static bool cin_is_cpp_namespace(char_u *s)
* case 234: a = b; * case 234: a = b;
* ^ * ^
*/ */
static char_u *after_label(char_u *l) static const char_u *after_label(const char_u *l)
{ {
for (; *l; ++l) { for (; *l; ++l) {
if (*l == ':') { if (*l == ':') {
@@ -610,10 +610,10 @@ static char_u *after_label(char_u *l)
*/ */
static int get_indent_nolabel(linenr_T lnum) // XXX static int get_indent_nolabel(linenr_T lnum) // XXX
{ {
char_u *l; const char_u *l;
pos_T fp; pos_T fp;
colnr_T col; colnr_T col;
char_u *p; const char_u *p;
l = ml_get(lnum); l = ml_get(lnum);
p = after_label(l); p = after_label(l);
@@ -632,9 +632,9 @@ static int get_indent_nolabel(linenr_T lnum) // XXX
* label: if (asdf && asdfasdf) * label: if (asdf && asdfasdf)
* ^ * ^
*/ */
static int skip_label(linenr_T lnum, char_u **pp) static int skip_label(linenr_T lnum, const char_u **pp)
{ {
char_u *l; const char_u *l;
int amount; int amount;
pos_T cursor_save; pos_T cursor_save;
@@ -715,8 +715,8 @@ static int cin_first_id_amount(void)
*/ */
static int cin_get_equal_amount(linenr_T lnum) static int cin_get_equal_amount(linenr_T lnum)
{ {
char_u *line; const char_u *line;
char_u *s; const char_u *s;
colnr_T col; colnr_T col;
pos_T fp; pos_T fp;
@@ -754,7 +754,7 @@ static int cin_get_equal_amount(linenr_T lnum)
/* /*
* Recognize a preprocessor statement: Any line that starts with '#'. * Recognize a preprocessor statement: Any line that starts with '#'.
*/ */
static int cin_ispreproc(char_u *s) static int cin_ispreproc(const char_u *s)
{ {
if (*skipwhite(s) == '#') if (*skipwhite(s) == '#')
return TRUE; return TRUE;
@@ -765,9 +765,9 @@ static int cin_ispreproc(char_u *s)
/// continuation line of a preprocessor statement. Decrease "*lnump" to the /// continuation line of a preprocessor statement. Decrease "*lnump" to the
/// start and return the line in "*pp". /// start and return the line in "*pp".
/// Put the amount of indent in "*amount". /// Put the amount of indent in "*amount".
static int cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount) static int cin_ispreproc_cont(const char_u **pp, linenr_T *lnump, int *amount)
{ {
char_u *line = *pp; const char_u *line = *pp;
linenr_T lnum = *lnump; linenr_T lnum = *lnump;
int retval = false; int retval = false;
int candidate_amount = *amount; int candidate_amount = *amount;
@@ -801,7 +801,7 @@ static int cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
/* /*
* Recognize the start of a C or C++ comment. * Recognize the start of a C or C++ comment.
*/ */
static int cin_iscomment(char_u *p) static int cin_iscomment(const char_u *p)
{ {
return p[0] == '/' && (p[1] == '*' || p[1] == '/'); return p[0] == '/' && (p[1] == '*' || p[1] == '/');
} }
@@ -809,7 +809,7 @@ static int cin_iscomment(char_u *p)
/* /*
* Recognize the start of a "//" comment. * Recognize the start of a "//" comment.
*/ */
static int cin_islinecomment(char_u *p) static int cin_islinecomment(const char_u *p)
{ {
return p[0] == '/' && p[1] == '/'; return p[0] == '/' && p[1] == '/';
} }
@@ -824,8 +824,8 @@ static int cin_islinecomment(char_u *p)
* both apply in order to determine initializations). * both apply in order to determine initializations).
*/ */
static char_u static char_u
cin_isterminated ( cin_isterminated(
char_u *s, const char_u *s,
int incl_open, // include '{' at the end as terminator int incl_open, // include '{' at the end as terminator
int incl_comma // recognize a trailing comma int incl_comma // recognize a trailing comma
) )
@@ -874,9 +874,9 @@ cin_isterminated (
/// lines here. /// lines here.
/// @param[in] first_lnum Where to start looking. /// @param[in] first_lnum Where to start looking.
/// @param[in] min_lnum The line before which we will not be looking. /// @param[in] min_lnum The line before which we will not be looking.
static int cin_isfuncdecl(char_u **sp, linenr_T first_lnum, linenr_T min_lnum) static int cin_isfuncdecl(const char_u **sp, linenr_T first_lnum, linenr_T min_lnum)
{ {
char_u *s; const char_u *s;
linenr_T lnum = first_lnum; linenr_T lnum = first_lnum;
linenr_T save_lnum = curwin->w_cursor.lnum; linenr_T save_lnum = curwin->w_cursor.lnum;
int retval = false; int retval = false;
@@ -977,12 +977,12 @@ done:
return retval; return retval;
} }
static int cin_isif(char_u *p) static int cin_isif(const char_u *p)
{ {
return STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]); return STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]);
} }
static int cin_iselse(char_u *p) static int cin_iselse(const char_u *p)
{ {
if (*p == '}') { // accept "} else" if (*p == '}') { // accept "} else"
p = cin_skipcomment(p + 1); p = cin_skipcomment(p + 1);
@@ -990,7 +990,7 @@ static int cin_iselse(char_u *p)
return STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]); return STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]);
} }
static int cin_isdo(char_u *p) static int cin_isdo(const char_u *p)
{ {
return STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]); return STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]);
} }
@@ -1000,7 +1000,7 @@ static int cin_isdo(char_u *p)
* We only accept a "while (condition) ;", with only white space between the * We only accept a "while (condition) ;", with only white space between the
* ')' and ';'. The condition may be spread over several lines. * ')' and ';'. The condition may be spread over several lines.
*/ */
static int cin_iswhileofdo(char_u *p, linenr_T lnum) // XXX static int cin_iswhileofdo(const char_u *p, linenr_T lnum) // XXX
{ {
pos_T cursor_save; pos_T cursor_save;
pos_T *trypos; pos_T *trypos;
@@ -1034,7 +1034,7 @@ static int cin_iswhileofdo(char_u *p, linenr_T lnum) // XXX
* Otherwise return !0 and update "*poffset" to point to the place where the * Otherwise return !0 and update "*poffset" to point to the place where the
* string was found. * string was found.
*/ */
static int cin_is_if_for_while_before_offset(char_u *line, int *poffset) static int cin_is_if_for_while_before_offset(const char_u *line, int *poffset)
{ {
int offset = *poffset; int offset = *poffset;
@@ -1078,9 +1078,9 @@ probablyFound:
*/ */
static int cin_iswhileofdo_end(int terminated) static int cin_iswhileofdo_end(int terminated)
{ {
char_u *line; const char_u *line;
char_u *p; const char_u *p;
char_u *s; const char_u *s;
pos_T *trypos; pos_T *trypos;
int i; int i;
@@ -1121,7 +1121,7 @@ static int cin_iswhileofdo_end(int terminated)
return FALSE; return FALSE;
} }
static int cin_isbreak(char_u *p) static int cin_isbreak(const char_u *p)
{ {
return STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]); return STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]);
} }
@@ -1141,10 +1141,10 @@ static int cin_isbreak(char_u *p)
*/ */
static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached) { static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached) {
lpos_T *pos = &cached->lpos; // find position lpos_T *pos = &cached->lpos; // find position
char_u *s; const char_u *s;
int class_or_struct, lookfor_ctor_init, cpp_base_class; int class_or_struct, lookfor_ctor_init, cpp_base_class;
linenr_T lnum = curwin->w_cursor.lnum; linenr_T lnum = curwin->w_cursor.lnum;
char_u *line = get_cursor_line_ptr(); const char_u *line = get_cursor_line_ptr();
if (pos->lnum <= lnum) { if (pos->lnum <= lnum) {
return cached->found; // Use the cached result return cached->found; // Use the cached result
@@ -1312,10 +1312,10 @@ static int get_baseclass_amount(int col)
* white space and comments. Skip strings and comments. * white space and comments. Skip strings and comments.
* Ignore "ignore" after "find" if it's not NULL. * Ignore "ignore" after "find" if it's not NULL.
*/ */
static int cin_ends_in(char_u *s, char_u *find, char_u *ignore) static int cin_ends_in(const char_u *s, const char_u *find, const char_u *ignore)
{ {
char_u *p = s; const char_u *p = s;
char_u *r; const char_u *r;
int len = (int)STRLEN(find); int len = (int)STRLEN(find);
while (*p != NUL) { while (*p != NUL) {
@@ -1336,7 +1336,7 @@ static int cin_ends_in(char_u *s, char_u *find, char_u *ignore)
/* /*
* Return TRUE when "s" starts with "word" and then a non-ID character. * Return TRUE when "s" starts with "word" and then a non-ID character.
*/ */
static int cin_starts_with(char_u *s, char *word) static int cin_starts_with(const char_u *s, const char *word)
{ {
int l = (int)STRLEN(word); int l = (int)STRLEN(word);
@@ -1344,9 +1344,9 @@ static int cin_starts_with(char_u *s, char *word)
} }
/// Recognize a `extern "C"` or `extern "C++"` linkage specifications. /// Recognize a `extern "C"` or `extern "C++"` linkage specifications.
static int cin_is_cpp_extern_c(char_u *s) static int cin_is_cpp_extern_c(const char_u *s)
{ {
char_u *p; const char_u *p;
int has_string_literal = false; int has_string_literal = false;
s = cin_skipcomment(s); s = cin_skipcomment(s);
@@ -1386,9 +1386,9 @@ static int cin_is_cpp_extern_c(char_u *s)
*/ */
static int cin_skip2pos(pos_T *trypos) static int cin_skip2pos(pos_T *trypos)
{ {
char_u *line; const char_u *line;
char_u *p; const char_u *p;
char_u *new_p; const char_u *new_p;
p = line = ml_get(trypos->lnum); p = line = ml_get(trypos->lnum);
while (*p && (colnr_T)(p - line) < trypos->col) { while (*p && (colnr_T)(p - line) < trypos->col) {
@@ -1531,7 +1531,7 @@ static int corr_ind_maxparen(pos_T *startpos)
* Set w_cursor.col to the column number of the last unmatched ')' or '{' in * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
* line "l". "l" must point to the start of the line. * line "l". "l" must point to the start of the line.
*/ */
static int find_last_paren(char_u *l, int start, int end) static int find_last_paren(const char_u *l, int start, int end)
{ {
int i; int i;
int retval = FALSE; int retval = FALSE;
@@ -1803,8 +1803,8 @@ int get_c_indent(void)
#define BRACE_AT_START 2 // '{' is at start of line #define BRACE_AT_START 2 // '{' is at start of line
#define BRACE_AT_END 3 // '{' is at end of line #define BRACE_AT_END 3 // '{' is at end of line
linenr_T ourscope; linenr_T ourscope;
char_u *l; const char_u *l;
char_u *look; const char_u *look;
char_u terminated; char_u terminated;
int lookfor; int lookfor;
#define LOOKFOR_INITIAL 0 #define LOOKFOR_INITIAL 0
@@ -3617,9 +3617,9 @@ laterend:
static int find_match(int lookfor, linenr_T ourscope) static int find_match(int lookfor, linenr_T ourscope)
{ {
char_u *look; const char_u *look;
pos_T *theirscope; pos_T *theirscope;
char_u *mightbeif; const char_u *mightbeif;
int elselevel; int elselevel;
int whilelevel; int whilelevel;