• Re: Compile time checking of standards conformance

    From Paavo Helde@21:1/5 to olcott on Thu May 23 09:23:42 2024
    XPost: comp.lang.c++

    On 23.05.2024 02:44, olcott wrote:
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to olcott on Thu May 23 16:43:27 2024
    XPost: comp.lang.c++

    On 23/05/2024 16:18, olcott wrote:
    On 5/23/2024 1:23 AM, Paavo Helde wrote:
    On 23.05.2024 02:44, olcott wrote:
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From James Kuyper@21:1/5 to Paavo Helde on Thu May 23 14:13:26 2024
    XPost: comp.lang.c++

    On 5/23/24 02:23, Paavo Helde wrote:
    ...
    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?

    ... 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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to olcott on Thu May 23 22:13:06 2024
    XPost: comp.lang.c++

    On 23/05/2024 17:14, olcott wrote:
    On 5/23/2024 9:43 AM, David Brown wrote:
    On 23/05/2024 16:18, olcott wrote:
    On 5/23/2024 1:23 AM, Paavo Helde wrote:
    On 23.05.2024 02:44, olcott wrote:
    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.


    /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.


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paavo Helde@21:1/5 to James Kuyper on Fri May 24 17:39:31 2024
    XPost: comp.lang.c++

    On 23.05.2024 21:13, James Kuyper wrote:
    On 5/23/24 02:23, Paavo Helde wrote:
    ...
    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.

    ... 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."


    It looks like they have clarified the rules in the last standard, my
    earlier draft (n4861) does not contain such verbiage.

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From James Kuyper@21:1/5 to Paavo Helde on Fri May 24 11:59:47 2024
    XPost: comp.lang.c++

    On 5/24/24 10:39, Paavo Helde wrote:
    On 23.05.2024 21:13, James Kuyper wrote:
    On 5/23/24 02:23, Paavo Helde wrote:
    ...
    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."


    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.

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)