-
Notifications
You must be signed in to change notification settings - Fork 132
Add Fortran/Fypp static analysis linter #1193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sbryngelson
wants to merge
4
commits into
MFlowCode:master
Choose a base branch
from
sbryngelson:feat/source-linter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+199
−6
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c184ae2
Add Fortran/Fypp static analysis linter
sbryngelson 6dc36e7
Add -Wconversion to debug builds and track in cleanliness CI
sbryngelson 9dc43a7
Fix pylint warnings in lint_source.py
sbryngelson 78124f3
Fix check_hardcoded_byte_size false-positive on inline comments
sbryngelson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| """Static analysis for Fortran/Fypp source code. | ||
|
|
||
| Checks for patterns that indicate copy-paste bugs, non-standard constructs, | ||
| and hardcoded assumptions that break under different build configurations. | ||
| """ | ||
|
|
||
| import re | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| # Source directory to scan (relative to repo root) | ||
| SRC_DIR = "src" | ||
|
|
||
| # Minimum stripped line length to consider for duplicate detection. | ||
| # Lines shorter than this (e.g. "end if", "end do") are ignored. | ||
| MIN_DUP_LINE_LEN = 40 | ||
|
|
||
|
|
||
| def _is_comment_or_blank(stripped: str) -> bool: | ||
| """True if stripped line is blank, a Fortran comment, or a Fypp directive.""" | ||
| return not stripped or stripped.startswith("!") or stripped.startswith("#:") | ||
|
|
||
|
|
||
| def _fortran_fpp_files(src_dir: Path): | ||
| """Yield all .f90 and .fpp files under src/.""" | ||
| yield from sorted(src_dir.rglob("*.f90")) | ||
| yield from sorted(src_dir.rglob("*.fpp")) | ||
|
|
||
|
|
||
| def _check_single_fypp_list(full_line: str, rel: Path, start_line: int) -> list[str]: | ||
| """Parse one Fypp ``#:for ... in [...]`` line and return errors for duplicates.""" | ||
| errors: list[str] = [] | ||
|
|
||
| bracket_start = full_line.find("[") | ||
| bracket_end = full_line.rfind("]") | ||
| if not 0 <= bracket_start < bracket_end: | ||
| return errors | ||
|
|
||
| list_content = full_line[bracket_start + 1:bracket_end] | ||
| list_content = list_content.replace("&", "") | ||
|
|
||
| # Extract single- or double-quoted entries | ||
| entries = re.findall(r"['\"]([^'\"]*)['\"]", list_content) | ||
|
|
||
| seen: dict[str, int] = {} | ||
| for pos, entry in enumerate(entries, 1): | ||
| if entry in seen: | ||
| errors.append( | ||
| f" {rel}:{start_line} Fypp list has duplicate" | ||
| f" entry '{entry}' (positions {seen[entry]}" | ||
| f" and {pos})." | ||
| " Fix: one copy is likely a typo for a" | ||
| " different variable" | ||
| ) | ||
| else: | ||
| seen[entry] = pos | ||
|
|
||
| return errors | ||
|
|
||
|
|
||
| def check_fypp_list_duplicates(repo_root: Path) -> list[str]: | ||
| """Check for duplicate entries in Fypp ``#:for VAR in [...]`` lists. | ||
|
|
||
| Copy-paste errors in broadcast lists or loop variable lists can silently | ||
| skip a variable while broadcasting another one twice. | ||
| """ | ||
| errors: list[str] = [] | ||
| src_dir = repo_root / SRC_DIR | ||
|
|
||
| for fpp in sorted(src_dir.rglob("*.fpp")): | ||
| lines = fpp.read_text(encoding="utf-8").splitlines() | ||
| rel = fpp.relative_to(repo_root) | ||
|
|
||
| i = 0 | ||
| while i < len(lines): | ||
| line = lines[i].strip() | ||
| if line.startswith("#:for") and " in " in line and "[" in line: | ||
| start_line = i + 1 # 1-indexed for display | ||
|
|
||
| # Accumulate across Fortran-style '&' continuation lines | ||
| full = line | ||
| while full.rstrip().endswith("&") and i + 1 < len(lines): | ||
| i += 1 | ||
| full += " " + lines[i].strip() | ||
|
|
||
| errors.extend(_check_single_fypp_list(full, rel, start_line)) | ||
| i += 1 | ||
|
|
||
| return errors | ||
|
|
||
|
|
||
| def check_duplicate_lines(repo_root: Path) -> list[str]: | ||
| """Flag identical adjacent non-trivial source lines. | ||
|
|
||
| Exact duplicate consecutive lines are almost always copy-paste errors: | ||
| a duplicated accumulation, a repeated subroutine argument, etc. | ||
| """ | ||
| errors: list[str] = [] | ||
| src_dir = repo_root / SRC_DIR | ||
|
|
||
| for src in _fortran_fpp_files(src_dir): | ||
| lines = src.read_text(encoding="utf-8").splitlines() | ||
| rel = src.relative_to(repo_root) | ||
|
|
||
| prev_stripped = "" | ||
| for i, line in enumerate(lines): | ||
| stripped = line.strip() | ||
| if ( | ||
| stripped == prev_stripped | ||
| and len(stripped) >= MIN_DUP_LINE_LEN | ||
| and not _is_comment_or_blank(stripped) | ||
| ): | ||
| display = stripped[:72] | ||
| if len(stripped) > 72: | ||
| display += "..." | ||
| errors.append( | ||
| f" {rel}:{i + 1} identical to previous line:" | ||
| f" '{display}'." | ||
| " Fix: check for accidental copy-paste" | ||
sbryngelson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| prev_stripped = stripped | ||
|
|
||
| return errors | ||
|
|
||
|
|
||
| def check_hardcoded_byte_size(repo_root: Path) -> list[str]: | ||
| """Flag ``int(8._wp, ...)`` patterns that assume 8-byte reals. | ||
|
|
||
| When MFC is built in single precision (``wp = real32``), reals are | ||
| 4 bytes. Hard-coding 8 makes MPI I/O read/write the wrong amount. | ||
| Use ``storage_size(0._wp)/8`` instead. | ||
| """ | ||
| errors: list[str] = [] | ||
| src_dir = repo_root / SRC_DIR | ||
| byte_re = re.compile(r"\bint\s*\(\s*8\._wp\b", re.IGNORECASE) | ||
|
|
||
| for src in _fortran_fpp_files(src_dir): | ||
| lines = src.read_text(encoding="utf-8").splitlines() | ||
| rel = src.relative_to(repo_root) | ||
|
|
||
| for i, line in enumerate(lines): | ||
| stripped = line.strip() | ||
| if _is_comment_or_blank(stripped): | ||
| continue | ||
| if byte_re.search(stripped.split("!")[0]): | ||
sbryngelson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| errors.append( | ||
| f" {rel}:{i + 1} hard-codes 8-byte real size." | ||
| " Fix: use 'storage_size(0._wp)/8' instead of" | ||
| " '8._wp'" | ||
sbryngelson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| return errors | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def main(): | ||
| repo_root = Path(__file__).resolve().parents[2] | ||
|
|
||
| all_errors: list[str] = [] | ||
| all_errors.extend(check_fypp_list_duplicates(repo_root)) | ||
| all_errors.extend(check_duplicate_lines(repo_root)) | ||
| all_errors.extend(check_hardcoded_byte_size(repo_root)) | ||
|
|
||
| if all_errors: | ||
| print("Fortran/Fypp source analysis failed:") | ||
| for e in all_errors: | ||
| print(e) | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.