xref: /src/contrib/llvm-project/clang/lib/Analysis/ProgramPoint.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
136981b17SDimitry Andric //==- ProgramPoint.cpp - Program Points for Path-Sensitive Analysis -*- C++ -*-/
236981b17SDimitry Andric //
322989816SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
422989816SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
522989816SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
636981b17SDimitry Andric //
736981b17SDimitry Andric //===----------------------------------------------------------------------===//
836981b17SDimitry Andric //
936981b17SDimitry Andric //  This file defines the interface ProgramPoint, which identifies a
1036981b17SDimitry Andric //  distinct location in a function.
1136981b17SDimitry Andric //
1236981b17SDimitry Andric //===----------------------------------------------------------------------===//
1336981b17SDimitry Andric 
1436981b17SDimitry Andric #include "clang/Analysis/ProgramPoint.h"
15cfca06d7SDimitry Andric #include "clang/AST/ASTContext.h"
1622989816SDimitry Andric #include "clang/Basic/JsonSupport.h"
1736981b17SDimitry Andric 
1836981b17SDimitry Andric using namespace clang;
1936981b17SDimitry Andric 
~ProgramPointTag()2036981b17SDimitry Andric ProgramPointTag::~ProgramPointTag() {}
2136981b17SDimitry Andric 
getProgramPoint(const Stmt * S,ProgramPoint::Kind K,const LocationContext * LC,const ProgramPointTag * tag)2236981b17SDimitry Andric ProgramPoint ProgramPoint::getProgramPoint(const Stmt *S, ProgramPoint::Kind K,
2336981b17SDimitry Andric                                            const LocationContext *LC,
2436981b17SDimitry Andric                                            const ProgramPointTag *tag){
2536981b17SDimitry Andric   switch (K) {
2636981b17SDimitry Andric     default:
2736981b17SDimitry Andric       llvm_unreachable("Unhandled ProgramPoint kind");
2836981b17SDimitry Andric     case ProgramPoint::PreStmtKind:
2936981b17SDimitry Andric       return PreStmt(S, LC, tag);
3036981b17SDimitry Andric     case ProgramPoint::PostStmtKind:
3136981b17SDimitry Andric       return PostStmt(S, LC, tag);
3236981b17SDimitry Andric     case ProgramPoint::PreLoadKind:
3336981b17SDimitry Andric       return PreLoad(S, LC, tag);
3436981b17SDimitry Andric     case ProgramPoint::PostLoadKind:
3536981b17SDimitry Andric       return PostLoad(S, LC, tag);
3636981b17SDimitry Andric     case ProgramPoint::PreStoreKind:
3736981b17SDimitry Andric       return PreStore(S, LC, tag);
3836981b17SDimitry Andric     case ProgramPoint::PostLValueKind:
3936981b17SDimitry Andric       return PostLValue(S, LC, tag);
4056d91b49SDimitry Andric     case ProgramPoint::PostStmtPurgeDeadSymbolsKind:
4156d91b49SDimitry Andric       return PostStmtPurgeDeadSymbols(S, LC, tag);
4256d91b49SDimitry Andric     case ProgramPoint::PreStmtPurgeDeadSymbolsKind:
4356d91b49SDimitry Andric       return PreStmtPurgeDeadSymbols(S, LC, tag);
4436981b17SDimitry Andric   }
4536981b17SDimitry Andric }
4636981b17SDimitry Andric 
dump() const47676fbe81SDimitry Andric LLVM_DUMP_METHOD void ProgramPoint::dump() const {
4822989816SDimitry Andric   return printJson(llvm::errs());
49676fbe81SDimitry Andric }
50676fbe81SDimitry Andric 
printJson(llvm::raw_ostream & Out,const char * NL) const5122989816SDimitry Andric void ProgramPoint::printJson(llvm::raw_ostream &Out, const char *NL) const {
52676fbe81SDimitry Andric   const ASTContext &Context =
53676fbe81SDimitry Andric       getLocationContext()->getAnalysisDeclContext()->getASTContext();
54676fbe81SDimitry Andric   const SourceManager &SM = Context.getSourceManager();
5522989816SDimitry Andric   const PrintingPolicy &PP = Context.getPrintingPolicy();
5622989816SDimitry Andric   const bool AddQuotes = true;
5722989816SDimitry Andric 
5822989816SDimitry Andric   Out << "\"kind\": \"";
59676fbe81SDimitry Andric   switch (getKind()) {
60676fbe81SDimitry Andric   case ProgramPoint::BlockEntranceKind:
6122989816SDimitry Andric     Out << "BlockEntrance\""
6222989816SDimitry Andric         << ", \"block_id\": "
63676fbe81SDimitry Andric         << castAs<BlockEntrance>().getBlock()->getBlockID();
64676fbe81SDimitry Andric     break;
65676fbe81SDimitry Andric 
66676fbe81SDimitry Andric   case ProgramPoint::FunctionExitKind: {
67676fbe81SDimitry Andric     auto FEP = getAs<FunctionExitPoint>();
6822989816SDimitry Andric     Out << "FunctionExit\""
6922989816SDimitry Andric         << ", \"block_id\": " << FEP->getBlock()->getBlockID()
7022989816SDimitry Andric         << ", \"stmt_id\": ";
7122989816SDimitry Andric 
72676fbe81SDimitry Andric     if (const ReturnStmt *RS = FEP->getStmt()) {
7322989816SDimitry Andric       Out << RS->getID(Context) << ", \"stmt\": ";
7422989816SDimitry Andric       RS->printJson(Out, nullptr, PP, AddQuotes);
7522989816SDimitry Andric     } else {
7622989816SDimitry Andric       Out << "null, \"stmt\": null";
77676fbe81SDimitry Andric     }
78676fbe81SDimitry Andric     break;
79676fbe81SDimitry Andric   }
80676fbe81SDimitry Andric   case ProgramPoint::BlockExitKind:
8122989816SDimitry Andric     llvm_unreachable("BlockExitKind");
82676fbe81SDimitry Andric     break;
83676fbe81SDimitry Andric   case ProgramPoint::CallEnterKind:
8422989816SDimitry Andric     Out << "CallEnter\"";
85676fbe81SDimitry Andric     break;
86676fbe81SDimitry Andric   case ProgramPoint::CallExitBeginKind:
8722989816SDimitry Andric     Out << "CallExitBegin\"";
88676fbe81SDimitry Andric     break;
89676fbe81SDimitry Andric   case ProgramPoint::CallExitEndKind:
9022989816SDimitry Andric     Out << "CallExitEnd\"";
91676fbe81SDimitry Andric     break;
92676fbe81SDimitry Andric   case ProgramPoint::EpsilonKind:
9322989816SDimitry Andric     Out << "EpsilonPoint\"";
94676fbe81SDimitry Andric     break;
95676fbe81SDimitry Andric 
9622989816SDimitry Andric   case ProgramPoint::LoopExitKind:
9722989816SDimitry Andric     Out << "LoopExit\", \"stmt\": \""
9822989816SDimitry Andric         << castAs<LoopExit>().getLoopStmt()->getStmtClassName() << '\"';
99676fbe81SDimitry Andric     break;
100676fbe81SDimitry Andric 
101676fbe81SDimitry Andric   case ProgramPoint::PreImplicitCallKind: {
102676fbe81SDimitry Andric     ImplicitCallPoint PC = castAs<ImplicitCallPoint>();
10322989816SDimitry Andric     Out << "PreCall\", \"decl\": \""
10422989816SDimitry Andric         << PC.getDecl()->getAsFunction()->getQualifiedNameAsString()
10522989816SDimitry Andric         << "\", \"location\": ";
10622989816SDimitry Andric     printSourceLocationAsJson(Out, PC.getLocation(), SM);
107676fbe81SDimitry Andric     break;
108676fbe81SDimitry Andric   }
109676fbe81SDimitry Andric 
110676fbe81SDimitry Andric   case ProgramPoint::PostImplicitCallKind: {
111676fbe81SDimitry Andric     ImplicitCallPoint PC = castAs<ImplicitCallPoint>();
11222989816SDimitry Andric     Out << "PostCall\", \"decl\": \""
11322989816SDimitry Andric         << PC.getDecl()->getAsFunction()->getQualifiedNameAsString()
11422989816SDimitry Andric         << "\", \"location\": ";
11522989816SDimitry Andric     printSourceLocationAsJson(Out, PC.getLocation(), SM);
116676fbe81SDimitry Andric     break;
117676fbe81SDimitry Andric   }
118676fbe81SDimitry Andric 
119676fbe81SDimitry Andric   case ProgramPoint::PostInitializerKind: {
12022989816SDimitry Andric     Out << "PostInitializer\", ";
121676fbe81SDimitry Andric     const CXXCtorInitializer *Init = castAs<PostInitializer>().getInitializer();
12222989816SDimitry Andric     if (const FieldDecl *FD = Init->getAnyMember()) {
12322989816SDimitry Andric       Out << "\"field_decl\": \"" << *FD << '\"';
12422989816SDimitry Andric     } else {
12522989816SDimitry Andric       Out << "\"type\": \"";
126676fbe81SDimitry Andric       QualType Ty = Init->getTypeSourceInfo()->getType();
127676fbe81SDimitry Andric       Ty = Ty.getLocalUnqualifiedType();
128676fbe81SDimitry Andric       Ty.print(Out, Context.getLangOpts());
12922989816SDimitry Andric       Out << '\"';
130676fbe81SDimitry Andric     }
131676fbe81SDimitry Andric     break;
132676fbe81SDimitry Andric   }
133676fbe81SDimitry Andric 
134676fbe81SDimitry Andric   case ProgramPoint::BlockEdgeKind: {
135676fbe81SDimitry Andric     const BlockEdge &E = castAs<BlockEdge>();
13622989816SDimitry Andric     const Stmt *T = E.getSrc()->getTerminatorStmt();
13722989816SDimitry Andric     Out << "Edge\", \"src_id\": " << E.getSrc()->getBlockID()
13822989816SDimitry Andric         << ", \"dst_id\": " << E.getDst()->getBlockID() << ", \"terminator\": ";
139676fbe81SDimitry Andric 
14022989816SDimitry Andric     if (!T) {
14122989816SDimitry Andric       Out << "null, \"term_kind\": null";
14222989816SDimitry Andric       break;
143676fbe81SDimitry Andric     }
144676fbe81SDimitry Andric 
14522989816SDimitry Andric     E.getSrc()->printTerminatorJson(Out, Context.getLangOpts(),
14622989816SDimitry Andric                                     /*AddQuotes=*/true);
14722989816SDimitry Andric     Out << ", \"location\": ";
14822989816SDimitry Andric     printSourceLocationAsJson(Out, T->getBeginLoc(), SM);
14922989816SDimitry Andric 
15022989816SDimitry Andric     Out << ", \"term_kind\": \"";
15122989816SDimitry Andric     if (isa<SwitchStmt>(T)) {
15222989816SDimitry Andric       Out << "SwitchStmt\", \"case\": ";
15322989816SDimitry Andric       if (const Stmt *Label = E.getDst()->getLabel()) {
15422989816SDimitry Andric         if (const auto *C = dyn_cast<CaseStmt>(Label)) {
15522989816SDimitry Andric           Out << "{ \"lhs\": ";
15622989816SDimitry Andric           if (const Stmt *LHS = C->getLHS()) {
15722989816SDimitry Andric             LHS->printJson(Out, nullptr, PP, AddQuotes);
15822989816SDimitry Andric           } else {
15922989816SDimitry Andric             Out << "null";
16022989816SDimitry Andric 	  }
16122989816SDimitry Andric 
16222989816SDimitry Andric           Out << ", \"rhs\": ";
16322989816SDimitry Andric           if (const Stmt *RHS = C->getRHS()) {
16422989816SDimitry Andric             RHS->printJson(Out, nullptr, PP, AddQuotes);
16522989816SDimitry Andric           } else {
16622989816SDimitry Andric             Out << "null";
16722989816SDimitry Andric           }
16822989816SDimitry Andric           Out << " }";
169676fbe81SDimitry Andric         } else {
170676fbe81SDimitry Andric           assert(isa<DefaultStmt>(Label));
17122989816SDimitry Andric           Out << "\"default\"";
172676fbe81SDimitry Andric         }
173676fbe81SDimitry Andric       } else {
17422989816SDimitry Andric         Out << "\"implicit default\"";
175676fbe81SDimitry Andric       }
17622989816SDimitry Andric     } else if (isa<IndirectGotoStmt>(T)) {
17722989816SDimitry Andric       // FIXME: More info.
17822989816SDimitry Andric       Out << "IndirectGotoStmt\"";
17922989816SDimitry Andric     } else {
18022989816SDimitry Andric       Out << "Condition\", \"value\": "
18122989816SDimitry Andric           << (*E.getSrc()->succ_begin() == E.getDst() ? "true" : "false");
182676fbe81SDimitry Andric     }
183676fbe81SDimitry Andric     break;
184676fbe81SDimitry Andric   }
185676fbe81SDimitry Andric 
186676fbe81SDimitry Andric   default: {
187676fbe81SDimitry Andric     const Stmt *S = castAs<StmtPoint>().getStmt();
188676fbe81SDimitry Andric     assert(S != nullptr && "Expecting non-null Stmt");
189676fbe81SDimitry Andric 
19022989816SDimitry Andric     Out << "Statement\", \"stmt_kind\": \"" << S->getStmtClassName()
19122989816SDimitry Andric         << "\", \"stmt_id\": " << S->getID(Context)
192519fc96cSDimitry Andric         << ", \"pointer\": \"" << (const void *)S << "\", ";
193519fc96cSDimitry Andric     if (const auto *CS = dyn_cast<CastExpr>(S))
194519fc96cSDimitry Andric       Out << "\"cast_kind\": \"" << CS->getCastKindName() << "\", ";
195519fc96cSDimitry Andric 
196519fc96cSDimitry Andric     Out << "\"pretty\": ";
197676fbe81SDimitry Andric 
19822989816SDimitry Andric     S->printJson(Out, nullptr, PP, AddQuotes);
19922989816SDimitry Andric 
20022989816SDimitry Andric     Out << ", \"location\": ";
20122989816SDimitry Andric     printSourceLocationAsJson(Out, S->getBeginLoc(), SM);
20222989816SDimitry Andric 
20322989816SDimitry Andric     Out << ", \"stmt_point_kind\": \"";
20422989816SDimitry Andric     if (getAs<PreLoad>())
20522989816SDimitry Andric       Out << "PreLoad";
20622989816SDimitry Andric     else if (getAs<PreStore>())
20722989816SDimitry Andric       Out << "PreStore";
208676fbe81SDimitry Andric     else if (getAs<PostAllocatorCall>())
20922989816SDimitry Andric       Out << "PostAllocatorCall";
21022989816SDimitry Andric     else if (getAs<PostCondition>())
21122989816SDimitry Andric       Out << "PostCondition";
21222989816SDimitry Andric     else if (getAs<PostLoad>())
21322989816SDimitry Andric       Out << "PostLoad";
21422989816SDimitry Andric     else if (getAs<PostLValue>())
21522989816SDimitry Andric       Out << "PostLValue";
21622989816SDimitry Andric     else if (getAs<PostStore>())
21722989816SDimitry Andric       Out << "PostStore";
21822989816SDimitry Andric     else if (getAs<PostStmt>())
21922989816SDimitry Andric       Out << "PostStmt";
22022989816SDimitry Andric     else if (getAs<PostStmtPurgeDeadSymbols>())
22122989816SDimitry Andric       Out << "PostStmtPurgeDeadSymbols";
22222989816SDimitry Andric     else if (getAs<PreStmtPurgeDeadSymbols>())
22322989816SDimitry Andric       Out << "PreStmtPurgeDeadSymbols";
22422989816SDimitry Andric     else if (getAs<PreStmt>())
22522989816SDimitry Andric       Out << "PreStmt";
22622989816SDimitry Andric     else {
22722989816SDimitry Andric       Out << "\nKind: '" << getKind();
22822989816SDimitry Andric       llvm_unreachable("' is unhandled StmtPoint kind!");
22922989816SDimitry Andric     }
230676fbe81SDimitry Andric 
23122989816SDimitry Andric     Out << '\"';
232676fbe81SDimitry Andric     break;
233676fbe81SDimitry Andric   }
234676fbe81SDimitry Andric   }
235676fbe81SDimitry Andric }
236676fbe81SDimitry Andric 
SimpleProgramPointTag(StringRef MsgProvider,StringRef Msg)2379f4dbff6SDimitry Andric SimpleProgramPointTag::SimpleProgramPointTag(StringRef MsgProvider,
2389f4dbff6SDimitry Andric                                              StringRef Msg)
2399f4dbff6SDimitry Andric   : Desc((MsgProvider + " : " + Msg).str()) {}
24036981b17SDimitry Andric 
getTagDescription() const24136981b17SDimitry Andric StringRef SimpleProgramPointTag::getTagDescription() const {
2429f4dbff6SDimitry Andric   return Desc;
24336981b17SDimitry Andric }
244