You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2021/03/04 20:18:47 UTC

[GitHub] [incubator-nuttx] gustavonihei opened a new pull request #2982: tools/nxstyle: Verify relative path in the file header

gustavonihei opened a new pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982


   ## Summary
   Add new verification to nxstyle tool based on the NuttX Coding Standard.
   From the [1.1 File Organization](https://cwiki.apache.org/confluence/display/NUTTX/Coding+Standard#fileorganization) section:
   - The relative path to the file from the top-level directory.
   
   ## Impact
   New verification rule for nxstyle, it will impact CI.
   
   ## Testing
   ```shell
   $ ./tools/checkpatch.sh -f sched/signal/sig_nanosleep.c
   sched/signal/sig_nanosleep.c:2:1: error: Relative file path does not match actual file
   ```
   ![image](https://user-images.githubusercontent.com/38959758/110025015-9bfc9280-7d0d-11eb-8b40-1cb7e2d8a566.png)
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 merged pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 merged pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982


   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588606939



##########
File path: tools/Makefile.host
##########
@@ -131,7 +131,8 @@ endif
 # nxstyle - Check a file for compliance to NuttX coding style
 
 nxstyle$(HOSTEXEEXT): nxstyle.c
-	$(Q) $(HOSTCC) $(HOSTCFLAGS) -o nxstyle$(HOSTEXEEXT) nxstyle.c
+	$(Q) $(HOSTCC) $(HOSTCFLAGS) -DTOPDIR=\"$(realpath $(TOPDIR))\" \

Review comment:
       Oops, I forgot about the apps folder. The implementation is trivial if we could assume the project structure will always be:
   - NuttX
     - nuttx
     - apps 




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588969407



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,57 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+              else
+                {
+                  bool file_header_mismatch = false;
+
+                  /* Checking whether g_file_name has the absolute path to
+                   * the file.
+                   * NOTE: Assuming that the project root folder name is
+                   *       "nuttx".
+                   */
+
+                  const char *root_substr = "/nuttx/";
+                  char *basedir = strstr(g_file_name, root_substr);

Review comment:
       Yes, you are right: my algo can't catch all error, but the frequent error.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588599440



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +837,51 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+#ifdef TOPDIR
+              else
+                {
+                  /* TOPDIR macro should have been defined via Makefile and
+                   * it is required for evaluating the relative file path in
+                   * the file header.
+                   * Otherwise, skip this verification.
+                   */
+
+                  /* Calculate the relative file path length minus the '\n'
+                   * character at the end of the string.
+                   */
+
+                  size_t filepath_len = strlen(&line[n + 2]) - 1;
+
+                  char *basedir = strstr(g_file_name, TOPDIR);
+                  if (basedir != NULL)
+                    {
+                      /* Add 1 to the offset for the slash character */
+
+                      size_t offset = strlen(TOPDIR) + 1;
+
+                      if (strncmp(&line[n + 2], basedir + offset,
+                                  filepath_len) != 0)

Review comment:
       You're right.
   But instead of modifying `line` I'll duplicate it to avoid messing with further verifications.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588244984



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,57 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+              else
+                {
+                  bool file_header_mismatch = false;
+
+                  /* Checking whether g_file_name has the absolute path to
+                   * the file.
+                   * NOTE: Assuming that the project root folder name is
+                   *       "nuttx".
+                   */
+
+                  const char *root_substr = "/nuttx/";
+                  char *basedir = strstr(g_file_name, root_substr);

Review comment:
       `strrstr` seems to be a C extension specific to Linux. It is not available in macOS.
   We could implement it ourselves in nxstyle, though.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588582473



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +837,51 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+#ifdef TOPDIR
+              else
+                {
+                  /* TOPDIR macro should have been defined via Makefile and
+                   * it is required for evaluating the relative file path in
+                   * the file header.
+                   * Otherwise, skip this verification.
+                   */
+
+                  /* Calculate the relative file path length minus the '\n'
+                   * character at the end of the string.
+                   */
+
+                  size_t filepath_len = strlen(&line[n + 2]) - 1;
+
+                  char *basedir = strstr(g_file_name, TOPDIR);
+                  if (basedir != NULL)
+                    {
+                      /* Add 1 to the offset for the slash character */
+
+                      size_t offset = strlen(TOPDIR) + 1;
+
+                      if (strncmp(&line[n + 2], basedir + offset,
+                                  filepath_len) != 0)

Review comment:
       can't catch the error like this:
   ```
   file name: ~/nuttx/sched/clock/Make.defs
   comment: sched/clock/Make.def
   ```
   It's better to remove '\n' from line and use strcmp instead




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588921091



##########
File path: tools/Makefile.host
##########
@@ -131,7 +131,8 @@ endif
 # nxstyle - Check a file for compliance to NuttX coding style
 
 nxstyle$(HOSTEXEEXT): nxstyle.c
-	$(Q) $(HOSTCC) $(HOSTCFLAGS) -o nxstyle$(HOSTEXEEXT) nxstyle.c
+	$(Q) $(HOSTCC) $(HOSTCFLAGS) -DTOPDIR=\"$(realpath $(TOPDIR))\" \

Review comment:
       Right now, to the best of my knowledge, `nxstyle` is only able to reliably report this kind of issue if it knows the root directory of each project.
   All of the other algorithms we've discussed so far either have blind spots (**false negatives**) or report false alarms (**false positives**).
   I understand that we should avoid accepting a solution that reports **false positives**, because it would mess with the CI reports and create some trouble to the developers.
   On the other hand, we could accept **false negatives** to a certain degree, because anything we end up accepting will be better than what we have today (no verification at all).
   We just need to agree on the best solution, or maybe someone prove that I am wrong and there is a better solution that is able to address every scenario with no false reports.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588506785



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,58 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+#ifdef TOPDIR
+              else
+                {
+                  /* TOPDIR macro should have been defined via Makefile and
+                   * it is required for evaluating the relative file path in
+                   * the file header.
+                   * Otherwise, skip this verification.
+                   */
+
+                  bool file_header_mismatch = false;
+                  char *basedir = strstr(g_file_name, TOPDIR);
+                  if (basedir != NULL)
+                    {
+                      /* g_file_name has the absolute path to the file. */
+
+                      /* Add 1 to the offset for the slash character */
+
+                      size_t offset = strlen(TOPDIR) + 1;
+
+                      if (strncmp(basedir + offset, &line[n + 2],
+                                  strlen(basedir + offset)) != 0)
+                        {
+                          file_header_mismatch = true;
+                        }
+                    }
+                  else if (strncmp(&line[n + 2], g_file_name,

Review comment:
       I am fine to fix the problem from script since I always use checkpatch.sh, but some poeple may invoke nxstyle directly. how about call realpath to convert the relative path to absolute path?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588630689



##########
File path: tools/Makefile.host
##########
@@ -131,7 +131,8 @@ endif
 # nxstyle - Check a file for compliance to NuttX coding style
 
 nxstyle$(HOSTEXEEXT): nxstyle.c
-	$(Q) $(HOSTCC) $(HOSTCFLAGS) -o nxstyle$(HOSTEXEEXT) nxstyle.c
+	$(Q) $(HOSTCC) $(HOSTCFLAGS) -DTOPDIR=\"$(realpath $(TOPDIR))\" \

Review comment:
       At least in the current state it won't report false positives.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588618424



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,57 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+              else
+                {
+                  bool file_header_mismatch = false;
+
+                  /* Checking whether g_file_name has the absolute path to
+                   * the file.
+                   * NOTE: Assuming that the project root folder name is
+                   *       "nuttx".
+                   */
+
+                  const char *root_substr = "/nuttx/";
+                  char *basedir = strstr(g_file_name, root_substr);

Review comment:
       Suppose the relative file path in the comment if `tools/nxstle.c`
   This condition will not be satisfied because `g_file_name[base - 1]` will point to the 't' character.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588592031



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,57 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+              else
+                {
+                  bool file_header_mismatch = false;
+
+                  /* Checking whether g_file_name has the absolute path to
+                   * the file.
+                   * NOTE: Assuming that the project root folder name is
+                   *       "nuttx".
+                   */
+
+                  const char *root_substr = "/nuttx/";
+                  char *basedir = strstr(g_file_name, root_substr);

Review comment:
       > 2. `base` seems to assume that the provided relative path in the header has at least has the same amount of characters. But this is usually not the case. If you remove a single char from the `tools/nxstyle.c` at the header, it will fail to identify the issue.
   
   Why not? "g_file_name[base - 1] == '/"' should catch it.
   




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588582823



##########
File path: tools/Makefile.host
##########
@@ -131,7 +131,8 @@ endif
 # nxstyle - Check a file for compliance to NuttX coding style
 
 nxstyle$(HOSTEXEEXT): nxstyle.c
-	$(Q) $(HOSTCC) $(HOSTCFLAGS) -o nxstyle$(HOSTEXEEXT) nxstyle.c
+	$(Q) $(HOSTCC) $(HOSTCFLAGS) -DTOPDIR=\"$(realpath $(TOPDIR))\" \

Review comment:
       can we handle the file under apps folder?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588856989



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,57 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+              else
+                {
+                  bool file_header_mismatch = false;
+
+                  /* Checking whether g_file_name has the absolute path to
+                   * the file.
+                   * NOTE: Assuming that the project root folder name is
+                   *       "nuttx".
+                   */
+
+                  const char *root_substr = "/nuttx/";
+                  char *basedir = strstr(g_file_name, root_substr);

Review comment:
       > Suppose the relative file path in the comment is `tools/nxstle.c`
   
   If user forget 'y' after 't', strcmp will report the mismatch.
   
   > This condition will not be satisfied because `g_file_name[base - 1]` will point to the 't' character.
   
   If user typo 'ools/nxstyle.c', g_file_name[base] equals 'o' and g_file_name[base - 1] equals 't'. Then "g_file_name[base - 1] == '/"' will catch it.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588290523



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,57 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+              else
+                {
+                  bool file_header_mismatch = false;
+
+                  /* Checking whether g_file_name has the absolute path to
+                   * the file.
+                   * NOTE: Assuming that the project root folder name is
+                   *       "nuttx".
+                   */
+
+                  const char *root_substr = "/nuttx/";
+                  char *basedir = strstr(g_file_name, root_substr);

Review comment:
       > If so we can change to:
   > 
   > ```
   > ssize_t base = strlen(g_file_name) - strlen(line + 2);
   > if (base >= 0 && (base == 0 || g_file_name[base - 1] == '/') &&
   >    strcmp(&g_file_name[base], line + 2) == 0
   > ```
   
   There are two issues with your suggested implementation:
   1) `line` contains the `\n` char at the end, this can be easily handled by calculating `base` with:
   `ssize_t base = strlen(g_file_name) - (strlen(&line[n + 2]) - 1);`
   2) `base` seems to assume that the provided relative path in the header has at least has the same amount of characters. But this is usually not the case. If you remove a single char from the `tools/nxstyle.c` at the header, it will fail to identify the issue.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588857156



##########
File path: tools/Makefile.host
##########
@@ -131,7 +131,8 @@ endif
 # nxstyle - Check a file for compliance to NuttX coding style
 
 nxstyle$(HOSTEXEEXT): nxstyle.c
-	$(Q) $(HOSTCC) $(HOSTCFLAGS) -o nxstyle$(HOSTEXEEXT) nxstyle.c
+	$(Q) $(HOSTCC) $(HOSTCFLAGS) -DTOPDIR=\"$(realpath $(TOPDIR))\" \

Review comment:
       But, can't catch error in apps folder too?:(




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588964427



##########
File path: tools/Makefile.host
##########
@@ -131,7 +131,8 @@ endif
 # nxstyle - Check a file for compliance to NuttX coding style
 
 nxstyle$(HOSTEXEEXT): nxstyle.c
-	$(Q) $(HOSTCC) $(HOSTCFLAGS) -o nxstyle$(HOSTEXEEXT) nxstyle.c
+	$(Q) $(HOSTCC) $(HOSTCFLAGS) -DTOPDIR=\"$(realpath $(TOPDIR))\" \

Review comment:
       So, as stupid as it may sound, but regarding files from the `apps` repository, I've just realized that the relative path in their headers all begin with `apps/`. This makes it easier for nxstyle to identify files from `apps` without knowing the project root folder.
   
   I've just updated the PR basically mixing both approaches:
   1) If TOPDIR had been defined via Makefile, it will use the most accurate algorithm for files from `nuttx` repository.
   2) Otherwise, it will use the algorithm suggested by @xiaoxiang781216 to identify issues with the relative path of files from `apps` repository (and also `nuttx` in case TOPDIR hadn't been defined).
   
   The only currently known limitation is that the approach **2** results in **false negatives** when one or more directories are omitted from the beginning of the relative path (.e.g.: "nxstyle.c" instead of "tools/nxstyle.c"). 




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588606939



##########
File path: tools/Makefile.host
##########
@@ -131,7 +131,8 @@ endif
 # nxstyle - Check a file for compliance to NuttX coding style
 
 nxstyle$(HOSTEXEEXT): nxstyle.c
-	$(Q) $(HOSTCC) $(HOSTCFLAGS) -o nxstyle$(HOSTEXEEXT) nxstyle.c
+	$(Q) $(HOSTCC) $(HOSTCFLAGS) -DTOPDIR=\"$(realpath $(TOPDIR))\" \

Review comment:
       Oops, I forgot about the apps folder. The implementation is trivial if we could assume the project structure will always be:
   - NuttX
     - nuttx
     - apps
   But I think that assumption is wrong...

##########
File path: tools/Makefile.host
##########
@@ -131,7 +131,8 @@ endif
 # nxstyle - Check a file for compliance to NuttX coding style
 
 nxstyle$(HOSTEXEEXT): nxstyle.c
-	$(Q) $(HOSTCC) $(HOSTCFLAGS) -o nxstyle$(HOSTEXEEXT) nxstyle.c
+	$(Q) $(HOSTCC) $(HOSTCFLAGS) -DTOPDIR=\"$(realpath $(TOPDIR))\" \

Review comment:
       Oops, I forgot about the apps folder. The implementation is trivial if we could assume the project structure will always be:
   - NuttX
     - nuttx
     - apps
   
   But I think that assumption is wrong...




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588575000



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,58 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+#ifdef TOPDIR
+              else
+                {
+                  /* TOPDIR macro should have been defined via Makefile and
+                   * it is required for evaluating the relative file path in
+                   * the file header.
+                   * Otherwise, skip this verification.
+                   */
+
+                  bool file_header_mismatch = false;
+                  char *basedir = strstr(g_file_name, TOPDIR);
+                  if (basedir != NULL)
+                    {
+                      /* g_file_name has the absolute path to the file. */
+
+                      /* Add 1 to the offset for the slash character */
+
+                      size_t offset = strlen(TOPDIR) + 1;
+
+                      if (strncmp(basedir + offset, &line[n + 2],
+                                  strlen(basedir + offset)) != 0)
+                        {
+                          file_header_mismatch = true;
+                        }
+                    }
+                  else if (strncmp(&line[n + 2], g_file_name,

Review comment:
       I was certain that `realpath` was not available on macOS, but that is only for the macOS shell 🤣 
   Nice! Now that nxstyle uses the absolute path, the solution becomes way simpler.
   Thanks, @xiaoxiang781216!




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r587956360



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,57 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+              else
+                {
+                  bool file_header_mismatch = false;
+
+                  /* Checking whether g_file_name has the absolute path to
+                   * the file.
+                   * NOTE: Assuming that the project root folder name is
+                   *       "nuttx".
+                   */
+
+                  const char *root_substr = "/nuttx/";
+                  char *basedir = strstr(g_file_name, root_substr);

Review comment:
       Yes, it is one of the limitations I listed in the description.
   I haven't found a nice way of making nxstyle handle both relative and absolute file paths, since the program has no knowledge about the environment.
   One way would be to provide the project root path via program arguments, but I think it is a bad solution.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588028615



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,57 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+              else
+                {
+                  bool file_header_mismatch = false;
+
+                  /* Checking whether g_file_name has the absolute path to
+                   * the file.
+                   * NOTE: Assuming that the project root folder name is
+                   *       "nuttx".
+                   */
+
+                  const char *root_substr = "/nuttx/";
+                  char *basedir = strstr(g_file_name, root_substr);

Review comment:
       how about this:
   ```
   const char *tmp = strrstr(g_file_name, line + 2);
   if (tmp && strlen(tmp) == strlen(line + 2) &&
       (tmp == g_file_name || *(tmp - 1) == '/'))
   ```
   Yes, it can't catch the well-designed test case, but should enough to catch the most typo error.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588265233



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,57 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+              else
+                {
+                  bool file_header_mismatch = false;
+
+                  /* Checking whether g_file_name has the absolute path to
+                   * the file.
+                   * NOTE: Assuming that the project root folder name is
+                   *       "nuttx".
+                   */
+
+                  const char *root_substr = "/nuttx/";
+                  char *basedir = strstr(g_file_name, root_substr);

Review comment:
       If so we can change to:
   ```
   ssize_t base = strlen(g_file_name) - strlen(line + 2);
   if (base >= 0 && (base == 0 || g_file_name[base - 1] == '/') &&
      strcmp(&g_file_name[base], line + 2) == 0
   ```




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588267507



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,57 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+              else
+                {
+                  bool file_header_mismatch = false;
+
+                  /* Checking whether g_file_name has the absolute path to
+                   * the file.
+                   * NOTE: Assuming that the project root folder name is
+                   *       "nuttx".
+                   */
+
+                  const char *root_substr = "/nuttx/";
+                  char *basedir = strstr(g_file_name, root_substr);

Review comment:
       I've just pushed a new proposal. Now `nxstyle` will only verify the relative file path if it knows the actually project root folder.
   The project root folder will be provided via Makefile, which is always the case when using via `checkpatch.sh`.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588405257



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,58 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+#ifdef TOPDIR
+              else
+                {
+                  /* TOPDIR macro should have been defined via Makefile and
+                   * it is required for evaluating the relative file path in
+                   * the file header.
+                   * Otherwise, skip this verification.
+                   */
+
+                  bool file_header_mismatch = false;
+                  char *basedir = strstr(g_file_name, TOPDIR);
+                  if (basedir != NULL)
+                    {
+                      /* g_file_name has the absolute path to the file. */
+
+                      /* Add 1 to the offset for the slash character */
+
+                      size_t offset = strlen(TOPDIR) + 1;
+
+                      if (strncmp(basedir + offset, &line[n + 2],
+                                  strlen(basedir + offset)) != 0)
+                        {
+                          file_header_mismatch = true;
+                        }
+                    }
+                  else if (strncmp(&line[n + 2], g_file_name,

Review comment:
       I think the new check will report the false alarm for the below command:
   ```
   cd nuttx/net
   ../tools/checkpatch.sh -f arp/arp.h
   ```
   I often use the similar command.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588918518



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,57 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+              else
+                {
+                  bool file_header_mismatch = false;
+
+                  /* Checking whether g_file_name has the absolute path to
+                   * the file.
+                   * NOTE: Assuming that the project root folder name is
+                   *       "nuttx".
+                   */
+
+                  const char *root_substr = "/nuttx/";
+                  char *basedir = strstr(g_file_name, root_substr);

Review comment:
       Ok, now I get. At first I thought your suggestion's objective was to identify the error, but now I understand that the condition you created will identify the correct file path. So the intended outcome is this:
   ```c
       if (base >= 0 && (base == 0 || g_file_name[base - 1] == '/') &&
           strcmp(&g_file_name[base], &line[n + 2]) == 0)
           {
               printf("No error\n");
           }
       else
           {
               printf("Error detected\n");
           }
   ```
   Yeah, this seems to work well. But without the knowledge of the project's root path, it will still fail to report an issue when one or more directories are missing in the beginning of the path:
   
   https://godbolt.org/z/6Ec7oq




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588028615



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,57 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+              else
+                {
+                  bool file_header_mismatch = false;
+
+                  /* Checking whether g_file_name has the absolute path to
+                   * the file.
+                   * NOTE: Assuming that the project root folder name is
+                   *       "nuttx".
+                   */
+
+                  const char *root_substr = "/nuttx/";
+                  char *basedir = strstr(g_file_name, root_substr);

Review comment:
       how about this:
   ```
   const char *tmp = strrstr(g_file_name, line + 2);
   if (tmp && strlen(tmp) == strlen(line + 2) &&
       (tmp == g_file_name || *(tmp - 1) == '/'))
   ```




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588921091



##########
File path: tools/Makefile.host
##########
@@ -131,7 +131,8 @@ endif
 # nxstyle - Check a file for compliance to NuttX coding style
 
 nxstyle$(HOSTEXEEXT): nxstyle.c
-	$(Q) $(HOSTCC) $(HOSTCFLAGS) -o nxstyle$(HOSTEXEEXT) nxstyle.c
+	$(Q) $(HOSTCC) $(HOSTCFLAGS) -DTOPDIR=\"$(realpath $(TOPDIR))\" \

Review comment:
       Right now, to the best of my knowledge, `nxstyle` is only able to reliably report this kind of issue if it knows what is the project root directory.
   All of the other algorithms we've discussed so far either have blind spots (**false negatives**) or report false alarms (**false positives**).
   I understand that we should avoid accepting a solution that reports **false positives**, because it would mess with the CI reports and create some trouble to the developers.
   On the other hand, we could accept **false negatives** to a certain degree, because anything we end up accepting will be better than what we have today (no verification at all).
   We just need to agree on the best solution, or maybe someone prove that I am wrong and there is a better solution that is able to address every scenario with no false reports.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
patacongo commented on pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#issuecomment-791069919


   I wonder if we do not need some way to suppress this check?  I often run nxstyle against files outside of the nuttx source tree especially when developing code or testing nxstyle.c.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588444037



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,58 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+#ifdef TOPDIR
+              else
+                {
+                  /* TOPDIR macro should have been defined via Makefile and
+                   * it is required for evaluating the relative file path in
+                   * the file header.
+                   * Otherwise, skip this verification.
+                   */
+
+                  bool file_header_mismatch = false;
+                  char *basedir = strstr(g_file_name, TOPDIR);
+                  if (basedir != NULL)
+                    {
+                      /* g_file_name has the absolute path to the file. */
+
+                      /* Add 1 to the offset for the slash character */
+
+                      size_t offset = strlen(TOPDIR) + 1;
+
+                      if (strncmp(basedir + offset, &line[n + 2],
+                                  strlen(basedir + offset)) != 0)
+                        {
+                          file_header_mismatch = true;
+                        }
+                    }
+                  else if (strncmp(&line[n + 2], g_file_name,

Review comment:
       I've tested this scenario with this patch and it handles correctly. Do you think it is acceptable?
   ```patch
   diff --git a/tools/checkpatch.sh b/tools/checkpatch.sh
   index 5ba15d817f..e1f392ec7b 100755
   --- a/tools/checkpatch.sh
   +++ b/tools/checkpatch.sh
   @@ -43,7 +43,7 @@ usage() {
    }
   
    check_file() {
   -  if ! $TOOLDIR/nxstyle $@ 2>&1; then
   +  if ! $TOOLDIR/nxstyle $(realpath $@) 2>&1; then
        fail=1
      fi
   ```




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r587954289



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,57 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+              else
+                {
+                  bool file_header_mismatch = false;
+
+                  /* Checking whether g_file_name has the absolute path to
+                   * the file.
+                   * NOTE: Assuming that the project root folder name is
+                   *       "nuttx".
+                   */
+
+                  const char *root_substr = "/nuttx/";
+                  char *basedir = strstr(g_file_name, root_substr);

Review comment:
       Doesn't this assume that the name of the root directory is nuttx/?  That is not always the case.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588618424



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,57 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+              else
+                {
+                  bool file_header_mismatch = false;
+
+                  /* Checking whether g_file_name has the absolute path to
+                   * the file.
+                   * NOTE: Assuming that the project root folder name is
+                   *       "nuttx".
+                   */
+
+                  const char *root_substr = "/nuttx/";
+                  char *basedir = strstr(g_file_name, root_substr);

Review comment:
       Suppose the relative file path in the comment is `tools/nxstle.c`
   This condition will not be satisfied because `g_file_name[base - 1]` will point to the 't' character.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588918518



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,57 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+              else
+                {
+                  bool file_header_mismatch = false;
+
+                  /* Checking whether g_file_name has the absolute path to
+                   * the file.
+                   * NOTE: Assuming that the project root folder name is
+                   *       "nuttx".
+                   */
+
+                  const char *root_substr = "/nuttx/";
+                  char *basedir = strstr(g_file_name, root_substr);

Review comment:
       Ok, now I get. At first I thought your suggestion's objective was to identify the error, but now I understand that the condition you created will identify the correct file path. So the intended outcome is this:
   ```c
       if (base >= 0 && (base == 0 || g_file_name[base - 1] == '/') &&
           strcmp(&g_file_name[base], &line[n + 2]) == 0)
           {
               printf("No error\n");
           }
       else
           {
               printf("Error detected\n");
           }
   ```
   Yeah, this seems to work well. But without the knowledge of the project's root path, it will still fail to report an issue when any directory is missing in the beginning of the path:
   
   https://godbolt.org/z/6Ec7oq




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588290523



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,57 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+              else
+                {
+                  bool file_header_mismatch = false;
+
+                  /* Checking whether g_file_name has the absolute path to
+                   * the file.
+                   * NOTE: Assuming that the project root folder name is
+                   *       "nuttx".
+                   */
+
+                  const char *root_substr = "/nuttx/";
+                  char *basedir = strstr(g_file_name, root_substr);

Review comment:
       > If so we can change to:
   > 
   > ```
   > ssize_t base = strlen(g_file_name) - strlen(line + 2);
   > if (base >= 0 && (base == 0 || g_file_name[base - 1] == '/') &&
   >    strcmp(&g_file_name[base], line + 2) == 0
   > ```
   
   There are two issues with your suggested implementation:
   1) `line` contains the `\n` char at the end, this can be easily handled by calculating `base` with:
   `ssize_t base = strlen(g_file_name) - (strlen(&line[n + 2]) - 1);`
   2) `base` seems to assume that the provided relative path in the header has at least has the same amount of characters. But this is usually not the case. If you remove a single char from the `tools/nxstyle.c` at the header, it will fail to recognize the issue.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588918518



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,57 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+              else
+                {
+                  bool file_header_mismatch = false;
+
+                  /* Checking whether g_file_name has the absolute path to
+                   * the file.
+                   * NOTE: Assuming that the project root folder name is
+                   *       "nuttx".
+                   */
+
+                  const char *root_substr = "/nuttx/";
+                  char *basedir = strstr(g_file_name, root_substr);

Review comment:
       Ok, now I get. At first I thought your suggestion's objective was to identify the error, but now I understand that the condition you created will identify the correct file path. So the intended outcome is this:
   ```c
       if (base >= 0 && (base == 0 || g_file_name[base - 1] == '/') &&
           strcmp(&g_file_name[base], &line[n + 2]) == 0)
           {
               printf("No error\n");
           }
       else
           {
               printf("Error detected\n");
           }
   ```
   Yeah, this seems to work well. But without the knowledge of the project's root path, it will fail to report an issue when the first directory is missing:
   
   https://godbolt.org/z/6Ec7oq




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#issuecomment-791148789


   We can provide an option to disable the check, so the developer can bypass the check case by case.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#issuecomment-791076780


   > I wonder if we do not need some way to suppress this check? I often run nxstyle against files outside of the nuttx source tree especially when developing code or testing nxstyle.c.
   
   A solution for this scenario would be to decouple this check from nxstyle, letting this responsibility to the CI. But the downside is that we would lose the local checking prior to opening the PR.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588444037



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,58 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+#ifdef TOPDIR
+              else
+                {
+                  /* TOPDIR macro should have been defined via Makefile and
+                   * it is required for evaluating the relative file path in
+                   * the file header.
+                   * Otherwise, skip this verification.
+                   */
+
+                  bool file_header_mismatch = false;
+                  char *basedir = strstr(g_file_name, TOPDIR);
+                  if (basedir != NULL)
+                    {
+                      /* g_file_name has the absolute path to the file. */
+
+                      /* Add 1 to the offset for the slash character */
+
+                      size_t offset = strlen(TOPDIR) + 1;
+
+                      if (strncmp(basedir + offset, &line[n + 2],
+                                  strlen(basedir + offset)) != 0)
+                        {
+                          file_header_mismatch = true;
+                        }
+                    }
+                  else if (strncmp(&line[n + 2], g_file_name,

Review comment:
       I've tested this scenario with this patch and it handles correctly. Do you think it is acceptable?
   It still does not eliminate the nxstyle limitation of not being able to handle paths that are not relative to the project root.
   ```patch
   diff --git a/tools/checkpatch.sh b/tools/checkpatch.sh
   index 5ba15d817f..e1f392ec7b 100755
   --- a/tools/checkpatch.sh
   +++ b/tools/checkpatch.sh
   @@ -43,7 +43,7 @@ usage() {
    }
   
    check_file() {
   -  if ! $TOOLDIR/nxstyle $@ 2>&1; then
   +  if ! $TOOLDIR/nxstyle $(realpath $@) 2>&1; then
        fail=1
      fi
   ```




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588964427



##########
File path: tools/Makefile.host
##########
@@ -131,7 +131,8 @@ endif
 # nxstyle - Check a file for compliance to NuttX coding style
 
 nxstyle$(HOSTEXEEXT): nxstyle.c
-	$(Q) $(HOSTCC) $(HOSTCFLAGS) -o nxstyle$(HOSTEXEEXT) nxstyle.c
+	$(Q) $(HOSTCC) $(HOSTCFLAGS) -DTOPDIR=\"$(realpath $(TOPDIR))\" \

Review comment:
       So, as stupid as it may sound, but regarding files from the `apps` repository, I've just realized that the relative path in their headers all begin with `apps/`. This makes it easier for nxstyle to identify files from `apps` without knowing the project root folder.
   
   I've just updated the PR basically mixing both approaches:
   1) If TOPDIR had been defined via Makefile, it will use the most accurate algorithm for files from `nuttx` repository.
   2) Otherwise, it will use the algorithm suggested by @xiaoxiang781216 to identify issues with the relative path of files from `apps` repository (and also `nuttx` in case TOPDIR hadn't been defined).
   
   The only currently known limitation is that the approach **2** results in **false negatives** when one or more directories are omitted from the beginning of the relative path (.e.g.: "nxstyle.c" instead of "tools/nxstyle.c"), but I believe that this is acceptable.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #2982: tools/nxstyle: Verify relative path in the file header

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #2982:
URL: https://github.com/apache/incubator-nuttx/pull/2982#discussion_r588444037



##########
File path: tools/nxstyle.c
##########
@@ -831,6 +831,58 @@ int main(int argc, char **argv, char **envp)
                ERROR("Missing file header comment block", lineno, 1);
             }
 
+          if (lineno == 2)
+            {
+              if (line[n] == '*' && line[n + 1] == '\n')
+                {
+                  ERROR("Missing relative file path in file header", lineno,
+                        n);
+                }
+              else if (isspace(line[n + 2]))
+                {
+                  ERROR("Too many whitespaces before relative file path",
+                        lineno, n);
+                }
+#ifdef TOPDIR
+              else
+                {
+                  /* TOPDIR macro should have been defined via Makefile and
+                   * it is required for evaluating the relative file path in
+                   * the file header.
+                   * Otherwise, skip this verification.
+                   */
+
+                  bool file_header_mismatch = false;
+                  char *basedir = strstr(g_file_name, TOPDIR);
+                  if (basedir != NULL)
+                    {
+                      /* g_file_name has the absolute path to the file. */
+
+                      /* Add 1 to the offset for the slash character */
+
+                      size_t offset = strlen(TOPDIR) + 1;
+
+                      if (strncmp(basedir + offset, &line[n + 2],
+                                  strlen(basedir + offset)) != 0)
+                        {
+                          file_header_mismatch = true;
+                        }
+                    }
+                  else if (strncmp(&line[n + 2], g_file_name,

Review comment:
       I've tested this scenario with this patch and it handles correctly. Do you think it is acceptable?
   It still does not eliminate the limitation that nxstyle is able to handle paths that are not relative to the project root.
   ```patch
   diff --git a/tools/checkpatch.sh b/tools/checkpatch.sh
   index 5ba15d817f..e1f392ec7b 100755
   --- a/tools/checkpatch.sh
   +++ b/tools/checkpatch.sh
   @@ -43,7 +43,7 @@ usage() {
    }
   
    check_file() {
   -  if ! $TOOLDIR/nxstyle $@ 2>&1; then
   +  if ! $TOOLDIR/nxstyle $(realpath $@) 2>&1; then
        fail=1
      fi
   ```




----------------------------------------------------------------
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:
users@infra.apache.org