[flang-commits] [flang] [flang] Emit warnings, not errors, for bad subscripts in dead code (PR #174040)
Peter Klausler via flang-commits
flang-commits at lists.llvm.org
Wed Dec 31 12:40:47 PST 2025
================
@@ -5372,6 +5371,88 @@ bool ExprChecker::Pre(const parser::DataImpliedDo &ido) {
return false;
}
+// Handle messages from dead code in block IF constructs
+bool ExprChecker::Pre(const parser::IfConstruct &construct) {
+ enum State {
+ NotYetKnownTrue,
+ WasKnownTrue,
+ TrueNow,
+ FalseNow
+ } state{NotYetKnownTrue};
+ auto &messages{exprAnalyzer_.GetContextualMessages()};
+ parser::CharBlock determiner; // source of relevant IF statement
+
+ auto consider{[&](const SomeExpr *expr, parser::CharBlock here) {
+ if (state == TrueNow) {
+ state = WasKnownTrue;
+ } else if (state != WasKnownTrue) {
+ state = NotYetKnownTrue;
+ if (expr) {
+ if (auto known{
+ evaluate::GetScalarConstantValue<evaluate::LogicalResult>(
+ *expr)}) {
+ state = known->IsTrue() ? TrueNow : FalseNow;
+ determiner = here;
+ }
+ }
+ }
+ }};
+ auto doBlock{[&, this](const parser::Block &block) {
+ if ((state == FalseNow || state == WasKnownTrue) && messages.messages()) {
+ parser::Messages inDeadCode;
+ {
+ auto restorer{messages.SetMessages(inDeadCode)};
+ parser::Walk(block, *this);
+ }
+ for (auto &msg : inDeadCode.messages()) {
+ if (msg.severity() == parser::Severity::ErrorUnlessDeadCode) {
+ msg.set_severity(parser::Severity::Warning);
+ msg.set_usageWarning(common::UsageWarning::BadValueInDeadCode);
+ msg.Attach(determiner,
+ "in code known to be dead due to this compile-time IF expression value"_en_US);
+ }
+ }
+ if (context_.ShouldWarn(common::UsageWarning::BadValueInDeadCode) ||
----------------
klausler wrote:
If we're going to be silent about cases that used to be hard errors in dead code, it would be weird to not also be silent about cases that used to be just warnings.
https://github.com/llvm/llvm-project/pull/174040
More information about the flang-commits
mailing list