doc/src/examples/globalVariables.qdoc
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the documentation of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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>
       
   142             <li>
       
   143                 <span class="variableName">mutableComplex2</span> in globals.cpp at line 15</li>
       
   144             <li>
       
   145                 <span class="variableName">constComplex1</span> in globals.cpp at line 16</li>
       
   146             <li>
       
   147                 <span class="variableName">constComplex2</span> in globals.cpp at line 17</li>
       
   148         </ol>
       
   149         <p>Mutable global variables with primitives types:</p>
       
   150         <ol>
       
   151             <li>
       
   152                 <span class="variableName">mutablePrimitive1</span> in globals.cpp at line 1</li>
       
   153             <li>
       
   154                 <span class="variableName">mutablePrimitive2</span> in globals.cpp at line 2</li>
       
   155         </ol>
       
   156         <p class="details">End report: 2008-12-16T13:43:49.65Z</p>
       
   157     </body>
       
   158 </html>
       
   159     \endraw
       
   160 
       
   161     \section1 XQuery Code Walk-Through  
       
   162 
       
   163     The XQuery source is in
       
   164     \c{examples/xmlpatterns/xquery/globalVariables/reportGlobals.xq}
       
   165     It begins with two variable declarations that begin the XQuery:
       
   166 
       
   167     \quotefromfile examples/xmlpatterns/xquery/globalVariables/reportGlobals.xq
       
   168     \skipto declare variable
       
   169     \printto (:
       
   170 
       
   171     The first variable, \c{$fileToOpen}, appears in the \c xmlpatterns
       
   172     command shown earlier, as \c{-param fileToOpen=globals.gccxml}.
       
   173     This binds the variable name to the file name. This variable is
       
   174     then used in the declaration of the second variable, \c{$inDoc},
       
   175     as the parameter to the
       
   176     \l{http://www.w3.org/TR/xpath-functions/#func-doc} {doc()}
       
   177     function. The \c{doc()} function returns the document node of
       
   178     \c{globals.gccxml}, which is assigned to \c{$inDoc} to be used
       
   179     later in the XQuery as the root node of our searches for global
       
   180     variables.
       
   181 
       
   182     Next skip to the end of the XQuery, where the \c{<html>} element
       
   183     is constructed. The \c{<html>} will contain a \c{<head>} element
       
   184     to specify a heading for the html page, followed by some style
       
   185     instructions for displaying the text, and then the \c{<body>}
       
   186     element.
       
   187 
       
   188     \quotefromfile examples/xmlpatterns/xquery/globalVariables/reportGlobals.xq
       
   189     \skipto <html xmlns
       
   190     \printuntil
       
   191 
       
   192     The \c{<body>} element contains a call to the \c{local:report()}
       
   193     function, which is where the query does the "heavy lifting."  Note
       
   194     the two \c{return} clauses separated by the \e {comma operator}
       
   195     about halfway down:
       
   196 
       
   197     \quotefromfile examples/xmlpatterns/xquery/globalVariables/reportGlobals.xq
       
   198     \skipto declare function local:report()
       
   199     \printuntil };
       
   200 
       
   201     The \c{return} clauses are like two separate queries. The comma
       
   202     operator separating them means that both \c{return} clauses are
       
   203     executed and both return their results, or, rather, both output
       
   204     their results. The first \c{return} clause searches for global
       
   205     variables with complex types, and the second searches for mutable
       
   206     global variables with primitive types.
       
   207 
       
   208     Here is the html generated for the \c{<body>} element. Compare
       
   209     it with the XQuery code above:
       
   210 
       
   211     \quotefromfile examples/xmlpatterns/xquery/globalVariables/globals.html
       
   212     \skipto <body>
       
   213     \printuntil </body>
       
   214      
       
   215     The XQuery declares three more local functions that are called in
       
   216     turn by the \c{local:report()} function. \c{isComplexType()}
       
   217     returns true if the variable has a complex type. The variable can
       
   218     be mutable or const.
       
   219     
       
   220     \quotefromfile examples/xmlpatterns/xquery/globalVariables/reportGlobals.xq
       
   221     \skipto declare function local:isComplexType
       
   222     \printuntil };
       
   223 
       
   224     \c{isPrimitive()} returns true if the variable has a primitive
       
   225     type. The variable must be mutable.
       
   226 
       
   227     \quotefromfile examples/xmlpatterns/xquery/globalVariables/reportGlobals.xq
       
   228     \skipto declare function local:isPrimitive
       
   229     \printuntil };
       
   230 
       
   231     \c{location()} returns a text constructed from the variable's file
       
   232     and line number attributes.
       
   233 
       
   234     \quotefromfile examples/xmlpatterns/xquery/globalVariables/reportGlobals.xq
       
   235     \skipto declare function local:location
       
   236     \printuntil };
       
   237 
       
   238  */