-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathanalyzer.clj
204 lines (179 loc) · 7.15 KB
/
analyzer.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
(ns refactor-nrepl.analyzer
(:refer-clojure :exclude [macroexpand-1])
(:require
[clojure.java.io :as io]
[clojure.string :as string]
[clojure.tools.analyzer :as ana]
[clojure.tools.analyzer.ast :refer [nodes]]
[clojure.tools.analyzer.jvm :as aj]
[clojure.tools.namespace.parse :refer [read-ns-decl]]
[clojure.tools.reader :as reader]
[clojure.walk :as walk]
[refactor-nrepl.config :as config]
[refactor-nrepl.core :as core]
[refactor-nrepl.ns.tracker :as tracker]
[refactor-nrepl.util :as util])
(:import
(java.io PushbackReader)
(java.util.regex Pattern)))
;;; The structure here is {ns {content-hash ast}}
(def ^:private ast-cache (atom {}))
(defn get-alias [as v]
(cond as (first v)
(= (first v) :as) (get-alias true (rest v))
:else (get-alias nil (rest v))))
(defn parse-ns
"Returns tuples with the ns as the first element and
a map of the aliases for the namespace as the second element
in the same format as ns-aliases"
[body]
(with-open [string-reader (java.io.StringReader. body)]
(let [ns-decl (read-ns-decl (PushbackReader. string-reader))
aliases (->> ns-decl
(filter list?)
(some #(when (#{:require} (first %)) %))
rest
(remove symbol?)
(filter #(contains? (set %) :as))
(#(zipmap (map (partial get-alias nil) %)
(map first %))))]
[(second ns-decl) aliases])))
(defn- noop-macroexpand-1 [form]
form)
(defn- get-ast-from-cache
[ns file-content]
(-> @ast-cache
(get ns)
(get (hash file-content))))
(defn- update-ast-cache
[file-content ns ast]
(swap! ast-cache assoc ns {(hash file-content) ast})
ast)
(defn- ns-on-cp? [ns]
(boolean (or (io/resource (-> ns str (string/replace \. \/) (string/replace \- \_) (str ".clj")))
(io/resource (-> ns str (string/replace \. \/) (string/replace \- \_) (str ".cljc"))))))
(defn- shadow-unresolvable-symbol-handler [symbol-ns symbol-name symbol-ast]
{:op :const
:form (:form symbol-ast)
:literal? true
:type :string
:val (if symbol-ns
(str symbol-ns "/" symbol-name)
symbol-name)
:children []})
(defn- shadow-wrong-tag-handler [_tag-key _origination-ast]
nil)
(defn- build-ast
[ns aliases]
(when (and (ns-on-cp? ns)
(not (util/self-referential? ns)))
;; Use `locking`, because AST analysis can perform arbitrary evaluation.
;; Parallel analysis is not safe, especially as it can perform `require` calls.
(locking core/require-lock ;; for both `require` and `aj/analyze-ns`
;; Performing this `require` makes it more likely that t.ana will succeed.
;; I believe it's because `require` will also require other namespaces recursively.
;; t.ana does so in theory as well, but it's slightly more rigid,
;; and/or does not always do the same exact thing the Clojure compiler would.
(require ns)
(let [opts {:passes-opts
{:validate/unresolvable-symbol-handler shadow-unresolvable-symbol-handler
:validate/throw-on-arity-mismatch false
:validate/wrong-tag-handler shadow-wrong-tag-handler}}]
(binding [ana/macroexpand-1 noop-macroexpand-1
reader/*data-readers* *data-readers*]
(assoc-in (aj/analyze-ns ns (aj/empty-env) opts) [0 :alias-info] aliases))))))
(defn- cachable-ast [file-content]
(let [[ns aliases] (parse-ns file-content)]
(when ns
(if-let [cached-ast-or-err (get-ast-from-cache ns file-content)]
cached-ast-or-err
(when-let [new-ast-or-err (try
(build-ast ns aliases)
(catch Throwable th
(util/maybe-log-exception th)
th))]
(update-ast-cache file-content ns new-ast-or-err))))))
(defn- throw-ast-in-bad-state
[file-content msg]
(throw (IllegalStateException.
(str "refactor-nrepl is unable to build an AST for "
(first (parse-ns file-content))
". tools.analyzer encountered the following problem: " msg))))
(defn ns-ast
"Build AST for a namespace"
[file-content]
(let [ast-or-err (cachable-ast file-content)
error? (instance? Throwable ast-or-err)
debug (:debug config/*config*)]
(when (and error? (System/getenv "CI"))
(.printStackTrace ^Throwable ast-or-err))
(cond
(and error? debug)
(throw ast-or-err)
error?
(throw-ast-in-bad-state file-content (.getMessage ^Throwable ast-or-err))
:else
ast-or-err)))
(defn- ast-stats []
(let [asts @ast-cache
map-entries (seq asts)]
(reduce (fn [acc [k v]]
(conj acc
(->> v
vals
(reduce (fn [init x]
(if (instance? Throwable x)
(reduced (list "error" (.getMessage ^Throwable x)))
init))
"OK"))
k))
()
map-entries)))
(defn warm-ast-cache []
(doseq [f (tracker/project-files-in-topo-order true)]
(try
(ns-ast (core/file-forms f #{:clj}))
(catch Throwable th
(when (System/getProperty "refactor-nrepl.internal.log-exceptions")
(-> th .printStackTrace))
nil ; noop, ast-status will be reported separately
)))
(ast-stats))
(defn node-at-loc? [^long loc-line ^long loc-column node]
(let [{:keys [^long line ^long end-line ^long column ^long end-column]} (:env node)]
;; The node for ::an-ns-alias/foo, when it appeared as a toplevel form,
;; had nil as position info
(and line end-line column end-column
(and (>= loc-line line)
(<= loc-line end-line)
(>= loc-column column)
(<= loc-column end-column)))))
(defn- normalize-anon-fn-params
"replaces anon fn params in a read form"
[form]
(walk/postwalk
(fn [token] (if (re-matches #"p\d+__\d+#" (str token)) 'p token)) form))
(defn- read-when-sexp [form]
(let [f-string (str form)]
(when (some #{\) \} \]} f-string)
(read-string f-string))))
(defn node-for-sexp?
"Is NODE the ast node for SEXP?"
[sexp node]
(binding [*read-eval* false]
(let [sexp-sans-comments-and-meta (normalize-anon-fn-params (read-string sexp))
pattern (re-pattern (Pattern/quote (str sexp-sans-comments-and-meta)))]
(if-let [forms (:raw-forms node)]
(some #(re-find pattern %)
(map (comp str normalize-anon-fn-params read-when-sexp) forms))
(= sexp-sans-comments-and-meta (-> (:form node)
read-when-sexp
normalize-anon-fn-params))))))
(defn top-level-form-index
[line column ns-ast]
(->> ns-ast
(map-indexed #(vector %1 (->> %2
nodes
(some (partial node-at-loc? line column)))))
(filter #(second %))
ffirst))