Skip to content

Commit 6a22c2d

Browse files
[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.
1 parent aa596fa commit 6a22c2d

File tree

11 files changed

+50
-13
lines changed

11 files changed

+50
-13
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5680,7 +5680,7 @@ def whatsloaded : Flag<["-"], "whatsloaded">;
56805680
def why_load : Flag<["-"], "why_load">;
56815681
def whyload : Flag<["-"], "whyload">, Alias<why_load>;
56825682
def w : Flag<["-"], "w">, HelpText<"Suppress all warnings">,
5683-
Visibility<[ClangOption, CC1Option]>,
5683+
Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
56845684
MarshallingInfoFlag<DiagnosticOpts<"IgnoreWarnings">>;
56855685
def x : JoinedOrSeparate<["-"], "x">,
56865686
Flags<[NoXarchOption]>,

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,8 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
757757
// Add other compile options
758758
addOtherOptions(Args, CmdArgs);
759759

760+
Args.AddLastArg(CmdArgs, options::OPT_w);
761+
760762
// Forward flags for OpenMP. We don't do this if the current action is an
761763
// device offloading action other than OpenMP.
762764
if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,

flang/include/flang/Frontend/FrontendOptions.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ class FrontendInputFile {
232232
struct FrontendOptions {
233233
FrontendOptions()
234234
: showHelp(false), showVersion(false), instrumentedParse(false),
235-
showColors(false), needProvenanceRangeToCharBlockMappings(false) {}
235+
showColors(false), needProvenanceRangeToCharBlockMappings(false),
236+
disableWarnings(false) {}
236237

237238
/// Show the -help text.
238239
unsigned showHelp : 1;
@@ -251,6 +252,9 @@ struct FrontendOptions {
251252
/// compilation.
252253
unsigned needProvenanceRangeToCharBlockMappings : 1;
253254

255+
/// Disable warnings emitted by the Frontend
256+
unsigned disableWarnings : 1;
257+
254258
/// Input values from `-fget-definition`
255259
struct GetDefinitionVals {
256260
unsigned line;

flang/include/flang/Parser/message.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class Messages {
284284
void Copy(const Messages &);
285285
void ResolveProvenances(const AllCookedSources &);
286286
void Emit(llvm::raw_ostream &, const AllCookedSources &,
287-
bool echoSourceLines = true) const;
287+
bool echoSourceLines = true, bool disableWarnings = false) const;
288288
void AttachTo(Message &, std::optional<Severity> = std::nullopt);
289289
bool AnyFatalError() const;
290290

flang/include/flang/Semantics/semantics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ class Semantics {
312312
return context_.FindScope(where);
313313
}
314314
bool AnyFatalError() const { return context_.AnyFatalError(); }
315-
void EmitMessages(llvm::raw_ostream &) const;
315+
void EmitMessages(llvm::raw_ostream &, bool disableWarnings = false) const;
316316
void DumpSymbols(llvm::raw_ostream &);
317317
void DumpSymbolsSources(llvm::raw_ostream &) const;
318318

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,9 @@ static bool parseFrontendArgs(FrontendOptions &opts, llvm::opt::ArgList &args,
711711
}
712712
}
713713

714+
if (const llvm::opt::Arg *A = args.getLastArg(clang::driver::options::OPT_w))
715+
opts.disableWarnings = true;
716+
714717
setUpFrontendBasedOnAction(opts);
715718
opts.dashX = dashX;
716719

flang/lib/Frontend/FrontendAction.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,10 @@ bool FrontendAction::runParse() {
163163
return false;
164164
}
165165

166+
bool disableWarnings = ci.getInvocation().getFrontendOpts().disableWarnings;
166167
// Report the diagnostics from getParsing
167-
ci.getParsing().messages().Emit(llvm::errs(), ci.getAllCookedSources());
168+
ci.getParsing().messages().Emit(llvm::errs(), ci.getAllCookedSources(), true,
169+
disableWarnings);
168170

169171
return true;
170172
}
@@ -187,8 +189,9 @@ bool FrontendAction::runSemanticChecks() {
187189
return false;
188190
}
189191

192+
bool disableWarnings = ci.getInvocation().getFrontendOpts().disableWarnings;
190193
// Report the diagnostics from the semantic checks
191-
semantics.EmitMessages(ci.getSemaOutputStream());
194+
semantics.EmitMessages(ci.getSemaOutputStream(), disableWarnings);
192195

193196
return true;
194197
}

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,10 @@ void PrintPreprocessedAction::executeAction() {
408408
outForPP, !ci.getInvocation().getPreprocessorOpts().noLineDirectives);
409409
}
410410

411+
bool disableWarnings = ci.getInvocation().getFrontendOpts().disableWarnings;
411412
// Print getDiagnostics from the prescanner
412-
ci.getParsing().messages().Emit(llvm::errs(), ci.getAllCookedSources());
413+
ci.getParsing().messages().Emit(llvm::errs(), ci.getAllCookedSources(), true,
414+
disableWarnings);
413415

414416
// If a pre-defined output stream exists, dump the preprocessed content there
415417
if (!ci.isOutputStreamNull()) {
@@ -544,6 +546,7 @@ void DebugDumpParseTreeAction::executeAction() {
544546
void DebugMeasureParseTreeAction::executeAction() {
545547
CompilerInstance &ci = this->getInstance();
546548

549+
bool disableWarnings = ci.getInvocation().getFrontendOpts().disableWarnings;
547550
// Parse. In case of failure, report and return.
548551
ci.getParsing().Parse(llvm::outs());
549552

@@ -555,12 +558,14 @@ void DebugMeasureParseTreeAction::executeAction() {
555558
ci.getDiagnostics().Report(diagID) << getCurrentFileOrBufferName();
556559

557560
ci.getParsing().messages().Emit(llvm::errs(),
558-
this->getInstance().getAllCookedSources());
561+
this->getInstance().getAllCookedSources(),
562+
true, disableWarnings);
559563
return;
560564
}
561565

562566
// Report the getDiagnostics from parsing
563-
ci.getParsing().messages().Emit(llvm::errs(), ci.getAllCookedSources());
567+
ci.getParsing().messages().Emit(llvm::errs(), ci.getAllCookedSources(), true,
568+
disableWarnings);
564569

565570
auto &parseTree{*ci.getParsing().parseTree()};
566571

flang/lib/Parser/message.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,14 @@ void Messages::ResolveProvenances(const AllCookedSources &allCooked) {
378378
}
379379

380380
void Messages::Emit(llvm::raw_ostream &o, const AllCookedSources &allCooked,
381-
bool echoSourceLines) const {
381+
bool echoSourceLines, bool disableWarnings) const {
382382
std::vector<const Message *> sorted;
383383
for (const auto &msg : messages_) {
384-
sorted.push_back(&msg);
384+
if (disableWarnings && msg.severity() == Severity::Warning) {
385+
// Do nothing
386+
} else {
387+
sorted.push_back(&msg);
388+
}
385389
}
386390
std::stable_sort(sorted.begin(), sorted.end(),
387391
[](const Message *x, const Message *y) { return x->SortBefore(*y); });

flang/lib/Semantics/semantics.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,10 @@ bool Semantics::Perform() {
593593
ModFileWriter{context_}.WriteAll();
594594
}
595595

596-
void Semantics::EmitMessages(llvm::raw_ostream &os) const {
597-
context_.messages().Emit(os, context_.allCookedSources());
596+
void Semantics::EmitMessages(
597+
llvm::raw_ostream &os, bool disableWarnings) const {
598+
context_.messages().Emit(
599+
os, context_.allCookedSources(), true, disableWarnings);
598600
}
599601

600602
void Semantics::DumpSymbols(llvm::raw_ostream &os) {

flang/test/Driver/w-option.f90

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
! RUN: %flang -c %s 2>&1 | FileCheck %s
2+
! RUN: %flang -c -w %s 2>&1 | FileCheck %s -check-prefix=CHECK-W --allow-empty
3+
! CHECK: warning: Label '40' is in a construct that should not be used as a branch target here
4+
! CHECK: warning: Label '50' is in a construct that should not be used as a branch target here
5+
! CHECK-W-NOT: warning
6+
7+
subroutine sub01(n)
8+
integer n
9+
GOTO (40,50,60) n
10+
if (n .eq. 1) then
11+
40 print *, "xyz"
12+
50 end if
13+
60 continue
14+
end subroutine sub01

0 commit comments

Comments
 (0)