diff options
author | Philip Chimento <philip.chimento@gmail.com> | 2019-12-15 22:12:30 -0500 |
---|---|---|
committer | Philip Chimento <philip.chimento@gmail.com> | 2019-12-19 18:13:23 -0500 |
commit | 010c21763220d69d107668708a81eb85eb927be9 (patch) | |
tree | f0088de6fc673eb811a3a864b769c6f09ac0b360 | |
parent | 53dfdbe970966966b208a034c91f9fb43bf7596b (diff) |
scripts: Add tartan-json, a script for processing compile_commands.json
This script runs all the compilations listed in a compile_commands.json
file (generated by Meson and CMake) under the Tartan analyzer.
Use this script to analyze a whole project by passing it the compilation
database file generated by Meson or CMake. An example with Meson would
be:
$ meson _build
$ tartan-json _build/compile_commands.json
See: #39
-rw-r--r-- | Makefile.am | 1 | ||||
-rwxr-xr-x | scripts/tartan-json | 67 |
2 files changed, 68 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am index 764ca8c..319e3d7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -74,6 +74,7 @@ clang_plugin_libtartan_la_LDFLAGS = \ dist_bin_SCRIPTS = \ scripts/tartan \ scripts/tartan-build \ + scripts/tartan-json \ $(NULL) # Code coverage diff --git a/scripts/tartan-json b/scripts/tartan-json new file mode 100755 index 0000000..560413a --- /dev/null +++ b/scripts/tartan-json @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +# coding: utf8 +# +# This file is part of Tartan. +# Copyright © 2019 Philip Chimento +# +# Tartan is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Tartan is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Tartan. If not, see <http://www.gnu.org/licenses/>. + +# Use this script to analyze a whole project by passing it the compilation +# database file generated by Meson or CMake. An example with Meson would be: +# +# $ meson _build +# $ tartan-json _build/compile_commands.json + +import argparse +import json +import os +import shlex +import subprocess +import sys + +parser = argparse.ArgumentParser( + description='''Run the Tartan analyzer on a whole compilation database, in +the compile_commands.json format such as generated by Meson or CMake.''') +parser.add_argument('json', type=argparse.FileType('r'), metavar='FILE', + help='path to compile_commands.json') +parser.add_argument('-q', '--quiet', action='store_true', + help="don't print progress messages") +parser.add_argument('tartan_options', nargs=argparse.REMAINDER, metavar='...', + help='extra options to pass on to Tartan') + +args = parser.parse_args() + +compile_db = json.load(args.json) +had_error = False + +for index, entry in enumerate(compile_db): + if not args.quiet: + full_path = os.path.normpath(os.path.join(entry['directory'], + entry['file'])) + print('[{}/{}] Processing {}'.format(index + 1, len(compile_db), + full_path)) + + new_env = dict(os.environ) + existing_tartan_options = new_env.get('TARTAN_OPTIONS', '') + new_env['TARTAN_OPTIONS'] = (existing_tartan_options + ' ' + + ' '.join(args.tartan_options)) + # -Wno-unused-command-line-argument is because linker arguments will be + # ignored and produce a warning with --analyze. + invocation = (['tartan', '--analyze', '-Wno-unused-command-line-argument'] + + shlex.split(entry['command'])) + result = subprocess.run(invocation, cwd=entry['directory'], env=new_env) + if result.returncode != 0: + had_error = True + +sys.exit(1 if had_error else 0) |