[flang-commits] [flang] 6d0fef4 - [flang][driver] Refactor methods for parsing options (nfc)

Andrzej Warzynski via flang-commits flang-commits at lists.llvm.org
Tue Apr 20 07:01:13 PDT 2021


Author: Andrzej Warzynski
Date: 2021-04-20T14:00:45Z
New Revision: 6d0fef48609cbec9579716b77998059501969352

URL: https://github.com/llvm/llvm-project/commit/6d0fef48609cbec9579716b77998059501969352
DIFF: https://github.com/llvm/llvm-project/commit/6d0fef48609cbec9579716b77998059501969352.diff

LOG: [flang][driver] Refactor methods for parsing options (nfc)

This is just a small update that makes sure that errors arising from
parsing command-line options are captured more visibly. Also, all
parsing methods will now consistently return either a bool ("may fail")
or void ("never fails").

An instance of `InputKind` coming from `-x` is added to
`FrontendOptions` rather then being returned from `ParseFrontendArgs`.
It's currently not used, but we will require it shortly. In particular,
once code-generation is available we will use it to differentiate
between LLVM IR and Fortran input. `FrontendOptions` is a very suitable
place to keep it.

This changes don't affect the error reporting in the driver. In this
respect these are non-functional-changes. However, it will simplify
things in the forthcoming patches in which we may need a better error
tracking/recovery mechanism.

Differential Revision: https://reviews.llvm.org/D100556

Added: 
    

Modified: 
    flang/include/flang/Frontend/FrontendOptions.h
    flang/lib/Frontend/CompilerInvocation.cpp

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Frontend/FrontendOptions.h b/flang/include/flang/Frontend/FrontendOptions.h
index 43b38f6db2a5d..24fbdd3aff2a9 100644
--- a/flang/include/flang/Frontend/FrontendOptions.h
+++ b/flang/include/flang/Frontend/FrontendOptions.h
@@ -222,6 +222,10 @@ class FrontendOptions {
   // source file.
   int fixedFormColumns_ = 72;
 
+  /// The input kind, either specified via -x argument or deduced from the input
+  /// file name.
+  InputKind dashX_;
+
   // Language features
   common::LanguageFeatureControl features_;
 

diff  --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index d1203c7912d03..a8879294eebf7 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -99,8 +99,8 @@ static void setUpFrontendBasedOnAction(FrontendOptions &opts) {
     opts.needProvenanceRangeToCharBlockMappings_ = true;
 }
 
-static InputKind ParseFrontendArgs(FrontendOptions &opts,
-    llvm::opt::ArgList &args, clang::DiagnosticsEngine &diags) {
+static void ParseFrontendArgs(FrontendOptions &opts, llvm::opt::ArgList &args,
+    clang::DiagnosticsEngine &diags) {
 
   // By default the frontend driver creates a ParseSyntaxOnly action.
   opts.programAction_ = ParseSyntaxOnly;
@@ -292,8 +292,7 @@ static InputKind ParseFrontendArgs(FrontendOptions &opts,
   }
 
   setUpFrontendBasedOnAction(opts);
-
-  return dashX;
+  opts.dashX_ = dashX;
 }
 
 // Generate the path to look for intrinsic modules
@@ -343,9 +342,10 @@ static void parsePreprocessorArgs(
 }
 
 /// Parses all semantic related arguments and populates the variables
-/// options accordingly.
-static void parseSemaArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
+/// options accordingly. Returns false if new errors are generated.
+static bool parseSemaArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
     clang::DiagnosticsEngine &diags) {
+  unsigned numErrorsBefore = diags.getNumErrors();
 
   // -J/module-dir option
   auto moduleDirList =
@@ -365,12 +365,16 @@ static void parseSemaArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
   if (args.hasArg(clang::driver::options::OPT_fdebug_module_writer)) {
     res.SetDebugModuleDir(true);
   }
+
+  return diags.getNumErrors() == numErrorsBefore;
 }
 
 /// Parses all diagnostics related arguments and populates the variables
-/// options accordingly.
-static void parseDiagArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
+/// options accordingly. Returns false if new errors are generated.
+static bool parseDiagArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
     clang::DiagnosticsEngine &diags) {
+  unsigned numErrorsBefore = diags.getNumErrors();
+
   // -Werror option
   // TODO: Currently throws a Diagnostic for anything other than -W<error>,
   // this has to change when other -W<opt>'s are supported.
@@ -385,12 +389,15 @@ static void parseDiagArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
       diags.Report(diagID);
     }
   }
+
+  return diags.getNumErrors() == numErrorsBefore;
 }
 
 /// Parses all Dialect related arguments and populates the variables
-/// options accordingly.
-static void parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
+/// options accordingly. Returns false if new errors are generated.
+static bool parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
     clang::DiagnosticsEngine &diags) {
+  unsigned numErrorsBefore = diags.getNumErrors();
 
   // -fdefault* family
   if (args.hasArg(clang::driver::options::OPT_fdefault_real_8)) {
@@ -446,7 +453,7 @@ static void parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
       diags.Report(diagID);
     }
   }
-  return;
+  return diags.getNumErrors() == numErrorsBefore;
 }
 
 bool CompilerInvocation::CreateFromArgs(CompilerInvocation &res,
@@ -476,14 +483,10 @@ bool CompilerInvocation::CreateFromArgs(CompilerInvocation &res,
 
   // Parse the frontend args
   ParseFrontendArgs(res.frontendOpts(), args, diags);
-  // Parse the preprocessor args
   parsePreprocessorArgs(res.preprocessorOpts(), args);
-  // Parse semantic args
-  parseSemaArgs(res, args, diags);
-  // Parse dialect arguments
-  parseDialectArgs(res, args, diags);
-  // Parse diagnostic arguments
-  parseDiagArgs(res, args, diags);
+  success &= parseSemaArgs(res, args, diags);
+  success &= parseDialectArgs(res, args, diags);
+  success &= parseDiagArgs(res, args, diags);
 
   return success;
 }


        


More information about the flang-commits mailing list