2013-01-10 05:49:28 +08:00
|
|
|
=========
|
|
|
|
LibFormat
|
|
|
|
=========
|
|
|
|
|
|
|
|
LibFormat is a library that implements automatic source code formatting based
|
|
|
|
on Clang. This documents describes the LibFormat interface and design as well
|
|
|
|
as some basic style discussions.
|
|
|
|
|
|
|
|
If you just want to use `clang-format` as a tool or integrated into an editor,
|
|
|
|
checkout :doc:`ClangFormat`.
|
|
|
|
|
|
|
|
Design
|
|
|
|
------
|
|
|
|
|
|
|
|
FIXME: Write up design.
|
|
|
|
|
|
|
|
|
|
|
|
Interface
|
|
|
|
---------
|
|
|
|
|
|
|
|
The core routine of LibFormat is ``reformat()``:
|
|
|
|
|
|
|
|
.. code-block:: c++
|
|
|
|
|
|
|
|
tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
|
|
|
|
SourceManager &SourceMgr,
|
|
|
|
std::vector<CharSourceRange> Ranges);
|
|
|
|
|
|
|
|
This reads a token stream out of the lexer ``Lex`` and reformats all the code
|
|
|
|
ranges in ``Ranges``. The ``FormatStyle`` controls basic decisions made during
|
2017-06-26 11:19:05 +08:00
|
|
|
formatting. A list of options can be found under :ref:`style-options`.
|
|
|
|
|
|
|
|
The style options are described in :doc:`ClangFormatStyleOptions`.
|
2013-01-10 05:49:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
.. _style-options:
|
|
|
|
|
|
|
|
Style Options
|
|
|
|
-------------
|
|
|
|
|
|
|
|
The style options describe specific formatting options that can be used in
|
|
|
|
order to make `ClangFormat` comply with different style guides. Currently,
|
2020-05-21 00:17:55 +08:00
|
|
|
several style guides are hard-coded:
|
2013-01-10 05:49:28 +08:00
|
|
|
|
|
|
|
.. code-block:: c++
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns a format style complying with the LLVM coding standards:
|
2018-11-05 01:02:00 +08:00
|
|
|
/// https://llvm.org/docs/CodingStandards.html.
|
2013-01-10 05:49:28 +08:00
|
|
|
FormatStyle getLLVMStyle();
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns a format style complying with Google's C++ style guide:
|
2013-01-10 05:49:28 +08:00
|
|
|
/// http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml.
|
|
|
|
FormatStyle getGoogleStyle();
|
|
|
|
|
2020-05-21 00:17:55 +08:00
|
|
|
/// Returns a format style complying with Chromium's style guide:
|
2021-11-04 03:41:24 +08:00
|
|
|
/// https://chromium.googlesource.com/chromium/src/+/refs/heads/main/styleguide/styleguide.md
|
2020-05-21 00:17:55 +08:00
|
|
|
FormatStyle getChromiumStyle();
|
|
|
|
|
|
|
|
/// Returns a format style complying with the GNU coding standards:
|
|
|
|
/// https://www.gnu.org/prep/standards/standards.html
|
|
|
|
FormatStyle getGNUStyle();
|
|
|
|
|
|
|
|
/// Returns a format style complying with Mozilla's style guide
|
|
|
|
/// https://firefox-source-docs.mozilla.org/code-quality/coding-style/index.html
|
|
|
|
FormatStyle getMozillaStyle();
|
|
|
|
|
|
|
|
/// Returns a format style complying with Webkit's style guide:
|
|
|
|
/// https://webkit.org/code-style-guidelines/
|
|
|
|
FormatStyle getWebkitStyle();
|
|
|
|
|
|
|
|
/// Returns a format style complying with Microsoft's style guide:
|
|
|
|
/// https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference
|
|
|
|
FormatStyle getMicrosoftStyle();
|
|
|
|
|
2013-01-10 05:49:28 +08:00
|
|
|
These options are also exposed in the :doc:`standalone tools <ClangFormat>`
|
|
|
|
through the `-style` option.
|
|
|
|
|
|
|
|
In the future, we plan on making this configurable.
|