Skip to content

Commit

Permalink
Merge pull request #17 from RelationalAI/adding-yield
Browse files Browse the repository at this point in the history
Adding yield
  • Loading branch information
bergel authored Feb 28, 2024
2 parents fa1524f + 69b8527 commit 58c3e03
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ const essential_filters = [no_filters; [StaticLint.MissingReference, StaticLint.


# Return (line, column) for a given offset in a source
function convert_offset_to_line_from_filename(offset::Int64, filename::String)
function convert_offset_to_line_from_filename(offset::Int, filename::String)
all_lines = open(io->readlines(io), filename)
return convert_offset_to_line_from_lines(offset, all_lines)
end

function convert_offset_to_line(offset::Int64, source::String)
function convert_offset_to_line(offset::Int, source::String)
return convert_offset_to_line_from_lines(offset, split(source, "\n"))
end


function convert_offset_to_line_from_lines(offset::Int64, all_lines)
function convert_offset_to_line_from_lines(offset::Int, all_lines)
offset < 0 && throw(BoundsError("source", offset))

current_index = 1
Expand Down
2 changes: 1 addition & 1 deletion src/linting/checks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function check_all(x::EXPR, opts::LintOptions, env::ExternalEnv, markers::Dict{S
check_const(x)

if opts.extended
for T in all_extended_rule_types
for T in all_extended_rule_types[]
check(T(), x, markers)
end
end
Expand Down
13 changes: 12 additions & 1 deletion src/linting/extended_checks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ struct ReentrantLock_Extension <: ExtendedRule end
struct SpinLock_Extension <: ExtendedRule end
struct Lock_Extension <: ExtendedRule end
struct Unlock_Extension <: ExtendedRule end
struct Yield_Extension <: ExtendedRule end
struct Sleep_Extension <: ExtendedRule end
struct Mmap_Extension <: ExtendedRule end

const all_extended_rule_types = InteractiveUtils.subtypes(ExtendedRule)
const all_extended_rule_types = Ref{Any}(InteractiveUtils.subtypes(ExtendedRule))

# template -> EXPR to be compared
const check_cache = Dict{String, CSTParser.EXPR}()
Expand All @@ -65,6 +68,7 @@ const error_msgs = Dict{String, String}()
function reset_static_lint_caches()
empty!(check_cache)
empty!(error_msgs)
all_extended_rule_types[] = InteractiveUtils.subtypes(ExtendedRule)
return nothing
end

Expand Down Expand Up @@ -128,3 +132,10 @@ function check(::Lock_Extension, x::EXPR)
end

check(::Unlock_Extension, x::EXPR) = generic_check(x, "unlock(hole_variable)", "`unlock` should be used with extreme caution.")
check(::Yield_Extension, x::EXPR) = generic_check(x, "yield()", "`yield` should be used with extreme caution.")
check(::Sleep_Extension, x::EXPR) = generic_check(x, "sleep(hole_variable)", "`sleep` should be used with extreme caution.")
function check(::Mmap_Extension, x::EXPR)
generic_check(x, "mmap(hole_variable_star)", "`mmap` should be used with extreme caution.")
generic_check(x, "Mmap.mmap(hole_variable_star)", "`mmap` should be used with extreme caution.")
end

29 changes: 29 additions & 0 deletions test/rai_rules_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,35 @@ end
@test lint_test(source,
"Line 15, column 9: `unlock` should be used with extreme caution.")
end

@testset "yield, sleep, map" begin
source = """
function wait_for_cooldown(count::UInt64, counts::HistogramCounts)
while count != @atomic counts.total_observations
yield()
sleep(0.011)
end
end
function use_of_map()
s = open("/tmp/mmap.bin")
m = read(s, Int)
n = read(s, Int)
A2 = mmap(s, Matrix{Int}, (m,n))
A3 = Mmap.mmap(s, Matrix{Int}, (m,n))
end
"""
@test lint_has_error_test(source)
@test lint_test(source,
"Line 3, column 9: `yield` should be used with extreme caution.")
@test lint_test(source,
"Line 4, column 9: `sleep` should be used with extreme caution.")
@test lint_test(source,
"Line 12, column 10: `mmap` should be used with extreme caution.")
@test lint_test(source,
"Line 13, column 10: `mmap` should be used with extreme caution.")
end
end

@testset "Comparison" begin
Expand Down

0 comments on commit 58c3e03

Please sign in to comment.