Skip to content

Commit b9cdd2e

Browse files
committed
initial commit
1 parent a32040e commit b9cdd2e

File tree

7 files changed

+96
-40
lines changed

7 files changed

+96
-40
lines changed

flang/include/flang/Parser/message.h

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,19 @@ class MessageFixedText {
5656

5757
CharBlock text() const { return text_; }
5858
bool empty() const { return text_.empty(); }
59-
Severity severity() const { return severity_; }
59+
Severity severity(bool warningsAreErrors = false) const {
60+
if (warningsAreErrors) {
61+
return Severity::Error;
62+
}
63+
return severity_;
64+
}
6065
MessageFixedText &set_severity(Severity severity) {
6166
severity_ = severity;
6267
return *this;
6368
}
64-
bool IsFatal() const {
65-
return severity_ == Severity::Error || severity_ == Severity::Todo;
69+
bool IsFatal(bool warningsAreErrors = false) const {
70+
Severity sev{severity(warningsAreErrors)};
71+
return sev == Severity::Error || sev == Severity::Todo;
6672
}
6773

6874
private:
@@ -105,22 +111,28 @@ class MessageFormattedText {
105111
public:
106112
template <typename... A>
107113
MessageFormattedText(const MessageFixedText &text, A &&...x)
108-
: severity_{text.severity()} {
114+
: severity_{text.severity(false)} {
109115
Format(&text, Convert(std::forward<A>(x))...);
110116
}
111117
MessageFormattedText(const MessageFormattedText &) = default;
112118
MessageFormattedText(MessageFormattedText &&) = default;
113119
MessageFormattedText &operator=(const MessageFormattedText &) = default;
114120
MessageFormattedText &operator=(MessageFormattedText &&) = default;
115121
const std::string &string() const { return string_; }
116-
bool IsFatal() const {
117-
return severity_ == Severity::Error || severity_ == Severity::Todo;
122+
Severity severity(bool warningsAreErrors = false) const {
123+
if (warningsAreErrors) {
124+
return Severity::Error;
125+
}
126+
return severity_;
118127
}
119-
Severity severity() const { return severity_; }
120128
MessageFormattedText &set_severity(Severity severity) {
121129
severity_ = severity;
122130
return *this;
123131
}
132+
bool IsFatal(bool warningsAreErrors = false) const {
133+
Severity sev{severity(warningsAreErrors)};
134+
return sev == Severity::Error || sev == Severity::Todo;
135+
}
124136
std::string MoveString() { return std::move(string_); }
125137
bool operator==(const MessageFormattedText &that) const {
126138
return severity_ == that.severity_ && string_ == that.string_;
@@ -281,8 +293,8 @@ class Message : public common::ReferenceCounted<Message> {
281293
}
282294

283295
bool SortBefore(const Message &that) const;
284-
bool IsFatal() const;
285-
Severity severity() const;
296+
bool IsFatal(bool warningsAreErrors = false) const;
297+
Severity severity(bool warningsAreErrors = false) const;
286298
Message &set_severity(Severity);
287299
std::optional<common::LanguageFeature> languageFeature() const;
288300
Message &set_languageFeature(common::LanguageFeature);
@@ -293,7 +305,8 @@ class Message : public common::ReferenceCounted<Message> {
293305
const AllCookedSources &) const;
294306
void Emit(llvm::raw_ostream &, const AllCookedSources &,
295307
bool echoSourceLine = true,
296-
const common::LanguageFeatureControl *hintFlags = nullptr) const;
308+
const common::LanguageFeatureControl *hintFlags = nullptr,
309+
bool warningsAreErrors = false) const;
297310

298311
// If this Message or any of its attachments locates itself via a CharBlock,
299312
// replace its location with the corresponding ProvenanceRange.
@@ -355,9 +368,9 @@ class Messages {
355368
void Emit(llvm::raw_ostream &, const AllCookedSources &,
356369
bool echoSourceLines = true,
357370
const common::LanguageFeatureControl *hintFlags = nullptr,
358-
std::size_t maxErrorsToEmit = 0) const;
371+
std::size_t maxErrorsToEmit = 0, bool warningsAreErrors = false) const;
359372
void AttachTo(Message &, std::optional<Severity> = std::nullopt);
360-
bool AnyFatalError() const;
373+
bool AnyFatalError(bool warningsAreErrors = false) const;
361374

362375
private:
363376
std::list<Message> messages_;

flang/lib/Frontend/FrontendAction.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ bool FrontendAction::reportFatalErrors(const char (&message)[N]) {
238238
instance->getDiagnostics().Report(diagID) << getCurrentFileOrBufferName();
239239
instance->getParsing().messages().Emit(
240240
llvm::errs(), instance->getAllCookedSources(),
241-
/*echoSourceLines=*/true, &features, maxErrors);
241+
/*echoSourceLines=*/true, &features, maxErrors,
242+
instance->getInvocation().getWarnAsErr());
242243
return true;
243244
}
244245
if (instance->getParsing().parseTree().has_value() &&
@@ -249,7 +250,8 @@ bool FrontendAction::reportFatalErrors(const char (&message)[N]) {
249250
instance->getDiagnostics().Report(diagID) << getCurrentFileOrBufferName();
250251
instance->getParsing().messages().Emit(
251252
llvm::errs(), instance->getAllCookedSources(),
252-
/*echoSourceLine=*/true, &features, maxErrors);
253+
/*echoSourceLine=*/true, &features, maxErrors,
254+
instance->getInvocation().getWarnAsErr());
253255
instance->getParsing().EmitMessage(
254256
llvm::errs(), instance->getParsing().finalRestingPlace(),
255257
"parser FAIL (final position)", "error: ", llvm::raw_ostream::RED);

flang/lib/Parser/message.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,21 @@ bool Message::SortBefore(const Message &that) const {
161161
location_, that.location_);
162162
}
163163

164-
bool Message::IsFatal() const {
165-
return severity() == Severity::Error || severity() == Severity::Todo;
164+
bool Message::IsFatal(bool warningsAreErrors) const {
165+
Severity sev{severity(warningsAreErrors)};
166+
return sev == Severity::Error || sev == Severity::Todo;
166167
}
167168

168-
Severity Message::severity() const {
169+
Severity Message::severity(bool warningsAreErrors) const {
169170
return common::visit(
170171
common::visitors{
171172
[](const MessageExpectedText &) { return Severity::Error; },
172-
[](const MessageFixedText &x) { return x.severity(); },
173-
[](const MessageFormattedText &x) { return x.severity(); },
173+
[=](const MessageFixedText &x) {
174+
return x.severity(warningsAreErrors);
175+
},
176+
[=](const MessageFormattedText &x) {
177+
return x.severity(warningsAreErrors);
178+
},
174179
},
175180
text_);
176181
}
@@ -295,15 +300,16 @@ static constexpr int MAX_CONTEXTS_EMITTED{2};
295300
static constexpr bool OMIT_SHARED_CONTEXTS{true};
296301

297302
void Message::Emit(llvm::raw_ostream &o, const AllCookedSources &allCooked,
298-
bool echoSourceLine,
299-
const common::LanguageFeatureControl *hintFlagPtr) const {
303+
bool echoSourceLine, const common::LanguageFeatureControl *hintFlagPtr,
304+
bool warningsAreErrors) const {
300305
std::optional<ProvenanceRange> provenanceRange{GetProvenanceRange(allCooked)};
301306
const AllSources &sources{allCooked.allSources()};
302307
const std::string text{ToString()};
308+
Severity sev{severity(warningsAreErrors)};
303309
const std::string hint{
304310
HintLanguageControlFlag(hintFlagPtr, languageFeature_, usageWarning_)};
305-
sources.EmitMessage(o, provenanceRange, text + hint, Prefix(severity()),
306-
PrefixColor(severity()), echoSourceLine);
311+
sources.EmitMessage(o, provenanceRange, text + hint, Prefix(sev),
312+
PrefixColor(sev), echoSourceLine);
307313
// Refers to whether the attachment in the loop below is a context, but can't
308314
// be declared inside the loop because the previous iteration's
309315
// attachment->attachmentIsContext_ indicates this.
@@ -453,7 +459,7 @@ void Messages::ResolveProvenances(const AllCookedSources &allCooked) {
453459

454460
void Messages::Emit(llvm::raw_ostream &o, const AllCookedSources &allCooked,
455461
bool echoSourceLines, const common::LanguageFeatureControl *hintFlagPtr,
456-
std::size_t maxErrorsToEmit) const {
462+
std::size_t maxErrorsToEmit, bool warningsAreErrors) const {
457463
std::vector<const Message *> sorted;
458464
for (const auto &msg : messages_) {
459465
sorted.push_back(&msg);
@@ -467,9 +473,9 @@ void Messages::Emit(llvm::raw_ostream &o, const AllCookedSources &allCooked,
467473
// Don't emit two identical messages for the same location
468474
continue;
469475
}
470-
msg->Emit(o, allCooked, echoSourceLines, hintFlagPtr);
476+
msg->Emit(o, allCooked, echoSourceLines, hintFlagPtr, warningsAreErrors);
471477
lastMsg = msg;
472-
if (msg->IsFatal()) {
478+
if (msg->IsFatal(warningsAreErrors)) {
473479
++errorsEmitted;
474480
}
475481
// If maxErrorsToEmit is 0, emit all errors, otherwise break after
@@ -491,9 +497,12 @@ void Messages::AttachTo(Message &msg, std::optional<Severity> severity) {
491497
messages_.clear();
492498
}
493499

494-
bool Messages::AnyFatalError() const {
500+
bool Messages::AnyFatalError(bool warningsAreErrors) const {
501+
if (messages_.empty()) {
502+
return false;
503+
}
495504
for (const auto &msg : messages_) {
496-
if (msg.IsFatal()) {
505+
if (msg.IsFatal(warningsAreErrors)) {
497506
return true;
498507
}
499508
}

flang/lib/Semantics/resolve-names.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7575,7 +7575,8 @@ bool DeclarationVisitor::OkToAddComponent(
75757575
if (msg) {
75767576
auto &said{Say2(name, std::move(*msg), *prev,
75777577
"Previous declaration of '%s'"_en_US)};
7578-
if (msg->severity() == parser::Severity::Error) {
7578+
if (msg->severity(/*warningsAreErrors=*/false) ==
7579+
parser::Severity::Error) {
75797580
Resolve(name, *prev);
75807581
return false;
75817582
}

flang/lib/Semantics/semantics.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,7 @@ const DeclTypeSpec &SemanticsContext::MakeLogicalType(int kind) {
376376
}
377377

378378
bool SemanticsContext::AnyFatalError() const {
379-
return !messages_.empty() &&
380-
(warningsAreErrors_ || messages_.AnyFatalError());
379+
return messages_.AnyFatalError(warningsAreErrors_);
381380
}
382381
bool SemanticsContext::HasError(const Symbol &symbol) {
383382
return errorSymbols_.count(symbol) > 0;
@@ -658,7 +657,7 @@ void Semantics::EmitMessages(llvm::raw_ostream &os) {
658657
context_.messages().ResolveProvenances(context_.allCookedSources());
659658
context_.messages().Emit(os, context_.allCookedSources(),
660659
/*echoSourceLine=*/true, &context_.languageFeatures(),
661-
/*maxErrorsToEmit=*/context_.maxErrors());
660+
context_.maxErrors(), context_.warningsAreErrors());
662661
}
663662

664663
void SemanticsContext::DumpSymbols(llvm::raw_ostream &os) {

flang/test/Driver/color-diagnostics-scan.f

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33
! Windows command prompt doesn't support ANSI escape sequences.
44
! REQUIRES: shell
55
6-
! RUN: not %flang %s -E -Werror -fcolor-diagnostics 2>&1 \
6+
! RUN: %flang %s -E -fcolor-diagnostics 2>&1 \
77
! RUN: | FileCheck %s --check-prefix=CHECK_CD
8-
! RUN: not %flang %s -E -Werror -fno-color-diagnostics 2>&1 \
8+
! RUN: %flang %s -E -fno-color-diagnostics 2>&1 \
99
! RUN: | FileCheck %s --check-prefix=CHECK_NCD
10-
! RUN: not %flang_fc1 -E -Werror %s -fcolor-diagnostics 2>&1 \
10+
! RUN: %flang_fc1 -E %s -fcolor-diagnostics 2>&1 \
1111
! RUN: | FileCheck %s --check-prefix=CHECK_CD
1212
13-
! RUN: not %flang %s -E -Werror -fdiagnostics-color 2>&1 \
13+
! RUN: %flang %s -E -fdiagnostics-color 2>&1 \
1414
! RUN: | FileCheck %s --check-prefix=CHECK_CD
15-
! RUN: not %flang %s -E -Werror -fno-diagnostics-color 2>&1 \
15+
! RUN: %flang %s -E -fno-diagnostics-color 2>&1 \
1616
! RUN: | FileCheck %s --check-prefix=CHECK_NCD
1717
18-
! RUN: not %flang %s -E -Werror -fdiagnostics-color=always 2>&1 \
18+
! RUN: %flang %s -E -fdiagnostics-color=always 2>&1 \
1919
! RUN: | FileCheck %s --check-prefix=CHECK_CD
20-
! RUN: not %flang %s -E -Werror -fdiagnostics-color=never 2>&1 \
20+
! RUN: %flang %s -E -fdiagnostics-color=never 2>&1 \
2121
! RUN: | FileCheck %s --check-prefix=CHECK_NCD
2222
23-
! RUN: not %flang_fc1 -E -Werror %s 2>&1 | FileCheck %s --check-prefix=CHECK_NCD
23+
! RUN: %flang_fc1 -E %s 2>&1 | FileCheck %s --check-prefix=CHECK_NCD
2424
2525
! CHECK_CD: {{.*}}[0;1;35mwarning: {{.*}}[0mCharacter in fixed-form label field must be a digit
2626
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
! RUN: not %flang_fc1 -Wfatal-errors -pedantic %s 2>&1 | FileCheck %s --check-prefix=CHECK1
2+
! RUN: not %flang_fc1 -pedantic -Werror %s 2>&1 | FileCheck %s --check-prefix=CHECK2
3+
! RUN: not %flang_fc1 -Wfatal-errors -pedantic -Werror %s 2>&1 | FileCheck %s --check-prefix=CHECK3
4+
5+
module m
6+
contains
7+
subroutine foo(a)
8+
real, intent(in), target :: a(:)
9+
end subroutine
10+
end module
11+
12+
program test
13+
use m
14+
real, target :: a(1)
15+
real :: b(1)
16+
call foo(a) ! ok
17+
!CHECK1: fatal-errors-warnings.f90:{{.*}} warning:
18+
!CHECK2: fatal-errors-warnings.f90:{{.*}} error:
19+
!CHECK3: fatal-errors-warnings.f90:{{.*}} error:
20+
call foo(b)
21+
!CHECK1: fatal-errors-warnings.f90:{{.*}} warning:
22+
!CHECK2: fatal-errors-warnings.f90:{{.*}} error:
23+
!CHECK3-NOT: error:
24+
!CHECK3-NOT: warning:
25+
call foo((a))
26+
!CHECK1: fatal-errors-warnings.f90:{{.*}} warning:
27+
!CHECK2: fatal-errors-warnings.f90:{{.*}} error:
28+
call foo(a([1]))
29+
!CHECK1: fatal-errors-warnings.f90:{{.*}} error:
30+
!CHECK2: fatal-errors-warnings.f90:{{.*}} error:
31+
call foo(a(1))
32+
end

0 commit comments

Comments
 (0)