From a8eb6044a9468a56cab63890820e17101ce8fd64 Mon Sep 17 00:00:00 2001 From: Neil Booth Date: Sun, 4 May 2003 20:03:55 +0000 Subject: [PATCH] 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 --- gcc/cppinit.c | 4 +++ gcc/cpplex.c | 35 ++++++++++++++++++++++++- gcc/doc/cpp.texi | 20 +++++++------- gcc/doc/cppopts.texi | 21 +++++++++------ gcc/testsuite/gcc.dg/cpp/Wtrigraphs-2.c | 20 ++++++++++++++ gcc/testsuite/gcc.dg/cpp/Wtrigraphs.c | 20 +++++++++++++- 6 files changed, 101 insertions(+), 19 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/cpp/Wtrigraphs-2.c diff --git a/gcc/cppinit.c b/gcc/cppinit.c index 74679c0111b..4066ee31dbd 100644 --- a/gcc/cppinit.c +++ b/gcc/cppinit.c @@ -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. */ diff --git a/gcc/cpplex.c b/gcc/cpplex.c index 4f0767e42c8..5f3e3f62fcb 100644 --- a/gcc/cpplex.c +++ b/gcc/cpplex.c @@ -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; } diff --git a/gcc/doc/cpp.texi b/gcc/doc/cpp.texi index cf962c4563a..173e341fb83 100644 --- a/gcc/doc/cpp.texi +++ b/gcc/doc/cpp.texi @@ -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 diff --git a/gcc/doc/cppopts.texi b/gcc/doc/cppopts.texi index 9212c80f755..490905a065c 100644 --- a/gcc/doc/cppopts.texi +++ b/gcc/doc/cppopts.texi @@ -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 diff --git a/gcc/testsuite/gcc.dg/cpp/Wtrigraphs-2.c b/gcc/testsuite/gcc.dg/cpp/Wtrigraphs-2.c new file mode 100644 index 00000000000..d1e822660bc --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/Wtrigraphs-2.c @@ -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 ??/\ + diff --git a/gcc/testsuite/gcc.dg/cpp/Wtrigraphs.c b/gcc/testsuite/gcc.dg/cpp/Wtrigraphs.c index 2dee1e53792..5ed6c98ad49 100644 --- a/gcc/testsuite/gcc.dg/cpp/Wtrigraphs.c +++ b/gcc/testsuite/gcc.dg/cpp/Wtrigraphs.c @@ -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 ??/\ +