Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Core/LoopWidening.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- LoopWidening.cpp - Widen loops -------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
///
9
/// This file contains functions which are used to widen loops. A loop may be
10
/// widened to approximate the exit state(s), without analyzing every
11
/// iteration. The widening is done by invalidating anything which might be
12
/// modified by the body of the loop.
13
///
14
//===----------------------------------------------------------------------===//
15
16
#include "clang/AST/AST.h"
17
#include "clang/ASTMatchers/ASTMatchFinder.h"
18
#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
19
#include "clang/StaticAnalyzer/Core/PathSensitive/LoopWidening.h"
20
21
using namespace clang;
22
using namespace ento;
23
using namespace clang::ast_matchers;
24
25
const auto MatchRef = "matchref";
26
27
/// Return the loops condition Stmt or NULL if LoopStmt is not a loop
28
48
static const Expr *getLoopCondition(const Stmt *LoopStmt) {
29
48
  switch (LoopStmt->getStmtClass()) {
30
48
  default:
31
0
    return nullptr;
32
48
  case Stmt::ForStmtClass:
33
25
    return cast<ForStmt>(LoopStmt)->getCond();
34
48
  case Stmt::WhileStmtClass:
35
19
    return cast<WhileStmt>(LoopStmt)->getCond();
36
48
  case Stmt::DoStmtClass:
37
4
    return cast<DoStmt>(LoopStmt)->getCond();
38
48
  }
39
48
}
40
41
namespace clang {
42
namespace ento {
43
44
ProgramStateRef getWidenedLoopState(ProgramStateRef PrevState,
45
                                    const LocationContext *LCtx,
46
48
                                    unsigned BlockCount, const Stmt *LoopStmt) {
47
48
48
48
  assert(isa<ForStmt>(LoopStmt) || isa<WhileStmt>(LoopStmt) ||
49
48
         isa<DoStmt>(LoopStmt));
50
48
51
48
  // Invalidate values in the current state.
52
48
  // TODO Make this more conservative by only invalidating values that might
53
48
  //      be modified by the body of the loop.
54
48
  // TODO Nested loops are currently widened as a result of the invalidation
55
48
  //      being so inprecise. When the invalidation is improved, the handling
56
48
  //      of nested loops will also need to be improved.
57
48
  ASTContext &ASTCtx = LCtx->getAnalysisDeclContext()->getASTContext();
58
48
  const StackFrameContext *STC = LCtx->getStackFrame();
59
48
  MemRegionManager &MRMgr = PrevState->getStateManager().getRegionManager();
60
48
  const MemRegion *Regions[] = {MRMgr.getStackLocalsRegion(STC),
61
48
                                MRMgr.getStackArgumentsRegion(STC),
62
48
                                MRMgr.getGlobalsRegion()};
63
48
  RegionAndSymbolInvalidationTraits ITraits;
64
144
  for (auto *Region : Regions) {
65
144
    ITraits.setTrait(Region,
66
144
                     RegionAndSymbolInvalidationTraits::TK_EntireMemSpace);
67
144
  }
68
48
69
48
  // References should not be invalidated.
70
48
  auto Matches = match(findAll(stmt(hasDescendant(varDecl(hasType(referenceType())).bind(MatchRef)))),
71
48
                       *LCtx->getDecl()->getBody(), ASTCtx);
72
48
  for (BoundNodes Match : Matches) {
73
2
    const VarDecl *VD = Match.getNodeAs<VarDecl>(MatchRef);
74
2
    assert(VD);
75
2
    const VarRegion *VarMem = MRMgr.getVarRegion(VD, LCtx);
76
2
    ITraits.setTrait(VarMem,
77
2
                     RegionAndSymbolInvalidationTraits::TK_PreserveContents);
78
2
  }
79
48
80
48
81
48
  // 'this' pointer is not an lvalue, we should not invalidate it. If the loop
82
48
  // is located in a method, constructor or destructor, the value of 'this'
83
48
  // pointer should remain unchanged.  Ignore static methods, since they do not
84
48
  // have 'this' pointers.
85
48
  const CXXMethodDecl *CXXMD = dyn_cast<CXXMethodDecl>(STC->getDecl());
86
48
  if (CXXMD && 
!CXXMD->isStatic()5
) {
87
5
    const CXXThisRegion *ThisR =
88
5
        MRMgr.getCXXThisRegion(CXXMD->getThisType(), STC);
89
5
    ITraits.setTrait(ThisR,
90
5
                     RegionAndSymbolInvalidationTraits::TK_PreserveContents);
91
5
  }
92
48
93
48
  return PrevState->invalidateRegions(Regions, getLoopCondition(LoopStmt),
94
48
                                      BlockCount, LCtx, true, nullptr, nullptr,
95
48
                                      &ITraits);
96
48
}
97
98
} // end namespace ento
99
} // end namespace clang