Skip to content

Commit

Permalink
Add ability to filter options in compile_commands.json
Browse files Browse the repository at this point in the history
  • Loading branch information
17451k committed Jan 25, 2021
1 parent 9083fca commit 485c304
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
12 changes: 10 additions & 2 deletions clade/extensions/cdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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]])
Expand Down
2 changes: 1 addition & 1 deletion clade/extensions/opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
1 change: 1 addition & 0 deletions clade/extensions/presets/presets.json
Original file line number Diff line number Diff line change
Expand Up @@ -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$"
],
Expand Down
8 changes: 8 additions & 0 deletions clade/scripts/compilation_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions tests/test_cdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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])
2 changes: 1 addition & 1 deletion tests/test_project/Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 485c304

Please sign in to comment.