You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ha...@apache.org on 2021/09/19 15:06:13 UTC

[incubator-nuttx] branch master updated: fix: nxstyle: detect --, ->, ++ not next to operand

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

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


The following commit(s) were added to refs/heads/master by this push:
     new b42d2a7  fix: nxstyle: detect --,->,++ not next to operand
b42d2a7 is described below

commit b42d2a72847914f02a94da48dde99db2e22cff70
Author: liucheng5 <li...@xiaomi.com>
AuthorDate: Fri Sep 10 17:44:41 2021 +0800

    fix: nxstyle: detect --,->,++ not next to operand
    
    The nxstyle check tool can't recongnize the style error that --, -> or ++ is not next to the operand. For example, "idx ++;", "p ->member" or "(-- idx)", which is in incorrect style, is not recongnized. This patch add detection for these cases.
    
    Signed-off-by: liucheng5 <li...@xiaomi.com>
---
 tools/nxstyle.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 79 insertions(+), 2 deletions(-)

diff --git a/tools/nxstyle.c b/tools/nxstyle.c
index 978ab78..5f2afca 100644
--- a/tools/nxstyle.c
+++ b/tools/nxstyle.c
@@ -415,6 +415,59 @@ static void check_spaces_leftright(char *line, int lineno, int ndx1, int ndx2)
 }
 
 /********************************************************************************
+ * Name: check_nospaces_leftright
+ *
+ * Description:
+ *   Check if there are whitespaces on the left of right. If there is, report
+ *   an error.
+ *
+ ********************************************************************************/
+
+static void check_nospaces_leftright(char *line, int lineno, int ndx1, int ndx2)
+{
+  if (ndx1 > 0 && line[ndx1 - 1] == ' ')
+    {
+      ERROR("There should be no spaces before the operator/assignment",
+            lineno, ndx1);
+    }
+
+  if (line[ndx2 + 1] == ' ')
+    {
+      ERROR("There should be no spaces after the operator/assignment",
+            lineno, ndx2);
+    }
+}
+
+/********************************************************************************
+ * Name: check_operand_leftright
+ *
+ * Description:
+ *   Check if the operator is next to an operand. If not, report the error.
+ *
+ ********************************************************************************/
+
+static void check_operand_leftright(char *line, int lineno, int ndx1, int ndx2)
+{
+  /* The cases below includes("xx" represents the operator):
+   *   " xx " | " xx(end)" | " xx;" | " xx\n" | " xx)" | " xx]"  - (ndx1 > 0)
+   *   "(xx " | "(xx(end)" | "(xx;" | "(xx\n" | "(xx)" | "(xx]"  - (ndx1 > 0)
+   *   "[xx " | "[xx(end)" | "[xx;" | "[xx\n" | "[xx)" | "[xx]"  - (ndx1 > 0)
+   *   "xx "  | "xx(end)"  | "xx;"  | "xx\n"  | "xx)"  | "xx]"   - (ndx1 = 0)
+   * In these cases, the operators must be not next any operands, thus errors
+   * are reported.
+   */
+
+  if (ndx1 > 0 && (line[ndx1 - 1] == ' ' || line[ndx1 - 1] == '(' ||
+                   line[ndx1 - 1] == '[') &&
+                  (line[ndx2 + 1] == ' ' || line[ndx2 + 1] == '\0' ||
+                   line[ndx2 + 1] == ';' || line[ndx2 + 1] == '\n' ||
+                   line[ndx2 + 1] == ')' || line[ndx2 + 1] == ']'))
+    {
+      ERROR("Operator must be next to an operand", lineno, ndx2);
+    }
+}
+
+/********************************************************************************
  * Name: block_comment_width
  *
  * Description:
@@ -2226,10 +2279,27 @@ int main(int argc, char **argv, char **envp)
 
                 case '-':
 
-                  /* ->, -- */
+                  /* -> */
+
+                  if (line[n + 1] == '>')
+                    {
+                      /* -> must have no whitespaces on its left or right */
+
+                      check_nospaces_leftright(line, lineno, n, n + 1);
+                      n++;
+                    }
+
+                  /* -- */
 
-                  if (line[n + 1] == '>' || line[n + 1] == '-')
+                  else if (line[n + 1] == '-')
                     {
+                      /* "--" should be next to its operand. If there are
+                       * whitespaces or non-operand characters on both left
+                       * and right (e.g. "a -- ", “a[i --]”, "(-- i)"),
+                       * there's an error.
+                       */
+
+                      check_operand_leftright(line, lineno, n, n + 1);
                       n++;
                     }
 
@@ -2270,6 +2340,13 @@ int main(int argc, char **argv, char **envp)
 
                   if (line[n + 1] == '+')
                     {
+                      /* "++" should be next to its operand. If there are
+                       * whitespaces or non-operand characters on both left
+                       * and right (e.g. "a ++ ", “a[i ++]”, "(++ i)"),
+                       * there's an error.
+                       */
+
+                      check_operand_leftright(line, lineno, n, n + 1);
                       n++;
                     }