Skip to content

Commit 1b1cc5a

Browse files
authored
Merge pull request #58 from julia-vscode/inclloop
safety check for include loops
2 parents dec3666 + 7db0983 commit 1b1cc5a

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

src/StaticLint.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ bindingof(m::Meta) = m.binding
4545
mutable struct State{T}
4646
file::T
4747
targetfile::Union{Nothing,T}
48+
included_files::Vector{String}
4849
scope::Scope
4950
delayed::Bool
5051
urefs::Vector{EXPR}
@@ -140,12 +141,18 @@ function followinclude(x, state::State)
140141
path = ""
141142
end
142143
if !isempty(path)
144+
if path in state.included_files
145+
seterror!(x, IncludeLoop)
146+
return
147+
end
143148
oldfile = state.file
144149
state.file = getfile(state.server, path)
150+
push!(state.included_files, getpath(state.file))
145151
setroot(state.file, getroot(oldfile))
146152
setscope!(getcst(state.file), nothing)
147153
state(getcst(state.file))
148154
state.file = oldfile
155+
pop!(state.included_files)
149156
else
150157
seterror!(x, MissingFile)
151158
end

src/linting/checks.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ PointlessOR,
99
PointlessAND,
1010
UnusedBinding,
1111
InvalidTypeDeclaration,
12+
IncludeLoop,
1213
MissingFile)
1314

15+
1416
const LintCodeDescriptions = Dict{LintCodes,String}(
1517
IncorrectCallNargs => "An incorrect number of function arguments has been passed.",
1618
IncorrectIterSpec => "A loop iterator has been used that will likely error.",
@@ -21,6 +23,7 @@ const LintCodeDescriptions = Dict{LintCodes,String}(
2123
PointlessAND => "The first argument of a `&&` call is `false`.",
2224
UnusedBinding => "The variable name has been bound but not used.",
2325
InvalidTypeDeclaration => "A non-DataType has been used in a type declaration statement.",
26+
IncludeLoop => "Loop detected, this file has already been included.",
2427
MissingFile => "The included file can not be found."
2528
)
2629

src/server.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ getsymbolserver(server::FileServer) = server.symbolserver
3939
function scopepass(file, target = nothing)
4040
server = file.server
4141
setscope!(getcst(file), Scope(nothing, getcst(file), Dict(), Dict{String,Any}("Base" => getsymbolserver(server)["Base"], "Core" => getsymbolserver(server)["Core"]), false))
42-
state = State(file, target, scopeof(getcst(file)), false, EXPR[], server)
42+
state = State(file, target, [getpath(file)], scopeof(getcst(file)), false, EXPR[], server)
4343
state(getcst(file))
4444
for uref in state.urefs
4545
s = retrieve_delayed_scope(uref)

0 commit comments

Comments
 (0)