1
|
1 |
# #################################################################
|
|
2 |
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
# All rights reserved.
|
|
4 |
#
|
|
5 |
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
6 |
#
|
|
7 |
# * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
8 |
# * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
9 |
# * Neither the name of Nokia Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10 |
#
|
|
11 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
12 |
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
|
|
13 |
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
14 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
15 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.#
|
|
16 |
#
|
|
17 |
# ConnectAndDontCloseMemberVariable.py
|
|
18 |
#
|
|
19 |
# Checks : Calling Connect() or Open() on a member variable without
|
|
20 |
# calling Close() in the destructor.
|
|
21 |
#
|
|
22 |
# Reason : If Connect() or Open() is called on any member variable,
|
|
23 |
# then Close() must be called in the destructor.
|
|
24 |
#
|
|
25 |
# #################################################################
|
|
26 |
|
|
27 |
script = CScript("ConnectAndDontCloseMemberVariable")
|
|
28 |
script.iReString = r"""
|
|
29 |
^\s* # start of line plus optional whitespace
|
|
30 |
i[A-Z]\w+ # member variable name
|
|
31 |
(\.|->) # "." or "->"
|
|
32 |
(Connect|Open)
|
|
33 |
\s*\(\s*\)
|
|
34 |
\s*;
|
|
35 |
"""
|
|
36 |
script.iFileExts = ["cpp"]
|
|
37 |
script.iCategory = KCategoryWrongFunctionality
|
|
38 |
script.iIgnore = KIgnoreCommentsAndQuotes
|
|
39 |
script.iSeverity = KSeverityMedium
|
|
40 |
|
|
41 |
ConnectAndDontCloseLeavingFunction = re.compile("""
|
|
42 |
[A-Za-z0-9]+
|
|
43 |
L
|
|
44 |
(C|D|P)*
|
|
45 |
\s*
|
|
46 |
\(
|
|
47 |
""", re.VERBOSE)
|
|
48 |
|
|
49 |
def ConnectNotCloseCompare(lines, currentline, rematch, filename):
|
|
50 |
line = lines[currentline]
|
|
51 |
m = rematch.search(line)
|
|
52 |
|
|
53 |
if m:
|
|
54 |
# get the variable name, between the '*' and the '='
|
|
55 |
startindex = line.find("i")
|
|
56 |
if (startindex == -1):
|
|
57 |
startindex = 0
|
|
58 |
endindex = line.find("Connect")
|
|
59 |
if (endindex == -1):
|
|
60 |
endindex = line.find("Open")
|
|
61 |
variable = line[startindex:endindex]
|
|
62 |
if (len(variable) < 1):
|
|
63 |
return 0
|
|
64 |
variable = TrimVariableName(variable)
|
|
65 |
|
|
66 |
# see if variable is closed within this function before a Leave
|
|
67 |
i = currentline
|
|
68 |
bracketdepth = GetBracketDepth(lines, i)
|
|
69 |
while (i < len(lines)) and (bracketdepth > 0):
|
|
70 |
line = lines[i]
|
|
71 |
if (line.find(variable) >= 0):
|
|
72 |
if (line.find("Close") >= 0):
|
|
73 |
return 0
|
|
74 |
|
|
75 |
bracketdepth += line.count("{")
|
|
76 |
bracketdepth -= line.count("}")
|
|
77 |
|
|
78 |
if (line.find("User::Leave") >= 0):
|
|
79 |
bracketdepth = 0
|
|
80 |
if (line.find("ELeave") >= 0):
|
|
81 |
bracketdepth = 0
|
|
82 |
if (ConnectAndDontCloseLeavingFunction.search(line)):
|
|
83 |
bracketdepth = 0
|
|
84 |
|
|
85 |
i = i + 1
|
|
86 |
|
|
87 |
# look for destructor and see if variable is closed there
|
|
88 |
stringToFind = "~" + scanner.iCurrentClassName
|
|
89 |
i = 1
|
|
90 |
foundDestructor = 0
|
|
91 |
bracketdepth = 0
|
|
92 |
while (i < len(lines)):
|
|
93 |
thisLine = lines[i]
|
|
94 |
if (thisLine.find(stringToFind) >= 0) and (foundDestructor == 0):
|
|
95 |
while (i < len(lines)) and (foundDestructor == 0):
|
|
96 |
thisLine = lines[i]
|
|
97 |
if (thisLine.find('{') >= 0):
|
|
98 |
foundDestructor = 1
|
|
99 |
else:
|
|
100 |
i = i + 1
|
|
101 |
|
|
102 |
if (foundDestructor):
|
|
103 |
bracketdepth += thisLine.count("{")
|
|
104 |
bracketdepth -= thisLine.count("}")
|
|
105 |
if (bracketdepth == 0):
|
|
106 |
return 1
|
|
107 |
|
|
108 |
if (thisLine.find(variable) >= 0):
|
|
109 |
if (thisLine.find("Close") >= 0):
|
|
110 |
return 0
|
|
111 |
i = i + 1
|
|
112 |
|
|
113 |
if (foundDestructor):
|
|
114 |
return 1
|
|
115 |
return 0
|
|
116 |
|
|
117 |
script.iCompare = ConnectNotCloseCompare
|
|
118 |
scanner.AddScript(script)
|