srcanamdw/codescanner/scripts/deleteMemberVariable.py
changeset 1 22878952f6e2
equal deleted inserted replaced
0:509e4801c378 1:22878952f6e2
       
     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 # deleteMemberVariable.py
       
    18 #
       
    19 # Checks : Member variable deleted incorrectly.
       
    20 #
       
    21 # Reason : When a member variable is deleted, it should be assigned 
       
    22 # either to NULL or to another value. This prevents accidental 
       
    23 # access of the deleted object. If a NewL() or other leaving 
       
    24 # function is called to reassign the member variable, it should 
       
    25 # first be assigned to NULL in case that function leaves.
       
    26 #
       
    27 # #################################################################
       
    28 
       
    29 script = CScript("deleteMemberVariable")
       
    30 script.iReString = r"""
       
    31 	delete		# delete command
       
    32 	\s*			# optional space
       
    33 	\(*			# optional open bracket
       
    34 	\s*			# optional space
       
    35 	i[A-Z]		# member variable name
       
    36 	"""
       
    37 script.iFileExts = ["cpp"]
       
    38 script.iCategory = KCategoryCanPanic
       
    39 script.iIgnore = KIgnoreCommentsAndQuotes
       
    40 script.iSeverity = KSeverityHigh
       
    41 
       
    42 deleteMemberVariableLeavingFunction = re.compile("""
       
    43 	[A-Za-z0-9]+
       
    44 	L
       
    45 	(C|D)*
       
    46 	\s*
       
    47 	\(
       
    48 	""", re.VERBOSE)
       
    49 
       
    50 deleteMemberVariableNewELeave = re.compile("""
       
    51 	new
       
    52 	\s*
       
    53 	\(
       
    54 	\s*
       
    55 	ELeave
       
    56 	\s*
       
    57 	\)
       
    58 	""", re.VERBOSE)
       
    59 
       
    60 deleteMemberVariableLeave = re.compile("""
       
    61 	User::Leave
       
    62 	""", re.VERBOSE)
       
    63 
       
    64 deleteMemberVariableArrayDeletion = re.compile("""
       
    65 	(\.|->)
       
    66 	\s*
       
    67 	Delete
       
    68 	\s*
       
    69 	\(
       
    70 	""", re.VERBOSE)
       
    71 
       
    72 deleteMemberVariableArrayDeletion2 = re.compile("""
       
    73 	(\.|->)
       
    74 	\s*
       
    75 	Remove
       
    76 	\s*
       
    77 	\(
       
    78 	""", re.VERBOSE)
       
    79 
       
    80 def deleteMemberVariableCompare(lines, currentline, rematch, filename):
       
    81 	line = lines[currentline]
       
    82 	m = rematch.search(line)
       
    83 
       
    84 	if m:
       
    85 		if (scanner.iCurrentMethodName.find(scanner.iCurrentClassName) > -1):
       
    86 			return 0
       
    87 
       
    88 		deletingArrayMember = 0
       
    89 		if (line.find("[")):
       
    90 			deletingArrayMember = 1
       
    91 
       
    92 		# get the variable name, between the 'delete' and the ';'
       
    93 		endindex = line.find(";")
       
    94 		if (endindex == -1):
       
    95 			endindex = len(line)
       
    96 		startindex = line.find("delete")
       
    97 		variable = line[startindex+6:endindex]
       
    98 		variable2 = TrimVariableName(variable)
       
    99 
       
   100 		bracketdepth = GetBracketDepth(lines, currentline)
       
   101 
       
   102 		i = currentline
       
   103 		while (i < len(lines)):
       
   104 			nextLine = lines[i]
       
   105 
       
   106 			if deleteMemberVariableLeavingFunction.search(nextLine):
       
   107 				return 1
       
   108 			if deleteMemberVariableLeave.search(nextLine):
       
   109 				return 1
       
   110 			if deleteMemberVariableNewELeave.search(nextLine):
       
   111 				return 1
       
   112 			
       
   113 			if (deletingArrayMember == 1):
       
   114 				if (nextLine.find(variable2)):
       
   115 					if deleteMemberVariableArrayDeletion.search(nextLine):
       
   116 						return 0
       
   117 					if deleteMemberVariableArrayDeletion2.search(nextLine):
       
   118 						return 0
       
   119 
       
   120 			foundAssignment = nextLine.find("=")
       
   121 			if (foundAssignment > -1 and nextLine[foundAssignment+1] != "="):
       
   122 				assigned = nextLine[:foundAssignment]
       
   123 				if (assigned.find(variable2)):
       
   124 					return 0
       
   125 
       
   126 			if (nextLine.find('{') >= 0):
       
   127 				bracketdepth = bracketdepth + 1
       
   128 			if (nextLine.find('}') >= 0):
       
   129 				bracketdepth = bracketdepth - 1
       
   130 				if (bracketdepth == 0):
       
   131 					return 1
       
   132 
       
   133 			i = i + 1
       
   134 	return 0
       
   135 
       
   136 script.iCompare	= deleteMemberVariableCompare
       
   137 scanner.AddScript(script)