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]

New warning: -Wmissing-include-dirs


A number of people have asked about such a warning, so I have
implemented it.  Users are frequently bitten by -I switches that refer
to non-existent directories, leading to incorrect header files (of the
same name) being picked up from later in the include path.  This is
best used in conjunction with -Werror to halt a built.

I've included documentation and a test case.  Okay for mainline?

2004-05-13  Ben Elliston  <bje@au.ibm.com>

	* c.opt (Wmissing-include-dirs): New.
	* c-opts.c (c_common_handle_option): Pass true for user_supplied_p
	to add_path () for -I, but false for OPT_idirafter, OPT_iquote and
	OPT_isystem. Handle case OPT_Wmissing_include_dirs.
	* c-incpath.h (add_path): Add fourth (bool) argument.
	* c-incpath.c (add_env_var_paths): Pass false to add_path ().
	(add_standard_paths): Likewise.
	(remove_duplicates) [REASON_NOENT]: Warn if -Wmissing-include-dirs
	is used and the directory was user-supplied via -I.
	(add_path): Set p->user_supplied_p.  Remove duplicated code by
	using add_cpp_dir_path ().
	* cpplib.h (struct cpp_options): Add warn_missing_include_dirs.
	(struct cpp_dir): Add user_supplied_p.
	* doc/invoke.texi (Warning Options): Document new option.

Index: c-incpath.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-incpath.c,v
retrieving revision 1.13
diff -u -p -r1.13 c-incpath.c
--- c-incpath.c	5 May 2004 12:20:18 -0000	1.13
+++ c-incpath.c	14 May 2004 01:38:42 -0000
@@ -114,7 +114,7 @@ add_env_var_paths (const char *env_var, 
 	  path[q - p] = '\0';
 	}
 
-      add_path (path, chain, chain == SYSTEM);
+      add_path (path, chain, chain == SYSTEM, false);
     }
 }
 
@@ -142,7 +142,7 @@ add_standard_paths (const char *sysroot,
 	      if (!strncmp (p->fname, cpp_GCC_INCLUDE_DIR, len))
 		{
 		  char *str = concat (iprefix, p->fname + len, NULL);
-		  add_path (str, SYSTEM, p->cxx_aware);
+		  add_path (str, SYSTEM, p->cxx_aware, false);
 		}
 	    }
 	}
@@ -160,7 +160,7 @@ add_standard_paths (const char *sysroot,
 	  else
 	    str = update_path (p->fname, p->component);
 
-	  add_path (str, SYSTEM, p->cxx_aware);
+	  add_path (str, SYSTEM, p->cxx_aware, false);
 	}
     }
 }
@@ -192,7 +192,13 @@ remove_duplicates (cpp_reader *pfile, st
 	  if (errno != ENOENT)
 	    cpp_errno (pfile, CPP_DL_ERROR, cur->name);
 	  else
-	    reason = REASON_NOENT;
+	    {
+	      /* If -Wmissing-include-dirs is given, warn. */
+	      cpp_options *opts = cpp_get_options (pfile);
+	      if (opts->warn_missing_include_dirs && cur->user_supplied_p)
+		cpp_errno (pfile, CPP_DL_WARNING, cur->name);
+	      reason = REASON_NOENT;
+	    }
 	}
       else if (!S_ISDIR (st.st_mode))
 	cpp_error_with_line (pfile, CPP_DL_ERROR, 0, 0,
@@ -317,7 +323,7 @@ add_cpp_dir_path (cpp_dir *p, int chain)
 /* Add PATH to the include chain CHAIN. PATH must be malloc-ed and
    NUL-terminated.  */
 void
-add_path (char *path, int chain, int cxx_aware)
+add_path (char *path, int chain, int cxx_aware, bool user_supplied_p)
 {
   cpp_dir *p;
 
@@ -329,12 +335,9 @@ add_path (char *path, int chain, int cxx
   else
     p->sysp = 0;
   p->construct = 0;
+  p->user_supplied_p = user_supplied_p;
 
-  if (tails[chain])
-    tails[chain]->next = p;
-  else
-    heads[chain] = p;
-  tails[chain] = p;
+  add_cpp_dir_path (p, chain);
 }
 
 /* Exported function to handle include chain merging, duplicate
Index: c-incpath.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-incpath.h,v
retrieving revision 1.5
diff -u -p -r1.5 c-incpath.h
--- c-incpath.h	15 Mar 2004 18:20:39 -0000	1.5
+++ c-incpath.h	14 May 2004 01:38:42 -0000
@@ -16,7 +16,7 @@ along with this program; if not, write t
 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 extern void split_quote_chain (void);
-extern void add_path (char *, int, int);
+extern void add_path (char *, int, int, bool);
 extern void register_include_chains (cpp_reader *, const char *,
 				     const char *, int, int, int);
 extern void add_cpp_dir_path (struct cpp_dir *, int);
Index: c-opts.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-opts.c,v
retrieving revision 1.113
diff -u -p -r1.113 c-opts.c
--- c-opts.c	13 May 2004 06:39:29 -0000	1.113
+++ c-opts.c	14 May 2004 01:38:43 -0000
@@ -297,7 +297,7 @@ c_common_handle_option (size_t scode, co
 
     case OPT_I:
       if (strcmp (arg, "-"))
-	add_path (xstrdup (arg), BRACKET, 0);
+	add_path (xstrdup (arg), BRACKET, 0, true);
       else
 	{
 	  if (quote_chain_split)
@@ -541,6 +541,10 @@ c_common_handle_option (size_t scode, co
       warn_missing_format_attribute = value;
       break;
 
+    case OPT_Wmissing_include_dirs:
+      cpp_opts->warn_missing_include_dirs = value;
+      break;
+
     case OPT_Wmissing_prototypes:
       warn_missing_prototypes = value;
       break;
@@ -939,7 +943,7 @@ c_common_handle_option (size_t scode, co
       break;
 
     case OPT_idirafter:
-      add_path (xstrdup (arg), AFTER, 0);
+      add_path (xstrdup (arg), AFTER, 0, true);
       break;
 
     case OPT_imacros:
@@ -952,7 +956,7 @@ c_common_handle_option (size_t scode, co
       break;
 
     case OPT_iquote:
-      add_path (xstrdup (arg), QUOTE, 0);
+      add_path (xstrdup (arg), QUOTE, 0, true);
       break;
 
     case OPT_isysroot:
@@ -960,7 +964,7 @@ c_common_handle_option (size_t scode, co
       break;
 
     case OPT_isystem:
-      add_path (xstrdup (arg), SYSTEM, 0);
+      add_path (xstrdup (arg), SYSTEM, 0, true);
       break;
 
     case OPT_iwithprefix:
@@ -1390,7 +1394,7 @@ add_prefixed_path (const char *suffix, s
   memcpy (path + prefix_len, suffix, suffix_len);
   path[prefix_len + suffix_len] = '\0';
 
-  add_path (path, chain, 0);
+  add_path (path, chain, 0, false);
 }
 
 /* Handle -D, -U, -A, -imacros, and the first -include.  */
Index: c.opt
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c.opt,v
retrieving revision 1.24
diff -u -p -r1.24 c.opt
--- c.opt	13 May 2004 06:39:30 -0000	1.24
+++ c.opt	14 May 2004 01:38:43 -0000
@@ -289,6 +289,10 @@ Wmissing-format-attribute
 C ObjC C++ ObjC++
 Warn about functions which might be candidates for format attributes
 
+Wmissing-include-dirs
+C ObjC C++ ObjC++
+Warn about user-specified include directories that do not exist
+
 Wmissing-prototypes
 C ObjC
 Warn about global functions without prototypes
Index: cpplib.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpplib.h,v
retrieving revision 1.281
diff -u -p -r1.281 cpplib.h
--- cpplib.h	5 May 2004 18:25:52 -0000	1.281
+++ cpplib.h	14 May 2004 01:38:43 -0000
@@ -264,6 +264,10 @@ struct cpp_options
   /* Nonzero means warn if slash-star appears in a comment.  */
   unsigned char warn_comments;
 
+  /* Nonzero means warn if a user-supplied include directory does not
+     exist.  */
+  unsigned char warn_missing_include_dirs;
+
   /* Nonzero means warn if there are any trigraphs.  */
   unsigned char warn_trigraphs;
 
@@ -439,6 +443,9 @@ struct cpp_dir
      directories in the search path.  */
   ino_t ino;
   dev_t dev;
+
+  /* Is this a user-supplied directory? */
+  bool user_supplied_p;
 };
 
 /* Name under which this program was invoked.  */
Index: doc/invoke.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/invoke.texi,v
retrieving revision 1.460
diff -u -p -r1.460 invoke.texi
--- doc/invoke.texi	13 May 2004 06:40:25 -0000	1.460
+++ doc/invoke.texi	14 May 2004 01:38:48 -0000
@@ -222,7 +222,8 @@ in the following sections.
 -Wno-invalid-offsetof  -Winvalid-pch @gol
 -Wlarger-than-@var{len}  -Wlong-long @gol
 -Wmain  -Wmissing-braces @gol
--Wmissing-format-attribute  -Wmissing-noreturn @gol
+-Wmissing-format-attribute  -Wmissing-include-dirs @gol
+-Wmissing-noreturn @gol
 -Wno-multichar  -Wnonnull  -Wpacked  -Wpadded @gol
 -Wparentheses  -Wpointer-arith  -Wredundant-decls @gol
 -Wreturn-type  -Wsequence-point  -Wshadow @gol
@@ -2229,6 +2230,10 @@ bracketed, but that for @samp{b} is full
 int a[2][2] = @{ 0, 1, 2, 3 @};
 int b[2][2] = @{ @{ 0, 1 @}, @{ 2, 3 @} @};
 @end smallexample
+
+@item -Wmissing-include-dirs @r{(C, C++, and Objective-C only)}
+@opindex Wmissing-include-dirs
+Warn if a user-supplied include directory does not exist.
 
 @item -Wparentheses
 @opindex Wparentheses

[testsuite]

2004-05-13  Ben Elliston  <bje@au.ibm.com>

	* gcc.dg/cpp/Wmissingdirs.c: New.

Index: testsuite/gcc.dg/cpp/Wmissingdirs.c
===================================================================
RCS file: testsuite/gcc.dg/cpp/Wmissingdirs.c
diff -N testsuite/gcc.dg/cpp/Wmissingdirs.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ testsuite/gcc.dg/cpp/Wmissingdirs.c	14 May 2004 01:38:58 -0000
@@ -0,0 +1,7 @@
+/* { dg-do preprocess } */
+/* { dg-options "-std=gnu99 -I /jolly/well/better/not/exist -Wmissing-include-dirs -fno-show-column" } */
+
+/* Test that -Wmissing-include-dirs issues a warning when a specified
+   directory does not exist.  Source Ben Elliston, 2004-05-13.  */
+
+/* { dg-warning "No such file or directory" "-Wmissing-include-dirs" { target *-*-* } 0 } */


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