[PATCH] D95042: [flang][driver] Move fixed/free from detection out of FrontendAction APIAll Fortran options should be set in CompilerInvocation before any ofFrontendActions is entered - that's the job of the driver. However, thisis a bit tricky with fixed and...

Andrzej Warzynski via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 20 06:16:49 PST 2021


awarzynski created this revision.
awarzynski requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

...free from detection introduced in
https://reviews.llvm.org/D94228.

Basically, fixed-free form detection needs to happen:

- separately for every input file (we might be compiling multiple Fortran files, some in free form, some in fixed form)
- before any frontend action (we need to specify `isFixedForm` in `Fortran::parser::Options` first)

In other words, we need this to happen early (before any
FrontendAction), but not too early (we need to know what the current
input file is). Ideally we would be able to set all Fortran options in
one place, but currently that's not possible.

All changes in this patch are NFCs (hence no new tests). Below is a

- move fixed/free form detection from `FrontendAction::ExecuteAction` to `CompilerInstance::ExecuteAction`
- add a bool flag in `FrontendInputFile` to mark a file as fixed/free form
- updated a few comments


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D95042

Files:
  flang/include/flang/Frontend/FrontendOptions.h
  flang/lib/Frontend/CompilerInstance.cpp
  flang/lib/Frontend/FrontendAction.cpp


Index: flang/lib/Frontend/FrontendAction.cpp
===================================================================
--- flang/lib/Frontend/FrontendAction.cpp
+++ flang/lib/Frontend/FrontendAction.cpp
@@ -53,12 +53,6 @@
 
   Fortran::parser::Options parserOptions =
       this->instance().invocation().fortranOpts();
-  // Set the fixed form flag based on the file extension
-  auto pathDotIndex{currentInputPath.rfind(".")};
-  if (pathDotIndex != std::string::npos) {
-    std::string pathSuffix{currentInputPath.substr(pathDotIndex + 1)};
-    parserOptions.isFixedForm = isFixedFormSuffix(pathSuffix);
-  }
 
   // Prescan. In case of failure, report and return.
   ci.parsing().Prescan(currentInputPath, parserOptions);
Index: flang/lib/Frontend/CompilerInstance.cpp
===================================================================
--- flang/lib/Frontend/CompilerInstance.cpp
+++ flang/lib/Frontend/CompilerInstance.cpp
@@ -138,16 +138,19 @@
 }
 
 bool CompilerInstance::ExecuteAction(FrontendAction &act) {
+  auto &invoc = this->invocation();
+
   // Set some sane defaults for the frontend.
-  // TODO: Instead of defaults we should be setting these options based on the
-  // user input.
-  this->invocation().SetDefaultFortranOpts();
-  // Set the fortran options to user-based input.
-  this->invocation().setFortranOpts();
+  invoc.SetDefaultFortranOpts();
+  // Update the fortran options based on user-based input.
+  invoc.setFortranOpts();
 
-  // Connect Input to a CompileInstance
+  // Run the frontend action `act` for every input file.
   for (const FrontendInputFile &fif : frontendOpts().inputs_) {
     if (act.BeginSourceFile(*this, fif)) {
+      // Switch between fixed and free form format based on the input file
+      // extension.
+      invoc.fortranOpts().isFixedForm = fif.IsFixedForm();
       if (llvm::Error err = act.Execute()) {
         consumeError(std::move(err));
       }
Index: flang/include/flang/Frontend/FrontendOptions.h
===================================================================
--- flang/include/flang/Frontend/FrontendOptions.h
+++ flang/include/flang/Frontend/FrontendOptions.h
@@ -104,10 +104,22 @@
   /// The kind of input, atm it contains language
   InputKind kind_;
 
+  /// Is this input file in fixed-form format? This is simply derived from the
+  /// file extension and should not be altered by consumers. For input from
+  /// stdin this is never modified.
+  bool isFixedForm_ = false;
+
 public:
   FrontendInputFile() = default;
   FrontendInputFile(llvm::StringRef file, InputKind kind)
-      : file_(file.str()), kind_(kind) {}
+      : file_(file.str()), kind_(kind) {
+
+    // Based on the extension, decide whether this is a fixed or free form
+    // file.
+    auto pathDotIndex{file.rfind(".")};
+    std::string pathSuffix{file.substr(pathDotIndex + 1)};
+    isFixedForm_ = isFixedFormSuffix(pathSuffix);
+  }
   FrontendInputFile(const llvm::MemoryBuffer *buffer, InputKind kind)
       : buffer_(buffer), kind_(kind) {}
 
@@ -116,6 +128,7 @@
   bool IsEmpty() const { return file_.empty() && buffer_ == nullptr; }
   bool IsFile() const { return !IsBuffer(); }
   bool IsBuffer() const { return buffer_ != nullptr; }
+  bool IsFixedForm() const { return isFixedForm_; }
 
   llvm::StringRef file() const {
     assert(IsFile());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95042.317858.patch
Type: text/x-patch
Size: 3319 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210120/5e65e680/attachment.bin>


More information about the llvm-commits mailing list