惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
Engineering at Meta
Engineering at Meta
The Register - Security
The Register - Security
N
Netflix TechBlog - Medium
D
Docker
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
月光博客
月光博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
DataBreaches.Net
IT之家
IT之家
V
V2EX
人人都是产品经理
人人都是产品经理
F
Fortinet All Blogs
J
Java Code Geeks
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
有赞技术团队
有赞技术团队
aimingoo的专栏
aimingoo的专栏
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
F
Full Disclosure
P
Proofpoint News Feed
B
Blog RSS Feed
T
Tor Project blog
T
The Blog of Author Tim Ferriss
B
Blog
Webroot Blog
Webroot Blog
腾讯CDC
T
Troy Hunt's Blog
T
Tailwind CSS Blog
H
Heimdal Security Blog
AWS News Blog
AWS News Blog
G
Google Developers Blog
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
A
About on SuperTechFans
SecWiki News
SecWiki News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
I
InfoQ
M
MIT News - Artificial intelligence
大猫的无限游戏
大猫的无限游戏
美团技术团队
L
LangChain Blog

kefan.me

Two Classes 在冰岛抵抗白昼 用Rust重构了后端 写前端如逆水行舟 鱼王 Coding in Wenyan 去开会成为一个人新生活的标志 Build a Web App - Part 2/2 Build a Web App - Part 1/2 我说我不来你非要让我来 痛苦还是无聊 从零搭建博客 写前端不是请客吃饭 一些片段 Running Valgrind on macOS Catalina Fetching Github Stats using GraphQL 适合私人沉迷 Odd Ways to Find Odd Numbers 关于李志 Miller-Rabin素性测试和大素数生成 你好,博客 再见
Install Clang-Format Without Root Privileges
2022-04-08 · via kefan.me

Prologue

While writing code, it is important to maintain file readability by following the correct coding practices, such as naming conventions, tab sizes, bracket placement, etc. Yet everyone has their own habit, I perfer my code to be indented by two spaces, but many people, including most of my professors, use four spaces or a tab (\t). There’s no right answers to this problem - the code would always look the same to the compilers, but it is necessary for us to follow a man-made standard for the sake of consistency.

Motivation

It is easy to keep track with the standard for short files, but for large projects it is usually tedious to check every line of code and see if each character is placed correctly. To make formatting easier, some developers came up with formatters, which are more often just cli tools or editor extensions, they check code styles and fix them automatically without breaking the original code. Some of the more famous ones include Prettier, which covers most of the frontend languages, Rubocop, which is used specifically for Ruby, or black, which is designed for Python (it’s suprising that they even need a formatter).

For older languages like C, we don’t have many options, the most well-supported tool for now is clang-format. It’s fast, customizable, and can be ran in a terminal. To install it we could simply do sudo apt install clang-format:

$ clang-format --version
clang-format version 14.0.0

But what if we don’t have root privileges?

Solution

It’s quite tricky, and there’s multiple approach to this problem, but seems like the easiest way is to download clang along with llvm and use clang-format as one of its submodules.

First we go to llvm’s release page and download the latest clang-llvm zip that suits our enviornment, which for me at the moment is clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz, then we’ll download it with

wget https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.0/clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz

tar -xvf clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz

It’s quite large so downloading and unpacking it might take a while, but luckily that’s the only step needed. We can use clang-format now with:

./clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/clang-format -h

We can also fix the path by using alias, like

alias cf="./clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/clang-format"

And that’s it.

More on Clang-Format

In order to format files using clang-format, we’ll need a .clang-foramt configuration file in the project directory. It contains the rules which will be applied to our code. We can generate it by cf -style=llvm -dump-config > .clang-format (notice I set cf as an alias to clang-format). As we can see here I used -style=llvm as the default styles, it could also be replaced by google, chromium, mozilla, webkit, or microsoft. We could now customize our style guide by editing the .clang-format file, which now might look something like this:

---
Language:        Cpp
# BasedOnStyle:  LLVM
AccessModifierOffset: -4 # -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands:   true
AlignTrailingComments: true
...

And we could use it as cf -style=file -i test_file.c. Obviously we could also apply some default format over the entire project as cf -i -style=WebKit *.cpp *.h

To visualize the formatting rules used, this website generates the configuration file, which saves us from going through the official documentation.

Thanks for reading.