-
Notifications
You must be signed in to change notification settings - Fork 14k
Add Flang-Tidy: A Fortran Static Analysis Tool #143883
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
base: main
Are you sure you want to change the base?
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
You can test this locally with the following command:darker --check --diff -r HEAD~1...HEAD flang/tools/docs/conf.py flang/tools/test/flang-tidy/check_flang_tidy.py flang/tools/test/lit.cfg.py View the diff from darker here.--- test/flang-tidy/check_flang_tidy.py 2025-06-12 11:37:10.000000 +0000
+++ test/flang-tidy/check_flang_tidy.py 2025-06-12 12:49:17.758998 +0000
@@ -104,13 +104,11 @@
if not has_check_message:
self.expect_no_diagnosis = True
if self.expect_no_diagnosis and self.has_check_messages:
- sys.exit(
- "%s not found in the input" % self.messages.prefix
- )
+ sys.exit("%s not found in the input" % self.messages.prefix)
assert self.has_check_messages or self.expect_no_diagnosis
def prepare_test_inputs(self):
cleaned_test = re.sub("// *CHECK-[A-Z0-9\\-]*:[^\r\n]*", "//", self.input_text)
write_file(self.temp_file_name, cleaned_test)
@@ -121,12 +119,12 @@
"flang-tidy",
self.temp_file_name,
]
+ ["--checks=" + self.check_name]
+ self.extra_args
- #+ ["--"]
- #+ self.flang_extra_args
+ # + ["--"]
+ # + self.flang_extra_args
)
if self.expect_flang_tidy_error:
args.insert(0, "not")
print("Running " + repr(args) + "...")
flang_tidy_output = try_run(args)
--- test/lit.cfg.py 2025-06-12 11:37:10.000000 +0000
+++ test/lit.cfg.py 2025-06-12 12:49:17.778699 +0000
@@ -14,19 +14,11 @@
# testFormat: The test format to use to interpret tests.
config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell)
# suffixes: A list of file extensions to treat as test files.
-config.suffixes = [
- ".f",
- ".f90",
- ".f95",
- ".f03",
- ".f08",
- ".mod",
- ".test"
-]
+config.suffixes = [".f", ".f90", ".f95", ".f03", ".f08", ".mod", ".test"]
# Exclude 'Inputs' directories as they are for test dependencies.
config.excludes = ["Inputs"]
# test_source_root: The root path where tests are located.
|
This is a massive PR. Would it be possible to carve it up further into infrastructure changes to introduce the new tool, documentation updates, and addition of the analysis? This would make reviewing easier. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like an awful lot of code to add one new warning message that could easily be part of the flang-new compiler itself, perhaps controlled by a new UsageWarning.
I agree. This is a lot to review at once. |
Summary
This PR introduces flang-tidy, a new Fortran static analysis tool for the LLVM project. flang-tidy is a flang-based "linter" that provides an extensible framework for diagnosing and fixing typical programming errors in Fortran code, including style violations, interface misuse, and bugs detectable through static analysis.
This PR includes only the core infrastructure of flang-tidy with only a single example check. All the other checks that we have implemented will be presented in a follow-up PR to not overwhelm the reviewers.
Motivation
The Fortran ecosystem has lacked a comprehensive, modern static analysis tool comparable to clang-tidy for C++. With the maturation of flang as LLVM's Fortran frontend, we now have the infrastructure needed to build such a tool.
Design
The below UML diagram shows the rough architecture of flang-tidy to give a brief overview of our design. This diagram is not exactly reflected as-is in the code, but should give reviewers a general idea of the layout.
Key Features
Check Categories
bugprone-
- Detects bug-prone code constructsmodernize-
- Advocates modern Fortran language featuresopenmp-
- Analyzes OpenMP constructs for correctnessperformance-
- Identifies performance-related issuesreadability-
- Improves code readability and maintainabilityUsage Examples
Future Work
Reviewers
@mjklemm @skatrak