From 002772c81e3340c04965f438dd6f4345b31892cd Mon Sep 17 00:00:00 2001 From: David Sidrane Date: Mon, 30 Dec 2019 10:34:43 -0800 Subject: [PATCH] nxstyle improvements with No tooling (#12) tools/nxstyle.c: Output compiler like error format. Added features Uses getops to pars command line. Supports -s silence all output -g provide a PASS fail message * tools/README.txt: Update with new nxstyle options --- tools/README.txt | 10 +- tools/nxstyle.c | 572 ++++++++++++++++++++++++++--------------------- 2 files changed, 320 insertions(+), 262 deletions(-) diff --git a/tools/README.txt b/tools/README.txt index aa7f7ab5c2..4adcfc4040 100644 --- a/tools/README.txt +++ b/tools/README.txt @@ -313,7 +313,15 @@ nxstyle.c standard. This program is completely ignorant of C syntax; it simply performs crude pattern matching to check the file. - Usage: nxstyle + Prints formatted messages that are classified as info, warn, error, + fatal. In a parsable format that can be used by editors and IDEs. + + Usage: nxstyle [-m ] [-v ] + nxstyle -h this help + nxstyle -v where level is + 0 - no output + 1 - PASS/FAIL + 2 - output each line (default) See also indent.sh and uncrustify.cfg diff --git a/tools/nxstyle.c b/tools/nxstyle.c index 4c162466f1..a7d9ce3f53 100644 --- a/tools/nxstyle.c +++ b/tools/nxstyle.c @@ -43,24 +43,107 @@ #include #include #include +#include +#include /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ -#define LINE_SIZE 512 +#define NXSTYLE_VERSION "0.01" + +#define LINE_SIZE 512 + +#define FATAL(m, l, o) message(FATAL, (m), (l), (o)) +#define FATALFL(m,s) message(FATAL, (m), -1, -1) +#define WARN(m, l, o) message(WARN, (m), (l), (o)) +#define ERROR(m, l, o) message(ERROR, (m), (l), (o)) +#define INFO(m, l, o) message(INFO, (m), (l), (o)) + +/**************************************************************************** + * Private types + ****************************************************************************/ + +enum class_e +{ + INFO, + WARN, + ERROR, + FATAL +}; + +const char *class_text[] = +{ + "info", + "warning", + "error", + "fatal" +}; + +enum file_e +{ + UNKNOWN, + C_HEADER, + C_SOURCE +}; + +/**************************************************************************** + * Private data + ****************************************************************************/ + +static char *g_file_name = ""; +static enum file_e g_file_type = UNKNOWN; +static int g_maxline = 78; +static int g_status = 0; +static int g_verbose = 2; /**************************************************************************** * Private Functions ****************************************************************************/ -static void show_usage(char *progname, int exitcode) +static void show_usage(char *progname, int exitcode, char *what) { - fprintf(stderr, "Usage: %s [-m ] \n", progname); - fprintf(stderr, " %s -h\n", progname); + fprintf(stderr, "%s version %s\n\n", basename(progname), NXSTYLE_VERSION); + if (what) + { + fprintf(stderr, "%s\n", what); + } + + fprintf(stderr, "Usage: %s [-m ] [-v ] \n", basename(progname)); + fprintf(stderr, " %s -h this help\n", basename(progname)); + fprintf(stderr, " %s -v where level is\n", basename(progname)); + fprintf(stderr, " 0 - no output\n"); + fprintf(stderr, " 1 - PASS/FAIL\n"); + fprintf(stderr, " 2 - output each line (default)\n"); exit(exitcode); } +static int message(enum class_e class, const char *text, int lineno, int ndx) +{ + FILE *out = stdout; + + if (class > INFO) + { + out = stderr; + g_status |= 1; + } + + if (g_verbose == 2) + { + if (lineno == -1 && ndx == -1) + { + fprintf(out, "%s:%s: %s\n", class_text[class], text, g_file_name); + } + else + { + fprintf(out, "%s:%d:%d: %s: %s\n", g_file_name, lineno, ndx, + class_text[class], text); + } + } + + return g_status; +} + static void check_spaces_left(char *line, int lineno, int ndx) { /* Unary operator should generally be preceded by a space but make also @@ -68,11 +151,10 @@ static void check_spaces_left(char *line, int lineno, int ndx) * expression or follow a right parentheses in the case of a cast. */ - if (ndx > 0 && line[ndx - 1] != ' ' && line[ndx - 1] != '(' && line[ndx - 1] != ')') + if (ndx-- > 0 && line[ndx] != ' ' && line[ndx] != '(' && line[ndx] != ')') { - fprintf(stderr, - "Operator/assignment must be preceded with whitespace at line %d:%d\n", - lineno, ndx); + ERROR("Operator/assignment must be preceded with whitespace", + lineno, ndx); } } @@ -80,16 +162,14 @@ static void check_spaces_leftright(char *line, int lineno, int ndx1, int ndx2) { if (ndx1 > 0 && line[ndx1 - 1] != ' ') { - fprintf(stderr, - "Operator/assignment must be preceded with whitespace at line %d:%d\n", - lineno, ndx1); + ERROR("Operator/assignment must be preceded with whitespace", + lineno, ndx1); } if (line[ndx2 + 1] != '\0' && line[ndx2 + 1] != '\n' && line[ndx2 + 1] != ' ') { - fprintf(stderr, - "Operator/assignment must be followed with whitespace at line %d:%d\n", - lineno, ndx2); + ERROR("Operator/assignment must be followed with whitespace", + lineno, ndx2); } } @@ -101,7 +181,7 @@ int main(int argc, char **argv, char **envp) { FILE *instream; /* File input stream */ char line[LINE_SIZE]; /* The current line being examined */ - char *filename; /* Name of the file to open */ + char buffer[100]; /* Localy format error strings */ char *lptr; /* Temporary pointer into line[] */ char *ext; /* Temporary file extension */ bool btabs; /* True: TAB characters found on the line */ @@ -114,7 +194,6 @@ int main(int argc, char **argv, char **envp) bool bquote; /* True: Backslash quoted character next */ bool bblank; /* Used to verify block comment terminator */ bool ppline; /* True: The next line the continuation of a pre-processor command */ - bool hdrfile; /* True: File is a header file */ bool bexternc; /* True: Within 'extern "C"' */ bool brhcomment; /* True: Comment to the right of code */ bool prevbrhcmt; /* True: previous line had comment to the right of code */ @@ -134,70 +213,77 @@ int main(int argc, char **argv, char **envp) int rbrace_lineno; /* Last line containing a right brace */ int externc_lineno; /* Last line where 'extern "C"' declared */ int linelen; /* Length of the line */ - int maxline; /* Lines longer that this generate warnings */ int n; int i; + int c; - maxline = 78; - filename = argv[1]; - - /* Usage: nxstyle [-m ] - * nxstyle -h - */ - - if (argc == 4) + while ((c = getopt(argc, argv, ":hv:gm:")) != -1) { - if (strcmp(argv[1], "-m") != 0) - { - fprintf(stderr, "Unrecognized argument\n"); - show_usage(argv[0], 1); - } + switch (c) + { + case 'm': + g_maxline = atoi(optarg); + if (g_maxline < 1) + { + show_usage(argv[0], 1, "Bad value for ."); + } + break; - maxline = atoi(argv[2]); - if (maxline < 1) - { - fprintf(stderr, "Bad value for \n"); - show_usage(argv[0], 1); - } + case 'v': + g_verbose = atoi(optarg); + if (g_verbose < 0 || g_verbose > 2) + { + show_usage(argv[0], 1, "Bad value for ."); + } + break; - filename = argv[3]; - } - else if (argc == 2) + 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) { - if (strcmp(argv[1], "-h") == 0) - { - show_usage(argv[0], 0); - } - } - else - { - fprintf(stderr, "Invalid number of arguments\n"); - show_usage(argv[0], 1); + show_usage(argv[0], 1, "No file name given."); } - instream = fopen(filename, "r"); + g_file_name = argv[optind]; + instream = fopen(g_file_name, "r"); + if (!instream) { - fprintf(stderr, "Failed to open %s\n", argv[1]); + FATALFL("Failed to open", g_file_name); return 1; } /* Are we parsing a header file? */ - hdrfile = false; - ext = strrchr(filename, '.'); + ext = strrchr(g_file_name, '.'); if (ext == 0) { - printf("No file extension\n"); + WARN("No file extension", 0 , 0); } else if (strcmp(ext, ".h") == 0) { - hdrfile = true; + g_file_type = C_HEADER; } - else if (strcmp(ext, ".c") != 0) + else if (strcmp(ext, ".c") == 0) { - printf("Unrecognized file extension: \"%s\"\n", ext + 1); + g_file_type = C_SOURCE; } btabs = false; /* True: TAB characters found on the line */ @@ -251,22 +337,20 @@ int main(int argc, char **argv, char **envp) { if (n > 0) { - fprintf(stderr, "Blank line contains whitespace at line %d\n", - lineno); + ERROR("Blank line contains whitespace", lineno, 1); } if (lineno == 1) { - fprintf(stderr, "File begins with a blank line\n"); + ERROR("File begins with a blank line", 1, 1); } else if (lineno == blank_lineno + 1) { - fprintf(stderr, "Too many blank lines at line %d\n", lineno); + ERROR("Too many blank lines", lineno, 1); } else if (lineno == lbrace_lineno + 1) { - fprintf(stderr, "Blank line follows left brace at line %d\n", - lineno); + ERROR("Blank line follows left brace", lineno, 1); } blank_lineno = lineno; @@ -296,9 +380,8 @@ int main(int argc, char **argv, char **envp) if (line[n] != '}' /* && line[n] != '#' */ && !prevbrhcmt) { - fprintf(stderr, - "Missing blank line after comment found at line %d\n", - comment_lineno); + ERROR("Missing blank line after comment", comment_lineno, + 1); } } @@ -309,50 +392,48 @@ int main(int argc, char **argv, char **envp) if (lineno == 1 && (line[n] != '/' || line[n + 1] != '*')) { - fprintf(stderr, - "Missing file header comment block at line 1\n"); + ERROR("Missing file header comment block", lineno, 1); } - /* Check for a blank line following a right brace */ + /* Check for a blank line following a right brace */ if (bfunctions && lineno == rbrace_lineno + 1) - { - /* Check if this line contains a right brace. A right brace - * must be followed by 'else', 'while', 'break', a blank line, - * another right brace, or a pre-processor directive like #endif - */ + { + /* Check if this line contains a right brace. A right brace + * must be followed by 'else', 'while', 'break', a blank line, + * another right brace, or a pre-processor directive like #endif + */ - if (strchr(line, '}') == NULL && line[n] != '#' && - strncmp(&line[n], "else", 4) != 0 && - strncmp(&line[n], "while", 5) != 0 && - strncmp(&line[n], "break", 5) != 0) - { - fprintf(stderr, - "Right brace must be followed by a blank line at line %d\n", - rbrace_lineno); - } + if (strchr(line, '}') == NULL && line[n] != '#' && + strncmp(&line[n], "else", 4) != 0 && + 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); + } - /* If the right brace is followed by a pre-proceeor command - * like #endif (but not #else or #elif), then set the right - * brace line number to the line number of the pre-processor - * command (it then must be followed by a blank line) - */ + /* If the right brace is followed by a pre-proceeor command + * like #endif (but not #else or #elif), then set the right + * brace line number to the line number of the pre-processor + * command (it then must be followed by a blank line) + */ - if (line[n] == '#') - { - int ii; + if (line[n] == '#') + { + int ii; - for (ii = n + 1; line[ii] != '\0' && isspace(line[ii]); ii++) - { - } + for (ii = n + 1; line[ii] != '\0' && isspace(line[ii]); ii++) + { + } - if (strncmp(&line[ii], "else", 4) != 0 && - strncmp(&line[ii], "elif", 4) != 0) - { - rbrace_lineno = lineno; - } - } - } + if (strncmp(&line[ii], "else", 4) != 0 && + strncmp(&line[ii], "elif", 4) != 0) + { + rbrace_lineno = lineno; + } + } + } } /* STEP 1: Find the indentation level and the start of real stuff on @@ -373,8 +454,7 @@ int main(int argc, char **argv, char **envp) { if (!btabs) { - fprintf(stderr, "TABs found. First detected at line %d:%d\n", - lineno, n); + ERROR("TABs found. First detected", lineno, n); btabs = true; } @@ -386,8 +466,8 @@ int main(int argc, char **argv, char **envp) { if (!bcrs) { - fprintf(stderr, "Carriage returns found. " - "First detected at line %d:%d\n", lineno, n); + ERROR("Carriage returns found. " + "First detected", lineno, n); bcrs = true; } } @@ -395,15 +475,17 @@ int main(int argc, char **argv, char **envp) default: { - fprintf(stderr, - "Unexpected white space character %02x found at line %d:%d\n", - line[n], lineno, n); + snprintf(buffer, sizeof(buffer), + "Unexpected white space character %02x found", + line[n]); + ERROR(buffer, lineno, n); } break; } } /* STEP 2: Detect some certain start of line conditions */ + /* Skip over pre-processor lines (or continuations of pre-processor * lines as indicated by ppline) */ @@ -457,9 +539,7 @@ int main(int argc, char **argv, char **envp) { /* TODO: This generates a false alarm if preceded by a label. */ - fprintf(stderr, - "Missing blank line before comment found at line %d\n", - lineno); + ERROR("Missing blank line before comment found", lineno, 1); } /* 'comment_lineno 'holds the line number of the last closing @@ -477,7 +557,9 @@ int main(int argc, char **argv, char **envp) (strcmp(line, " * Private Functions\n") == 0 || strcmp(line, " * Public Functions\n") == 0)) { - //fprintf(stderr, "Functions begin at line %d:%d\n", lineno, n); +#if 0 /* Ask Greg why it was commented out */ + ERROR("Functions begin", lineno, n); +#endif bfunctions = true; } @@ -538,6 +620,7 @@ int main(int argc, char **argv, char **envp) */ /* REVISIT: Also picks up function return types */ + /* REVISIT: Logic problem for nested data/function declarations */ if ((!bfunctions || bnest > 0) && dnest == 0) @@ -566,9 +649,7 @@ int main(int argc, char **argv, char **envp) { if (tmppnest == 0 && line[i] == ',') { - fprintf(stderr, - "Multiple data definitions on line %d\n", - lineno); + ERROR("Multiple data definitions", lineno, i + 1); break; } else if (line[i] == '(') @@ -606,15 +687,22 @@ int main(int argc, char **argv, char **envp) else if (strncmp(&line[indent], "break ", 6) == 0 || strncmp(&line[indent], "case ", 5) == 0 || -// strncmp(&line[indent], "case ", 5) == 0 || /* Part of switch */ +#if 0 /* Part of switch */ + strncmp(&line[indent], "case ", 5) == 0 || +#endif strncmp(&line[indent], "continue ", 9) == 0 || -// strncmp(&line[indent], "default ", 8) == 0 || /* Part of switch */ + +#if 0 /* Part of switch */ + strncmp(&line[indent], "default ", 8) == 0 || +#endif strncmp(&line[indent], "do ", 3) == 0 || strncmp(&line[indent], "else ", 5) == 0 || strncmp(&line[indent], "goto ", 5) == 0 || strncmp(&line[indent], "if ", 3) == 0 || strncmp(&line[indent], "return ", 7) == 0 || -// strncmp(&line[indent], "switch ", 7) == 0 || /* Doesn't follow pattern */ +#if 0 /* Doesn't follow pattern */ + strncmp(&line[indent], "switch ", 7) == 0 || +#endif strncmp(&line[indent], "while ", 6) == 0) { bstatm = true; @@ -638,19 +726,18 @@ int main(int argc, char **argv, char **envp) strncmp(&line[indent], "if(", 3) == 0 || strncmp(&line[indent], "while(", 6) == 0) { - fprintf(stderr, "Missing whitespace after keyword at line %d:%d\n", lineno, n); + ERROR("Missing whitespace after keyword", lineno, n); bstatm = true; } else if (strncmp(&line[indent], "for(", 4) == 0) { - fprintf(stderr, "Missing whitespace after keyword at line %d:%d\n", lineno, n); + ERROR("Missing whitespace after keyword", lineno, n); bfor = true; bstatm = true; } else if (strncmp(&line[indent], "switch(", 7) == 0) { - fprintf(stderr, "Missing whitespace after keyword at line %d:%d\n", - lineno, n); + ERROR("Missing whitespace after keyword", lineno, n); bswitch = true; } @@ -669,8 +756,7 @@ int main(int argc, char **argv, char **envp) { if (!btabs) { - fprintf(stderr, "TABs found. First detected at line %d:%d\n", - lineno, n); + ERROR("TABs found. First detected", lineno, n); btabs = true; } } @@ -678,16 +764,17 @@ int main(int argc, char **argv, char **envp) { if (!bcrs) { - fprintf(stderr, "Carriage returns found. " - "First detected at line %d:%d\n", lineno, n); + ERROR("Carriage returns found. " + "First detected", lineno, n); bcrs = true; } } else if (line[n] != ' ') { - fprintf(stderr, - "Unexpected white space character %02x found at line %d:%d\n", - line[n], lineno, n); + snprintf(buffer, sizeof(buffer), + "Unexpected white space character %02x found", + line[n]); + ERROR(buffer, lineno, n); } } @@ -805,15 +892,11 @@ int main(int argc, char **argv, char **envp) * considered false alarms. */ - fprintf(stderr, - "Mixed case identifier found at line %d:%d\n", - lineno, ident_index); + ERROR("Mixed case identifier found", lineno, ident_index); } else if (have_upper) { - fprintf(stderr, - "Upper case hex constant found at line %d:%d\n", - lineno, ident_index); + ERROR("Upper case hex constant found", lineno, ident_index); } } @@ -835,14 +918,11 @@ int main(int argc, char **argv, char **envp) { if (line[n + 2] == '\n') { - fprintf(stderr, "C comment opening on separate line at %d:%d\n", - lineno, n); + ERROR("C comment opening on separate line", lineno, n); } else if (!isspace((int)line[n + 2]) && line[n + 2] != '*') { - fprintf(stderr, - "Missing space after opening C comment at line %d:%d\n", - lineno, n); + ERROR("Missing space after opening C comment", lineno, n); } /* Increment the count of nested comments */ @@ -869,14 +949,12 @@ int main(int argc, char **argv, char **envp) { if (n < 2) { - fprintf(stderr, "Closing C comment not indented at line %d:%d\n", - lineno, n); + ERROR("Closing C comment not indented", lineno, n); } else if (!isspace((int)line[n + 1]) && line[n - 2] != '*') { - fprintf(stderr, - "Missing space before closing C comment at line %d:%d\n", - lineno, n); + ERROR("Missing space before closing C comment", lineno, + n); } /* Check for block comments that are not on a separate line. @@ -887,9 +965,8 @@ int main(int argc, char **argv, char **envp) if (prevncomment > 0 && !bblank && !brhcomment) { - fprintf(stderr, - "Block comment terminator must be on a separate line at line %d:%d\n", - lineno, n); + ERROR("Block comment terminator must be on a " + "separate line", lineno, n); } #if 0 @@ -899,9 +976,7 @@ int main(int argc, char **argv, char **envp) if (line[n + 1] != '\n') { - fprintf(stderr, - "Garbage on line after C comment at line %d:%d\n", - lineno, n); + ERROR("Garbage on line after C comment", lineno, n); } #endif @@ -937,9 +1012,7 @@ int main(int argc, char **argv, char **envp) */ ncomment = 0; - fprintf(stderr, - "Closing without opening comment at line %d:%d\n", - lineno, n); + ERROR("Closing without opening comment", lineno, n); } n++; @@ -955,8 +1028,7 @@ int main(int argc, char **argv, char **envp) if ((n < 5 || strncmp(&line[n - 5], "http://", 7) != 0) && (n < 6 || strncmp(&line[n - 6], "https://", 8) != 0)) { - fprintf(stderr, "C++ style comment at %d:%d\n", - lineno, n); + ERROR("C++ style comment", lineno, n); n++; continue; } @@ -1018,18 +1090,15 @@ int main(int argc, char **argv, char **envp) if (dnest == 0 || !bfunctions) { - fprintf(stderr, - "Left bracket not on separate line at %d:%d\n", - lineno, n); + ERROR("Left bracket not on separate line", lineno, + n); } } else if (line[n + 1] != '\n') { if (dnest == 0) { - fprintf(stderr, - "Garbage follows left bracket at line %d:%d\n", - lineno, n); + ERROR("Garbage follows left bracket", lineno, n); } } @@ -1063,7 +1132,7 @@ int main(int argc, char **argv, char **envp) if (bnest < 1) { - fprintf(stderr, "Unmatched right brace at line %d:%d\n", lineno, n); + ERROR("Unmatched right brace", lineno, n); } else { @@ -1093,8 +1162,7 @@ int main(int argc, char **argv, char **envp) { if (dnest == 0) { - fprintf(stderr, - "Right bracket not on separate line at %d:%d\n", + ERROR("Right bracket not on separate line", lineno, n); } } @@ -1150,48 +1218,40 @@ int main(int argc, char **argv, char **envp) if (strncmp(&line[sndx], "while", 5) == 0) { - fprintf(stderr, - "'while' must be on a separate line %d:%d\n", + ERROR("'while' must be on a separate line", lineno, sndx); } else if (line[endx] == ',') { - fprintf(stderr, - "Multiple data definitions on line %d:%d\n", + ERROR("Multiple data definitions on line", lineno, endx); } else if (line[endx] == ';') { if (whitespace) { - fprintf(stderr, - "Space precedes semi-colon at line %d:%d\n", - lineno, endx); + ERROR("Space precedes semi-colon", + lineno, endx); } } else { - fprintf(stderr, - "Garbage follows right bracket at line %d:%d\n", - lineno, n); + ERROR("Garbage follows right bracket", + lineno, n); } } else { - fprintf(stderr, - "Garbage follows right bracket at line %d:%d\n", - lineno, n); + ERROR("Garbage follows right bracket", lineno, n); } } /* The right brace should not be preceded with a a blank line */ - if (lineno == blank_lineno + 1) { - fprintf(stderr, - "Blank line precedes right brace at line %d\n", - lineno); + ERROR("Blank line precedes right brace at line", + lineno, 1); } rbrace_lineno = lineno; @@ -1208,11 +1268,9 @@ int main(int argc, char **argv, char **envp) /* Check for inappropriate space around parentheses */ - if (line[n + 1] == ' ' /* && !bfor */) + if (line[n + 1] == ' ') /* && !bfor */ { - fprintf(stderr, - "Space follows left parenthesis at line %d:%d\n", - lineno, n); + ERROR("Space follows left parenthesis", lineno, n); } } break; @@ -1223,8 +1281,7 @@ int main(int argc, char **argv, char **envp) if (pnest < 1) { - fprintf(stderr, "Unmatched right parentheses at line %d:%d\n", - lineno, n); + ERROR("Unmatched right parentheses", lineno, n); pnest = 0; } else @@ -1238,9 +1295,7 @@ int main(int argc, char **argv, char **envp) if (n > 0 && n != indent && line[n - 1] == ' ' && !bfor) { - fprintf(stderr, - "Space precedes right parenthesis at line %d:%d\n", - lineno, n); + ERROR("Space precedes right parenthesis", lineno, n); } } break; @@ -1251,9 +1306,7 @@ int main(int argc, char **argv, char **envp) { if (line[n + 1] == ' ') { - fprintf(stderr, - "Space follows left bracket at line %d:%d\n", - lineno, n); + ERROR("Space follows left bracket", lineno, n); } } break; @@ -1262,9 +1315,7 @@ int main(int argc, char **argv, char **envp) { if (n > 0 && line[n - 1] == ' ') { - fprintf(stderr, - "Space precedes right bracket at line %d:%d\n", - lineno, n); + ERROR("Space precedes right bracket", lineno, n); } } break; @@ -1275,8 +1326,7 @@ int main(int argc, char **argv, char **envp) { if (!isspace((int)line[n + 1])) { - fprintf(stderr, "Missing whitespace after semicolon at line %d:%d\n", - lineno, n); + ERROR("Missing whitespace after semicolon", lineno, n); } /* Semicolon terminates a declaration/definition if there @@ -1296,8 +1346,7 @@ int main(int argc, char **argv, char **envp) { if (!isspace((int)line[n + 1])) { - fprintf(stderr, "Missing whitespace after comma at line %d:%d\n", - lineno, n); + ERROR("Missing whitespace after comma", lineno, n); } } break; @@ -1327,6 +1376,7 @@ int main(int argc, char **argv, char **envp) /* Check for space around various operators */ case '-': + /* ->, -- */ if (line[n + 1] == '>' || line[n + 1] == '-') @@ -1353,6 +1403,7 @@ int main(int argc, char **argv, char **envp) break; case '+': + /* ++ */ if (line[n + 1] == '+') @@ -1402,6 +1453,7 @@ int main(int argc, char **argv, char **envp) break; case '/': + /* C comment terminator*/ if (line[n - 1] == '*') @@ -1418,7 +1470,7 @@ int main(int argc, char **argv, char **envp) if ((n < 5 || strncmp(&line[n - 5], "http://", 7) != 0) && (n < 6 || strncmp(&line[n - 6], "https://", 8) != 0)) { - fprintf(stderr, "C++ style comment on at %d:%d\n", + ERROR("C++ style comment on at %d:%d\n", lineno, n); } @@ -1443,6 +1495,7 @@ int main(int argc, char **argv, char **envp) break; case '*': + /* *\/, ** */ if (line[n] == '*' && @@ -1470,9 +1523,8 @@ int main(int argc, char **argv, char **envp) if (line[n - 1] != ' ') { - fprintf(stderr, - "Operator/assignment must be preceded with whitespace at line %d:%d\n", - lineno, n); + ERROR("Operator/assignment must be preceded " + "with whitespace", lineno, n); } break; @@ -1498,6 +1550,7 @@ int main(int argc, char **argv, char **envp) break; case '%': + /* %= */ if (line[n + 1] == '=') @@ -1513,6 +1566,7 @@ int main(int argc, char **argv, char **envp) break; case '<': + /* <=, <<, <<= */ if (line[n + 1] == '=') @@ -1541,6 +1595,7 @@ int main(int argc, char **argv, char **envp) break; case '>': + /* >=, >>, >>= */ if (line[n + 1] == '=') @@ -1569,6 +1624,7 @@ int main(int argc, char **argv, char **envp) break; case '|': + /* |=, || */ if (line[n + 1] == '=') @@ -1589,6 +1645,7 @@ int main(int argc, char **argv, char **envp) break; case '^': + /* ^= */ if (line[n + 1] == '=') @@ -1604,6 +1661,7 @@ int main(int argc, char **argv, char **envp) break; case '=': + /* == */ if (line[n + 1] == '=') @@ -1623,6 +1681,7 @@ int main(int argc, char **argv, char **envp) break; case '!': + /* != */ if (line[n + 1] == '=') @@ -1661,18 +1720,22 @@ int main(int argc, char **argv, char **envp) if (n > 1 && isspace((int)line[n - 1]) && line[n - 1] != '\r') { - fprintf(stderr, - "Dangling whitespace at the end of line %d:%d\n", - lineno, n); + ERROR("Dangling whitespace at the end of line", lineno, n); } /* Check for long lines */ - if (n > maxline) + if (n > g_maxline) { - fprintf(stderr, - "Long line found at %d:%d\n", - lineno, n); + if (g_file_type == C_SOURCE) + { + ERROR("Long line found", lineno, n); + } + else if (g_file_type == C_HEADER) + + { + WARN("Long line found", lineno, n); + } } } @@ -1695,8 +1758,7 @@ int main(int argc, char **argv, char **envp) if (ncomment > 1 || !brhcomment) { - fprintf(stderr, "No indentation line %d:%d\n", - lineno, indent); + ERROR("No indentation line", lineno, indent); } } else if (indent == 1 && line[0] == ' ' && line[1] == '*') @@ -1705,20 +1767,17 @@ int main(int argc, char **argv, char **envp) } else if (indent > 0 && line[indent] == '\n') { - fprintf(stderr, "Whitespace on blank line at line %d:%d\n", - lineno, indent); + ERROR("Whitespace on blank line", lineno, indent); } else if (indent > 0 && indent < 2) { if (bnest > 0) { - fprintf(stderr, "Insufficient indentation line %d:%d\n", - lineno, indent); + ERROR("Insufficient indentation", lineno, indent); } else { - fprintf(stderr, "Expected indentation line %d:%d\n", - lineno, indent); + ERROR("Expected indentation line", lineno, indent); } } else if (indent > 0 && !bswitch) @@ -1732,20 +1791,16 @@ int main(int argc, char **argv, char **envp) if ((indent & 3) != 2 && !brhcomment) { - fprintf(stderr, - "Bad comment alignment at line %d:%d\n", - lineno, indent); + ERROR("Bad comment alignment", lineno, indent); } /* REVISIT: This screws up in cases where there is C code, * followed by a comment that continues on the next line. */ - else if (line[indent+1] != '*') + else if (line[indent + 1] != '*') { - fprintf(stderr, - "Missing asterisk in comment at line %d:%d\n", - lineno, indent); + ERROR("Missing asterisk in comment", lineno, indent); } } else if (line[indent] == '*') @@ -1759,11 +1814,10 @@ int main(int argc, char **argv, char **envp) * Those may be unaligned. */ - if ((indent & 3) != 3 && bfunctions && dnest == 0 && !brhcomment) + if ((indent & 3) != 3 && bfunctions && dnest == 0 && + !brhcomment) { - fprintf(stderr, - "Bad comment block alignment at line %d:%d\n", - lineno, indent); + ERROR("Bad comment block alignment", lineno, indent); } if (line[indent + 1] != ' ' && @@ -1771,9 +1825,8 @@ int main(int argc, char **argv, char **envp) line[indent + 1] != '\n' && line[indent + 1] != '/') { - fprintf(stderr, - "Invalid character after asterisk in comment block at line %d:%d\n", - lineno, indent); + ERROR("Invalid character after asterisk " + "in comment block", lineno, indent); } } @@ -1783,8 +1836,7 @@ int main(int argc, char **argv, char **envp) else if (prevncomment > 0) { - fprintf(stderr, "Missing asterisk in comment block at line %d:%d\n", - lineno, indent); + ERROR("Missing asterisk in comment block", lineno, indent); } } } @@ -1803,14 +1855,14 @@ int main(int argc, char **argv, char **envp) if (isalpha((int)line[indent])) { - for (i = indent + 1; isalnum((int)line[i]) || line[i] == '_'; i++); + for (i = indent + 1; isalnum((int)line[i]) || + line[i] == '_'; i++); blabel = (line[i] == ':'); } if (!blabel && !bexternc) { - fprintf(stderr, "No indentation line %d:%d\n", - lineno, indent); + ERROR("No indentation line", lineno, indent); } } } @@ -1820,13 +1872,11 @@ int main(int argc, char **argv, char **envp) } else if (indent > 0 && line[indent] == '\n') { - fprintf(stderr, "Whitespace on blank line at line %d:%d\n", - lineno, indent); + ERROR("Whitespace on blank line", lineno, indent); } else if (indent > 0 && indent < 2) { - fprintf(stderr, "Insufficient indentation line %d:%d\n", - lineno, indent); + ERROR("Insufficient indentation line", lineno, indent); } else if (line[indent] == '{') { @@ -1837,13 +1887,11 @@ int main(int argc, char **argv, char **envp) if (!bfunctions && (indent & 1) != 0) { - fprintf(stderr, "Bad left brace alignment at line %d:%d\n", - lineno, indent); + ERROR("Bad left brace alignment", lineno, indent); } else if ((indent & 3) != 0 && !bswitch && dnest == 0) { - fprintf(stderr, "Bad left brace alignment at line %d:%d\n", - lineno, indent); + ERROR("Bad left brace alignment", lineno, indent); } } else if (line[indent] == '}') @@ -1855,20 +1903,18 @@ int main(int argc, char **argv, char **envp) if (!bfunctions && (indent & 1) != 0) { - fprintf(stderr, "right left brace alignment at line %d:%d\n", - lineno, indent); + ERROR("right left brace alignment", lineno, indent); } else if ((indent & 3) != 0 && !bswitch && prevdnest == 0) { - fprintf(stderr, "Bad right brace alignment at line %d:%d\n", - lineno, indent); + ERROR("Bad right brace alignment", lineno, indent); } } else if (indent > 0) { /* REVISIT: Generates false alarms when a statement continues on - * the next line. The bstatm check limits to lines beginning with - * C keywords. + * the next line. The bstatm check limits to lines beginning + * with C keywords. * REVISIT: The bstatm check will not detect statements that * do not begin with a C keyword (such as assignment statements). * REVISIT: Generates false alarms on comments at the end of @@ -1884,8 +1930,7 @@ int main(int argc, char **argv, char **envp) { if ((indent & 3) != 2) { - fprintf(stderr, "Bad alignment at line %d:%d\n", - lineno, indent); + ERROR("Bad alignment", lineno, indent); } } @@ -1896,24 +1941,29 @@ int main(int argc, char **argv, char **envp) else if (indent == 1 || indent == 3) { - fprintf(stderr, "Small odd alignment at line %d:%d\n", - lineno, indent); + ERROR("Small odd alignment", lineno, indent); } } } } - if (!bfunctions && !hdrfile) + if (!bfunctions && g_file_type == C_SOURCE) { - fprintf(stderr, "ERROR: \"Private/Public Functions\" not found!\n"); - fprintf(stderr, " File could not be checked.\n"); + ERROR("\"Private/Public Functions\" not found!" + " File was not be checked", lineno, 1); } if (ncomment > 0 || bstring) { - fprintf(stderr, "ERROR: In a comment/string at end of file\n"); + ERROR("Comment or string found at end of file", lineno, 1); } fclose(instream); - return 0; + if (g_verbose == 1) + { + fprintf(stderr, "%s: %s nxstyle check\n", g_file_name, + g_status == 0 ? "PASSED" : "FAILED"); + } + + return g_status; }