LibFormat
本文为译文,点击 此处查看原文。
LibFormat
是一个基于Clang实现自动源代码格式化的库。本文档描述了LibFormat
接口和设计,以及一些基本的样式讨论。
如果您只想使用clang-format
作为一个工具或集成到一个编辑器中,请签出ClangFormat。
1. 设计
FIXME:补充设计。
2. 接口
LibFormat
的核心例程是reformat()
:
tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
SourceManager &SourceMgr,
std::vector<CharSourceRange> Ranges);
这将从lexer Lex
读取一个token
流,并在Ranges
内重新格式化所有代码范围。FormatStyle
控制格式化过程中做出的基本决策。可以在Style Options下找到Options列表。
Style Options 在 Clang-Format Style Options 中描述。
3. Style Options
Style Options
描述了特定的格式化选项,可以使用这些选项使ClangFormat
符合不同的样式指南。目前,有两种样式指南是硬编码的:
/// 返回符合LLVM编码标准的一个格式样式:
/// https://llvm.org/docs/CodingStandards.html.
FormatStyle getLLVMStyle();
/// 返回符合谷歌的C++样式指南的一个格式样式:
/// http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml.
FormatStyle getGoogleStyle();
这些选项也通过-style
选项在独立工具中公开。
在将来,我们计划使其可配置。