srcanamdw/codescanner/scripts/callActiveObjectWithoutCheckingOrStopping.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 # callActiveObjectWithoutCheckingOrStopping.py
       
    18 #
       
    19 # Checks : Active object called without checking whether it is 
       
    20 # active or canceling it first.
       
    21 #
       
    22 # Reason : If an active object is started twice, a panic occurs. 
       
    23 # CodeScanner picks out places where there is a call to a Start(), 
       
    24 # Queue(), or After() function on a member variable, without a 
       
    25 # previous call to IsActive(), Cancel(), or Stop(). In general, 
       
    26 # if starting a timer, there should at least be a call to IsActive() 
       
    27 # to ensure that the timer is not already running.
       
    28 #
       
    29 # #################################################################
       
    30 
       
    31 script = CScript("callActiveObjectWithoutCheckingOrStopping")
       
    32 script.iReString = r"""\("""
       
    33 script.iFileExts = ["cpp"]
       
    34 script.iCategory = KCategoryCodeReviewGuides
       
    35 script.iIgnore = KIgnoreCommentsAndQuotes
       
    36 script.iSeverity = KSeverityLow
       
    37 
       
    38 activeObjectIsActive = re.compile("""
       
    39 	(\.|->)
       
    40 	\s*
       
    41 	IsActive
       
    42 	\s*
       
    43 	\(
       
    44 	\s*
       
    45 	\)
       
    46 	""", re.VERBOSE)
       
    47 
       
    48 activeObjectCancel = re.compile("""
       
    49 	(\.|->)
       
    50 	\s*
       
    51 	Cancel
       
    52 	\s*
       
    53 	\(
       
    54 	\s*
       
    55 	\)
       
    56 	""", re.VERBOSE)
       
    57 
       
    58 activeObjectStop = re.compile("""
       
    59 	(\.|->)
       
    60 	\s*
       
    61 	Stop
       
    62 	\s*
       
    63 	\(
       
    64 	\s*
       
    65 	\)
       
    66 	""", re.VERBOSE)
       
    67 
       
    68 activeObjectCreate = re.compile("""
       
    69 	=
       
    70 	\s*
       
    71 	\w+
       
    72 	::
       
    73 	New
       
    74 	\w*
       
    75 	\s*
       
    76 	\(
       
    77 	""", re.VERBOSE)
       
    78 
       
    79 activeObjectCreate2 = re.compile("""
       
    80 	=
       
    81 	\s*
       
    82 	new
       
    83 	\s*
       
    84 	\(
       
    85 	\s*
       
    86 	ELeave
       
    87 	\s*
       
    88 	\)
       
    89 	""", re.VERBOSE)
       
    90 
       
    91 activeObjectStart = re.compile("""
       
    92 	(\.|->)
       
    93 	\s*
       
    94 	Start
       
    95 	\s*
       
    96 	\(
       
    97 	""", re.VERBOSE)
       
    98 
       
    99 activeObjectQueue = re.compile("""
       
   100 	(\.|->)
       
   101 	\s*
       
   102 	Queue
       
   103 	\s*
       
   104 	\(
       
   105 	""", re.VERBOSE)
       
   106 
       
   107 activeObjectAfter = re.compile("""
       
   108 	(\.|->)
       
   109 	\s*
       
   110 	After
       
   111 	\s*
       
   112 	\(
       
   113 	""", re.VERBOSE)
       
   114 
       
   115 # RTimer::After() takes 2 arguments
       
   116 rtimerObjectAfter = re.compile("""
       
   117 	(\.|->)
       
   118 	\s*
       
   119 	After
       
   120 	\s*
       
   121 	\(
       
   122 	\s*
       
   123 	\w+			# first argument
       
   124 	\s*
       
   125 	,
       
   126 	""", re.VERBOSE)
       
   127 
       
   128 # Non-active types with After(), Queue() or Start() function
       
   129 KNonActiveTypes = {
       
   130 	"KAnimation"					: "Animation",
       
   131 	"KAnimator"						: "Animator",
       
   132 	"KClockSourcePeriodicUtility"	: "ClockSourcePeriodicUtility",
       
   133 	"KCodecWrapper"					: "CodecWrapper",
       
   134 	"KCommTimer"					: "CommTimer",
       
   135 	"KEmbeddedStore"				: "EmbeddedStore",
       
   136 	"KFormulaTextLexer"				: "FormulaTextLexer",
       
   137 	"KObexServer"					: "ObexServer",
       
   138 	"KSpriteAnimation"				: "SpriteAnimation",
       
   139 	"KRConnection"					: "RConnection",
       
   140 	"KRSubConnection"				: "RSubConnection",
       
   141 	"KRTest"						: "RTest",
       
   142 	"KRTimer"						: "RTimer",
       
   143 	"KValidityPeriod"				: "ValidityPeriod",
       
   144 	"KVideoPlayHwDevice"			: "VideoPlayHwDevice",
       
   145 	"KVideoRecordHwDevice"			: "VideoRecordHwDevice",
       
   146 }
       
   147 
       
   148 def isNonActiveObject(variable):
       
   149 	for name, value in KNonActiveTypes.items():
       
   150 		if (variable.lower().find(value.lower()) <> -1):
       
   151 			return True
       
   152 	return False
       
   153 
       
   154 def activeObjectCompare(lines, currentline, rematch, filename):
       
   155 	line = lines[currentline]
       
   156 	m = rematch.search(line)
       
   157 
       
   158 	if m:
       
   159 		if (scanner.iCurrentMethodName == "Start"):
       
   160 			return 0
       
   161 		if (scanner.iCurrentMethodName == "Queue"):
       
   162 			return 0
       
   163 		if (scanner.iCurrentMethodName == "After"):
       
   164 			return 0
       
   165 
       
   166 		checkForIsActive = 0	
       
   167 		if (activeObjectStart.search(line)):
       
   168 			checkForIsActive = 1
       
   169 		if (activeObjectQueue.search(line)):
       
   170 			checkForIsActive = 1
       
   171 		if (activeObjectAfter.search(line)):
       
   172 			# make sure we are not dealing with RTimer::After()
       
   173 			if (not rtimerObjectAfter.search(line)):
       
   174 				checkForIsActive = 1
       
   175 		if (checkForIsActive == 1):
       
   176 			varEnd = line.find("->")
       
   177 			if (varEnd < 0):
       
   178 				varEnd = line.find(".")
       
   179 			if (varEnd < 0):
       
   180 				return 0
       
   181 			
       
   182 			variable = TrimVariableName(line[:varEnd])
       
   183 			if (len(variable) == 0):
       
   184 				return 0
       
   185 
       
   186 			# if a local variable then unlikely to have been started already
       
   187 			if (len(variable) > 2):
       
   188 				if (variable[0] != 'i'):
       
   189 					return 0
       
   190 				else:
       
   191 					if (variable[1] < 'A') or (variable[1] > 'Z'):
       
   192 						return 0
       
   193 
       
   194 			# ignore non-active object
       
   195 			if isNonActiveObject(variable):
       
   196 				return 0
       
   197 
       
   198 			i = currentline
       
   199 			while (i > scanner.iCurrentMethodStart):
       
   200 				line = lines[i]
       
   201 				varPos = line.find(variable)
       
   202 				if (varPos >= 0):
       
   203 					cutLine = line[varPos:]
       
   204 					if (activeObjectIsActive.search(cutLine)):
       
   205 						return 0
       
   206 					if (activeObjectCancel.search(cutLine)):
       
   207 						return 0
       
   208 					if (activeObjectStop.search(cutLine)):
       
   209 						return 0
       
   210 					if (activeObjectCreate.search(cutLine)):
       
   211 						return 0
       
   212 					if (activeObjectCreate2.search(cutLine)):
       
   213 						return 0
       
   214 				i = i - 1
       
   215 			return 1
       
   216 
       
   217 	return 0
       
   218 
       
   219 script.iCompare	= activeObjectCompare
       
   220 scanner.AddScript(script)