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

推荐订阅源

Jina AI
Jina AI
C
Cybersecurity and Infrastructure Security Agency CISA
美团技术团队
J
Java Code Geeks
博客园 - 聂微东
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 叶小钗
雷峰网
雷峰网
爱范儿
爱范儿
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
腾讯CDC
酷 壳 – CoolShell
酷 壳 – CoolShell
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
The Cloudflare Blog
博客园 - Franky
Engineering at Meta
Engineering at Meta
IT之家
IT之家
Last Week in AI
Last Week in AI
Recent Announcements
Recent Announcements
The Register - Security
The Register - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
The Exploit Database - CXSecurity.com
I
Intezer
V
Vulnerabilities – Threatpost
Simon Willison's Weblog
Simon Willison's Weblog
NISL@THU
NISL@THU
S
Security @ Cisco Blogs
T
Tenable Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Project Zero
Project Zero
H
Hacker News: Front Page
SecWiki News
SecWiki News
L
LINUX DO - 最新话题
Hacker News: Ask HN
Hacker News: Ask HN
Forbes - Security
Forbes - Security
C
CERT Recently Published Vulnerability Notes
T
Threatpost
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
V2EX - 技术
V2EX - 技术

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.