Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools/nxstyle: Verify relative path in the file header #2982

Conversation

gustavonihei
Copy link
Contributor

@gustavonihei gustavonihei commented Mar 4, 2021

Summary

Add new verification to nxstyle tool based on the NuttX Coding Standard.
From the 1.1 File Organization section:

  • The relative path to the file from the top-level directory.

Limitations:

  • For supporting input file provided with absolute path, nxstyle will search for the root folder assuming that it is named nuttx.
  • When executed directly (e.g. not via checkpatch.sh), nxstyle will report a false positive in the following scenarios:
    • Path to the input file starts with ./
    • Path to the input file is not relative to the project root directory
  • Currently it does not support the files from the apps/ folder (treated as false negatives).
  • Failure to identify (i.e: false negative report) the scenario when one or more directories are omitted from the beginning of the relative path (.e.g.: "nxstyle.c" instead of "tools/nxstyle.c")

Impact

New verification rule for nxstyle, it will impact CI.

Testing

$ ./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

@gustavonihei gustavonihei marked this pull request as draft March 4, 2021 22:01
@gustavonihei gustavonihei force-pushed the feature/nxstyle-relative-path-header branch from eaf6269 to 4f88072 Compare March 5, 2021 00:40
@gustavonihei gustavonihei marked this pull request as ready for review March 5, 2021 00:54
@patacongo
Copy link
Contributor

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.

tools/nxstyle.c Outdated
*/

const char *root_substr = "/nuttx/";
char *basedir = strstr(g_file_name, root_substr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

    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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@gustavonihei
Copy link
Contributor Author

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.

@xiaoxiang781216
Copy link
Contributor

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

@gustavonihei gustavonihei force-pushed the feature/nxstyle-relative-path-header branch 3 times, most recently from 2bdddac to 6857b81 Compare March 5, 2021 13:27
tools/nxstyle.c Outdated Show resolved Hide resolved
@gustavonihei gustavonihei force-pushed the feature/nxstyle-relative-path-header branch from 6857b81 to d2ffbee Compare March 5, 2021 18:38
tools/nxstyle.c Outdated Show resolved Hide resolved
@gustavonihei gustavonihei force-pushed the feature/nxstyle-relative-path-header branch from d2ffbee to cb15be0 Compare March 5, 2021 19:20
@gustavonihei gustavonihei force-pushed the feature/nxstyle-relative-path-header branch from cb15be0 to 8004219 Compare March 7, 2021 02:58
Copy link
Contributor

@xiaoxiang781216 xiaoxiang781216 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks @gustavonihei

@xiaoxiang781216 xiaoxiang781216 merged commit 95c8c99 into apache:master Mar 7, 2021
@gustavonihei gustavonihei deleted the feature/nxstyle-relative-path-header branch March 22, 2021 13:20
@btashton btashton added this to To-Add in Release Notes - 10.1.0 Apr 12, 2021
@jerpelea jerpelea moved this from To-Add to Minor in Release Notes - 10.1.0 Apr 12, 2021
@jerpelea jerpelea moved this from Minor to Build System in Release Notes - 10.1.0 Apr 14, 2021
@jerpelea jerpelea moved this from Build System to Added in Release Notes - 10.1.0 Apr 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants