OSDN Git Service

* cpplex.c: Fix trigraph replacement within strings.
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / gcc.dg / cpp / lexstrng.c
1 /* Copyright (C) 2000 Free Software Foundation, Inc.  */
2
3 /* { dg-do run } */
4 /* { dg-options "-trigraphs" } */
5
6 /* Test lexing of strings and character constants.  */
7
8 #include <string.h>
9
10 #ifndef __WCHAR_TYPE__
11 #define __WCHAR_TYPE__ int
12 #endif
13 typedef __WCHAR_TYPE__ wchar_t;
14
15 extern int strcmp (const char *, const char *);
16 extern int puts (const char *);
17 extern void abort (void);
18 #define err(str) do { puts(str); abort(); } while (0)
19
20 /* Escaped newlines.  */
21 const char *str1 = "s\
22 t\
23 \
24 r??/
25   1";
26
27 const char x = '\
28 ??/
29 b';
30
31 /* Test escaped terminators.  */
32 const char *term = "\"\\\"\\";
33 const char termc = '\'';
34 const char *terms = "'";
35
36 /* Test wide strings and chars are lexed.  */
37 const wchar_t wchar = L'w';
38 const wchar_t* wstring = L"wide string";
39
40 /* Test all 9 trigraphs embedded in a string.  Test trigraphs do not
41    survive an embedded backslash newline.  Test trigraphs preceded by
42    a '?' are still noticed.  */
43 const char *t = "??/\??<??>??=??)??\
44 (??(??!??'??-???=???/
45 ?-";
46
47 int main (int argc, char *argv[])
48 {
49   if (strcmp (str1, "str  1"))
50     err ("str1");
51
52   if (x != 'b')
53     err ("b");
54
55   /* We have to split the string up to avoid trigraph replacement
56      here.  Split the 2 trigraphs after both 1 and 2 ?s; just doing
57      this exposed a bug in the initial release of the tokenized lexer.  */
58   if (strcmp (t, "\\{}#]?" "?([|^~?#??" "-"))
59     err ("Embedded trigraphs");
60
61   if (term[0] != '"' || term[1] != '\\' || term[2] != '"'
62       || term[3] != '\\' || term[4] != '\0')
63     err ("Escaped string terminators");
64
65   if (termc != terms[0])
66     err ("Escaped character constant terminator");
67
68   return 0;
69 }