1919#include " flang/Common/reference-counted.h"
2020#include " flang/Common/restorer.h"
2121#include " flang/Support/Fortran-features.h"
22+ #include " llvm/Support/ErrorHandling.h"
2223#include < cstddef>
2324#include < cstring>
2425#include < forward_list>
@@ -334,14 +335,36 @@ class Messages {
334335 return messages_.emplace_back (std::forward<A>(args)...);
335336 }
336337
338+ // AddWarning bypasses the language feature control, it is only exposed for
339+ // legacy code that cannot be easily refactored to use Warn().
337340 template <typename ... A>
338- Message &Say (common::LanguageFeature feature , A &&...args) {
339- return Say ( std::forward<A>(args)...). set_languageFeature (feature );
341+ Message &AddWarning (common::UsageWarning warning , A &&...args) {
342+ return messages_. emplace_back (warning, std::forward<A>(args)...);
340343 }
341344
342345 template <typename ... A>
343- Message &Say (common::UsageWarning warning, A &&...args) {
344- return Say (std::forward<A>(args)...).set_usageWarning (warning);
346+ Message &AddWarning (common::LanguageFeature feature, A &&...args) {
347+ return messages_.emplace_back (feature, std::forward<A>(args)...);
348+ }
349+
350+ template <typename ... A>
351+ Message *Warn (bool isInModuleFile,
352+ const common::LanguageFeatureControl &control,
353+ common::LanguageFeature feature, A &&...args) {
354+ if (!isInModuleFile && control.ShouldWarn (feature)) {
355+ return &AddWarning (feature, std::forward<A>(args)...);
356+ }
357+ return nullptr ;
358+ }
359+
360+ template <typename ... A>
361+ Message *Warn (bool isInModuleFile,
362+ const common::LanguageFeatureControl &control,
363+ common::UsageWarning warning, A &&...args) {
364+ if (!isInModuleFile && control.ShouldWarn (warning)) {
365+ return &AddWarning (warning, std::forward<A>(args)...);
366+ }
367+ return nullptr ;
345368 }
346369
347370 void Annex (Messages &&that) {
@@ -422,24 +445,6 @@ class ContextualMessages {
422445 return Say (at.value_or (at_), std::forward<A>(args)...);
423446 }
424447
425- template <typename ... A>
426- Message *Say (common::LanguageFeature feature, A &&...args) {
427- Message *msg{Say (std::forward<A>(args)...)};
428- if (msg) {
429- msg->set_languageFeature (feature);
430- }
431- return msg;
432- }
433-
434- template <typename ... A>
435- Message *Say (common::UsageWarning warning, A &&...args) {
436- Message *msg{Say (std::forward<A>(args)...)};
437- if (msg) {
438- msg->set_usageWarning (warning);
439- }
440- return msg;
441- }
442-
443448 Message *Say (Message &&msg) {
444449 if (messages_ != nullptr ) {
445450 if (contextMessage_) {
@@ -451,6 +456,39 @@ class ContextualMessages {
451456 }
452457 }
453458
459+ template <typename FeatureOrUsageWarning, typename ... A>
460+ Message *Warn (bool isInModuleFile,
461+ const common::LanguageFeatureControl &control,
462+ FeatureOrUsageWarning feature, CharBlock at, A &&...args) {
463+ if (messages_ != nullptr ) {
464+ if (Message *
465+ msg{messages_->Warn (isInModuleFile, control, feature, at,
466+ std::forward<A>(args)...)}) {
467+ if (contextMessage_) {
468+ msg->SetContext (contextMessage_.get ());
469+ }
470+ return msg;
471+ }
472+ }
473+ return nullptr ;
474+ }
475+
476+ template <typename FeatureOrUsageWarning, typename ... A>
477+ Message *Warn (bool isInModuleFile,
478+ const common::LanguageFeatureControl &control,
479+ FeatureOrUsageWarning feature, A &&...args) {
480+ return Warn (
481+ isInModuleFile, control, feature, at_, std::forward<A>(args)...);
482+ }
483+
484+ template <typename FeatureOrUsageWarning, typename ... A>
485+ Message *Warn (bool isInModuleFile,
486+ const common::LanguageFeatureControl &control,
487+ FeatureOrUsageWarning feature, std::optional<CharBlock> at, A &&...args) {
488+ return Warn (isInModuleFile, control, feature, at.value_or (at_),
489+ std::forward<A>(args)...);
490+ }
491+
454492private:
455493 CharBlock at_;
456494 Messages *messages_{nullptr };
0 commit comments