[PATCH] D35955: clang-format: Add preprocessor directive indentation

Erik Uhlmann via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 27 12:54:47 PDT 2017


euhlmann created this revision.
euhlmann added a project: clang.

This is an implementation for bug 17362 <https://bugs.llvm.org/attachment.cgi?bugid=17362> which adds support for indenting preprocessor statements inside if/ifdef/endif. This takes previous work from fmauch and makes it into a full feature.
The context of this patch is that I'm a VMware intern, and I implemented this because VMware needs the feature. As such, some decisions were made based on what VMware wants, and I would appreciate suggestions on expanding this if necessary to use-cases other people may want.

Example:

  #if FOO             #if FOO
  #define BAR   ->    # define BAR
  #endif              #endif



- It's controlled by a new bool config option `IndentPPDirectives`
- Indenting happens after the hash but the hash counts as a column. In other words, one space is inserted for the first indent level and two spaces for every other level.

  #if FOO
  # if BAR
  #   include <foo>
  # endif
  #endif

This suited VMware's needs, but if not counting the hash as a column is significant, this could be configurable instead.

- Tabs are supported if enabled with `UseTab`
- There's a heuristic for detecting include guards and not indenting them. It looks for the pattern

  #ifndef <var>

immediately followed by

  #define <var>

This was chosen because it's simple, but it will cause problems if that pattern appears when it's not meant to be an include guard.


https://reviews.llvm.org/D35955

Files:
  docs/ClangFormatStyleOptions.rst
  include/clang/Format/Format.h
  lib/Format/ContinuationIndenter.cpp
  lib/Format/Format.cpp
  lib/Format/UnwrappedLineFormatter.cpp
  lib/Format/UnwrappedLineParser.cpp
  lib/Format/UnwrappedLineParser.h
  unittests/Format/FormatTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D35955.108484.patch
Type: text/x-patch
Size: 9407 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170727/75558666/attachment-0001.bin>


More information about the cfe-commits mailing list