This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 27e9ca7590ed0340000f46855b297f2b924281fb Author: raiden00pl <[email protected]> AuthorDate: Fri Aug 21 10:22:50 2026 +0200 tools/nxstyle: make the tool comply with the coding standard The check added by the preceding commit reports ten braces in nxstyle.c that do not line up with the brace they close, and a comment that does not share the column of the run it belongs to. Bring the whole file into line with the standard, so that the checks added by the commits that follow are not introduced against a file that breaks them. Signed-off-by: raiden00pl <[email protected]> Assisted-by: Claude Code --- tools/nxstyle.c | 1539 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 776 insertions(+), 763 deletions(-) diff --git a/tools/nxstyle.c b/tools/nxstyle.c index c7aff891ce4..6fa5c81688c 100644 --- a/tools/nxstyle.c +++ b/tools/nxstyle.c @@ -928,7 +928,7 @@ static void backslash_to_slash(char *str) { if (*p == '\\') { - *p = '/'; + *p = '/'; } } } @@ -1011,8 +1011,8 @@ static void check_spaces_left(char *line, int lineno, int ndx) if (ndx-- > 0 && line[ndx] != ' ' && line[ndx] != '(' && line[ndx] != ')') { - ERROR("Operator/assignment must be preceded with whitespace", - lineno, ndx); + ERROR("Operator/assignment must be preceded with whitespace", + lineno, ndx); } } @@ -1027,14 +1027,14 @@ static void check_spaces_leftright(char *line, int lineno, int ndx1, int ndx2) { if (ndx1 > 0 && line[ndx1 - 1] != ' ') { - ERROR("Operator/assignment must be preceded with whitespace", - lineno, ndx1); + ERROR("Operator/assignment must be preceded with whitespace", + lineno, ndx1); } if (line[ndx2 + 1] != '\0' && line[ndx2 + 1] != '\n' && line[ndx2 + 1] != ' ') { - ERROR("Operator/assignment must be followed with whitespace", - lineno, ndx2); + ERROR("Operator/assignment must be followed with whitespace", + lineno, ndx2); } } @@ -1408,7 +1408,11 @@ int main(int argc, char **argv, char **envp) int brace_indent; /* Indentation of the keyword awaiting a brace */ int ctrl_brace; /* Alignment required of a brace on this line, or -1 */ int rbrace_match; /* Alignment of the left brace closed on this line */ - int lbrace_indent[MAX_BRACE]; /* Indentation of each open left brace */ + + /* Indentation of each open left brace */ + + int lbrace_indent[MAX_BRACE]; + enum pptype_e ppline; /* > 0: The next line the continuation of a * pre-processor command */ int rhcomment; /* Indentation of Comment to the right of code @@ -1442,48 +1446,48 @@ int main(int argc, char **argv, char **envp) while ((c = getopt(argc, argv, ":hv:gm:r:")) != -1) { switch (c) - { - case 'm': - excess = atoi(optarg); - if (excess < 1) - { - show_usage(argv[0], 1, "Bad value for <excess>."); - excess = 0; - } - - break; - - case 'v': - g_verbose = atoi(optarg); - if (g_verbose < 0 || g_verbose > 2) - { - show_usage(argv[0], 1, "Bad value for <level>."); - } - - break; - - case 'r': - g_rangestart[g_rangenumber] = atoi(strtok(optarg, ",")); - g_rangecount[g_rangenumber++] = atoi(strtok(NULL, ",")); - break; - - case 'h': - show_usage(argv[0], 0, NULL); - break; - - case ':': - show_usage(argv[0], 1, "Missing argument."); - break; - - case '?': - show_usage(argv[0], 1, "Unrecognized option."); - break; - - default: - show_usage(argv[0], 0, NULL); - break; - } - } + { + case 'm': + excess = atoi(optarg); + if (excess < 1) + { + show_usage(argv[0], 1, "Bad value for <excess>."); + excess = 0; + } + + break; + + case 'v': + g_verbose = atoi(optarg); + if (g_verbose < 0 || g_verbose > 2) + { + show_usage(argv[0], 1, "Bad value for <level>."); + } + + break; + + case 'r': + g_rangestart[g_rangenumber] = atoi(strtok(optarg, ",")); + g_rangecount[g_rangenumber++] = atoi(strtok(NULL, ",")); + break; + + case 'h': + show_usage(argv[0], 0, NULL); + break; + + case ':': + show_usage(argv[0], 1, "Missing argument."); + break; + + case '?': + show_usage(argv[0], 1, "Unrecognized option."); + break; + + default: + show_usage(argv[0], 0, NULL); + break; + } + } if (optind < argc - 1 || argv[optind] == NULL) { @@ -1657,8 +1661,8 @@ int main(int argc, char **argv, char **envp) if (line[n] != '}' && line[n] != '#' && prevrhcmt == 0) { - ERROR("Missing blank line after comment", comment_lineno, - 1); + ERROR("Missing blank line after comment", comment_lineno, + 1); } } @@ -1669,7 +1673,7 @@ int main(int argc, char **argv, char **envp) if (lineno == 1 && (line[n] != '/' || line[n + 1] != '*')) { - ERROR("Missing file header comment block", lineno, 1); + ERROR("Missing file header comment block", lineno, 1); } if (lineno == 2) @@ -1699,6 +1703,7 @@ int main(int argc, char **argv, char **envp) */ char *basedir = strstr(g_file_name, TOPDIR); + if (basedir != NULL) { /* Add 1 to the offset for the slash character */ @@ -1789,8 +1794,8 @@ int main(int argc, char **argv, char **envp) strncmp(&line[n], "while", 5) != 0 && strncmp(&line[n], "break", 5) != 0) { - ERROR("Right brace must be followed by a blank line", - rbrace_lineno, n + 1); + ERROR("Right brace must be followed by a blank line", + rbrace_lineno, n + 1); } /* If the right brace is followed by a pre-processor command @@ -1824,43 +1829,43 @@ int main(int argc, char **argv, char **envp) { switch (line[n]) { - case ' ': - { - indent++; - } - break; + case ' ': + { + indent++; + } + break; - case '\t': - { - if (!btabs) - { - ERROR("TABs found. First detected", lineno, n); - btabs = true; - } + case '\t': + { + if (!btabs) + { + ERROR("TABs found. First detected", lineno, n); + btabs = true; + } - indent = (indent + 4) & ~3; - } - break; + indent = (indent + 4) & ~3; + } + break; - case '\r': - { - if (!bcrs) - { - ERROR("Carriage returns found. " - "First detected", lineno, n); - bcrs = true; - } - } - break; + case '\r': + { + if (!bcrs) + { + ERROR("Carriage returns found. " + "First detected", lineno, n); + bcrs = true; + } + } + break; - default: - { - snprintf(buffer, sizeof(buffer), - "Unexpected white space character %02x found", - line[n]); - ERROR(buffer, lineno, n); - } - break; + default: + { + snprintf(buffer, sizeof(buffer), + "Unexpected white space character %02x found", + line[n]); + ERROR(buffer, lineno, n); + } + break; } } @@ -1899,8 +1904,8 @@ int main(int argc, char **argv, char **envp) if (line[ii] != '\0') { /* Make sure that pre-processor definitions are all in - * the pre-processor definitions section. - */ + * the pre-processor definitions section. + */ ppline = PPLINE_OTHER; @@ -1991,7 +1996,7 @@ int main(int argc, char **argv, char **envp) ppline = PPLINE_ENDIF; } - } + } } if (ppline == PPLINE_IF || ppline == PPLINE_ELIF) @@ -2061,7 +2066,7 @@ int main(int argc, char **argv, char **envp) } else if (!isspace((int)line[n + 2]) && line[n + 2] != '*') { - ERROR("Missing space after opening C comment", lineno, n); + ERROR("Missing space after opening C comment", lineno, n); } if (strstr(lptr, "*/") == NULL) @@ -2140,7 +2145,7 @@ int main(int argc, char **argv, char **envp) * by a label. */ - ERROR("Missing blank line before comment found", lineno, 1); + ERROR("Missing blank line before comment found", lineno, 1); } /* 'comment_lineno 'holds the line number of the last closing @@ -2253,7 +2258,7 @@ int main(int argc, char **argv, char **envp) { if (tmppnest == 0 && !tmpbstring && line[i] == ',') { - ERROR("Multiple data definitions", lineno, i + 1); + ERROR("Multiple data definitions", lineno, i + 1); break; } else if (line[i] == '(') @@ -2486,78 +2491,78 @@ int main(int argc, char **argv, char **envp) * Hz for frequencies (including KHz, MHz, etc.) */ - if (!have_lower && islower(line[n])) - { - switch (line[n]) - { - /* A sequence containing 'v' may occur at the - * beginning of the identifier. - */ - - case 'v': - if (n > 1 && - line[n - 2] == 'I' && - line[n - 1] == 'P' && - (line[n + 1] == '4' || - line[n + 1] == '6')) - { - } - else if (n > 3 && - line[n - 4] == 'I' && - line[n - 3] == 'C' && - line[n - 2] == 'M' && - line[n - 1] == 'P' && - line[n + 1] == '6') - { - } - else if (n > 3 && - line[n - 4] == 'I' && - line[n - 3] == 'G' && - line[n - 2] == 'M' && - line[n - 1] == 'P' && - line[n + 1] == '2') - { - } - else - { - have_lower = true; - } - break; - - /* Sequences containing 'p', 'd', or 'z' must have - * been preceded by upper case characters. - */ - - case 'p': - if (!have_upper || n < 1 || - !isdigit(line[n - 1]) || - !isdigit(line[n + 1])) - { - have_lower = true; - } - break; - - case 'd': - if (!have_upper || !isdigit(line[n + 1])) - { - have_lower = true; - } - break; - - case 'z': - if (!have_upper || n < 1 || - line[n - 1] != 'H') - { - have_lower = true; - } - break; - break; - - default: - have_lower = true; - break; - } - } + if (!have_lower && islower(line[n])) + { + switch (line[n]) + { + /* A sequence containing 'v' may occur at the + * beginning of the identifier. + */ + + case 'v': + if (n > 1 && + line[n - 2] == 'I' && + line[n - 1] == 'P' && + (line[n + 1] == '4' || + line[n + 1] == '6')) + { + } + else if (n > 3 && + line[n - 4] == 'I' && + line[n - 3] == 'C' && + line[n - 2] == 'M' && + line[n - 1] == 'P' && + line[n + 1] == '6') + { + } + else if (n > 3 && + line[n - 4] == 'I' && + line[n - 3] == 'G' && + line[n - 2] == 'M' && + line[n - 1] == 'P' && + line[n + 1] == '2') + { + } + else + { + have_lower = true; + } + break; + + /* Sequences containing 'p', 'd', or 'z' must have + * been preceded by upper case characters. + */ + + case 'p': + if (!have_upper || n < 1 || + !isdigit(line[n - 1]) || + !isdigit(line[n + 1])) + { + have_lower = true; + } + break; + + case 'd': + if (!have_upper || !isdigit(line[n + 1])) + { + have_lower = true; + } + break; + + case 'z': + if (!have_upper || n < 1 || + line[n - 1] != 'H') + { + have_lower = true; + } + break; + break; + + default: + have_lower = true; + break; + } + } n++; } @@ -2585,13 +2590,13 @@ int main(int argc, char **argv, char **envp) line[ident_index] != 'X') || line[ident_index - 1] != '0') { - ERROR("Mixed case identifier found", - lineno, ident_index); + ERROR("Mixed case identifier found", + lineno, ident_index); } else if (have_upper) { - ERROR("Upper case hex constant found", - lineno, ident_index); + ERROR("Upper case hex constant found", + lineno, ident_index); } } @@ -2617,7 +2622,7 @@ int main(int argc, char **argv, char **envp) } else if (!isspace((int)line[n + 2]) && line[n + 2] != '*') { - ERROR("Missing space after opening C comment", lineno, n); + ERROR("Missing space after opening C comment", lineno, n); } /* Increment the count of nested comments */ @@ -2679,8 +2684,8 @@ int main(int argc, char **argv, char **envp) } else if (!isspace((int)line[n - 2]) && line[n - 2] != '*') { - ERROR("Missing space before closing C comment", lineno, - n); + ERROR("Missing space before closing C comment", lineno, + n); } /* Check for block comments that are not on a separate line. @@ -2691,8 +2696,8 @@ int main(int argc, char **argv, char **envp) if (prevncomment > 0 && !bblank && rhcomment == 0) { - ERROR("Block comment terminator must be on a " - "separate line", lineno, n); + ERROR("Block comment terminator must be on a " + "separate line", lineno, n); } #if 0 @@ -2702,7 +2707,7 @@ int main(int argc, char **argv, char **envp) if (line[n + 1] != '\n') { - ERROR("Garbage on line after C comment", lineno, n); + ERROR("Garbage on line after C comment", lineno, n); } #endif @@ -2806,727 +2811,734 @@ int main(int argc, char **argv, char **envp) { switch (line[n]) { - /* Handle logic nested with curly braces */ - - case '{': - { - if (n > indent) - { - /* REVISIT: dnest is always > 0 here if bfunctions == - * false. - */ + /* Handle logic nested with curly braces */ - if (dnest == 0 || !bfunctions || lineno == rbrace_lineno) - { - ERROR("Left bracket not on separate line", lineno, - n); - } - } - else if (line[n + 1] != '\n') - { - if (dnest == 0) - { - ERROR("Garbage follows left bracket", lineno, n); - } - } + case '{': + { + if (n > indent) + { + /* REVISIT: dnest is always > 0 here if bfunctions == + * false. + */ - bnest++; + if (dnest == 0 || !bfunctions || + lineno == rbrace_lineno) + { + ERROR("Left bracket not on separate line", lineno, + n); + } + } + else if (line[n + 1] != '\n') + { + if (dnest == 0) + { + ERROR("Garbage follows left bracket", lineno, n); + } + } - /* Remember where a brace beginning a line sits, so the - * brace closing it can be checked. -1 for any other. - */ + bnest++; - if (bnest >= 1 && bnest <= MAX_BRACE) - { - lbrace_indent[bnest - 1] = n == indent ? indent : -1; - } + /* Remember where a brace beginning a line sits, so the + * brace closing it can be checked. -1 for any other. + */ - if (dnest > 0) - { - dnest++; - } + if (bnest >= 1 && bnest <= MAX_BRACE) + { + lbrace_indent[bnest - 1] = n == indent ? indent : -1; + } - /* Check if we are within 'extern "C"', we don't - * normally indent in that case because the 'extern "C"' - * is conditioned on __cplusplus. - */ + if (dnest > 0) + { + dnest++; + } - if (lineno == externc_lineno || - lineno - 1 == externc_lineno) - { - bexternc = true; - } + /* Check if we are within 'extern "C"', we don't + * normally indent in that case because the 'extern "C"' + * is conditioned on __cplusplus. + */ - /* Suppress error for comment following a left brace */ + if (lineno == externc_lineno || + lineno - 1 == externc_lineno) + { + bexternc = true; + } - noblank_lineno = lineno; - lbrace_lineno = lineno; - } - break; + /* Suppress error for comment following a left brace */ - case '}': - { - /* Decrement the brace nesting level */ - - if (bnest < 1) - { - ERROR("Unmatched right brace", lineno, n); - } - else - { - bnest--; - if (bnest < 1) - { - bnest = 0; - bswitch = false; - } - - /* Recover the alignment of the matching left brace */ - - if (n == indent && bnest < MAX_BRACE) - { - rbrace_match = lbrace_indent[bnest]; - } - } - - /* Decrement the declaration nesting level */ - - if (dnest < 3) - { - dnest = 0; - bexternc = false; - } - else - { - dnest--; - } + noblank_lineno = lineno; + lbrace_lineno = lineno; + } + break; - /* The right brace should be on a separate line */ + case '}': + { + /* Decrement the brace nesting level */ - if (n > indent) - { - if (dnest == 0) - { - ERROR("Right bracket not on separate line", - lineno, n); - } - } + if (bnest < 1) + { + ERROR("Unmatched right brace", lineno, n); + } + else + { + bnest--; + if (bnest < 1) + { + bnest = 0; + bswitch = false; + } - /* Check for garbage following the left brace */ + /* Recover the alignment of the matching left brace */ - if (line[n + 1] != '\n' && - line[n + 1] != ',' && - line[n + 1] != ';') - { - int sndx = n + 1; - bool whitespace = false; + if (n == indent && bnest < MAX_BRACE) + { + rbrace_match = lbrace_indent[bnest]; + } + } - /* Skip over spaces */ + /* Decrement the declaration nesting level */ - while (line[sndx] == ' ') - { - sndx++; - } + if (dnest < 3) + { + dnest = 0; + bexternc = false; + } + else + { + dnest--; + } - /* One possibility is that the right bracket is - * followed by an identifier then a semi-colon. - * Comma is possible to but would be a case of - * multiple declaration of multiple instances. - */ + /* The right brace should be on a separate line */ - if (line[sndx] == '_' || isalpha(line[sndx])) - { - int endx = sndx; + if (n > indent) + { + if (dnest == 0) + { + ERROR("Right bracket not on separate line", + lineno, n); + } + } - /* Skip to the end of the identifier. Checking - * for mixed case identifiers will be done - * elsewhere. - */ + /* Check for garbage following the left brace */ - while (line[endx] == '_' || - isalnum(line[endx])) - { - endx++; - } + if (line[n + 1] != '\n' && + line[n + 1] != ',' && + line[n + 1] != ';') + { + int sndx = n + 1; + bool whitespace = false; - /* Skip over spaces */ + /* Skip over spaces */ - while (line[endx] == ' ') - { - whitespace = true; - endx++; - } + while (line[sndx] == ' ') + { + sndx++; + } - /* Handle according to what comes after the - * identifier. - */ + /* One possibility is that the right bracket is + * followed by an identifier then a semi-colon. + * Comma is possible to but would be a case of + * multiple declaration of multiple instances. + */ - if (strncmp(&line[sndx], "while", 5) == 0) - { - ERROR("'while' must be on a separate line", - lineno, sndx); - } - else if (line[endx] == ',') - { - ERROR("Multiple data definitions on line", - lineno, endx); - } - else if (line[endx] == ';') - { - if (whitespace) - { - ERROR("Space precedes semi-colon", - lineno, endx); - } - } - else if (line[endx] == '=') - { - /* There's a struct initialization following */ + if (line[sndx] == '_' || isalpha(line[sndx])) + { + int endx = sndx; - check_spaces_leftright(line, lineno, endx, endx); - dnest = 1; - } - else - { - ERROR("Garbage follows right bracket", - lineno, n); - } - } - else - { - ERROR("Garbage follows right bracket", lineno, n); - } - } + /* Skip to the end of the identifier. Checking + * for mixed case identifiers will be done + * elsewhere. + */ - /* The right brace should not be preceded with a a blank - * line. - */ + while (line[endx] == '_' || + isalnum(line[endx])) + { + endx++; + } - if (lineno == blank_lineno + 1) - { - ERROR("Blank line precedes right brace at line", - lineno, 1); - } + /* Skip over spaces */ - rbrace_lineno = lineno; - } - break; + while (line[endx] == ' ') + { + whitespace = true; + endx++; + } - /* Handle logic with parentheses */ + /* Handle according to what comes after the + * identifier. + */ - case '(': - { - /* Increase the parenthetical nesting level */ + if (strncmp(&line[sndx], "while", 5) == 0) + { + ERROR("'while' must be on a separate line", + lineno, sndx); + } + else if (line[endx] == ',') + { + ERROR("Multiple data definitions on line", + lineno, endx); + } + else if (line[endx] == ';') + { + if (whitespace) + { + ERROR("Space precedes semi-colon", + lineno, endx); + } + } + else if (line[endx] == '=') + { + /* There's a struct initialization following */ + + check_spaces_leftright(line, lineno, + endx, endx); + dnest = 1; + } + else + { + ERROR("Garbage follows right bracket", + lineno, n); + } + } + else + { + ERROR("Garbage follows right bracket", lineno, n); + } + } - pnest++; + /* The right brace should not be preceded with a a blank + * line. + */ - /* Check for inappropriate space around parentheses */ + if (lineno == blank_lineno + 1) + { + ERROR("Blank line precedes right brace at line", + lineno, 1); + } - if (line[n + 1] == ' ') - { - ERROR("Space follows left parenthesis", lineno, n); - } - } - break; + rbrace_lineno = lineno; + } + break; - case ')': - { - /* Decrease the parenthetical nesting level */ + /* Handle logic with parentheses */ - if (pnest < 1) - { - ERROR("Unmatched right parentheses", lineno, n); - pnest = 0; - } - else - { - pnest--; - } + case '(': + { + /* Increase the parenthetical nesting level */ - /* Allow ')' as first thing on the line (n == indent) - * Allow "for (xx; xx; )" (bfor == true) - */ + pnest++; - if (n > 0 && n != indent && line[n - 1] == ' ' && !bfor) - { - ERROR("Space precedes right parenthesis", lineno, n); - } + /* Check for inappropriate space around parentheses */ - /* Unset bif if last parenthesis is closed */ + if (line[n + 1] == ' ') + { + ERROR("Space follows left parenthesis", lineno, n); + } + } + break; - if (bif == true && pnest == 0) - { - bif = false; - } + case ')': + { + /* Decrease the parenthetical nesting level */ - /* Remember where the header ends, to see what follows */ + if (pnest < 1) + { + ERROR("Unmatched right parentheses", lineno, n); + pnest = 0; + } + else + { + pnest--; + } - if (ctrl_kw != NULL && pnest == 0 && ctrl_hdrend < 0) - { - ctrl_hdrend = n; - } - } - break; + /* Allow ')' as first thing on the line (n == indent) + * Allow "for (xx; xx; )" (bfor == true) + */ - /* Check for inappropriate space around square brackets */ + if (n > 0 && n != indent && line[n - 1] == ' ' && !bfor) + { + ERROR("Space precedes right parenthesis", lineno, n); + } - case '[': - { - if (line[n + 1] == ' ') - { - ERROR("Space follows left bracket", lineno, n); - } - } - break; + /* Unset bif if last parenthesis is closed */ - case ']': - { - if (n > 0 && line[n - 1] == ' ') - { - ERROR("Space precedes right bracket", lineno, n); - } - } - break; + if (bif == true && pnest == 0) + { + bif = false; + } - /* Semi-colon may terminate a declaration */ + /* Remember where the header ends, to see what follows */ - case ';': - { - if (!isspace((int)line[n + 1])) - { - ERROR("Missing whitespace after semicolon", lineno, n); - } + if (ctrl_kw != NULL && pnest == 0 && ctrl_hdrend < 0) + { + ctrl_hdrend = n; + } + } + break; - /* Semicolon terminates a declaration/definition if there - * was no left curly brace (i.e., dnest is only 1). - */ + /* Check for inappropriate space around square brackets */ - if (dnest == 1) - { - dnest = 0; - } - } - break; - case ':': - { - if (bcase == true) - { - char *ndx = &line[n + 1]; - while ((int)isspace(*ndx)) - { - ndx++; - } + case '[': + { + if (line[n + 1] == ' ') + { + ERROR("Space follows left bracket", lineno, n); + } + } + break; - if (*ndx != '\0' && *ndx != '/') - { - ERROR("Case statement should be on a new line", - lineno, n); - } + case ']': + { + if (n > 0 && line[n - 1] == ' ') + { + ERROR("Space precedes right bracket", lineno, n); + } + } + break; - bcase = false; - } - } - break; - case ',': - { - if (!isspace((int)line[n + 1])) - { - ERROR("Missing whitespace after comma", lineno, n); - } - } - break; + /* Semi-colon may terminate a declaration */ - /* Skip over character constants */ + case ';': + { + if (!isspace((int)line[n + 1])) + { + ERROR("Missing whitespace after semicolon", lineno, n); + } - case '\'': - { - int endndx = n + 2; + /* Semicolon terminates a declaration/definition if there + * was no left curly brace (i.e., dnest is only 1). + */ - if (line[n + 1] != '\n' && line[n + 1] != '\0') - { - if (line[n + 1] == '\\') - { - for (; - line[endndx] != '\n' && - line[endndx] != '\0' && - line[endndx] != '\''; - endndx++); - } + if (dnest == 1) + { + dnest = 0; + } + } + break; + case ':': + { + if (bcase == true) + { + char *ndx = &line[n + 1]; - n = endndx; - } - } - break; + while ((int)isspace(*ndx)) + { + ndx++; + } - /* Check for space around various operators */ + if (*ndx != '\0' && *ndx != '/') + { + ERROR("Case statement should be on a new line", + lineno, n); + } - case '-': + bcase = false; + } + } + break; + case ',': + { + if (!isspace((int)line[n + 1])) + { + ERROR("Missing whitespace after comma", lineno, n); + } + } + break; - /* -> */ + /* Skip over character constants */ - if (line[n + 1] == '>') + case '\'': { - /* -> must have no whitespaces on its left or right */ + int endndx = n + 2; - check_nospaces_leftright(line, lineno, n, n + 1); - n++; + if (line[n + 1] != '\n' && line[n + 1] != '\0') + { + if (line[n + 1] == '\\') + { + for (; + line[endndx] != '\n' && + line[endndx] != '\0' && + line[endndx] != '\''; + endndx++); + } + + n = endndx; + } } + break; - /* -- */ + /* Check for space around various operators */ - else if (line[n + 1] == '-') - { - /* "--" should be next to its operand. If there are - * whitespaces or non-operand characters on both left - * and right (e.g. "a -- ", "a[i --]", "(-- i)"), - * there's an error. - */ + case '-': - check_operand_leftright(line, lineno, n, n + 1); - n++; - } + /* -> */ - /* -= */ + if (line[n + 1] == '>') + { + /* -> must have no whitespaces on its left or right */ - else if (line[n + 1] == '=') - { - check_spaces_leftright(line, lineno, n, n + 1); - n++; - } + check_nospaces_leftright(line, lineno, n, n + 1); + n++; + } - /* Scientific notation with a negative exponent (eg. 10e-10) - * REVISIT: This fails for cases where the variable name - * ends with 'e' preceded by a digit: - * a = abc1e-10; - * a = ABC1E-10; - */ + /* -- */ - else if ((line[n - 1] == 'e' || line[n - 1] == 'E') && - isdigit(line[n + 1]) && isdigit(line[n - 2])) - { - n++; - } - else - { - /* '-' may function as a unary operator and snuggle - * on the left. - */ + else if (line[n + 1] == '-') + { + /* "--" should be next to its operand. If there are + * whitespaces or non-operand characters on both left + * and right (e.g. "a -- ", "a[i --]", "(-- i)"), + * there's an error. + */ - check_spaces_left(line, lineno, n); - } + check_operand_leftright(line, lineno, n, n + 1); + n++; + } - break; + /* -= */ - case '+': + else if (line[n + 1] == '=') + { + check_spaces_leftright(line, lineno, n, n + 1); + n++; + } - /* ++ */ + /* Scientific notation with a negative exponent (eg. 10e-10) + * REVISIT: This fails for cases where the variable name + * ends with 'e' preceded by a digit: + * a = abc1e-10; + * a = ABC1E-10; + */ - if (line[n + 1] == '+') - { - /* "++" should be next to its operand. If there are - * whitespaces or non-operand characters on both left - * and right (e.g. "a ++ ", "a[i ++]", "(++ i)"), - * there's an error. - */ + else if ((line[n - 1] == 'e' || line[n - 1] == 'E') && + isdigit(line[n + 1]) && isdigit(line[n - 2])) + { + n++; + } + else + { + /* '-' may function as a unary operator and snuggle + * on the left. + */ - check_operand_leftright(line, lineno, n, n + 1); - n++; - } + check_spaces_left(line, lineno, n); + } - /* += */ + break; - else if (line[n + 1] == '=') - { - check_spaces_leftright(line, lineno, n, n + 1); - n++; - } - else - { - /* '+' may function as a unary operator and snuggle - * on the left. - */ + case '+': - check_spaces_left(line, lineno, n); - } + /* ++ */ - break; + if (line[n + 1] == '+') + { + /* "++" should be next to its operand. If there are + * whitespaces or non-operand characters on both left + * and right (e.g. "a ++ ", "a[i ++]", "(++ i)"), + * there's an error. + */ - case '&': + check_operand_leftright(line, lineno, n, n + 1); + n++; + } - /* &<variable> OR &(<expression>) */ + /* += */ - if (isalpha((int)line[n + 1]) || line[n + 1] == '_' || - line[n + 1] == '(') - { - } + else if (line[n + 1] == '=') + { + check_spaces_leftright(line, lineno, n, n + 1); + n++; + } + else + { + /* '+' may function as a unary operator and snuggle + * on the left. + */ - /* &&, &= */ + check_spaces_left(line, lineno, n); + } - else if (line[n + 1] == '=' || line[n + 1] == '&') - { - check_spaces_leftright(line, lineno, n, n + 1); - n++; - } - else - { - check_spaces_leftright(line, lineno, n, n); - } + break; - break; + case '&': - case '/': + /* &<variable> OR &(<expression>) */ - /* C comment terminator */ + if (isalpha((int)line[n + 1]) || line[n + 1] == '_' || + line[n + 1] == '(') + { + } - if (line[n - 1] == '*') - { - n++; - } + /* &&, &= */ + + else if (line[n + 1] == '=' || line[n + 1] == '&') + { + check_spaces_leftright(line, lineno, n, n + 1); + n++; + } + else + { + check_spaces_leftright(line, lineno, n, n); + } + + break; + + case '/': + + /* C comment terminator */ + + if (line[n - 1] == '*') + { + n++; + } /* C++-style comment */ - else if (line[n + 1] == '/') - { - /* Check for "http://" or "https://" */ + else if (line[n + 1] == '/') + { + /* Check for "http://" or "https://" */ - if ((n < 5 || strncmp(&line[n - 5], "http://", 7) != 0) && - (n < 6 || strncmp(&line[n - 6], "https://", 8) != 0)) - { - ERROR("C++ style comment on at %d:%d\n", - lineno, n); - } + if ((n < 5 || + strncmp(&line[n - 5], "http://", 7) != 0) && + (n < 6 || + strncmp(&line[n - 6], "https://", 8) != 0)) + { + ERROR("C++ style comment on at %d:%d\n", + lineno, n); + } - n++; - } + n++; + } - /* /= */ + /* /= */ - else if (line[n + 1] == '=') - { - check_spaces_leftright(line, lineno, n, n + 1); - n++; - } + else if (line[n + 1] == '=') + { + check_spaces_leftright(line, lineno, n, n + 1); + n++; + } - /* Division operator */ + /* Division operator */ - else - { - check_spaces_leftright(line, lineno, n, n); - } + else + { + check_spaces_leftright(line, lineno, n, n); + } - break; + break; - case '*': + case '*': - /* *\/, ** */ + /* *\/, ** */ - if (line[n] == '*' && - (line[n + 1] == '/' || - line[n + 1] == '*')) - { - n++; - break; - } + if (line[n] == '*' && + (line[n + 1] == '/' || + line[n + 1] == '*')) + { + n++; + break; + } - /* *<variable>, *(<expression>) */ + /* *<variable>, *(<expression>) */ - else if (isalpha((int)line[n + 1]) || - line[n + 1] == '_' || - line[n + 1] == '(') - { - break; - } + else if (isalpha((int)line[n + 1]) || + line[n + 1] == '_' || + line[n + 1] == '(') + { + break; + } - /* (<type> *) */ + /* (<type> *) */ - else if (line[n + 1] == ')') - { - /* REVISIT: This gives false alarms on syntax like *--ptr */ + else if (line[n + 1] == ')') + { + /* REVISIT: This gives false alarms on syntax + * like *--ptr + */ - if (line[n - 1] != ' ' && line[n - 1] != '(') - { - ERROR("Operator/assignment must be preceded " - "with whitespace", lineno, n); - } + if (line[n - 1] != ' ' && line[n - 1] != '(') + { + ERROR("Operator/assignment must be preceded " + "with whitespace", lineno, n); + } - break; - } + break; + } - /* *= */ + /* *= */ - else if (line[n + 1] == '=') - { - check_spaces_leftright(line, lineno, n, n + 1); - n++; - } - else - { - /* A single '*' may be an binary operator, but - * it could also be a unary operator when used to deference - * a pointer. - */ + else if (line[n + 1] == '=') + { + check_spaces_leftright(line, lineno, n, n + 1); + n++; + } + else + { + /* A single '*' may be an binary operator, but + * it could also be a unary operator when used to + * deference a pointer. + */ - check_spaces_left(line, lineno, n); - } + check_spaces_left(line, lineno, n); + } - break; + break; - case '%': + case '%': - /* %= */ + /* %= */ - if (line[n + 1] == '=') - { - check_spaces_leftright(line, lineno, n, n + 1); - n++; - } - else - { - check_spaces_leftright(line, lineno, n, n); - } + if (line[n + 1] == '=') + { + check_spaces_leftright(line, lineno, n, n + 1); + n++; + } + else + { + check_spaces_leftright(line, lineno, n, n); + } - break; + break; - case '<': + case '<': - /* <=, <<, <<= */ + /* <=, <<, <<= */ - if (line[n + 1] == '=') - { - check_spaces_leftright(line, lineno, n, n + 1); - n++; - } - else if (line[n + 1] == '<') - { - if (line[n + 2] == '=') - { - check_spaces_leftright(line, lineno, n, n + 2); - n += 2; - } - else - { - check_spaces_leftright(line, lineno, n, n + 1); - n++; - } - } - else - { - check_spaces_leftright(line, lineno, n, n); - } + if (line[n + 1] == '=') + { + check_spaces_leftright(line, lineno, n, n + 1); + n++; + } + else if (line[n + 1] == '<') + { + if (line[n + 2] == '=') + { + check_spaces_leftright(line, lineno, n, n + 2); + n += 2; + } + else + { + check_spaces_leftright(line, lineno, n, n + 1); + n++; + } + } + else + { + check_spaces_leftright(line, lineno, n, n); + } - break; + break; - case '>': + case '>': - /* >=, >>, >>= */ + /* >=, >>, >>= */ - if (line[n + 1] == '=') - { - check_spaces_leftright(line, lineno, n, n + 1); - n++; - } - else if (line[n + 1] == '>') - { - if (line[n + 2] == '=') - { - check_spaces_leftright(line, lineno, n, n + 2); - n += 2; - } - else - { - check_spaces_leftright(line, lineno, n, n + 1); - n++; - } - } - else - { - check_spaces_leftright(line, lineno, n, n); - } + if (line[n + 1] == '=') + { + check_spaces_leftright(line, lineno, n, n + 1); + n++; + } + else if (line[n + 1] == '>') + { + if (line[n + 2] == '=') + { + check_spaces_leftright(line, lineno, n, n + 2); + n += 2; + } + else + { + check_spaces_leftright(line, lineno, n, n + 1); + n++; + } + } + else + { + check_spaces_leftright(line, lineno, n, n); + } - break; + break; - case '|': + case '|': - /* |=, || */ + /* |=, || */ - if (line[n + 1] == '=') - { - check_spaces_leftright(line, lineno, n, n + 1); - n++; - } - else if (line[n + 1] == '|') - { - check_spaces_leftright(line, lineno, n, n + 1); - n++; - } - else - { - check_spaces_leftright(line, lineno, n, n); - } + if (line[n + 1] == '=') + { + check_spaces_leftright(line, lineno, n, n + 1); + n++; + } + else if (line[n + 1] == '|') + { + check_spaces_leftright(line, lineno, n, n + 1); + n++; + } + else + { + check_spaces_leftright(line, lineno, n, n); + } - break; - case '^': + break; + case '^': - /* ^= */ + /* ^= */ - if (line[n + 1] == '=') - { - check_spaces_leftright(line, lineno, n, n + 1); - n++; - } - else - { - check_spaces_leftright(line, lineno, n, n); - } + if (line[n + 1] == '=') + { + check_spaces_leftright(line, lineno, n, n + 1); + n++; + } + else + { + check_spaces_leftright(line, lineno, n, n); + } - break; + break; - case '=': + case '=': - /* == */ + /* == */ - if (line[n + 1] == '=') - { - check_spaces_leftright(line, lineno, n, n + 1); - n++; - } - else - { - check_spaces_leftright(line, lineno, n, n); - } + if (line[n + 1] == '=') + { + check_spaces_leftright(line, lineno, n, n + 1); + n++; + } + else + { + check_spaces_leftright(line, lineno, n, n); + } - break; + break; - case '~': - check_spaces_left(line, lineno, n); - break; + case '~': + check_spaces_left(line, lineno, n); + break; - case '!': + case '!': - /* != */ + /* != */ - if (line[n + 1] == '=') - { - check_spaces_leftright(line, lineno, n, n + 1); - n++; - } + if (line[n + 1] == '=') + { + check_spaces_leftright(line, lineno, n, n + 1); + n++; + } - /* !! */ + /* !! */ - else if (line[n + 1] == '!') - { - check_spaces_left(line, lineno, n); - n++; - } - else - { - check_spaces_left(line, lineno, n); - } + else if (line[n + 1] == '!') + { + check_spaces_left(line, lineno, n); + n++; + } + else + { + check_spaces_left(line, lineno, n); + } - break; + break; - default: - break; + default: + break; } } } @@ -3540,6 +3552,7 @@ int main(int argc, char **argv, char **envp) */ int m = n; + if (line[m] == '\0' && m > 0) { m--; @@ -3741,7 +3754,7 @@ int main(int argc, char **argv, char **envp) if ((indent & 3) != 2 && rhcomment == 0) { - ERROR("Bad comment alignment", lineno, indent); + ERROR("Bad comment alignment", lineno, indent); } /* REVISIT: This screws up in cases where there is C code, @@ -3750,7 +3763,7 @@ int main(int argc, char **argv, char **envp) else if (line[indent + 1] != '*') { - ERROR("Missing asterisk in comment", lineno, indent); + ERROR("Missing asterisk in comment", lineno, indent); } } else if (line[indent] == '*') @@ -3768,7 +3781,7 @@ int main(int argc, char **argv, char **envp) if ((indent & 3) != 3 && bfunctions && dnest == 0 && rhcomment == 0) { - ERROR("Bad comment block alignment", lineno, indent); + ERROR("Bad comment block alignment", lineno, indent); } if (line[indent + 1] != ' ' && @@ -3776,8 +3789,8 @@ int main(int argc, char **argv, char **envp) line[indent + 1] != '\n' && line[indent + 1] != '/') { - ERROR("Invalid character after asterisk " - "in comment block", lineno, indent); + ERROR("Invalid character after asterisk " + "in comment block", lineno, indent); } } @@ -3798,9 +3811,9 @@ int main(int argc, char **argv, char **envp) { if (indent == 0 && strchr("\n#{}", line[0]) == NULL) { - /* Ignore if we are at global scope */ + /* Ignore if we are at global scope */ - if (prevbnest > 0) + if (prevbnest > 0) { bool blabel = false;
