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

推荐订阅源

D
DataBreaches.Net
量子位
博客园 - 三生石上(FineUI控件)
Hacker News - Newest:
Hacker News - Newest: "LLM"
月光博客
月光博客
博客园 - 叶小钗
爱范儿
爱范儿
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Security Archives - TechRepublic
Security Archives - TechRepublic
小众软件
小众软件
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
V
Visual Studio Blog
V
V2EX
Schneier on Security
Schneier on Security
Cloudbric
Cloudbric
有赞技术团队
有赞技术团队
C
Check Point Blog
T
Troy Hunt's Blog
Google DeepMind News
Google DeepMind News
Google DeepMind News
Google DeepMind News
TaoSecurity Blog
TaoSecurity Blog
Engineering at Meta
Engineering at Meta
Recent Commits to openclaw:main
Recent Commits to openclaw:main
S
Schneier on Security
N
Netflix TechBlog - Medium
Project Zero
Project Zero
Last Week in AI
Last Week in AI
N
News and Events Feed by Topic
Microsoft Azure Blog
Microsoft Azure Blog
NISL@THU
NISL@THU
T
The Exploit Database - CXSecurity.com
AWS News Blog
AWS News Blog
博客园 - 聂微东
S
Securelist
腾讯CDC
O
OpenAI News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
A
About on SuperTechFans
Microsoft Security Blog
Microsoft Security Blog
B
Blog
博客园 - 【当耐特】
Y
Y Combinator Blog
N
News and Events Feed by Topic
Recorded Future
Recorded Future
Vercel News
Vercel News
PCI Perspectives
PCI Perspectives
Security Latest
Security Latest

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.