Skip to content

Commit

Permalink
empty file crash fix
Browse files Browse the repository at this point in the history
  • Loading branch information
f0xeri committed Feb 24, 2024
1 parent 9e7532b commit 8eb9451
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
3 changes: 3 additions & 0 deletions driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Slangc {
processUnit(mainModuleName, true);
for (auto &error : errors)
log() << error;
if (!errors.empty()) return;
std::stringstream clangCallStream;
clangCallStream << "clang ";
std::ranges::copy(std::views::transform(options.getInputFilePaths(), [](const std::filesystem::path &p){ return p.string(); }), std::ostream_iterator<std::string>(clangCallStream, ".o "));
Expand Down Expand Up @@ -53,6 +54,8 @@ namespace Slangc {
auto codeGen = CodeGen(*context, std::move(parser.moduleAST), isMainModule);
if (!containsErrors(errors))
codeGen.process(errors);
else
return context;
//codeGen.dumpIRToFile(filepath.string() + ".ll");
Optimizer optimizer(options.getOptimizationLevel());
optimizer.run(*codeGen.getModule());
Expand Down
2 changes: 1 addition & 1 deletion parser/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Slangc {
context.moduleName = moduleName;
moduleAST = create<ModuleDeclNode>(loc, moduleName, create<BlockStmtNode>(loc, std::vector<StmtPtrVariant>()));
advance();
while (token->type != TokenType::Start)
while (token->type != TokenType::Start && token->type != TokenType::EndOfFile)
{
if (token->type == TokenType::Function || token->type == TokenType::Procedure || (token->type == TokenType::Extern && ((token + 1)->type == TokenType::Procedure || (token + 1)->type == TokenType::Function))) {
auto funcDecl = parseFuncDecl();
Expand Down
5 changes: 3 additions & 2 deletions source/SourceBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ namespace Slangc {
auto SourceBuffer::CreateFromFile(std::string_view path) -> llvm::Expected<SourceBuffer> {
std::string filename = std::string(path);
if (!filename.ends_with(".sl")) {
return llvm::make_error<llvm::StringError>(filename + " : slang source file extension must be .sl", llvm::inconvertibleErrorCode());
return llvm::make_error<llvm::StringError>(filename + ": slang source file extension must be .sl", llvm::inconvertibleErrorCode());
}
constexpr auto read_size = std::size_t{4096};
auto stream = std::basic_ifstream<char32_t>{path.data()};
stream.exceptions(std::ios_base::badbit);

if (!stream) {
return llvm::make_error<llvm::StringError>(filename + " : file not found", llvm::inconvertibleErrorCode());
return llvm::make_error<llvm::StringError>(filename + ": file not found", llvm::inconvertibleErrorCode());
}

auto result = std::u32string{};
Expand All @@ -34,6 +34,7 @@ namespace Slangc {
std::string str;
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;
str = converter.to_bytes(result);
if (str.empty()) return llvm::make_error<llvm::StringError>(filename + ": file is empty", llvm::inconvertibleErrorCode());
return SourceBuffer{std::move(filename), std::move(str)};
}

Expand Down

0 comments on commit 8eb9451

Please sign in to comment.