Skip to content

Commit 0f973ac

Browse files
authored
[flang] Tag warnings with LanguageFeature or UsageWarning (#110304)
(This is a big patch, but it's nearly an NFC. No test results have changed and all Fortran tests in the LLVM test suites work as expected.) Allow a parser::Message for a warning to be marked with the common::LanguageFeature or common::UsageWarning that controls it. This will allow a later patch to add hooks whereby a driver will be able to decorate warning messages with the names of its options that enable each particular warning, and to add hooks whereby a driver can map those enumerators by name to command-line options that enable/disable the language feature and enable/disable the messages. The default settings in the constructor for LanguageFeatureControl were moved from its header file into its C++ source file. Hooks for a driver to use to map the name of a feature or warning to its enumerator were also added. To simplify the tagging of warnings with their corresponding language feature or usage warning, to ensure that they are properly controlled by ShouldWarn(), and to ensure that warnings never issue at code sites in module files, two new Warn() member function templates were added to SemanticsContext and other contextual frameworks. Warn() can't be used before source locations can be mapped to scopes, but the bulk of existing code blocks testing ShouldWarn() and FindModuleFile() before calling Say() were convertible into calls to Warn(). The ones that were not convertible were extended with explicit calls to Message::set_languageFeature() and set_usageWarning().
1 parent 4cd1f9a commit 0f973ac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1093
-938
lines changed

flang/include/flang/Common/Fortran-features.h

Lines changed: 6 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "flang/Common/Fortran.h"
1313
#include "flang/Common/enum-set.h"
1414
#include "flang/Common/idioms.h"
15+
#include <optional>
1516
#include <vector>
1617

1718
namespace Fortran::common {
@@ -48,7 +49,7 @@ ENUM_CLASS(LanguageFeature, BackslashEscapes, OldDebugLines,
4849
ImpliedDoIndexScope, DistinctCommonSizes, OddIndexVariableRestrictions,
4950
IndistinguishableSpecifics, SubroutineAndFunctionSpecifics,
5051
EmptySequenceType, NonSequenceCrayPointee, BranchIntoConstruct,
51-
BadBranchTarget, ConvertedArgument, HollerithPolymorphic, ListDirectedSize,
52+
BadBranchTarget, HollerithPolymorphic, ListDirectedSize,
5253
NonBindCInteroperability, CudaManaged, CudaUnified,
5354
PolymorphicActualAllocatableOrPointerToMonomorphicDummy, RelaxedPureDummy,
5455
UndefinableAsynchronousOrVolatileActual, AutomaticInMainProgram, PrintCptr,
@@ -76,80 +77,12 @@ ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
7677
using LanguageFeatures = EnumSet<LanguageFeature, LanguageFeature_enumSize>;
7778
using UsageWarnings = EnumSet<UsageWarning, UsageWarning_enumSize>;
7879

80+
std::optional<LanguageFeature> FindLanguageFeature(const char *);
81+
std::optional<UsageWarning> FindUsageWarning(const char *);
82+
7983
class LanguageFeatureControl {
8084
public:
81-
LanguageFeatureControl() {
82-
// These features must be explicitly enabled by command line options.
83-
disable_.set(LanguageFeature::OldDebugLines);
84-
disable_.set(LanguageFeature::OpenACC);
85-
disable_.set(LanguageFeature::OpenMP);
86-
disable_.set(LanguageFeature::CUDA); // !@cuf
87-
disable_.set(LanguageFeature::CudaManaged);
88-
disable_.set(LanguageFeature::CudaUnified);
89-
disable_.set(LanguageFeature::ImplicitNoneTypeNever);
90-
disable_.set(LanguageFeature::ImplicitNoneTypeAlways);
91-
disable_.set(LanguageFeature::DefaultSave);
92-
disable_.set(LanguageFeature::SaveMainProgram);
93-
// These features, if enabled, conflict with valid standard usage,
94-
// so there are disabled here by default.
95-
disable_.set(LanguageFeature::BackslashEscapes);
96-
disable_.set(LanguageFeature::LogicalAbbreviations);
97-
disable_.set(LanguageFeature::XOROperator);
98-
disable_.set(LanguageFeature::OldStyleParameter);
99-
// These warnings are enabled by default, but only because they used
100-
// to be unconditional. TODO: prune this list
101-
warnLanguage_.set(LanguageFeature::ExponentMatchingKindParam);
102-
warnLanguage_.set(LanguageFeature::RedundantAttribute);
103-
warnLanguage_.set(LanguageFeature::SubroutineAndFunctionSpecifics);
104-
warnLanguage_.set(LanguageFeature::EmptySequenceType);
105-
warnLanguage_.set(LanguageFeature::NonSequenceCrayPointee);
106-
warnLanguage_.set(LanguageFeature::BranchIntoConstruct);
107-
warnLanguage_.set(LanguageFeature::BadBranchTarget);
108-
warnLanguage_.set(LanguageFeature::ConvertedArgument);
109-
warnLanguage_.set(LanguageFeature::HollerithPolymorphic);
110-
warnLanguage_.set(LanguageFeature::ListDirectedSize);
111-
warnUsage_.set(UsageWarning::ShortArrayActual);
112-
warnUsage_.set(UsageWarning::FoldingException);
113-
warnUsage_.set(UsageWarning::FoldingAvoidsRuntimeCrash);
114-
warnUsage_.set(UsageWarning::FoldingValueChecks);
115-
warnUsage_.set(UsageWarning::FoldingFailure);
116-
warnUsage_.set(UsageWarning::FoldingLimit);
117-
warnUsage_.set(UsageWarning::Interoperability);
118-
warnUsage_.set(UsageWarning::Bounds);
119-
warnUsage_.set(UsageWarning::Preprocessing);
120-
warnUsage_.set(UsageWarning::Scanning);
121-
warnUsage_.set(UsageWarning::OpenAccUsage);
122-
warnUsage_.set(UsageWarning::ProcPointerCompatibility);
123-
warnUsage_.set(UsageWarning::VoidMold);
124-
warnUsage_.set(UsageWarning::KnownBadImplicitInterface);
125-
warnUsage_.set(UsageWarning::EmptyCase);
126-
warnUsage_.set(UsageWarning::CaseOverflow);
127-
warnUsage_.set(UsageWarning::CUDAUsage);
128-
warnUsage_.set(UsageWarning::IgnoreTKRUsage);
129-
warnUsage_.set(UsageWarning::ExternalInterfaceMismatch);
130-
warnUsage_.set(UsageWarning::DefinedOperatorArgs);
131-
warnUsage_.set(UsageWarning::Final);
132-
warnUsage_.set(UsageWarning::ZeroDoStep);
133-
warnUsage_.set(UsageWarning::UnusedForallIndex);
134-
warnUsage_.set(UsageWarning::OpenMPUsage);
135-
warnUsage_.set(UsageWarning::ModuleFile);
136-
warnUsage_.set(UsageWarning::DataLength);
137-
warnUsage_.set(UsageWarning::IgnoredDirective);
138-
warnUsage_.set(UsageWarning::HomonymousSpecific);
139-
warnUsage_.set(UsageWarning::HomonymousResult);
140-
warnUsage_.set(UsageWarning::IgnoredIntrinsicFunctionType);
141-
warnUsage_.set(UsageWarning::PreviousScalarUse);
142-
warnUsage_.set(UsageWarning::RedeclaredInaccessibleComponent);
143-
warnUsage_.set(UsageWarning::ImplicitShared);
144-
warnUsage_.set(UsageWarning::IndexVarRedefinition);
145-
warnUsage_.set(UsageWarning::IncompatibleImplicitInterfaces);
146-
warnUsage_.set(UsageWarning::BadTypeForTarget);
147-
warnUsage_.set(UsageWarning::VectorSubscriptFinalization);
148-
warnUsage_.set(UsageWarning::UndefinedFunctionResult);
149-
warnUsage_.set(UsageWarning::UselessIomsg);
150-
// New warnings, on by default
151-
warnLanguage_.set(LanguageFeature::SavedLocalInSpecExpr);
152-
}
85+
LanguageFeatureControl();
15386
LanguageFeatureControl(const LanguageFeatureControl &) = default;
15487

15588
void Enable(LanguageFeature f, bool yes = true) { disable_.set(f, !yes); }

flang/include/flang/Parser/message.h

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "char-block.h"
1616
#include "char-set.h"
1717
#include "provenance.h"
18+
#include "flang/Common/Fortran-features.h"
1819
#include "flang/Common/idioms.h"
1920
#include "flang/Common/reference-counted.h"
2021
#include "flang/Common/restorer.h"
@@ -202,6 +203,26 @@ class Message : public common::ReferenceCounted<Message> {
202203
Message(ProvenanceRange pr, const MessageExpectedText &t)
203204
: location_{pr}, text_{t} {}
204205

206+
Message(common::LanguageFeature feature, ProvenanceRange pr,
207+
const MessageFixedText &t)
208+
: location_{pr}, text_{t}, languageFeature_{feature} {}
209+
Message(common::LanguageFeature feature, ProvenanceRange pr,
210+
const MessageFormattedText &s)
211+
: location_{pr}, text_{s}, languageFeature_{feature} {}
212+
Message(common::LanguageFeature feature, ProvenanceRange pr,
213+
MessageFormattedText &&s)
214+
: location_{pr}, text_{std::move(s)}, languageFeature_{feature} {}
215+
216+
Message(common::UsageWarning warning, ProvenanceRange pr,
217+
const MessageFixedText &t)
218+
: location_{pr}, text_{t}, usageWarning_{warning} {}
219+
Message(common::UsageWarning warning, ProvenanceRange pr,
220+
const MessageFormattedText &s)
221+
: location_{pr}, text_{s}, usageWarning_{warning} {}
222+
Message(common::UsageWarning warning, ProvenanceRange pr,
223+
MessageFormattedText &&s)
224+
: location_{pr}, text_{std::move(s)}, usageWarning_{warning} {}
225+
205226
Message(CharBlock csr, const MessageFixedText &t)
206227
: location_{csr}, text_{t} {}
207228
Message(CharBlock csr, const MessageFormattedText &s)
@@ -211,10 +232,41 @@ class Message : public common::ReferenceCounted<Message> {
211232
Message(CharBlock csr, const MessageExpectedText &t)
212233
: location_{csr}, text_{t} {}
213234

235+
Message(
236+
common::LanguageFeature feature, CharBlock csr, const MessageFixedText &t)
237+
: location_{csr}, text_{t}, languageFeature_{feature} {}
238+
Message(common::LanguageFeature feature, CharBlock csr,
239+
const MessageFormattedText &s)
240+
: location_{csr}, text_{s}, languageFeature_{feature} {}
241+
Message(
242+
common::LanguageFeature feature, CharBlock csr, MessageFormattedText &&s)
243+
: location_{csr}, text_{std::move(s)}, languageFeature_{feature} {}
244+
245+
Message(
246+
common::UsageWarning warning, CharBlock csr, const MessageFixedText &t)
247+
: location_{csr}, text_{t}, usageWarning_{warning} {}
248+
Message(common::UsageWarning warning, CharBlock csr,
249+
const MessageFormattedText &s)
250+
: location_{csr}, text_{s}, usageWarning_{warning} {}
251+
Message(common::UsageWarning warning, CharBlock csr, MessageFormattedText &&s)
252+
: location_{csr}, text_{std::move(s)}, usageWarning_{warning} {}
253+
214254
template <typename RANGE, typename A, typename... As>
215255
Message(RANGE r, const MessageFixedText &t, A &&x, As &&...xs)
216256
: location_{r}, text_{MessageFormattedText{
217257
t, std::forward<A>(x), std::forward<As>(xs)...}} {}
258+
template <typename RANGE, typename A, typename... As>
259+
Message(common::LanguageFeature feature, RANGE r, const MessageFixedText &t,
260+
A &&x, As &&...xs)
261+
: location_{r}, text_{MessageFormattedText{
262+
t, std::forward<A>(x), std::forward<As>(xs)...}},
263+
languageFeature_{feature} {}
264+
template <typename RANGE, typename A, typename... As>
265+
Message(common::UsageWarning warning, RANGE r, const MessageFixedText &t,
266+
A &&x, As &&...xs)
267+
: location_{r}, text_{MessageFormattedText{
268+
t, std::forward<A>(x), std::forward<As>(xs)...}},
269+
usageWarning_{warning} {}
218270

219271
Reference attachment() const { return attachment_; }
220272

@@ -232,6 +284,10 @@ class Message : public common::ReferenceCounted<Message> {
232284
bool IsFatal() const;
233285
Severity severity() const;
234286
Message &set_severity(Severity);
287+
std::optional<common::LanguageFeature> languageFeature() const;
288+
Message &set_languageFeature(common::LanguageFeature);
289+
std::optional<common::UsageWarning> usageWarning() const;
290+
Message &set_usageWarning(common::UsageWarning);
235291
std::string ToString() const;
236292
std::optional<ProvenanceRange> GetProvenanceRange(
237293
const AllCookedSources &) const;
@@ -256,6 +312,8 @@ class Message : public common::ReferenceCounted<Message> {
256312
text_;
257313
bool attachmentIsContext_{false};
258314
Reference attachment_;
315+
std::optional<common::LanguageFeature> languageFeature_;
316+
std::optional<common::UsageWarning> usageWarning_;
259317
};
260318

261319
class Messages {
@@ -275,6 +333,16 @@ class Messages {
275333
return messages_.emplace_back(std::forward<A>(args)...);
276334
}
277335

336+
template <typename... A>
337+
Message &Say(common::LanguageFeature feature, A &&...args) {
338+
return Say(std::forward<A>(args)...).set_languageFeature(feature);
339+
}
340+
341+
template <typename... A>
342+
Message &Say(common::UsageWarning warning, A &&...args) {
343+
return Say(std::forward<A>(args)...).set_usageWarning(warning);
344+
}
345+
278346
void Annex(Messages &&that) {
279347
messages_.splice(messages_.end(), that.messages_);
280348
}
@@ -330,6 +398,10 @@ class ContextualMessages {
330398
return common::ScopedSet(messages_, nullptr);
331399
}
332400

401+
template <typename... A> Message *Say(A &&...args) {
402+
return Say(at_, std::forward<A>(args)...);
403+
}
404+
333405
template <typename... A> Message *Say(CharBlock at, A &&...args) {
334406
if (messages_ != nullptr) {
335407
auto &msg{messages_->Say(at, std::forward<A>(args)...)};
@@ -347,8 +419,22 @@ class ContextualMessages {
347419
return Say(at.value_or(at_), std::forward<A>(args)...);
348420
}
349421

350-
template <typename... A> Message *Say(A &&...args) {
351-
return Say(at_, std::forward<A>(args)...);
422+
template <typename... A>
423+
Message *Say(common::LanguageFeature feature, A &&...args) {
424+
Message *msg{Say(std::forward<A>(args)...)};
425+
if (msg) {
426+
msg->set_languageFeature(feature);
427+
}
428+
return msg;
429+
}
430+
431+
template <typename... A>
432+
Message *Say(common::UsageWarning warning, A &&...args) {
433+
Message *msg{Say(std::forward<A>(args)...)};
434+
if (msg) {
435+
msg->set_usageWarning(warning);
436+
}
437+
return msg;
352438
}
353439

354440
Message *Say(Message &&msg) {

flang/include/flang/Semantics/expression.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ class ExpressionAnalyzer {
123123
template <typename... A> parser::Message *Say(A &&...args) {
124124
return GetContextualMessages().Say(std::forward<A>(args)...);
125125
}
126+
template <typename FeatureOrUsageWarning, typename... A>
127+
parser::Message *Warn(
128+
FeatureOrUsageWarning warning, parser::CharBlock at, A &&...args) {
129+
return context_.Warn(warning, at, std::forward<A>(args)...);
130+
}
131+
template <typename FeatureOrUsageWarning, typename... A>
132+
parser::Message *Warn(FeatureOrUsageWarning warning, A &&...args) {
133+
return Warn(
134+
warning, GetContextualMessages().at(), std::forward<A>(args)...);
135+
}
126136

127137
template <typename T, typename... A>
128138
parser::Message *SayAt(const T &parsed, A &&...args) {

flang/include/flang/Semantics/semantics.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,24 @@ class SemanticsContext {
188188
return message;
189189
}
190190

191+
template <typename FeatureOrUsageWarning, typename... A>
192+
parser::Message *Warn(
193+
FeatureOrUsageWarning warning, parser::CharBlock at, A &&...args) {
194+
if (languageFeatures_.ShouldWarn(warning) && !IsInModuleFile(at)) {
195+
parser::Message &msg{
196+
messages_.Say(warning, at, std::forward<A>(args)...)};
197+
return &msg;
198+
} else {
199+
return nullptr;
200+
}
201+
}
202+
203+
template <typename FeatureOrUsageWarning, typename... A>
204+
parser::Message *Warn(FeatureOrUsageWarning warning, A &&...args) {
205+
CHECK(location_);
206+
return Warn(warning, *location_, std::forward<A>(args)...);
207+
}
208+
191209
const Scope &FindScope(parser::CharBlock) const;
192210
Scope &FindScope(parser::CharBlock);
193211
void UpdateScopeIndex(Scope &, parser::CharBlock);
@@ -270,7 +288,7 @@ class SemanticsContext {
270288
std::multimap<parser::CharBlock, Scope &, ScopeIndexComparator>;
271289
ScopeIndex::iterator SearchScopeIndex(parser::CharBlock);
272290

273-
void CheckIndexVarRedefine(
291+
parser::Message *CheckIndexVarRedefine(
274292
const parser::CharBlock &, const Symbol &, parser::MessageFixedText &&);
275293
void CheckError(const Symbol &);
276294

0 commit comments

Comments
 (0)