Discussion:
Compile time checking of standards conformance
(too old to reply)
olcott
2024-05-22 23:44:48 UTC
Permalink
Does enables conformance mean: Flags non conformance?

/std:c11
The /std:c11 option enables ISO C11 conformance. It's available starting
in Visual Studio 2019 version 16.8.

/std:c17
The /std:c17 option enables ISO C17 conformance. It's available starting
in Visual Studio 2019 version 16.8.

https://learn.microsoft.com/en-us/cpp/build/reference/std-specify-language-standard-version?view=msvc-170
--
Copyright 2024 Olcott "Talent hits a target no one else can hit; Genius
hits a target no one else can see." Arthur Schopenhauer
Paavo Helde
2024-05-23 06:23:42 UTC
Permalink
Post by olcott
Does enables conformance mean: Flags non conformance?
No. Enabling conformance means enabling support for the conforming features.

Flagging non-conformance is not possible in general as there are more
creative fools out there than there are compiler writers.

There are zillions of ways to write non-conforming code, for example an
endless loop is not conforming in C++. The compiler is not obliged to
diagnose it, but it is allowed for the compiler to silently optimize the
non-conforming code away, assuming it is never called. At least that's
what g++ folks think.
olcott
2024-05-23 13:05:42 UTC
Permalink
Post by Paavo Helde
Post by olcott
Does enables conformance mean: Flags non conformance?
No. Enabling conformance means enabling support for the conforming features.
Flagging non-conformance is not possible in general as there are more
creative fools out there than there are compiler writers.
There are zillions of ways to write non-conforming code, for example an
endless loop is not conforming in C++. The compiler is not obliged to
diagnose it, but it is allowed for the compiler to silently optimize the
non-conforming code away, assuming it is never called. At least that's
what g++ folks think.
Thanks.
--
Copyright 2024 Olcott "Talent hits a target no one else can hit; Genius
hits a target no one else can see." Arthur Schopenhauer
olcott
2024-05-23 14:18:25 UTC
Permalink
Post by Paavo Helde
Post by olcott
Does enables conformance mean: Flags non conformance?
No. Enabling conformance means enabling support for the conforming features.
Flagging non-conformance is not possible in general as there are more
creative fools out there than there are compiler writers.
There are zillions of ways to write non-conforming code, for example an
endless loop is not conforming in C++. The compiler is not obliged to
diagnose it, but it is allowed for the compiler to silently optimize the
non-conforming code away, assuming it is never called. At least that's
what g++ folks think.
I would think that syntactically non-conforming code could be
flagged by the parser.
--
Copyright 2024 Olcott "Talent hits a target no one else can hit; Genius
hits a target no one else can see." Arthur Schopenhauer
David Brown
2024-05-23 14:43:27 UTC
Permalink
Post by olcott
Post by Paavo Helde
Post by olcott
Does enables conformance mean: Flags non conformance?
No. Enabling conformance means enabling support for the conforming features.
Flagging non-conformance is not possible in general as there are more
creative fools out there than there are compiler writers.
There are zillions of ways to write non-conforming code, for example
an endless loop is not conforming in C++. The compiler is not obliged
to diagnose it, but it is allowed for the compiler to silently
optimize the non-conforming code away, assuming it is never called. At
least that's what g++ folks think.
I would think that syntactically non-conforming code could be
flagged by the parser.
Syntax errors and many types of constraint violations will be flagged by
the parser. But there are endless possibilities of non-conforming
behaviour that cannot be seen at compile time.
olcott
2024-05-23 15:14:42 UTC
Permalink
Post by David Brown
Post by olcott
Post by Paavo Helde
Post by olcott
Does enables conformance mean: Flags non conformance?
No. Enabling conformance means enabling support for the conforming features.
Flagging non-conformance is not possible in general as there are more
creative fools out there than there are compiler writers.
There are zillions of ways to write non-conforming code, for example
an endless loop is not conforming in C++. The compiler is not obliged
to diagnose it, but it is allowed for the compiler to silently
optimize the non-conforming code away, assuming it is never called.
At least that's what g++ folks think.
I would think that syntactically non-conforming code could be
flagged by the parser.
Syntax errors and many types of constraint violations will be flagged by
the parser.  But there are endless possibilities of non-conforming
behaviour that cannot be seen at compile time.
Does enables conformance mean: Flags syntactic non conformance?

/std:c11
The /std:c11 option enables ISO C11 conformance. It's available starting
in Visual Studio 2019 version 16.8.

/std:c17
The /std:c17 option enables ISO C17 conformance. It's available starting
in Visual Studio 2019 version 16.8.
--
Copyright 2024 Olcott "Talent hits a target no one else can hit; Genius
hits a target no one else can see." Arthur Schopenhauer
David Brown
2024-05-23 20:13:06 UTC
Permalink
Post by olcott
Post by David Brown
Post by olcott
Post by Paavo Helde
Post by olcott
Does enables conformance mean: Flags non conformance?
No. Enabling conformance means enabling support for the conforming features.
Flagging non-conformance is not possible in general as there are
more creative fools out there than there are compiler writers.
There are zillions of ways to write non-conforming code, for example
an endless loop is not conforming in C++. The compiler is not
obliged to diagnose it, but it is allowed for the compiler to
silently optimize the non-conforming code away, assuming it is never
called. At least that's what g++ folks think.
I would think that syntactically non-conforming code could be
flagged by the parser.
Syntax errors and many types of constraint violations will be flagged
by the parser.  But there are endless possibilities of non-conforming
behaviour that cannot be seen at compile time.
Does enables conformance mean: Flags syntactic non conformance?
Not specifically, no. But enabling pedantic mode in gcc should give
messages for all syntax errors and constraint violations for which
diagnostics are required. I have no idea about MSVC and what it does
there, but gcc considers any cases of missing required diagnostics (when
in pedantic mode) to be a bug in the compiler.
Post by olcott
/std:c11
The /std:c11 option enables ISO C11 conformance. It's available starting
in Visual Studio 2019 version 16.8.
/std:c17
The /std:c17 option enables ISO C17 conformance. It's available starting
in Visual Studio 2019 version 16.8.
James Kuyper
2024-05-23 18:13:26 UTC
Permalink
On 5/23/24 02:23, Paavo Helde wrote:
...
Post by Paavo Helde
There are zillions of ways to write non-conforming code, for example an
endless loop is not conforming in C++.
Citation, please? How does it fail to conform?
Post by Paavo Helde
... The compiler is not obliged to
diagnose it, but it is allowed for the compiler to silently optimize the
non-conforming code away, assuming it is never called. At least that's
what g++ folks think.
The C standard contains the following wording:

"An iteration statement may be assumed by the implementation to
terminate if its controlling expression is not a constant expression200)
, and none of the following operations are performed in its body,
controlling expression or (in the case of a for statement)
itsfexpression-3201) :
— input/output operations
— accessing a volatile object
— synchronization or atomic operations."

In essence, those restrictions mean that the body of the loop does
nothing that need take up any time, so it can be optimized to a nop.
Infinitely many repetitions times 0 execution time has a product that is
mathematically meaningless, but the C standard permits optimizing to
terminate after a finite amount of time.

Note that this does not apply to endless loops occurring by other means,
such as goto.

The optimization you describe would therefore be permitted for C if
those conditions are met - but I can find no corresponding language in
the latest draft I have of the C++ standard (n4928.pdf, dated
2022-12-18). It's a big standard - I might have missed something.
Paavo Helde
2024-05-24 14:39:31 UTC
Permalink
Post by James Kuyper
...
Post by Paavo Helde
There are zillions of ways to write non-conforming code, for example an
endless loop is not conforming in C++.
Citation, please? How does it fail to conform?
It may well be I messed up something. I had a vague memory that g++
could legally optimize away the code only because it contained UB, but
now I'm not so sure any more.
Post by James Kuyper
Post by Paavo Helde
... The compiler is not obliged to
diagnose it, but it is allowed for the compiler to silently optimize the
non-conforming code away, assuming it is never called. At least that's
what g++ folks think.
"An iteration statement may be assumed by the implementation to
terminate if its controlling expression is not a constant expression200)
, and none of the following operations are performed in its body,
controlling expression or (in the case of a for statement)
— input/output operations
— accessing a volatile object
— synchronization or atomic operations."
It looks like they have clarified the rules in the last standard, my
earlier draft (n4861) does not contain such verbiage.
Post by James Kuyper
In essence, those restrictions mean that the body of the loop does
nothing that need take up any time, so it can be optimized to a nop.
Infinitely many repetitions times 0 execution time has a product that is
mathematically meaningless, but the C standard permits optimizing to
terminate after a finite amount of time.
Note that this does not apply to endless loops occurring by other means,
such as goto.
The 4861 draft gives an implementation of a while loop in terms of GOTO
and says these are equivalent.
James Kuyper
2024-05-24 15:59:47 UTC
Permalink
...
Post by Paavo Helde
Post by James Kuyper
"An iteration statement may be assumed by the implementation to
terminate if its controlling expression is not a constant expression200)
, and none of the following operations are performed in its body,
controlling expression or (in the case of a for statement)
— input/output operations
— accessing a volatile object
— synchronization or atomic operations."
It looks like they have clarified the rules in the last standard, my
earlier draft (n4861) does not contain such verbiage.
I did a context change up above; you might have missed it. I'm talking
about the C standard, because it gives this permission, before pointing
out that the C++ standard does not. According to
<https://www.open-std.org/jtc1/sc22/wg14/www/wg14_document_log.htm>, the
highest C document number is currently n3265.

The latest public draft of the C++ standard is
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/n4950.pdf>

wg21 does not appear to have a document_log.htm like wg14, so I have not
been able to find n4861.pdf. I have a copy of n4860.pdf, which is a
draft of the C++ standard dated 2020-03-31.
Post by Paavo Helde
Post by James Kuyper
In essence, those restrictions mean that the body of the loop does
nothing that need take up any time, so it can be optimized to a nop.
Infinitely many repetitions times 0 execution time has a product that is
mathematically meaningless, but the C standard permits optimizing to
terminate after a finite amount of time.
Note that this does not apply to endless loops occurring by other means,
such as goto.
The 4861 draft gives an implementation of a while loop in terms of GOTO
and says these are equivalent.
Note that the equivalence of those two constructs is not exact - for
instance, a continue statement is allowed only inside and actual
iteration statement, and not inside code that is otherwise equivalent,
but using goto.
Permission is given to conclude that iteration statements terminate. I
don't think the permission extends to equivalent constructs created
using goto.
Note that C is case sensitive language, so it's best not to capitalize
lower-case C keywords.
Tim Rentsch
2024-05-25 07:47:53 UTC
Permalink
Post by James Kuyper
Permission is given to conclude that iteration statements
terminate. I don't think the permission extends to equivalent
constructs created using goto.
C++ differs from C in this regard.

Tim Rentsch
2024-05-25 07:41:09 UTC
Permalink
...
Post by Paavo Helde
There are zillions of ways to write non-conforming code, for
example an endless loop is not conforming in C++.
Citation, please? How does it fail to conform?
It may well be I messed up something. I had a vague memory that g++
could legally optimize away the code only because it contained UB,
but now I'm not so sure any more.
Just briefly:

The rules are different in C and C++.

Endless loops are allowed in both languages.

I won't say any more about C except to emphasize that it has
different rules (than C++ does) with respect to endless loops.

It should go without saying that any program that crosses over
into undefined behavior might do anything at all. However I'm
pretty sure that there was no undefined behavior in the programs
being considered here.

In C++, endless loops that satisfy certain properties may be
assumed by the compiler to terminate, regardless of whether they
do or not. That statement applies whether or not there are any
iteration statements involved.

In the particular case of a statement such as

HERE: goto HERE;

a C++ compiler is within its rights to treat this statement as
though it were

HERE: ;

to the best of my understanding of the C++ standard.

(Since the thread has lost the code example being discusses I
have nothing more to say about that code.)

I hope these comments help clarify the matter.
Keith Thompson
2024-05-23 22:32:06 UTC
Permalink
Post by Paavo Helde
Post by olcott
Does enables conformance mean: Flags non conformance?
No. Enabling conformance means enabling support for the conforming features.
Flagging non-conformance is not possible in general as there are more
creative fools out there than there are compiler writers.
There are zillions of ways to write non-conforming code, for example
an endless loop is not conforming in C++. The compiler is not obliged
to diagnose it, but it is allowed for the compiler to silently
optimize the non-conforming code away, assuming it is never called. At
least that's what g++ folks think.
A conforming C compiler must produce a diagnostic for any violation of
any syntax rule or constraint. Many C constructs (for example signed
integer overflow) have *undefined behavior*, which need not be
diagnosed.

C++ has similar rules.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+***@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Loading...