From f2590bc4bd87a8431fb1ddd6122f2e456e7dc3d1 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Sun, 25 Nov 2007 21:52:00 +0000 Subject: [PATCH] Warn about unused annotations. --- pybindgen/gccxmlparser.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pybindgen/gccxmlparser.py b/pybindgen/gccxmlparser.py index 8b8c58d..d89ea25 100644 --- a/pybindgen/gccxmlparser.py +++ b/pybindgen/gccxmlparser.py @@ -170,10 +170,19 @@ type_registry = GccXmlTypeRegistry() class AnnotationsScanner(object): def __init__(self): self.files = {} # file name -> list(lines) + self.used_annotations = {} # file name -> list(line_numbers) self._comment_rx = re.compile(r"^\s*//\s+-\*-(.*)-\*-\s*") self._global_annotation_rx = re.compile(r"(\w+)=([^\s;]+)") self._param_annotation_rx = re.compile(r"@(\w+)\(([^;]+)\)") + def _declare_used_annotation(self, file_name, line_number): + try: + l = self.used_annotations[file_name] + except KeyError: + l = [] + self.used_annotations[file_name] = l + l.append(line_number) + def get_annotations(self, file_name, line_number): """ file_name -- absolute file name where the definition is @@ -195,6 +204,7 @@ class AnnotationsScanner(object): if m is None: break line = m.group(1).strip() + self._declare_used_annotation(file_name, line_number + 2) for annotation_str in line.split(';'): annotation_str = annotation_str.strip() m = self._global_annotation_rx.match(annotation_str) @@ -226,6 +236,22 @@ class AnnotationsScanner(object): return True else: raise ValueError("bad boolean value %r" % value) + + def warn_unused_annotations(self): + for file_name, lines in self.files.iteritems(): + try: + used_annotations = self.used_annotations[file_name] + except KeyError: + used_annotations = [] + for line_number, line in enumerate(lines): + m = self._comment_rx.match(line) + if m is None: + continue + #print >> sys.stderr, (line_number+1), used_annotations + if (line_number + 1) not in used_annotations: + warnings.warn_explicit("unused annotation", + Warning, file_name, line_number+1) + annotations_scanner = AnnotationsScanner() @@ -271,6 +297,8 @@ class ModuleParser(object): self._scan_namespace_types(module, module_namespace) self._scan_namespace_functions(module, module_namespace) + annotations_scanner.warn_unused_annotations() + return module def _scan_namespace_types(self, module, module_namespace):