1 //===- DynamicTypeMap.cpp - Dynamic Type Info related APIs ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines APIs that track and query dynamic type information. This
11 // information can be used to devirtualize calls during the symbolic execution
12 // or do type checking.
14 //===----------------------------------------------------------------------===//
16 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h"
17 #include "clang/Basic/LLVM.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h"
21 #include "llvm/Support/Casting.h"
22 #include "llvm/Support/raw_ostream.h"
28 DynamicTypeInfo getDynamicTypeInfo(ProgramStateRef State,
29 const MemRegion *Reg) {
30 Reg = Reg->StripCasts();
32 // Look up the dynamic type in the GDM.
33 const DynamicTypeInfo *GDMType = State->get<DynamicTypeMap>(Reg);
37 // Otherwise, fall back to what we know about the region.
38 if (const auto *TR = dyn_cast<TypedRegion>(Reg))
39 return DynamicTypeInfo(TR->getLocationType(), /*CanBeSubclass=*/false);
41 if (const auto *SR = dyn_cast<SymbolicRegion>(Reg)) {
42 SymbolRef Sym = SR->getSymbol();
43 return DynamicTypeInfo(Sym->getType());
49 ProgramStateRef setDynamicTypeInfo(ProgramStateRef State, const MemRegion *Reg,
50 DynamicTypeInfo NewTy) {
51 Reg = Reg->StripCasts();
52 ProgramStateRef NewState = State->set<DynamicTypeMap>(Reg, NewTy);
57 void printDynamicTypeInfo(ProgramStateRef State, raw_ostream &Out,
58 const char *NL, const char *Sep) {
60 for (const auto &I : State->get<DynamicTypeMap>()) {
62 Out << NL << "Dynamic types of regions:" << NL;
65 const MemRegion *MR = I.first;
66 const DynamicTypeInfo &DTI = I.second;
69 Out << DTI.getType()->getPointeeType().getAsString();
70 if (DTI.canBeASubClass()) {
71 Out << " (or its subclass)";
74 Out << "Invalid type info";