Add support for compiling multiple files at once. (#3182)

Rearranges driver logic into CompilationUnits in order to associate
artifacts from the various stages of compilation.

Note, I'm not totally sure what the right thing to do is for
lower/codegen, so I'm just doing a rote change there for now that
mirrors prior phases (this is all the code supports anyways, so is
probably right for now regardless).

SourceBuffer error output is moved local for consistency with other
steps, and so that it's less ambiguous whether the error should be
expected to already include a filename.
This commit is contained in:
Jon Ross-Perkins
2023-09-06 21:34:59 +00:00
committed by GitHub
parent 76576c1387
commit 9ac92ad71b
13 changed files with 307 additions and 177 deletions
+14 -7
View File
@@ -7,33 +7,40 @@
#include <limits>
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FormatVariadic.h"
namespace Carbon {
auto SourceBuffer::CreateFromFile(llvm::vfs::FileSystem& fs,
llvm::raw_ostream& error_stream,
llvm::StringRef filename)
-> ErrorOr<SourceBuffer> {
-> std::optional<SourceBuffer> {
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>> file =
fs.openFileForRead(filename);
if (file.getError()) {
return Error(file.getError().message());
error_stream << "Error opening `" << filename
<< "`: " << file.getError().message();
return std::nullopt;
}
llvm::ErrorOr<llvm::vfs::Status> status = (*file)->status();
if (status.getError()) {
return Error(status.getError().message());
error_stream << "Error getting status for `" << filename
<< "`: " << file.getError().message();
return std::nullopt;
}
auto size = status->getSize();
if (size >= std::numeric_limits<int32_t>::max()) {
return Error(
llvm::formatv("`{0}` is over the 2GiB input limit.", filename));
error_stream << "Cannot load `" << filename
<< "`: file is over the 2GiB input limit.";
return std::nullopt;
}
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer =
(*file)->getBuffer(filename, size, /*RequiresNullTerminator=*/false);
if (buffer.getError()) {
return Error(buffer.getError().message());
error_stream << "Error reading `" << filename
<< "`: " << file.getError().message();
return std::nullopt;
}
return SourceBuffer(filename.str(), std::move(buffer.get()));