Skip to content

Commit 87cbc6c

Browse files
committed
test to ensure diagnostics are logged
1 parent 6afec89 commit 87cbc6c

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

mlir/test/python/ir/diagnostic_handler.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import gc
44
from mlir.ir import *
5+
from mlir._mlir_libs._mlirPythonTestNanobind import test_diagnostics_with_errors_and_notes
56

67

78
def run(f):
@@ -222,3 +223,16 @@ def callback2(d):
222223
# CHECK: CALLBACK2: foobar
223224
# CHECK: CALLBACK1: foobar
224225
loc.emit_error("foobar")
226+
227+
# CHECK-LABEL: TEST: testBuiltInDiagnosticsHandler
228+
@run
229+
def testBuiltInDiagnosticsHandler():
230+
ctx = Context()
231+
232+
try:
233+
test_diagnostics_with_errors_and_notes(ctx)
234+
except ValueError as e:
235+
# CHECK: created error
236+
# CHECK: MLIRPythonCAPI
237+
print(e)
238+

mlir/test/python/lib/PythonTestModuleNanobind.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
#include "PythonTestCAPI.h"
1212
#include "mlir-c/BuiltinAttributes.h"
1313
#include "mlir-c/BuiltinTypes.h"
14+
#include "mlir-c/Diagnostics.h"
1415
#include "mlir-c/IR.h"
16+
#include "mlir/Bindings/Python/Diagnostics.h"
1517
#include "mlir/Bindings/Python/Nanobind.h"
1618
#include "mlir/Bindings/Python/NanobindAdaptors.h"
19+
#include "nanobind/nanobind.h"
1720

1821
namespace nb = nanobind;
1922
using namespace mlir::python::nanobind_adaptors;
@@ -45,6 +48,15 @@ NB_MODULE(_mlirPythonTestNanobind, m) {
4548
},
4649
nb::arg("registry"));
4750

51+
m.def("test_diagnostics_with_errors_and_notes", [](MlirContext ctx) {
52+
mlirContextPrintStackTraceOnDiagnostic(ctx, true);
53+
mlir::python::CollectDiagnosticsToStringScope handler(ctx);
54+
55+
auto loc = mlirLocationUnknownGet(ctx);
56+
mlirEmitError(loc, "created error");
57+
throw nb::value_error(handler.takeMessage().c_str());
58+
});
59+
4860
mlir_attribute_subclass(m, "TestAttr",
4961
mlirAttributeIsAPythonTestTestAttribute,
5062
mlirPythonTestTestAttributeGetTypeID)

0 commit comments

Comments
 (0)