Skip to content

RFC: introduce check macro that can be disabled with a flag #25576

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,7 @@ export
@polly,

@assert,
@check,
@__dot__,
@enum,
@label,
Expand Down
1 change: 1 addition & 0 deletions base/options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct JLOptions
opt_level::Int8
debug_level::Int8
check_bounds::Int8
julia_debug::Int8
depwarn::Int8
warn_overwrite::Int8
can_inline::Int8
Expand Down
2 changes: 1 addition & 1 deletion base/pkg/pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ init(meta::AbstractString=DEFAULT_META, branch::AbstractString=META_BRANCH) = Di

function __init__()
vers = "v$(VERSION.major).$(VERSION.minor)"
pushfirst!(Base.LOAD_CACHE_PATH, abspath(Dir._pkgroot(), "lib", vers))
pushfirst!(Base.LOAD_CACHE_PATH, abspath(Dir._pkgroot(), "lib", Base.JLOptions().julia_debug == 1 ? "debug" : ""), vers)
end

"""
Expand Down
8 changes: 8 additions & 0 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,14 @@ function __init__()
init_threadcall()
end

macro check(x)
if JLOptions().julia_debug == 1
return :($(esc(x)))
else
return nothing
end
end

INCLUDE_STATE = 3 # include = include_relative

end # baremodule Base
Expand Down
4 changes: 4 additions & 0 deletions doc/man/julia.1
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ Control whether inlining is permitted (overrides functions declared as @inline)
--check-bounds={yes|no}
Emit bounds checks always or never (ignoring declarations)

.TP
--debug={yes|no}
Enable the @check macro and always emit bounds checks

.TP
--math-mode={ieee|user}
Always use IEEE semantics for math (ignoring declarations),
Expand Down
18 changes: 18 additions & 0 deletions src/jloptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ jl_options_t jl_options = { 0, // quiet
1, // debug_level [release build]
#endif
JL_OPTIONS_CHECK_BOUNDS_DEFAULT, // check_bounds
#ifdef JL_DEBUG_BUILD
1, // julia debug mode [debug build]
#else
0, // julia debug mode [release build]
#endif
JL_OPTIONS_DEPWARN_ON, // deprecation warning
0, // method overwrite warning
1, // can_inline
Expand Down Expand Up @@ -118,6 +123,7 @@ static const char opts[] =
#endif
" --inline={yes|no} Control whether inlining is permitted, including overriding @inline declarations\n"
" --check-bounds={yes|no} Emit bounds checks always or never (ignoring declarations)\n"
" --debug={yes|no} Enable the @check macro and always emit bounds checks\n"
#ifdef USE_POLLY
" --polly={yes|no} Enable or disable the polyhedral optimizer Polly (overrides @polly declaration)\n"
#endif
Expand Down Expand Up @@ -151,6 +157,7 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
opt_code_coverage,
opt_track_allocation,
opt_check_bounds,
opt_julia_debug,
opt_output_jit_bc,
opt_output_unopt_bc,
opt_output_bc,
Expand Down Expand Up @@ -200,6 +207,7 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
{ "track-allocation",optional_argument, 0, opt_track_allocation },
{ "optimize", optional_argument, 0, 'O' },
{ "check-bounds", required_argument, 0, opt_check_bounds },
{ "debug", required_argument, 0, opt_julia_debug },
{ "output-bc", required_argument, 0, opt_output_bc },
{ "output-unopt-bc", required_argument, 0, opt_output_unopt_bc },
{ "output-jit-bc", required_argument, 0, opt_output_jit_bc },
Expand Down Expand Up @@ -489,6 +497,16 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
else
jl_errorf("julia: invalid argument to --check-bounds={yes|no} (%s)", optarg);
break;
case opt_julia_debug:
if (!strcmp(optarg,"yes")) {
jl_options.check_bounds = 1;
jl_options.check_bounds = JL_OPTIONS_CHECK_BOUNDS_ON;
}
else if (!strcmp(optarg,"no"))
jl_options.check_bounds = 0;
else
jl_errorf("julia: invalid argument to --debug={yes|no} (%s)", optarg);
break;
case opt_output_bc:
jl_options.outputbc = optarg;
if (!jl_options.image_file_specified) jl_options.image_file = NULL;
Expand Down
1 change: 1 addition & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -1716,6 +1716,7 @@ typedef struct {
int8_t malloc_log;
int8_t opt_level;
int8_t debug_level;
int8_t julia_debug;
int8_t check_bounds;
int8_t depwarn;
int8_t warn_overwrite;
Expand Down