[PATCH] D120244: [clang][sema] Enable first-class bool support for C2x

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 21 04:36:19 PST 2022


tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, tahonermann.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This implements N2393.

I am a bit confused by the proposal only taking about adding "true" and "false" constants but then still using "bool" as a type. I'm not sure if this part of the patch is 100% correct.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120244

Files:
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/Sema/c2x-bool.c


Index: clang/test/Sema/c2x-bool.c
===================================================================
--- /dev/null
+++ clang/test/Sema/c2x-bool.c
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c2x -x c -fsyntax-only -verify %s
+
+_Static_assert(_Generic(true, _Bool : 1, default: 0));
+_Static_assert(_Generic(false, _Bool : 1, default: 0));
+
+_Static_assert(_Generic(true, bool : 1, default: 0));
+_Static_assert(_Generic(false, bool : 1, default: 0));
+
+_Static_assert(_Generic(true, bool : true, default: false));
+_Static_assert(_Generic(false, bool : true, default: false));
+
+_Static_assert(true == (bool)+1);
+_Static_assert(false == (bool)+0);
+
+
+static void *f = false; // expected-warning {{to null from a constant boolean expression}}
+static int one = true;
+static int zero = false;
+
+static void do_work() {
+  char *str = "Foo";
+  str[0] = 'f';
+  str[true] = 'f'; // TODO: This should warn(?)
+}
Index: clang/lib/Frontend/CompilerInvocation.cpp
===================================================================
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -3245,7 +3245,7 @@
   Opts.RenderScript = IK.getLanguage() == Language::RenderScript;
 
   // OpenCL and C++ both have bool, true, false keywords.
-  Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
+  Opts.Bool = Opts.OpenCL || Opts.CPlusPlus || Opts.C2x;
 
   // OpenCL has half keyword
   Opts.Half = Opts.OpenCL;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120244.410276.patch
Type: text/x-patch
Size: 1466 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220221/2d83b3e8/attachment-0001.bin>


More information about the cfe-commits mailing list