BufferDerefCheck.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //===--- BufferDerefCheck.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 "BufferDerefCheck.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. #include "clang/Tooling/FixIt.h"
  12. using namespace clang::ast_matchers;
  13. namespace clang::tidy::mpi {
  14. void BufferDerefCheck::registerMatchers(MatchFinder *Finder) {
  15. Finder->addMatcher(callExpr().bind("CE"), this);
  16. }
  17. void BufferDerefCheck::check(const MatchFinder::MatchResult &Result) {
  18. const auto *CE = Result.Nodes.getNodeAs<CallExpr>("CE");
  19. if (!CE->getDirectCallee())
  20. return;
  21. if (!FuncClassifier)
  22. FuncClassifier.emplace(*Result.Context);
  23. const IdentifierInfo *Identifier = CE->getDirectCallee()->getIdentifier();
  24. if (!Identifier || !FuncClassifier->isMPIType(Identifier))
  25. return;
  26. // These containers are used, to capture the type and expression of a buffer.
  27. SmallVector<const Type *, 1> BufferTypes;
  28. SmallVector<const Expr *, 1> BufferExprs;
  29. // Adds the type and expression of a buffer that is used in the MPI call
  30. // expression to the captured containers.
  31. auto AddBuffer = [&CE, &Result, &BufferTypes,
  32. &BufferExprs](const size_t BufferIdx) {
  33. // Skip null pointer constants and in place 'operators'.
  34. if (CE->getArg(BufferIdx)->isNullPointerConstant(
  35. *Result.Context, Expr::NPC_ValueDependentIsNull) ||
  36. tooling::fixit::getText(*CE->getArg(BufferIdx), *Result.Context) ==
  37. "MPI_IN_PLACE")
  38. return;
  39. const Expr *ArgExpr = CE->getArg(BufferIdx);
  40. if (!ArgExpr)
  41. return;
  42. const Type *ArgType = ArgExpr->IgnoreImpCasts()->getType().getTypePtr();
  43. if (!ArgType)
  44. return;
  45. BufferExprs.push_back(ArgExpr);
  46. BufferTypes.push_back(ArgType);
  47. };
  48. // Collect buffer types and argument expressions for all buffers used in the
  49. // MPI call expression. The number passed to the lambda corresponds to the
  50. // argument index of the currently verified MPI function call.
  51. if (FuncClassifier->isPointToPointType(Identifier)) {
  52. AddBuffer(0);
  53. } else if (FuncClassifier->isCollectiveType(Identifier)) {
  54. if (FuncClassifier->isReduceType(Identifier)) {
  55. AddBuffer(0);
  56. AddBuffer(1);
  57. } else if (FuncClassifier->isScatterType(Identifier) ||
  58. FuncClassifier->isGatherType(Identifier) ||
  59. FuncClassifier->isAlltoallType(Identifier)) {
  60. AddBuffer(0);
  61. AddBuffer(3);
  62. } else if (FuncClassifier->isBcastType(Identifier)) {
  63. AddBuffer(0);
  64. }
  65. }
  66. checkBuffers(BufferTypes, BufferExprs);
  67. }
  68. void BufferDerefCheck::checkBuffers(ArrayRef<const Type *> BufferTypes,
  69. ArrayRef<const Expr *> BufferExprs) {
  70. for (size_t I = 0; I < BufferTypes.size(); ++I) {
  71. unsigned IndirectionCount = 0;
  72. const Type *BufferType = BufferTypes[I];
  73. llvm::SmallVector<IndirectionType, 1> Indirections;
  74. // Capture the depth and types of indirections for the passed buffer.
  75. while (true) {
  76. if (BufferType->isPointerType()) {
  77. BufferType = BufferType->getPointeeType().getTypePtr();
  78. Indirections.push_back(IndirectionType::Pointer);
  79. } else if (BufferType->isArrayType()) {
  80. BufferType = BufferType->getArrayElementTypeNoTypeQual();
  81. Indirections.push_back(IndirectionType::Array);
  82. } else {
  83. break;
  84. }
  85. ++IndirectionCount;
  86. }
  87. if (IndirectionCount > 1) {
  88. // Referencing an array with '&' is valid, as this also points to the
  89. // beginning of the array.
  90. if (IndirectionCount == 2 &&
  91. Indirections[0] == IndirectionType::Pointer &&
  92. Indirections[1] == IndirectionType::Array)
  93. return;
  94. // Build the indirection description in reverse order of discovery.
  95. std::string IndirectionDesc;
  96. for (auto It = Indirections.rbegin(); It != Indirections.rend(); ++It) {
  97. if (!IndirectionDesc.empty())
  98. IndirectionDesc += "->";
  99. if (*It == IndirectionType::Pointer) {
  100. IndirectionDesc += "pointer";
  101. } else {
  102. IndirectionDesc += "array";
  103. }
  104. }
  105. const auto Loc = BufferExprs[I]->getSourceRange().getBegin();
  106. diag(Loc, "buffer is insufficiently dereferenced: %0") << IndirectionDesc;
  107. }
  108. }
  109. }
  110. void BufferDerefCheck::onEndOfTranslationUnit() { FuncClassifier.reset(); }
  111. } // namespace clang::tidy::mpi