1//===- TestDiagnostics.cpp - Test Diagnostic Utilities --------------------===//
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
18using namespace mlir;
19
20namespace {
21struct TestDiagnosticFilterPass
22 : public PassWrapper<TestDiagnosticFilterPass,
23 InterfacePass<SymbolOpInterface>> {
24 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestDiagnosticFilterPass)
25
26 StringRef getArgument() const final { return "test-diagnostic-filter"; }
27 StringRef getDescription() const final {
28 return "Test diagnostic filtering support.";
29 }
30 TestDiagnosticFilterPass() = default;
31 TestDiagnosticFilterPass(const TestDiagnosticFilterPass &) {}
32
33 void runOnOperation() override {
34 llvm::errs() << "Test '" << getOperation().getName() << "'\n";
35
36 // Build a diagnostic handler that has filtering capabilities.
37 auto filterFn = [&](Location loc) {
38 // Ignore non-file locations.
39 FileLineColLoc fileLoc = dyn_cast<FileLineColLoc>(loc);
40 if (!fileLoc)
41 return true;
42
43 // Don't show file locations if their name contains a filter.
44 return llvm::none_of(filters, [&](StringRef filter) {
45 return fileLoc.getFilename().strref().contains(filter);
46 });
47 };
48 llvm::SourceMgr sourceMgr;
49 SourceMgrDiagnosticHandler handler(sourceMgr, &getContext(), llvm::errs(),
50 filterFn);
51
52 // Emit a diagnostic for every operation with a valid loc.
53 getOperation()->walk([&](Operation *op) {
54 if (LocationAttr locAttr = op->getAttrOfType<LocationAttr>(name: "test.loc"))
55 emitError(loc: locAttr, message: "test diagnostic");
56 });
57 }
58
59 ListOption<std::string> filters{
60 *this, "filters",
61 llvm::cl::desc("Specifies the diagnostic file name filters.")};
62};
63
64} // namespace
65
66namespace mlir {
67namespace test {
68void registerTestDiagnosticsPass() {
69 PassRegistration<TestDiagnosticFilterPass>{};
70}
71} // namespace test
72} // namespace mlir
73

source code of mlir/test/lib/IR/TestDiagnostics.cpp