'What are the examples of non-ISO practices, which are not found by -pedantic?
https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html :
Some users try to use
-Wpedantic
to check programs for strict ISO C conformance. They soon find that it does not do quite what they want: it finds some non-ISO practices, but not all—only those for which ISO C requires a diagnostic, and some others for which diagnostics have been added.
What are the examples of non-ISO practices, which are not found by -pedantic
?
Solution 1:[1]
The problem here is fundamentally that it's possible to write C programs containing errors (not just failures to achieve strict conformance) that it is not reasonable to demand a C compiler detect. At the time the standard was originally written (1989), whole-program analysis was out of the question, and even now, it's expensive. And one will always be able to gin up constructs like
extern _Bool collatz_sequence_does_not_terminate(int);
int foo(int n) {
int rv;
if (collatz_sequence_does_not_terminate(n)) {
return rv;
}
return 23;
}
where the answer to "does this program have undefined behavior" depends on a mathematical problem whose solution is unknown.
So the manual is trying to warn you, not just that GCC can't detect all possible violations of ISO C conformance, but that no compiler can. It's worded a little too preciously, if I still worked on GCC I might revise it to be clearer.
If you want a concrete example of something that makes the program nonconformant, isn't diagnosed at all, and is relatively likely to come up in real life, try this on for size:
/* a.c */
#include <stdint.h>
uint64_t val = 0x0123456789abcdef;
/* b.c */
#include <stdio.h>
extern double val;
int main(void) {
printf("%g\n", val);
return 0;
}
Whole-program analysis is required to detect the type mismatch between the two files. It's not a difficult case; the compiler could annotate each global symbol with its type and the linker could check all uses of each symbol for consistency with the definition. But I'm not aware of any toolchain, nor any static analysis product, that will detect this error for variables.
Solution 2:[2]
GCC support other forms of constant expressions that are not required by ISO C. For example:
int main() {
const int m = 5;
static int n = m;
return n;
}
This code compiles fine in "pedantic" mode even though static
variable is initialized with something that is not a constant expressions conforming to ISO C.
Solution 3:[3]
- The C Preprocessor, 11.1 Implementation-defined behavior:
GCC allows the ‘$’ character in identifiers as an extension for most targets. This is true regardless of the std= switch, since this extension cannot conflict with standards-conforming programs. When preprocessing assembler, however, dollars are not identifier characters by default.
UPD. 2. Allowing "plain complex
":
#include <complex.h>
complex x;
$ gcc t0.c -std=c11 -pedantic -Wall -Wextra
<nothing>
C11, 6.2.5 Types, 11:
There are three complex types, designated as
float _Complex
,double _Complex
, andlong double _Complex
.
and 7.3.1 Introduction, 4:
The macro
complex
expands to_Complex
;
UPD. 3. __has_attribute
:
#if __has_attribute(xxx)
#endif
int x;
$ gcc t0.c -std=c11 -pedantic -Wall -Wextra
<nothing>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 |