This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 780366de62 tools: nxstyle if statement check
780366de62 is described below

commit 780366de6269227fced5d8395d192b6d20be1c36
Author: Luchian Mihai <[email protected]>
AuthorDate: Mon Jul 29 20:59:32 2024 +0300

    tools: nxstyle if statement check
    
    nxstyle will print an error on a single line if statement
---
 tools/nxstyle.c | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)

diff --git a/tools/nxstyle.c b/tools/nxstyle.c
index 2c736b9146..358aefa9e7 100644
--- a/tools/nxstyle.c
+++ b/tools/nxstyle.c
@@ -1084,6 +1084,7 @@ int main(int argc, char **argv, char **envp)
   bool bfunctions;      /* True: In private or public functions */
   bool bstatm;          /* True: This line is beginning of a statement */
   bool bfor;            /* True: This line is beginning of a 'for' statement */
+  bool bif;             /* True: This line is beginning of a 'if' statement */
   bool bswitch;         /* True: Within a switch statement */
   bool bstring;         /* True: Within a string */
   bool bquote;          /* True: Backslash quoted character next */
@@ -1256,6 +1257,7 @@ int main(int argc, char **argv, char **envp)
       bstatm       = false;    /* True: This line is beginning of a
                                 * statement */
       bfor         = false;    /* REVISIT: Implies for() is all on one line */
+      bif          = false;    /* True: This line is beginning of a 'if' 
statement */
 
       /* If we are not in a comment, then this certainly is not a right-hand
        * comment.
@@ -1772,7 +1774,10 @@ int main(int argc, char **argv, char **envp)
       /* Check for a single line comment */
 
       linelen = strlen(line);
-      if (linelen >= 5)      /* Minimum is slash, star, star, slash, newline */
+
+      /* Minimum is slash, star, star, slash, newline */
+
+      if (linelen >= 5)
         {
           lptr = strstr(line, "*/");
           if (line[indent] == '/' && line[indent + 1] == '*' &&
@@ -1964,7 +1969,6 @@ int main(int argc, char **argv, char **envp)
                    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 ||
     #if 0 /* Doesn't follow pattern */
                    strncmp(&line[indent], "switch ", 7) == 0 ||
@@ -1974,6 +1978,12 @@ int main(int argc, char **argv, char **envp)
               bstatm = true;
             }
 
+          else if(strncmp(&line[indent], "if ", 3) == 0)
+            {
+              bif    = true;
+              bstatm = true;
+            }
+
           /* Spacing works a little differently for and switch statements */
 
           else if (strncmp(&line[indent], "for ", 4) == 0)
@@ -1989,12 +1999,17 @@ int main(int argc, char **argv, char **envp)
           /* Also check for C keywords with missing white space */
 
           else if (strncmp(&line[indent], "do(", 3) == 0 ||
-                   strncmp(&line[indent], "if(", 3) == 0 ||
                    strncmp(&line[indent], "while(", 6) == 0)
             {
               ERROR("Missing whitespace after keyword", lineno, n);
               bstatm = true;
             }
+          else if (strncmp(&line[indent], "if(", 3) == 0)
+            {
+              ERROR("Missing whitespace after keyword", lineno, n);
+              bif   = true;
+              bstatm = true;
+            }
           else if (strncmp(&line[indent], "for(", 4) == 0)
             {
               ERROR("Missing whitespace after keyword", lineno, n);
@@ -2584,7 +2599,7 @@ int main(int argc, char **argv, char **envp)
 
                    /* Check for inappropriate space around parentheses */
 
-                    if (line[n + 1] == ' ')  /* && !bfor */
+                    if (line[n + 1] == ' ')
                       {
                          ERROR("Space follows left parenthesis", lineno, n);
                       }
@@ -2596,11 +2611,11 @@ int main(int argc, char **argv, char **envp)
                     /* Decrease the parenthetical nesting level */
 
                     if (pnest < 1)
-                     {
-                       ERROR("Unmatched right parentheses", lineno, n);
-                       pnest = 0;
-                     }
-                   else
+                      {
+                        ERROR("Unmatched right parentheses", lineno, n);
+                        pnest = 0;
+                      }
+                    else
                      {
                        pnest--;
                      }
@@ -2611,7 +2626,12 @@ int main(int argc, char **argv, char **envp)
 
                     if (n > 0 && n != indent && line[n - 1] == ' ' && !bfor)
                       {
-                         ERROR("Space precedes right parenthesis", lineno, n);
+                        ERROR("Space precedes right parenthesis", lineno, n);
+                      }
+
+                    if (bif == true && pnest == 0 && line[n + 1] != '\n')
+                      {
+                        ERROR("If statement followed by garbage", lineno, n);
                       }
                   }
                   break;

Reply via email to