Skip to content
This repository has been archived by the owner on May 21, 2021. It is now read-only.

Commit

Permalink
[clang_ast_proj] generate {get,update}_var_decl_tuple
Browse files Browse the repository at this point in the history
Summary:
* I'm adding a new Infer predicate that needs to extract var_decl_info from all the VarDecl-based nodes
* This would seem to be the cleanest most general method
* Also added unit test coverage

Test Plan:
* `make clang_plugin_test` (includes new unit tests)
* Testing of dependent diff

Reviewers: jrm, ddino, jul

Reviewed By: jul

Subscribers: mcl

Differential Revision: https://phabricator.intern.facebook.com/D14367310

Signature: 14367310:1552053475:3fd33913e449ba6a95cc025bad2d39712de5101f
  • Loading branch information
David Lively committed Mar 8, 2019
1 parent 1f77ff5 commit ee66e72
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 19 deletions.
16 changes: 16 additions & 0 deletions clang-ocaml/clang_ast_proj.ml.p
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ let update_tag_decl_tuple __f = function
| x -> x


let get_var_decl_tuple = function
#define DECL(DERIVED, BASE)
#define ABSTRACT_DECL(DECL)
#define VAR(DERIVED, BASE) | DERIVED@@Decl (@DERIVED@_decl_tuple) -> Some (var_decl_tuple)
#include <clang/AST/DeclNodes.inc>
| _ -> None

let update_var_decl_tuple __f = function
#define DECL(DERIVED, BASE)
#define ABSTRACT_DECL(DECL)
#define VAR(DERIVED, BASE) | DERIVED@@Decl (@DERIVED@_decl_tuple) -> \
let (var_decl_tuple) = __f (var_decl_tuple) in DERIVED@@Decl (@DERIVED@_decl_tuple)
#include <clang/AST/DeclNodes.inc>
| x -> x


let get_stmt_kind_string = function
#define STMT(CLASS, PARENT) | CLASS (@CLASS@_tuple) -> s(CLASS)
#define ABSTRACT_STMT(STMT)
Expand Down
35 changes: 17 additions & 18 deletions clang-ocaml/clang_ast_proj.mli.p
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,32 @@

open Clang_ast_t

val get_cast_kind : stmt -> cast_kind option
val get_decl_context_tuple : decl -> (decl_context_tuple) option
val get_decl_kind_string : decl -> string
val get_decl_tuple : decl -> (decl_tuple)
val get_decl_context_tuple : decl -> (decl_context_tuple) option
val get_expr_tuple : stmt -> (expr_tuple) option
val get_named_decl_tuple : decl -> (named_decl_tuple) option
val get_type_decl_tuple : decl -> (type_decl_tuple) option
val get_tag_decl_tuple : decl -> (tag_decl_tuple) option

val get_stmt_kind_string : stmt -> string
val get_stmt_tuple : stmt -> (stmt_tuple)
val get_expr_tuple : stmt -> (expr_tuple) option

val get_tag_decl_tuple : decl -> (tag_decl_tuple) option
val get_type_decl_tuple : decl -> (type_decl_tuple) option
val get_type_tuple : c_type -> (type_tuple)
val get_var_decl_tuple : decl -> (var_decl_tuple) option

val update_decl_tuple : ((decl_tuple) -> (decl_tuple)) -> decl -> decl
val update_decl_context_tuple : ((decl_context_tuple) -> (decl_context_tuple)) -> decl -> decl
val update_named_decl_tuple : ((named_decl_tuple) -> (named_decl_tuple)) -> decl -> decl
val update_type_decl_tuple : ((type_decl_tuple) -> (type_decl_tuple)) -> decl -> decl
val update_tag_decl_tuple : ((tag_decl_tuple) -> (tag_decl_tuple)) -> decl -> decl

val update_stmt_tuple : ((stmt_tuple) -> (stmt_tuple)) -> stmt -> stmt
val update_expr_tuple : ((expr_tuple) -> (expr_tuple)) -> stmt -> stmt

val is_valid_astnode_kind : string -> bool
val is_valid_binop_kind_name : string -> bool
val is_valid_unop_kind_name : string -> bool

val string_of_binop_kind : binary_operator_kind -> string
val string_of_unop_kind : unary_operator_kind -> string
val is_valid_astnode_kind : string -> bool
val string_of_cast_kind : cast_kind -> string
val get_cast_kind : stmt -> cast_kind option
val string_of_unop_kind : unary_operator_kind -> string

val update_decl_context_tuple : ((decl_context_tuple) -> (decl_context_tuple)) -> decl -> decl
val update_decl_tuple : ((decl_tuple) -> (decl_tuple)) -> decl -> decl
val update_expr_tuple : ((expr_tuple) -> (expr_tuple)) -> stmt -> stmt
val update_named_decl_tuple : ((named_decl_tuple) -> (named_decl_tuple)) -> decl -> decl
val update_stmt_tuple : ((stmt_tuple) -> (stmt_tuple)) -> stmt -> stmt
val update_tag_decl_tuple : ((tag_decl_tuple) -> (tag_decl_tuple)) -> decl -> decl
val update_type_decl_tuple : ((type_decl_tuple) -> (type_decl_tuple)) -> decl -> decl
val update_var_decl_tuple : ((var_decl_tuple) -> (var_decl_tuple)) -> decl -> decl
33 changes: 32 additions & 1 deletion clang-ocaml/clang_ast_proj_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ let name_info name = {ni_name= name; ni_qual_name= [name]}
let append_name_info info suffix =
{ni_name= info.ni_name ^ suffix; ni_qual_name= List.map (fun x -> x ^ suffix) info.ni_qual_name}

let qual_type ptr =
{ qt_type_ptr= ptr
; qt_is_const= false
; qt_is_restrict= false
; qt_is_volatile= false }

let var_decl_info ~is_global =
{ vdi_is_global= is_global
; vdi_is_extern= false
; vdi_is_static_local= false
; vdi_is_static_data_member= false
; vdi_is_const_expr= false
; vdi_is_init_ice= false
; vdi_is_init_expr_cxx11_constant= false
; vdi_init_expr= None
; vdi_parm_index_in_function= None
; vdi_storage_class= None }


let () =
let di = decl_info empty_source_location empty_source_location in
Expand All @@ -50,4 +68,17 @@ let () =
(Some (di, name_info "foobar")) ;
let di2 = decl_info (source_location ~file:"bla" ()) (source_location ~file:"bleh" ()) in
let decl3 = update_decl_tuple (fun _ -> di2) decl in
assert_equal "update_decl_tuple" (get_decl_tuple decl3) di2
assert_equal "update_decl_tuple" (get_decl_tuple decl3) di2 ;

assert_equal "get_var_decl_tuple_none" (get_var_decl_tuple decl) None ;
let vdi = var_decl_info ~is_global:true in
let qt = qual_type (Clang_ast_types.TypePtr.wrap 0) in
let var_decl = ParmVarDecl(di, name_info "fooey", qt, vdi) in
assert_equal "get_var_decl_tuple" (get_var_decl_tuple var_decl)
(Some (di, name_info "fooey", qt, vdi)) ;
let updated_var_decl = update_var_decl_tuple
(fun (di, ni, qt, vdi) ->
(di, append_name_info ni "-mod", qt, var_decl_info ~is_global:false))
var_decl in
assert_equal "update_var_decl_tuple" (get_var_decl_tuple updated_var_decl)
(Some (di, name_info "fooey-mod", qt, var_decl_info ~is_global:false)) ;

0 comments on commit ee66e72

Please sign in to comment.