[flang-commits] [clang] [flang] [Flang][Driver] Add support for -w option (PR #90420)

Kiran Chandramohan via flang-commits flang-commits at lists.llvm.org
Sun Apr 28 15:22:30 PDT 2024


https://github.com/kiranchandramohan created https://github.com/llvm/llvm-project/pull/90420

A quick and dirty implementation of the -w option. Filters the warning messages generated by the Frontend during emission.

TODO: Add more tests
TODO: Ignore MLIR, LLVM IR and Driver warnings
TODO: Check whether we can avoid generating rather than filtering the warning options in the frontend
TODO: Check the clang implementation of -w

>From 6a22c2de281bc04c9f71b7fcc8289e4217b70e08 Mon Sep 17 00:00:00 2001
From: Kiran Chandramohan <kiran.chandramohan at arm.com>
Date: Fri, 26 Apr 2024 09:26:11 +0000
Subject: [PATCH] [Flang][Driver] Add support for -w option

A quick and dirty implementation of the -w option. Filters
the warning messages generated by the Frontend during emission.

TODO: Add more tests
TODO: Ignore MLIR, LLVM IR and Driver warnings
TODO: Check whether we can avoid generating rather than filtering
the warning options in the frontend.
---
 clang/include/clang/Driver/Options.td          |  2 +-
 clang/lib/Driver/ToolChains/Flang.cpp          |  2 ++
 flang/include/flang/Frontend/FrontendOptions.h |  6 +++++-
 flang/include/flang/Parser/message.h           |  2 +-
 flang/include/flang/Semantics/semantics.h      |  2 +-
 flang/lib/Frontend/CompilerInvocation.cpp      |  3 +++
 flang/lib/Frontend/FrontendAction.cpp          |  7 +++++--
 flang/lib/Frontend/FrontendActions.cpp         | 11 ++++++++---
 flang/lib/Parser/message.cpp                   |  8 ++++++--
 flang/lib/Semantics/semantics.cpp              |  6 ++++--
 flang/test/Driver/w-option.f90                 | 14 ++++++++++++++
 11 files changed, 50 insertions(+), 13 deletions(-)
 create mode 100644 flang/test/Driver/w-option.f90

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 086aedefc11878..e73aee3f63fa47 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5680,7 +5680,7 @@ def whatsloaded : Flag<["-"], "whatsloaded">;
 def why_load : Flag<["-"], "why_load">;
 def whyload : Flag<["-"], "whyload">, Alias<why_load>;
 def w : Flag<["-"], "w">, HelpText<"Suppress all warnings">,
-  Visibility<[ClangOption, CC1Option]>,
+  Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
   MarshallingInfoFlag<DiagnosticOpts<"IgnoreWarnings">>;
 def x : JoinedOrSeparate<["-"], "x">,
 Flags<[NoXarchOption]>,
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 6d93c1f3d7034a..6d1372152601e5 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -757,6 +757,8 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
   // Add other compile options
   addOtherOptions(Args, CmdArgs);
 
+  Args.AddLastArg(CmdArgs, options::OPT_w);
+
   // Forward flags for OpenMP. We don't do this if the current action is an
   // device offloading action other than OpenMP.
   if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
diff --git a/flang/include/flang/Frontend/FrontendOptions.h b/flang/include/flang/Frontend/FrontendOptions.h
index 06b1318f243b08..acbc55a75f9a11 100644
--- a/flang/include/flang/Frontend/FrontendOptions.h
+++ b/flang/include/flang/Frontend/FrontendOptions.h
@@ -232,7 +232,8 @@ class FrontendInputFile {
 struct FrontendOptions {
   FrontendOptions()
       : showHelp(false), showVersion(false), instrumentedParse(false),
-        showColors(false), needProvenanceRangeToCharBlockMappings(false) {}
+        showColors(false), needProvenanceRangeToCharBlockMappings(false),
+        disableWarnings(false) {}
 
   /// Show the -help text.
   unsigned showHelp : 1;
@@ -251,6 +252,9 @@ struct FrontendOptions {
   /// compilation.
   unsigned needProvenanceRangeToCharBlockMappings : 1;
 
+  /// Disable warnings emitted by the Frontend
+  unsigned disableWarnings : 1;
+
   /// Input values from `-fget-definition`
   struct GetDefinitionVals {
     unsigned line;
diff --git a/flang/include/flang/Parser/message.h b/flang/include/flang/Parser/message.h
index 668559aeec9478..d1eb6e6f5aa52d 100644
--- a/flang/include/flang/Parser/message.h
+++ b/flang/include/flang/Parser/message.h
@@ -284,7 +284,7 @@ class Messages {
   void Copy(const Messages &);
   void ResolveProvenances(const AllCookedSources &);
   void Emit(llvm::raw_ostream &, const AllCookedSources &,
-      bool echoSourceLines = true) const;
+      bool echoSourceLines = true, bool disableWarnings = false) const;
   void AttachTo(Message &, std::optional<Severity> = std::nullopt);
   bool AnyFatalError() const;
 
diff --git a/flang/include/flang/Semantics/semantics.h b/flang/include/flang/Semantics/semantics.h
index c8ee71945d8bde..456c2928f5b368 100644
--- a/flang/include/flang/Semantics/semantics.h
+++ b/flang/include/flang/Semantics/semantics.h
@@ -312,7 +312,7 @@ class Semantics {
     return context_.FindScope(where);
   }
   bool AnyFatalError() const { return context_.AnyFatalError(); }
-  void EmitMessages(llvm::raw_ostream &) const;
+  void EmitMessages(llvm::raw_ostream &, bool disableWarnings = false) const;
   void DumpSymbols(llvm::raw_ostream &);
   void DumpSymbolsSources(llvm::raw_ostream &) const;
 
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index f1b7b53975398e..4a19592edf6ed8 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -711,6 +711,9 @@ static bool parseFrontendArgs(FrontendOptions &opts, llvm::opt::ArgList &args,
     }
   }
 
+  if (const llvm::opt::Arg *A = args.getLastArg(clang::driver::options::OPT_w))
+    opts.disableWarnings = true;
+
   setUpFrontendBasedOnAction(opts);
   opts.dashX = dashX;
 
diff --git a/flang/lib/Frontend/FrontendAction.cpp b/flang/lib/Frontend/FrontendAction.cpp
index bb1c239540d9f5..5c037e54184ab4 100644
--- a/flang/lib/Frontend/FrontendAction.cpp
+++ b/flang/lib/Frontend/FrontendAction.cpp
@@ -163,8 +163,10 @@ bool FrontendAction::runParse() {
     return false;
   }
 
+  bool disableWarnings = ci.getInvocation().getFrontendOpts().disableWarnings;
   // Report the diagnostics from getParsing
-  ci.getParsing().messages().Emit(llvm::errs(), ci.getAllCookedSources());
+  ci.getParsing().messages().Emit(llvm::errs(), ci.getAllCookedSources(), true,
+                                  disableWarnings);
 
   return true;
 }
@@ -187,8 +189,9 @@ bool FrontendAction::runSemanticChecks() {
     return false;
   }
 
+  bool disableWarnings = ci.getInvocation().getFrontendOpts().disableWarnings;
   // Report the diagnostics from the semantic checks
-  semantics.EmitMessages(ci.getSemaOutputStream());
+  semantics.EmitMessages(ci.getSemaOutputStream(), disableWarnings);
 
   return true;
 }
diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index 531616e7926aca..b5fc3064d28bd1 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -408,8 +408,10 @@ void PrintPreprocessedAction::executeAction() {
         outForPP, !ci.getInvocation().getPreprocessorOpts().noLineDirectives);
   }
 
+  bool disableWarnings = ci.getInvocation().getFrontendOpts().disableWarnings;
   // Print getDiagnostics from the prescanner
-  ci.getParsing().messages().Emit(llvm::errs(), ci.getAllCookedSources());
+  ci.getParsing().messages().Emit(llvm::errs(), ci.getAllCookedSources(), true,
+                                  disableWarnings);
 
   // If a pre-defined output stream exists, dump the preprocessed content there
   if (!ci.isOutputStreamNull()) {
@@ -544,6 +546,7 @@ void DebugDumpParseTreeAction::executeAction() {
 void DebugMeasureParseTreeAction::executeAction() {
   CompilerInstance &ci = this->getInstance();
 
+  bool disableWarnings = ci.getInvocation().getFrontendOpts().disableWarnings;
   // Parse. In case of failure, report and return.
   ci.getParsing().Parse(llvm::outs());
 
@@ -555,12 +558,14 @@ void DebugMeasureParseTreeAction::executeAction() {
     ci.getDiagnostics().Report(diagID) << getCurrentFileOrBufferName();
 
     ci.getParsing().messages().Emit(llvm::errs(),
-                                    this->getInstance().getAllCookedSources());
+                                    this->getInstance().getAllCookedSources(),
+                                    true, disableWarnings);
     return;
   }
 
   // Report the getDiagnostics from parsing
-  ci.getParsing().messages().Emit(llvm::errs(), ci.getAllCookedSources());
+  ci.getParsing().messages().Emit(llvm::errs(), ci.getAllCookedSources(), true,
+                                  disableWarnings);
 
   auto &parseTree{*ci.getParsing().parseTree()};
 
diff --git a/flang/lib/Parser/message.cpp b/flang/lib/Parser/message.cpp
index a56337d65e5f31..a04e0eddfc1923 100644
--- a/flang/lib/Parser/message.cpp
+++ b/flang/lib/Parser/message.cpp
@@ -378,10 +378,14 @@ void Messages::ResolveProvenances(const AllCookedSources &allCooked) {
 }
 
 void Messages::Emit(llvm::raw_ostream &o, const AllCookedSources &allCooked,
-    bool echoSourceLines) const {
+    bool echoSourceLines, bool disableWarnings) const {
   std::vector<const Message *> sorted;
   for (const auto &msg : messages_) {
-    sorted.push_back(&msg);
+    if (disableWarnings && msg.severity() == Severity::Warning) {
+      // Do nothing
+    } else {
+      sorted.push_back(&msg);
+    }
   }
   std::stable_sort(sorted.begin(), sorted.end(),
       [](const Message *x, const Message *y) { return x->SortBefore(*y); });
diff --git a/flang/lib/Semantics/semantics.cpp b/flang/lib/Semantics/semantics.cpp
index e58a8f3b22c06c..ce5703f381cee8 100644
--- a/flang/lib/Semantics/semantics.cpp
+++ b/flang/lib/Semantics/semantics.cpp
@@ -593,8 +593,10 @@ bool Semantics::Perform() {
       ModFileWriter{context_}.WriteAll();
 }
 
-void Semantics::EmitMessages(llvm::raw_ostream &os) const {
-  context_.messages().Emit(os, context_.allCookedSources());
+void Semantics::EmitMessages(
+    llvm::raw_ostream &os, bool disableWarnings) const {
+  context_.messages().Emit(
+      os, context_.allCookedSources(), true, disableWarnings);
 }
 
 void Semantics::DumpSymbols(llvm::raw_ostream &os) {
diff --git a/flang/test/Driver/w-option.f90 b/flang/test/Driver/w-option.f90
new file mode 100644
index 00000000000000..bf024280340e88
--- /dev/null
+++ b/flang/test/Driver/w-option.f90
@@ -0,0 +1,14 @@
+! RUN: %flang -c %s 2>&1 | FileCheck %s
+! RUN: %flang -c -w %s 2>&1 | FileCheck %s -check-prefix=CHECK-W --allow-empty
+! CHECK: warning: Label '40' is in a construct that should not be used as a branch target here
+! CHECK: warning: Label '50' is in a construct that should not be used as a branch target here
+! CHECK-W-NOT: warning
+
+subroutine sub01(n)
+  integer n
+  GOTO (40,50,60) n
+  if (n .eq. 1) then
+40   print *, "xyz"
+50 end if
+60 continue
+end subroutine sub01



More information about the flang-commits mailing list