Skip to content

Commit

Permalink
fix after rebase
Browse files Browse the repository at this point in the history
Signed-off-by: Jade Abraham <[email protected]>
  • Loading branch information
jabraham17 committed Dec 11, 2024
1 parent dc7fdb0 commit 3623cf8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
4 changes: 1 addition & 3 deletions tools/chplcheck/src/chplcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@

def print_violation(loc: RuleLocation, name: str):
first_line, _ = loc.start()
print(
"{}:{}: node violates rule {}".format(loc.path(), first_line, name)
)
print("{}:{}: node violates rule {}".format(loc.path(), first_line, name))


def load_module(driver: LintDriver, file_path: str):
Expand Down
30 changes: 23 additions & 7 deletions tools/chplcheck/src/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ def rules_and_descriptions(self):
# Use a dict in case a rule is registered multiple times.
to_return = {}

for rule in itertools.chain(self.BasicRules, self.AdvancedRules, self.LocationRules):
for rule in itertools.chain(
self.BasicRules, self.AdvancedRules, self.LocationRules
):
to_return[rule.name] = rule.check_func.__doc__

to_return = list(to_return.items())
Expand Down Expand Up @@ -139,7 +141,11 @@ def _check_rule(
self,
context: chapel.Context,
root: chapel.AstNode,
rule: Union[rule_types.BasicRule, rule_types.AdvancedRule, rule_types.LocationRule],
rule: Union[
rule_types.BasicRule,
rule_types.AdvancedRule,
rule_types.LocationRule,
],
) -> Iterator[rule_types.CheckResult]:

# If we should ignore the rule no matter the node, no reason to run
Expand Down Expand Up @@ -193,7 +199,9 @@ def fixit(self, checkfunc):

def decorator_fixit(func):
found = False
for rule in itertools.chain(self.BasicRules, self.AdvancedRules, self.LocationRules):
for rule in itertools.chain(
self.BasicRules, self.AdvancedRules, self.LocationRules
):
if rule.name == checkfunc.__name__:
rule.fixit_funcs.append(func)
found = True
Expand Down Expand Up @@ -253,7 +261,9 @@ def wrapper_advanced_rule(*args, **kwargs):
else:
return decorator_advanced_rule(_func)

def location_rule(self, _func=None, *, default=True, settings: List[str]=[]):
def location_rule(
self, _func=None, *, default=True, settings: List[str] = []
):
"""
This method is a decorator for adding location-only based rules to the
driver. A location rule is a function that gets called on a filepath
Expand Down Expand Up @@ -296,7 +306,9 @@ def validate_rule_settings(self) -> List[str]:
"""

all_rule_settings = set()
for rule in itertools.chain(self.BasicRules, self.AdvancedRules, self.LocationRules):
for rule in itertools.chain(
self.BasicRules, self.AdvancedRules, self.LocationRules
):
all_rule_settings.update(rule.settings)

unrecognized_settings = []
Expand All @@ -315,10 +327,14 @@ def run_checks(
"""

for ast in asts:
for rule in itertools.chain(self.BasicRules, self.AdvancedRules, self.LocationRules):
for rule in itertools.chain(
self.BasicRules, self.AdvancedRules, self.LocationRules
):
for toreport in self._check_rule(context, ast, rule):
node = toreport[1]
if node and self._has_internal_name(node):
if not self.config.check_internal_prefixes and (
not node or self.has_internal_prefix(node)
):
continue

yield toreport
24 changes: 19 additions & 5 deletions tools/chplcheck/src/rule_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def _build_ignore_fixit(
ignore.description = "Ignore this warning"
return ignore


@dataclass
class RuleLocation:
path_: str
Expand All @@ -47,8 +48,10 @@ class RuleLocation:

def path(self) -> str:
return self.path_

def start(self) -> typing.Tuple[int, int]:
return self.start_

def end(self) -> typing.Tuple[int, int]:
return self.end_

Expand Down Expand Up @@ -188,7 +191,10 @@ def fixits(self, context: chapel.Context, name: str) -> typing.List[Fixit]:
to_return = self._fixits
return to_return

_LocationRuleResult = typing.Iterator[typing.Union[RuleLocation, LocationRuleResult]]

_LocationRuleResult = typing.Iterator[
typing.Union[RuleLocation, LocationRuleResult]
]
"""Internal type for location rule results"""


Expand All @@ -198,11 +204,14 @@ def fixits(self, context: chapel.Context, name: str) -> typing.List[Fixit]:
"""Function type for location rules"""



RuleResult = typing.Union[_BasicRuleResult, _AdvancedRuleResult, _LocationRuleResult]
RuleResult = typing.Union[
_BasicRuleResult, _AdvancedRuleResult, _LocationRuleResult
]
"""Union type for all rule results"""

CheckResult = typing.Tuple[RuleLocation, typing.Optional[chapel.AstNode], str, typing.List[Fixit]]
CheckResult = typing.Tuple[
RuleLocation, typing.Optional[chapel.AstNode], str, typing.List[Fixit]
]


VarResultType = typing.TypeVar("VarResultType")
Expand Down Expand Up @@ -386,13 +395,18 @@ def check(
loc = RuleLocation.from_chapel(node.location())
yield (loc, node, self.name, fixits)


class LocationRule(Rule[LocationRuleResult]):
"""
Class containing all information for the driver about advanced
"""

def __init__(
self, driver, name: str, check_func: LocationRuleCheck, settings: typing.List[str]
self,
driver,
name: str,
check_func: LocationRuleCheck,
settings: typing.List[str],
) -> None:
super().__init__(driver, name, settings)
self.check_func = check_func
Expand Down
11 changes: 8 additions & 3 deletions tools/chplcheck/src/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
from chapel import *
from driver import LintDriver
from fixits import Fixit, Edit
from rule_types import BasicRuleResult, AdvancedRuleResult, LocationRuleResult, RuleLocation
from rule_types import (
BasicRuleResult,
AdvancedRuleResult,
LocationRuleResult,
RuleLocation,
)


def variables(node: AstNode):
Expand Down Expand Up @@ -1041,14 +1046,14 @@ def FixMissingInIntent(context: Context, result: AdvancedRuleResult):
return [fixit]

@driver.location_rule(settings=[".Max"])
def LineLength(_: chapel.Context, path: str, lines: List[str], Max = None):
def LineLength(_: chapel.Context, path: str, lines: List[str], Max=None):
"""
Warn for lines that exceed a maximum length.
By default, the maximum line length is 80 characters.
"""

Max = Max or "80" # default to 80 characters
Max = Max or "80" # default to 80 characters
try:
max_line_length = int(Max)
except ValueError:
Expand Down

0 comments on commit 3623cf8

Please sign in to comment.