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
Add clang-tidy run on zig sources, minor CMake improvements #993
Conversation
b359184
to
442bf34
Compare
Thanks for the PR. Did you find anything worth fixing with clang-tidy? |
There were a lot of warnings so I was going to go through them for another PR. But I'd be happy to add them here. |
Actually took a look now and see a lot of potential memory leaks in the analyze.cpp file. |
Is there a reason the structs in the compiler don't have destructors (i.e. |
valgrind confirms this:
|
The c++ compiler treats memory as one big ArenaAllocator. It all gets freed when the compiler exits and finishes the job. We don't bother freeing memory before this. The self-hosted compiler has different requirements - it will be used as a long running process that will perform multiple services, for example compilation every time a file on disk changes, or implementing Language Server Protocol. |
I see. OK let me see if I can change that analysis pass then. |
@@ -1037,7 +1037,7 @@ static AstNode *ast_parse_fn_proto_partial(ParseContext *pc, size_t *token_index | |||
} else if (next_token->id == TokenIdBang) { | |||
*token_index += 1; | |||
node->data.fn_proto.auto_err_set = true; | |||
next_token = &pc->tokens->at(*token_index); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dead store
@@ -1025,7 +1025,7 @@ static AstNode *ast_parse_fn_proto_partial(ParseContext *pc, size_t *token_index | |||
if (next_token->id == TokenIdKeywordVar) { | |||
node->data.fn_proto.return_var_token = next_token; | |||
*token_index += 1; | |||
next_token = &pc->tokens->at(*token_index); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dead store
@@ -18440,7 +18457,7 @@ static void buf_read_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue | |||
case TypeTableEntryIdVoid: | |||
return; | |||
case TypeTableEntryIdBool: | |||
val->data.x_bool = (buf[0] != 0); | |||
val->data.x_bool = (buf[0] != 0); // NOLINT(clang-analyzer-core.UndefinedBinaryOperatorResult) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ignore warning for uninitialized value. Can occur if this is within uninitialized array (or at least that is my interpretation.
case ConstPtrSpecialFunction: | ||
zig_panic("TODO"); | ||
} else { | ||
assert(parent_ptr != nullptr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assert parent is not null.
@@ -17433,29 +17446,33 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio | |||
ptr_val->data.x_ptr.mut = parent_ptr->data.x_ptr.mut; | |||
} | |||
} else if (ptr_is_undef) { | |||
assert(ptr_val != nullptr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assert pointer is not null.
@@ -12027,7 +12030,7 @@ static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node | |||
|
|||
static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_node, | |||
IrInstruction *arg, Scope **child_scope, size_t *next_proto_i, | |||
GenericFnTypeId *generic_id, FnTypeId *fn_type_id, IrInstruction **casted_args, | |||
GenericFnTypeId *generic_id, FnTypeId *fn_type_id, IrInstruction ***casted_args, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need triple pointer to return pointer to allocated memory.
@@ -1116,8 +1117,10 @@ static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *sourc | |||
|
|||
if (fn_ref) | |||
ir_ref_instruction(fn_ref, irb->current_basic_block); | |||
for (size_t i = 0; i < arg_count; i += 1) | |||
for (size_t i = 0; i < arg_count; i += 1) { | |||
assert(args != nullptr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If arg_count > 0
, then args
must not be null.
@@ -5828,20 +5827,20 @@ static const uint8_t int_sizes_in_bits[] = { | |||
}; | |||
|
|||
struct CIntTypeInfo { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optimize struct padding.
@@ -4306,7 +4306,7 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f | |||
LLVMValueRef alloc_fn_val = LLVMGetParam(fn_val, next_arg); | |||
next_arg += 1; | |||
|
|||
LLVMValueRef stack_trace_val; | |||
LLVMValueRef stack_trace_val = nullptr; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix issue with using uninitialized value.
@@ -4317,7 +4317,6 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f | |||
LLVMValueRef err_code_ptr = LLVMGetParam(fn_val, next_arg); | |||
next_arg += 1; | |||
LLVMValueRef coro_size = LLVMGetParam(fn_val, next_arg); | |||
next_arg += 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dead store.
I think this is largely a distraction.
I appreciate your efforts @isaachier. I encourage you to redirect your energy toward more rewarding things. There are a lot of cool things to work on - feel free to hop on IRC if you want to discuss some ideas. |
I definitely understand. It has been interesting exploring this code, but I agree this is more trouble than its worth. |
CMAKE_CURRENT_(SOURCE|BINARY)_DIR
instead ofCMAKE_(SOURCE|BINARY)_DIR
.ENABLE_CLANG_TIDY
option. Added.clang-tidy
file as config for this analysis.