forked from myint/cppclean
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcppclean
More file actions
Latest commit
executable file
·168 lines (143 loc) · 6.12 KB
/
Copy pathcppclean
File metadata and controls
executable file
·168 lines (143 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
#
# Copyright 2007 Neal Norwitz
# Portions Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Find warnings for C++ code."""
from __future__ importabsolute_import
from __future__ importprint_function
from __future__ importunicode_literals
importargparse
importfnmatch
importos
importsys
fromcppimport__version__
fromcppimportast
fromcppimportfind_warnings
fromcppimportnonvirtual_dtors
fromcppimportstatic_data
fromcppimporttokenize
fromcppimportutils
defmatch_file(filename, exclude_patterns):
"""Return True if file is a C++ file or a directory."""
base_name=os.path.basename(filename)
ifbase_name.startswith('.'):
returnFalse
forpatterninexclude_patterns:
iffnmatch.fnmatch(base_name, pattern):
returnFalse
iffind_warnings.is_header_file(filename):
returnTrue
iffind_warnings.is_cpp_file(filename):
returnTrue
ifos.path.isdir(filename):
returnTrue
returnFalse
deffind_files(filenames, exclude_patterns):
"""Yield filenames."""
whilefilenames:
name=filenames.pop(0)
ifos.path.isdir(name):
forroot, directories, childreninos.walk(name):
filenames+= [os.path.join(root, f) forfinsorted(children)
ifmatch_file(os.path.join(root, f),
exclude_patterns)]
directories[:] = [dfordindirectories
ifmatch_file(os.path.join(root, d),
exclude_patterns)]
else:
yieldname
defmain():
parser=argparse.ArgumentParser()
parser.add_argument('files', nargs='+')
parser.add_argument('--exclude', action='append',
dest='exclude_patterns', default=[], metavar='pattern',
help='exclude files matching this pattern; '
'specify this multiple times for multiple '
'patterns')
parser.add_argument('--include-path', '-i', '-I', action='append',
dest='include_paths', default=[],
metavar='path',
help='add a header include path; '
'specify this multiple times for multiple '
'include paths')
parser.add_argument('--include-path-system', '-s', action='append',
dest='include_system_paths', default=[],
metavar='sys_path',
help='same as --include-path but explicitly '
'designates all header files found in these '
'directories as "system" includes')
parser.add_argument('--include-path-non-system', '-n',
action='append', dest='include_nonsystem_paths',
metavar='nonsys_path',
default=[],
help='same as --include-path but explicitly '
'designates all header files found in these '
'directories as "non-system" includes')
parser.add_argument('--verbose', action='store_true',
help='print verbose messages')
parser.add_argument('--version', action='version',
version='%(prog)s '+__version__)
parser.add_argument('--quiet', '-q', action='store_true',
help='ignore parse errors')
args=parser.parse_args()
# For Python 2 where argparse does not return Unicode.
args.files= [filename.decode(sys.getfilesystemencoding())
ifhasattr(filename, 'decode') elsefilename
forfilenameinargs.files]
all_includes=list(set(
args.include_paths+args.include_system_paths+
args.include_nonsystem_paths))
status=0
forfilenamein (
sorted(find_files(args.files,
exclude_patterns=args.exclude_patterns))
):
ifargs.verbose:
print('Processing', filename, file=sys.stderr)
try:
source=utils.read_file(filename)
ifsourceisNone:
continue
builder=ast.builder_from_source(source,
filename,
args.include_system_paths,
args.include_nonsystem_paths,
quiet=args.quiet)
entire_ast=list([_ffor_finbuilder.generate() if_f])
excepttokenize.TokenErrorasexception:
ifargs.verbose:
print('{}: token error: {}'.format(filename, exception),
file=sys.stderr)
continue
except (ast.ParseError,
UnicodeDecodeError) asexception:
ifnotargs.quiet:
print('{}: parsing error: {}'.format(filename, exception),
file=sys.stderr)
continue
formodulein [find_warnings,
nonvirtual_dtors,
static_data]:
ifmodule.run(filename, source, entire_ast,
include_paths=all_includes,
system_include_paths=args.include_system_paths,
nonsystem_include_paths=args.include_nonsystem_paths,
quiet=args.quiet):
status=1
returnstatus
try:
sys.exit(main())
exceptKeyboardInterrupt:
sys.exit(1)