Skip to content

Commit

Permalink
Fix bug where analysing subexpr relied on them not being analysed. Fi…
Browse files Browse the repository at this point in the history
…x issue where converting a const initializer bool to integer failed. Fix of issue where the case check assumed other cases were const values.
  • Loading branch information
lerno committed Jul 16, 2023
1 parent 89e0849 commit ebf50b1
Show file tree
Hide file tree
Showing 10 changed files with 389 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ jobs:
install: git binutils mingw-w64-x86_64-ninja mingw-w64-x86_64-cmake mingw-w64-x86_64-toolchain mingw-w64-x86_64-python
- shell: msys2 {0}
run: |
pacman --noconfirm -U https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-llvm-15.0.3-1-any.pkg.tar.zst
pacman --noconfirm -U https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-lld-15.0.3-1-any.pkg.tar.zst
pacman --noconfirm -U https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-llvm-16.0.5-1-any.pkg.tar.zst
pacman --noconfirm -U https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-lld-16.0.5-1-any.pkg.tar.zst
- name: CMake
run: |
cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
Expand Down
27 changes: 18 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@ if(MSVC)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Od /Zi /EHsc")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /Od /Zi /EHsc")
else()
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -fno-exceptions")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -gdwarf-3 -fno-exceptions")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -gdwarf-3 -O3 -fno-exceptions")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -gdwarf-3 -fno-exceptions")
if (true)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -fno-exceptions")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -gdwarf-3 -fno-exceptions")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -gdwarf-3 -O3 -fno-exceptions")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -gdwarf-3 -fno-exceptions")
else()
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O1 -fsanitize=undefined -fno-exceptions")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O1 -fsanitize=undefined -fno-exceptions")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -gdwarf-3 -O3 -fsanitize=undefined -fno-exceptions")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -gdwarf-3 -fsanitize=undefined -fno-exceptions")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined -fno-exceptions")
endif()
endif()
#set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O1 -fsanitize=undefined")
#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O1 -fsanitize=undefined")
#set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -gdwarf-3 -O3 -fsanitize=undefined")
#set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -gdwarf-3 -fsanitize=undefined")
#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")



set(C3_LLVM_VERSION "auto" CACHE STRING "Use LLVM version [default: auto]")
option(C3_USE_MIMALLOC "Use built-in mimalloc" OFF)
Expand Down Expand Up @@ -265,6 +270,7 @@ add_executable(c3c
src/utils/malloc.c
src/utils/stringutils.c
src/utils/taskqueue.c
src/utils/stacktrace.c
src/utils/json.c
src/build/project.c
src/utils/vmem.c
Expand Down Expand Up @@ -341,6 +347,9 @@ endif()

if (WIN32)
target_link_libraries(c3c Winhttp.lib)
target_link_libraries(c3c imagehlp.lib)
elseif(APPLE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-pie")
endif()

if (CURL_FOUND)
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ static void free_arenas(void)
if (debug_stats) print_arena_status();
}

static void compiler_print_bench(void)
void compiler_print_bench(void)
{
if (debug_stats)
{
Expand Down
1 change: 1 addition & 0 deletions src/compiler/number.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ static inline bool compare_fps(Real left, Real right, BinaryOp op)
bool expr_const_compare(const ExprConst *left, const ExprConst *right, BinaryOp op)
{
bool is_eq;
assert(left->const_kind == right->const_kind);
switch (left->const_kind)
{
case CONST_BOOL:
Expand Down
1 change: 1 addition & 0 deletions src/compiler/sema_casts.c
Original file line number Diff line number Diff line change
Expand Up @@ -1772,6 +1772,7 @@ static void vector_const_initializer_convert_to_type(ConstInitializer *initializ
if (is_neg_conversion)
{
bool is_true = initializer->init_value->const_expr.b;
initializer->init_value->const_expr.const_kind = CONST_INTEGER;
initializer->init_value->const_expr.ixx = (Int)
{ .i = is_true ? (Int128) { UINT64_MAX, UINT64_MAX } : (Int128) { 0, 0 },
.type = to_flat->type_kind };
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/sema_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ static inline bool sema_binary_promote_top_down(SemaContext *context, Expr *bina
static inline bool sema_binary_analyse_subexpr(SemaContext *context, Expr *binary, Expr *left, Expr *right)
{
// Special handling of f = FOO_BAR
if (right->expr_kind == EXPR_IDENTIFIER && right->identifier_expr.is_const)
if (right->resolve_status != RESOLVE_DONE && right->expr_kind == EXPR_IDENTIFIER && right->identifier_expr.is_const)
{
if (!sema_analyse_expr(context, left)) return false;
if (type_flatten(left->type)->type_kind == TYPE_ENUM)
Expand All @@ -1031,7 +1031,7 @@ static inline bool sema_binary_analyse_subexpr(SemaContext *context, Expr *binar
}
}
// Special handling of f = FOO_BAR
if (left->expr_kind == EXPR_IDENTIFIER && left->identifier_expr.is_const)
if (left->resolve_status != RESOLVE_DONE && left->expr_kind == EXPR_IDENTIFIER && left->identifier_expr.is_const)
{
if (!sema_analyse_expr(context, right)) return false;
if (type_flatten(right->type)->type_kind == TYPE_ENUM)
Expand Down
4 changes: 3 additions & 1 deletion src/compiler/sema_stmts.c
Original file line number Diff line number Diff line change
Expand Up @@ -2083,7 +2083,9 @@ static inline bool sema_check_value_case(SemaContext *context, Type *switch_type
{
Ast *other = cases[i];
if (other->ast_kind != AST_CASE_STMT) continue;
ExprConst *other_const = &exprptr(other->case_stmt.expr)->const_expr;
Expr *other_expr = exprptr(other->case_stmt.expr);
if (!expr_is_const(other_expr)) continue;
ExprConst *other_const = &other_expr->const_expr;
ExprConst *other_to_const = other->case_stmt.to_expr ? &exprptr(other->case_stmt.to_expr)->const_expr : other_const;
if (expr_const_in_range(const_expr, other_const, other_to_const))
{
Expand Down
1 change: 1 addition & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ int main_real(int argc, const char *argv[])
"------------------------------------------------------------\n");
bench_begin();

install_stacktrace(argv[0]);
// Setjmp will allow us to add things like fuzzing with
// easy restarts.
int result = setjmp(on_error_jump);
Expand Down
4 changes: 3 additions & 1 deletion src/utils/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@

#ifdef _MSC_VER
#define PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop))
#endif
#endif

void install_stacktrace(const char *program_name);
Loading

0 comments on commit ebf50b1

Please sign in to comment.