]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c-family: add -fsearch-include-path
authorJason Merrill <jason@redhat.com>
Fri, 17 May 2024 01:43:20 +0000 (21:43 -0400)
committerJason Merrill <jason@redhat.com>
Sun, 17 Nov 2024 15:23:21 +0000 (16:23 +0100)
The C++ modules code has a -fmodule-header (or -x c++-{user,system}-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 I proposed to live on the include path at
bits/std.cc.  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.

The libcpp change is to allow -fsearch-include-path{,=user} to find files in
the current working directory, like -include.  This can be handy for a quick
compile of both std.cc and a file that imports it, e.g.

g++ -std=c++20 -fmodules -fsearch-include-path bits/std.cc importer.cc

gcc/ChangeLog:

* doc/cppopts.texi: Document -fsearch-include-path.
* doc/invoke.texi: Mention it for modules.

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.

libcpp/ChangeLog:

* internal.h (search_path_head): Declare.
* files.cc (search_path_head): No longer static.
* init.cc (cpp_read_main_file): Use it.

gcc/c-family/c-opts.cc
gcc/c-family/c.opt
gcc/cp/module.cc
gcc/doc/cppopts.texi
gcc/doc/invoke.texi
libcpp/files.cc
libcpp/init.cc
libcpp/internal.h

index 3f2cabf9448f8e631bc57c36927229b9e60f4c41..f66a0a0ca8884858131d995d211a65ccc7dc7122 100644 (file)
@@ -770,6 +770,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;
index 61cfe33c251279577f181264d0031ab65500886f..8224c82bfdf80a7edac74fdd27201c28a233be17 100644 (file)
@@ -2280,6 +2280,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.
index fe9cdd9bc242790e22645b73c2d73eb06288e26a..27eb39b48fa3bca09130ec5bc1ed3fcea4e1b535 100644 (file)
@@ -21224,7 +21224,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);
     }
 }
 
index 5b5b0848ae86dbf3645609801cd82bf823a6cd5a..748db5ea579763703027affe86a730332c6b62ae 100644 (file)
@@ -270,6 +270,21 @@ 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@r{[}=@var{kind}@r{]}
+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 -fsearch-include-path bits/stdc++.h bits/std.cc
+@end smallexample
+
+@var{kind} defaults to @samp{user}, which looks on the @code{#include
+"@dots{}"} search path; you can also explicitly specify @samp{system}
+for the @code{#include <@dots{}>} search path.
+
 @opindex ftabstop
 @item -ftabstop=@var{width}
 Set the distance between tab stops.  This helps the preprocessor report
index 929feaf23fbbdad5866ebc62effd69c165e0504c..8aeccb1953a7e2772be90790c1780954bd62ec16 100644 (file)
@@ -38001,6 +38001,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
index c63f5824a53505dabcd4aea9fc1db88ecff07559..840dffccce428abc2aac5cc4de64e996121d9b42 100644 (file)
@@ -189,9 +189,6 @@ static bool read_file_guts (cpp_reader *pfile, _cpp_file *file,
                            location_t loc, const char *input_charset);
 static bool read_file (cpp_reader *pfile, _cpp_file *file,
                       location_t loc);
-static struct cpp_dir *search_path_head (cpp_reader *, const char *fname,
-                                        int angle_brackets, enum include_type,
-                                        bool suppress_diagnostic = false);
 static const char *dir_name_of_file (_cpp_file *file);
 static void open_file_failed (cpp_reader *pfile, _cpp_file *file, int,
                              location_t);
@@ -1081,7 +1078,7 @@ _cpp_mark_file_once_only (cpp_reader *pfile, _cpp_file *file)
 /* Return the directory from which searching for FNAME should start,
    considering the directive TYPE and ANGLE_BRACKETS.  If there is
    nothing left in the path, returns NULL.  */
-static struct cpp_dir *
+struct cpp_dir *
 search_path_head (cpp_reader *pfile, const char *fname, int angle_brackets,
                  enum include_type type, bool suppress_diagnostic)
 {
index 355e5017649e0da58487c714d9a39cf1d978e4a2..3ab120a36e6aacb322fbebbce1f748c01bfbad19 100644 (file)
@@ -744,14 +744,12 @@ cpp_read_main_file (cpp_reader *pfile, const char *fname, bool injecting)
     /* Set the default target (if there is none already).  */
     deps_add_default_target (deps, fname);
 
+  auto main_search = CPP_OPTION (pfile, main_search);
+  bool angle = main_search == CMS_system;
+  cpp_dir *start_dir = (main_search < CMS_user ? &pfile->no_search_path
+                       : search_path_head (pfile, fname, angle, IT_CMDLINE));
   pfile->main_file
-    = _cpp_find_file (pfile, fname,
-                     CPP_OPTION (pfile, preprocessed) ? &pfile->no_search_path
-                     : CPP_OPTION (pfile, main_search) == CMS_user
-                     ? pfile->quote_include
-                     : CPP_OPTION (pfile, main_search) == CMS_system
-                     ? pfile->bracket_include : &pfile->no_search_path,
-                     /*angle=*/0, _cpp_FFK_NORMAL, 0);
+    = _cpp_find_file (pfile, fname, start_dir, angle, _cpp_FFK_NORMAL, 0);
 
   if (_cpp_find_failed (pfile->main_file))
     return NULL;
index e65198e89daa9e4c92f4c06ed82a2154a43e8849..d91acd64ba3917844a6e4e344871ede77ebf0475 100644 (file)
@@ -766,6 +766,8 @@ extern _cpp_file *_cpp_find_file (cpp_reader *, const char *, cpp_dir *,
                                  int angle, _cpp_find_file_kind, location_t);
 extern bool _cpp_find_failed (_cpp_file *);
 extern void _cpp_mark_file_once_only (cpp_reader *, struct _cpp_file *);
+extern cpp_dir *search_path_head (cpp_reader *, const char *, int,
+                                 include_type, bool = false);
 extern const char *_cpp_find_header_unit (cpp_reader *, const char *file,
                                          bool angle_p,  location_t);
 extern int _cpp_stack_embed (cpp_reader *, const char *, bool,