1 //===- DynamicType.h - Dynamic type related APIs ----------------*- 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 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 
15 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_DYNAMICTYPE_H
16 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_DYNAMICTYPE_H
17 
18 #include "clang/AST/Type.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicCastInfo.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeInfo.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
25 #include "llvm/ADT/ImmutableMap.h"
26 
27 namespace clang {
28 namespace ento {
29 
30 /// Get dynamic type information for the region \p MR.
31 DynamicTypeInfo getDynamicTypeInfo(ProgramStateRef State, const MemRegion *MR);
32 
33 /// Get raw dynamic type information for the region \p MR.
34 /// It might return null.
35 const DynamicTypeInfo *getRawDynamicTypeInfo(ProgramStateRef State,
36                                              const MemRegion *MR);
37 
38 /// Get dynamic type information stored in a class object represented by \p Sym.
39 DynamicTypeInfo getClassObjectDynamicTypeInfo(ProgramStateRef State,
40                                               SymbolRef Sym);
41 
42 /// Get dynamic cast information from \p CastFromTy to \p CastToTy of \p MR.
43 const DynamicCastInfo *getDynamicCastInfo(ProgramStateRef State,
44                                           const MemRegion *MR,
45                                           QualType CastFromTy,
46                                           QualType CastToTy);
47 
48 /// Set dynamic type information of the region; return the new state.
49 ProgramStateRef setDynamicTypeInfo(ProgramStateRef State, const MemRegion *MR,
50                                    DynamicTypeInfo NewTy);
51 
52 /// Set dynamic type information of the region; return the new state.
53 ProgramStateRef setDynamicTypeInfo(ProgramStateRef State, const MemRegion *MR,
54                                    QualType NewTy, bool CanBeSubClassed = true);
55 
56 /// Set constraint on a type contained in a class object; return the new state.
57 ProgramStateRef setClassObjectDynamicTypeInfo(ProgramStateRef State,
58                                               SymbolRef Sym,
59                                               DynamicTypeInfo NewTy);
60 
61 /// Set constraint on a type contained in a class object; return the new state.
62 ProgramStateRef setClassObjectDynamicTypeInfo(ProgramStateRef State,
63                                               SymbolRef Sym, QualType NewTy,
64                                               bool CanBeSubClassed = true);
65 
66 /// Set dynamic type and cast information of the region; return the new state.
67 ProgramStateRef setDynamicTypeAndCastInfo(ProgramStateRef State,
68                                           const MemRegion *MR,
69                                           QualType CastFromTy,
70                                           QualType CastToTy,
71                                           bool IsCastSucceeds);
72 
73 /// Removes the dead type informations from \p State.
74 ProgramStateRef removeDeadTypes(ProgramStateRef State, SymbolReaper &SR);
75 
76 /// Removes the dead cast informations from \p State.
77 ProgramStateRef removeDeadCasts(ProgramStateRef State, SymbolReaper &SR);
78 
79 /// Removes the dead Class object type informations from \p State.
80 ProgramStateRef removeDeadClassObjectTypes(ProgramStateRef State,
81                                            SymbolReaper &SR);
82 
83 void printDynamicTypeInfoJson(raw_ostream &Out, ProgramStateRef State,
84                               const char *NL = "\n", unsigned int Space = 0,
85                               bool IsDot = false);
86 
87 } // namespace ento
88 } // namespace clang
89 
90 #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_DYNAMICTYPE_H
91