1 //===- TestDiagnostics.cpp - Test Diagnostic Utilities --------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains test passes for constructing and resolving dominance
12 //===----------------------------------------------------------------------===//
14 #include "mlir/IR/SymbolTable.h"
15 #include "mlir/Pass/Pass.h"
16 #include "llvm/Support/SourceMgr.h"
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
);
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
);
48 llvm::SourceMgr sourceMgr
;
49 SourceMgrDiagnosticHandler
handler(sourceMgr
, &getContext(), llvm::errs(),
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");
59 ListOption
<std::string
> filters
{
61 llvm::cl::desc("Specifies the diagnostic file name filters.")};
68 void registerTestDiagnosticsPass() {
69 PassRegistration
<TestDiagnosticFilterPass
>{};