https://github.com/arichardson updated https://github.com/llvm/llvm-project/pull/236283
>From 5817a18d40392d74c77ada258ca0175c1164c49c Mon Sep 17 00:00:00 2001 From: Alex Richardson <[email protected]> Date: Tue, 27 Oct 2026 17:35:12 -0700 Subject: [PATCH 1/2] [nspr] initial commit --- llvm/unittests/Support/CMakeLists.txt | 1 + .../SourceMgrDiagnosticVerifierTest.cpp | 108 ++++++++++++++++++ .../secondary/llvm/unittests/Support/BUILD.gn | 1 + .../verify-diagnostics-highlight.mlir | 13 +++ 4 files changed, 123 insertions(+) create mode 100644 llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp create mode 100644 mlir/test/mlir-opt/verify-diagnostics-highlight.mlir diff --git a/llvm/unittests/Support/CMakeLists.txt b/llvm/unittests/Support/CMakeLists.txt index f9732ff4e0e4bb..c5db78950d21b4 100644 --- a/llvm/unittests/Support/CMakeLists.txt +++ b/llvm/unittests/Support/CMakeLists.txt @@ -90,6 +90,7 @@ add_llvm_unittest(SupportTests SignalsTest.cpp SipHashTest.cpp SourceMgrTest.cpp + SourceMgrDiagnosticVerifierTest.cpp SpecialCaseListTest.cpp SuffixTreeTest.cpp SwapByteOrderTest.cpp diff --git a/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp b/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp new file mode 100644 index 00000000000000..2ac7355fd70b6c --- /dev/null +++ b/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp @@ -0,0 +1,108 @@ +//===- unittests/Support/SourceMgrDiagnosticVerifierTest.cpp --------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/SourceMgrDiagnosticVerifier.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/raw_ostream.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +using namespace llvm; +using MatchResult = SourceMgrDiagnosticVerifier::MatchResult; +using testing::HasSubstr; + +namespace { + +class SourceMgrDiagnosticVerifierTest : public testing::Test { +protected: + SourceMgr SM; + std::string Output; + raw_string_ostream OS{Output}; + SourceMgrDiagnosticVerifier Verifier; + + const MemoryBuffer *addBuffer(StringRef Text, StringRef BufferName) { + unsigned ID = SM.AddNewSourceBuffer( + MemoryBuffer::getMemBuffer(Text, BufferName), SMLoc()); + return SM.getMemoryBuffer(ID); + } + + MatchResult processError(const MemoryBuffer *Buf, unsigned LineNo, + StringRef Message) { + return Verifier.process(OS, SM, SourceMgr::DK_Error, /*hasLoc=*/true, Buf, + LineNo, Message); + } +}; + +} // namespace + +TEST_F(SourceMgrDiagnosticVerifierTest, BuffersWithSameName) { + const MemoryBuffer *Buf1 = + addBuffer("op // expected-error {{first}}\n", "same.s"); + const MemoryBuffer *Buf2 = + addBuffer("op // expected-error {{second}}\n", "same.s"); + + // Directives only match diagnostics in their own buffer. + EXPECT_EQ(processError(Buf2, 1, "first"), MatchResult::Unexpected); + // FIXME: Buf1's directives are never scanned since Buf2 has the same name. + EXPECT_EQ(processError(Buf1, 1, "first"), MatchResult::Unexpected); + EXPECT_EQ(processError(Buf2, 1, "second"), MatchResult::Matched); +} + +TEST_F(SourceMgrDiagnosticVerifierTest, BufferAddedLater) { + // E.g. an included file without any diagnostics. + addBuffer("op // expected-error {{missing}}\n", "inc.s"); + + // FIXME: Buffers without diagnostics are never scanned, so their directives + // are not checked. + EXPECT_TRUE(Verifier.verify(OS, SM)); + EXPECT_EQ(Output, ""); +} + +TEST_F(SourceMgrDiagnosticVerifierTest, OctalLineOffset) { + const MemoryBuffer *Buf = addBuffer( + "op\n\n\n\n\n\n\n\n\n\n// expected-error@-010 {{decimal}}\n", "test.s"); + // FIXME: '010' is parsed as octal. + EXPECT_EQ(processError(Buf, 3, "decimal"), MatchResult::Matched); +} + +// FIXME: '08' is not valid octal, so the offset is read uninitialized. +TEST_F(SourceMgrDiagnosticVerifierTest, DISABLED_LeadingZeroLineOffset) { + const MemoryBuffer *Buf = addBuffer( + "op\n\n\n\n\n\n\n\n// expected-error@-08 {{decimal}}\n", "test.s"); + EXPECT_EQ(processError(Buf, 1, "decimal"), MatchResult::Matched); +} + +// FIXME: The offset does not fit in an int, so it is read uninitialized. +TEST_F(SourceMgrDiagnosticVerifierTest, DISABLED_OverflowingLineOffset) { + const MemoryBuffer *Buf = + addBuffer("// expected-error@+99999999999 {{overflow}}\n", "test.s"); + (void)Verifier.computeExpectedDiags(OS, SM, Buf); + EXPECT_THAT( + Output, + HasSubstr("test.s:1:4: error: invalid line offset '+99999999999'")); +} + +TEST_F(SourceMgrDiagnosticVerifierTest, OutOfRangeLineOffsets) { + const MemoryBuffer *Buf = addBuffer("// expected-error@-1 {{line zero}}\n" + "// expected-error@-3 {{before start}}\n" + "// expected-error@+99 {{past end}}\n", + "test.s"); + (void)Verifier.computeExpectedDiags(OS, SM, Buf); + EXPECT_FALSE(Verifier.verify(OS, SM)); + // Line 0 is allowed since MLIR uses it for the implicit top-level module. + EXPECT_THAT(Output, + HasSubstr("test.s:1:4: error: expected error \"line zero\" " + "was not produced")); + // FIXME: Offsets that point outside the file are not rejected. + EXPECT_THAT(Output, HasSubstr("test.s:2:4: error: expected error \"before " + "start\" was not produced")); + EXPECT_THAT(Output, + HasSubstr("test.s:3:4: error: expected error \"past end\" " + "was not produced")); +} diff --git a/llvm/utils/gn/secondary/llvm/unittests/Support/BUILD.gn b/llvm/utils/gn/secondary/llvm/unittests/Support/BUILD.gn index 168b609279cab6..7569f7e6fcbb91 100644 --- a/llvm/utils/gn/secondary/llvm/unittests/Support/BUILD.gn +++ b/llvm/utils/gn/secondary/llvm/unittests/Support/BUILD.gn @@ -93,6 +93,7 @@ unittest("SupportTests") { "SignalsTest.cpp", "SipHashTest.cpp", "SourceMgrTest.cpp", + "SourceMgrDiagnosticVerifierTest.cpp", "SpecialCaseListTest.cpp", "SuffixTreeTest.cpp", "SwapByteOrderTest.cpp", diff --git a/mlir/test/mlir-opt/verify-diagnostics-highlight.mlir b/mlir/test/mlir-opt/verify-diagnostics-highlight.mlir new file mode 100644 index 00000000000000..391845ed1cbcb8 --- /dev/null +++ b/mlir/test/mlir-opt/verify-diagnostics-highlight.mlir @@ -0,0 +1,13 @@ +// Verifier errors highlight the directive. + +// RUN: rm -rf %t && split-file %s %t && cd %t +// RUN: not mlir-opt --verify-diagnostics input.mlir 2>&1 \ +// RUN: | FileCheck %s --strict-whitespace + +// FIXME: The highlight ends in the middle of the directive. +// CHECK: input.mlir:1:4: error: expected error "never produced" was not produced +// CHECK-NEXT: {{^}}// expected-error {{[{][{]}}never produced{{[}][}]}}{{$}} +// CHECK-NEXT: {{^}} ^~~~~~~~~~~~~~{{$}} + +//--- input.mlir +// expected-error {{never produced}} >From f5760547053125dfda9c3adee9d36cfa9bdb647d Mon Sep 17 00:00:00 2001 From: Alex Richardson <[email protected]> Date: Wed, 28 Oct 2026 11:11:43 -0700 Subject: [PATCH 2/2] [nspr] update --- llvm/test/tools/llvm-mc/verify/highlight.s | 14 +++ .../test/tools/llvm-mc/verify/include-twice.s | 17 +++ llvm/test/tools/llvm-mc/verify/include.s | 14 +++ llvm/test/tools/llvm-mc/verify/line-offsets.s | 53 +++++++++ llvm/unittests/Support/CMakeLists.txt | 1 - .../SourceMgrDiagnosticVerifierTest.cpp | 108 ------------------ .../secondary/llvm/unittests/Support/BUILD.gn | 1 - .../verify-diagnostics-highlight.mlir | 13 --- 8 files changed, 98 insertions(+), 123 deletions(-) create mode 100644 llvm/test/tools/llvm-mc/verify/highlight.s create mode 100644 llvm/test/tools/llvm-mc/verify/include-twice.s create mode 100644 llvm/test/tools/llvm-mc/verify/include.s create mode 100644 llvm/test/tools/llvm-mc/verify/line-offsets.s delete mode 100644 llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp delete mode 100644 mlir/test/mlir-opt/verify-diagnostics-highlight.mlir diff --git a/llvm/test/tools/llvm-mc/verify/highlight.s b/llvm/test/tools/llvm-mc/verify/highlight.s new file mode 100644 index 00000000000000..69b8784925a762 --- /dev/null +++ b/llvm/test/tools/llvm-mc/verify/highlight.s @@ -0,0 +1,14 @@ +## Verifier errors highlight the directive. + +# REQUIRES: x86-registered-target +# RUN: rm -rf %t && split-file %s %t && cd %t +# RUN: not llvm-mc --triple=x86_64 --verify input.s 2>&1 \ +# RUN: | FileCheck %s --strict-whitespace + +## FIXME: The highlight ends in the middle of the directive. +# CHECK: input.s:1:7: error: expected error "never produced" was not produced +# CHECK-NEXT: {{^}}nop # expected-error {{[{][{]}}never produced{{[}][}]}}{{$}} +# CHECK-NEXT: {{^}} ^~~~~~~~~~~~~~{{$}} + +#--- input.s +nop # expected-error {{never produced}} diff --git a/llvm/test/tools/llvm-mc/verify/include-twice.s b/llvm/test/tools/llvm-mc/verify/include-twice.s new file mode 100644 index 00000000000000..3c730ebcdcb81b --- /dev/null +++ b/llvm/test/tools/llvm-mc/verify/include-twice.s @@ -0,0 +1,17 @@ +## Directives in a file that is included twice are checked separately for each +## inclusion. + +# REQUIRES: x86-registered-target +# RUN: rm -rf %t && split-file %s %t && cd %t +## FIXME: Both inclusions share one list of directives, so the second +## diagnostic finds the directive already used. +# RUN: not llvm-mc --triple=x86_64 --verify main.s 2>&1 \ +# RUN: | FileCheck %s --implicit-check-not=error: +# CHECK: inc.s:1:1: error: unexpected error: unknown directive + +#--- main.s +.include "inc.s" +.include "inc.s" + +#--- inc.s +.foo_directive # expected-error {{unknown directive}} diff --git a/llvm/test/tools/llvm-mc/verify/include.s b/llvm/test/tools/llvm-mc/verify/include.s new file mode 100644 index 00000000000000..40a31e40367d34 --- /dev/null +++ b/llvm/test/tools/llvm-mc/verify/include.s @@ -0,0 +1,14 @@ +## A directive in an included file (inc.s) is checked even if no diagnostics +## are reported in that file. + +# REQUIRES: x86-registered-target +# RUN: rm -rf %t && split-file %s %t && cd %t +## FIXME: inc.s is never scanned since it has no diagnostics. +# RUN: llvm-mc --triple=x86_64 --verify main.s + +#--- main.s +.include "inc.s" + +#--- inc.s +nop +# expected-error@-1 {{this will never happen}} diff --git a/llvm/test/tools/llvm-mc/verify/line-offsets.s b/llvm/test/tools/llvm-mc/verify/line-offsets.s new file mode 100644 index 00000000000000..a794f86979064d --- /dev/null +++ b/llvm/test/tools/llvm-mc/verify/line-offsets.s @@ -0,0 +1,53 @@ +## Tests for '@+N' and '@-N' line offsets in --verify directives. + +# REQUIRES: x86-registered-target +# RUN: rm -rf %t && split-file %s %t && cd %t + +#--- octal.s +## FIXME: '010' is parsed as octal (8), so the directive points at line 6. +# RUN: not llvm-mc --triple=x86_64 --verify octal.s 2>&1 \ +# RUN: | FileCheck octal.s --implicit-check-not=error: +.foo_directive +# CHECK: octal.s:[[#@LINE-1]]:1: error: unexpected error: unknown directive + + + + + + + + +# expected-error@-010 {{unknown directive}} +# CHECK: octal.s:[[#@LINE-1]]:3: error: expected error "unknown directive" was not produced + +#--- leading-zero.s +## FIXME: '08' is not valid octal, so the offset is read uninitialized. +# DISABLED: llvm-mc --triple=x86_64 --verify leading-zero.s +.foo_directive + + + + + + + +# expected-error@-08 {{unknown directive}} + +#--- overflow.s +## FIXME: The offset does not fit in an int, so it is read uninitialized. +# DISABLED: not llvm-mc --triple=x86_64 --verify overflow.s 2>&1 \ +# DISABLED: | FileCheck overflow.s --implicit-check-not=error: +# expected-error@+99999999999 {{overflow}} +# CHECK: overflow.s:[[#@LINE-1]]:3: error: invalid line offset '+99999999999' + +#--- out-of-range.s +## FIXME: Offsets before the start or past the end of the file are accepted. +# RUN: not llvm-mc --triple=x86_64 --verify out-of-range.s 2>&1 \ +# RUN: | FileCheck out-of-range.s --implicit-check-not=error: +## Line 0 is allowed since MLIR uses it for the implicit top-level module. +# expected-error@-5 {{line zero}} +# expected-error@-99 {{before start}} +# expected-error@+99 {{past end}} +# CHECK: out-of-range.s:5:3: error: expected error "line zero" was not produced +# CHECK: out-of-range.s:6:3: error: expected error "before start" was not produced +# CHECK: out-of-range.s:7:3: error: expected error "past end" was not produced diff --git a/llvm/unittests/Support/CMakeLists.txt b/llvm/unittests/Support/CMakeLists.txt index c5db78950d21b4..f9732ff4e0e4bb 100644 --- a/llvm/unittests/Support/CMakeLists.txt +++ b/llvm/unittests/Support/CMakeLists.txt @@ -90,7 +90,6 @@ add_llvm_unittest(SupportTests SignalsTest.cpp SipHashTest.cpp SourceMgrTest.cpp - SourceMgrDiagnosticVerifierTest.cpp SpecialCaseListTest.cpp SuffixTreeTest.cpp SwapByteOrderTest.cpp diff --git a/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp b/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp deleted file mode 100644 index 2ac7355fd70b6c..00000000000000 --- a/llvm/unittests/Support/SourceMgrDiagnosticVerifierTest.cpp +++ /dev/null @@ -1,108 +0,0 @@ -//===- unittests/Support/SourceMgrDiagnosticVerifierTest.cpp --------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "llvm/Support/SourceMgrDiagnosticVerifier.h" -#include "llvm/Support/MemoryBuffer.h" -#include "llvm/Support/SourceMgr.h" -#include "llvm/Support/raw_ostream.h" -#include "gmock/gmock.h" -#include "gtest/gtest.h" - -using namespace llvm; -using MatchResult = SourceMgrDiagnosticVerifier::MatchResult; -using testing::HasSubstr; - -namespace { - -class SourceMgrDiagnosticVerifierTest : public testing::Test { -protected: - SourceMgr SM; - std::string Output; - raw_string_ostream OS{Output}; - SourceMgrDiagnosticVerifier Verifier; - - const MemoryBuffer *addBuffer(StringRef Text, StringRef BufferName) { - unsigned ID = SM.AddNewSourceBuffer( - MemoryBuffer::getMemBuffer(Text, BufferName), SMLoc()); - return SM.getMemoryBuffer(ID); - } - - MatchResult processError(const MemoryBuffer *Buf, unsigned LineNo, - StringRef Message) { - return Verifier.process(OS, SM, SourceMgr::DK_Error, /*hasLoc=*/true, Buf, - LineNo, Message); - } -}; - -} // namespace - -TEST_F(SourceMgrDiagnosticVerifierTest, BuffersWithSameName) { - const MemoryBuffer *Buf1 = - addBuffer("op // expected-error {{first}}\n", "same.s"); - const MemoryBuffer *Buf2 = - addBuffer("op // expected-error {{second}}\n", "same.s"); - - // Directives only match diagnostics in their own buffer. - EXPECT_EQ(processError(Buf2, 1, "first"), MatchResult::Unexpected); - // FIXME: Buf1's directives are never scanned since Buf2 has the same name. - EXPECT_EQ(processError(Buf1, 1, "first"), MatchResult::Unexpected); - EXPECT_EQ(processError(Buf2, 1, "second"), MatchResult::Matched); -} - -TEST_F(SourceMgrDiagnosticVerifierTest, BufferAddedLater) { - // E.g. an included file without any diagnostics. - addBuffer("op // expected-error {{missing}}\n", "inc.s"); - - // FIXME: Buffers without diagnostics are never scanned, so their directives - // are not checked. - EXPECT_TRUE(Verifier.verify(OS, SM)); - EXPECT_EQ(Output, ""); -} - -TEST_F(SourceMgrDiagnosticVerifierTest, OctalLineOffset) { - const MemoryBuffer *Buf = addBuffer( - "op\n\n\n\n\n\n\n\n\n\n// expected-error@-010 {{decimal}}\n", "test.s"); - // FIXME: '010' is parsed as octal. - EXPECT_EQ(processError(Buf, 3, "decimal"), MatchResult::Matched); -} - -// FIXME: '08' is not valid octal, so the offset is read uninitialized. -TEST_F(SourceMgrDiagnosticVerifierTest, DISABLED_LeadingZeroLineOffset) { - const MemoryBuffer *Buf = addBuffer( - "op\n\n\n\n\n\n\n\n// expected-error@-08 {{decimal}}\n", "test.s"); - EXPECT_EQ(processError(Buf, 1, "decimal"), MatchResult::Matched); -} - -// FIXME: The offset does not fit in an int, so it is read uninitialized. -TEST_F(SourceMgrDiagnosticVerifierTest, DISABLED_OverflowingLineOffset) { - const MemoryBuffer *Buf = - addBuffer("// expected-error@+99999999999 {{overflow}}\n", "test.s"); - (void)Verifier.computeExpectedDiags(OS, SM, Buf); - EXPECT_THAT( - Output, - HasSubstr("test.s:1:4: error: invalid line offset '+99999999999'")); -} - -TEST_F(SourceMgrDiagnosticVerifierTest, OutOfRangeLineOffsets) { - const MemoryBuffer *Buf = addBuffer("// expected-error@-1 {{line zero}}\n" - "// expected-error@-3 {{before start}}\n" - "// expected-error@+99 {{past end}}\n", - "test.s"); - (void)Verifier.computeExpectedDiags(OS, SM, Buf); - EXPECT_FALSE(Verifier.verify(OS, SM)); - // Line 0 is allowed since MLIR uses it for the implicit top-level module. - EXPECT_THAT(Output, - HasSubstr("test.s:1:4: error: expected error \"line zero\" " - "was not produced")); - // FIXME: Offsets that point outside the file are not rejected. - EXPECT_THAT(Output, HasSubstr("test.s:2:4: error: expected error \"before " - "start\" was not produced")); - EXPECT_THAT(Output, - HasSubstr("test.s:3:4: error: expected error \"past end\" " - "was not produced")); -} diff --git a/llvm/utils/gn/secondary/llvm/unittests/Support/BUILD.gn b/llvm/utils/gn/secondary/llvm/unittests/Support/BUILD.gn index 7569f7e6fcbb91..168b609279cab6 100644 --- a/llvm/utils/gn/secondary/llvm/unittests/Support/BUILD.gn +++ b/llvm/utils/gn/secondary/llvm/unittests/Support/BUILD.gn @@ -93,7 +93,6 @@ unittest("SupportTests") { "SignalsTest.cpp", "SipHashTest.cpp", "SourceMgrTest.cpp", - "SourceMgrDiagnosticVerifierTest.cpp", "SpecialCaseListTest.cpp", "SuffixTreeTest.cpp", "SwapByteOrderTest.cpp", diff --git a/mlir/test/mlir-opt/verify-diagnostics-highlight.mlir b/mlir/test/mlir-opt/verify-diagnostics-highlight.mlir deleted file mode 100644 index 391845ed1cbcb8..00000000000000 --- a/mlir/test/mlir-opt/verify-diagnostics-highlight.mlir +++ /dev/null @@ -1,13 +0,0 @@ -// Verifier errors highlight the directive. - -// RUN: rm -rf %t && split-file %s %t && cd %t -// RUN: not mlir-opt --verify-diagnostics input.mlir 2>&1 \ -// RUN: | FileCheck %s --strict-whitespace - -// FIXME: The highlight ends in the middle of the directive. -// CHECK: input.mlir:1:4: error: expected error "never produced" was not produced -// CHECK-NEXT: {{^}}// expected-error {{[{][{]}}never produced{{[}][}]}}{{$}} -// CHECK-NEXT: {{^}} ^~~~~~~~~~~~~~{{$}} - -//--- input.mlir -// expected-error {{never produced}} _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
