OSDN Git Service

* cpphash.h (struct spec_nodes): Add n_true and n_false.
authorzack <zack@138bc75d-0d04-0410-961f-82ee72b054a4>
Wed, 7 Feb 2001 18:32:42 +0000 (18:32 +0000)
committerzack <zack@138bc75d-0d04-0410-961f-82ee72b054a4>
Wed, 7 Feb 2001 18:32:42 +0000 (18:32 +0000)
* cppinit.c (cpp_create_reader): Initialize them.
(append_include_chain): cxx_aware arg might be unused.
* cppexp.c (lex): In C++ mode, recognize 'true' and 'false'
keywords and give them their phase 7 meaning.  Pedwarn about
this unless '__bool_true_false_are_defined' is defined.

* g++.dg/stdbool-if.C: New test.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@39523 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/cppexp.c
gcc/cppinit.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/stdbool-if.C [new file with mode: 0644]

index bbfc2fe..2f385b7 100644 (file)
@@ -1,3 +1,12 @@
+2001-02-07  Zack Weinberg  <zack@wolery.stanford.edu>
+
+       * cpphash.h (struct spec_nodes): Add n_true and n_false.
+       * cppinit.c (cpp_create_reader): Initialize them.
+       (append_include_chain): cxx_aware arg might be unused.
+       * cppexp.c (lex): In C++ mode, recognize 'true' and 'false'
+       keywords and give them their phase 7 meaning.  Pedwarn about
+       this unless '__bool_true_false_are_defined' is defined.
+
 2001-02-07  Alexandre Oliva  <aoliva@redhat.com>
 
        * lcm.c (optimize_mode_switching): Emit mode_set before the
index bea20a2..11cde70 100644 (file)
@@ -426,18 +426,36 @@ lex (pfile, skip_evaluation, token)
 
          return parse_defined (pfile);
        }
-      /* Controlling #if expressions cannot contain identifiers (they
-        could become macros in the future).  */
-      pfile->mi_state = MI_FAILED;
-
-      op.op = CPP_INT;
-      op.unsignedp = 0;
-      op.value = 0;
+      else if (CPP_OPTION (pfile, cplusplus)
+              && (token->val.node == pfile->spec_nodes.n_true
+                  || token->val.node == pfile->spec_nodes.n_false))
+       {
+         op.op = CPP_INT;
+         op.unsignedp = 0;
+         op.value = (token->val.node == pfile->spec_nodes.n_true);
+
+         /* Warn about use of true or false in #if when pedantic
+            and stdbool.h has not been included.  */
+         if (CPP_PEDANTIC (pfile)
+             && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
+           cpp_pedwarn (pfile, "ISO C++ does not permit \"%s\" in #if",
+                        token->val.node->name);
+         return op;
+       }
+      else
+       {
+         /* Controlling #if expressions cannot contain identifiers (they
+            could become macros in the future).  */
+         pfile->mi_state = MI_FAILED;
 
-      if (CPP_OPTION (pfile, warn_undef) && !skip_evaluation)
-       cpp_warning (pfile, "\"%s\" is not defined", token->val.node->name);
+         op.op = CPP_INT;
+         op.unsignedp = 0;
+         op.value = 0;
 
-      return op;
+         if (CPP_OPTION (pfile, warn_undef) && !skip_evaluation)
+           cpp_warning (pfile, "\"%s\" is not defined", token->val.node->name);
+         return op;
+       }
 
     case CPP_HASH:
       {
index 845d35b..5393666 100644 (file)
@@ -203,7 +203,7 @@ append_include_chain (pfile, dir, path, cxx_aware)
      cpp_reader *pfile;
      char *dir;
      int path;
-     int cxx_aware;
+     int cxx_aware ATTRIBUTE_UNUSED;
 {
   struct cpp_pending *pend = CPP_OPTION (pfile, pending);
   struct file_name_list *new;
@@ -554,6 +554,8 @@ cpp_create_reader (lang)
   s = &pfile->spec_nodes;
   s->n_L                = cpp_lookup (pfile, DSC("L"));
   s->n_defined         = cpp_lookup (pfile, DSC("defined"));
+  s->n_true            = cpp_lookup (pfile, DSC("true"));
+  s->n_false           = cpp_lookup (pfile, DSC("false"));
   s->n__Pragma         = cpp_lookup (pfile, DSC("_Pragma"));
   s->n__STRICT_ANSI__   = cpp_lookup (pfile, DSC("__STRICT_ANSI__"));
   s->n__CHAR_UNSIGNED__ = cpp_lookup (pfile, DSC("__CHAR_UNSIGNED__"));
index 3e77bda..b40f2d5 100644 (file)
@@ -1,3 +1,7 @@
+2001-02-07  Zack Weinberg  <zack@wolery.stanford.edu>
+
+       * g++.dg/stdbool-if.C: New test.
+
 Wed Feb  7 09:54:47 2001  Ovidiu Predescu  <ovidiu@cup.hp.com>
 
        * objc/execute/fdecl.m: Added main().
diff --git a/gcc/testsuite/g++.dg/stdbool-if.C b/gcc/testsuite/g++.dg/stdbool-if.C
new file mode 100644 (file)
index 0000000..e9800bf
--- /dev/null
@@ -0,0 +1,33 @@
+/* { dg-do compile } */
+/* { dg-options -pedantic } */
+
+/* test of 'true' and 'false' in #if.  this is accepted with a pedwarn
+   before stdbool.h is included, silently afterward.  */
+
+/* Make sure they're viable keywords.  */
+bool a = true;
+bool b = false;
+
+#if true               /* { dg-warning "true" "true in #if pedwarn" } */
+#else
+#error true is false   /* { dg-bogus "true" "true is false" } */
+#endif
+
+#if false              /* { dg-warning "false" "false in #if pedwarn" } */
+#error false is true   /* { dg-bogus "false" "false is true" } */
+#endif
+
+#include <stdbool.h>
+
+/* Must still be viable keywords.  */
+bool c = true;
+bool d = false;
+
+#if true               /* { dg-bogus "true" "true in #if with stdbool.h" } */
+#else
+#error true is false   /* { dg-bogus "true" "true is false" } */
+#endif
+
+#if false              /* { dg-bogus "false" "false in #if with stdbool.h" } */
+#error false is true   /* { dg-bogus "false" "false is true" } */
+#endif