Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does GCC emit a warning when using trigraphs, but not when using digraphs?

Code:

#include <stdio.h>

int main(void)
{
  ??< puts("Hello Folks!"); ??>
}

The above program, when compiled with GCC 4.8.1 with -Wall and -std=c11, gives the following warning:

source_file.c: In function ‘main’:
source_file.c:8:5: warning: trigraph ??< converted to { [-Wtrigraphs]
     ??< puts("Hello Folks!"); ??>
 ^
source_file.c:8:30: warning: trigraph ??> converted to } [-Wtrigraphs]

But when I change the body of main to:

<% puts("Hello Folks!"); %>

no warnings are thrown.

So, Why does the compiler warn me when using trigraphs, but not when using digraphs?

like image 751
Spikatrix Avatar asked May 11 '15 11:05

Spikatrix


People also ask

How does GCC treat warning errors?

The warning is emitted only with --coverage enabled. By default, this warning is enabled and is treated as an error. -Wno-coverage-invalid-line-number can be used to disable the warning or -Wno-error=coverage-invalid-line-number can be used to disable the error.

Which GCC flag is used to enable all compiler warnings?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.

Which flag would you pass to your C++ compiler so it warns you about implicit conversions?

By default, Fuchsia C/C++ code compiles with the flag -Wconversion , which prohibits implicit type conversions that may alter a value.

How do you use werror?

You can do this by specifying Werror=warning-name , which will cause that specific warning to generate an error. For example, a warning that I promote to an error is -Wreturn-type .


2 Answers

Because trigraphs have the undesirable effect of silently changing code. This means that the same source file is valid both with and without trigraph replacement, but leads to different code. This is especially problematic in string literals, like "<em>What??</em>".

Language design and language evolution should strive to avoid silent changes. Having the compiler warn about trigraphs is a good thing to have.

Contrast this with digraphs, which were new tokens that do not lead to silent changes.

like image 68
Jens Avatar answered Oct 14 '22 13:10

Jens


This gcc document on pre-processing gives a pretty good rationale for a warning (emphasis mine):

Trigraphs are not popular and many compilers implement them incorrectly. Portable code should not rely on trigraphs being either converted or ignored. With -Wtrigraphs GCC will warn you when a trigraph may change the meaning of your program if it were converted.

and in this gcc document on Tokenization explains digraphs unlike trigraphs do not potential negative side effects (emphasis mine):

There are also six digraphs, which the C++ standard calls alternative tokens, which are merely alternate ways to spell other punctuators. This is a second attempt to work around missing punctuation in obsolete systems. It has no negative side effects, unlike trigraphs,

like image 27
Shafik Yaghmour Avatar answered Oct 14 '22 13:10

Shafik Yaghmour