2018-03-21 08:14:43 +08:00
|
|
|
//===- UninitializedValues.cpp - Find Uninitialized Values ----------------===//
|
2011-01-15 10:58:47 +08:00
|
|
|
//
|
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
|
2011-01-15 10:58:47 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements uninitialized values analysis for source-level CFGs.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-03-21 08:14:43 +08:00
|
|
|
#include "clang/Analysis/Analyses/UninitializedValues.h"
|
2012-12-01 23:09:41 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2011-01-15 10:58:47 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2018-03-21 08:14:43 +08:00
|
|
|
#include "clang/AST/DeclBase.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/OperationKinds.h"
|
|
|
|
#include "clang/AST/Stmt.h"
|
|
|
|
#include "clang/AST/StmtObjC.h"
|
2013-05-16 07:22:55 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2018-03-21 08:14:43 +08:00
|
|
|
#include "clang/AST/Type.h"
|
2014-09-23 16:34:41 +08:00
|
|
|
#include "clang/Analysis/Analyses/PostOrderCFGView.h"
|
2017-09-07 05:45:03 +08:00
|
|
|
#include "clang/Analysis/AnalysisDeclContext.h"
|
2012-12-01 23:09:41 +08:00
|
|
|
#include "clang/Analysis/CFG.h"
|
2012-09-13 08:21:35 +08:00
|
|
|
#include "clang/Analysis/DomainSpecific/ObjCNoReturn.h"
|
2018-03-21 08:14:43 +08:00
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "llvm/ADT/BitVector.h"
|
2012-12-01 23:09:41 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2018-03-21 08:14:43 +08:00
|
|
|
#include "llvm/ADT/None.h"
|
2012-12-01 23:09:41 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/PackedVector.h"
|
|
|
|
#include "llvm/ADT/SmallBitVector.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2018-03-21 08:14:43 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
2011-01-15 10:58:47 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2012-07-14 07:33:44 +08:00
|
|
|
#define DEBUG_LOGGING 0
|
|
|
|
|
2011-01-27 10:29:34 +08:00
|
|
|
static bool isTrackedVar(const VarDecl *vd, const DeclContext *dc) {
|
2011-03-17 11:06:11 +08:00
|
|
|
if (vd->isLocalVarDecl() && !vd->hasGlobalStorage() &&
|
2014-06-11 08:31:00 +08:00
|
|
|
!vd->isExceptionVariable() && !vd->isInitCapture() &&
|
2015-04-28 05:27:54 +08:00
|
|
|
!vd->isImplicit() && vd->getDeclContext() == dc) {
|
2011-03-17 11:06:11 +08:00
|
|
|
QualType ty = vd->getType();
|
2015-03-03 22:54:25 +08:00
|
|
|
return ty->isScalarType() || ty->isVectorType() || ty->isRecordType();
|
2011-03-17 11:06:11 +08:00
|
|
|
}
|
|
|
|
return false;
|
2011-01-18 12:53:25 +08:00
|
|
|
}
|
|
|
|
|
2011-01-15 10:58:47 +08:00
|
|
|
//------------------------------------------------------------------------====//
|
2011-03-15 12:57:27 +08:00
|
|
|
// DeclToIndex: a mapping from Decls we track to value indices.
|
2011-01-15 10:58:47 +08:00
|
|
|
//====------------------------------------------------------------------------//
|
|
|
|
|
|
|
|
namespace {
|
2018-03-21 08:14:43 +08:00
|
|
|
|
2011-03-15 12:57:27 +08:00
|
|
|
class DeclToIndex {
|
2011-01-15 10:58:47 +08:00
|
|
|
llvm::DenseMap<const VarDecl *, unsigned> map;
|
2018-03-21 08:14:43 +08:00
|
|
|
|
2011-01-15 10:58:47 +08:00
|
|
|
public:
|
2018-03-21 08:14:43 +08:00
|
|
|
DeclToIndex() = default;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-01-15 10:58:47 +08:00
|
|
|
/// Compute the actual mapping from declarations to bits.
|
|
|
|
void computeMap(const DeclContext &dc);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-01-15 10:58:47 +08:00
|
|
|
/// Return the number of declarations in the map.
|
|
|
|
unsigned size() const { return map.size(); }
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-01-15 10:58:47 +08:00
|
|
|
/// Returns the bit vector index for a given declaration.
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<unsigned> getValueIndex(const VarDecl *d) const;
|
2011-01-15 10:58:47 +08:00
|
|
|
};
|
2018-03-21 08:14:43 +08:00
|
|
|
|
|
|
|
} // namespace
|
2011-01-15 10:58:47 +08:00
|
|
|
|
2011-03-15 12:57:27 +08:00
|
|
|
void DeclToIndex::computeMap(const DeclContext &dc) {
|
2011-01-15 10:58:47 +08:00
|
|
|
unsigned count = 0;
|
|
|
|
DeclContext::specific_decl_iterator<VarDecl> I(dc.decls_begin()),
|
|
|
|
E(dc.decls_end());
|
|
|
|
for ( ; I != E; ++I) {
|
2012-06-07 04:45:41 +08:00
|
|
|
const VarDecl *vd = *I;
|
2011-01-27 10:29:34 +08:00
|
|
|
if (isTrackedVar(vd, &dc))
|
2011-01-15 10:58:47 +08:00
|
|
|
map[vd] = count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<unsigned> DeclToIndex::getValueIndex(const VarDecl *d) const {
|
2011-03-29 09:40:00 +08:00
|
|
|
llvm::DenseMap<const VarDecl *, unsigned>::const_iterator I = map.find(d);
|
2011-01-15 10:58:47 +08:00
|
|
|
if (I == map.end())
|
2013-02-21 09:47:18 +08:00
|
|
|
return None;
|
2011-01-15 10:58:47 +08:00
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------====//
|
|
|
|
// CFGBlockValues: dataflow values for CFG blocks.
|
|
|
|
//====------------------------------------------------------------------------//
|
|
|
|
|
2011-03-15 12:57:38 +08:00
|
|
|
// These values are defined in such a way that a merge can be done using
|
|
|
|
// a bitwise OR.
|
|
|
|
enum Value { Unknown = 0x0, /* 00 */
|
|
|
|
Initialized = 0x1, /* 01 */
|
|
|
|
Uninitialized = 0x2, /* 10 */
|
|
|
|
MayUninitialized = 0x3 /* 11 */ };
|
|
|
|
|
|
|
|
static bool isUninitialized(const Value v) {
|
|
|
|
return v >= Uninitialized;
|
|
|
|
}
|
2018-03-21 08:14:43 +08:00
|
|
|
|
2011-03-15 12:57:38 +08:00
|
|
|
static bool isAlwaysUninit(const Value v) {
|
|
|
|
return v == Uninitialized;
|
|
|
|
}
|
2011-03-15 12:57:29 +08:00
|
|
|
|
2011-03-26 20:38:21 +08:00
|
|
|
namespace {
|
2011-03-15 12:57:29 +08:00
|
|
|
|
2018-03-21 08:14:43 +08:00
|
|
|
using ValueVector = llvm::PackedVector<Value, 2, llvm::SmallBitVector>;
|
2011-01-21 01:37:17 +08:00
|
|
|
|
2011-01-15 10:58:47 +08:00
|
|
|
class CFGBlockValues {
|
|
|
|
const CFG &cfg;
|
2012-09-29 00:44:29 +08:00
|
|
|
SmallVector<ValueVector, 8> vals;
|
2011-03-15 12:57:27 +08:00
|
|
|
ValueVector scratch;
|
2011-03-15 13:30:12 +08:00
|
|
|
DeclToIndex declToIndex;
|
2018-03-21 08:14:43 +08:00
|
|
|
|
2011-01-15 10:58:47 +08:00
|
|
|
public:
|
|
|
|
CFGBlockValues(const CFG &cfg);
|
2012-07-19 12:59:05 +08:00
|
|
|
|
2011-04-05 07:29:12 +08:00
|
|
|
unsigned getNumEntries() const { return declToIndex.size(); }
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
void computeSetOfDeclarations(const DeclContext &dc);
|
2018-03-21 08:14:43 +08:00
|
|
|
|
2012-07-19 12:59:05 +08:00
|
|
|
ValueVector &getValueVector(const CFGBlock *block) {
|
2012-09-29 00:44:29 +08:00
|
|
|
return vals[block->getBlockID()];
|
2012-07-19 12:59:05 +08:00
|
|
|
}
|
2011-01-21 01:37:17 +08:00
|
|
|
|
2012-07-03 07:23:04 +08:00
|
|
|
void setAllScratchValues(Value V);
|
2011-03-15 12:57:27 +08:00
|
|
|
void mergeIntoScratch(ValueVector const &source, bool isFirst);
|
|
|
|
bool updateValueVectorWithScratch(const CFGBlock *block);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-01-15 10:58:47 +08:00
|
|
|
bool hasNoDeclarations() const {
|
2011-03-15 13:30:12 +08:00
|
|
|
return declToIndex.size() == 0;
|
2011-01-15 10:58:47 +08:00
|
|
|
}
|
2011-08-20 09:15:28 +08:00
|
|
|
|
2011-01-15 10:58:47 +08:00
|
|
|
void resetScratch();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-03-15 12:57:27 +08:00
|
|
|
ValueVector::reference operator[](const VarDecl *vd);
|
2012-05-25 10:17:09 +08:00
|
|
|
|
|
|
|
Value getValue(const CFGBlock *block, const CFGBlock *dstBlock,
|
|
|
|
const VarDecl *vd) {
|
2013-02-21 06:23:23 +08:00
|
|
|
const Optional<unsigned> &idx = declToIndex.getValueIndex(vd);
|
2012-05-25 10:17:09 +08:00
|
|
|
assert(idx.hasValue());
|
2012-07-19 12:59:05 +08:00
|
|
|
return getValueVector(block)[idx.getValue()];
|
2012-05-25 10:17:09 +08:00
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
};
|
2018-03-21 08:14:43 +08:00
|
|
|
|
|
|
|
} // namespace
|
2011-01-15 10:58:47 +08:00
|
|
|
|
2012-07-19 12:59:05 +08:00
|
|
|
CFGBlockValues::CFGBlockValues(const CFG &c) : cfg(c), vals(0) {}
|
2011-01-15 10:58:47 +08:00
|
|
|
|
|
|
|
void CFGBlockValues::computeSetOfDeclarations(const DeclContext &dc) {
|
2011-03-15 13:30:12 +08:00
|
|
|
declToIndex.computeMap(dc);
|
2012-07-19 12:59:05 +08:00
|
|
|
unsigned decls = declToIndex.size();
|
|
|
|
scratch.resize(decls);
|
|
|
|
unsigned n = cfg.getNumBlockIDs();
|
|
|
|
if (!n)
|
|
|
|
return;
|
|
|
|
vals.resize(n);
|
2018-03-21 08:14:43 +08:00
|
|
|
for (auto &val : vals)
|
|
|
|
val.resize(decls);
|
2011-01-21 01:37:17 +08:00
|
|
|
}
|
|
|
|
|
2012-07-14 07:33:44 +08:00
|
|
|
#if DEBUG_LOGGING
|
2011-03-15 12:57:27 +08:00
|
|
|
static void printVector(const CFGBlock *block, ValueVector &bv,
|
2011-02-02 01:43:18 +08:00
|
|
|
unsigned num) {
|
|
|
|
llvm::errs() << block->getBlockID() << " :";
|
2018-03-21 08:14:43 +08:00
|
|
|
for (const auto &i : bv)
|
|
|
|
llvm::errs() << ' ' << i;
|
2011-02-02 01:43:18 +08:00
|
|
|
llvm::errs() << " : " << num << '\n';
|
|
|
|
}
|
|
|
|
#endif
|
2011-01-15 10:58:47 +08:00
|
|
|
|
2012-07-03 07:23:04 +08:00
|
|
|
void CFGBlockValues::setAllScratchValues(Value V) {
|
|
|
|
for (unsigned I = 0, E = scratch.size(); I != E; ++I)
|
|
|
|
scratch[I] = V;
|
|
|
|
}
|
|
|
|
|
2011-10-07 08:42:48 +08:00
|
|
|
void CFGBlockValues::mergeIntoScratch(ValueVector const &source,
|
|
|
|
bool isFirst) {
|
|
|
|
if (isFirst)
|
|
|
|
scratch = source;
|
|
|
|
else
|
|
|
|
scratch |= source;
|
|
|
|
}
|
|
|
|
|
2011-03-15 12:57:27 +08:00
|
|
|
bool CFGBlockValues::updateValueVectorWithScratch(const CFGBlock *block) {
|
2012-07-19 12:59:05 +08:00
|
|
|
ValueVector &dst = getValueVector(block);
|
2011-01-15 10:58:47 +08:00
|
|
|
bool changed = (dst != scratch);
|
|
|
|
if (changed)
|
|
|
|
dst = scratch;
|
2012-07-14 07:33:44 +08:00
|
|
|
#if DEBUG_LOGGING
|
2011-02-02 01:43:18 +08:00
|
|
|
printVector(block, scratch, 0);
|
|
|
|
#endif
|
2011-01-21 01:37:17 +08:00
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2011-01-15 10:58:47 +08:00
|
|
|
void CFGBlockValues::resetScratch() {
|
|
|
|
scratch.reset();
|
|
|
|
}
|
|
|
|
|
2011-03-15 12:57:27 +08:00
|
|
|
ValueVector::reference CFGBlockValues::operator[](const VarDecl *vd) {
|
2013-02-21 06:23:23 +08:00
|
|
|
const Optional<unsigned> &idx = declToIndex.getValueIndex(vd);
|
2011-01-15 10:58:47 +08:00
|
|
|
assert(idx.hasValue());
|
|
|
|
return scratch[idx.getValue()];
|
|
|
|
}
|
|
|
|
|
2014-09-23 16:34:41 +08:00
|
|
|
//------------------------------------------------------------------------====//
|
|
|
|
// Worklist: worklist for dataflow analysis.
|
|
|
|
//====------------------------------------------------------------------------//
|
|
|
|
|
|
|
|
namespace {
|
2018-03-21 08:14:43 +08:00
|
|
|
|
2014-09-23 16:34:41 +08:00
|
|
|
class DataflowWorklist {
|
|
|
|
PostOrderCFGView::iterator PO_I, PO_E;
|
|
|
|
SmallVector<const CFGBlock *, 20> worklist;
|
|
|
|
llvm::BitVector enqueuedBlocks;
|
2018-03-21 08:14:43 +08:00
|
|
|
|
2014-09-23 16:34:41 +08:00
|
|
|
public:
|
|
|
|
DataflowWorklist(const CFG &cfg, PostOrderCFGView &view)
|
2018-03-21 08:14:43 +08:00
|
|
|
: PO_I(view.begin()), PO_E(view.end()),
|
|
|
|
enqueuedBlocks(cfg.getNumBlockIDs(), true) {
|
|
|
|
// Treat the first block as already analyzed.
|
|
|
|
if (PO_I != PO_E) {
|
|
|
|
assert(*PO_I == &cfg.getEntry());
|
|
|
|
enqueuedBlocks[(*PO_I)->getBlockID()] = false;
|
|
|
|
++PO_I;
|
|
|
|
}
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2014-09-23 16:34:41 +08:00
|
|
|
void enqueueSuccessors(const CFGBlock *block);
|
|
|
|
const CFGBlock *dequeue();
|
|
|
|
};
|
|
|
|
|
2018-03-21 08:14:43 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void DataflowWorklist::enqueueSuccessors(const CFGBlock *block) {
|
2014-09-23 16:34:41 +08:00
|
|
|
for (CFGBlock::const_succ_iterator I = block->succ_begin(),
|
|
|
|
E = block->succ_end(); I != E; ++I) {
|
|
|
|
const CFGBlock *Successor = *I;
|
|
|
|
if (!Successor || enqueuedBlocks[Successor->getBlockID()])
|
|
|
|
continue;
|
|
|
|
worklist.push_back(Successor);
|
|
|
|
enqueuedBlocks[Successor->getBlockID()] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const CFGBlock *DataflowWorklist::dequeue() {
|
|
|
|
const CFGBlock *B = nullptr;
|
|
|
|
|
|
|
|
// First dequeue from the worklist. This can represent
|
|
|
|
// updates along backedges that we want propagated as quickly as possible.
|
|
|
|
if (!worklist.empty())
|
|
|
|
B = worklist.pop_back_val();
|
|
|
|
|
|
|
|
// Next dequeue from the initial reverse post order. This is the
|
|
|
|
// theoretical ideal in the presence of no back edges.
|
|
|
|
else if (PO_I != PO_E) {
|
|
|
|
B = *PO_I;
|
|
|
|
++PO_I;
|
|
|
|
}
|
2018-03-21 08:14:43 +08:00
|
|
|
else
|
2014-09-23 16:34:41 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
assert(enqueuedBlocks[B->getBlockID()] == true);
|
|
|
|
enqueuedBlocks[B->getBlockID()] = false;
|
|
|
|
return B;
|
|
|
|
}
|
|
|
|
|
2011-01-15 10:58:47 +08:00
|
|
|
//------------------------------------------------------------------------====//
|
2012-07-17 08:06:14 +08:00
|
|
|
// Classification of DeclRefExprs as use or initialization.
|
2011-01-15 10:58:47 +08:00
|
|
|
//====------------------------------------------------------------------------//
|
|
|
|
|
|
|
|
namespace {
|
2018-03-21 08:14:43 +08:00
|
|
|
|
2011-01-15 10:58:47 +08:00
|
|
|
class FindVarResult {
|
|
|
|
const VarDecl *vd;
|
|
|
|
const DeclRefExpr *dr;
|
2018-03-21 08:14:43 +08:00
|
|
|
|
2011-01-15 10:58:47 +08:00
|
|
|
public:
|
2012-07-17 08:06:14 +08:00
|
|
|
FindVarResult(const VarDecl *vd, const DeclRefExpr *dr) : vd(vd), dr(dr) {}
|
|
|
|
|
2011-01-15 10:58:47 +08:00
|
|
|
const DeclRefExpr *getDeclRefExpr() const { return dr; }
|
|
|
|
const VarDecl *getDecl() const { return vd; }
|
|
|
|
};
|
2012-07-17 08:06:14 +08:00
|
|
|
|
2018-03-21 08:14:43 +08:00
|
|
|
} // namespace
|
|
|
|
|
2012-07-17 08:06:14 +08:00
|
|
|
static const Expr *stripCasts(ASTContext &C, const Expr *Ex) {
|
|
|
|
while (Ex) {
|
|
|
|
Ex = Ex->IgnoreParenNoopCasts(C);
|
2018-03-21 08:14:43 +08:00
|
|
|
if (const auto *CE = dyn_cast<CastExpr>(Ex)) {
|
2012-07-17 08:06:14 +08:00
|
|
|
if (CE->getCastKind() == CK_LValueBitCast) {
|
|
|
|
Ex = CE->getSubExpr();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return Ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If E is an expression comprising a reference to a single variable, find that
|
|
|
|
/// variable.
|
|
|
|
static FindVarResult findVar(const Expr *E, const DeclContext *DC) {
|
2018-03-21 08:14:43 +08:00
|
|
|
if (const auto *DRE =
|
|
|
|
dyn_cast<DeclRefExpr>(stripCasts(DC->getParentASTContext(), E)))
|
|
|
|
if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
|
2012-07-17 08:06:14 +08:00
|
|
|
if (isTrackedVar(VD, DC))
|
|
|
|
return FindVarResult(VD, DRE);
|
2014-05-20 12:30:07 +08:00
|
|
|
return FindVarResult(nullptr, nullptr);
|
2012-07-17 08:06:14 +08:00
|
|
|
}
|
|
|
|
|
2018-03-21 08:14:43 +08:00
|
|
|
namespace {
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Classify each DeclRefExpr as an initialization or a use. Any
|
2012-07-17 08:06:14 +08:00
|
|
|
/// DeclRefExpr which isn't explicitly classified will be assumed to have
|
|
|
|
/// escaped the analysis and will be treated as an initialization.
|
|
|
|
class ClassifyRefs : public StmtVisitor<ClassifyRefs> {
|
|
|
|
public:
|
|
|
|
enum Class {
|
|
|
|
Init,
|
|
|
|
Use,
|
|
|
|
SelfInit,
|
|
|
|
Ignore
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
const DeclContext *DC;
|
2018-03-21 08:14:43 +08:00
|
|
|
llvm::DenseMap<const DeclRefExpr *, Class> Classification;
|
2012-07-17 08:06:14 +08:00
|
|
|
|
|
|
|
bool isTrackedVar(const VarDecl *VD) const {
|
|
|
|
return ::isTrackedVar(VD, DC);
|
|
|
|
}
|
|
|
|
|
|
|
|
void classify(const Expr *E, Class C);
|
|
|
|
|
|
|
|
public:
|
|
|
|
ClassifyRefs(AnalysisDeclContext &AC) : DC(cast<DeclContext>(AC.getDecl())) {}
|
|
|
|
|
|
|
|
void VisitDeclStmt(DeclStmt *DS);
|
|
|
|
void VisitUnaryOperator(UnaryOperator *UO);
|
|
|
|
void VisitBinaryOperator(BinaryOperator *BO);
|
|
|
|
void VisitCallExpr(CallExpr *CE);
|
|
|
|
void VisitCastExpr(CastExpr *CE);
|
[OPENMP]Initial fix PR42392: Improve -Wuninitialized warnings for OpenMP programs.
Summary:
Some OpenMP clauses rely on the values of the variables. If the variable
is not initialized and used in OpenMP clauses that depend on the
variables values, it should be reported that the uninitialized variable
is used in the OpenMP clause expression.
This patch adds initial processing for uninitialized variables in OpenMP
constructs. Currently, it checks for use of the uninitialized variables
in the structured blocks.
Reviewers: NoQ, Szelethus, dcoughlin, xazax.hun, a.sidorin, george.karpenkov, szepet
Subscribers: rnkovacs, guansong, jfb, jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64356
llvm-svn: 365786
2019-07-11 22:54:17 +08:00
|
|
|
void VisitOMPExecutableDirective(OMPExecutableDirective *ED);
|
2012-07-17 08:06:14 +08:00
|
|
|
|
|
|
|
void operator()(Stmt *S) { Visit(S); }
|
|
|
|
|
|
|
|
Class get(const DeclRefExpr *DRE) const {
|
|
|
|
llvm::DenseMap<const DeclRefExpr*, Class>::const_iterator I
|
|
|
|
= Classification.find(DRE);
|
|
|
|
if (I != Classification.end())
|
|
|
|
return I->second;
|
|
|
|
|
2018-03-21 08:14:43 +08:00
|
|
|
const auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
|
2012-07-17 08:06:14 +08:00
|
|
|
if (!VD || !isTrackedVar(VD))
|
|
|
|
return Ignore;
|
|
|
|
|
|
|
|
return Init;
|
|
|
|
}
|
|
|
|
};
|
2018-03-21 08:14:43 +08:00
|
|
|
|
|
|
|
} // namespace
|
2012-07-17 08:06:14 +08:00
|
|
|
|
|
|
|
static const DeclRefExpr *getSelfInitExpr(VarDecl *VD) {
|
2016-10-29 23:28:25 +08:00
|
|
|
if (VD->getType()->isRecordType())
|
|
|
|
return nullptr;
|
2012-07-17 08:06:14 +08:00
|
|
|
if (Expr *Init = VD->getInit()) {
|
2018-03-21 08:14:43 +08:00
|
|
|
const auto *DRE =
|
|
|
|
dyn_cast<DeclRefExpr>(stripCasts(VD->getASTContext(), Init));
|
2012-07-17 08:06:14 +08:00
|
|
|
if (DRE && DRE->getDecl() == VD)
|
|
|
|
return DRE;
|
|
|
|
}
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2012-07-17 08:06:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClassifyRefs::classify(const Expr *E, Class C) {
|
2013-01-19 08:25:06 +08:00
|
|
|
// The result of a ?: could also be an lvalue.
|
|
|
|
E = E->IgnoreParens();
|
2018-03-21 08:14:43 +08:00
|
|
|
if (const auto *CO = dyn_cast<ConditionalOperator>(E)) {
|
2014-08-28 06:15:10 +08:00
|
|
|
classify(CO->getTrueExpr(), C);
|
2013-01-19 08:25:06 +08:00
|
|
|
classify(CO->getFalseExpr(), C);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:14:43 +08:00
|
|
|
if (const auto *BCO = dyn_cast<BinaryConditionalOperator>(E)) {
|
2014-08-28 06:15:10 +08:00
|
|
|
classify(BCO->getFalseExpr(), C);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:14:43 +08:00
|
|
|
if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E)) {
|
2014-08-28 06:15:10 +08:00
|
|
|
classify(OVE->getSourceExpr(), C);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:14:43 +08:00
|
|
|
if (const auto *ME = dyn_cast<MemberExpr>(E)) {
|
|
|
|
if (const auto *VD = dyn_cast<VarDecl>(ME->getMemberDecl())) {
|
2015-03-03 22:54:25 +08:00
|
|
|
if (!VD->isStaticDataMember())
|
|
|
|
classify(ME->getBase(), C);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:14:43 +08:00
|
|
|
if (const auto *BO = dyn_cast<BinaryOperator>(E)) {
|
2015-03-03 22:54:25 +08:00
|
|
|
switch (BO->getOpcode()) {
|
|
|
|
case BO_PtrMemD:
|
|
|
|
case BO_PtrMemI:
|
|
|
|
classify(BO->getLHS(), C);
|
|
|
|
return;
|
|
|
|
case BO_Comma:
|
2014-08-28 06:15:10 +08:00
|
|
|
classify(BO->getRHS(), C);
|
2015-03-03 22:54:25 +08:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2014-08-28 06:15:10 +08:00
|
|
|
}
|
|
|
|
|
2012-07-17 08:06:14 +08:00
|
|
|
FindVarResult Var = findVar(E, DC);
|
|
|
|
if (const DeclRefExpr *DRE = Var.getDeclRefExpr())
|
|
|
|
Classification[DRE] = std::max(Classification[DRE], C);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClassifyRefs::VisitDeclStmt(DeclStmt *DS) {
|
2014-03-15 01:01:24 +08:00
|
|
|
for (auto *DI : DS->decls()) {
|
2018-03-21 08:14:43 +08:00
|
|
|
auto *VD = dyn_cast<VarDecl>(DI);
|
2012-07-17 08:06:14 +08:00
|
|
|
if (VD && isTrackedVar(VD))
|
|
|
|
if (const DeclRefExpr *DRE = getSelfInitExpr(VD))
|
|
|
|
Classification[DRE] = SelfInit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClassifyRefs::VisitBinaryOperator(BinaryOperator *BO) {
|
|
|
|
// Ignore the evaluation of a DeclRefExpr on the LHS of an assignment. If this
|
|
|
|
// is not a compound-assignment, we will treat it as initializing the variable
|
|
|
|
// when TransferFunctions visits it. A compound-assignment does not affect
|
|
|
|
// whether a variable is uninitialized, and there's no point counting it as a
|
|
|
|
// use.
|
2012-07-17 09:27:33 +08:00
|
|
|
if (BO->isCompoundAssignmentOp())
|
|
|
|
classify(BO->getLHS(), Use);
|
2015-03-03 22:54:25 +08:00
|
|
|
else if (BO->getOpcode() == BO_Assign || BO->getOpcode() == BO_Comma)
|
2012-07-17 08:06:14 +08:00
|
|
|
classify(BO->getLHS(), Ignore);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClassifyRefs::VisitUnaryOperator(UnaryOperator *UO) {
|
|
|
|
// Increment and decrement are uses despite there being no lvalue-to-rvalue
|
|
|
|
// conversion.
|
|
|
|
if (UO->isIncrementDecrementOp())
|
|
|
|
classify(UO->getSubExpr(), Use);
|
|
|
|
}
|
|
|
|
|
[OPENMP]Initial fix PR42392: Improve -Wuninitialized warnings for OpenMP programs.
Summary:
Some OpenMP clauses rely on the values of the variables. If the variable
is not initialized and used in OpenMP clauses that depend on the
variables values, it should be reported that the uninitialized variable
is used in the OpenMP clause expression.
This patch adds initial processing for uninitialized variables in OpenMP
constructs. Currently, it checks for use of the uninitialized variables
in the structured blocks.
Reviewers: NoQ, Szelethus, dcoughlin, xazax.hun, a.sidorin, george.karpenkov, szepet
Subscribers: rnkovacs, guansong, jfb, jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64356
llvm-svn: 365786
2019-07-11 22:54:17 +08:00
|
|
|
void ClassifyRefs::VisitOMPExecutableDirective(OMPExecutableDirective *ED) {
|
|
|
|
for (Stmt *S : OMPExecutableDirective::used_clauses_children(ED->clauses()))
|
|
|
|
classify(cast<Expr>(S), Use);
|
|
|
|
}
|
|
|
|
|
2015-03-03 22:54:25 +08:00
|
|
|
static bool isPointerToConst(const QualType &QT) {
|
|
|
|
return QT->isAnyPointerType() && QT->getPointeeType().isConstQualified();
|
|
|
|
}
|
|
|
|
|
2012-07-17 08:06:14 +08:00
|
|
|
void ClassifyRefs::VisitCallExpr(CallExpr *CE) {
|
2014-08-26 12:30:55 +08:00
|
|
|
// Classify arguments to std::move as used.
|
2017-09-29 00:16:39 +08:00
|
|
|
if (CE->isCallToStdMove()) {
|
|
|
|
// RecordTypes are handled in SemaDeclCXX.cpp.
|
|
|
|
if (!CE->getArg(0)->getType()->isRecordType())
|
|
|
|
classify(CE->getArg(0), Use);
|
|
|
|
return;
|
2014-08-26 12:30:55 +08:00
|
|
|
}
|
|
|
|
|
2015-03-03 22:54:25 +08:00
|
|
|
// If a value is passed by const pointer or by const reference to a function,
|
|
|
|
// we should not assume that it is initialized by the call, and we
|
|
|
|
// conservatively do not assume that it is used.
|
2012-07-17 08:06:14 +08:00
|
|
|
for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
|
2015-03-03 22:54:25 +08:00
|
|
|
I != E; ++I) {
|
|
|
|
if ((*I)->isGLValue()) {
|
|
|
|
if ((*I)->getType().isConstQualified())
|
|
|
|
classify((*I), Ignore);
|
|
|
|
} else if (isPointerToConst((*I)->getType())) {
|
|
|
|
const Expr *Ex = stripCasts(DC->getParentASTContext(), *I);
|
2018-03-21 08:14:43 +08:00
|
|
|
const auto *UO = dyn_cast<UnaryOperator>(Ex);
|
2015-03-03 22:54:25 +08:00
|
|
|
if (UO && UO->getOpcode() == UO_AddrOf)
|
|
|
|
Ex = UO->getSubExpr();
|
|
|
|
classify(Ex, Ignore);
|
|
|
|
}
|
|
|
|
}
|
2012-07-17 08:06:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClassifyRefs::VisitCastExpr(CastExpr *CE) {
|
|
|
|
if (CE->getCastKind() == CK_LValueToRValue)
|
|
|
|
classify(CE->getSubExpr(), Use);
|
2018-03-21 08:14:43 +08:00
|
|
|
else if (const auto *CSE = dyn_cast<CStyleCastExpr>(CE)) {
|
2012-07-17 08:06:14 +08:00
|
|
|
if (CSE->getType()->isVoidType()) {
|
|
|
|
// Squelch any detected load of an uninitialized value if
|
|
|
|
// we cast it to void.
|
|
|
|
// e.g. (void) x;
|
|
|
|
classify(CSE->getSubExpr(), Ignore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------====//
|
|
|
|
// Transfer function for uninitialized values analysis.
|
|
|
|
//====------------------------------------------------------------------------//
|
|
|
|
|
|
|
|
namespace {
|
2018-03-21 08:14:43 +08:00
|
|
|
|
2011-07-19 22:18:48 +08:00
|
|
|
class TransferFunctions : public StmtVisitor<TransferFunctions> {
|
2011-01-15 10:58:47 +08:00
|
|
|
CFGBlockValues &vals;
|
|
|
|
const CFG &cfg;
|
2012-05-25 10:17:09 +08:00
|
|
|
const CFGBlock *block;
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext ∾
|
2012-07-17 08:06:14 +08:00
|
|
|
const ClassifyRefs &classification;
|
2012-09-13 08:21:35 +08:00
|
|
|
ObjCNoReturn objCNoRet;
|
2012-11-17 15:18:30 +08:00
|
|
|
UninitVariablesHandler &handler;
|
2012-07-17 08:06:14 +08:00
|
|
|
|
2011-01-15 10:58:47 +08:00
|
|
|
public:
|
|
|
|
TransferFunctions(CFGBlockValues &vals, const CFG &cfg,
|
2012-05-25 10:17:09 +08:00
|
|
|
const CFGBlock *block, AnalysisDeclContext &ac,
|
2012-07-17 08:06:14 +08:00
|
|
|
const ClassifyRefs &classification,
|
2012-11-17 15:18:30 +08:00
|
|
|
UninitVariablesHandler &handler)
|
2018-03-21 08:14:43 +08:00
|
|
|
: vals(vals), cfg(cfg), block(block), ac(ac),
|
|
|
|
classification(classification), objCNoRet(ac.getASTContext()),
|
|
|
|
handler(handler) {}
|
2012-07-17 08:06:14 +08:00
|
|
|
|
2012-05-25 07:45:35 +08:00
|
|
|
void reportUse(const Expr *ex, const VarDecl *vd);
|
2011-01-26 03:13:48 +08:00
|
|
|
|
2012-09-13 08:21:35 +08:00
|
|
|
void VisitBinaryOperator(BinaryOperator *bo);
|
2011-01-26 03:13:48 +08:00
|
|
|
void VisitBlockExpr(BlockExpr *be);
|
2012-07-03 07:23:04 +08:00
|
|
|
void VisitCallExpr(CallExpr *ce);
|
2011-01-19 05:18:58 +08:00
|
|
|
void VisitDeclRefExpr(DeclRefExpr *dr);
|
2012-09-13 08:21:35 +08:00
|
|
|
void VisitDeclStmt(DeclStmt *ds);
|
|
|
|
void VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS);
|
|
|
|
void VisitObjCMessageExpr(ObjCMessageExpr *ME);
|
[OPENMP]Initial fix PR42392: Improve -Wuninitialized warnings for OpenMP programs.
Summary:
Some OpenMP clauses rely on the values of the variables. If the variable
is not initialized and used in OpenMP clauses that depend on the
variables values, it should be reported that the uninitialized variable
is used in the OpenMP clause expression.
This patch adds initial processing for uninitialized variables in OpenMP
constructs. Currently, it checks for use of the uninitialized variables
in the structured blocks.
Reviewers: NoQ, Szelethus, dcoughlin, xazax.hun, a.sidorin, george.karpenkov, szepet
Subscribers: rnkovacs, guansong, jfb, jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64356
llvm-svn: 365786
2019-07-11 22:54:17 +08:00
|
|
|
void VisitOMPExecutableDirective(OMPExecutableDirective *ED);
|
2012-05-25 10:17:09 +08:00
|
|
|
|
2011-01-27 10:29:34 +08:00
|
|
|
bool isTrackedVar(const VarDecl *vd) {
|
|
|
|
return ::isTrackedVar(vd, cast<DeclContext>(ac.getDecl()));
|
|
|
|
}
|
2012-05-25 10:17:09 +08:00
|
|
|
|
2012-07-17 08:06:14 +08:00
|
|
|
FindVarResult findVar(const Expr *ex) {
|
|
|
|
return ::findVar(ex, cast<DeclContext>(ac.getDecl()));
|
|
|
|
}
|
|
|
|
|
2012-05-25 10:17:09 +08:00
|
|
|
UninitUse getUninitUse(const Expr *ex, const VarDecl *vd, Value v) {
|
|
|
|
UninitUse Use(ex, isAlwaysUninit(v));
|
|
|
|
|
|
|
|
assert(isUninitialized(v));
|
|
|
|
if (Use.getKind() == UninitUse::Always)
|
|
|
|
return Use;
|
|
|
|
|
|
|
|
// If an edge which leads unconditionally to this use did not initialize
|
|
|
|
// the variable, we can say something stronger than 'may be uninitialized':
|
|
|
|
// we can say 'either it's used uninitialized or you have dead code'.
|
|
|
|
//
|
|
|
|
// We track the number of successors of a node which have been visited, and
|
|
|
|
// visit a node once we have visited all of its successors. Only edges where
|
|
|
|
// the variable might still be uninitialized are followed. Since a variable
|
|
|
|
// can't transfer from being initialized to being uninitialized, this will
|
|
|
|
// trace out the subgraph which inevitably leads to the use and does not
|
|
|
|
// initialize the variable. We do not want to skip past loops, since their
|
|
|
|
// non-termination might be correlated with the initialization condition.
|
|
|
|
//
|
|
|
|
// For example:
|
|
|
|
//
|
|
|
|
// void f(bool a, bool b) {
|
|
|
|
// block1: int n;
|
|
|
|
// if (a) {
|
|
|
|
// block2: if (b)
|
|
|
|
// block3: n = 1;
|
|
|
|
// block4: } else if (b) {
|
|
|
|
// block5: while (!a) {
|
|
|
|
// block6: do_work(&a);
|
|
|
|
// n = 2;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// block7: if (a)
|
|
|
|
// block8: g();
|
|
|
|
// block9: return n;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Starting from the maybe-uninitialized use in block 9:
|
|
|
|
// * Block 7 is not visited because we have only visited one of its two
|
|
|
|
// successors.
|
|
|
|
// * Block 8 is visited because we've visited its only successor.
|
|
|
|
// From block 8:
|
|
|
|
// * Block 7 is visited because we've now visited both of its successors.
|
|
|
|
// From block 7:
|
|
|
|
// * Blocks 1, 2, 4, 5, and 6 are not visited because we didn't visit all
|
|
|
|
// of their successors (we didn't visit 4, 3, 5, 6, and 5, respectively).
|
|
|
|
// * Block 3 is not visited because it initializes 'n'.
|
|
|
|
// Now the algorithm terminates, having visited blocks 7 and 8, and having
|
|
|
|
// found the frontier is blocks 2, 4, and 5.
|
|
|
|
//
|
|
|
|
// 'n' is definitely uninitialized for two edges into block 7 (from blocks 2
|
|
|
|
// and 4), so we report that any time either of those edges is taken (in
|
|
|
|
// each case when 'b == false'), 'n' is used uninitialized.
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector<const CFGBlock*, 32> Queue;
|
|
|
|
SmallVector<unsigned, 32> SuccsVisited(cfg.getNumBlockIDs(), 0);
|
2012-05-25 10:17:09 +08:00
|
|
|
Queue.push_back(block);
|
|
|
|
// Specify that we've already visited all successors of the starting block.
|
|
|
|
// This has the dual purpose of ensuring we never add it to the queue, and
|
|
|
|
// of marking it as not being a candidate element of the frontier.
|
|
|
|
SuccsVisited[block->getBlockID()] = block->succ_size();
|
|
|
|
while (!Queue.empty()) {
|
2013-08-24 00:11:15 +08:00
|
|
|
const CFGBlock *B = Queue.pop_back_val();
|
2013-09-13 02:49:10 +08:00
|
|
|
|
|
|
|
// If the use is always reached from the entry block, make a note of that.
|
|
|
|
if (B == &cfg.getEntry())
|
|
|
|
Use.setUninitAfterCall();
|
|
|
|
|
2012-05-25 10:17:09 +08:00
|
|
|
for (CFGBlock::const_pred_iterator I = B->pred_begin(), E = B->pred_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
const CFGBlock *Pred = *I;
|
2014-02-27 08:24:00 +08:00
|
|
|
if (!Pred)
|
|
|
|
continue;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2013-09-13 02:49:10 +08:00
|
|
|
Value AtPredExit = vals.getValue(Pred, B, vd);
|
|
|
|
if (AtPredExit == Initialized)
|
2012-05-25 10:17:09 +08:00
|
|
|
// This block initializes the variable.
|
|
|
|
continue;
|
2013-09-13 02:49:10 +08:00
|
|
|
if (AtPredExit == MayUninitialized &&
|
2014-05-20 12:30:07 +08:00
|
|
|
vals.getValue(B, nullptr, vd) == Uninitialized) {
|
2013-09-13 02:49:10 +08:00
|
|
|
// This block declares the variable (uninitialized), and is reachable
|
|
|
|
// from a block that initializes the variable. We can't guarantee to
|
|
|
|
// give an earlier location for the diagnostic (and it appears that
|
|
|
|
// this code is intended to be reachable) so give a diagnostic here
|
|
|
|
// and go no further down this path.
|
|
|
|
Use.setUninitAfterDecl();
|
|
|
|
continue;
|
|
|
|
}
|
2012-05-25 10:17:09 +08:00
|
|
|
|
2012-07-14 07:33:44 +08:00
|
|
|
unsigned &SV = SuccsVisited[Pred->getBlockID()];
|
|
|
|
if (!SV) {
|
|
|
|
// When visiting the first successor of a block, mark all NULL
|
|
|
|
// successors as having been visited.
|
|
|
|
for (CFGBlock::const_succ_iterator SI = Pred->succ_begin(),
|
|
|
|
SE = Pred->succ_end();
|
|
|
|
SI != SE; ++SI)
|
|
|
|
if (!*SI)
|
|
|
|
++SV;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (++SV == Pred->succ_size())
|
2012-05-25 10:17:09 +08:00
|
|
|
// All paths from this block lead to the use and don't initialize the
|
|
|
|
// variable.
|
|
|
|
Queue.push_back(Pred);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan the frontier, looking for blocks where the variable was
|
|
|
|
// uninitialized.
|
2018-03-21 08:14:43 +08:00
|
|
|
for (const auto *Block : cfg) {
|
2012-05-25 10:17:09 +08:00
|
|
|
unsigned BlockID = Block->getBlockID();
|
2019-05-24 09:34:22 +08:00
|
|
|
const Stmt *Term = Block->getTerminatorStmt();
|
2012-05-25 10:17:09 +08:00
|
|
|
if (SuccsVisited[BlockID] && SuccsVisited[BlockID] < Block->succ_size() &&
|
|
|
|
Term) {
|
|
|
|
// This block inevitably leads to the use. If we have an edge from here
|
|
|
|
// to a post-dominator block, and the variable is uninitialized on that
|
|
|
|
// edge, we have found a bug.
|
|
|
|
for (CFGBlock::const_succ_iterator I = Block->succ_begin(),
|
|
|
|
E = Block->succ_end(); I != E; ++I) {
|
|
|
|
const CFGBlock *Succ = *I;
|
|
|
|
if (Succ && SuccsVisited[Succ->getBlockID()] >= Succ->succ_size() &&
|
|
|
|
vals.getValue(Block, Succ, vd) == Uninitialized) {
|
|
|
|
// Switch cases are a special case: report the label to the caller
|
|
|
|
// as the 'terminator', not the switch statement itself. Suppress
|
|
|
|
// situations where no label matched: we can't be sure that's
|
|
|
|
// possible.
|
|
|
|
if (isa<SwitchStmt>(Term)) {
|
|
|
|
const Stmt *Label = Succ->getLabel();
|
|
|
|
if (!Label || !isa<SwitchCase>(Label))
|
|
|
|
// Might not be possible.
|
|
|
|
continue;
|
|
|
|
UninitUse::Branch Branch;
|
|
|
|
Branch.Terminator = Label;
|
|
|
|
Branch.Output = 0; // Ignored.
|
|
|
|
Use.addUninitBranch(Branch);
|
|
|
|
} else {
|
|
|
|
UninitUse::Branch Branch;
|
|
|
|
Branch.Terminator = Term;
|
|
|
|
Branch.Output = I - Block->succ_begin();
|
|
|
|
Use.addUninitBranch(Branch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Use;
|
|
|
|
}
|
2011-01-15 10:58:47 +08:00
|
|
|
};
|
2018-03-21 08:14:43 +08:00
|
|
|
|
|
|
|
} // namespace
|
2011-01-15 10:58:47 +08:00
|
|
|
|
2012-05-25 07:45:35 +08:00
|
|
|
void TransferFunctions::reportUse(const Expr *ex, const VarDecl *vd) {
|
|
|
|
Value v = vals[vd];
|
|
|
|
if (isUninitialized(v))
|
2012-11-17 15:18:30 +08:00
|
|
|
handler.handleUseOfUninitVariable(vd, getUninitUse(ex, vd, v));
|
2011-01-15 10:58:47 +08:00
|
|
|
}
|
|
|
|
|
2012-07-17 08:06:14 +08:00
|
|
|
void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS) {
|
2011-01-27 10:01:31 +08:00
|
|
|
// This represents an initialization of the 'element' value.
|
2018-03-21 08:14:43 +08:00
|
|
|
if (const auto *DS = dyn_cast<DeclStmt>(FS->getElement())) {
|
|
|
|
const auto *VD = cast<VarDecl>(DS->getSingleDecl());
|
2012-07-17 08:06:14 +08:00
|
|
|
if (isTrackedVar(VD))
|
|
|
|
vals[VD] = Initialized;
|
2011-01-27 10:01:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[OPENMP]Initial fix PR42392: Improve -Wuninitialized warnings for OpenMP programs.
Summary:
Some OpenMP clauses rely on the values of the variables. If the variable
is not initialized and used in OpenMP clauses that depend on the
variables values, it should be reported that the uninitialized variable
is used in the OpenMP clause expression.
This patch adds initial processing for uninitialized variables in OpenMP
constructs. Currently, it checks for use of the uninitialized variables
in the structured blocks.
Reviewers: NoQ, Szelethus, dcoughlin, xazax.hun, a.sidorin, george.karpenkov, szepet
Subscribers: rnkovacs, guansong, jfb, jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64356
llvm-svn: 365786
2019-07-11 22:54:17 +08:00
|
|
|
void TransferFunctions::VisitOMPExecutableDirective(
|
|
|
|
OMPExecutableDirective *ED) {
|
|
|
|
for (Stmt *S : OMPExecutableDirective::used_clauses_children(ED->clauses())) {
|
|
|
|
assert(S && "Expected non-null used-in-clause child.");
|
|
|
|
Visit(S);
|
|
|
|
}
|
|
|
|
if (!ED->isStandaloneDirective())
|
|
|
|
Visit(ED->getStructuredBlock());
|
|
|
|
}
|
|
|
|
|
2011-01-26 03:13:48 +08:00
|
|
|
void TransferFunctions::VisitBlockExpr(BlockExpr *be) {
|
2011-04-01 06:32:41 +08:00
|
|
|
const BlockDecl *bd = be->getBlockDecl();
|
2014-03-15 02:34:04 +08:00
|
|
|
for (const auto &I : bd->captures()) {
|
|
|
|
const VarDecl *vd = I.getVariable();
|
2011-04-01 06:32:41 +08:00
|
|
|
if (!isTrackedVar(vd))
|
|
|
|
continue;
|
2014-03-15 02:34:04 +08:00
|
|
|
if (I.isByRef()) {
|
2011-04-01 06:32:41 +08:00
|
|
|
vals[vd] = Initialized;
|
|
|
|
continue;
|
|
|
|
}
|
2012-05-25 07:45:35 +08:00
|
|
|
reportUse(be, vd);
|
2011-01-26 03:13:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-03 07:23:04 +08:00
|
|
|
void TransferFunctions::VisitCallExpr(CallExpr *ce) {
|
2012-09-12 13:53:43 +08:00
|
|
|
if (Decl *Callee = ce->getCalleeDecl()) {
|
|
|
|
if (Callee->hasAttr<ReturnsTwiceAttr>()) {
|
|
|
|
// After a call to a function like setjmp or vfork, any variable which is
|
|
|
|
// initialized anywhere within this function may now be initialized. For
|
|
|
|
// now, just assume such a call initializes all variables. FIXME: Only
|
|
|
|
// mark variables as initialized if they have an initializer which is
|
|
|
|
// reachable from here.
|
|
|
|
vals.setAllScratchValues(Initialized);
|
|
|
|
}
|
|
|
|
else if (Callee->hasAttr<AnalyzerNoReturnAttr>()) {
|
|
|
|
// Functions labeled like "analyzer_noreturn" are often used to denote
|
|
|
|
// "panic" functions that in special debug situations can still return,
|
|
|
|
// but for the most part should not be treated as returning. This is a
|
|
|
|
// useful annotation borrowed from the static analyzer that is useful for
|
|
|
|
// suppressing branch-specific false positives when we call one of these
|
|
|
|
// functions but keep pretending the path continues (when in reality the
|
|
|
|
// user doesn't care).
|
|
|
|
vals.setAllScratchValues(Unknown);
|
|
|
|
}
|
|
|
|
}
|
2012-07-03 07:23:04 +08:00
|
|
|
}
|
|
|
|
|
2011-07-19 22:18:48 +08:00
|
|
|
void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) {
|
2012-07-17 08:06:14 +08:00
|
|
|
switch (classification.get(dr)) {
|
|
|
|
case ClassifyRefs::Ignore:
|
|
|
|
break;
|
|
|
|
case ClassifyRefs::Use:
|
|
|
|
reportUse(dr, cast<VarDecl>(dr->getDecl()));
|
|
|
|
break;
|
|
|
|
case ClassifyRefs::Init:
|
|
|
|
vals[cast<VarDecl>(dr->getDecl())] = Initialized;
|
|
|
|
break;
|
|
|
|
case ClassifyRefs::SelfInit:
|
2012-11-17 15:18:30 +08:00
|
|
|
handler.handleSelfInit(cast<VarDecl>(dr->getDecl()));
|
2012-07-17 08:06:14 +08:00
|
|
|
break;
|
2011-01-15 10:58:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-17 08:06:14 +08:00
|
|
|
void TransferFunctions::VisitBinaryOperator(BinaryOperator *BO) {
|
|
|
|
if (BO->getOpcode() == BO_Assign) {
|
|
|
|
FindVarResult Var = findVar(BO->getLHS());
|
|
|
|
if (const VarDecl *VD = Var.getDecl())
|
|
|
|
vals[VD] = Initialized;
|
2011-01-15 10:58:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-17 08:06:14 +08:00
|
|
|
void TransferFunctions::VisitDeclStmt(DeclStmt *DS) {
|
2014-03-15 01:01:24 +08:00
|
|
|
for (auto *DI : DS->decls()) {
|
2018-03-21 08:14:43 +08:00
|
|
|
auto *VD = dyn_cast<VarDecl>(DI);
|
2012-07-17 08:06:14 +08:00
|
|
|
if (VD && isTrackedVar(VD)) {
|
|
|
|
if (getSelfInitExpr(VD)) {
|
|
|
|
// If the initializer consists solely of a reference to itself, we
|
|
|
|
// explicitly mark the variable as uninitialized. This allows code
|
|
|
|
// like the following:
|
|
|
|
//
|
|
|
|
// int x = x;
|
|
|
|
//
|
|
|
|
// to deliberately leave a variable uninitialized. Different analysis
|
|
|
|
// clients can detect this pattern and adjust their reporting
|
|
|
|
// appropriately, but we need to continue to analyze subsequent uses
|
|
|
|
// of the variable.
|
|
|
|
vals[VD] = Uninitialized;
|
|
|
|
} else if (VD->getInit()) {
|
|
|
|
// Treat the new variable as initialized.
|
|
|
|
vals[VD] = Initialized;
|
|
|
|
} else {
|
|
|
|
// No initializer: the variable is now uninitialized. This matters
|
|
|
|
// for cases like:
|
|
|
|
// while (...) {
|
|
|
|
// int n;
|
|
|
|
// use(n);
|
|
|
|
// n = 0;
|
|
|
|
// }
|
|
|
|
// FIXME: Mark the variable as uninitialized whenever its scope is
|
|
|
|
// left, since its scope could be re-entered by a jump over the
|
|
|
|
// declaration.
|
|
|
|
vals[VD] = Uninitialized;
|
2011-07-19 22:18:48 +08:00
|
|
|
}
|
2011-01-26 12:49:43 +08:00
|
|
|
}
|
|
|
|
}
|
2011-01-15 10:58:47 +08:00
|
|
|
}
|
|
|
|
|
2012-09-13 08:21:35 +08:00
|
|
|
void TransferFunctions::VisitObjCMessageExpr(ObjCMessageExpr *ME) {
|
|
|
|
// If the Objective-C message expression is an implicit no-return that
|
|
|
|
// is not modeled in the CFG, set the tracked dataflow values to Unknown.
|
|
|
|
if (objCNoRet.isImplicitNoReturn(ME)) {
|
|
|
|
vals.setAllScratchValues(Unknown);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-15 10:58:47 +08:00
|
|
|
//------------------------------------------------------------------------====//
|
|
|
|
// High-level "driver" logic for uninitialized values analysis.
|
|
|
|
//====------------------------------------------------------------------------//
|
|
|
|
|
2011-01-21 01:37:17 +08:00
|
|
|
static bool runOnBlock(const CFGBlock *block, const CFG &cfg,
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext &ac, CFGBlockValues &vals,
|
2012-07-17 08:06:14 +08:00
|
|
|
const ClassifyRefs &classification,
|
2011-04-05 04:30:58 +08:00
|
|
|
llvm::BitVector &wasAnalyzed,
|
2012-11-17 15:18:30 +08:00
|
|
|
UninitVariablesHandler &handler) {
|
2011-04-05 04:30:58 +08:00
|
|
|
wasAnalyzed[block->getBlockID()] = true;
|
2011-01-15 10:58:47 +08:00
|
|
|
vals.resetScratch();
|
2012-07-19 12:59:05 +08:00
|
|
|
// Merge in values of predecessor blocks.
|
2011-01-15 10:58:47 +08:00
|
|
|
bool isFirst = true;
|
|
|
|
for (CFGBlock::const_pred_iterator I = block->pred_begin(),
|
|
|
|
E = block->pred_end(); I != E; ++I) {
|
2011-09-03 03:39:26 +08:00
|
|
|
const CFGBlock *pred = *I;
|
2014-02-27 08:24:00 +08:00
|
|
|
if (!pred)
|
|
|
|
continue;
|
2011-09-03 03:39:26 +08:00
|
|
|
if (wasAnalyzed[pred->getBlockID()]) {
|
2012-07-19 12:59:05 +08:00
|
|
|
vals.mergeIntoScratch(vals.getValueVector(pred), isFirst);
|
2011-09-03 03:39:26 +08:00
|
|
|
isFirst = false;
|
|
|
|
}
|
2011-01-15 10:58:47 +08:00
|
|
|
}
|
|
|
|
// Apply the transfer function.
|
2012-07-17 08:06:14 +08:00
|
|
|
TransferFunctions tf(vals, cfg, block, ac, classification, handler);
|
2018-03-21 08:14:43 +08:00
|
|
|
for (const auto &I : *block) {
|
|
|
|
if (Optional<CFGStmt> cs = I.getAs<CFGStmt>())
|
|
|
|
tf.Visit(const_cast<Stmt *>(cs->getStmt()));
|
2011-01-15 10:58:47 +08:00
|
|
|
}
|
2011-03-15 12:57:27 +08:00
|
|
|
return vals.updateValueVectorWithScratch(block);
|
2011-01-15 10:58:47 +08:00
|
|
|
}
|
|
|
|
|
2018-03-21 08:14:43 +08:00
|
|
|
namespace {
|
|
|
|
|
2012-11-17 15:18:30 +08:00
|
|
|
/// PruneBlocksHandler is a special UninitVariablesHandler that is used
|
|
|
|
/// to detect when a CFGBlock has any *potential* use of an uninitialized
|
|
|
|
/// variable. It is mainly used to prune out work during the final
|
|
|
|
/// reporting pass.
|
|
|
|
struct PruneBlocksHandler : public UninitVariablesHandler {
|
|
|
|
/// Records if a CFGBlock had a potential use of an uninitialized variable.
|
|
|
|
llvm::BitVector hadUse;
|
|
|
|
|
|
|
|
/// Records if any CFGBlock had a potential use of an uninitialized variable.
|
2018-03-21 08:14:43 +08:00
|
|
|
bool hadAnyUse = false;
|
2012-11-17 15:18:30 +08:00
|
|
|
|
|
|
|
/// The current block to scribble use information.
|
2018-03-21 08:14:43 +08:00
|
|
|
unsigned currentBlock = 0;
|
|
|
|
|
|
|
|
PruneBlocksHandler(unsigned numBlocks) : hadUse(numBlocks, false) {}
|
|
|
|
|
|
|
|
~PruneBlocksHandler() override = default;
|
2012-11-17 15:18:30 +08:00
|
|
|
|
2014-03-14 14:02:07 +08:00
|
|
|
void handleUseOfUninitVariable(const VarDecl *vd,
|
|
|
|
const UninitUse &use) override {
|
2012-11-17 15:18:30 +08:00
|
|
|
hadUse[currentBlock] = true;
|
|
|
|
hadAnyUse = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Called when the uninitialized variable analysis detects the
|
|
|
|
/// idiom 'int x = x'. All other uses of 'x' within the initializer
|
|
|
|
/// are handled by handleUseOfUninitVariable.
|
2014-03-14 14:02:07 +08:00
|
|
|
void handleSelfInit(const VarDecl *vd) override {
|
2012-11-17 15:18:30 +08:00
|
|
|
hadUse[currentBlock] = true;
|
|
|
|
hadAnyUse = true;
|
|
|
|
}
|
|
|
|
};
|
2018-03-21 08:14:43 +08:00
|
|
|
|
|
|
|
} // namespace
|
2012-11-17 15:18:30 +08:00
|
|
|
|
2011-07-07 00:21:37 +08:00
|
|
|
void clang::runUninitializedVariablesAnalysis(
|
|
|
|
const DeclContext &dc,
|
|
|
|
const CFG &cfg,
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext &ac,
|
2011-07-07 00:21:37 +08:00
|
|
|
UninitVariablesHandler &handler,
|
|
|
|
UninitVariablesAnalysisStats &stats) {
|
2011-01-15 10:58:47 +08:00
|
|
|
CFGBlockValues vals(cfg);
|
|
|
|
vals.computeSetOfDeclarations(dc);
|
|
|
|
if (vals.hasNoDeclarations())
|
|
|
|
return;
|
2011-04-05 07:29:12 +08:00
|
|
|
|
2011-07-07 00:21:37 +08:00
|
|
|
stats.NumVariablesAnalyzed = vals.getNumEntries();
|
|
|
|
|
2012-07-17 08:06:14 +08:00
|
|
|
// Precompute which expressions are uses and which are initializations.
|
|
|
|
ClassifyRefs classification(ac);
|
|
|
|
cfg.VisitBlockStmts(classification);
|
|
|
|
|
2011-04-05 07:29:12 +08:00
|
|
|
// Mark all variables uninitialized at the entry.
|
|
|
|
const CFGBlock &entry = cfg.getEntry();
|
2012-07-19 12:59:05 +08:00
|
|
|
ValueVector &vec = vals.getValueVector(&entry);
|
|
|
|
const unsigned n = vals.getNumEntries();
|
2018-03-21 08:14:43 +08:00
|
|
|
for (unsigned j = 0; j < n; ++j) {
|
2012-07-19 12:59:05 +08:00
|
|
|
vec[j] = Uninitialized;
|
2011-04-05 07:29:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Proceed with the workist.
|
2014-09-23 16:34:41 +08:00
|
|
|
DataflowWorklist worklist(cfg, *ac.getAnalysis<PostOrderCFGView>());
|
2011-03-15 12:57:32 +08:00
|
|
|
llvm::BitVector previouslyVisited(cfg.getNumBlockIDs());
|
2011-01-15 10:58:47 +08:00
|
|
|
worklist.enqueueSuccessors(&cfg.getEntry());
|
2011-04-05 04:30:58 +08:00
|
|
|
llvm::BitVector wasAnalyzed(cfg.getNumBlockIDs(), false);
|
2011-09-03 03:39:26 +08:00
|
|
|
wasAnalyzed[cfg.getEntry().getBlockID()] = true;
|
2012-11-17 15:18:30 +08:00
|
|
|
PruneBlocksHandler PBH(cfg.getNumBlockIDs());
|
2011-01-15 10:58:47 +08:00
|
|
|
|
|
|
|
while (const CFGBlock *block = worklist.dequeue()) {
|
2012-11-17 15:18:30 +08:00
|
|
|
PBH.currentBlock = block->getBlockID();
|
|
|
|
|
2011-01-15 10:58:47 +08:00
|
|
|
// Did the block change?
|
2012-07-17 08:06:14 +08:00
|
|
|
bool changed = runOnBlock(block, cfg, ac, vals,
|
2012-11-17 15:18:30 +08:00
|
|
|
classification, wasAnalyzed, PBH);
|
2011-07-07 00:21:37 +08:00
|
|
|
++stats.NumBlockVisits;
|
2011-01-15 10:58:47 +08:00
|
|
|
if (changed || !previouslyVisited[block->getBlockID()])
|
2018-07-31 03:24:48 +08:00
|
|
|
worklist.enqueueSuccessors(block);
|
2011-01-15 10:58:47 +08:00
|
|
|
previouslyVisited[block->getBlockID()] = true;
|
|
|
|
}
|
2012-11-17 15:18:30 +08:00
|
|
|
|
|
|
|
if (!PBH.hadAnyUse)
|
|
|
|
return;
|
|
|
|
|
2013-01-11 19:37:08 +08:00
|
|
|
// Run through the blocks one more time, and report uninitialized variables.
|
2018-03-21 08:14:43 +08:00
|
|
|
for (const auto *block : cfg)
|
2012-11-17 15:18:30 +08:00
|
|
|
if (PBH.hadUse[block->getBlockID()]) {
|
|
|
|
runOnBlock(block, cfg, ac, vals, classification, wasAnalyzed, handler);
|
2011-07-07 00:21:37 +08:00
|
|
|
++stats.NumBlockVisits;
|
|
|
|
}
|
2011-01-15 10:58:47 +08:00
|
|
|
}
|
|
|
|
|
2018-03-21 08:14:43 +08:00
|
|
|
UninitVariablesHandler::~UninitVariablesHandler() = default;
|