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