Skip to content

Commit

Permalink
[flang] Be more persistent in search for non-intrinsic module file
Browse files Browse the repository at this point in the history
When a particular module name has been brought into a compilation
as an intrinsic module via USE,INTRINSIC, perhaps indirectly via
an enclosing USE statement, f18 will resolve later USE statements
to it unless they are USE,NON_INTRINSIC.  This is not entirely
correct -- a bare USE statement for a module name that happens to
match that of an intrinsic module should still search the search
paths for a non-intrinsic module file of the same name.

Differential Revision: https://reviews.llvm.org/D132163
  • Loading branch information
klausler committed Aug 18, 2022
1 parent c1a7783 commit dcbfabb
Showing 1 changed file with 37 additions and 22 deletions.
59 changes: 37 additions & 22 deletions flang/lib/Semantics/mod-file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -928,49 +928,64 @@ Scope *ModFileReader::Read(const SourceName &name,
return scope;
}
ancestorName = ancestor->GetName().value().ToString();
} else {
if (!isIntrinsic.value_or(false)) {
auto it{context_.globalScope().find(name)};
if (it != context_.globalScope().end()) {
Scope *scope{it->second->scope()};
if (scope->kind() == Scope::Kind::Module) {
return scope;
} else {
notAModule = scope->symbol();
// USE, NON_INTRINSIC global name isn't a module?
fatalError = isIntrinsic.has_value();
}
}
}
if (isIntrinsic.value_or(true)) {
auto it{context_.intrinsicModulesScope().find(name)};
if (it != context_.intrinsicModulesScope().end()) {
return it->second->scope();
}
if (!isIntrinsic.value_or(false) && !ancestor) {
// Already present in the symbol table as a usable non-intrinsic module?
auto it{context_.globalScope().find(name)};
if (it != context_.globalScope().end()) {
Scope *scope{it->second->scope()};
if (scope->kind() == Scope::Kind::Module) {
return scope;
} else {
notAModule = scope->symbol();
// USE, NON_INTRINSIC global name isn't a module?
fatalError = isIntrinsic.has_value();
}
}
}
auto path{ModFileName(name, ancestorName, context_.moduleFileSuffix())};
parser::Parsing parsing{context_.allCookedSources()};
parser::Options options;
options.isModuleFile = true;
options.features.Enable(common::LanguageFeature::BackslashEscapes);
options.features.Enable(common::LanguageFeature::OpenMP);
if (!isIntrinsic.value_or(false) && !notAModule) {
// Scan non-intrinsic module directories
// The search for this module file will scan non-intrinsic module
// directories. If a directory is in both the intrinsic and non-intrinsic
// directory lists, the intrinsic module directory takes precedence.
options.searchDirectories = context_.searchDirectories();
// If a directory is in both lists, the intrinsic module directory
// takes precedence.
for (const auto &dir : context_.intrinsicModuleDirectories()) {
std::remove(options.searchDirectories.begin(),
options.searchDirectories.end(), dir);
}
options.searchDirectories.insert(options.searchDirectories.begin(), "."s);
}
bool foundNonIntrinsicModuleFile{false};
if (!isIntrinsic) {
std::list<std::string> searchDirs;
for (const auto &d : options.searchDirectories) {
searchDirs.push_back(d);
}
foundNonIntrinsicModuleFile =
parser::LocateSourceFile(path, searchDirs).has_value();
}
if (isIntrinsic.value_or(!foundNonIntrinsicModuleFile)) {
// Explicitly intrinsic, or not specified and not found in the search
// path; see whether it's already in the symbol table as an intrinsic
// module.
auto it{context_.intrinsicModulesScope().find(name)};
if (it != context_.intrinsicModulesScope().end()) {
return it->second->scope();
}
}
// We don't have this module in the symbol table yet.
// Find its module file and parse it. Define or extend the search
// path with intrinsic module directories, if appropriate.
if (isIntrinsic.value_or(true)) {
for (const auto &dir : context_.intrinsicModuleDirectories()) {
options.searchDirectories.push_back(dir);
}
}
auto path{ModFileName(name, ancestorName, context_.moduleFileSuffix())};
const auto *sourceFile{fatalError ? nullptr : parsing.Prescan(path, options)};
if (fatalError || parsing.messages().AnyFatalError()) {
if (!silent) {
Expand Down

0 comments on commit dcbfabb

Please sign in to comment.