Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable compiler warning when using double underscore

Tags:

c

gcc

clang

Let's assume the following code

int __foo(void) {
    return 0;
}

int _BAR(void) {
    return 3;
}

int main(void) {
    return __foo() & _BAR();
}

Double underscore and a single underscore followed by an uppercase letter symbols are reserved and therefore not allowed (This is a C++ question, but it also mentions the C rules).

I tried -Wall -Wextra -pedantic options on gcc and -Weverything option on clang, both do not warn about this.

Is there any way to enable a compiler warning for this?

like image 741
hellow Avatar asked Nov 05 '18 12:11

hellow


2 Answers

GCC and Clang appear not to offer such a feature.

The documentation for GCC warning messages is here (for version 8.2; to seek documentation for other versions, start here). None of them mention checking for reserved identifiers or identifiers that begin with an underscore followed by an underscore or capital letter, except certain special cases (such as the built-in __FILE__) that are not useful for this question.

Clang’s documentation is here (that appears to be a link for the current version, so expected it to be updated in the future). It similarly has no mention of checking for reserved identifiers.

In Clang, -Weverything enables all diagnostics, so, if no diagnostic appears when compiling sample code with -Weverything, the desired diagnostic is not implemented in Clang.

There does not appear to be any reason a compiler cannot do this. Clang does track where source text originates. For example, if macro expansions result in a syntax error, Clang prints multiple diagnostic lines showing the names, line numbers, and file names of the macros involved. Furthermore, Clang suppresses warnings in system headers and can be told to treat additional files (such as headers for libraries) similarly with #pragma clang system_header. So it seems feasible for Clang to produce a warning for any reserved identifier that does not originate in a system header. The lack of such a feature may be due to lack of demand.

like image 112
Eric Postpischil Avatar answered Oct 31 '22 14:10

Eric Postpischil


A compiler can't practically warn you of this. Once the preprocessor has included any standard library files (which can of course contain double underscores), the compiler doesn't really know the origins of such code.

A good IDE or static analyser can warn you however.

like image 26
Bathsheba Avatar answered Oct 31 '22 13:10

Bathsheba