This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch] Add new hook to diagnose address space usage


This adds a new hook that allows to emit better diagnostics if an address space is used that's not available.

One solution would be no not register the address space with c_register_addr_space but that gives ugly report like

error: expected '=', ',', ';', 'asm' or '__attribute__' before 'f321'

The hook allows better diagnostics: The address spaces are registered with c_register_addr_space and if the parser comes across an address space it provides the hook with the needed information, in particular the location of the token so that the message would be something like

ptiny.c:12:11: error: address space '__flash2' not supported for devices with flash size up to 128 KiB
 const int __flash2 f321[] = { 321, 654 };
           ^~~~~~~~

This only works if address spaces are part of the language dialect.

Default implementation is void.

The intended user is the avr backend which currently emits diagnostics in some hook like TARGET_INSERT_ATTRIBUTER, but the locations are out of sync there.

Ok for trunk if bootstrap passes?

Johann


gcc/
	* target.def (addr_space): Add new diagnose_usage to hook vector.
	* targhooks.c (default_addr_space_diagnose_usage): Add default
	implementation and...
	* targhooks.h (default_addr_space_diagnose_usage): ... its prototype.
	* c/c-parser.c (c_lex_one_token) [CPP_NAME]: If the token
	is some address space, call targetm.addr_space.diagnose_usage.
	* doc/tm.texi.in (Named Address Spaces): Add anchor for
	TARGET_ADDR_SPACE_DIAGNOSE_USAGE documentation.
	* doc/tm.texi: Regenerate.
Index: target.def
===================================================================
--- target.def	(revision 237587)
+++ target.def	(working copy)
@@ -3241,6 +3241,21 @@ The result is the value to be used with
  int, (addr_space_t as),
  default_addr_space_debug)
 
+/* Function to emit custom diagnostic if an address space is used.  */
+DEFHOOK
+(diagnose_usage,
+ "Define this hook if the availability of an address space depends on\n\
+command line options and some diagnostics shall be printed when the\n\
+address space is used.  This hook is called during parsing and allows\n\
+to emit a better diagnostic compared to the case where the address space\n\
+was not registered with @code{c_register_addr_space}.  @var{as} is\n\
+the address space as registered with @code{c_register_addr_space}.\n\
+@var{loc} is the location of the address space qualifier token.\n\
+Return true if the hook issued an error and false, otherwise.\n\
+The default implementation does nothing.",
+ bool, (addr_space_t as, location_t loc),
+ default_addr_space_diagnose_usage)
+
 HOOK_VECTOR_END (addr_space)
 
 #undef HOOK_PREFIX
Index: targhooks.c
===================================================================
--- targhooks.c	(revision 237587)
+++ targhooks.c	(working copy)
@@ -1291,6 +1291,14 @@ default_addr_space_debug (addr_space_t a
   return as;
 }
 
+bool
+default_addr_space_diagnose_usage (addr_space_t ARG_UNUSED (as),
+				   location_t ARG_UNUSED (loc))
+{
+  return false;
+}
+	 
+
 /* The default hook for TARGET_ADDR_SPACE_CONVERT. This hook should never be
    called for targets with only a generic address space.  */
 
Index: targhooks.h
===================================================================
--- targhooks.h	(revision 237587)
+++ targhooks.h	(working copy)
@@ -181,6 +181,7 @@ extern rtx default_addr_space_legitimize
 extern bool default_addr_space_subset_p (addr_space_t, addr_space_t);
 extern bool default_addr_space_zero_address_valid (addr_space_t);
 extern int default_addr_space_debug (addr_space_t);
+extern bool default_addr_space_diagnose_usage (addr_space_t, location_t);
 extern rtx default_addr_space_convert (rtx, tree, tree);
 extern unsigned int default_case_values_threshold (void);
 extern bool default_have_conditional_execution (void);
Index: c/c-parser.c
===================================================================
--- c/c-parser.c	(revision 237587)
+++ c/c-parser.c	(working copy)
@@ -300,6 +300,9 @@ c_lex_one_token (c_parser *parser, c_tok
 	    else if (rid_code >= RID_FIRST_ADDR_SPACE
 		     && rid_code <= RID_LAST_ADDR_SPACE)
 	      {
+		addr_space_t as;
+		as = (addr_space_t) (rid_code - RID_FIRST_ADDR_SPACE);
+		targetm.addr_space.diagnose_usage (as, token->location);
 		token->id_kind = C_ID_ADDRSPACE;
 		token->keyword = rid_code;
 		break;
Index: doc/tm.texi.in
===================================================================
--- doc/tm.texi.in	(revision 237587)
+++ doc/tm.texi.in	(working copy)
@@ -7486,6 +7486,8 @@ c_register_addr_space ("__ea", ADDR_SPAC
 
 @hook TARGET_ADDR_SPACE_DEBUG
 
+@hook TARGET_ADDR_SPACE_DIAGNOSE_USAGE
+
 @node Misc
 @section Miscellaneous Parameters
 @cindex parameters, miscellaneous
Index: doc/tm.texi
===================================================================
--- doc/tm.texi	(revision 237587)
+++ doc/tm.texi	(working copy)
@@ -10431,6 +10431,18 @@ Define this to define how the address sp
 The result is the value to be used with @code{DW_AT_address_class}.
 @end deftypefn
 
+@deftypefn {Target Hook} bool TARGET_ADDR_SPACE_DIAGNOSE_USAGE (addr_space_t @var{as}, location_t @var{loc})
+Define this hook if the availability of an address space depends on
+command line options and some diagnostics shall be printed when the
+address space is used.  This hook is called during parsing and allows
+to emit a better diagnostic compared to the case where the address space
+was not registered with @code{c_register_addr_space}.  @var{as} is
+the address space as registered with @code{c_register_addr_space}.
+@var{loc} is the location of the address space qualifier token.
+Return true if the hook issued an error and false, otherwise.
+The default implementation does nothing.
+@end deftypefn
+
 @node Misc
 @section Miscellaneous Parameters
 @cindex parameters, miscellaneous

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]