clang  9.0.0svn
DynamicTypeMap.cpp
Go to the documentation of this file.
1 //===- DynamicTypeMap.cpp - Dynamic Type Info related APIs ----------------===//
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 defines APIs that track and query dynamic type information. This
10 // information can be used to devirtualize calls during the symbolic execution
11 // or do type checking.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "clang/Basic/LLVM.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <cassert>
23 
24 namespace clang {
25 namespace ento {
26 
28  const MemRegion *Reg) {
29  Reg = Reg->StripCasts();
30 
31  // Look up the dynamic type in the GDM.
32  const DynamicTypeInfo *GDMType = State->get<DynamicTypeMap>(Reg);
33  if (GDMType)
34  return *GDMType;
35 
36  // Otherwise, fall back to what we know about the region.
37  if (const auto *TR = dyn_cast<TypedRegion>(Reg))
38  return DynamicTypeInfo(TR->getLocationType(), /*CanBeSubclass=*/false);
39 
40  if (const auto *SR = dyn_cast<SymbolicRegion>(Reg)) {
41  SymbolRef Sym = SR->getSymbol();
42  return DynamicTypeInfo(Sym->getType());
43  }
44 
45  return {};
46 }
47 
49  DynamicTypeInfo NewTy) {
50  Reg = Reg->StripCasts();
51  ProgramStateRef NewState = State->set<DynamicTypeMap>(Reg, NewTy);
52  assert(NewState);
53  return NewState;
54 }
55 
57  const char *NL, const char *Sep) {
58  bool First = true;
59  for (const auto &I : State->get<DynamicTypeMap>()) {
60  if (First) {
61  Out << NL << "Dynamic types of regions:" << NL;
62  First = false;
63  }
64  const MemRegion *MR = I.first;
65  const DynamicTypeInfo &DTI = I.second;
66  Out << MR << " : ";
67  if (DTI.isValid()) {
68  Out << DTI.getType()->getPointeeType().getAsString();
69  if (DTI.canBeASubClass()) {
70  Out << " (or its subclass)";
71  }
72  } else {
73  Out << "Invalid type info";
74  }
75  Out << NL;
76  }
77 }
78 
80  static int index = 0;
81  return &index;
82 }
83 
84 } // namespace ento
85 } // namespace clang
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:94
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
Definition: Type.cpp:504
Symbolic value.
Definition: SymExpr.h:29
bool isValid() const
Return false if no dynamic type info is available.
The GDM component containing the dynamic type info.
LineState State
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
QualType getType() const
Returns the currently inferred upper bound on the runtime type.
virtual QualType getType() const =0
bool canBeASubClass() const
Returns false if the type information is precise (the type T is the only type in the lattice)...
Stores the currently inferred strictest bound on the runtime type of a region in a given state along ...
ProgramStateRef setDynamicTypeInfo(ProgramStateRef State, const MemRegion *Reg, DynamicTypeInfo NewTy)
Set dynamic type information of the region; return the new state.
Dataflow Directional Tag Classes.
DynamicTypeInfo getDynamicTypeInfo(ProgramStateRef State, const MemRegion *Reg)
Get dynamic type information for a region.
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Definition: Type.h:970
const MemRegion * StripCasts(bool StripBaseAndDerivedCasts=true) const
Definition: MemRegion.cpp:1193
void printDynamicTypeInfo(ProgramStateRef State, raw_ostream &Out, const char *NL, const char *Sep)