gemini-code-assist[bot] commented on code in PR #19722:
URL: https://github.com/apache/tvm/pull/19722#discussion_r3391159345
##########
python/tvm/script/parser/core/diagnostics.py:
##########
@@ -185,73 +185,83 @@ def getsourcelines(obj):
inspect.getfile = _patched_inspect_getfile
+def _format_source_snippet(
+ source_lines: list, lineno: int, col_offset: int, end_col_offset: int
+) -> str:
+ """Format a source code snippet with a column marker.
+
+ Parameters
+ ----------
+ source_lines : list of str
+ Lines of the source code.
+
+ lineno : int
+ 1-based line number in the source.
+
+ col_offset : int
+ 1-based starting column (inclusive).
+
+ end_col_offset : int
+ 1-based ending column (exclusive).
+
+ Returns
+ -------
+ snippet : str
+ Formatted source snippet with a caret-marker line.
+ """
+ idx = lineno - 1
+ if 0 <= idx < len(source_lines):
+ line_text = source_lines[idx].rstrip("\n")
+ line_header = f" {lineno} "
+ no_line_header = " " * len(line_header)
+ marker = ""
+ for i in range(1, len(line_text) + 1):
+ if col_offset <= i < end_col_offset:
+ marker += "^"
+ else:
+ marker += " "
+ return f"{no_line_header}| \n{line_header}|
{line_text}\n{no_line_header}| {marker}"
+ return ""
+
+
class Diagnostics:
"""Diagnostics class for error reporting in parser.
+ Formats parse errors with source location context and raises directly,
+ without going through DiagnosticContext.
+
Parameters
----------
source : Source
The source code.
-
- ctx : diagnostics.DiagnosticContext
- The diagnostic context for diagnostics.
"""
source: Source
- ctx: diagnostics.DiagnosticContext
def __init__(self, source: Source):
- mod = IRModule()
- mod.source_map.add(source.source_name, source.full_source)
self.source = source
- self.ctx = diagnostics.DiagnosticContext(mod,
diagnostics.get_renderer())
- def _emit(self, node: doc.AST, message: str, level:
diagnostics.DiagnosticLevel) -> None:
- """Emit a diagnostic.
+ def error(self, node: doc.AST, message: str) -> None:
+ """Emit a diagnostic error by raising with source location context.
Parameters
----------
node : doc.AST
- The node with diagnostic information.
+ The node with diagnostic error.
message : str
The diagnostic message.
-
- level : diagnostics.DiagnosticLevel
- The diagnostic level.
"""
lineno = getattr(node, "lineno", 1)
col_offset = getattr(node, "col_offset", self.source.start_column)
- end_lineno = getattr(node, "end_lineno", lineno)
end_col_offset = getattr(node, "end_col_offset", col_offset)
lineno += self.source.start_line - 1
- end_lineno += self.source.start_line - 1
col_offset += self.source.start_column + 1
end_col_offset += self.source.start_column + 1
- self.ctx.emit(
- diagnostics.Diagnostic(
- level=level,
- span=Span(
- source_name=SourceName(self.source.source_name),
- line=lineno,
- end_line=end_lineno,
- column=col_offset,
- end_column=end_col_offset,
- ),
- message=message,
- )
- )
- def error(self, node: doc.AST, message: str) -> None:
- """Emit a diagnostic error.
-
- Parameters
- ----------
- node : doc.AST
- The node with diagnostic error.
+ source_lines = self.source.full_source.splitlines(keepends=True)
+ snippet = _format_source_snippet(source_lines, lineno, col_offset,
end_col_offset)
- message : str
- The diagnostic message.
- """
- self._emit(node, message, diagnostics.DiagnosticLevel.ERROR)
- self.ctx.render()
+ location = f"{self.source.source_name}:{lineno}:{col_offset}"
+ formatted = f"error: {message}\n --> {location}\n{snippet}"
+ raise DiagnosticError(formatted)
Review Comment:

The refactoring of the `Diagnostics` class appears to have removed support
for multi-line error reporting. The previous implementation utilized
`end_lineno` from the AST node to create a `Span` object that could cover
multiple lines. The new implementation, however, only uses `lineno`, and
`_format_source_snippet` is designed to format a single line.
If the parser can generate diagnostics that span multiple lines, this change
could be a regression in error reporting quality. Was this simplification
intentional? If multi-line errors are a possibility, it would be beneficial to
restore this functionality.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]