@@ -343,6 +343,107 @@ An overview of all the command-line options:
343343 some-check.SomeOption: 'some value'
344344 ...
345345
346+ Clang-Tidy Automation
347+ =====================
348+
349+ :program: `clang-tidy ` can analyze multiple source files by specifying them on
350+ the command line. For larger projects, automation scripts provide additional
351+ functionality like parallel execution and integration with version control
352+ systems.
353+
354+ Running Clang-Tidy in Parallel
355+ -------------------------------
356+
357+ :program: `clang-tidy ` can process multiple files sequentially, but for projects
358+ with many source files, the :program: `run-clang-tidy.py ` script provides
359+ parallel execution to significantly reduce analysis time. This script is
360+ included with clang-tidy and runs :program: `clang-tidy ` over all files in a
361+ compilation database or a specified path concurrently.
362+
363+ The script requires a compilation database (``compile_commands.json ``) which
364+ can be generated by build systems like CMake (using
365+ ``-DCMAKE_EXPORT_COMPILE_COMMANDS=ON ``) or by tools like `Bear `_.
366+
367+ The script supports most of the same options as :program: `clang-tidy ` itself,
368+ including ``-checks= ``, ``-fix ``, ``-header-filter= ``, and configuration
369+ options. Run ``run-clang-tidy.py --help `` for a complete list of available
370+ options.
371+
372+ Example invocations:
373+
374+ .. code-block :: console
375+
376+ # Run clang-tidy on all files in the compilation database in parallel
377+ $ run-clang-tidy.py -p=build/
378+
379+ # Run with specific checks and apply fixes
380+ $ run-clang-tidy.py -p=build/ -fix -checks=-*,readability-*
381+
382+ # Run on specific files/directories with header filtering
383+ $ run-clang-tidy.py -p=build/ -header-filter=src/ src/
384+
385+ # Run with parallel execution (uses all CPU cores by default)
386+ $ run-clang-tidy.py -p=build/ -j 4
387+
388+ Running Clang-Tidy on Diff
389+ ---------------------------
390+
391+ The :program: `clang-tidy-diff.py ` script allows you to run
392+ :program: `clang-tidy ` on the lines that have been modified in your working
393+ directory or in a specific diff. Importantly, :program: `clang-tidy-diff.py ` only reports
394+ diagnostics for changed lines; :program: `clang-tidy ` still analyzes the entire
395+ file and filters out unchanged lines after analysis, so this does not improve
396+ performance. This is particularly useful for code reviews and continuous
397+ integration, as it focuses analysis on the changed code rather than the entire
398+ codebase.
399+
400+ The script can work with various diff sources:
401+
402+ * Git working directory changes
403+ * Output from ``git diff ``
404+ * Output from ``svn diff ``
405+ * Patch files
406+
407+ Example invocations:
408+
409+ .. code-block :: console
410+
411+ # Run clang-tidy on all changes in the working directory
412+ $ git diff -U0 --no-color HEAD^ | clang-tidy-diff.py -p1
413+
414+ # Run with specific checks and apply fixes
415+ $ git diff -U0 --no-color HEAD^ | clang-tidy-diff.py -p1 -fix \
416+ -checks=-*,readability-*
417+
418+ # Run on staged changes
419+ $ git diff -U0 --no-color --cached | clang-tidy-diff.py -p1
420+
421+ # Run on changes between two commits
422+ $ git diff -U0 --no-color HEAD~2 HEAD | clang-tidy-diff.py -p1
423+
424+ # Run on a patch file
425+ $ clang-tidy-diff.py -p1 < changes.patch
426+
427+ The ``-p1 `` option tells the script to strip one level of path prefix from
428+ the diff, which is typically needed for Git diffs. The script supports most of
429+ the same options as :program: `clang-tidy ` itself, including ``-checks= ``,
430+ ``-fix ``, ``-header-filter= ``, and configuration options.
431+
432+ While :program: `clang-tidy-diff.py ` is useful for focusing on recent changes,
433+ relying solely on it may lead to incomplete analysis. Since the script only
434+ reports warnings from the modified lines, it may miss issues that are caused
435+ by the changes but manifest elsewhere in the code. For example, changes that
436+ only add lines to a function may cause it to violate size limits (e.g.,
437+ `readability-function-size <checks/readability/function-size.html >`_), but the
438+ diagnostic will be reported at the function declaration, which may not be in
439+ the diff and thus filtered out. Modifications to header files may also affect
440+ many implementation files, but only warnings in the modified header lines will
441+ be reported.
442+
443+ For comprehensive analysis, especially before merging significant changes,
444+ consider running :program: `clang-tidy ` on the entire affected files or the
445+ whole project using :program: `run-clang-tidy.py `.
446+
346447.. _clang-tidy-nolint :
347448
348449Suppressing Undesired Diagnostics
@@ -465,5 +566,6 @@ example, ``NOLINTBEGIN(check-name)`` can be paired with
465566:program: `clang-tidy ` will generate a ``clang-tidy-nolint `` error diagnostic if
466567any ``NOLINTBEGIN ``/``NOLINTEND `` comment violates these requirements.
467568
569+ .. _Bear : https://github.com/rizsotto/Bear
468570.. _LibTooling : https://clang.llvm.org/docs/LibTooling.html
469571.. _How To Setup Tooling For LLVM : https://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
0 commit comments