Skip to content

Commit

Permalink
More conservative use of getcwd and some cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Jan 30, 2025
1 parent b040736 commit 66c33d5
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 72 deletions.
7 changes: 2 additions & 5 deletions src/build/build_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -1381,11 +1381,8 @@ ArchOsTarget arch_os_target_from_string(const char *target)

static const char *check_dir(const char *path)
{
static char *original_path = NULL;
if (!original_path)
{
original_path = getcwd(NULL, 0);
}
char original_path[PATH_MAX + 1];
if (!getcwd(original_path, PATH_MAX)) error_exit("Failed to store path.");
if (!dir_change(path)) error_exit("The path \"%s\" does not point to a valid directory.", path);
if (!dir_change(original_path)) FAIL_WITH_ERR("Failed to change path to %s.", original_path);
return path;
Expand Down
13 changes: 8 additions & 5 deletions src/build/project_creation.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,20 +382,23 @@ static bool check_name(const char *name)

static char* get_cwd_project_name()
{
char *full_path = getcwd(NULL, 0);
char full_path[PATH_MAX];
if (!getcwd(full_path, PATH_MAX)) return NULL;
size_t len = strlen(full_path);
for (size_t i = len; i > 0; i--)
for (size_t i = len - 1; i > 0; i--)
{
switch (full_path[i])
{
case '/':
#if PLATFORM_WINDOWS
case '\\':
#endif
return &full_path[i + 1];
case '/':
return str_copy(&full_path[i + 1], len - i - 1);
default:
break;
}
}
return full_path;
return str_copy(full_path, len);
}

static void chdir_or_fail(BuildOptions *build_options, const char *name)
Expand Down
23 changes: 10 additions & 13 deletions src/compiler/bigint.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// a copy of which can be found in the LICENSE file.

#include "compiler_internal.h"
#include <inttypes.h>
#include <math.h>

UNUSED static inline uint32_t u32_min(uint32_t a, uint32_t b)
Expand Down Expand Up @@ -116,23 +115,21 @@ Int128 i128_from_str(const char *str)

UNUSED Int128 i128_from_strl(const char *str, const char *end)
{
char c;
Int128 x = { 0, 0 };
while (str != end)
{
c = *(str++);
char c = *(str++);
x = i128_add64(i128_mult64(x, 10), (uint64_t) (c - '0'));
}
return x;
}

UNUSED Int128 i128_from_hexstrl(const char *str, const char *end)
{
char c;
Int128 x = { 0, 0 };
while (str != end)
{
c = *(str++);
char c = *(str++);
x = i128_add64(i128_shl64(x, 4), (uint64_t)char_hex_to_nibble(c));
}
return x;
Expand Down Expand Up @@ -198,7 +195,7 @@ Int128 i128_xor(Int128 op1, Int128 op2)

Int128 i128_neg(Int128 op1)
{
if (!op1.low && !op1.low) return op1;
if (!op1.low && !op1.high) return op1;
return i128_add64(i128_not(op1), 1);
}

Expand Down Expand Up @@ -323,7 +320,7 @@ Int128 i128_from_float_unsigned(Real d)
UNUSED bool i128_get_bit(const Int128 *op, int bit)
{
ASSERT(bit < 128 && bit >= 0);
if (bit > 63)
if (bit > 63) // NOLINT
{
return (op->high >> (bit - 64)) & 1;
}
Expand Down Expand Up @@ -442,9 +439,9 @@ static uint32_t ctz64(uint64_t n)
return c;
}

uint32_t i128_ctz(const Int128 *n)
uint32_t i128_ctz(const Int128 *op)
{
return !n->low ? ctz64(n->high) + 64 : ctz64(n->low);
return !op->low ? ctz64(op->high) + 64 : ctz64(op->low);
}

static uint32_t clz64(uint64_t n)
Expand Down Expand Up @@ -886,13 +883,13 @@ Int int_shr64(Int op1, uint64_t op2)
return (Int){ i128_extend(i128_ashr64(op1.i, op2), op1.type), op1.type };
}

Int int_shl64(Int op1, uint64_t op2)
Int int_shl64(Int op, uint64_t op2)
{
if (type_kind_is_unsigned(op1.type))
if (type_kind_is_unsigned(op.type))
{
return (Int){ i128_extend(i128_shl64(op1.i, op2), op1.type), op1.type };
return (Int){ i128_extend(i128_shl64(op.i, op2), op.type), op.type };
}
return (Int){ i128_extend(i128_shl64(op1.i, op2), op1.type), op1.type };
return (Int){ i128_extend(i128_shl64(op.i, op2), op.type), op.type };
}

Real int_to_real(Int op)
Expand Down
22 changes: 7 additions & 15 deletions src/compiler/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
#include "../build/project.h"
#include <compiler_tests/benchmark.h>
#include "../utils/whereami.h"
#if PLATFORM_POSIX
#include <sys/wait.h>
#endif
#if LLVM_AVAILABLE
#include "c3_llvm.h"
#endif
Expand Down Expand Up @@ -347,6 +344,7 @@ void delete_object_files(const char **files, size_t count)
{
for (size_t i = 0; i < count; i++)
{
assert(files);
file_delete_file(files[i]);
}
}
Expand Down Expand Up @@ -479,6 +477,7 @@ void compiler_compile(void)
{
case BACKEND_C:
gen_contexts = c_gen(modules, module_count);
(void)gen_contexts;
error_exit("Unfinished C backend!");
case BACKEND_LLVM:
#if LLVM_AVAILABLE
Expand Down Expand Up @@ -936,7 +935,6 @@ void print_syntax(BuildOptions *options)

if (options->print_keywords)
{
int index = 1;
for (int i = 1; i < TOKEN_LAST; i++)
{
const char *name = token_type_to_string((TokenType)i);
Expand All @@ -949,7 +947,6 @@ void print_syntax(BuildOptions *options)
}
if (options->print_operators)
{
int index = 1;
for (int i = 1; i < TOKEN_LAST; i++)
{
if (i == TOKEN_DOCS_START || i == TOKEN_DOCS_END) continue;
Expand Down Expand Up @@ -991,7 +988,6 @@ void print_syntax(BuildOptions *options)
}
if (options->print_project_properties)
{
int width;
puts("Project properties");
puts("------------------");
for (int i = 0; i < project_default_keys_count; i++)
Expand All @@ -1009,7 +1005,6 @@ void print_syntax(BuildOptions *options)
}
if (options->print_manifest_properties)
{
int width;
puts("Manifest properties");
puts("------------------");
for (int i = 0; i < manifest_default_keys_count; i++)
Expand All @@ -1035,10 +1030,10 @@ void print_syntax(BuildOptions *options)
puts(" 4. Mult | * / %");
puts(" 5. Shift | << >>");
puts(" 6. Bitwise | ^ | &");
puts(" 7. Additive | + -");
puts(" 7. Additive | + - +++");
puts(" 8. Relational | < > <= >= == !=");
puts(" 9. And | &&");
puts("10. Or | ||");
puts(" 9. And | && &&&");
puts("10. Or | || |||");
puts("11. Ternary | ?: ??");
puts("12. Assign | = *= /= %= -= += |= &= ^= <<= >>=");
}
Expand Down Expand Up @@ -1111,13 +1106,11 @@ void execute_scripts(void)
{
error_exit("This target has 'exec' directives, to run it trust level must be set to '--trust=full'.");
}
char *old_path = NULL;
char old_path[PATH_MAX + 1];
if (compiler.build.script_dir)
{
old_path = getcwd(NULL, 0);
if (!dir_change(compiler.build.script_dir))
if (getcwd(old_path, PATH_MAX) && !dir_change(compiler.build.script_dir))
{
free(old_path);
error_exit("Failed to open script dir '%s'", compiler.build.script_dir);
}
}
Expand Down Expand Up @@ -1146,7 +1139,6 @@ PRINT_SCRIPT:;
}
}
dir_change(old_path);
free(old_path);
}

static void check_sanitizer_options(BuildTarget *target)
Expand Down
31 changes: 9 additions & 22 deletions src/compiler/sema_initializers.c
Original file line number Diff line number Diff line change
Expand Up @@ -824,13 +824,10 @@ bool sema_expr_analyse_initializer_list(SemaContext *context, Type *to, Expr *ex
expr->type = to;
return true;
}
else
{
expr->resolve_status = RESOLVE_DONE;
expr_insert_addr(expr);
if (!sema_analyse_expr(context, expr)) return false;
return cast_explicit(context, expr, to);
}
expr->resolve_status = RESOLVE_DONE;
expr_insert_addr(expr);
if (!sema_analyse_expr(context, expr)) return false;
return cast_explicit(context, expr, to);
}
case TYPE_POINTER:
case TYPE_FUNC_PTR:
Expand Down Expand Up @@ -1049,20 +1046,19 @@ static inline void sema_update_const_initializer_with_designator_union(ConstInit
}

static inline ConstInitializer *sema_update_const_initializer_at_index(ConstInitializer *const_init, Type *element_type,
ArraySize array_count, ArrayIndex index,
ArrayIndex *insert_index_ref, Expr *value)
ArrayIndex index,
ArrayIndex *insert_index_ref)
{
ConstInitializer **array_elements = const_init->init_array.elements;
ArrayIndex insert_index = *insert_index_ref;
ASSERT(insert_index >= array_count || array_elements);
ArrayIndex array_count = vec_size(array_elements);
// Walk to the insert point or until we reached the end of the array.
while (insert_index < array_count && array_elements[insert_index]->init_array_value.index < index)
{
insert_index++;
}
// Pick up the initializer at the insert point.
ConstInitializer *initializer = insert_index < array_count ? array_elements[insert_index] : NULL;
ConstInitializer *inner_value;

// If we don't have an initializer, the location needs to be at the end.
// Create and append:
Expand Down Expand Up @@ -1111,14 +1107,8 @@ void const_init_rewrite_array_at(ConstInitializer *const_init, Expr *value, Arra

Type *element_type = type_flatten(const_init->type->array.base);

// Get all the elements in the array
ConstInitializer **array_elements = const_init->init_array.elements;

unsigned array_count = vec_size(array_elements);
ArrayIndex insert_index = 0;

ConstInitializer *inner_value = sema_update_const_initializer_at_index(const_init, element_type,
array_count, index, &insert_index, value);
ConstInitializer *inner_value = sema_update_const_initializer_at_index(const_init, element_type, index, &insert_index);

const_init_rewrite_to_value(inner_value, value);
}
Expand Down Expand Up @@ -1149,14 +1139,11 @@ static inline void sema_update_const_initializer_with_designator_array(ConstInit

// Get all the elements in the array

unsigned array_count = vec_size(const_init->init_array.elements);
ArrayIndex insert_index = 0;

for (ArrayIndex index = low_index; index <= high_index; index++)
{
ConstInitializer *inner_value = sema_update_const_initializer_at_index(const_init, element_type,
array_count, index, &insert_index,
value);
ConstInitializer *inner_value = sema_update_const_initializer_at_index(const_init, element_type, index, &insert_index);

// Update
if (!is_last_path_element)
Expand Down
5 changes: 2 additions & 3 deletions src/compiler/sema_passes.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,13 @@ static Decl **sema_run_exec(CompilationUnit *unit, Decl *decl)
}
File *file;
// TODO fix Win32
char old_path_buffer[PATH_MAX];
char *old_path = NULL;
if (compiler.build.script_dir)
{
old_path = getcwd(NULL, 0);
old_path = getcwd(old_path, PATH_MAX);
if (!dir_change(compiler.build.script_dir))
{
free(old_path);
RETURN_PRINT_ERROR_AT(NULL, decl, "Failed to open script dir '%s'", compiler.build.script_dir);
}
}
Expand All @@ -302,7 +302,6 @@ static Decl **sema_run_exec(CompilationUnit *unit, Decl *decl)
if (old_path)
{
success = dir_change(old_path);
free(old_path);
if (!success)
{
RETURN_PRINT_ERROR_AT(NULL, decl, "Failed to open run dir '%s'", compiler.build.script_dir);
Expand Down
7 changes: 0 additions & 7 deletions src/compiler/source_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
// a copy of which can be found in the LICENSE file.

#include <sys/stat.h>
#ifdef _MSC_VER

#define PATH_MAX 260

#else
#include <limits.h>
#endif
#include "compiler_internal.h"

static const size_t LEXER_FILES_START_CAPACITY = 128;
Expand Down
7 changes: 5 additions & 2 deletions src/utils/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
#ifndef _MSC_VER
#ifdef _MSC_VER
#define PATH_MAX 260
#else
#include <unistd.h>
#include <limits.h>
#endif

#define NO_ARENA 0
Expand Down Expand Up @@ -111,7 +114,7 @@
"https://github.com/c3lang/c3c/issues/new so that we can get it fixed.", _string, __func__, __FILE__, __LINE__, ##__VA_ARGS__); } while(0)


#define ASSERT(_condition) while (!(_condition)) { FATAL_ERROR("Violated assert: " #_condition); }
#define ASSERT(_condition) do { if (!(_condition)) { FATAL_ERROR("Violated assert: " #_condition); } } while (0)
#define WARNING(_string, ...) do { eprintf("WARNING: "); eprintf(_string, ##__VA_ARGS__); eprintf("\n"); } while(0)
#define UNREACHABLE FATAL_ERROR("Should be unreachable");

Expand Down

0 comments on commit 66c33d5

Please sign in to comment.