Skip to content

Commit

Permalink
[OSS] Add pre-commit script to check for invalid inclusions.
Browse files Browse the repository at this point in the history
  • Loading branch information
cdleary committed Mar 6, 2025
1 parent 94ffd57 commit 0592635
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ repos:
hooks:
- id: end-of-file-fixer
exclude: '^(xls/contrib/.*|docs_src/bazel_rules_macros.md|.*\.(sh|ir|txt|patch|csv))$'
- repo: local
hooks:
- id: check-cpp-absolute-includes
name: Check C++ absolute includes
entry: xls/dev_tools/check_cpp_includes.py
language: python
files: ^xls/.*\.(h|cc)$
exclude: ^xls/contrib/
types: [text]
69 changes: 69 additions & 0 deletions xls/dev_tools/check_cpp_includes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/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.

import sys
import re

ALLOWED_INCLUDE_STARTS = {
'xls/',
'absl/',
'gmock/',
'gtest/',
'benchmark/',
'llvm/',
'fuzztest/',
'verible/',
're2/',
'z3/',
'google/protobuf/',
'grpcpp/',
'grpc/',
'openssl/',
'nlohmann/',
'cppitertools/',
'ortools/',
'external/zstd/',
'riegeli/',
'tools/cpp/runfiles',
'linenoise.h',
'libs/json11/',
'%s', # For format strings embedded in files.
}

def check_file(filename):
with open(filename, 'r') as f:
content = f.read()

# Look for quoted (non-system) includes
includes = re.findall(r'#include\s*"([^"]*)"', content)
bad_includes = [inc for inc in includes
if not any(inc.startswith(start) for start in ALLOWED_INCLUDE_STARTS)]

if bad_includes:
print(f'{filename}: Found non-absolute includes:')
for include in bad_includes:
print(f' {include}')
return 1
return 0

def main():
exit_code = 0
for filename in sys.argv[1:]:
exit_code |= check_file(filename)
sys.exit(exit_code)

if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion xls/jit/jit_channel_queue_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <vector>

#include "absl/log/check.h"
#include "include/benchmark/benchmark.h"
#include "benchmark/benchmark.h"
#include "xls/ir/channel.h"
#include "xls/ir/channel_ops.h"
#include "xls/ir/package.h"
Expand Down
2 changes: 1 addition & 1 deletion xls/jit/value_to_native_layout_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <random>
#include <vector>

#include "include/benchmark/benchmark.h"
#include "benchmark/benchmark.h"
#include "xls/interpreter/random_value.h"
#include "xls/ir/ir_parser.h"
#include "xls/ir/package.h"
Expand Down

0 comments on commit 0592635

Please sign in to comment.