Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable more compiler warnings #432

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ set(SOURCES

add_executable(spice ${SOURCES} ${ANTLR_Spice_CXX_OUTPUTS})

# Enable pedantic warnings
target_compile_options(spice PRIVATE -Wpedantic -Wall)

# Include Antlr components
include_directories(../lib/antlr4/runtime/Cpp/runtime/src)
include_directories(${ANTLR_Spice_OUTPUT_DIR})
Expand Down
3 changes: 2 additions & 1 deletion src/ast/ASTNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ std::string ASTNode::getErrorMessage() const {
}
for (size_t suffixContext = 0; suffixContext < ERROR_MESSAGE_CONTEXT; suffixContext++) {
extSourceInterval.b++;
if (extSourceInterval.b > inputStream->size() || inputStream->getText(extSourceInterval).find('\n') != std::string::npos) {
if (static_cast<size_t>(extSourceInterval.b) > inputStream->size() ||
inputStream->getText(extSourceInterval).find('\n') != std::string::npos) {
extSourceInterval.b--;
break;
}
Expand Down
28 changes: 14 additions & 14 deletions src/ast/ASTNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ class ASTNode {
customItemsInitialization(manifestationCount);
}

virtual std::vector<std::vector<const Function *>> *getOpFctPointers() { // LCOV_EXCL_LINE
assert_fail("The given node does not overload the getOpFctPointers function"); // LCOV_EXCL_LINE
return nullptr; // LCOV_EXCL_LINE
} // LCOV_EXCL_LINE
virtual const std::vector<std::vector<const Function *>> *getOpFctPointers() const { // LCOV_EXCL_LINE
assert_fail("The given node does not overload the getOpFctPointers function"); // LCOV_EXCL_LINE
return nullptr; // LCOV_EXCL_LINE
} // LCOV_EXCL_LINE
virtual std::vector<std::vector<const Function *>> *getOpFctPointers() { // LCOV_EXCL_LINE
assert_fail("The given node does not overload the getOpFctPointers function"); // LCOV_EXCL_LINE
return nullptr; // LCOV_EXCL_LINE
} // LCOV_EXCL_LINE
[[nodiscard]] virtual const std::vector<std::vector<const Function *>> *getOpFctPointers() const { // LCOV_EXCL_LINE
assert_fail("The given node does not overload the getOpFctPointers function"); // LCOV_EXCL_LINE
return nullptr; // LCOV_EXCL_LINE
} // LCOV_EXCL_LINE

virtual void customItemsInitialization(size_t) {} // Noop

Expand Down Expand Up @@ -130,7 +130,7 @@ class ASTNode {
return children.size() == 1 && children.front()->returnsOnAllControlPaths(doSetPredecessorsUnreachable);
}

[[nodiscard]] virtual std::vector<Function *> *getFctManifestations(const std::string &fctName) { // LCOV_EXCL_LINE
[[nodiscard]] virtual std::vector<Function *> *getFctManifestations(const std::string &) { // LCOV_EXCL_LINE
assert_fail("Must be called on a FctDefNode, ProcDefNode, ExtDeclNode, StructDefNode or SignatureNode"); // LCOV_EXCL_LINE
return nullptr; // LCOV_EXCL_LINE
} // LCOV_EXCL_LINE
Expand Down Expand Up @@ -240,7 +240,7 @@ class FctDefBaseNode : public ASTNode {

// Other methods
[[nodiscard]] std::string getSymbolTableEntryName() const { return Function::getSymbolTableEntryName(name->name, codeLoc); }
std::vector<Function *> *getFctManifestations(const std::string &_) override { return &manifestations; }
std::vector<Function *> *getFctManifestations(const std::string &) override { return &manifestations; }
[[nodiscard]] bool isFctOrProcDef() const override { return true; }
bool returnsOnAllControlPaths(bool *doSetPredecessorsUnreachable) const override;

Expand Down Expand Up @@ -469,7 +469,7 @@ class ExtDeclNode : public ASTNode {
[[nodiscard]] TypeLstNode *argTypeLst() const { return getChild<TypeLstNode>(); }

// Other methods
std::vector<Function *> *getFctManifestations(const std::string &_) override { return &extFunctionManifestations; }
std::vector<Function *> *getFctManifestations(const std::string &) override { return &extFunctionManifestations; }
[[nodiscard]] std::string getScopeId() const {
const char *prefix = hasReturnType ? "func:" : "proc:";
return prefix + codeLoc.toString();
Expand Down Expand Up @@ -908,7 +908,7 @@ class SignatureNode : public ASTNode {
[[nodiscard]] TypeLstNode *paramTypeLst() const { return getChild<TypeLstNode>(hasTemplateTypes ? 1 : 0); }

// Other methods
std::vector<Function *> *getFctManifestations(const std::string &_) override { return &signatureManifestations; }
std::vector<Function *> *getFctManifestations(const std::string &) override { return &signatureManifestations; }

// Public members
Type signatureType = SignatureNode::TYPE_NONE;
Expand Down Expand Up @@ -1160,7 +1160,7 @@ class ReturnStmtNode : public ASTNode {
[[nodiscard]] AssignExprNode *assignExpr() const { return getChild<AssignExprNode>(); }

// Other methods
[[nodiscard]] bool returnsOnAllControlPaths(bool *doSetPredecessorsUnreachable) const override { return true; }
[[nodiscard]] bool returnsOnAllControlPaths(bool *) const override { return true; }
[[nodiscard]] StmtLstNode *getParentScopeNode() const {
assert(dynamic_cast<StmtLstNode *>(parent->parent) != nullptr);
return spice_pointer_cast<StmtLstNode *>(parent->parent);
Expand Down Expand Up @@ -1332,7 +1332,7 @@ class PanicCallNode : public ASTNode {

// Other methods
[[nodiscard]] bool hasCompileTimeValue() const override { return false; }
[[nodiscard]] bool returnsOnAllControlPaths(bool *doSetPredecessorsUnreachable) const override { return true; }
[[nodiscard]] bool returnsOnAllControlPaths(bool *) const override { return true; }
};

// ======================================================= AssignExprNode ========================================================
Expand Down
3 changes: 0 additions & 3 deletions src/irgenerator/DebugInfoGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,6 @@ void DebugInfoGenerator::finalize() {
diBuilder->finalize();
}

#pragma clang diagnostic push
#pragma ide diagnostic ignored "misc-no-recursion"
llvm::DIType *DebugInfoGenerator::getDITypeForSymbolType(const ASTNode *node, const SymbolType &symbolType) const {
// Pointer type
if (symbolType.isPtr()) {
Expand Down Expand Up @@ -417,6 +415,5 @@ llvm::DIType *DebugInfoGenerator::getDITypeForSymbolType(const ASTNode *node, co

return baseDiType;
}
#pragma clang diagnostic pop

} // namespace spice::compiler
46 changes: 22 additions & 24 deletions src/irgenerator/GenExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,32 +124,31 @@ std::any IRGenerator::visitLogicalOrExpr(const LogicalOrExprNode *node) {
llvm::Value *firstOperandValue = resolveValue(node->operands().front());

// Prepare an array for value-to-block-mapping
std::pair<llvm::BasicBlock *, llvm::Value *> shortCircuitBlocks[node->operands().size()];
std::vector<std::pair<llvm::BasicBlock *, llvm::Value *>> shortCircuitBlocks;
shortCircuitBlocks.reserve(node->operands().size());
// The first element is the first operand value with the original block
shortCircuitBlocks[0] = {builder.GetInsertBlock(), firstOperandValue};
shortCircuitBlocks.emplace_back(builder.GetInsertBlock(), firstOperandValue);
// Create a block for each additional operand and save it to the mapping
for (size_t i = 1; i < node->operands().size(); i++) {
llvm::BasicBlock *nextBlock = createBlock("lor." + std::to_string(i) + "." + codeLoc);
shortCircuitBlocks[i] = {nextBlock, nullptr};
}
for (size_t i = 1; i < node->operands().size(); i++)
shortCircuitBlocks.emplace_back(createBlock("lor." + std::to_string(i) + "." + codeLoc), nullptr);
// Create conditional jump to the exit block if the first operand was true, otherwise to the next block
insertCondJump(firstOperandValue, bExit, shortCircuitBlocks[1].first);
insertCondJump(firstOperandValue, bExit, shortCircuitBlocks.at(1).first);

// Create block for each operand
for (int i = 1; i < node->operands().size(); i++) {
for (size_t i = 1; i < node->operands().size(); i++) {
// Switch to the next block
switchToBlock(shortCircuitBlocks[i].first);
switchToBlock(shortCircuitBlocks.at(i).first);
// Evaluate operand and save the result in the mapping
shortCircuitBlocks[i].second = resolveValue(node->operands()[i]);
shortCircuitBlocks.at(i).second = resolveValue(node->operands()[i]);
// Replace the array entry with the current insert block, since the insert block could have changed in the meantime
shortCircuitBlocks[i].first = builder.GetInsertBlock();
shortCircuitBlocks.at(i).first = builder.GetInsertBlock();
// Check if there are more blocks to process
if (i == node->operands().size() - 1) {
// Insert a simple jump to the exit block for the last block
insertJump(bExit);
} else {
// Create conditional jump to the exit block if the first operand was true, otherwise to the next block
insertCondJump(shortCircuitBlocks[i].second, bExit, shortCircuitBlocks[i + 1].first);
insertCondJump(shortCircuitBlocks.at(i).second, bExit, shortCircuitBlocks.at(i + 1).first);
}
}

Expand Down Expand Up @@ -179,32 +178,31 @@ std::any IRGenerator::visitLogicalAndExpr(const LogicalAndExprNode *node) {
llvm::Value *firstOperandValue = resolveValue(node->operands().front());

// Prepare an array for value-to-block-mapping
std::pair<llvm::BasicBlock *, llvm::Value *> shortCircuitBlocks[node->operands().size()];
std::vector<std::pair<llvm::BasicBlock *, llvm::Value *>> shortCircuitBlocks;
shortCircuitBlocks.reserve(node->operands().size());
// The first element is the first operand value with the original block
shortCircuitBlocks[0] = {builder.GetInsertBlock(), firstOperandValue};
shortCircuitBlocks.emplace_back(builder.GetInsertBlock(), firstOperandValue);
// Create a block for each additional operand and save it to the mapping
for (size_t i = 1; i < node->operands().size(); i++) {
llvm::BasicBlock *nextBlock = createBlock("land." + std::to_string(i) + "." + codeLoc);
shortCircuitBlocks[i] = {nextBlock, nullptr};
}
for (size_t i = 1; i < node->operands().size(); i++)
shortCircuitBlocks.emplace_back(createBlock("land." + std::to_string(i) + "." + codeLoc), nullptr);
// Create conditional jump to the exit block if the first operand was true, otherwise to the next block
insertCondJump(firstOperandValue, shortCircuitBlocks[1].first, bExit);
insertCondJump(firstOperandValue, shortCircuitBlocks.at(1).first, bExit);

// Create block for each operand
for (int i = 1; i < node->operands().size(); i++) {
for (size_t i = 1; i < node->operands().size(); i++) {
// Switch to the next block
switchToBlock(shortCircuitBlocks[i].first);
switchToBlock(shortCircuitBlocks.at(i).first);
// Evaluate operand and save the result in the mapping
shortCircuitBlocks[i].second = resolveValue(node->operands()[i]);
shortCircuitBlocks.at(i).second = resolveValue(node->operands()[i]);
// Replace the array entry with the current insert block, since the insert block could have changed in the meantime
shortCircuitBlocks[i].first = builder.GetInsertBlock();
shortCircuitBlocks.at(i).first = builder.GetInsertBlock();
// Check if there are more blocks to process
if (i == node->operands().size() - 1) {
// Insert a simple jump to the exit block for the last block
insertJump(bExit);
} else {
// Create conditional jump to the exit block if the operand was true, otherwise to the next block
insertCondJump(shortCircuitBlocks[i].second, shortCircuitBlocks[i + 1].first, bExit);
insertCondJump(shortCircuitBlocks.at(i).second, shortCircuitBlocks.at(i + 1).first, bExit);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/irgenerator/GenImplicit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,12 @@ void IRGenerator::generateCtorBodyPreamble(Scope *bodyScope) {
// Store VTable to first struct field if required
Struct *spiceStruct = structSymbolType.getStruct(nullptr);
assert(spiceStruct != nullptr);
if (spiceStruct->vtable != nullptr) {
assert(spiceStruct->vtableType != nullptr);
if (spiceStruct->vTableData.vtable != nullptr) {
assert(spiceStruct->vTableData.vtableType != nullptr);
// Store VTable to field address at index 0
thisAddressLoaded = insertLoad(builder.getPtrTy(), thisAddress);
llvm::Value *indices[3] = {builder.getInt32(0), builder.getInt32(0), builder.getInt32(2)};
llvm::Value *gepResult = insertInBoundsGEP(spiceStruct->vtableType, spiceStruct->vtable, indices);
llvm::Value *gepResult = insertInBoundsGEP(spiceStruct->vTableData.vtableType, spiceStruct->vTableData.vtable, indices);
insertStore(gepResult, thisAddressLoaded);
}

Expand Down
26 changes: 13 additions & 13 deletions src/irgenerator/GenVTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ llvm::Constant *IRGenerator::generateTypeInfoName(StructBase *spiceStruct) {
global->setLinkage(llvm::GlobalValue::ExternalLinkage);
global->setComdat(module->getOrInsertComdat(globalName));

return spiceStruct->typeInfoName = global;
return spiceStruct->vTableData.typeInfoName = global;
}

llvm::Constant *IRGenerator::generateTypeInfo(StructBase *spiceStruct) {
Expand All @@ -43,7 +43,7 @@ llvm::Constant *IRGenerator::generateTypeInfo(StructBase *spiceStruct) {
std::vector<llvm::Type *> typeInfoFieldTypes = {ptrTy, ptrTy};
for (size_t i = 0; i < interfaceTypes.size(); i++)
typeInfoFieldTypes.push_back(ptrTy);
spiceStruct->typeInfoType = llvm::StructType::get(context, typeInfoFieldTypes);
spiceStruct->vTableData.typeInfoType = llvm::StructType::get(context, typeInfoFieldTypes);

// Generate type info values
llvm::Constant *typeInfoVTable = llvm::Constant::getNullValue(ptrTy);
Expand All @@ -59,15 +59,15 @@ llvm::Constant *IRGenerator::generateTypeInfo(StructBase *spiceStruct) {
fieldValues.push_back(typeInfoName);
for (const SymbolType &interfaceType : interfaceTypes) {
Interface *interface = interfaceType.getInterface(nullptr);
assert(interface != nullptr && interface->typeInfo != nullptr);
assert(interface != nullptr && interface->vTableData.typeInfo != nullptr);
const std::string interfaceMangledName = NameMangling::mangleTypeInfo(interface);
llvm::Constant *global = module->getOrInsertGlobal(interfaceMangledName, builder.getPtrTy());
fieldValues.push_back(global);
}
llvm::Constant *typeInfo = llvm::ConstantStruct::get(spiceStruct->typeInfoType, fieldValues);
llvm::Constant *typeInfo = llvm::ConstantStruct::get(spiceStruct->vTableData.typeInfoType, fieldValues);

// Generate global variable
module->getOrInsertGlobal(mangledName, spiceStruct->typeInfoType);
module->getOrInsertGlobal(mangledName, spiceStruct->vTableData.typeInfoType);
llvm::GlobalVariable *global = module->getNamedGlobal(mangledName);
global->setInitializer(typeInfo);

Expand All @@ -79,7 +79,7 @@ llvm::Constant *IRGenerator::generateTypeInfo(StructBase *spiceStruct) {
global->setComdat(module->getOrInsertComdat(mangledName));
global->setAlignment(llvm::MaybeAlign(8));

return spiceStruct->typeInfo = global;
return spiceStruct->vTableData.typeInfo = global;
}

llvm::Constant *IRGenerator::generateVTable(StructBase *spiceStruct) {
Expand All @@ -94,10 +94,10 @@ llvm::Constant *IRGenerator::generateVTable(StructBase *spiceStruct) {
// Generate VTable type
llvm::PointerType *ptrTy = llvm::PointerType::get(context, 0);
llvm::ArrayType *vtableArrayTy = llvm::ArrayType::get(ptrTy, arrayElementCount);
spiceStruct->vtableType = llvm::StructType::get(context, vtableArrayTy, false);
spiceStruct->vTableData.vtableType = llvm::StructType::get(context, vtableArrayTy, false);

const std::string mangledName = NameMangling::mangleVTable(spiceStruct);
module->getOrInsertGlobal(mangledName, spiceStruct->vtableType);
module->getOrInsertGlobal(mangledName, spiceStruct->vTableData.vtableType);
llvm::GlobalVariable *global = module->getNamedGlobal(mangledName);

// Set global attributes
Expand All @@ -108,7 +108,7 @@ llvm::Constant *IRGenerator::generateVTable(StructBase *spiceStruct) {
global->setComdat(module->getOrInsertComdat(mangledName));
global->setAlignment(llvm::MaybeAlign(8));

return spiceStruct->vtable = global;
return spiceStruct->vTableData.vtable = global;
}

void IRGenerator::generateVTableInitializer(StructBase *spiceStruct) {
Expand All @@ -121,21 +121,21 @@ void IRGenerator::generateVTableInitializer(StructBase *spiceStruct) {
// Generate VTable type
llvm::PointerType *ptrTy = llvm::PointerType::get(context, 0);
llvm::ArrayType *vtableArrayTy = llvm::ArrayType::get(ptrTy, arrayElementCount);
assert(spiceStruct->vtableType);
assert(spiceStruct->vTableData.vtableType);

// Generate VTable values
std::vector<llvm::Constant *> arrayValues;
arrayValues.push_back(llvm::Constant::getNullValue(ptrTy)); // nullptr as safety guard
arrayValues.push_back(spiceStruct->typeInfo); // TypeInfo to identify the type for the VTable
arrayValues.push_back(spiceStruct->vTableData.typeInfo); // TypeInfo to identify the type for the VTable
for (Function *virtualMethod : virtualMethods) {
assert(spiceStruct->scope->type == ScopeType::INTERFACE || virtualMethod->llvmFunction != nullptr);
arrayValues.push_back(virtualMethod->llvmFunction ?: llvm::Constant::getNullValue(ptrTy));
arrayValues.push_back(virtualMethod->llvmFunction ? virtualMethod->llvmFunction : llvm::Constant::getNullValue(ptrTy));
}

// Generate VTable struct
std::vector<llvm::Constant *> fieldValues;
fieldValues.push_back(llvm::ConstantArray::get(vtableArrayTy, arrayValues));
llvm::Constant *initializer = llvm::ConstantStruct::get(spiceStruct->vtableType, fieldValues);
llvm::Constant *initializer = llvm::ConstantStruct::get(spiceStruct->vTableData.vtableType, fieldValues);

const std::string mangledName = NameMangling::mangleVTable(spiceStruct);
llvm::GlobalVariable *global = module->getNamedGlobal(mangledName);
Expand Down
5 changes: 0 additions & 5 deletions src/irgenerator/OpRuleConversionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1683,11 +1683,6 @@ LLVMExprResult OpRuleConversionManager::callOperatorOverloadFct(const ASTNode *n
if constexpr (N == 2)
argValues[1] = paramTypes[1].isRef() ? opV[3]() : opV[2]();

// Obtain information about the call
const bool isImported = accessScope->isImportedBy(irGenerator->rootScope);
const CodeLoc &callLoc = node->codeLoc;
const CodeLoc &defLoc = opFct->entry->getDeclCodeLoc();

// Function is not defined in the current module -> declare it
if (!irGenerator->module->getFunction(mangledName)) {
// Get returnType
Expand Down
2 changes: 1 addition & 1 deletion src/linker/ExternalLinkerInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace spice::compiler {
class ExternalLinkerInterface {
public:
// Constructors
explicit ExternalLinkerInterface(const CliOptions &cliOptions) : cliOptions(cliOptions), outputPath(cliOptions.outputPath){};
explicit ExternalLinkerInterface(const CliOptions &cliOptions) : outputPath(cliOptions.outputPath), cliOptions(cliOptions){};

// Public methods
void prepare();
Expand Down
4 changes: 2 additions & 2 deletions src/model/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class Function {
// Constructors
Function(std::string name, SymbolTableEntry *entry, SymbolType thisType, SymbolType returnType, ParamList paramList,
std::vector<GenericType> templateTypes, ASTNode *declNode)
: name(std::move(name)), entry(entry), thisType(std::move(thisType)), returnType(std::move(returnType)),
paramList(std::move(paramList)), templateTypes(std::move(templateTypes)), declNode(declNode) {}
: name(std::move(name)), thisType(std::move(thisType)), returnType(std::move(returnType)), paramList(std::move(paramList)),
templateTypes(std::move(templateTypes)), entry(entry), declNode(declNode) {}
Function() = default;

// Public methods
Expand Down
Loading