Home | History | Annotate | Download | only in Core
      1 //==- DynamicTypeMap.cpp - Dynamic Type Info related APIs ----------*- 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 APIs that track and query dynamic type information. This
     11 //  information can be used to devirtualize calls during the symbolic exection
     12 //  or do type checking.
     13 //
     14 //===----------------------------------------------------------------------===//
     15 
     16 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h"
     17 
     18 namespace clang {
     19 namespace ento {
     20 
     21 DynamicTypeInfo getDynamicTypeInfo(ProgramStateRef State,
     22                                    const MemRegion *Reg) {
     23   Reg = Reg->StripCasts();
     24 
     25   // Look up the dynamic type in the GDM.
     26   const DynamicTypeInfo *GDMType = State->get<DynamicTypeMap>(Reg);
     27   if (GDMType)
     28     return *GDMType;
     29 
     30   // Otherwise, fall back to what we know about the region.
     31   if (const TypedRegion *TR = dyn_cast<TypedRegion>(Reg))
     32     return DynamicTypeInfo(TR->getLocationType(), /*CanBeSubclass=*/false);
     33 
     34   if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg)) {
     35     SymbolRef Sym = SR->getSymbol();
     36     return DynamicTypeInfo(Sym->getType());
     37   }
     38 
     39   return DynamicTypeInfo();
     40 }
     41 
     42 ProgramStateRef setDynamicTypeInfo(ProgramStateRef State, const MemRegion *Reg,
     43                                    DynamicTypeInfo NewTy) {
     44   Reg = Reg->StripCasts();
     45   ProgramStateRef NewState = State->set<DynamicTypeMap>(Reg, NewTy);
     46   assert(NewState);
     47   return NewState;
     48 }
     49 
     50 } // namespace ento
     51 } // namespace clang
     52