Skip to content

Commit 46387cd

Browse files
authored
[flang] Compile the output of -fdebug-unparse-with-modules (#135696)
The output of a compilation with the -fdebug-unparse-with-modules option comprises its normal unparsed output along with the regenerated contents of any modules that were required from module files. This is handy for producing stand-alone test cases. The modules' contents are generated by the same code that writes module files, so they can contain some USE associations to private entities in other modules that are necessary to complete local declarations, usually initializers. Such USE associations to private entities are not flagged as fatal errors when modules are read from module files, but they currently are caught when the output produced by this option is being read back in to the compiler. Handle this case by softening the error to a warning when one module uses a private entity from another with an alias containing the non-conforming '$' character. (I could have omitted the message altogether, but there are other valid warnings that will occur due to undefined function result variables; further, I didn't want to provide a general hole around the protection of private names.)
1 parent 21a406c commit 46387cd

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

flang/docs/ModFiles.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,14 @@ Entities that have been included in a module by means of USE association
9898
are represented in the module file with `USE` statements.
9999
Name aliases are sometimes necessary when an entity from another
100100
module is needed for a declaration and conflicts with another
101-
entity of the same name.
101+
entity of the same name, or is `PRIVATE`.
102+
These aliases have currency symbols (`$`) in them.
103+
When a module
104+
is parsed from a module file, no error is emitted for associating
105+
such an alias with a `PRIVATE` name.
106+
A module parsed from another source file that is not a module file
107+
(notably, the output of the `-fdebug-unparse-with-modules` option)
108+
will emit only warnings.
102109

103110
## Reading and writing module files
104111

flang/lib/Semantics/resolve-names.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3325,9 +3325,19 @@ ModuleVisitor::SymbolRename ModuleVisitor::AddUse(
33253325
// Privacy is not enforced in module files so that generic interfaces
33263326
// can be resolved to private specific procedures in specification
33273327
// expressions.
3328-
Say(useName, "'%s' is PRIVATE in '%s'"_err_en_US, MakeOpName(useName),
3329-
useModuleScope_->GetName().value());
3330-
return {};
3328+
// Local names that contain currency symbols ('$') are created by the
3329+
// module file writer when a private name in another module is needed to
3330+
// process a local declaration. These can show up in the output of
3331+
// -fdebug-unparse-with-modules, too, so go easy on them.
3332+
if (currScope().IsModule() &&
3333+
localName.ToString().find("$") != std::string::npos) {
3334+
Say(useName, "'%s' is PRIVATE in '%s'"_warn_en_US, MakeOpName(useName),
3335+
useModuleScope_->GetName().value());
3336+
} else {
3337+
Say(useName, "'%s' is PRIVATE in '%s'"_err_en_US, MakeOpName(useName),
3338+
useModuleScope_->GetName().value());
3339+
return {};
3340+
}
33313341
}
33323342
auto &localSymbol{MakeSymbol(localName)};
33333343
DoAddUse(useName, localName, localSymbol, *useSymbol);

flang/test/Semantics/resolve11.f90

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,17 @@ subroutine s5
8686
use m5, only: foo, fun
8787
print *, fun() ! ok
8888
end
89+
90+
module m6
91+
!WARNING: 'foo' is PRIVATE in 'm5'
92+
use m5, only: name$with$dollar => foo
93+
!ERROR: 'foo' is PRIVATE in 'm5'
94+
use m5, only: normal_name => foo
95+
end
96+
97+
subroutine s6
98+
!The special dispensation for USE association of private names to local
99+
!aliases with '$' in them only applies to modules.
100+
!ERROR: 'foo' is PRIVATE in 'm5'
101+
use m5, only: name$with$dollar => foo
102+
end

0 commit comments

Comments
 (0)