def test_project_reader_file_by_file(self): reader = parser.project_reader_t(self.config) decls = reader.read_files( [parser.file_configuration_t( self.header, self.config.start_with_declarations)], parser.COMPILATION_MODE.FILE_BY_FILE) self.__check_result(decls)
def __init__(self, *args): parser_test_case.parser_test_case_t.__init__(self, *args) self.__files = [ 'core_ns_join_1.hpp', 'core_ns_join_2.hpp', 'core_ns_join_3.hpp', 'core_membership.hpp', 'core_class_hierarchy.hpp', 'core_types.hpp', 'core_diamand_hierarchy_base.hpp', 'core_diamand_hierarchy_derived1.hpp', 'core_diamand_hierarchy_derived2.hpp', 'core_diamand_hierarchy_final_derived.hpp', 'core_overloads_1.hpp', 'core_overloads_2.hpp', 'typedefs_base.hpp'] # for i, f in enumerate(self.__files): # f = parser.create_cached_source_fc( # os.path.join( autoconfig.data_directory, f) # , os.path.join( autoconfig.data_directory, f + '.xml') ) # self.__files[i] = f prj_reader = parser.project_reader_t(self.config) self.decls = prj_reader.read_files( self.__files, compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE)
def profile_project2(): he = r"2003\Vc7\PlatformSDK\Include\windows.h" include_std_header = r"D:\Program Files\Microsoft Visual Studio .NET " + he reader = parser.project_reader_t( parser.xml_generator_configuration_t( xml_generator_path=autoconfig.generator_path)) reader.read_files([include_std_header])
def __test_correctness_impl(self, file_name): prj_reader = parser.project_reader_t(self.config) prj_decls = prj_reader.read_files( [file_name] * 2, compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) src_reader = parser.source_reader_t(self.config) src_decls = src_reader.read_file(file_name) if src_decls != prj_decls: s = src_decls[0] p = prj_decls[0] sr = file( os.path.join(autoconfig.build_directory, file_name + '.sr.txt'), 'w+') pr = file( os.path.join(autoconfig.build_directory, file_name + '.pr.txt'), 'w+') declarations.print_declarations( s, writer=lambda l: sr.write(l + os.linesep)) declarations.print_declarations( p, writer=lambda l: pr.write(l + os.linesep)) sr.close() pr.close() self.fail( "There is a difference between declarations in file %s." % file_name)
def test_project_reader_all_at_once(self): reader = parser.project_reader_t(self.config) decls = reader.read_files( [parser.file_configuration_t( self.header, self.config.start_with_declarations)], parser.COMPILATION_MODE.ALL_AT_ONCE) self.__check_result(decls)
def parse_files(path, files): # Find the location of the xml generator (castxml or gccxml) #generator_path, generator_name = utils.find_xml_generator() #print("GENER " + generator_path) # Configure the xml generator args = { 'include_paths':[path], 'keep_xml': True } if(xml_generator != None): args['xml_generator'] =xml_generator if(xml_generator_path != None): args['xml_generator_path'] =xml_generator_path xml_generator_config = parser.xml_generator_configuration_t(**args) # not sure this actually does anything when compilation_mode is set to "ALL_AT_ONCE" def cache_file(filename): return parser.file_configuration_t( data=filename, content_type=parser.CONTENT_TYPE.CACHED_SOURCE_FILE, cached_source_file=filename.replace(path, "tmp/xml")+".xml") cached_files = [cache_file(f) for f in files] project_reader = parser.project_reader_t(xml_generator_config) decls = project_reader.read_files( cached_files, compilation_mode=parser.COMPILATION_MODE.ALL_AT_ONCE) return declarations.get_global_namespace(decls)
def test_project_reader_all_at_once(self): reader = parser.project_reader_t(self.config) decls = reader.read_files([ parser.file_configuration_t(self.header, self.config.start_with_declarations) ], parser.COMPILATION_MODE.ALL_AT_ONCE) self.__check_result(decls)
def test_project_reader_file_by_file(self): reader = parser.project_reader_t(self.config) decls = reader.read_files([ parser.file_configuration_t(self.header, self.config.start_with_declarations) ], parser.COMPILATION_MODE.FILE_BY_FILE) self.__check_result(decls)
def __test_correctness_impl(self, file_name): prj_reader = parser.project_reader_t(self.config) prj_decls = prj_reader.read_files( [file_name] * 2, compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) src_reader = parser.source_reader_t(self.config) src_decls = src_reader.read_file(file_name) if src_decls != prj_decls: s = src_decls[0] p = prj_decls[0] sr = open( os.path.join( autoconfig.build_directory, file_name + '.sr.txt'), 'w+') pr = open( os.path.join( autoconfig.build_directory, file_name + '.pr.txt'), 'w+') declarations.print_declarations( s, writer=lambda l: sr.write(l + os.linesep)) declarations.print_declarations( p, writer=lambda l: pr.write(l + os.linesep)) sr.close() pr.close() self.fail( "There is a difference between declarations in file %s." % file_name)
def __parse_declarations(self, files, xml_generator_config, compilation_mode, cache, indexing_suite_version): if None is xml_generator_config: xml_generator_config = parser.xml_generator_configuration_t() if None is compilation_mode: compilation_mode = parser.COMPILATION_MODE.FILE_BY_FILE start_time = timer() self.logger.debug('parsing files - started') reader = parser.project_reader_t(xml_generator_config, cache, decl_wrappers.dwfactory_t()) decls = reader.read_files(files, compilation_mode) self.logger.debug('parsing files - done( %f seconds )' % (timer() - start_time)) self.logger.debug('settings declarations defaults - started') global_ns = decls_package.matcher.get_single( decls_package.namespace_matcher_t(name='::'), decls) if indexing_suite_version != 1: for cls in global_ns.classes(allow_empty=True): cls.indexing_suite_version = indexing_suite_version for cls in global_ns.decls( decl_type=decls_package.class_declaration_t, allow_empty=True): cls.indexing_suite_version = indexing_suite_version start_time = timer() self.__apply_decls_defaults(decls) self.logger.debug( 'settings declarations defaults - done( %f seconds )' % (timer() - start_time)) return global_ns
def test(self): src_reader = parser.source_reader_t(self.config) src_decls = src_reader.read_file(self.__fname) xmlfile = src_reader.create_xml_file(self.__fname) print(xmlfile) try: conf_t = parser.file_configuration_t fconfig = conf_t( data=xmlfile, start_with_declarations=None, content_type=conf_t.CONTENT_TYPE.GCCXML_GENERATED_FILE) prj_reader = parser.project_reader_t(self.config) prj_decls = prj_reader.read_files( [fconfig], compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) declarations.dump_declarations( src_decls, os.path.join( autoconfig.build_directory, 'xmlfile_reader.src.txt')) declarations.dump_declarations( prj_decls, os.path.join( autoconfig.build_directory, 'xmlfile_reader.prj.txt')) if src_decls != prj_decls: self.fail( "There is a difference between declarations in file %s." % self.__fname) finally: pass # utils.remove_file_no_raise( xmlfile, self.config )
def profile_project(): include_std_header = os.path.join(autoconfig.data_directory, 'include_std.hpp') reader = parser.project_reader_t( parser.xml_generator_configuration_t( xml_generator_path=autoconfig.generator_path)) reader.read_files([include_std_header])
def __parse_declarations( self, files, gccxml_config, compilation_mode, cache, indexing_suite_version ): if None is gccxml_config: gccxml_config = parser.config_t() if None is compilation_mode: compilation_mode = parser.COMPILATION_MODE.FILE_BY_FILE start_time = time.clock() self.logger.debug( 'parsing files - started' ) reader = parser.project_reader_t( gccxml_config, cache, decl_wrappers.dwfactory_t() ) decls = reader.read_files( files, compilation_mode ) self.logger.debug( 'parsing files - done( %f seconds )' % ( time.clock() - start_time ) ) self.logger.debug( 'settings declarations defaults - started' ) global_ns = decls_package.matcher.get_single( decls_package.namespace_matcher_t( name='::' ) , decls ) if indexing_suite_version != 1: for cls in global_ns.classes(): cls.indexing_suite_version = indexing_suite_version try: for cls in global_ns.decls(decl_type=decls_package.class_declaration_t): cls.indexing_suite_version = indexing_suite_version except RuntimeError: pass start_time = time.clock() self.__apply_decls_defaults(decls) self.logger.debug( 'settings declarations defaults - done( %f seconds )' % ( time.clock() - start_time ) ) return global_ns
def test(self): db = pypp_utils.exposed_decls_db_t() config = parser.config_t( gccxml_path=autoconfig.gccxml.executable, compiler=pygccxml.utils.native_compiler.get_gccxml_compiler()) reader = parser.project_reader_t(config, None, decl_wrappers.dwfactory_t()) decls = reader.read_files([parser.create_text_fc(self.CODE)]) global_ns = declarations.get_global_namespace(decls) ns = global_ns.namespace('ns') ns_skip = global_ns.namespace('ns_skip') global_ns.exclude() ns.include() db.register_decls(global_ns, []) for x in ns.decls(recursive=True): self.failUnless(db.is_exposed(x) == True) for x in ns_skip.decls(recursive=True): self.failUnless(db.is_exposed(x) == False) db.save(os.path.join(autoconfig.build_dir, 'exposed.db.pypp')) db2 = pypp_utils.exposed_decls_db_t() db2.load(os.path.join(autoconfig.build_dir, 'exposed.db.pypp')) for x in ns.decls(recursive=True): self.failUnless(db.is_exposed(x) == True) ns_skip = global_ns.namespace('ns_skip') for x in ns_skip.decls(recursive=True): self.failUnless(db.is_exposed(x) == False)
def test(self): src_reader = parser.source_reader_t(self.config) src_decls = src_reader.read_file(self.__fname) xmlfile = src_reader.create_xml_file(self.__fname) print(xmlfile) try: conf_t = parser.file_configuration_t fconfig = conf_t( data=xmlfile, start_with_declarations=None, content_type=conf_t.CONTENT_TYPE.GCCXML_GENERATED_FILE) prj_reader = parser.project_reader_t(self.config) prj_decls = prj_reader.read_files( [fconfig], compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) declarations.dump_declarations( src_decls, os.path.join( autoconfig.build_directory, 'xmlfile_reader.src.txt')) declarations.dump_declarations( prj_decls, os.path.join( autoconfig.build_directory, 'xmlfile_reader.prj.txt')) if src_decls != prj_decls: self.fail( "There is a difference between declarations in file %s." % self.__fname) finally: pass # utils.remove_file_no_raise( xmlfile )
def test(self): db = pypp_utils.exposed_decls_db_t() config = parser.config_t( gccxml_path=autoconfig.gccxml.executable ) reader = parser.project_reader_t( config, None, decl_wrappers.dwfactory_t() ) decls = reader.read_files( [parser.create_text_fc(self.CODE)] ) global_ns = declarations.get_global_namespace( decls ) ns = global_ns.namespace( 'ns' ) ns_skip = global_ns.namespace( 'ns_skip' ) global_ns.exclude() ns.include() db.register_decls( global_ns, [] ) for x in ns.decls(recursive=True): self.failUnless( db.is_exposed( x ) == True ) for x in ns_skip.decls(recursive=True): self.failUnless( db.is_exposed( x ) == False ) db.save( os.path.join( autoconfig.build_dir, 'exposed.db.pypp' ) ) db2 = pypp_utils.exposed_decls_db_t() db2.load( os.path.join( autoconfig.build_dir, 'exposed.db.pypp' ) ) for x in ns.decls(recursive=True): self.failUnless( db.is_exposed( x ) == True ) ns_skip = global_ns.namespace( 'ns_skip' ) for x in ns_skip.decls(recursive=True): self.failUnless( db.is_exposed( x ) == False )
def parse_big_file(): path = os.path.join(autoconfig.data_directory, 'big.xml') reader = parser.project_reader_t( parser.xml_generator_configuration_t( xml_generator_path=autoconfig.xml_generator_path)) reader.read_files([parser.create_gccxml_fc(path)]) reader.read_files([parser.create_gccxml_fc(path)]) reader.read_files([parser.create_gccxml_fc(path)])
def profile_project(): include_std_header = os.path.join( autoconfig.data_directory, 'include_std.hpp') reader = parser.project_reader_t( parser.xml_generator_configuration_t( xml_generator_path=autoconfig.generator_path)) reader.read_files([include_std_header])
def test_on_big_file(file_name, count): file_name = os.path.join(autoconfig.data_directory, file_name) for i in range(count): reader = parser.project_reader_t( parser.xml_generator_configuration_t( xml_generator_path=autoconfig.generator_path)) decls = reader.read_files([parser.create_gccxml_fc(file_name)]) global_ns = declarations.get_global_namespace(decls) global_ns.init_optimizer()
def test(self): prj_reader = parser.project_reader_t(self.config) decls = prj_reader.read_files( self.__files, compilation_mode=parser.COMPILATION_MODE.ALL_AT_ONCE) files = declarations.declaration_files(decls) result = set() for fn in files: result.add(os.path.split(fn)[1]) self.assertTrue(set(self.__files).issubset(result))
def test(self): fconfig = parser.file_configuration_t( data="int i;", start_with_declarations=None, content_type=parser.file_configuration_t.CONTENT_TYPE.TEXT ) prj_reader = parser.project_reader_t(self.config) decls = prj_reader.read_files([fconfig], compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) var_i = declarations.find_declaration(decls, type=declarations.variable_t, name="i") self.failUnless(var_i, "Variable i has not been found.")
def test(self): prj_reader = parser.project_reader_t(self.config) decls = prj_reader.read_files( self.__files, compilation_mode=parser.COMPILATION_MODE.ALL_AT_ONCE) files = declarations.declaration_files(decls) result = set() for fn in files: result.add(os.path.split(fn)[1]) self.failUnless(set(self.__files).issubset(result))
def show_xml( self, file_configuration, compiler_config ): try: reader = parser.project_reader_t( config=compiler_config ) content = reader.read_xml( file_configuration ) return content, '' #return xml.sax.saxutils.escape( content ), '' except Exception, error: user_msg = [ 'Error occured during code generation process!' ] user_msg.append( 'Error:' ) user_msg.append( str( error ) ) return '', '\n'.join( user_msg )
def test_project_on_include_std_dot_hpp(): include_std_header = os.path.join(autoconfig.data_directory, 'include_std.hpp') clock_prev = time.perf_counter() dcache = parser.file_cache_t(dcache_file_name) reader = parser.project_reader_t( parser.xml_generator_configuration_t( xml_generator_path=autoconfig.generator_path), dcache) reader.read_files([include_std_header]) dcache.flush() clock_now = time.perf_counter() print(('without cache: %f seconds' % (clock_now - clock_prev))) clock_prev = time.perf_counter() dcache = parser.file_cache_t(dcache_file_name) reader = parser.project_reader_t( parser.xml_generator_configuration_t( xml_generator_path=autoconfig.generator_path), dcache) reader.read_files([include_std_header]) clock_now = time.perf_counter() print(('with cache : %f seconds' % (clock_now - clock_prev)))
def test(self): fconfig = parser.file_configuration_t( data='int i;', start_with_declarations=None, content_type=parser.file_configuration_t.CONTENT_TYPE.TEXT) prj_reader = parser.project_reader_t(self.config) decls = prj_reader.read_files( [fconfig], compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) var_i = declarations.find_declaration( decls, decl_type=declarations.variable_t, name='i') self.assertTrue(var_i, "Variable i has not been found.")
def show_declarations( self, file_configuration, compiler_config ): try: reader = parser.project_reader_t( config=compiler_config ) decls = reader.read_files( [file_configuration] ) global_ns = declarations.get_global_namespace( decls ) tmp = [] declarations.print_declarations( decls, verbose=False, writer=lambda x: tmp.append( x.rstrip() ) ) return os.linesep.join( tmp ), '' except Exception, error: user_msg = [ 'Error occured during code generation process!' ] user_msg.append( 'Error:' ) user_msg.append( str( error ) ) return '', '\n'.join( user_msg )
def test(self): prj_reader = parser.project_reader_t( self.config ) prj_decls = prj_reader.read_files( self.__files , compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE ) src_reader = parser.source_reader_t( self.config ) src_decls = src_reader.read_file( 'separate_compilation/all.h' ) declarations.dump_declarations( src_decls , os.path.join( autoconfig.build_directory, 'separate_compilation.sr.txt' ) ) declarations.dump_declarations( prj_decls , os.path.join( autoconfig.build_directory, 'separate_compilation.pr.txt' ) ) self.failUnless( src_decls == prj_decls, "There is a difference between declarations" )
def __parse_declarations( self, files, gccxml_config, compilation_mode=None, cache=None ): if None is gccxml_config: gccxml_config = parser.config_t() if None is compilation_mode: compilation_mode = parser.COMPILATION_MODE.FILE_BY_FILE start_time = time.clock() self.logger.debug( 'parsing files - started' ) reader = parser.project_reader_t( gccxml_config, cache, decl_wrappers.dwfactory_t() ) decls = reader.read_files( files, compilation_mode ) self.logger.debug( 'parsing files - done( %f seconds )' % ( time.clock() - start_time ) ) return decls_package.matcher.get_single( decls_package.namespace_matcher_t( name='::' ) , decls )
def test_project_on_include_std_dot_hpp(): include_std_header = os.path.join( autoconfig.data_directory, 'include_std.hpp') clock_prev = time.clock() dcache = parser.file_cache_t(dcache_file_name) reader = parser.project_reader_t( parser.xml_generator_configuration_t( xml_generator_path=autoconfig.generator_path), dcache) reader.read_files([include_std_header]) dcache.flush() clock_now = time.clock() print('without cache: %f seconds' % (clock_now - clock_prev)) clock_prev = time.clock() dcache = parser.file_cache_t(dcache_file_name) reader = parser.project_reader_t( parser.xml_generator_configuration_t( xml_generator_path=autoconfig.generator_path), dcache) reader.read_files([include_std_header]) clock_now = time.clock() print('with cache : %f seconds' % (clock_now - clock_prev))
def test(self): fconfig = parser.file_configuration_t( data=self.__fname , content_type=parser.CONTENT_TYPE.CACHED_SOURCE_FILE ) try: prj_reader = parser.project_reader_t( self.config ) prj_decls = prj_reader.read_files( [fconfig] , compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE ) self.failUnless( os.path.exists( fconfig.cached_source_file ) ) mtime1 = os.stat(fconfig.cached_source_file)[stat.ST_MTIME] prj_decls = prj_reader.read_files( [fconfig] , compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE ) mtime2 = os.stat(fconfig.cached_source_file)[stat.ST_MTIME] self.failUnless( mtime1 == mtime2 ) finally: utils.remove_file_no_raise( fconfig.cached_source_file )
def test(self): fconfig = parser.file_configuration_t( data=self.__fname, content_type=parser.CONTENT_TYPE.CACHED_SOURCE_FILE) try: prj_reader = parser.project_reader_t(self.config) prj_reader.read_files( [fconfig], compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) self.assertTrue(os.path.exists(fconfig.cached_source_file)) mtime1 = os.stat(fconfig.cached_source_file)[stat.ST_MTIME] prj_reader.read_files( [fconfig], compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) mtime2 = os.stat(fconfig.cached_source_file)[stat.ST_MTIME] self.assertTrue(mtime1 == mtime2) finally: utils.remove_file_no_raise(fconfig.cached_source_file, self.config)
def test(self): src_reader = parser.source_reader_t( self.config ) src_decls = src_reader.read_file( self.__fname ) xmlfile = src_reader.create_xml_file( self.__fname ) try: fconfig = parser.file_configuration_t( data=xmlfile , start_with_declarations=None , content_type=parser.file_configuration_t.CONTENT_TYPE.GCCXML_GENERATED_FILE ) prj_reader = parser.project_reader_t( self.config ) prj_decls = prj_reader.read_files( [fconfig] , compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE ) self.failUnless( src_decls == prj_decls , "There is a difference between declarations in file %s." % self.__fname ) finally: utils.remove_file_no_raise( xmlfile )
def __init__(self, *args): parser_test_case.parser_test_case_t.__init__(self, *args) self.__files = [ 'core_ns_join_1.hpp' , 'core_ns_join_2.hpp' , 'core_ns_join_3.hpp' , 'core_membership.hpp' , 'core_class_hierarchy.hpp' , 'core_types.hpp' , 'core_diamand_hierarchy_base.hpp' , 'core_diamand_hierarchy_derived1.hpp' , 'core_diamand_hierarchy_derived2.hpp' , 'core_diamand_hierarchy_final_derived.hpp' , 'core_overloads_1.hpp' , 'core_overloads_2.hpp' , 'typedefs_base.hpp' ] prj_reader = parser.project_reader_t( self.config ) self.decls = prj_reader.read_files( self.__files , compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE )
def __init__(self, *args): parser_test_case.parser_test_case_t.__init__(self, *args) self.__files = [ 'core_ns_join_1.hpp', 'core_ns_join_2.hpp', 'core_ns_join_3.hpp', 'core_membership.hpp', 'core_class_hierarchy.hpp', 'core_types.hpp', 'core_diamand_hierarchy_base.hpp', 'core_diamand_hierarchy_derived1.hpp', 'core_diamand_hierarchy_derived2.hpp', 'core_diamand_hierarchy_final_derived.hpp', 'core_overloads_1.hpp', 'core_overloads_2.hpp', 'typedefs_base.hpp' ] #~ for i, f in enumerate(self.__files): #~ f = parser.create_cached_source_fc( os.path.join( autoconfig.data_directory, f) #~ , os.path.join( autoconfig.data_directory, f + '.xml') ) #~ self.__files[i] = f prj_reader = parser.project_reader_t(self.config) self.decls = prj_reader.read_files( self.__files, compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE)
def test(self): prj_reader = parser.project_reader_t(self.config) prj_decls = prj_reader.read_files( self.__files, compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) src_reader = parser.source_reader_t(self.config) src_decls = src_reader.read_file('separate_compilation/all.h') declarations.dump_declarations( src_decls, os.path.join(autoconfig.build_directory, 'separate_compilation.sr.txt')) declarations.dump_declarations( prj_decls, os.path.join(autoconfig.build_directory, 'separate_compilation.pr.txt')) self.assertTrue(src_decls == prj_decls, "There is a difference between declarations")
warnings.simplefilter("error", Warning) # Find out the file location within the sources tree this_module_dir_path = os.path.abspath( os.path.dirname(sys.modules[__name__].__file__)) # Find out the c++ parser generator_path, generator_name = utils.find_xml_generator() # Configure the xml generator xml_generator_config = parser.xml_generator_configuration_t( xml_generator_path=generator_path, xml_generator=generator_name) # The c++ file we want to parse filename = "example.hpp" filename = this_module_dir_path + "/" + filename file_config = parser.file_configuration_t( data=filename, content_type=parser.CONTENT_TYPE.CACHED_SOURCE_FILE) project_reader = parser.project_reader_t(xml_generator_config) decls = project_reader.read_files( [file_config], compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) global_namespace = declarations.get_global_namespace(decls) value = global_namespace.namespace("ns") print("My name is: " + value.name)
def parse(self): """Parse the header files and setup the known declarations. Currently this method can only be called once. This method can be called anytime after initialization and all Template() calls have been made. :rtype: Returns the root of the declaration tree @rtype: L{IDecl<declwrapper.IDecl>} @postcondition: This class can act as a wrapper for namespace("::") and all declarations are set to be ignored. """ if self.mHeaderFiles == []: raise ValueError, "No header files specified" if self.mVerbose: print "Parsing headers: ", self.mHeaderFiles # Record the time when parsing started... self.mStartTime = time.time() # Create and initialize the config object parser_cfg = parser.config_t(self.mGccXmlPath, self.mWorkingDir, self.mIncludePaths, define_symbols=self.mDefines, undefine_symbols=self.mUndefines, start_with_declarations=None) full_header_list = self.mHeaderFiles[:] # Handle template instantiation as needed temp_file, temp_filename = (None, None) template_instantiation_text = self.buildTemplateFileContents() if None != template_instantiation_text: temp_filename = pygccxml.utils.create_temp_file_name(suffix=".h") temp_file = file(temp_filename, 'w') temp_file.write(template_instantiation_text) temp_file.close() if self.mVerbose: print " creating template instantiation file: ", temp_filename full_header_list.append(temp_filename) # Create the cache object... if self.mCacheDir != None: if self.mVerbose: print "Using directory cache in '%s'" % self.mCacheDir cache = parser.directory_cache_t(self.mCacheDir) elif self.mCacheFile != None: if self.mVerbose: print "Using file cache '%s'" % self.mCacheFile cache = parser.file_cache_t(self.mCacheFile) else: if self.mVerbose: print "No cache in use" cache = None # Create the parser object... the_parser = parser.project_reader_t( config=parser_cfg, cache=cache, decl_factory=decl_wrappers.dwfactory_t()) # ...and parse the headers parsed_decls = the_parser.read_files( full_header_list, parser.project_reader.COMPILATION_MODE.FILE_BY_FILE) assert len(parsed_decls) == 1 # assume that we get root of full tree self.mDeclRoot = parsed_decls[0] # Parse the files and add to decl root # - then traverse tree setting everything to ignore self.mDeclRootWrapper = DeclWrapper(self.mDeclRoot) # Set the module builder instance (this is done here and not in the # constructor so that Allen's DeclWrapper object still work as well) self.mDeclRootWrapper.modulebuilder = self self.mDeclRootWrapper.ignore() # Cleanup if temp_filename: pygccxml.utils.remove_file_no_raise(temp_filename) typedef_decls = declarations.make_flatten(parsed_decls) typedef_decls = decls = filter( lambda x: (isinstance(x, declarations.typedef_t) and not x.name.startswith( '__') and x.location.file_name != "<internal>"), typedef_decls) self.mTypeDefMap = {} for d in typedef_decls: type_def_name = d.name full_name = declarations.full_name(d) if full_name.startswith("::"): # Remove the base namespace full_name = full_name[2:] real_type_name = d.type.decl_string if real_type_name.startswith("::"): # Remove base namespace real_type_name = real_type_name[2:] self.mTypeDefMap[full_name] = real_type_name self.mParseEndTime = time.time() if self.mVerbose: print "Completed parsing in %s." % self._time2str( self.mParseEndTime - self.mStartTime) return self.mDeclRootWrapper
import os import sys import warnings warnings.simplefilter("error", Warning) # Find out the file location within the sources tree this_module_dir_path = os.path.abspath( os.path.dirname(sys.modules[__name__].__file__)) # Find out the c++ parser generator_path, generator_name = utils.find_xml_generator() # Configure the xml generator xml_generator_config = parser.xml_generator_configuration_t( xml_generator_path=generator_path, xml_generator=generator_name) # The c++ file we want to parse filename = "example.hpp" filename = this_module_dir_path + "/" + filename file_config = parser.file_configuration_t( data=filename, content_type=parser.CONTENT_TYPE.CACHED_SOURCE_FILE) project_reader = parser.project_reader_t(xml_generator_config) decls = project_reader.read_files( [file_config], compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE) global_namespace = declarations.get_global_namespace(decls) value = global_namespace.namespace("ns") print("My name is: " + value.name)
def parse(self): """Parse the header files and setup the known declarations. Currently this method can only be called once. This method can be called anytime after initialization and all Template() calls have been made. @returns: Returns the root of the declaration tree @rtype: L{IDecl<declwrapper.IDecl>} @postcondition: This class can act as a wrapper for namespace("::") and all declarations are set to be ignored. """ if self.mHeaderFiles==[]: raise ValueError, "No header files specified" if self.mVerbose: print "Parsing headers: ", self.mHeaderFiles # Record the time when parsing started... self.mStartTime = time.time() # Create and initialize the config object parser_cfg = parser.config_t(self.mGccXmlPath, self.mWorkingDir, self.mIncludePaths, define_symbols=self.mDefines, undefine_symbols=self.mUndefines, start_with_declarations=None) full_header_list = self.mHeaderFiles[:] # Handle template instantiation as needed temp_file, temp_filename = (None,None) template_instantiation_text = self.buildTemplateFileContents() if None != template_instantiation_text: temp_filename = pygccxml.utils.create_temp_file_name(suffix=".h") temp_file = file(temp_filename, 'w') temp_file.write(template_instantiation_text) temp_file.close() if self.mVerbose: print " creating template instantiation file: ", temp_filename full_header_list.append(temp_filename) # Create the cache object... if self.mCacheDir!=None: if self.mVerbose: print "Using directory cache in '%s'"%self.mCacheDir cache = parser.directory_cache_t(self.mCacheDir) elif self.mCacheFile!=None: if self.mVerbose: print "Using file cache '%s'"%self.mCacheFile cache = parser.file_cache_t(self.mCacheFile) else: if self.mVerbose: print "No cache in use" cache = None # Create the parser object... the_parser = parser.project_reader_t(config=parser_cfg, cache=cache, decl_factory=decl_wrappers.dwfactory_t()) # ...and parse the headers parsed_decls = the_parser.read_files(full_header_list, parser.project_reader.COMPILATION_MODE.FILE_BY_FILE) assert len(parsed_decls) == 1 # assume that we get root of full tree self.mDeclRoot = parsed_decls[0] # Parse the files and add to decl root # - then traverse tree setting everything to ignore self.mDeclRootWrapper = DeclWrapper(self.mDeclRoot) # Set the module builder instance (this is done here and not in the # constructor so that Allen's DeclWrapper object still work as well) self.mDeclRootWrapper.modulebuilder = self self.mDeclRootWrapper.ignore() # Cleanup if temp_filename: pygccxml.utils.remove_file_no_raise( temp_filename ) typedef_decls = declarations.make_flatten(parsed_decls) typedef_decls = decls = filter( lambda x: (isinstance( x, declarations.typedef_t ) and not x.name.startswith('__') and x.location.file_name != "<internal>") , typedef_decls ) self.mTypeDefMap = {} for d in typedef_decls: type_def_name = d.name full_name = declarations.full_name(d) if full_name.startswith("::"): # Remove the base namespace full_name = full_name[2:] real_type_name = d.type.decl_string if real_type_name.startswith("::"): # Remove base namespace real_type_name = real_type_name[2:] self.mTypeDefMap[full_name] = real_type_name self.mParseEndTime = time.time() if self.mVerbose: print "Completed parsing in %s."%self._time2str(self.mParseEndTime-self.mStartTime) return self.mDeclRootWrapper