Skip to content

Commit 1ffd772

Browse files
committed
tidy
1 parent ebfae55 commit 1ffd772

File tree

14 files changed

+345
-0
lines changed

14 files changed

+345
-0
lines changed

clang-tools-extra/clang-tidy/misc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ set_target_properties(genconfusable PROPERTIES FOLDER "Clang Tools Extra/Sourceg
2020
add_clang_library(clangTidyMiscModule STATIC
2121
ConstCorrectnessCheck.cpp
2222
CoroutineHostileRAIICheck.cpp
23+
DanglingRefUtilsAsyncCheck.cpp
2324
DefinitionsInHeadersCheck.cpp
2425
ConfusableIdentifierCheck.cpp
2526
HeaderIncludeCycleCheck.cpp
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===--- DanglingRefUtilsAsyncCheck.cpp - clang-tidy ----------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "DanglingRefUtilsAsyncCheck.h"
10+
#include "clang/ASTMatchers/ASTMatchFinder.h"
11+
12+
using namespace clang::ast_matchers;
13+
14+
namespace clang::tidy::misc {
15+
16+
void DanglingRefUtilsAsyncCheck::registerMatchers(MatchFinder* Finder) {
17+
// if (0) {
18+
Finder->addMatcher(
19+
declRefExpr(
20+
hasParent(lambdaExpr(hasAncestor(
21+
expr(hasAncestor(cxxMemberCallExpr(has(memberExpr(
22+
hasDeclaration(namedDecl(hasName("push_back"))),
23+
has(declRefExpr(hasDeclaration(decl().bind("t1")))))))),
24+
has(implicitCastExpr(has(declRefExpr(
25+
hasDeclaration(namedDecl(hasName("Async"))))))))
26+
.bind("x")))),
27+
hasDeclaration(decl().bind("decl")))
28+
.bind("root"),
29+
this);
30+
//}
31+
/*
32+
Finder->addMatcher(declRefExpr(hasParent(lambdaExpr(hasParent(
33+
expr(hasParent(expr().bind("call")))))))
34+
.bind("root"),
35+
this);
36+
*/
37+
}
38+
39+
void DanglingRefUtilsAsyncCheck::check(const MatchFinder::MatchResult& Result) {
40+
const auto* Usage = Result.Nodes.getNodeAs<DeclRefExpr>("root");
41+
const auto* Tasks = Result.Nodes.getNodeAs<Decl>("t1");
42+
const auto* XDecl = Result.Nodes.getNodeAs<Decl>("decl");
43+
44+
if (XDecl->getLocation() > Tasks->getLocation()) {
45+
// diag(Tasks->getLocation(), "Tasks allocation", DiagnosticIDs::Note);
46+
diag(Usage->getLocation(),
47+
"Might be a use-after-delete if exception is thrown inside lambda");
48+
// diag(XDecl->getLocation(), "Variable declaration");
49+
}
50+
}
51+
52+
} // namespace clang::tidy::misc
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===--- DanglingRefUtilsAsyncCheck.h - clang-tidy --------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_DANGLINGREFUTILSASYNCCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_DANGLINGREFUTILSASYNCCHECK_H
11+
12+
#include "../ClangTidyCheck.h"
13+
14+
namespace clang::tidy::misc {
15+
16+
/// Find dangling refs with utils::Async().
17+
///
18+
/// For the user-facing documentation see:
19+
/// http://clang.llvm.org/extra/clang-tidy/checks/misc/dangling-ref-utils-async.html
20+
class DanglingRefUtilsAsyncCheck : public ClangTidyCheck {
21+
public:
22+
DanglingRefUtilsAsyncCheck(StringRef Name, ClangTidyContext *Context)
23+
: ClangTidyCheck(Name, Context) {}
24+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
25+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
26+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
27+
return LangOpts.CPlusPlus;
28+
}
29+
};
30+
31+
} // namespace clang::tidy::misc
32+
33+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_DANGLINGREFUTILSASYNCCHECK_H

clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "ConfusableIdentifierCheck.h"
1313
#include "ConstCorrectnessCheck.h"
1414
#include "CoroutineHostileRAIICheck.h"
15+
#include "DanglingRefUtilsAsyncCheck.h"
1516
#include "DefinitionsInHeadersCheck.h"
1617
#include "HeaderIncludeCycleCheck.h"
1718
#include "IncludeCleanerCheck.h"
@@ -45,6 +46,8 @@ class MiscModule : public ClangTidyModule {
4546
"misc-const-correctness");
4647
CheckFactories.registerCheck<CoroutineHostileRAIICheck>(
4748
"misc-coroutine-hostile-raii");
49+
CheckFactories.registerCheck<DanglingRefUtilsAsyncCheck>(
50+
"misc-dangling-ref-utils-async");
4851
CheckFactories.registerCheck<DefinitionsInHeadersCheck>(
4952
"misc-definitions-in-headers");
5053
CheckFactories.registerCheck<HeaderIncludeCycleCheck>(

clang-tools-extra/clang-tidy/performance/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_clang_library(clangTidyPerformanceModule STATIC
1414
InefficientVectorOperationCheck.cpp
1515
MoveConstArgCheck.cpp
1616
MoveConstructorInitCheck.cpp
17+
MoveSharedPtrCheck.cpp
1718
NoAutomaticMoveCheck.cpp
1819
NoIntToPtrCheck.cpp
1920
NoexceptDestructorCheck.cpp
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//===--- MoveSharedPtrCheck.cpp - clang-tidy ------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "MoveSharedPtrCheck.h"
10+
#include "../utils/DeclRefExprUtils.h"
11+
#include "clang/ASTMatchers/ASTMatchFinder.h"
12+
13+
using namespace clang::ast_matchers;
14+
15+
namespace clang::tidy::performance {
16+
17+
using utils::decl_ref_expr::allDeclRefExprs;
18+
19+
void MoveSharedPtrCheck::registerMatchers(MatchFinder* Finder) {
20+
Finder->addMatcher(
21+
declRefExpr(
22+
hasDeclaration(
23+
varDecl(hasAncestor(functionDecl().bind("func"))).bind("decl")),
24+
hasParent(expr(hasParent(cxxConstructExpr())).bind("use_parent"))
25+
26+
)
27+
.bind("use"),
28+
this);
29+
}
30+
31+
const Expr* MoveSharedPtrCheck::getLastVarUsage(const VarDecl& Var,
32+
const Decl& Func,
33+
ASTContext& Context) {
34+
auto Exprs = allDeclRefExprs(Var, Func, Context);
35+
36+
const Expr* LastExpr = nullptr;
37+
for (const auto& Expr : Exprs) {
38+
if (!LastExpr) LastExpr = Expr;
39+
40+
if (LastExpr->getBeginLoc() < Expr->getBeginLoc()) LastExpr = Expr;
41+
}
42+
43+
// diag(LastExpr->getBeginLoc(), "last usage");
44+
return LastExpr;
45+
}
46+
47+
const std::string_view kSharedPtr = "std::shared_ptr<";
48+
49+
void MoveSharedPtrCheck::check(const MatchFinder::MatchResult& Result) {
50+
const auto* MatchedDecl = Result.Nodes.getNodeAs<VarDecl>("decl");
51+
const auto* MatchedFunc = Result.Nodes.getNodeAs<FunctionDecl>("func");
52+
const auto* MatchedUse = Result.Nodes.getNodeAs<Expr>("use");
53+
const auto* MatchedUseCall = Result.Nodes.getNodeAs<CallExpr>("use_parent");
54+
55+
if (MatchedUseCall) return;
56+
57+
auto Type = MatchedDecl->getType().getAsString();
58+
if (std::string_view(Type).substr(0, kSharedPtr.size()) != kSharedPtr) return;
59+
60+
const auto* LastUsage =
61+
getLastVarUsage(*MatchedDecl, *MatchedFunc, *Result.Context);
62+
if (LastUsage == nullptr) return;
63+
64+
if (LastUsage->getBeginLoc() > MatchedUse->getBeginLoc()) {
65+
// "use" is not the last reference to x
66+
return;
67+
}
68+
69+
diag(LastUsage->getBeginLoc(), Type);
70+
diag(LastUsage->getBeginLoc(), "Could be std::move()");
71+
}
72+
73+
} // namespace clang::tidy::performance
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===--- MoveSharedPtrCheck.h - clang-tidy ----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_MOVESHAREDPTRCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_MOVESHAREDPTRCHECK_H
11+
12+
#include "../ClangTidyCheck.h"
13+
14+
namespace clang::tidy::performance {
15+
16+
/// adds std::move() to std::shared_ptr.
17+
///
18+
/// For the user-facing documentation see:
19+
/// http://clang.llvm.org/extra/clang-tidy/checks/performance/move-shared-ptr.html
20+
class MoveSharedPtrCheck : public ClangTidyCheck {
21+
public:
22+
MoveSharedPtrCheck(StringRef Name, ClangTidyContext *Context)
23+
: ClangTidyCheck(Name, Context) {}
24+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
25+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
26+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
27+
return LangOpts.CPlusPlus;
28+
}
29+
30+
private:
31+
const Expr* getLastVarUsage(const VarDecl& Var, const Decl& Func, ASTContext &Context);
32+
33+
llvm::DenseMap<const VarDecl *, SourceLocation> last_usage_;
34+
};
35+
36+
} // namespace clang::tidy::performance
37+
38+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_MOVESHAREDPTRCHECK_H

clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "InefficientVectorOperationCheck.h"
2020
#include "MoveConstArgCheck.h"
2121
#include "MoveConstructorInitCheck.h"
22+
#include "MoveSharedPtrCheck.h"
2223
#include "NoAutomaticMoveCheck.h"
2324
#include "NoIntToPtrCheck.h"
2425
#include "NoexceptDestructorCheck.h"
@@ -53,6 +54,8 @@ class PerformanceModule : public ClangTidyModule {
5354
"performance-move-const-arg");
5455
CheckFactories.registerCheck<MoveConstructorInitCheck>(
5556
"performance-move-constructor-init");
57+
CheckFactories.registerCheck<MoveSharedPtrCheck>(
58+
"performance-move-shared-ptr");
5659
CheckFactories.registerCheck<NoAutomaticMoveCheck>(
5760
"performance-no-automatic-move");
5861
CheckFactories.registerCheck<NoIntToPtrCheck>("performance-no-int-to-ptr");

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,22 @@ New checks
218218
Gives warnings for tagged unions, where the number of tags is
219219
different from the number of data members inside the union.
220220

221+
- New :doc:`misc-dangling-ref-utils-async
222+
<clang-tidy/checks/misc/dangling-ref-utils-async>` check.
223+
224+
Find dangling refs with utils::Async().
225+
221226
- New :doc:`modernize-use-integer-sign-comparison
222227
<clang-tidy/checks/modernize/use-integer-sign-comparison>` check.
223228

224229
Replace comparisons between signed and unsigned integers with their safe
225230
C++20 ``std::cmp_*`` alternative, if available.
226231

232+
- New :doc:`performance-move-shared-ptr
233+
<clang-tidy/checks/performance/move-shared-ptr>` check.
234+
235+
adds std::move() to std::shared_ptr.
236+
227237
- New :doc:`portability-template-virtual-member-function
228238
<clang-tidy/checks/portability/template-virtual-member-function>` check.
229239

clang-tools-extra/docs/clang-tidy/checks/list.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ Clang-Tidy Checks
254254
:doc:`misc-confusable-identifiers <misc/confusable-identifiers>`,
255255
:doc:`misc-const-correctness <misc/const-correctness>`, "Yes"
256256
:doc:`misc-coroutine-hostile-raii <misc/coroutine-hostile-raii>`,
257+
:doc:`misc-dangling-ref-utils-async <misc/dangling-ref-utils-async>`,
257258
:doc:`misc-definitions-in-headers <misc/definitions-in-headers>`, "Yes"
258259
:doc:`misc-header-include-cycle <misc/header-include-cycle>`,
259260
:doc:`misc-include-cleaner <misc/include-cleaner>`, "Yes"
@@ -339,6 +340,7 @@ Clang-Tidy Checks
339340
:doc:`performance-inefficient-vector-operation <performance/inefficient-vector-operation>`, "Yes"
340341
:doc:`performance-move-const-arg <performance/move-const-arg>`, "Yes"
341342
:doc:`performance-move-constructor-init <performance/move-constructor-init>`,
343+
:doc:`performance-move-shared-ptr <performance/move-shared-ptr>`, "Yes"
342344
:doc:`performance-no-automatic-move <performance/no-automatic-move>`,
343345
:doc:`performance-no-int-to-ptr <performance/no-int-to-ptr>`,
344346
:doc:`performance-noexcept-destructor <performance/noexcept-destructor>`, "Yes"

0 commit comments

Comments
 (0)