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

[whitespace/parens] Spurious warning within comment #210

Open
alejandro-colomar opened this issue Sep 13, 2022 · 4 comments
Open

[whitespace/parens] Spurious warning within comment #210

alejandro-colomar opened this issue Sep 13, 2022 · 4 comments

Comments

@alejandro-colomar
Copy link

cpplint(1) is triggering a warning from something that resembles a function but within a comment. Comments can have anything in them, and doesn't necessarily represent a function (and in this case, obviously isn't).

alx@asus5775:~/src/linux/man-pages/man-pages$ cat tmp/src/man3/dlopen.3.d/dlopen.c

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>

#include <gnu/lib-names.h>  /* Defines LIBM_SO (which will be a
                               string such as "libm.so.6") */
int
main(void)
{
    void *handle;
    double (*cosine)(double);
    char *error;

    handle = dlopen(LIBM_SO, RTLD_LAZY);
    if (!handle) {
        fprintf(stderr, "%s\n", dlerror());
        exit(EXIT_FAILURE);
    }

    dlerror();    /* Clear any existing error */

    cosine = (double (*)(double)) dlsym(handle, "cos");

    /* According to the ISO C standard, casting between function
       pointers and 'void *', as done above, produces undefined results.
       POSIX.1-2001 and POSIX.1-2008 accepted this state of affairs and
       proposed the following workaround:

           *(void **) (&cosine) = dlsym(handle, "cos");

       This (clumsy) cast conforms with the ISO C standard and will
       avoid any compiler warnings.

       The 2013 Technical Corrigendum 1 to POSIX.1-2008 improved matters
       by requiring that conforming implementations support casting
       'void *' to a function pointer.  Nevertheless, some compilers
       (e.g., gcc with the '-pedantic' option) may complain about the
       cast used in this program. */

    error = dlerror();
    if (error != NULL) {
        fprintf(stderr, "%s\n", error);
        exit(EXIT_FAILURE);
    }

    printf("%f\n", (*cosine)(2.0));
    dlclose(handle);
    exit(EXIT_SUCCESS);
}
alx@asus5775:~/src/linux/man-pages/man-pages$ make tmp/src/man3/dlopen.3.d/dlopen.lint-c.cpplint.touch V=1
LINT (cpplint)	tmp/src/man3/dlopen.3.d/dlopen.lint-c.cpplint.touch
cpplint   tmp/src/man3/dlopen.3.d/dlopen.c >/dev/null
tmp/src/man3/dlopen.3.d/dlopen.c:6:  Extra space before ( in function call  [whitespace/parens] [4]
make: *** [lib/lint-c.mk:64: tmp/src/man3/dlopen.3.d/dlopen.lint-c.cpplint.touch] Error 1
@tkruse
Copy link

tkruse commented Jan 29, 2023

Thanks for reporting.
cpplint is not great at dealing with multiline /* ... */ comments. cpplint is not based on a parser, but on rules checking line by line. As such, handling complex multiline comments can cause it to report false positives. I don't think this will be fixed in the future.

@alejandro-colomar
Copy link
Author

alejandro-colomar commented Jan 29, 2023

I modified the above program to actually use the line that was in the comment, to see what would happen:

$ cat tmp/src/man3/dlopen.3.d/dlopen.c

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>

#include <gnu/lib-names.h>  /* Defines LIBM_SO (which will be a
                               string such as "libm.so.6") */
int
main(void)
{
    void *handle;
    double (*cosine)(double);
    char *error;

    handle = dlopen(LIBM_SO, RTLD_LAZY);
    if (!handle) {
        fprintf(stderr, "%s\n", dlerror());
        exit(EXIT_FAILURE);
    }

    dlerror();    /* Clear any existing error */

//    cosine = (double (*)(double)) dlsym(handle, "cos");

    *(void **) (&cosine) = dlsym(handle, "cos");

    error = dlerror();
    if (error != NULL) {
        fprintf(stderr, "%s\n", error);
        exit(EXIT_FAILURE);
    }

    printf("%f\n", (*cosine)(2.0));
    dlclose(handle);
    exit(EXIT_SUCCESS);
}

That line of code is fine, according to the standards. But cpplint(1) still warns; see below.

$ make V=1 lint-c-cpplint
LINT (cpplint)	tmp/src/man3/dlopen.3.d/dlopen.lint-c.cpplint.touch
cpplint   tmp/src/man3/dlopen.3.d/dlopen.c >/dev/null
tmp/src/man3/dlopen.3.d/dlopen.c:6:  Extra space before ( in function call  [whitespace/parens] [4]
tmp/src/man3/dlopen.3.d/dlopen.c:25:  Weird number of spaces at line-start.  Are you using a 2-space indent?  [whitespace/indent] [3]
make: *** [lib/lint-c.mk:64: tmp/src/man3/dlopen.3.d/dlopen.lint-c.cpplint.touch] Error 1

However, it makes sense if cpplint(1) is not powerful enough to parse such complex lines of code, and would understand if it can't be fixed without important changes to the program.

@tkruse
Copy link

tkruse commented Jan 29, 2023

I believe the warning is about this comment:

#include <gnu/lib-names.h>  /* Defines LIBM_SO (which will be a
                               string such as "libm.so.6") */

And I think cpplint has trouble identiifying /* when it's not at the start of a line, because it could be something like ("bla /*...").

@alejandro-colomar
Copy link
Author

Ahh, yes, sorry, I forgot about it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants