From 0d48657d7378a4b1cb25ed181bca8020eae520f1 Mon Sep 17 00:00:00 2001 From: Simon Baldwin Date: Mon, 28 Jul 2008 11:55:11 +0000 Subject: [PATCH] c-pragma.c (handle_pragma_message): New function. * c-pragma.c (handle_pragma_message): New function. (init_pragma): Register handle_pragma_message. * doc/extend.texi (Diagnostic Pragmas): Added #pragma message documentation. * gcc.dg/pragma-message.c: New. From-SVN: r138206 --- gcc/ChangeLog | 7 ++++ gcc/c-pragma.c | 35 ++++++++++++++++++ gcc/doc/extend.texi | 29 +++++++++++++++ gcc/testsuite/ChangeLog | 4 ++ gcc/testsuite/gcc.dg/pragma-message.c | 53 +++++++++++++++++++++++++++ 5 files changed, 128 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/pragma-message.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 0c271d28b58..545f7112090 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2008-07-28 Simon Baldwin + + * c-pragma.c (handle_pragma_message): New function. + (init_pragma): Register handle_pragma_message. + * doc/extend.texi (Diagnostic Pragmas): Added #pragma message + documentation. + 2008-07-27 Victor Kaplansky PR tree-optimization/35252 diff --git a/gcc/c-pragma.c b/gcc/c-pragma.c index 6e4043ae672..b2bbfae8276 100644 --- a/gcc/c-pragma.c +++ b/gcc/c-pragma.c @@ -1173,6 +1173,39 @@ handle_pragma_optimize(cpp_reader *ARG_UNUSED(dummy)) } } +/* Print a plain user-specified message. */ + +static void +handle_pragma_message (cpp_reader *ARG_UNUSED(dummy)) +{ + enum cpp_ttype token; + tree x, message = 0; + + token = pragma_lex (&x); + if (token == CPP_OPEN_PAREN) + { + token = pragma_lex (&x); + if (token == CPP_STRING) + message = x; + else + GCC_BAD ("expected a string after %<#pragma message%>"); + if (pragma_lex (&x) != CPP_CLOSE_PAREN) + GCC_BAD ("malformed %<#pragma message%>, ignored"); + } + else if (token == CPP_STRING) + message = x; + else + GCC_BAD ("expected a string after %<#pragma message%>"); + + gcc_assert (message); + + if (pragma_lex (&x) != CPP_EOF) + warning (OPT_Wpragmas, "junk at end of %<#pragma message%>"); + + if (TREE_STRING_LENGTH (message) > 1) + inform ("#pragma message: %s", TREE_STRING_POINTER (message)); +} + /* A vector of registered pragma callbacks. */ DEF_VEC_O (pragma_handler); @@ -1341,6 +1374,8 @@ init_pragma (void) c_register_pragma_with_expansion (0, "redefine_extname", handle_pragma_redefine_extname); c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix); + c_register_pragma_with_expansion (0, "message", handle_pragma_message); + #ifdef REGISTER_TARGET_PRAGMAS REGISTER_TARGET_PRAGMAS (); #endif diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 4b1d302a48d..4c82dc60f2c 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -11616,6 +11616,35 @@ strict control over project policies. @end table +GCC also offers a simple mechanism for printing messages during +compilation. + +@table @code +@item #pragma message @var{string} +@cindex pragma, diagnostic + +Prints @var{string} as a compiler message on compilation. The message +is informational only, and is neither a compilation warning nor an error. + +@smallexample +#pragma message "Compiling " __FILE__ "..." +@end smallexample + +@var{string} may be parenthesized, and is printed with location +information. For example, + +@smallexample +#define DO_PRAGMA(x) _Pragma (#x) +#define TODO(x) DO_PRAGMA(message ("TODO - " #x)) + +TODO(Remember to fix this) +@end smallexample + +prints @samp{/tmp/file.c:4: note: #pragma message: +TODO - Remember to fix this}. + +@end table + @node Visibility Pragmas @subsection Visibility Pragmas diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index b5ee93b713e..66d32125533 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2008-07-28 Simon Baldwin + + * gcc.dg/pragma-message.c: New. + 2008-07-27 Victor Kaplansky PR tree-optimization/35252 diff --git a/gcc/testsuite/gcc.dg/pragma-message.c b/gcc/testsuite/gcc.dg/pragma-message.c new file mode 100644 index 00000000000..0f9c6bf447b --- /dev/null +++ b/gcc/testsuite/gcc.dg/pragma-message.c @@ -0,0 +1,53 @@ +/* Test that #pragma message "..." writes compiler messages. */ + +#pragma message /* { dg-warning "expected a string" } */ +#pragma message 0 /* { dg-warning "expected a string" } */ +#pragma message id /* { dg-warning "expected a string" } */ +#pragma message ( /* { dg-warning "expected a string" } */ +#pragma message (0 /* { dg-warning "expected a string" } */ +#pragma message (id /* { dg-warning "expected a string" } */ +#pragma message () /* { dg-warning "expected a string" } */ +#pragma message (0) /* { dg-warning "expected a string" } */ +#pragma message (id) /* { dg-warning "expected a string" } */ + +/* gcc prefixes '#pragma message ...' output with filename and line number, + then 'note: #pragma message: ', allowing dg-message to check output. + If unexpected pragma messages are printed (anything not caught by a + matching dg-message), dejagnu will report these as excess errors. */ + +#pragma message " +/* { dg-error "missing terminating" "" { target *-*-* } 18 } */ +/* { dg-warning "expected a string" "" { target *-*-* } 18 } */ +#pragma message "Bad 1 +/* { dg-error "missing terminating" "" { target *-*-* } 21 } */ +/* { dg-warning "expected a string" "" { target *-*-* } 21 } */ +#pragma message ("Bad 2 +/* { dg-error "missing terminating" "" { target *-*-* } 24 } */ +/* { dg-warning "expected a string" "" { target *-*-* } 24 } */ +#pragma message ("Bad 3" +/* { dg-warning "malformed '#pragma message" "" { target *-*-* } 27 } */ + +#pragma message "" junk +/* { dg-warning "junk at end of '#pragma message'" "" { target *-*-* } 30 } */ + +#pragma message ("") junk +/* { dg-warning "junk at end of '#pragma message'" "" { target *-*-* } 33 } */ + +#pragma message "" /* No output expected for empty messages. */ +#pragma message ("") + +#pragma message "Okay 1" /* { dg-message "Okay 1" } */ +#pragma message ("Okay 2") /* { dg-message "Okay 2" } */ +#define THREE "3" +#pragma message ("Okay " THREE) /* { dg-message "Okay 3" } */ + +/* Create a TODO() that prints a message on compilation. */ +#define DO_PRAGMA(x) _Pragma (#x) +#define TODO(x) DO_PRAGMA(message ("TODO - " #x)) +TODO(Okay 4) /* { dg-message "TODO - Okay 4" } */ + +#if 0 +#pragma message ("Not printed") +#endif + +int unused; /* Silence `ISO C forbids an empty translation unit' warning. */ -- 2.30.2