https://github.com/arichardson updated https://github.com/llvm/llvm-project/pull/227570
>From e6f8a4821954f3eff1099c04ab2cf8c7b5c61cef Mon Sep 17 00:00:00 2001 From: Alex Richardson <[email protected]> Date: Mon, 2 Nov 2026 22:11:04 -0800 Subject: [PATCH] [nspr] initial commit --- llvm/docs/CommandGuide/llvm-mc.md | 5 ++- .../Support/SourceMgrDiagnosticVerifier.h | 25 ++++++++------ .../Support/SourceMgrDiagnosticVerifier.cpp | 33 +++++++++++++------ llvm/test/tools/llvm-mc/verify/basic.s | 24 ++++++++++++-- llvm/tools/llvm-mc/llvm-mc.cpp | 5 ++- mlir/docs/Diagnostics.md | 16 +++++---- mlir/lib/IR/Diagnostics.cpp | 3 +- 7 files changed, 79 insertions(+), 32 deletions(-) diff --git a/llvm/docs/CommandGuide/llvm-mc.md b/llvm/docs/CommandGuide/llvm-mc.md index e96294b9384d16..ed3840aea07c08 100644 --- a/llvm/docs/CommandGuide/llvm-mc.md +++ b/llvm/docs/CommandGuide/llvm-mc.md @@ -165,12 +165,13 @@ them (similar to Clang's `-verify`): .foo # expected-error {{unknown directive}} .bar # expected-error-re@-1 {{unknown {{directive|instruction}}}} +.baz # expected-error:1 {{unknown directive}} ``` Directives have the form: ```text -<prefix>-<kind>[-re][@<offset>] [<count>] {{<text>}} +<prefix>-<kind>[-re][@<offset>][:<col>] [<count>] {{<text>}} ``` - `<prefix>`: `expected` by default, or any prefix listed in the @@ -183,6 +184,8 @@ Directives have the form: - `@+N` / `@-N`: `N` lines below or above the directive. - `@above` / `@below`: the nearest non-directive line above or below. - `@unknown`: a diagnostic with no source location. +- `:<col>`: the expected 1-based column. (Note that in Clang, `@<file>:<line>` + instead refers to a line in another file.) - `<count>`: the number of times the diagnostic is expected (defaults to 1). At most one directive is recognized per line. {program}`llvm-mc` succeeds if all diff --git a/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h b/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h index 0bcb136e621b4a..54f63807edb47d 100644 --- a/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h +++ b/llvm/include/llvm/Support/SourceMgrDiagnosticVerifier.h @@ -74,6 +74,9 @@ class LLVM_ABI SourceMgrDiagnosticVerifier { SourceMgr::DiagKind kind; /// The line number the expected diagnostic should be on. unsigned lineNo; + /// The column the expected diagnostic should be at, or 0 if any column + /// matches. + unsigned colNo = 0; /// The location of the expected diagnostic within the input file. SMLoc fileLoc; /// The number of times this diagnostic is expected. @@ -112,18 +115,20 @@ class LLVM_ABI SourceMgrDiagnosticVerifier { } /// Matches a single actual diagnostic against the expected diagnostics - /// recorded for \p buf / \p lineNo, computing them first via \p - /// computeExpectedDiags if they haven't been already. If \p hasLoc is - /// false, the diagnostic has no location and is matched against the - /// '@unknown' list instead (\p buf / \p lineNo are ignored). If \p hasLoc - /// is true but \p buf is null (e.g. the diagnostic's file isn't a known - /// buffer), the diagnostic is matched against an empty list, i.e. it can - /// never match and is always unexpected. On a near miss, prints a message - /// through \p mgr. \p reportUnexpected controls whether near misses / - /// unexpected diagnostics are reported at all. + /// recorded for \p buf / \p lineNo / \p colNo (1-based), computing them + /// first via \p computeExpectedDiags if they haven't been already. If \p + /// hasLoc is false, the diagnostic has no location and is matched against + /// the '@unknown' list instead (\p buf / \p lineNo / \p colNo are + /// ignored). If \p hasLoc is true but \p buf is null (e.g. the + /// diagnostic's file isn't a known buffer), the diagnostic is matched + /// against an empty list, i.e. it can never match and is always unexpected. + /// On a near miss, prints a message through \p mgr. \p reportUnexpected + /// controls whether near misses / unexpected diagnostics are reported at + /// all. MatchResult process(raw_ostream &os, SourceMgr &mgr, SourceMgr::DiagKind kind, bool hasLoc, const MemoryBuffer *buf, unsigned lineNo, - StringRef message, bool reportUnexpected = true); + unsigned colNo, StringRef message, + bool reportUnexpected = true); /// Reports (through \p mgr) any expected diagnostic that was never matched /// by a call to \p process. Returns whether verification succeeded overall, diff --git a/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp b/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp index 864c3d35110d81..4f4dad7e197ba7 100644 --- a/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp +++ b/llvm/lib/Support/SourceMgrDiagnosticVerifier.cpp @@ -100,7 +100,7 @@ static Regex buildExpectedRegex(ArrayRef<StringRef> prefixes) { }, [&] { pattern += '|'; }); pattern += ")-(error|note|remark|warning)(-re)?" - "( *@([+-][0-9]+|above|below|unknown))?" + "( *@([+-][0-9]+|above|below|unknown))?(:([0-9]+))?" "( +([0-9]+))? *{{(.*)}}$"; return Regex(pattern); } @@ -131,7 +131,7 @@ SourceMgrDiagnosticVerifier::computeExpectedDiags(raw_ostream &os, SmallVector<StringRef, 100> lines; buf->getBuffer().split(lines, '\n'); for (unsigned lineNo = 0, e = lines.size(); lineNo < e; ++lineNo) { - SmallVector<StringRef, 10> matches; + SmallVector<StringRef, 12> matches; if (!expected.match(lines[lineNo].rtrim(), &matches)) { // Check for designators that apply to this line. if (!designatorsForNextLine.empty()) { @@ -157,7 +157,7 @@ SourceMgrDiagnosticVerifier::computeExpectedDiags(raw_ostream &os, assert(matches[3] == "note"); kind = SourceMgr::DK_Note; } - ExpectedDiag record(kind, lineNo + 1, expectedStart, matches[9]); + ExpectedDiag record(kind, lineNo + 1, expectedStart, matches[11]); // Check to see if this is a regex match, i.e. it includes the `-re`. if (!matches[4].empty() && !record.computeRegex(os, mgr)) { @@ -166,9 +166,16 @@ SourceMgrDiagnosticVerifier::computeExpectedDiags(raw_ostream &os, } if (!matches[8].empty() && - (matches[8].getAsInteger(10, record.numExpected) || + (matches[8].getAsInteger(10, record.colNo) || record.colNo == 0)) { + record.emitError(os, mgr, "invalid column '" + matches[7] + "'"); + ok = false; + continue; + } + + if (!matches[10].empty() && + (matches[10].getAsInteger(10, record.numExpected) || record.numExpected == 0)) { - record.emitError(os, mgr, "invalid count '" + matches[8] + "'"); + record.emitError(os, mgr, "invalid count '" + matches[10] + "'"); ok = false; continue; } @@ -208,6 +215,11 @@ SourceMgrDiagnosticVerifier::computeExpectedDiags(raw_ostream &os, record.lineNo -= offset; } } else if (offsetMatch.consume_front("unknown")) { + if (record.colNo != 0) { + record.emitError(os, mgr, "cannot specify a column with '@unknown'"); + ok = false; + continue; + } // This is matching unknown locations. record.fileLoc = SMLoc(); expectedUnknownLocDiags.emplace_back(std::move(record)); @@ -232,11 +244,10 @@ SourceMgrDiagnosticVerifier::computeExpectedDiags(raw_ostream &os, return expectedDiags; } -SourceMgrDiagnosticVerifier::MatchResult -SourceMgrDiagnosticVerifier::process(raw_ostream &os, SourceMgr &mgr, - SourceMgr::DiagKind kind, bool hasLoc, - const MemoryBuffer *buf, unsigned lineNo, - StringRef message, bool reportUnexpected) { +SourceMgrDiagnosticVerifier::MatchResult SourceMgrDiagnosticVerifier::process( + raw_ostream &os, SourceMgr &mgr, SourceMgr::DiagKind kind, bool hasLoc, + const MemoryBuffer *buf, unsigned lineNo, unsigned colNo, StringRef message, + bool reportUnexpected) { MutableArrayRef<ExpectedDiag> diags; if (hasLoc) { // If the buffer couldn't be resolved, `diags` stays empty: a diagnostic @@ -258,6 +269,8 @@ SourceMgrDiagnosticVerifier::process(raw_ostream &os, SourceMgr &mgr, // File line must match (unless it's an unknown location). if (hasLoc && e.lineNo != lineNo) continue; + if (hasLoc && e.colNo != 0 && e.colNo != colNo) + continue; if (e.numSeen == e.numExpected || !e.match(message)) continue; if (e.kind == kind) { diff --git a/llvm/test/tools/llvm-mc/verify/basic.s b/llvm/test/tools/llvm-mc/verify/basic.s index 97cb46daedc1fe..db299dbdfa6ed6 100644 --- a/llvm/test/tools/llvm-mc/verify/basic.s +++ b/llvm/test/tools/llvm-mc/verify/basic.s @@ -5,7 +5,8 @@ #--- match.s ## Matching directives: a substring match, a '-re' regex match, '@+N' and -## '@-N' offsets, '@above'/'@below', and directives in '#' and '//' comments. +## '@-N' offsets, '@above'/'@below', ':<col>' columns, and directives in '#' +## and '//' comments. # RUN: llvm-mc --triple=x86_64 --verify match.s ## Output files are not kept if assembling failed. # RUN: llvm-mc --triple=x86_64 --verify --filetype=obj -o match.o match.s @@ -28,6 +29,7 @@ .qux_directive # expected-error {{unknown directive}} .quux_directive // expected-error {{unknown directive}} +.col_directive # expected-error:1 {{unknown directive}} ## A diagnostic that is reported twice on a line needs two directives or a count. .dup_directive; .dup_directive @@ -43,7 +45,25 @@ movl $1, %eax, %ebx; movl $1, %eax, %ebx # expected-error@-2 {{invalid operand}} movl $1, %eax, %ebx -# expected-error@-1 {{invalid operand for instruction}} +# expected-error@-1:16 {{invalid operand for instruction}} + +#--- wrong-column.s +## A directive with the wrong column fails. +# RUN: not llvm-mc --triple=x86_64 --verify wrong-column.s 2>&1 \ +# RUN: | FileCheck wrong-column.s +.foo_directive # expected-error:2 {{unknown directive}} +# CHECK: wrong-column.s:[[#@LINE-1]]:1: error: unexpected error: unknown directive +# CHECK: error: expected error "unknown directive" was not produced + +#--- invalid-column.s +## Invalid columns are rejected. +# RUN: not llvm-mc --triple=x86_64 --verify invalid-column.s 2>&1 \ +# RUN: | FileCheck invalid-column.s +nop +# expected-error@-1:0 {{never happens}} +# expected-error@unknown:1 {{never happens}} +# CHECK: error: invalid column ':0' +# CHECK: error: cannot specify a column with '@unknown' #--- custom-prefix.s ## Custom prefixes are recognized instead of 'expected', but not as the suffix diff --git a/llvm/tools/llvm-mc/llvm-mc.cpp b/llvm/tools/llvm-mc/llvm-mc.cpp index 363c43a2b2f030..db519635ce4172 100644 --- a/llvm/tools/llvm-mc/llvm-mc.cpp +++ b/llvm/tools/llvm-mc/llvm-mc.cpp @@ -550,8 +550,11 @@ int main(int argc, char **argv) { const MemoryBuffer *Buf = nullptr; if (unsigned ID = Loc.isValid() ? SrcMgr.FindBufferContainingLoc(Loc) : 0) Buf = SrcMgr.getMemoryBuffer(ID); + // SMDiagnostic columns are 0-based, but printed (and written in + // directives) as 1-based. if (Verifier->process(errs(), SrcMgr, Diag.getKind(), Loc.isValid(), Buf, - Diag.getLineNo(), Diag.getMessage()) != + Diag.getLineNo(), Diag.getColumnNo() + 1, + Diag.getMessage()) != SourceMgrDiagnosticVerifier::MatchResult::Unexpected) return; SourceMgrDiagnosticVerifier::printMessage( diff --git a/mlir/docs/Diagnostics.md b/mlir/docs/Diagnostics.md index e14bc98a3f8624..123b4a511e4cf9 100644 --- a/mlir/docs/Diagnostics.md +++ b/mlir/docs/Diagnostics.md @@ -318,21 +318,23 @@ This handler is a wrapper around a llvm::SourceMgr that is used to verify that certain diagnostics have been emitted to the context. To use this handler, annotate your source file with expected diagnostics in the form of: -* `expected-(error|note|remark|warning)(-re)?(@([+-][0-9]+|above|below|unknown))?( [0-9]+)? {{ message }}` +* `expected-(error|note|remark|warning)(-re)?(@([+-][0-9]+|above|below|unknown))?(:[0-9]+)?( [0-9]+)? {{ message }}` The provided `message` is a string expected to be contained within the generated diagnostic. The `-re` suffix may be used to enable regex matching within the `message`. When present, the `message` may define regex match sequences within `{{` `}}` blocks. The regular expression matcher supports Extended POSIX regular -expressions (ERE). An optional positive integer count before `{{` specifies how -many diagnostics the directive must match (default: 1). `expected-` must not be -preceded by an alphanumeric character, hyphen, or underscore (so e.g. -`unexpected-error` is ignored). A few examples are shown below: +expressions (ERE). An optional `:<col>` requires the diagnostic to be emitted at +the given 1-based column. An optional positive integer count before `{{` +specifies how many diagnostics the directive must match (default: 1). +`expected-` must not be preceded by an alphanumeric character, hyphen, or +underscore (so e.g. `unexpected-error` is ignored). A few examples are shown +below: ```mlir -// Expect an error on the same line. +// Expect an error on the same line, at column 9. func.func @bad_branch() { - cf.br ^missing // expected-error {{reference to an undefined block}} + cf.br ^missing // expected-error:9 {{reference to an undefined block}} } // Expect an error on an adjacent line. diff --git a/mlir/lib/IR/Diagnostics.cpp b/mlir/lib/IR/Diagnostics.cpp index b4348043492670..6310c63348a96d 100644 --- a/mlir/lib/IR/Diagnostics.cpp +++ b/mlir/lib/IR/Diagnostics.cpp @@ -721,7 +721,8 @@ void SourceMgrDiagnosticVerifierHandler::process(LocationAttr loc, using MatchResult = llvm::SourceMgrDiagnosticVerifier::MatchResult; MatchResult result = impl->verifier.process( os, mgr, getDiagKind(kind), /*hasLoc=*/static_cast<bool>(fileLoc), buf, - fileLoc ? fileLoc.getLine() : 0, msg, reportUnexpected); + fileLoc ? fileLoc.getLine() : 0, fileLoc ? fileLoc.getColumn() : 0, msg, + reportUnexpected); if (result == MatchResult::Unexpected) emitDiagnostic(loc, "unexpected " + getDiagKindStr(kind) + ": " + msg, DiagnosticSeverity::Error); _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
