xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/Analysis/ProgramPoint.cpp (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg //==- ProgramPoint.cpp - Program Points for Path-Sensitive Analysis -*- C++ -*-/
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg //  This file defines the interface ProgramPoint, which identifies a
107330f729Sjoerg //  distinct location in a function.
117330f729Sjoerg //
127330f729Sjoerg //===----------------------------------------------------------------------===//
137330f729Sjoerg 
147330f729Sjoerg #include "clang/Analysis/ProgramPoint.h"
15*e038c9c4Sjoerg #include "clang/AST/ASTContext.h"
167330f729Sjoerg #include "clang/Basic/JsonSupport.h"
177330f729Sjoerg 
187330f729Sjoerg using namespace clang;
197330f729Sjoerg 
~ProgramPointTag()207330f729Sjoerg ProgramPointTag::~ProgramPointTag() {}
217330f729Sjoerg 
getProgramPoint(const Stmt * S,ProgramPoint::Kind K,const LocationContext * LC,const ProgramPointTag * tag)227330f729Sjoerg ProgramPoint ProgramPoint::getProgramPoint(const Stmt *S, ProgramPoint::Kind K,
237330f729Sjoerg                                            const LocationContext *LC,
247330f729Sjoerg                                            const ProgramPointTag *tag){
257330f729Sjoerg   switch (K) {
267330f729Sjoerg     default:
277330f729Sjoerg       llvm_unreachable("Unhandled ProgramPoint kind");
287330f729Sjoerg     case ProgramPoint::PreStmtKind:
297330f729Sjoerg       return PreStmt(S, LC, tag);
307330f729Sjoerg     case ProgramPoint::PostStmtKind:
317330f729Sjoerg       return PostStmt(S, LC, tag);
327330f729Sjoerg     case ProgramPoint::PreLoadKind:
337330f729Sjoerg       return PreLoad(S, LC, tag);
347330f729Sjoerg     case ProgramPoint::PostLoadKind:
357330f729Sjoerg       return PostLoad(S, LC, tag);
367330f729Sjoerg     case ProgramPoint::PreStoreKind:
377330f729Sjoerg       return PreStore(S, LC, tag);
387330f729Sjoerg     case ProgramPoint::PostLValueKind:
397330f729Sjoerg       return PostLValue(S, LC, tag);
407330f729Sjoerg     case ProgramPoint::PostStmtPurgeDeadSymbolsKind:
417330f729Sjoerg       return PostStmtPurgeDeadSymbols(S, LC, tag);
427330f729Sjoerg     case ProgramPoint::PreStmtPurgeDeadSymbolsKind:
437330f729Sjoerg       return PreStmtPurgeDeadSymbols(S, LC, tag);
447330f729Sjoerg   }
457330f729Sjoerg }
467330f729Sjoerg 
dump() const477330f729Sjoerg LLVM_DUMP_METHOD void ProgramPoint::dump() const {
487330f729Sjoerg   return printJson(llvm::errs());
497330f729Sjoerg }
507330f729Sjoerg 
printJson(llvm::raw_ostream & Out,const char * NL) const517330f729Sjoerg void ProgramPoint::printJson(llvm::raw_ostream &Out, const char *NL) const {
527330f729Sjoerg   const ASTContext &Context =
537330f729Sjoerg       getLocationContext()->getAnalysisDeclContext()->getASTContext();
547330f729Sjoerg   const SourceManager &SM = Context.getSourceManager();
557330f729Sjoerg   const PrintingPolicy &PP = Context.getPrintingPolicy();
567330f729Sjoerg   const bool AddQuotes = true;
577330f729Sjoerg 
587330f729Sjoerg   Out << "\"kind\": \"";
597330f729Sjoerg   switch (getKind()) {
607330f729Sjoerg   case ProgramPoint::BlockEntranceKind:
617330f729Sjoerg     Out << "BlockEntrance\""
627330f729Sjoerg         << ", \"block_id\": "
637330f729Sjoerg         << castAs<BlockEntrance>().getBlock()->getBlockID();
647330f729Sjoerg     break;
657330f729Sjoerg 
667330f729Sjoerg   case ProgramPoint::FunctionExitKind: {
677330f729Sjoerg     auto FEP = getAs<FunctionExitPoint>();
687330f729Sjoerg     Out << "FunctionExit\""
697330f729Sjoerg         << ", \"block_id\": " << FEP->getBlock()->getBlockID()
707330f729Sjoerg         << ", \"stmt_id\": ";
717330f729Sjoerg 
727330f729Sjoerg     if (const ReturnStmt *RS = FEP->getStmt()) {
737330f729Sjoerg       Out << RS->getID(Context) << ", \"stmt\": ";
747330f729Sjoerg       RS->printJson(Out, nullptr, PP, AddQuotes);
757330f729Sjoerg     } else {
767330f729Sjoerg       Out << "null, \"stmt\": null";
777330f729Sjoerg     }
787330f729Sjoerg     break;
797330f729Sjoerg   }
807330f729Sjoerg   case ProgramPoint::BlockExitKind:
817330f729Sjoerg     llvm_unreachable("BlockExitKind");
827330f729Sjoerg     break;
837330f729Sjoerg   case ProgramPoint::CallEnterKind:
847330f729Sjoerg     Out << "CallEnter\"";
857330f729Sjoerg     break;
867330f729Sjoerg   case ProgramPoint::CallExitBeginKind:
877330f729Sjoerg     Out << "CallExitBegin\"";
887330f729Sjoerg     break;
897330f729Sjoerg   case ProgramPoint::CallExitEndKind:
907330f729Sjoerg     Out << "CallExitEnd\"";
917330f729Sjoerg     break;
927330f729Sjoerg   case ProgramPoint::EpsilonKind:
937330f729Sjoerg     Out << "EpsilonPoint\"";
947330f729Sjoerg     break;
957330f729Sjoerg 
967330f729Sjoerg   case ProgramPoint::LoopExitKind:
977330f729Sjoerg     Out << "LoopExit\", \"stmt\": \""
987330f729Sjoerg         << castAs<LoopExit>().getLoopStmt()->getStmtClassName() << '\"';
997330f729Sjoerg     break;
1007330f729Sjoerg 
1017330f729Sjoerg   case ProgramPoint::PreImplicitCallKind: {
1027330f729Sjoerg     ImplicitCallPoint PC = castAs<ImplicitCallPoint>();
1037330f729Sjoerg     Out << "PreCall\", \"decl\": \""
1047330f729Sjoerg         << PC.getDecl()->getAsFunction()->getQualifiedNameAsString()
1057330f729Sjoerg         << "\", \"location\": ";
1067330f729Sjoerg     printSourceLocationAsJson(Out, PC.getLocation(), SM);
1077330f729Sjoerg     break;
1087330f729Sjoerg   }
1097330f729Sjoerg 
1107330f729Sjoerg   case ProgramPoint::PostImplicitCallKind: {
1117330f729Sjoerg     ImplicitCallPoint PC = castAs<ImplicitCallPoint>();
1127330f729Sjoerg     Out << "PostCall\", \"decl\": \""
1137330f729Sjoerg         << PC.getDecl()->getAsFunction()->getQualifiedNameAsString()
1147330f729Sjoerg         << "\", \"location\": ";
1157330f729Sjoerg     printSourceLocationAsJson(Out, PC.getLocation(), SM);
1167330f729Sjoerg     break;
1177330f729Sjoerg   }
1187330f729Sjoerg 
1197330f729Sjoerg   case ProgramPoint::PostInitializerKind: {
1207330f729Sjoerg     Out << "PostInitializer\", ";
1217330f729Sjoerg     const CXXCtorInitializer *Init = castAs<PostInitializer>().getInitializer();
1227330f729Sjoerg     if (const FieldDecl *FD = Init->getAnyMember()) {
1237330f729Sjoerg       Out << "\"field_decl\": \"" << *FD << '\"';
1247330f729Sjoerg     } else {
1257330f729Sjoerg       Out << "\"type\": \"";
1267330f729Sjoerg       QualType Ty = Init->getTypeSourceInfo()->getType();
1277330f729Sjoerg       Ty = Ty.getLocalUnqualifiedType();
1287330f729Sjoerg       Ty.print(Out, Context.getLangOpts());
1297330f729Sjoerg       Out << '\"';
1307330f729Sjoerg     }
1317330f729Sjoerg     break;
1327330f729Sjoerg   }
1337330f729Sjoerg 
1347330f729Sjoerg   case ProgramPoint::BlockEdgeKind: {
1357330f729Sjoerg     const BlockEdge &E = castAs<BlockEdge>();
1367330f729Sjoerg     const Stmt *T = E.getSrc()->getTerminatorStmt();
1377330f729Sjoerg     Out << "Edge\", \"src_id\": " << E.getSrc()->getBlockID()
1387330f729Sjoerg         << ", \"dst_id\": " << E.getDst()->getBlockID() << ", \"terminator\": ";
1397330f729Sjoerg 
1407330f729Sjoerg     if (!T) {
1417330f729Sjoerg       Out << "null, \"term_kind\": null";
1427330f729Sjoerg       break;
1437330f729Sjoerg     }
1447330f729Sjoerg 
1457330f729Sjoerg     E.getSrc()->printTerminatorJson(Out, Context.getLangOpts(),
1467330f729Sjoerg                                     /*AddQuotes=*/true);
1477330f729Sjoerg     Out << ", \"location\": ";
1487330f729Sjoerg     printSourceLocationAsJson(Out, T->getBeginLoc(), SM);
1497330f729Sjoerg 
1507330f729Sjoerg     Out << ", \"term_kind\": \"";
1517330f729Sjoerg     if (isa<SwitchStmt>(T)) {
1527330f729Sjoerg       Out << "SwitchStmt\", \"case\": ";
1537330f729Sjoerg       if (const Stmt *Label = E.getDst()->getLabel()) {
1547330f729Sjoerg         if (const auto *C = dyn_cast<CaseStmt>(Label)) {
1557330f729Sjoerg           Out << "{ \"lhs\": ";
1567330f729Sjoerg           if (const Stmt *LHS = C->getLHS()) {
1577330f729Sjoerg             LHS->printJson(Out, nullptr, PP, AddQuotes);
1587330f729Sjoerg           } else {
1597330f729Sjoerg             Out << "null";
1607330f729Sjoerg 	  }
1617330f729Sjoerg 
1627330f729Sjoerg           Out << ", \"rhs\": ";
1637330f729Sjoerg           if (const Stmt *RHS = C->getRHS()) {
1647330f729Sjoerg             RHS->printJson(Out, nullptr, PP, AddQuotes);
1657330f729Sjoerg           } else {
1667330f729Sjoerg             Out << "null";
1677330f729Sjoerg           }
1687330f729Sjoerg           Out << " }";
1697330f729Sjoerg         } else {
1707330f729Sjoerg           assert(isa<DefaultStmt>(Label));
1717330f729Sjoerg           Out << "\"default\"";
1727330f729Sjoerg         }
1737330f729Sjoerg       } else {
1747330f729Sjoerg         Out << "\"implicit default\"";
1757330f729Sjoerg       }
1767330f729Sjoerg     } else if (isa<IndirectGotoStmt>(T)) {
1777330f729Sjoerg       // FIXME: More info.
1787330f729Sjoerg       Out << "IndirectGotoStmt\"";
1797330f729Sjoerg     } else {
1807330f729Sjoerg       Out << "Condition\", \"value\": "
1817330f729Sjoerg           << (*E.getSrc()->succ_begin() == E.getDst() ? "true" : "false");
1827330f729Sjoerg     }
1837330f729Sjoerg     break;
1847330f729Sjoerg   }
1857330f729Sjoerg 
1867330f729Sjoerg   default: {
1877330f729Sjoerg     const Stmt *S = castAs<StmtPoint>().getStmt();
1887330f729Sjoerg     assert(S != nullptr && "Expecting non-null Stmt");
1897330f729Sjoerg 
1907330f729Sjoerg     Out << "Statement\", \"stmt_kind\": \"" << S->getStmtClassName()
1917330f729Sjoerg         << "\", \"stmt_id\": " << S->getID(Context)
1927330f729Sjoerg         << ", \"pointer\": \"" << (const void *)S << "\", ";
1937330f729Sjoerg     if (const auto *CS = dyn_cast<CastExpr>(S))
1947330f729Sjoerg       Out << "\"cast_kind\": \"" << CS->getCastKindName() << "\", ";
1957330f729Sjoerg 
1967330f729Sjoerg     Out << "\"pretty\": ";
1977330f729Sjoerg 
1987330f729Sjoerg     S->printJson(Out, nullptr, PP, AddQuotes);
1997330f729Sjoerg 
2007330f729Sjoerg     Out << ", \"location\": ";
2017330f729Sjoerg     printSourceLocationAsJson(Out, S->getBeginLoc(), SM);
2027330f729Sjoerg 
2037330f729Sjoerg     Out << ", \"stmt_point_kind\": \"";
2047330f729Sjoerg     if (getAs<PreLoad>())
2057330f729Sjoerg       Out << "PreLoad";
2067330f729Sjoerg     else if (getAs<PreStore>())
2077330f729Sjoerg       Out << "PreStore";
2087330f729Sjoerg     else if (getAs<PostAllocatorCall>())
2097330f729Sjoerg       Out << "PostAllocatorCall";
2107330f729Sjoerg     else if (getAs<PostCondition>())
2117330f729Sjoerg       Out << "PostCondition";
2127330f729Sjoerg     else if (getAs<PostLoad>())
2137330f729Sjoerg       Out << "PostLoad";
2147330f729Sjoerg     else if (getAs<PostLValue>())
2157330f729Sjoerg       Out << "PostLValue";
2167330f729Sjoerg     else if (getAs<PostStore>())
2177330f729Sjoerg       Out << "PostStore";
2187330f729Sjoerg     else if (getAs<PostStmt>())
2197330f729Sjoerg       Out << "PostStmt";
2207330f729Sjoerg     else if (getAs<PostStmtPurgeDeadSymbols>())
2217330f729Sjoerg       Out << "PostStmtPurgeDeadSymbols";
2227330f729Sjoerg     else if (getAs<PreStmtPurgeDeadSymbols>())
2237330f729Sjoerg       Out << "PreStmtPurgeDeadSymbols";
2247330f729Sjoerg     else if (getAs<PreStmt>())
2257330f729Sjoerg       Out << "PreStmt";
2267330f729Sjoerg     else {
2277330f729Sjoerg       Out << "\nKind: '" << getKind();
2287330f729Sjoerg       llvm_unreachable("' is unhandled StmtPoint kind!");
2297330f729Sjoerg     }
2307330f729Sjoerg 
2317330f729Sjoerg     Out << '\"';
2327330f729Sjoerg     break;
2337330f729Sjoerg   }
2347330f729Sjoerg   }
2357330f729Sjoerg }
2367330f729Sjoerg 
SimpleProgramPointTag(StringRef MsgProvider,StringRef Msg)2377330f729Sjoerg SimpleProgramPointTag::SimpleProgramPointTag(StringRef MsgProvider,
2387330f729Sjoerg                                              StringRef Msg)
2397330f729Sjoerg   : Desc((MsgProvider + " : " + Msg).str()) {}
2407330f729Sjoerg 
getTagDescription() const2417330f729Sjoerg StringRef SimpleProgramPointTag::getTagDescription() const {
2427330f729Sjoerg   return Desc;
2437330f729Sjoerg }
244