Skip to content

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

Originns
Copy link

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.

grafik

Key Features

Check Categories

  • bugprone- - Detects bug-prone code constructs
  • modernize- - Advocates modern Fortran language features
  • openmp- - Analyzes OpenMP constructs for correctness
  • performance- - Identifies performance-related issues
  • readability- - Improves code readability and maintainability

Usage Examples

# Basic usage with compilation flags
flang-tidy test.f90 -- -Imy_project/include -DMY_DEFINES

# Enable specific check categories
flang-tidy test.f90 -checks=-*,bugprone-*,-bugprone-arithmetic-*

Future Work

  • Additional check categories (security, HPC-specific patterns)
  • Automatic fix suggestions for more check types
  • Enhanced OpenMP analysis capabilities

Reviewers

@mjklemm @skatrak

Copy link

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 @ followed by their GitHub username.

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.

@llvmbot llvmbot added the flang Flang issues not falling into any other category label Jun 12, 2025
Copy link

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Developer Policy and LLVM Discourse for more information.

Copy link

⚠️ Python code formatter, darker found issues in your code. ⚠️

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.

@mjklemm
Copy link
Contributor

mjklemm commented Jun 12, 2025

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.

@klausler klausler removed their request for review June 12, 2025 15:48
Copy link
Contributor

@klausler klausler left a 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.

@tarunprabhu
Copy link
Contributor

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.

I agree. This is a lot to review at once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants