Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[GPU] Fix regex for include directive handling (#39280)

Pawel Raasze357b2c0ac8a
diff --git a/.github/scripts/check_copyright.py b/.github/scripts/check_copyright.py
index 2acd7594e57..38aeafcbbc6 100644
--- a/.github/scripts/check_copyright.py
+++ b/.github/scripts/check_copyright.py
@@ -74,6 +74,20 @@ def get_encoding_line_number(file_path: str) -> int:
return -1
+def get_header_start_line(file_path: str) -> int:
+ """Return the 0-indexed line where the copyright header must start (after a Python shebang/encoding line)."""
+ encoding_line = get_encoding_line_number(file_path)
+ if encoding_line >= 0:
+ return encoding_line + 1
+ if not file_path.endswith('.py'):
+ return 0
+ try:
+ with open(file_path, 'r', encoding='utf-8-sig', errors='ignore') as f:
+ return 1 if f.readline().startswith('#!') else 0
+ except Exception:
+ return 0
+
+
def get_expected_header(file_path: str) -> str:
"""Get the expected copyright header for a file."""
info = get_file_type_info(file_path)
@@ -99,15 +113,10 @@ def get_file_header(file_path: str, num_lines: int = 3) -> str:
"""Read the first few lines of a file, handling BOM and encoding declaration if present."""
try:
with open(file_path, 'r', encoding='utf-8-sig', errors='ignore') as f:
- all_lines = [f.readline() for _ in range(num_lines + 2)] # Read extra lines to account for encoding
-
- # For Python files, skip the encoding line when checking copyright
- encoding_line = get_encoding_line_number(file_path)
- if encoding_line >= 0:
- # Skip encoding line and return the copyright-relevant lines
- return ''.join(all_lines[encoding_line + 1:encoding_line + 1 + num_lines])
-
- return ''.join(all_lines[:num_lines])
+ all_lines = [f.readline() for _ in range(num_lines + 2)] # Read extra lines to account for shebang/encoding
+
+ header_start = get_header_start_line(file_path)
+ return ''.join(all_lines[header_start:header_start + num_lines])
except Exception as e:
print(f"Warning: Could not read {file_path}: {e}")
return ""
@@ -142,8 +151,7 @@ def generate_diff(file_path: str) -> str:
comment_style, has_extra_line = info
current_year = datetime.now().year
- encoding_line = get_encoding_line_number(file_path)
- header_start = encoding_line + 1 if encoding_line >= 0 else 0
+ header_start = get_header_start_line(file_path)
has_copyright = any('Copyright' in line or 'SPDX-License-Identifier' in line
for line in original_lines[header_start:header_start + 5])
diff --git a/docs/dev/coding_style.md b/docs/dev/coding_style.md
index 3325aa2c6ca..016f7127ab9 100644
--- a/docs/dev/coding_style.md
+++ b/docs/dev/coding_style.md
@@ -26,6 +26,8 @@ Python (`.py`):
# SPDX-License-Identifier: Apache-2.0
```
+In Python files the header may be preceded by a shebang and/or an encoding declaration.
+
To check and fix headers automatically, the script at `.github/scripts/check_copyright.py` can be used. If it exits non-zero it writes `copyright_fixes.diff`, which can be applied with `patch -p1 < copyright_fixes.diff`.
Note: automatically generated patch can occasionally produce incorrect changes — in some cases, the manual fix is required.
diff --git a/src/plugins/intel_gpu/src/graph/common_utils/kernels_db_gen.py b/src/plugins/intel_gpu/src/graph/common_utils/kernels_db_gen.py
index 83015585a0e..ba3f21b6775 100644
--- a/src/plugins/intel_gpu/src/graph/common_utils/kernels_db_gen.py
+++ b/src/plugins/intel_gpu/src/graph/common_utils/kernels_db_gen.py
@@ -87,7 +87,7 @@ class Code2CHeaders(object):
return self.process_includes(included_content, include_paths, processed_includes)
return ''
- content = re.sub(r'#include\s+"([^"]+)"(\s+\[\[no_opt\]\])?', replace_include, content)
+ content = re.sub(r'#\s*include\s+"([^"]+)"(\s+\[\[no_opt\]\])?', replace_include, content)
return '\n'.join(batch_headers) + '\n' + content.strip()
diff --git a/src/plugins/intel_gpu/src/graph/common_utils/test_kernels_db_gen.py b/src/plugins/intel_gpu/src/graph/common_utils/test_kernels_db_gen.py
index d0122dfebfc..3f237b099f7 100644
--- a/src/plugins/intel_gpu/src/graph/common_utils/test_kernels_db_gen.py
+++ b/src/plugins/intel_gpu/src/graph/common_utils/test_kernels_db_gen.py
@@ -192,5 +192,27 @@ int result1=GET_VECTOR_ELEMENT(vec,0);
os.remove(self.header_path)
os.remove(self.no_opt_header_path)
+ def test_indented_includes(self):
+ os.makedirs(self.headers_folder, exist_ok=True)
+ with open(self.header_path, "w") as f:
+ f.write("#define REGULAR_HEADER_MACRO 1\n")
+
+ processor = Code2CHeaders(self.kernels_folder, self.headers_folder, "cm")
+ for directive in ("#include", "# include"):
+ with self.subTest(directive=directive):
+ content = f"""
+#if defined(SOME_EMU)
+{directive} "emu/missing_header.h"
+#endif
+{directive} "include/header.cl"
+"""
+ result = processor.process_includes(content, [self.kernels_folder])
+
+ self.assertNotIn("include", result)
+ self.assertIn("#define REGULAR_HEADER_MACRO 1", result)
+
+ os.remove(self.header_path)
+
+
if __name__ == '__main__':
unittest.main()