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);
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. */
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 *));
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
}
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,
}
}
+ _cpp_process_line_notes (pfile, true);
return false;
}
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.
-
-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.
+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, 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
@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
@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
--- /dev/null
+/* { 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 ??/\
+
/* { 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. */
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 ??/\
+