Home >Backend Development >C++ >Create a C/C++ code formatting tool using Clang tool

Create a C/C++ code formatting tool using Clang tool

WBOY
WBOYforward
2023-08-26 13:09:181448browse

Create a C/C++ code formatting tool using Clang tool

In this tutorial, we will be discussing a program to create a C/C code formatting tool with the help of clang tools.

SETUP

sudo apt install python
sudo apt install clang-format-3.5

We will then create a Python file in a location where the current user has read and write permissions.

Example

import os
cpp_extensions = (".cxx",".cpp",".c", ".hxx", ".hh", ".cc", ".hpp")
for root, dirs, files in os.walk(os.getcwd()):
   for file in files:
      if file.endswith(cpp_extensions):
         os.system("clang-format-3.5 -i -style=file " + root + "/" + file)

Creates a file format file in the current user's top-level directory.

Output

clang-format-3.5 -style=google -dump-config > .clang-format

Finally copy this file to the top-level directory of the current project.

Now you can use your own code formatting tool. Just run the created Python file and you're ready to go!

The above is the detailed content of Create a C/C++ code formatting tool using Clang tool. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete