clang API Documentation
00001 //== CheckerContext.h - Context info for path-sensitive checkers--*- C++ -*--=// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines CheckerContext that provides contextual info for 00011 // path-sensitive checkers. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_CLANG_SA_CORE_PATHSENSITIVE_CHECKERCONTEXT 00016 #define LLVM_CLANG_SA_CORE_PATHSENSITIVE_CHECKERCONTEXT 00017 00018 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 00019 00020 namespace clang { 00021 namespace ento { 00022 00023 class CheckerContext { 00024 ExprEngine &Eng; 00025 /// The current exploded(symbolic execution) graph node. 00026 ExplodedNode *Pred; 00027 /// The flag is true if the (state of the execution) has been modified 00028 /// by the checker using this context. For example, a new transition has been 00029 /// added or a bug report issued. 00030 bool Changed; 00031 /// The tagged location, which is used to generate all new nodes. 00032 const ProgramPoint Location; 00033 NodeBuilder &NB; 00034 00035 public: 00036 /// If we are post visiting a call, this flag will be set if the 00037 /// call was inlined. In all other cases it will be false. 00038 const bool wasInlined; 00039 00040 CheckerContext(NodeBuilder &builder, 00041 ExprEngine &eng, 00042 ExplodedNode *pred, 00043 const ProgramPoint &loc, 00044 bool wasInlined = false) 00045 : Eng(eng), 00046 Pred(pred), 00047 Changed(false), 00048 Location(loc), 00049 NB(builder), 00050 wasInlined(wasInlined) { 00051 assert(Pred->getState() && 00052 "We should not call the checkers on an empty state."); 00053 } 00054 00055 AnalysisManager &getAnalysisManager() { 00056 return Eng.getAnalysisManager(); 00057 } 00058 00059 ConstraintManager &getConstraintManager() { 00060 return Eng.getConstraintManager(); 00061 } 00062 00063 StoreManager &getStoreManager() { 00064 return Eng.getStoreManager(); 00065 } 00066 00067 /// \brief Returns the previous node in the exploded graph, which includes 00068 /// the state of the program before the checker ran. Note, checkers should 00069 /// not retain the node in their state since the nodes might get invalidated. 00070 ExplodedNode *getPredecessor() { return Pred; } 00071 ProgramStateRef getState() const { return Pred->getState(); } 00072 00073 /// \brief Check if the checker changed the state of the execution; ex: added 00074 /// a new transition or a bug report. 00075 bool isDifferent() { return Changed; } 00076 00077 /// \brief Returns the number of times the current block has been visited 00078 /// along the analyzed path. 00079 unsigned getCurrentBlockCount() const { 00080 return NB.getContext().getCurrentBlockCount(); 00081 } 00082 00083 ASTContext &getASTContext() { 00084 return Eng.getContext(); 00085 } 00086 00087 const LangOptions &getLangOpts() const { 00088 return Eng.getContext().getLangOpts(); 00089 } 00090 00091 const LocationContext *getLocationContext() const { 00092 return Pred->getLocationContext(); 00093 } 00094 00095 BugReporter &getBugReporter() { 00096 return Eng.getBugReporter(); 00097 } 00098 00099 SourceManager &getSourceManager() { 00100 return getBugReporter().getSourceManager(); 00101 } 00102 00103 SValBuilder &getSValBuilder() { 00104 return Eng.getSValBuilder(); 00105 } 00106 00107 SymbolManager &getSymbolManager() { 00108 return getSValBuilder().getSymbolManager(); 00109 } 00110 00111 bool isObjCGCEnabled() const { 00112 return Eng.isObjCGCEnabled(); 00113 } 00114 00115 ProgramStateManager &getStateManager() { 00116 return Eng.getStateManager(); 00117 } 00118 00119 AnalysisDeclContext *getCurrentAnalysisDeclContext() const { 00120 return Pred->getLocationContext()->getAnalysisDeclContext(); 00121 } 00122 00123 /// \brief If the given node corresponds to a PostStore program point, retrieve 00124 /// the location region as it was uttered in the code. 00125 /// 00126 /// This utility can be useful for generating extensive diagnostics, for 00127 /// example, for finding variables that the given symbol was assigned to. 00128 static const MemRegion *getLocationRegionIfPostStore(const ExplodedNode *N) { 00129 ProgramPoint L = N->getLocation(); 00130 if (const PostStore *PSL = dyn_cast<PostStore>(&L)) 00131 return reinterpret_cast<const MemRegion*>(PSL->getLocationValue()); 00132 return 0; 00133 } 00134 00135 /// \brief Generates a new transition in the program state graph 00136 /// (ExplodedGraph). Uses the default CheckerContext predecessor node. 00137 /// 00138 /// @param State The state of the generated node. 00139 /// @param Tag The tag is used to uniquely identify the creation site. If no 00140 /// tag is specified, a default tag, unique to the given checker, 00141 /// will be used. Tags are used to prevent states generated at 00142 /// different sites from caching out. 00143 ExplodedNode *addTransition(ProgramStateRef State, 00144 const ProgramPointTag *Tag = 0) { 00145 return addTransitionImpl(State, false, 0, Tag); 00146 } 00147 00148 /// \brief Generates a default transition (containing checker tag but no 00149 /// checker state changes). 00150 ExplodedNode *addTransition() { 00151 return addTransition(getState()); 00152 } 00153 00154 /// \brief Generates a new transition with the given predecessor. 00155 /// Allows checkers to generate a chain of nodes. 00156 /// 00157 /// @param State The state of the generated node. 00158 /// @param Pred The transition will be generated from the specified Pred node 00159 /// to the newly generated node. 00160 /// @param Tag The tag to uniquely identify the creation site. 00161 /// @param IsSink Mark the new node as sink, which will stop exploration of 00162 /// the given path. 00163 ExplodedNode *addTransition(ProgramStateRef State, 00164 ExplodedNode *Pred, 00165 const ProgramPointTag *Tag = 0, 00166 bool IsSink = false) { 00167 return addTransitionImpl(State, IsSink, Pred, Tag); 00168 } 00169 00170 /// \brief Generate a sink node. Generating sink stops exploration of the 00171 /// given path. 00172 ExplodedNode *generateSink(ProgramStateRef state = 0) { 00173 return addTransitionImpl(state ? state : getState(), true); 00174 } 00175 00176 /// \brief Emit the diagnostics report. 00177 void EmitReport(BugReport *R) { 00178 Changed = true; 00179 Eng.getBugReporter().EmitReport(R); 00180 } 00181 00182 /// \brief Get the declaration of the called function (path-sensitive). 00183 const FunctionDecl *getCalleeDecl(const CallExpr *CE) const; 00184 00185 /// \brief Get the name of the called function (path-sensitive). 00186 StringRef getCalleeName(const FunctionDecl *FunDecl) const; 00187 00188 /// \brief Get the name of the called function (path-sensitive). 00189 StringRef getCalleeName(const CallExpr *CE) const { 00190 const FunctionDecl *FunDecl = getCalleeDecl(CE); 00191 return getCalleeName(FunDecl); 00192 } 00193 00194 /// Given a function declaration and a name checks if this is a C lib 00195 /// function with the given name. 00196 bool isCLibraryFunction(const FunctionDecl *FD, StringRef Name); 00197 static bool isCLibraryFunction(const FunctionDecl *FD, StringRef Name, 00198 ASTContext &Context); 00199 00200 /// \brief Depending on wither the location corresponds to a macro, return 00201 /// either the macro name or the token spelling. 00202 /// 00203 /// This could be useful when checkers' logic depends on whether a function 00204 /// is called with a given macro argument. For example: 00205 /// s = socket(AF_INET,..) 00206 /// If AF_INET is a macro, the result should be treated as a source of taint. 00207 /// 00208 /// \sa clang::Lexer::getSpelling(), clang::Lexer::getImmediateMacroName(). 00209 StringRef getMacroNameOrSpelling(SourceLocation &Loc); 00210 00211 private: 00212 ExplodedNode *addTransitionImpl(ProgramStateRef State, 00213 bool MarkAsSink, 00214 ExplodedNode *P = 0, 00215 const ProgramPointTag *Tag = 0) { 00216 if (!State || (State == Pred->getState() && !Tag && !MarkAsSink)) 00217 return Pred; 00218 00219 Changed = true; 00220 ExplodedNode *node = NB.generateNode(Tag ? Location.withTag(Tag) : Location, 00221 State, 00222 P ? P : Pred, MarkAsSink); 00223 return node; 00224 } 00225 }; 00226 00227 /// \brief A helper class which wraps a boolean value set to false by default. 00228 struct DefaultBool { 00229 bool Val; 00230 DefaultBool() : Val(false) {} 00231 operator bool() const { return Val; } 00232 DefaultBool &operator=(bool b) { Val = b; return *this; } 00233 }; 00234 00235 } // end GR namespace 00236 00237 } // end clang namespace 00238 00239 #endif