diff options
| author | Josh Rahm <joshuarahm@gmail.com> | 2023-01-25 18:31:31 +0000 |
|---|---|---|
| committer | Josh Rahm <joshuarahm@gmail.com> | 2023-01-25 18:31:31 +0000 |
| commit | 9243becbedbb6a1592208051f8fa2b090dcc5e7d (patch) | |
| tree | 607c2a862ec3f4399b8766383f6f8e04c4aa43b4 /src/nvim/indent_c.c | |
| parent | 9e40b6e9e1bc67f2d856adb837ee64dd0e25b717 (diff) | |
| parent | 3c48d3c83fc21dbc0841f9210f04bdb073d73cd1 (diff) | |
| download | rneovim-usermarks.tar.gz rneovim-usermarks.tar.bz2 rneovim-usermarks.zip | |
Merge remote-tracking branch 'upstream/master' into usermarksusermarks
Diffstat (limited to 'src/nvim/indent_c.c')
| -rw-r--r-- | src/nvim/indent_c.c | 1667 |
1 files changed, 735 insertions, 932 deletions
diff --git a/src/nvim/indent_c.c b/src/nvim/indent_c.c index 34a3de4f78..1c771073b2 100644 --- a/src/nvim/indent_c.c +++ b/src/nvim/indent_c.c @@ -1,19 +1,26 @@ // This is an open source non-commercial project. Dear PVS-Studio, please check // it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com -#include <assert.h> #include <inttypes.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> #include "nvim/ascii.h" +#include "nvim/buffer_defs.h" #include "nvim/charset.h" #include "nvim/cursor.h" #include "nvim/edit.h" +#include "nvim/globals.h" #include "nvim/indent.h" #include "nvim/indent_c.h" +#include "nvim/macros.h" #include "nvim/mark.h" #include "nvim/memline.h" #include "nvim/memory.h" #include "nvim/option.h" +#include "nvim/pos.h" #include "nvim/search.h" #include "nvim/strings.h" #include "nvim/vim.h" @@ -27,11 +34,9 @@ typedef struct { #ifdef INCLUDE_GENERATED_DECLARATIONS # include "indent_c.c.generated.h" #endif -/* - * Find the start of a comment, not knowing if we are in a comment right now. - * Search starts at w_cursor.lnum and goes backwards. - * Return NULL when not inside a comment. - */ +// Find the start of a comment, not knowing if we are in a comment right now. +// Search starts at w_cursor.lnum and goes backwards. +// Return NULL when not inside a comment. static pos_T *ind_find_start_comment(void) // XXX { return find_start_comment(curbuf->b_ind_maxcomment); @@ -48,10 +53,8 @@ pos_T *find_start_comment(int ind_maxcomment) // XXX break; } - /* - * Check if the comment start we found is inside a string. - * If it is then restrict the search to below this line and try again. - */ + // Check if the comment start we found is inside a string. + // If it is then restrict the search to below this line and try again. if (!is_pos_in_string(ml_get(pos->lnum), pos->col)) { break; } @@ -97,11 +100,9 @@ static pos_T *ind_find_start_CORS(linenr_T *is_raw) return comment_pos; } -/* - * Find the start of a raw string, not knowing if we are in one right now. - * Search starts at w_cursor.lnum and goes backwards. - * Return NULL when not inside a raw string. - */ +// Find the start of a raw string, not knowing if we are in one right now. +// Search starts at w_cursor.lnum and goes backwards. +// Return NULL when not inside a raw string. static pos_T *find_start_rawstring(int ind_maxcomment) // XXX { pos_T *pos; @@ -127,17 +128,13 @@ static pos_T *find_start_rawstring(int ind_maxcomment) // XXX return pos; } -/* - * Skip to the end of a "string" and a 'c' character. - * If there is no string or character, return argument unmodified. - */ -static const char_u *skip_string(const char_u *p) +// Skip to the end of a "string" and a 'c' character. +// If there is no string or character, return argument unmodified. +static const char *skip_string(const char *p) { int i; - /* - * We loop, because strings may be concatenated: "date""time". - */ + // We loop, because strings may be concatenated: "date""time". for (;; p++) { if (p[0] == '\'') { // 'c' or '\n' or '\000' if (p[1] == NUL) { // ' at end of line @@ -167,14 +164,14 @@ static const char_u *skip_string(const char_u *p) } } else if (p[0] == 'R' && p[1] == '"') { // Raw string: R"[delim](...)[delim]" - const char *delim = (char *)p + 2; - const char *paren = vim_strchr((char *)delim, '('); + const char *delim = p + 2; + const char *paren = vim_strchr(delim, '('); if (paren != NULL) { const ptrdiff_t delim_len = paren - delim; for (p += 3; *p; p++) { - if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0 + if (p[0] == ')' && strncmp(p + 1, delim, (size_t)delim_len) == 0 && p[delim_len + 1] == '"') { p += delim_len + 1; break; @@ -194,9 +191,9 @@ static const char_u *skip_string(const char_u *p) } /// @returns true if "line[col]" is inside a C string. -int is_pos_in_string(const char_u *line, colnr_T col) +int is_pos_in_string(const char *line, colnr_T col) { - const char_u *p; + const char *p; for (p = line; *p && (colnr_T)(p - line) < col; p++) { p = skip_string(p); @@ -204,29 +201,24 @@ int is_pos_in_string(const char_u *line, colnr_T col) return !((colnr_T)(p - line) <= col); } -/* - * Functions for C-indenting. - * Most of this originally comes from Eric Fischer. - */ -/* - * Below "XXX" means that this function may unlock the current line. - */ - -/* - * Return true if the string "line" starts with a word from 'cinwords'. - */ -bool cin_is_cinword(const char_u *line) +// Functions for C-indenting. +// Most of this originally comes from Eric Fischer. + +// Below "XXX" means that this function may unlock the current line. + +/// @return true if the string "line" starts with a word from 'cinwords'. +bool cin_is_cinword(const char *line) { bool retval = false; - size_t cinw_len = STRLEN(curbuf->b_p_cinw) + 1; - char_u *cinw_buf = xmalloc(cinw_len); - line = (char_u *)skipwhite((char *)line); + size_t cinw_len = strlen(curbuf->b_p_cinw) + 1; + char *cinw_buf = xmalloc(cinw_len); + line = skipwhite(line); - for (char *cinw = (char *)curbuf->b_p_cinw; *cinw;) { - size_t len = copy_option_part(&cinw, (char *)cinw_buf, cinw_len, ","); - if (STRNCMP(line, cinw_buf, len) == 0 - && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1]))) { + for (char *cinw = curbuf->b_p_cinw; *cinw;) { + size_t len = copy_option_part(&cinw, cinw_buf, cinw_len, ","); + if (strncmp(line, cinw_buf, len) == 0 + && (!vim_iswordc((uint8_t)line[len]) || !vim_iswordc((uint8_t)line[len - 1]))) { retval = true; break; } @@ -237,21 +229,19 @@ bool cin_is_cinword(const char_u *line) return retval; } -/* - * Skip over white space and C comments within the line. - * Also skip over Perl/shell comments if desired. - */ -static const char_u *cin_skipcomment(const char_u *s) +// Skip over white space and C comments within the line. +// Also skip over Perl/shell comments if desired. +static const char *cin_skipcomment(const char *s) { while (*s) { - const char_u *prev_s = s; + const char *prev_s = s; - s = (char_u *)skipwhite((char *)s); + s = skipwhite(s); - /* Perl/shell # comment comment continues until eol. Require a space - * before # to avoid recognizing $#array. */ + // Perl/shell # comment comment continues until eol. Require a space + // before # to avoid recognizing $#array. if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#') { - s += STRLEN(s); + s += strlen(s); break; } if (*s != '/') { @@ -259,7 +249,7 @@ static const char_u *cin_skipcomment(const char_u *s) } s++; if (*s == '/') { // slash-slash comment continues till eol - s += STRLEN(s); + s += strlen(s); break; } if (*s != '*') { @@ -275,28 +265,24 @@ static const char_u *cin_skipcomment(const char_u *s) return s; } -/* - * Return TRUE if there is no code at *s. White space and comments are - * not considered code. - */ -static int cin_nocode(const char_u *s) +/// Return true if there is no code at *s. White space and comments are +/// not considered code. +static int cin_nocode(const char *s) { return *cin_skipcomment(s) == NUL; } -/* - * Check previous lines for a "//" line comment, skipping over blank lines. - */ +// Check previous lines for a "//" line comment, skipping over blank lines. static pos_T *find_line_comment(void) // XXX { static pos_T pos; - char_u *line; - char_u *p; + char *line; + char *p; pos = curwin->w_cursor; while (--pos.lnum > 0) { line = ml_get(pos.lnum); - p = (char_u *)skipwhite((char *)line); + p = skipwhite(line); if (cin_islinecomment(p)) { pos.col = (int)(p - line); return &pos; @@ -309,21 +295,21 @@ static pos_T *find_line_comment(void) // XXX } /// Checks if `text` starts with "key:". -static bool cin_has_js_key(const char_u *text) +static bool cin_has_js_key(const char *text) { - const char_u *s = (char_u *)skipwhite((char *)text); + const char *s = skipwhite(text); - char_u quote = 0; + char quote = 0; if (*s == '\'' || *s == '"') { // can be 'key': or "key": quote = *s; s++; } - if (!vim_isIDc(*s)) { // need at least one ID character + if (!vim_isIDc((uint8_t)(*s))) { // need at least one ID character return false; } - while (vim_isIDc(*s)) { + while (vim_isIDc((uint8_t)(*s))) { s++; } if (*s && *s == quote) { @@ -338,14 +324,14 @@ static bool cin_has_js_key(const char_u *text) /// Checks if string matches "label:"; move to character after ':' if true. /// "*s" must point to the start of the label, if there is one. -static bool cin_islabel_skip(const char_u **s) +static bool cin_islabel_skip(const char **s) FUNC_ATTR_NONNULL_ALL { - if (!vim_isIDc(**s)) { // need at least one ID character + if (!vim_isIDc((uint8_t)(**s))) { // need at least one ID character return false; } - while (vim_isIDc(**s)) { + while (vim_isIDc((uint8_t)(**s))) { (*s)++; } @@ -359,7 +345,7 @@ static bool cin_islabel_skip(const char_u **s) // Note: curwin->w_cursor must be where we are looking for the label. bool cin_islabel(void) // XXX { - const char_u *s = cin_skipcomment(get_cursor_line_ptr()); + const char *s = cin_skipcomment(get_cursor_line_ptr()); // Exclude "default" from labels, since it should be indented // like a switch label. Same for C++ scope declarations. @@ -369,26 +355,23 @@ bool cin_islabel(void) // XXX if (cin_isscopedecl(s)) { return false; } + if (!cin_islabel_skip(&s)) { return false; } - /* - * Only accept a label if the previous line is terminated or is a case - * label. - */ + // Only accept a label if the previous line is terminated or is a case + // label. pos_T cursor_save; pos_T *trypos; - const char_u *line; + const char *line; cursor_save = curwin->w_cursor; while (curwin->w_cursor.lnum > 1) { - --curwin->w_cursor.lnum; + curwin->w_cursor.lnum--; - /* - * If we're in a comment or raw string now, skip to the start of - * it. - */ + // If we're in a comment or raw string now, skip to the start of + // it. curwin->w_cursor.col = 0; if ((trypos = ind_find_start_CORS(NULL)) != NULL) { // XXX curwin->w_cursor = *trypos; @@ -403,7 +386,7 @@ bool cin_islabel(void) // XXX } curwin->w_cursor = cursor_save; - if (cin_isterminated(line, TRUE, FALSE) + if (cin_isterminated(line, true, false) || cin_isscopedecl(line) || cin_iscase(line, true) || (cin_islabel_skip(&line) && cin_nocode(line))) { @@ -415,14 +398,12 @@ bool cin_islabel(void) // XXX return true; // label at start of file??? } -/* - * Recognize structure initialization and enumerations: - * "[typedef] [static|public|protected|private] enum" - * "[typedef] [static|public|protected|private] = {" - */ +// Recognize structure initialization and enumerations: +// "[typedef] [static|public|protected|private] enum" +// "[typedef] [static|public|protected|private] = {" static int cin_isinit(void) { - const char_u *s; + const char *s; static char *skip[] = { "static", "public", "protected", "private" }; s = cin_skipcomment(get_cursor_line_ptr()); @@ -434,7 +415,7 @@ static int cin_isinit(void) for (;;) { int i, l; - for (i = 0; i < (int)ARRAY_SIZE(skip); ++i) { + for (i = 0; i < (int)ARRAY_SIZE(skip); i++) { l = (int)strlen(skip[i]); if (cin_starts_with(s, skip[i])) { s = cin_skipcomment(s + l); @@ -451,21 +432,21 @@ static int cin_isinit(void) return true; } - if (cin_ends_in(s, (char_u *)"=", (char_u *)"{")) { + if (cin_ends_in(s, "=", "{")) { return true; } - return FALSE; + return false; } /// Recognize a switch label: "case .*:" or "default:". /// /// @param strict Allow relaxed check of case statement for JS -bool cin_iscase(const char_u *s, bool strict) +bool cin_iscase(const char *s, bool strict) { s = cin_skipcomment(s); if (cin_starts_with(s, "case")) { - for (s += 4; *s; ++s) { + for (s += 4; *s; s++) { s = cin_skipcomment(s); if (*s == NUL) { break; @@ -485,9 +466,8 @@ bool cin_iscase(const char_u *s, bool strict) // JS etc. if (strict) { return false; // stop at string - } else { - return true; } + return true; } } return false; @@ -499,30 +479,28 @@ bool cin_iscase(const char_u *s, bool strict) return false; } -/* - * Recognize a "default" switch label. - */ -static int cin_isdefault(const char_u *s) +// Recognize a "default" switch label. +static int cin_isdefault(const char *s) { - return STRNCMP(s, "default", 7) == 0 + return strncmp(s, "default", 7) == 0 && *(s = cin_skipcomment(s + 7)) == ':' && s[1] != ':'; } /// Recognize a scope declaration label set in 'cinscopedecls'. -bool cin_isscopedecl(const char_u *p) +bool cin_isscopedecl(const char *p) { - const char_u *s = cin_skipcomment(p); + const char *s = cin_skipcomment(p); - const size_t cinsd_len = STRLEN(curbuf->b_p_cinsd) + 1; - char_u *cinsd_buf = xmalloc(cinsd_len); + const size_t cinsd_len = strlen(curbuf->b_p_cinsd) + 1; + char *cinsd_buf = xmalloc(cinsd_len); bool found = false; - for (char *cinsd = (char *)curbuf->b_p_cinsd; *cinsd;) { - const size_t len = copy_option_part(&cinsd, (char *)cinsd_buf, cinsd_len, ","); - if (STRNCMP(s, cinsd_buf, len) == 0) { - const char_u *skip = cin_skipcomment(s + len); + for (char *cinsd = curbuf->b_p_cinsd; *cinsd;) { + const size_t len = copy_option_part(&cinsd, cinsd_buf, cinsd_len, ","); + if (strncmp(s, cinsd_buf, len) == 0) { + const char *skip = cin_skipcomment(s + len); if (*skip == ':' && skip[1] != ':') { found = true; break; @@ -539,33 +517,33 @@ bool cin_isscopedecl(const char_u *p) #define FIND_NAMESPACE_LIM 20 // Recognize a "namespace" scope declaration. -static bool cin_is_cpp_namespace(const char_u *s) +static bool cin_is_cpp_namespace(const char *s) { - const char_u *p; + const char *p; bool has_name = false; bool has_name_start = false; s = cin_skipcomment(s); - if (STRNCMP(s, "inline", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6]))) { - s = cin_skipcomment((char_u *)skipwhite((char *)s + 6)); + if (strncmp(s, "inline", 6) == 0 && (s[6] == NUL || !vim_iswordc((uint8_t)s[6]))) { + s = cin_skipcomment(skipwhite(s + 6)); } - if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9]))) { - p = cin_skipcomment((char_u *)skipwhite((char *)s + 9)); + if (strncmp(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc((uint8_t)s[9]))) { + p = cin_skipcomment(skipwhite(s + 9)); while (*p != NUL) { if (ascii_iswhite(*p)) { has_name = true; // found end of a name - p = cin_skipcomment((char_u *)skipwhite((char *)p)); + p = cin_skipcomment(skipwhite(p)); } else if (*p == '{') { break; - } else if (vim_iswordc(*p)) { + } else if (vim_iswordc((uint8_t)(*p))) { has_name_start = true; if (has_name) { return false; // word character after skipping past name } p++; - } else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2])) { + } else if (p[0] == ':' && p[1] == ':' && vim_iswordc((uint8_t)p[2])) { if (!has_name_start || has_name) { return false; } @@ -580,15 +558,13 @@ static bool cin_is_cpp_namespace(const char_u *s) return false; } -/* - * Return a pointer to the first non-empty non-comment character after a ':'. - * Return NULL if not found. - * case 234: a = b; - * ^ - */ -static const char_u *after_label(const char_u *l) +// Return a pointer to the first non-empty non-comment character after a ':'. +// Return NULL if not found. +// case 234: a = b; +// ^ +static const char *after_label(const char *l) { - for (; *l; ++l) { + for (; *l; l++) { if (*l == ':') { if (l[1] == ':') { // skip over "::" for C++ l++; @@ -609,16 +585,14 @@ static const char_u *after_label(const char_u *l) return l; } -/* - * Get indent of line "lnum", skipping a label. - * Return 0 if there is nothing after the label. - */ +// Get indent of line "lnum", skipping a label. +// Return 0 if there is nothing after the label. static int get_indent_nolabel(linenr_T lnum) // XXX { - const char_u *l; + const char *l; pos_T fp; colnr_T col; - const char_u *p; + const char *p; l = ml_get(lnum); p = after_label(l); @@ -632,15 +606,13 @@ static int get_indent_nolabel(linenr_T lnum) // XXX return (int)col; } -/* - * Find indent for line "lnum", ignoring any case or jump label. - * Also return a pointer to the text (after the label) in "pp". - * label: if (asdf && asdfasdf) - * ^ - */ -static int skip_label(linenr_T lnum, const char_u **pp) +// Find indent for line "lnum", ignoring any case or jump label. +// Also return a pointer to the text (after the label) in "pp". +// label: if (asdf && asdfasdf) +// ^ +static int skip_label(linenr_T lnum, const char **pp) { - const char_u *l; + const char *l; int amount; pos_T cursor_save; @@ -664,78 +636,75 @@ static int skip_label(linenr_T lnum, const char_u **pp) return amount; } -/* - * Return the indent of the first variable name after a type in a declaration. - * int a, indent of "a" - * static struct foo b, indent of "b" - * enum bla c, indent of "c" - * Returns zero when it doesn't look like a declaration. - */ +// Return the indent of the first variable name after a type in a declaration. +// int a, indent of "a" +// static struct foo b, indent of "b" +// enum bla c, indent of "c" +// Returns zero when it doesn't look like a declaration. static int cin_first_id_amount(void) { - char_u *line, *p, *s; + char *line, *p, *s; int len; pos_T fp; colnr_T col; line = get_cursor_line_ptr(); - p = (char_u *)skipwhite((char *)line); + p = skipwhite(line); len = (int)(skiptowhite(p) - p); - if (len == 6 && STRNCMP(p, "static", 6) == 0) { - p = (char_u *)skipwhite((char *)p + 6); + if (len == 6 && strncmp(p, "static", 6) == 0) { + p = skipwhite(p + 6); len = (int)(skiptowhite(p) - p); } - if (len == 6 && STRNCMP(p, "struct", 6) == 0) { - p = (char_u *)skipwhite((char *)p + 6); - } else if (len == 4 && STRNCMP(p, "enum", 4) == 0) { - p = (char_u *)skipwhite((char *)p + 4); - } else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0) - || (len == 6 && STRNCMP(p, "signed", 6) == 0)) { - s = (char_u *)skipwhite((char *)p + len); - if ((STRNCMP(s, "int", 3) == 0 && ascii_iswhite(s[3])) - || (STRNCMP(s, "long", 4) == 0 && ascii_iswhite(s[4])) - || (STRNCMP(s, "short", 5) == 0 && ascii_iswhite(s[5])) - || (STRNCMP(s, "char", 4) == 0 && ascii_iswhite(s[4]))) { + if (len == 6 && strncmp(p, "struct", 6) == 0) { + p = skipwhite(p + 6); + } else if (len == 4 && strncmp(p, "enum", 4) == 0) { + p = skipwhite(p + 4); + } else if ((len == 8 && strncmp(p, "unsigned", 8) == 0) + || (len == 6 && strncmp(p, "signed", 6) == 0)) { + s = skipwhite(p + len); + if ((strncmp(s, "int", 3) == 0 && ascii_iswhite(s[3])) + || (strncmp(s, "long", 4) == 0 && ascii_iswhite(s[4])) + || (strncmp(s, "short", 5) == 0 && ascii_iswhite(s[5])) + || (strncmp(s, "char", 4) == 0 && ascii_iswhite(s[4]))) { p = s; } } - for (len = 0; vim_isIDc(p[len]); len++) {} + for (len = 0; vim_isIDc((uint8_t)p[len]); len++) {} if (len == 0 || !ascii_iswhite(p[len]) || cin_nocode(p)) { return 0; } - p = (char_u *)skipwhite((char *)p + len); + p = skipwhite(p + len); fp.lnum = curwin->w_cursor.lnum; fp.col = (colnr_T)(p - line); getvcol(curwin, &fp, &col, NULL, NULL); return (int)col; } -/* - * Return the indent of the first non-blank after an equal sign. - * char *foo = "here"; - * Return zero if no (useful) equal sign found. - * Return -1 if the line above "lnum" ends in a backslash. - * foo = "asdf\ - * asdf\ - * here"; - */ +// Return the indent of the first non-blank after an equal sign. +// char *foo = "here"; +// Return zero if no (useful) equal sign found. +// Return -1 if the line above "lnum" ends in a backslash. +// foo = "asdf{backslash} +// asdf{backslash} +// here"; static int cin_get_equal_amount(linenr_T lnum) { - const char_u *line; - const char_u *s; + const char *line; + const char *s; colnr_T col; pos_T fp; if (lnum > 1) { line = ml_get(lnum - 1); - if (*line != NUL && line[STRLEN(line) - 1] == '\\') { + if (*line != NUL && line[strlen(line) - 1] == '\\') { return -1; } } - line = s = ml_get(lnum); - while (*s != NUL && vim_strchr("=;{}\"'", *s) == NULL) { + s = ml_get(lnum); + line = s; + while (*s != NUL && vim_strchr("=;{}\"'", (uint8_t)(*s)) == NULL) { if (cin_iscomment(s)) { // ignore comments s = cin_skipcomment(s); } else { @@ -746,7 +715,7 @@ static int cin_get_equal_amount(linenr_T lnum) return 0; } - s = (char_u *)skipwhite((char *)s + 1); + s = skipwhite(s + 1); if (cin_nocode(s)) { return 0; } @@ -761,35 +730,33 @@ static int cin_get_equal_amount(linenr_T lnum) return (int)col; } -/* - * Recognize a preprocessor statement: Any line that starts with '#'. - */ -static int cin_ispreproc(const char_u *s) +// Recognize a preprocessor statement: Any line that starts with '#'. +static int cin_ispreproc(const char *s) { - if (*skipwhite((char *)s) == '#') { + if (*skipwhite(s) == '#') { return true; } - return FALSE; + return false; } -/// Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a +/// Return true if line "*pp" at "*lnump" is a preprocessor statement or a /// continuation line of a preprocessor statement. Decrease "*lnump" to the /// start and return the line in "*pp". /// Put the amount of indent in "*amount". -static int cin_ispreproc_cont(const char_u **pp, linenr_T *lnump, int *amount) +static int cin_ispreproc_cont(const char **pp, linenr_T *lnump, int *amount) { - const char_u *line = *pp; + const char *line = *pp; linenr_T lnum = *lnump; int retval = false; int candidate_amount = *amount; - if (*line != NUL && line[STRLEN(line) - 1] == '\\') { + if (*line != NUL && line[strlen(line) - 1] == '\\') { candidate_amount = get_indent_lnum(lnum); } for (;;) { if (cin_ispreproc(line)) { - retval = TRUE; + retval = true; *lnump = lnum; break; } @@ -797,7 +764,7 @@ static int cin_ispreproc_cont(const char_u **pp, linenr_T *lnump, int *amount) break; } line = ml_get(--lnum); - if (*line == NUL || line[STRLEN(line) - 1] != '\\') { + if (*line == NUL || line[strlen(line) - 1] != '\\') { break; } } @@ -811,18 +778,14 @@ static int cin_ispreproc_cont(const char_u **pp, linenr_T *lnump, int *amount) return retval; } -/* - * Recognize the start of a C or C++ comment. - */ -static int cin_iscomment(const char_u *p) +// Recognize the start of a C or C++ comment. +static int cin_iscomment(const char *p) { return p[0] == '/' && (p[1] == '*' || p[1] == '/'); } -/* - * Recognize the start of a "//" comment. - */ -static int cin_islinecomment(const char_u *p) +// Recognize the start of a "//" comment. +static int cin_islinecomment(const char *p) { return p[0] == '/' && p[1] == '/'; } @@ -838,11 +801,11 @@ static int cin_islinecomment(const char_u *p) /// /// @return the character terminating the line (ending char's have precedence if /// both apply in order to determine initializations). -static char_u cin_isterminated(const char_u *s, int incl_open, int incl_comma) +static char cin_isterminated(const char *s, int incl_open, int incl_comma) { - char_u found_start = 0; + char found_start = 0; unsigned n_open = 0; - int is_else = FALSE; + int is_else = false; s = cin_skipcomment(s); @@ -889,9 +852,9 @@ static char_u cin_isterminated(const char_u *s, int incl_open, int incl_comma) /// lines here. /// @param[in] first_lnum Where to start looking. /// @param[in] min_lnum The line before which we will not be looking. -static int cin_isfuncdecl(const char_u **sp, linenr_T first_lnum, linenr_T min_lnum) +static int cin_isfuncdecl(const char **sp, linenr_T first_lnum, linenr_T min_lnum) { - const char_u *s; + const char *s; linenr_T lnum = first_lnum; linenr_T save_lnum = curwin->w_cursor.lnum; int retval = false; @@ -952,7 +915,7 @@ static int cin_isfuncdecl(const char_u **sp, linenr_T first_lnum, linenr_T min_l // defined(y) lnum = first_lnum - 1; s = ml_get(lnum); - if (*s == NUL || s[STRLEN(s) - 1] != '\\') { + if (*s == NUL || s[strlen(s) - 1] != '\\') { retval = true; } goto done; @@ -960,10 +923,10 @@ static int cin_isfuncdecl(const char_u **sp, linenr_T first_lnum, linenr_T min_l if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s)) { int comma = (*s == ','); - /* ',' at the end: continue looking in the next line. - * At the end: check for ',' in the next line, for this style: - * func(arg1 - * , arg2) */ + // ',' at the end: continue looking in the next line. + // At the end: check for ',' in the next line, for this style: + // func(arg1 + // , arg2) for (;;) { if (lnum >= curbuf->b_ml.ml_line_count) { break; @@ -978,7 +941,7 @@ static int cin_isfuncdecl(const char_u **sp, linenr_T first_lnum, linenr_T min_l } // Require a comma at end of the line or a comma or ')' at the // start of next line. - s = (char_u *)skipwhite((char *)s); + s = skipwhite(s); if (!just_started && (!comma && *s != ',' && *s != ')')) { break; } @@ -999,30 +962,28 @@ done: return retval; } -static int cin_isif(const char_u *p) +static int cin_isif(const char *p) { - return STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]); + return strncmp(p, "if", 2) == 0 && !vim_isIDc((uint8_t)p[2]); } -static int cin_iselse(const char_u *p) +static int cin_iselse(const char *p) { if (*p == '}') { // accept "} else" p = cin_skipcomment(p + 1); } - return STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]); + return strncmp(p, "else", 4) == 0 && !vim_isIDc((uint8_t)p[4]); } -static int cin_isdo(const char_u *p) +static int cin_isdo(const char *p) { - return STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]); + return strncmp(p, "do", 2) == 0 && !vim_isIDc((uint8_t)p[2]); } -/* - * Check if this is a "while" that should have a matching "do". - * We only accept a "while (condition) ;", with only white space between the - * ')' and ';'. The condition may be spread over several lines. - */ -static int cin_iswhileofdo(const char_u *p, linenr_T lnum) // XXX +// Check if this is a "while" that should have a matching "do". +// We only accept a "while (condition) ;", with only white space between the +// ')' and ';'. The condition may be spread over several lines. +static int cin_iswhileofdo(const char *p, linenr_T lnum) // XXX { pos_T cursor_save; pos_T *trypos; @@ -1050,13 +1011,11 @@ static int cin_iswhileofdo(const char_u *p, linenr_T lnum) // XXX return retval; } -/* - * Check whether in "p" there is an "if", "for" or "while" before "*poffset". - * Return 0 if there is none. - * Otherwise return !0 and update "*poffset" to point to the place where the - * string was found. - */ -static int cin_is_if_for_while_before_offset(const char_u *line, int *poffset) +// Check whether in "p" there is an "if", "for" or "while" before "*poffset". +// Return 0 if there is none. +// Otherwise return !0 and update "*poffset" to point to the place where the +// string was found. +static int cin_is_if_for_while_before_offset(const char *line, int *poffset) { int offset = *poffset; @@ -1068,19 +1027,19 @@ static int cin_is_if_for_while_before_offset(const char_u *line, int *poffset) } offset -= 1; - if (!STRNCMP(line + offset, "if", 2)) { + if (!strncmp(line + offset, "if", 2)) { goto probablyFound; } if (offset >= 1) { offset -= 1; - if (!STRNCMP(line + offset, "for", 3)) { + if (!strncmp(line + offset, "for", 3)) { goto probablyFound; } if (offset >= 2) { offset -= 2; - if (!STRNCMP(line + offset, "while", 5)) { + if (!strncmp(line + offset, "while", 5)) { goto probablyFound; } } @@ -1088,26 +1047,24 @@ static int cin_is_if_for_while_before_offset(const char_u *line, int *poffset) return 0; probablyFound: - if (!offset || !vim_isIDc(line[offset - 1])) { + if (!offset || !vim_isIDc((uint8_t)line[offset - 1])) { *poffset = offset; return 1; } return 0; } -/* - * Return TRUE if we are at the end of a do-while. - * do - * nothing; - * while (foo - * && bar); <-- here - * Adjust the cursor to the line with "while". - */ +/// Return true if we are at the end of a do-while. +/// do +/// nothing; +/// while (foo +/// && bar); <-- here +/// Adjust the cursor to the line with "while". static int cin_iswhileofdo_end(int terminated) { - const char_u *line; - const char_u *p; - const char_u *s; + const char *line; + const char *p; + const char *s; pos_T *trypos; int i; @@ -1119,10 +1076,10 @@ static int cin_iswhileofdo_end(int terminated) while (*p != NUL) { p = cin_skipcomment(p); if (*p == ')') { - s = (char_u *)skipwhite((char *)p + 1); + s = skipwhite(p + 1); if (*s == ';' && cin_nocode(s + 1)) { - /* Found ");" at end of the line, now check there is "while" - * before the matching '('. XXX */ + // Found ");" at end of the line, now check there is "while" + // before the matching '('. XXX i = (int)(p - line); curwin->w_cursor.col = i; trypos = find_match_paren(curbuf->b_ind_maxparen); @@ -1133,7 +1090,7 @@ static int cin_iswhileofdo_end(int terminated) } if (cin_starts_with(s, "while")) { curwin->w_cursor.lnum = trypos->lnum; - return TRUE; + return true; } } @@ -1146,34 +1103,32 @@ static int cin_iswhileofdo_end(int terminated) p++; } } - return FALSE; + return false; } -static int cin_isbreak(const char_u *p) +static int cin_isbreak(const char *p) { - return STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]); + return strncmp(p, "break", 5) == 0 && !vim_isIDc((uint8_t)p[5]); } -/* - * Find the position of a C++ base-class declaration or - * constructor-initialization. eg: - * - * class MyClass : - * baseClass <-- here - * class MyClass : public baseClass, - * anotherBaseClass <-- here (should probably lineup ??) - * MyClass::MyClass(...) : - * baseClass(...) <-- here (constructor-initialization) - * - * This is a lot of guessing. Watch out for "cond ? func() : foo". - */ +// Find the position of a C++ base-class declaration or +// constructor-initialization. eg: +// +// class MyClass : +// baseClass <-- here +// class MyClass : public baseClass, +// anotherBaseClass <-- here (should probably lineup ??) +// MyClass::MyClass(...) : +// baseClass(...) <-- here (constructor-initialization) +// +// This is a lot of guessing. Watch out for "cond ? func() : foo". static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached) { lpos_T *pos = &cached->lpos; // find position - const char_u *s; + const char *s; int class_or_struct, lookfor_ctor_init, cpp_base_class; linenr_T lnum = curwin->w_cursor.lnum; - const char_u *line = get_cursor_line_ptr(); + const char *line = get_cursor_line_ptr(); if (pos->lnum <= lnum) { return cached->found; // Use the cached result @@ -1181,7 +1136,7 @@ static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached) pos->col = 0; - s = (char_u *)skipwhite((char *)line); + s = skipwhite(line); if (*s == '#') { // skip #define FOO x ? (x) : x return false; } @@ -1190,24 +1145,23 @@ static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached) return false; } - cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; - - /* Search for a line starting with '#', empty, ending in ';' or containing - * '{' or '}' and start below it. This handles the following situations: - * a = cond ? - * func() : - * asdf; - * func::foo() - * : something - * {} - * Foo::Foo (int one, int two) - * : something(4), - * somethingelse(3) - * {} - */ + cpp_base_class = lookfor_ctor_init = class_or_struct = false; + + // Search for a line starting with '#', empty, ending in ';' or containing + // '{' or '}' and start below it. This handles the following situations: |