NOTE: I stop using Eclipse for many years. So this plugin is almost abandoned.
An Eclipse plugin that integrates the clang-format tool as an alternative C/C++ code formatter and checks C++ coding style with the cpplint.py tool.
A consistent coding style is important for a project. And many projects use tools to format the code and check coding style. Many developers use Eclipse as a C/C++ IDE, but it is a little difficult to integrate an external tool to Eclipse. People have to switch to a command line and run the tools to format the code and check the coding style. And then they need to switch back to Eclipse to find the line and fix the coding style issue based on the tool's output. For the "lazy" people like me, this is irritating.
The expected behaviors is that people just format the code fragment by first selecting it and then pressing Command + Shift + f
on MacOS or Ctrl + Shift + f
on Linux and other systems. Further more, the coding style checker is run whenever a file is saved and all the issues are marked on the editor. That is exactly what CppStyle does.
There are many C/C++ code format tools such as "astyle" but currently "clang-format" is my favorite. It has several pre-defined styles and is highly configurable.
cpplint.py is a C++ coding style checker provided by google. It can be used to check the C++ code against the Google C++ coding style. It can detect many style errors and maintain the consistency of coding style.
cpplint.py https://google.github.io/styleguide/cppguide.html#cpplint
clang-format http://clang.llvm.org/docs/ClangFormat.html
sudo curl -L "https://raw.githubusercontent.com/google/styleguide/gh-pages/cpplint/cpplint.py" -o /usr/bin/cpplint.py
sudo chmod a+x /usr/bin/cpplint.py
clang-format can be built from llvm/clang source. But installing from binary is much easier.
For Ubuntu
sudo apt-get install clang-format-3.4
sudo ln -s /usr/bin/clang-format-3.4 /usr/bin/clang-format
On 64 bit platform, clang-format can also be downloaded from this page.
If you prefer, you can download the entire LLVM toolchain and extract the clang-format binary yourself. Just extract the .tar.xz file and copy bin/clang-format into your PATH (e.g. /usr/local/bin). - Set the path to the clang-format binaries.
Drag the above button to your running Eclipse workspace
[Go to Eclipse Marketplace page] (https://marketplace.eclipse.org/content/cppstyle)
CppStyle can be installed like other eclipse plugins from this site.
http://www.zhanwei.wang/CppStyle/update (Latest)
http://www.zhanwei.wang/CppStyle/oxygen (Eclipse Oxygen)
http://www.zhanwei.wang/CppStyle/mars (Eclipse Mars)
http://www.zhanwei.wang/CppStyle/luna (Eclipse Luna)
http://www.zhanwei.wang/CppStyle/kepler (Eclipse Kepler)
Go to Help -> Install New Software page, click Add button and then enter a name (CppStyle
) and the above URL, and then click OK.
Select CppStyle from drop-down list and then check the name CppStyle listed in the page. And then click Next and OK until restart.
- Build CppStye with maven first.
mvn clean package
- Install CppStyle with local update site
file:///<YOUR_CODE_PATH>/update/target/site
Restart Eclipse.
To configure CppStyle globally, go to Preferences -> C/C++ -> CppStyle dialog.
To configure CppStyle for a C/C++ project, go to Project properties -> CppStyle dialog.
To enable CppStyle(clang-format) as default C/C++ code formatter, go to Preferences -> C/C++ -> Code Style -> Formatter page and switch "Code Formatter" from [built-in] to "CppStyle (clang-format)"
To enable CppStyle(clang-format) as C/C++ code formatter for a project, go to Project properties -> C/C++ General -> Formatter page and switch "Code Formatter" from [built-in] to "CppStyle (clang-format)"
CppStyle does not support appending command line parameters to clang-format and cpplint.py. So, use their respective configuration files to do this.
CppStyle will pass the full absolute path of the source file to clang-format in command line. And clang-format will try to find the configuration file named .clang-format in the source file's path, and its parent's path if not found previously, and parent's path of the parent and so on.
So put the configuration file .clang-format into the project's root directory can make it work for all source files in the project.
Further more, you can also add the configuration file .clang-format into Eclipse workspace root directory to make it work for all projects in the workspace.
To generate the clang-format configuration file .clang-format:
clang-format -dump-config -style=Google > .clang-format
If no configure file named .clang-format is found, "-style=Google" will be passed to clang-format and Google style will be used by default.
By default, if you enable cpplint.py
in CppStyle page, cpplint.py
will be triggered every time when you save a file. You can also trigger cpplint.py
by click the button Run C/C++ Code Analysis in the popup menu when you right click the file, directory or the entire project in Project Explorer.
CppStyle will pass source file's full absolute path to cpplint.py
in the command line. And cpplint.py
also supports per-directory configuration by the configuration file named CPPLINT.cfg
.
If Root is set in project property page, --root=Root will pass to cpplint.py
.
CPPLINT.cfg
file can contain a number of key=value pairs.
Currently the following options are supported:
set noparent
filter=+filter1,-filter2,...
exclude_files=regex
linelength=80
To get the details of these options you can run the command:
cpplint.py --help
On Windows, the path in the CppStyle settings should point to an executable of cpplint. One option is to create an executable from cpplint.py using PyInstaller.
There are two ways to enable or disable cpplint.py on specific issues or files. The first and recommended one is to use configure file named CPPLINT.cfg
. The benefit of using configure file is that it can be version controlled and shared with others in a team. It also can produce the consistent result if you use cpplint.py
in command line instead of CppStyle in Eclipse.
The "filter" option is similar in function to --filter flag. It specifies
message filters in addition to the |_DEFAULT_FILTERS| and those specified
through --filter command-line flag.
"exclude_files" allows to specify a regular expression to be matched against
a file name. If the expression matches, the file is skipped and not run
through liner.
The other way is to configure Code Analysis in Preferences -> C/C++ -> Code Analysis -> Cpplint Issues globally, or in Project property -> C/C++ General -> Code Analysis -> Cpplint Issues for a C/C++ project. Eclipse Mars has bug to prevent opening Code Analysis page in project's property.