Fix all warnings
This commit is contained in:
parent
174d397b89
commit
07b691ba7a
3
.vscode/c_cpp_properties.json
vendored
3
.vscode/c_cpp_properties.json
vendored
@ -14,8 +14,7 @@
|
||||
"-Wall",
|
||||
"-Weffc++",
|
||||
"-Wextra",
|
||||
"-Wconversion",
|
||||
"-Wsign-conversion"
|
||||
"-Werror"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
@ -17,6 +17,7 @@ add_definitions(${LLVM_DEFINITIONS_LIST})
|
||||
# Executable
|
||||
add_executable(${PROJECT_NAME} src/main.cpp src/tokens.cpp src/parsing.cpp src/ast.cpp src/codegen.cpp src/types.cpp)
|
||||
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
|
||||
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Weffc++ -Wextra -Wpedantic -Werror)
|
||||
|
||||
# Find the libraries that correspond to the LLVM components
|
||||
# that we wish to use
|
||||
|
||||
@ -15,7 +15,7 @@ namespace codegen {
|
||||
}
|
||||
|
||||
namespace AST {
|
||||
codegen::StackValue IntLiteralExpression::codegen(codegen::Builder& builder, codegen::Scope& scope) {
|
||||
codegen::StackValue IntLiteralExpression::codegen(codegen::Builder& builder, codegen::Scope&) {
|
||||
auto ty = builder.builder->getInt32Ty();
|
||||
|
||||
auto stack_type = new types::FundamentalType{ types::FundamentalTypeKind::Int };
|
||||
@ -163,9 +163,6 @@ namespace AST {
|
||||
|
||||
|
||||
void Function::codegen(codegen::Builder& builder, codegen::Scope& scope) {
|
||||
|
||||
auto ret_ty = this->m_return_ty->codegen(builder);
|
||||
|
||||
std::shared_ptr<types::Type> ret_ty_ptr{ std::move(this->m_return_ty) };
|
||||
std::vector<std::shared_ptr<types::Type>> param_ty_ptrs{};
|
||||
|
||||
@ -236,6 +233,6 @@ namespace types {
|
||||
}
|
||||
|
||||
llvm::Type* PointerType::codegen(codegen::Builder& builder) {
|
||||
return llvm::PointerType::get(this->m_inner->codegen(builder), 0);
|
||||
return llvm::PointerType::get(*builder.context, 0);
|
||||
}
|
||||
}
|
||||
@ -11,8 +11,8 @@
|
||||
|
||||
namespace codegen {
|
||||
struct StackValue {
|
||||
llvm::Value* value;
|
||||
std::shared_ptr<types::Type> ty;
|
||||
llvm::Value* value = nullptr;
|
||||
std::shared_ptr<types::Type> ty = nullptr;
|
||||
};
|
||||
|
||||
struct Scope {
|
||||
|
||||
11
src/main.cpp
11
src/main.cpp
@ -15,13 +15,14 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cctype>
|
||||
#include <string>
|
||||
|
||||
#include "tokens.h"
|
||||
#include "parsing.h"
|
||||
|
||||
void llvm_hello_world();
|
||||
|
||||
std::string read_file(char* filepath) {
|
||||
std::string read_file(std::string& filepath) {
|
||||
std::ifstream input{ filepath };
|
||||
if (!input) {
|
||||
std::cerr << "Failed to read " << filepath << std::endl;
|
||||
@ -42,7 +43,9 @@ std::string read_file(char* filepath) {
|
||||
|
||||
int main() {
|
||||
|
||||
std::string out{ read_file("test.c") };
|
||||
std::string filename{ "test.c" };
|
||||
|
||||
std::string out{ read_file(filename) };
|
||||
|
||||
std::cout << out << std::endl;
|
||||
|
||||
@ -56,7 +59,7 @@ int main() {
|
||||
std::vector<std::unique_ptr<AST::TopLevelStatement>> statements;
|
||||
auto statement = parsing::parse_top_level_statement(stream);
|
||||
while (statement.ok()) {
|
||||
statements.push_back(std::move(statement.unwrap()));
|
||||
statements.push_back(statement.unwrap());
|
||||
statement = parsing::parse_top_level_statement(stream);
|
||||
}
|
||||
if (stream.peek().type != token::Type::Eof) {
|
||||
@ -101,7 +104,7 @@ int main() {
|
||||
auto Target = llvm::TargetRegistry::lookupTarget(TargetTriple, Error);
|
||||
|
||||
llvm::TargetOptions opt;
|
||||
auto TargetMachine = Target->createTargetMachine(TargetTriple, "generic", "", opt, llvm::Reloc::PIC_);
|
||||
auto TargetMachine = Target->createTargetMachine(llvm::Triple(TargetTriple), "generic", "", opt, llvm::Reloc::PIC_);
|
||||
builder.mod->setDataLayout(TargetMachine->createDataLayout());
|
||||
builder.mod->setTargetTriple(TargetMachine->getTargetTriple());
|
||||
|
||||
|
||||
@ -16,7 +16,7 @@ namespace parsing {
|
||||
auto ty = new types::FundamentalType{ types::FundamentalTypeKind::Int };
|
||||
return new std::unique_ptr<types::Type>{ ty };
|
||||
}
|
||||
catch (std::runtime_error error) {
|
||||
catch (std::runtime_error& error) {
|
||||
return new std::string{ error.what() };
|
||||
}
|
||||
}
|
||||
@ -41,7 +41,7 @@ namespace parsing {
|
||||
throw std::runtime_error("Expected expression");
|
||||
}
|
||||
}
|
||||
catch (std::runtime_error error) {
|
||||
catch (std::runtime_error& error) {
|
||||
return new std::string{ error.what() };
|
||||
}
|
||||
}
|
||||
@ -64,7 +64,7 @@ namespace parsing {
|
||||
|
||||
inner.expect(token::Type::Symbol, ")");
|
||||
|
||||
auto fn_call = new AST::FunctionCallExpression{ std::move(plain_expr.unwrap()), std::move(args) };
|
||||
auto fn_call = new AST::FunctionCallExpression{ plain_expr.unwrap(), std::move(args) };
|
||||
plain_expr = new std::unique_ptr<AST::Expression>{ fn_call };
|
||||
}
|
||||
|
||||
@ -72,7 +72,7 @@ namespace parsing {
|
||||
stream.m_position = inner.m_position;
|
||||
return plain_expr;
|
||||
}
|
||||
catch (std::runtime_error error) {
|
||||
catch (std::runtime_error& error) {
|
||||
return new std::string{ error.what() };
|
||||
}
|
||||
}
|
||||
@ -107,7 +107,7 @@ namespace parsing {
|
||||
|
||||
throw std::runtime_error("Expected binop");
|
||||
}
|
||||
catch (std::runtime_error error) {
|
||||
catch (std::runtime_error& error) {
|
||||
return new std::string{ error.what() };
|
||||
}
|
||||
}
|
||||
@ -138,7 +138,7 @@ namespace parsing {
|
||||
auto lhs = parse_primary_expression(stream).unwrap();
|
||||
return new std::unique_ptr{ parse_rhs(stream, std::move(lhs), 0) };
|
||||
}
|
||||
catch (std::runtime_error error) {
|
||||
catch (std::runtime_error& error) {
|
||||
return new std::string{ error.what() };
|
||||
}
|
||||
}
|
||||
@ -163,7 +163,7 @@ namespace parsing {
|
||||
auto init = new AST::InitializationStatement{ std::move(ty), name.content, std::move(expr) };
|
||||
return new std::unique_ptr<AST::InitializationStatement>{ init };
|
||||
}
|
||||
catch (std::runtime_error error) {
|
||||
catch (std::runtime_error& error) {
|
||||
return new std::string{ error.what() };
|
||||
}
|
||||
}
|
||||
@ -218,7 +218,7 @@ namespace parsing {
|
||||
}
|
||||
|
||||
}
|
||||
catch (std::runtime_error error) {
|
||||
catch (std::runtime_error& error) {
|
||||
return new std::string{ error.what() };
|
||||
}
|
||||
}
|
||||
@ -260,7 +260,7 @@ namespace parsing {
|
||||
auto fun = new AST::Function{ std::move(type), std::move(params), name_token.content, std::move(statements) };
|
||||
return new std::unique_ptr<AST::TopLevelStatement>{ fun };
|
||||
}
|
||||
catch (std::runtime_error error) {
|
||||
catch (std::runtime_error& error) {
|
||||
return new std::string(error.what());
|
||||
}
|
||||
}
|
||||
|
||||
@ -57,7 +57,7 @@ namespace token {
|
||||
|
||||
Token TokenStream::peek(int length) {
|
||||
int new_pos = m_position + length;
|
||||
if (new_pos < 0 || new_pos > m_tokens.size()) {
|
||||
if (new_pos < 0 || new_pos > static_cast<int>(m_tokens.size())) {
|
||||
return Token{ Type::Eof, {} };
|
||||
}
|
||||
return m_tokens[new_pos];
|
||||
@ -92,7 +92,7 @@ namespace token {
|
||||
std::vector<token::Token> tokenize(std::string_view text) {
|
||||
std::vector<token::Token> tokens{};
|
||||
|
||||
for (int i = 0; i < text.length();) {
|
||||
for (int i = 0; i < static_cast<int>(text.length());) {
|
||||
char c = text[i];
|
||||
|
||||
if (std::isdigit(c)) {
|
||||
|
||||
@ -50,7 +50,7 @@ namespace types {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::pair<llvm::Value*, std::shared_ptr<Type>> FundamentalType::load(codegen::Builder& builder, llvm::Value* ptr) {
|
||||
std::pair<llvm::Value*, std::shared_ptr<Type>> FundamentalType::load(codegen::Builder&, llvm::Value* ptr) {
|
||||
auto self = std::make_shared<FundamentalType>(*this);
|
||||
return std::pair(ptr, self);
|
||||
}
|
||||
@ -110,24 +110,24 @@ namespace types {
|
||||
return this->m_ret_ty;
|
||||
}
|
||||
|
||||
std::pair<llvm::Value*, std::shared_ptr<Type>> FunctionType::load(codegen::Builder& builder, llvm::Value* ptr) {
|
||||
std::pair<llvm::Value*, std::shared_ptr<Type>> FunctionType::load(codegen::Builder&, llvm::Value* ptr) {
|
||||
auto self = std::make_shared<FunctionType>(*this);
|
||||
return std::pair(ptr, self);
|
||||
}
|
||||
|
||||
llvm::Value* FunctionType::add(codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs) {
|
||||
llvm::Value* FunctionType::add(codegen::Builder&, llvm::Value*, llvm::Value*) {
|
||||
throw std::runtime_error("Invalid operation for functions");
|
||||
}
|
||||
|
||||
llvm::Value* FunctionType::sub(codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs) {
|
||||
llvm::Value* FunctionType::sub(codegen::Builder&, llvm::Value*, llvm::Value*) {
|
||||
throw std::runtime_error("Invalid operation for functions");
|
||||
}
|
||||
|
||||
llvm::Value* FunctionType::lt(codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs) {
|
||||
llvm::Value* FunctionType::lt(codegen::Builder&, llvm::Value*, llvm::Value*) {
|
||||
throw std::runtime_error("Invalid operation for functions");
|
||||
}
|
||||
|
||||
llvm::Value* FunctionType::gt(codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs) {
|
||||
llvm::Value* FunctionType::gt(codegen::Builder&, llvm::Value*, llvm::Value*) {
|
||||
throw std::runtime_error("Invalid operation for functions");
|
||||
}
|
||||
|
||||
@ -148,19 +148,19 @@ namespace types {
|
||||
);
|
||||
}
|
||||
|
||||
llvm::Value* PointerType::add(codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs) {
|
||||
llvm::Value* PointerType::add(codegen::Builder&, llvm::Value*, llvm::Value*) {
|
||||
throw std::runtime_error("Invalid operation for pointers");
|
||||
}
|
||||
|
||||
llvm::Value* PointerType::sub(codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs) {
|
||||
llvm::Value* PointerType::sub(codegen::Builder&, llvm::Value*, llvm::Value*) {
|
||||
throw std::runtime_error("Invalid operation for pointers");
|
||||
}
|
||||
|
||||
llvm::Value* PointerType::lt(codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs) {
|
||||
llvm::Value* PointerType::lt(codegen::Builder&, llvm::Value*, llvm::Value*) {
|
||||
throw std::runtime_error("Invalid operation for pointers");
|
||||
}
|
||||
|
||||
llvm::Value* PointerType::gt(codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs) {
|
||||
llvm::Value* PointerType::gt(codegen::Builder&, llvm::Value*, llvm::Value*) {
|
||||
throw std::runtime_error("Invalid operation for pointers");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user