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