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