1#!/usr/bin/env python
2
3# Copyright (C) 2001-2012 Artifex Software, Inc.
4# All Rights Reserved.
5#
6# This software is provided AS-IS with no warranty, either express or
7# implied.
8#
9# This software is distributed under license and may not be copied,
10# modified or distributed except as expressly authorized under the terms
11# of the license contained in the file LICENSE in this distribution.
12#
13# Refer to licensing information at http://www.artifex.com or contact
14# Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134, San Rafael,
15# CA  94903, U.S.A., +1(415)492-9861, for further information.
16#
17
18
19# Check for C++ style comments
20
21import os
22import re
23from glob import glob
24
25import gsconf
26from gstestutils import GSTestCase, gsRunTestsMain
27
28class GSCheckForComments(GSTestCase):
29
30    def __init__(self, root, dirName, extensions=['*'], skip=[]):
31        self.root = root
32        self.dirName = dirName
33        self.extensions = extensions
34        self.skip = skip
35        GSTestCase.__init__(self)
36
37    def shortDescription(self):
38        return "All relevant files must not have C++ style comments"\
39               " (checking %s)" % (self.dirName,)
40
41    def runTest(self):
42        d, extns, skip = self.root + self.dirName, self.extensions, self.skip
43        skip = map((lambda o,d=d: d + os.sep + o), skip)
44        incorrect = []
45        for e in extns:
46            for f in glob(d + os.sep + '*.' + e):
47                if f in skip or os.path.isdir(f):
48                    continue
49                fp = open(f, 'r')
50                text_code = fp.read()
51                fp.close()
52
53                pattern = re.compile("(\\\\.)|(\")|(')|(/\*)|(\*/)|(//)")
54                mi = pattern.finditer(text_code)
55                try:
56                    inComment = 0
57		    inString = 0
58                    inStringSq = 0	# single quoted string
59                    inStringDq = 0	# double quoted string
60                    while 1:
61                        m = mi.next()
62                        mstr = m.group()
63			if mstr[0] == '\\':		# skip quoted characters (may be ' or ")
64			    continue
65                        if mstr == '"' and not inComment and not inStringSq:
66                            inStringDq = not inStringDq
67			    inString = inStringDq
68                            continue
69                        if mstr == "'" and not inComment and not inStringDq:
70                            inStringSq = not inStringSq
71			    inString = inStringSq
72                            continue
73                        if not inString and mstr == '/*':
74                            inComment = 1
75                            continue
76                        if inComment and mstr == '*/':
77                            inComment = 0
78                            continue
79                        if not inString and not inComment and mstr == '//':
80                            incorrect.append(f)
81                            break
82                except StopIteration:
83                    continue
84
85        if incorrect:
86            incorrect = ['These %d files have C++ style comments:' % (len(incorrect),)] + incorrect
87
88        self.failIfMessages(incorrect)
89
90## Main stuff
91
92checkDirs = [
93    ('src', ['c', 'h'],
94     # list of exempt files
95     ['dwdll.h',
96      'dwimg.h',
97      'dwinst.h',
98      'dwsetup.h',
99      'dwtext.h',
100      'dwuninst.h',
101      'gdevhpij.c',
102      'dmmain.c',
103      'gdevmac.c',
104      'gdevmacxf.c',
105      'gdevwdib.c',
106      'gp_mac.c',
107      'gp_macio.c',
108      'macsysstat.h'
109     ])
110    ]
111
112def addTests(suite, gsroot, **args):
113    for dir, extns, skip in checkDirs:
114        suite.addTest(GSCheckForComments(gsroot, dir, extns, skip))
115
116if __name__ == "__main__":
117    gsRunTestsMain(addTests)
118