2013-08-13 05:20:55 +08:00
|
|
|
//===- Consumed.cpp --------------------------------------------*- C++ --*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// A intra-procedural analysis for checking consumed properties. This is based,
|
|
|
|
// in part, on research on linear types.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Attr.h"
|
|
|
|
#include "clang/AST/DeclCXX.h"
|
|
|
|
#include "clang/AST/ExprCXX.h"
|
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
|
|
|
#include "clang/AST/StmtCXX.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2013-08-13 05:20:55 +08:00
|
|
|
#include "clang/AST/Type.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "clang/Analysis/Analyses/Consumed.h"
|
2013-08-13 05:20:55 +08:00
|
|
|
#include "clang/Analysis/Analyses/PostOrderCFGView.h"
|
|
|
|
#include "clang/Analysis/AnalysisContext.h"
|
|
|
|
#include "clang/Analysis/CFG.h"
|
|
|
|
#include "clang/Basic/OperatorKinds.h"
|
|
|
|
#include "clang/Basic/SourceLocation.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2013-08-30 01:26:57 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2013-08-13 05:20:55 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-03-09 19:36:40 +08:00
|
|
|
#include <memory>
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
// TODO: Adjust states of args to constructors in the same way that arguments to
|
|
|
|
// function calls are handled.
|
|
|
|
// TODO: Use information from tests in for- and while-loop conditional.
|
2013-09-04 04:11:38 +08:00
|
|
|
// TODO: Add notes about the actual and expected state for
|
2013-08-30 01:26:57 +08:00
|
|
|
// TODO: Correctly identify unreachable blocks when chaining boolean operators.
|
2013-10-05 05:28:06 +08:00
|
|
|
// TODO: Adjust the parser and AttributesList class to support lists of
|
|
|
|
// identifiers.
|
2013-08-30 01:26:57 +08:00
|
|
|
// TODO: Warn about unreachable code.
|
|
|
|
// TODO: Switch to using a bitmap to track unreachable blocks.
|
|
|
|
// TODO: Handle variable definitions, e.g. bool valid = x.isValid();
|
|
|
|
// if (valid) ...; (Deferred)
|
2013-08-13 05:20:55 +08:00
|
|
|
// TODO: Take notes on state transitions to provide better warning messages.
|
|
|
|
// (Deferred)
|
|
|
|
// TODO: Test nested conditionals: A) Checking the same value multiple times,
|
|
|
|
// and 2) Checking different values. (Deferred)
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace consumed;
|
|
|
|
|
|
|
|
// Key method definition
|
|
|
|
ConsumedWarningsHandlerBase::~ConsumedWarningsHandlerBase() {}
|
|
|
|
|
2013-10-18 02:19:31 +08:00
|
|
|
static SourceLocation getFirstStmtLoc(const CFGBlock *Block) {
|
|
|
|
// Find the source location of the first statement in the block, if the block
|
|
|
|
// is not empty.
|
|
|
|
for (CFGBlock::const_iterator BI = Block->begin(), BE = Block->end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>())
|
|
|
|
return CS->getStmt()->getLocStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block is empty.
|
|
|
|
// If we have one successor, return the first statement in that block
|
|
|
|
if (Block->succ_size() == 1 && *Block->succ_begin())
|
|
|
|
return getFirstStmtLoc(*Block->succ_begin());
|
|
|
|
|
|
|
|
return SourceLocation();
|
|
|
|
}
|
|
|
|
|
2013-10-12 05:55:33 +08:00
|
|
|
static SourceLocation getLastStmtLoc(const CFGBlock *Block) {
|
2013-10-10 02:30:24 +08:00
|
|
|
// Find the source location of the last statement in the block, if the block
|
|
|
|
// is not empty.
|
2013-10-12 05:55:33 +08:00
|
|
|
if (const Stmt *StmtNode = Block->getTerminator()) {
|
2013-10-10 02:30:24 +08:00
|
|
|
return StmtNode->getLocStart();
|
|
|
|
} else {
|
2013-10-12 05:55:33 +08:00
|
|
|
for (CFGBlock::const_reverse_iterator BI = Block->rbegin(),
|
|
|
|
BE = Block->rend(); BI != BE; ++BI) {
|
2013-10-10 02:30:24 +08:00
|
|
|
if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>())
|
|
|
|
return CS->getStmt()->getLocStart();
|
|
|
|
}
|
|
|
|
}
|
2013-10-18 02:19:31 +08:00
|
|
|
|
|
|
|
// If we have one successor, return the first statement in that block
|
|
|
|
SourceLocation Loc;
|
|
|
|
if (Block->succ_size() == 1 && *Block->succ_begin())
|
|
|
|
Loc = getFirstStmtLoc(*Block->succ_begin());
|
|
|
|
if (Loc.isValid())
|
|
|
|
return Loc;
|
2013-10-18 06:53:04 +08:00
|
|
|
|
2013-10-18 02:19:31 +08:00
|
|
|
// If we have one predecessor, return the last statement in that block
|
|
|
|
if (Block->pred_size() == 1 && *Block->pred_begin())
|
|
|
|
return getLastStmtLoc(*Block->pred_begin());
|
|
|
|
|
|
|
|
return Loc;
|
2013-10-10 02:30:24 +08:00
|
|
|
}
|
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
static ConsumedState invertConsumedUnconsumed(ConsumedState State) {
|
|
|
|
switch (State) {
|
|
|
|
case CS_Unconsumed:
|
|
|
|
return CS_Consumed;
|
|
|
|
case CS_Consumed:
|
|
|
|
return CS_Unconsumed;
|
|
|
|
case CS_None:
|
|
|
|
return CS_None;
|
|
|
|
case CS_Unknown:
|
|
|
|
return CS_Unknown;
|
|
|
|
}
|
|
|
|
llvm_unreachable("invalid enum");
|
|
|
|
}
|
|
|
|
|
2013-10-05 05:28:06 +08:00
|
|
|
static bool isCallableInState(const CallableWhenAttr *CWAttr,
|
|
|
|
ConsumedState State) {
|
|
|
|
|
|
|
|
CallableWhenAttr::callableState_iterator I = CWAttr->callableState_begin(),
|
|
|
|
E = CWAttr->callableState_end();
|
|
|
|
|
|
|
|
for (; I != E; ++I) {
|
|
|
|
|
|
|
|
ConsumedState MappedAttrState = CS_None;
|
|
|
|
|
|
|
|
switch (*I) {
|
|
|
|
case CallableWhenAttr::Unknown:
|
|
|
|
MappedAttrState = CS_Unknown;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CallableWhenAttr::Unconsumed:
|
|
|
|
MappedAttrState = CS_Unconsumed;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CallableWhenAttr::Consumed:
|
|
|
|
MappedAttrState = CS_Consumed;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MappedAttrState == State)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-14 08:36:53 +08:00
|
|
|
|
2013-08-31 06:56:34 +08:00
|
|
|
static bool isConsumableType(const QualType &QT) {
|
2013-10-31 23:38:12 +08:00
|
|
|
if (QT->isPointerType() || QT->isReferenceType())
|
|
|
|
return false;
|
|
|
|
|
2013-08-31 06:56:34 +08:00
|
|
|
if (const CXXRecordDecl *RD = QT->getAsCXXRecordDecl())
|
|
|
|
return RD->hasAttr<ConsumableAttr>();
|
2013-10-31 23:38:12 +08:00
|
|
|
|
|
|
|
return false;
|
2013-08-31 06:56:34 +08:00
|
|
|
}
|
|
|
|
|
2014-01-14 08:36:53 +08:00
|
|
|
static bool isAutoCastType(const QualType &QT) {
|
|
|
|
if (QT->isPointerType() || QT->isReferenceType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (const CXXRecordDecl *RD = QT->getAsCXXRecordDecl())
|
|
|
|
return RD->hasAttr<ConsumableAutoCastAttr>();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isSetOnReadPtrType(const QualType &QT) {
|
|
|
|
if (const CXXRecordDecl *RD = QT->getPointeeCXXRecordDecl())
|
|
|
|
return RD->hasAttr<ConsumableSetOnReadAttr>();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
static bool isKnownState(ConsumedState State) {
|
|
|
|
switch (State) {
|
|
|
|
case CS_Unconsumed:
|
|
|
|
case CS_Consumed:
|
|
|
|
return true;
|
|
|
|
case CS_None:
|
|
|
|
case CS_Unknown:
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-30 04:36:09 +08:00
|
|
|
llvm_unreachable("invalid enum");
|
2013-08-30 01:26:57 +08:00
|
|
|
}
|
|
|
|
|
2014-01-17 07:07:16 +08:00
|
|
|
static bool isRValueRef(QualType ParamType) {
|
|
|
|
return ParamType->isRValueReferenceType();
|
2013-10-19 03:25:18 +08:00
|
|
|
}
|
|
|
|
|
2013-08-23 04:44:47 +08:00
|
|
|
static bool isTestingFunction(const FunctionDecl *FunDecl) {
|
2013-10-30 04:28:41 +08:00
|
|
|
return FunDecl->hasAttr<TestTypestateAttr>();
|
2013-08-23 04:44:47 +08:00
|
|
|
}
|
|
|
|
|
2014-01-14 08:36:53 +08:00
|
|
|
static bool isPointerOrRef(QualType ParamType) {
|
|
|
|
return ParamType->isPointerType() || ParamType->isReferenceType();
|
2013-10-19 03:25:18 +08:00
|
|
|
}
|
|
|
|
|
2013-09-06 09:28:43 +08:00
|
|
|
static ConsumedState mapConsumableAttrState(const QualType QT) {
|
|
|
|
assert(isConsumableType(QT));
|
|
|
|
|
|
|
|
const ConsumableAttr *CAttr =
|
|
|
|
QT->getAsCXXRecordDecl()->getAttr<ConsumableAttr>();
|
|
|
|
|
|
|
|
switch (CAttr->getDefaultState()) {
|
|
|
|
case ConsumableAttr::Unknown:
|
|
|
|
return CS_Unknown;
|
|
|
|
case ConsumableAttr::Unconsumed:
|
|
|
|
return CS_Unconsumed;
|
|
|
|
case ConsumableAttr::Consumed:
|
|
|
|
return CS_Consumed;
|
|
|
|
}
|
|
|
|
llvm_unreachable("invalid enum");
|
|
|
|
}
|
|
|
|
|
2013-10-18 07:23:53 +08:00
|
|
|
static ConsumedState
|
|
|
|
mapParamTypestateAttrState(const ParamTypestateAttr *PTAttr) {
|
|
|
|
switch (PTAttr->getParamState()) {
|
|
|
|
case ParamTypestateAttr::Unknown:
|
2013-10-12 07:03:26 +08:00
|
|
|
return CS_Unknown;
|
2013-10-18 07:23:53 +08:00
|
|
|
case ParamTypestateAttr::Unconsumed:
|
2013-10-12 07:03:26 +08:00
|
|
|
return CS_Unconsumed;
|
2013-10-18 07:23:53 +08:00
|
|
|
case ParamTypestateAttr::Consumed:
|
2013-10-12 07:03:26 +08:00
|
|
|
return CS_Consumed;
|
|
|
|
}
|
|
|
|
llvm_unreachable("invalid_enum");
|
|
|
|
}
|
|
|
|
|
2013-09-04 04:43:00 +08:00
|
|
|
static ConsumedState
|
|
|
|
mapReturnTypestateAttrState(const ReturnTypestateAttr *RTSAttr) {
|
2013-09-04 04:11:38 +08:00
|
|
|
switch (RTSAttr->getState()) {
|
|
|
|
case ReturnTypestateAttr::Unknown:
|
|
|
|
return CS_Unknown;
|
|
|
|
case ReturnTypestateAttr::Unconsumed:
|
|
|
|
return CS_Unconsumed;
|
|
|
|
case ReturnTypestateAttr::Consumed:
|
|
|
|
return CS_Consumed;
|
|
|
|
}
|
2013-09-04 04:43:00 +08:00
|
|
|
llvm_unreachable("invalid enum");
|
2013-09-04 04:11:38 +08:00
|
|
|
}
|
|
|
|
|
2013-10-18 07:23:53 +08:00
|
|
|
static ConsumedState mapSetTypestateAttrState(const SetTypestateAttr *STAttr) {
|
|
|
|
switch (STAttr->getNewState()) {
|
|
|
|
case SetTypestateAttr::Unknown:
|
|
|
|
return CS_Unknown;
|
|
|
|
case SetTypestateAttr::Unconsumed:
|
|
|
|
return CS_Unconsumed;
|
|
|
|
case SetTypestateAttr::Consumed:
|
|
|
|
return CS_Consumed;
|
|
|
|
}
|
|
|
|
llvm_unreachable("invalid_enum");
|
|
|
|
}
|
|
|
|
|
2013-08-13 05:20:55 +08:00
|
|
|
static StringRef stateToString(ConsumedState State) {
|
|
|
|
switch (State) {
|
|
|
|
case consumed::CS_None:
|
|
|
|
return "none";
|
|
|
|
|
|
|
|
case consumed::CS_Unknown:
|
|
|
|
return "unknown";
|
|
|
|
|
|
|
|
case consumed::CS_Unconsumed:
|
|
|
|
return "unconsumed";
|
|
|
|
|
|
|
|
case consumed::CS_Consumed:
|
|
|
|
return "consumed";
|
|
|
|
}
|
2013-08-13 08:11:59 +08:00
|
|
|
llvm_unreachable("invalid enum");
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
|
2013-10-12 06:30:48 +08:00
|
|
|
static ConsumedState testsFor(const FunctionDecl *FunDecl) {
|
|
|
|
assert(isTestingFunction(FunDecl));
|
2013-10-30 04:28:41 +08:00
|
|
|
switch (FunDecl->getAttr<TestTypestateAttr>()->getTestState()) {
|
|
|
|
case TestTypestateAttr::Unconsumed:
|
2013-10-12 06:30:48 +08:00
|
|
|
return CS_Unconsumed;
|
2013-10-30 04:28:41 +08:00
|
|
|
case TestTypestateAttr::Consumed:
|
2013-10-12 06:30:48 +08:00
|
|
|
return CS_Consumed;
|
|
|
|
}
|
|
|
|
llvm_unreachable("invalid enum");
|
|
|
|
}
|
|
|
|
|
2013-08-13 05:20:55 +08:00
|
|
|
namespace {
|
2013-08-30 01:26:57 +08:00
|
|
|
struct VarTestResult {
|
|
|
|
const VarDecl *Var;
|
|
|
|
ConsumedState TestsFor;
|
|
|
|
};
|
|
|
|
} // end anonymous::VarTestResult
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace consumed {
|
|
|
|
|
|
|
|
enum EffectiveOp {
|
|
|
|
EO_And,
|
|
|
|
EO_Or
|
|
|
|
};
|
|
|
|
|
|
|
|
class PropagationInfo {
|
|
|
|
enum {
|
|
|
|
IT_None,
|
|
|
|
IT_State,
|
2013-11-16 08:22:43 +08:00
|
|
|
IT_VarTest,
|
2013-08-30 01:26:57 +08:00
|
|
|
IT_BinTest,
|
2013-11-16 08:22:43 +08:00
|
|
|
IT_Var,
|
|
|
|
IT_Tmp
|
2013-08-30 01:26:57 +08:00
|
|
|
} InfoType;
|
2013-08-30 02:00:58 +08:00
|
|
|
|
|
|
|
struct BinTestTy {
|
|
|
|
const BinaryOperator *Source;
|
|
|
|
EffectiveOp EOp;
|
|
|
|
VarTestResult LTest;
|
|
|
|
VarTestResult RTest;
|
|
|
|
};
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
union {
|
2013-08-13 05:20:55 +08:00
|
|
|
ConsumedState State;
|
2013-11-16 08:22:43 +08:00
|
|
|
VarTestResult VarTest;
|
2013-08-13 05:20:55 +08:00
|
|
|
const VarDecl *Var;
|
2013-11-16 08:22:43 +08:00
|
|
|
const CXXBindTemporaryExpr *Tmp;
|
2013-08-30 02:00:58 +08:00
|
|
|
BinTestTy BinTest;
|
2013-08-13 05:20:55 +08:00
|
|
|
};
|
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
public:
|
|
|
|
PropagationInfo() : InfoType(IT_None) {}
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
PropagationInfo(const VarTestResult &VarTest)
|
|
|
|
: InfoType(IT_VarTest), VarTest(VarTest) {}
|
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
PropagationInfo(const VarDecl *Var, ConsumedState TestsFor)
|
2013-11-16 08:22:43 +08:00
|
|
|
: InfoType(IT_VarTest) {
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
VarTest.Var = Var;
|
|
|
|
VarTest.TestsFor = TestsFor;
|
2013-08-30 01:26:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PropagationInfo(const BinaryOperator *Source, EffectiveOp EOp,
|
|
|
|
const VarTestResult <est, const VarTestResult &RTest)
|
|
|
|
: InfoType(IT_BinTest) {
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
BinTest.Source = Source;
|
|
|
|
BinTest.EOp = EOp;
|
|
|
|
BinTest.LTest = LTest;
|
|
|
|
BinTest.RTest = RTest;
|
|
|
|
}
|
|
|
|
|
|
|
|
PropagationInfo(const BinaryOperator *Source, EffectiveOp EOp,
|
|
|
|
const VarDecl *LVar, ConsumedState LTestsFor,
|
|
|
|
const VarDecl *RVar, ConsumedState RTestsFor)
|
|
|
|
: InfoType(IT_BinTest) {
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
BinTest.Source = Source;
|
|
|
|
BinTest.EOp = EOp;
|
|
|
|
BinTest.LTest.Var = LVar;
|
|
|
|
BinTest.LTest.TestsFor = LTestsFor;
|
|
|
|
BinTest.RTest.Var = RVar;
|
|
|
|
BinTest.RTest.TestsFor = RTestsFor;
|
|
|
|
}
|
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
PropagationInfo(ConsumedState State)
|
|
|
|
: InfoType(IT_State), State(State) {}
|
2013-10-05 05:28:06 +08:00
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
PropagationInfo(const VarDecl *Var) : InfoType(IT_Var), Var(Var) {}
|
2013-11-16 08:22:43 +08:00
|
|
|
PropagationInfo(const CXXBindTemporaryExpr *Tmp)
|
|
|
|
: InfoType(IT_Tmp), Tmp(Tmp) {}
|
2013-08-30 01:26:57 +08:00
|
|
|
|
|
|
|
const ConsumedState & getState() const {
|
|
|
|
assert(InfoType == IT_State);
|
|
|
|
return State;
|
|
|
|
}
|
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
const VarTestResult & getVarTest() const {
|
|
|
|
assert(InfoType == IT_VarTest);
|
|
|
|
return VarTest;
|
2013-08-30 01:26:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const VarTestResult & getLTest() const {
|
|
|
|
assert(InfoType == IT_BinTest);
|
|
|
|
return BinTest.LTest;
|
|
|
|
}
|
|
|
|
|
|
|
|
const VarTestResult & getRTest() const {
|
|
|
|
assert(InfoType == IT_BinTest);
|
|
|
|
return BinTest.RTest;
|
|
|
|
}
|
|
|
|
|
|
|
|
const VarDecl * getVar() const {
|
|
|
|
assert(InfoType == IT_Var);
|
|
|
|
return Var;
|
|
|
|
}
|
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
const CXXBindTemporaryExpr * getTmp() const {
|
|
|
|
assert(InfoType == IT_Tmp);
|
|
|
|
return Tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConsumedState getAsState(const ConsumedStateMap *StateMap) const {
|
|
|
|
assert(isVar() || isTmp() || isState());
|
|
|
|
|
|
|
|
if (isVar())
|
|
|
|
return StateMap->getState(Var);
|
|
|
|
else if (isTmp())
|
|
|
|
return StateMap->getState(Tmp);
|
|
|
|
else if (isState())
|
|
|
|
return State;
|
|
|
|
else
|
|
|
|
return CS_None;
|
|
|
|
}
|
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
EffectiveOp testEffectiveOp() const {
|
|
|
|
assert(InfoType == IT_BinTest);
|
|
|
|
return BinTest.EOp;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BinaryOperator * testSourceNode() const {
|
|
|
|
assert(InfoType == IT_BinTest);
|
|
|
|
return BinTest.Source;
|
|
|
|
}
|
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
inline bool isValid() const { return InfoType != IT_None; }
|
|
|
|
inline bool isState() const { return InfoType == IT_State; }
|
|
|
|
inline bool isVarTest() const { return InfoType == IT_VarTest; }
|
|
|
|
inline bool isBinTest() const { return InfoType == IT_BinTest; }
|
|
|
|
inline bool isVar() const { return InfoType == IT_Var; }
|
|
|
|
inline bool isTmp() const { return InfoType == IT_Tmp; }
|
|
|
|
|
|
|
|
bool isTest() const {
|
|
|
|
return InfoType == IT_VarTest || InfoType == IT_BinTest;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isPointerToValue() const {
|
|
|
|
return InfoType == IT_Var || InfoType == IT_Tmp;
|
|
|
|
}
|
2013-08-30 01:26:57 +08:00
|
|
|
|
|
|
|
PropagationInfo invertTest() const {
|
2013-11-16 08:22:43 +08:00
|
|
|
assert(InfoType == IT_VarTest || InfoType == IT_BinTest);
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
if (InfoType == IT_VarTest) {
|
|
|
|
return PropagationInfo(VarTest.Var,
|
|
|
|
invertConsumedUnconsumed(VarTest.TestsFor));
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
} else if (InfoType == IT_BinTest) {
|
|
|
|
return PropagationInfo(BinTest.Source,
|
|
|
|
BinTest.EOp == EO_And ? EO_Or : EO_And,
|
|
|
|
BinTest.LTest.Var, invertConsumedUnconsumed(BinTest.LTest.TestsFor),
|
|
|
|
BinTest.RTest.Var, invertConsumedUnconsumed(BinTest.RTest.TestsFor));
|
|
|
|
} else {
|
|
|
|
return PropagationInfo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
static inline void
|
|
|
|
setStateForVarOrTmp(ConsumedStateMap *StateMap, const PropagationInfo &PInfo,
|
|
|
|
ConsumedState State) {
|
|
|
|
|
|
|
|
assert(PInfo.isVar() || PInfo.isTmp());
|
|
|
|
|
|
|
|
if (PInfo.isVar())
|
|
|
|
StateMap->setState(PInfo.getVar(), State);
|
|
|
|
else
|
|
|
|
StateMap->setState(PInfo.getTmp(), State);
|
|
|
|
}
|
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
class ConsumedStmtVisitor : public ConstStmtVisitor<ConsumedStmtVisitor> {
|
2013-08-13 05:20:55 +08:00
|
|
|
|
|
|
|
typedef llvm::DenseMap<const Stmt *, PropagationInfo> MapType;
|
|
|
|
typedef std::pair<const Stmt *, PropagationInfo> PairType;
|
|
|
|
typedef MapType::iterator InfoEntry;
|
2013-08-30 01:26:57 +08:00
|
|
|
typedef MapType::const_iterator ConstInfoEntry;
|
|
|
|
|
2013-08-13 07:49:39 +08:00
|
|
|
AnalysisDeclContext &AC;
|
2013-08-13 05:20:55 +08:00
|
|
|
ConsumedAnalyzer &Analyzer;
|
|
|
|
ConsumedStateMap *StateMap;
|
|
|
|
MapType PropagationMap;
|
2014-01-14 08:36:53 +08:00
|
|
|
|
2013-08-13 05:20:55 +08:00
|
|
|
void forwardInfo(const Stmt *From, const Stmt *To);
|
2014-01-14 08:36:53 +08:00
|
|
|
void copyInfo(const Stmt *From, const Stmt *To, ConsumedState CS);
|
|
|
|
ConsumedState getInfo(const Stmt *From);
|
|
|
|
void setInfo(const Stmt *To, ConsumedState NS);
|
|
|
|
void propagateReturnType(const Stmt *Call, const FunctionDecl *Fun);
|
2013-10-19 07:11:49 +08:00
|
|
|
|
2013-08-13 05:20:55 +08:00
|
|
|
public:
|
2013-10-12 05:55:33 +08:00
|
|
|
void checkCallability(const PropagationInfo &PInfo,
|
|
|
|
const FunctionDecl *FunDecl,
|
|
|
|
SourceLocation BlameLoc);
|
2014-01-14 08:36:53 +08:00
|
|
|
bool handleCall(const CallExpr *Call, const Expr *ObjArg,
|
|
|
|
const FunctionDecl *FunD);
|
2013-08-13 05:20:55 +08:00
|
|
|
|
|
|
|
void VisitBinaryOperator(const BinaryOperator *BinOp);
|
|
|
|
void VisitCallExpr(const CallExpr *Call);
|
|
|
|
void VisitCastExpr(const CastExpr *Cast);
|
2013-10-12 05:55:33 +08:00
|
|
|
void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Temp);
|
2013-08-13 05:20:55 +08:00
|
|
|
void VisitCXXConstructExpr(const CXXConstructExpr *Call);
|
|
|
|
void VisitCXXMemberCallExpr(const CXXMemberCallExpr *Call);
|
|
|
|
void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *Call);
|
|
|
|
void VisitDeclRefExpr(const DeclRefExpr *DeclRef);
|
|
|
|
void VisitDeclStmt(const DeclStmt *DelcS);
|
|
|
|
void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Temp);
|
|
|
|
void VisitMemberExpr(const MemberExpr *MExpr);
|
2013-08-30 06:36:05 +08:00
|
|
|
void VisitParmVarDecl(const ParmVarDecl *Param);
|
2013-09-04 04:11:38 +08:00
|
|
|
void VisitReturnStmt(const ReturnStmt *Ret);
|
2013-08-13 05:20:55 +08:00
|
|
|
void VisitUnaryOperator(const UnaryOperator *UOp);
|
|
|
|
void VisitVarDecl(const VarDecl *Var);
|
2013-08-13 07:49:39 +08:00
|
|
|
|
2013-08-30 06:36:05 +08:00
|
|
|
ConsumedStmtVisitor(AnalysisDeclContext &AC, ConsumedAnalyzer &Analyzer,
|
|
|
|
ConsumedStateMap *StateMap)
|
|
|
|
: AC(AC), Analyzer(Analyzer), StateMap(StateMap) {}
|
2013-08-30 01:26:57 +08:00
|
|
|
|
|
|
|
PropagationInfo getInfo(const Stmt *StmtNode) const {
|
|
|
|
ConstInfoEntry Entry = PropagationMap.find(StmtNode);
|
|
|
|
|
|
|
|
if (Entry != PropagationMap.end())
|
|
|
|
return Entry->second;
|
|
|
|
else
|
|
|
|
return PropagationInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset(ConsumedStateMap *NewStateMap) {
|
|
|
|
StateMap = NewStateMap;
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-14 08:36:53 +08:00
|
|
|
|
|
|
|
void ConsumedStmtVisitor::forwardInfo(const Stmt *From, const Stmt *To) {
|
|
|
|
InfoEntry Entry = PropagationMap.find(From);
|
|
|
|
if (Entry != PropagationMap.end())
|
|
|
|
PropagationMap.insert(PairType(To, Entry->second));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create a new state for To, which is initialized to the state of From.
|
|
|
|
// If NS is not CS_None, sets the state of From to NS.
|
|
|
|
void ConsumedStmtVisitor::copyInfo(const Stmt *From, const Stmt *To,
|
|
|
|
ConsumedState NS) {
|
|
|
|
InfoEntry Entry = PropagationMap.find(From);
|
|
|
|
if (Entry != PropagationMap.end()) {
|
|
|
|
PropagationInfo& PInfo = Entry->second;
|
|
|
|
ConsumedState CS = PInfo.getAsState(StateMap);
|
|
|
|
if (CS != CS_None)
|
|
|
|
PropagationMap.insert(PairType(To, CS));
|
|
|
|
if (NS != CS_None && PInfo.isPointerToValue())
|
|
|
|
setStateForVarOrTmp(StateMap, PInfo, NS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get the ConsumedState for From
|
|
|
|
ConsumedState ConsumedStmtVisitor::getInfo(const Stmt *From) {
|
|
|
|
InfoEntry Entry = PropagationMap.find(From);
|
|
|
|
if (Entry != PropagationMap.end()) {
|
|
|
|
PropagationInfo& PInfo = Entry->second;
|
|
|
|
return PInfo.getAsState(StateMap);
|
|
|
|
}
|
|
|
|
return CS_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we already have info for To then update it, otherwise create a new entry.
|
|
|
|
void ConsumedStmtVisitor::setInfo(const Stmt *To, ConsumedState NS) {
|
|
|
|
InfoEntry Entry = PropagationMap.find(To);
|
|
|
|
if (Entry != PropagationMap.end()) {
|
|
|
|
PropagationInfo& PInfo = Entry->second;
|
|
|
|
if (PInfo.isPointerToValue())
|
|
|
|
setStateForVarOrTmp(StateMap, PInfo, NS);
|
|
|
|
} else if (NS != CS_None) {
|
|
|
|
PropagationMap.insert(PairType(To, PropagationInfo(NS)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-27 04:34:59 +08:00
|
|
|
void ConsumedStmtVisitor::checkCallability(const PropagationInfo &PInfo,
|
2013-08-23 04:44:47 +08:00
|
|
|
const FunctionDecl *FunDecl,
|
2013-10-12 05:55:33 +08:00
|
|
|
SourceLocation BlameLoc) {
|
2013-11-16 08:22:43 +08:00
|
|
|
assert(!PInfo.isTest());
|
2014-01-14 08:36:53 +08:00
|
|
|
|
2013-10-05 05:28:06 +08:00
|
|
|
const CallableWhenAttr *CWAttr = FunDecl->getAttr<CallableWhenAttr>();
|
2013-12-19 10:58:51 +08:00
|
|
|
if (!CWAttr)
|
|
|
|
return;
|
2014-01-14 08:36:53 +08:00
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
if (PInfo.isVar()) {
|
2013-11-16 08:22:43 +08:00
|
|
|
ConsumedState VarState = StateMap->getState(PInfo.getVar());
|
2014-01-14 08:36:53 +08:00
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
if (VarState == CS_None || isCallableInState(CWAttr, VarState))
|
2013-10-05 05:28:06 +08:00
|
|
|
return;
|
2014-01-14 08:36:53 +08:00
|
|
|
|
2013-10-05 05:28:06 +08:00
|
|
|
Analyzer.WarningsHandler.warnUseInInvalidState(
|
2013-11-16 08:22:43 +08:00
|
|
|
FunDecl->getNameAsString(), PInfo.getVar()->getNameAsString(),
|
2013-10-12 05:55:33 +08:00
|
|
|
stateToString(VarState), BlameLoc);
|
2014-01-14 08:36:53 +08:00
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
} else {
|
|
|
|
ConsumedState TmpState = PInfo.getAsState(StateMap);
|
2014-01-14 08:36:53 +08:00
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
if (TmpState == CS_None || isCallableInState(CWAttr, TmpState))
|
2013-10-05 05:28:06 +08:00
|
|
|
return;
|
2014-01-14 08:36:53 +08:00
|
|
|
|
2013-10-05 05:28:06 +08:00
|
|
|
Analyzer.WarningsHandler.warnUseOfTempInInvalidState(
|
2013-11-16 08:22:43 +08:00
|
|
|
FunDecl->getNameAsString(), stateToString(TmpState), BlameLoc);
|
2013-08-23 04:44:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
|
2014-01-14 08:36:53 +08:00
|
|
|
// Factors out common behavior for function, method, and operator calls.
|
|
|
|
// Check parameters and set parameter state if necessary.
|
|
|
|
// Returns true if the state of ObjArg is set, or false otherwise.
|
|
|
|
bool ConsumedStmtVisitor::handleCall(const CallExpr *Call, const Expr *ObjArg,
|
|
|
|
const FunctionDecl *FunD) {
|
|
|
|
unsigned Offset = 0;
|
2014-01-17 07:07:16 +08:00
|
|
|
if (isa<CXXOperatorCallExpr>(Call) && isa<CXXMethodDecl>(FunD))
|
|
|
|
Offset = 1; // first argument is 'this'
|
2014-01-14 08:36:53 +08:00
|
|
|
|
|
|
|
// check explicit parameters
|
|
|
|
for (unsigned Index = Offset; Index < Call->getNumArgs(); ++Index) {
|
|
|
|
// Skip variable argument lists.
|
|
|
|
if (Index - Offset >= FunD->getNumParams())
|
|
|
|
break;
|
|
|
|
|
|
|
|
const ParmVarDecl *Param = FunD->getParamDecl(Index - Offset);
|
|
|
|
QualType ParamType = Param->getType();
|
|
|
|
|
|
|
|
InfoEntry Entry = PropagationMap.find(Call->getArg(Index));
|
|
|
|
|
|
|
|
if (Entry == PropagationMap.end() || Entry->second.isTest())
|
|
|
|
continue;
|
|
|
|
PropagationInfo PInfo = Entry->second;
|
|
|
|
|
|
|
|
// Check that the parameter is in the correct state.
|
|
|
|
if (ParamTypestateAttr *PTA = Param->getAttr<ParamTypestateAttr>()) {
|
|
|
|
ConsumedState ParamState = PInfo.getAsState(StateMap);
|
|
|
|
ConsumedState ExpectedState = mapParamTypestateAttrState(PTA);
|
|
|
|
|
|
|
|
if (ParamState != ExpectedState)
|
|
|
|
Analyzer.WarningsHandler.warnParamTypestateMismatch(
|
|
|
|
Call->getArg(Index)->getExprLoc(),
|
|
|
|
stateToString(ExpectedState), stateToString(ParamState));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(Entry->second.isVar() || Entry->second.isTmp()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Adjust state on the caller side.
|
2014-01-17 07:07:16 +08:00
|
|
|
if (isRValueRef(ParamType))
|
2014-01-14 08:36:53 +08:00
|
|
|
setStateForVarOrTmp(StateMap, PInfo, consumed::CS_Consumed);
|
|
|
|
else if (ReturnTypestateAttr *RT = Param->getAttr<ReturnTypestateAttr>())
|
|
|
|
setStateForVarOrTmp(StateMap, PInfo, mapReturnTypestateAttrState(RT));
|
2014-01-17 07:07:16 +08:00
|
|
|
else if (isPointerOrRef(ParamType) &&
|
|
|
|
(!ParamType->getPointeeType().isConstQualified() ||
|
|
|
|
isSetOnReadPtrType(ParamType)))
|
|
|
|
setStateForVarOrTmp(StateMap, PInfo, consumed::CS_Unknown);
|
2014-01-14 08:36:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!ObjArg)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// check implicit 'self' parameter, if present
|
|
|
|
InfoEntry Entry = PropagationMap.find(ObjArg);
|
|
|
|
if (Entry != PropagationMap.end()) {
|
|
|
|
PropagationInfo PInfo = Entry->second;
|
|
|
|
checkCallability(PInfo, FunD, Call->getExprLoc());
|
|
|
|
|
|
|
|
if (SetTypestateAttr *STA = FunD->getAttr<SetTypestateAttr>()) {
|
|
|
|
if (PInfo.isVar()) {
|
|
|
|
StateMap->setState(PInfo.getVar(), mapSetTypestateAttrState(STA));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (PInfo.isTmp()) {
|
|
|
|
StateMap->setState(PInfo.getTmp(), mapSetTypestateAttrState(STA));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (isTestingFunction(FunD) && PInfo.isVar()) {
|
|
|
|
PropagationMap.insert(PairType(Call,
|
|
|
|
PropagationInfo(PInfo.getVar(), testsFor(FunD))));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
|
2014-01-14 08:36:53 +08:00
|
|
|
|
2013-09-04 04:11:38 +08:00
|
|
|
void ConsumedStmtVisitor::propagateReturnType(const Stmt *Call,
|
2014-01-14 08:36:53 +08:00
|
|
|
const FunctionDecl *Fun) {
|
|
|
|
QualType RetType = Fun->getCallResultType();
|
|
|
|
if (RetType->isReferenceType())
|
|
|
|
RetType = RetType->getPointeeType();
|
|
|
|
|
|
|
|
if (isConsumableType(RetType)) {
|
2013-09-04 04:11:38 +08:00
|
|
|
ConsumedState ReturnState;
|
2013-12-19 10:58:51 +08:00
|
|
|
if (ReturnTypestateAttr *RTA = Fun->getAttr<ReturnTypestateAttr>())
|
|
|
|
ReturnState = mapReturnTypestateAttrState(RTA);
|
2013-09-04 04:11:38 +08:00
|
|
|
else
|
2014-01-14 08:36:53 +08:00
|
|
|
ReturnState = mapConsumableAttrState(RetType);
|
2013-09-04 04:11:38 +08:00
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
PropagationMap.insert(PairType(Call, PropagationInfo(ReturnState)));
|
2013-08-30 01:26:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-14 08:36:53 +08:00
|
|
|
|
2013-08-13 05:20:55 +08:00
|
|
|
void ConsumedStmtVisitor::VisitBinaryOperator(const BinaryOperator *BinOp) {
|
|
|
|
switch (BinOp->getOpcode()) {
|
2013-08-30 01:26:57 +08:00
|
|
|
case BO_LAnd:
|
|
|
|
case BO_LOr : {
|
|
|
|
InfoEntry LEntry = PropagationMap.find(BinOp->getLHS()),
|
|
|
|
REntry = PropagationMap.find(BinOp->getRHS());
|
|
|
|
|
|
|
|
VarTestResult LTest, RTest;
|
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
if (LEntry != PropagationMap.end() && LEntry->second.isVarTest()) {
|
|
|
|
LTest = LEntry->second.getVarTest();
|
2013-08-30 01:26:57 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
LTest.Var = NULL;
|
|
|
|
LTest.TestsFor = CS_None;
|
|
|
|
}
|
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
if (REntry != PropagationMap.end() && REntry->second.isVarTest()) {
|
|
|
|
RTest = REntry->second.getVarTest();
|
2013-08-30 01:26:57 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
RTest.Var = NULL;
|
|
|
|
RTest.TestsFor = CS_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(LTest.Var == NULL && RTest.Var == NULL))
|
|
|
|
PropagationMap.insert(PairType(BinOp, PropagationInfo(BinOp,
|
|
|
|
static_cast<EffectiveOp>(BinOp->getOpcode() == BO_LOr), LTest, RTest)));
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-08-13 05:20:55 +08:00
|
|
|
case BO_PtrMemD:
|
|
|
|
case BO_PtrMemI:
|
|
|
|
forwardInfo(BinOp->getLHS(), BinOp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-17 08:40:40 +08:00
|
|
|
static bool isStdNamespace(const DeclContext *DC) {
|
|
|
|
if (!DC->isNamespace()) return false;
|
|
|
|
while (DC->getParent()->isNamespace())
|
|
|
|
DC = DC->getParent();
|
|
|
|
const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
|
|
|
|
|
|
|
|
return ND && ND->getName() == "std" &&
|
|
|
|
ND->getDeclContext()->isTranslationUnit();
|
|
|
|
}
|
|
|
|
|
2013-08-13 05:20:55 +08:00
|
|
|
void ConsumedStmtVisitor::VisitCallExpr(const CallExpr *Call) {
|
2014-01-14 08:36:53 +08:00
|
|
|
const FunctionDecl *FunDecl = Call->getDirectCallee();
|
|
|
|
if (!FunDecl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Special case for the std::move function.
|
|
|
|
// TODO: Make this more specific. (Deferred)
|
|
|
|
if (Call->getNumArgs() == 1 &&
|
|
|
|
FunDecl->getNameAsString() == "move" &&
|
|
|
|
isStdNamespace(FunDecl->getDeclContext())) {
|
|
|
|
copyInfo(Call->getArg(0), Call, CS_Consumed);
|
|
|
|
return;
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
2014-01-14 08:36:53 +08:00
|
|
|
|
|
|
|
handleCall(Call, 0, FunDecl);
|
|
|
|
propagateReturnType(Call, FunDecl);
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConsumedStmtVisitor::VisitCastExpr(const CastExpr *Cast) {
|
2013-08-30 01:26:57 +08:00
|
|
|
forwardInfo(Cast->getSubExpr(), Cast);
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
|
2013-10-12 05:55:33 +08:00
|
|
|
void ConsumedStmtVisitor::VisitCXXBindTemporaryExpr(
|
|
|
|
const CXXBindTemporaryExpr *Temp) {
|
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
InfoEntry Entry = PropagationMap.find(Temp->getSubExpr());
|
|
|
|
|
|
|
|
if (Entry != PropagationMap.end() && !Entry->second.isTest()) {
|
|
|
|
StateMap->setState(Temp, Entry->second.getAsState(StateMap));
|
|
|
|
PropagationMap.insert(PairType(Temp, PropagationInfo(Temp)));
|
|
|
|
}
|
2013-10-12 05:55:33 +08:00
|
|
|
}
|
|
|
|
|
2013-08-13 05:20:55 +08:00
|
|
|
void ConsumedStmtVisitor::VisitCXXConstructExpr(const CXXConstructExpr *Call) {
|
|
|
|
CXXConstructorDecl *Constructor = Call->getConstructor();
|
2013-08-13 07:49:39 +08:00
|
|
|
|
|
|
|
ASTContext &CurrContext = AC.getASTContext();
|
2013-08-13 05:20:55 +08:00
|
|
|
QualType ThisType = Constructor->getThisType(CurrContext)->getPointeeType();
|
|
|
|
|
2013-10-19 02:36:21 +08:00
|
|
|
if (!isConsumableType(ThisType))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// FIXME: What should happen if someone annotates the move constructor?
|
2013-12-19 10:58:51 +08:00
|
|
|
if (ReturnTypestateAttr *RTA = Constructor->getAttr<ReturnTypestateAttr>()) {
|
|
|
|
// TODO: Adjust state of args appropriately.
|
|
|
|
ConsumedState RetState = mapReturnTypestateAttrState(RTA);
|
2014-01-14 08:36:53 +08:00
|
|
|
PropagationMap.insert(PairType(Call, PropagationInfo(RetState)));
|
|
|
|
} else if (Constructor->isDefaultConstructor()) {
|
2013-10-19 02:36:21 +08:00
|
|
|
PropagationMap.insert(PairType(Call,
|
2013-11-16 08:22:43 +08:00
|
|
|
PropagationInfo(consumed::CS_Consumed)));
|
2014-01-14 08:36:53 +08:00
|
|
|
} else if (Constructor->isMoveConstructor()) {
|
|
|
|
copyInfo(Call->getArg(0), Call, CS_Consumed);
|
2013-10-19 02:36:21 +08:00
|
|
|
} else if (Constructor->isCopyConstructor()) {
|
2014-01-14 08:36:53 +08:00
|
|
|
// Copy state from arg. If setStateOnRead then set arg to CS_Unknown.
|
|
|
|
ConsumedState NS =
|
|
|
|
isSetOnReadPtrType(Constructor->getThisType(CurrContext)) ?
|
|
|
|
CS_Unknown : CS_None;
|
|
|
|
copyInfo(Call->getArg(0), Call, NS);
|
2013-10-19 02:36:21 +08:00
|
|
|
} else {
|
2013-11-16 08:22:43 +08:00
|
|
|
// TODO: Adjust state of args appropriately.
|
2013-10-19 02:36:21 +08:00
|
|
|
ConsumedState RetState = mapConsumableAttrState(ThisType);
|
2013-11-16 08:22:43 +08:00
|
|
|
PropagationMap.insert(PairType(Call, PropagationInfo(RetState)));
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-14 08:36:53 +08:00
|
|
|
|
2013-08-13 05:20:55 +08:00
|
|
|
void ConsumedStmtVisitor::VisitCXXMemberCallExpr(
|
2014-01-14 08:36:53 +08:00
|
|
|
const CXXMemberCallExpr *Call) {
|
|
|
|
CXXMethodDecl* MD = Call->getMethodDecl();
|
|
|
|
if (!MD)
|
|
|
|
return;
|
|
|
|
|
|
|
|
handleCall(Call, Call->getImplicitObjectArgument(), MD);
|
|
|
|
propagateReturnType(Call, MD);
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
|
2014-01-14 08:36:53 +08:00
|
|
|
|
2013-08-13 05:20:55 +08:00
|
|
|
void ConsumedStmtVisitor::VisitCXXOperatorCallExpr(
|
2014-01-14 08:36:53 +08:00
|
|
|
const CXXOperatorCallExpr *Call) {
|
|
|
|
|
2013-08-13 05:20:55 +08:00
|
|
|
const FunctionDecl *FunDecl =
|
|
|
|
dyn_cast_or_null<FunctionDecl>(Call->getDirectCallee());
|
|
|
|
if (!FunDecl) return;
|
2014-01-14 08:36:53 +08:00
|
|
|
|
|
|
|
if (Call->getOperator() == OO_Equal) {
|
|
|
|
ConsumedState CS = getInfo(Call->getArg(1));
|
|
|
|
if (!handleCall(Call, Call->getArg(0), FunDecl))
|
|
|
|
setInfo(Call->getArg(0), CS);
|
|
|
|
return;
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
2014-01-14 08:36:53 +08:00
|
|
|
|
|
|
|
if (const CXXMemberCallExpr *MCall = dyn_cast<CXXMemberCallExpr>(Call))
|
|
|
|
handleCall(MCall, MCall->getImplicitObjectArgument(), FunDecl);
|
|
|
|
else
|
|
|
|
handleCall(Call, Call->getArg(0), FunDecl);
|
|
|
|
|
|
|
|
propagateReturnType(Call, FunDecl);
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConsumedStmtVisitor::VisitDeclRefExpr(const DeclRefExpr *DeclRef) {
|
|
|
|
if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(DeclRef->getDecl()))
|
|
|
|
if (StateMap->getState(Var) != consumed::CS_None)
|
|
|
|
PropagationMap.insert(PairType(DeclRef, PropagationInfo(Var)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsumedStmtVisitor::VisitDeclStmt(const DeclStmt *DeclS) {
|
2014-03-15 01:01:24 +08:00
|
|
|
for (const auto *DI : DeclS->decls())
|
|
|
|
if (isa<VarDecl>(DI))
|
|
|
|
VisitVarDecl(cast<VarDecl>(DI));
|
2013-08-13 05:20:55 +08:00
|
|
|
|
|
|
|
if (DeclS->isSingleDecl())
|
|
|
|
if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(DeclS->getSingleDecl()))
|
|
|
|
PropagationMap.insert(PairType(DeclS, PropagationInfo(Var)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsumedStmtVisitor::VisitMaterializeTemporaryExpr(
|
|
|
|
const MaterializeTemporaryExpr *Temp) {
|
|
|
|
|
2013-10-24 22:28:17 +08:00
|
|
|
forwardInfo(Temp->GetTemporaryExpr(), Temp);
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConsumedStmtVisitor::VisitMemberExpr(const MemberExpr *MExpr) {
|
|
|
|
forwardInfo(MExpr->getBase(), MExpr);
|
|
|
|
}
|
|
|
|
|
2013-08-30 06:36:05 +08:00
|
|
|
|
|
|
|
void ConsumedStmtVisitor::VisitParmVarDecl(const ParmVarDecl *Param) {
|
2013-09-06 09:28:43 +08:00
|
|
|
QualType ParamType = Param->getType();
|
|
|
|
ConsumedState ParamState = consumed::CS_None;
|
2013-10-18 07:23:53 +08:00
|
|
|
|
2013-12-19 10:58:51 +08:00
|
|
|
if (const ParamTypestateAttr *PTA = Param->getAttr<ParamTypestateAttr>())
|
|
|
|
ParamState = mapParamTypestateAttrState(PTA);
|
|
|
|
else if (isConsumableType(ParamType))
|
|
|
|
ParamState = mapConsumableAttrState(ParamType);
|
2014-01-17 07:07:16 +08:00
|
|
|
else if (isRValueRef(ParamType) &&
|
|
|
|
isConsumableType(ParamType->getPointeeType()))
|
2013-12-19 10:58:51 +08:00
|
|
|
ParamState = mapConsumableAttrState(ParamType->getPointeeType());
|
|
|
|
else if (ParamType->isReferenceType() &&
|
2014-01-17 07:07:16 +08:00
|
|
|
isConsumableType(ParamType->getPointeeType()))
|
2013-09-06 09:28:43 +08:00
|
|
|
ParamState = consumed::CS_Unknown;
|
2013-10-18 07:23:53 +08:00
|
|
|
|
|
|
|
if (ParamState != CS_None)
|
2013-09-06 09:28:43 +08:00
|
|
|
StateMap->setState(Param, ParamState);
|
2013-08-30 06:36:05 +08:00
|
|
|
}
|
|
|
|
|
2013-09-04 04:11:38 +08:00
|
|
|
void ConsumedStmtVisitor::VisitReturnStmt(const ReturnStmt *Ret) {
|
2013-10-18 06:53:04 +08:00
|
|
|
ConsumedState ExpectedState = Analyzer.getExpectedReturnState();
|
|
|
|
|
|
|
|
if (ExpectedState != CS_None) {
|
2013-09-04 04:11:38 +08:00
|
|
|
InfoEntry Entry = PropagationMap.find(Ret->getRetValue());
|
|
|
|
|
|
|
|
if (Entry != PropagationMap.end()) {
|
2013-11-16 08:22:43 +08:00
|
|
|
ConsumedState RetState = Entry->second.getAsState(StateMap);
|
2013-09-04 04:11:38 +08:00
|
|
|
|
|
|
|
if (RetState != ExpectedState)
|
|
|
|
Analyzer.WarningsHandler.warnReturnTypestateMismatch(
|
|
|
|
Ret->getReturnLoc(), stateToString(ExpectedState),
|
|
|
|
stateToString(RetState));
|
|
|
|
}
|
|
|
|
}
|
2013-10-18 06:53:04 +08:00
|
|
|
|
|
|
|
StateMap->checkParamsForReturnTypestate(Ret->getLocStart(),
|
|
|
|
Analyzer.WarningsHandler);
|
2013-09-04 04:11:38 +08:00
|
|
|
}
|
|
|
|
|
2013-08-13 05:20:55 +08:00
|
|
|
void ConsumedStmtVisitor::VisitUnaryOperator(const UnaryOperator *UOp) {
|
2013-08-30 01:26:57 +08:00
|
|
|
InfoEntry Entry = PropagationMap.find(UOp->getSubExpr()->IgnoreParens());
|
|
|
|
if (Entry == PropagationMap.end()) return;
|
|
|
|
|
|
|
|
switch (UOp->getOpcode()) {
|
|
|
|
case UO_AddrOf:
|
|
|
|
PropagationMap.insert(PairType(UOp, Entry->second));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UO_LNot:
|
2013-11-16 08:22:43 +08:00
|
|
|
if (Entry->second.isTest())
|
2013-08-30 01:26:57 +08:00
|
|
|
PropagationMap.insert(PairType(UOp, Entry->second.invertTest()));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-05 05:28:06 +08:00
|
|
|
// TODO: See if I need to check for reference types here.
|
2013-08-13 05:20:55 +08:00
|
|
|
void ConsumedStmtVisitor::VisitVarDecl(const VarDecl *Var) {
|
2013-08-31 06:56:34 +08:00
|
|
|
if (isConsumableType(Var->getType())) {
|
2013-08-30 06:36:05 +08:00
|
|
|
if (Var->hasInit()) {
|
2013-11-16 08:22:43 +08:00
|
|
|
MapType::iterator VIT = PropagationMap.find(
|
|
|
|
Var->getInit()->IgnoreImplicit());
|
2013-10-19 07:11:49 +08:00
|
|
|
if (VIT != PropagationMap.end()) {
|
|
|
|
PropagationInfo PInfo = VIT->second;
|
2013-11-16 08:22:43 +08:00
|
|
|
ConsumedState St = PInfo.getAsState(StateMap);
|
|
|
|
|
2013-10-19 07:11:49 +08:00
|
|
|
if (St != consumed::CS_None) {
|
|
|
|
StateMap->setState(Var, St);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-08-30 06:36:05 +08:00
|
|
|
}
|
2013-10-19 07:11:49 +08:00
|
|
|
// Otherwise
|
|
|
|
StateMap->setState(Var, consumed::CS_Unknown);
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-30 01:26:57 +08:00
|
|
|
}} // end clang::consumed::ConsumedStmtVisitor
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace consumed {
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
void splitVarStateForIf(const IfStmt * IfNode, const VarTestResult &Test,
|
|
|
|
ConsumedStateMap *ThenStates,
|
|
|
|
ConsumedStateMap *ElseStates) {
|
|
|
|
|
|
|
|
ConsumedState VarState = ThenStates->getState(Test.Var);
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
if (VarState == CS_Unknown) {
|
|
|
|
ThenStates->setState(Test.Var, Test.TestsFor);
|
2013-10-05 05:28:06 +08:00
|
|
|
ElseStates->setState(Test.Var, invertConsumedUnconsumed(Test.TestsFor));
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
} else if (VarState == invertConsumedUnconsumed(Test.TestsFor)) {
|
|
|
|
ThenStates->markUnreachable();
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-10-05 05:28:06 +08:00
|
|
|
} else if (VarState == Test.TestsFor) {
|
2013-08-30 01:26:57 +08:00
|
|
|
ElseStates->markUnreachable();
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
void splitVarStateForIfBinOp(const PropagationInfo &PInfo,
|
|
|
|
ConsumedStateMap *ThenStates, ConsumedStateMap *ElseStates) {
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
const VarTestResult <est = PInfo.getLTest(),
|
|
|
|
&RTest = PInfo.getRTest();
|
|
|
|
|
|
|
|
ConsumedState LState = LTest.Var ? ThenStates->getState(LTest.Var) : CS_None,
|
|
|
|
RState = RTest.Var ? ThenStates->getState(RTest.Var) : CS_None;
|
|
|
|
|
|
|
|
if (LTest.Var) {
|
|
|
|
if (PInfo.testEffectiveOp() == EO_And) {
|
|
|
|
if (LState == CS_Unknown) {
|
|
|
|
ThenStates->setState(LTest.Var, LTest.TestsFor);
|
|
|
|
|
|
|
|
} else if (LState == invertConsumedUnconsumed(LTest.TestsFor)) {
|
|
|
|
ThenStates->markUnreachable();
|
|
|
|
|
|
|
|
} else if (LState == LTest.TestsFor && isKnownState(RState)) {
|
2013-10-05 05:28:06 +08:00
|
|
|
if (RState == RTest.TestsFor)
|
|
|
|
ElseStates->markUnreachable();
|
|
|
|
else
|
2013-08-30 01:26:57 +08:00
|
|
|
ThenStates->markUnreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2013-10-05 05:28:06 +08:00
|
|
|
if (LState == CS_Unknown) {
|
2013-08-30 01:26:57 +08:00
|
|
|
ElseStates->setState(LTest.Var,
|
|
|
|
invertConsumedUnconsumed(LTest.TestsFor));
|
|
|
|
|
2013-10-05 05:28:06 +08:00
|
|
|
} else if (LState == LTest.TestsFor) {
|
2013-08-30 01:26:57 +08:00
|
|
|
ElseStates->markUnreachable();
|
|
|
|
|
|
|
|
} else if (LState == invertConsumedUnconsumed(LTest.TestsFor) &&
|
|
|
|
isKnownState(RState)) {
|
|
|
|
|
2013-10-05 05:28:06 +08:00
|
|
|
if (RState == RTest.TestsFor)
|
|
|
|
ElseStates->markUnreachable();
|
|
|
|
else
|
2013-08-30 01:26:57 +08:00
|
|
|
ThenStates->markUnreachable();
|
|
|
|
}
|
|
|
|
}
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
if (RTest.Var) {
|
|
|
|
if (PInfo.testEffectiveOp() == EO_And) {
|
|
|
|
if (RState == CS_Unknown)
|
|
|
|
ThenStates->setState(RTest.Var, RTest.TestsFor);
|
|
|
|
else if (RState == invertConsumedUnconsumed(RTest.TestsFor))
|
|
|
|
ThenStates->markUnreachable();
|
|
|
|
|
2013-10-05 05:28:06 +08:00
|
|
|
} else {
|
2013-08-30 01:26:57 +08:00
|
|
|
if (RState == CS_Unknown)
|
|
|
|
ElseStates->setState(RTest.Var,
|
|
|
|
invertConsumedUnconsumed(RTest.TestsFor));
|
|
|
|
else if (RState == RTest.TestsFor)
|
|
|
|
ElseStates->markUnreachable();
|
|
|
|
}
|
|
|
|
}
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
|
2013-10-10 02:30:24 +08:00
|
|
|
bool ConsumedBlockInfo::allBackEdgesVisited(const CFGBlock *CurrBlock,
|
|
|
|
const CFGBlock *TargetBlock) {
|
|
|
|
|
|
|
|
assert(CurrBlock && "Block pointer must not be NULL");
|
|
|
|
assert(TargetBlock && "TargetBlock pointer must not be NULL");
|
|
|
|
|
|
|
|
unsigned int CurrBlockOrder = VisitOrder[CurrBlock->getBlockID()];
|
|
|
|
for (CFGBlock::const_pred_iterator PI = TargetBlock->pred_begin(),
|
|
|
|
PE = TargetBlock->pred_end(); PI != PE; ++PI) {
|
|
|
|
if (*PI && CurrBlockOrder < VisitOrder[(*PI)->getBlockID()] )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-13 05:20:55 +08:00
|
|
|
void ConsumedBlockInfo::addInfo(const CFGBlock *Block,
|
|
|
|
ConsumedStateMap *StateMap,
|
|
|
|
bool &AlreadyOwned) {
|
|
|
|
|
2013-10-10 02:30:24 +08:00
|
|
|
assert(Block && "Block pointer must not be NULL");
|
2013-08-13 05:20:55 +08:00
|
|
|
|
|
|
|
ConsumedStateMap *Entry = StateMapsArray[Block->getBlockID()];
|
|
|
|
|
|
|
|
if (Entry) {
|
|
|
|
Entry->intersect(StateMap);
|
|
|
|
|
|
|
|
} else if (AlreadyOwned) {
|
|
|
|
StateMapsArray[Block->getBlockID()] = new ConsumedStateMap(*StateMap);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
StateMapsArray[Block->getBlockID()] = StateMap;
|
|
|
|
AlreadyOwned = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsumedBlockInfo::addInfo(const CFGBlock *Block,
|
|
|
|
ConsumedStateMap *StateMap) {
|
|
|
|
|
2013-10-10 02:30:24 +08:00
|
|
|
assert(Block != NULL && "Block pointer must not be NULL");
|
2013-08-13 05:20:55 +08:00
|
|
|
|
|
|
|
ConsumedStateMap *Entry = StateMapsArray[Block->getBlockID()];
|
|
|
|
|
|
|
|
if (Entry) {
|
|
|
|
Entry->intersect(StateMap);
|
|
|
|
delete StateMap;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
StateMapsArray[Block->getBlockID()] = StateMap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:30:24 +08:00
|
|
|
ConsumedStateMap* ConsumedBlockInfo::borrowInfo(const CFGBlock *Block) {
|
|
|
|
assert(Block && "Block pointer must not be NULL");
|
|
|
|
assert(StateMapsArray[Block->getBlockID()] && "Block has no block info");
|
|
|
|
|
2013-08-13 05:20:55 +08:00
|
|
|
return StateMapsArray[Block->getBlockID()];
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:30:24 +08:00
|
|
|
void ConsumedBlockInfo::discardInfo(const CFGBlock *Block) {
|
|
|
|
unsigned int BlockID = Block->getBlockID();
|
|
|
|
delete StateMapsArray[BlockID];
|
|
|
|
StateMapsArray[BlockID] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConsumedStateMap* ConsumedBlockInfo::getInfo(const CFGBlock *Block) {
|
|
|
|
assert(Block && "Block pointer must not be NULL");
|
|
|
|
|
|
|
|
ConsumedStateMap *StateMap = StateMapsArray[Block->getBlockID()];
|
|
|
|
if (isBackEdgeTarget(Block)) {
|
|
|
|
return new ConsumedStateMap(*StateMap);
|
|
|
|
} else {
|
|
|
|
StateMapsArray[Block->getBlockID()] = NULL;
|
|
|
|
return StateMap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConsumedBlockInfo::isBackEdge(const CFGBlock *From, const CFGBlock *To) {
|
|
|
|
assert(From && "From block must not be NULL");
|
|
|
|
assert(To && "From block must not be NULL");
|
|
|
|
|
|
|
|
return VisitOrder[From->getBlockID()] > VisitOrder[To->getBlockID()];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConsumedBlockInfo::isBackEdgeTarget(const CFGBlock *Block) {
|
|
|
|
assert(Block != NULL && "Block pointer must not be NULL");
|
|
|
|
|
|
|
|
// Anything with less than two predecessors can't be the target of a back
|
|
|
|
// edge.
|
|
|
|
if (Block->pred_size() < 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned int BlockVisitOrder = VisitOrder[Block->getBlockID()];
|
|
|
|
for (CFGBlock::const_pred_iterator PI = Block->pred_begin(),
|
|
|
|
PE = Block->pred_end(); PI != PE; ++PI) {
|
|
|
|
if (*PI && BlockVisitOrder < VisitOrder[(*PI)->getBlockID()])
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
|
2013-10-18 06:53:04 +08:00
|
|
|
void ConsumedStateMap::checkParamsForReturnTypestate(SourceLocation BlameLoc,
|
|
|
|
ConsumedWarningsHandlerBase &WarningsHandler) const {
|
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
for (VarMapType::const_iterator DMI = VarMap.begin(), DME = VarMap.end();
|
|
|
|
DMI != DME; ++DMI) {
|
2013-10-18 06:53:04 +08:00
|
|
|
|
|
|
|
if (isa<ParmVarDecl>(DMI->first)) {
|
|
|
|
const ParmVarDecl *Param = cast<ParmVarDecl>(DMI->first);
|
2013-12-19 10:58:51 +08:00
|
|
|
const ReturnTypestateAttr *RTA = Param->getAttr<ReturnTypestateAttr>();
|
2013-10-18 06:53:04 +08:00
|
|
|
|
2013-12-19 10:58:51 +08:00
|
|
|
if (!RTA)
|
|
|
|
continue;
|
2013-10-18 06:53:04 +08:00
|
|
|
|
2013-12-19 10:58:51 +08:00
|
|
|
ConsumedState ExpectedState = mapReturnTypestateAttrState(RTA);
|
|
|
|
if (DMI->second != ExpectedState)
|
2013-10-18 06:53:04 +08:00
|
|
|
WarningsHandler.warnParamReturnTypestateMismatch(BlameLoc,
|
|
|
|
Param->getNameAsString(), stateToString(ExpectedState),
|
|
|
|
stateToString(DMI->second));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
void ConsumedStateMap::clearTemporaries() {
|
|
|
|
TmpMap.clear();
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:30:24 +08:00
|
|
|
ConsumedState ConsumedStateMap::getState(const VarDecl *Var) const {
|
2013-11-16 08:22:43 +08:00
|
|
|
VarMapType::const_iterator Entry = VarMap.find(Var);
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
if (Entry != VarMap.end())
|
2013-08-13 05:20:55 +08:00
|
|
|
return Entry->second;
|
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
return CS_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConsumedState
|
|
|
|
ConsumedStateMap::getState(const CXXBindTemporaryExpr *Tmp) const {
|
|
|
|
TmpMapType::const_iterator Entry = TmpMap.find(Tmp);
|
|
|
|
|
|
|
|
if (Entry != TmpMap.end())
|
|
|
|
return Entry->second;
|
|
|
|
|
|
|
|
return CS_None;
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConsumedStateMap::intersect(const ConsumedStateMap *Other) {
|
|
|
|
ConsumedState LocalState;
|
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
if (this->From && this->From == Other->From && !Other->Reachable) {
|
|
|
|
this->markUnreachable();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
for (VarMapType::const_iterator DMI = Other->VarMap.begin(),
|
|
|
|
DME = Other->VarMap.end(); DMI != DME; ++DMI) {
|
2013-08-13 05:20:55 +08:00
|
|
|
|
|
|
|
LocalState = this->getState(DMI->first);
|
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
if (LocalState == CS_None)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (LocalState != DMI->second)
|
2013-11-16 08:22:43 +08:00
|
|
|
VarMap[DMI->first] = CS_Unknown;
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:30:24 +08:00
|
|
|
void ConsumedStateMap::intersectAtLoopHead(const CFGBlock *LoopHead,
|
|
|
|
const CFGBlock *LoopBack, const ConsumedStateMap *LoopBackStates,
|
|
|
|
ConsumedWarningsHandlerBase &WarningsHandler) {
|
|
|
|
|
|
|
|
ConsumedState LocalState;
|
2013-10-12 05:55:33 +08:00
|
|
|
SourceLocation BlameLoc = getLastStmtLoc(LoopBack);
|
2013-10-10 02:30:24 +08:00
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
for (VarMapType::const_iterator DMI = LoopBackStates->VarMap.begin(),
|
|
|
|
DME = LoopBackStates->VarMap.end(); DMI != DME; ++DMI) {
|
2013-10-10 02:30:24 +08:00
|
|
|
|
|
|
|
LocalState = this->getState(DMI->first);
|
|
|
|
|
|
|
|
if (LocalState == CS_None)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (LocalState != DMI->second) {
|
2013-11-16 08:22:43 +08:00
|
|
|
VarMap[DMI->first] = CS_Unknown;
|
2013-10-10 02:30:24 +08:00
|
|
|
WarningsHandler.warnLoopStateMismatch(
|
|
|
|
BlameLoc, DMI->first->getNameAsString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
void ConsumedStateMap::markUnreachable() {
|
|
|
|
this->Reachable = false;
|
2013-11-16 08:22:43 +08:00
|
|
|
VarMap.clear();
|
|
|
|
TmpMap.clear();
|
2013-08-30 01:26:57 +08:00
|
|
|
}
|
|
|
|
|
2013-08-13 05:20:55 +08:00
|
|
|
void ConsumedStateMap::setState(const VarDecl *Var, ConsumedState State) {
|
2013-11-16 08:22:43 +08:00
|
|
|
VarMap[Var] = State;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsumedStateMap::setState(const CXXBindTemporaryExpr *Tmp,
|
|
|
|
ConsumedState State) {
|
|
|
|
TmpMap[Tmp] = State;
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
|
2013-08-23 04:44:47 +08:00
|
|
|
void ConsumedStateMap::remove(const VarDecl *Var) {
|
2013-11-16 08:22:43 +08:00
|
|
|
VarMap.erase(Var);
|
2013-08-23 04:44:47 +08:00
|
|
|
}
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-10-10 02:30:24 +08:00
|
|
|
bool ConsumedStateMap::operator!=(const ConsumedStateMap *Other) const {
|
2013-11-16 08:22:43 +08:00
|
|
|
for (VarMapType::const_iterator DMI = Other->VarMap.begin(),
|
|
|
|
DME = Other->VarMap.end(); DMI != DME; ++DMI) {
|
2013-10-10 02:30:24 +08:00
|
|
|
|
|
|
|
if (this->getState(DMI->first) != DMI->second)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-06 09:28:43 +08:00
|
|
|
void ConsumedAnalyzer::determineExpectedReturnState(AnalysisDeclContext &AC,
|
|
|
|
const FunctionDecl *D) {
|
|
|
|
QualType ReturnType;
|
|
|
|
if (const CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
|
|
|
|
ASTContext &CurrContext = AC.getASTContext();
|
|
|
|
ReturnType = Constructor->getThisType(CurrContext)->getPointeeType();
|
|
|
|
} else
|
|
|
|
ReturnType = D->getCallResultType();
|
|
|
|
|
2013-12-19 10:58:51 +08:00
|
|
|
if (const ReturnTypestateAttr *RTSAttr = D->getAttr<ReturnTypestateAttr>()) {
|
2013-09-06 09:28:43 +08:00
|
|
|
const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
|
|
|
|
if (!RD || !RD->hasAttr<ConsumableAttr>()) {
|
|
|
|
// FIXME: This should be removed when template instantiation propagates
|
|
|
|
// attributes at template specialization definition, not
|
|
|
|
// declaration. When it is removed the test needs to be enabled
|
|
|
|
// in SemaDeclAttr.cpp.
|
|
|
|
WarningsHandler.warnReturnTypestateForUnconsumableType(
|
|
|
|
RTSAttr->getLocation(), ReturnType.getAsString());
|
|
|
|
ExpectedReturnState = CS_None;
|
|
|
|
} else
|
|
|
|
ExpectedReturnState = mapReturnTypestateAttrState(RTSAttr);
|
2014-01-14 08:36:53 +08:00
|
|
|
} else if (isConsumableType(ReturnType)) {
|
|
|
|
if (isAutoCastType(ReturnType)) // We can auto-cast the state to the
|
|
|
|
ExpectedReturnState = CS_None; // expected state.
|
|
|
|
else
|
|
|
|
ExpectedReturnState = mapConsumableAttrState(ReturnType);
|
|
|
|
}
|
2013-09-06 09:28:43 +08:00
|
|
|
else
|
|
|
|
ExpectedReturnState = CS_None;
|
|
|
|
}
|
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
bool ConsumedAnalyzer::splitState(const CFGBlock *CurrBlock,
|
|
|
|
const ConsumedStmtVisitor &Visitor) {
|
2014-03-08 04:03:18 +08:00
|
|
|
|
|
|
|
std::unique_ptr<ConsumedStateMap> FalseStates(
|
|
|
|
new ConsumedStateMap(*CurrStates));
|
2013-08-30 01:26:57 +08:00
|
|
|
PropagationInfo PInfo;
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
if (const IfStmt *IfNode =
|
|
|
|
dyn_cast_or_null<IfStmt>(CurrBlock->getTerminator().getStmt())) {
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
const Stmt *Cond = IfNode->getCond();
|
|
|
|
|
|
|
|
PInfo = Visitor.getInfo(Cond);
|
|
|
|
if (!PInfo.isValid() && isa<BinaryOperator>(Cond))
|
|
|
|
PInfo = Visitor.getInfo(cast<BinaryOperator>(Cond)->getRHS());
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
if (PInfo.isVarTest()) {
|
2013-08-30 01:26:57 +08:00
|
|
|
CurrStates->setSource(Cond);
|
|
|
|
FalseStates->setSource(Cond);
|
2013-11-16 08:22:43 +08:00
|
|
|
splitVarStateForIf(IfNode, PInfo.getVarTest(), CurrStates,
|
2013-10-25 23:33:28 +08:00
|
|
|
FalseStates.get());
|
2013-08-30 01:26:57 +08:00
|
|
|
|
|
|
|
} else if (PInfo.isBinTest()) {
|
|
|
|
CurrStates->setSource(PInfo.testSourceNode());
|
|
|
|
FalseStates->setSource(PInfo.testSourceNode());
|
2013-10-25 23:33:28 +08:00
|
|
|
splitVarStateForIfBinOp(PInfo, CurrStates, FalseStates.get());
|
2013-08-13 05:20:55 +08:00
|
|
|
|
|
|
|
} else {
|
2013-08-30 01:26:57 +08:00
|
|
|
return false;
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
} else if (const BinaryOperator *BinOp =
|
|
|
|
dyn_cast_or_null<BinaryOperator>(CurrBlock->getTerminator().getStmt())) {
|
|
|
|
|
|
|
|
PInfo = Visitor.getInfo(BinOp->getLHS());
|
2013-11-16 08:22:43 +08:00
|
|
|
if (!PInfo.isVarTest()) {
|
2013-08-30 01:26:57 +08:00
|
|
|
if ((BinOp = dyn_cast_or_null<BinaryOperator>(BinOp->getLHS()))) {
|
|
|
|
PInfo = Visitor.getInfo(BinOp->getRHS());
|
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
if (!PInfo.isVarTest())
|
2013-08-30 01:26:57 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrStates->setSource(BinOp);
|
|
|
|
FalseStates->setSource(BinOp);
|
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
const VarTestResult &Test = PInfo.getVarTest();
|
2013-08-30 01:26:57 +08:00
|
|
|
ConsumedState VarState = CurrStates->getState(Test.Var);
|
|
|
|
|
|
|
|
if (BinOp->getOpcode() == BO_LAnd) {
|
|
|
|
if (VarState == CS_Unknown)
|
|
|
|
CurrStates->setState(Test.Var, Test.TestsFor);
|
|
|
|
else if (VarState == invertConsumedUnconsumed(Test.TestsFor))
|
|
|
|
CurrStates->markUnreachable();
|
|
|
|
|
|
|
|
} else if (BinOp->getOpcode() == BO_LOr) {
|
|
|
|
if (VarState == CS_Unknown)
|
|
|
|
FalseStates->setState(Test.Var,
|
|
|
|
invertConsumedUnconsumed(Test.TestsFor));
|
|
|
|
else if (VarState == Test.TestsFor)
|
|
|
|
FalseStates->markUnreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-13 05:20:55 +08:00
|
|
|
CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin();
|
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
if (*SI)
|
|
|
|
BlockInfo.addInfo(*SI, CurrStates);
|
|
|
|
else
|
|
|
|
delete CurrStates;
|
|
|
|
|
|
|
|
if (*++SI)
|
2014-03-08 03:33:25 +08:00
|
|
|
BlockInfo.addInfo(*SI, FalseStates.release());
|
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
CurrStates = NULL;
|
|
|
|
return true;
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConsumedAnalyzer::run(AnalysisDeclContext &AC) {
|
|
|
|
const FunctionDecl *D = dyn_cast_or_null<FunctionDecl>(AC.getDecl());
|
2013-09-11 07:10:10 +08:00
|
|
|
if (!D)
|
|
|
|
return;
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-09-11 07:10:10 +08:00
|
|
|
CFG *CFGraph = AC.getCFG();
|
|
|
|
if (!CFGraph)
|
|
|
|
return;
|
2013-10-18 02:19:31 +08:00
|
|
|
|
2013-09-06 09:28:43 +08:00
|
|
|
determineExpectedReturnState(AC, D);
|
2013-10-10 02:30:24 +08:00
|
|
|
|
2013-08-13 05:20:55 +08:00
|
|
|
PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>();
|
2013-10-10 02:30:24 +08:00
|
|
|
// AC.getCFG()->viewCFG(LangOptions());
|
|
|
|
|
|
|
|
BlockInfo = ConsumedBlockInfo(CFGraph->getNumBlockIDs(), SortedGraph);
|
2013-08-13 05:20:55 +08:00
|
|
|
|
|
|
|
CurrStates = new ConsumedStateMap();
|
2013-08-30 06:36:05 +08:00
|
|
|
ConsumedStmtVisitor Visitor(AC, *this, CurrStates);
|
|
|
|
|
|
|
|
// Add all trackable parameters to the state map.
|
2014-03-07 23:12:56 +08:00
|
|
|
for (auto PI : D->params()) {
|
|
|
|
Visitor.VisitParmVarDecl(PI);
|
2013-08-30 06:36:05 +08:00
|
|
|
}
|
2013-08-13 05:20:55 +08:00
|
|
|
|
|
|
|
// Visit all of the function's basic blocks.
|
|
|
|
for (PostOrderCFGView::iterator I = SortedGraph->begin(),
|
|
|
|
E = SortedGraph->end(); I != E; ++I) {
|
|
|
|
|
|
|
|
const CFGBlock *CurrBlock = *I;
|
|
|
|
|
|
|
|
if (CurrStates == NULL)
|
|
|
|
CurrStates = BlockInfo.getInfo(CurrBlock);
|
2013-08-30 01:26:57 +08:00
|
|
|
|
|
|
|
if (!CurrStates) {
|
|
|
|
continue;
|
|
|
|
|
|
|
|
} else if (!CurrStates->isReachable()) {
|
|
|
|
delete CurrStates;
|
|
|
|
CurrStates = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Visitor.reset(CurrStates);
|
|
|
|
|
2013-08-13 05:20:55 +08:00
|
|
|
// Visit all of the basic block's statements.
|
|
|
|
for (CFGBlock::const_iterator BI = CurrBlock->begin(),
|
|
|
|
BE = CurrBlock->end(); BI != BE; ++BI) {
|
|
|
|
|
2013-08-23 04:44:47 +08:00
|
|
|
switch (BI->getKind()) {
|
|
|
|
case CFGElement::Statement:
|
2013-08-13 05:20:55 +08:00
|
|
|
Visitor.Visit(BI->castAs<CFGStmt>().getStmt());
|
2013-08-23 04:44:47 +08:00
|
|
|
break;
|
2013-10-12 05:55:33 +08:00
|
|
|
|
|
|
|
case CFGElement::TemporaryDtor: {
|
|
|
|
const CFGTemporaryDtor DTor = BI->castAs<CFGTemporaryDtor>();
|
|
|
|
const CXXBindTemporaryExpr *BTE = DTor.getBindTemporaryExpr();
|
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
Visitor.checkCallability(PropagationInfo(BTE),
|
|
|
|
DTor.getDestructorDecl(AC.getASTContext()),
|
|
|
|
BTE->getExprLoc());
|
2013-10-12 05:55:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CFGElement::AutomaticObjectDtor: {
|
|
|
|
const CFGAutomaticObjDtor DTor = BI->castAs<CFGAutomaticObjDtor>();
|
2013-11-16 08:22:43 +08:00
|
|
|
SourceLocation Loc = DTor.getTriggerStmt()->getLocEnd();
|
2013-10-12 05:55:33 +08:00
|
|
|
const VarDecl *Var = DTor.getVarDecl();
|
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
Visitor.checkCallability(PropagationInfo(Var),
|
|
|
|
DTor.getDestructorDecl(AC.getASTContext()),
|
|
|
|
Loc);
|
2013-10-12 05:55:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-08-23 04:44:47 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
|
2013-11-16 08:22:43 +08:00
|
|
|
CurrStates->clearTemporaries();
|
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
// TODO: Handle other forms of branching with precision, including while-
|
|
|
|
// and for-loops. (Deferred)
|
|
|
|
if (!splitState(CurrBlock, Visitor)) {
|
|
|
|
CurrStates->setSource(NULL);
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-10-10 02:30:24 +08:00
|
|
|
if (CurrBlock->succ_size() > 1 ||
|
|
|
|
(CurrBlock->succ_size() == 1 &&
|
|
|
|
(*CurrBlock->succ_begin())->pred_size() > 1)) {
|
2013-08-13 05:20:55 +08:00
|
|
|
|
2013-08-30 01:26:57 +08:00
|
|
|
bool OwnershipTaken = false;
|
|
|
|
|
|
|
|
for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
|
|
|
|
SE = CurrBlock->succ_end(); SI != SE; ++SI) {
|
|
|
|
|
2013-10-10 02:30:24 +08:00
|
|
|
if (*SI == NULL) continue;
|
|
|
|
|
|
|
|
if (BlockInfo.isBackEdge(CurrBlock, *SI)) {
|
|
|
|
BlockInfo.borrowInfo(*SI)->intersectAtLoopHead(*SI, CurrBlock,
|
|
|
|
CurrStates,
|
|
|
|
WarningsHandler);
|
|
|
|
|
|
|
|
if (BlockInfo.allBackEdgesVisited(*SI, CurrBlock))
|
|
|
|
BlockInfo.discardInfo(*SI);
|
|
|
|
} else {
|
|
|
|
BlockInfo.addInfo(*SI, CurrStates, OwnershipTaken);
|
|
|
|
}
|
2013-08-30 01:26:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!OwnershipTaken)
|
|
|
|
delete CurrStates;
|
|
|
|
|
|
|
|
CurrStates = NULL;
|
2013-08-13 05:20:55 +08:00
|
|
|
}
|
|
|
|
}
|
2013-10-18 06:53:04 +08:00
|
|
|
|
|
|
|
if (CurrBlock == &AC.getCFG()->getExit() &&
|
|
|
|
D->getCallResultType()->isVoidType())
|
|
|
|
CurrStates->checkParamsForReturnTypestate(D->getLocation(),
|
|
|
|
WarningsHandler);
|
2013-08-13 05:20:55 +08:00
|
|
|
} // End of block iterator.
|
|
|
|
|
|
|
|
// Delete the last existing state map.
|
|
|
|
delete CurrStates;
|
|
|
|
|
|
|
|
WarningsHandler.emitDiagnostics();
|
|
|
|
}
|
|
|
|
}} // end namespace clang::consumed
|