Skip to content

Commit 92d27d8

Browse files
ashermancinellibanach-space
authored andcommitted
[flang] Lift -Werror checks into local functions
Lift checks for -Werror into local functions. Reviewed By: awarzynski Differential Revision: https://reviews.llvm.org/D101261
1 parent 87d1de5 commit 92d27d8

File tree

2 files changed

+33
-46
lines changed

2 files changed

+33
-46
lines changed

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ bool reportFatalSemanticErrors(const Fortran::semantics::Semantics &semantics,
4545
return false;
4646
}
4747

48+
template <unsigned N>
49+
static bool reportFatalErrors(
50+
const FrontendAction *act, const char (&message)[N]) {
51+
CompilerInstance &ci = act->instance();
52+
if (!ci.parsing().messages().empty() &&
53+
(ci.invocation().warnAsErr() ||
54+
ci.parsing().messages().AnyFatalError())) {
55+
const unsigned diagID = ci.diagnostics().getCustomDiagID(
56+
clang::DiagnosticsEngine::Error, message);
57+
ci.diagnostics().Report(diagID) << act->GetCurrentFileOrBufferName();
58+
ci.parsing().messages().Emit(llvm::errs(), ci.allCookedSources());
59+
return true;
60+
}
61+
return false;
62+
}
63+
64+
inline bool reportFatalScanningErrors(const FrontendAction *act) {
65+
return reportFatalErrors(act, "Could not scan %0");
66+
}
67+
68+
inline bool reportFatalParsingErrors(const FrontendAction *act) {
69+
return reportFatalErrors(act, "Could not parse %0");
70+
}
71+
4872
bool PrescanAction::BeginSourceFileAction(CompilerInstance &c1) {
4973
CompilerInstance &ci = this->instance();
5074
std::string currentInputPath{GetCurrentFileOrBufferName()};
@@ -53,18 +77,7 @@ bool PrescanAction::BeginSourceFileAction(CompilerInstance &c1) {
5377
// Prescan. In case of failure, report and return.
5478
ci.parsing().Prescan(currentInputPath, parserOptions);
5579

56-
if (!ci.parsing().messages().empty() &&
57-
(ci.invocation().warnAsErr() ||
58-
ci.parsing().messages().AnyFatalError())) {
59-
const unsigned diagID = ci.diagnostics().getCustomDiagID(
60-
clang::DiagnosticsEngine::Error, "Could not scan %0");
61-
ci.diagnostics().Report(diagID) << GetCurrentFileOrBufferName();
62-
ci.parsing().messages().Emit(llvm::errs(), ci.allCookedSources());
63-
64-
return false;
65-
}
66-
67-
return true;
80+
return !reportFatalScanningErrors(this);
6881
}
6982

7083
bool PrescanAndParseAction::BeginSourceFileAction(CompilerInstance &c1) {
@@ -88,27 +101,14 @@ bool PrescanAndParseAction::BeginSourceFileAction(CompilerInstance &c1) {
88101
// Prescan. In case of failure, report and return.
89102
ci.parsing().Prescan(currentInputPath, parserOptions);
90103

91-
if (ci.parsing().messages().AnyFatalError()) {
92-
const unsigned diagID = ci.diagnostics().getCustomDiagID(
93-
clang::DiagnosticsEngine::Error, "Could not scan %0");
94-
ci.diagnostics().Report(diagID) << GetCurrentFileOrBufferName();
95-
ci.parsing().messages().Emit(llvm::errs(), ci.allCookedSources());
96-
104+
if (reportFatalScanningErrors(this))
97105
return false;
98-
}
99106

100107
// Parse. In case of failure, report and return.
101108
ci.parsing().Parse(llvm::outs());
102109

103-
if (ci.parsing().messages().AnyFatalError()) {
104-
unsigned diagID = ci.diagnostics().getCustomDiagID(
105-
clang::DiagnosticsEngine::Error, "Could not parse %0");
106-
ci.diagnostics().Report(diagID) << GetCurrentFileOrBufferName();
107-
108-
ci.parsing().messages().Emit(
109-
llvm::errs(), this->instance().allCookedSources());
110+
if (reportFatalParsingErrors(this))
110111
return false;
111-
}
112112

113113
// Report the diagnostics from parsing
114114
ci.parsing().messages().Emit(llvm::errs(), ci.allCookedSources());
@@ -124,31 +124,14 @@ bool PrescanAndSemaAction::BeginSourceFileAction(CompilerInstance &c1) {
124124
// Prescan. In case of failure, report and return.
125125
ci.parsing().Prescan(currentInputPath, parserOptions);
126126

127-
if (!ci.parsing().messages().empty() &&
128-
(ci.invocation().warnAsErr() ||
129-
ci.parsing().messages().AnyFatalError())) {
130-
const unsigned diagID = ci.diagnostics().getCustomDiagID(
131-
clang::DiagnosticsEngine::Error, "Could not scan %0");
132-
ci.diagnostics().Report(diagID) << GetCurrentFileOrBufferName();
133-
ci.parsing().messages().Emit(llvm::errs(), ci.allCookedSources());
134-
127+
if (reportFatalScanningErrors(this))
135128
return false;
136-
}
137129

138130
// Parse. In case of failure, report and return.
139131
ci.parsing().Parse(llvm::outs());
140132

141-
if (!ci.parsing().messages().empty() &&
142-
(ci.invocation().warnAsErr() ||
143-
ci.parsing().messages().AnyFatalError())) {
144-
unsigned diagID = ci.diagnostics().getCustomDiagID(
145-
clang::DiagnosticsEngine::Error, "Could not parse %0");
146-
ci.diagnostics().Report(diagID) << GetCurrentFileOrBufferName();
147-
148-
ci.parsing().messages().Emit(
149-
llvm::errs(), this->instance().allCookedSources());
133+
if (reportFatalParsingErrors(this))
150134
return false;
151-
}
152135

153136
// Report the diagnostics from parsing
154137
ci.parsing().messages().Emit(llvm::errs(), ci.allCookedSources());

flang/test/Driver/parse-error.f95

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
! Verify that parsing errors are correctly reported by the driver
2+
! Focuses on actions inheriting from the following:
3+
! * PrescanAndSemaAction (-fsyntax-only)
4+
! * PrescanAndParseAction (-fdebug-unparse-no-sema)
25

6+
! RUN: not %flang_fc1 -fdebug-unparse-no-sema %s 2>&1 | FileCheck %s --check-prefix=ERROR
37
! RUN: not %flang_fc1 -fsyntax-only %s 2>&1 | FileCheck %s --check-prefix=ERROR
48

59
! ERROR: Could not parse {{.*}}parse-error.f95

0 commit comments

Comments
 (0)