<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aditya Goyal</title>
    <description>The latest articles on DEV Community by Aditya Goyal (@aditya_goyal_4b3b6ea326c0).</description>
    <link>https://dev.to/aditya_goyal_4b3b6ea326c0</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3844822%2F8e338b18-36fc-444f-9564-a44f45562e81.jpg</url>
      <title>DEV Community: Aditya Goyal</title>
      <link>https://dev.to/aditya_goyal_4b3b6ea326c0</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aditya_goyal_4b3b6ea326c0"/>
    <language>en</language>
    <item>
      <title>clang-format-inc: Format Only the Lines You Changed in C++</title>
      <dc:creator>Aditya Goyal</dc:creator>
      <pubDate>Thu, 26 Mar 2026 13:48:51 +0000</pubDate>
      <link>https://dev.to/aditya_goyal_4b3b6ea326c0/clang-format-inc-format-only-the-lines-you-changed-in-c-3kg</link>
      <guid>https://dev.to/aditya_goyal_4b3b6ea326c0/clang-format-inc-format-only-the-lines-you-changed-in-c-3kg</guid>
      <description>&lt;p&gt;If you've ever tried enforcing clang-format on a large legacy C++ codebase, you've hit this problem:&lt;/p&gt;

&lt;p&gt;mirrors-clang-format reformats entire files — your PR becomes 80% formatting noise, 20% actual change&lt;br&gt;
git clang-format --staged has the right idea, but breaks in CI — the staging area is empty when pre-commit runs with --from-ref/--to-ref&lt;br&gt;
So you either reformat everything (noisy) or nothing (useless).&lt;/p&gt;

&lt;p&gt;The fix: format only changed lines&lt;br&gt;
darker solved this problem for Python/Black years ago. The idea is simple: diff the current changes, extract the added-line ranges, and only run the formatter on those ranges.&lt;/p&gt;

&lt;p&gt;I built the same thing for C++: clang-format-inc.&lt;/p&gt;

&lt;p&gt;How it works&lt;/p&gt;

&lt;p&gt;git diff -U0 --cached -- &lt;br&gt;
  └─ parse added-line ranges: { "foo.cpp": [(5, 8), (20, 22)] }&lt;br&gt;
      └─ clang-format --lines=5:8 --lines=20:22 -i foo.cpp&lt;br&gt;
The --lines flag tells clang-format to reformat only those ranges. Everything outside the diff is untouched.&lt;/p&gt;

&lt;p&gt;In CI, it reads PRE_COMMIT_FROM_REF/PRE_COMMIT_TO_REF (set automatically by pre-commit) and diffs those two refs instead.&lt;/p&gt;

&lt;p&gt;Setup&lt;/p&gt;

&lt;h1&gt;
  
  
  .pre-commit-config.yaml
&lt;/h1&gt;

&lt;p&gt;repos:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;repo: &lt;a href="https://github.com/goyaladitya05/clang-format-inc" rel="noopener noreferrer"&gt;https://github.com/goyaladitya05/clang-format-inc&lt;/a&gt;
rev: v1.0.0
hooks:

&lt;ul&gt;
&lt;li&gt;id: clang-format-inc
clang-format installs automatically — no system dependency needed.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;What else it can do&lt;br&gt;
Check mode — report without fixing (useful for CI that shouldn't auto-commit):&lt;/p&gt;

&lt;p&gt;clang-format-inc --check&lt;br&gt;
Diff mode — print exactly what would change:&lt;/p&gt;

&lt;p&gt;clang-format-inc --diff&lt;br&gt;
Skip generated files:&lt;/p&gt;

&lt;p&gt;clang-format-inc --exclude 'third_party/|generated/'&lt;br&gt;
Parallel processing for large jobs:&lt;/p&gt;

&lt;p&gt;clang-format-inc --workers 4&lt;br&gt;
Why not just use clang-format-diff.py?&lt;br&gt;
LLVM ships a script that does something similar. Two reasons I didn't use it:&lt;/p&gt;

&lt;p&gt;It's Apache-2.0 — bundling it into an MIT package adds license complexity&lt;br&gt;
It's a vendored file that needs tracking against upstream LLVM releases&lt;br&gt;
clang-format-inc reimplements the diff-parsing logic in ~30 lines of pure Python with full test coverage.&lt;/p&gt;

&lt;p&gt;Links&lt;br&gt;
📦 PyPI&lt;br&gt;
📖 Docs&lt;br&gt;
🐙 GitHub&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>devtools</category>
      <category>opensource</category>
      <category>git</category>
    </item>
  </channel>
</rss>
