Skip to content

[clang-tidy][mlir] Make rewrite more conservative. #150757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 28, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 11 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,23 @@ 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 rewrite given an lvalue builder, else just warn.
{makeRule(cxxMemberCallExpr(unless(on(cxxTemporaryObjectExpr())), base),
rewrite(node("call"), node("builder"), callArgs("call")),
message),
makeRule(base, noopEdit(node("call")), message)});
}
} // namespace

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@ 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+2]]:3: warning: use 'OpType::create(builder, ...)' instead of 'builder.create<OpType>(...)' [llvm-use-new-mlir-op-builder]
// CHECK-FIXES: mlir::OpBuilder().create<mlir::ModuleOp>(builder.getUnknownLoc());
mlir::OpBuilder().create<mlir::ModuleOp>(builder.getUnknownLoc());
}