[flang-commits] [flang] b1e1090 - [flang] Downgrade an error to a warning for specific circumstances (#155675)
via flang-commits
flang-commits at lists.llvm.org
Fri Aug 29 07:49:37 PDT 2025
Author: Peter Klausler
Date: 2025-08-29T07:49:33-07:00
New Revision: b1e109064cf53621408662b2cebfbd70bbcd501b
URL: https://github.com/llvm/llvm-project/commit/b1e109064cf53621408662b2cebfbd70bbcd501b
DIFF: https://github.com/llvm/llvm-project/commit/b1e109064cf53621408662b2cebfbd70bbcd501b.diff
LOG: [flang] Downgrade an error to a warning for specific circumstances (#155675)
We emit an error on the component name in the structure constructor
"__builtin_c_ptr(__address=0)", which is the value of "c_ptr_null()",
because the component name "__address" is PRIVATE to an intrinsic
module. The error is specifically omitted, however, when the name
appears in a module file, since it's what we emit for "c_ptr_null()" in
initializers.
This patch carves out another exception -- downgrading the error to a
warning -- for the case of a PRIVATE component name in a structure
constructor from an intrinsic module when the structure constructor
appears in a module. This case arises when module files are being
reprocessed as Fortran source in order to convert them to hermetic
module files.
Added:
Modified:
flang/include/flang/Semantics/tools.h
flang/lib/Semantics/expression.cpp
flang/lib/Semantics/tools.cpp
flang/test/Semantics/c_loc01.f90
Removed:
################################################################################
diff --git a/flang/include/flang/Semantics/tools.h b/flang/include/flang/Semantics/tools.h
index 966a30f7081fd..cb1def32dfe0c 100644
--- a/flang/include/flang/Semantics/tools.h
+++ b/flang/include/flang/Semantics/tools.h
@@ -261,7 +261,7 @@ bool IsAccessible(const Symbol &, const Scope &);
// Return an error if a symbol is not accessible from a scope
std::optional<parser::MessageFormattedText> CheckAccessibleSymbol(
- const Scope &, const Symbol &);
+ const Scope &, const Symbol &, bool inStructureConstructor = false);
// Analysis of image control statements
bool IsImageControlStmt(const parser::ExecutableConstruct &);
diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index ea587b6831b75..ccccf60adae5d 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -2198,7 +2198,8 @@ MaybeExpr ExpressionAnalyzer::CheckStructureConstructor(
}
if (symbol) {
const semantics::Scope &innermost{context_.FindScope(exprSource)};
- if (auto msg{CheckAccessibleSymbol(innermost, *symbol)}) {
+ if (auto msg{CheckAccessibleSymbol(
+ innermost, *symbol, /*inStructureConstructor=*/true)}) {
Say(exprSource, std::move(*msg));
}
if (checkConflicts) {
diff --git a/flang/lib/Semantics/tools.cpp b/flang/lib/Semantics/tools.cpp
index e9b1d0d7795e1..28829d3eda308 100644
--- a/flang/lib/Semantics/tools.cpp
+++ b/flang/lib/Semantics/tools.cpp
@@ -1170,7 +1170,7 @@ bool IsAccessible(const Symbol &original, const Scope &scope) {
}
std::optional<parser::MessageFormattedText> CheckAccessibleSymbol(
- const Scope &scope, const Symbol &symbol) {
+ const Scope &scope, const Symbol &symbol, bool inStructureConstructor) {
if (IsAccessible(symbol, scope)) {
return std::nullopt;
} else if (FindModuleFileContaining(scope)) {
@@ -1179,10 +1179,20 @@ std::optional<parser::MessageFormattedText> CheckAccessibleSymbol(
// whose structure constructors reference private components.
return std::nullopt;
} else {
+ const Scope &module{DEREF(FindModuleContaining(symbol.owner()))};
+ // Subtlety: Sometimes we want to be able to convert a generated
+ // module file back into Fortran, perhaps to convert it into a
+ // hermetic module file. Don't emit a fatal error for things like
+ // "__builtin_c_ptr(__address=0)" that came from expansions of
+ // "cptr_null()"; specifically, just warn about structure constructor
+ // component names from intrinsic modules when in a module.
+ parser::MessageFixedText text{FindModuleContaining(scope) &&
+ module.parent().IsIntrinsicModules() &&
+ inStructureConstructor && symbol.owner().IsDerivedType()
+ ? "PRIVATE name '%s' is accessible only within module '%s'"_warn_en_US
+ : "PRIVATE name '%s' is accessible only within module '%s'"_err_en_US};
return parser::MessageFormattedText{
- "PRIVATE name '%s' is accessible only within module '%s'"_err_en_US,
- symbol.name(),
- DEREF(FindModuleContaining(symbol.owner())).GetName().value()};
+ std::move(text), symbol.name(), module.GetName().value()};
}
}
diff --git a/flang/test/Semantics/c_loc01.f90 b/flang/test/Semantics/c_loc01.f90
index da8a0e5bdd9e1..16f5618b6330f 100644
--- a/flang/test/Semantics/c_loc01.f90
+++ b/flang/test/Semantics/c_loc01.f90
@@ -66,3 +66,12 @@ pure integer function purefun2(p)
purefun2 = 1
end
end module
+
+module m2
+ use iso_c_binding
+ ! In this context (structure constructor from intrinsic module being used directly
+ ! in another module), emit only a warning, since this module might have originally
+ ! been a module file that was converted back into Fortran.
+ !WARNING: PRIVATE name '__address' is accessible only within module '__fortran_builtins'
+ type(c_ptr) :: p = c_ptr(0)
+end
More information about the flang-commits
mailing list