[flang-commits] [flang] 1dd8da2 - [flang] Fix semantics check for RETURN statement
Emil Kieri via flang-commits
flang-commits at lists.llvm.org
Mon May 2 13:06:30 PDT 2022
Author: Emil Kieri
Date: 2022-05-02T22:04:58+02:00
New Revision: 1dd8da2d7c75c2a34bf85a37a819755d60ddfb2b
URL: https://github.com/llvm/llvm-project/commit/1dd8da2d7c75c2a34bf85a37a819755d60ddfb2b
DIFF: https://github.com/llvm/llvm-project/commit/1dd8da2d7c75c2a34bf85a37a819755d60ddfb2b.diff
LOG: [flang] Fix semantics check for RETURN statement
The RETURN statement is allowed in functions and subroutines, but not
in main programs. It is however a common extension, which we also
implement, to allow RETURN from main programs -- we only issue a
portability warning when -pedantic or -std=f2018 are set.
This patch fixes false positives for this portability warning, where it
was triggered also when RETURN was present in functions or subroutines.
Fixexs #55080
Reviewed By: PeteSteinfeld
Differential Revision: https://reviews.llvm.org/D124732
Added:
flang/test/Semantics/altreturn07.f90
Modified:
flang/lib/Semantics/check-return.cpp
flang/test/Semantics/altreturn02.f90
Removed:
################################################################################
diff --git a/flang/lib/Semantics/check-return.cpp b/flang/lib/Semantics/check-return.cpp
index b28c82b37bb84..ec2600bac3c68 100644
--- a/flang/lib/Semantics/check-return.cpp
+++ b/flang/lib/Semantics/check-return.cpp
@@ -36,7 +36,8 @@ void ReturnStmtChecker::Leave(const parser::ReturnStmt &returnStmt) {
IsFunction(*subprogramScope->GetSymbol()))) {
context_.Say(
"RETURN with expression is only allowed in SUBROUTINE subprogram"_err_en_US);
- } else if (context_.ShouldWarn(common::LanguageFeature::ProgramReturn)) {
+ } else if (subprogramScope->kind() == Scope::Kind::MainProgram &&
+ context_.ShouldWarn(common::LanguageFeature::ProgramReturn)) {
context_.Say("RETURN should not appear in a main program"_port_en_US);
}
}
diff --git a/flang/test/Semantics/altreturn02.f90 b/flang/test/Semantics/altreturn02.f90
index a142871732198..f17d173ccbbd8 100644
--- a/flang/test/Semantics/altreturn02.f90
+++ b/flang/test/Semantics/altreturn02.f90
@@ -1,4 +1,4 @@
-! RUN: %python %S/test_errors.py %s %flang_fc1
+! RUN: %flang_fc1 -fsyntax-only -pedantic %s 2>&1 | FileCheck %s --allow-empty
! Check subroutine with alt return
SUBROUTINE TEST (N, *, *)
@@ -6,3 +6,5 @@ SUBROUTINE TEST (N, *, *)
IF ( N .EQ. 1 ) RETURN 1
RETURN 2
END
+! CHECK-NOT: error:
+! CHECK-NOT: portability:
diff --git a/flang/test/Semantics/altreturn07.f90 b/flang/test/Semantics/altreturn07.f90
new file mode 100644
index 0000000000000..b0cb60a98a328
--- /dev/null
+++ b/flang/test/Semantics/altreturn07.f90
@@ -0,0 +1,6 @@
+! RUN: %flang_fc1 -fsyntax-only -pedantic %s 2>&1 | FileCheck %s
+! Test extension: RETURN from main program
+
+return
+! CHECK: portability: RETURN should not appear in a main program
+end
More information about the flang-commits
mailing list