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][RFC] Immediate compiler exit on missing header file


This patch is an attempt to provide a simple way for gcc to exit immediately
when it runs across a missing header file.  At present, gcc tries to continue
with compilation.  Where a header is missing, the cascade of follow on errors
often obscures the initial cause.

Though the patch works, and is short, I'm not entirely thrilled about it,
and would welcome ideas for improvement or rewrite:

  - Returning only EOF from the lexer on error to unwind the parser stack
    looks a bit flimsy.  Long jump out or exit() both seemed worse, though.
  - The -W... flag does not work with -Wno-error=, because cpplib does not
    use gcc's diagnostic machinery, but rather its own.  I couldn't see a
    ready way round that.  Perhaps just a flag that isn't attached to -W?

Thanks for any ideas or feedback.


gcc/ChangeLog
2009-03-29  Simon Baldwin  <simonb@google.com>

	* c.opt (Wmissing-header-file): New flag.
	* c-opts.c (c_common_handle_option): Handle new flag.
	* doc/invoke.texi (Warning Options): Documented -Wmissing-header-file.

libcpp/ChangeLog
2009-03-29  Simon Baldwin  <simonb@google.com>

	* internal.h (cpp_reader): Add return_only_eof flag.
        * files.c (_cpp_find_file): Set return_only_eof on missing header.
	* include/cpplib.h (cpp_options): Add new structure flag
	eofs_on_missing_include_files.
	* lex.c (_cpp_lex_token): Convert all return tokens to EOF if
	return_only_eof.


Index: gcc/doc/invoke.texi
===================================================================
--- gcc/doc/invoke.texi	(revision 145225)
+++ gcc/doc/invoke.texi	(working copy)
@@ -244,7 +244,7 @@ Objective-C and Objective-C++ Dialects}.
 -Winvalid-pch -Wlarger-than=@var{len}  -Wunsafe-loop-optimizations @gol
 -Wlogical-op -Wlong-long @gol
 -Wmain  -Wmissing-braces  -Wmissing-field-initializers @gol
--Wmissing-format-attribute  -Wmissing-include-dirs @gol
+-Wmissing-format-attribute  -Wmissing-header-file  -Wmissing-include-dirs @gol
 -Wmissing-noreturn  -Wno-mudflap @gol
 -Wno-multichar  -Wnonnull  -Wno-overflow @gol
 -Woverlength-strings  -Wpacked  -Wpacked-bitfield-compat  -Wpadded @gol
@@ -3010,6 +3010,11 @@ int b[2][2] = @{ @{ 0, 1 @}, @{ 2, 3 @} 
 
 This warning is enabled by @option{-Wall}.
 
+@item -Wmissing-header-file @r{(C, C++, Objective-C and Objective-C++ only)}
+@opindex Wmissing-header-file
+@opindex Wno-missing-header-file
+Stop compilation with an error on missing include file.
+
 @item -Wmissing-include-dirs @r{(C, C++, Objective-C and Objective-C++ only)}
 @opindex Wmissing-include-dirs
 @opindex Wno-missing-include-dirs
Index: gcc/c.opt
===================================================================
--- gcc/c.opt	(revision 145225)
+++ gcc/c.opt	(working copy)
@@ -308,6 +308,10 @@ Wmissing-format-attribute
 C ObjC C++ ObjC++ Var(warn_missing_format_attribute) Warning
 Warn about functions which might be candidates for format attributes
 
+Wmissing-header-file
+C ObjC C++ ObjC++ Warning
+Stop compilation with an error on missing include file
+
 Wmissing-include-dirs
 C ObjC C++ ObjC++ Warning
 Warn about user-specified include directories that do not exist
Index: gcc/c-opts.c
===================================================================
--- gcc/c-opts.c	(revision 145225)
+++ gcc/c-opts.c	(working copy)
@@ -454,6 +454,10 @@ c_common_handle_option (size_t scode, co
       enable_warning_as_error ("implicit-function-declaration", value, CL_C | CL_ObjC); 
       break;
 
+    case OPT_Wmissing_header_file:
+      cpp_opts->eofs_on_missing_include_files = value;
+      break;
+
     case OPT_Wformat:
       set_Wformat (value);
       break;
Index: libcpp/files.c
===================================================================
--- libcpp/files.c	(revision 145225)
+++ libcpp/files.c	(working copy)
@@ -497,6 +497,10 @@ _cpp_find_file (cpp_reader *pfile, const
 		cpp_error (pfile, CPP_DL_ERROR,
 			   "use -Winvalid-pch for more information");
 	    }
+
+	  /* If requested, only return a stream of EOFs from now on.  */
+	  if (CPP_OPTION (pfile, eofs_on_missing_include_files))
+	    pfile->return_only_eof = 1;
 	  break;
 	}
 
Index: libcpp/include/cpplib.h
===================================================================
--- libcpp/include/cpplib.h	(revision 145225)
+++ libcpp/include/cpplib.h	(working copy)
@@ -321,6 +321,10 @@ struct cpp_options
   /* Nonzero means warn if slash-star appears in a comment.  */
   unsigned char warn_comments;
 
+  /* Nonzero means return a stream of eof's if an include file does not
+     exist.  */
+  unsigned char eofs_on_missing_include_files;
+
   /* Nonzero means warn if a user-supplied include directory does not
      exist.  */
   unsigned char warn_missing_include_dirs;
Index: libcpp/internal.h
===================================================================
--- libcpp/internal.h	(revision 145225)
+++ libcpp/internal.h	(working copy)
@@ -391,6 +391,10 @@ struct cpp_reader
   /* Error counter for exit code.  */
   unsigned int errors;
 
+  /* Flag to force only EOF token returns, used to coerce the lexer into
+     a hard error state.  */
+  unsigned int return_only_eof : 1;
+
   /* Buffer to hold macro definition string.  */
   unsigned char *macro_buffer;
   unsigned int macro_buffer_len;
Index: libcpp/lex.c
===================================================================
--- libcpp/lex.c	(revision 145225)
+++ libcpp/lex.c	(working copy)
@@ -944,6 +944,10 @@ _cpp_lex_token (cpp_reader *pfile)
 	break;
     }
 
+  /* Convert all returned tokens to EOF if failing on error.  */
+  if (pfile->return_only_eof)
+    result->type = CPP_EOF;
+
   return result;
 }
 


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