diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -661,7 +661,7 @@ HelpText<"Restrict all prior -I flags to double-quoted inclusion and " "remove current directory from include path">; def I : JoinedOrSeparate<["-"], "I">, Group, - Flags<[CC1Option,CC1AsOption]>, MetaVarName<"">, + Flags<[CC1Option,CC1AsOption,FlangOption,FC1Option]>, MetaVarName<"">, HelpText<"Add directory to include search path. If there are multiple -I " "options, these directories are searched in the order they are " "given before the standard system directories are searched. " diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp --- a/clang/lib/Driver/ToolChains/Flang.cpp +++ b/clang/lib/Driver/ToolChains/Flang.cpp @@ -22,6 +22,7 @@ void Flang::AddPreprocessingOptions(const ArgList &Args, ArgStringList &CmdArgs) const { Args.AddAllArgs(CmdArgs, {options::OPT_D, options::OPT_U}); + Args.AddAllArgs(CmdArgs, options::OPT_I); } void Flang::ConstructJob(Compilation &C, const JobAction &JA, diff --git a/flang/include/flang/Frontend/PreprocessorOptions.h b/flang/include/flang/Frontend/PreprocessorOptions.h --- a/flang/include/flang/Frontend/PreprocessorOptions.h +++ b/flang/include/flang/Frontend/PreprocessorOptions.h @@ -24,6 +24,8 @@ class PreprocessorOptions { public: std::vector> macros; + // Search directories specified by the user with -I + std::vector searchDirectoriesFromDashI; public: PreprocessorOptions() {} diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp --- a/flang/lib/Frontend/CompilerInvocation.cpp +++ b/flang/lib/Frontend/CompilerInvocation.cpp @@ -171,6 +171,10 @@ opts.addMacroUndef(currentArg->getValue()); } } + + // Add the ordered list of -I's. + for (const auto *currentArg : args.filtered(clang::driver::options::OPT_I)) + opts.searchDirectoriesFromDashI.emplace_back(currentArg->getValue()); } bool CompilerInvocation::CreateFromArgs(CompilerInvocation &res, @@ -256,4 +260,9 @@ const auto &preprocessorOptions = preprocessorOpts(); collectMacroDefinitions(preprocessorOptions, fortranOptions); -} \ No newline at end of file + + for (const auto &searchDirectoryFromI : + preprocessorOptions.searchDirectoriesFromDashI) { + fortranOptions.searchDirectories.emplace_back(searchDirectoryFromI); + } +} diff --git a/flang/test/Flang-Driver/Inputs/included.h b/flang/test/Flang-Driver/Inputs/included.h new file mode 100644 --- /dev/null +++ b/flang/test/Flang-Driver/Inputs/included.h @@ -0,0 +1 @@ +#define X \ No newline at end of file diff --git a/flang/test/Flang-Driver/driver-help-hidden.f90 b/flang/test/Flang-Driver/driver-help-hidden.f90 --- a/flang/test/Flang-Driver/driver-help-hidden.f90 +++ b/flang/test/Flang-Driver/driver-help-hidden.f90 @@ -24,6 +24,7 @@ ! CHECK-NEXT: -fcolor-diagnostics Enable colors in diagnostics ! CHECK-NEXT: -fno-color-diagnostics Disable colors in diagnostics ! CHECK-NEXT: -help Display available options +! CHECK-NEXT: -I Add directory to include search path. If there are multiple -I options, these directories are searched in the order they are given before the standard system directories are searched. If the same directory is in the SYSTEM include search paths, for example if also specified with -isystem, the -I option will be ignored ! CHECK-NEXT: -o Write output to ! CHECK-NEXT: -test-io Run the InputOuputTest action. Use for development and testing only. ! CHECK-NEXT: -U Undefine macro diff --git a/flang/test/Flang-Driver/driver-help.f90 b/flang/test/Flang-Driver/driver-help.f90 --- a/flang/test/Flang-Driver/driver-help.f90 +++ b/flang/test/Flang-Driver/driver-help.f90 @@ -24,6 +24,7 @@ ! HELP-NEXT: -fcolor-diagnostics Enable colors in diagnostics ! HELP-NEXT: -fno-color-diagnostics Disable colors in diagnostics ! HELP-NEXT: -help Display available options +! HELP-NEXT: -I Add directory to include search path. If there are multiple -I options, these directories are searched in the order they are given before the standard system directories are searched. If the same directory is in the SYSTEM include search paths, for example if also specified with -isystem, the -I option will be ignored ! HELP-NEXT: -o Write output to ! HELP-NEXT: -U Undefine macro ! HELP-NEXT: --version Print version information @@ -37,6 +38,7 @@ ! HELP-FC1-NEXT: -D = Define to (or 1 if omitted) ! HELP-FC1-NEXT: -E Only run the preprocessor ! HELP-FC1-NEXT: -help Display available options +! HELP-FC1-NEXT: -I Add directory to include search path. If there are multiple -I options, these directories are searched in the order they are given before the standard system directories are searched. If the same directory is in the SYSTEM include search paths, for example if also specified with -isystem, the -I option will be ignored ! HELP-FC1-NEXT: -o Write output to ! HELP-FC1-NEXT: -U Undefine macro ! HELP-FC1-NEXT: --version Print version information diff --git a/flang/test/Flang-Driver/include_header.f90 b/flang/test/Flang-Driver/include_header.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Flang-Driver/include_header.f90 @@ -0,0 +1,39 @@ +! Ensure argument -I works as expected. + +! REQUIRES: new-flang-driver + +!-------------------------- +! FLANG DRIVER (flang-new) +!-------------------------- +! RUN: %flang-new -E %s 2>&1 | FileCheck %s --check-prefix=UNINCLUDED +! RUN: %flang-new -E -I %S/Inputs %s 2>&1 | FileCheck %s --check-prefix=INCLUDED +! RUN: %flang-new -E -I %S/InvalidFolder %s 2>&1 | FileCheck %s --check-prefix=UNINCLUDED + +!----------------------------------------- +! FRONTEND FLANG DRIVER (flang-new -fc1) +!----------------------------------------- +! RUN: %flang-new -fc1 -E %s 2>&1 | FileCheck %s --check-prefix=UNINCLUDED +! RUN: %flang-new -fc1 -E -I %S/Inputs %s 2>&1 | FileCheck %s --check-prefix=INCLUDED +! RUN: %flang-new -fc1 -E -I %S/InvalidFolder %s 2>&1 | FileCheck %s --check-prefix=UNINCLUDED + +!-------------------------------------------- +! EXPECTED OUTPUT FOR MISSING INCLUDED FILE +!-------------------------------------------- +! UNINCLUDED:program b +! UNINCLUDED-NOT:program a +! UNINCLUDED-NEXT:end + +!------------------------------ +! EXPECTED OUTPUT FOR INCLUDE +!------------------------------ +! INCLUDED:program a +! INCLUDED-NOT:program b +! INCLUDED-NEXT:end + +#include +#ifdef X +program A +#else +program B +#endif +end \ No newline at end of file