[flang-commits] [clang] [flang] [flang][driver] Add pre-processing type to `.i` files (PR #104664)

Kareem Ergawy via flang-commits flang-commits at lists.llvm.org
Fri Aug 16 22:29:00 PDT 2024


https://github.com/ergawy created https://github.com/llvm/llvm-project/pull/104664

This diff allows `.i` files emitted by flang-new to be treated as valid files in the pre-processing phase. This, in turn, allows flang-new to add pre-processing options (e.g. `-I`) when launching compilation jobs for these files.

This solves a bug when using `--save-temps` with source files that include modules from non-standard directories, for example:
```
flang-new -c --save-temps -I/tmp/module_dir -fno-integrated-as \
  /tmp/ModuleUser.f90
```
The problem was that `.i` files were treated as "binary" files and therefore the return value for `types::getPreprocessedType(InputType)` in `Flang::ConstructJob(...)` was `types::TY_INVALID`.

>From b193aec2b608e576fcfabbaa4ba5967a7f8bae9b Mon Sep 17 00:00:00 2001
From: ergawy <kareem.ergawy at amd.com>
Date: Sat, 17 Aug 2024 00:20:11 -0500
Subject: [PATCH] [flang][driver] Add pre-processing type to `.i` files

This diff allows `.i` files emitted by flang-new to be treated as valid
files in the pre-processing phase. This, in turn, allows flang-new to
add pre-processing options (e.g. `-I`) when launching compilation jobs
for these files.

This solves a bug when using `--save-temps` with source files that
include modules from non-standard directories, for example:
```
flang-new -c --save-temps -I/tmp/module_dir -fno-integrated-as \
  /tmp/ModuleUser.f90
```
The problem was that `.i` files were treated as "binary" files and
therefore the return value for `types::getPreprocessedType(InputType)`
in `Flang::ConstructJob(...)` was `types::TY_INVALID`.
---
 clang/include/clang/Driver/Types.def        |  2 +-
 flang/test/Driver/save-temps-use-module.f90 | 23 +++++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/Driver/save-temps-use-module.f90

diff --git a/clang/include/clang/Driver/Types.def b/clang/include/clang/Driver/Types.def
index 0e0cae5fb7068d..b4e9e1f9f3f8b6 100644
--- a/clang/include/clang/Driver/Types.def
+++ b/clang/include/clang/Driver/Types.def
@@ -79,7 +79,7 @@ TYPE("c++-module-cpp-output",    PP_CXXModule, INVALID,         "iim",    phases
 TYPE("ada",                      Ada,          INVALID,         nullptr,  phases::Compile, phases::Backend, phases::Assemble, phases::Link)
 TYPE("assembler",                PP_Asm,       INVALID,         "s",      phases::Assemble, phases::Link)
 TYPE("assembler-with-cpp",       Asm,          PP_Asm,          "S",      phases::Preprocess, phases::Assemble, phases::Link)
-TYPE("f95",                      PP_Fortran,   INVALID,         "i",      phases::Compile, phases::Backend, phases::Assemble, phases::Link)
+TYPE("f95",                      PP_Fortran,   PP_Fortran,      "i",      phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
 TYPE("f95-cpp-input",            Fortran,      PP_Fortran,      nullptr,  phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
 TYPE("java",                     Java,         INVALID,         nullptr,  phases::Compile, phases::Backend, phases::Assemble, phases::Link)
 
diff --git a/flang/test/Driver/save-temps-use-module.f90 b/flang/test/Driver/save-temps-use-module.f90
new file mode 100644
index 00000000000000..ad191ab631e9bb
--- /dev/null
+++ b/flang/test/Driver/save-temps-use-module.f90
@@ -0,0 +1,23 @@
+! Tests that `--save-temps` works properly when a module from a non standard dir
+! is included with `-I/...`.
+
+! RUN: rm -rf %t && split-file %s %t
+! RUN: mkdir %t/mod_inc_dir
+! RUN: mv %t/somemodule.mod %t/mod_inc_dir
+! RUN: %flang -c --save-temps=obj -I%t/mod_inc_dir -fno-integrated-as \
+! RUN:   %t/ModuleUser.f90 -o %t/ModuleUser.o
+! RUN: ls %t | FileCheck %s
+
+! Verify that the temp file(s) were written to disk.
+! CHECK: ModuleUser.i
+
+!--- somemodule.mod
+!mod$ v1 sum:e9e8fd2bd49e8daa
+module SomeModule
+
+end module SomeModule
+!--- ModuleUser.f90
+
+module User
+  use SomeModule
+end module User



More information about the flang-commits mailing list