diff mbox series

[RFC,1/4] c-family: add -fsearch-include-path

Message ID 20241009225246.2770766-1-jason@redhat.com
State New
Headers show
Series [RFC,1/4] c-family: add -fsearch-include-path | expand

Commit Message

Jason Merrill Oct. 9, 2024, 10:47 p.m. UTC
Tested x86_64-pc-linux-gnu.  Any thoughts?

-- 8< --

The C++ modules code has a -fmodule-header option to specify looking up
headers to compile to header units on the usual include paths.  I'd like to
have the same functionality for full C++20 modules such as module std, which
could also live on the include path.  But this behavior doesn't seem
necessarily connected to modules, so I'm proposing a general C/C++ option to
specify the behavior of looking in the include path for the input files
specified on the command line.

Other ideas for the name of the option are very welcome.

gcc/ChangeLog:

	* doc/cppopts.texi: Document -fsearch-include-path.

gcc/c-family/ChangeLog:

	* c.opt: Add -fsearch-include-path.
	* c-opts.cc (c_common_post_options): Handle it.

gcc/cp/ChangeLog:

	* module.cc (module_preprocess_options): Don't override it.
---
 gcc/doc/cppopts.texi   | 11 +++++++++++
 gcc/doc/invoke.texi    |  5 +++++
 gcc/c-family/c.opt     |  7 +++++++
 gcc/c-family/c-opts.cc | 13 +++++++++++++
 gcc/cp/module.cc       |  3 ++-
 5 files changed, 38 insertions(+), 1 deletion(-)


base-commit: dcee0b6547211a428b75adb03a461285fed0f20d
diff mbox series

Patch

diff --git a/gcc/doc/cppopts.texi b/gcc/doc/cppopts.texi
index 5b5b0848ae8..e3686b63337 100644
--- a/gcc/doc/cppopts.texi
+++ b/gcc/doc/cppopts.texi
@@ -270,6 +270,17 @@  When preprocessing, do not shorten system header paths with canonicalization.
 @item -fmax-include-depth=@var{depth}
 Set the maximum depth of the nested #include. The default is 200. 
 
+@opindex fsearch-include-path
+@item -fsearch-include-path
+Look for input files on the #include path, not just the current
+directory.  This is particularly useful with C++20 modules, for which
+both header units and module interface units need to be compiled
+directly:
+
+@smallexample
+g++ -c -std=c++20 -fmodules-ts -fsearch-include-path bits/stdc++.h std.cppm
+@end smallexample
+
 @opindex ftabstop
 @item -ftabstop=@var{width}
 Set the distance between tab stops.  This helps the preprocessor report
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index c0c8bf1c29a..c69d032323e 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -38042,6 +38042,11 @@  installed.  Specifying the language as one of these variants also
 inhibits output of the object file, as header files have no associated
 object file.
 
+Alternately, or for a module interface unit in an installed location,
+you can use @option{-fsearch-include-path} to specify that the main
+source file should be found on the include path rather than the
+current directory.
+
 Header units can be used in much the same way as precompiled headers
 (@pxref{Precompiled Headers}), but with fewer restrictions: an
 #include that is translated to a header unit import can appear at any
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 1f2e72a0bb7..0aada1c3080 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -2237,6 +2237,13 @@  frtti
 C++ ObjC++ Optimization Var(flag_rtti) Init(1)
 Generate run time type descriptor information.
 
+fsearch-include-path
+C ObjC C++ ObjC++
+Look for the main source file on the include path.
+
+fsearch-include-path=
+C++ ObjC++ Joined RejectNegative Undocumented
+
 fshort-enums
 C ObjC C++ ObjC++ LTO Optimization Var(flag_short_enums)
 Use the narrowest integer type possible for enumeration types.
diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc
index 510e0870140..2798b4d295f 100644
--- a/gcc/c-family/c-opts.cc
+++ b/gcc/c-family/c-opts.cc
@@ -769,6 +769,19 @@  c_common_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value,
       cpp_opts->traditional = 1;
       break;
 
+    case OPT_fsearch_include_path:
+      cpp_opts->main_search = CMS_user;
+      break;
+
+    case OPT_fsearch_include_path_:
+      if (!strcmp (arg, "user"))
+	cpp_opts->main_search = CMS_user;
+      else if (!strcmp (arg, "system"))
+	cpp_opts->main_search = CMS_system;
+      else
+	error ("invalid argument %qs to %<-fsearch-include-path%>", arg);
+      break;
+
     case OPT_v:
       verbose = true;
       break;
diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index 2dc59ce8a12..e58c057a96e 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -21069,7 +21069,8 @@  module_preprocess_options (cpp_reader *reader)
 	}
       auto *opt = cpp_get_options (reader);
       opt->module_directives = true;
-      opt->main_search = cpp_main_search (flag_header_unit);
+      if (opt->main_search == CMS_none)
+	opt->main_search = cpp_main_search (flag_header_unit);
     }
 }