Go to file
Jakub Jelinek 8f185d3d7a libcpp: Fix up comma diagnostics in preprocessor for C++ [PR120778]
The P2843R3 Preprocessing is never undefined paper contains comments
that various compilers handle comma operators in preprocessor expressions
incorrectly and I think they are right.

In both C and C++ the grammar uses constant-expression non-terminal
for #if/#elif and in both C and C++ that NT is conditional-expression,
so
  #if 1, 2
is IMHO clearly wrong in both languages.

C89 then says for constant-expression
"Constant expressions shall not contain assignment, increment, decrement,
function-call, or comma operators, except when they are contained within the
operand of a sizeof operator."
Because all the remaining identifiers in the #if/#elif expression are
replaced with 0 I think assignments, increment, decrement and function-call
aren't that big deal because (0 = 1) or ++4 etc. are all invalid, but
for comma expressions I think it matters.  In r0-56429 PR456 Joseph has
added !CPP_OPTION (pfile, c99) to handle that correctly.
Then C99 changed that to:
"Constant expressions shall not contain assignment, increment, decrement, function-call,
or comma operators, except when they are contained within a subexpression that is not
evaluated."
That made for C99+
  #if 1 || (1, 2)
etc. valid but
  #if (1, 2)
is still invalid, ditto
  #if 1 ? 1, 2 : 3

In C++ I can't find anything like that though, and as can be seen on say
int a[(1, 2)];
int b[1 ? 1, 2 : 3];
being accepted by C++ and rejected by C while
int c[1, 2];
int d[1 ? 2 : 3, 4];
being rejected in both C and C++, so I think for C++ it is indeed just the
grammar that prevents #if 1, 2.  When it is the second operand of ?: or
inside of () the grammar just uses expression and that allows comma
operator.

So, the following patch uses different decisions for C++ when to diagnose
comma operator in preprocessor expressions, for C++ tracks if it is inside
of () (obviously () around #embed clauses don't count unless one uses
limit ((1, 2)) etc.) or inside of the second ?: operand and allows comma
operator there and disallows elsewhere.

BTW, I wonder if anything in the standard disallows <=> in the preprocessor
expressions.  Say
  #if (0 <=> 1) < 0
etc.
  #include <compare>
  constexpr int a = (0 <=> 1) < 0;
is valid (but not valid without #include <compare>) and the expressions
don't use any identifiers.

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

	PR c++/120778
	* internal.h (struct lexer_state): Add comma_ok member.
	* expr.cc (_cpp_parse_expr): Initialize it to 0, increment on
	CPP_OPEN_PAREN and CPP_QUERY, decrement on CPP_CLOSE_PAREN
	and CPP_COLON.
	(num_binary_op): For C++ pedwarn on comma operator if
	pfile->state.comma_ok is 0 instead of !c99 or skip_eval.

	* g++.dg/cpp/if-comma-1.C: New test.
2025-07-30 13:20:59 +02:00
.forgejo top-level: Add pull request template for Forgejo 2024-10-23 19:45:09 +01:00
.github Minor formatting fix for newly-added file from previous commit 2023-11-01 19:28:56 -04:00
INSTALL
c++tools Daily bump. 2025-06-03 00:18:06 +00:00
config Daily bump. 2025-05-24 00:17:55 +00:00
contrib Daily bump. 2025-07-29 00:19:24 +00:00
fixincludes Daily bump. 2024-07-12 00:17:52 +00:00
gcc libcpp: Fix up comma diagnostics in preprocessor for C++ [PR120778] 2025-07-30 13:20:59 +02:00
gnattools Daily bump. 2025-06-23 00:16:33 +00:00
gotools Daily bump. 2024-04-16 00:18:06 +00:00
include Daily bump. 2025-07-10 00:20:18 +00:00
libada Update copyright years. 2025-01-02 11:59:57 +01:00
libatomic Daily bump. 2025-05-23 00:17:35 +00:00
libbacktrace Daily bump. 2025-04-11 00:17:32 +00:00
libcc1 Daily bump. 2025-07-26 00:19:51 +00:00
libcody Update Copyright year in ChangeLog files 2025-01-02 11:13:18 +01:00
libcpp libcpp: Fix up comma diagnostics in preprocessor for C++ [PR120778] 2025-07-30 13:20:59 +02:00
libdecnumber Update copyright years. 2025-01-02 11:59:57 +01:00
libffi Daily bump. 2024-10-26 00:19:39 +00:00
libgcc Daily bump. 2025-07-18 00:19:21 +00:00
libgcobol Daily bump. 2025-07-14 00:16:48 +00:00
libgfortran fortran: implement split for fortran 2023 2025-07-30 10:30:46 +02:00
libgm2 Daily bump. 2025-07-24 00:20:56 +00:00
libgo runtime: avoid libc memmove and memclr 2025-07-08 15:49:16 -07:00
libgomp Daily bump. 2025-07-22 00:19:29 +00:00
libgrust Daily bump. 2025-04-01 00:19:09 +00:00
libiberty Daily bump. 2025-07-10 00:20:18 +00:00
libitm Daily bump. 2025-05-10 00:17:59 +00:00
libobjc Update copyright years. 2025-01-02 11:59:57 +01:00
libphobos Daily bump. 2025-07-02 00:19:39 +00:00
libquadmath Daily bump. 2025-04-10 00:18:06 +00:00
libsanitizer [sanitizer_common] Fix build on ppc64+musl (#120036) 2025-06-27 15:13:53 -06:00
libssp Update copyright years. 2025-01-02 11:59:57 +01:00
libstdc++-v3 Daily bump. 2025-07-30 00:19:13 +00:00
libvtv Daily bump. 2025-05-10 00:17:59 +00:00
lto-plugin Daily bump. 2025-03-07 00:17:19 +00:00
maintainer-scripts Daily bump. 2025-07-05 00:19:48 +00:00
zlib Daily bump. 2023-10-23 00:16:43 +00:00
.b4-config Add config file so b4 uses inbox.sourceware.org automatically 2024-07-28 11:13:16 +01:00
.dir-locals.el dir-locals: apply our C settings in C++ also 2024-07-31 20:38:27 +02:00
.gitattributes
.gitignore gccrs: git: Ignore libgrust build folders 2025-03-17 16:35:21 +01:00
ABOUT-NLS
COPYING
COPYING.LIB
COPYING.RUNTIME
COPYING3
COPYING3.LIB
ChangeLog Daily bump. 2025-07-23 00:20:17 +00:00
ChangeLog.jit
ChangeLog.tree-ssa
MAINTAINERS MAINTAINERS: Add myself as C++ front end reviewer 2025-07-22 14:02:15 -04:00
Makefile.def [AUTOFDO] Fix autogen remake issue 2025-05-29 08:47:19 +10:00
Makefile.in Ada: Introduce GNATMAKE_FOR_BUILD Makefile variable 2025-06-23 00:11:19 +02:00
Makefile.tpl Ada: Introduce GNATMAKE_FOR_BUILD Makefile variable 2025-06-23 00:11:19 +02:00
README
SECURITY.txt Remove Debian from SECURITY.txt 2024-11-19 12:27:33 +01:00
ar-lib
compile
config-ml.in Remove extra argument from subst macro 2025-03-13 07:26:04 +01:00
config.guess
config.rpath
config.sub
configure Ada: Introduce GNATMAKE_FOR_BUILD Makefile variable 2025-06-23 00:11:19 +02:00
configure.ac Ada: Introduce GNATMAKE_FOR_BUILD Makefile variable 2025-06-23 00:11:19 +02:00
depcomp
install-sh
libtool-ldflags
libtool.m4 Build: fix error in fixinclude configure 2023-11-22 11:54:33 +01:00
ltgcc.m4
ltmain.sh ltmain.sh: allow more flags at link-time 2024-09-25 19:05:24 +01:00
ltoptions.m4
ltsugar.m4
ltversion.m4
lt~obsolete.m4
missing
mkdep
mkinstalldirs
move-if-change
multilib.am
symlink-tree
test-driver
ylwrap

README

This directory contains the GNU Compiler Collection (GCC).

The GNU Compiler Collection is free software.  See the files whose
names start with COPYING for copying permission.  The manuals, and
some of the runtime libraries, are under different terms; see the
individual source files for details.

The directory INSTALL contains copies of the installation information
as HTML and plain text.  The source of this information is
gcc/doc/install.texi.  The installation information includes details
of what is included in the GCC sources and what files GCC installs.

See the file gcc/doc/gcc.texi (together with other files that it
includes) for usage and porting information.  An online readable
version of the manual is in the files gcc/doc/gcc.info*.

See http://gcc.gnu.org/bugs/ for how to report bugs usefully.

Copyright years on GCC source files may be listed using range
notation, e.g., 1987-2012, indicating that every year in the range,
inclusive, is a copyrightable year that could otherwise be listed
individually.