Ted Kremenek | e8f7316 | 2011-08-12 23:04:46 +0000 | [diff] [blame] | 1 | //==- ProgramPoint.cpp - Program Points for Path-Sensitive Analysis -*- C++ -*-/ |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Ted Kremenek | e8f7316 | 2011-08-12 23:04:46 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the interface ProgramPoint, which identifies a |
| 10 | // distinct location in a function. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Analysis/ProgramPoint.h" |
| 15 | |
| 16 | using namespace clang; |
| 17 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 18 | ProgramPointTag::~ProgramPointTag() {} |
Ted Kremenek | e8f7316 | 2011-08-12 23:04:46 +0000 | [diff] [blame] | 19 | |
Anna Zaks | 8de8cfd | 2011-10-07 21:01:38 +0000 | [diff] [blame] | 20 | ProgramPoint ProgramPoint::getProgramPoint(const Stmt *S, ProgramPoint::Kind K, |
| 21 | const LocationContext *LC, |
| 22 | const ProgramPointTag *tag){ |
| 23 | switch (K) { |
| 24 | default: |
| 25 | llvm_unreachable("Unhandled ProgramPoint kind"); |
| 26 | case ProgramPoint::PreStmtKind: |
| 27 | return PreStmt(S, LC, tag); |
| 28 | case ProgramPoint::PostStmtKind: |
| 29 | return PostStmt(S, LC, tag); |
| 30 | case ProgramPoint::PreLoadKind: |
| 31 | return PreLoad(S, LC, tag); |
| 32 | case ProgramPoint::PostLoadKind: |
| 33 | return PostLoad(S, LC, tag); |
| 34 | case ProgramPoint::PreStoreKind: |
| 35 | return PreStore(S, LC, tag); |
Anna Zaks | 8de8cfd | 2011-10-07 21:01:38 +0000 | [diff] [blame] | 36 | case ProgramPoint::PostLValueKind: |
| 37 | return PostLValue(S, LC, tag); |
Anna Zaks | 7e53bd6 | 2012-04-20 21:59:08 +0000 | [diff] [blame] | 38 | case ProgramPoint::PostStmtPurgeDeadSymbolsKind: |
| 39 | return PostStmtPurgeDeadSymbols(S, LC, tag); |
| 40 | case ProgramPoint::PreStmtPurgeDeadSymbolsKind: |
| 41 | return PreStmtPurgeDeadSymbols(S, LC, tag); |
Anna Zaks | 8de8cfd | 2011-10-07 21:01:38 +0000 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
Eric Fiselier | 407584c | 2018-09-30 18:05:39 +0000 | [diff] [blame] | 45 | LLVM_DUMP_METHOD void ProgramPoint::dump() const { |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 46 | return printJson(llvm::errs()); |
Eric Fiselier | 407584c | 2018-09-30 18:05:39 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Csaba Dabis | 9ee26c8 | 2019-05-29 18:17:18 +0000 | [diff] [blame] | 49 | static void printLocJson(raw_ostream &Out, SourceLocation Loc, |
| 50 | const SourceManager &SM) { |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 51 | Out << "\"location\": "; |
| 52 | if (!Loc.isFileID()) { |
| 53 | Out << "null"; |
| 54 | return; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 55 | } |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 56 | |
| 57 | Out << "{ \"line\": " << SM.getExpansionLineNumber(Loc) |
Csaba Dabis | 3a4a60e | 2019-06-24 16:19:39 +0000 | [diff] [blame^] | 58 | << ", \"column\": " << SM.getExpansionColumnNumber(Loc) |
| 59 | << ", \"file\": \"" << SM.getFilename(Loc) << "\" }"; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 62 | void ProgramPoint::printJson(llvm::raw_ostream &Out, const char *NL) const { |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 63 | const ASTContext &Context = |
| 64 | getLocationContext()->getAnalysisDeclContext()->getASTContext(); |
| 65 | const SourceManager &SM = Context.getSourceManager(); |
Csaba Dabis | 9ee26c8 | 2019-05-29 18:17:18 +0000 | [diff] [blame] | 66 | const PrintingPolicy &PP = Context.getPrintingPolicy(); |
| 67 | const bool AddQuotes = true; |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 68 | |
| 69 | Out << "\"kind\": \""; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 70 | switch (getKind()) { |
| 71 | case ProgramPoint::BlockEntranceKind: |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 72 | Out << "BlockEntrance\"" |
| 73 | << ", \"block_id\": " |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 74 | << castAs<BlockEntrance>().getBlock()->getBlockID(); |
| 75 | break; |
| 76 | |
| 77 | case ProgramPoint::FunctionExitKind: { |
| 78 | auto FEP = getAs<FunctionExitPoint>(); |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 79 | Out << "FunctionExit\"" |
| 80 | << ", \"block_id\": " << FEP->getBlock()->getBlockID() |
| 81 | << ", \"stmt_id\": "; |
| 82 | |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 83 | if (const ReturnStmt *RS = FEP->getStmt()) { |
Csaba Dabis | 9ee26c8 | 2019-05-29 18:17:18 +0000 | [diff] [blame] | 84 | Out << RS->getID(Context) << ", \"stmt\": "; |
| 85 | RS->printJson(Out, nullptr, PP, AddQuotes); |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 86 | } else { |
| 87 | Out << "null, \"stmt\": null"; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 88 | } |
| 89 | break; |
| 90 | } |
| 91 | case ProgramPoint::BlockExitKind: |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 92 | llvm_unreachable("BlockExitKind"); |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 93 | break; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 94 | case ProgramPoint::CallEnterKind: |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 95 | Out << "CallEnter\""; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 96 | break; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 97 | case ProgramPoint::CallExitBeginKind: |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 98 | Out << "CallExitBegin\""; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 99 | break; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 100 | case ProgramPoint::CallExitEndKind: |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 101 | Out << "CallExitEnd\""; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 102 | break; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 103 | case ProgramPoint::EpsilonKind: |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 104 | Out << "EpsilonPoint\""; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 105 | break; |
| 106 | |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 107 | case ProgramPoint::LoopExitKind: |
| 108 | Out << "LoopExit\", \"stmt\": \"" |
| 109 | << castAs<LoopExit>().getLoopStmt()->getStmtClassName() << '\"'; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 110 | break; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 111 | |
| 112 | case ProgramPoint::PreImplicitCallKind: { |
| 113 | ImplicitCallPoint PC = castAs<ImplicitCallPoint>(); |
Csaba Dabis | 02be650 | 2019-05-29 18:21:14 +0000 | [diff] [blame] | 114 | Out << "PreCall\", \"decl\": \"" |
| 115 | << PC.getDecl()->getAsFunction()->getQualifiedNameAsString() << "\", "; |
Csaba Dabis | 9ee26c8 | 2019-05-29 18:17:18 +0000 | [diff] [blame] | 116 | printLocJson(Out, PC.getLocation(), SM); |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 117 | break; |
| 118 | } |
| 119 | |
| 120 | case ProgramPoint::PostImplicitCallKind: { |
| 121 | ImplicitCallPoint PC = castAs<ImplicitCallPoint>(); |
Csaba Dabis | 02be650 | 2019-05-29 18:21:14 +0000 | [diff] [blame] | 122 | Out << "PostCall\", \"decl\": \"" |
| 123 | << PC.getDecl()->getAsFunction()->getQualifiedNameAsString() << "\", "; |
Csaba Dabis | 9ee26c8 | 2019-05-29 18:17:18 +0000 | [diff] [blame] | 124 | printLocJson(Out, PC.getLocation(), SM); |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 125 | break; |
| 126 | } |
| 127 | |
| 128 | case ProgramPoint::PostInitializerKind: { |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 129 | Out << "PostInitializer\", "; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 130 | const CXXCtorInitializer *Init = castAs<PostInitializer>().getInitializer(); |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 131 | if (const FieldDecl *FD = Init->getAnyMember()) { |
| 132 | Out << "\"field_decl\": \"" << *FD << '\"'; |
| 133 | } else { |
| 134 | Out << "\"type\": \""; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 135 | QualType Ty = Init->getTypeSourceInfo()->getType(); |
| 136 | Ty = Ty.getLocalUnqualifiedType(); |
| 137 | Ty.print(Out, Context.getLangOpts()); |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 138 | Out << '\"'; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 139 | } |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | case ProgramPoint::BlockEdgeKind: { |
| 144 | const BlockEdge &E = castAs<BlockEdge>(); |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 145 | const Stmt *T = E.getSrc()->getTerminatorStmt(); |
| 146 | Out << "Edge\", \"src_id\": " << E.getSrc()->getBlockID() |
Csaba Dabis | dea605e | 2019-05-29 18:29:31 +0000 | [diff] [blame] | 147 | << ", \"dst_id\": " << E.getDst()->getBlockID() << ", \"terminator\": "; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 148 | |
Csaba Dabis | dea605e | 2019-05-29 18:29:31 +0000 | [diff] [blame] | 149 | if (!T) { |
| 150 | Out << "null, \"term_kind\": null"; |
| 151 | break; |
| 152 | } |
| 153 | |
| 154 | E.getSrc()->printTerminatorJson(Out, Context.getLangOpts(), |
| 155 | /*AddQuotes=*/true); |
| 156 | Out << ", "; |
Csaba Dabis | 9ee26c8 | 2019-05-29 18:17:18 +0000 | [diff] [blame] | 157 | printLocJson(Out, T->getBeginLoc(), SM); |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 158 | |
Csaba Dabis | 9ee26c8 | 2019-05-29 18:17:18 +0000 | [diff] [blame] | 159 | Out << ", \"term_kind\": \""; |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 160 | if (isa<SwitchStmt>(T)) { |
| 161 | Out << "SwitchStmt\", \"case\": "; |
| 162 | if (const Stmt *Label = E.getDst()->getLabel()) { |
| 163 | if (const auto *C = dyn_cast<CaseStmt>(Label)) { |
| 164 | Out << "{ \"lhs\": "; |
Csaba Dabis | 9ee26c8 | 2019-05-29 18:17:18 +0000 | [diff] [blame] | 165 | if (const Stmt *LHS = C->getLHS()) { |
| 166 | LHS->printJson(Out, nullptr, PP, AddQuotes); |
| 167 | } else { |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 168 | Out << "null"; |
Csaba Dabis | 9ee26c8 | 2019-05-29 18:17:18 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 171 | Out << ", \"rhs\": "; |
Csaba Dabis | 9ee26c8 | 2019-05-29 18:17:18 +0000 | [diff] [blame] | 172 | if (const Stmt *RHS = C->getRHS()) { |
| 173 | RHS->printJson(Out, nullptr, PP, AddQuotes); |
| 174 | } else { |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 175 | Out << "null"; |
Csaba Dabis | 9ee26c8 | 2019-05-29 18:17:18 +0000 | [diff] [blame] | 176 | } |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 177 | Out << " }"; |
| 178 | } else { |
| 179 | assert(isa<DefaultStmt>(Label)); |
| 180 | Out << "\"default\""; |
| 181 | } |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 182 | } else { |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 183 | Out << "\"implicit default\""; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 184 | } |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 185 | } else if (isa<IndirectGotoStmt>(T)) { |
| 186 | // FIXME: More info. |
| 187 | Out << "IndirectGotoStmt\""; |
| 188 | } else { |
| 189 | Out << "Condition\", \"value\": " |
| 190 | << (*E.getSrc()->succ_begin() == E.getDst() ? "true" : "false"); |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 191 | } |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 192 | break; |
| 193 | } |
| 194 | |
| 195 | default: { |
| 196 | const Stmt *S = castAs<StmtPoint>().getStmt(); |
| 197 | assert(S != nullptr && "Expecting non-null Stmt"); |
| 198 | |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 199 | Out << "Statement\", \"stmt_kind\": \"" << S->getStmtClassName() |
| 200 | << "\", \"stmt_id\": " << S->getID(Context) |
| 201 | << ", \"pointer\": \"" << (const void *)S << "\", \"pretty\": "; |
| 202 | |
Csaba Dabis | 9ee26c8 | 2019-05-29 18:17:18 +0000 | [diff] [blame] | 203 | S->printJson(Out, nullptr, PP, AddQuotes); |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 204 | |
Csaba Dabis | 9ee26c8 | 2019-05-29 18:17:18 +0000 | [diff] [blame] | 205 | Out << ", "; |
| 206 | printLocJson(Out, S->getBeginLoc(), SM); |
Csaba Dabis | 13e491c | 2019-05-29 18:05:53 +0000 | [diff] [blame] | 207 | |
Csaba Dabis | fa880e6 | 2019-06-12 18:24:02 +0000 | [diff] [blame] | 208 | Out << ", \"stmt_point_kind\": \""; |
| 209 | if (getAs<PreLoad>()) |
| 210 | Out << "PreLoad"; |
| 211 | else if (getAs<PreStore>()) |
| 212 | Out << "PreStore"; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 213 | else if (getAs<PostAllocatorCall>()) |
Csaba Dabis | fa880e6 | 2019-06-12 18:24:02 +0000 | [diff] [blame] | 214 | Out << "PostAllocatorCall"; |
| 215 | else if (getAs<PostCondition>()) |
| 216 | Out << "PostCondition"; |
| 217 | else if (getAs<PostLoad>()) |
| 218 | Out << "PostLoad"; |
| 219 | else if (getAs<PostLValue>()) |
| 220 | Out << "PostLValue"; |
| 221 | else if (getAs<PostStore>()) |
| 222 | Out << "PostStore"; |
| 223 | else if (getAs<PostStmt>()) |
| 224 | Out << "PostStmt"; |
| 225 | else if (getAs<PostStmtPurgeDeadSymbols>()) |
| 226 | Out << "PostStmtPurgeDeadSymbols"; |
| 227 | else if (getAs<PreStmtPurgeDeadSymbols>()) |
| 228 | Out << "PreStmtPurgeDeadSymbols"; |
| 229 | else if (getAs<PreStmt>()) |
| 230 | Out << "PreStmt"; |
| 231 | else { |
| 232 | Out << "\nKind: '" << getKind(); |
| 233 | llvm_unreachable("' is unhandled StmtPoint kind!"); |
| 234 | } |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 235 | |
Csaba Dabis | fa880e6 | 2019-06-12 18:24:02 +0000 | [diff] [blame] | 236 | Out << '\"'; |
George Karpenkov | 27ec210 | 2018-09-27 01:46:18 +0000 | [diff] [blame] | 237 | break; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 242 | SimpleProgramPointTag::SimpleProgramPointTag(StringRef MsgProvider, |
Anton Yartsev | 6a61922 | 2014-02-17 18:25:34 +0000 | [diff] [blame] | 243 | StringRef Msg) |
| 244 | : Desc((MsgProvider + " : " + Msg).str()) {} |
Ted Kremenek | e8f7316 | 2011-08-12 23:04:46 +0000 | [diff] [blame] | 245 | |
| 246 | StringRef SimpleProgramPointTag::getTagDescription() const { |
Anton Yartsev | 6a61922 | 2014-02-17 18:25:34 +0000 | [diff] [blame] | 247 | return Desc; |
Ted Kremenek | e8f7316 | 2011-08-12 23:04:46 +0000 | [diff] [blame] | 248 | } |