File: XRef.py

package info (click to toggle)
synopsis 0.8.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 10,112 kB
  • ctags: 12,996
  • sloc: cpp: 34,254; ansic: 33,620; python: 10,975; sh: 7,261; xml: 6,369; makefile: 773; asm: 445
file content (173 lines) | stat: -rw-r--r-- 6,261 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#
# Copyright (C) 2000 Stephen Davies
# Copyright (C) 2000 Stefan Seefeld
# All rights reserved.
# Licensed to the public under the terms of the GNU LGPL (>= 2),
# see the file COPYING for details.
#

from Synopsis.Processor import Parameter
from Synopsis import AST, Type, Util
from Synopsis.Formatters.TOC import TOC, Linker
from Synopsis.Formatters.HTML.View import View
from Synopsis.Formatters.HTML.Tags import *
from Synopsis.Formatters.XRef import *

import os

class XRef(View):
   """A module for creating views full of xref infos"""

   xref_file = Parameter('', '')
   link_to_scope = Parameter(True, '')

   def register(self, processor):

      View.register(self, processor)
      self.__filename = None
      self.__title = None
      self.__toc = None
      if self.xref_file:
         processor.xref.load(self.xref_file)

   def get_toc(self, start):
      """Returns the toc for XRef"""

      if self.__toc: return self.__toc
      self.__toc = TOC(None)
      # Add an entry for every xref
      xref = self.processor.xref
      for name in xref.get_all_names():
         view = xref.get_page_for(name)
         file = self.processor.file_layout.special('xref%d'%view)
         file = file + '#' + Util.quote('::'.join(name))
         self.__toc.insert(TOC.Entry(name, file, 'C++', 'xref'))
      return self.__toc

   def filename(self):
      """Returns the current filename,
      which may change over the lifetime of this object"""

      return self.__filename

   def title(self):
      """Returns the current title,
      which may change over the lifetime of this object"""

      return self.__title

   def process(self, start):
      """Creates a view for every bunch of xref infos"""

      page_info = self.processor.xref.get_page_info()
      if not page_info: return
      for i in range(len(page_info)):
         self.__filename = self.processor.file_layout.xref(i)
         self.__title = 'Cross Reference page #%d'%i

         self.start_file()
         self.write(self.processor.navigation_bar(self.filename()))
         self.write(entity('h1', self.__title))
         for name in page_info[i]:
            self.write('<div class="xref-name">')
            self.process_name(name)
            self.write('</div>')
         self.end_file()

   def register_filenames(self, start):
      """Registers each view"""

      page_info = self.processor.xref.get_page_info()
      if not page_info: return
      for i in range(len(page_info)):
         filename = self.processor.file_layout.special('xref%d'%i)
         self.processor.register_filename(filename, self, i)
    
   def process_link(self, file, line, scope):
      """Outputs the info for one link"""

      # Make a link to the highlighted source
      file_link = self.processor.file_layout.file_source(file)
      file_link = rel(self.filename(), file_link) + "#%d"%line
      # Try and make a descriptive
      desc = ''
      type = self.processor.ast.types().get(scope)
      if isinstance(type, Type.Declared):
         desc = ' ' + type.declaration().type()
      # Try and find a link to the scope
      scope_text = '::'.join(scope)
      entry = self.processor.toc[scope]
      if entry:
         scope_text = href(rel(self.filename(), entry.link), escape(scope_text))
      else:
         scope_text = escape(scope_text)
      # Output list element
      self.write('<li><a href="%s">%s:%s</a>: in%s %s</li>\n'%(
         file_link, file, line, desc, scope_text))
    
   def describe_decl(self, decl):
      """Returns a description of the declaration. Detects constructors and
      destructors"""

      name = decl.name()
      if isinstance(decl, AST.Function) and len(name) > 1:
         real = decl.realname()[-1]
         if name[-2] == real:
            return 'Constructor '
         elif real[0] == '~' and name[-2] == real[1:]:
            return 'Destructor '
      return decl.type().capitalize() + ' '

   def process_name(self, name):
      """Outputs the info for a given name"""
      
      target_data = self.processor.xref.get_info(name)
      if not target_data: return

      jname = '::'.join(name)
      self.write(entity('a', '', name=Util.quote(escape(jname))))
      desc = ''
      decl = None
      type = self.processor.ast.types().get(name)
      if isinstance(type, Type.Declared):
         decl = type.declaration()
         desc = self.describe_decl(decl)
      self.write(entity('h2', desc + escape(jname)) + '<ul>\n')
	
      if self.link_to_scope:
         type = self.processor.ast.types().get(name, None)
         if isinstance(type, Type.Declared):
            link = self.processor.file_layout.link(type.declaration())
            self.write('<li>'+href(rel(self.__filename, link), 'Documentation')+'</li>')
      if target_data[0]:
         self.write('<li>Defined at:<ul>\n')
         for file, line, scope in target_data[0]:
            self.process_link(file, line, scope)
         self.write('</ul></li>\n')
      if target_data[1]:
         self.write('<li>Called from:<ul>\n')
         for file, line, scope in target_data[1]:
            self.process_link(file, line, scope)
         self.write('</ul></li>\n')
      if target_data[2]:
         self.write('<li>Referenced from:<ul>\n')
         for file, line, scope in target_data[2]:
            self.process_link(file, line, scope)
         self.write('</ul></li>\n')
      if isinstance(decl, AST.Scope):
         self.write('<li>Declarations:<ul>\n')
         for child in decl.declarations():
            file, line = child.file().filename(), child.line()
            file_link = self.processor.file_layout.file_source(file)
            file_link = rel(self.filename(),file_link) + '#%d'%line
            file_href = '<a href="%s">%s:%s</a>: '%(file_link,file,line)
            cname = child.name()
            entry = self.processor.toc[cname]
            type = self.describe_decl(child)
            if entry:
               link = href(rel(self.filename(), entry.link), escape(Util.ccolonName(cname, name)))
               self.write(entity('li', file_href + type + link))
            else:
               self.write(entity('li', file_href + type + Util.ccolonName(cname, name)))
         self.write('</ul></li>\n')
      self.write('</ul>\n')