[clang-format] Sort include does not handle pre-compiled headers well.

Hi Daniel,
Sorry, I sent the initial message to the wrong list.

When using pre-compiled header, the Main Header for the file will not be detected since the first header needs to be the precompiled header.

I was looking at fixing it, but I am not quite sure how to go fixing it?

This will result in the main header being moved.

Expected:
MyFile.cpp:

#include “stdafx.h”
#include “MyFile.h”
#include “AnotherHeader.h”

Actual:

MyFile.cpp:

#include “stdafx.h”
#include “AnotherHeader.h”
#include “MyFile.h”

This can be somewhat worked around by leaving a line after the Main Header.

Fix Option 1:
Add a new option: PercompiledHeaderRegEx, and if detected assign priority -1, and keep on looking for MainHeader (Not great since we use unsigned)

Fix Option 2:
Add a keyword that can be used instead of the regular expression to refer to the MainHeader. This way one can add a regular expression for the pre-compiled header priority 0, and one for MainHeader priority 1. Consider it main header if first header with category >= MainHeaderCategory.

Fix Option 3:
A better plan?

Do you have any thought on the problem?
Kind Regards,
Jean-Philippe.

I’m curious about this too – not just precompiled headers, other Windows headers too.

From a header-sorting script we use in Chromium:

  # The win32 api has all sorts of implicit include order dependencies :-/
  # Give a few headers special sort keys that make sure they appear before all
  # other headers.
  if line.startswith('<windows.h>'):  # Must be before e.g. shellapi.h
    return '0'
  if line.startswith('<atlbase.h>'):  # Must be before atlapp.h.
    return '1' + line
  if line.startswith('<ole2.h>'):  # Must be before e.g. intshcut.h
    return '1' + line
  if line.startswith('<unknwn.h>'):  # Must be before e.g. intshcut.h
    return '1' + line