Skip to content

[Lint] Add option --lint-abort-on-error #81999

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

Merged
merged 1 commit into from
Feb 20, 2024

Conversation

jasilvanus
Copy link
Contributor

This option makes the lint pass abort if errors were found.

This is intended to help lit testing where the lint pass is used and lint errors should be detected.
Previously, this required checking for non-empty stderr.

This option makes the lint pass abort if errors were found.

This is intended to help lit testing where the lint pass is used
and lint errors should be detected.
Previously, this required checking for non-empty stderr.
@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Feb 16, 2024
@llvmbot
Copy link
Member

llvmbot commented Feb 16, 2024

@llvm/pr-subscribers-llvm-analysis

Author: Jannik Silvanus (jasilvanus)

Changes

This option makes the lint pass abort if errors were found.

This is intended to help lit testing where the lint pass is used and lint errors should be detected.
Previously, this required checking for non-empty stderr.


Full diff: https://github.com/llvm/llvm-project/pull/81999.diff

2 Files Affected:

  • (modified) llvm/lib/Analysis/Lint.cpp (+9)
  • (added) llvm/test/Analysis/Lint/abort-on-error.ll (+10)
diff --git a/llvm/lib/Analysis/Lint.cpp b/llvm/lib/Analysis/Lint.cpp
index 16635097d20afe..0694c2995dfcce 100644
--- a/llvm/lib/Analysis/Lint.cpp
+++ b/llvm/lib/Analysis/Lint.cpp
@@ -77,6 +77,11 @@
 
 using namespace llvm;
 
+static const char LintAbortOnErrorArgName[] = "lint-abort-on-error";
+static cl::opt<bool>
+    LintAbortOnError(LintAbortOnErrorArgName, cl::init(false),
+                     cl::desc("In the Lint pass, abort on errors."));
+
 namespace {
 namespace MemRef {
 static const unsigned Read = 1;
@@ -715,6 +720,10 @@ PreservedAnalyses LintPass::run(Function &F, FunctionAnalysisManager &AM) {
   Lint L(Mod, DL, AA, AC, DT, TLI);
   L.visit(F);
   dbgs() << L.MessagesStr.str();
+  if (LintAbortOnError && !L.MessagesStr.str().empty())
+    report_fatal_error(Twine("Linter found errors, aborting. (enabled by --") +
+                           LintAbortOnErrorArgName + ")",
+                       false);
   return PreservedAnalyses::all();
 }
 
diff --git a/llvm/test/Analysis/Lint/abort-on-error.ll b/llvm/test/Analysis/Lint/abort-on-error.ll
new file mode 100644
index 00000000000000..3efc38aea887cc
--- /dev/null
+++ b/llvm/test/Analysis/Lint/abort-on-error.ll
@@ -0,0 +1,10 @@
+; RUN: not opt -passes=lint -disable-output --lint-abort-on-error %s 2>&1 | FileCheck %s
+
+; CHECK: Undefined behavior: Division by zero
+; CHECK-NEXT:   %b = sdiv i32 %a, 0
+; CHECK-NEXT: LLVM ERROR: Linter found errors, aborting. (enabled by --lint-abort-on-error)
+
+define i32 @sdiv_by_zero(i32 %a) {
+  %b = sdiv i32 %a, 0
+  ret i32 %b
+}

@jasilvanus jasilvanus requested a review from Flakebi February 19, 2024 11:21
Copy link
Member

@Flakebi Flakebi left a comment

Choose a reason for hiding this comment

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

Nice! LGTM

@jasilvanus jasilvanus merged commit 756ff96 into llvm:main Feb 20, 2024
@jasilvanus jasilvanus deleted the jsilvanu/lint-abort-on-error branch February 20, 2024 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:analysis Includes value tracking, cost tables and constant folding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants