srcanamdw/codescanner/scripts/worryingcomments.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 # worryingcomments.py
       
    18 #
       
    19 # Checks : Worrying comments.
       
    20 #
       
    21 # Reason : Typically, exclamation and question marks in comments 
       
    22 # indicate that something odd is in the code or that it is 
       
    23 # unfinished or not understood fully.
       
    24 #
       
    25 # #################################################################
       
    26 
       
    27 script = CScript("worryingcomments")
       
    28 script.iFileExts = ["h", "cpp", "c"]
       
    29 script.iCategory = KCategoryCodeReviewGuides
       
    30 script.iIgnore = KIgnoreQuotes
       
    31 script.iSeverity = KSeverityLow
       
    32 
       
    33 reWordsStr = r"""\!|\?|[Zz]{3}|kludge|workaround|\scrap|hack"""
       
    34 scriptNode = script.ScriptConfig()
       
    35 if (scriptNode <> None):
       
    36 	for wordNode in scriptNode.getElementsByTagName("worryRE"):
       
    37 		reWordsStr = wordNode.firstChild.nodeValue
       
    38 		print "Note: 'worrying comments' pattern configured as: " + reWordsStr
       
    39 		break
       
    40 script.iReString = r"""
       
    41 	/(/|\*)						# "//" or "/*"
       
    42 	.*("""+reWordsStr+r""")		# skip to "!", "?", "zzz", "kludge", "workaround", " crap" or "hack"
       
    43 	"""
       
    44 
       
    45 def worryingcommentcodecompare(lines, currentline, rematch, filename):
       
    46 	m = rematch.search(lines[currentline])
       
    47 	if m:
       
    48 		line = lines[currentline]
       
    49 		i = 0
       
    50 		inCommentBlock = 0
       
    51 
       
    52 		while i < len(line):
       
    53 			if not inCommentBlock:
       
    54 				if (line[i] == "/"):
       
    55 					if (line[i + 1] == "/"):
       
    56 						# note i+3 to bypass the character after the start of the comment as can be an in-source documentation directive
       
    57 						return (line[i+3:].find("!")<>-1) or (line[i+3:].find("?")<>-1)
       
    58 					elif (line[i + 1] == "*"):
       
    59 						inCommentBlock = 1
       
    60 						i += 2
       
    61 						continue
       
    62 			else:
       
    63 				endIndex = line[i:].find("*/")
       
    64 				if (endIndex <> -1):
       
    65 					inCommentBlock = 0
       
    66 					# note, that first character is ignored in comments as can be direction to the in-source documentation tool
       
    67 					if (line[i+1:i + endIndex + 2].find("!") <> -1) or (line[i+1:i + endIndex + 2].find("?") <> -1):
       
    68 						return 1
       
    69 					i += endIndex + 2
       
    70 					continue
       
    71 				else:
       
    72 					return 1			
       
    73 			i += 1		
       
    74 	return 0
       
    75 
       
    76 script.iCompare = worryingcommentcodecompare
       
    77 scanner.AddScript(script)