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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
#!/usr/bin/env python3
# Copyright (c) Meta Platforms, Inc. and affiliates.
# SPDX-License-Identifier: LGPL-2.1-or-later
import argparse
import json
import os
import os.path
import re
import subprocess
import sys
import sysconfig
import tempfile
import yaml
BUILD_BASE = "build/compile_commands"
CDB = BUILD_BASE + "/compile_commands.json"
IWYU_REGEXES = [
("add", r"(.*) should add these lines:"),
("remove", r"(.*) should remove these lines:"),
("include_list", r"The full include-list for (.*):"),
("none", r"---"),
("none", r"\(.* has correct #includes/fwd-decls\)"),
]
# Python.h is the canonical header for the Python C API. The actual definitions
# come from internal header files, so we need an IWYU mapping file. Ideally we
# could do this with include mappings. Unfortunately, Python.h uses ""-style
# includes for those headers, one of which is "object.h". This conflicts with
# libdrgn's "object.h", and IWYU doesn't seem to have a way to distinguish
# between those in the mapping file. So, we generate symbol mappings with the
# find-all-symbols Clang tool.
def gen_python_mapping_file(mapping_path):
# These headers are guaranteed to be included by Python.h. See
# https://docs.python.org/3/c-api/intro.html#include-files.
IMPLIED_HEADERS = (
"<assert.h>",
"<errno.h>",
"<limits.h>",
"<stdio.h>",
"<stdlib.h>",
"<string.h>",
)
include = sysconfig.get_path("include")
platinclude = sysconfig.get_path("platinclude")
with open(
mapping_path + ".tmp", "w"
) as imp, tempfile.TemporaryDirectory() as tmpdir:
imp.write("[\n")
for header in IMPLIED_HEADERS:
imp.write(
f' {{"include": ["{header}", "public", "<Python.h>", "public"]}},\n'
)
build_dir = os.path.join(tmpdir, "build")
os.mkdir(build_dir)
source = os.path.join(build_dir, "python.c")
with open(source, "w") as f:
f.write("#include <Python.h>")
commands = [
{
"arguments": [
"clang",
"-I",
include,
"-I",
platinclude,
"-c",
"python.c",
],
"directory": build_dir,
"file": "python.c",
}
]
with open(os.path.join(build_dir, "compile_commands.json"), "w") as f:
json.dump(commands, f)
symbols_dir = os.path.join(tmpdir, "find_all_symbols")
os.mkdir(symbols_dir)
subprocess.check_call(
[
"find-all-symbols",
"-p=" + build_dir,
"--output-dir=" + symbols_dir,
source,
]
)
find_all_symbols_db = os.path.join(tmpdir, "find_all_symbols_db.yaml")
subprocess.check_call(
[
"find-all-symbols",
"-p=" + build_dir,
"--merge-dir=" + symbols_dir,
find_all_symbols_db,
]
)
with open(find_all_symbols_db, "r") as f:
for document in yaml.safe_load_all(f):
name = document["Name"]
path = document["FilePath"]
if path.startswith(include + "/"):
header = path[len(include) + 1 :]
elif path.startswith(platinclude + "/"):
header = path[len(platinclude) + 1 :]
else:
continue
if header == "pyconfig.h":
# Probably best not to use these.
continue
imp.write(
f' {{"symbol": ["{name}", "private", "<Python.h>", "public"]}}, # From {header}\n'
)
# "cpython/object.h" defines struct _typeobject { ... } PyTypeObject.
# For some reason, include-what-you-mean wants struct _typeobject, but
# find-all-symbols only reports PyTypeObject. Add it manually.
imp.write(
' {"symbol": ["_typeobject", "private", "<Python.h>", "public"]}, # From cpython/object.h\n'
)
imp.write("]\n")
os.rename(mapping_path + ".tmp", mapping_path)
def main():
parser = argparse.ArgumentParser(description="run include-what-you-use on drgn")
parser.add_argument(
"source", nargs="*", help="run on given file instead of all source files"
)
args = parser.parse_args()
if args.source:
sources = {os.path.realpath(source) for source in args.source}
os.makedirs(BUILD_BASE, exist_ok=True)
subprocess.check_call(
[
"bear",
"--output",
CDB,
"--append",
"--",
sys.executable,
"setup.py",
"build",
"-b",
BUILD_BASE,
"build_ext",
]
)
python_mapping_file = os.path.join(
BUILD_BASE,
f"python.{sysconfig.get_platform()}.{sysconfig.get_python_version()}.imp",
)
if not os.path.exists(python_mapping_file):
gen_python_mapping_file(python_mapping_file)
with open(CDB, "r") as f:
commands = json.load(f)
for command in commands:
if (
args.source
and os.path.realpath(os.path.join(command["directory"], command["file"]))
not in sources
):
continue
with subprocess.Popen(
["include-what-you-use"]
+ command["arguments"][1:]
+ [
"-Xiwyu",
"--mapping_file=" + os.path.abspath(python_mapping_file),
"-w", # We don't want warnings from Clang.
],
cwd=command["directory"],
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
) as proc:
state = "none"
header = None
lines = []
for line in proc.stdout:
line = line.rstrip("\n")
match = None
for new_state, regex in IWYU_REGEXES:
match = re.fullmatch(regex, line)
if match:
break
if match:
state = new_state
if state != "none":
path = os.path.relpath(
os.path.join(command["directory"], match.group(1))
)
if state in ("add", "remove"):
header = f"{path} should {state} these lines:"
else:
header = None
lines.clear()
elif line and state != "include_list":
if header is not None:
print("\n" + header)
header = None
print(line)
if __name__ == "__main__":
main()
|