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