[llvm] [Lint] Add option --lint-abort-on-error (PR #81999)

via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 16 07:22:20 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: Jannik Silvanus (jasilvanus)

<details>
<summary>Changes</summary>

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
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
+}

``````````

</details>


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


More information about the llvm-commits mailing list