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.
This commit is contained in:
Gregory Nutt 2020-01-12 13:07:54 -03:00 committed by Alan Carvalho de Assis
parent bb5ad4df23
commit 6309165fe0
10 changed files with 157 additions and 32 deletions

View File

@ -58,9 +58,9 @@
#define NTP_MAX_ADJUST 500 #define NTP_MAX_ADJUST 500
/********************************************************************** /****************************************************************************
* Private Data * Private Data
**********************************************************************/ ****************************************************************************/
static struct timespec g_clock_wall_time; static struct timespec g_clock_wall_time;
static uint64_t g_clock_last_counter; static uint64_t g_clock_last_counter;

View File

@ -212,7 +212,8 @@ void nxmq_alloc_desblock(void)
/* Allocate a block of message descriptors */ /* 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) if (mqdesblock)
{ {
int i; int i;

View File

@ -381,8 +381,8 @@ static inline bool pg_startfill(void)
DEBUGASSERT(result == OK); DEBUGASSERT(result == OK);
#else #else
/* If CONFIG_PAGING_BLOCKINGFILL is defined, then up_fillpage is non-blocking /* 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 * call. In this case up_fillpage() will accept an additional argument: The
* fill worker thread will provide a callback function, pg_callback. * page fill worker thread will provide a callback function, pg_callback.
* *
* Calling up_fillpage will start an asynchronous page fill. 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 * ill be called when the page fill is finished (or an error occurs). This

View File

@ -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. /* The interrupts stay disabled until after we sample the
* This is because when debug is enabled and the console is used * errno. This is because when debug is enabled and the
* for debug output, then the errno can be altered by interrupt * console is used for debug output, then the errno can be
* handling! (bad) * altered by interrupt handling! (bad)
*/ */
leave_critical_section(flags); leave_critical_section(flags);

View File

@ -108,7 +108,8 @@ static const char g_pthreadname[] = "<pthread>";
* *
****************************************************************************/ ****************************************************************************/
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 #if CONFIG_TASK_NAME_SIZE > 0
/* Copy the pthread name into the TCB */ /* Copy the pthread name into the TCB */

View File

@ -46,9 +46,9 @@
#ifdef CONFIG_SCHED_CRITMONITOR #ifdef CONFIG_SCHED_CRITMONITOR
/************************************************************************************ /****************************************************************************
* Private Data * Private Data
************************************************************************************/ ****************************************************************************/
/* Start time when pre-emption disabled or critical section entered. */ /* 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]; static uint32_t g_crit_start[1];
#endif #endif
/************************************************************************************ /****************************************************************************
* Public Data * Public Data
************************************************************************************/ ****************************************************************************/
/* Maximum time with pre-emption disabled or within critical section. */ /* Maximum time with pre-emption disabled or within critical section. */

View File

@ -90,7 +90,8 @@ static FAR struct posix_timer_s *timer_allocate(void)
{ {
/* Allocate a new timer from the heap */ /* 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; pt_flags = 0;
} }

View File

@ -133,7 +133,9 @@ static int work_lpthread(int argc, char *argv[])
{ {
/* Perform garbage collection. This cleans-up memory de-allocations /* Perform garbage collection. This cleans-up memory de-allocations
* that were queued because they could not be freed in that execution * 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 * NOTE: If the work thread is disabled, this clean-up is performed by
* the IDLE thread (at a very, very low priority). * the IDLE thread (at a very, very low priority).
* *

View File

@ -316,12 +316,12 @@ nxstyle.c
Prints formatted messages that are classified as info, warn, error, Prints formatted messages that are classified as info, warn, error,
fatal. In a parsable format that can be used by editors and IDEs. fatal. In a parsable format that can be used by editors and IDEs.
Usage: nxstyle [-m <maxline>] [-v <level>] [-r <start,count>] <filename> Usage: nxstyle [-m <excess>] [-v <level>] [-r <start,count>] <filename>
nxstyle -h this help nxstyle -h this help
nxstyle -v <level> where level is nxstyle -v <level> where level is
0 - no output 0 - no output
1 - PASS/FAIL 1 - PASS/FAIL
2 - output each line (default) 2 - output each line (default)
See also indent.sh and uncrustify.cfg See also indent.sh and uncrustify.cfg

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* tools/nxstyle.c * 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 <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -43,6 +43,7 @@
#include <string.h> #include <string.h>
#include <strings.h> #include <strings.h>
#include <ctype.h> #include <ctype.h>
#include <limits.h>
#include <unistd.h> #include <unistd.h>
#include <libgen.h> #include <libgen.h>
@ -54,13 +55,15 @@
#define LINE_SIZE 512 #define LINE_SIZE 512
#define RANGE_NUMBER 4096 #define RANGE_NUMBER 4096
#define DEFAULT_WIDTH 78
#define FATAL(m, l, o) message(FATAL, (m), (l), (o)) #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 WARN(m, l, o) message(WARN, (m), (l), (o))
#define ERROR(m, l, o) message(ERROR, (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 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 * Private types
@ -95,7 +98,7 @@ enum file_e
static char *g_file_name = ""; static char *g_file_name = "";
static enum file_e g_file_type = UNKNOWN; 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_status = 0;
static int g_verbose = 2; static int g_verbose = 2;
static int g_rangenumber = 0; 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, "%s\n", what);
} }
fprintf(stderr, "Usage: %s [-m <maxline>] [-v <level>] [-r <start,count>] <filename>\n", fprintf(stderr, "Usage: %s [-m <excess>] [-v <level>] [-r <start,count>] <filename>\n",
basename(progname)); basename(progname));
fprintf(stderr, " %s -h this help\n", basename(progname)); fprintf(stderr, " %s -h this help\n", basename(progname));
fprintf(stderr, " %s -v <level> where level is\n", basename(progname)); fprintf(stderr, " %s -v <level> 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 * Public Functions
****************************************************************************/ ****************************************************************************/
@ -239,20 +349,24 @@ int main(int argc, char **argv, char **envp)
int rbrace_lineno; /* Last line containing a right brace */ int rbrace_lineno; /* Last line containing a right brace */
int externc_lineno; /* Last line where 'extern "C"' declared */ int externc_lineno; /* Last line where 'extern "C"' declared */
int linelen; /* Length of the line */ int linelen; /* Length of the line */
int excess;
int n; int n;
int i; int i;
int c; int c;
excess = 0;
while ((c = getopt(argc, argv, ":hv:gm:r:")) != -1) while ((c = getopt(argc, argv, ":hv:gm:r:")) != -1)
{ {
switch (c) switch (c)
{ {
case 'm': case 'm':
g_maxline = atoi(optarg); excess = atoi(optarg);
if (g_maxline < 1) if (excess < 1)
{ {
show_usage(argv[0], 1, "Bad value for <maxline>."); show_usage(argv[0], 1, "Bad value for <excess>.");
excess = 0;
} }
break; break;
case 'v': case 'v':
@ -261,7 +375,8 @@ int main(int argc, char **argv, char **envp)
{ {
show_usage(argv[0], 1, "Bad value for <level>."); show_usage(argv[0], 1, "Bad value for <level>.");
} }
break;
break;
case 'r': case 'r':
g_rangestart[g_rangenumber] = atoi(strtok(optarg, ",")); g_rangestart[g_rangenumber] = atoi(strtok(optarg, ","));
@ -300,6 +415,11 @@ int main(int argc, char **argv, char **envp)
return 1; return 1;
} }
/* Determine the line width */
g_maxline = get_line_width(instream) + excess;
rewind(instream);
/* Are we parsing a header file? */ /* Are we parsing a header file? */
ext = strrchr(g_file_name, '.'); 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 */ 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 */ bfunctions = false; /* True: In private or public functions */
bswitch = false; /* True: Within a switch statement */ bswitch = false; /* True: Within a switch statement */
bstring = false; /* True: Within a string */ bstring = false; /* True: Within a string */