package pfff

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file check_variables_cpp.ml

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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
(* Yoann Padioleau
 *
 * Copyright (C) 2010 Facebook
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation, with the
 * special exception on linking described in file license.txt.
 * 
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the file
 * license.txt for more details.
 *)

open Cst_cpp

module Ast = Cst_cpp
module V = Visitor_cpp
module E = Error_code
module S = Scope_code

(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(*
 * This file mostly deals with scoping issues. Scoping is different
 * from typing! Those are 2 orthogonal programming language notions.
 * 
 * TODO: could move generic code in scope_code.ml
 *)

(*****************************************************************************)
(* Types, constants *)
(*****************************************************************************)

type environment = 
  (Ast.name * (Scope_code.t * int ref)) list list 

(*****************************************************************************)
(* Environment *)
(*****************************************************************************)

(* Looking up a variable in the environment. 
*)

let rec lookup_env2 s env = 
  match env with 
  | [] -> raise Not_found
  | []::zs -> lookup_env2 s zs
  | ((a,b)::xs)::zs -> 
      if Ast.string_of_name_tmp a = s 
      then b 
      else lookup_env2 s (xs::zs)
let lookup_env a b = 
  Common.profile_code "CheckVar.lookup_env" (fun () -> lookup_env2  a b)

let lookup_env_opt a b = 
  Common2.optionise (fun () -> lookup_env a b)

let is_top_env env =
  List.length env = 1

(*****************************************************************************)
(* (Semi) Globals, Julia's style *)
(*****************************************************************************)

(* use a ref because we may want to modify it *)
let (initial_env: environment ref) = 
(* less:  Env_php.globals_builtins +> List.map (fun s ->
 * fake_dname s, (S.Global, ref 1)
 * )
 *)
  ref [[]]

(* opti: cache ? use hash ? *)
let _scoped_env = ref !initial_env

(* TODO use generic implem of Common ? *)
let new_scope() = _scoped_env := []::!_scoped_env 
let del_scope() = _scoped_env := List.tl !_scoped_env
let top_scope() = List.hd !_scoped_env

(*
let do_in_new_scope f = 
  begin
    new_scope();
    let res = f() in
    del_scope();
    res
  end
*)

let add_in_scope namedef =
  let (current, older) = Common2.uncons !_scoped_env in
  _scoped_env := (namedef::current)::older


let add_binding2 k v  = 
(*
  let info = Ast.info_of_name_tmp k in
  if !Flag.debug_checker 
  then pr2 (spf "adding binding %s" 
               (Ast.string_of_info info));
*)
  add_in_scope (k, v) 

let add_binding k v = 
  Common.profile_code "CV.add_binding" (fun () -> add_binding2 k v)

(*****************************************************************************)
(* checks *)
(*****************************************************************************)

let do_in_new_scope_and_check f = 
  new_scope();
  let res = f() in

  let top = top_scope () in
  del_scope();

  top |> List.rev |> List.iter (fun (name, (scope, aref)) ->
    if !aref = 0 
    then 
      let s = Ast.string_of_name_tmp name in
      let ii = List.hd (Ast.ii_of_id_name name) in
      E.error ii (E.UnusedVariable (s, scope))
  );
  res

(*****************************************************************************)
(* Scoped visitor *)
(*****************************************************************************)

(* For each introduced binding (param, exception, foreach, etc), 
 * we add the binding in the environment with a counter, a la checkModule.
 * todo: ?(find_entity = None) 
 *)
let visit_prog prog = 

  let hooks = { V.default_visitor with

    (* 1: scoping management *)
    V.kcompound =  (fun (k, _) x ->
      do_in_new_scope_and_check (fun () -> k x)
    );

    V.kcpp = (fun (k, _) x ->
      do_in_new_scope_and_check (fun () -> 
        (match x with
        | Define (_, _id, DefineFunc params, _body) ->
            params |> Ast.unparen |> Ast.uncomma |> List.iter (fun (name) ->
              (match name with
              | (s, [ii]) ->
                add_binding (None, noQscope, IdIdent (s,ii)) (S.Param, ref 0);
              | _ -> ()
              );
            );
        | _ -> ()
        );
        k x
      )
    );

    (* 2: adding defs of name in environment *)
    V.kparameter = (fun (k, _) param ->
      param.p_name  |> Common.do_option (fun ident ->
        add_binding (None, noQscope, IdIdent ident) (S.Param, ref 0);
      );
      k param
    );

    V.kblock_decl = (fun (k, _) x ->
      match x with
      | DeclList (xs_comma, _) ->
          xs_comma |> Ast.uncomma |> List.iter (fun onedecl ->
            onedecl.v_namei |> Common.do_option (fun (name, _ini_opt) ->
              let scope = 
                if is_top_env !_scoped_env || 
                   (match onedecl.v_storage with 
                   | Sto (Extern,_) -> true 
                   | _ -> false
                   )
                then S.Global
                else S.Local
              in
              add_binding name (scope, ref 0);
            );
          );
          k x
      | MacroDecl _ ->
          k x
      | (Asm (_, _, _, _)
        |NameSpaceAlias (_, _, _, _, _)|UsingDirective (_, _, _, _)
        | UsingDecl _) -> ()
    );


    (* 3: checking uses *)

    V.kexpr = (fun (k, _) x ->
      match (Ast.unwrap x) with
      | Id (name, idinfo) ->
          (* assert scope_ref = S.Unknown ? *)
          let s = Ast.string_of_name_tmp name in
          (match lookup_env_opt s !_scoped_env with
          | None -> 
              idinfo.i_scope <- S.Global;
          | Some (scope, _) ->
              idinfo.i_scope <- scope;
          );
          k x
      | _ -> k x
    );
  }
  in

  let visitor = V.mk_visitor hooks in
  visitor (Program prog)

(*****************************************************************************)
(* Main entry point *)
(*****************************************************************************)
let check_and_annotate_program2 prog =
  (* globals (re)initialialisation *) 
  _scoped_env := !initial_env;
  visit_prog prog;
  ()

let check_and_annotate_program a = 
  Common.profile_code "Checker.variables" (fun () -> 
    check_and_annotate_program2 a)
OCaml

Innovation. Community. Security.