From 1ae36ea608987bdcdab2d068b09c057ff68d8e1c Mon Sep 17 00:00:00 2001 From: goerlibe <23436477+goerlibe@users.noreply.github.com> Date: Fri, 10 Nov 2023 10:12:50 +0100 Subject: [PATCH] feat(code_generator): indentation of pragmas --- .../CodeGenerator/classes/ContentBuffer.py | 11 +++++++++-- discopop_library/CodeGenerator/classes/Line.py | 3 +++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/discopop_library/CodeGenerator/classes/ContentBuffer.py b/discopop_library/CodeGenerator/classes/ContentBuffer.py index a49831fa7..2da03f6e3 100644 --- a/discopop_library/CodeGenerator/classes/ContentBuffer.py +++ b/discopop_library/CodeGenerator/classes/ContentBuffer.py @@ -59,14 +59,16 @@ def get_modified_source_code(self) -> str: result += line.content return result - def append_line_before(self, parent_line_num: int, line: Line): + def append_line_before(self, parent_line_num: int, line: Line, match_indentation: bool = True): """Appends line before the specified parent_line_num""" for idx, potential_parent_line in enumerate(self.lines): if potential_parent_line.line_num == parent_line_num: + if match_indentation: + line.content = potential_parent_line.get_indentation() + line.content self.lines.insert(idx, line) return - def append_line_after(self, parent_line_num: int, line: Line): + def append_line_after(self, parent_line_num: int, line: Line, match_indentation: bool = True): """Appends line after the specified parent_line_num""" for idx, potential_parent_line in enumerate(self.lines): if potential_parent_line.line_num == parent_line_num: @@ -74,6 +76,8 @@ def append_line_after(self, parent_line_num: int, line: Line): self.lines.insert(idx + 1, line) else: self.lines.append(line) + if match_indentation: + line.content = potential_parent_line.get_indentation() + line.content return def add_pragma( @@ -86,6 +90,7 @@ def add_pragma( compile_check_command: Optional[str] = None, CC="clang", CXX="clang++", + match_indentation: bool = True, ) -> bool: """insert pragma into the maintained list of source code lines. Returns True if the pragma resulted in a valid (resp. compilable) code transformation. @@ -106,6 +111,8 @@ def add_pragma( # construct line pragma_line = self.line_type(pragma.start_line) + + # adding as comment if add_as_comment: pragma_line.content = "// " else: diff --git a/discopop_library/CodeGenerator/classes/Line.py b/discopop_library/CodeGenerator/classes/Line.py index eb3b2d199..e43010033 100644 --- a/discopop_library/CodeGenerator/classes/Line.py +++ b/discopop_library/CodeGenerator/classes/Line.py @@ -22,3 +22,6 @@ def __init__(self, parent_line_num: int, line_num=None, content=""): self.owns_region = None self.belongs_to_regions = [] self.belongs_to_original_line = parent_line_num + + def get_indentation(self) -> str: + return self.content[: len(self.content) - len(self.content.lstrip())]