srcanamdw/codescanner/scripts/inheritanceorder.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 # inheritaenceorder.py
       
    18 #
       
    19 # Checks : Incorrect inheritance order of M and C classes.
       
    20 #
       
    21 # Reason : If a C class inherits first from an M class and then a 
       
    22 # C class, a panic can occur when trying to pop a CBase pointer 
       
    23 # pointing to such a class from the cleanup stack when in fact a 
       
    24 # pointer pointing to the first predecessor, the mixin class, was 
       
    25 # popped instead.
       
    26 #
       
    27 # #################################################################
       
    28 
       
    29 script = CScript("inheritanceorder")
       
    30 script.iReString = r"""
       
    31     ^\s*         # optional whitespace
       
    32     class
       
    33     \s+          # whitespace
       
    34     (\w+::)?
       
    35     (\w+)        # class name
       
    36     \s*          # optional whitespace
       
    37     :
       
    38     (.*)         # inheritance list
       
    39     """
       
    40 script.iFileExts = ["h", "cpp"]
       
    41 script.iCategory = KCategoryCanPanic
       
    42 script.iIgnore = KIgnoreCommentsAndQuotes
       
    43 script.iSeverity = KSeverityHigh
       
    44 
       
    45 def inheritancecompare(lines, currentline, rematch, filename):
       
    46     m = rematch.search(lines[currentline])
       
    47     if m:
       
    48         className = m.group(2)
       
    49         if className[0] <> "C" or (len(className) == 1) or not className[1].isupper():
       
    50             return 0
       
    51 
       
    52         inheritanceString = m.group(3)
       
    53 
       
    54         # check for inheritance list spanning multiple lines
       
    55         i = currentline + 1
       
    56         while (inheritanceString.find("{") == -1) and i < len(lines):
       
    57             if (inheritanceString.find(";") <> -1):
       
    58                 return 0
       
    59             inheritanceString += lines[i]
       
    60             i += 1
       
    61 
       
    62         # construct inheritance class list
       
    63         inheritancelist = inheritanceString.split(",")
       
    64         reclass = re.compile("[\s:]*(public|protected|private)?\s*([\w:]+)")
       
    65         classlist = []
       
    66         for inheritance in inheritancelist:
       
    67             match = reclass.search(inheritance)
       
    68             if match:
       
    69                 inheritclass = match.group(2)
       
    70                 colonpos = inheritclass.rfind(":")
       
    71                 if (colonpos <> -1):
       
    72                     inheritclass = inheritclass[colonpos + 1:]
       
    73                 classlist.append(inheritclass)
       
    74 
       
    75         # check the order of M and C classes in inheritance class list 
       
    76         mclassIndex = -1
       
    77         cclassIndex = -1
       
    78         for classname in classlist:
       
    79             if (len(classname) > 2):
       
    80                 if classname[0] == "M" and classname[1].isupper() and mclassIndex == -1:
       
    81                     mclassIndex = classlist.index(classname)
       
    82                 if classname[0] == "C" and classname[1].isupper() and cclassIndex == -1:
       
    83                     cclassIndex = classlist.index(classname)
       
    84         if mclassIndex != -1 and cclassIndex != -1 and mclassIndex < cclassIndex:
       
    85             return 1
       
    86         else:
       
    87             return 0
       
    88 
       
    89     return 0
       
    90 
       
    91 script.iCompare = inheritancecompare
       
    92 scanner.AddScript(script)