This article introduces a code formatting tool called Clang-format and explains in detail how to format code in Keil MDK5 using the Clang-format plugin, and shows how Clang-format formats code. If you are a C/C++ programmer, this article will help you in your daily development work.
Clang-format IntroductionClang-format is a tool for code formatting that helps developers achieve a consistent code style across different programming languages and coding styles. It supports multiple programming languages, including C++, Java, JavaScript, and Objective-C, and provides a rich set of code formatting options to meet the coding style needs of different teams and individuals.
Clang-format is implemented in the LLVM (Low Level Virtual Machine) based Clang compiler, which uses Clang's parsing engine to understand the code structure and format the code according to the rules specified in the configuration file. It can be used via command line tools or editor plugins and is very easy to integrate into development environments.
Using Clang-format can bring many benefits, such as:
- Improve readability and consistency of code.
- Reduce discussions and disputes about formatting issues in code reviews.
- Makes code easier to maintain and modify.
In short, Clang-format is a powerful, flexible and easy-to-use code formatting tool that helps developers improve efficiency and quality when writing and maintaining code.
Clang-format DownloadDownload the latest version: https://github.com/llvm/llvm-project/releases
Open the Github version release page of LLVM, the latest version now (20230517) is 16.0.4, click Show all assets to expand all entries, then find LLVM-xx-win64.exe and click Download.
Download win64 version or linux version for your computer.
Here's how to extract the Clang-format.exe file from it.
Install LLVM-16.0.4-win64.exe, find clang-format.exe in the path after installation
Clang-format comes with six configurations:
- LLVM: This is the default configuration and follows the code style of the LLVM project.
- Google: This follows Google's C++ code style.
- Chromium: This follows the code style of the Chromium project.
- Microsoft: This follows Microsoft's C++ code style.
- Mozilla: follows the code style of the Mozilla project.
- WebKit: follows the code style of the WebKit project.
The command to export the configuration of a style (Microsoft for example) is:
C:\Program Files\LLVM\bin\clang-format.exe -style="Microsoft" --dump-config > C:\Program Files\LLVM\Microsoft.clang-format
It is recommended to modify it based on some kind of self-configuration instead of configuring it from scratch by yourself. I personally modified my code based on Microsoft's style to my personal satisfaction.
To learn how to configure clang format, please refer to the official documentation: https://clang.llvm.org/docs/ClangFormatStyleOptions.html
Set Clang-format in KeilConfigure Clang-format in the same way as you configure Astyle.
Open Keil, Tools->Customize Tools Menu...
My Command: C:\ProgramFiles\Coding_Standard\AStyle\bin\AStyle.exe
My Arguments are shown below, indicating that the specified xx.clang-format file configuration is used for formatting.
If you want to use a certain style (in the case of Microsoft) of default configuration, Arguments can be:
-style=Microsoft -i !E
You can call it via Tools->Clang format Current File after the setting is done. Note that you must first save the changes with Ctrl+S before calling the formatting program for the program to work.
If you want to call Clang-format more easily, you can set a shortcut key as follows:
Edit->Configuration... Open the Configuration window, find Tools:Style Current File in the Shorteut Keys interface, click Create Shortcut, and enter the shortcut you want to set.
Comments
Please log in or sign up to comment.