Skip to content

Commit

Permalink
feat(code_generator): indentation of pragmas
Browse files Browse the repository at this point in the history
  • Loading branch information
goerlibe authored and lukasrothenberger committed Nov 15, 2023
1 parent 9223b07 commit 1ae36ea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
11 changes: 9 additions & 2 deletions discopop_library/CodeGenerator/classes/ContentBuffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,25 @@ 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:
if idx + 1 < len(self.lines):
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(
Expand All @@ -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.
Expand All @@ -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 = "//<DiscoPoP-IGNORED> "
else:
Expand Down
3 changes: 3 additions & 0 deletions discopop_library/CodeGenerator/classes/Line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())]

0 comments on commit 1ae36ea

Please sign in to comment.