Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
description: Automatically install any specific version of clang-format and format C/C++ code
entry: clang-format-hook
language: python
files: \.(h\+\+|h|hh|hxx|hpp|c|cc|cpp|c\+\+|cxx)$
types_or: [c++, c]
require_serial: false

- id: clang-tidy
name: clang-tidy
description: Automatically install any specific version of clang-tidy and diagnose/fix typical programming errors
entry: clang-tidy-hook
language: python
files: \.(h\+\+|h|hh|hxx|hpp|c|cc|cpp|c\+\+|cxx)$
types_or: [c++, c]
require_serial: false
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ repos:
args: [--checks=.clang-tidy] # Loads checks from .clang-tidy file
```

> [!TIP]
> Install the latest version of `clang-format` and `clang-tidy` if not specified. You can specify the version using the `--version` argument in the `args` list as shown below.

### Custom Clang Tool Version

To use specific versions of clang-format and clang-tidy (using Python wheel packages):
Expand Down
4 changes: 2 additions & 2 deletions cpp_linter_hooks/clang_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

def run_clang_format(args=None) -> Tuple[int, str]:
hook_args, other_args = parser.parse_known_args(args)
tool_name = ensure_installed("clang-format", hook_args.version)
command = [tool_name, "-i"]
ensure_installed("clang-format", hook_args.version)
command = ["clang-format", "-i"]

# Add verbose flag if requested
if hook_args.verbose:
Expand Down
5 changes: 2 additions & 3 deletions cpp_linter_hooks/clang_tidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@

def run_clang_tidy(args=None) -> Tuple[int, str]:
hook_args, other_args = parser.parse_known_args(args)
tool_name = ensure_installed("clang-tidy", hook_args.version)
command = [tool_name]
command.extend(other_args)
ensure_installed("clang-tidy", hook_args.version)
command = ["clang-tidy"] + other_args

retval = 0
output = ""
Expand Down
12 changes: 1 addition & 11 deletions cpp_linter_hooks/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,6 @@ def _resolve_install(tool: str, version: Optional[str]) -> Optional[Path]:
else DEFAULT_CLANG_TIDY_VERSION
)

# Additional safety check in case DEFAULT versions are None
if user_version is None:
user_version = (
DEFAULT_CLANG_FORMAT_VERSION
if tool == "clang-format"
else DEFAULT_CLANG_TIDY_VERSION
)

path = shutil.which(tool)
if path:
runtime_version = _get_runtime_version(tool)
Expand All @@ -219,12 +211,10 @@ def is_installed(tool: str) -> Optional[Path]:
return None


def ensure_installed(tool: str, version: Optional[str] = None) -> str:
def ensure_installed(tool: str, version: Optional[str] = None) -> None:
"""Ensure a tool is installed, resolving its version if necessary."""
LOG.info("Ensuring %s is installed", tool)
tool_path = _resolve_install(tool, version)
if tool_path:
LOG.info("%s available at %s", tool, tool_path)
return tool
LOG.warning("%s not found and could not be installed", tool)
return tool
Loading