OSDN Git Service

Implement #pragma GCC warning/error
authorfw <fw@138bc75d-0d04-0410-961f-82ee72b054a4>
Thu, 4 Oct 2012 15:33:11 +0000 (15:33 +0000)
committerfw <fw@138bc75d-0d04-0410-961f-82ee72b054a4>
Thu, 4 Oct 2012 15:33:11 +0000 (15:33 +0000)
2012-10-04  Florian Weimer  <fweimer@redhat.com>

* doc/cpp.texi (Pragmas): Document #pragma GCC warning, #pragma
GCC error.

2012-10-04  Florian Weimer  <fweimer@redhat.com>

* c-c++-common/cpp/diagnostic-pragma-1.c: New testcase.

2012-10-04  Florian Weimer  <fweimer@redhat.com>

* directives.c (do_pragma_warning_or_error): New.
(do_pragma_warning): New.
(do_pragma_error): New.
(_cpp_init_internal_pragmas): Register new pragmas.

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

gcc/ChangeLog
gcc/doc/cpp.texi
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/cpp/diagnostic-pragma-1.c [new file with mode: 0644]
libcpp/ChangeLog
libcpp/directives.c

index 1e030d9..959086c 100644 (file)
@@ -1,3 +1,8 @@
+2012-10-04  Florian Weimer  <fweimer@redhat.com>
+
+       * doc/cpp.texi (Pragmas): Document #pragma GCC warning, #pragma
+       GCC error.
+
 2012-10-04  Richard Guenther  <rguenther@suse.de>
 
        PR middle-end/54735
index b363db0..fa5989e 100644 (file)
@@ -3634,6 +3634,15 @@ This pragma takes no arguments.  It causes the rest of the code in the
 current file to be treated as if it came from a system header.
 @xref{System Headers}.
 
+@item #pragma GCC warning
+@itemx #pragma GCC error
+@code{#pragma GCC warning "message"} causes the preprocessor to issue
+a warning diagnostic with the text @samp{message}.  The message
+contained in the pragma must be a single string literal.  Similarly,
+@code{#pragma GCC error "message"} issues an error message.  Unlike
+the @samp{#warning} and @samp{#error} directives, these pragmas can be
+embedded in preprocessor macros using @samp{_Pragma}.
+
 @end ftable
 
 @node Other Directives
index 306d277..e058566 100644 (file)
@@ -1,3 +1,7 @@
+2012-10-04  Florian Weimer  <fweimer@redhat.com>
+
+       * c-c++-common/cpp/diagnostic-pragma-1.c: New testcase.
+
 2012-10-04  Paolo Carlini  <paolo.carlini@oracle.com>
 
        PR c++/54323
diff --git a/gcc/testsuite/c-c++-common/cpp/diagnostic-pragma-1.c b/gcc/testsuite/c-c++-common/cpp/diagnostic-pragma-1.c
new file mode 100644 (file)
index 0000000..9867c94
--- /dev/null
@@ -0,0 +1,11 @@
+// { dg-do compile }
+
+#pragma GCC warning "warn-a" // { dg-warning warn-a }
+#pragma GCC error "err-b" // { dg-error err-b }
+
+#define CONST1 _Pragma("GCC warning \"warn-c\"") 1
+#define CONST2 _Pragma("GCC error \"err-d\"") 2
+
+char a[CONST1]; // { dg-warning warn-c }
+char b[CONST2]; // { dg-error err-d }
+
index a26def2..957b216 100644 (file)
@@ -1,3 +1,10 @@
+2012-10-04  Florian Weimer  <fweimer@redhat.com>
+
+       * directives.c (do_pragma_warning_or_error): New.
+       (do_pragma_warning): New.
+       (do_pragma_error): New.
+       (_cpp_init_internal_pragmas): Register new pragmas.
+
 2012-09-25  Dehao Chen  <dehao@google.com>
 
        PR middle-end/54704
index a8f2cc4..3c79b6d 100644 (file)
@@ -1,7 +1,5 @@
 /* CPP Library. (Directive handling.)
-   Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-   1999, 2000, 2001, 2002, 2003, 2004, 2005,
-   2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
+   Copyright (C) 1986-2012 Free Software Foundation, Inc.
    Contributed by Per Bothner, 1994-95.
    Based on CCCP program by Paul Rubin, June 1986
    Adapted to ANSI C, Richard Stallman, Jan 1987
@@ -118,6 +116,9 @@ static void do_pragma_once (cpp_reader *);
 static void do_pragma_poison (cpp_reader *);
 static void do_pragma_system_header (cpp_reader *);
 static void do_pragma_dependency (cpp_reader *);
+static void do_pragma_warning_or_error (cpp_reader *, bool error);
+static void do_pragma_warning (cpp_reader *);
+static void do_pragma_error (cpp_reader *);
 static void do_linemarker (cpp_reader *);
 static const cpp_token *get_token_no_padding (cpp_reader *);
 static const cpp_token *get__Pragma_string (cpp_reader *);
@@ -1263,6 +1264,8 @@ _cpp_init_internal_pragmas (cpp_reader *pfile)
   register_pragma_internal (pfile, "GCC", "system_header",
                            do_pragma_system_header);
   register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
+  register_pragma_internal (pfile, "GCC", "warning", do_pragma_warning);
+  register_pragma_internal (pfile, "GCC", "error", do_pragma_error);
 }
 
 /* Return the number of registered pragmas in PE.  */
@@ -1634,6 +1637,42 @@ do_pragma_dependency (cpp_reader *pfile)
   free ((void *) fname);
 }
 
+/* Issue a diagnostic with the message taken from the pragma.  If
+   ERROR is true, the diagnostic is a warning, otherwise, it is an
+   error.  */
+static void
+do_pragma_warning_or_error (cpp_reader *pfile, bool error)
+{
+  const cpp_token *tok = _cpp_lex_token (pfile);
+  cpp_string str;
+  if (tok->type != CPP_STRING
+      || !cpp_interpret_string_notranslate (pfile, &tok->val.str, 1, &str,
+                                           CPP_STRING)
+      || str.len == 0)
+    {
+      cpp_error (pfile, CPP_DL_ERROR, "invalid \"#pragma GCC %s\" directive",
+                error ? "error" : "warning");
+      return;
+    }
+  cpp_error (pfile, error ? CPP_DL_ERROR : CPP_DL_WARNING,
+            "%s", str.text);
+  free ((void *)str.text);
+}
+
+/* Issue a warning diagnostic.  */
+static void
+do_pragma_warning (cpp_reader *pfile)
+{
+  do_pragma_warning_or_error (pfile, false);
+}
+
+/* Issue an error diagnostic.  */
+static void
+do_pragma_error (cpp_reader *pfile)
+{
+  do_pragma_warning_or_error (pfile, true);
+}
+
 /* Get a token but skip padding.  */
 static const cpp_token *
 get_token_no_padding (cpp_reader *pfile)