Open Bug 1679522 Opened 4 years ago Updated 2 years ago

Enable SortIncludes option of clang-format

Categories

(Developer Infrastructure :: Source Code Analysis, task)

Tracking

(Not tracked)

REOPENED

People

(Reporter: sg, Assigned: andi)

References

Details

(Keywords: leave-open)

Attachments

(14 files, 1 obsolete file)

47 bytes, text/x-phabricator-request
Details | Review
47 bytes, text/x-phabricator-request
Details | Review
47 bytes, text/x-phabricator-request
Details | Review
47 bytes, text/x-phabricator-request
Details | Review
47 bytes, text/x-phabricator-request
Details | Review
47 bytes, text/x-phabricator-request
Details | Review
47 bytes, text/x-phabricator-request
Details | Review
47 bytes, text/x-phabricator-request
Details | Review
47 bytes, text/x-phabricator-request
Details | Review
47 bytes, text/x-phabricator-request
Details | Review
47 bytes, text/x-phabricator-request
Details | Review
47 bytes, text/x-phabricator-request
Details | Review
47 bytes, text/x-phabricator-request
Details | Review
47 bytes, text/x-phabricator-request
Details | Review

clang-format provides the ability to sort include directives via the SortIncludes option. We should check if we can use that to automate adherence to the coding style, at least to some degree. There are various rules that can be configured. If it's not possible to implement the rules written down in the coding style, these could be adapted to match clang-format's abilities.

Other relevant options here are IncludeBlocks and IncludeCategories.

It seems that clang-format always sorts ignoring case, so we should probably adapt the coding style draft section accordingly. (The current codebase, here there is a visible sorting, is divided on whether case is ignored or not.)

Sorting includes requires that the headers are self-contained, i.e. they include all other headers they need. I will check to what degree this causes issues.

Conditional includes are however, not affected by this. They are preserved as is, resp. if a conditional block includes multiple includes, these are sorted only within that block.

Another issue here are macros defined in system headers, which need to be undefined after they are included somewhere, possibly indirectly. A notorious case is None, which is defined by some X11 headers, which is undefined through X11UndefineNone.h. This must be immediately included after including the system header that caused it to be defined. This could be addressed by using a wrapper for that system header that ensures it is undefined appropriately.

check_spidermonkey_style.py checks some convention for sorting & grouping includes that I don't fully understand. Some things might (probably should) be incorporated into the global rules, e.g. oddly_ordered_inclnames. Others are entangled with custom header naming conventions for SpiderMonkey.

Maybe this functionality can be retired at some point in favor of using clang-format to handle this? Maybe with a custom rule for js/src

Attachment #9190008 - Attachment is obsolete: true
Assignee: nobody → sgiesecke
Status: NEW → ASSIGNED

The js code is the only (?) part of the code base that already does automatic sorting/grouping of include directives, using a custom mechanism. There are different options on how to proceed with this when activating SortIncludes for the codebase.

  • Use the existing clang-format configuration for js as well (see https://phabricator.services.mozilla.com/D98312 on how that would currently look)
  • Make some extensions to the general clang-format configuration, and use that for js as well (e.g. I think we could add the rules for grouping *-inl headers separately to the general rules, but some other rules are very js-specific, e.g. for grouping js includes separately)
  • Make a separate clang-format configuration for js
  • Disable SortIncludes for js (so for js the situation would stay unchanged after this bug)

(In the first three cases, the existing code for checking and fixing include directive ordering in config/check_spidermonkey_style.py could be removed)

:jandem, WDYT?

Flags: needinfo?(jdemooij)

(In reply to Simon Giesecke [:sg] [he/him] from comment #10)

  • Disable SortIncludes for js (so for js the situation would stay unchanged after this bug)

I'd prefer this option. I think SM engineers are happy with the #include order and grouping enforced by the check_spidermonkey_style.py script.

Let me drop a link to this bug in our Matrix room to see what others think.

Flags: needinfo?(jdemooij)

(In reply to Jan de Mooij [:jandem] from comment #11)

(In reply to Simon Giesecke [:sg] [he/him] from comment #10)

  • Disable SortIncludes for js (so for js the situation would stay unchanged after this bug)

I'd prefer this option. I think SM engineers are happy with the #include order and grouping enforced by the check_spidermonkey_style.py script.

Let me drop a link to this bug in our Matrix room to see what others think.

Thanks! I left a note there as well. I also updated the config and patch for js to follow the existing rules more closely. There are a few things that clang-format can't do as it seems:

  • Keep the order of subdirectories and casing as is. clang-format seems to always sort subdirectories after the base directory (so all foo/bar/.h includes after all foo/.h includes), and otherwise always case-insensitive.
  • Use the corresponding *-inl.h as the main include. It can be configured to do the reverse, i.e. regard foo.h as the main include when ordering includes in foo-inl.h.
Attachment #9190361 - Attachment description: Bug 1679522 - Reformat mfbt after activating SortIncludes. r=sylvestre → Bug 1679522 - Reformat mfbt after activating SortIncludes.
Depends on: 1680469
Attachment #9190526 - Attachment description: Bug 1679522 - Reformat js after activating SortIncludes. r=jandem → Bug 1679522 - Reformat js after activating SortIncludes.

Depends on D98078

  • Add missing include directives and forward declarations.
  • Remove some extra include directives.
  • Add missing namespace qualifications.
  • Move include directives out of namespace in toolkit/xre/GlobalSemaphore.h

Depends on D98604

Unfortunately, mozzconf defines a macro named crc32, which causes a naming
conflict with the struct members in zipstruct.h. This patch renames the
latter to avoid that conflict. Otherwise, there's a brittle dependency on
include order.

Depends on D98895

Attachment #9190360 - Attachment description: Bug 1679522 - Add missing include directives and pin order of includes where required. r=andi → Bug 1679522 - Pin order of includes where required before resorting. r=andi
Keywords: leave-open
Pushed by sgiesecke@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/4c3d3c0ed93d Fix include directives and forward declarations. r=andi,necko-reviewers,jgilbert https://hg.mozilla.org/integration/autoland/rev/5ac5ac506bc6 Use <> style for including windows system headers. r=andi

I have landed the prerequisites that were ready for landing. andi, maybe you can move this forward?

Flags: needinfo?(bpostelnicu)
Assignee: simon.giesecke → bpostelnicu
Flags: needinfo?(bpostelnicu)

The leave-open keyword is there and there is no activity for 6 months.
:andi, maybe it's time to close this bug?
For more information, please visit auto_nag documentation.

Flags: needinfo?(bpostelnicu)
Flags: needinfo?(bpostelnicu)
Product: Firefox Build System → Developer Infrastructure
Status: ASSIGNED → RESOLVED
Closed: 2 years ago
Resolution: --- → FIXED

This is a major blocker since we don't have support for self contained headers.

Status: RESOLVED → REOPENED
Keywords: leave-open
Resolution: FIXED → ---
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: