Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
refactor: Add extra warnings flags and apply first round of refactori…
Browse files Browse the repository at this point in the history
…ng (#77)

* Apply first refactoring round

* Fix documentation issues
  • Loading branch information
xmnlab committed Jan 12, 2023
1 parent 60cbac1 commit 40e2f88
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 147 deletions.
10 changes: 10 additions & 0 deletions artifacts/compile_commands.json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
"command": "{{CONDA_PREFIX}}/bin/clang++ -DGFLAGS_IS_A_DLL=0 -DGLOG_CUSTOM_PREFIX_SUPPORT -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I{{PROJECT_PATH}}/src -fvisibility-inlines-hidden -std=c++20 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem {{CONDA_PREFIX}}/include -O3 -DNDEBUG -fprofile-instr-generate -fcoverage-mapping -std=c++20 -o CMakeFiles/arx.dir/src/codegen/ast-to-object.cpp.o -c {{PROJECT_PATH}}/src/codegen/ast-to-object.cpp",
"file": "{{PROJECT_PATH}}/src/codegen/ast-to-object.cpp"
},
{
"directory": "{{PROJECT_PATH}}/build",
"command": "{{CONDA_PREFIX}}/bin/clang++ -DGFLAGS_IS_A_DLL=0 -DGLOG_CUSTOM_PREFIX_SUPPORT -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I{{PROJECT_PATH}}/src -fvisibility-inlines-hidden -std=c++20 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem {{CONDA_PREFIX}}/include -O3 -DNDEBUG -fprofile-instr-generate -fcoverage-mapping -std=c++20 -o CMakeFiles/arx.dir/src/codegen/ast-to-stdout.cpp.o -c {{PROJECT_PATH}}/src/codegen/ast-to-stdout.cpp",
"file": "{{PROJECT_PATH}}/src/codegen/ast-to-stdout.cpp"
},
{
"directory": "{{PROJECT_PATH}}/build",
"command": "{{CONDA_PREFIX}}/bin/clang++ -DGFLAGS_IS_A_DLL=0 -DGLOG_CUSTOM_PREFIX_SUPPORT -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I{{PROJECT_PATH}}/src -fvisibility-inlines-hidden -std=c++20 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem {{CONDA_PREFIX}}/include -O3 -DNDEBUG -fprofile-instr-generate -fcoverage-mapping -std=c++20 -o CMakeFiles/arx.dir/src/utils.cpp.o -c {{PROJECT_PATH}}/src/utils.cpp",
Expand Down Expand Up @@ -94,6 +99,11 @@
"command": "{{CONDA_PREFIX}}/bin/clang++ -DGFLAGS_IS_A_DLL=0 -DGLOG_CUSTOM_PREFIX_SUPPORT -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I{{PROJECT_PATH}}/src -I{{PROJECT_PATH}}/build/tests -fvisibility-inlines-hidden -std=c++20 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem {{CONDA_PREFIX}}/include -fsanitize=address -O1 -fno-omit-frame-pointer -fprofile-instr-generate -fcoverage-mapping -O3 -DNDEBUG -fprofile-instr-generate -fcoverage-mapping -std=c++20 -o tests/CMakeFiles/ArxTests.dir/__/src/codegen/ast-to-object.cpp.o -c {{PROJECT_PATH}}/src/codegen/ast-to-object.cpp",
"file": "{{PROJECT_PATH}}/src/codegen/ast-to-object.cpp"
},
{
"directory": "{{PROJECT_PATH}}/build",
"command": "{{CONDA_PREFIX}}/bin/clang++ -DGFLAGS_IS_A_DLL=0 -DGLOG_CUSTOM_PREFIX_SUPPORT -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I{{PROJECT_PATH}}/src -I{{PROJECT_PATH}}/build/tests -fvisibility-inlines-hidden -std=c++20 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem {{CONDA_PREFIX}}/include -fsanitize=address -O1 -fno-omit-frame-pointer -fprofile-instr-generate -fcoverage-mapping -O3 -DNDEBUG -fprofile-instr-generate -fcoverage-mapping -std=c++20 -o tests/CMakeFiles/ArxTests.dir/__/src/codegen/ast-to-stdout.cpp.o -c {{PROJECT_PATH}}/src/codegen/ast-to-stdout.cpp",
"file": "{{PROJECT_PATH}}/src/codegen/ast-to-stdout.cpp"
},
{
"directory": "{{PROJECT_PATH}}/build",
"command": "{{CONDA_PREFIX}}/bin/clang++ -DGFLAGS_IS_A_DLL=0 -DGLOG_CUSTOM_PREFIX_SUPPORT -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I{{PROJECT_PATH}}/src -I{{PROJECT_PATH}}/build/tests -fvisibility-inlines-hidden -std=c++20 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem {{CONDA_PREFIX}}/include -fsanitize=address -O1 -fno-omit-frame-pointer -fprofile-instr-generate -fcoverage-mapping -O3 -DNDEBUG -fprofile-instr-generate -fcoverage-mapping -std=c++20 -o tests/CMakeFiles/ArxTests.dir/__/src/utils.cpp.o -c {{PROJECT_PATH}}/src/utils.cpp",
Expand Down
26 changes: 24 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,33 @@ project('arx', 'cpp', 'c',
license : 'Apache-2.0',
version : '1.5.0', # semantic-release
default_options : [
'warning_level=3',
'warning_level=everything',
'cpp_std=c++20',
]
)

add_global_arguments(
'-Wpedantic',
'-Wno-c++98-compat-pedantic',
'-Wno-padded',
'-Wno-missing-prototypes',
'-Wshadow',
'-Wnon-virtual-dtor',
'-Wcast-align',
'-Wunused',
'-Woverloaded-virtual',
'-Wconversion',
'-Wsign-conversion',
'-Wdouble-promotion',
'-Wformat=2',
'-Wimplicit-fallthrough',
'-Wsuggest-override',
'-Wnull-dereference',
'-Wold-style-cast',
language : 'cpp'
)


PROJECT_PATH = meson.source_root()

cxx = meson.get_compiler('cpp')
Expand All @@ -25,7 +47,7 @@ project_src_files = files(
# note: note working yet
# PROJECT_PATH + '/src/codegen/ast-to-llvm.cpp',
PROJECT_PATH + '/src/codegen/ast-to-object.cpp',
PROJECT_PATH + '/src/codegen/ast-to-output.cpp',
PROJECT_PATH + '/src/codegen/ast-to-stdout.cpp',
PROJECT_PATH + '/src/error.cpp',
PROJECT_PATH + '/src/io.cpp',
PROJECT_PATH + '/src/lexer.cpp',
Expand Down
59 changes: 30 additions & 29 deletions src/codegen/ast-to-object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,8 @@ class ASTToObjectVisitor : public Visitor {
};

/**
* @brief
* @param Name
* @return
* @brief Put the function defined by the given name to result_func.
* @param Name Function name
*
* First, see if the function has already been added to the current
* module. If not, check whether we can codegen the declaration from some
Expand All @@ -127,10 +126,10 @@ auto ASTToObjectVisitor::getFunction(std::string Name) -> void {
}

/**
* @brief
* @param TheFunction
* @param VarName
* @return
* @brief Create the Entry Block Allocation.
* @param TheFunction The llvm function
* @param VarName The variable name
* @return An llvm allocation instance.
*
* CreateEntryBlockAlloca - Create an alloca instruction in the entry
* block of the function. This is used for mutable variables etc.
Expand All @@ -143,14 +142,17 @@ auto ASTToObjectVisitor::CreateEntryBlockAlloca(
llvm::Type::getDoubleTy(*this->TheContext), nullptr, VarName);
}

/**
* @brief Set to nullptr result_val and result_func in order to avoid trash.
*
*/
auto ASTToObjectVisitor::clean() -> void {
this->result_val = nullptr;
this->result_func = nullptr;
}

/**
* @brief
* @return
* @brief Code generation for NumberExprAST.
*
*/
auto ASTToObjectVisitor::visit(NumberExprAST* expr) -> void {
Expand All @@ -159,8 +161,7 @@ auto ASTToObjectVisitor::visit(NumberExprAST* expr) -> void {
}

/**
* @brief Stat a variable in the function.
* @return The variable loaded into the llvm.
* @brief Code generation for VariableExprAST.
*
*/
auto ASTToObjectVisitor::visit(VariableExprAST* expr) -> void {
Expand All @@ -177,8 +178,7 @@ auto ASTToObjectVisitor::visit(VariableExprAST* expr) -> void {
}

/**
* @brief
* @return
* @brief Code generation for UnaryExprAST.
*
*/
auto ASTToObjectVisitor::visit(UnaryExprAST* expr) -> void {
Expand All @@ -201,8 +201,7 @@ auto ASTToObjectVisitor::visit(UnaryExprAST* expr) -> void {
}

/**
* @brief
* @return
* @brief Code generation for BinaryExprAST.
*
*/
auto ASTToObjectVisitor::visit(BinaryExprAST* expr) -> void {
Expand Down Expand Up @@ -278,8 +277,7 @@ auto ASTToObjectVisitor::visit(BinaryExprAST* expr) -> void {
}

/**
* @brief Look up the name in the global module table.
* @return
* @brief Code generation for CallExprAST.
*
*/
auto ASTToObjectVisitor::visit(CallExprAST* expr) -> void {
Expand Down Expand Up @@ -310,7 +308,7 @@ auto ASTToObjectVisitor::visit(CallExprAST* expr) -> void {
}

/**
* @brief
* @brief Code generation for IfExprAST.
*/
auto ASTToObjectVisitor::visit(IfExprAST* expr) -> void {
expr->Cond.get()->accept(this);
Expand Down Expand Up @@ -384,6 +382,11 @@ auto ASTToObjectVisitor::visit(IfExprAST* expr) -> void {
return;
}

/**
* @brief Code generation for ForExprAST.
*
* @param expr A `for` expression.
*/
auto ASTToObjectVisitor::visit(ForExprAST* expr) -> void {
llvm::Function* TheFunction = this->Builder->GetInsertBlock()->getParent();

Expand Down Expand Up @@ -489,8 +492,7 @@ auto ASTToObjectVisitor::visit(ForExprAST* expr) -> void {
}

/**
* @brief
* @return Return the body computation.
* @brief Code generation for VarExprAST.
*
*/
auto ASTToObjectVisitor::visit(VarExprAST* expr) -> void {
Expand Down Expand Up @@ -550,8 +552,7 @@ auto ASTToObjectVisitor::visit(VarExprAST* expr) -> void {
}

/**
* @brief
* @return
* @brief Code generation for PrototypeExprAST.
*
*/
auto ASTToObjectVisitor::visit(PrototypeAST* expr) -> void {
Expand All @@ -574,8 +575,7 @@ auto ASTToObjectVisitor::visit(PrototypeAST* expr) -> void {
}

/**
* @brief
* @return
* @brief Code generation for FunctionExprAST.
*
* Transfer ownership of the prototype to the FunctionProtos map, but
* keep a reference to it for use below.
Expand Down Expand Up @@ -634,7 +634,7 @@ auto ASTToObjectVisitor::visit(FunctionAST* expr) -> void {
}

/**
* @brief Open a new module.
* @brief Initialize LLVM Module And PassManager.
*
*/
auto ASTToObjectVisitor::InitializeModuleAndPassManager() -> void {
Expand All @@ -647,7 +647,7 @@ auto ASTToObjectVisitor::InitializeModuleAndPassManager() -> void {
}

/**
* @brief
* @brief The main loop that walks the AST.
* top ::= definition | external | expression | ';'
*/
auto ASTToObjectVisitor::MainLoop(TreeAST* ast) -> void {
Expand All @@ -671,7 +671,7 @@ auto ASTToObjectVisitor::MainLoop(TreeAST* ast) -> void {
*
*/
extern "C" DLLEXPORT auto putchard(double X) -> double {
fputc((char) X, stderr);
fputc(static_cast<char>(X), stderr);
return 0;
}

Expand All @@ -685,8 +685,9 @@ extern "C" DLLEXPORT auto printd(double X) -> double {
}

/**
* @brief
* @brief Compile an AST to object file.
*
* @param tree_ast The AST tree object.
*/
auto compile(TreeAST* tree_ast) -> void {
auto codegen = new ASTToObjectVisitor();
Expand Down Expand Up @@ -768,7 +769,7 @@ auto compile(TreeAST* tree_ast) -> void {
}

/**
* @brief
* @brief Open the Arx shell.
*
*/
auto open_shell() -> void {
Expand Down
27 changes: 19 additions & 8 deletions src/codegen/ast-to-output.cpp → src/codegen/ast-to-stdout.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ast-to-output.h"
#include "ast-to-stdout.h"
#include <iostream>
#include "parser.h"

Expand All @@ -21,7 +21,8 @@ class ASTToOutputVisitor : public Visitor {
virtual void visit(VarExprAST*) override;
virtual void visit(PrototypeAST*) override;
virtual void visit(FunctionAST*) override;
virtual void clean() override{};

virtual void clean() override {}

auto indentation() -> std::string {
std::string _indent(this->indent, ' ');
Expand All @@ -30,13 +31,13 @@ class ASTToOutputVisitor : public Visitor {

auto set_annotation(std::string annotation) -> void {
this->annotation = annotation;
};
}

auto get_annotation() -> std::string {
std::string _anno = this->annotation;
this->annotation = "";
return _anno;
};
}
};

void ASTToOutputVisitor::visit(NumberExprAST* expr) {
Expand Down Expand Up @@ -173,14 +174,24 @@ void ASTToOutputVisitor::visit(ForExprAST* expr) {

void ASTToOutputVisitor::visit(VarExprAST* expr) {
// TODO: implement it
std::cout << "(VarExprAST"
<< ")" << std::endl;
std::cout << "(VarExprAST " << std::endl;
this->indent += INDENT_SIZE;

for (auto var_expr = expr->VarNames.begin();
var_expr != expr->VarNames.end();
++var_expr) {
var_expr->second->accept(this);
std::cout << "," << std::endl;
}

this->indent -= INDENT_SIZE;

std::cout << ")" << std::endl;
}

void ASTToOutputVisitor::visit(PrototypeAST* expr) {
// TODO: implement it
std::cout << "(PrototypeAST"
<< ")" << std::endl;
std::cout << "(PrototypeAST " << expr->Name << ")" << std::endl;
}

void ASTToOutputVisitor::visit(FunctionAST* expr) {
Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions src/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

namespace llvm {
/**
* @brief
* "llvm/IR/Value.h"
* @brief "llvm/IR/Value.h"
*
*/
class Value;
Expand Down
10 changes: 5 additions & 5 deletions src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ std::string OUTPUT_FILE{""};
bool INPUT_FROM_STDIN = false;

/**
* @brief
* @return
* @brief Get a char from the buffer of from the default input.
* @return An integer represenation of a char from the buffer.
*
*/
auto get_char() -> int {
Expand All @@ -24,7 +24,7 @@ auto get_char() -> int {
}

/**
* @brief
* @brief Copy the file content to the buffer.
*
*/
auto file_to_buffer(std::string filename) -> void {
Expand All @@ -41,7 +41,7 @@ auto file_to_buffer(std::string filename) -> void {
}

/**
* @brief
* @brief Copy the given string to the buffer.
*
*/
auto string_to_buffer(std::string value) -> void {
Expand All @@ -51,7 +51,7 @@ auto string_to_buffer(std::string value) -> void {
}

/**
* @brief
* @brief Load the content file or the standard input to the buffer.
*
*/
auto load_input_to_buffer() -> void {
Expand Down
6 changes: 3 additions & 3 deletions src/jit.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ namespace llvm {

public:
/**
* @param ES
* @param JTMB
* @param DL
* @param ES ExecutionSession
* @param JTMB JITTargetMachineBuilder
* @param DL DataLayout
*/
ArxJIT(
std::unique_ptr<ExecutionSession> ES,
Expand Down
Loading

0 comments on commit 40e2f88

Please sign in to comment.