title: How can I temporarily disable clang-format include sorting?
body: <p>I am changing my project from using Artistic Style to <strong>clang-format</strong>. One feature I'm excited to use is include file sorting. However, there's one issue I don't know how to get around. I'm using <strong>clang-format</strong> 7.0.1 on CentOS 7 (from <strong>llvm-toolset-7.0</strong> in the SCLo repository).</p>
  <p>Our standard states that in a class header the base class will be included first. We then included &quot;system&quot;(ish) headers, followed by local directory, and libraries in decreasing order of coupling. I want to change the standard to be more in line with the <a href="https://llvm.org/docs/CodingStandards.html#include-style" rel="nofollow noreferrer">llvm guidelines</a>.</p>
  <p>The problem is that I don't know how to force my base class(es) to remain first in the header. I'm willing to modify every class header (once) if that is what is required.</p>
  <p>Here is a sample input file, manually constructed according to the old project standards:</p>
  <pre class="lang-cpp prettyprint-override"><code>#pragma once
  
  #include &quot;util/Application.hh&quot;
  #include &quot;ZMQmixin.hh&quot;
  
  #include &lt;filesystem&gt;
  #include &lt;json/json.h&gt;
  
  #include &quot;ZMQconfig.hh&quot;
  #include &quot;ZMQendpoint.hh&quot;
  #include &quot;Platform.hh&quot;
  
  #include &quot;Foo/bar.hh&quot;
  
  #include &quot;icd/Published.hh&quot;
  #include &quot;icd/NavPoint.hh&quot;
  
  class Sample : public Application
               , public ZMQmixin
  {
      using Base = Application;
  public:
      Sample(int num_io_threads = 1);
  };
  </code></pre>
  <p>Here is a minimal <strong>.clang-format</strong> file that I'm using to sort my includes:</p>
  <pre><code>Language:                                       Cpp
  BreakBeforeBraces:                              Allman
  
  AlwaysBreakAfterDefinitionReturnType:           None
  AlwaysBreakAfterReturnType:                     TopLevelDefinitions
  
  BreakInheritanceList:                           BeforeComma
  
  SortIncludes:                                   true
  IncludeBlocks:                                  Merge # Regroup # or Preserve ?
  IncludeCategories:
    # include file order:
    #    1 current directory (&quot;Foo.hh&quot;)
    #    2 parent directory -- only in unit tests (&quot;../Foo.hh&quot;)
    #    5 other directory   (&quot;bar/Baz.hh&quot;)
    #   15 osw|Common/*.hh
    #   20 icd/*.hh
    #   30 util/*.hh
    #   40 &lt;pseudo/system.h&gt; (boost, armadillo, zmq, etc.)
    #   45 &lt;c++include&gt;
    #   46 &lt;cwrapper&gt;  (cmath, etc.)
    #   50 &lt;c-include.h&gt;
    #   55 &lt;other/include.h&gt;
  
    ## Regexes must be ordered so that the most specific pattern matches first,
    ## when multiple patterns might match
  
    # current dir
    - Regex:           '^&quot;[[:alnum:]]+\.hh'
      Priority:        1
  
    # parent dir (unit test)
    - Regex:           '^&quot;\.\.\/[[:alnum:]_-]+'
      Priority:        2
  
    # project headers, in decreasing order of coupling
    - Regex:           '^&quot;(osw|Common)/'
      Priority:        15
    - Regex:           '^&quot;icd/'
      Priority:        20
    - Regex:           '^&quot;util/'
      Priority:        30
  
    # any other &quot;x/y.hh&quot; header is assumed to be a project header, but very coupled
    - Regex:           '^&quot;[[:alnum:]]+/'
      Priority:        5
  
    # pseudo system headers
    - Regex:           '^(&lt;|&quot;)(boost|armadillo|json|zmq)/'
      Priority:        40
  
    # override for C++ headers that begin with 'c' and lack any special character
    - Regex:           '^&lt;(chrono|compare|charconv|complex)&gt;'
      Priority:        45
    # C-language headers
    - Regex:           '^&lt;c[a-z]+&gt;'
      Priority:        46
    # C++ language headers
    - Regex:           '^&lt;([A-Za-z0-9-_]+)&gt;'
      Priority:        45
  
    # system headers (top level)
    - Regex:           '^&lt;[A-Za-z0-9-_]+\.h&gt;'
      Priority:        50
    # system headers (sub-dirs)
    - Regex:           '^&lt;.*&gt;'
      Priority:        55
  
  Standard:                                       Cpp11
  ...
  </code></pre>
  <p>Focusing just on the include ordering, <strong>clang-format</strong> produces this:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &quot;Platform.hh&quot;
  #include &quot;ZMQconfig.hh&quot;
  #include &quot;ZMQendpoint.hh&quot;
  #include &quot;ZMQmixin.hh&quot;
  #include &quot;Foo/bar.hh&quot;
  #include &quot;icd/NavPoint.hh&quot;
  #include &quot;icd/Published.hh&quot;
  #include &quot;util/Application.hh&quot;
  #include &lt;json/json.h&gt;
  #include &lt;filesystem&gt;
  </code></pre>
  <p>As you can see, the blocks have been merged and the rules applied. Now, if I try to disable clang-format, just around the header blocks:</p>
  <pre class="lang-cpp prettyprint-override"><code>// clang-format off
  #include &quot;util/Application.hh&quot;
  #include &quot;ZMQmixin.hh&quot;
  // clang-format on
  
  #include &lt;filesystem&gt;
  #include &lt;json/json.h&gt;
  
  #include &quot;ZMQconfig.hh&quot;
  #include &quot;ZMQendpoint.hh&quot;
  #include &quot;Platform.hh&quot;
  
  #include &quot;Foo/bar.hh&quot;
  
  #include &quot;icd/Published.hh&quot;
  #include &quot;icd/NavPoint.hh&quot;
  </code></pre>
  <p>include sorting is completely disabled. In playing with this, I noticed that a comment at the end of a block inhibits block merging but not block sorting:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &quot;ZMQmixin.hh&quot;
  #include &quot;util/Application.hh&quot;
  // clang-format on
  
  #include &quot;Platform.hh&quot;
  #include &quot;ZMQconfig.hh&quot;
  #include &quot;ZMQendpoint.hh&quot;
  #include &quot;Foo/bar.hh&quot;
  #include &quot;icd/NavPoint.hh&quot;
  #include &quot;icd/Published.hh&quot;
  #include &lt;json/json.h&gt;
  #include &lt;filesystem&gt;
  </code></pre>
  <p>This is pretty close; I have very few instances of multiple inheritance where the sorting of base classes is problematic.</p>
  <p>Is there a better way? Something I'm missing?</p>