218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
# File 'lib/adsl/extract/rails/action_instrumenter.rb', line 218
def initialize(ar_class_names, instrument_domain = Dir.pwd)
super instrument_domain
@branch_index = 0
render_stmts = [:respond_to, :render, :redirect_to, :respond_with]
replace :call do |sexp|
next sexp unless sexp.length >= 3 and sexp[1].nil? and render_stmts.include?(sexp[2])
s(:call, nil, :ins_mark_render_statement)
end
replace :iter do |sexp|
next sexp unless sexp[1].length >= 3 and sexp[1][0] == :call and sexp[1][1].nil? and render_stmts.include?(sexp[1][2])
s(:call, nil, :ins_mark_render_statement)
end
replace :defn, :defs do |sexp|
= sexp.sexp_type == :defn ? 3 : 4
stmts = sexp.pop(sexp.length - )
single_stmt = stmts.length > 1 ? s(:block, *stmts) : stmts.first
explore_all = s(:iter,
s(:call, nil, :ins_explore_all, s(:lit, sexp[ - 2])),
s(:args),
single_stmt)
sexp.push explore_all
sexp
end
replace :call do |sexp|
next sexp unless sexp[2] == :raise
s(:call, nil, :ins_do_raise)
end
replace :return do |sexp|
s(:call, nil, :ins_do_return, *sexp.sexp_body)
end
replace :lasgn, :iasgn, :cvasgn, :cvdecl, :gasgn, :masgn, :op_asgn_or, :unless_in => [:args, :op_asgn_or] do |sexp|
next sexp if sexp.length <= 2
variables_and_prefixes = if sexp.sexp_type == :masgn
sexp[1].sexp_body.map{ |asgn_type, var| [asgn_type.to_s[0..-5], var] }
elsif sexp.sexp_type == :op_asgn_or
[]
else
[[sexp[0].to_s[0..-5], sexp[1]]]
end
prepare_assignments = variables_and_prefixes.map{ |prefix, var|
s(:op_asgn_or, s("#{prefix}var".to_sym, var), s("#{prefix}asgn".to_sym, var, s(:nil)))
}
var_names = if sexp.sexp_type == :masgn
sexp[1].sexp_body.map{ |var| s(:str, var[1].to_s) }.to_a
elsif sexp.sexp_type == :op_asgn_or
[s(:str, sexp[1][1].to_s)]
else
[s(:str, sexp[1].to_s)]
end
values = if sexp.sexp_type == :masgn
sexp[2]
elsif sexp.sexp_type == :op_asgn_or
s(:array, sexp[2][2])
else
s(:array, sexp[2])
end
operator = sexp.sexp_type == :op_asgn_or ? s(:str, '||=') : s(:str, '=')
s(:block,
*prepare_assignments,
s(:call, nil, :ins_multi_assignment, s(:call, nil, :binding), s(:array, *var_names), values, operator)
)
end
replace :defn, :defs, :block, :iter do |sexp|
first_stmt_index = case sexp.sexp_type
when :defn; 3
when :defs; 4
when :iter; 3
when :block; 1
end
(first_stmt_index..sexp.length-1).each do |index|
unless [:if, :return].include?(sexp[index].sexp_type) ||
(sexp[index][1].nil? && [:ins_push_frame, :ins_pop_frame].include?(sexp[index][2]))
sexp[index] = s(:call, nil, :ins_stmt, sexp[index])
end
end
sexp
end
replace :if do |sexp|
if sexp.may_return_or_raise?
sexp[1] = s(:call, nil, :ins_branch_choice, sexp[1], s(:lit, @branch_index += 1))
sexp[2] = s(:block, sexp[2]) unless sexp[2].nil? or sexp[2].sexp_type == :block
sexp[3] = s(:block, sexp[3]) unless sexp[3].nil? or sexp[3].sexp_type == :block
sexp
else
block1_sexp = sexp[2] || s(:nil)
block1 = block1_sexp.sexp_type == :block ? block1_sexp : s(:block, block1_sexp)
block2_sexp = sexp[3] || s(:nil)
block2 = block2_sexp.sexp_type == :block ? block2_sexp : s(:block, block2_sexp)
s(:call, nil, :ins_if,
sexp[1],
s(:array,
s(:call, nil, :ins_push_frame),
s(:splat, s(:rescue,
s(:array, block1, s(:call, nil, :ins_pop_frame)),
s(:resbody,
s(:array, s(:const, :Exception)),
s(:array, s(:nil), s(:call, nil, :ins_pop_frame))
)
))
),
s(:array,
s(:call, nil, :ins_push_frame),
s(:splat, s(:rescue,
s(:array, block2, s(:call, nil, :ins_pop_frame)),
s(:resbody,
s(:array, s(:const, :Exception)),
s(:array, s(:nil), s(:call, nil, :ins_pop_frame))
)
))
)
)
end
end
replace :rescue do |sexp|
resbody = sexp.sexp_body.select{ |a| a.sexp_type == :resbody }.first
exception_type_array = s(:array, *resbody[1].sexp_body)
res_block = resbody[2..-1]
res_block = res_block.length > 1 ? s(:block, *res_block) : res_block.first
s(:if, s(:nil), res_block, sexp[1])
end
replace :attrasgn do |sexp|
s(:call, *sexp.sexp_body)
end
replace :defn, :defs do |sexp|
make_returns_explicit sexp
sexp
end
replace :iter do |sexp|
next sexp unless (
sexp[1].sexp_type == :call &&
sexp[1][1] == nil &&
[:collection_action, :member_action, :page_action].include?(sexp[1][2])
)
s(:defn, sexp[1][3][1], sexp[2], sexp[3])
end
end
|