ClangFormat

  • 1. 独立的工具
  • 2. Vim 集成
  • 3. Emacs 集成
  • 4. BBEdit 集成
  • 5. CLion 集成
  • 6. Visual Studio 集成
  • 7. 用于补丁重新格式化的脚本(Script for patch reformatting)

本文为译文,点击 此处查看原文。

ClangFormat描述了一组构建在 LibFormat 之上的工具。它可以以多种方式支持您的工作流,包括一个独立的工具和编辑器集成。

1. 独立的工具

clang-format位于clang/tools/clang-format中,可用于格式化C/C++/Java/JavaScript/Objective-C/Protobuf/C#代码。

$ clang-format -help
OVERVIEW: A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf/C# code.If no arguments are specified, it formats the code from standard input
and writes the result to the standard output.
If <file>s are given, it reformats the files. If -i is specified
together with <file>s, the files are edited in-place. Otherwise, the
result is written to the standard output.USAGE: clang-format [options] [<file> ...]OPTIONS:Clang-format options:-assume-filename=<string> - When reading from stdin, clang-format assumes thisfilename to look for a style config file (with-style=file) and to determine the language.-cursor=<uint>            - The position of the cursor when invokingclang-format from an editor integration-dump-config              - Dump configuration options to stdout and exit.Can be used with -style option.-fallback-style=<string>  - The name of the predefined style used as afallback in case clang-format is invoked with-style=file, but can not find the .clang-formatfile to use.Use -fallback-style=none to skip formatting.-i                        - Inplace edit <file>s, if specified.-length=<uint>            - Format a range of this length (in bytes).Multiple ranges can be formatted by specifyingseveral -offset and -length pairs.When only a single -offset is specified without-length, clang-format will format up to the endof the file.Can only be used with one input file.-lines=<string>           - <start line>:<end line> - format a range oflines (both 1-based).Multiple ranges can be formatted by specifyingseveral -lines arguments.Can't be used with -offset and -length.Can only be used with one input file.-offset=<uint>            - Format a range starting at this byte offset.Multiple ranges can be formatted by specifyingseveral -offset and -length pairs.Can only be used with one input file.-output-replacements-xml  - Output replacements as XML.-sort-includes            - Sort touched include lines-style=<string>           - Coding style, currently supports:LLVM, Google, Chromium, Mozilla, WebKit.Use -style=file to load style configuration from.clang-format file located in one of the parentdirectories of the source file (or currentdirectory for stdin).Use -style="{key: value, ...}" to set specificparameters, e.g.:-style="{BasedOnStyle: llvm, IndentWidth: 8}"-verbose                  - If set, shows the list of processed filesGeneric Options:-help                     - Display available options (-help-hidden for more)-help-list                - Display list of available options (-help-list-hidden for more)-version                  - Display the version of this program

当所需的代码格式化样式与可用选项不同时,可以使用 -style="{key: value, ...}" 选项;或者将样式配置放在您的项目目录中的.clang-format_clang-format文件中,并使用clang-format -style=file,以自定义样式。

创建 .clang-format 文件的一个简单方法是:

clang-format -style=llvm -dump-config > .clang-format

在 Clang-Format 样式选项中描述了可用的样式选项。

2. Vim 集成

这里有为vim设计的一个集成,允许您在当前缓冲区上运行clang-format独立工具,可以选择要重新格式化的区域。该集成具有一个python文件的形式,可以在clang/tools/clang-format/clang-format.py中找到。
这可以通过在您的.vimrc中添加以下内容来集成:

map <C-K> :pyf <path-to-this-file>/clang-format.py<cr>
imap <C-K> <c-o>:pyf <path-to-this-file>/clang-format.py<cr>

第一行启用clang-format以支持NORMAL模式和VISUAL模式,第二行添加了对INSERT模式的支持。如果需要在一个不同的键上使用clang-format (C-K 代表 Ctrl+k),则将“C-K”更改为另一个绑定。
通过这种集成,您可以按下绑定键,clang-format将以NORMAL模式和INSERT模式格式化当前行,并以VISUAL模式格式化选择区域。行或区域被扩展到下一个更大的语法实体。
它对当前可能未保存的缓冲区进行操作,不创建或保存任何文件。要还原格式,只需撤消即可。

另一种选择是在保存一个文件时更改格式,从而在编码工作流中实现一个零工作量的集成。要做到这一点,请将以下内容添加到您的.vimr

function! Formatonsave()let l:formatdiff = 1pyf ~/llvm/tools/clang/tools/clang-format/clang-format.py
endfunction
autocmd BufWritePre *.h,*.cc,*.cpp call Formatonsave()

3. Emacs 集成

vim的集成类似,也有为emacs设计的一个集成。它可以在clang/tools/clang-format/clang-format.el中找到,并将以下内容添加到您的.emacs来使用这个集成:

(load "<path-to-clang>/tools/clang-format/clang-format.el")
(global-set-key [C-M-tab] 'clang-format-region)

这将函数clang-format-region绑定到C-M-tab,然后将格式化当前行或选中区域。

4. BBEdit 集成

clang-format不能与BBEdit一起用作一个文本过滤器,但是通过一个脚本可以很好地工作。实现此集成的AppleScript可以在clang/tools/clang-format/clang-format-bbedit.applescript中找到;在~/Library/Application Support/BBEdit/Scripts中放置一个副本,并编辑其中的path以指向您的clang-format的本地副本。
通过这种集成,您可以从Script菜单中选择脚本,并且clang-format将对选择进行格式化。注意,您可以通过重命名脚本来重命名菜单项,并且可以在Menus & Shortcuts下的BBEdit首选项中为菜单项分配键盘快捷方式。

5. CLion 集成

clang-format作为一种可选的代码格式化程序集成到CLion中。它在默认情况下是禁用的,可以在Settings/Preferences | Editor | Code Style中打开。
如果启用了clang-format支持,CLion会在打开一个项目时检测配置文件,并建议覆盖当前 IDE 设置。然后将.clang-format文件中的代码样式规则自动应用于所有编辑器操作,包括自动完成(auto-completion)、代码生成(code generatio)和重构(refactorings)。

6. Visual Studio 集成

从 alpha构建站点下载最新的 Visual Studio 扩展。默认的绑定键是 Ctrl-R、Ctrl-F。

7. 用于补丁重新格式化的脚本(Script for patch reformatting)

这个python脚本lang/tools/clang-format/clang-format-diff.py解析一个统一的 diff 的输出,并用clang-format重新格式化所有包含的行。

usage: clang-format-diff.py [-h] [-i] [-p NUM] [-regex PATTERN] [-style STYLE]Reformat changed lines in diff. Without -i option just output the diff that
would be introduced.optional arguments:-h, --help      show this help message and exit-i              apply edits to files instead of displaying a diff-p NUM          strip the smallest prefix containing P slashes-regex PATTERN  custom pattern selecting file paths to reformat-style STYLE    formatting style to apply (LLVM, Google, Chromium, Mozilla,WebKit)

因此,要重新格式化最新 git 提交中的所有行,只需:

git diff -U0 --no-color HEAD^ | clang-format-diff.py -i -p1

使用 Mercurial/hg

hg diff -U0 --color=never | clang-format-diff.py -i -p1

在一个 SVN 客户端中,你可以这样做:

svn diff --diff-cmd=diff -x -U0 | clang-format-diff.py -i

选项-U0将创建一个没有上下文行的 diff(脚本也将格式化这些行)。

使用 Clang Tools —— ClangFormat相关推荐

  1. Vim与clang-format

    2019独角兽企业重金招聘Python工程师标准>>> 前一段时间因工作需要参与了一个开源项目,在向github提交代码后提示格式校验失败. 项目的管理者告知,要想修改的代码能入库需 ...

  2. [转载] Sublime Text 3 插件 ClangFormat 格式化 C/C++版

    参考链接: 在Clang工具的帮助下创建C/C++代码格式化工具 Sublime Text 3 ClangFormat格式化 C/C++版 插件安装 ctrl+shift+p找到Package Con ...

  3. Clang 新手教程来啦!

    长话短说! 这篇新手教程会让你弄清楚什么是clang.clang AST.clang plugins 和clang tools 等等,让你大概知道clang 可以解决什么问题,而且小白也是可以用cla ...

  4. clang入门大全以及clang全家桶介绍

    Clang介绍 Clang项目属于LLVM项目(实现编译器调试器等所有的一切功能,对标GNU家族中的程序开发工具链)中的一项,在LLVM这个庞大的开源项目中,github源码显示中https://gi ...

  5. LLVM Clang前端编译与调试

    LLVM Clang前端编译与调试 iOS 关于编译 o 一.Objective-C 编译过程 o 为什么需要重新编译? o 编译步骤 o 二.编译步骤的详细说明 o 1.预处理 o 2.编译 o 词 ...

  6. 在CentOS 6.6 64bit上编译安装LLVM3.7,Clang,Libc++和libc++abi

    一.简介 LLVM LLVM(之前称为低级虚拟机Low Level Virtual Machine)是一种非常强大的编译器基础架构框架,专门为使用您喜爱的编程语言编写的程序的编译时.链接时和运行时优化 ...

  7. v深入研究Clang(四) Clang编译器的简单分析

    作者:史宁宁(snsn1984) 首先我们确定下Clang编译器的详细内容和涵盖范围.之前在<LLVM每日谈之二十 Everything && Clang driver > ...

  8. 深入研究Clang(九) Clang代码阅读之打log读流程2

    继续上一篇,同样的hello.c,同样的执行过程,只不过继续添加了一些log信息,而且对代码进行了更近一步的挖掘.先看输入和输出的log信息(前半部分): shining@shining-Virtua ...

  9. 在Linux上编译LLVM/Clang 8.0.0等全部源代码

    2019独角兽企业重金招聘Python工程师标准>>> 本教程使用的操作系统是Ubuntu Linux 18.04.2 LTS版本,编译器是GCC 6.5.0(不用新版的原因是某些代 ...

  10. 基于clang插件的一种iOS包大小瘦身方案

    引子 \ 包瘦身,包瘦身,包瘦身,重要的事情说三遍. \ 最近公司一款iOS APP(本文只讨论使用Objective C开发的iOS安装包)一直在瘦身,我们团队的APP也愈发庞大了.而要解决这个问题 ...

最新文章

  1. 计算机专业黑板报迎新,大学开学迎新黑板报
  2. 数据库笔记1:数据库基本语句
  3. mysql 错误记录
  4. 统计分析SQL Server Profiler 跟踪的SQL
  5. [云炬ThinkPython阅读笔记]2.5 运算顺序
  6. 反射机制2,Class类的使用
  7. 2020高速公路shp文件_全国按轴收费方案最终版!今天起,高速公路就按这个标准收费...
  8. zookeeper 源码阅读(3)
  9. 互联网服务应用协议设计
  10. devops 业务模型_如何为DevOps转型建立业务案例
  11. linux如何配置网络设置_如何在Linux中配置网络
  12. socket网络编程 java_Java Web 基础(一) 基于TCP的Socket网络编程
  13. Cesium 场景设置~黑白、夜视和描边
  14. smith圆图重要性_(完整版)smith史密斯圆图(个人总结),推荐文档
  15. 尾矿库监测 GNSS北斗高精度定位终端机应用
  16. 自定义CardView
  17. 知名清理工具或成恶意软件
  18. 为什么中国神仙比较负责任?
  19. 虚拟化bug定位神器之一 【gdbcoredump】--libvirtd hang 住
  20. usb string 描述符以及语言ID

热门文章

  1. 单片机c语言入门自学,单片机C语言入门教程
  2. windows下文件路径太深,无法删除解决办法
  3. 电子设计竞赛应该如何准备?
  4. Java学习视频教程 云析学院Java高级架构实战系列
  5. ffplay播放flv文件没有声音的解决方法
  6. Windows Server 2003 安装教程
  7. python实验教程_python语言程序设计实践教程实验七
  8. mysql 在线语法检查工具_「mysql 管理工具」五大开源MySQL管理工具! - seo实验室
  9. 写web项目时出现的错误:来自“http://localhost:63342/Demo/test01/day0618/css/a.css”的资源已被阻止,因为 MIME 类型(“text/html”)
  10. 音乐直链php,【原创】百度音乐直链 + 实现方法