|
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 # missingcancel.py |
|
18 # |
|
19 # Checks : Cancel() not called in active object's destructor. |
|
20 # |
|
21 # Reason : Cancel() should always be called in active object's |
|
22 # destructor to cancel an outstanding request if there is one. |
|
23 # If there is no request pending then Cancel() just does nothing, |
|
24 # but if we do not call Cancel() when having an outstanding request |
|
25 # a panic will be raised. CodeScanner occasionally gives false |
|
26 # positives for this issue. Individual cases should be |
|
27 # investigated. |
|
28 # |
|
29 # ################################################################# |
|
30 |
|
31 script = CScript("missingcancel") |
|
32 script.iReString = r""" |
|
33 :: |
|
34 \s* # optional whitespace |
|
35 ~C(\w+) |
|
36 """ |
|
37 script.iFileExts = ["cpp"] |
|
38 script.iCategory = KCategoryCodingStandards |
|
39 script.iIgnore = KIgnoreCommentsAndQuotes |
|
40 script.iSeverity = KSeverityLow |
|
41 |
|
42 # possible active and timer object types |
|
43 activeObjectType = re.compile("\w+Active$") |
|
44 timerObjectType = re.compile("\w+Timer$") |
|
45 |
|
46 cancelFunction = re.compile(""" |
|
47 Cancel |
|
48 \s* |
|
49 \( |
|
50 """, re.VERBOSE) |
|
51 |
|
52 def isActiveDestructor(destructorType): |
|
53 if (activeObjectType.match(destructorType)) or (timerObjectType.match(destructorType)): |
|
54 return True |
|
55 return False |
|
56 |
|
57 def cancelcompare(lines, currentline, rematch, filename): |
|
58 line = lines[currentline] |
|
59 m = rematch.search(line) |
|
60 |
|
61 if m: |
|
62 destructorType = m.group(1) |
|
63 # skip non-active types |
|
64 if (not isActiveDestructor(destructorType)): |
|
65 return 0 |
|
66 |
|
67 i = currentline |
|
68 |
|
69 # find opening { in function |
|
70 while (line.count("{") == 0): |
|
71 i = i + 1 |
|
72 if (i >= len(lines)): |
|
73 return 1 |
|
74 line = lines[i] |
|
75 |
|
76 # if one-line or empty function |
|
77 if (line.count("}") > 0): |
|
78 if (cancelFunction.search(line)): |
|
79 return 0 |
|
80 else: |
|
81 return 1 |
|
82 |
|
83 i = i + 1 |
|
84 bracketDepth = 1 |
|
85 while (i < len(lines)): |
|
86 line = lines[i] |
|
87 if (cancelFunction.search(line)): |
|
88 return 0 |
|
89 |
|
90 bracketDepth += line.count("{") - line.count("}") |
|
91 if (bracketDepth == 0): |
|
92 return 1 |
|
93 i = i + 1 |
|
94 return 0 |
|
95 |
|
96 script.iCompare = cancelcompare |
|
97 scanner.AddScript(script) |