LinuxCommandLibrary

clang-check

Check C/C++/Objective-C source code

TLDR

Run default checks on a source file

$ clang-check [path/to/file.cpp] --
copy

Dump the Abstract Syntax Tree for debugging
$ clang-check [path/to/file.cpp] -ast-dump --
copy

Filter AST by Name
$ clang-check [path/to/file.cpp] -ast-dump -ast-dump-filter FunctionName
copy

Pretty-Print AST
$ clang-check [path/to/file.cpp] -ast-print --
copy

SYNOPSIS

clang-check [options] <source_file> […]

PARAMETERS

-analyze
    Enable static analysis instead of syntax checking

-p path
    Path to compilation database or output prefix

-extra-arg=arg
    Additional argument passed to underlying Clang invocation

-extra-arg-before=arg
    Additional argument prepended to Clang command line

-ast-dump
    Dump the Abstract Syntax Tree (AST)

-ast-dump-filter=pattern
    Dump AST nodes matching pattern

-fixit file
    Apply fix-it hints to specified file

-fix-what-you-can
    Apply all fix-its

-view=viewer
    Use viewer for diagnostics (e.g., 'xcode', 'vi')

-verify-prefix=prefix
    Prefix for expected diagnostics in verification mode

DESCRIPTION

clang-check is a command-line tool from the Clang/LLVM project designed to analyze C, C++, and Objective-C source files for syntax errors, warnings, and static analysis issues. It leverages libclang, Clang's stable C interface, to parse and diagnose code without full compilation. Primarily used for quick checks in editors or CI pipelines, it supports integration with compile_commands.json databases for accurate preprocessing.

Key features include syntax-only checking (default), static analysis via -analyze, AST dumping, and fix-it suggestions. It's lightweight and fast, making it ideal for on-the-fly validation during development. Unlike full compilers, it focuses on diagnostics rather than generating object code.

Usage typically involves specifying a source file and optional build path. It outputs errors, warnings, and notes in a compiler-like format, with support for viewing diagnostics in tools like Xcode or Vim plugins.

CAVEATS

Deprecated in favor of more advanced tools like clang-tidy; limited to libclang capabilities; requires Clang installation.

EXAMPLES

Basic syntax check: clang-check foo.c
Static analysis: clang-check -analyze -p build/ foo.c
With compile DB: clang-check -p build/ src/*.cpp

DEPENDENCIES

Requires LLVM/Clang development package (e.g., clang-tools on Debian/Ubuntu); compile_commands.json recommended for complex projects.

HISTORY

Introduced in Clang 3.4 as part of clang-tools-extra; evolved with LLVM releases for better libclang integration; usage peaked with editor plugins but declined with clang-tidy's rise since LLVM 3.8.

SEE ALSO

clang(1), clang-tidy(1), clang-analyze(1), libclang(3)

Copied to clipboard