]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Add testcase for CWG2577 [PR120778]
authorJakub Jelinek <jakub@redhat.com>
Thu, 7 Aug 2025 06:53:23 +0000 (08:53 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Thu, 7 Aug 2025 06:54:11 +0000 (08:54 +0200)
And here is the last part of the paper.  Contrary what the paper claims
(clearly they haven't tried -pedantic nor -pedantic-errors), I think we
already diagnose everything we should.

2025-08-07  Jakub Jelinek  <jakub@redhat.com>

PR preprocessor/120778
* g++.dg/DRs/dr2577-1.C: New test.
* g++.dg/DRs/dr2577-2.C: New test.
* g++.dg/DRs/dr2577-2.h: New file.
* g++.dg/DRs/dr2577-3.C: New test.
* g++.dg/DRs/dr2577-3.h: New file.

gcc/testsuite/g++.dg/DRs/dr2577-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/DRs/dr2577-2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/DRs/dr2577-2.h [new file with mode: 0644]
gcc/testsuite/g++.dg/DRs/dr2577-3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/DRs/dr2577-3.h [new file with mode: 0644]

diff --git a/gcc/testsuite/g++.dg/DRs/dr2577-1.C b/gcc/testsuite/g++.dg/DRs/dr2577-1.C
new file mode 100644 (file)
index 0000000..784b6a8
--- /dev/null
@@ -0,0 +1,40 @@
+// DR 2577 - Undefined behavior for preprocessing directives in macro arguments
+// { dg-do preprocess }
+// { dg-options "-pedantic-errors" }
+
+#define A(x)
+#define B(x, y)
+A(
+#if 1                          // { dg-error "embedding a directive within macro arguments is not portable" }
+1
+#else                          // { dg-error "embedding a directive within macro arguments is not portable" }
+2
+#endif                         // { dg-error "embedding a directive within macro arguments is not portable" }
+)
+B(1,
+#line 234                      // { dg-error "embedding a directive within macro arguments is not portable" }
+)
+#line 18
+A(
+#define C 1                    // { dg-error "embedding a directive within macro arguments is not portable" }
+)
+A(
+#undef C                       // { dg-error "embedding a directive within macro arguments is not portable" }
+)
+B(42,
+# 234 "dr2577-1.C"             // { dg-error "embedding a directive within macro arguments is not portable" }
+)                              // { dg-error "style of line directive is a GCC extension" "" { target *-*-* } .-1 }
+#line 28 "dr2577-1.C"
+B(
+#warning "foobar"              // { dg-error "embedding a directive within macro arguments is not portable" }
+, 12)                          // { dg-error "'#warning' before C\\\+\\\+23 is a GCC extension" "" { target c++20_down } .-1 }
+                               // { dg-warning "#warning \"foobar\"" "" { target *-*-* } .-2 }
+A(
+#pragma GCC diagnostics push   // { dg-error "embedding a directive within macro arguments is not portable" }
+)
+B(5,
+#pragma GCC diagnostics pop    // { dg-error "embedding a directive within macro arguments is not portable" }
+)
+A(
+#error foobar                  // { dg-error "embedding a directive within macro arguments is not portable" }
+)                              // { dg-error "#error foobar" "" { target *-*-* } .-1 }
diff --git a/gcc/testsuite/g++.dg/DRs/dr2577-2.C b/gcc/testsuite/g++.dg/DRs/dr2577-2.C
new file mode 100644 (file)
index 0000000..e54006a
--- /dev/null
@@ -0,0 +1,13 @@
+// DR 2577 - Undefined behavior for preprocessing directives in macro arguments
+// { dg-do preprocess }
+// { dg-options "-pedantic-errors" }
+
+#define A(x, y, z) x + y + z
+int a = A(
+#include "dr2577-2.h"          // { dg-error "embedding a directive within macro arguments is not portable" }
+,
+#include "dr2577-2.h"
+,
+#include "dr2577-2.h"
+);
+// { dg-error "unterminated argument list invoking macro 'A'" "" { target *-*-* } 0 }
diff --git a/gcc/testsuite/g++.dg/DRs/dr2577-2.h b/gcc/testsuite/g++.dg/DRs/dr2577-2.h
new file mode 100644 (file)
index 0000000..d00491f
--- /dev/null
@@ -0,0 +1 @@
+1
diff --git a/gcc/testsuite/g++.dg/DRs/dr2577-3.C b/gcc/testsuite/g++.dg/DRs/dr2577-3.C
new file mode 100644 (file)
index 0000000..6ebf419
--- /dev/null
@@ -0,0 +1,7 @@
+// DR 2577 - Undefined behavior for preprocessing directives in macro arguments
+// { dg-do preprocess }
+// { dg-options "-pedantic-errors" }
+
+#define A(x) x
+int a = A(
+#include "dr2577-3.h"          // { dg-error "embedding a directive within macro arguments is not portable" }
diff --git a/gcc/testsuite/g++.dg/DRs/dr2577-3.h b/gcc/testsuite/g++.dg/DRs/dr2577-3.h
new file mode 100644 (file)
index 0000000..5e36ce0
--- /dev/null
@@ -0,0 +1 @@
+1)