This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Introduce a prototype of a "header guard enforcement" tool #48903
Merged
Merged
Changes from 6 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
340a691
Initial commit: directory for header_guard_check.
matanlurey 0058f60
WIP.
matanlurey 3aed665
Add minimal library with some validation.
matanlurey 1784e68
Working basic tool.
matanlurey 90892fa
WS.
matanlurey 5b21cc1
Tweak.
matanlurey 934d838
Merge remote-tracking branch 'upstream/main' into engine-pragma-once-…
matanlurey 98e3307
Tweak.
matanlurey 3afe63c
++
matanlurey 59e98e6
++
matanlurey acc939a
++
matanlurey 8d3b0f9
++
matanlurey 1c8fa0e
++
matanlurey 7d1a621
Change WS.
matanlurey 68ce106
++
matanlurey 4a55b15
Merge remote-tracking branch 'upstream/main' into engine-pragma-once-…
matanlurey 6df1543
Merge branch 'main' into engine-pragma-once-tool
matanlurey 5212ade
Tweak WS.
matanlurey d6f86fb
Merge remote-tracking branch 'upstream/main' into engine-pragma-once-…
matanlurey f9bfce1
Tweak expected name.
matanlurey 1b6ea20
++
matanlurey de04172
++
matanlurey dd2b5b7
++
matanlurey 713416d
++
matanlurey 42cae8b
Merge branch 'main' into engine-pragma-once-tool
matanlurey 534618b
Merge remote-tracking branch 'upstream/main' into engine-pragma-once-…
matanlurey 03b9099
Tweak rules.
matanlurey 6593be6
++
matanlurey 26fa528
Address feedback.
matanlurey 1d1627a
++
matanlurey f91b3fd
++
matanlurey 7107ef1
++
matanlurey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # header_guard_check | ||
|
|
||
| A tool to check that C++ header guards are used consistently in the engine. | ||
|
|
||
| ```shell | ||
| # Assuming you are in the `flutter` root of the engine repo. | ||
| dart ./tools/header_guard_check/bin/main.dart | ||
| ``` | ||
|
|
||
| The tool checks _all_ header files for the following pattern: | ||
|
|
||
| ```h | ||
| // path/to/file.h | ||
|
|
||
| #ifndef PATH_TO_FILE_H_ | ||
| #define PATH_TO_FILE_H_ | ||
| ... | ||
| #endif // PATH_TO_FILE_H_ | ||
| ``` | ||
|
|
||
| If the header file does not follow this pattern, the tool will print an error | ||
| message and exit with a non-zero exit code. For more information about why we | ||
| use this pattern, see [the Google C++ style guide](https://google.github.io/styleguide/cppguide.html#The__define_Guard). | ||
|
|
||
| > [!IMPORTANT] | ||
| > This is a prototype tool and is not yet integrated into the engine's CI, nor | ||
| > provides a way to fix the header guards automatically. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'dart:io' as io; | ||
|
|
||
| import 'package:header_guard_check/header_guard_check.dart'; | ||
|
|
||
| Future<int> main(List<String> arguments) async { | ||
| final int result = await HeaderGuardCheck.fromCommandLine(arguments).run(); | ||
| if (result != 0) { | ||
| io.exit(result); | ||
| } | ||
| return result; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'dart:io' as io; | ||
|
|
||
| import 'package:args/args.dart'; | ||
| import 'package:engine_repo_tools/engine_repo_tools.dart'; | ||
| import 'package:meta/meta.dart'; | ||
| import 'package:path/path.dart' as p; | ||
|
|
||
| import 'src/header_file.dart'; | ||
|
|
||
| /// Checks C++ header files for header guards. | ||
| @immutable | ||
| final class HeaderGuardCheck { | ||
| /// Creates a new header guard checker. | ||
| const HeaderGuardCheck({ | ||
| required this.source, | ||
| required this.exclude, | ||
| }); | ||
|
|
||
| /// Parses the command line arguments and creates a new header guard checker. | ||
| factory HeaderGuardCheck.fromCommandLine(List<String> arguments) { | ||
| final ArgResults argResults = _parser.parse(arguments); | ||
| return HeaderGuardCheck( | ||
| source: Engine.fromSrcPath(argResults['root'] as String), | ||
| exclude: argResults['exclude'] as List<String>, | ||
| ); | ||
| } | ||
|
|
||
| /// Engine source root. | ||
| final Engine source; | ||
|
|
||
| /// Path directories to exclude from the check. | ||
| final List<String> exclude; | ||
|
|
||
| /// Runs the header guard check. | ||
| Future<int> run() async { | ||
| final List<io.File> files = <io.File>[]; | ||
|
|
||
| // Recursive search for header files. | ||
matanlurey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| final io.Directory dir = source.flutterDir; | ||
| await for (final io.FileSystemEntity entity in dir.list(recursive: true)) { | ||
| if (entity is io.File && entity.path.endsWith('.h')) { | ||
| // Check that the file is not excluded. | ||
| bool excluded = false; | ||
| for (final String excludePath in exclude) { | ||
| if (p.isWithin(excludePath, entity.path)) { | ||
| excluded = true; | ||
| break; | ||
| } | ||
| } | ||
| if (!excluded) { | ||
| files.add(entity); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Check each file. | ||
| final List<HeaderFile> badFiles = <HeaderFile>[]; | ||
| for (final io.File file in files) { | ||
matanlurey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| final HeaderFile headerFile = HeaderFile.parse(file.path); | ||
| if (headerFile.pragmaOnce != null) { | ||
| io.stderr.writeln(headerFile.pragmaOnce!.message('Unexpected #pragma once')); | ||
| badFiles.add(headerFile); | ||
| continue; | ||
| } | ||
| if (headerFile.guard == null) { | ||
| io.stderr.writeln('Missing header guard in ${headerFile.path}'); | ||
| badFiles.add(headerFile); | ||
| continue; | ||
| } | ||
|
|
||
| // Determine what the expected guard should be. | ||
| // Find the relative path from the engine root to the file. | ||
| final String relativePath = p.relative(file.path, from: source.flutterDir.path); | ||
| final String underscoredRelativePath = relativePath.replaceAll(p.separator, '_'); | ||
| final String expectedGuard = 'FLUTTER_${p.withoutExtension(underscoredRelativePath).toUpperCase().replaceAll('.', '_')}_H_'; | ||
| if (headerFile.guard!.ifndefValue != expectedGuard) { | ||
| io.stderr.writeln(headerFile.guard!.ifndefSpan!.message('Expected #ifndef $expectedGuard')); | ||
| badFiles.add(headerFile); | ||
| continue; | ||
| } | ||
| if (headerFile.guard!.defineValue != expectedGuard) { | ||
| io.stderr.writeln(headerFile.guard!.defineSpan!.message('Expected #define $expectedGuard')); | ||
| badFiles.add(headerFile); | ||
| continue; | ||
| } | ||
| if (headerFile.guard!.endifValue != expectedGuard) { | ||
| io.stderr.writeln(headerFile.guard!.endifSpan!.message('Expected #endif // $expectedGuard')); | ||
| badFiles.add(headerFile); | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| if (badFiles.isNotEmpty) { | ||
| io.stdout.writeln('The following ${badFiles.length} files have invalid header guards:'); | ||
| for (final HeaderFile headerFile in badFiles) { | ||
| io.stdout.writeln(' ${headerFile.path}'); | ||
| } | ||
| return 1; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| final Engine? _engine = Engine.tryFindWithin(p.dirname(p.fromUri(io.Platform.script))); | ||
|
|
||
| final ArgParser _parser = ArgParser() | ||
| ..addOption( | ||
| 'root', | ||
| abbr: 'r', | ||
| help: 'Path to the engine source root.', | ||
| valueHelp: 'path/to/engine/src', | ||
| defaultsTo: _engine?.srcDir.path, | ||
| ) | ||
| ..addMultiOption( | ||
| 'exclude', | ||
| abbr: 'e', | ||
| help: 'Path directories to exclude from the check.', | ||
| valueHelp: 'path/to/dir', | ||
| defaultsTo: _engine != null ? <String>[ | ||
| p.join(_engine!.flutterDir.path, 'build'), | ||
| p.join(_engine!.flutterDir.path, 'prebuilts'), | ||
| p.join(_engine!.flutterDir.path, 'third_party'), | ||
| ] : null, | ||
| ); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍