示例#1
0
文件: interp.py 项目: slox3r/pfp
	def _parse_string(self, string, predefines=True):
		exts = []
		if predefines:
			for predefine in self._predefines:
				try:
					ast = py010parser.parse_string(predefine, parser=self._parser, cpp_path=self._cpp_path, cpp_args=self._cpp_args)
					exts += ast.ext
				except:
					pass

		res = py010parser.parse_string(string, parser=self._parser, cpp_path=self._cpp_path, cpp_args=self._cpp_args)
		res.ext = exts + res.ext

		return res
示例#2
0
 def test_untypedefd_enum_as_typeid(self):
     res = parse_string("""
         enum <ulong> BLAH {
             BLAH1, BLAH2, BLAH3
         };
         local BLAH x;
     """, optimize=True)
示例#3
0
 def test_basic(self):
     res = parse_string("""
         struct NAME {
                 int		stringLength;
                 char	name[stringLength];
         } name;
     """, optimize=True)
示例#4
0
    def test_value_types(self):
        res = parse_string("""
            time_t var1;
            OLETIME var2;
            FILETIME var3;
            DOSTIME var4;
            DOSDATE var5;
            HFLOAT var6;
            hfloat var7;
            DOUBLE var8;
            double var9;
            FLOAT var10;
            float var11;
            __uint64 var12;
            QWORD var13;
            UINT64 var14;
            UQUAD var15;
            uquad var16;
            uint64 var17;
            __int64 var18;
            INT64 var19;
            QUAD var20;
            quad var21;
            int64 var22;
            DWORD var23;
            ULONG var24;
            UINT32 var25;
            UINT var26;
            ulong var27;
            uint32 var28;
            uint var29;
            LONG var30;
            INT32 var31;
            INT var32;
            long var33;
            int32 var34;
            int var35;
            WORD var36;
            UINT16 var37;
            USHORT var38;
            uint16 var39;
            ushort var40;
            INT16 var41;
            SHORT var42;
            int16 var43;
            short var44;
            UBYTE var45;
            UCHAR var46;
            ubyte var47;
            uchar var48;
            BYTE var49;
            CHAR var50;
            byte var51;
            char var52;

            string var53;
            wstring var54;
            wchar_t var55;
        """, optimize=True)
示例#5
0
    def test_pass_by_reference(self):
        res = parse_string("""
        void some_function(int &num, int &num2) {
        }

        void some_function(int &num2) {
        }
        """, optimize=True)
示例#6
0
 def test_struct_bitfield_with_metadata(self):
     res = parse_string("""
         typedef struct tgCifDirEntry {
                 uint16 storage_method : 2;
                 uint16 data_type : 3;
                 uint16 id_code : 11 <format=hex>;
         } CifDirEntry <read=ReadCifDirEntry>;
     """, optimize=True)
示例#7
0
 def test_initializer_in_struct(self):
     res = parse_string("""
         local int b = 11;
         typedef struct BLAH {
             local int a = 10;
             int a:10;
         } blah;
     """, optimize=True)
示例#8
0
 def test_declaration_in_if(self):
     res = parse_string("""
         if(1) {
             int c;
         } else {
             int b;
         }
     """, optimize=True)
示例#9
0
 def test_declaration_in_struct(self):
     res = parse_string("""
         int c;
         switch(c) {
             case 1:
                 c++;
             case 2:
                 int c;
         }
     """, optimize=True)
示例#10
0
 def test_c_keywords_in_enum(self):
     res = parse_string("""
         enum <int> BLAH {
             goto,
             register,
             extern,
             goto,
             volatile,
             static
         };
         local BLAH x;
     """, optimize=True)
示例#11
0
    def test_block_item_at_root(self):
        # had to get rid of the default int ret val on functions
        # from pycparser

        res = parse_string("""
        int a = 10;
        void some_function(int num) {
            some_function();
        }
        a++;
        some_function();
        """, optimize=True)
示例#12
0
 def test_nested_bitfield_in_struct(self):
     res = parse_string("""
         typedef struct BLAH {
             int a;
             switch(a) {
                 case 10:
                     int b:10;
                 default:
                     int c:10;
             }
         } blah;
     """, optimize=True)
示例#13
0
    def test_struct_with_args_calling_not_func_decl2(self):
        res = parse_string("""
            typedef struct(int a) {
                char chars[a];
            } test_structure;

            local int size = 4;
            test_structure test(size); // this SHOULD NOT be a function declaration
        """, predefine_types=False)
        decl = res.children()[2][1]
        self.assertEqual(decl.type.__class__, c_ast.StructCallTypeDecl)
        self.assertEqual(decl.type.args.__class__, c_ast.ExprList)
示例#14
0
    def test_bitfield_in_if(self):
        res = parse_string("""
            struct {
                if(1) {
                    int field1:16;
                    //int field1;
                }
            } blah;
        """, optimize=True, predefine_types=False)

        bitfield_decl = res.children()[0][1].type.type.children()[0][1].iftrue.children()[0][1]
        self.assertNotEqual(type(bitfield_decl), dict)
示例#15
0
    def test_if_in_struct(self):
        res = parse_string("""
            struct BLAH {
                int a:1;
                int b:2;
                int c:29;

                if(hello) {
                    b = 10;
                }
            } blah;
            """, optimize=True)
示例#16
0
    def test_comment_single_line(self):
        res = parse_string("""
            // this is a comment
            int blah;
        """, optimize=True, predefine_types=False)

        self.assertEqual(len(res.children()), 1, "should only have one node in the AST")

        node = res.children()[0][1]
        self.assertEqual(node.name, "blah")
        self.assertEqual(node.type.declname, "blah")
        self.assertEqual(node.type.type.names, ["int"])
示例#17
0
    def test_basic_struct_typedef(self):
        res = parse_string("""
            typedef union {
                int some_int;
                struct {
                    char a;
                    char b;
                    char c;
                    char d;
                } some_chars;
            } blah;

            blah some_union;
        """, optimize=True, predefine_types=False)
示例#18
0
    def test_structs_with_params_resetting1(self):
        res = parse_string("""
            typedef struct (int a, int b) {
                char chars1[a];
                char chars2[b];
            } blah;

            blah test(2, 3);
        """, optimize=True)

        res = parse_string("""
            typedef union {
                int some_int;
                struct {
                    char a;
                    char b;
                    char c;
                    char d;
                } some_chars;
            } blah;

            blah some_union;
        """, optimize=True)
示例#19
0
    def test_switch_in_struct(self):
        res = parse_string("""
            struct BLAH {
                int c;

                switch(c) {
                    case 1:
                        int aa;
                    case 2:
                        int bb;
                    default:
                        int cc;
                }
            } blah;
            """, optimize=True)
示例#20
0
    def test_basic_struct_with_args_calling(self):
        res = parse_string("""
            typedef struct (int a) {
                int blah;
            } SPECIAL_STRUCT;

            SPECIAL_STRUCT test(10);

            int blah() {
                return 10;
            }
        """, optimize=True, predefine_types=False)
        decl = res.children()[1][1]
        self.assertTrue(isinstance(decl.type, c_ast.StructCallTypeDecl))
        decl_args = decl.type.args.children()
        self.assertEqual(decl_args[0][1].value, "10")
        self.assertEqual(len(decl_args), 1)
示例#21
0
    def test_nested_structs(self):
        res = parse_string("""
            struct FILE {
                    struct HEADER {
                        char    type[4];
                        int     version;
                        int     numRecords;
                    } header;

                    struct RECORD {
                        int     employeeId;
                        char    name[40];
                        float   salary;
                    } record[ header.numRecords ];

                } file;
        """, optimize=True)
示例#22
0
    def test_struct_with_args_calling_not_func_decl5(self):
        res = parse_string("""
            // variable-width integer used by encoded_value types
            typedef struct (int size, int type) {
                local int s = size + 1;
                local int t = type;
                local int i;
                
                for(i=0; i<s; i++) {
                    ubyte val <comment="Encoded value element">;
                }
            } EncodedValue <read=EncodedValueRead, optimize=false>;

            string EncodedValueRead(EncodedValue &v) {
                local string s = "";

                return s;
            }
        """, predefine_types=True)
示例#23
0
    def test_enum_types(self):
        # note that there have been problems using a built-in
        # type (int/float/etc) vs the typedefd ones, TYPEID vs 
        res = parse_string("""
        enum <ulong> COLORS {
            WHITE = 1
        } var1;

        enum <int> COLORS {
            WHITE = 1
        } var1;

        enum IFD_dirtype {
            IFD_TYPE_EXIF = 1,
            IFD_TYPE_GEOTAG,
            IFD_TYPE_CASIO_QV_R62,
        };

        enum {
            TEST,
            TEST2
        } blah;
        """, optimize=True)
示例#24
0
    def test_single_decl_in_if_else(self):
        res = parse_string("""
            if(1)
                ushort blah;
            else
                ushort blah;

            if(1) {
                ushort blah;
            } else
                ushort blah;

            if(1)
                ushort blah;
            else {
                ushort blah;
            }

            if(1) {
                ushort blah;
            } else {
                ushort blah;
            }
        """, optimize=True)
示例#25
0
 def test_runtime_declared_type(self):
     res = parse_string("""
         void ReadAscString1(StrAscii1 &s) {
             ;
         }
     """, optimize=True, predefine_types=False)
示例#26
0
    def test_implicit_struct_typedef(self):
        res = parse_string("""
            struct Blah { int a; } blah;

            Blah b;
        """, optimize=True)
示例#27
0
 def test_single_decl_in_if(self):
     res = parse_string("""
         if(1)
             ushort blah;
     """, optimize=True)
示例#28
0
 def test_basic_struct(self):
     res = parse_string("""
         struct NAME {
             int blah;
         } name;
     """, optimize=True, predefine_types=False)
示例#29
0
 def test_single_decl_in_do_while_loop(self):
     res = parse_string("""
         do
             ushort blah;
         while(1);
     """, optimize=True)
示例#30
0
 def test_metadata_with_space2(self):
     res = parse_string("""
         int a < key1 = value1 , key2 = value2 >;
     """,
                        optimize=True)
示例#31
0
 def test_startof_unary(self):
     res = parse_string("""
         startof(this);
     """)
示例#32
0
 def test_local_keyword(self):
     res = parse_string("""
         local int a;
         local int b;
     """, optimize=True)
示例#33
0
 def test_exists_unary(self):
     res = parse_string("""
         exists(this);
     """)
示例#34
0
 def test_function_exists_unary(self):
     res = parse_string("""
         function_exists(this);
     """)
示例#35
0
 def test_metadata(self):
     res = parse_string("""
         local int a <hidden=true>;
     """, optimize=True)
示例#36
0
def from_template(template: str) -> st.SearchStrategy[bytes]:
    """Parse the given template into a strategy."""
    ast = py010parser.parse_string(template)
    assert ast is True, "we know we don't actually handle templates yet"
    return st.just(b"")
示例#37
0
 def test_metadata_with_string_value(self):
     res = parse_string("""
         int a <comment="this is a comment", key=val>;
         int a <comment="this is a comment">;
     """, optimize=True)
示例#38
0
    def test_two_part_struct_decl(self):
        res = parse_string("""
            struct StructTest;

            StructTest testing;
        """, optimize=True)
示例#39
0
 def test_preprocessor_with_string(self):
     res = parse_string("""
         //this shouldn't cause any problems
         int a;
     """,
                        optimize=True)
示例#40
0
 def test_single_decl_in_for_loop(self):
     res = parse_string("""
         for(j = 0; j < 10; j++)
             ushort blah;
     """, optimize=True)
示例#41
0
 def test_bitfield_outside_of_struct(self):
     res = parse_string("""
         uint blah1:4;
         uint blah2:8;
         uint blah3:4;
     """, optimize=True, predefine_types=True)
示例#42
0
 def test_typedef(self):
     res = parse_string("""
         typedef unsigned int UINT2;
         UINT2 blah;
     """, optimize=True)
示例#43
0
 def test_basic_struct_with_args2(self):
     res = parse_string("""
         typedef struct (int a) {
             int blah;
         } SPECIAL_STRUCT;
     """, optimize=True, predefine_types=False)
示例#44
0
 def test_parentof_unary(self):
     res = parse_string("""
         parentof(this);
     """)