Skip to content

Commit

Permalink
remove ebpf clang pre-commit (#27645)
Browse files Browse the repository at this point in the history
  • Loading branch information
brycekahle authored Jul 18, 2024
1 parent 81bf3b2 commit 1296242
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 192 deletions.
42 changes: 0 additions & 42 deletions .clang-tidy

This file was deleted.

9 changes: 0 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,6 @@ repos:
language: system
types_or: [c, c++, c#]
pass_filenames: false
- id: clang-format
name: clang-format
description: clang-format
entry: 'inv pre-commit.check-clang-format'
language: system
require_serial: true
pass_filenames: false
files: '^pkg/(ebpf|network|security)/.*\.(c|h)$'
exclude: '^pkg/ebpf/(c/bpf_endian|c/bpf_helpers|compiler/clang-stdarg).h$'
- id: go-mod-tidy
name: go-mod-tidy
description: check that all go.mod files are tidy
Expand Down
22 changes: 0 additions & 22 deletions tasks/pre_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

from tasks.libs.common.color import color_message
from tasks.libs.common.git import get_staged_files
from tasks.tools import clang_format
from tasks.tools.clang_format import ExitStatus

DEFAULT_PRE_COMMIT_CONFIG = ".pre-commit-config.yaml"
DEVA_PRE_COMMIT_CONFIG = ".pre-commit-config-deva.yaml"
Expand Down Expand Up @@ -72,23 +70,3 @@ def check_set_x(ctx):
print(error, file=sys.stderr)
print(color_message('error:', 'red'), 'No shell script should use "set -x"', file=sys.stderr)
raise Exit(code=1)


@task
def check_clang_format(ctx):
files = [
file
for file in get_staged_files(ctx)
if (
re.match(r"^pkg/(ebpf|network|security)/.*\.(c|h)$", file)
and not re.match(
"^pkg/ebpf/(c/bpf_endian|c/bpf_helpers|compiler/clang-stdarg).h$",
file,
)
)
]

if files:
res = clang_format.run(files)
if res != ExitStatus.SUCCESS:
raise Exit(code=res)
119 changes: 0 additions & 119 deletions tasks/system_probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,125 +963,6 @@ def kitchen_prepare(ctx, kernel_release=None, ci=False, packages=""):
ctx.run(f"echo {get_commit_sha(ctx)} > {BUILD_COMMIT}")


@task
def clang_format(ctx, targets=None, fix=False, fail_on_issue=False):
"""
Format C code using clang-format
"""
ctx.run("which clang-format")
if isinstance(targets, str):
# when this function is called from the command line, targets are passed
# as comma separated tokens in a string
targets = targets.split(',')

if not targets:
targets = get_ebpf_targets()

# remove externally maintained files
ignored_files = [
"pkg/ebpf/c/bpf_builtins.h",
"pkg/ebpf/c/bpf_core_read.h",
"pkg/ebpf/c/bpf_cross_compile.h",
"pkg/ebpf/c/bpf_endian.h",
"pkg/ebpf/c/bpf_helpers.h",
"pkg/ebpf/c/bpf_helper_defs.h",
"pkg/ebpf/c/bpf_tracing.h",
"pkg/ebpf/c/bpf_tracing_custom.h",
"pkg/ebpf/c/compiler.h",
"pkg/ebpf/c/map-defs.h",
"pkg/ebpf/c/vmlinux_5_15_0.h",
"pkg/ebpf/c/vmlinux_5_15_0_arm.h",
"pkg/ebpf/compiler/clang-stdarg.h",
]
for f in ignored_files:
if f in targets:
targets.remove(f)

fmt_cmd = "clang-format -i --style=file --fallback-style=none"
if not fix:
fmt_cmd = fmt_cmd + " --dry-run"
if fail_on_issue:
fmt_cmd = fmt_cmd + " --Werror"

ctx.run(f"{fmt_cmd} {' '.join(targets)}")


@task
def clang_tidy(ctx, fix=False, fail_on_issue=False, kernel_release=None):
"""
Lint C code using clang-tidy
"""

print("checking for clang-tidy executable...")
ctx.run("which clang-tidy")

build_flags = get_ebpf_build_flags()
build_flags.append("-DDEBUG=1")
build_flags.append("-emit-llvm")
build_flags.extend(get_kernel_headers_flags(kernel_release=kernel_release))

bpf_dir = os.path.join(".", "pkg", "ebpf")
base_files = glob.glob(f"{bpf_dir}/c/**/*.c")

network_c_dir = os.path.join(".", "pkg", "network", "ebpf", "c")
network_files = list(base_files)
network_files.extend(glob.glob(f"{network_c_dir}/**/*.c"))
network_flags = list(build_flags)
network_flags.append(f"-I{network_c_dir}")
network_flags.append(f"-I{os.path.join(network_c_dir, 'prebuilt')}")
network_flags.append(f"-I{os.path.join(network_c_dir, 'runtime')}")
network_checks = [
"-readability-function-cognitive-complexity",
"-readability-isolate-declaration",
"-clang-analyzer-security.insecureAPI.bcmp",
]
run_tidy(
ctx,
files=network_files,
build_flags=network_flags,
fix=fix,
fail_on_issue=fail_on_issue,
checks=network_checks,
)

security_agent_c_dir = os.path.join(".", "pkg", "security", "ebpf", "c")
security_files = list(base_files)
security_files.extend(glob.glob(f"{security_agent_c_dir}/**/*.c"))
security_flags = list(build_flags)
security_flags.append(f"-I{security_agent_c_dir}")
security_flags.append(f"-I{security_agent_c_dir}/include")
security_flags.append("-DUSE_SYSCALL_WRAPPER=0")
security_checks = ["-readability-function-cognitive-complexity", "-readability-isolate-declaration"]
run_tidy(
ctx,
files=security_files,
build_flags=security_flags,
fix=fix,
fail_on_issue=fail_on_issue,
checks=security_checks,
)


def run_tidy(ctx, files, build_flags, fix=False, fail_on_issue=False, checks=None):
flags = ["--quiet"]
if fix:
flags.append("--fix")
if fail_on_issue:
flags.append("--warnings-as-errors='*'")

if checks is not None:
flags.append(f"--checks={','.join(checks)}")

ctx.run(f"clang-tidy {' '.join(flags)} {' '.join(files)} -- {' '.join(build_flags)}", warn=True)


def get_ebpf_targets():
files = glob.glob("pkg/ebpf/c/*.[c,h]")
files.extend(glob.glob("pkg/network/ebpf/c/**/*.[c,h]", recursive=True))
files.extend(glob.glob("pkg/security/ebpf/c/**/*.[c,h]", recursive=True))
return files


def get_kernel_arch() -> Arch:
# Mapping used by the kernel, from https://elixir.bootlin.com/linux/latest/source/scripts/subarch.include
kernel_arch = (
Expand Down

0 comments on commit 1296242

Please sign in to comment.