cppinit.c (cpp_create_reader, [...]): Warn about trigraphs unless explicity set or -trigraphs.

* cppinit.c (cpp_create_reader, post_options): Warn about
	trigraphs unless explicity set or -trigraphs.
	* cpplex.c (warn_in_comment): New.
	(_cpp_process_line_notes): Better handling of -Wtrigraphs.
	(_cpp_skip_block_comment): Add call to _cpp_process_line_notes.
	* doc/cppopts.texi, doc/cpp.texi: Update.
testsuite:
	* gcc.dg/cpp/Wtrigraphs.c: Update.
	* gcc.dg/cpp/Wtrigraphs-2.c: New tests.

From-SVN: r66459
This commit is contained in:
Neil Booth 2003-05-04 20:03:55 +00:00
parent 09780dfb65
commit a8eb6044a9
6 changed files with 101 additions and 19 deletions

View File

@ -149,6 +149,7 @@ cpp_create_reader (lang, table)
CPP_OPTION (pfile, show_column) = 1;
CPP_OPTION (pfile, tabstop) = 8;
CPP_OPTION (pfile, operator_names) = 1;
CPP_OPTION (pfile, warn_trigraphs) = 2;
CPP_OPTION (pfile, warn_endif_labels) = 1;
CPP_OPTION (pfile, warn_deprecated) = 1;
CPP_OPTION (pfile, warn_long_long) = !CPP_OPTION (pfile, c99);
@ -554,6 +555,9 @@ post_options (pfile)
CPP_OPTION (pfile, traditional) = 0;
}
if (CPP_OPTION (pfile, warn_trigraphs) == 2)
CPP_OPTION (pfile, warn_trigraphs) = !CPP_OPTION (pfile, trigraphs);
if (CPP_OPTION (pfile, traditional))
{
/* Traditional CPP does not accurately track column information. */

View File

@ -63,6 +63,7 @@ static void save_comment PARAMS ((cpp_reader *, cpp_token *, const uchar *,
cppchar_t));
static void create_literal PARAMS ((cpp_reader *, cpp_token *, const uchar *,
unsigned int, enum cpp_ttype));
static bool warn_in_comment PARAMS ((cpp_reader *, _cpp_line_note *));
static int name_p PARAMS ((cpp_reader *, const cpp_string *));
static cppchar_t maybe_read_ucn PARAMS ((cpp_reader *, const uchar **));
static tokenrun *next_tokenrun PARAMS ((tokenrun *));
@ -180,6 +181,36 @@ _cpp_clean_line (pfile)
buffer->next_line = s + 1;
}
/* Return true if the trigraph indicated by NOTE should be warned
about in a comment. */
static bool
warn_in_comment (pfile, note)
cpp_reader *pfile;
_cpp_line_note *note;
{
const uchar *p;
/* Within comments we don't warn about trigraphs, unless the
trigraph forms an escaped newline, as that may change
behaviour. */
if (note->type != '/')
return false;
/* If -trigraphs, then this was an escaped newline iff the next note
is coincident. */
if (CPP_OPTION (pfile, trigraphs))
return note[1].pos == note->pos;
/* Otherwise, see if this forms an escaped newline. */
p = note->pos + 3;
while (is_nvspace (*p))
p++;
/* There might have been escaped newlines between the trigraph and the
newline we found. Hence the position test. */
return (*p == '\n' && p < note[1].pos);
}
/* Process the notes created by add_line_note as far as the current
location. */
void
@ -219,7 +250,8 @@ _cpp_process_line_notes (pfile, in_comment)
}
else if (_cpp_trigraph_map[note->type])
{
if (!in_comment && CPP_OPTION (pfile, warn_trigraphs))
if (CPP_OPTION (pfile, warn_trigraphs)
&& (!in_comment || warn_in_comment (pfile, note)))
{
if (CPP_OPTION (pfile, trigraphs))
cpp_error_with_line (pfile, DL_WARNING, pfile->line, col,
@ -284,6 +316,7 @@ _cpp_skip_block_comment (pfile)
}
}
_cpp_process_line_notes (pfile, true);
return false;
}

View File

@ -293,16 +293,18 @@ obsolete systems that lack some of C's punctuation to use C@. For
example, @samp{??/} stands for @samp{\}, so @t{'??/n'} is a character
constant for a newline.
Trigraphs are not popular and many compilers implement them incorrectly.
Portable code should not rely on trigraphs being either converted or
ignored. If you use the @option{-Wall} or @option{-Wtrigraphs} options,
GCC will warn you when a trigraph would change the meaning of your
program if it were converted.
Trigraphs are not popular and many compilers implement them
incorrectly. Portable code should not rely on trigraphs being either
converted or ignored. With @option{-Wtrigraphs} GCC will warn you
when a trigraph may change the meaning of your program if it were
converted. @xref{Wtrigraphs}.
In a string constant, you can prevent a sequence of question marks from
being confused with a trigraph by inserting a backslash between the
question marks. @t{"(??\?)"} is the string @samp{(???)}, not
@samp{(?]}. Traditional C compilers do not recognize this idiom.
In a string constant, you can prevent a sequence of question marks
from being confused with a trigraph by inserting a backslash between
the question marks, or by separating the string literal at the
trigraph and making use of string literal concatenation. @t{"(??\?)"}
is the string @samp{(???)}, not @samp{(?]}. Traditional C compilers
do not recognize these idioms.
The nine trigraphs and their replacements are

View File

@ -69,10 +69,12 @@ use @option{-o} to specify the output file.
@item -Wall
@opindex Wall
Turns on all optional warnings which are desirable for normal code. At
present this is @option{-Wcomment} and @option{-Wtrigraphs}. Note that
many of the preprocessor's warnings are on by default and have no
options to control them.
Turns on all optional warnings which are desirable for normal code.
At present this is @option{-Wcomment}, @option{-Wtrigraphs},
@option{-Wmultichar} and a warning about integer promotion causing a
change of sign in @code{#if} expressions. Note that many of the
preprocessor's warnings are on by default and have no options to
control them.
@item -Wcomment
@itemx -Wcomments
@ -84,10 +86,13 @@ comment, or whenever a backslash-newline appears in a @samp{//} comment.
@item -Wtrigraphs
@opindex Wtrigraphs
Warn if any trigraphs are encountered. This option used to take effect
only if @option{-trigraphs} was also specified, but now works
independently. Warnings are not given for trigraphs within comments, as
they do not affect the meaning of the program.
@anchor{Wtrigraphs}
Warn if any trigraphs that may change the meaning of a program are
encountered. This option is in effect unless trigraphs are turned on,
and is implied by @option{-Wall}. With the exception of a trigraph
that would form an escaped newline, warnings are not given for
trigraphs within comments as they do not affect the meaning of the
program.
@item -Wtraditional
@opindex Wtraditional

View File

@ -0,0 +1,20 @@
/* { dg-do preprocess } */
/* { dg-options "-std=c99 -Wtrigraphs -fno-show-column" } */
/* Test we don't double warn for trigraphs immediately after preceding
text. Source Neil Booth. 4 May 2003. */
/* { dg-bogus "ignored" } Test ??< ??= a few ??/ random things in
{ dg-warning "converted" } some ??/
{ dg-bogus "ignored" } ??< comments. */
// { dg-bogus "ignored" } More ??/ comment ??> tests.
// { dg-warning "converted" } Another ??/
Test
// { dg-warning "converted" } And another with space after ??/
the escape
// { dg-bogus "ignored" } A tricky one ??/\

View File

@ -1,5 +1,5 @@
/* { dg-do preprocess } */
/* { dg-options "-Wtrigraphs -fno-show-column" } */
/* { dg-options "-std=gnu99 -Wtrigraphs -fno-show-column" } */
/* Test we don't double warn for trigraphs immediately after preceding
text. Source Neil Booth. 22 Nov 2000. */
@ -7,3 +7,21 @@
abcdef??< /* { dg-warning "ignored" } */
123456??> /* { dg-warning "ignored" } */
+??= /* { dg-warning "ignored" } */
/* Test we warn of escaped newlines only in comments. Source Neil
Booth. 4 May 2003. */
/* { dg-bogus "ignored" } Test ??< ??= a few ??/ random things in
{ dg-warning "ignored" } some ??/
{ dg-bogus "ignored" } ??< comments. */
// { dg-bogus "ignored" } More ??/ comment ??> tests.
// { dg-warning "ignored" } Another ??/
Test
// { dg-warning "ignored" } And another with space after ??/
the escape
// { dg-bogus "ignored" } A tricky one ??/\