From 043afb2a089d22e59dd0a6e9d4bb2ca3aa342432 Mon Sep 17 00:00:00 2001 From: Neil Booth Date: Tue, 4 Jul 2000 22:26:16 +0000 Subject: [PATCH] cpplex.c: Fix trigraph replacement within strings. * cpplex.c: Fix trigraph replacement within strings. * gcc.dg/cpp/lexident.c, gcc.dg/cpp/lexnum.c, gcc.dg/cpp/lexstrng.c: New tests. From-SVN: r34868 --- gcc/ChangeLog | 4 ++ gcc/cpplex.c | 2 +- gcc/testsuite/ChangeLog | 5 +++ gcc/testsuite/gcc.dg/cpp/lexident.c | 25 +++++++++++ gcc/testsuite/gcc.dg/cpp/lexnum.c | 50 +++++++++++++++++++++ gcc/testsuite/gcc.dg/cpp/lexstrng.c | 69 +++++++++++++++++++++++++++++ 6 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/cpp/lexident.c create mode 100644 gcc/testsuite/gcc.dg/cpp/lexnum.c create mode 100644 gcc/testsuite/gcc.dg/cpp/lexstrng.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 074c22649fc..df412c1efa8 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,7 @@ +2000-07-05 Neil Booth + + * cpplex.c: Fix trigraph replacement within strings. + 2000-07-04 Kaveh R. Ghazi * rs6000/aix.h (ASM_GENERATE_INTERNAL_LABEL): Fix format specifier. diff --git a/gcc/cpplex.c b/gcc/cpplex.c index a45336d1158..b0fba653a61 100644 --- a/gcc/cpplex.c +++ b/gcc/cpplex.c @@ -853,7 +853,7 @@ trigraph_replace (pfile, src, limit) /* Starting with src[1], find two consecutive '?'. The case of no trigraphs is streamlined. */ - for (; src + 1 < limit; src += 2) + for (src++; src + 1 < limit; src += 2) { if (src[0] != '?') continue; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 2824980484e..6b233fc675e 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2000-07-05 Neil Booth + + * gcc.dg/cpp/lexident.c, gcc.dg/cpp/lexnum.c, + gcc.dg/cpp/lexstrng.c: New tests. + 2000-07-04 Neil Booth * gcc.dg/cpp/macro1.c: Add more macro expansion tests. diff --git a/gcc/testsuite/gcc.dg/cpp/lexident.c b/gcc/testsuite/gcc.dg/cpp/lexident.c new file mode 100644 index 00000000000..c914023b0fd --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/lexident.c @@ -0,0 +1,25 @@ +/* Copyright (C) 2000 Free Software Foundation, Inc. */ + +/* { dg-do preprocess } */ +/* { dg-options "-trigraphs" } */ + +/* Test lexing of identifiers. */ + +/* Escaped newlines, _ and $ in identifiers. */ +#def\ +\ +ine foo_ + +#d\ +ef??/ +in\ +e b\ +a$r + +#ifndef foo_ +#error foo_ +#endif + +#ifndef ba$r +#error ba$r +#endif diff --git a/gcc/testsuite/gcc.dg/cpp/lexnum.c b/gcc/testsuite/gcc.dg/cpp/lexnum.c new file mode 100644 index 00000000000..7df15540833 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/lexnum.c @@ -0,0 +1,50 @@ +/* Copyright (C) 2000 Free Software Foundation, Inc. */ + +/* { dg-do run } */ +/* { dg-options "-trigraphs" } */ + +/* Test lexing of numbers. */ + +extern int puts (const char *); +extern void abort (void); +#define err(str) do { puts(str); abort(); } while (0) + +/* Escaped newlines. */ +#define foo 12\ +3\ +\ +4??/ +5 + +#if foo != 12345 +#error foo +#endif + +int main (int argc, char *argv[]) +{ + double a = 5.; + double x = .5; + +/* Decimal points, including initially and immediately before and + after an escaped newline. */ + if (a != 5) + err ("a"); + if (x != .\ +5) + err ("x != .5"); + x = 25\ +.\ +6; + if (x != 25.6) + err ("x != 25.6"); + + /* Test exponentials and their signs. A buggy lexer is more likely + to fail the compile, but never mind. */ + if (250 != 25e+1 || 250 != 25e1 || 250 != 2500e-1) + err ("exponentials"); + + /* Todo: p exponentials, and how to test preprocessing number + tokenisation? */ + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/cpp/lexstrng.c b/gcc/testsuite/gcc.dg/cpp/lexstrng.c new file mode 100644 index 00000000000..4fbb3b555eb --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/lexstrng.c @@ -0,0 +1,69 @@ +/* Copyright (C) 2000 Free Software Foundation, Inc. */ + +/* { dg-do run } */ +/* { dg-options "-trigraphs" } */ + +/* Test lexing of strings and character constants. */ + +#include + +#ifndef __WCHAR_TYPE__ +#define __WCHAR_TYPE__ int +#endif +typedef __WCHAR_TYPE__ wchar_t; + +extern int strcmp (const char *, const char *); +extern int puts (const char *); +extern void abort (void); +#define err(str) do { puts(str); abort(); } while (0) + +/* Escaped newlines. */ +const char *str1 = "s\ +t\ +\ +r??/ + 1"; + +const char x = '\ +??/ +b'; + +/* Test escaped terminators. */ +const char *term = "\"\\\"\\"; +const char termc = '\''; +const char *terms = "'"; + +/* Test wide strings and chars are lexed. */ +const wchar_t wchar = L'w'; +const wchar_t* wstring = L"wide string"; + +/* Test all 9 trigraphs embedded in a string. Test trigraphs do not + survive an embedded backslash newline. Test trigraphs preceded by + a '?' are still noticed. */ +const char *t = "??/\????=??)??\ +(??(??!??'??-???=???/ +?-"; + +int main (int argc, char *argv[]) +{ + if (strcmp (str1, "str 1")) + err ("str1"); + + if (x != 'b') + err ("b"); + + /* We have to split the string up to avoid trigraph replacement + here. Split the 2 trigraphs after both 1 and 2 ?s; just doing + this exposed a bug in the initial release of the tokenized lexer. */ + if (strcmp (t, "\\{}#]?" "?([|^~?#??" "-")) + err ("Embedded trigraphs"); + + if (term[0] != '"' || term[1] != '\\' || term[2] != '"' + || term[3] != '\\' || term[4] != '\0') + err ("Escaped string terminators"); + + if (termc != terms[0]) + err ("Escaped character constant terminator"); + + return 0; +} -- 2.30.2