-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
[clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check #121291
Open
vbvictor
wants to merge
3
commits into
llvm:main
Choose a base branch
from
vbvictor:bugprone-reset-call
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+428
−0
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
//===--- ResetCallCheck.cpp - clang-tidy ----------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "ResetCallCheck.h" | ||
#include "clang/AST/ASTContext.h" | ||
#include "clang/ASTMatchers/ASTMatchFinder.h" | ||
#include "clang/ASTMatchers/ASTMatchers.h" | ||
#include "clang/Lex/Lexer.h" | ||
|
||
using namespace clang::ast_matchers; | ||
|
||
namespace clang::tidy::bugprone { | ||
|
||
namespace { | ||
|
||
AST_MATCHER_P(CallExpr, everyArgumentMatches, | ||
ast_matchers::internal::Matcher<Expr>, InnerMatcher) { | ||
if (Node.getNumArgs() == 0) | ||
return true; | ||
|
||
for (const auto *Arg : Node.arguments()) { | ||
if (!InnerMatcher.matches(*Arg, Finder, Builder)) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
AST_MATCHER(CXXMethodDecl, hasOnlyDefaultArgs) { | ||
if (Node.param_empty()) | ||
return true; | ||
|
||
for (const auto *Param : Node.parameters()) { | ||
if (!Param->hasDefaultArg()) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
} // namespace | ||
|
||
void ResetCallCheck::registerMatchers(MatchFinder *Finder) { | ||
const auto IsSmartptr = hasAnyName("::std::unique_ptr", "::std::shared_ptr"); | ||
|
||
const auto ResetMethod = | ||
cxxMethodDecl(hasName("reset"), hasOnlyDefaultArgs()); | ||
|
||
const auto TypeWithReset = | ||
anyOf(cxxRecordDecl(hasMethod(ResetMethod)), | ||
classTemplateSpecializationDecl( | ||
hasSpecializedTemplate(classTemplateDecl(has(ResetMethod))))); | ||
|
||
const auto SmartptrWithBugproneReset = classTemplateSpecializationDecl( | ||
IsSmartptr, | ||
hasTemplateArgument( | ||
0, templateArgument(refersToType(hasUnqualifiedDesugaredType( | ||
recordType(hasDeclaration(TypeWithReset))))))); | ||
|
||
// Find a.reset() calls | ||
Finder->addMatcher( | ||
cxxMemberCallExpr(callee(memberExpr(member(hasName("reset")))), | ||
everyArgumentMatches(cxxDefaultArgExpr()), | ||
on(expr(hasType(SmartptrWithBugproneReset)))) | ||
.bind("smartptrResetCall"), | ||
this); | ||
|
||
// Find a->reset() calls | ||
Finder->addMatcher( | ||
cxxMemberCallExpr( | ||
callee(memberExpr( | ||
member(ResetMethod), | ||
hasObjectExpression( | ||
cxxOperatorCallExpr( | ||
hasOverloadedOperatorName("->"), | ||
hasArgument( | ||
0, expr(hasType( | ||
classTemplateSpecializationDecl(IsSmartptr))))) | ||
.bind("OpCall")))), | ||
everyArgumentMatches(cxxDefaultArgExpr())) | ||
.bind("objectResetCall"), | ||
this); | ||
} | ||
|
||
void ResetCallCheck::check(const MatchFinder::MatchResult &Result) { | ||
const auto *SmartptrResetCall = | ||
Result.Nodes.getNodeAs<CXXMemberCallExpr>("smartptrResetCall"); | ||
const auto *ObjectResetCall = | ||
Result.Nodes.getNodeAs<CXXMemberCallExpr>("objectResetCall"); | ||
|
||
if (SmartptrResetCall) { | ||
const auto *Member = cast<MemberExpr>(SmartptrResetCall->getCallee()); | ||
|
||
auto DiagBuilder = diag(SmartptrResetCall->getBeginLoc(), | ||
"bugprone 'reset()' call on smart pointer"); | ||
|
||
DiagBuilder << FixItHint::CreateReplacement( | ||
SourceRange(Member->getOperatorLoc(), | ||
Member->getOperatorLoc().getLocWithOffset(0)), | ||
" ="); | ||
|
||
DiagBuilder << FixItHint::CreateReplacement( | ||
SourceRange(Member->getMemberLoc(), SmartptrResetCall->getEndLoc()), | ||
" nullptr"); | ||
} | ||
|
||
if (ObjectResetCall) { | ||
const auto *Arrow = Result.Nodes.getNodeAs<CXXOperatorCallExpr>("OpCall"); | ||
|
||
const auto SmartptrSourceRange = | ||
Lexer::getAsCharRange(Arrow->getArg(0)->getSourceRange(), | ||
*Result.SourceManager, getLangOpts()); | ||
|
||
auto DiagBuilder = diag(ObjectResetCall->getBeginLoc(), | ||
"bugprone 'reset()' call on smart pointer"); | ||
|
||
DiagBuilder << FixItHint::CreateInsertion(SmartptrSourceRange.getBegin(), | ||
"(*"); | ||
DiagBuilder << FixItHint::CreateInsertion(SmartptrSourceRange.getEnd(), | ||
")"); | ||
|
||
DiagBuilder << FixItHint::CreateReplacement( | ||
CharSourceRange::getCharRange( | ||
Arrow->getOperatorLoc(), | ||
Arrow->getOperatorLoc().getLocWithOffset(2)), | ||
"."); | ||
} | ||
} | ||
|
||
} // namespace clang::tidy::bugprone |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//===--- ResetCallCheck.h - clang-tidy --------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RESETCALLCHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RESETCALLCHECK_H | ||
|
||
#include "../ClangTidyCheck.h" | ||
|
||
namespace clang::tidy::bugprone { | ||
|
||
/// Finds potentially erroneous calls to 'reset()' method on smart pointers when | ||
/// the pointee type also has a 'reset()' method | ||
/// | ||
/// For the user-facing documentation see: | ||
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/reset-call.html | ||
class ResetCallCheck : public ClangTidyCheck { | ||
public: | ||
ResetCallCheck(StringRef Name, ClangTidyContext *Context) | ||
: ClangTidyCheck(Name, Context) {} | ||
void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { | ||
return LangOpts.CPlusPlus; | ||
} | ||
}; | ||
|
||
} // namespace clang::tidy::bugprone | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RESETCALLCHECK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
clang-tools-extra/docs/clang-tidy/checks/bugprone/reset-call.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
.. title:: clang-tidy - bugprone-reset-call | ||
|
||
bugprone-reset-call | ||
=================== | ||
|
||
Finds calls to ``reset()`` method on smart pointers where the pointee type | ||
also has a ``reset()`` method, which makes it easy to accidentally | ||
make the pointer null when intending to reset the underlying object. | ||
|
||
.. code-block:: c++ | ||
|
||
struct Resettable { | ||
void reset() { /* Own reset logic */ } | ||
}; | ||
|
||
auto ptr = std::make_unique<Resettable>(); | ||
|
||
ptr->reset(); // Calls underlying reset method | ||
ptr.reset(); // Makes the pointer null | ||
|
||
Both calls are valid C++ code, but the second one might not be | ||
what the developer intended, as it destroys the pointed-to object | ||
rather than resetting its state. | ||
It's easy to make such typo because the difference between ``.`` and ``->`` is really small. | ||
|
||
The recommended approach is to make the intent explicit by using either member access or direct assignment: | ||
|
||
.. code-block:: c++ | ||
|
||
std::unique_ptr<Resettable> ptr = std::make_unique<Resettable>(); | ||
|
||
(*ptr).reset(); // Clearly calls underlying reset method | ||
ptr = nullptr; // Clearly makes the pointer null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Please make this statement same as statement in Release Notes.
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.
Done, also removed parentheses in
reset
to follow general style