xiaoxiang781216 commented on a change in pull request #12: nxstyle improvements with No tooling URL: https://github.com/apache/incubator-nuttx/pull/12#discussion_r361990500
########## File path: tools/nxstyle.c ########## @@ -43,53 +43,128 @@ #include <string.h> #include <strings.h> #include <ctype.h> +#include <unistd.h> +#include <libgen.h> /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ -#define LINE_SIZE 512 +#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 bool g_gonogo = false; +static int g_status = 0; +static char *g_file_name = ""; +static enum file_e g_file_type = UNKNOWN; /**************************************************************************** * 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 <maxline>] <filename>\n", progname); - fprintf(stderr, " %s -h\n", progname); + if (what) + { + fprintf(stderr, "%s\n", what); + } + + fprintf(stderr, "Usage: %s [-m <maxline>] [-s] [-g] <filename>\n", basename(progname)); + fprintf(stderr, " %s -h this help\n", basename(progname)); + fprintf(stderr, " %s -s silent\n", basename(progname)); + fprintf(stderr, " %s -g go no go output only\n", basename(progname)); 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_gonogo) + { + 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 * follow a left parenthesis at the beginning of a parenthetical list or * 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 - 1] != ' ' && line[ndx - 1] != + '(' && line[ndx - 1] != ')') { Review comment: can we change to: if (ndx-- >0 && line[ndx] != ' ' && line[ndx] != '(' && line[ndx] != ')')? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services