cgcccompiler-warningsdigraphstrigraphs

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?


Solution

  • 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,