Static Code Analysis for Julia
using Pkg
Pkg.add("StaticLint")
using StaticLint
This package supports LanguageServer.jl functionality broadly by:
Identifying and marking errors (5.) is, in general, dependent on steps 1-4. These are achieved through a single pass over the ST of a project. A pass over a single EXPR
is achieved through calling a State
object on the ST. This State
requires an AbstractServer
that determines how files within a project are loaded and makes packages available for loading.
For a given expression x
this pass will:
resolve_import
). This either explicitly imports variables into the current state (for statements such as import/using SomeModule: binding1, binding2
) or makes the exported bindings of a modules available more generally (e.g. using SomeOtherModule
). The availability of includable packages is handled by the getsymbolserver
function called on the state.server
.x
introduces a new variable. mark_bindings!
performs this and may mark bindings for child nodes of x
(e.g. when called on an expression that defines a Function
this will mark the arguments of the signature as introducing bindings.)x
to the variable list of the current scope (add_binding
).mark_globals
).deprecate
, enum
, goto
, label
, and nospecialize
.x
as needed (scopes
).A.B.c
). A name is first checked against bindings introduced within a scope then against exported variables of modules loaded into the scope. If this fails to resolve the name this is repeated for the parent scope. References that fail to resolve at this point, and are within a delayed scope (i.e. within a function) are added to a list to be resolved later.x
is a call to include(path_expr)
attempt to resolve path_expr
to a loadable file from state.server
and pass across the files ST (followinclude
). x
(traverse
) in execution order. This means, for example, that in the expression a = b
we traverse b
then a
(ignoring the operator).
As mentioned, an AbstractServer
is required to hold files within a project and provide access to user installed packages. An implementation must support the following functions:
StaticLint.hasfile(server, path)::Bool
: Does the server have a file matching the name path
.
StaticLint.getfile(server, path)::AbstractFile
: Retrieves the file path
- assumes the server has the file.
StaticLint.setfile(server, path, file)::AbstractFile
: Stores file
in the server under the name path
, returning the file.
StaticLint.canloadfile(server, path)::Bool
: Can the server load the file denoted by path
, likely from an external source.
StaticLint.loadfile(server, path)::AbstractFile
: Load the file at path
from an external source (i.e. the hard drive).
StaticLint.getsymbolserver(server)::Dict{String,SymbolServer.ModuleStore}
: Retrieve the server's depot of loadable packages.
An AbstractFile
must support the following:
StaticLint.getpath(file)
: Retrieve the path of a file.
StaticLint.getroot(file)
: Retrieve the root of a file. The root is the main/first file in a file structure. For example the StaticLint.jl
file is the root of all files (including itself) in src/
.
StaticLint.setroot(file, root)
: Set the root of a file.
StaticLint.getcst(file)
: Retrieve the cst of a file.
StaticLint.setcst(file, cst::CSTParser.EXPR)
: Set the cst of a file.
StaticLint.getserver(file)
: Retrieve the server holding of a file.
StaticLint.setserver(file, server::AbstractServer)
: Set the server of a file.
StaticLint.semantic_pass(file, target = nothing(optional))
: Run a full pass on the ST of a project (i.e. ST of all linked files). It is expected that file
is the root of the project.