Skip to content

Commit

Permalink
Replaced manual mmap with llvm::MemoryBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidTruby committed Feb 27, 2020
1 parent 7649e5d commit 4430289
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 214 deletions.
4 changes: 2 additions & 2 deletions include/flang/Parser/provenance.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ class AllSources {

void PushSearchPathDirectory(std::string);
std::string PopSearchPathDirectory();
const SourceFile *Open(std::string path, std::stringstream *error);
const SourceFile *ReadStandardInput(std::stringstream *error);
const SourceFile *Open(std::string path, llvm::raw_ostream &error);
const SourceFile *ReadStandardInput(llvm::raw_ostream &error);

ProvenanceRange AddIncludedFile(
const SourceFile &, ProvenanceRange, bool isModule = false);
Expand Down
23 changes: 10 additions & 13 deletions include/flang/Parser/source.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string>
#include <utility>
#include <vector>
#include "llvm/Support/MemoryBuffer.h"

namespace Fortran::parser {

Expand All @@ -39,34 +40,30 @@ class SourceFile {
explicit SourceFile(Encoding e) : encoding_{e} {}
~SourceFile();
std::string path() const { return path_; }
const char *content() const { return content_; }
std::size_t bytes() const { return bytes_; }
llvm::ArrayRef<char> content() const { return buf_->getBuffer().slice(bom_end_, buf_end_ - bom_end_); }
std::size_t bytes() const { return content().size(); }
std::size_t lines() const { return lineStart_.size(); }
Encoding encoding() const { return encoding_; }

bool Open(std::string path, std::stringstream *error);
bool ReadStandardInput(std::stringstream *error);
bool Open(std::string path, llvm::raw_ostream &error);
bool ReadStandardInput(llvm::raw_ostream &error);
void Close();
SourcePosition FindOffsetLineAndColumn(std::size_t) const;
std::size_t GetLineStartOffset(int lineNumber) const {
return lineStart_.at(lineNumber - 1);
}

private:
bool ReadFile(std::string errorPath, std::stringstream *error);
void ReadFile();
void IdentifyPayload();
void RecordLineStarts();

std::string path_;
int fileDescriptor_{-1};
bool isMemoryMapped_{false};
const char *address_{nullptr}; // raw content
std::size_t size_{0};
const char *content_{nullptr}; // usable content
std::size_t bytes_{0};
std::unique_ptr<llvm::WritableMemoryBuffer> buf_;
std::vector<std::size_t> lineStart_;
std::string normalized_;
Encoding encoding_{Encoding::UTF_8};
std::size_t bom_end_ {0};
std::size_t buf_end_;
Encoding encoding_;
};
}
#endif // FORTRAN_PARSER_SOURCE_H_
2 changes: 1 addition & 1 deletion lib/Parser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ add_library(FortranParser
)

target_link_libraries(FortranParser
FortranCommon
FortranCommon LLVMSupport
)

install (TARGETS FortranParser
Expand Down
7 changes: 4 additions & 3 deletions lib/Parser/parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
}
}

std::stringstream fileError;
std::string fileError_buf;
llvm::raw_string_ostream fileError{fileError_buf};
const SourceFile *sourceFile;
if (path == "-") {
sourceFile = allSources.ReadStandardInput(&fileError);
sourceFile = allSources.ReadStandardInput(fileError);
} else {
sourceFile = allSources.Open(path, &fileError);
sourceFile = allSources.Open(path, fileError);
}
if (!fileError.str().empty()) {
ProvenanceRange range{allSources.AddCompilerInsertion(path)};
Expand Down
5 changes: 3 additions & 2 deletions lib/Parser/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,9 @@ void Preprocessor::Directive(const TokenSequence &dir, Prescanner *prescanner) {
"#include: empty include file name"_err_en_US);
return;
}
std::stringstream error;
const SourceFile *included{allSources_.Open(include, &error)};
std::string error_buf;
llvm::raw_string_ostream error{error_buf};
const SourceFile *included{allSources_.Open(include, error)};
if (!included) {
prescanner->Say(dir.GetTokenProvenanceRange(dirOffset),
"#include: %s"_err_en_US, error.str());
Expand Down
7 changes: 4 additions & 3 deletions lib/Parser/prescan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void Prescanner::Prescan(ProvenanceRange range) {
std::size_t offset{0};
const SourceFile *source{allSources.GetSourceFile(startProvenance_, &offset)};
CHECK(source);
start_ = source->content() + offset;
start_ = source->content().data() + offset;
limit_ = start_ + range.size();
nextLine_ = start_;
const bool beganInFixedForm{inFixedForm_};
Expand Down Expand Up @@ -736,14 +736,15 @@ void Prescanner::FortranInclude(const char *firstQuote) {
Say(GetProvenanceRange(garbage, p),
"excess characters after path name"_en_US);
}
std::stringstream error;
std::string error_buf;
llvm::raw_string_ostream error{error_buf};
Provenance provenance{GetProvenance(nextLine_)};
AllSources &allSources{cooked_.allSources()};
const SourceFile *currentFile{allSources.GetSourceFile(provenance)};
if (currentFile) {
allSources.PushSearchPathDirectory(DirectoryName(currentFile->path()));
}
const SourceFile *included{allSources.Open(path, &error)};
const SourceFile *included{allSources.Open(path, error)};
if (currentFile) {
allSources.PopSearchPathDirectory();
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Parser/provenance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ std::string AllSources::PopSearchPathDirectory() {
return directory;
}

const SourceFile *AllSources::Open(std::string path, std::stringstream *error) {
const SourceFile *AllSources::Open(std::string path, llvm::raw_ostream &error) {
std::unique_ptr<SourceFile> source{std::make_unique<SourceFile>(encoding_)};
if (source->Open(LocateSourceFile(path, searchPath_), error)) {
return ownedSourceFiles_.emplace_back(std::move(source)).get();
Expand All @@ -175,7 +175,7 @@ const SourceFile *AllSources::Open(std::string path, std::stringstream *error) {
}
}

const SourceFile *AllSources::ReadStandardInput(std::stringstream *error) {
const SourceFile *AllSources::ReadStandardInput(llvm::raw_ostream &error) {
std::unique_ptr<SourceFile> source{std::make_unique<SourceFile>(encoding_)};
if (source->ReadStandardInput(error)) {
return ownedSourceFiles_.emplace_back(std::move(source)).get();
Expand Down Expand Up @@ -227,7 +227,7 @@ void AllSources::EmitMessage(std::ostream &o,
o << ':' << pos.line << ':' << pos.column;
o << ": " << message << '\n';
if (echoSourceLine) {
const char *text{inc.source.content() +
const char *text{inc.source.content().data() +
inc.source.GetLineStartOffset(pos.line)};
o << " ";
for (const char *p{text}; *p != '\n'; ++p) {
Expand Down
Loading

0 comments on commit 4430289

Please sign in to comment.