| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_CHECKERCONTEXT_H |
| 15 | #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_CHECKERCONTEXT_H |
| 16 | |
| 17 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
| 18 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
| 19 | |
| 20 | namespace clang { |
| 21 | namespace ento { |
| 22 | |
| 23 | class CheckerContext { |
| 24 | ExprEngine &Eng; |
| 25 | |
| 26 | ExplodedNode *Pred; |
| 27 | |
| 28 | |
| 29 | |
| 30 | bool Changed; |
| 31 | |
| 32 | const ProgramPoint Location; |
| 33 | NodeBuilder &NB; |
| 34 | |
| 35 | public: |
| 36 | |
| 37 | |
| 38 | const bool wasInlined; |
| 39 | |
| 40 | CheckerContext(NodeBuilder &builder, |
| 41 | ExprEngine &eng, |
| 42 | ExplodedNode *pred, |
| 43 | const ProgramPoint &loc, |
| 44 | bool wasInlined = false) |
| 45 | : Eng(eng), |
| 46 | Pred(pred), |
| 47 | Changed(false), |
| 48 | Location(loc), |
| 49 | NB(builder), |
| 50 | wasInlined(wasInlined) { |
| 51 | (0) . __assert_fail ("Pred->getState() && \"We should not call the checkers on an empty state.\"", "/home/seafit/code_projects/clang_source/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h", 52, __PRETTY_FUNCTION__))" file_link="../../../../../../include/assert.h.html#88" macro="true">assert(Pred->getState() && |
| 52 | (0) . __assert_fail ("Pred->getState() && \"We should not call the checkers on an empty state.\"", "/home/seafit/code_projects/clang_source/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h", 52, __PRETTY_FUNCTION__))" file_link="../../../../../../include/assert.h.html#88" macro="true"> "We should not call the checkers on an empty state."); |
| 53 | } |
| 54 | |
| 55 | AnalysisManager &getAnalysisManager() { |
| 56 | return Eng.getAnalysisManager(); |
| 57 | } |
| 58 | |
| 59 | ConstraintManager &getConstraintManager() { |
| 60 | return Eng.getConstraintManager(); |
| 61 | } |
| 62 | |
| 63 | StoreManager &getStoreManager() { |
| 64 | return Eng.getStoreManager(); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | ExplodedNode *getPredecessor() { return Pred; } |
| 71 | const ProgramStateRef &getState() const { return Pred->getState(); } |
| 72 | |
| 73 | |
| 74 | |
| 75 | bool isDifferent() { return Changed; } |
| 76 | |
| 77 | |
| 78 | |
| 79 | unsigned blockCount() const { |
| 80 | return NB.getContext().blockCount(); |
| 81 | } |
| 82 | |
| 83 | ASTContext &getASTContext() { |
| 84 | return Eng.getContext(); |
| 85 | } |
| 86 | |
| 87 | const LangOptions &getLangOpts() const { |
| 88 | return Eng.getContext().getLangOpts(); |
| 89 | } |
| 90 | |
| 91 | const LocationContext *getLocationContext() const { |
| 92 | return Pred->getLocationContext(); |
| 93 | } |
| 94 | |
| 95 | const StackFrameContext *getStackFrame() const { |
| 96 | return Pred->getStackFrame(); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | bool inTopFrame() const { return getLocationContext()->inTopFrame(); } |
| 101 | |
| 102 | BugReporter &getBugReporter() { |
| 103 | return Eng.getBugReporter(); |
| 104 | } |
| 105 | |
| 106 | SourceManager &getSourceManager() { |
| 107 | return getBugReporter().getSourceManager(); |
| 108 | } |
| 109 | |
| 110 | SValBuilder &getSValBuilder() { |
| 111 | return Eng.getSValBuilder(); |
| 112 | } |
| 113 | |
| 114 | SymbolManager &getSymbolManager() { |
| 115 | return getSValBuilder().getSymbolManager(); |
| 116 | } |
| 117 | |
| 118 | ProgramStateManager &getStateManager() { |
| 119 | return Eng.getStateManager(); |
| 120 | } |
| 121 | |
| 122 | AnalysisDeclContext *getCurrentAnalysisDeclContext() const { |
| 123 | return Pred->getLocationContext()->getAnalysisDeclContext(); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | unsigned getBlockID() const { |
| 128 | return NB.getContext().getBlock()->getBlockID(); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | static const MemRegion *getLocationRegionIfPostStore(const ExplodedNode *N) { |
| 137 | ProgramPoint L = N->getLocation(); |
| 138 | if (Optional<PostStore> PSL = L.getAs<PostStore>()) |
| 139 | return reinterpret_cast<const MemRegion*>(PSL->getLocationValue()); |
| 140 | return nullptr; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | SVal getSVal(const Stmt *S) const { |
| 145 | return Pred->getSVal(S); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | |
| 150 | bool isGreaterOrEqual(const Expr *E, unsigned long long Val); |
| 151 | |
| 152 | |
| 153 | bool isNegative(const Expr *E); |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | ExplodedNode *addTransition(ProgramStateRef State = nullptr, |
| 165 | const ProgramPointTag *Tag = nullptr) { |
| 166 | return addTransitionImpl(State ? State : getState(), false, nullptr, Tag); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | ExplodedNode *addTransition(ProgramStateRef State, |
| 177 | ExplodedNode *Pred, |
| 178 | const ProgramPointTag *Tag = nullptr) { |
| 179 | return addTransitionImpl(State, false, Pred, Tag); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | ExplodedNode *generateSink(ProgramStateRef State, ExplodedNode *Pred, |
| 186 | const ProgramPointTag *Tag = nullptr) { |
| 187 | return addTransitionImpl(State ? State : getState(), true, Pred, Tag); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | ExplodedNode *generateErrorNode(ProgramStateRef State = nullptr, |
| 198 | const ProgramPointTag *Tag = nullptr) { |
| 199 | return generateSink(State, Pred, |
| 200 | (Tag ? Tag : Location.getTag())); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | ExplodedNode * |
| 211 | generateNonFatalErrorNode(ProgramStateRef State = nullptr, |
| 212 | const ProgramPointTag *Tag = nullptr) { |
| 213 | return addTransition(State, (Tag ? Tag : Location.getTag())); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | void emitReport(std::unique_ptr<BugReport> R) { |
| 218 | Changed = true; |
| 219 | Eng.getBugReporter().emitReport(std::move(R)); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | |
| 224 | StringRef getDeclDescription(const Decl *D); |
| 225 | |
| 226 | |
| 227 | const FunctionDecl *getCalleeDecl(const CallExpr *CE) const; |
| 228 | |
| 229 | |
| 230 | StringRef getCalleeName(const FunctionDecl *FunDecl) const; |
| 231 | |
| 232 | |
| 233 | const IdentifierInfo *getCalleeIdentifier(const CallExpr *CE) const { |
| 234 | const FunctionDecl *FunDecl = getCalleeDecl(CE); |
| 235 | if (FunDecl) |
| 236 | return FunDecl->getIdentifier(); |
| 237 | else |
| 238 | return nullptr; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | StringRef getCalleeName(const CallExpr *CE) const { |
| 243 | const FunctionDecl *FunDecl = getCalleeDecl(CE); |
| 244 | return getCalleeName(FunDecl); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | static bool isCLibraryFunction(const FunctionDecl *FD, |
| 258 | StringRef Name = StringRef()); |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | StringRef getMacroNameOrSpelling(SourceLocation &Loc); |
| 270 | |
| 271 | private: |
| 272 | ExplodedNode *addTransitionImpl(ProgramStateRef State, |
| 273 | bool MarkAsSink, |
| 274 | ExplodedNode *P = nullptr, |
| 275 | const ProgramPointTag *Tag = nullptr) { |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | if (!State || (State == Pred->getState() && !Tag && !MarkAsSink)) |
| 289 | return Pred; |
| 290 | |
| 291 | Changed = true; |
| 292 | const ProgramPoint &LocalLoc = (Tag ? Location.withTag(Tag) : Location); |
| 293 | if (!P) |
| 294 | P = Pred; |
| 295 | |
| 296 | ExplodedNode *node; |
| 297 | if (MarkAsSink) |
| 298 | node = NB.generateSink(LocalLoc, State, P); |
| 299 | else |
| 300 | node = NB.generateNode(LocalLoc, State, P); |
| 301 | return node; |
| 302 | } |
| 303 | }; |
| 304 | |
| 305 | } |
| 306 | |
| 307 | } |
| 308 | |
| 309 | #endif |
| 310 | |