Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Flang] RFC: Add support for -w option 1/n #90420

Merged
merged 3 commits into from
May 8, 2024

Conversation

kiranchandramohan
Copy link
Contributor

@kiranchandramohan kiranchandramohan commented Apr 28, 2024

Add support for the -w option to switch OFF all Flang
warnings. This patch only supports switching OFF the
frontend warnings.

TODO : Support for MLIR, LLVM and Driver warnings.
TODO : Support interactions between -w, -pedantic, -Wall

@kiranchandramohan kiranchandramohan changed the title [Flang][Driver] Add support for -w option [Flang] Add support for -w option Apr 28, 2024
@kiranchandramohan kiranchandramohan changed the title [Flang] Add support for -w option [Flang] RFC: Add support for -w option Apr 29, 2024
@jeffhammond
Copy link
Member

Thanks! I built it already and will test later today.

@klausler
Copy link
Contributor

I am finishing up a patch that ensures that all warning messages are conditional in Semantics.

@klausler
Copy link
Contributor

See #90518, which I think would greatly ease implementation of -w.

@jeffhammond
Copy link
Member

This solves the problem that motivated my initial complaint on Slack.

@jeffhammond jeffhammond marked this pull request as ready for review April 30, 2024 10:53
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' flang:driver flang Flang issues not falling into any other category flang:semantics flang:parser labels Apr 30, 2024
@jeffhammond jeffhammond marked this pull request as draft April 30, 2024 10:53
@llvmbot
Copy link
Collaborator

llvmbot commented Apr 30, 2024

@llvm/pr-subscribers-flang-parser
@llvm/pr-subscribers-flang-semantics
@llvm/pr-subscribers-flang-driver
@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-driver

Author: Kiran Chandramohan (kiranchandramohan)

Changes

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, Driver and pedantic Semantics warnings
TODO: Check whether we can avoid generating rather than filtering the warning options in the frontend
TODO: Check whether a ShouldWarn should precede each warning in the frontend as in the case of portability warnings.
TODO: Check the clang implementation of -w


Full diff: https://github.com/llvm/llvm-project/pull/90420.diff

11 Files Affected:

  • (modified) clang/include/clang/Driver/Options.td (+1-1)
  • (modified) clang/lib/Driver/ToolChains/Flang.cpp (+2)
  • (modified) flang/include/flang/Frontend/FrontendOptions.h (+5-1)
  • (modified) flang/include/flang/Parser/message.h (+1-1)
  • (modified) flang/include/flang/Semantics/semantics.h (+1-1)
  • (modified) flang/lib/Frontend/CompilerInvocation.cpp (+3)
  • (modified) flang/lib/Frontend/FrontendAction.cpp (+5-2)
  • (modified) flang/lib/Frontend/FrontendActions.cpp (+8-3)
  • (modified) flang/lib/Parser/message.cpp (+6-2)
  • (modified) flang/lib/Semantics/semantics.cpp (+4-2)
  • (added) flang/test/Driver/w-option.f90 (+14)
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

@jeffhammond
Copy link
Member

Sorry, I bumped the "Ready for Review" button on accident.

@kiranchandramohan
Copy link
Contributor Author

Thanks @jeffhammond. I will wait for @klausler to submit #90518 which will make it easier for the driver and will provide more fine-grained control of warnings.

@kiranchandramohan kiranchandramohan marked this pull request as ready for review May 6, 2024 21:23
@kiranchandramohan kiranchandramohan changed the title [Flang] RFC: Add support for -w option [Flang] RFC: Add support for -w option 1/n May 6, 2024
Copy link
Contributor

@banach-space banach-space left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Once you have more than one prefix, CHECK becomes noise. At least IMHO 😅 (we all know that these are "check" lines). You could use more descriptive prefixes instead, e.g. CHECK-PORT -> PORTABILITY (it wasn't obvious to me that PORT stood for "portability"), CHECK -> DEFAULT.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Add support for the -w option to switch OFF all Flang
warnings. This patch only supports switching OFF the
frontend warnings.

TODO : Support for MLIR, LLVM and Driver warnings.
TODO : Support interactions between -w, -pedantic, -Wall
@kiranchandramohan kiranchandramohan merged commit 602df27 into llvm:main May 8, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category flang:driver flang:parser flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants