Coverage Report

Created: 2018-11-16 02:38

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/StaticAnalyzer/Checkers/SValExplainer.h
Line
Count
Source (jump to first uncovered line)
1
//== SValExplainer.h - Symbolic value explainer -----------------*- C++ -*--==//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
//  This file defines SValExplainer, a class for pretty-printing a
11
//  human-readable description of a symbolic value. For example,
12
//  "reg_$0<x>" is turned into "initial value of variable 'x'".
13
//
14
//===----------------------------------------------------------------------===//
15
16
#ifndef LLVM_CLANG_STATICANALYZER_CHECKERS_SVALEXPLAINER_H
17
#define LLVM_CLANG_STATICANALYZER_CHECKERS_SVALEXPLAINER_H
18
19
#include "clang/AST/DeclCXX.h"
20
#include "clang/StaticAnalyzer/Core/PathSensitive/SValVisitor.h"
21
22
namespace clang {
23
24
namespace ento {
25
26
class SValExplainer : public FullSValVisitor<SValExplainer, std::string> {
27
private:
28
  ASTContext &ACtx;
29
30
11
  std::string printStmt(const Stmt *S) {
31
11
    std::string Str;
32
11
    llvm::raw_string_ostream OS(Str);
33
11
    S->printPretty(OS, nullptr, PrintingPolicy(ACtx.getLangOpts()));
34
11
    return OS.str();
35
11
  }
36
37
15
  bool isThisObject(const SymbolicRegion *R) {
38
15
    if (auto S = dyn_cast<SymbolRegionValue>(R->getSymbol()))
39
11
      if (isa<CXXThisRegion>(S->getRegion()))
40
3
        return true;
41
12
    return false;
42
12
  }
43
44
public:
45
45
  SValExplainer(ASTContext &Ctx) : ACtx(Ctx) {}
46
47
0
  std::string VisitUnknownVal(UnknownVal V) {
48
0
    return "unknown value";
49
0
  }
50
51
1
  std::string VisitUndefinedVal(UndefinedVal V) {
52
1
    return "undefined value";
53
1
  }
54
55
23
  std::string VisitLocMemRegionVal(loc::MemRegionVal V) {
56
23
    const MemRegion *R = V.getRegion();
57
23
    // Avoid the weird "pointer to pointee of ...".
58
23
    if (auto SR = dyn_cast<SymbolicRegion>(R)) {
59
8
      // However, "pointer to 'this' object" is fine.
60
8
      if (!isThisObject(SR))
61
7
        return Visit(SR->getSymbol());
62
16
    }
63
16
    return "pointer to " + Visit(R);
64
16
  }
65
66
2
  std::string VisitLocConcreteInt(loc::ConcreteInt V) {
67
2
    llvm::APSInt I = V.getValue();
68
2
    std::string Str;
69
2
    llvm::raw_string_ostream OS(Str);
70
2
    OS << "concrete memory address '" << I << "'";
71
2
    return OS.str();
72
2
  }
73
74
14
  std::string VisitNonLocSymbolVal(nonloc::SymbolVal V) {
75
14
    return Visit(V.getSymbol());
76
14
  }
77
78
4
  std::string VisitNonLocConcreteInt(nonloc::ConcreteInt V) {
79
4
    llvm::APSInt I = V.getValue();
80
4
    std::string Str;
81
4
    llvm::raw_string_ostream OS(Str);
82
4
    OS << (I.isSigned() ? "signed " : 
"unsigned "0
) << I.getBitWidth()
83
4
       << "-bit integer '" << I << "'";
84
4
    return OS.str();
85
4
  }
86
87
2
  std::string VisitNonLocLazyCompoundVal(nonloc::LazyCompoundVal V) {
88
2
    return "lazily frozen compound value of " + Visit(V.getRegion());
89
2
  }
90
91
18
  std::string VisitSymbolRegionValue(const SymbolRegionValue *S) {
92
18
    const MemRegion *R = S->getRegion();
93
18
    // Special handling for argument values.
94
18
    if (auto V = dyn_cast<VarRegion>(R))
95
14
      if (auto D = dyn_cast<ParmVarDecl>(V->getDecl()))
96
13
        return "argument '" + D->getQualifiedNameAsString() + "'";
97
5
    return "initial value of " + Visit(R);
98
5
  }
99
100
7
  std::string VisitSymbolConjured(const SymbolConjured *S) {
101
7
    return "symbol of type '" + S->getType().getAsString() +
102
7
           "' conjured at statement '" + printStmt(S->getStmt()) + "'";
103
7
  }
104
105
3
  std::string VisitSymbolDerived(const SymbolDerived *S) {
106
3
    return "value derived from (" + Visit(S->getParentSymbol()) +
107
3
           ") for " + Visit(S->getRegion());
108
3
  }
109
110
1
  std::string VisitSymbolExtent(const SymbolExtent *S) {
111
1
    return "extent of " + Visit(S->getRegion());
112
1
  }
113
114
1
  std::string VisitSymbolMetadata(const SymbolMetadata *S) {
115
1
    return "metadata of type '" + S->getType().getAsString() + "' tied to " +
116
1
           Visit(S->getRegion());
117
1
  }
118
119
2
  std::string VisitSymIntExpr(const SymIntExpr *S) {
120
2
    std::string Str;
121
2
    llvm::raw_string_ostream OS(Str);
122
2
    OS << "(" << Visit(S->getLHS()) << ") "
123
2
       << std::string(BinaryOperator::getOpcodeStr(S->getOpcode())) << " "
124
2
       << S->getRHS();
125
2
    return OS.str();
126
2
  }
127
128
  // TODO: IntSymExpr doesn't appear in practice.
129
  // Add the relevant code once it does.
130
131
1
  std::string VisitSymSymExpr(const SymSymExpr *S) {
132
1
    return "(" + Visit(S->getLHS()) + ") " +
133
1
           std::string(BinaryOperator::getOpcodeStr(S->getOpcode())) +
134
1
           " (" + Visit(S->getRHS()) + ")";
135
1
  }
136
137
  // TODO: SymbolCast doesn't appear in practice.
138
  // Add the relevant code once it does.
139
140
7
  std::string VisitSymbolicRegion(const SymbolicRegion *R) {
141
7
    // Explain 'this' object here.
142
7
    // TODO: Explain CXXThisRegion itself, find a way to test it.
143
7
    if (isThisObject(R))
144
2
      return "'this' object";
145
5
    // Objective-C objects are not normal symbolic regions. At least,
146
5
    // they're always on the heap.
147
5
    if (R->getSymbol()->getType()
148
5
            .getCanonicalType()->getAs<ObjCObjectPointerType>())
149
2
      return "object at " + Visit(R->getSymbol());
150
3
    // Other heap-based symbolic regions are also special.
151
3
    if (isa<HeapSpaceRegion>(R->getMemorySpace()))
152
1
      return "heap segment that starts at " + Visit(R->getSymbol());
153
2
    return "pointee of " + Visit(R->getSymbol());
154
2
  }
155
156
1
  std::string VisitAllocaRegion(const AllocaRegion *R) {
157
1
    return "region allocated by '" + printStmt(R->getExpr()) + "'";
158
1
  }
159
160
0
  std::string VisitCompoundLiteralRegion(const CompoundLiteralRegion *R) {
161
0
    return "compound literal " + printStmt(R->getLiteralExpr());
162
0
  }
163
164
1
  std::string VisitStringRegion(const StringRegion *R) {
165
1
    return "string literal " + R->getString();
166
1
  }
167
168
6
  std::string VisitElementRegion(const ElementRegion *R) {
169
6
    std::string Str;
170
6
    llvm::raw_string_ostream OS(Str);
171
6
    OS << "element of type '" << R->getElementType().getAsString()
172
6
       << "' with index ";
173
6
    // For concrete index: omit type of the index integer.
174
6
    if (auto I = R->getIndex().getAs<nonloc::ConcreteInt>())
175
5
      OS << I->getValue();
176
1
    else
177
1
      OS << "'" << Visit(R->getIndex()) << "'";
178
6
    OS << " of " + Visit(R->getSuperRegion());
179
6
    return OS.str();
180
6
  }
181
182
16
  std::string VisitVarRegion(const VarRegion *R) {
183
16
    const VarDecl *VD = R->getDecl();
184
16
    std::string Name = VD->getQualifiedNameAsString();
185
16
    if (isa<ParmVarDecl>(VD))
186
6
      return "parameter '" + Name + "'";
187
10
    else if (VD->hasAttr<BlocksAttr>())
188
2
      return "block variable '" + Name + "'";
189
8
    else if (VD->hasLocalStorage())
190
1
      return "local variable '" + Name + "'";
191
7
    else if (VD->isStaticLocal())
192
1
      return "static local variable '" + Name + "'";
193
6
    else if (VD->hasGlobalStorage())
194
6
      return "global variable '" + Name + "'";
195
6
    else
196
6
      llvm_unreachable("A variable is either local or global");
197
16
  }
198
199
2
  std::string VisitObjCIvarRegion(const ObjCIvarRegion *R) {
200
2
    return "instance variable '" + R->getDecl()->getNameAsString() + "' of " +
201
2
           Visit(R->getSuperRegion());
202
2
  }
203
204
6
  std::string VisitFieldRegion(const FieldRegion *R) {
205
6
    return "field '" + R->getDecl()->getNameAsString() + "' of " +
206
6
           Visit(R->getSuperRegion());
207
6
  }
208
209
3
  std::string VisitCXXTempObjectRegion(const CXXTempObjectRegion *R) {
210
3
    return "temporary object constructed at statement '" +
211
3
           printStmt(R->getExpr()) + "'";
212
3
  }
213
214
1
  std::string VisitCXXBaseObjectRegion(const CXXBaseObjectRegion *R) {
215
1
    return "base object '" + R->getDecl()->getQualifiedNameAsString() +
216
1
           "' inside " + Visit(R->getSuperRegion());
217
1
  }
218
219
0
  std::string VisitSVal(SVal V) {
220
0
    std::string Str;
221
0
    llvm::raw_string_ostream OS(Str);
222
0
    OS << V;
223
0
    return "a value unsupported by the explainer: (" +
224
0
           std::string(OS.str()) + ")";
225
0
  }
226
227
0
  std::string VisitSymExpr(SymbolRef S) {
228
0
    std::string Str;
229
0
    llvm::raw_string_ostream OS(Str);
230
0
    S->dumpToStream(OS);
231
0
    return "a symbolic expression unsupported by the explainer: (" +
232
0
           std::string(OS.str()) + ")";
233
0
  }
234
235
0
  std::string VisitMemRegion(const MemRegion *R) {
236
0
    std::string Str;
237
0
    llvm::raw_string_ostream OS(Str);
238
0
    OS << R;
239
0
    return "a memory region unsupported by the explainer (" +
240
0
           std::string(OS.str()) + ")";
241
0
  }
242
};
243
244
} // end namespace ento
245
246
} // end namespace clang
247
248
#endif