From e8548453d7eb1469b0049652b9bf5c1a61fed68a Mon Sep 17 00:00:00 2001 From: cedretaber Date: Tue, 8 Mar 2022 01:44:52 +0900 Subject: [PATCH 1/6] Make header files acceptable --- sample/import1.c | 9 + sample/import1.h | 3 + sample/import2.c | 11 + sample/import2.c.yojson | 1858 +++++++++++++++++++++++++++++++++++++++ src/lib/ast.ml | 13 +- src/lib/util.ml | 8 + src/lib/util.mli | 6 + src/lib/yojson2ast.ml | 14 +- 8 files changed, 1912 insertions(+), 10 deletions(-) create mode 100644 sample/import1.c create mode 100644 sample/import1.h create mode 100644 sample/import2.c create mode 100644 sample/import2.c.yojson diff --git a/sample/import1.c b/sample/import1.c new file mode 100644 index 0000000..1ceed4c --- /dev/null +++ b/sample/import1.c @@ -0,0 +1,9 @@ +int sum(int a, int b) +{ + return a + b; +} + +int product(int a, int b) +{ + return a * b; +} diff --git a/sample/import1.h b/sample/import1.h new file mode 100644 index 0000000..dcbb1b7 --- /dev/null +++ b/sample/import1.h @@ -0,0 +1,3 @@ +int sum(int a, int b); + +int product(int a, int b); diff --git a/sample/import2.c b/sample/import2.c new file mode 100644 index 0000000..2f13a17 --- /dev/null +++ b/sample/import2.c @@ -0,0 +1,11 @@ +#include "import1.h" + +int test() +{ + int a, b, c, d; + a = 42; + b = 99; + c = sum(a, b); + d = product(c, d); + return c + d; +} diff --git a/sample/import2.c.yojson b/sample/import2.c.yojson new file mode 100644 index 0000000..c8c1a40 --- /dev/null +++ b/sample/import2.c.yojson @@ -0,0 +1,1858 @@ +<"TranslationUnitDecl" : ( + { + "pointer" : 1, + "source_range" : ( + { + }, + { + } + ) + }, + [ + <"TypedefDecl" : ( + { + "pointer" : 2, + "source_range" : ( + { + }, + { + } + ), + "is_implicit" : true + }, + { + "name" : "__int128_t", + "qual_name" : [ + "__int128_t" + ] + }, + 0, + { + } + )>, + <"TypedefDecl" : ( + { + "pointer" : 3, + "source_range" : ( + { + }, + { + } + ), + "is_implicit" : true + }, + { + "name" : "__uint128_t", + "qual_name" : [ + "__uint128_t" + ] + }, + 0, + { + } + )>, + <"TypedefDecl" : ( + { + "pointer" : 4, + "source_range" : ( + { + }, + { + } + ), + "is_implicit" : true + }, + { + "name" : "__NSConstantString", + "qual_name" : [ + "__NSConstantString" + ] + }, + 0, + { + } + )>, + <"TypedefDecl" : ( + { + "pointer" : 5, + "source_range" : ( + { + }, + { + } + ), + "is_implicit" : true + }, + { + "name" : "__builtin_ms_va_list", + "qual_name" : [ + "__builtin_ms_va_list" + ] + }, + 0, + { + } + )>, + <"TypedefDecl" : ( + { + "pointer" : 6, + "source_range" : ( + { + }, + { + } + ), + "is_implicit" : true + }, + { + "name" : "__builtin_va_list", + "qual_name" : [ + "__builtin_va_list" + ] + }, + 0, + { + } + )>, + <"FunctionDecl" : ( + { + "pointer" : 7, + "source_range" : ( + { + "file" : "clang2cabs/sample/import1.h", + "line" : 1, + "column" : 1 + }, + { + "column" : 21 + } + ), + "is_used" : true, + "is_this_declaration_referenced" : true + }, + { + "name" : "sum", + "qual_name" : [ + "sum" + ] + }, + { + "type_ptr" : 8 + }, + { + "parameters" : [ + <"ParmVarDecl" : ( + { + "pointer" : 9, + "source_range" : ( + { + "column" : 9 + }, + { + "column" : 13 + } + ) + }, + { + "name" : "a", + "qual_name" : [ + "a" + ] + }, + { + "type_ptr" : 10 + }, + { + "parm_index_in_function" : 0 + } + )>, + <"ParmVarDecl" : ( + { + "pointer" : 11, + "source_range" : ( + { + "column" : 16 + }, + { + "column" : 20 + } + ) + }, + { + "name" : "b", + "qual_name" : [ + "b" + ] + }, + { + "type_ptr" : 10 + }, + { + "parm_index_in_function" : 1 + } + )> + ] + } + )>, + <"FunctionDecl" : ( + { + "pointer" : 12, + "source_range" : ( + { + "line" : 3, + "column" : 1 + }, + { + "column" : 25 + } + ), + "is_used" : true, + "is_this_declaration_referenced" : true + }, + { + "name" : "product", + "qual_name" : [ + "product" + ] + }, + { + "type_ptr" : 8 + }, + { + "parameters" : [ + <"ParmVarDecl" : ( + { + "pointer" : 13, + "source_range" : ( + { + "column" : 13 + }, + { + "column" : 17 + } + ) + }, + { + "name" : "a", + "qual_name" : [ + "a" + ] + }, + { + "type_ptr" : 10 + }, + { + "parm_index_in_function" : 0 + } + )>, + <"ParmVarDecl" : ( + { + "pointer" : 14, + "source_range" : ( + { + "column" : 20 + }, + { + "column" : 24 + } + ) + }, + { + "name" : "b", + "qual_name" : [ + "b" + ] + }, + { + "type_ptr" : 10 + }, + { + "parm_index_in_function" : 1 + } + )> + ] + } + )>, + <"FunctionDecl" : ( + { + "pointer" : 15, + "source_range" : ( + { + "file" : "clang2cabs/sample/import2.c", + "line" : 3, + "column" : 1 + }, + { + "line" : 11, + "column" : 1 + } + ) + }, + { + "name" : "test", + "qual_name" : [ + "test" + ] + }, + { + "type_ptr" : 16 + }, + { + "decl_ptr_with_body" : 15, + "body" : <"CompoundStmt" : ( + { + "pointer" : 17, + "source_range" : ( + { + "line" : 4, + "column" : 1 + }, + { + "line" : 11, + "column" : 1 + } + ) + }, + [ + <"DeclStmt" : ( + { + "pointer" : 18, + "source_range" : ( + { + "line" : 5, + "column" : 5 + }, + { + "column" : 19 + } + ) + }, + [ + ], + [ + <"VarDecl" : ( + { + "pointer" : 19, + "source_range" : ( + { + "column" : 5 + }, + { + "column" : 9 + } + ), + "is_used" : true, + "is_this_declaration_referenced" : true + }, + { + "name" : "a", + "qual_name" : [ + "a" + ] + }, + { + "type_ptr" : 10 + }, + { + } + )>, + <"VarDecl" : ( + { + "pointer" : 20, + "source_range" : ( + { + "column" : 5 + }, + { + "column" : 12 + } + ), + "is_used" : true, + "is_this_declaration_referenced" : true + }, + { + "name" : "b", + "qual_name" : [ + "b" + ] + }, + { + "type_ptr" : 10 + }, + { + } + )>, + <"VarDecl" : ( + { + "pointer" : 21, + "source_range" : ( + { + "column" : 5 + }, + { + "column" : 15 + } + ), + "is_used" : true, + "is_this_declaration_referenced" : true + }, + { + "name" : "c", + "qual_name" : [ + "c" + ] + }, + { + "type_ptr" : 10 + }, + { + } + )>, + <"VarDecl" : ( + { + "pointer" : 22, + "source_range" : ( + { + "column" : 5 + }, + { + "column" : 18 + } + ), + "is_used" : true, + "is_this_declaration_referenced" : true + }, + { + "name" : "d", + "qual_name" : [ + "d" + ] + }, + { + "type_ptr" : 10 + }, + { + } + )> + ] + )>, + <"BinaryOperator" : ( + { + "pointer" : 23, + "source_range" : ( + { + "line" : 6, + "column" : 5 + }, + { + "column" : 9 + } + ) + }, + [ + <"DeclRefExpr" : ( + { + "pointer" : 24, + "source_range" : ( + { + "column" : 5 + }, + { + "column" : 5 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 10 + }, + "value_kind" : <"LValue"> + }, + { + "decl_ref" : { + "kind" : <"Var">, + "decl_pointer" : 19, + "name" : { + "name" : "a", + "qual_name" : [ + "a" + ] + }, + "qual_type" : { + "type_ptr" : 10 + } + } + } + )>, + <"IntegerLiteral" : ( + { + "pointer" : 25, + "source_range" : ( + { + "column" : 9 + }, + { + "column" : 9 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "is_signed" : true, + "bitwidth" : 32, + "value" : "42" + } + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "kind" : <"Assign"> + } + )>, + <"BinaryOperator" : ( + { + "pointer" : 26, + "source_range" : ( + { + "line" : 7, + "column" : 5 + }, + { + "column" : 9 + } + ) + }, + [ + <"DeclRefExpr" : ( + { + "pointer" : 27, + "source_range" : ( + { + "column" : 5 + }, + { + "column" : 5 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 10 + }, + "value_kind" : <"LValue"> + }, + { + "decl_ref" : { + "kind" : <"Var">, + "decl_pointer" : 20, + "name" : { + "name" : "b", + "qual_name" : [ + "b" + ] + }, + "qual_type" : { + "type_ptr" : 10 + } + } + } + )>, + <"IntegerLiteral" : ( + { + "pointer" : 28, + "source_range" : ( + { + "column" : 9 + }, + { + "column" : 9 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "is_signed" : true, + "bitwidth" : 32, + "value" : "99" + } + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "kind" : <"Assign"> + } + )>, + <"BinaryOperator" : ( + { + "pointer" : 29, + "source_range" : ( + { + "line" : 8, + "column" : 5 + }, + { + "column" : 17 + } + ) + }, + [ + <"DeclRefExpr" : ( + { + "pointer" : 30, + "source_range" : ( + { + "column" : 5 + }, + { + "column" : 5 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 10 + }, + "value_kind" : <"LValue"> + }, + { + "decl_ref" : { + "kind" : <"Var">, + "decl_pointer" : 21, + "name" : { + "name" : "c", + "qual_name" : [ + "c" + ] + }, + "qual_type" : { + "type_ptr" : 10 + } + } + } + )>, + <"CallExpr" : ( + { + "pointer" : 31, + "source_range" : ( + { + "column" : 9 + }, + { + "column" : 17 + } + ) + }, + [ + <"ImplicitCastExpr" : ( + { + "pointer" : 32, + "source_range" : ( + { + "column" : 9 + }, + { + "column" : 9 + } + ) + }, + [ + <"DeclRefExpr" : ( + { + "pointer" : 33, + "source_range" : ( + { + "column" : 9 + }, + { + "column" : 9 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 8 + } + }, + { + "decl_ref" : { + "kind" : <"Function">, + "decl_pointer" : 7, + "name" : { + "name" : "sum", + "qual_name" : [ + "sum" + ] + }, + "qual_type" : { + "type_ptr" : 8 + } + } + } + )> + ], + { + "qual_type" : { + "type_ptr" : 34 + } + }, + { + "cast_kind" : <"FunctionToPointerDecay">, + "base_path" : [ + ] + }, + false + )>, + <"ImplicitCastExpr" : ( + { + "pointer" : 35, + "source_range" : ( + { + "column" : 13 + }, + { + "column" : 13 + } + ) + }, + [ + <"DeclRefExpr" : ( + { + "pointer" : 36, + "source_range" : ( + { + "column" : 13 + }, + { + "column" : 13 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 10 + }, + "value_kind" : <"LValue"> + }, + { + "decl_ref" : { + "kind" : <"Var">, + "decl_pointer" : 19, + "name" : { + "name" : "a", + "qual_name" : [ + "a" + ] + }, + "qual_type" : { + "type_ptr" : 10 + } + } + } + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "cast_kind" : <"LValueToRValue">, + "base_path" : [ + ] + }, + false + )>, + <"ImplicitCastExpr" : ( + { + "pointer" : 37, + "source_range" : ( + { + "column" : 16 + }, + { + "column" : 16 + } + ) + }, + [ + <"DeclRefExpr" : ( + { + "pointer" : 38, + "source_range" : ( + { + "column" : 16 + }, + { + "column" : 16 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 10 + }, + "value_kind" : <"LValue"> + }, + { + "decl_ref" : { + "kind" : <"Var">, + "decl_pointer" : 20, + "name" : { + "name" : "b", + "qual_name" : [ + "b" + ] + }, + "qual_type" : { + "type_ptr" : 10 + } + } + } + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "cast_kind" : <"LValueToRValue">, + "base_path" : [ + ] + }, + false + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + } + } + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "kind" : <"Assign"> + } + )>, + <"BinaryOperator" : ( + { + "pointer" : 39, + "source_range" : ( + { + "line" : 9, + "column" : 5 + }, + { + "column" : 21 + } + ) + }, + [ + <"DeclRefExpr" : ( + { + "pointer" : 40, + "source_range" : ( + { + "column" : 5 + }, + { + "column" : 5 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 10 + }, + "value_kind" : <"LValue"> + }, + { + "decl_ref" : { + "kind" : <"Var">, + "decl_pointer" : 22, + "name" : { + "name" : "d", + "qual_name" : [ + "d" + ] + }, + "qual_type" : { + "type_ptr" : 10 + } + } + } + )>, + <"CallExpr" : ( + { + "pointer" : 41, + "source_range" : ( + { + "column" : 9 + }, + { + "column" : 21 + } + ) + }, + [ + <"ImplicitCastExpr" : ( + { + "pointer" : 42, + "source_range" : ( + { + "column" : 9 + }, + { + "column" : 9 + } + ) + }, + [ + <"DeclRefExpr" : ( + { + "pointer" : 43, + "source_range" : ( + { + "column" : 9 + }, + { + "column" : 9 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 8 + } + }, + { + "decl_ref" : { + "kind" : <"Function">, + "decl_pointer" : 12, + "name" : { + "name" : "product", + "qual_name" : [ + "product" + ] + }, + "qual_type" : { + "type_ptr" : 8 + } + } + } + )> + ], + { + "qual_type" : { + "type_ptr" : 34 + } + }, + { + "cast_kind" : <"FunctionToPointerDecay">, + "base_path" : [ + ] + }, + false + )>, + <"ImplicitCastExpr" : ( + { + "pointer" : 44, + "source_range" : ( + { + "column" : 17 + }, + { + "column" : 17 + } + ) + }, + [ + <"DeclRefExpr" : ( + { + "pointer" : 45, + "source_range" : ( + { + "column" : 17 + }, + { + "column" : 17 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 10 + }, + "value_kind" : <"LValue"> + }, + { + "decl_ref" : { + "kind" : <"Var">, + "decl_pointer" : 21, + "name" : { + "name" : "c", + "qual_name" : [ + "c" + ] + }, + "qual_type" : { + "type_ptr" : 10 + } + } + } + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "cast_kind" : <"LValueToRValue">, + "base_path" : [ + ] + }, + false + )>, + <"ImplicitCastExpr" : ( + { + "pointer" : 46, + "source_range" : ( + { + "column" : 20 + }, + { + "column" : 20 + } + ) + }, + [ + <"DeclRefExpr" : ( + { + "pointer" : 47, + "source_range" : ( + { + "column" : 20 + }, + { + "column" : 20 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 10 + }, + "value_kind" : <"LValue"> + }, + { + "decl_ref" : { + "kind" : <"Var">, + "decl_pointer" : 22, + "name" : { + "name" : "d", + "qual_name" : [ + "d" + ] + }, + "qual_type" : { + "type_ptr" : 10 + } + } + } + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "cast_kind" : <"LValueToRValue">, + "base_path" : [ + ] + }, + false + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + } + } + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "kind" : <"Assign"> + } + )>, + <"ReturnStmt" : ( + { + "pointer" : 48, + "source_range" : ( + { + "line" : 10, + "column" : 5 + }, + { + "column" : 16 + } + ) + }, + [ + <"BinaryOperator" : ( + { + "pointer" : 49, + "source_range" : ( + { + "column" : 12 + }, + { + "column" : 16 + } + ) + }, + [ + <"ImplicitCastExpr" : ( + { + "pointer" : 50, + "source_range" : ( + { + "column" : 12 + }, + { + "column" : 12 + } + ) + }, + [ + <"DeclRefExpr" : ( + { + "pointer" : 51, + "source_range" : ( + { + "column" : 12 + }, + { + "column" : 12 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 10 + }, + "value_kind" : <"LValue"> + }, + { + "decl_ref" : { + "kind" : <"Var">, + "decl_pointer" : 21, + "name" : { + "name" : "c", + "qual_name" : [ + "c" + ] + }, + "qual_type" : { + "type_ptr" : 10 + } + } + } + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "cast_kind" : <"LValueToRValue">, + "base_path" : [ + ] + }, + false + )>, + <"ImplicitCastExpr" : ( + { + "pointer" : 52, + "source_range" : ( + { + "column" : 16 + }, + { + "column" : 16 + } + ) + }, + [ + <"DeclRefExpr" : ( + { + "pointer" : 53, + "source_range" : ( + { + "column" : 16 + }, + { + "column" : 16 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 10 + }, + "value_kind" : <"LValue"> + }, + { + "decl_ref" : { + "kind" : <"Var">, + "decl_pointer" : 22, + "name" : { + "name" : "d", + "qual_name" : [ + "d" + ] + }, + "qual_type" : { + "type_ptr" : 10 + } + } + } + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "cast_kind" : <"LValueToRValue">, + "base_path" : [ + ] + }, + false + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "kind" : <"Add"> + } + )> + ] + )> + ] + )> + } + )>, + <"TypedefDecl" : ( + { + "pointer" : 54, + "source_range" : ( + { + }, + { + } + ), + "is_implicit" : true + }, + { + "name" : "instancetype", + "qual_name" : [ + "instancetype" + ] + }, + 55, + { + } + )> + ], + { + }, + { + "input_path" : "clang2cabs/sample/import2.c", + "input_kind" : <"IK_C">, + "integer_type_widths" : { + "char_type" : 8, + "short_type" : 16, + "int_type" : 32, + "long_type" : 64, + "longlong_type" : 64 + }, + "types" : [ + <"BuiltinType" : ( + { + "pointer" : 56 + }, + <"Void"> + )>, + <"BuiltinType" : ( + { + "pointer" : 57 + }, + <"Bool"> + )>, + <"BuiltinType" : ( + { + "pointer" : 58 + }, + <"Char_S"> + )>, + <"BuiltinType" : ( + { + "pointer" : 59 + }, + <"SChar"> + )>, + <"BuiltinType" : ( + { + "pointer" : 60 + }, + <"Short"> + )>, + <"BuiltinType" : ( + { + "pointer" : 10 + }, + <"Int"> + )>, + <"BuiltinType" : ( + { + "pointer" : 61 + }, + <"Long"> + )>, + <"BuiltinType" : ( + { + "pointer" : 62 + }, + <"LongLong"> + )>, + <"BuiltinType" : ( + { + "pointer" : 63 + }, + <"UChar"> + )>, + <"BuiltinType" : ( + { + "pointer" : 64 + }, + <"UShort"> + )>, + <"BuiltinType" : ( + { + "pointer" : 65 + }, + <"UInt"> + )>, + <"BuiltinType" : ( + { + "pointer" : 66 + }, + <"ULong"> + )>, + <"BuiltinType" : ( + { + "pointer" : 67 + }, + <"ULongLong"> + )>, + <"BuiltinType" : ( + { + "pointer" : 68 + }, + <"Float"> + )>, + <"BuiltinType" : ( + { + "pointer" : 69 + }, + <"Double"> + )>, + <"BuiltinType" : ( + { + "pointer" : 70 + }, + <"LongDouble"> + )>, + <"BuiltinType" : ( + { + "pointer" : 71 + }, + <"Float128"> + )>, + <"BuiltinType" : ( + { + "pointer" : 72 + }, + <"Float16"> + )>, + <"BuiltinType" : ( + { + "pointer" : 73 + }, + <"ShortAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 74 + }, + <"Accum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 75 + }, + <"LongAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 76 + }, + <"UShortAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 77 + }, + <"UAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 78 + }, + <"ULongAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 79 + }, + <"ShortFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 80 + }, + <"Fract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 81 + }, + <"LongFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 82 + }, + <"UShortFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 83 + }, + <"UFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 84 + }, + <"ULongFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 85 + }, + <"SatShortAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 86 + }, + <"SatAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 87 + }, + <"SatLongAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 88 + }, + <"SatUShortAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 89 + }, + <"SatUAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 90 + }, + <"SatULongAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 91 + }, + <"SatShortFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 92 + }, + <"SatFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 93 + }, + <"SatLongFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 94 + }, + <"SatUShortFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 95 + }, + <"SatUFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 96 + }, + <"SatULongFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 97 + }, + <"Int128"> + )>, + <"BuiltinType" : ( + { + "pointer" : 98 + }, + <"UInt128"> + )>, + <"BuiltinType" : ( + { + "pointer" : 99 + }, + <"WChar_S"> + )>, + <"BuiltinType" : ( + { + "pointer" : 100 + }, + <"Char8"> + )>, + <"BuiltinType" : ( + { + "pointer" : 101 + }, + <"Dependent"> + )>, + <"BuiltinType" : ( + { + "pointer" : 102 + }, + <"Overload"> + )>, + <"BuiltinType" : ( + { + "pointer" : 103 + }, + <"BoundMember"> + )>, + <"BuiltinType" : ( + { + "pointer" : 104 + }, + <"PseudoObject"> + )>, + <"BuiltinType" : ( + { + "pointer" : 105 + }, + <"UnknownAny"> + )>, + <"BuiltinType" : ( + { + "pointer" : 106 + }, + <"ARCUnbridgedCast"> + )>, + <"BuiltinType" : ( + { + "pointer" : 107 + }, + <"BuiltinFn"> + )>, + <"ComplexType" : ( + { + "pointer" : 108 + } + )>, + <"ComplexType" : ( + { + "pointer" : 109 + } + )>, + <"ComplexType" : ( + { + "pointer" : 110 + } + )>, + <"ComplexType" : ( + { + "pointer" : 111 + } + )>, + <"BuiltinType" : ( + { + "pointer" : 112 + }, + <"ObjCId"> + )>, + <"BuiltinType" : ( + { + "pointer" : 113 + }, + <"ObjCClass"> + )>, + <"BuiltinType" : ( + { + "pointer" : 114 + }, + <"ObjCSel"> + )>, + <"PointerType" : ( + { + "pointer" : 115 + }, + { + "type_ptr" : 56 + } + )>, + <"BuiltinType" : ( + { + "pointer" : 116 + }, + <"NullPtr"> + )>, + <"BuiltinType" : ( + { + "pointer" : 117 + }, + <"Half"> + )>, + <"BuiltinType" : ( + { + "pointer" : 118 + }, + <"BFloat16"> + )>, + <"RecordType" : ( + { + "pointer" : 119 + }, + 120 + )>, + <"PointerType" : ( + { + "pointer" : 121 + }, + { + "type_ptr" : 10, + "is_const" : true + } + )>, + <"PointerType" : ( + { + "pointer" : 122 + }, + { + "type_ptr" : 58, + "is_const" : true + } + )>, + <"PointerType" : ( + { + "pointer" : 123 + }, + { + "type_ptr" : 58 + } + )>, + <"RecordType" : ( + { + "pointer" : 124 + }, + 125 + )>, + <"ConstantArrayType" : ( + { + "pointer" : 126 + }, + { + "element_type" : { + "type_ptr" : 124 + }, + "stride" : 24 + }, + 1 + )>, + <"FunctionProtoType" : ( + { + "pointer" : 8 + }, + { + "return_type" : { + "type_ptr" : 10 + } + }, + { + "params_type" : [ + { + "type_ptr" : 10 + }, + { + "type_ptr" : 10 + } + ] + } + )>, + <"FunctionNoProtoType" : ( + { + "pointer" : 16 + }, + { + "return_type" : { + "type_ptr" : 10 + } + } + )>, + <"PointerType" : ( + { + "pointer" : 34 + }, + { + "type_ptr" : 8 + } + )>, + <"ObjCObjectType" : ( + { + "pointer" : 127 + }, + { + "base_type" : 112 + } + )>, + <"ObjCObjectPointerType" : ( + { + "pointer" : 128 + }, + { + "type_ptr" : 127 + } + )>, + <"TypedefType" : ( + { + "pointer" : 129, + "desugared_type" : 128 + }, + { + "child_type" : { + "type_ptr" : 128 + }, + "decl_ptr" : 130 + } + )>, + <"TypedefType" : ( + { + "pointer" : 55, + "desugared_type" : 128 + }, + { + "child_type" : { + "type_ptr" : 129 + }, + "decl_ptr" : 54 + } + )>, + <"NoneType" : ( + { + "pointer" : 0 + } + )> + ] + } +)> diff --git a/src/lib/ast.ml b/src/lib/ast.ml index 8399f2e..dcd3cd1 100644 --- a/src/lib/ast.ml +++ b/src/lib/ast.ml @@ -1,3 +1,5 @@ +open Util + type file = string * definition list and type_specifier = @@ -81,9 +83,14 @@ and show_definition indent = function indent ^ "Function(\n" ^ indent ^ " name: " ^ show_name func_name ^ "; return_type: " ^ show_specifier return_type ^ "\n" ^ indent ^ " arguments: " ^ args ^ "\n" ^ - indent ^ " {\n" ^ - show_block (" " ^ indent) block ^ "\n" ^ - indent ^ " }\n" ^ + ( + if List.is_empty block then + "" + else + indent ^ " {\n" ^ + show_block (" " ^ indent) block ^ "\n" ^ + indent ^ " }\n" + ) ^ indent ^ ")" | DECDEF init_name_group -> let init_name_group = show_init_name_group (" " ^ indent) init_name_group in diff --git a/src/lib/util.ml b/src/lib/util.ml index 91d87f3..8f6436f 100644 --- a/src/lib/util.ml +++ b/src/lib/util.ml @@ -22,6 +22,14 @@ let load_from_file filename = using_in filename (fun ch -> Marshal.from_channel ch) +module List = struct + include List + + let is_empty = function + | [] -> true + | _ -> false +end + module Result = struct include Result diff --git a/src/lib/util.mli b/src/lib/util.mli index 189a3f7..0c1f5e4 100644 --- a/src/lib/util.mli +++ b/src/lib/util.mli @@ -4,6 +4,12 @@ val save_to_file : string -> 'a -> (unit, exn) result val load_from_file : string -> ('a, exn) result +module List : sig + include module type of List + + val is_empty : 'a list -> bool +end + module Result : sig include module type of Result diff --git a/src/lib/yojson2ast.ml b/src/lib/yojson2ast.ml index a347ffc..d662cd4 100644 --- a/src/lib/yojson2ast.ml +++ b/src/lib/yojson2ast.ml @@ -493,13 +493,13 @@ let ast_of_yojson typemap function_typeinfo definitions = | _ -> [] end in - let body = - begin match List.assoc_opt "body" data with - | Some body -> parse_body typemap body - | None -> raise (Invalid_Yojson ("Body not found.", yojson)) - end - in - Ast.FUNDEF (single_name, params, body) :: definitions + begin match List.assoc_opt "body" data with + | Some body -> + let body = parse_body typemap body in + Ast.FUNDEF (single_name, params, body) :: definitions + | None -> + Ast.FUNDEF (single_name, params, []) :: definitions + end | _ -> definitions in From 1ceb17d59c4c124b3d4fa55dc67b61e4ae3ab28b Mon Sep 17 00:00:00 2001 From: cedretaber Date: Tue, 8 Mar 2022 01:51:47 +0900 Subject: [PATCH 2/6] Enhance README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index b52f229..998945e 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,10 @@ ## Requirement * clang plugin by facebook infer +* C source files according to the definition of ['small C'][smallc] + * The source code, including the include destination, must be strictly ['small C'][smallc] compliant. + +[smallc]: https://hackmd.io/d-mZWBTmReSPqx8HUMIk_g ## Comand line interfaces From b966dcca1da419fae2a0c96317b561819e422d46 Mon Sep 17 00:00:00 2001 From: cedretaber Date: Tue, 8 Mar 2022 02:51:01 +0900 Subject: [PATCH 3/6] Support for fixed-length array --- sample/sarray.c | 7 + sample/sarray.c.yojson | 1369 ++++++++++++++++++++++++++++++++++++++++ src/lib/ast.ml | 8 +- src/lib/ast.mli | 32 +- src/lib/ast2cabs.ml | 5 +- src/lib/cabs.ml | 3 +- src/lib/yojson2ast.ml | 35 +- 7 files changed, 1445 insertions(+), 14 deletions(-) create mode 100644 sample/sarray.c create mode 100644 sample/sarray.c.yojson diff --git a/sample/sarray.c b/sample/sarray.c new file mode 100644 index 0000000..ed9e6b7 --- /dev/null +++ b/sample/sarray.c @@ -0,0 +1,7 @@ +void test(int a) +{ + int arr[3]; + arr[0] = a; + arr[1] = a; + arr[2] = a; +} diff --git a/sample/sarray.c.yojson b/sample/sarray.c.yojson new file mode 100644 index 0000000..d05a3f0 --- /dev/null +++ b/sample/sarray.c.yojson @@ -0,0 +1,1369 @@ +<"TranslationUnitDecl" : ( + { + "pointer" : 1, + "source_range" : ( + { + }, + { + } + ) + }, + [ + <"TypedefDecl" : ( + { + "pointer" : 2, + "source_range" : ( + { + }, + { + } + ), + "is_implicit" : true + }, + { + "name" : "__int128_t", + "qual_name" : [ + "__int128_t" + ] + }, + 0, + { + } + )>, + <"TypedefDecl" : ( + { + "pointer" : 3, + "source_range" : ( + { + }, + { + } + ), + "is_implicit" : true + }, + { + "name" : "__uint128_t", + "qual_name" : [ + "__uint128_t" + ] + }, + 0, + { + } + )>, + <"TypedefDecl" : ( + { + "pointer" : 4, + "source_range" : ( + { + }, + { + } + ), + "is_implicit" : true + }, + { + "name" : "__NSConstantString", + "qual_name" : [ + "__NSConstantString" + ] + }, + 0, + { + } + )>, + <"TypedefDecl" : ( + { + "pointer" : 5, + "source_range" : ( + { + }, + { + } + ), + "is_implicit" : true + }, + { + "name" : "__builtin_ms_va_list", + "qual_name" : [ + "__builtin_ms_va_list" + ] + }, + 0, + { + } + )>, + <"TypedefDecl" : ( + { + "pointer" : 6, + "source_range" : ( + { + }, + { + } + ), + "is_implicit" : true + }, + { + "name" : "__builtin_va_list", + "qual_name" : [ + "__builtin_va_list" + ] + }, + 0, + { + } + )>, + <"FunctionDecl" : ( + { + "pointer" : 7, + "source_range" : ( + { + "file" : "clang2cabs/sample/sarray.c", + "line" : 1, + "column" : 1 + }, + { + "line" : 7, + "column" : 1 + } + ) + }, + { + "name" : "test", + "qual_name" : [ + "test" + ] + }, + { + "type_ptr" : 8 + }, + { + "parameters" : [ + <"ParmVarDecl" : ( + { + "pointer" : 9, + "source_range" : ( + { + "line" : 1, + "column" : 11 + }, + { + "column" : 15 + } + ), + "is_used" : true, + "is_this_declaration_referenced" : true + }, + { + "name" : "a", + "qual_name" : [ + "a" + ] + }, + { + "type_ptr" : 10 + }, + { + "parm_index_in_function" : 0 + } + )> + ], + "decl_ptr_with_body" : 7, + "body" : <"CompoundStmt" : ( + { + "pointer" : 11, + "source_range" : ( + { + "line" : 2, + "column" : 1 + }, + { + "line" : 7, + "column" : 1 + } + ) + }, + [ + <"DeclStmt" : ( + { + "pointer" : 12, + "source_range" : ( + { + "line" : 3, + "column" : 5 + }, + { + "column" : 15 + } + ) + }, + [ + ], + [ + <"VarDecl" : ( + { + "pointer" : 13, + "source_range" : ( + { + "column" : 5 + }, + { + "column" : 14 + } + ), + "is_used" : true, + "is_this_declaration_referenced" : true + }, + { + "name" : "arr", + "qual_name" : [ + "arr" + ] + }, + { + "type_ptr" : 14 + }, + { + } + )> + ] + )>, + <"BinaryOperator" : ( + { + "pointer" : 15, + "source_range" : ( + { + "line" : 4, + "column" : 5 + }, + { + "column" : 14 + } + ) + }, + [ + <"ArraySubscriptExpr" : ( + { + "pointer" : 16, + "source_range" : ( + { + "column" : 5 + }, + { + "column" : 10 + } + ) + }, + [ + <"ImplicitCastExpr" : ( + { + "pointer" : 17, + "source_range" : ( + { + "column" : 5 + }, + { + "column" : 5 + } + ) + }, + [ + <"DeclRefExpr" : ( + { + "pointer" : 18, + "source_range" : ( + { + "column" : 5 + }, + { + "column" : 5 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 14 + }, + "value_kind" : <"LValue"> + }, + { + "decl_ref" : { + "kind" : <"Var">, + "decl_pointer" : 13, + "name" : { + "name" : "arr", + "qual_name" : [ + "arr" + ] + }, + "qual_type" : { + "type_ptr" : 14 + } + } + } + )> + ], + { + "qual_type" : { + "type_ptr" : 19 + } + }, + { + "cast_kind" : <"ArrayToPointerDecay">, + "base_path" : [ + ] + }, + false + )>, + <"IntegerLiteral" : ( + { + "pointer" : 20, + "source_range" : ( + { + "column" : 9 + }, + { + "column" : 9 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "is_signed" : true, + "bitwidth" : 32, + "value" : "0" + } + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + }, + "value_kind" : <"LValue"> + } + )>, + <"ImplicitCastExpr" : ( + { + "pointer" : 21, + "source_range" : ( + { + "column" : 14 + }, + { + "column" : 14 + } + ) + }, + [ + <"DeclRefExpr" : ( + { + "pointer" : 22, + "source_range" : ( + { + "column" : 14 + }, + { + "column" : 14 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 10 + }, + "value_kind" : <"LValue"> + }, + { + "decl_ref" : { + "kind" : <"ParmVar">, + "decl_pointer" : 9, + "name" : { + "name" : "a", + "qual_name" : [ + "a" + ] + }, + "qual_type" : { + "type_ptr" : 10 + } + } + } + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "cast_kind" : <"LValueToRValue">, + "base_path" : [ + ] + }, + false + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "kind" : <"Assign"> + } + )>, + <"BinaryOperator" : ( + { + "pointer" : 23, + "source_range" : ( + { + "line" : 5, + "column" : 5 + }, + { + "column" : 14 + } + ) + }, + [ + <"ArraySubscriptExpr" : ( + { + "pointer" : 24, + "source_range" : ( + { + "column" : 5 + }, + { + "column" : 10 + } + ) + }, + [ + <"ImplicitCastExpr" : ( + { + "pointer" : 25, + "source_range" : ( + { + "column" : 5 + }, + { + "column" : 5 + } + ) + }, + [ + <"DeclRefExpr" : ( + { + "pointer" : 26, + "source_range" : ( + { + "column" : 5 + }, + { + "column" : 5 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 14 + }, + "value_kind" : <"LValue"> + }, + { + "decl_ref" : { + "kind" : <"Var">, + "decl_pointer" : 13, + "name" : { + "name" : "arr", + "qual_name" : [ + "arr" + ] + }, + "qual_type" : { + "type_ptr" : 14 + } + } + } + )> + ], + { + "qual_type" : { + "type_ptr" : 19 + } + }, + { + "cast_kind" : <"ArrayToPointerDecay">, + "base_path" : [ + ] + }, + false + )>, + <"IntegerLiteral" : ( + { + "pointer" : 27, + "source_range" : ( + { + "column" : 9 + }, + { + "column" : 9 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "is_signed" : true, + "bitwidth" : 32, + "value" : "1" + } + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + }, + "value_kind" : <"LValue"> + } + )>, + <"ImplicitCastExpr" : ( + { + "pointer" : 28, + "source_range" : ( + { + "column" : 14 + }, + { + "column" : 14 + } + ) + }, + [ + <"DeclRefExpr" : ( + { + "pointer" : 29, + "source_range" : ( + { + "column" : 14 + }, + { + "column" : 14 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 10 + }, + "value_kind" : <"LValue"> + }, + { + "decl_ref" : { + "kind" : <"ParmVar">, + "decl_pointer" : 9, + "name" : { + "name" : "a", + "qual_name" : [ + "a" + ] + }, + "qual_type" : { + "type_ptr" : 10 + } + } + } + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "cast_kind" : <"LValueToRValue">, + "base_path" : [ + ] + }, + false + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "kind" : <"Assign"> + } + )>, + <"BinaryOperator" : ( + { + "pointer" : 30, + "source_range" : ( + { + "line" : 6, + "column" : 5 + }, + { + "column" : 14 + } + ) + }, + [ + <"ArraySubscriptExpr" : ( + { + "pointer" : 31, + "source_range" : ( + { + "column" : 5 + }, + { + "column" : 10 + } + ) + }, + [ + <"ImplicitCastExpr" : ( + { + "pointer" : 32, + "source_range" : ( + { + "column" : 5 + }, + { + "column" : 5 + } + ) + }, + [ + <"DeclRefExpr" : ( + { + "pointer" : 33, + "source_range" : ( + { + "column" : 5 + }, + { + "column" : 5 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 14 + }, + "value_kind" : <"LValue"> + }, + { + "decl_ref" : { + "kind" : <"Var">, + "decl_pointer" : 13, + "name" : { + "name" : "arr", + "qual_name" : [ + "arr" + ] + }, + "qual_type" : { + "type_ptr" : 14 + } + } + } + )> + ], + { + "qual_type" : { + "type_ptr" : 19 + } + }, + { + "cast_kind" : <"ArrayToPointerDecay">, + "base_path" : [ + ] + }, + false + )>, + <"IntegerLiteral" : ( + { + "pointer" : 34, + "source_range" : ( + { + "column" : 9 + }, + { + "column" : 9 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "is_signed" : true, + "bitwidth" : 32, + "value" : "2" + } + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + }, + "value_kind" : <"LValue"> + } + )>, + <"ImplicitCastExpr" : ( + { + "pointer" : 35, + "source_range" : ( + { + "column" : 14 + }, + { + "column" : 14 + } + ) + }, + [ + <"DeclRefExpr" : ( + { + "pointer" : 36, + "source_range" : ( + { + "column" : 14 + }, + { + "column" : 14 + } + ) + }, + [ + ], + { + "qual_type" : { + "type_ptr" : 10 + }, + "value_kind" : <"LValue"> + }, + { + "decl_ref" : { + "kind" : <"ParmVar">, + "decl_pointer" : 9, + "name" : { + "name" : "a", + "qual_name" : [ + "a" + ] + }, + "qual_type" : { + "type_ptr" : 10 + } + } + } + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "cast_kind" : <"LValueToRValue">, + "base_path" : [ + ] + }, + false + )> + ], + { + "qual_type" : { + "type_ptr" : 10 + } + }, + { + "kind" : <"Assign"> + } + )> + ] + )> + } + )>, + <"TypedefDecl" : ( + { + "pointer" : 37, + "source_range" : ( + { + }, + { + } + ), + "is_implicit" : true + }, + { + "name" : "instancetype", + "qual_name" : [ + "instancetype" + ] + }, + 38, + { + } + )> + ], + { + }, + { + "input_path" : "clang2cabs/sample/sarray.c", + "input_kind" : <"IK_C">, + "integer_type_widths" : { + "char_type" : 8, + "short_type" : 16, + "int_type" : 32, + "long_type" : 64, + "longlong_type" : 64 + }, + "types" : [ + <"BuiltinType" : ( + { + "pointer" : 39 + }, + <"Void"> + )>, + <"BuiltinType" : ( + { + "pointer" : 40 + }, + <"Bool"> + )>, + <"BuiltinType" : ( + { + "pointer" : 41 + }, + <"Char_S"> + )>, + <"BuiltinType" : ( + { + "pointer" : 42 + }, + <"SChar"> + )>, + <"BuiltinType" : ( + { + "pointer" : 43 + }, + <"Short"> + )>, + <"BuiltinType" : ( + { + "pointer" : 10 + }, + <"Int"> + )>, + <"BuiltinType" : ( + { + "pointer" : 44 + }, + <"Long"> + )>, + <"BuiltinType" : ( + { + "pointer" : 45 + }, + <"LongLong"> + )>, + <"BuiltinType" : ( + { + "pointer" : 46 + }, + <"UChar"> + )>, + <"BuiltinType" : ( + { + "pointer" : 47 + }, + <"UShort"> + )>, + <"BuiltinType" : ( + { + "pointer" : 48 + }, + <"UInt"> + )>, + <"BuiltinType" : ( + { + "pointer" : 49 + }, + <"ULong"> + )>, + <"BuiltinType" : ( + { + "pointer" : 50 + }, + <"ULongLong"> + )>, + <"BuiltinType" : ( + { + "pointer" : 51 + }, + <"Float"> + )>, + <"BuiltinType" : ( + { + "pointer" : 52 + }, + <"Double"> + )>, + <"BuiltinType" : ( + { + "pointer" : 53 + }, + <"LongDouble"> + )>, + <"BuiltinType" : ( + { + "pointer" : 54 + }, + <"Float128"> + )>, + <"BuiltinType" : ( + { + "pointer" : 55 + }, + <"Float16"> + )>, + <"BuiltinType" : ( + { + "pointer" : 56 + }, + <"ShortAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 57 + }, + <"Accum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 58 + }, + <"LongAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 59 + }, + <"UShortAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 60 + }, + <"UAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 61 + }, + <"ULongAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 62 + }, + <"ShortFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 63 + }, + <"Fract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 64 + }, + <"LongFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 65 + }, + <"UShortFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 66 + }, + <"UFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 67 + }, + <"ULongFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 68 + }, + <"SatShortAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 69 + }, + <"SatAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 70 + }, + <"SatLongAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 71 + }, + <"SatUShortAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 72 + }, + <"SatUAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 73 + }, + <"SatULongAccum"> + )>, + <"BuiltinType" : ( + { + "pointer" : 74 + }, + <"SatShortFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 75 + }, + <"SatFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 76 + }, + <"SatLongFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 77 + }, + <"SatUShortFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 78 + }, + <"SatUFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 79 + }, + <"SatULongFract"> + )>, + <"BuiltinType" : ( + { + "pointer" : 80 + }, + <"Int128"> + )>, + <"BuiltinType" : ( + { + "pointer" : 81 + }, + <"UInt128"> + )>, + <"BuiltinType" : ( + { + "pointer" : 82 + }, + <"WChar_S"> + )>, + <"BuiltinType" : ( + { + "pointer" : 83 + }, + <"Char8"> + )>, + <"BuiltinType" : ( + { + "pointer" : 84 + }, + <"Dependent"> + )>, + <"BuiltinType" : ( + { + "pointer" : 85 + }, + <"Overload"> + )>, + <"BuiltinType" : ( + { + "pointer" : 86 + }, + <"BoundMember"> + )>, + <"BuiltinType" : ( + { + "pointer" : 87 + }, + <"PseudoObject"> + )>, + <"BuiltinType" : ( + { + "pointer" : 88 + }, + <"UnknownAny"> + )>, + <"BuiltinType" : ( + { + "pointer" : 89 + }, + <"ARCUnbridgedCast"> + )>, + <"BuiltinType" : ( + { + "pointer" : 90 + }, + <"BuiltinFn"> + )>, + <"ComplexType" : ( + { + "pointer" : 91 + } + )>, + <"ComplexType" : ( + { + "pointer" : 92 + } + )>, + <"ComplexType" : ( + { + "pointer" : 93 + } + )>, + <"ComplexType" : ( + { + "pointer" : 94 + } + )>, + <"BuiltinType" : ( + { + "pointer" : 95 + }, + <"ObjCId"> + )>, + <"BuiltinType" : ( + { + "pointer" : 96 + }, + <"ObjCClass"> + )>, + <"BuiltinType" : ( + { + "pointer" : 97 + }, + <"ObjCSel"> + )>, + <"PointerType" : ( + { + "pointer" : 98 + }, + { + "type_ptr" : 39 + } + )>, + <"BuiltinType" : ( + { + "pointer" : 99 + }, + <"NullPtr"> + )>, + <"BuiltinType" : ( + { + "pointer" : 100 + }, + <"Half"> + )>, + <"BuiltinType" : ( + { + "pointer" : 101 + }, + <"BFloat16"> + )>, + <"RecordType" : ( + { + "pointer" : 102 + }, + 103 + )>, + <"PointerType" : ( + { + "pointer" : 104 + }, + { + "type_ptr" : 10, + "is_const" : true + } + )>, + <"PointerType" : ( + { + "pointer" : 105 + }, + { + "type_ptr" : 41, + "is_const" : true + } + )>, + <"PointerType" : ( + { + "pointer" : 106 + }, + { + "type_ptr" : 41 + } + )>, + <"RecordType" : ( + { + "pointer" : 107 + }, + 108 + )>, + <"ConstantArrayType" : ( + { + "pointer" : 109 + }, + { + "element_type" : { + "type_ptr" : 107 + }, + "stride" : 24 + }, + 1 + )>, + <"FunctionProtoType" : ( + { + "pointer" : 8 + }, + { + "return_type" : { + "type_ptr" : 39 + } + }, + { + "params_type" : [ + { + "type_ptr" : 10 + } + ] + } + )>, + <"ConstantArrayType" : ( + { + "pointer" : 14 + }, + { + "element_type" : { + "type_ptr" : 10 + }, + "stride" : 4 + }, + 3 + )>, + <"PointerType" : ( + { + "pointer" : 19 + }, + { + "type_ptr" : 10 + } + )>, + <"ObjCObjectType" : ( + { + "pointer" : 110 + }, + { + "base_type" : 95 + } + )>, + <"ObjCObjectPointerType" : ( + { + "pointer" : 111 + }, + { + "type_ptr" : 110 + } + )>, + <"TypedefType" : ( + { + "pointer" : 112, + "desugared_type" : 111 + }, + { + "child_type" : { + "type_ptr" : 111 + }, + "decl_ptr" : 113 + } + )>, + <"TypedefType" : ( + { + "pointer" : 38, + "desugared_type" : 111 + }, + { + "child_type" : { + "type_ptr" : 112 + }, + "decl_ptr" : 37 + } + )>, + <"NoneType" : ( + { + "pointer" : 0 + } + )> + ] + } +)> diff --git a/src/lib/ast.ml b/src/lib/ast.ml index dcd3cd1..fa543c1 100644 --- a/src/lib/ast.ml +++ b/src/lib/ast.ml @@ -10,7 +10,8 @@ and specifier = type_specifier list and decl_type = | JUSTBASE - | ARRAY of decl_type + | PTR of decl_type + | ARRAY of decl_type * expression and init_name_group = specifier * init_name list @@ -22,7 +23,7 @@ and single_name = specifier * name and definition = | FUNDEF of single_name * single_name list * block - | DECDEF of init_name_group (* global variable(s), or function prototype *) + | DECDEF of init_name_group and block = statement list @@ -115,7 +116,8 @@ and show_init_name ((name, decl_type), init_expr) = name ^ " = " ^ show_expression expr and show_decl_type = function | JUSTBASE -> "" - | ARRAY decl_type -> show_decl_type decl_type ^ "[]" + | PTR decl_type -> "*" ^ show_decl_type decl_type + | ARRAY (decl_type, expr) -> show_decl_type decl_type ^ "[" ^ show_expression expr ^ "]" and show_specifier specifier_list = String.concat ", " @@ List.map show_type_specifier specifier_list and show_type_specifier = function diff --git a/src/lib/ast.mli b/src/lib/ast.mli index 8a336e7..fb03ca4 100644 --- a/src/lib/ast.mli +++ b/src/lib/ast.mli @@ -4,21 +4,51 @@ and type_specifier = | Tvoid | Tint +(* In the 'small C' range, only type information is passed here. *) and specifier = type_specifier list +(* Declarator type. They modify the base type given in the specifier. + * e.g. + * int[3][4] -> ARRAY(ARRAY(JUSTBASE, "3"), "4") + * int -> JUSTBASE + *) and decl_type = | JUSTBASE - | ARRAY of decl_type + | PTR of decl_type (* This is not used currently. *) + | ARRAY of decl_type * expression +(* like name_group, except the declared variables are allowed to have initializers + * e.g. + * int x = 1, y = 2; + *) and init_name_group = specifier * init_name list +(* The decl_type is in the order in which they are printed. Only the name of + * the declared identifier is pulled out. The attributes are those that are + * printed after the declarator + * e.g. + * in "int x[]", "x[]" is the declarator; "x" will be pulled out as + * the string, and decl_type will be ARRAY(JUSTBASE) + *) and name = string * decl_type +(* A variable declarator ("name") with an initializer. *) and init_name = name * init_expression +(* Single names are for declarations that cannot come in groups, like + * function parameters and functions. + *) and single_name = specifier * name +(* + * Declaration definition (at toplevel) + *) and definition = + (* + * single_name: The function's name and type + * single_name_list: The arguments' names and types + * block: The function's body + *) | FUNDEF of single_name * single_name list * block | DECDEF of init_name_group (* global variable(s), or function prototype *) diff --git a/src/lib/ast2cabs.ml b/src/lib/ast2cabs.ml index 300b85f..ab1f4f8 100644 --- a/src/lib/ast2cabs.ml +++ b/src/lib/ast2cabs.ml @@ -110,7 +110,10 @@ and conv_specifiler type_specifiers : Cabs.specifier = and conv_decl_type : Ast.decl_type -> Cabs.decl_type = function | Ast.JUSTBASE -> Cabs.JUSTBASE - | Ast.ARRAY decl_type -> Cabs.ARRAY (conv_decl_type decl_type, [], Cabs.NOTHING) + | Ast.PTR decl_type -> + Cabs.PTR ([], conv_decl_type decl_type) + | Ast.ARRAY (decl_type, expr) -> + Cabs.ARRAY (conv_decl_type decl_type, [], conv_expression expr) and conv_name (name, decl_type) : Cabs.name = name, conv_decl_type decl_type, [], dummy_loc diff --git a/src/lib/cabs.ml b/src/lib/cabs.ml index 154e3bb..9d5d36b 100644 --- a/src/lib/cabs.ml +++ b/src/lib/cabs.ml @@ -17,7 +17,8 @@ and spec_elem = and specifier = spec_elem list and decl_type = - JUSTBASE + JUSTBASE + | PTR of attribute list * decl_type (* Not appearing because it is outside the range of 'small c'. *) | ARRAY of decl_type * attribute list * expression and name_group = specifier * name list diff --git a/src/lib/yojson2ast.ml b/src/lib/yojson2ast.ml index d662cd4..a472be8 100644 --- a/src/lib/yojson2ast.ml +++ b/src/lib/yojson2ast.ml @@ -1,8 +1,9 @@ exception Invalid_Yojson of string * Yojson.Safe.t -type c_type = +type [@warning "-37"] c_type = | Just of Ast.type_specifier | Pointer of int + | Array of { ptr: int; size: int } module PointerMap = Map.Make(Int) @@ -10,9 +11,17 @@ let find_type typemap i = let rec impl p = match PointerMap.find_opt p typemap with | Some (Just tpe) -> Some (tpe, Ast.JUSTBASE) - | Some (Pointer p) -> - begin match impl p with - | Some (tpe, decl_type) -> Some (tpe, Ast.ARRAY decl_type) + | Some (Pointer ptr) -> + begin match impl ptr with + | Some (tpe, decl_type) -> + Some (tpe, Ast.PTR decl_type) + | None -> None + end + | Some (Array { ptr; size }) -> + begin match impl ptr with + | Some (tpe, decl_type) -> + let expr = Ast.CONSTANT (Ast.CONST_INT (string_of_int size)) in + Some (tpe, Ast.ARRAY (decl_type, expr)) | None -> None end | None -> None @@ -36,14 +45,15 @@ let make_typemap typedata = List.fold_left (fun map -> function )) ) -> PointerMap.add i (Just Ast.Tvoid) map - | `Variant ( + (* in 'small C', we do not treat pointer type. *) + (* | `Variant ( "PointerType", Some (`Tuple ( `Assoc [("pointer", `Int i)] :: `Assoc [("type_ptr", `Int p)] :: _ )) ) -> - PointerMap.add i (Pointer p) map + PointerMap.add i (Pointer p) map *) | `Variant ( "ConstantArrayType", Some (`Tuple ( `Assoc [("pointer", `Int i)] :: @@ -51,10 +61,11 @@ let make_typemap typedata = List.fold_left (fun map -> function ("element_type", `Assoc [("type_ptr", `Int p)]); _stride ] :: + (`Int size) :: _ )) ) -> - PointerMap.add i (Pointer p) map + PointerMap.add i (Array { ptr= p; size }) map | _ -> map ) @@ -64,6 +75,7 @@ let make_typemap typedata = List.fold_left (fun map -> function let show_c_type = function | Just tpe -> Ast.show_type_specifier tpe | Pointer p -> "Pointer->" ^ string_of_int p + | Array { ptr; size } -> "Array[" ^ string_of_int size ^ "]->" ^ string_of_int ptr let show_typemap typemap = typemap |> PointerMap.iter (fun idx tpe -> @@ -478,9 +490,16 @@ let ast_of_yojson typemap function_typeinfo definitions = | Some { return_type; _ } -> begin match return_type with | Just tpe -> tpe, Ast.JUSTBASE + | Array { ptr; size} -> + begin match find_type typemap ptr with + | Some (tpe, decl_type) -> + let expr = Ast.CONSTANT (Ast.CONST_INT (string_of_int size)) in + tpe, (Ast.ARRAY (decl_type, expr)) + | None -> raise (Invalid_Yojson ("Function type not found.", yojson)) + end | Pointer i -> match find_type typemap i with - | Some (tpe, decl_type) -> tpe, (Ast.ARRAY decl_type) + | Some (tpe, decl_type) -> tpe, (Ast.PTR decl_type) | None -> raise (Invalid_Yojson ("Function type not found.", yojson)) end | None -> raise (Invalid_Yojson ("Function type not found.", yojson)) From a87ce0b024c8402d22cdc5cdf7bdc7f0f9257d65 Mon Sep 17 00:00:00 2001 From: cedretaber Date: Tue, 8 Mar 2022 15:34:24 +0900 Subject: [PATCH 4/6] Add 'small C' definition to README.md --- README.md | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 998945e..f829a57 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,30 @@ ## Requirement * clang plugin by facebook infer -* C source files according to the definition of ['small C'][smallc] - * The source code, including the include destination, must be strictly ['small C'][smallc] compliant. +* C source files according to the definition of ['small C'](#small-c) + * The source code, including the include destination, must be strictly ['small C'](#small-c) compliant. -[smallc]: https://hackmd.io/d-mZWBTmReSPqx8HUMIk_g +### small C + +``` +#include +#define + +P ::= x=e; | if (b) P else P | while (b) P | for (e; e; e) P | return e; | return; + | { P ... P } +e ::= int-constants (..., -1, 0, 1, 2, ...) | variables | a[e] + for array name a + | e + e | e - e | e * e | e / e | e % e | (e) | f(e, ..., e) + for function name f +b ::= e == e | e != e | e < e | e > e | e <= e | e >= e | (e) | !e + | e && e | e '||' e + +type ::= void | int +次元配列 +関数定義と呼び出し +グローバル変数 +ローカル変数 (関数定義のはじめだけ) +``` ## Comand line interfaces From d085889234a67c95f9bdbe8f271e3ffcbc324c2b Mon Sep 17 00:00:00 2001 From: cedretaber Date: Tue, 8 Mar 2022 15:36:30 +0900 Subject: [PATCH 5/6] Update README.md --- README.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f829a57..74ec0a2 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,7 @@ # CLANG Parser -## Requirement - -* clang plugin by facebook infer -* C source files according to the definition of ['small C'](#small-c) - * The source code, including the include destination, must be strictly ['small C'](#small-c) compliant. - -### small C +Parses C source code to create an abstract syntax tree. +The definition of "small C", the language to be parsed, is as follows: ``` #include @@ -28,6 +23,10 @@ type ::= void | int ローカル変数 (関数定義のはじめだけ) ``` +## Requirement + +* clang plugin by facebook infer + ## Comand line interfaces `.mas` is a file to which Marshal saves clang abstract syntax trees in From 1edb40ec256ad8fc3b0e9a6c9a6bd76ccc39d452 Mon Sep 17 00:00:00 2001 From: cedretaber Date: Tue, 8 Mar 2022 16:39:45 +0900 Subject: [PATCH 6/6] Use doc comment --- src/lib/ast.mli | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/lib/ast.mli b/src/lib/ast.mli index fb03ca4..8271084 100644 --- a/src/lib/ast.mli +++ b/src/lib/ast.mli @@ -4,10 +4,13 @@ and type_specifier = | Tvoid | Tint -(* In the 'small C' range, only type information is passed here. *) +(** + * In the 'small C' range, only type information is passed here. + *) and specifier = type_specifier list -(* Declarator type. They modify the base type given in the specifier. +(** + * Declarator type. They modify the base type given in the specifier. * e.g. * int[3][4] -> ARRAY(ARRAY(JUSTBASE, "3"), "4") * int -> JUSTBASE @@ -17,13 +20,15 @@ and decl_type = | PTR of decl_type (* This is not used currently. *) | ARRAY of decl_type * expression -(* like name_group, except the declared variables are allowed to have initializers +(** + * like name_group, except the declared variables are allowed to have initializers * e.g. * int x = 1, y = 2; *) and init_name_group = specifier * init_name list -(* The decl_type is in the order in which they are printed. Only the name of +(** + * The decl_type is in the order in which they are printed. Only the name of * the declared identifier is pulled out. The attributes are those that are * printed after the declarator * e.g. @@ -32,25 +37,28 @@ and init_name_group = specifier * init_name list *) and name = string * decl_type -(* A variable declarator ("name") with an initializer. *) +(** + * A variable declarator ("name") with an initializer. + *) and init_name = name * init_expression -(* Single names are for declarations that cannot come in groups, like +(** + * Single names are for declarations that cannot come in groups, like * function parameters and functions. *) and single_name = specifier * name -(* +(** * Declaration definition (at toplevel) *) and definition = (* * single_name: The function's name and type * single_name_list: The arguments' names and types - * block: The function's body + * block: The function's body and function prototype has empty body *) | FUNDEF of single_name * single_name list * block - | DECDEF of init_name_group (* global variable(s), or function prototype *) + | DECDEF of init_name_group (* global variable(s) *) and block = statement list