jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

548 lines
16KB

  1. #! /usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. # Michel Mooij, michel.mooij7@gmail.com
  4. """
  5. Tool Description
  6. ================
  7. This module provides a waf wrapper (i.e. waftool) around the C/C++ source code
  8. checking tool 'cppcheck'.
  9. See http://cppcheck.sourceforge.net/ for more information on the cppcheck tool
  10. itself.
  11. Note that many linux distributions already provide a ready to install version
  12. of cppcheck. On fedora, for instance, it can be installed using yum:
  13. 'sudo yum install cppcheck'
  14. Usage
  15. =====
  16. In order to use this waftool simply add it to the 'options' and 'configure'
  17. functions of your main waf script as shown in the example below:
  18. def options(opt):
  19. opt.load('cppcheck', tooldir='./waftools')
  20. def configure(conf):
  21. conf.load('cppcheck')
  22. Note that example shown above assumes that the cppcheck waftool is located in
  23. the sub directory named 'waftools'.
  24. When configured as shown in the example above, cppcheck will automatically
  25. perform a source code analysis on all C/C++ build tasks that have been
  26. defined in your waf build system.
  27. The example shown below for a C program will be used as input for cppcheck when
  28. building the task.
  29. def build(bld):
  30. bld.program(name='foo', src='foobar.c')
  31. The result of the source code analysis will be stored both as xml and html
  32. files in the build location for the task. Should any error be detected by
  33. cppcheck the build will be aborted and a link to the html report will be shown.
  34. When needed source code checking by cppcheck can be disabled per task, per
  35. detected error or warning for a particular task. It can be also be disabled for
  36. all tasks.
  37. In order to exclude a task from source code checking add the skip option to the
  38. task as shown below:
  39. def build(bld):
  40. bld.program(
  41. name='foo',
  42. src='foobar.c'
  43. cppcheck_skip=True
  44. )
  45. When needed problems detected by cppcheck may be suppressed using a file
  46. containing a list of suppression rules. The relative or absolute path to this
  47. file can be added to the build task as shown in the example below:
  48. bld.program(
  49. name='bar',
  50. src='foobar.c',
  51. cppcheck_suppress='bar.suppress'
  52. )
  53. A cppcheck suppress file should contain one suppress rule per line. Each of
  54. these rules will be passed as an '--suppress=<rule>' argument to cppcheck.
  55. Dependencies
  56. ================
  57. This waftool depends on the python pygments module, it is used for source code
  58. syntax highlighting when creating the html reports. see http://pygments.org/ for
  59. more information on this package.
  60. Remarks
  61. ================
  62. The generation of the html report is originally based on the cppcheck-htmlreport.py
  63. script that comes shipped with the cppcheck tool.
  64. """
  65. import os, sys
  66. import xml.etree.ElementTree as ElementTree
  67. from waflib import Task, TaskGen, Logs, Context
  68. PYGMENTS_EXC_MSG= '''
  69. The required module 'pygments' could not be found. Please install it using your
  70. platform package manager (e.g. apt-get or yum), using 'pip' or 'easy_install',
  71. see 'http://pygments.org/download/' for installation instructions.
  72. '''
  73. try:
  74. import pygments
  75. from pygments import formatters, lexers
  76. except ImportError as e:
  77. Logs.warn(PYGMENTS_EXC_MSG)
  78. raise e
  79. def options(opt):
  80. opt.add_option('--cppcheck-skip', dest='cppcheck_skip',
  81. default=False, action='store_true',
  82. help='do not check C/C++ sources (default=False)')
  83. opt.add_option('--cppcheck-err-resume', dest='cppcheck_err_resume',
  84. default=False, action='store_true',
  85. help='continue in case of errors (default=False)')
  86. opt.add_option('--cppcheck-bin-enable', dest='cppcheck_bin_enable',
  87. default='warning,performance,portability,style,unusedFunction', action='store',
  88. help="cppcheck option '--enable=' for binaries (default=warning,performance,portability,style,unusedFunction)")
  89. opt.add_option('--cppcheck-lib-enable', dest='cppcheck_lib_enable',
  90. default='warning,performance,portability,style', action='store',
  91. help="cppcheck option '--enable=' for libraries (default=warning,performance,portability,style)")
  92. opt.add_option('--cppcheck-std-c', dest='cppcheck_std_c',
  93. default='c99', action='store',
  94. help='cppcheck standard to use when checking C (default=c99)')
  95. opt.add_option('--cppcheck-std-cxx', dest='cppcheck_std_cxx',
  96. default='c++03', action='store',
  97. help='cppcheck standard to use when checking C++ (default=c++03)')
  98. opt.add_option('--cppcheck-check-config', dest='cppcheck_check_config',
  99. default=False, action='store_true',
  100. help='forced check for missing buildin include files, e.g. stdio.h (default=False)')
  101. opt.add_option('--cppcheck-max-configs', dest='cppcheck_max_configs',
  102. default='20', action='store',
  103. help='maximum preprocessor (--max-configs) define iterations (default=20)')
  104. def configure(conf):
  105. if conf.options.cppcheck_skip:
  106. conf.env.CPPCHECK_SKIP = [True]
  107. conf.env.CPPCHECK_STD_C = conf.options.cppcheck_std_c
  108. conf.env.CPPCHECK_STD_CXX = conf.options.cppcheck_std_cxx
  109. conf.env.CPPCHECK_MAX_CONFIGS = conf.options.cppcheck_max_configs
  110. conf.env.CPPCHECK_BIN_ENABLE = conf.options.cppcheck_bin_enable
  111. conf.env.CPPCHECK_LIB_ENABLE = conf.options.cppcheck_lib_enable
  112. conf.find_program('cppcheck', var='CPPCHECK')
  113. @TaskGen.feature('c')
  114. @TaskGen.feature('cxx')
  115. def cppcheck_execute(self):
  116. if len(self.env.CPPCHECK_SKIP) or self.bld.options.cppcheck_skip:
  117. return
  118. if getattr(self, 'cppcheck_skip', False):
  119. return
  120. task = self.create_task('cppcheck')
  121. task.cmd = _tgen_create_cmd(self)
  122. task.fatal = []
  123. if not self.bld.options.cppcheck_err_resume:
  124. task.fatal.append('error')
  125. def _tgen_create_cmd(self):
  126. features = getattr(self, 'features', [])
  127. std_c = self.env.CPPCHECK_STD_C
  128. std_cxx = self.env.CPPCHECK_STD_CXX
  129. max_configs = self.env.CPPCHECK_MAX_CONFIGS
  130. bin_enable = self.env.CPPCHECK_BIN_ENABLE
  131. lib_enable = self.env.CPPCHECK_LIB_ENABLE
  132. cmd = '%s' % self.env.CPPCHECK
  133. args = ['--inconclusive','--report-progress','--verbose','--xml','--xml-version=2']
  134. args.append('--max-configs=%s' % max_configs)
  135. if 'cxx' in features:
  136. args.append('--language=c++')
  137. args.append('--std=%s' % std_cxx)
  138. else:
  139. args.append('--language=c')
  140. args.append('--std=%s' % std_c)
  141. if self.bld.options.cppcheck_check_config:
  142. args.append('--check-config')
  143. if set(['cprogram','cxxprogram']) & set(features):
  144. args.append('--enable=%s' % bin_enable)
  145. else:
  146. args.append('--enable=%s' % lib_enable)
  147. for src in self.to_list(getattr(self, 'source', [])):
  148. args.append('%r' % src)
  149. for inc in self.to_incnodes(self.to_list(getattr(self, 'includes', []))):
  150. args.append('-I%r' % inc)
  151. for inc in self.to_incnodes(self.to_list(self.env.INCLUDES)):
  152. args.append('-I%r' % inc)
  153. return '%s %s' % (cmd, ' '.join(args))
  154. class cppcheck(Task.Task):
  155. quiet = True
  156. def run(self):
  157. stderr = self.generator.bld.cmd_and_log(self.cmd, quiet=Context.STDERR, output=Context.STDERR)
  158. self._save_xml_report(stderr)
  159. defects = self._get_defects(stderr)
  160. index = self._create_html_report(defects)
  161. self._errors_evaluate(defects, index)
  162. return 0
  163. def _save_xml_report(self, s):
  164. '''use cppcheck xml result string, add the command string used to invoke cppcheck
  165. and save as xml file.
  166. '''
  167. header = '%s\n' % s.splitlines()[0]
  168. root = ElementTree.fromstring(s)
  169. cmd = ElementTree.SubElement(root.find('cppcheck'), 'cmd')
  170. cmd.text = str(self.cmd)
  171. body = ElementTree.tostring(root)
  172. node = self.generator.path.get_bld().find_or_declare('cppcheck.xml')
  173. node.write(header + body)
  174. def _get_defects(self, xml_string):
  175. '''evaluate the xml string returned by cppcheck (on sdterr) and use it to create
  176. a list of defects.
  177. '''
  178. defects = []
  179. for error in ElementTree.fromstring(xml_string).iter('error'):
  180. defect = {}
  181. defect['id'] = error.get('id')
  182. defect['severity'] = error.get('severity')
  183. defect['msg'] = str(error.get('msg')).replace('<','&lt;')
  184. defect['verbose'] = error.get('verbose')
  185. for location in error.findall('location'):
  186. defect['file'] = location.get('file')
  187. defect['line'] = str(int(location.get('line')) - 1)
  188. defects.append(defect)
  189. return defects
  190. def _create_html_report(self, defects):
  191. files, css_style_defs = self._create_html_files(defects)
  192. index = self._create_html_index(files)
  193. self._create_css_file(css_style_defs)
  194. return index
  195. def _create_html_files(self, defects):
  196. sources = {}
  197. defects = [defect for defect in defects if defect.has_key('file')]
  198. for defect in defects:
  199. name = defect['file']
  200. if not sources.has_key(name):
  201. sources[name] = [defect]
  202. else:
  203. sources[name].append(defect)
  204. files = {}
  205. css_style_defs = None
  206. bpath = self.generator.path.get_bld().abspath()
  207. names = sources.keys()
  208. for i in range(0,len(names)):
  209. name = names[i]
  210. htmlfile = 'cppcheck/%i.html' % (i)
  211. errors = sources[name]
  212. files[name] = { 'htmlfile': '%s/%s' % (bpath, htmlfile), 'errors': errors }
  213. css_style_defs = self._create_html_file(name, htmlfile, errors)
  214. return files, css_style_defs
  215. def _create_html_file(self, sourcefile, htmlfile, errors):
  216. name = self.generator.get_name()
  217. root = ElementTree.fromstring(CPPCHECK_HTML_FILE)
  218. title = root.find('head/title')
  219. title.text = 'cppcheck - report - %s' % name
  220. body = root.find('body')
  221. for div in body.findall('div'):
  222. if div.get('id') == 'page':
  223. page = div
  224. break
  225. for div in page.findall('div'):
  226. if div.get('id') == 'header':
  227. h1 = div.find('h1')
  228. h1.text = 'cppcheck report - %s' % name
  229. if div.get('id') == 'content':
  230. content = div
  231. srcnode = self.generator.bld.root.find_node(sourcefile)
  232. hl_lines = [e['line'] for e in errors if e.has_key('line')]
  233. formatter = CppcheckHtmlFormatter(linenos=True, style='colorful', hl_lines=hl_lines, lineanchors='line')
  234. formatter.errors = [e for e in errors if e.has_key('line')]
  235. css_style_defs = formatter.get_style_defs('.highlight')
  236. lexer = pygments.lexers.guess_lexer_for_filename(sourcefile, "")
  237. s = pygments.highlight(srcnode.read(), lexer, formatter)
  238. table = ElementTree.fromstring(s)
  239. content.append(table)
  240. s = ElementTree.tostring(root, method='html')
  241. s = CCPCHECK_HTML_TYPE + s
  242. node = self.generator.path.get_bld().find_or_declare(htmlfile)
  243. node.write(s)
  244. return css_style_defs
  245. def _create_html_index(self, files):
  246. name = self.generator.get_name()
  247. root = ElementTree.fromstring(CPPCHECK_HTML_FILE)
  248. title = root.find('head/title')
  249. title.text = 'cppcheck - report - %s' % name
  250. body = root.find('body')
  251. for div in body.findall('div'):
  252. if div.get('id') == 'page':
  253. page = div
  254. break
  255. for div in page.findall('div'):
  256. if div.get('id') == 'header':
  257. h1 = div.find('h1')
  258. h1.text = 'cppcheck report - %s' % name
  259. if div.get('id') == 'content':
  260. content = div
  261. self._create_html_table(content, files)
  262. s = ElementTree.tostring(root, method='html')
  263. s = CCPCHECK_HTML_TYPE + s
  264. node = self.generator.path.get_bld().find_or_declare('cppcheck/index.html')
  265. node.write(s)
  266. return node
  267. def _create_html_table(self, content, files):
  268. table = ElementTree.fromstring(CPPCHECK_HTML_TABLE)
  269. for name, val in files.items():
  270. f = val['htmlfile']
  271. s = '<tr><td colspan="4"><a href="%s">%s</a></td></tr>\n' % (f,name)
  272. row = ElementTree.fromstring(s)
  273. table.append(row)
  274. errors = sorted(val['errors'], key=lambda e: int(e['line']) if e.has_key('line') else sys.maxint)
  275. for e in errors:
  276. if not e.has_key('line'):
  277. s = '<tr><td></td><td>%s</td><td>%s</td><td>%s</td></tr>\n' % (e['id'], e['severity'], e['msg'])
  278. else:
  279. attr = ''
  280. if e['severity'] == 'error':
  281. attr = 'class="error"'
  282. s = '<tr><td><a href="%s#line-%s">%s</a></td>' % (f, e['line'], e['line'])
  283. s+= '<td>%s</td><td>%s</td><td %s>%s</td></tr>\n' % (e['id'], e['severity'], attr, e['msg'])
  284. row = ElementTree.fromstring(s)
  285. table.append(row)
  286. content.append(table)
  287. def _create_css_file(self, css_style_defs):
  288. css = str(CPPCHECK_CSS_FILE)
  289. if css_style_defs:
  290. css = "%s\n%s\n" % (css, css_style_defs)
  291. node = self.generator.path.get_bld().find_or_declare('cppcheck/style.css')
  292. node.write(css)
  293. def _errors_evaluate(self, errors, http_index):
  294. name = self.generator.get_name()
  295. fatal = self.fatal
  296. severity = [err['severity'] for err in errors]
  297. problems = [err for err in errors if err['severity'] != 'information']
  298. if set(fatal) & set(severity):
  299. exc = "\n"
  300. exc += "\nccpcheck detected fatal error(s) in task '%s', see report for details:" % name
  301. exc += "\n file://%r" % (http_index)
  302. exc += "\n"
  303. self.generator.bld.fatal(exc)
  304. elif len(problems):
  305. msg = "\nccpcheck detected (possible) problem(s) in task '%s', see report for details:" % name
  306. msg += "\n file://%r" % http_index
  307. msg += "\n"
  308. Logs.error(msg)
  309. class CppcheckHtmlFormatter(pygments.formatters.HtmlFormatter):
  310. errors = []
  311. def wrap(self, source, outfile):
  312. line_no = 1
  313. for i, t in super(CppcheckHtmlFormatter, self).wrap(source, outfile):
  314. # If this is a source code line we want to add a span tag at the end.
  315. if i == 1:
  316. for error in self.errors:
  317. if int(error['line']) == line_no:
  318. t = t.replace('\n', CPPCHECK_HTML_ERROR % error['msg'])
  319. line_no = line_no + 1
  320. yield i, t
  321. CCPCHECK_HTML_TYPE = \
  322. '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n'
  323. CPPCHECK_HTML_FILE = """
  324. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" [<!ENTITY nbsp "&#160;">]>
  325. <html>
  326. <head>
  327. <title>cppcheck - report - XXX</title>
  328. <link href="style.css" rel="stylesheet" type="text/css" />
  329. <style type="text/css">
  330. </style>
  331. </head>
  332. <body class="body">
  333. <div id="page-header">&nbsp;</div>
  334. <div id="page">
  335. <div id="header">
  336. <h1>cppcheck report - XXX</h1>
  337. </div>
  338. <div id="menu">
  339. <a href="index.html">Defect list</a>
  340. </div>
  341. <div id="content">
  342. </div>
  343. <div id="footer">
  344. <div>cppcheck - a tool for static C/C++ code analysis</div>
  345. <div>
  346. Internet: <a href="http://cppcheck.sourceforge.net">http://cppcheck.sourceforge.net</a><br/>
  347. Forum: <a href="http://apps.sourceforge.net/phpbb/cppcheck/">http://apps.sourceforge.net/phpbb/cppcheck/</a><br/>
  348. IRC: #cppcheck at irc.freenode.net
  349. </div>
  350. &nbsp;
  351. </div>
  352. &nbsp;
  353. </div>
  354. <div id="page-footer">&nbsp;</div>
  355. </body>
  356. </html>
  357. """
  358. CPPCHECK_HTML_TABLE = """
  359. <table>
  360. <tr>
  361. <th>Line</th>
  362. <th>Id</th>
  363. <th>Severity</th>
  364. <th>Message</th>
  365. </tr>
  366. </table>
  367. """
  368. CPPCHECK_HTML_ERROR = \
  369. '<span style="background: #ffaaaa;padding: 3px;">&lt;--- %s</span>\n'
  370. CPPCHECK_CSS_FILE = """
  371. body.body {
  372. font-family: Arial;
  373. font-size: 13px;
  374. background-color: black;
  375. padding: 0px;
  376. margin: 0px;
  377. }
  378. .error {
  379. font-family: Arial;
  380. font-size: 13px;
  381. background-color: #ffb7b7;
  382. padding: 0px;
  383. margin: 0px;
  384. }
  385. th, td {
  386. min-width: 100px;
  387. text-align: left;
  388. }
  389. #page-header {
  390. clear: both;
  391. width: 1200px;
  392. margin: 20px auto 0px auto;
  393. height: 10px;
  394. border-bottom-width: 2px;
  395. border-bottom-style: solid;
  396. border-bottom-color: #aaaaaa;
  397. }
  398. #page {
  399. width: 1160px;
  400. margin: auto;
  401. border-left-width: 2px;
  402. border-left-style: solid;
  403. border-left-color: #aaaaaa;
  404. border-right-width: 2px;
  405. border-right-style: solid;
  406. border-right-color: #aaaaaa;
  407. background-color: White;
  408. padding: 20px;
  409. }
  410. #page-footer {
  411. clear: both;
  412. width: 1200px;
  413. margin: auto;
  414. height: 10px;
  415. border-top-width: 2px;
  416. border-top-style: solid;
  417. border-top-color: #aaaaaa;
  418. }
  419. #header {
  420. width: 100%;
  421. height: 70px;
  422. background-image: url(logo.png);
  423. background-repeat: no-repeat;
  424. background-position: left top;
  425. border-bottom-style: solid;
  426. border-bottom-width: thin;
  427. border-bottom-color: #aaaaaa;
  428. }
  429. #menu {
  430. margin-top: 5px;
  431. text-align: left;
  432. float: left;
  433. width: 100px;
  434. height: 300px;
  435. }
  436. #menu > a {
  437. margin-left: 10px;
  438. display: block;
  439. }
  440. #content {
  441. float: left;
  442. width: 1020px;
  443. margin: 5px;
  444. padding: 0px 10px 10px 10px;
  445. border-left-style: solid;
  446. border-left-width: thin;
  447. border-left-color: #aaaaaa;
  448. }
  449. #footer {
  450. padding-bottom: 5px;
  451. padding-top: 5px;
  452. border-top-style: solid;
  453. border-top-width: thin;
  454. border-top-color: #aaaaaa;
  455. clear: both;
  456. font-size: 10px;
  457. }
  458. #footer > div {
  459. float: left;
  460. width: 33%;
  461. }
  462. """