Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions llvm/utils/filecheck_lint/filecheck_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,40 @@ class FileRange:
"""Stores the coordinates of a span on a single line within a file.
Comment thread
klensy marked this conversation as resolved.
Outdated

Attributes:
line: the line number
start_column: the (inclusive) column where the span starts
end_column: the (inclusive) column where the span ends
content: line str
start_byte: the (inclusive) byte offset the span starts
end_byte: the (inclusive) byte offset the span ends
"""

line: int
start_column: int
end_column: int
content: str
start_byte: int
end_byte: int

def __init__(
self, content: str, start_byte: int, end_byte: int
): # pylint: disable=g-doc-args
"""Derives a span's coordinates based on a string and start/end bytes.
Comment thread
klensy marked this conversation as resolved.
Outdated
"""
Stores the coordinates of a span based on a string and start/end bytes.

`start_byte` and `end_byte` are assumed to be on the same line.
"""
content_before_span = content[:start_byte]
self.line = content_before_span.count("\n") + 1
self.start_column = start_byte - content_before_span.rfind("\n")
self.end_column = self.start_column + (end_byte - start_byte - 1)
self.content = content
self.start_byte = start_byte
self.end_byte = end_byte

def __str__(self) -> str:
return f"{self.line}:{self.start_column}-{self.end_column}"
def as_str(self):
"""
Derives span from line and coordinates.

start_column: the (inclusive) column where the span starts
end_column: the (inclusive) column where the span ends
"""
content_before_span = self.content[: self.start_byte]
Comment thread
klensy marked this conversation as resolved.
Outdated
line = content_before_span.count("\n") + 1
start_column = self.start_byte - content_before_span.rfind("\n")
end_column = start_column + (self.end_byte - self.start_byte - 1)

return f"{line}:{start_column}-{end_column}"


class Diagnostic:
Expand Down Expand Up @@ -134,7 +145,7 @@ def __init__(
self.fix = fix

def __str__(self) -> str:
return f"{self.filepath}:" + str(self.filerange) + f": {self.summary()}"
return f"{self.filepath}:" + self.filerange.as_str() + f": {self.summary()}"

def summary(self) -> str:
return (
Expand Down Expand Up @@ -228,7 +239,8 @@ def find_best_match(typo):
)

potential_directives = find_potential_directives(content)

# Cache score and best_match to skip recalculating.
score_and_best_match_for_potential_directive = dict()
for filerange, potential_directive in potential_directives:
# TODO(bchetioui): match count directives more finely. We skip directives
# starting with 'CHECK-COUNT-' for the moment as they require more complex
Expand All @@ -244,7 +256,16 @@ def find_best_match(typo):
if len(potential_directive) > max(map(len, all_directives)) + threshold:
continue

score, best_match = find_best_match(potential_directive)
if potential_directive not in score_and_best_match_for_potential_directive:
score, best_match = find_best_match(potential_directive)
score_and_best_match_for_potential_directive[potential_directive] = (
score,
best_match,
)
else:
score, best_match = score_and_best_match_for_potential_directive[
potential_directive
]
if score == 0: # This is an actual directive, ignore.
continue
elif score <= threshold and best_match not in _ignore:
Expand Down
18 changes: 3 additions & 15 deletions llvm/utils/filecheck_lint/filecheck_lint_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,15 @@ def test_find_potential_directives_comment_prefix(self):
results = list(fcl.find_potential_directives(content))
assert len(results) == 3
pos, match = results[0]
assert (
pos.line == 1
and pos.start_column == len("junk; ") + 1
and pos.end_column == len(lines[0]) - 1
)
assert pos.as_str() == "1:7-11"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind deriving your string here (and below) using the previous start_column and end_column logic?
These explain the expected behaviour better, imo.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you describe what you mean?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry---I was not clear. I meant to ask whether 7 could be assigned from len("junk// ") + 1 and 11 could be assigned from len(lines[1]) - 1.

I think this makes the expected behaviour clearer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like copypaste and not maintainable - change in input lines only will break tests without fixes here too.
Maybe add comment like:

junk; CHCK1:
      |   |
      |   column 11
      column 7

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like copypaste and not maintainable - change in input lines only will break tests without fixes here too.

The same tests will break in both cases.

Maybe add comment like:

Adding a comment here is strictly worse; that too will get out of sync, but will do so completely silently since the comment doesn't get executed.

Anyway, this is not a huge deal, and you seem to feel strongly about this, so let's keep it as is.

assert match == "CHCK1"

pos, match = results[1]
assert (
pos.line == 2
and pos.start_column == len("junk// ") + 1
and pos.end_column == len(lines[1]) - 1
)
assert pos.as_str() == "2:8-12"
assert match == "CHCK2"

pos, match = results[2]
assert (
pos.line == 3
and pos.start_column == 1
and pos.end_column == len(lines[2]) - 1
)
assert pos.as_str() == "3:1-10"
assert match == "SOME CHCK3"

def test_levenshtein(self):
Expand Down