skatrak created this revision.
skatrak added reviewers: dpalermo, jsjodin, domada, raghavendhra.
Herald added a reviewer: sscalpone.
Herald added a reviewer: awarzynski.
Herald added a subscriber: sunshaoce.
Herald added projects: Flang, All.
skatrak requested review of this revision.
Herald added subscribers: cfe-commits, jdoerfert.
Herald added a project: clang.

This patch adds support for the -include flag to include header files before
parsing the input. It should implement the same behavior as the flag with the
same name in clang.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D143493

Files:
  clang/include/clang/Driver/Options.td
  flang/include/flang/Frontend/PreprocessorOptions.h
  flang/include/flang/Parser/parsing.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Parser/parsing.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/include-file.f90

Index: flang/test/Driver/include-file.f90
===================================================================
--- /dev/null
+++ flang/test/Driver/include-file.f90
@@ -0,0 +1,13 @@
+! Ensure argument -include works as expected with an included header file.
+
+! RUN: %flang_fc1 -E -include "%S/Inputs/basic-header-one.h" %s  2>&1 | FileCheck %s --check-prefix=INCLUDED
+! RUN: not %flang_fc1 -E -include does-not-exist.h %s  2>&1 | FileCheck %s --check-prefix=MISSING
+
+! INCLUDED: program MainDirectoryOne
+! INCLUDED-NOT: program X
+
+! MISSING: error: Source file 'does-not-exist.h' was not found
+! MISSING-NOT: program
+
+program X
+end
Index: flang/test/Driver/driver-help.f90
===================================================================
--- flang/test/Driver/driver-help.f90
+++ flang/test/Driver/driver-help.f90
@@ -144,6 +144,7 @@
 ! HELP-FC1-NEXT: -fsyntax-only          Run the preprocessor, parser and semantic analysis stages
 ! HELP-FC1-NEXT: -fxor-operator         Enable .XOR. as a synonym of .NEQV.
 ! HELP-FC1-NEXT: -help                  Display available options
+! HELP-FC1-NEXT: -include <file>        Include file before parsing
 ! HELP-FC1-NEXT: -init-only             Only execute frontend initialization
 ! HELP-FC1-NEXT: -I <dir>               Add directory to the end of the list of include search paths
 ! HELP-FC1-NEXT: -load <dsopath>        Load the named plugin (dynamic shared object)
Index: flang/lib/Parser/parsing.cpp
===================================================================
--- flang/lib/Parser/parsing.cpp
+++ flang/lib/Parser/parsing.cpp
@@ -84,6 +84,24 @@
     prescanner.AddCompilerDirectiveSentinel("$omp");
     prescanner.AddCompilerDirectiveSentinel("$"); // OMP conditional line
   }
+
+  // Prescan -include's
+  for (auto include : options.includes) {
+    const SourceFile *includeFile =
+        allSources.Open(include, fileError, "."s /*prepend to search path*/);
+
+    if (!fileError.str().empty()) {
+      ProvenanceRange range{allSources.AddCompilerInsertion(include)};
+      messages_.Say(range, "%s"_err_en_US, fileError.str());
+      return sourceFile;
+    }
+    CHECK(includeFile);
+
+    ProvenanceRange range{
+        allSources.AddIncludedFile(*includeFile, ProvenanceRange{})};
+    prescanner.Prescan(range);
+  }
+
   ProvenanceRange range{allSources.AddIncludedFile(
       *sourceFile, ProvenanceRange{}, options.isModuleFile)};
   prescanner.Prescan(range);
Index: flang/lib/Frontend/CompilerInvocation.cpp
===================================================================
--- flang/lib/Frontend/CompilerInvocation.cpp
+++ flang/lib/Frontend/CompilerInvocation.cpp
@@ -543,6 +543,11 @@
        args.filtered(clang::driver::options::OPT_fintrinsic_modules_path))
     opts.searchDirectoriesFromIntrModPath.emplace_back(currentArg->getValue());
 
+  // Add the ordered list of -include's
+  for (const auto *currentArg :
+       args.filtered(clang::driver::options::OPT_include))
+    opts.includes.emplace_back(currentArg->getValue());
+
   // -cpp/-nocpp
   if (const auto *currentArg = args.getLastArg(
           clang::driver::options::OPT_cpp, clang::driver::options::OPT_nocpp))
@@ -914,6 +919,11 @@
       preprocessorOptions.searchDirectoriesFromIntrModPath.begin(),
       preprocessorOptions.searchDirectoriesFromIntrModPath.end());
 
+  // Adding includes specified by -include
+  fortranOptions.includes.insert(fortranOptions.includes.end(),
+                                 preprocessorOptions.includes.begin(),
+                                 preprocessorOptions.includes.end());
+
   //  Add the default intrinsic module directory
   fortranOptions.intrinsicModuleDirectories.emplace_back(getIntrinsicDir());
 
Index: flang/include/flang/Parser/parsing.h
===================================================================
--- flang/include/flang/Parser/parsing.h
+++ flang/include/flang/Parser/parsing.h
@@ -34,6 +34,7 @@
   std::vector<std::string> searchDirectories;
   std::vector<std::string> intrinsicModuleDirectories;
   std::vector<Predefinition> predefinitions;
+  std::vector<std::string> includes;
   bool instrumentedParse{false};
   bool isModuleFile{false};
   bool needProvenanceRangeToCharBlockMappings{false};
Index: flang/include/flang/Frontend/PreprocessorOptions.h
===================================================================
--- flang/include/flang/Frontend/PreprocessorOptions.h
+++ flang/include/flang/Frontend/PreprocessorOptions.h
@@ -47,6 +47,8 @@
   std::vector<std::string> searchDirectoriesFromDashI;
   // Search directories specified by the user with -fintrinsic-modules-path
   std::vector<std::string> searchDirectoriesFromIntrModPath;
+  // Includes specified by the user with -include
+  std::vector<std::string> includes;
 
   PPMacrosFlag macrosFlag = PPMacrosFlag::Unknown;
 
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -3357,7 +3357,7 @@
   MarshallingInfoStringVector<PreprocessorOpts<"MacroIncludes">>;
 def image__base : Separate<["-"], "image_base">;
 def include_ : JoinedOrSeparate<["-", "--"], "include">, Group<clang_i_Group>, EnumName<"include">,
-    MetaVarName<"<file>">, HelpText<"Include file before parsing">, Flags<[CC1Option]>;
+    MetaVarName<"<file>">, HelpText<"Include file before parsing">, Flags<[CC1Option, FC1Option]>;
 def include_pch : Separate<["-"], "include-pch">, Group<clang_i_Group>, Flags<[CC1Option]>,
   HelpText<"Include precompiled header file">, MetaVarName<"<file>">,
   MarshallingInfoString<PreprocessorOpts<"ImplicitPCHInclude">>;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to