From 485c3041d090290fa6ce8bbdded43143526b9f8b Mon Sep 17 00:00:00 2001 From: Ilya Shchepetkov Date: Mon, 25 Jan 2021 17:46:41 +0300 Subject: [PATCH] Add ability to filter options in compile_commands.json --- clade/extensions/cdb.py | 12 ++++++++++-- clade/extensions/opts.py | 2 +- clade/extensions/presets/presets.json | 1 + clade/scripts/compilation_database.py | 8 ++++++++ tests/test_cdb.py | 9 +++++++-- tests/test_project/Makefile | 2 +- 6 files changed, 28 insertions(+), 6 deletions(-) diff --git a/clade/extensions/cdb.py b/clade/extensions/cdb.py index d6065e4..f7c7f59 100644 --- a/clade/extensions/cdb.py +++ b/clade/extensions/cdb.py @@ -16,10 +16,11 @@ import os from clade.extensions.abstract import Extension +from clade.extensions.opts import filter_opts class CDB(Extension): - requires = ["SrcGraph"] + requires = ["SrcGraph", "Storage"] __version__ = "1" @@ -43,7 +44,14 @@ def parse(self, cmds_file): for cmd in cmds: for i, cmd_in in enumerate(cmd["in"]): - arguments = [cmd["command"][0]] + cmd["opts"] + [cmd_in] + if self.conf.get("CDB.filter_opts", False): + opts = filter_opts( + cmd["opts"], self.extensions["Storage"].get_storage_path + ) + else: + opts = cmd["opts"] + + arguments = [cmd["command"][0]] + opts + [cmd_in] if cmd["out"]: if "-c" in cmd["opts"]: arguments.extend(["-o", cmd["out"][i]]) diff --git a/clade/extensions/opts.py b/clade/extensions/opts.py index 17b7587..4c33800 100644 --- a/clade/extensions/opts.py +++ b/clade/extensions/opts.py @@ -513,7 +513,7 @@ ] cif_supported_opts = ( - ["-D", "-U", "-nostdinc", "-fshort-wchar", "-std", "--std"] + ["-D", "-U", "-nostdinc", "-fshort-wchar", "-std", "--std", "-c"] + ["{}$".format(opt) for opt in gcc_optimization_opts] + cif_include_opts ) diff --git a/clade/extensions/presets/presets.json b/clade/extensions/presets/presets.json index 53e01c4..be94101 100644 --- a/clade/extensions/presets/presets.json +++ b/clade/extensions/presets/presets.json @@ -43,6 +43,7 @@ "Info.aspectator": null, "PidGraph.as_picture": false, "PidGraph.filter_cmds_by_pid": true, + "CDB.filter_opts": false, "AR.which_list": [ "(/|-)ar$" ], diff --git a/clade/scripts/compilation_database.py b/clade/scripts/compilation_database.py index 5717f46..a967588 100644 --- a/clade/scripts/compilation_database.py +++ b/clade/scripts/compilation_database.py @@ -57,6 +57,13 @@ def parse_args(args, work_dir): "--cmds", help="path to the file with intercepted commands", ) + parser.add_argument( + "-f", + "--filter", + help="filter irrelevant options", + action="store_true", + default=False, + ) parser.add_argument( dest="command", nargs=argparse.REMAINDER, help="build command to run" ) @@ -86,6 +93,7 @@ def prepare_conf(args): conf["log_level"] = "ERROR" conf["preset"] = args.preset conf["CDB.output"] = os.path.abspath(args.output) + conf["CDB.filter_opts"] = args.filter return conf diff --git a/tests/test_cdb.py b/tests/test_cdb.py index fe3fed6..cf7d668 100644 --- a/tests/test_cdb.py +++ b/tests/test_cdb.py @@ -14,15 +14,17 @@ # limitations under the License. import os +import pytest from clade import Clade from clade.scripts.compilation_database import main -def test_cdb(tmpdir, cmds_file): +@pytest.mark.parametrize("filter_opts", [True, False]) +def test_cdb(tmpdir, cmds_file, filter_opts): cdb_json = os.path.join(str(tmpdir), "cdb.json") - c = Clade(tmpdir, cmds_file, conf={"CDB.output": cdb_json}) + c = Clade(tmpdir, cmds_file, conf={"CDB.output": cdb_json, "CDB.filter_opts": filter_opts}) e = c.parse("CDB") cdb = e.load_cdb() @@ -39,6 +41,9 @@ def test_cdb(tmpdir, cmds_file): for arg in cmd["arguments"]: assert isinstance(arg, str) + if filter_opts: + assert "-fsyntax-only" not in cmd["arguments"] + def test_cdb_main(tmpdir, cmds_file): main(["-o", os.path.join(str(tmpdir), "cdb.json"), "--cmds", cmds_file]) diff --git a/tests/test_project/Makefile b/tests/test_project/Makefile index 61361fe..8e89812 100644 --- a/tests/test_project/Makefile +++ b/tests/test_project/Makefile @@ -1,7 +1,7 @@ all: gcc zero.c main.c -o tmp_main -D TEST_MACRO -O3 mv tmp_main main - gcc zero.c main.c -o /dev/null + gcc zero.c main.c -o /dev/null -fsyntax-only gcc zero.c -M -MF zero.txt -clang -cc1 /dev/null gcc -c zero.c main.c