-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[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
Conversation
Don't create a fix where object invoked on is a temporary object as create method requires a reference.
@llvm/pr-subscribers-clang-tidy Author: Jacques Pienaar (jpienaar) ChangesDon't create a fix where object invoked on is a temporary object as create method requires a reference. Full diff: https://github.com/llvm/llvm-project/pull/150757.diff 2 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/llvm/UseNewMLIROpBuilderCheck.cpp b/clang-tools-extra/clang-tidy/llvm/UseNewMLIROpBuilderCheck.cpp
index 0b28ea2508977..131c5c32eb780 100644
--- a/clang-tools-extra/clang-tidy/llvm/UseNewMLIROpBuilderCheck.cpp
+++ b/clang-tools-extra/clang-tidy/llvm/UseNewMLIROpBuilderCheck.cpp
@@ -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.
+ makeRule(base, noopEdit(node("call")), message)});
}
} // namespace
diff --git a/clang-tools-extra/test/clang-tidy/checkers/llvm/use-new-mlir-op-builder.cpp b/clang-tools-extra/test/clang-tidy/checkers/llvm/use-new-mlir-op-builder.cpp
index 57e026c10bf53..3b8d5da968f7f 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/llvm/use-new-mlir-op-builder.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/llvm/use-new-mlir-op-builder.cpp
@@ -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());
}
|
@llvm/pr-subscribers-clang-tools-extra Author: Jacques Pienaar (jpienaar) ChangesDon't create a fix where object invoked on is a temporary object as create method requires a reference. Full diff: https://github.com/llvm/llvm-project/pull/150757.diff 2 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/llvm/UseNewMLIROpBuilderCheck.cpp b/clang-tools-extra/clang-tidy/llvm/UseNewMLIROpBuilderCheck.cpp
index 0b28ea2508977..131c5c32eb780 100644
--- a/clang-tools-extra/clang-tidy/llvm/UseNewMLIROpBuilderCheck.cpp
+++ b/clang-tools-extra/clang-tidy/llvm/UseNewMLIROpBuilderCheck.cpp
@@ -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.
+ makeRule(base, noopEdit(node("call")), message)});
}
} // namespace
diff --git a/clang-tools-extra/test/clang-tidy/checkers/llvm/use-new-mlir-op-builder.cpp b/clang-tools-extra/test/clang-tidy/checkers/llvm/use-new-mlir-op-builder.cpp
index 57e026c10bf53..3b8d5da968f7f 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/llvm/use-new-mlir-op-builder.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/llvm/use-new-mlir-op-builder.cpp
@@ -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());
}
|
makeRule(cxxMemberCallExpr(unless(on(cxxTemporaryObjectExpr())), base), | ||
rewrite(node("call"), node("builder"), callArgs("call")), | ||
message), | ||
// Warn on calls on temporary objects only. |
There was a problem hiding this comment.
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"?
There was a problem hiding this comment.
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.
I'm going to wait a little bit longer to see if anyone has any further comment (I think this is relatively direct one, but wanted to make sure). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with nit
clang-tools-extra/test/clang-tidy/checkers/llvm/use-new-mlir-op-builder.cpp
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Don't create a fix where object invoked on is a temporary object as create method requires a reference.