Fix GCC build problem with 288f05f related to SmallVector. (#116958)
[llvm-project.git] / mlir / test / lib / IR / TestDiagnostics.cpp
blob578486c0a3b141bb98ec323e6135980e4afb08bb
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.
12 //===----------------------------------------------------------------------===//
14 #include "mlir/IR/SymbolTable.h"
15 #include "mlir/Pass/Pass.h"
16 #include "llvm/Support/SourceMgr.h"
18 using namespace mlir;
20 namespace {
21 struct TestDiagnosticFilterPass
22 : public PassWrapper<TestDiagnosticFilterPass,
23 InterfacePass<SymbolOpInterface>> {
24 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestDiagnosticFilterPass)
26 StringRef getArgument() const final { return "test-diagnostic-filter"; }
27 StringRef getDescription() const final {
28 return "Test diagnostic filtering support.";
30 TestDiagnosticFilterPass() = default;
31 TestDiagnosticFilterPass(const TestDiagnosticFilterPass &) {}
33 void runOnOperation() override {
34 llvm::errs() << "Test '" << getOperation().getName() << "'\n";
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;
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 });
48 llvm::SourceMgr sourceMgr;
49 SourceMgrDiagnosticHandler handler(sourceMgr, &getContext(), llvm::errs(),
50 filterFn);
52 // Emit a diagnostic for every operation with a valid loc.
53 getOperation()->walk([&](Operation *op) {
54 if (LocationAttr locAttr = op->getAttrOfType<LocationAttr>("test.loc"))
55 emitError(locAttr, "test diagnostic");
56 });
59 ListOption<std::string> filters{
60 *this, "filters",
61 llvm::cl::desc("Specifies the diagnostic file name filters.")};
64 } // namespace
66 namespace mlir {
67 namespace test {
68 void registerTestDiagnosticsPass() {
69 PassRegistration<TestDiagnosticFilterPass>{};
71 } // namespace test
72 } // namespace mlir