[flang-commits] [flang] ee9c917 - [flang] Fix bogus branch target error on END SELECT

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Mon Jun 13 11:43:06 PDT 2022


Author: Peter Klausler
Date: 2022-06-13T11:42:55-07:00
New Revision: ee9c9170480a5f628846eb82d300e88d4add45d5

URL: https://github.com/llvm/llvm-project/commit/ee9c9170480a5f628846eb82d300e88d4add45d5
DIFF: https://github.com/llvm/llvm-project/commit/ee9c9170480a5f628846eb82d300e88d4add45d5.diff

LOG: [flang] Fix bogus branch target error on END SELECT

The scope model used for branch target checking treats a label
on an END SELECT statement as if it were in the previous CASE block.
This makes it illegal to GO TO that label from within any earlier
CASE block in that statement.  Fix by treating the CASE blocks as
nested scopes within the scope of the SELECT construct.

Also, add a "warning:" tag to related warning messages.

Differential Revision: https://reviews.llvm.org/D127425

Added: 
    

Modified: 
    flang/lib/Semantics/resolve-labels.cpp
    flang/test/Semantics/label05.f90
    flang/test/Semantics/label06.f90
    flang/test/Semantics/label07.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/resolve-labels.cpp b/flang/lib/Semantics/resolve-labels.cpp
index b099d24070cc7..c54740033b10d 100644
--- a/flang/lib/Semantics/resolve-labels.cpp
+++ b/flang/lib/Semantics/resolve-labels.cpp
@@ -233,13 +233,16 @@ class ParseTreeAnalyzer {
     using LabeledConstructEndStmts = std::tuple<parser::EndAssociateStmt,
         parser::EndBlockStmt, parser::EndChangeTeamStmt,
         parser::EndCriticalStmt, parser::EndDoStmt, parser::EndForallStmt,
-        parser::EndIfStmt, parser::EndSelectStmt, parser::EndWhereStmt>;
+        parser::EndIfStmt, parser::EndWhereStmt>;
     using LabeledProgramUnitEndStmts =
         std::tuple<parser::EndFunctionStmt, parser::EndMpSubprogramStmt,
             parser::EndProgramStmt, parser::EndSubroutineStmt>;
     auto targetFlags{ConstructBranchTargetFlags(statement)};
     if constexpr (common::HasMember<A, LabeledConstructStmts>) {
       AddTargetLabelDefinition(label.value(), targetFlags, ParentScope());
+    } else if constexpr (std::is_same_v<A, parser::EndSelectStmt>) {
+      // the label on an END SELECT is not in the last case
+      AddTargetLabelDefinition(label.value(), targetFlags, ParentScope(), true);
     } else if constexpr (common::HasMember<A, LabeledConstructEndStmts>) {
       constexpr bool isExecutableConstructEndStmt{true};
       AddTargetLabelDefinition(label.value(), targetFlags, currentScope_,
@@ -286,19 +289,23 @@ class ParseTreeAnalyzer {
   bool Pre(const parser::CaseConstruct &caseConstruct) {
     return PushConstructName(caseConstruct);
   }
+  void Post(const parser::SelectCaseStmt &) { PushScope(); }
   bool Pre(const parser::CaseConstruct::Case &) { return SwitchToNewScope(); }
   bool Pre(const parser::SelectRankConstruct &selectRankConstruct) {
     return PushConstructName(selectRankConstruct);
   }
+  void Post(const parser::SelectRankStmt &) { PushScope(); }
   bool Pre(const parser::SelectRankConstruct::RankCase &) {
     return SwitchToNewScope();
   }
   bool Pre(const parser::SelectTypeConstruct &selectTypeConstruct) {
     return PushConstructName(selectTypeConstruct);
   }
+  void Post(const parser::SelectTypeStmt &) { PushScope(); }
   bool Pre(const parser::SelectTypeConstruct::TypeCase &) {
     return SwitchToNewScope();
   }
+  void Post(const parser::EndSelectStmt &) { PopScope(); }
   bool Pre(const parser::WhereConstruct &whereConstruct) {
     return PushConstructName(whereConstruct);
   }
@@ -994,7 +1001,7 @@ void CheckScopeConstraints(const SourceStmtList &stmts,
       context.Say(position,
           isFatal
               ? "Label '%u' is in a construct that prevents its use as a branch target here"_err_en_US
-              : "Label '%u' is in a construct that prevents its use as a branch target here"_en_US,
+              : "Label '%u' is in a construct that should not be used as a branch target here"_warn_en_US,
           SayLabel(label));
     }
   }

diff  --git a/flang/test/Semantics/label05.f90 b/flang/test/Semantics/label05.f90
index 51fd90263911c..944bb438f28d6 100644
--- a/flang/test/Semantics/label05.f90
+++ b/flang/test/Semantics/label05.f90
@@ -1,7 +1,6 @@
 ! RUN: not %flang_fc1 -fdebug-unparse-with-symbols %s 2>&1 | FileCheck %s
 ! CHECK: Label '50' was not found
-! CHECK-NOT: error: Label '55' is in a construct that prevents its use as a branch target here
-! CHECK: Label '55' is in a construct that prevents its use as a branch target here
+! CHECK: warning: Label '55' is in a construct that should not be used as a branch target here
 ! CHECK: Label '70' is not a branch target
 ! CHECK: Control flow use of '70'
 ! CHECK: error: Label '80' is in a construct that prevents its use as a branch target here

diff  --git a/flang/test/Semantics/label06.f90 b/flang/test/Semantics/label06.f90
index 1bccdf19fcbeb..d3674d6dfc0a3 100644
--- a/flang/test/Semantics/label06.f90
+++ b/flang/test/Semantics/label06.f90
@@ -1,10 +1,10 @@
 ! RUN: not %flang_fc1 -fdebug-unparse-with-symbols %s 2>&1 | FileCheck %s
-! CHECK: Label '10' is in a construct that prevents its use as a branch target here
+! CHECK: warning: Label '10' is in a construct that should not be used as a branch target here
 ! CHECK: Label '20' was not found
 ! CHECK: Label '30' is not a branch target
 ! CHECK: Control flow use of '30'
-! CHECK: Label '40' is in a construct that prevents its use as a branch target here
-! CHECK: Label '50' is in a construct that prevents its use as a branch target here
+! CHECK: warning: Label '40' is in a construct that should not be used as a branch target here
+! CHECK: warning: Label '50' is in a construct that should not be used as a branch target here
 
 subroutine sub00(n)
   GOTO (10,20,30) n

diff  --git a/flang/test/Semantics/label07.f90 b/flang/test/Semantics/label07.f90
index 3f9af80a826bb..ae80969716a2b 100644
--- a/flang/test/Semantics/label07.f90
+++ b/flang/test/Semantics/label07.f90
@@ -1,7 +1,7 @@
 ! RUN: not %flang_fc1 -fdebug-unparse-with-symbols %s 2>&1 | FileCheck %s
 ! CHECK: Label '30' is not a branch target
 ! CHECK: Control flow use of '30'
-! CHECK: Label '10' is in a construct that prevents its use as a branch target here
+! CHECK: warning: Label '10' is in a construct that should not be used as a branch target here
 ! CHECK: Label '20' was not found
 ! CHECK: Label '60' was not found
 


        


More information about the flang-commits mailing list