Skip to content

Commit

Permalink
Add a builtin for checking the value of compile-time options.
Browse files Browse the repository at this point in the history
Use it to fix the #14173 test in the case of MEMDEBUG.
  • Loading branch information
maleadt committed Aug 18, 2016
1 parent adecd73 commit 28161bb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,34 @@ JL_DLLEXPORT void jl_breakpoint(jl_value_t *v)
// put a breakpoint in your debugger here
}

// check whether a compile-time option is defined, returning a boolean value
JL_DLLEXPORT int8_t jl_get_option(jl_value_t *v)
{
// fragile set of macros to detect whether an option is defined.
// only works for non-valued definitions, ie. expanding to nothing if defined
#define JL_DEFINED(option) JL_DEFINED_##option
#define JL_DEFINED_ 1

#define JL_HANDLE_OPTION(input, option) \
const int8_t JL_DEFINED_##option = 0; \
if (strcmp(input, #option) == 0) { \
return JL_DEFINED(option); \
}

if (!jl_is_symbol(v))
jl_error("option should be a symbol");
char *key = jl_symbol_name((jl_sym_t*) v);

// dummy options for testing purposes
#define ALWAYS_DEFINED
JL_HANDLE_OPTION(key, ALWAYS_DEFINED);
JL_HANDLE_OPTION(key, NEVER_DEFINED);

JL_HANDLE_OPTION(key, MEMDEBUG);

jl_errorf("unknown option '%s'", key);
}

#ifdef __cplusplus
}
#endif
7 changes: 7 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4476,3 +4476,10 @@ function f18054()
return Cint(0)
end
cfunction(f18054, Cint, ())

# jl_get_option
@test_throws ErrorException ccall(:jl_get_option, Bool, (Any,), "invalid")
@test ccall(:jl_get_option, Bool, (Any,), :ALWAYS_DEFINED) == true
@test ccall(:jl_get_option, Bool, (Any,), :NEVER_DEFINED) == false
@test_throws ErrorException ccall(:jl_get_option, Bool, (Any,), :UNHANDLED) == true

3 changes: 2 additions & 1 deletion test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ module Tmp14173
A = randn(2000, 2000)
end
whos(IOBuffer(), Tmp14173) # warm up
@test @allocated(whos(IOBuffer(), Tmp14173)) < 10000
const MEMDEBUG = ccall(:jl_get_option, Bool, (Any,), :MEMDEBUG)
@test @allocated(whos(IOBuffer(), Tmp14173)) < (MEMDEBUG ? 20000 : 8000)

## test conversion from UTF-8 to UTF-16 (for Windows APIs)

Expand Down

0 comments on commit 28161bb

Please sign in to comment.