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]

cpplib: Groundwork for making cpp_reader an abstract data type


i.e. opaque.  Zack and I want to get as much out of cpplib.h as
possible; i.e. reduce cpplib's external interface.

There's more to this, but that will follow in subsequent patches.

This bootstrapped as part of a larger patch.  I've been careful and
made sure that cpp0 compiles and behaves properly, but I've not
bootstrapped with it alone.

Neil.

	* cppinit.c (cpp_handle_option): help_only is now part of the
	cpp_options structure.
	* cpplib.c (cpp_errors, cpp_get_options, cpp_get_callbacks,
	cpp_set_callbacks): New functions.
	* cpplib.h (cpp_callbacks): Break out as a named structure.
	(cpp_options): Move help_only here from cpp_reader.
	(CPP_FATAL_ERRORS): Update to use cpp_errors.
	(cpp_errors, cpp_get_options, cpp_get_callbacks,
	cpp_set_callbacks): New prototypes.
	* cppmain.c (main): Update for help_only.

Index: cppinit.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppinit.c,v
retrieving revision 1.140
diff -u -p -r1.140 cppinit.c
--- cppinit.c	2001/01/11 22:20:51	1.140
+++ cppinit.c	2001/01/13 00:46:34
@@ -1290,18 +1290,18 @@ cpp_handle_option (pfile, argc, argv)
 	case OPT_h:
 	case OPT__help:
 	  print_help ();
-	  pfile->help_only = 1;
+	  CPP_OPTION (pfile, help_only) = 1;
 	  break;
 	case OPT_target__help:
           /* Print if any target specific options. cpplib has none, but
 	     make sure help_only gets set.  */
-	  pfile->help_only = 1;
+	  CPP_OPTION (pfile, help_only) = 1;
           break;
 
 	  /* --version inhibits compilation, -version doesn't. -v means
 	     verbose and -version.  Historical reasons, don't ask.  */
 	case OPT__version:
-	  pfile->help_only = 1;
+	  CPP_OPTION (pfile, help_only) = 1;
 	  goto version;
 	case OPT_v:
 	  CPP_OPTION (pfile, verbose) = 1;
Index: cpplib.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpplib.c,v
retrieving revision 1.234
diff -u -p -r1.234 cpplib.c
--- cpplib.c	2000/12/18 19:00:25	1.234
+++ cpplib.c	2001/01/13 00:46:47
@@ -1714,6 +1714,39 @@ handle_assertion (pfile, str, type)
   run_directive (pfile, type, BUF_CL_OPTION, str, count);
 }
 
+/* The number of errors for a given reader.  */
+unsigned int
+cpp_errors (pfile)
+     cpp_reader *pfile;
+{
+  return pfile->errors;
+}
+
+/* The options structure.  */
+cpp_options *
+cpp_get_options (pfile)
+     cpp_reader *pfile;
+{
+  return &pfile->opts;
+}
+
+/* The callbacks structure.  */
+cpp_callbacks *
+cpp_get_callbacks (pfile)
+     cpp_reader *pfile;
+{
+  return &pfile->cb;
+}
+
+/* Copy the given callbacks structure to our own.  */
+void
+cpp_set_callbacks (pfile, cb)
+     cpp_reader *pfile;
+     cpp_callbacks *cb;
+{
+  pfile->cb = *cb;
+}
+
 /* Push a new buffer on the buffer stack.  Returns the new buffer; it
    doesn't fail.  It does not generate a file change call back; that
    is the responsibility of the caller.  */
Index: cpplib.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpplib.h,v
retrieving revision 1.158
diff -u -p -r1.158 cpplib.h
--- cpplib.h	2001/01/10 21:32:14	1.158
+++ cpplib.h	2001/01/13 00:46:47
@@ -42,6 +42,7 @@ typedef struct cpp_pool cpp_pool;
 typedef struct cpp_macro cpp_macro;
 typedef struct cpp_lexer_pos cpp_lexer_pos;
 typedef struct cpp_lookahead cpp_lookahead;
+typedef struct cpp_callbacks cpp_callbacks;
 
 struct directive;		/* These are deliberately incomplete.  */
 struct answer;
@@ -419,6 +420,11 @@ struct cpp_options
 
   /* Treat C++ alternate operator names special.  */
   unsigned char operator_names;
+
+  /* True if --help, --version or --target-help appeared in the
+     options.  Stand-alone CPP should then bail out after option
+     parsing; drivers might want to continue printing help.  */
+  unsigned char help_only;
 };
 
 struct lexer_state
@@ -485,6 +491,19 @@ struct cpp_file_change
   unsigned char externc;	/* Nonzero if wrapper needed.  */
 };
 
+/* Call backs.  */
+struct cpp_callbacks
+{
+    void (*file_change) PARAMS ((cpp_reader *, const cpp_file_change *));
+    void (*include) PARAMS ((cpp_reader *, const unsigned char *,
+			     const cpp_token *));
+    void (*define) PARAMS ((cpp_reader *, cpp_hashnode *));
+    void (*undef) PARAMS ((cpp_reader *, cpp_hashnode *));
+    void (*poison) PARAMS ((cpp_reader *));
+    void (*ident) PARAMS ((cpp_reader *, const cpp_string *));
+    void (*def_pragma) PARAMS ((cpp_reader *));
+};
+
 /* A cpp_reader encapsulates the "state" of a pre-processor run.
    Applying cpp_get_token repeatedly yields a stream of pre-processor
    tokens.  Usually, there is only one cpp_reader object active.  */
@@ -581,16 +600,7 @@ struct cpp_reader
   struct pragma_entry *pragmas;
 
   /* Call backs.  */
-  struct {
-    void (*file_change) PARAMS ((cpp_reader *, const cpp_file_change *));
-    void (*include) PARAMS ((cpp_reader *, const unsigned char *,
-			     const cpp_token *));
-    void (*define) PARAMS ((cpp_reader *, cpp_hashnode *));
-    void (*undef) PARAMS ((cpp_reader *, cpp_hashnode *));
-    void (*poison) PARAMS ((cpp_reader *));
-    void (*ident) PARAMS ((cpp_reader *, const cpp_string *));
-    void (*def_pragma) PARAMS ((cpp_reader *));
-  } cb;
+  struct cpp_callbacks cb;
 
   /* User visible options.  */
   struct cpp_options opts;
@@ -608,15 +618,11 @@ struct cpp_reader
 
   /* True if we are skipping a failed conditional group.  */
   unsigned char skipping;
-
-  /* True if --help appeared in the options.  Caller should then bail
-     out after option parsing and printing its own help.  See cppmain.c.  */
-  unsigned char help_only;
 };
 
 #define CPP_FATAL_LIMIT 1000
 /* True if we have seen a "fatal" error. */
-#define CPP_FATAL_ERRORS(READER) ((READER)->errors >= CPP_FATAL_LIMIT)
+#define CPP_FATAL_ERRORS(PFILE) (cpp_errors (PFILE) >= CPP_FATAL_LIMIT)
 
 #define CPP_OPTION(PFILE, OPTION) ((PFILE)->opts.OPTION)
 #define CPP_BUFFER(PFILE) ((PFILE)->buffer)
@@ -699,6 +705,15 @@ struct cpp_hashnode
 /* Call this first to get a handle to pass to other functions.  */
 extern cpp_reader *cpp_create_reader PARAMS ((enum c_lang));
 
+/* Call these to get pointers to the options and callback structures
+   for a given reader.  These pointers are good until you call
+   cpp_finish on that reader.  You can either edit the callbacks
+   through the pointer returned from cpp_get_callbacks, or set them
+   with cpp_set_callbacks.  */
+extern cpp_options *cpp_get_options PARAMS ((cpp_reader *));
+extern cpp_callbacks *cpp_get_callbacks PARAMS ((cpp_reader *));
+extern void cpp_set_callbacks PARAMS ((cpp_reader *, cpp_callbacks *));
+
 /* Now call cpp_handle_option[s] to handle 1[or more] switches.  The
    return value is the number of arguments used.  If
    cpp_handle_options returns without using all arguments, it couldn't
@@ -709,6 +724,9 @@ extern cpp_reader *cpp_create_reader PAR
 extern int cpp_handle_options PARAMS ((cpp_reader *, int, char **));
 extern int cpp_handle_option PARAMS ((cpp_reader *, int, char **));
 extern void cpp_post_options PARAMS ((cpp_reader *));
+
+/* Error count.  */
+extern unsigned int cpp_errors PARAMS ((cpp_reader *));
 
 extern unsigned int cpp_token_len PARAMS ((const cpp_token *));
 extern unsigned char *cpp_token_as_text PARAMS ((cpp_reader *,
Index: cppmain.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppmain.c,v
retrieving revision 1.53
diff -u -p -r1.53 cppmain.c
--- cppmain.c	2001/01/11 21:30:16	1.53
+++ cppmain.c	2001/01/13 00:46:47
@@ -87,7 +87,7 @@ main (argc, argv)
      line, it will have set pfile->help_only to indicate this.  Exit
      successfully.  [The library does not exit itself, because
      e.g. cc1 needs to print its own --help message at this point.]  */
-  if (pfile->help_only)
+  if (CPP_OPTION (pfile, help_only))
     return (SUCCESS_EXIT_CODE);
 
   /* Open the output now.  We must do so even if no_output is on,

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