[PATCH] D133801: Extraction of a matcher for an unused value from an expression from the bugprone-unused-return-value check
Bartłomiej Cieślar via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 13 12:36:37 PDT 2022
barcisz created this revision.
Herald added a subscriber: carlosgalvezp.
Herald added a project: All.
barcisz requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.
This diff extracts the matcher for an unused expression result from bugprone-unused-return-value into a utility. This diff is a prerequisite to the diff that introduces
the cuda-unsafe-api-call-check
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D133801
Files:
clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
clang-tools-extra/clang-tidy/utils/Matchers.h
Index: clang-tools-extra/clang-tidy/utils/Matchers.h
===================================================================
--- clang-tools-extra/clang-tidy/utils/Matchers.h
+++ clang-tools-extra/clang-tidy/utils/Matchers.h
@@ -49,6 +49,51 @@
return pointerType(pointee(qualType(isConstQualified())));
}
+// Matches the statements in a GNU statement-expression that are not returned
+// from it.
+AST_MATCHER_P(StmtExpr, hasUnreturning,
+ clang::ast_matchers::internal::Matcher<Stmt>, matcher) {
+ const auto compoundStmt = Node.getSubStmt();
+ assert(compoundStmt);
+
+ clang::ast_matchers::internal::BoundNodesTreeBuilder result;
+ bool matched = false;
+ for (auto stmt = compoundStmt->body_begin();
+ stmt + 1 < compoundStmt->body_end(); ++stmt) {
+ clang::ast_matchers::internal::BoundNodesTreeBuilder builderInner(*Builder);
+ assert(stmt && *stmt);
+ if (matcher.matches(**stmt, Finder, &builderInner)) {
+ result.addMatch(builderInner);
+ matched = true;
+ }
+ }
+ *Builder = result;
+ return matched;
+}
+
+// Matches all of the nodes (simmilar to forEach) that match the matcher
+// and have return values not used in any statement.
+AST_MATCHER_FUNCTION_P(ast_matchers::StatementMatcher, isValueUnused,
+ ast_matchers::StatementMatcher, Matcher) {
+ using namespace ast_matchers;
+ const auto UnusedInCompoundStmt =
+ compoundStmt(forEach(Matcher), unless(hasParent(stmtExpr())));
+ const auto UnusedInGnuExprStmt = stmtExpr(hasUnreturning(Matcher));
+ const auto UnusedInIfStmt =
+ ifStmt(eachOf(hasThen(Matcher), hasElse(Matcher)));
+ const auto UnusedInWhileStmt = whileStmt(hasBody(Matcher));
+ const auto UnusedInDoStmt = doStmt(hasBody(Matcher));
+ const auto UnusedInForStmt = forStmt(
+ eachOf(hasLoopInit(Matcher), hasIncrement(Matcher), hasBody(Matcher)));
+ const auto UnusedInRangeForStmt = cxxForRangeStmt(hasBody(Matcher));
+ const auto UnusedInCaseStmt = switchCase(forEach(Matcher));
+ const auto Unused =
+ stmt(anyOf(UnusedInCompoundStmt, UnusedInGnuExprStmt, UnusedInIfStmt,
+ UnusedInWhileStmt, UnusedInDoStmt, UnusedInForStmt,
+ UnusedInRangeForStmt, UnusedInCaseStmt));
+ return stmt(eachOf(Unused, forEachDescendant(Unused)));
+}
+
// A matcher implementation that matches a list of type name regular expressions
// against a NamedDecl. If a regular expression contains the substring "::"
// matching will occur against the qualified name, otherwise only the typename.
Index: clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "UnusedReturnValueCheck.h"
+#include "../utils/Matchers.h"
#include "../utils/OptionsUtils.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
@@ -159,10 +160,7 @@
auto UnusedInCaseStmt = switchCase(forEach(MatchedCallExpr));
Finder->addMatcher(
- stmt(anyOf(UnusedInCompoundStmt, UnusedInIfStmt, UnusedInWhileStmt,
- UnusedInDoStmt, UnusedInForStmt, UnusedInRangeForStmt,
- UnusedInCaseStmt)),
- this);
+ functionDecl(hasBody(matchers::isValueUnused(MatchedCallExpr))), this);
}
void UnusedReturnValueCheck::check(const MatchFinder::MatchResult &Result) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133801.459842.patch
Type: text/x-patch
Size: 3581 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220913/060b10d6/attachment.bin>
More information about the cfe-commits
mailing list