This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [Fortran, Patch] Fix CPP <include_path> (PR 37821)
- From: Tobias Burnus <burnus at net-b dot de>
- To: fortran at gcc dot gnu dot org, gcc-patches at gcc dot gnu dot org
- Date: Thu, 16 Oct 2008 20:52:53 +0200
- Subject: Re: [Fortran, Patch] Fix CPP <include_path> (PR 37821)
Hi all,
attached a revised patch as the previous patch broke "include"
("#include" worked) for ".".
The new patch features:
* #include "..." and include '...' test cases
* directories where the source files resist come first
in the include list (both #include "..." and include)
* "." is not added for #include "..." as licpp does so
automatically
* the location of the sourcefile is not added for
#include <...> but -I dir is also used for <...>
(This was the problem reported in the PR.)
* With explicit -cpp the -I dir are only passed once
to f951 (still twice for the implict (.F etc.) cpp
calling).
Bootstrapped and regtested on x86-64-linux
I also did several manual tests (for -cpp the "-v" option
shows the search paths). [I would not mind if someone could
also playround with include (w/ and w/o -cpp) and with
#include "..." and #include <...>. I think everything should
work, but I'm not positive.]
OK for the trunk?
Tobias
PS: I have just started wondering whether libcpp automatically
adds "subdir" to the search path for "gfortran subdir/file.F".
I'm not sure whether I tested this; I think with this
patch the path is added, which might not be needed but it
does not harm (= does what is should and shows no warning
with -v [this is also tested for by the new test case]).
2008-08-16 Tobias Burnus <burnus@net-b.de>
PR fortran/37821
* cpp.c (gfc_cpp_add_include_path): New "quote" argument.
(gfc_cpp_handle_option): Use it.
* cpp.h (gfc_cpp_add_include_path): Update prototype.
* scanner.c (add_path_to_list): Argument to add at head.
(gfc_add_include_path): Add new argument.
(gfc_add_intrinsic_modules_path) Update call.
* gfortran.h (gfc_add_include_path): Update prototype.
* options.c (gfc_post_options,gfc_handle_module_path_options,
gfc_handle_option): Update call.
* lang-spec.h (F951_OPTIONS): Don't insert include path twice.
2008-08-16 Tobias Burnus <burnus@net-b.de>
PR fortran/37821
* gfortran.dg/include_4.f90: New.
* gfortran.dg/include_5.f90: New.
* gfortran.dg/include_4.inc: New.
Index: gcc/fortran/cpp.c
===================================================================
--- gcc/fortran/cpp.c (Revision 141129)
+++ gcc/fortran/cpp.c (Arbeitskopie)
@@ -360,7 +360,7 @@ gfc_cpp_handle_option (size_t scode, con
case OPT_iquote:
case OPT_isystem:
- gfc_cpp_add_include_path (xstrdup(arg), true);
+ gfc_cpp_add_include_path (xstrdup(arg), true, false);
break;
case OPT_nostdinc:
@@ -622,16 +622,20 @@ gfc_cpp_done (void)
cpp_clear_file_cache (cpp_in);
}
-/* PATH must be malloc-ed and NULL-terminated. */
+/* PATH must be malloc-ed and NULL-terminated. If quote is true, the
+ PATH is only available for "..." and not for <...>. */
void
-gfc_cpp_add_include_path (char *path, bool user_supplied)
+gfc_cpp_add_include_path (char *path, bool user_supplied, bool quote)
{
/* CHAIN sets cpp_dir->sysp which differs from 0 if PATH is a system
include path. Fortran does not define any system include paths. */
- int chain = 0;
int cxx_aware = 0;
- add_path (path, chain, cxx_aware, user_supplied);
+ /* "." is already taken care of by libcpp. */
+ if (quote && path[0] == '.' && path[1] == '\0')
+ return;
+
+ add_path (path, quote ? QUOTE : BRACKET, cxx_aware, user_supplied);
}
void
Index: gcc/fortran/gfortran.h
===================================================================
--- gcc/fortran/gfortran.h (Revision 141129)
+++ gcc/fortran/gfortran.h (Arbeitskopie)
@@ -2091,7 +2091,7 @@ bool gfc_in_match_data (void);
void gfc_scanner_done_1 (void);
void gfc_scanner_init_1 (void);
-void gfc_add_include_path (const char *, bool);
+void gfc_add_include_path (const char *, bool, bool);
void gfc_add_intrinsic_modules_path (const char *);
void gfc_release_include_path (void);
FILE *gfc_open_included_file (const char *, bool, bool);
Index: gcc/fortran/cpp.h
===================================================================
--- gcc/fortran/cpp.h (Revision 141129)
+++ gcc/fortran/cpp.h (Arbeitskopie)
@@ -40,7 +40,7 @@ gfc_try gfc_cpp_preprocess (const char *
void gfc_cpp_done (void);
-void gfc_cpp_add_include_path (char *path, bool user_supplied);
+void gfc_cpp_add_include_path (char *path, bool user_supplied, bool quote);
void gfc_cpp_register_include_paths (void);
Index: gcc/fortran/scanner.c
===================================================================
--- gcc/fortran/scanner.c (Revision 141129)
+++ gcc/fortran/scanner.c (Arbeitskopie)
@@ -307,7 +307,7 @@ gfc_scanner_done_1 (void)
static void
add_path_to_list (gfc_directorylist **list, const char *path,
- bool use_for_modules)
+ bool use_for_modules, bool head)
{
gfc_directorylist *dir;
const char *p;
@@ -317,11 +317,15 @@ add_path_to_list (gfc_directorylist **li
if (*p++ == '\0')
return;
- dir = *list;
- if (!dir)
- dir = *list = XCNEW (gfc_directorylist);
+ if (head || *list == NULL)
+ {
+ dir = XCNEW (gfc_directorylist);
+ if (!head)
+ *list = dir;
+ }
else
{
+ dir = *list;
while (dir->next)
dir = dir->next;
@@ -329,7 +333,9 @@ add_path_to_list (gfc_directorylist **li
dir = dir->next;
}
- dir->next = NULL;
+ dir->next = head ? *list : NULL;
+ if (head)
+ *list = dir;
dir->use_for_modules = use_for_modules;
dir->path = XCNEWVEC (char, strlen (p) + 2);
strcpy (dir->path, p);
@@ -338,17 +344,17 @@ add_path_to_list (gfc_directorylist **li
void
-gfc_add_include_path (const char *path, bool use_for_modules)
+gfc_add_include_path (const char *path, bool use_for_modules, bool file_dir)
{
- add_path_to_list (&include_dirs, path, use_for_modules);
- gfc_cpp_add_include_path (xstrdup(path), true);
+ add_path_to_list (&include_dirs, path, use_for_modules, file_dir);
+ gfc_cpp_add_include_path (xstrdup(path), true, file_dir);
}
void
gfc_add_intrinsic_modules_path (const char *path)
{
- add_path_to_list (&intrinsic_modules_dirs, path, true);
+ add_path_to_list (&intrinsic_modules_dirs, path, true, false);
}
Index: gcc/fortran/lang-specs.h
===================================================================
--- gcc/fortran/lang-specs.h (Revision 141129)
+++ gcc/fortran/lang-specs.h (Arbeitskopie)
@@ -23,7 +23,7 @@
#define F951_CPP_OPTIONS "%{!nocpp: -cpp %g.f90 %(cpp_options)\
%{E|M|MM:%(cpp_debug_options) -fsyntax-only} %{E}}"
-#define F951_OPTIONS "%(cc1_options) %{J*} %{I*}\
+#define F951_OPTIONS "%(cc1_options) %{J*} %{!cpp: %{I*}}\
%{!nostdinc:-fintrinsic-modules-path finclude%s}\
%{!fsyntax-only:%(invoke_as)}"
#define F951_SOURCE_FORM "%{!ffree-form:-ffixed-form}"
Index: gcc/fortran/options.c
===================================================================
--- gcc/fortran/options.c (Revision 141129)
+++ gcc/fortran/options.c (Arbeitskopie)
@@ -264,10 +264,10 @@ gfc_post_options (const char **pfilename
source_path = (char *) alloca (i + 1);
memcpy (source_path, canon_source_file, i);
source_path[i] = 0;
- gfc_add_include_path (source_path, true);
+ gfc_add_include_path (source_path, true, true);
}
else
- gfc_add_include_path (".", true);
+ gfc_add_include_path (".", true, true);
if (canon_source_file != gfc_source_file)
gfc_free (CONST_CAST (char *, canon_source_file));
@@ -406,7 +406,7 @@ gfc_handle_module_path_options (const ch
strcpy (gfc_option.module_dir, arg);
strcat (gfc_option.module_dir, "/");
- gfc_add_include_path (gfc_option.module_dir, true);
+ gfc_add_include_path (gfc_option.module_dir, true, false);
}
@@ -638,7 +638,7 @@ gfc_handle_option (size_t scode, const c
break;
case OPT_fintrinsic_modules_path:
- gfc_add_include_path (arg, false);
+ gfc_add_include_path (arg, false, false);
gfc_add_intrinsic_modules_path (arg);
break;
@@ -739,7 +739,7 @@ gfc_handle_option (size_t scode, const c
break;
case OPT_I:
- gfc_add_include_path (arg, true);
+ gfc_add_include_path (arg, true, false);
break;
case OPT_J:
Index: gcc/testsuite/gfortran.dg/include_4.inc
===================================================================
--- gcc/testsuite/gfortran.dg/include_4.inc (Revision 0)
+++ gcc/testsuite/gfortran.dg/include_4.inc (Revision 0)
@@ -0,0 +1,4 @@
+! Used by include_4.f90 and include_5.f90
+! PR fortran/37821
+!
+integer, parameter :: i4 = 4
Index: gcc/testsuite/gfortran.dg/include_5.f90
===================================================================
--- gcc/testsuite/gfortran.dg/include_5.f90 (Revision 0)
+++ gcc/testsuite/gfortran.dg/include_5.f90 (Revision 0)
@@ -0,0 +1,18 @@
+! { dg-do compile }
+! { dg-options "-cpp" }
+!
+! PR fortran/37821
+!
+! Ensure that for #include "..." and for include the
+! current directory/directory of the source file is
+! included.
+
+subroutine one()
+ include "include_4.inc"
+ integer(i4) :: i
+end subroutine one
+
+subroutine two()
+# include "include_4.inc"
+ integer(i4) :: i
+end subroutine two
Index: gcc/testsuite/gfortran.dg/include_4.f90
===================================================================
--- gcc/testsuite/gfortran.dg/include_4.f90 (Revision 0)
+++ gcc/testsuite/gfortran.dg/include_4.f90 (Revision 0)
@@ -0,0 +1,12 @@
+! { dg-do compile }
+!
+! PR fortran/37821
+!
+! Ensure that for #include "..." and for include the
+! current directory/directory of the source file is
+! included. See also include_5.f90
+
+subroutine one()
+ include "include_4.inc"
+ integer(i4) :: i
+end subroutine one