Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Introduce a prototype of a "header guard enforcement" tool #48903

Merged
merged 32 commits into from
Jan 26, 2024

Conversation

matanlurey
Copy link
Contributor

@matanlurey matanlurey commented Dec 12, 2023

Closes flutter/flutter#133415.


This is a prototype I threw together in about 1-2 hours. It enforces and automatically fixes header guards that don't match the the Google C++ style guide. For example, here is (trimmed) output at HEAD:

line 5, column 1 of impeller/aiks/picture.h: Unexpected #pragma once
  ╷
5 │ #pragma once
  │ ^^^^^^^^^^^^
  ╵
line 5, column 1 of flow/stopwatch.h: Expected #ifndef FLUTTER_FLOW_STOPWATCH_H_
  ╷
5 │ #ifndef FLUTTER_FLOW_INSTRUMENTATION_H_
  │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  ╵
The following 731 files have invalid header guards:

matanlurey added a commit that referenced this pull request Dec 13, 2023
Automatically generated by #48903.

Progress towards flutter/flutter#133415.
@Hixie
Copy link
Contributor

Hixie commented Dec 14, 2023

test-exempt: tested by #48903

@matanlurey
Copy link
Contributor Author

Ok, I think I'm ready to land the tool (without enforcing it on CI - that will be another PR). PTAL.

Copy link
Member

@gaaclarke gaaclarke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a couple of suggestions. There were a few places where there may be false positives, but we'll get those sorted out easy if this is running on ci.


// Recursive search for header files.
final io.Directory dir = source.flutterDir;
await for (final io.FileSystemEntity entity in dir.list(recursive: true)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend pulling this out to a generator, this method is long and probably looks really familiar to something you've written dozens of times before =)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Or I misunderstood your comment, so please feel free to add more.


// Check each file.
final List<HeaderFile> badFiles = <HeaderFile>[];
for (final io.File file in files) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be split into a generator too, then you don't have all of these continues.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

/// Returns the expected header guard for this file, relative to [engineRoot].
///
/// For example, if the file is `foo/bar/baz.h`, this will return `FLUTTER_FOO_BAR_BAZ_H_`.
String expectedName({required String engineRoot}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: a method name should have a verb

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

return 'FLUTTER_${underscoredRelativePath.toUpperCase()}_H_';
}

/// Updates the file at [path] to have the expected header guard.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the return value bool?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment.

Copy link
Member

@zanderso zanderso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matanlurey matanlurey requested a review from zanderso January 26, 2024 19:38
@matanlurey
Copy link
Contributor Author

Needs an entry in https://github.com/flutter/engine/blob/main/testing/run_tests.py#L975 to run the tests on CI.

Done.

@matanlurey matanlurey added the autosubmit Merge PR when tree becomes green via auto submit App label Jan 26, 2024
auto-submit bot pushed a commit that referenced this pull request Jan 26, 2024
#50069)

Generated by #48903 (`dart ./tools/header_guard_check/bin/main.dart --fix`).

As discussed with @cbracken and @jmagman, the guards are not technically needed on the Mac/iOS code, but they (a) do not hurt and (b) still provide value if for some reason `#include` is used instead of `#import` (though I suspect we could try to add that to the tool in the future as well).
Copy link
Contributor

auto-submit bot commented Jan 26, 2024

auto label is removed for flutter/engine/48903, due to - The status or check suite Mac mac_host_engine has failed. Please fix the issues identified (or deflake) before re-applying this label.

@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jan 26, 2024
@matanlurey matanlurey added the autosubmit Merge PR when tree becomes green via auto submit App label Jan 26, 2024
final List<String> exclude;

/// Runs the header guard check.
Future<int> run() async {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jan 26, 2024
Copy link
Contributor

auto-submit bot commented Jan 26, 2024

auto label is removed for flutter/engine/48903, due to - The status or check suite Linux linux_fuchsia has failed. Please fix the issues identified (or deflake) before re-applying this label.

@matanlurey matanlurey merged commit d33b2dd into flutter:main Jan 26, 2024
@matanlurey matanlurey deleted the engine-pragma-once-tool branch January 26, 2024 21:16
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Jan 26, 2024
…142347)

flutter/engine@525bd7d...a65a1b5

2024-01-26 [email protected] Roll Skia from c32aa37effcc to 6279c88b9e29 (1 revision) (flutter/engine#50098)
2024-01-26 [email protected] [Impeller] add compute pass API for memory barriers, re-enable for Vulkan. (flutter/engine#49946)
2024-01-26 [email protected] Introduce a prototype of a "header guard enforcement" tool (flutter/engine#48903)
2024-01-26 [email protected] Roll Skia from e24124912cc3 to c32aa37effcc (1 revision) (flutter/engine#50094)
2024-01-26 [email protected] [web] Do not wipe the PlatformViewManager when disposing of a view. (flutter/engine#49991)
2024-01-26 [email protected] Finish landing missing/incorrect header guards across `flutter/engine` (flutter/engine#50069)
2024-01-26 [email protected] Roll Skia from 32f6bff0f193 to e24124912cc3 (2 revisions) (flutter/engine#50093)
2024-01-26 [email protected] Roll Skia from cbdf09d69efc to 32f6bff0f193 (3 revisions) (flutter/engine#50092)
2024-01-26 [email protected] Roll Dart SDK from 5636e338e0b9 to 7337995bc851 (1 revision) (flutter/engine#50087)
2024-01-26 [email protected] Fix Shell::Screenshot for Impeller (flutter/engine#50072)
2024-01-26 [email protected] Roll Skia from ae73baacb793 to cbdf09d69efc (1 revision) (flutter/engine#50085)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-engine-flutter-autoroll
Please CC [email protected],[email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Migrate #pragma once to #ifndef *_H_ guards and catch backslides
5 participants