Skip to content

Commit

Permalink
Let clang-format sort includes.
Browse files Browse the repository at this point in the history
After this, `git cl format` will reorder includes in blocks of #includes
that are not separated by newlines.

This works in almost all cases, but it can break some code e.g.

  #include <windows.h>
  #include <shellapi.h>

clang-format will reorder these now, but shellapi.h only compiles if
windows.h was included first. Relying on this is brittle, so replace
code like this with

  #include <windows.h>

  // Must be after windows.h:
  #include <shellapi.h>

Since clang-format doesn't reorder across blocks, this will do the right
thing.

This also means you're still on the hook of putting blocks with user headers,
C++ headers, and C headers in the right order.

This will hopefully replace src/tools/sort-headers.py which contains
some hacky heuristics -- but just inserting newlines between includes
when needed (with a comment) seems like a better tradeoff anyhow.
And the automatic integration with `git cl format` is nice.

(clang-format has IncludeIsMainRegex and IncludeCategories for adding
heuristics, but we shouldn't use these, they're too complicated.)

BUG=688155
TBR=brettw

Review-Url: https://codereview.chromium.org/2669263003
Cr-Commit-Position: refs/heads/master@{#447875}
  • Loading branch information
nico authored and Commit bot committed Feb 2, 2017
1 parent 9b2a0b1 commit 6b08b5d
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ BasedOnStyle: Chromium
# 'vector<vector<int>>'. ('Auto' means that clang-format will only use
# 'int>>' if the file already contains at least one such instance.)
Standard: Cpp11

# TODO(thakis): Default this to true in -style=Chromium if we decide to keep it.
SortIncludes: true

# Make sure code like:
# IPC_BEGIN_MESSAGE_MAP()
# IPC_MESSAGE_HANDLER(WidgetHostViewHost_Update, OnUpdate)
Expand Down

0 comments on commit 6b08b5d

Please sign in to comment.