NsiqCppStyle misidentifies the function name, in both declarations and definitions, when GCC's function attribute specifier follows the true function name. In the following code snippets:
The __attribute__ modifier in GCC is a way to specify special attributes of functions or variables. This feature allows developers to attach characteristics to function declarations to allow the compiler to perform more error checking or code optimization.
Here are a few examples of what you can do with __attribute__:
__attribute__((constructor)) and __attribute__((destructor)): These attributes, when used with a function, ensure that the function is called before main() when the program starts, or after main() when the program exits, respectively.
__attribute__((deprecated)): This attribute, when used with a function, produces a warning whenever the function is used elsewhere in the code, indicating that the function is deprecated.
__attribute__((packed)): This attribute, when used with a structure or union type definition, specifies that each member (after the first) is placed to minimize the memory required.
__attribute__((noreturn)): This attribute, when used with a function, tells the compiler that the function does not return. The compiler can then optimize the code with this assumption.
Remember that __attribute__ is specific to GCC and may not work with other compilers. If you're writing portable code, you should use it conditionally.
I have not investigated NsiqCppStyle's parser code, but I would wager that the function attribute specifier ("\_\_attribute\_\_((destructor))") looks too much like a function name and a parenthesized argument list. NsiqCppStyle might be taking the latest such match on the line as the function name.
The Problem
NsiqCppStyle misidentifies the function name, in both declarations and definitions, when GCC's function attribute specifier follows the true function name. In the following code snippets:
NsiqCppStyle incorrectly identifies the token "__attribute__" as the function name.
This is contrasted by NsiqCppStyle correctly identifying the function name when the GCC attribute specifier precedes the true function name:
Technical Background (source: GitHub Copilot)
The
__attribute__
modifier in GCC is a way to specify special attributes of functions or variables. This feature allows developers to attach characteristics to function declarations to allow the compiler to perform more error checking or code optimization.Here are a few examples of what you can do with
__attribute__
:__attribute__((constructor))
and__attribute__((destructor))
: These attributes, when used with a function, ensure that the function is called beforemain()
when the program starts, or aftermain()
when the program exits, respectively.__attribute__((deprecated))
: This attribute, when used with a function, produces a warning whenever the function is used elsewhere in the code, indicating that the function is deprecated.__attribute__((packed))
: This attribute, when used with a structure or union type definition, specifies that each member (after the first) is placed to minimize the memory required.__attribute__((noreturn))
: This attribute, when used with a function, tells the compiler that the function does not return. The compiler can then optimize the code with this assumption.Remember that
__attribute__
is specific to GCC and may not work with other compilers. If you're writing portable code, you should use it conditionally.trace-callback.py shows the error
I created a test file
~/junk/test.cpp
:CD-ing to the nsiqcppstyle root folder and running the following command:
/usr/bin/python3.11 trace-callbacks.py ~/junk/test.cpp > ~/junk/test.log
produced a full trace of all NsiqCppStyle callback function parameter lists (the complete output file is attached to this ticket). Here is the relevant output to illustrate this problem:
The true function name ("
finiUtracer_1
") is identified as a regular token:while the "
__attribute__
" value is identified as the FunctionName:Thoughts
I have not investigated NsiqCppStyle's parser code, but I would wager that the function attribute specifier ("
\_\_attribute\_\_((destructor))
") looks too much like a function name and a parenthesized argument list. NsiqCppStyle might be taking the latest such match on the line as the function name.