|
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 # LFunctionCantLeave.py |
|
18 # |
|
19 # Checks : L-functions that cannot leave. |
|
20 # |
|
21 # Reason : A function should not be named with an 'L' if it cannot |
|
22 # leave. The only exception is in virtual functions where the |
|
23 # function name is defined in the base class so the L cannot be |
|
24 # emoved. For example, RunL(). |
|
25 # |
|
26 # ################################################################# |
|
27 |
|
28 script = CScript("LFunctionCantLeave") |
|
29 script.iReString = r""" |
|
30 [A-Za-z0-9]+ # return type |
|
31 \s+ |
|
32 [C|T|R][A-Za-z0-9]+ # class name |
|
33 :: |
|
34 ([A-Za-z0-9]+L(C|D)*) # leaving function name (possible LC or LD function) |
|
35 \s* # optional whitespace |
|
36 \( # open bracket |
|
37 .* # parameters |
|
38 \) # close bracket |
|
39 \s* |
|
40 [^;] # no semicolon after function definition |
|
41 """ |
|
42 script.iFileExts = ["cpp"] |
|
43 script.iCategory = KCategoryCodeReviewGuides |
|
44 script.iIgnore = KIgnoreCommentsAndQuotes |
|
45 script.iSeverity = KSeverityLow |
|
46 |
|
47 lfunctioncantleaveLeavingMethod = re.compile(""" |
|
48 [A-Za-z0-9]+ |
|
49 L |
|
50 (C|D)* |
|
51 \( |
|
52 """, re.VERBOSE) |
|
53 |
|
54 lfunctioncantleaveUserLeave = re.compile(""" |
|
55 User::Leave |
|
56 """, re.VERBOSE) |
|
57 |
|
58 lfunctioncantleaveNewELeave = re.compile(""" |
|
59 new |
|
60 \s* |
|
61 \( |
|
62 \s* |
|
63 ELeave |
|
64 \s* |
|
65 \) |
|
66 """, re.VERBOSE) |
|
67 |
|
68 reLFunctionIgnoreStr = "RunL" |
|
69 scriptNode = script.ScriptConfig() |
|
70 if (scriptNode <> None): |
|
71 for wordNode in scriptNode.getElementsByTagName("LFunctionIgnoreRE"): |
|
72 reLFunctionIgnoreStr = wordNode.firstChild.nodeValue |
|
73 print "Note: ignoring the following functions when checking for L-functions that cannot leave: " + reLFunctionIgnoreStr |
|
74 break |
|
75 if len(reLFunctionIgnoreStr) > 0: |
|
76 reLFunctionIgnores = re.compile(reLFunctionIgnoreStr, re.IGNORECASE) |
|
77 else : |
|
78 reLFunctionIgnores = None |
|
79 |
|
80 def lfunctioncantleavecompare(lines, currentline, rematch, filename): |
|
81 line = lines[currentline] |
|
82 m = rematch.search(line) |
|
83 |
|
84 if m: |
|
85 #ignore functions on the ignored list |
|
86 functionName = m.group(1) |
|
87 if reLFunctionIgnores: |
|
88 if reLFunctionIgnores.match(functionName): |
|
89 return 0 |
|
90 |
|
91 i = currentline |
|
92 |
|
93 # find opening { in function |
|
94 while (line.count("{") == 0): |
|
95 i = i + 1 |
|
96 if (i >= len(lines)): |
|
97 return 1 |
|
98 line = lines[i] |
|
99 |
|
100 # if empty function (or one-line function?) |
|
101 if (line.count("}") > 0): |
|
102 return 1 |
|
103 |
|
104 i = i + 1 |
|
105 bracketDepth = 1 |
|
106 |
|
107 while (i < len(lines)): |
|
108 line = lines[i] |
|
109 if lfunctioncantleaveLeavingMethod.search(line): |
|
110 return 0 |
|
111 if lfunctioncantleaveUserLeave.search(line): |
|
112 return 0 |
|
113 if lfunctioncantleaveNewELeave.search(line): |
|
114 return 0 |
|
115 |
|
116 bracketDepth += line.count("{") - line.count("}") |
|
117 if (bracketDepth == 0): |
|
118 return 1 |
|
119 i = i + 1 |
|
120 return 0 |
|
121 |
|
122 script.iCompare = lfunctioncantleavecompare |
|
123 scanner.AddScript(script) |