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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
/*!
\example xmlpatterns/xquery/globalVariables
\title C++ Source Code Analyzer Example
This example uses XQuery and the \c xmlpatterns command line utility to
query C++ source code.
\tableofcontents
\section1 Introduction
Suppose we want to analyze C++ source code to find coding standard
violations and instances of bad or inefficient patterns. We can do
it using the common searching and pattern matching utilities to
process the C++ files (e.g., \c{grep}, \c{sed}, and \c{awk}). Now
we can also use XQuery with the QtXmlPatterns module.
An extension to the \c{g++} open source C++ compiler
(\l{http://public.kitware.com/GCC_XML/HTML/Index.html} {GCC-XML})
generates an XML description of C++ source code declarations. This
XML description can then be processed by QtXmlPatterns using
XQueries to navigate the XML description of the C++ source and
produce a report. Consider the problem of finding mutable global
variables:
\section2 Reporting Uses of Mutable Global Variables
Suppose we want to introduce threading to a C++ application that
was originally written without threading. In a threaded program,
mutable global variables can cause bugs, because one thread might
change a global variable that other threads are reading, or two
threads might try to set the same global variable. So when
converting our program to use threading, one of the things we must
do is protect the global variables to prevent the bugs described
above. How can we use XQuery and
\l{http://public.kitware.com/GCC_XML/HTML/Index.html} {GCC-XML} to
find the variables that need protecting?
\section3 A C++ application
Consider the declarations in this hypothetical C++ application:
\snippet examples/xmlpatterns/xquery/globalVariables/globals.cpp 0
\section3 The XML description of the C++ application
Submitting this C++ source to
\l{http://public.kitware.com/GCC_XML/HTML/Index.html} {GCC-XML}
produces this XML description:
\quotefromfile examples/xmlpatterns/xquery/globalVariables/globals.gccxml
\printuntil
\section3 The XQuery for finding global variables
We need an XQuery to find the global variables in the XML
description. Here is our XQuery source. We walk through it in
\l{XQuery Code Walk-Through}.
\quotefromfile examples/xmlpatterns/xquery/globalVariables/reportGlobals.xq
\printuntil
\section3 Running the XQuery
To run the XQuery using the \c xmlpatterns command line utility,
enter the following command:
\code
xmlpatterns reportGlobals.xq -param fileToOpen=globals.gccxml -output globals.html
\endcode
\section3 The XQuery output
The \c xmlpatterns command loads and parses \c globals.gccxml,
runs the XQuery \c reportGlobals.xq, and generates this report:
\raw HTML
<html xmlns="http://www.w3.org/1999/xhtml/" xml:lang="en" lang="en">
<head>
<title>Global variables report for globals.gccxml</title>
</head>
<style type="text/css">
.details
{
text-align: left;
font-size: 80%;
color: blue
}
.variableName
{
font-family: courier;
color: blue
}
</style>
<body>
<p class="details">Start report: 2008-12-16T13:43:49.65Z</p>
<p>Global variables with complex types:</p>
<ol>
<li>
<span class="variableName">mutableComplex1</span> in globals.cpp at line 14</li>
<li>
<span class="variableName">mutableComplex2</span> in globals.cpp at line 15</li>
<li>
<span class="variableName">constComplex1</span> in globals.cpp at line 16</li>
<li>
<span class="variableName">constComplex2</span> in globals.cpp at line 17</li>
</ol>
<p>Mutable global variables with primitives types:</p>
<ol>
<li>
<span class="variableName">mutablePrimitive1</span> in globals.cpp at line 1</li>
<li>
<span class="variableName">mutablePrimitive2</span> in globals.cpp at line 2</li>
</ol>
<p class="details">End report: 2008-12-16T13:43:49.65Z</p>
</body>
</html>
\endraw
\section1 XQuery Code Walk-Through
The XQuery source is in
\c{examples/xmlpatterns/xquery/globalVariables/reportGlobals.xq}
It begins with two variable declarations that begin the XQuery:
\quotefromfile examples/xmlpatterns/xquery/globalVariables/reportGlobals.xq
\skipto declare variable
\printto (:
The first variable, \c{$fileToOpen}, appears in the \c xmlpatterns
command shown earlier, as \c{-param fileToOpen=globals.gccxml}.
This binds the variable name to the file name. This variable is
then used in the declaration of the second variable, \c{$inDoc},
as the parameter to the
\l{http://www.w3.org/TR/xpath-functions/#func-doc} {doc()}
function. The \c{doc()} function returns the document node of
\c{globals.gccxml}, which is assigned to \c{$inDoc} to be used
later in the XQuery as the root node of our searches for global
variables.
Next skip to the end of the XQuery, where the \c{<html>} element
is constructed. The \c{<html>} will contain a \c{<head>} element
to specify a heading for the html page, followed by some style
instructions for displaying the text, and then the \c{<body>}
element.
\quotefromfile examples/xmlpatterns/xquery/globalVariables/reportGlobals.xq
\skipto <html xmlns
\printuntil
The \c{<body>} element contains a call to the \c{local:report()}
function, which is where the query does the "heavy lifting." Note
the two \c{return} clauses separated by the \e {comma operator}
about halfway down:
\quotefromfile examples/xmlpatterns/xquery/globalVariables/reportGlobals.xq
\skipto declare function local:report()
\printuntil };
The \c{return} clauses are like two separate queries. The comma
operator separating them means that both \c{return} clauses are
executed and both return their results, or, rather, both output
their results. The first \c{return} clause searches for global
variables with complex types, and the second searches for mutable
global variables with primitive types.
Here is the html generated for the \c{<body>} element. Compare
it with the XQuery code above:
\quotefromfile examples/xmlpatterns/xquery/globalVariables/globals.html
\skipto <body>
\printuntil </body>
The XQuery declares three more local functions that are called in
turn by the \c{local:report()} function. \c{isComplexType()}
returns true if the variable has a complex type. The variable can
be mutable or const.
\quotefromfile examples/xmlpatterns/xquery/globalVariables/reportGlobals.xq
\skipto declare function local:isComplexType
\printuntil };
\c{isPrimitive()} returns true if the variable has a primitive
type. The variable must be mutable.
\quotefromfile examples/xmlpatterns/xquery/globalVariables/reportGlobals.xq
\skipto declare function local:isPrimitive
\printuntil };
\c{location()} returns a text constructed from the variable's file
and line number attributes.
\quotefromfile examples/xmlpatterns/xquery/globalVariables/reportGlobals.xq
\skipto declare function local:location
\printuntil };
*/
|