@@ -45,6 +45,30 @@ bool reportFatalSemanticErrors(const Fortran::semantics::Semantics &semantics,
45
45
return false ;
46
46
}
47
47
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
+
48
72
bool PrescanAction::BeginSourceFileAction (CompilerInstance &c1) {
49
73
CompilerInstance &ci = this ->instance ();
50
74
std::string currentInputPath{GetCurrentFileOrBufferName ()};
@@ -53,18 +77,7 @@ bool PrescanAction::BeginSourceFileAction(CompilerInstance &c1) {
53
77
// Prescan. In case of failure, report and return.
54
78
ci.parsing ().Prescan (currentInputPath, parserOptions);
55
79
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 );
68
81
}
69
82
70
83
bool PrescanAndParseAction::BeginSourceFileAction (CompilerInstance &c1) {
@@ -88,27 +101,14 @@ bool PrescanAndParseAction::BeginSourceFileAction(CompilerInstance &c1) {
88
101
// Prescan. In case of failure, report and return.
89
102
ci.parsing ().Prescan (currentInputPath, parserOptions);
90
103
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 ))
97
105
return false ;
98
- }
99
106
100
107
// Parse. In case of failure, report and return.
101
108
ci.parsing ().Parse (llvm::outs ());
102
109
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 ))
110
111
return false ;
111
- }
112
112
113
113
// Report the diagnostics from parsing
114
114
ci.parsing ().messages ().Emit (llvm::errs (), ci.allCookedSources ());
@@ -124,31 +124,14 @@ bool PrescanAndSemaAction::BeginSourceFileAction(CompilerInstance &c1) {
124
124
// Prescan. In case of failure, report and return.
125
125
ci.parsing ().Prescan (currentInputPath, parserOptions);
126
126
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 ))
135
128
return false ;
136
- }
137
129
138
130
// Parse. In case of failure, report and return.
139
131
ci.parsing ().Parse (llvm::outs ());
140
132
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 ))
150
134
return false ;
151
- }
152
135
153
136
// Report the diagnostics from parsing
154
137
ci.parsing ().messages ().Emit (llvm::errs (), ci.allCookedSources ());
0 commit comments