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 |
# magicnumbers.py
|
|
18 |
#
|
|
19 |
# Checks : Use of magic numbers.
|
|
20 |
#
|
|
21 |
# Reason : Magic numbers - that is, numbers that are hard-coded
|
|
22 |
# into the source code and instead of being presented as constants
|
|
23 |
# - make code difficult to maintain and give no indication of why
|
|
24 |
# a calculation is the way it is. Magic numbers should be replaced
|
|
25 |
# with named constants.
|
|
26 |
#
|
|
27 |
# #################################################################
|
|
28 |
|
|
29 |
script = CScript("magicnumbers")
|
|
30 |
script.iReString = "(.*[^a-zA-Z0-9_])([0-9]+)"
|
|
31 |
script.iFileExts = ["cpp"]
|
|
32 |
script.iCategory = KCategoryCodingStandards
|
|
33 |
script.iIgnore = KIgnoreCommentsAndQuotes
|
|
34 |
script.iSeverity = KSeverityLow
|
|
35 |
|
|
36 |
reEnum = re.compile("^\s*E\w+\s*=\s*");
|
|
37 |
|
|
38 |
def magiccompare(lines, currentline, rematch, filename):
|
|
39 |
m = rematch.search(lines[currentline])
|
|
40 |
if m:
|
|
41 |
pretext = m.group(1)
|
|
42 |
number = m.group(2)
|
|
43 |
if (number == "0") or (number == "1") or (number == '2'):
|
|
44 |
return 0
|
|
45 |
elif (pretext.find("const") <> -1):
|
|
46 |
return 0
|
|
47 |
else:
|
|
48 |
m2 = reEnum.search(pretext)
|
|
49 |
if m2:
|
|
50 |
return 0
|
|
51 |
else:
|
|
52 |
return 1
|
|
53 |
|
|
54 |
script.iCompare = magiccompare
|
|
55 |
scanner.AddScript(script)
|