Guide [Tutorial] Format Your Code with Clang Power Tools in Visual Studio

Welcome to our Community
Wanting to join the rest of our members? Feel free to sign up today.
Sign up

Twinz

Premium
Premium
Joined
Jun 17, 2024
Messages
16
Solutions
1
Reaction score
23
Points
3

Tutorial: Source Formatting with Clang Power Tools​

The goal of this tutorial is to ensure that all developers on a project use the same code style rules. Here’s how to configure Clang Power Tools in Visual Studio to standardize code formatting.


Step 1: Installing the Clang Power Tools Extension​

  • Open Visual Studio.
  • Go to Extensions -> Manage Extensions -> Online.
  • Search for Clang Power Tools and download the extension.
1718792569175.png
  • Restart Visual Studio and accept any modifications.
  • You should now see new action buttons at the top of Visual Studio.
1718792599040.png

Step 2: Configuring the Style File​

  • Click on the Settings button at the top of Visual Studio, then go to Format -> Clang Format Edit Open.
1718792654775.png
  • Choose Open Configurator.
  • At the bottom left, select the style you wanted. I prefer the LLVM style with some custom rules.
  • Paste your code into the Input area to see the formatted version in the Output area.
  • Adjust the formatting rules on the left to achieve your desired style.
  • Important: Ensure that "SortIncludes" is set to FALSE to avoid build compilation errors.
  • When satisfied with your style, click the Export .clang-format button.
  • Save the file as .clang-format in your source folder.
  • Important: If you name it different than just ".clang-format" it will not work !

1718792703627.png

I'm sharing the content of the .clang-format file, it's not perfect as everyone has their own preferences but it can give you a preset.
# Format Style Options - Created with Clang Power Tools
---
BasedOnStyle: LLVM
BreakBeforeBraces: Linux
SortIncludes: false
...

Step 3: Activating the Formatting​

  • Go back to Visual Studio in the Clang Power Tools Settings in the Format section.
  • Choose Style -> file.
  • Enable Format on save.
1718792919816.png
  • Restart Visual Studio.
  • Test the formatting on a file by pressing Ctrl+S.
  • Verify that the result matches your expectations. If not, go back to the clang file configuration and adjust the rules.

Step 4: Formatting All Your Projects​

  • To format all the files in your project, right-click on it -> Clang Power Tools -> Format.
1718792942038.png
  • Repeat this for each project.


Your project is now fully formatted, and any new code will be automatically formatted on save.

Don’t forget to share your .clang-format file with your collaborators to ensure consistent code style.

Hope you enjoyed this tutorial !
 

Attachments

  • 1718792502289.png
    1718792502289.png
    32.5 KB · Views: 14
  • 1718792616016.png
    1718792616016.png
    854 bytes · Views: 7
  • 1718792647369.png
    1718792647369.png
    73.6 KB · Views: 8
  • 1718792687451.png
    1718792687451.png
    165.7 KB · Views: 12
Quality guide, appreciate it.
 
  • Like
Reactions: Twinz