# HG changeset patch # User Paul Boddie # Date 1309726311 -7200 # Node ID cf329206154e53f92e60057451a3f84a17223f1f # Parent af19598efe329ca43827eaecee8e599ddeffc03c Introduced constant usage tracking. diff -r af19598efe32 -r cf329206154e TO_DO.txt --- a/TO_DO.txt Sun Jul 03 20:11:13 2011 +0200 +++ b/TO_DO.txt Sun Jul 03 22:51:51 2011 +0200 @@ -137,8 +137,6 @@ Object Coverage =============== -Incorporate constants into the coverage, eliminating unused constants. - Support __init__ traversal (and other implicit names) more effectively. Other diff -r af19598efe32 -r cf329206154e micropython/__init__.py --- a/micropython/__init__.py Sun Jul 03 20:11:13 2011 +0200 +++ b/micropython/__init__.py Sun Jul 03 22:51:51 2011 +0200 @@ -376,7 +376,8 @@ # Constant records. self.constant_values = {} - self.constant_list = None # cache for constants + self.constants_used = set() + self.constant_references = {} self.init_predefined_constants() # Attribute usage. @@ -579,6 +580,11 @@ self.attribute_users_visited.add(from_name) + # Get constant references. + + for const in self.constant_references.get(from_name, []): + self.constants_used.add(const) + # The getattr function is a special case: it can potentially reference # any known attribute. Since accessor attributes must be known # constants, the intersection of known constants and attributes is used @@ -709,12 +715,24 @@ # Constant accounting. + def use_constant(self, const, from_name): + + """ + Register the given 'const' as being used in the program from within an + object with the specified 'from_name'. + """ + + if not self.constant_references.has_key(from_name): + self.constant_references[from_name] = set() + + self.constant_references[from_name].add(const) + def init_predefined_constants(self): "Ensure the predefined constants." for name, value in self.predefined_constants.items(): - self.make_constant(value) + self.constants_used.add(self.make_constant(value)) def get_predefined_constant(self, name): @@ -750,10 +768,7 @@ "Return a list of constants." - if self.constant_list is None: - self.constant_list = list(self.constant_values.values()) - - return self.constant_list + return self.constants_used # Import methods. diff -r af19598efe32 -r cf329206154e micropython/inspect.py --- a/micropython/inspect.py Sun Jul 03 20:11:13 2011 +0200 +++ b/micropython/inspect.py Sun Jul 03 22:51:51 2011 +0200 @@ -345,6 +345,13 @@ unit = self.get_namespace() self.importer.use_name(name, unit.full_name(), value) + def use_constant(self, const): + + "Use the given 'const' within the current namespace/unit." + + unit = self.get_namespace() + self.importer.use_constant(const, unit.full_name()) + # Attribute usage methods. # These are convenience methods which refer to the specific namespace's # implementation of these operations. @@ -511,7 +518,9 @@ """ self.use_specific_attribute("__builtins__", self.importer.get_constant_type_name(value)) - return self.importer.make_constant(value) + const = self.importer.make_constant(value) + self.use_constant(const) + return const def _visitFunction(self, node, name):