Skip to content

Commit 4d307a8

Browse files
committed
Add option to remove preprocessor macro detection
1 parent e366144 commit 4d307a8

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

CppHeaderParser/CppHeaderParser.py

+26-16
Original file line numberDiff line numberDiff line change
@@ -2702,7 +2702,14 @@ def show(self):
27022702
for className in list(self.classes.keys()):
27032703
self.classes[className].show()
27042704

2705-
def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
2705+
def __init__(
2706+
self,
2707+
headerFileName,
2708+
argType="file",
2709+
encoding=None,
2710+
preprocessed=False,
2711+
**kwargs
2712+
):
27062713
"""Create the parsed C++ header file parse tree
27072714
27082715
headerFileName - Name of the file to parse OR actual file contents (depends on argType)
@@ -2802,21 +2809,24 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
28022809
"[ ]+", " ", supportedAccessSpecifier[i]
28032810
).strip()
28042811

2805-
# Change multi line #defines and expressions to single lines maintaining line nubmers
2806-
# Based from http://stackoverflow.com/questions/2424458/regular-expression-to-match-cs-multiline-preprocessor-statements
2807-
matches = re.findall(r"(?m)^(?:.*\\\r?\n)+.*$", headerFileStr)
2808-
is_define = re.compile(r"[ \t\v]*#[Dd][Ee][Ff][Ii][Nn][Ee]")
2809-
for m in matches:
2810-
# Keep the newlines so that linecount doesnt break
2811-
num_newlines = len([a for a in m if a == "\n"])
2812-
if is_define.match(m):
2813-
new_m = m.replace("\n", "<CppHeaderParser_newline_temp_replacement>\\n")
2814-
else:
2815-
# Just expression taking up multiple lines, make it take 1 line for easier parsing
2816-
new_m = m.replace("\\\n", " ")
2817-
if num_newlines > 0:
2818-
new_m += "\n" * (num_newlines)
2819-
headerFileStr = headerFileStr.replace(m, new_m)
2812+
if not preprocessed:
2813+
# Change multi line #defines and expressions to single lines maintaining line nubmers
2814+
# Based from http://stackoverflow.com/questions/2424458/regular-expression-to-match-cs-multiline-preprocessor-statements
2815+
matches = re.findall(r"(?m)^(?:.*\\\r?\n)+.*$", headerFileStr)
2816+
is_define = re.compile(r"[ \t\v]*#[Dd][Ee][Ff][Ii][Nn][Ee]")
2817+
for m in matches:
2818+
# Keep the newlines so that linecount doesnt break
2819+
num_newlines = len([a for a in m if a == "\n"])
2820+
if is_define.match(m):
2821+
new_m = m.replace(
2822+
"\n", "<CppHeaderParser_newline_temp_replacement>\\n"
2823+
)
2824+
else:
2825+
# Just expression taking up multiple lines, make it take 1 line for easier parsing
2826+
new_m = m.replace("\\\n", " ")
2827+
if num_newlines > 0:
2828+
new_m += "\n" * (num_newlines)
2829+
headerFileStr = headerFileStr.replace(m, new_m)
28202830

28212831
# Filter out Extern "C" statements. These are order dependent
28222832
matches = re.findall(

0 commit comments

Comments
 (0)