Skip to content

Commit

Permalink
Makes sure assertion respects JL_NDEBUG in C++ code
Browse files Browse the repository at this point in the history
This generalizes `fix_llvm_assert.h` to `julia_assert.h` and replace (almost) all uses of
`cassert` and `assert.h` with `julia_assert.h`.
Changes the rule for including `julia_assert.h` so that includes of `assert.h` from external
headers (including `libsupport`) won't accidientally disables assertion.
Without this patch, the `assert.h` included in `libsupport.h` (in `julia.h`) may disable
assertions in our c++ code.
  • Loading branch information
yuyichao committed Sep 2, 2017
1 parent 92ff781 commit 45b0013
Show file tree
Hide file tree
Showing 58 changed files with 104 additions and 75 deletions.
3 changes: 1 addition & 2 deletions src/APInt-C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
#include <llvm/ADT/APFloat.h>
#include <llvm/Support/MathExtras.h>

#include "fix_llvm_assert.h"

#include "APInt-C.h"
#include "julia.h"
#include "julia_assert.h"

using namespace llvm;

Expand Down
4 changes: 2 additions & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ LLVM_LIBS := support
endif

# headers are used for dependency tracking, while public headers will be part of the dist
HEADERS := $(BUILDDIR)/julia_version.h $(wildcard $(SRCDIR)/support/*.h) $(addprefix $(SRCDIR)/,julia.h julia_threads.h tls.h locks.h atomics.h julia_internal.h options.h timing.h)
PUBLIC_HEADERS := $(BUILDDIR)/julia_version.h $(wildcard $(SRCDIR)/support/*.h) $(addprefix $(SRCDIR)/,julia.h julia_threads.h tls.h locks.h atomics.h)
HEADERS := $(BUILDDIR)/julia_version.h $(wildcard $(SRCDIR)/support/*.h) $(addprefix $(SRCDIR)/,julia.h julia_assert.h julia_threads.h tls.h locks.h atomics.h julia_internal.h options.h timing.h)
PUBLIC_HEADERS := $(BUILDDIR)/julia_version.h $(wildcard $(SRCDIR)/support/*.h) $(addprefix $(SRCDIR)/,julia.h julia_assert.h julia_threads.h tls.h locks.h atomics.h)
ifeq ($(USE_SYSTEM_LIBUV),0)
ifeq ($(OS),WINNT)
HEADERS += $(build_includedir)/tree.h
Expand Down
4 changes: 2 additions & 2 deletions src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#ifdef _OS_WINDOWS_
#include <malloc.h>
#endif
#include "julia.h"
#include "julia_internal.h"
#include "julia_assert.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -157,7 +157,7 @@ jl_array_t *jl_new_array_for_deserialization(jl_value_t *atype, uint32_t ndims,
return _new_array_(atype, ndims, dims, isunboxed, elsz);
}

#ifndef NDEBUG
#ifndef JL_NDEBUG
static inline int is_ntuple_long(jl_value_t *v)
{
if (!jl_is_tuple(v))
Expand Down
2 changes: 1 addition & 1 deletion src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#ifdef _OS_WINDOWS_
#include <malloc.h>
#endif
#include "julia.h"
#include "julia_internal.h"
#include "flisp.h"
#include "julia_assert.h"

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <string.h>
#include <stdarg.h>
#include <setjmp.h>
#include <assert.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
Expand All @@ -25,6 +24,7 @@
#include "julia_internal.h"
#include "builtin_proto.h"
#include "intrinsics.h"
#include "julia_assert.h"

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion src/cgmemmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "options.h"

#include <llvm/ExecutionEngine/SectionMemoryManager.h>
#include "fix_llvm_assert.h"
#include "julia.h"
#include "julia_internal.h"

Expand All @@ -25,6 +24,7 @@
#ifdef _OS_FREEBSD_
# include <sys/types.h>
#endif
#include "julia_assert.h"

namespace {

Expand Down
3 changes: 1 addition & 2 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include <vector>
#include <set>
#include <cstdio>
#include <cassert>
#include <iostream>
#include <functional>

Expand Down Expand Up @@ -85,7 +84,6 @@
#include <polly/ScopDetection.h>
#endif
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include "fix_llvm_assert.h"

using namespace llvm;
namespace llvm {
Expand All @@ -100,6 +98,7 @@ namespace llvm {
#include "julia_internal.h"
#include "jitlayers.h"
#include "codegen_shared.h"
#include "julia_assert.h"

// LLVM version compatibility macros
legacy::PassManager *jl_globalPM;
Expand Down
2 changes: 1 addition & 1 deletion src/datatype.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include "julia.h"
#include "julia_internal.h"
#include "julia_assert.h"

#ifdef __cplusplus
extern "C" {
Expand Down
3 changes: 1 addition & 2 deletions src/debuginfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <llvm/Object/MachO.h>
#include <llvm/Object/COFF.h>
#include <llvm/Object/ELFObjectFile.h>
#include "fix_llvm_assert.h"

using namespace llvm;

Expand All @@ -46,7 +45,7 @@ using llvm_file_magic = sys::fs::file_magic;
#include <vector>
#include <set>
#include <cstdio>
#include <cassert>
#include "julia_assert.h"

typedef object::SymbolRef SymRef;

Expand Down
2 changes: 1 addition & 1 deletion src/disasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@
#include <llvm/IR/Module.h>
#include <llvm/IR/IntrinsicInst.h>
#include "llvm/IR/AssemblyAnnotationWriter.h"
#include "fix_llvm_assert.h"

#include "julia.h"
#include "julia_internal.h"

using namespace llvm;
#include "debuginfo.h"
#include "julia_assert.h"

// helper class for tracking inlining context while printing debug info
class DILineInfoPrinter {
Expand Down
2 changes: 1 addition & 1 deletion src/dlload.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/stat.h>

#include "platform.h"
Expand All @@ -16,6 +15,7 @@
#include <unistd.h>
#include <dlfcn.h>
#endif
#include "julia_assert.h"

#ifdef __cplusplus
extern "C" {
Expand Down
7 changes: 3 additions & 4 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "julia.h"
#include "julia_internal.h"
Expand All @@ -20,6 +19,7 @@
#else
#define RUNNING_ON_VALGRIND 0
#endif
#include "julia_assert.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -2240,15 +2240,14 @@ JL_DLLEXPORT void jl_fill_argnames(jl_array_t *data, jl_array_t *names)
}
else {
uint8_t *d = (uint8_t*)data->data;
#ifndef NDEBUG
assert(jl_typeis(data, jl_array_uint8_type));
int b3 = d[1];
int b2 = d[2];
int b1 = d[3];
int b0 = d[4];
int nslots = b0 | (b1<<8) | (b2<<16) | (b3<<24);
assert(nslots >= nargs);
#endif
(void)nslots;
char *namestr = (char*)d + 5;
for (i = 0; i < nargs; i++) {
size_t namelen = strlen(namestr);
Expand Down Expand Up @@ -2312,7 +2311,7 @@ JL_DLLEXPORT int jl_save_incremental(const char *fname, jl_array_t *worklist)
return 0;
}

#ifndef NDEBUG
#ifndef JL_NDEBUG
// skip the performance optimizations of jl_types_equal and just use subtyping directly
// one of these types is invalid - that's why we're doing the recache type operation
static int jl_invalid_types_equal(jl_datatype_t *a, jl_datatype_t *b)
Expand Down
16 changes: 0 additions & 16 deletions src/fix_llvm_assert.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/gc-debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// so that we can always use the assert macro in this file
// for use under their respective enable flags
#undef NDEBUG
#include <assert.h>
#include "julia_assert.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions src/gc-pages.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#ifndef _OS_WINDOWS_
# include <sys/resource.h>
#endif
#include "julia_assert.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions src/gc.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This file is a part of Julia. License is MIT: https://julialang.org/license

#include "gc.h"
#include "julia_assert.h"

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion src/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#ifndef _MSC_VER
#include <strings.h>
#endif
#include <assert.h>
#include <inttypes.h>
#include "julia.h"
#include "julia_internal.h"
Expand All @@ -25,6 +24,7 @@
#define MAP_ANONYMOUS MAP_ANON
#endif
#endif
#include "julia_assert.h"

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion src/gf.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "julia.h"
#include "julia_internal.h"
#ifndef _OS_WINDOWS_
#include <unistd.h>
#endif
#include "julia_assert.h"

// @nospecialize has no effect if the number of overlapping methods is greater than this
#define MAX_UNSPECIALIZED_CONFLICTS 32
Expand Down
2 changes: 1 addition & 1 deletion src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <fcntl.h>

#include <errno.h>
Expand All @@ -24,6 +23,7 @@
#include "builtin_proto.h"
#undef DEFINE_BUILTIN_GLOBALS
#include "threading.h"
#include "julia_assert.h"

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

#include <stdlib.h>
#include <setjmp.h>
#include <assert.h>
#ifdef _OS_WINDOWS_
#include <malloc.h>
#endif
#include "julia.h"
#include "julia_internal.h"
#include "builtin_proto.h"
#include "julia_assert.h"

#ifdef __cplusplus
extern "C" {
Expand Down
3 changes: 2 additions & 1 deletion src/jitlayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ namespace llvm {
#include <llvm/ADT/StringSet.h>
#include <llvm/ADT/SmallSet.h>
#include "codegen_shared.h"
#include "fix_llvm_assert.h"

using namespace llvm;

#include "julia.h"
#include "julia_internal.h"
#include "jitlayers.h"
#include "julia_assert.h"

RTDyldMemoryManager* createRTDyldMemoryManager(void);

static Type *T_void;
Expand Down
2 changes: 1 addition & 1 deletion src/jitlayers.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
extern legacy::PassManager *jl_globalPM;

#include <llvm/Target/TargetMachine.h>
#include "fix_llvm_assert.h"
#include "julia_assert.h"

extern "C" {
extern int globalUnique;
Expand Down
3 changes: 2 additions & 1 deletion src/jl_uv.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#ifdef _OS_WINDOWS_
#include <ws2tcpip.h>
Expand All @@ -28,6 +27,8 @@
#define write _write
#endif

#include "julia_assert.h"

#ifdef __cplusplus
#include <cstring>
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion src/jlapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "julia.h"
#include "options.h"
#include "julia_assert.h"

#ifdef __cplusplus
#include <cfenv>
Expand Down
1 change: 1 addition & 0 deletions src/jloptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#else
#include "getopt.h"
#endif
#include "julia_assert.h"

#ifdef _OS_WINDOWS_
char *shlib_ext = ".dll";
Expand Down
Loading

0 comments on commit 45b0013

Please sign in to comment.