1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | /*!
|
---|
43 | \example xmlpatterns/xquery/globalVariables
|
---|
44 | \title C++ Source Code Analyzer Example
|
---|
45 |
|
---|
46 | This example uses XQuery and the \c xmlpatterns command line utility to
|
---|
47 | query C++ source code.
|
---|
48 |
|
---|
49 | \tableofcontents
|
---|
50 |
|
---|
51 | \section1 Introduction
|
---|
52 |
|
---|
53 | Suppose we want to analyze C++ source code to find coding standard
|
---|
54 | violations and instances of bad or inefficient patterns. We can do
|
---|
55 | it using the common searching and pattern matching utilities to
|
---|
56 | process the C++ files (e.g., \c{grep}, \c{sed}, and \c{awk}). Now
|
---|
57 | we can also use XQuery with the QtXmlPatterns module.
|
---|
58 |
|
---|
59 | An extension to the \c{g++} open source C++ compiler
|
---|
60 | (\l{http://public.kitware.com/GCC_XML/HTML/Index.html} {GCC-XML})
|
---|
61 | generates an XML description of C++ source code declarations. This
|
---|
62 | XML description can then be processed by QtXmlPatterns using
|
---|
63 | XQueries to navigate the XML description of the C++ source and
|
---|
64 | produce a report. Consider the problem of finding mutable global
|
---|
65 | variables:
|
---|
66 |
|
---|
67 | \section2 Reporting Uses of Mutable Global Variables
|
---|
68 |
|
---|
69 | Suppose we want to introduce threading to a C++ application that
|
---|
70 | was originally written without threading. In a threaded program,
|
---|
71 | mutable global variables can cause bugs, because one thread might
|
---|
72 | change a global variable that other threads are reading, or two
|
---|
73 | threads might try to set the same global variable. So when
|
---|
74 | converting our program to use threading, one of the things we must
|
---|
75 | do is protect the global variables to prevent the bugs described
|
---|
76 | above. How can we use XQuery and
|
---|
77 | \l{http://public.kitware.com/GCC_XML/HTML/Index.html} {GCC-XML} to
|
---|
78 | find the variables that need protecting?
|
---|
79 |
|
---|
80 | \section3 A C++ application
|
---|
81 |
|
---|
82 | Consider the declarations in this hypothetical C++ application:
|
---|
83 |
|
---|
84 | \snippet examples/xmlpatterns/xquery/globalVariables/globals.cpp 0
|
---|
85 |
|
---|
86 | \section3 The XML description of the C++ application
|
---|
87 |
|
---|
88 | Submitting this C++ source to
|
---|
89 | \l{http://public.kitware.com/GCC_XML/HTML/Index.html} {GCC-XML}
|
---|
90 | produces this XML description:
|
---|
91 |
|
---|
92 | \quotefromfile examples/xmlpatterns/xquery/globalVariables/globals.gccxml
|
---|
93 | \printuntil
|
---|
94 |
|
---|
95 | \section3 The XQuery for finding global variables
|
---|
96 |
|
---|
97 | We need an XQuery to find the global variables in the XML
|
---|
98 | description. Here is our XQuery source. We walk through it in
|
---|
99 | \l{XQuery Code Walk-Through}.
|
---|
100 |
|
---|
101 | \quotefromfile examples/xmlpatterns/xquery/globalVariables/reportGlobals.xq
|
---|
102 | \printuntil
|
---|
103 |
|
---|
104 | \section3 Running the XQuery
|
---|
105 |
|
---|
106 | To run the XQuery using the \c xmlpatterns command line utility,
|
---|
107 | enter the following command:
|
---|
108 |
|
---|
109 | \code
|
---|
110 | xmlpatterns reportGlobals.xq -param fileToOpen=globals.gccxml -output globals.html
|
---|
111 | \endcode
|
---|
112 |
|
---|
113 | \section3 The XQuery output
|
---|
114 |
|
---|
115 | The \c xmlpatterns command loads and parses \c globals.gccxml,
|
---|
116 | runs the XQuery \c reportGlobals.xq, and generates this report:
|
---|
117 |
|
---|
118 | \raw HTML
|
---|
119 | <html xmlns="http://www.w3.org/1999/xhtml/" xml:lang="en" lang="en">
|
---|
120 | <head>
|
---|
121 | <title>Global variables report for globals.gccxml</title>
|
---|
122 | </head>
|
---|
123 | <style type="text/css">
|
---|
124 | .details
|
---|
125 | {
|
---|
126 | text-align: left;
|
---|
127 | font-size: 80%;
|
---|
128 | color: blue
|
---|
129 | }
|
---|
130 | .variableName
|
---|
131 | {
|
---|
132 | font-family: courier;
|
---|
133 | color: blue
|
---|
134 | }
|
---|
135 | </style>
|
---|
136 | <body>
|
---|
137 | <p class="details">Start report: 2008-12-16T13:43:49.65Z</p>
|
---|
138 | <p>Global variables with complex types:</p>
|
---|
139 | <ol>
|
---|
140 | <li>
|
---|
141 | <span class="variableName">mutableComplex1</span> in globals.cpp at line 14</li>
|
---|
|
---|