From 6309165fe052e2e849f6d5478957910f05132800 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 12 Jan 2020 13:07:54 -0300 Subject: [PATCH] tools/nxstyle.c: Add automatic detection of line width based on examining the width of all block comments. Includes a check to assure that all block comments use the same line width. Verified against all .c files under /sched. There were a few cosmetic changes to the coding style under /sched to account to new, correctly detected problems in the /sched files. --- sched/clock/clock_timekeeping.c | 4 +- sched/mqueue/mq_initialize.c | 3 +- sched/paging/pg_worker.c | 4 +- sched/pthread/pthread_condtimedwait.c | 8 +- sched/pthread/pthread_create.c | 3 +- sched/sched/sched_critmonitor.c | 8 +- sched/timer/timer_create.c | 3 +- sched/wqueue/kwork_lpthread.c | 4 +- tools/README.txt | 12 +-- tools/nxstyle.c | 140 ++++++++++++++++++++++++-- 10 files changed, 157 insertions(+), 32 deletions(-) diff --git a/sched/clock/clock_timekeeping.c b/sched/clock/clock_timekeeping.c index 50dd18e384..a14ba58080 100644 --- a/sched/clock/clock_timekeeping.c +++ b/sched/clock/clock_timekeeping.c @@ -58,9 +58,9 @@ #define NTP_MAX_ADJUST 500 -/********************************************************************** +/**************************************************************************** * Private Data - **********************************************************************/ + ****************************************************************************/ static struct timespec g_clock_wall_time; static uint64_t g_clock_last_counter; diff --git a/sched/mqueue/mq_initialize.c b/sched/mqueue/mq_initialize.c index 626e32480a..1083ca218b 100644 --- a/sched/mqueue/mq_initialize.c +++ b/sched/mqueue/mq_initialize.c @@ -212,7 +212,8 @@ void nxmq_alloc_desblock(void) /* Allocate a block of message descriptors */ - mqdesblock = (FAR struct mq_des_block_s *)kmm_malloc(sizeof(struct mq_des_block_s)); + mqdesblock = (FAR struct mq_des_block_s *) + kmm_malloc(sizeof(struct mq_des_block_s)); if (mqdesblock) { int i; diff --git a/sched/paging/pg_worker.c b/sched/paging/pg_worker.c index bdcb33722b..4a5f3f1eb0 100644 --- a/sched/paging/pg_worker.c +++ b/sched/paging/pg_worker.c @@ -381,8 +381,8 @@ static inline bool pg_startfill(void) DEBUGASSERT(result == OK); #else /* If CONFIG_PAGING_BLOCKINGFILL is defined, then up_fillpage is non-blocking - * call. In this case up_fillpage() will accept an additional argument: The page - * fill worker thread will provide a callback function, pg_callback. + * call. In this case up_fillpage() will accept an additional argument: The + * page fill worker thread will provide a callback function, pg_callback. * * Calling up_fillpage will start an asynchronous page fill. pg_callback * ill be called when the page fill is finished (or an error occurs). This diff --git a/sched/pthread/pthread_condtimedwait.c b/sched/pthread/pthread_condtimedwait.c index f130f83efe..24ec24ded9 100644 --- a/sched/pthread/pthread_condtimedwait.c +++ b/sched/pthread/pthread_condtimedwait.c @@ -307,10 +307,10 @@ int pthread_cond_timedwait(FAR pthread_cond_t *cond, FAR pthread_mutex_t *mutex, } } - /* The interrupts stay disabled until after we sample the errno. - * This is because when debug is enabled and the console is used - * for debug output, then the errno can be altered by interrupt - * handling! (bad) + /* The interrupts stay disabled until after we sample the + * errno. This is because when debug is enabled and the + * console is used for debug output, then the errno can be + * altered by interrupt handling! (bad) */ leave_critical_section(flags); diff --git a/sched/pthread/pthread_create.c b/sched/pthread/pthread_create.c index 657e791a7c..6b9106ef80 100644 --- a/sched/pthread/pthread_create.c +++ b/sched/pthread/pthread_create.c @@ -108,7 +108,8 @@ static const char g_pthreadname[] = ""; * ****************************************************************************/ -static inline void pthread_argsetup(FAR struct pthread_tcb_s *tcb, pthread_addr_t arg) +static inline void pthread_argsetup(FAR struct pthread_tcb_s *tcb, + pthread_addr_t arg) { #if CONFIG_TASK_NAME_SIZE > 0 /* Copy the pthread name into the TCB */ diff --git a/sched/sched/sched_critmonitor.c b/sched/sched/sched_critmonitor.c index 933756388e..897e5ce471 100644 --- a/sched/sched/sched_critmonitor.c +++ b/sched/sched/sched_critmonitor.c @@ -46,9 +46,9 @@ #ifdef CONFIG_SCHED_CRITMONITOR -/************************************************************************************ +/**************************************************************************** * Private Data - ************************************************************************************/ + ****************************************************************************/ /* Start time when pre-emption disabled or critical section entered. */ @@ -60,9 +60,9 @@ static uint32_t g_premp_start[1]; static uint32_t g_crit_start[1]; #endif -/************************************************************************************ +/**************************************************************************** * Public Data - ************************************************************************************/ + ****************************************************************************/ /* Maximum time with pre-emption disabled or within critical section. */ diff --git a/sched/timer/timer_create.c b/sched/timer/timer_create.c index 795153a878..b19170d6d0 100644 --- a/sched/timer/timer_create.c +++ b/sched/timer/timer_create.c @@ -90,7 +90,8 @@ static FAR struct posix_timer_s *timer_allocate(void) { /* Allocate a new timer from the heap */ - ret = (FAR struct posix_timer_s *)kmm_malloc(sizeof(struct posix_timer_s)); + ret = (FAR struct posix_timer_s *) + kmm_malloc(sizeof(struct posix_timer_s)); pt_flags = 0; } diff --git a/sched/wqueue/kwork_lpthread.c b/sched/wqueue/kwork_lpthread.c index 4ae6ca32ff..ef2f12a7e1 100644 --- a/sched/wqueue/kwork_lpthread.c +++ b/sched/wqueue/kwork_lpthread.c @@ -133,7 +133,9 @@ static int work_lpthread(int argc, char *argv[]) { /* Perform garbage collection. This cleans-up memory de-allocations * that were queued because they could not be freed in that execution - * context (for example, if the memory was freed from an interrupt handler). + * context (for example, if the memory was freed from an interrupt + * handler). + * * NOTE: If the work thread is disabled, this clean-up is performed by * the IDLE thread (at a very, very low priority). * diff --git a/tools/README.txt b/tools/README.txt index e297caf2d6..cf357fb632 100644 --- a/tools/README.txt +++ b/tools/README.txt @@ -316,12 +316,12 @@ nxstyle.c 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 ] [-r ] - nxstyle -h this help - nxstyle -v where level is - 0 - no output - 1 - PASS/FAIL - 2 - output each line (default) + Usage: nxstyle [-m ] [-v ] [-r ] + 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 2a109ea7cc..c7155afd8a 100644 --- a/tools/nxstyle.c +++ b/tools/nxstyle.c @@ -1,7 +1,7 @@ /**************************************************************************** * tools/nxstyle.c * - * Copyright (C) 2015, 2018-2019 Gregory Nutt. All rights reserved. + * Copyright (C) 2015, 2018-2020 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -43,6 +43,7 @@ #include #include #include +#include #include #include @@ -54,13 +55,15 @@ #define LINE_SIZE 512 #define RANGE_NUMBER 4096 +#define DEFAULT_WIDTH 78 #define FATAL(m, l, o) message(FATAL, (m), (l), (o)) -#define FATALFL(m,s) message(FATAL, (m), -1, -1) +#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 ERRORFL(m, s) message(ERROR, (m), -1, -1) #define INFO(m, l, o) message(INFO, (m), (l), (o)) -#define INFOFL(m,s) message(INFO, (m), -1, -1) +#define INFOFL(m, s) message(INFO, (m), -1, -1) /**************************************************************************** * Private types @@ -95,7 +98,7 @@ enum file_e static char *g_file_name = ""; static enum file_e g_file_type = UNKNOWN; -static int g_maxline = 78; +static int g_maxline = DEFAULT_WIDTH; static int g_status = 0; static int g_verbose = 2; static int g_rangenumber = 0; @@ -114,7 +117,7 @@ static void show_usage(char *progname, int exitcode, char *what) fprintf(stderr, "%s\n", what); } - fprintf(stderr, "Usage: %s [-m ] [-v ] [-r ] \n", + fprintf(stderr, "Usage: %s [-m ] [-v ] [-r ] \n", basename(progname)); fprintf(stderr, " %s -h this help\n", basename(progname)); fprintf(stderr, " %s -v where level is\n", basename(progname)); @@ -199,6 +202,113 @@ static void check_spaces_leftright(char *line, int lineno, int ndx1, int ndx2) } } +static int block_comment_width(char *line) +{ + int b; + int e; + int n; + + /* Skip over any leading whitespace on the line */ + + for (b = 0; isspace(line[b]); b++) + { + } + + /* Skip over any trailing whitespace at the end of the line */ + + for (e = strlen(line) - 1; isspace(line[e]); e--) + { + } + + /* Number of characters on the line */ + + n = e - b + 1; + if (n < 4) + { + return 0; + } + + /* The first line of a block comment starts with "[slash]***" and ends with + * "***" + */ + + if (strncmp(&line[b], "/***", 4) == 0 && + strncmp(&line[e - 2], "***", 3) == 0) + { + /* Return the the length of the line up to the final '*' */ + + return e + 1; + } + + /* The last line of a block begins with whitespace then "***" and ends + * with "***[slash]" + */ + + if (strncmp(&line[b], "***", 3) == 0 && + strncmp(&line[e - 4], "***/", 4) == 0) + { + /* Return the the length of the line up to the final '*' */ + + return e; + } + + /* But there is also a special single line comment that begins with "[slash]* " + * and ends with "***[slash]" + */ + + if (strncmp(&line[b], "/*", 2) == 0 && + strncmp(&line[e - 4], "***/", 4) == 0) + { + /* Return the the length of the line up to the final '*' */ + + return e; + } + + /* Return zero if the line is not the first or last line of a block + * comment. + */ + + return 0; +} + +static int get_line_width(FILE *instream) +{ + char line[LINE_SIZE]; /* The current line being examined */ + int max = 0; + int min = INT_MAX; + int len; + + while (fgets(line, LINE_SIZE, instream)) + { + len = block_comment_width(line); + if (len > 0) + { + if (len > max) + { + max = len; + } + + if (len < min) + { + min = len; + } + } + } + + if (max < min) + { + ERRORFL("No block comments found", g_file_name); + return DEFAULT_WIDTH; + } + else if (max != min) + { + ERRORFL("Block comments have different lengths", g_file_name); + return DEFAULT_WIDTH; + } + + return min; +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -239,20 +349,24 @@ 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 excess; int n; int i; int c; + excess = 0; while ((c = getopt(argc, argv, ":hv:gm:r:")) != -1) { switch (c) { case 'm': - g_maxline = atoi(optarg); - if (g_maxline < 1) + excess = atoi(optarg); + if (excess < 1) { - show_usage(argv[0], 1, "Bad value for ."); + show_usage(argv[0], 1, "Bad value for ."); + excess = 0; } + break; case 'v': @@ -261,7 +375,8 @@ int main(int argc, char **argv, char **envp) { show_usage(argv[0], 1, "Bad value for ."); } - break; + + break; case 'r': g_rangestart[g_rangenumber] = atoi(strtok(optarg, ",")); @@ -300,6 +415,11 @@ int main(int argc, char **argv, char **envp) return 1; } + /* Determine the line width */ + + g_maxline = get_line_width(instream) + excess; + rewind(instream); + /* Are we parsing a header file? */ ext = strrchr(g_file_name, '.'); @@ -324,7 +444,7 @@ int main(int argc, char **argv, char **envp) } btabs = false; /* True: TAB characters found on the line */ - bcrs = false; /* True: Carriable return found on the line */ + bcrs = false; /* True: Carriage return found on the line */ bfunctions = false; /* True: In private or public functions */ bswitch = false; /* True: Within a switch statement */ bstring = false; /* True: Within a string */