DanglingHandleCheck.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //===--- DanglingHandleCheck.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. #include "DanglingHandleCheck.h"
  9. #include "../utils/Matchers.h"
  10. #include "../utils/OptionsUtils.h"
  11. #include "clang/AST/ASTContext.h"
  12. #include "clang/ASTMatchers/ASTMatchFinder.h"
  13. using namespace clang::ast_matchers;
  14. using namespace clang::tidy::matchers;
  15. namespace clang::tidy::bugprone {
  16. namespace {
  17. ast_matchers::internal::BindableMatcher<Stmt>
  18. handleFrom(const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle,
  19. const ast_matchers::internal::Matcher<Expr> &Arg) {
  20. return expr(
  21. anyOf(cxxConstructExpr(hasDeclaration(cxxMethodDecl(ofClass(IsAHandle))),
  22. hasArgument(0, Arg)),
  23. cxxMemberCallExpr(hasType(cxxRecordDecl(IsAHandle)),
  24. callee(memberExpr(member(cxxConversionDecl()))),
  25. on(Arg))));
  26. }
  27. ast_matchers::internal::Matcher<Stmt> handleFromTemporaryValue(
  28. const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle) {
  29. // If a ternary operator returns a temporary value, then both branches hold a
  30. // temporary value. If one of them is not a temporary then it must be copied
  31. // into one to satisfy the type of the operator.
  32. const auto TemporaryTernary =
  33. conditionalOperator(hasTrueExpression(cxxBindTemporaryExpr()),
  34. hasFalseExpression(cxxBindTemporaryExpr()));
  35. return handleFrom(IsAHandle, anyOf(cxxBindTemporaryExpr(), TemporaryTernary));
  36. }
  37. ast_matchers::internal::Matcher<RecordDecl> isASequence() {
  38. return hasAnyName("::std::deque", "::std::forward_list", "::std::list",
  39. "::std::vector");
  40. }
  41. ast_matchers::internal::Matcher<RecordDecl> isASet() {
  42. return hasAnyName("::std::set", "::std::multiset", "::std::unordered_set",
  43. "::std::unordered_multiset");
  44. }
  45. ast_matchers::internal::Matcher<RecordDecl> isAMap() {
  46. return hasAnyName("::std::map", "::std::multimap", "::std::unordered_map",
  47. "::std::unordered_multimap");
  48. }
  49. ast_matchers::internal::BindableMatcher<Stmt> makeContainerMatcher(
  50. const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle) {
  51. // This matcher could be expanded to detect:
  52. // - Constructors: eg. vector<string_view>(3, string("A"));
  53. // - emplace*(): This requires a different logic to determine that
  54. // the conversion will happen inside the container.
  55. // - map's insert: This requires detecting that the pair conversion triggers
  56. // the bug. A little more complicated than what we have now.
  57. return callExpr(
  58. hasAnyArgument(
  59. ignoringParenImpCasts(handleFromTemporaryValue(IsAHandle))),
  60. anyOf(
  61. // For sequences: assign, push_back, resize.
  62. cxxMemberCallExpr(
  63. callee(functionDecl(hasAnyName("assign", "push_back", "resize"))),
  64. on(expr(hasType(hasUnqualifiedDesugaredType(
  65. recordType(hasDeclaration(recordDecl(isASequence())))))))),
  66. // For sequences and sets: insert.
  67. cxxMemberCallExpr(callee(functionDecl(hasName("insert"))),
  68. on(expr(hasType(hasUnqualifiedDesugaredType(
  69. recordType(hasDeclaration(recordDecl(
  70. anyOf(isASequence(), isASet()))))))))),
  71. // For maps: operator[].
  72. cxxOperatorCallExpr(callee(cxxMethodDecl(ofClass(isAMap()))),
  73. hasOverloadedOperatorName("[]"))));
  74. }
  75. } // anonymous namespace
  76. DanglingHandleCheck::DanglingHandleCheck(StringRef Name,
  77. ClangTidyContext *Context)
  78. : ClangTidyCheck(Name, Context),
  79. HandleClasses(utils::options::parseStringList(Options.get(
  80. "HandleClasses",
  81. "std::basic_string_view;std::experimental::basic_string_view"))),
  82. IsAHandle(cxxRecordDecl(hasAnyName(HandleClasses)).bind("handle")) {}
  83. void DanglingHandleCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
  84. Options.store(Opts, "HandleClasses",
  85. utils::options::serializeStringList(HandleClasses));
  86. }
  87. void DanglingHandleCheck::registerMatchersForVariables(MatchFinder *Finder) {
  88. const auto ConvertedHandle = handleFromTemporaryValue(IsAHandle);
  89. // Find 'Handle foo(ReturnsAValue());'
  90. Finder->addMatcher(
  91. varDecl(hasType(hasUnqualifiedDesugaredType(
  92. recordType(hasDeclaration(cxxRecordDecl(IsAHandle))))),
  93. hasInitializer(
  94. exprWithCleanups(has(ignoringParenImpCasts(ConvertedHandle)))
  95. .bind("bad_stmt"))),
  96. this);
  97. // Find 'Handle foo = ReturnsAValue();'
  98. Finder->addMatcher(
  99. traverse(TK_AsIs,
  100. varDecl(hasType(hasUnqualifiedDesugaredType(recordType(
  101. hasDeclaration(cxxRecordDecl(IsAHandle))))),
  102. unless(parmVarDecl()),
  103. hasInitializer(exprWithCleanups(
  104. has(ignoringParenImpCasts(handleFrom(
  105. IsAHandle, ConvertedHandle))))
  106. .bind("bad_stmt")))),
  107. this);
  108. // Find 'foo = ReturnsAValue(); // foo is Handle'
  109. Finder->addMatcher(
  110. traverse(TK_AsIs,
  111. cxxOperatorCallExpr(callee(cxxMethodDecl(ofClass(IsAHandle))),
  112. hasOverloadedOperatorName("="),
  113. hasArgument(1, ConvertedHandle))
  114. .bind("bad_stmt")),
  115. this);
  116. // Container insertions that will dangle.
  117. Finder->addMatcher(
  118. traverse(TK_AsIs, makeContainerMatcher(IsAHandle).bind("bad_stmt")),
  119. this);
  120. }
  121. void DanglingHandleCheck::registerMatchersForReturn(MatchFinder *Finder) {
  122. // Return a local.
  123. Finder->addMatcher(
  124. traverse(
  125. TK_AsIs,
  126. returnStmt(
  127. // The AST contains two constructor calls:
  128. // 1. Value to Handle conversion.
  129. // 2. Handle copy construction.
  130. // We have to match both.
  131. has(ignoringImplicit(handleFrom(
  132. IsAHandle,
  133. handleFrom(IsAHandle,
  134. declRefExpr(to(varDecl(
  135. // Is function scope ...
  136. hasAutomaticStorageDuration(),
  137. // ... and it is a local array or Value.
  138. anyOf(hasType(arrayType()),
  139. hasType(hasUnqualifiedDesugaredType(
  140. recordType(hasDeclaration(recordDecl(
  141. unless(IsAHandle)))))))))))))),
  142. // Temporary fix for false positives inside lambdas.
  143. unless(hasAncestor(lambdaExpr())))
  144. .bind("bad_stmt")),
  145. this);
  146. // Return a temporary.
  147. Finder->addMatcher(
  148. traverse(
  149. TK_AsIs,
  150. returnStmt(has(exprWithCleanups(has(ignoringParenImpCasts(handleFrom(
  151. IsAHandle, handleFromTemporaryValue(IsAHandle)))))))
  152. .bind("bad_stmt")),
  153. this);
  154. }
  155. void DanglingHandleCheck::registerMatchers(MatchFinder *Finder) {
  156. registerMatchersForVariables(Finder);
  157. registerMatchersForReturn(Finder);
  158. }
  159. void DanglingHandleCheck::check(const MatchFinder::MatchResult &Result) {
  160. auto *Handle = Result.Nodes.getNodeAs<CXXRecordDecl>("handle");
  161. diag(Result.Nodes.getNodeAs<Stmt>("bad_stmt")->getBeginLoc(),
  162. "%0 outlives its value")
  163. << Handle->getQualifiedNameAsString();
  164. }
  165. } // namespace clang::tidy::bugprone