1

Follow-up question for: clang: <string literal> + <expression returning int> leads to confusing warning: adding 'int' to a string does not append to the string.

Does "strictly conforming program" + no extensions mean "no diagnostics emitted"?

Reason: better understanding of the term "strictly conforming program".

pmor
  • 5,392
  • 4
  • 17
  • 36
  • That exact question is proving that it is not. The program is strictly conforming, yet the diagnostic is emitted. The compiler might chose to emit diagnostics about whatever it wills. – Eugene Sh. Aug 09 '21 at 17:38
  • @EugeneSh. The compiler behavior can't count as a proof. It could very well be non-conforming. – HolyBlackCat Aug 09 '21 at 17:40
  • 1
    @HolyBlackCat The standard only mandates when a diagnostic *should* be emitted, not when it should not. – Eugene Sh. Aug 09 '21 at 17:40
  • very nice warning. A lots of people try to concatenate the strings using `+` – 0___________ Aug 09 '21 at 18:00
  • 2
    This is one of the most useful reasons to have diagnostics: when the program is strictly conforming but the compiler guesses that its defined behavior is not what you intended. Like the classic example `if (x = 7) printf("x is seven");`. – Nate Eldredge Aug 09 '21 at 18:31
  • @NateEldredge Would the project "C compiler, which reports any failure to conform to ISO C" be useful? – pmor Aug 09 '21 at 18:41

2 Answers2

5

An implementation may generate diagnostics even if a program is conforming.

Section 5.1.1.3p1 of the C standard regarding diagnostics states:

A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined. Diagnostic messages need not be produced in other circumstances.9)


  1. The intent is that an implementation should identify the nature of, and where possible localize, each violation. Of course, an implementation is free to produce any number of diagnostics as long as a valid program is still correctly translated. It may also successfully translate an invalid program

The portion in bold in footnote 9 states that additional diagnostics may be produced.

dbush
  • 205,898
  • 23
  • 218
  • 273
2

Does "strictly conforming program" + no extensions == no diagnostics emitted?

No.

The only things for which the language specification requires diagnostics to be emitted are invalid syntax and constraint violations:

A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined. Diagnostic messages need not be produced in other circumstances.

(C2017, 5.1.1.3/1; emphasis added)

By definition, a strictly conforming program exhibits only valid syntax and does not contain any constraint violations, therefore the specification does not require a conforming implementation to emit any diagnostics when presented with such a program.

HOWEVER, the specification does not forbid implementations to emit diagnostics other than those that are required, and most implementations do, under some circumstances, emit diagnostics that are not required. The specification allows this, as clarified by footnote 9, which says, in part:

Of course, an implementation is free to produce any number of diagnostics as long as a valid program is still correctly translated.

Note also that "'strictly conforming program' + no extensions" is redundant. A program that makes use of any language extensions may conform, but it does not strictly conform:

A strictly conforming program shall use only those features of the language and library specified in this International Standard. It shall not produce output dependent on any unspecified, undefined,or implementation-defined behavior, and shall not exceed any minimum implementation limit.

(C2017 4/5; emphasis added)

John Bollinger
  • 160,171
  • 8
  • 81
  • 157