CheckerContext.h revision 0e12ebfd3ef9ad5d894466c6e4910ac5e6041034
16bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis//== CheckerContext.h - Context info for path-sensitive checkers--*- C++ -*--=//
26bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis//
36bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis//                     The LLVM Compiler Infrastructure
46bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis//
56bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis// This file is distributed under the University of Illinois Open Source
66bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis// License. See LICENSE.TXT for details.
76bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis//
86bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
96bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis//
106bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis//  This file defines CheckerContext that provides contextual info for
116bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis// path-sensitive checkers.
126bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis//
136bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
146bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis
156bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis#ifndef LLVM_CLANG_SA_CORE_PATHSENSITIVE_CHECKERCONTEXT
166bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis#define LLVM_CLANG_SA_CORE_PATHSENSITIVE_CHECKERCONTEXT
176bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis
186bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
196bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis
206bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidisnamespace clang {
216bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidisnamespace ento {
226bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis
236bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidisclass CheckerContext {
246bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  ExprEngine &Eng;
256bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  ExplodedNode *Pred;
263f5e8d87dbf449d8b39fe96068415428594d370eAnna Zaks  const ProgramPoint Location;
273152b3cb5b6a2f797d0972c81a5eb3fd69c0d620Anna Zaks  NodeBuilder &NB;
28063e0887ad65d666d23ee3178436ad6507abbd1bAnna Zaks
296bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidispublic:
308ff5c41f2bde7ebbe568b4c15e59f14b8befae66Anna Zaks  CheckerContext(NodeBuilder &builder,
3118c66fdc3c4008d335885695fe36fb5353c5f672Ted Kremenek                 ExprEngine &eng,
3218c66fdc3c4008d335885695fe36fb5353c5f672Ted Kremenek                 ExplodedNode *pred,
33063e0887ad65d666d23ee3178436ad6507abbd1bAnna Zaks                 const ProgramPoint &loc)
348ff5c41f2bde7ebbe568b4c15e59f14b8befae66Anna Zaks    : Eng(eng),
3518c66fdc3c4008d335885695fe36fb5353c5f672Ted Kremenek      Pred(pred),
363f5e8d87dbf449d8b39fe96068415428594d370eAnna Zaks      Location(loc),
37063e0887ad65d666d23ee3178436ad6507abbd1bAnna Zaks      NB(builder) {}
386bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis
396bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  AnalysisManager &getAnalysisManager() {
406bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis    return Eng.getAnalysisManager();
416bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  }
426bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis
436bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  ConstraintManager &getConstraintManager() {
446bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis    return Eng.getConstraintManager();
456bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  }
466bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis
476bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  StoreManager &getStoreManager() {
486bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis    return Eng.getStoreManager();
496bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  }
506bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis
51a2a860306e3697fcf7a12c5ba59551ca60578968Anna Zaks  /// \brief Returns the previous node in the exploded graph, which includes
52a2a860306e3697fcf7a12c5ba59551ca60578968Anna Zaks  /// the state of the program before the checker ran. Note, checkers should
53a2a860306e3697fcf7a12c5ba59551ca60578968Anna Zaks  /// not retain the node in their state since the nodes might get invalidated.
5439ac1876f6f9a1a8e0070f0df61036c7ba05202bAnna Zaks  ExplodedNode *getPredecessor() { return Pred; }
55063e0887ad65d666d23ee3178436ad6507abbd1bAnna Zaks  const ProgramState *getState() { return Pred->getState(); }
566bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis
575d0ea6d62e076c776ddad028c4eb615783be1323Anna Zaks  /// \brief Returns the number of times the current block has been visited
585d0ea6d62e076c776ddad028c4eb615783be1323Anna Zaks  /// along the analyzed path.
59ebae6d0209e1ec3d5ea14f9e63bd0d740218ed14Anna Zaks  unsigned getCurrentBlockCount() {
60ebae6d0209e1ec3d5ea14f9e63bd0d740218ed14Anna Zaks    return NB.getContext().getCurrentBlockCount();
61ebae6d0209e1ec3d5ea14f9e63bd0d740218ed14Anna Zaks  }
625d0ea6d62e076c776ddad028c4eb615783be1323Anna Zaks
636bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  ASTContext &getASTContext() {
646bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis    return Eng.getContext();
656bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  }
666bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis
6739ac1876f6f9a1a8e0070f0df61036c7ba05202bAnna Zaks  const LocationContext *getLocationContext() {
6839ac1876f6f9a1a8e0070f0df61036c7ba05202bAnna Zaks    return Pred->getLocationContext();
6939ac1876f6f9a1a8e0070f0df61036c7ba05202bAnna Zaks  }
7039ac1876f6f9a1a8e0070f0df61036c7ba05202bAnna Zaks
716bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  BugReporter &getBugReporter() {
726bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis    return Eng.getBugReporter();
736bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  }
746bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis
756bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  SourceManager &getSourceManager() {
766bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis    return getBugReporter().getSourceManager();
776bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  }
786bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis
796bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  SValBuilder &getSValBuilder() {
806bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis    return Eng.getSValBuilder();
816bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  }
826bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis
833f10e32b15e54f507aed90cb72d73c7acaa500bbAnna Zaks  SymbolManager &getSymbolManager() {
843f10e32b15e54f507aed90cb72d73c7acaa500bbAnna Zaks    return getSValBuilder().getSymbolManager();
853f10e32b15e54f507aed90cb72d73c7acaa500bbAnna Zaks  }
863f10e32b15e54f507aed90cb72d73c7acaa500bbAnna Zaks
8717a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose  bool isObjCGCEnabled() {
8817a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose    return Eng.isObjCGCEnabled();
8917a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose  }
9017a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose
916a93bd526c5136ee5a26871e829cf5a8548a1c6aAnna Zaks  ProgramStateManager &getStateManager() {
926a93bd526c5136ee5a26871e829cf5a8548a1c6aAnna Zaks    return Eng.getStateManager();
936a93bd526c5136ee5a26871e829cf5a8548a1c6aAnna Zaks  }
946a93bd526c5136ee5a26871e829cf5a8548a1c6aAnna Zaks
95063e0887ad65d666d23ee3178436ad6507abbd1bAnna Zaks  AnalysisDeclContext *getCurrentAnalysisDeclContext() const {
96063e0887ad65d666d23ee3178436ad6507abbd1bAnna Zaks    return Pred->getLocationContext()->getAnalysisDeclContext();
97063e0887ad65d666d23ee3178436ad6507abbd1bAnna Zaks  }
98063e0887ad65d666d23ee3178436ad6507abbd1bAnna Zaks
990bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  /// \brief Generates a new transition in the program state graph
1000bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  /// (ExplodedGraph). Uses the default CheckerContext predecessor node.
1010bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  ///
1020bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  /// @param State The state of the generated node.
1030bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  /// @param Tag The tag is used to uniquely identify the creation site. If no
1040bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  ///        tag is specified, a default tag, unique to the given checker,
1050bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  ///        will be used. Tags are used to prevent states generated at
1060bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  ///        different sites from caching out.
1070bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  ExplodedNode *addTransition(const ProgramState *State,
1080bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks                              const ProgramPointTag *Tag = 0) {
1090bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks    return addTransitionImpl(State, false, 0, Tag);
1106bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  }
1110bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks
1120bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  /// \brief Generates a default transition (containing checker tag but no
1130bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  /// checker state changes).
1140bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  ExplodedNode *addTransition() {
1150bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks    return addTransition(getState());
116063e0887ad65d666d23ee3178436ad6507abbd1bAnna Zaks  }
117063e0887ad65d666d23ee3178436ad6507abbd1bAnna Zaks
1180bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  /// \brief Generates a new transition with the given predecessor.
1198ba721428af297e540fb40b176eeeea0ee010c1fAnna Zaks  /// Allows checkers to generate a chain of nodes.
1200bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  ///
1210bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  /// @param State The state of the generated node.
1220bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  /// @param Pred The transition will be generated from the specified Pred node
1230bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  ///             to the newly generated node.
1240bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  /// @param Tag The tag to uniquely identify the creation site.
1250bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  /// @param IsSink Mark the new node as sink, which will stop exploration of
1260bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  ///               the given path.
1270bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks  ExplodedNode *addTransition(const ProgramState *State,
1280bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks                             ExplodedNode *Pred,
1290bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks                             const ProgramPointTag *Tag = 0,
1300bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks                             bool IsSink = false) {
1310bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks    return addTransitionImpl(State, IsSink, Pred, Tag);
1326bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  }
1336bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis
1348ba721428af297e540fb40b176eeeea0ee010c1fAnna Zaks  /// \brief Generate a sink node. Generating sink stops exploration of the
1358ba721428af297e540fb40b176eeeea0ee010c1fAnna Zaks  /// given path.
13618c66fdc3c4008d335885695fe36fb5353c5f672Ted Kremenek  ExplodedNode *generateSink(const ProgramState *state = 0) {
1370bd6b110e908892d4b5c8671a9f435a1d72ad16aAnna Zaks    return addTransitionImpl(state ? state : getState(), true);
1386bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  }
1396bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis
140063e0887ad65d666d23ee3178436ad6507abbd1bAnna Zaks  /// \brief Emit the diagnostics report.
1416bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  void EmitReport(BugReport *R) {
1426bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis    Eng.getBugReporter().EmitReport(R);
1436bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  }
1446bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis
1450e12ebfd3ef9ad5d894466c6e4910ac5e6041034Anna Zaks  /// \brief Get the name of the called function (path-sensitive).
1460e12ebfd3ef9ad5d894466c6e4910ac5e6041034Anna Zaks  StringRef getCalleeName(const CallExpr *CE) {
1470e12ebfd3ef9ad5d894466c6e4910ac5e6041034Anna Zaks    const ProgramState *State = getState();
1480e12ebfd3ef9ad5d894466c6e4910ac5e6041034Anna Zaks    const Expr *Callee = CE->getCallee();
1490e12ebfd3ef9ad5d894466c6e4910ac5e6041034Anna Zaks    SVal L = State->getSVal(Callee);
1500e12ebfd3ef9ad5d894466c6e4910ac5e6041034Anna Zaks
1510e12ebfd3ef9ad5d894466c6e4910ac5e6041034Anna Zaks    const FunctionDecl *funDecl = L.getAsFunctionDecl();
1520e12ebfd3ef9ad5d894466c6e4910ac5e6041034Anna Zaks    if (!funDecl)
1530e12ebfd3ef9ad5d894466c6e4910ac5e6041034Anna Zaks      return StringRef();
1540e12ebfd3ef9ad5d894466c6e4910ac5e6041034Anna Zaks    IdentifierInfo *funI = funDecl->getIdentifier();
1550e12ebfd3ef9ad5d894466c6e4910ac5e6041034Anna Zaks    if (!funI)
1560e12ebfd3ef9ad5d894466c6e4910ac5e6041034Anna Zaks      return StringRef();
1570e12ebfd3ef9ad5d894466c6e4910ac5e6041034Anna Zaks    return funI->getName();
1580e12ebfd3ef9ad5d894466c6e4910ac5e6041034Anna Zaks  }
1590e12ebfd3ef9ad5d894466c6e4910ac5e6041034Anna Zaks
1606bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidisprivate:
16139ac1876f6f9a1a8e0070f0df61036c7ba05202bAnna Zaks  ExplodedNode *addTransitionImpl(const ProgramState *State,
16239ac1876f6f9a1a8e0070f0df61036c7ba05202bAnna Zaks                                 bool MarkAsSink,
16339ac1876f6f9a1a8e0070f0df61036c7ba05202bAnna Zaks                                 ExplodedNode *P = 0,
16439ac1876f6f9a1a8e0070f0df61036c7ba05202bAnna Zaks                                 const ProgramPointTag *Tag = 0) {
16539ac1876f6f9a1a8e0070f0df61036c7ba05202bAnna Zaks    assert(State);
166df95d146c13cf02e106b32b01d147577d6d6b5a1Anna Zaks    if (State == Pred->getState() && !Tag && !MarkAsSink)
167df95d146c13cf02e106b32b01d147577d6d6b5a1Anna Zaks      return Pred;
168df95d146c13cf02e106b32b01d147577d6d6b5a1Anna Zaks
16939ac1876f6f9a1a8e0070f0df61036c7ba05202bAnna Zaks    ExplodedNode *node = NB.generateNode(Tag ? Location.withTag(Tag) : Location,
17039ac1876f6f9a1a8e0070f0df61036c7ba05202bAnna Zaks                                        State,
17139ac1876f6f9a1a8e0070f0df61036c7ba05202bAnna Zaks                                        P ? P : Pred, MarkAsSink);
1726bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis    return node;
1736bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis  }
1746bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis};
1756bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis
1766bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis} // end GR namespace
1776bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis
1786bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis} // end clang namespace
1796bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis
1806bcb48dc67e417e0ecce803f28d13bbea2ee0243Argyrios Kyrtzidis#endif
181