-
Notifications
You must be signed in to change notification settings - Fork 182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[dev_tools] Add script to check whether header guards are style-compliant. #1828
Closed
cdleary
wants to merge
2
commits into
google:main
from
xlsynth:cdleary/2025-01-04-dev-util-header-guard-check
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,81 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright 2025 The XLS Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# pylint: disable=missing-function-docstring | ||
|
||
"""Script to run from XLS repo root, checks if header guards are compliant.""" | ||
|
||
import os | ||
import re | ||
import sys | ||
def get_expected_guard(filepath, repo_root): | ||
# Convert the file path relative to repo root to an uppercase header guard | ||
# format. | ||
relative_path = os.path.relpath(filepath, repo_root) | ||
guard = relative_path.upper().replace(os.sep, '_').replace('.', '_') + '_' | ||
return guard | ||
|
||
def check_header_guard(filepath, expected_guard): | ||
with open(filepath, 'r') as file: | ||
lines = file.readlines() | ||
|
||
# Check for the presence of the expected header guard. | ||
guard_pattern = re.compile(r'#ifndef\s+(\S+)') | ||
for line in lines: | ||
match = guard_pattern.match(line.strip()) | ||
if match: | ||
actual_guard = match.group(1) | ||
return actual_guard == expected_guard, actual_guard | ||
|
||
return False, None | ||
|
||
def find_h_files(repo_root): | ||
# Find all `.h` files within the repo root, excluding `xls/contrib` and | ||
# `third_party`. | ||
h_files = [] | ||
for root, _, files in os.walk(repo_root): | ||
if 'xls/contrib' in root or 'third_party' in root: | ||
continue | ||
for file in files: | ||
if file.endswith('.h'): | ||
h_files.append(os.path.join(root, file)) | ||
return h_files | ||
|
||
def main(): | ||
repo_root = os.path.join(os.getcwd()) | ||
h_files = find_h_files(repo_root) | ||
|
||
non_compliant_files = [] | ||
|
||
for h_file in h_files: | ||
expected_guard = get_expected_guard(h_file, repo_root) | ||
compliant, actual_guard = check_header_guard(h_file, expected_guard) | ||
if not compliant: | ||
non_compliant_files.append((h_file, expected_guard, actual_guard)) | ||
|
||
if non_compliant_files: | ||
print('Non-style-compliant header files:') | ||
for file, expected, actual in non_compliant_files: | ||
print(file) | ||
print(f' want: {expected}') | ||
print(f' got: {actual if actual else "None"}') | ||
sys.exit(-1) | ||
cdleary marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else: | ||
print('All header files are style compliant.') | ||
|
||
if __name__ == '__main__': | ||
main() | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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.
Is or 'third_party' in root needed? I don't think github has this directory.
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.
https://github.com/google/xls/tree/main/third_party