feat(gpu): add function to find includes in C/C++ files

This commit is contained in:
Anthony Berg 2025-06-30 13:53:18 +02:00
parent 5eab354968
commit ecfdaaa39e
2 changed files with 22 additions and 2 deletions

View File

@ -1,3 +1,4 @@
import re
from pathlib import Path
@ -6,3 +7,22 @@ def get_project_root() -> Path:
Gets the root directory of the project.
"""
return Path(__file__).parent.parent.parent
def get_includes(file: str, local: bool = False) -> list[str]:
"""
Finds all the includes used in C/C++ in a string from a file.
Args:
file: The text of the code.
local: Only gets the includes that are explicitly defined as being local
(includes with quotes instead of ``<>``)
Returns:
A list of the includes (without ``#include ""``) from the given string.
"""
pattern = '^\\W*#include\\W+(.+?)\\W*$'
if local:
pattern = '^\\W*#include\\s+"(.+?)"\\W*$'
return re.findall(pattern, file, re.M)

View File

@ -5,7 +5,7 @@ import re
import logging
from hashlib import md5
from GPUSimulators.common.utils import get_project_root
from GPUSimulators.common.utils import get_project_root, get_includes
class Context(object):
@ -83,7 +83,7 @@ class Context(object):
kernel_hasher.update(str(modified).encode('utf-8'))
# Find all the includes
includes = re.findall('^\\W*#include\\W+(.+?)\\W*$', file_str, re.M)
includes = get_includes(file_str)
# Iterate through everything that looks like is an ``include``
for include_file in includes: