2017-09-13 04:00:42 +08:00
|
|
|
//===--- OwningMemoryCheck.cpp - clang-tidy--------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-09-13 04:00:42 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "OwningMemoryCheck.h"
|
|
|
|
#include "../utils/Matchers.h"
|
|
|
|
#include "../utils/OptionsUtils.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
using namespace clang::ast_matchers::internal;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace cppcoreguidelines {
|
|
|
|
|
[clang-tidy] introduce legacy resource functions to 'cppcoreguidelines-owning-memory'
Summary:
This patch introduces support for legacy C-style resource functions that must obey
the 'owner<>' semantics.
- added legacy creators like malloc,fopen,...
- added legacy consumers like free,fclose,...
This helps codes that mostly benefit from owner:
Legacy, C-Style code that isn't feasable to port directly to RAII but needs a step in between
to identify actual resource management and just using the resources.
Reviewers: aaron.ballman, alexfh, hokein
Reviewed By: aaron.ballman
Subscribers: nemanjai, JDevlieghere, xazax.hun, kbarton
Differential Revision: https://reviews.llvm.org/D38396
llvm-svn: 316092
2017-10-19 00:14:15 +08:00
|
|
|
// FIXME: Copied from 'NoMallocCheck.cpp'. Has to be refactored into 'util' or
|
|
|
|
// something like that.
|
|
|
|
namespace {
|
|
|
|
Matcher<FunctionDecl> hasAnyListedName(const std::string &FunctionNames) {
|
|
|
|
const std::vector<std::string> NameList =
|
|
|
|
utils::options::parseStringList(FunctionNames);
|
|
|
|
return hasAnyName(std::vector<StringRef>(NameList.begin(), NameList.end()));
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void OwningMemoryCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
|
|
|
|
Options.store(Opts, "LegacyResourceProducers", LegacyResourceProducers);
|
|
|
|
Options.store(Opts, "LegacyResourceConsumers", LegacyResourceConsumers);
|
|
|
|
}
|
|
|
|
|
2017-09-13 04:00:42 +08:00
|
|
|
/// Match common cases, where the owner semantic is relevant, like function
|
|
|
|
/// calls, delete expressions and others.
|
|
|
|
void OwningMemoryCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
const auto OwnerDecl = typeAliasTemplateDecl(hasName("::gsl::owner"));
|
|
|
|
const auto IsOwnerType = hasType(OwnerDecl);
|
[clang-tidy] introduce legacy resource functions to 'cppcoreguidelines-owning-memory'
Summary:
This patch introduces support for legacy C-style resource functions that must obey
the 'owner<>' semantics.
- added legacy creators like malloc,fopen,...
- added legacy consumers like free,fclose,...
This helps codes that mostly benefit from owner:
Legacy, C-Style code that isn't feasable to port directly to RAII but needs a step in between
to identify actual resource management and just using the resources.
Reviewers: aaron.ballman, alexfh, hokein
Reviewed By: aaron.ballman
Subscribers: nemanjai, JDevlieghere, xazax.hun, kbarton
Differential Revision: https://reviews.llvm.org/D38396
llvm-svn: 316092
2017-10-19 00:14:15 +08:00
|
|
|
|
|
|
|
const auto LegacyCreatorFunctions = hasAnyListedName(LegacyResourceProducers);
|
|
|
|
const auto LegacyConsumerFunctions =
|
|
|
|
hasAnyListedName(LegacyResourceConsumers);
|
|
|
|
|
|
|
|
// Legacy functions that are use for resource management but cannot be
|
|
|
|
// updated to use `gsl::owner<>`, like standard C memory management.
|
|
|
|
const auto CreatesLegacyOwner =
|
|
|
|
callExpr(callee(functionDecl(LegacyCreatorFunctions)));
|
|
|
|
// C-style functions like `::malloc()` sometimes create owners as void*
|
|
|
|
// which is expected to be cast to the correct type in C++. This case
|
2021-11-02 15:14:25 +08:00
|
|
|
// must be caught explicitly.
|
[clang-tidy] introduce legacy resource functions to 'cppcoreguidelines-owning-memory'
Summary:
This patch introduces support for legacy C-style resource functions that must obey
the 'owner<>' semantics.
- added legacy creators like malloc,fopen,...
- added legacy consumers like free,fclose,...
This helps codes that mostly benefit from owner:
Legacy, C-Style code that isn't feasable to port directly to RAII but needs a step in between
to identify actual resource management and just using the resources.
Reviewers: aaron.ballman, alexfh, hokein
Reviewed By: aaron.ballman
Subscribers: nemanjai, JDevlieghere, xazax.hun, kbarton
Differential Revision: https://reviews.llvm.org/D38396
llvm-svn: 316092
2017-10-19 00:14:15 +08:00
|
|
|
const auto LegacyOwnerCast =
|
|
|
|
castExpr(hasSourceExpression(CreatesLegacyOwner));
|
|
|
|
// Functions that do manual resource management but cannot be updated to use
|
|
|
|
// owner. Best example is `::free()`.
|
|
|
|
const auto LegacyOwnerConsumers = functionDecl(LegacyConsumerFunctions);
|
|
|
|
|
2017-09-13 04:00:42 +08:00
|
|
|
const auto CreatesOwner =
|
[clang-tidy] introduce legacy resource functions to 'cppcoreguidelines-owning-memory'
Summary:
This patch introduces support for legacy C-style resource functions that must obey
the 'owner<>' semantics.
- added legacy creators like malloc,fopen,...
- added legacy consumers like free,fclose,...
This helps codes that mostly benefit from owner:
Legacy, C-Style code that isn't feasable to port directly to RAII but needs a step in between
to identify actual resource management and just using the resources.
Reviewers: aaron.ballman, alexfh, hokein
Reviewed By: aaron.ballman
Subscribers: nemanjai, JDevlieghere, xazax.hun, kbarton
Differential Revision: https://reviews.llvm.org/D38396
llvm-svn: 316092
2017-10-19 00:14:15 +08:00
|
|
|
anyOf(cxxNewExpr(),
|
|
|
|
callExpr(callee(
|
|
|
|
functionDecl(returns(qualType(hasDeclaration(OwnerDecl)))))),
|
|
|
|
CreatesLegacyOwner, LegacyOwnerCast);
|
|
|
|
|
|
|
|
const auto ConsideredOwner = eachOf(IsOwnerType, CreatesOwner);
|
2017-09-13 04:00:42 +08:00
|
|
|
|
|
|
|
// Find delete expressions that delete non-owners.
|
|
|
|
Finder->addMatcher(
|
2020-12-11 07:52:35 +08:00
|
|
|
traverse(TK_AsIs,
|
2019-11-12 23:15:56 +08:00
|
|
|
cxxDeleteExpr(hasDescendant(declRefExpr(unless(ConsideredOwner))
|
|
|
|
.bind("deleted_variable")))
|
|
|
|
.bind("delete_expr")),
|
2017-09-13 04:00:42 +08:00
|
|
|
this);
|
|
|
|
|
[clang-tidy] introduce legacy resource functions to 'cppcoreguidelines-owning-memory'
Summary:
This patch introduces support for legacy C-style resource functions that must obey
the 'owner<>' semantics.
- added legacy creators like malloc,fopen,...
- added legacy consumers like free,fclose,...
This helps codes that mostly benefit from owner:
Legacy, C-Style code that isn't feasable to port directly to RAII but needs a step in between
to identify actual resource management and just using the resources.
Reviewers: aaron.ballman, alexfh, hokein
Reviewed By: aaron.ballman
Subscribers: nemanjai, JDevlieghere, xazax.hun, kbarton
Differential Revision: https://reviews.llvm.org/D38396
llvm-svn: 316092
2017-10-19 00:14:15 +08:00
|
|
|
// Ignoring the implicit casts is vital because the legacy owners do not work
|
|
|
|
// with the 'owner<>' annotation and therefore always implicitly cast to the
|
|
|
|
// legacy type (even 'void *').
|
|
|
|
//
|
|
|
|
// Furthermore, legacy owner functions are assumed to use raw pointers for
|
|
|
|
// resources. This check assumes that all pointer arguments of a legacy
|
|
|
|
// functions shall be 'gsl::owner<>'.
|
|
|
|
Finder->addMatcher(
|
2020-12-11 07:52:35 +08:00
|
|
|
traverse(TK_AsIs, callExpr(callee(LegacyOwnerConsumers),
|
|
|
|
hasAnyArgument(expr(
|
|
|
|
unless(ignoringImpCasts(ConsideredOwner)),
|
|
|
|
hasType(pointerType()))))
|
|
|
|
.bind("legacy_consumer")),
|
[clang-tidy] introduce legacy resource functions to 'cppcoreguidelines-owning-memory'
Summary:
This patch introduces support for legacy C-style resource functions that must obey
the 'owner<>' semantics.
- added legacy creators like malloc,fopen,...
- added legacy consumers like free,fclose,...
This helps codes that mostly benefit from owner:
Legacy, C-Style code that isn't feasable to port directly to RAII but needs a step in between
to identify actual resource management and just using the resources.
Reviewers: aaron.ballman, alexfh, hokein
Reviewed By: aaron.ballman
Subscribers: nemanjai, JDevlieghere, xazax.hun, kbarton
Differential Revision: https://reviews.llvm.org/D38396
llvm-svn: 316092
2017-10-19 00:14:15 +08:00
|
|
|
this);
|
|
|
|
|
2017-09-13 04:00:42 +08:00
|
|
|
// Matching assignment to owners, with the rhs not being an owner nor creating
|
|
|
|
// one.
|
2019-11-12 23:15:56 +08:00
|
|
|
Finder->addMatcher(
|
2020-12-11 07:52:35 +08:00
|
|
|
traverse(TK_AsIs,
|
2019-11-12 23:15:56 +08:00
|
|
|
binaryOperator(isAssignmentOperator(), hasLHS(IsOwnerType),
|
|
|
|
hasRHS(unless(ConsideredOwner)))
|
|
|
|
.bind("owner_assignment")),
|
|
|
|
this);
|
2017-09-13 04:00:42 +08:00
|
|
|
|
|
|
|
// Matching initialization of owners with non-owners, nor creating owners.
|
|
|
|
Finder->addMatcher(
|
2020-12-11 07:52:35 +08:00
|
|
|
traverse(TK_AsIs,
|
2019-11-12 23:15:56 +08:00
|
|
|
namedDecl(
|
|
|
|
varDecl(hasInitializer(unless(ConsideredOwner)), IsOwnerType)
|
|
|
|
.bind("owner_initialization"))),
|
2017-09-13 04:00:42 +08:00
|
|
|
this);
|
|
|
|
|
|
|
|
const auto HasConstructorInitializerForOwner =
|
|
|
|
has(cxxConstructorDecl(forEachConstructorInitializer(
|
2018-11-25 10:41:01 +08:00
|
|
|
cxxCtorInitializer(
|
|
|
|
isMemberInitializer(), forField(IsOwnerType),
|
|
|
|
withInitializer(
|
|
|
|
// Avoid templatesdeclaration with
|
|
|
|
// excluding parenListExpr.
|
|
|
|
allOf(unless(ConsideredOwner), unless(parenListExpr()))))
|
2017-09-13 04:00:42 +08:00
|
|
|
.bind("owner_member_initializer"))));
|
|
|
|
|
|
|
|
// Match class member initialization that expects owners, but does not get
|
|
|
|
// them.
|
2020-12-11 07:52:35 +08:00
|
|
|
Finder->addMatcher(
|
|
|
|
traverse(TK_AsIs, cxxRecordDecl(HasConstructorInitializerForOwner)),
|
|
|
|
this);
|
2017-09-13 04:00:42 +08:00
|
|
|
|
|
|
|
// Matching on assignment operations where the RHS is a newly created owner,
|
|
|
|
// but the LHS is not an owner.
|
2020-03-09 08:48:51 +08:00
|
|
|
Finder->addMatcher(binaryOperator(isAssignmentOperator(),
|
2018-11-25 10:41:01 +08:00
|
|
|
hasLHS(unless(IsOwnerType)),
|
|
|
|
hasRHS(CreatesOwner))
|
|
|
|
.bind("bad_owner_creation_assignment"),
|
|
|
|
this);
|
2017-09-13 04:00:42 +08:00
|
|
|
|
|
|
|
// Matching on initialization operations where the initial value is a newly
|
|
|
|
// created owner, but the LHS is not an owner.
|
|
|
|
Finder->addMatcher(
|
2021-11-13 07:40:18 +08:00
|
|
|
traverse(TK_AsIs, namedDecl(varDecl(allOf(hasInitializer(CreatesOwner),
|
|
|
|
unless(IsOwnerType)))
|
|
|
|
.bind("bad_owner_creation_variable"))),
|
2017-09-13 04:00:42 +08:00
|
|
|
this);
|
|
|
|
|
|
|
|
// Match on all function calls that expect owners as arguments, but didn't
|
|
|
|
// get them.
|
|
|
|
Finder->addMatcher(
|
|
|
|
callExpr(forEachArgumentWithParam(
|
|
|
|
expr(unless(ConsideredOwner)).bind("expected_owner_argument"),
|
|
|
|
parmVarDecl(IsOwnerType))),
|
|
|
|
this);
|
|
|
|
|
|
|
|
// Matching for function calls where one argument is a created owner, but the
|
|
|
|
// parameter type is not an owner.
|
|
|
|
Finder->addMatcher(callExpr(forEachArgumentWithParam(
|
|
|
|
expr(CreatesOwner).bind("bad_owner_creation_argument"),
|
|
|
|
parmVarDecl(unless(IsOwnerType))
|
|
|
|
.bind("bad_owner_creation_parameter"))),
|
|
|
|
this);
|
|
|
|
|
|
|
|
// Matching on functions, that return an owner/resource, but don't declare
|
|
|
|
// their return type as owner.
|
|
|
|
Finder->addMatcher(
|
2018-11-25 10:41:01 +08:00
|
|
|
functionDecl(hasDescendant(returnStmt(hasReturnValue(ConsideredOwner))
|
|
|
|
.bind("bad_owner_return")),
|
|
|
|
unless(returns(qualType(hasDeclaration(OwnerDecl)))))
|
2017-09-13 04:00:42 +08:00
|
|
|
.bind("function_decl"),
|
|
|
|
this);
|
|
|
|
|
|
|
|
// Match on classes that have an owner as member, but don't declare a
|
|
|
|
// destructor to properly release the owner.
|
|
|
|
Finder->addMatcher(
|
|
|
|
cxxRecordDecl(
|
2018-11-25 10:41:01 +08:00
|
|
|
has(fieldDecl(IsOwnerType).bind("undestructed_owner_member")),
|
|
|
|
anyOf(unless(has(cxxDestructorDecl())),
|
|
|
|
has(cxxDestructorDecl(anyOf(isDefaulted(), isDeleted())))))
|
2017-09-13 04:00:42 +08:00
|
|
|
.bind("non_destructor_class"),
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OwningMemoryCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto &Nodes = Result.Nodes;
|
|
|
|
|
|
|
|
bool CheckExecuted = false;
|
|
|
|
CheckExecuted |= handleDeletion(Nodes);
|
[clang-tidy] introduce legacy resource functions to 'cppcoreguidelines-owning-memory'
Summary:
This patch introduces support for legacy C-style resource functions that must obey
the 'owner<>' semantics.
- added legacy creators like malloc,fopen,...
- added legacy consumers like free,fclose,...
This helps codes that mostly benefit from owner:
Legacy, C-Style code that isn't feasable to port directly to RAII but needs a step in between
to identify actual resource management and just using the resources.
Reviewers: aaron.ballman, alexfh, hokein
Reviewed By: aaron.ballman
Subscribers: nemanjai, JDevlieghere, xazax.hun, kbarton
Differential Revision: https://reviews.llvm.org/D38396
llvm-svn: 316092
2017-10-19 00:14:15 +08:00
|
|
|
CheckExecuted |= handleLegacyConsumers(Nodes);
|
2017-09-13 04:00:42 +08:00
|
|
|
CheckExecuted |= handleExpectedOwner(Nodes);
|
|
|
|
CheckExecuted |= handleAssignmentAndInit(Nodes);
|
|
|
|
CheckExecuted |= handleAssignmentFromNewOwner(Nodes);
|
|
|
|
CheckExecuted |= handleReturnValues(Nodes);
|
|
|
|
CheckExecuted |= handleOwnerMembers(Nodes);
|
|
|
|
|
2021-07-19 12:44:08 +08:00
|
|
|
(void)CheckExecuted;
|
2017-09-13 04:00:42 +08:00
|
|
|
assert(CheckExecuted &&
|
|
|
|
"None of the subroutines executed, logic error in matcher!");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OwningMemoryCheck::handleDeletion(const BoundNodes &Nodes) {
|
|
|
|
// Result of delete matchers.
|
|
|
|
const auto *DeleteStmt = Nodes.getNodeAs<CXXDeleteExpr>("delete_expr");
|
|
|
|
const auto *DeletedVariable =
|
|
|
|
Nodes.getNodeAs<DeclRefExpr>("deleted_variable");
|
|
|
|
|
|
|
|
// Deletion of non-owners, with `delete variable;`
|
|
|
|
if (DeleteStmt) {
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(DeleteStmt->getBeginLoc(),
|
2017-09-13 04:00:42 +08:00
|
|
|
"deleting a pointer through a type that is "
|
|
|
|
"not marked 'gsl::owner<>'; consider using a "
|
|
|
|
"smart pointer instead")
|
|
|
|
<< DeletedVariable->getSourceRange();
|
2017-10-05 00:49:20 +08:00
|
|
|
|
|
|
|
// FIXME: The declaration of the variable that was deleted can be
|
|
|
|
// rewritten.
|
|
|
|
const ValueDecl *Decl = DeletedVariable->getDecl();
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(Decl->getBeginLoc(), "variable declared here", DiagnosticIDs::Note)
|
2017-10-05 00:49:20 +08:00
|
|
|
<< Decl->getSourceRange();
|
|
|
|
|
2017-09-13 04:00:42 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
[clang-tidy] introduce legacy resource functions to 'cppcoreguidelines-owning-memory'
Summary:
This patch introduces support for legacy C-style resource functions that must obey
the 'owner<>' semantics.
- added legacy creators like malloc,fopen,...
- added legacy consumers like free,fclose,...
This helps codes that mostly benefit from owner:
Legacy, C-Style code that isn't feasable to port directly to RAII but needs a step in between
to identify actual resource management and just using the resources.
Reviewers: aaron.ballman, alexfh, hokein
Reviewed By: aaron.ballman
Subscribers: nemanjai, JDevlieghere, xazax.hun, kbarton
Differential Revision: https://reviews.llvm.org/D38396
llvm-svn: 316092
2017-10-19 00:14:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool OwningMemoryCheck::handleLegacyConsumers(const BoundNodes &Nodes) {
|
|
|
|
// Result of matching for legacy consumer-functions like `::free()`.
|
|
|
|
const auto *LegacyConsumer = Nodes.getNodeAs<CallExpr>("legacy_consumer");
|
|
|
|
|
2020-04-05 14:28:11 +08:00
|
|
|
// FIXME: `freopen` should be handled separately because it takes the filename
|
[clang-tidy] introduce legacy resource functions to 'cppcoreguidelines-owning-memory'
Summary:
This patch introduces support for legacy C-style resource functions that must obey
the 'owner<>' semantics.
- added legacy creators like malloc,fopen,...
- added legacy consumers like free,fclose,...
This helps codes that mostly benefit from owner:
Legacy, C-Style code that isn't feasable to port directly to RAII but needs a step in between
to identify actual resource management and just using the resources.
Reviewers: aaron.ballman, alexfh, hokein
Reviewed By: aaron.ballman
Subscribers: nemanjai, JDevlieghere, xazax.hun, kbarton
Differential Revision: https://reviews.llvm.org/D38396
llvm-svn: 316092
2017-10-19 00:14:15 +08:00
|
|
|
// as a pointer, which should not be an owner. The argument that is an owner
|
|
|
|
// is known and the false positive coming from the filename can be avoided.
|
|
|
|
if (LegacyConsumer) {
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(LegacyConsumer->getBeginLoc(),
|
[clang-tidy] introduce legacy resource functions to 'cppcoreguidelines-owning-memory'
Summary:
This patch introduces support for legacy C-style resource functions that must obey
the 'owner<>' semantics.
- added legacy creators like malloc,fopen,...
- added legacy consumers like free,fclose,...
This helps codes that mostly benefit from owner:
Legacy, C-Style code that isn't feasable to port directly to RAII but needs a step in between
to identify actual resource management and just using the resources.
Reviewers: aaron.ballman, alexfh, hokein
Reviewed By: aaron.ballman
Subscribers: nemanjai, JDevlieghere, xazax.hun, kbarton
Differential Revision: https://reviews.llvm.org/D38396
llvm-svn: 316092
2017-10-19 00:14:15 +08:00
|
|
|
"calling legacy resource function without passing a 'gsl::owner<>'")
|
|
|
|
<< LegacyConsumer->getSourceRange();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2017-09-13 04:00:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool OwningMemoryCheck::handleExpectedOwner(const BoundNodes &Nodes) {
|
|
|
|
// Result of function call matchers.
|
|
|
|
const auto *ExpectedOwner = Nodes.getNodeAs<Expr>("expected_owner_argument");
|
|
|
|
|
|
|
|
// Expected function argument to be owner.
|
|
|
|
if (ExpectedOwner) {
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(ExpectedOwner->getBeginLoc(),
|
2017-09-13 04:00:42 +08:00
|
|
|
"expected argument of type 'gsl::owner<>'; got %0")
|
|
|
|
<< ExpectedOwner->getType() << ExpectedOwner->getSourceRange();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Assignment and initialization of owner variables.
|
|
|
|
bool OwningMemoryCheck::handleAssignmentAndInit(const BoundNodes &Nodes) {
|
|
|
|
const auto *OwnerAssignment =
|
|
|
|
Nodes.getNodeAs<BinaryOperator>("owner_assignment");
|
|
|
|
const auto *OwnerInitialization =
|
|
|
|
Nodes.getNodeAs<VarDecl>("owner_initialization");
|
|
|
|
const auto *OwnerInitializer =
|
|
|
|
Nodes.getNodeAs<CXXCtorInitializer>("owner_member_initializer");
|
|
|
|
|
|
|
|
// Assignments to owners.
|
|
|
|
if (OwnerAssignment) {
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(OwnerAssignment->getBeginLoc(),
|
2017-09-13 04:00:42 +08:00
|
|
|
"expected assignment source to be of type 'gsl::owner<>'; got %0")
|
|
|
|
<< OwnerAssignment->getRHS()->getType()
|
|
|
|
<< OwnerAssignment->getSourceRange();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialization of owners.
|
|
|
|
if (OwnerInitialization) {
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(OwnerInitialization->getBeginLoc(),
|
2017-09-13 04:00:42 +08:00
|
|
|
"expected initialization with value of type 'gsl::owner<>'; got %0")
|
|
|
|
<< OwnerInitialization->getAnyInitializer()->getType()
|
|
|
|
<< OwnerInitialization->getSourceRange();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initializer of class constructors that initialize owners.
|
|
|
|
if (OwnerInitializer) {
|
|
|
|
diag(OwnerInitializer->getSourceLocation(),
|
|
|
|
"expected initialization of owner member variable with value of type "
|
|
|
|
"'gsl::owner<>'; got %0")
|
|
|
|
// FIXME: the expression from getInit has type 'void', but the type
|
|
|
|
// of the supplied argument would be of interest.
|
|
|
|
<< OwnerInitializer->getInit()->getType()
|
|
|
|
<< OwnerInitializer->getSourceRange();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Problematic assignment and initializations, since the assigned value is a
|
|
|
|
/// newly created owner.
|
|
|
|
bool OwningMemoryCheck::handleAssignmentFromNewOwner(const BoundNodes &Nodes) {
|
|
|
|
const auto *BadOwnerAssignment =
|
|
|
|
Nodes.getNodeAs<BinaryOperator>("bad_owner_creation_assignment");
|
|
|
|
const auto *BadOwnerInitialization =
|
|
|
|
Nodes.getNodeAs<VarDecl>("bad_owner_creation_variable");
|
|
|
|
|
|
|
|
const auto *BadOwnerArgument =
|
|
|
|
Nodes.getNodeAs<Expr>("bad_owner_creation_argument");
|
|
|
|
const auto *BadOwnerParameter =
|
|
|
|
Nodes.getNodeAs<ParmVarDecl>("bad_owner_creation_parameter");
|
|
|
|
|
|
|
|
// Bad assignments to non-owners, where the RHS is a newly created owner.
|
|
|
|
if (BadOwnerAssignment) {
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(BadOwnerAssignment->getBeginLoc(),
|
2017-09-13 04:00:42 +08:00
|
|
|
"assigning newly created 'gsl::owner<>' to non-owner %0")
|
|
|
|
<< BadOwnerAssignment->getLHS()->getType()
|
|
|
|
<< BadOwnerAssignment->getSourceRange();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bad initialization of non-owners, where the RHS is a newly created owner.
|
|
|
|
if (BadOwnerInitialization) {
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(BadOwnerInitialization->getBeginLoc(),
|
2017-09-13 04:00:42 +08:00
|
|
|
"initializing non-owner %0 with a newly created 'gsl::owner<>'")
|
|
|
|
<< BadOwnerInitialization->getType()
|
|
|
|
<< BadOwnerInitialization->getSourceRange();
|
2017-10-05 00:49:20 +08:00
|
|
|
|
|
|
|
// FIXME: FixitHint to rewrite the type of the initialized variable
|
|
|
|
// as 'gsl::owner<OriginalType>'
|
2017-09-13 04:00:42 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function call, where one arguments is a newly created owner, but the
|
|
|
|
// parameter type is not.
|
|
|
|
if (BadOwnerArgument) {
|
|
|
|
assert(BadOwnerParameter &&
|
|
|
|
"parameter for the problematic argument not found");
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(BadOwnerArgument->getBeginLoc(), "initializing non-owner argument of "
|
2017-09-13 04:00:42 +08:00
|
|
|
"type %0 with a newly created "
|
|
|
|
"'gsl::owner<>'")
|
|
|
|
<< BadOwnerParameter->getType() << BadOwnerArgument->getSourceRange();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OwningMemoryCheck::handleReturnValues(const BoundNodes &Nodes) {
|
|
|
|
// Function return statements, that are owners/resources, but the function
|
|
|
|
// declaration does not declare its return value as owner.
|
|
|
|
const auto *BadReturnType = Nodes.getNodeAs<ReturnStmt>("bad_owner_return");
|
|
|
|
const auto *Function = Nodes.getNodeAs<FunctionDecl>("function_decl");
|
|
|
|
|
|
|
|
// Function return values, that should be owners but aren't.
|
|
|
|
if (BadReturnType) {
|
2017-10-05 00:49:20 +08:00
|
|
|
// The returned value is a resource or variable that was not annotated with
|
|
|
|
// owner<> and the function return type is not owner<>.
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(BadReturnType->getBeginLoc(),
|
2017-09-13 04:00:42 +08:00
|
|
|
"returning a newly created resource of "
|
|
|
|
"type %0 or 'gsl::owner<>' from a "
|
|
|
|
"function whose return type is not 'gsl::owner<>'")
|
|
|
|
<< Function->getReturnType() << BadReturnType->getSourceRange();
|
2017-10-05 00:49:20 +08:00
|
|
|
|
|
|
|
// FIXME: Rewrite the return type as 'gsl::owner<OriginalType>'
|
2017-09-13 04:00:42 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OwningMemoryCheck::handleOwnerMembers(const BoundNodes &Nodes) {
|
|
|
|
// Classes, that have owners as member, but do not declare destructors
|
|
|
|
// accordingly.
|
|
|
|
const auto *BadClass = Nodes.getNodeAs<CXXRecordDecl>("non_destructor_class");
|
|
|
|
|
|
|
|
// Classes, that contains owners, but do not declare destructors.
|
|
|
|
if (BadClass) {
|
|
|
|
const auto *DeclaredOwnerMember =
|
|
|
|
Nodes.getNodeAs<FieldDecl>("undestructed_owner_member");
|
|
|
|
assert(DeclaredOwnerMember &&
|
|
|
|
"match on class with bad destructor but without a declared owner");
|
|
|
|
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(DeclaredOwnerMember->getBeginLoc(),
|
2017-09-13 04:00:42 +08:00
|
|
|
"member variable of type 'gsl::owner<>' requires the class %0 to "
|
|
|
|
"implement a destructor to release the owned resource")
|
|
|
|
<< BadClass;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cppcoreguidelines
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|