Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions clang-tools-extra/clang-tidy/llvm/UseNewMLIROpBuilderCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,24 @@ EditGenerator rewrite(RangeSelector Call, RangeSelector Builder,
}

RewriteRuleWith<std::string> useNewMlirOpBuilderCheckRule() {
return makeRule(
Stencil message = cat("use 'OpType::create(builder, ...)' instead of "
"'builder.create<OpType>(...)'");
// Match a create call on an OpBuilder.
ast_matchers::internal::Matcher<Stmt> base =
cxxMemberCallExpr(
on(expr(hasType(
cxxRecordDecl(isSameOrDerivedFrom("::mlir::OpBuilder"))))
.bind("builder")),
callee(cxxMethodDecl(hasTemplateArgument(0, templateArgument()))),
callee(cxxMethodDecl(hasName("create"))))
.bind("call"),
rewrite(node("call"), node("builder"), callArgs("call")),
cat("use 'OpType::create(builder, ...)' instead of "
"'builder.create<OpType>(...)'"));
.bind("call");
return applyFirst(
{// Attempt to rewrite with a concrete builder.
makeRule(cxxMemberCallExpr(unless(on(cxxTemporaryObjectExpr())), base),
rewrite(node("call"), node("builder"), callArgs("call")),
message),
// Warn on calls on temporary objects only.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused about the comment here.
Isn't this "always warn, but rewrite only when having a lvalue builder"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this is meant as "Warn only" on temporary as in, don't attempt rewrite. I can perhaps combine both comments to one as you wrote.

makeRule(base, noopEdit(node("call")), message)});
}
} // namespace

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@ void f() {
// CHECK-MESSAGES: :[[@LINE+2]]:3: warning: use 'OpType::create(builder, ...)' instead of 'builder.create<OpType>(...)' [llvm-use-new-mlir-op-builder]
// CHECK-FIXES: mlir::ModuleOp::create(ib)
ib.create<mlir::ModuleOp>( );

// CHECK-MESSAGES: :[[@LINE+1]]:3: warning: use 'OpType::create(builder, ...)' instead of 'builder.create<OpType>(...)' [llvm-use-new-mlir-op-builder]
mlir::OpBuilder().create<mlir::ModuleOp>(builder.getUnknownLoc());
}