xref: /llvm-project/mlir/test/lib/IR/TestDiagnosticsMetadata.cpp (revision 12dba4d48461c571cb5bdb7c3ac6078b9a357a02)
1 //===- TestDiagnosticsMetadata.cpp - Test Diagnostic Metatdata ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains test passes for constructing and resolving dominance
10 // information.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/IR/SymbolTable.h"
15 #include "mlir/Pass/Pass.h"
16 #include "llvm/Support/SourceMgr.h"
17 
18 using namespace mlir;
19 
20 namespace {
21 struct TestDiagnosticMetadataPass
22     : public PassWrapper<TestDiagnosticMetadataPass,
23                          InterfacePass<SymbolOpInterface>> {
24   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestDiagnosticMetadataPass)
25 
26   StringRef getArgument() const final { return "test-diagnostic-metadata"; }
27   StringRef getDescription() const final { return "Test diagnostic metadata."; }
28   TestDiagnosticMetadataPass() = default;
29   TestDiagnosticMetadataPass(const TestDiagnosticMetadataPass &) {}
30 
31   void runOnOperation() override {
32     llvm::errs() << "Test '" << getOperation().getName() << "'\n";
33 
34     // Build a diagnostic handler that has filtering capabilities.
35     ScopedDiagnosticHandler handler(&getContext(), [](mlir::Diagnostic &diag) {
36       return mlir::success(
37           llvm::none_of(diag.getMetadata(), [](mlir::DiagnosticArgument &arg) {
38             return arg.getKind() == mlir::DiagnosticArgument::
39                                         DiagnosticArgumentKind::String &&
40                    arg.getAsString().contains("hello");
41           }));
42     });
43 
44     // Emit a diagnostic for every operation with a valid loc.
45     getOperation()->walk([&](Operation *op) {
46       if (StringAttr strAttr = op->getAttrOfType<StringAttr>("attr")) {
47         if (strAttr.getValue() == "emit_error")
48           emitError(op->getLoc(), "test diagnostic metadata")
49               .getUnderlyingDiagnostic()
50               ->getMetadata()
51               .push_back(DiagnosticArgument("hello"));
52       }
53     });
54   }
55 };
56 
57 } // namespace
58 
59 namespace mlir {
60 namespace test {
61 void registerTestDiagnosticsMetadataPass() {
62   PassRegistration<TestDiagnosticMetadataPass>{};
63 }
64 } // namespace test
65 } // namespace mlir
66