[flang-commits] [flang] dc0d56f - [flang] Warn about local names that are the same as their enclosing program unit
Peter Klausler via flang-commits
flang-commits at lists.llvm.org
Sat Dec 3 17:52:00 PST 2022
Author: Peter Klausler
Date: 2022-12-03T17:47:35-08:00
New Revision: dc0d56febba37ba672c1800be70a3ff5a42f4769
URL: https://github.com/llvm/llvm-project/commit/dc0d56febba37ba672c1800be70a3ff5a42f4769
DIFF: https://github.com/llvm/llvm-project/commit/dc0d56febba37ba672c1800be70a3ff5a42f4769.diff
LOG: [flang] Warn about local names that are the same as their enclosing program unit
Modules, submodules, main programs, and BLOCK DATA subprograms have names
that cannot be used within their scope, so we allow those names to be
used for other entities in the scope. This might not be entirely
conformant with the language standard, so warn about it.
Differential Revision: https://reviews.llvm.org/D139146
Added:
Modified:
flang/docs/Extensions.md
flang/lib/Semantics/check-declarations.cpp
flang/test/Semantics/OpenMP/omp-declare-target03.f90
flang/test/Semantics/OpenMP/omp-threadprivate03.f90
flang/test/Semantics/bind-c02.f90
flang/test/Semantics/resolve05.f90
flang/test/Semantics/resolve20.f90
Removed:
################################################################################
diff --git a/flang/docs/Extensions.md b/flang/docs/Extensions.md
index eea4249e9034..69855bfb1c29 100644
--- a/flang/docs/Extensions.md
+++ b/flang/docs/Extensions.md
@@ -513,3 +513,10 @@ end module
application codes that expect exterior symbols whose names match
components to be visible in a derived-type definition's default initialization
expressions, and so f18 follows that precedent.
+
+* 19.3.1p1 "Within its scope, a local identifier of an entity of class (1)
+ or class (4) shall not be the same as a global identifier used in that scope..."
+ is read so as to allow the name of a module, submodule, main program,
+ or `BLOCK DATA` subprogram to also be the name of an local entity in its
+ scope, with a portability warning, since that global name is not actually
+ capable of being "used" in its scope.
diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index 4f6109e399b5..4ae4eae6c1cc 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -1857,6 +1857,31 @@ void CheckHelper::Check(const Scope &scope) {
if (scope.kind() == Scope::Kind::BlockData) {
CheckBlockData(scope);
}
+ if (auto name{scope.GetName()}) {
+ auto iter{scope.find(*name)};
+ if (iter != scope.end()) {
+ const char *kind{nullptr};
+ switch (scope.kind()) {
+ case Scope::Kind::Module:
+ kind = scope.symbol()->get<ModuleDetails>().isSubmodule()
+ ? "submodule"
+ : "module";
+ break;
+ case Scope::Kind::MainProgram:
+ kind = "main program";
+ break;
+ case Scope::Kind::BlockData:
+ kind = "BLOCK DATA subprogram";
+ break;
+ default:;
+ }
+ if (kind) {
+ messages_.Say(iter->second->name(),
+ "Name '%s' declared in a %s should not have the same name as the %s"_port_en_US,
+ *name, kind, kind);
+ }
+ }
+ }
CheckGenericOps(scope);
}
}
diff --git a/flang/test/Semantics/OpenMP/omp-declare-target03.f90 b/flang/test/Semantics/OpenMP/omp-declare-target03.f90
index 45064ff2ea25..c437a3e7bccc 100644
--- a/flang/test/Semantics/OpenMP/omp-declare-target03.f90
+++ b/flang/test/Semantics/OpenMP/omp-declare-target03.f90
@@ -12,6 +12,7 @@ program main
!ERROR: The module name or main program name cannot be in a DECLARE TARGET directive
!$omp declare target (mod1)
+ !PORTABILITY: Name 'main' declared in a main program should not have the same name as the main program
!ERROR: The module name or main program name cannot be in a DECLARE TARGET directive
!$omp declare target (main)
end
diff --git a/flang/test/Semantics/OpenMP/omp-threadprivate03.f90 b/flang/test/Semantics/OpenMP/omp-threadprivate03.f90
index d5ce4a9f8d79..2a59a96ec1cf 100644
--- a/flang/test/Semantics/OpenMP/omp-threadprivate03.f90
+++ b/flang/test/Semantics/OpenMP/omp-threadprivate03.f90
@@ -13,6 +13,7 @@ program main
!ERROR: The module name or main program name cannot be in a THREADPRIVATE directive
!$omp threadprivate(mod1)
+ !PORTABILITY: Name 'main' declared in a main program should not have the same name as the main program
!ERROR: The module name or main program name cannot be in a THREADPRIVATE directive
!$omp threadprivate(main)
diff --git a/flang/test/Semantics/bind-c02.f90 b/flang/test/Semantics/bind-c02.f90
index c207e2a136df..18b909425090 100644
--- a/flang/test/Semantics/bind-c02.f90
+++ b/flang/test/Semantics/bind-c02.f90
@@ -18,6 +18,7 @@ subroutine proc() bind(c)
!ERROR: Only variable and named common block can be in BIND statement
bind(c) :: sub
+ !PORTABILITY: Name 'm' declared in a module should not have the same name as the module
bind(c) :: m ! no error for implicit type variable
type my_type
diff --git a/flang/test/Semantics/resolve05.f90 b/flang/test/Semantics/resolve05.f90
index 5c045aa99a59..736babf07808 100644
--- a/flang/test/Semantics/resolve05.f90
+++ b/flang/test/Semantics/resolve05.f90
@@ -1,12 +1,20 @@
! RUN: %python %S/test_errors.py %s %flang_fc1
program p
- integer :: p ! this is ok
+ !PORTABILITY: Name 'p' declared in a main program should not have the same name as the main program
+ integer :: p
end
module m
- integer :: m ! this is ok
+ !PORTABILITY: Name 'm' declared in a module should not have the same name as the module
+ integer :: m
end
submodule(m) sm
- integer :: sm ! this is ok
+ !PORTABILITY: Name 'sm' declared in a submodule should not have the same name as the submodule
+ integer :: sm
+end
+block data bd
+ !PORTABILITY: Name 'bd' declared in a BLOCK DATA subprogram should not have the same name as the BLOCK DATA subprogram
+ type bd
+ end type
end
module m2
type :: t
diff --git a/flang/test/Semantics/resolve20.f90 b/flang/test/Semantics/resolve20.f90
index be6fe968fbe6..b1d73341b57b 100644
--- a/flang/test/Semantics/resolve20.f90
+++ b/flang/test/Semantics/resolve20.f90
@@ -37,7 +37,8 @@ subroutine forward
type :: bad3
end type
- type :: m ! the name of a module can be used as a local identifier
+ !PORTABILITY: Name 'm' declared in a module should not have the same name as the module
+ type :: m
end type m
!ERROR: EXTERNAL attribute was already specified on 'a'
More information about the flang-commits
mailing list