Skip to content

Commit 604a5bb

Browse files
banach-spacememfrob
authored andcommitted
Revert "[flang] Search for #include "file" in right directory"
This reverts commit d987b61b1dce9948801ac37704477e7c257100b1. As pointed out in https://reviews.llvm.org/D95388, the reverted commit causes build failures in the following Flang buildbots: * http://lab.llvm.org:8011/#/builders/32/builds/2642 * http://lab.llvm.org:8011/#/builders/33/builds/2131 * http://lab.llvm.org:8011/#/builders/135/builds/1473 * http://lab.llvm.org:8011/#/builders/66/builds/1559 * http://lab.llvm.org:8011/#/builders/134/builds/1409 * http://lab.llvm.org:8011/#/builders/132/builds/1817 I'm guessing that the patch was only tested with `FLANG_BUILD_NEW_DRIVER=Off` (i.e. the default). The builders listed above set `FLANG_BUILD_NEW_DRIVER` to `On`. Although fixing the build is relatively easy, the reverted patch modifies the behaviour of the frontend, which breaks driver tests. In particular, in https://reviews.llvm.org/D93453 support for `-I` was added that depends on the current behaviour. The reverted patch changes that behaviour. Either the tests have to be updated or the change fine-tuned.
1 parent 1c68397 commit 604a5bb

File tree

8 files changed

+34
-48
lines changed

8 files changed

+34
-48
lines changed

flang/include/flang/Parser/provenance.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ class AllSources {
148148
return *this;
149149
}
150150

151-
void AppendSearchPathDirectory(std::string); // new last directory
152-
const SourceFile *Open(std::string path, llvm::raw_ostream &error,
153-
const std::optional<std::string> &prependPath);
151+
void PushSearchPathDirectory(std::string);
152+
std::string PopSearchPathDirectory();
153+
const SourceFile *Open(std::string path, llvm::raw_ostream &error);
154154
const SourceFile *ReadStandardInput(llvm::raw_ostream &error);
155155

156156
ProvenanceRange AddIncludedFile(
@@ -210,7 +210,7 @@ class AllSources {
210210
ProvenanceRange range_;
211211
std::map<char, Provenance> compilerInsertionProvenance_;
212212
std::vector<std::unique_ptr<SourceFile>> ownedSourceFiles_;
213-
std::list<std::string> searchPath_;
213+
std::vector<std::string> searchPath_;
214214
Encoding encoding_{Encoding::UTF_8};
215215
};
216216

flang/include/flang/Parser/source.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
#include "characters.h"
1818
#include "llvm/Support/MemoryBuffer.h"
1919
#include <cstddef>
20-
#include <list>
21-
#include <optional>
2220
#include <string>
2321
#include <utility>
2422
#include <vector>
@@ -30,8 +28,8 @@ class raw_ostream;
3028
namespace Fortran::parser {
3129

3230
std::string DirectoryName(std::string path);
33-
std::optional<std::string> LocateSourceFile(
34-
std::string name, const std::list<std::string> &searchPath);
31+
std::string LocateSourceFile(
32+
std::string name, const std::vector<std::string> &searchPath);
3533

3634
class SourceFile;
3735

flang/lib/Parser/parsing.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
2525
AllSources &allSources{allCooked_.allSources()};
2626
if (options.isModuleFile) {
2727
for (const auto &path : options.searchDirectories) {
28-
allSources.AppendSearchPathDirectory(path);
28+
allSources.PushSearchPathDirectory(path);
2929
}
3030
}
3131

@@ -35,8 +35,7 @@ const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
3535
if (path == "-") {
3636
sourceFile = allSources.ReadStandardInput(fileError);
3737
} else {
38-
std::optional<std::string> currentDirectory{"."};
39-
sourceFile = allSources.Open(path, fileError, currentDirectory);
38+
sourceFile = allSources.Open(path, fileError);
4039
}
4140
if (!fileError.str().empty()) {
4241
ProvenanceRange range{allSources.AddCompilerInsertion(path)};
@@ -47,12 +46,12 @@ const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
4746

4847
if (!options.isModuleFile) {
4948
// For .mod files we always want to look in the search directories.
50-
// For normal source files we don't add them until after the primary
49+
// For normal source files we don't push them until after the primary
5150
// source file has been opened. If foo.f is missing from the current
5251
// working directory, we don't want to accidentally read another foo.f
5352
// from another directory that's on the search path.
5453
for (const auto &path : options.searchDirectories) {
55-
allSources.AppendSearchPathDirectory(path);
54+
allSources.PushSearchPathDirectory(path);
5655
}
5756
}
5857

flang/lib/Parser/preprocessor.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ void Preprocessor::Directive(const TokenSequence &dir, Prescanner *prescanner) {
399399
if (j == tokens) {
400400
return;
401401
}
402-
CHECK(prescanner); // TODO: change to reference
403402
if (dir.TokenAt(j).ToString() != "#") {
404403
prescanner->Say(dir.GetTokenProvenanceRange(j), "missing '#'"_err_en_US);
405404
return;
@@ -579,7 +578,6 @@ void Preprocessor::Directive(const TokenSequence &dir, Prescanner *prescanner) {
579578
return;
580579
}
581580
std::string include;
582-
std::optional<std::string> prependPath;
583581
if (dir.TokenAt(j).ToString() == "<") { // #include <foo>
584582
std::size_t k{j + 1};
585583
if (k >= tokens) {
@@ -600,12 +598,6 @@ void Preprocessor::Directive(const TokenSequence &dir, Prescanner *prescanner) {
600598
} else if ((include = dir.TokenAt(j).ToString()).substr(0, 1) == "\"" &&
601599
include.substr(include.size() - 1, 1) == "\"") { // #include "foo"
602600
include = include.substr(1, include.size() - 2);
603-
// #include "foo" starts search in directory of file containing
604-
// the directive
605-
auto prov{dir.GetTokenProvenanceRange(dirOffset).start()};
606-
if (const auto *currentFile{allSources_.GetSourceFile(prov)}) {
607-
prependPath = DirectoryName(currentFile->path());
608-
}
609601
} else {
610602
prescanner->Say(dir.GetTokenProvenanceRange(j < tokens ? j : tokens - 1),
611603
"#include: expected name of file to include"_err_en_US);
@@ -623,7 +615,7 @@ void Preprocessor::Directive(const TokenSequence &dir, Prescanner *prescanner) {
623615
}
624616
std::string buf;
625617
llvm::raw_string_ostream error{buf};
626-
const SourceFile *included{allSources_.Open(include, error, prependPath)};
618+
const SourceFile *included{allSources_.Open(include, error)};
627619
if (!included) {
628620
prescanner->Say(dir.GetTokenProvenanceRange(dirOffset),
629621
"#include: %s"_err_en_US, error.str());

flang/lib/Parser/prescan.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,11 +760,14 @@ void Prescanner::FortranInclude(const char *firstQuote) {
760760
std::string buf;
761761
llvm::raw_string_ostream error{buf};
762762
Provenance provenance{GetProvenance(nextLine_)};
763-
std::optional<std::string> prependPath;
764-
if (const SourceFile * currentFile{allSources_.GetSourceFile(provenance)}) {
765-
prependPath = DirectoryName(currentFile->path());
763+
const SourceFile *currentFile{allSources_.GetSourceFile(provenance)};
764+
if (currentFile) {
765+
allSources_.PushSearchPathDirectory(DirectoryName(currentFile->path()));
766+
}
767+
const SourceFile *included{allSources_.Open(path, error)};
768+
if (currentFile) {
769+
allSources_.PopSearchPathDirectory();
766770
}
767-
const SourceFile *included{allSources_.Open(path, error, prependPath)};
768771
if (!included) {
769772
Say(provenance, "INCLUDE: %s"_err_en_US, error.str());
770773
} else if (included->bytes() > 0) {

flang/lib/Parser/provenance.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -156,28 +156,20 @@ const char &AllSources::operator[](Provenance at) const {
156156
return origin[origin.covers.MemberOffset(at)];
157157
}
158158

159-
void AllSources::AppendSearchPathDirectory(std::string directory) {
159+
void AllSources::PushSearchPathDirectory(std::string directory) {
160160
// gfortran and ifort append to current path, PGI prepends
161161
searchPath_.push_back(directory);
162162
}
163163

164-
const SourceFile *AllSources::Open(std::string path, llvm::raw_ostream &error,
165-
const std::optional<std::string> &prependPath) {
164+
std::string AllSources::PopSearchPathDirectory() {
165+
std::string directory{searchPath_.back()};
166+
searchPath_.pop_back();
167+
return directory;
168+
}
169+
170+
const SourceFile *AllSources::Open(std::string path, llvm::raw_ostream &error) {
166171
std::unique_ptr<SourceFile> source{std::make_unique<SourceFile>(encoding_)};
167-
if (prependPath) {
168-
// Set to "." for the initial source file; set to the directory name
169-
// of the including file for #include "quoted-file" directives &
170-
// INCLUDE statements.
171-
searchPath_.push_front(*prependPath);
172-
}
173-
std::optional<std::string> found{LocateSourceFile(path, searchPath_)};
174-
if (prependPath) {
175-
searchPath_.pop_front();
176-
}
177-
if (!found) {
178-
error << "Source file '" << path << "' was not found";
179-
return nullptr;
180-
} else if (source->Open(*found, error)) {
172+
if (source->Open(LocateSourceFile(path, searchPath_), error)) {
181173
return ownedSourceFiles_.emplace_back(std::move(source)).get();
182174
} else {
183175
return nullptr;

flang/lib/Parser/source.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ std::string DirectoryName(std::string path) {
5656
return pathBuf.str().str();
5757
}
5858

59-
std::optional<std::string> LocateSourceFile(
60-
std::string name, const std::list<std::string> &searchPath) {
61-
if (name == "-" || llvm::sys::path::is_absolute(name)) {
59+
std::string LocateSourceFile(
60+
std::string name, const std::vector<std::string> &searchPath) {
61+
if (name.empty() || name == "-" || llvm::sys::path::is_absolute(name)) {
6262
return name;
6363
}
6464
for (const std::string &dir : searchPath) {
@@ -70,7 +70,7 @@ std::optional<std::string> LocateSourceFile(
7070
return path.str().str();
7171
}
7272
}
73-
return std::nullopt;
73+
return name;
7474
}
7575

7676
std::size_t RemoveCarriageReturns(llvm::MutableArrayRef<char> buf) {
@@ -123,6 +123,7 @@ bool SourceFile::Open(std::string path, llvm::raw_ostream &error) {
123123
bool SourceFile::ReadStandardInput(llvm::raw_ostream &error) {
124124
Close();
125125
path_ = "standard input";
126+
126127
auto buf_or = llvm::MemoryBuffer::getSTDIN();
127128
if (!buf_or) {
128129
auto err = buf_or.getError();
@@ -145,6 +146,7 @@ void SourceFile::ReadFile() {
145146
auto tmp_buf{llvm::WritableMemoryBuffer::getNewUninitMemBuffer(
146147
content().size() + 1)};
147148
llvm::copy(content(), tmp_buf->getBufferStart());
149+
Close();
148150
buf_ = std::move(tmp_buf);
149151
}
150152
buf_end_++;

flang/tools/f18/f18.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ struct DriverOptions {
8484
bool verbose{false}; // -v
8585
bool compileOnly{false}; // -c
8686
std::string outputPath; // -o path
87-
std::vector<std::string> searchDirectories; // -I dir
87+
std::vector<std::string> searchDirectories{"."s}; // -I dir
8888
std::string moduleDirectory{"."s}; // -module dir
8989
std::string moduleFileSuffix{".mod"}; // -moduleSuffix suff
9090
bool forcedForm{false}; // -Mfixed or -Mfree appeared

0 commit comments

Comments
 (0)