From 57cb40611d2b494d6edf881c468efe32b0edafb9 Mon Sep 17 00:00:00 2001 From: Alexandre Bergel Date: Tue, 27 Feb 2024 16:14:32 +0100 Subject: [PATCH 1/3] Adding yield --- src/linting/extended_checks.jl | 2 ++ test/rai_rules_tests.jl | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/linting/extended_checks.jl b/src/linting/extended_checks.jl index aa7fe7a..c420390 100644 --- a/src/linting/extended_checks.jl +++ b/src/linting/extended_checks.jl @@ -53,6 +53,7 @@ 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 const all_extended_rule_types = InteractiveUtils.subtypes(ExtendedRule) @@ -128,3 +129,4 @@ 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.") diff --git a/test/rai_rules_tests.jl b/test/rai_rules_tests.jl index ef8292c..cc9d40d 100644 --- a/test/rai_rules_tests.jl +++ b/test/rai_rules_tests.jl @@ -372,6 +372,20 @@ end @test lint_test(source, "Line 15, column 9: `unlock` should be used with extreme caution.") end + + @testset "yield" begin + source = """ + function wait_for_cooldown(count::UInt64, counts::HistogramCounts) + while count != @atomic counts.total_observations + yield() + end + end + """ + @test lint_has_error_test(source) + @test lint_test(source, + "Line 3, column 9: `yield` should be used with extreme caution.") + end + end @testset "Comparison" begin From 3cdf2eb1dfe1fed842bc93033ef1fe805f5b94e9 Mon Sep 17 00:00:00 2001 From: Alexandre Bergel Date: Wed, 28 Feb 2024 14:50:23 +0100 Subject: [PATCH 2/3] Support yield, sleep, map --- src/linting/checks.jl | 2 +- src/linting/extended_checks.jl | 11 ++++++++++- test/rai_rules_tests.jl | 19 +++++++++++++++++-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/linting/checks.jl b/src/linting/checks.jl index 7654deb..1713140 100644 --- a/src/linting/checks.jl +++ b/src/linting/checks.jl @@ -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 diff --git a/src/linting/extended_checks.jl b/src/linting/extended_checks.jl index c420390..5b34e1e 100644 --- a/src/linting/extended_checks.jl +++ b/src/linting/extended_checks.jl @@ -54,8 +54,10 @@ 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}() @@ -66,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 @@ -130,3 +133,9 @@ 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 + diff --git a/test/rai_rules_tests.jl b/test/rai_rules_tests.jl index cc9d40d..b452220 100644 --- a/test/rai_rules_tests.jl +++ b/test/rai_rules_tests.jl @@ -373,19 +373,34 @@ end "Line 15, column 9: `unlock` should be used with extreme caution.") end - @testset "yield" begin + @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 From 69b852723249f2d418d304e0af566286a01a75fb Mon Sep 17 00:00:00 2001 From: Alexandre Bergel Date: Wed, 28 Feb 2024 14:50:54 +0100 Subject: [PATCH 3/3] Fixing platform issue --- src/interface.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index b99e7e6..55cf8e0 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -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