From 86132a15dc5dd551b48f5abc12e57f4324475701 Mon Sep 17 00:00:00 2001 From: Tim Keith Date: Mon, 24 Jun 2019 12:35:17 -0700 Subject: [PATCH] [flang] Fix bug in writing PRIVATE subprograms to .mod file Subprograms can't have the PRIVATE prefix on them when they are defined. So if they are private, add a private-stmt for them. Fixes flang-compiler/f18#519 Original-commit: flang-compiler/f18@d3670aa176cbc79a7d26fc927e9d1e1349900756 Reviewed-on: https://github.com/flang-compiler/f18/pull/523 --- flang/lib/semantics/mod-file.cc | 14 ++++++++-- flang/test/semantics/modfile01.f90 | 45 +++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/flang/lib/semantics/mod-file.cc b/flang/lib/semantics/mod-file.cc index 19afac34e045..a50d2cea7b20 100644 --- a/flang/lib/semantics/mod-file.cc +++ b/flang/lib/semantics/mod-file.cc @@ -52,6 +52,7 @@ static void PutBound(std::ostream &, const Bound &); static std::ostream &PutAttrs(std::ostream &, Attrs, const MaybeExpr & = std::nullopt, std::string before = ","s, std::string after = ""s); +static std::ostream &PutAttr(std::ostream &, Attr); static std::ostream &PutLower(std::ostream &, const Symbol &); static std::ostream &PutLower(std::ostream &, const DeclTypeSpec &); static std::ostream &PutLower(std::ostream &, const std::string &); @@ -284,6 +285,11 @@ void ModFileWriter::PutSubprogram(const Symbol &symbol) { bindAttrs.set(Attr::BIND_C, true); attrs.set(Attr::BIND_C, false); } + if (attrs.test(Attr::PRIVATE)) { + PutAttr(decls_, Attr::PRIVATE) << "::"; + PutLower(decls_, symbol) << "\n"; + attrs.set(Attr::PRIVATE, false); + } bool isInterface{details.isInterface()}; std::ostream &os{isInterface ? decls_ : contains_}; if (isInterface) { @@ -365,7 +371,7 @@ void ModFileWriter::PutUse(const Symbol &symbol) { void ModFileWriter::PutUseExtraAttr( Attr attr, const Symbol &local, const Symbol &use) { if (local.attrs().test(attr) && !use.attrs().test(attr)) { - PutLower(useExtraAttrs_, AttrToString(attr)) << "::"; + PutAttr(useExtraAttrs_, attr) << "::"; PutLower(useExtraAttrs_, local) << '\n'; } } @@ -552,12 +558,16 @@ std::ostream &PutAttrs(std::ostream &os, Attrs attrs, const MaybeExpr &bindName, for (std::size_t i{0}; i < Attr_enumSize; ++i) { Attr attr{static_cast(i)}; if (attrs.test(attr)) { - PutLower(os << before, AttrToString(attr)) << after; + PutAttr(os << before, attr) << after; } } return os; } +std::ostream &PutAttr(std::ostream &os, Attr attr) { + return PutLower(os, AttrToString(attr)); +} + std::ostream &PutLower(std::ostream &os, const Symbol &symbol) { return PutLower(os, symbol.name().ToString()); } diff --git a/flang/test/semantics/modfile01.f90 b/flang/test/semantics/modfile01.f90 index 56b8f80ab644..8de9f1b90686 100644 --- a/flang/test/semantics/modfile01.f90 +++ b/flang/test/semantics/modfile01.f90 @@ -1,4 +1,4 @@ -! Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. +! Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved. ! ! Licensed under the Apache License, Version 2.0 (the "License"); ! you may not use this file except in compliance with the License. @@ -37,3 +37,46 @@ end !end type !type(t)::x !end + +! Check correct modfile generation for type with private module procedure. + +module m2 + private :: s1 +contains + subroutine s1() + end + subroutine s2() + end +end + +!Expect: m2.mod +!module m2 +! private::s1 +!contains +! subroutine s1() +! end +! subroutine s2() +! end +!end + +module m3 + private + public :: f1 +contains + real function f1() + end + real function f2() + end +end + +!Expect: m3.mod +!module m3 +! private::f2 +!contains +! function f1() +! real(4)::f1 +! end +! function f2() +! real(4)::f2 +! end +!end