summaryrefslogtreecommitdiff
path: root/gcc/testsuite/c-c++-common
diff options
context:
space:
mode:
authorNathan Sidwell <nathan@acm.org>2020-05-08 11:06:49 -0700
committerNathan Sidwell <nathan@acm.org>2020-05-08 11:13:29 -0700
commitb224c3763e018e8bdd0047b3eb283992fb655ce0 (patch)
tree0b9853fb61dcbb380a433dec1f58679c77036663 /gcc/testsuite/c-c++-common
parentbc484e250990393e887f7239157cc85ce6fadcce (diff)
preprocessor: Reimplement directives only processing, support raw literals.
The existing directives-only code (a) punched a hole through the libcpp interface and (b) didn't support raw string literals. This reimplements this preprocessing mode. I added a proper callback interface, and adjusted c-ppoutput to use it. Sadly I cannot get rid of the libcpp/internal.h include for unrelated reasons. The new scanner is in lex.x, and works doing some backwards scanning when it finds a charater of interest. This reduces the number of cases one has to deal with in forward scanning. It may have different failure mode than forward scanning on bad tokenization. Finally, Moved some cpp tests from the c-specific dg.gcc/cpp directory to the c-c++-common/cpp shared directory, libcpp/ * directives-only.c: Delete. * Makefile.in (libcpp_a_OBJS, libcpp_a_SOURCES): Remove it. * include/cpplib.h (enum CPP_DO_task): New enum. (cpp_directive_only_preprocess): Declare. * internal.h (_cpp_dir_only_callbacks): Delete. (_cpp_preprocess_dir_only): Delete. * lex.c (do_peek_backslask, do_peek_next, do_peek_prev): New. (cpp_directives_only_process): New implementation. gcc/c-family/ Reimplement directives only processing. * c-ppoutput.c (token_streamer): Ne. (directives_only_cb): New. Swallow ... (print_lines_directives_only): ... this. (scan_translation_unit_directives_only): Reimplment using the published interface. gcc/testsuite/ * gcc.dg/cpp/counter-[23].c: Move to c-c+_-common/cpp. * gcc.dg/cpp/dir-only-*: Likewise. * c-c++-common/cpp/dir-only-[78].c: New.
Diffstat (limited to 'gcc/testsuite/c-c++-common')
-rw-r--r--gcc/testsuite/c-c++-common/cpp/counter-2.c14
-rw-r--r--gcc/testsuite/c-c++-common/cpp/counter-3.c10
-rw-r--r--gcc/testsuite/c-c++-common/cpp/dir-only-1.c73
-rw-r--r--gcc/testsuite/c-c++-common/cpp/dir-only-1.h3
-rw-r--r--gcc/testsuite/c-c++-common/cpp/dir-only-2.c12
-rw-r--r--gcc/testsuite/c-c++-common/cpp/dir-only-3.c13
-rw-r--r--gcc/testsuite/c-c++-common/cpp/dir-only-3a.h8
-rw-r--r--gcc/testsuite/c-c++-common/cpp/dir-only-3b.h9
-rw-r--r--gcc/testsuite/c-c++-common/cpp/dir-only-4.c6
-rw-r--r--gcc/testsuite/c-c++-common/cpp/dir-only-5.c7
-rw-r--r--gcc/testsuite/c-c++-common/cpp/dir-only-6.c7
-rw-r--r--gcc/testsuite/c-c++-common/cpp/dir-only-7.c35
-rw-r--r--gcc/testsuite/c-c++-common/cpp/dir-only-8.c38
13 files changed, 235 insertions, 0 deletions
diff --git a/gcc/testsuite/c-c++-common/cpp/counter-2.c b/gcc/testsuite/c-c++-common/cpp/counter-2.c
new file mode 100644
index 00000000000..7d6578d9ce3
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/cpp/counter-2.c
@@ -0,0 +1,14 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+ Contributed by Ollie Wild <aaw@google.com>. */
+
+/* { dg-do preprocess } */
+/* { dg-options -fdirectives-only } */
+
+/* Tests __COUNTER__ macro expansion is disabled inside directives with
+ -fdirectives-only. */
+
+#ifdef __COUNTER__ /* Macro not expanded. */
+#endif
+
+#if __COUNTER__ == 0 /* { dg-error "__COUNTER__ expanded inside directive with -fdirectives-only" } */
+#endif
diff --git a/gcc/testsuite/c-c++-common/cpp/counter-3.c b/gcc/testsuite/c-c++-common/cpp/counter-3.c
new file mode 100644
index 00000000000..3b1824f25d5
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/cpp/counter-3.c
@@ -0,0 +1,10 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+ Contributed by Ollie Wild <aaw@google.com>. */
+
+/* { dg-do preprocess } */
+/* { dg-options "-fdirectives-only -fpreprocessed" } */
+
+/* Tests __COUNTER__ macro expansion is enabled outside directives with
+ -fdirectives-only. */
+
+int zero = __COUNTER__;
diff --git a/gcc/testsuite/c-c++-common/cpp/dir-only-1.c b/gcc/testsuite/c-c++-common/cpp/dir-only-1.c
new file mode 100644
index 00000000000..3c2261683c9
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/cpp/dir-only-1.c
@@ -0,0 +1,73 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+ Contributed by Ollie Wild <aaw@google.com>. */
+
+/* { dg-do preprocess } */
+/* { dg-options -fdirectives-only } */
+
+/* Tests scan_translation_unit_directives_only()'s handling of corner cases. */
+
+/* Ignore directives inside block comments...
+#error directive inside block comment
+*/
+
+// Escaped newline doesn't terminate line comment \
+#error directive inside line comment
+
+/* A comment canot start inside a string. */
+const char *c1 = "/*";
+#define NOT_IN_COMMENT
+const char *c2 = "*/";
+#ifndef NOT_IN_COMMENT
+#error Comment started inside a string literal
+#endif
+
+/* Escaped newline handling. */
+int i; \
+#error ignored escaped newline
+ \
+ \
+#define BOL
+#ifndef BOL
+#error escaped newline did not preserve beginning of line
+#endif
+
+/* Handles \\ properly at the end of a string. */
+"string ends in \\"/*
+#error Missed string terminator.
+*/
+
+/* Handles macro expansion in preprocessing directives. */
+#define HEADER "dir-only-1.h"
+#include HEADER
+#ifndef GOT_HEADER
+#error Failed to include header.
+#endif
+
+/\
+*
+#define IN_COMMENT
+*/
+#ifdef IN_COMMENT
+#error Escaped newline breaks block comment initiator.
+#endif
+
+/*
+*\
+/
+#define NOT_IN_COMMENT2
+/**/
+#ifndef NOT_IN_COMMENT2
+#error Escaped newline breaks block comment terminator.
+#endif
+
+/* Test escaped newline inside character escape sequence. */
+"\\
+\"/*
+#error Missed string terminator
+*/
+
+/* Block comments don't mask trailing preprocessing
+ directive. */ #define NOT_MASKED
+#ifndef NOT_MASKED
+#error Comment masks trailing directive.
+#endif
diff --git a/gcc/testsuite/c-c++-common/cpp/dir-only-1.h b/gcc/testsuite/c-c++-common/cpp/dir-only-1.h
new file mode 100644
index 00000000000..96dbcc0483c
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/cpp/dir-only-1.h
@@ -0,0 +1,3 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+ Contributed by Ollie Wild <aaw@google.com>. */
+#define GOT_HEADER
diff --git a/gcc/testsuite/c-c++-common/cpp/dir-only-2.c b/gcc/testsuite/c-c++-common/cpp/dir-only-2.c
new file mode 100644
index 00000000000..489b4d6ee34
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/cpp/dir-only-2.c
@@ -0,0 +1,12 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+ Contributed by Ollie Wild <aaw@google.com>. */
+
+/* { dg-do preprocess } */
+/* { dg-options "-fpreprocessed -fdirectives-only -DNOT_SET" } */
+
+/* Tests -fdirectives-only + -fpreprocessed. */
+
+/* Check this is not defined. */
+#ifdef NOT_SET
+#error Command line macro not disabled.
+#endif
diff --git a/gcc/testsuite/c-c++-common/cpp/dir-only-3.c b/gcc/testsuite/c-c++-common/cpp/dir-only-3.c
new file mode 100644
index 00000000000..e6eaa018096
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/cpp/dir-only-3.c
@@ -0,0 +1,13 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+ Contributed by Ollie Wild <aaw@google.com>. */
+
+/* { dg-do preprocess } */
+/* { dg-options "-fdirectives-only -H" } */
+/* { dg-message "dir-only-3a\.h\n\[^\n\]*dir-only-3b\.h\n\[^\n\]*dir-only-3a\.h\n" "include guard check" { target *-*-* } 0 } */
+
+/* Tests include guards. */
+
+#include "dir-only-3a.h"
+#include "dir-only-3b.h"
+#include "dir-only-3b.h"
+#include "dir-only-3a.h"
diff --git a/gcc/testsuite/c-c++-common/cpp/dir-only-3a.h b/gcc/testsuite/c-c++-common/cpp/dir-only-3a.h
new file mode 100644
index 00000000000..6644bbfb0d4
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/cpp/dir-only-3a.h
@@ -0,0 +1,8 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+ Contributed by Ollie Wild <aaw@google.com>. */
+
+extern int outside_guard
+
+#ifndef DIR_ONLY_3A_H
+#define DIR_ONLY_3A_H
+#endif
diff --git a/gcc/testsuite/c-c++-common/cpp/dir-only-3b.h b/gcc/testsuite/c-c++-common/cpp/dir-only-3b.h
new file mode 100644
index 00000000000..4edaa7b6ba5
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/cpp/dir-only-3b.h
@@ -0,0 +1,9 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+ Contributed by Ollie Wild <aaw@google.com>. */
+
+#ifndef DIR_ONLY_3B_H
+#define DIR_ONLY_3B_H
+
+extern int inside guard;
+
+#endif
diff --git a/gcc/testsuite/c-c++-common/cpp/dir-only-4.c b/gcc/testsuite/c-c++-common/cpp/dir-only-4.c
new file mode 100644
index 00000000000..25f1b463b6d
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/cpp/dir-only-4.c
@@ -0,0 +1,6 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+ Contributed by Ollie Wild <aaw@google.com>. */
+
+/* { dg-do preprocess } */
+/* { dg-options "-fdirectives-only -Wunused-macros" } */
+/* { dg-error "'-fdirectives-only' is incompatible with '-Wunused-macros'\n" "'-Wunused-macros' check" { target *-*-* } 0 } */
diff --git a/gcc/testsuite/c-c++-common/cpp/dir-only-5.c b/gcc/testsuite/c-c++-common/cpp/dir-only-5.c
new file mode 100644
index 00000000000..747006a682b
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/cpp/dir-only-5.c
@@ -0,0 +1,7 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+ Contributed by Ollie Wild <aaw@google.com>. */
+
+/* C++ silently ignores traditional! */
+/* { dg-do preprocess { target c } } */
+/* { dg-options "-fdirectives-only -traditional" } */
+/* { dg-error "'-fdirectives-only' is incompatible with '-traditional'\n" "'-traditional' check" { target *-*-* } 0 } */
diff --git a/gcc/testsuite/c-c++-common/cpp/dir-only-6.c b/gcc/testsuite/c-c++-common/cpp/dir-only-6.c
new file mode 100644
index 00000000000..0023205faf7
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/cpp/dir-only-6.c
@@ -0,0 +1,7 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+ Contributed by Ollie Wild <aaw@google.com>. */
+
+/* { dg-do preprocess } */
+/* { dg-options -fdirectives-only } */
+
+/* { dg-error "unterminated comment" }
diff --git a/gcc/testsuite/c-c++-common/cpp/dir-only-7.c b/gcc/testsuite/c-c++-common/cpp/dir-only-7.c
new file mode 100644
index 00000000000..b9fb4d88528
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/cpp/dir-only-7.c
@@ -0,0 +1,35 @@
+// { dg-do preprocess }
+// { dg-options "-std=c++11" { target c++ } }
+// { dg-options "-std=gnu99" { target c } }
+// { dg-additional-options -fdirectives-only }
+
+R"stuff(
+)nope"
+#error in raw literal
+)stuff"
+// comment
+#define bob 1
+// " comment
+#if !bob
+#error "no bob"
+#endif
+
+bob\
+\
+R"regular string not an erroneous raw one"
+
+"regular"R"***(not a raw string"
+#define HERE 1
+ //)***"
+#ifndef HERE
+#error "oops no HERE"
+#endif
+ /* comment */
+
+
+0e+R"*(not a raw string"
+#define CPP_NUM 1
+ //)*"
+#ifndef CPP_NUM
+#error "oops no CPP_NUM"
+#endif
diff --git a/gcc/testsuite/c-c++-common/cpp/dir-only-8.c b/gcc/testsuite/c-c++-common/cpp/dir-only-8.c
new file mode 100644
index 00000000000..b0e00f5b542
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/cpp/dir-only-8.c
@@ -0,0 +1,38 @@
+// { dg-do preprocess { target c++ } }
+// { dg-options "-std=c++14" }
+// { dg-additional-options -fdirectives-only }
+
+012'bcd
+#define A 1
+// '
+#ifndef A
+#error Fell into first char const
+#endif
+enum { A = 195'936'478 }; 'a'
+#define AA 1
+ // 'a
+#ifndef AA
+#error Fell into early char const
+#endif
+
+012\
+'bcd
+#define B 1
+// '
+#ifndef B
+#error Fell into second char const
+#endif
+
+.012'b
+#define C 1
+// '
+#ifndef C
+#error Fell into third char const
+#endif
+
+.0e+12'b
+#define D 1
+// '
+#ifndef D
+#error Fell into fourth char const
+#endif