11 #include "../utils/Matchers.h" 12 #include "../utils/OptionsUtils.h" 13 #include "clang/AST/ASTContext.h" 14 #include "clang/ASTMatchers/ASTMatchFinder.h" 23 namespace cppcoreguidelines {
28 Matcher<FunctionDecl> hasAnyListedName(
const std::string &FunctionNames) {
29 const std::vector<std::string> NameList =
31 return hasAnyName(std::vector<StringRef>(NameList.begin(), NameList.end()));
36 Options.store(Opts,
"LegacyResourceProducers", LegacyResourceProducers);
37 Options.store(Opts,
"LegacyResourceConsumers", LegacyResourceConsumers);
42 void OwningMemoryCheck::registerMatchers(MatchFinder *Finder) {
43 if (!getLangOpts().CPlusPlus11)
46 const auto OwnerDecl = typeAliasTemplateDecl(hasName(
"::gsl::owner"));
47 const auto IsOwnerType = hasType(OwnerDecl);
49 const auto LegacyCreatorFunctions = hasAnyListedName(LegacyResourceProducers);
50 const auto LegacyConsumerFunctions =
51 hasAnyListedName(LegacyResourceConsumers);
55 const auto CreatesLegacyOwner =
56 callExpr(callee(functionDecl(LegacyCreatorFunctions)));
60 const auto LegacyOwnerCast =
61 castExpr(hasSourceExpression(CreatesLegacyOwner));
64 const auto LegacyOwnerConsumers = functionDecl(LegacyConsumerFunctions);
66 const auto CreatesOwner =
69 functionDecl(returns(qualType(hasDeclaration(OwnerDecl)))))),
70 CreatesLegacyOwner, LegacyOwnerCast);
72 const auto ConsideredOwner = eachOf(IsOwnerType, CreatesOwner);
78 declRefExpr(unless(ConsideredOwner)).bind(
"deleted_variable")))
91 allOf(callee(LegacyOwnerConsumers),
92 hasAnyArgument(allOf(unless(ignoringImpCasts(ConsideredOwner)),
93 hasType(pointerType())))))
94 .bind(
"legacy_consumer"),
99 Finder->addMatcher(binaryOperator(allOf(matchers::isAssignmentOperator(),
101 hasRHS(unless(ConsideredOwner))))
102 .bind(
"owner_assignment"),
108 varDecl(allOf(hasInitializer(unless(ConsideredOwner)), IsOwnerType))
109 .bind(
"owner_initialization")),
112 const auto HasConstructorInitializerForOwner =
113 has(cxxConstructorDecl(forEachConstructorInitializer(
114 cxxCtorInitializer(allOf(isMemberInitializer(), forField(IsOwnerType),
118 allOf(unless(ConsideredOwner),
119 unless(parenListExpr())))))
120 .bind(
"owner_member_initializer"))));
124 Finder->addMatcher(cxxRecordDecl(HasConstructorInitializerForOwner),
this);
129 binaryOperator(allOf(matchers::isAssignmentOperator(),
130 hasLHS(unless(IsOwnerType)), hasRHS(CreatesOwner)))
131 .bind(
"bad_owner_creation_assignment"),
137 namedDecl(varDecl(eachOf(allOf(hasInitializer(CreatesOwner),
138 unless(IsOwnerType)),
139 allOf(hasInitializer(ConsideredOwner),
140 hasType(autoType().bind(
"deduced_type")))))
141 .bind(
"bad_owner_creation_variable")),
147 callExpr(forEachArgumentWithParam(
148 expr(unless(ConsideredOwner)).bind(
"expected_owner_argument"),
149 parmVarDecl(IsOwnerType))),
154 Finder->addMatcher(callExpr(forEachArgumentWithParam(
155 expr(CreatesOwner).bind(
"bad_owner_creation_argument"),
156 parmVarDecl(unless(IsOwnerType))
157 .bind(
"bad_owner_creation_parameter"))),
164 allOf(hasDescendant(returnStmt(hasReturnValue(ConsideredOwner))
165 .bind(
"bad_owner_return")),
166 unless(returns(qualType(hasDeclaration(OwnerDecl))))))
167 .bind(
"function_decl"),
175 has(fieldDecl(IsOwnerType).bind(
"undestructed_owner_member")),
176 anyOf(unless(has(cxxDestructorDecl())),
177 has(cxxDestructorDecl(anyOf(isDefaulted(), isDeleted()))))))
178 .bind(
"non_destructor_class"),
182 void OwningMemoryCheck::check(
const MatchFinder::MatchResult &Result) {
183 const auto &Nodes = Result.Nodes;
185 bool CheckExecuted =
false;
186 CheckExecuted |= handleDeletion(Nodes);
187 CheckExecuted |= handleLegacyConsumers(Nodes);
188 CheckExecuted |= handleExpectedOwner(Nodes);
189 CheckExecuted |= handleAssignmentAndInit(Nodes);
190 CheckExecuted |= handleAssignmentFromNewOwner(Nodes);
191 CheckExecuted |= handleReturnValues(Nodes);
192 CheckExecuted |= handleOwnerMembers(Nodes);
194 assert(CheckExecuted &&
195 "None of the subroutines executed, logic error in matcher!");
198 bool OwningMemoryCheck::handleDeletion(
const BoundNodes &Nodes) {
200 const auto *DeleteStmt = Nodes.getNodeAs<CXXDeleteExpr>(
"delete_expr");
201 const auto *DeletedVariable =
202 Nodes.getNodeAs<DeclRefExpr>(
"deleted_variable");
206 diag(DeleteStmt->getLocStart(),
207 "deleting a pointer through a type that is " 208 "not marked 'gsl::owner<>'; consider using a " 209 "smart pointer instead")
210 << DeletedVariable->getSourceRange();
214 const ValueDecl *Decl = DeletedVariable->getDecl();
215 diag(Decl->getLocStart(),
"variable declared here", DiagnosticIDs::Note)
216 << Decl->getSourceRange();
223 bool OwningMemoryCheck::handleLegacyConsumers(
const BoundNodes &Nodes) {
225 const auto *LegacyConsumer = Nodes.getNodeAs<CallExpr>(
"legacy_consumer");
230 if (LegacyConsumer) {
231 diag(LegacyConsumer->getLocStart(),
232 "calling legacy resource function without passing a 'gsl::owner<>'")
233 << LegacyConsumer->getSourceRange();
239 bool OwningMemoryCheck::handleExpectedOwner(
const BoundNodes &Nodes) {
241 const auto *ExpectedOwner = Nodes.getNodeAs<Expr>(
"expected_owner_argument");
245 diag(ExpectedOwner->getLocStart(),
246 "expected argument of type 'gsl::owner<>'; got %0")
247 << ExpectedOwner->getType() << ExpectedOwner->getSourceRange();
254 bool OwningMemoryCheck::handleAssignmentAndInit(
const BoundNodes &Nodes) {
255 const auto *OwnerAssignment =
256 Nodes.getNodeAs<BinaryOperator>(
"owner_assignment");
257 const auto *OwnerInitialization =
258 Nodes.getNodeAs<VarDecl>(
"owner_initialization");
259 const auto *OwnerInitializer =
260 Nodes.getNodeAs<CXXCtorInitializer>(
"owner_member_initializer");
263 if (OwnerAssignment) {
264 diag(OwnerAssignment->getLocStart(),
265 "expected assignment source to be of type 'gsl::owner<>'; got %0")
266 << OwnerAssignment->getRHS()->getType()
267 << OwnerAssignment->getSourceRange();
272 if (OwnerInitialization) {
273 diag(OwnerInitialization->getLocStart(),
274 "expected initialization with value of type 'gsl::owner<>'; got %0")
275 << OwnerInitialization->getAnyInitializer()->getType()
276 << OwnerInitialization->getSourceRange();
281 if (OwnerInitializer) {
282 diag(OwnerInitializer->getSourceLocation(),
283 "expected initialization of owner member variable with value of type " 284 "'gsl::owner<>'; got %0")
287 << OwnerInitializer->getInit()->getType()
288 << OwnerInitializer->getSourceRange();
296 bool OwningMemoryCheck::handleAssignmentFromNewOwner(
const BoundNodes &Nodes) {
297 const auto *BadOwnerAssignment =
298 Nodes.getNodeAs<BinaryOperator>(
"bad_owner_creation_assignment");
299 const auto *BadOwnerInitialization =
300 Nodes.getNodeAs<VarDecl>(
"bad_owner_creation_variable");
302 const auto *BadOwnerArgument =
303 Nodes.getNodeAs<Expr>(
"bad_owner_creation_argument");
304 const auto *BadOwnerParameter =
305 Nodes.getNodeAs<ParmVarDecl>(
"bad_owner_creation_parameter");
308 if (BadOwnerAssignment) {
309 diag(BadOwnerAssignment->getLocStart(),
310 "assigning newly created 'gsl::owner<>' to non-owner %0")
311 << BadOwnerAssignment->getLHS()->getType()
312 << BadOwnerAssignment->getSourceRange();
317 if (BadOwnerInitialization) {
318 diag(BadOwnerInitialization->getLocStart(),
319 "initializing non-owner %0 with a newly created 'gsl::owner<>'")
320 << BadOwnerInitialization->getType()
321 << BadOwnerInitialization->getSourceRange();
328 if (Nodes.getNodeAs<AutoType>(
"deduced_type")) {
329 diag(BadOwnerInitialization->getLocStart(),
330 "type deduction did not result in an owner", DiagnosticIDs::Note);
337 if (BadOwnerArgument) {
338 assert(BadOwnerParameter &&
339 "parameter for the problematic argument not found");
340 diag(BadOwnerArgument->getLocStart(),
"initializing non-owner argument of " 341 "type %0 with a newly created " 343 << BadOwnerParameter->getType() << BadOwnerArgument->getSourceRange();
349 bool OwningMemoryCheck::handleReturnValues(
const BoundNodes &Nodes) {
352 const auto *BadReturnType = Nodes.getNodeAs<ReturnStmt>(
"bad_owner_return");
353 const auto *Function = Nodes.getNodeAs<FunctionDecl>(
"function_decl");
359 diag(BadReturnType->getLocStart(),
360 "returning a newly created resource of " 361 "type %0 or 'gsl::owner<>' from a " 362 "function whose return type is not 'gsl::owner<>'")
363 << Function->getReturnType() << BadReturnType->getSourceRange();
371 bool OwningMemoryCheck::handleOwnerMembers(
const BoundNodes &Nodes) {
374 const auto *BadClass = Nodes.getNodeAs<CXXRecordDecl>(
"non_destructor_class");
378 const auto *DeclaredOwnerMember =
379 Nodes.getNodeAs<FieldDecl>(
"undestructed_owner_member");
380 assert(DeclaredOwnerMember &&
381 "match on class with bad destructor but without a declared owner");
383 diag(DeclaredOwnerMember->getLocStart(),
384 "member variable of type 'gsl::owner<>' requires the class %0 to " 385 "implement a destructor to release the owned resource")
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
std::map< std::string, std::string > OptionMap