|
1 (: |
|
2 This query opens a C++ GCC-XML output file, and outputs a report describing the use |
|
3 of globals variables. |
|
4 |
|
5 Run it, by invoking, for instance: |
|
6 |
|
7 patternist reportGlobal.xq fileToOpen=globals.gccxml > globals.html |
|
8 |
|
9 "fileToOpen=globals.gccxml" binds the string "globals.gccxml" to the variable |
|
10 "fileToOpen." It identifies the GCC-XML file to open. |
|
11 |
|
12 :) |
|
13 declare variable $inDoc as document-node() := doc($fileToOpen); |
|
14 |
|
15 (: Determines whether the type by ID @p typeId is a complex type such as QString. :) |
|
16 declare function local:isComplexType($typeID as xs:string) as xs:boolean |
|
17 { |
|
18 (: We're being a bit crude here and only checks whether it's a class. We |
|
19 actually should check whether it has non-synthesized, |
|
20 constructors, I believe. :) |
|
21 |
|
22 exists($inDoc/GCC_XML/Class[@id = $typeID]) |
|
23 or |
|
24 (: We also want const-qualified variables. :) |
|
25 exists($inDoc/GCC_XML/Class[@id = $inDoc/GCC_XML/CvQualifiedType[@id = $typeID]/@type]) |
|
26 }; |
|
27 |
|
28 declare function local:isPrimitive($typeId as xs:string) as xs:boolean |
|
29 { |
|
30 exists($inDoc/GCC_XML/FundamentalType[@id = $typeId]) |
|
31 }; |
|
32 |
|
33 (: Returns a string for human consumption that describes |
|
34 the location of @p block. :) |
|
35 declare function local:location($block as element()) as xs:string |
|
36 { |
|
37 concat($inDoc/GCC_XML/File[@id = $block/@file]/@name, " at line ", $block/@line) |
|
38 }; |
|
39 |
|
40 declare function local:report() as element()+ |
|
41 { |
|
42 let $complexVariables as element(Variable)* := $inDoc/GCC_XML/Variable[local:isComplexType(@type)] |
|
43 return if(exists($complexVariables)) (: Is the length larger than zero? :) |
|
44 |
|
45 then (<p xmlns="http://www.w3.org/1999/xhtml/">The following global, complex variables were found:</p>, |
|
46 <ol xmlns="http://www.w3.org/1999/xhtml/"> |
|
47 { |
|
48 (: For each Variable in $complexVariables... :) |
|
49 $complexVariables/<li><span class="variableName">{string(@name)}</span> in {local:location(.)}</li> |
|
50 } |
|
51 </ol>) |
|
52 |
|
53 else <p xmlns="http://www.w3.org/1999/xhtml/">No global variables that are of complex types were found.</p> |
|
54 |
|
55 , |
|
56 |
|
57 (: List primitive, mutable types. :) |
|
58 let $primitiveVariables as element(Variable)+ := $inDoc/GCC_XML/Variable[local:isPrimitive(@type)] |
|
59 return if(exists($primitiveVariables)) |
|
60 |
|
61 then (<p xmlns="http://www.w3.org/1999/xhtml/">The following mutable primitives were found:</p>, |
|
62 <ol xmlns="http://www.w3.org/1999/xhtml/"> |
|
63 { |
|
64 (: For each Variable in $complexVariables... :) |
|
65 $primitiveVariables/<li><span class="variableName">{string(@name)}</span> in {local:location(.)}</li> |
|
66 } |
|
67 </ol>) |
|
68 |
|
69 else <p xmlns="http://www.w3.org/1999/xhtml/">No global variables that are of complex types were found.</p> |
|
70 }; |
|
71 |
|
72 <html xmlns="http://www.w3.org/1999/xhtml/" xml:lang="en" lang="en"> |
|
73 <head> |
|
74 <title>Global variables report for {$fileToOpen}</title> |
|
75 </head> |
|
76 <style type="text/css"> |
|
77 .details |
|
78 {{ |
|
79 text-align: center; |
|
80 font-size: 80%; |
|
81 color: gray |
|
82 }} |
|
83 .variableName |
|
84 {{ |
|
85 font-family: courier |
|
86 }} |
|
87 </style> |
|
88 |
|
89 <body> |
|
90 { |
|
91 (: We don't want simple types that are const, but all other types. |
|
92 One could frown upon const integers and say enums should be used instead, but |
|
93 let's be gentle. :) |
|
94 |
|
95 local:report() |
|
96 } |
|
97 |
|
98 <p class="details">This report was generated on</p> |
|
99 </body> |
|
100 |
|
101 </html> |