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.,
grep,sed,和awk). Now we can also use XQuery with the Qt XML Patterns module.An extension to the
g++open source C++ compiler ( GCC-XML ) generates an XML description of C++ source code declarations. This XML description can then be processed by Qt XML Patterns using XQueries to navigate the XML description of the C++ source and produce a report. Consider the problem of finding 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 GCC-XML to find the variables that need protecting?
Consider the declarations in this hypothetical C++ application:
1. int mutablePrimitive1; 2. int mutablePrimitive2; 3. const int constPrimitive1 = 4; 4. const int constPrimitive2 = 3; 5. 6. class ComplexClass 7. { 8. public: 9. ComplexClass(); 10. ComplexClass(const ComplexClass &); 11. ~ComplexClass(); 12. }; 13. 14. ComplexClass mutableComplex1; 15. ComplexClass mutableComplex2; 16. const ComplexClass constComplex1; 17. const ComplexClass constComplex2; 18. 19. int main() 20. { 22. int localVariable; 23. localVariable = 0; 24. return localVariable; 25. }
Submitting this C++ source to GCC-XML produces this XML description:
We need an XQuery to find the global variables in the XML description. Here is our XQuery source. We walk through it in XQuery 代码演练 .
To run the XQuery 使用
xmlpatternscommand line utility, enter the following command:xmlpatterns reportGlobals.xq -param fileToOpen=globals.gccxml -output globals.html
xmlpatternscommand loads and parsesglobals.gccxml, runs the XQueryreportGlobals.xq, and generates this report:Start report: 2008-12-16T13:43:49.65Z
Global variables with complex types:
mutableComplex1 in globals.cpp at line 14
mutableComplex2 in globals.cpp at line 15
constComplex1 in globals.cpp at line 16
constComplex2 in globals.cpp at line 17
Mutable global variables with primitives types:
mutablePrimitive1 in globals.cpp at line 1
mutablePrimitive2 in globals.cpp at line 2
End report: 2008-12-16T13:43:49.65Z
XQuery source is in
examples/xmlpatterns/xquery/globalVariables/reportGlobals.xqIt begins with two variable declarations that begin the XQuery :第一个变量,
$fileToOpen, appears in thexmlpatternscommand shown earlier, as-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,$inDoc, as the parameter to the doc() function. Thedoc()function returns the document node ofglobals.gccxml, which is assigned to$inDocto 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
<html>element is constructed. The<html>will contain a<head>element to specify a heading for the html page, followed by some style instructions for displaying the text, and then the<body>元素。
<body>element contains a call to thelocal:report()function, which is where the query does the “heavy lifting.” Note the tworeturnclauses separated by the comma operator about halfway down:
returnclauses are like two separate queries. The comma operator separating them means that bothreturnclauses are executed and both return their results, or, rather, both output their results. The firstreturnclause 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
<body>element. Compare it with the XQuery code above:XQuery declares three more local functions that are called in turn by the
local:report()函数。isComplexType()returns true if the variable has a complex type. The variable can be mutable or const.
isPrimitive()returns true if the variable has a primitive type. The variable must be mutable.
location()returns a text constructed from the variable’s file and line number attributes.