1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
//===-- lib/Parser/source.cpp ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "flang/Parser/source.h"
#include "flang/Common/idioms.h"
#include "flang/Parser/char-buffer.h"
#include "llvm/Support/Errno.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <memory>
#include <vector>
namespace Fortran::parser {
SourceFile::~SourceFile() { Close(); }
static std::vector<std::size_t> FindLineStarts(llvm::StringRef source) {
std::vector<std::size_t> result;
if (source.size() > 0) {
CHECK(source.back() == '\n' && "missing ultimate newline");
std::size_t at{0};
do {
result.push_back(at);
at = source.find('\n', at) + 1;
} while (at < source.size());
result.shrink_to_fit();
}
return result;
}
void SourceFile::RecordLineStarts() {
lineStart_ = FindLineStarts({content().data(), bytes()});
}
// Check for a Unicode byte order mark (BOM).
// Module files all have one; so can source files.
void SourceFile::IdentifyPayload() {
llvm::StringRef content{buf_->getBufferStart(), buf_->getBufferSize()};
constexpr llvm::StringLiteral UTF8_BOM{"\xef\xbb\xbf"};
if (content.startswith(UTF8_BOM)) {
bom_end_ = UTF8_BOM.size();
encoding_ = Encoding::UTF_8;
}
}
std::string DirectoryName(std::string path) {
llvm::SmallString<128> pathBuf{path};
llvm::sys::path::remove_filename(pathBuf);
return pathBuf.str().str();
}
std::optional<std::string> LocateSourceFile(
std::string name, const std::list<std::string> &searchPath) {
if (name == "-" || llvm::sys::path::is_absolute(name)) {
return name;
}
for (const std::string &dir : searchPath) {
llvm::SmallString<128> path{dir};
llvm::sys::path::append(path, name);
bool isDir{false};
auto er = llvm::sys::fs::is_directory(path, isDir);
if (!er && !isDir) {
return path.str().str();
}
}
return std::nullopt;
}
std::size_t RemoveCarriageReturns(llvm::MutableArrayRef<char> buf) {
std::size_t wrote{0};
char *buffer{buf.data()};
char *p{buf.data()};
std::size_t bytes = buf.size();
while (bytes > 0) {
void *vp{static_cast<void *>(p)};
void *crvp{std::memchr(vp, '\r', bytes)};
char *crcp{static_cast<char *>(crvp)};
if (!crcp) {
std::memmove(buffer + wrote, p, bytes);
wrote += bytes;
break;
}
std::size_t chunk = crcp - p;
auto advance{chunk + 1};
if (chunk + 1 >= bytes || crcp[1] == '\n') {
// CR followed by LF or EOF: omit
} else if ((chunk == 0 && p == buf.data()) || crcp[-1] == '\n') {
// CR preceded by LF or BOF: omit
} else {
// CR in line: retain
++chunk;
}
std::memmove(buffer + wrote, p, chunk);
wrote += chunk;
p += advance;
bytes -= advance;
}
return wrote;
}
bool SourceFile::Open(std::string path, llvm::raw_ostream &error) {
Close();
path_ = path;
std::string errorPath{"'"s + path_ + "'"};
auto bufOr{llvm::WritableMemoryBuffer::getFile(path)};
if (!bufOr) {
auto err = bufOr.getError();
error << "Could not open " << errorPath << ": " << err.message();
return false;
}
buf_ = std::move(bufOr.get());
ReadFile();
return true;
}
bool SourceFile::ReadStandardInput(llvm::raw_ostream &error) {
Close();
path_ = "standard input";
auto buf_or = llvm::MemoryBuffer::getSTDIN();
if (!buf_or) {
auto err = buf_or.getError();
error << err.message();
return false;
}
auto inbuf = std::move(buf_or.get());
buf_ =
llvm::WritableMemoryBuffer::getNewUninitMemBuffer(inbuf->getBufferSize());
llvm::copy(inbuf->getBuffer(), buf_->getBufferStart());
ReadFile();
return true;
}
void SourceFile::ReadFile() {
buf_end_ = RemoveCarriageReturns(buf_->getBuffer());
if (content().size() == 0 || content().back() != '\n') {
// Don't bother to copy if we have spare memory
if (content().size() >= buf_->getBufferSize()) {
auto tmp_buf{llvm::WritableMemoryBuffer::getNewUninitMemBuffer(
content().size() + 1)};
llvm::copy(content(), tmp_buf->getBufferStart());
buf_ = std::move(tmp_buf);
}
buf_end_++;
buf_->getBuffer()[buf_end_ - 1] = '\n';
}
IdentifyPayload();
RecordLineStarts();
}
void SourceFile::Close() {
path_.clear();
buf_.reset();
}
SourcePosition SourceFile::FindOffsetLineAndColumn(std::size_t at) const {
CHECK(at < bytes());
auto it = llvm::upper_bound(lineStart_, at);
auto low = std::distance(lineStart_.begin(), it - 1);
return {*this, static_cast<int>(low + 1),
static_cast<int>(at - lineStart_[low] + 1)};
}
} // namespace Fortran::parser
|