| 1 | //===- SSAFAnalysesCommon.h -------------------------------------*- C++ -*-===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Common code in SSAF analyses implementations |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | #ifndef LLVM_CLANG_SCALABLESTATICANALYSIS_ANALYSES_SSAFANALYSESCOMMON_H |
| 13 | #define LLVM_CLANG_SCALABLESTATICANALYSIS_ANALYSES_SSAFANALYSESCOMMON_H |
| 14 | |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/ASTTypeTraits.h" |
| 17 | #include "clang/AST/Decl.h" |
| 18 | #include "clang/ScalableStaticAnalysis/Core/Model/EntityId.h" |
| 19 | #include "clang/ScalableStaticAnalysis/Core/TUSummary/TUSummaryBuilder.h" |
| 20 | #include "clang/ScalableStaticAnalysis/Core/TUSummary/TUSummaryExtractor.h" |
| 21 | #include "llvm/ADT/DenseMap.h" |
| 22 | #include "llvm/ADT/STLFunctionalExtras.h" |
| 23 | #include "llvm/Support/Debug.h" |
| 24 | #include "llvm/Support/Error.h" |
| 25 | #include "llvm/Support/JSON.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | #include <memory> |
| 28 | |
| 29 | namespace clang::ssaf { |
| 30 | ///\return a short descriptions of a json::Value |
| 31 | std::string describeJSONValue(const llvm::json::Value &V); |
| 32 | ///\return a short descriptions of a json::Array |
| 33 | std::string describeJSONValue(const llvm::json::Array &A); |
| 34 | ///\return a short descriptions of a json::Object |
| 35 | std::string describeJSONValue(const llvm::json::Object &O); |
| 36 | |
| 37 | template <typename NodeTy, typename... Ts> |
| 38 | llvm::Error makeErrAtNode(clang::ASTContext &Ctx, const NodeTy *N, |
| 39 | llvm::StringRef Fmt, const Ts &...Args) { |
| 40 | std::string LocStr = N->getBeginLoc().printToString(Ctx.getSourceManager()); |
| 41 | return llvm::createStringError((Fmt + " at %s" ).str().c_str(), Args..., |
| 42 | LocStr.c_str()); |
| 43 | } |
| 44 | |
| 45 | template <typename JSONTy, typename... Ts> |
| 46 | llvm::Error makeSawButExpectedError(const JSONTy &Saw, llvm::StringRef Expected, |
| 47 | const Ts &...ExpectedArgs) { |
| 48 | std::string Fmt = ("saw %s but expected " + Expected).str(); |
| 49 | std::string SawStr = describeJSONValue(Saw); |
| 50 | |
| 51 | return llvm::createStringError(Fmt.c_str(), SawStr.c_str(), ExpectedArgs...); |
| 52 | } |
| 53 | |
| 54 | ///\return true iff expression `E` has pointer or array type. |
| 55 | inline bool hasPtrOrArrType(const Expr *E) { |
| 56 | return llvm::isa<clang::PointerType, clang::ArrayType>( |
| 57 | Val: E->getType().getCanonicalType()); |
| 58 | } |
| 59 | |
| 60 | ///\return true iff Decl `D` has (reference-to) pointer or array type. |
| 61 | inline bool hasPtrOrArrType(const ValueDecl *D) { |
| 62 | return llvm::isa<clang::PointerType, clang::ArrayType>( |
| 63 | Val: D->getType().getNonReferenceType().getCanonicalType()); |
| 64 | } |
| 65 | |
| 66 | llvm::Error makeEntityNameErr(clang::ASTContext &Ctx, |
| 67 | const clang::NamedDecl *D); |
| 68 | |
| 69 | /// Log a warning from an llvm::Error |
| 70 | inline void logWarningFromError(llvm::Error Err) { |
| 71 | DEBUG_WITH_TYPE("ssaf-analyses" , llvm::errs() << Err); |
| 72 | llvm::consumeError(Err: std::move(Err)); |
| 73 | } |
| 74 | |
| 75 | /// Find all contributors in an AST. The found contributors are organized as a |
| 76 | /// map from the canonical declaration of each entity to all of its |
| 77 | /// declarations. |
| 78 | void findContributors( |
| 79 | ASTContext &Ctx, |
| 80 | llvm::DenseMap<const NamedDecl *, std::vector<const NamedDecl *>> |
| 81 | &Contributors); |
| 82 | |
| 83 | /// Perform "MatchAction" on each Stmt and Decl belonging to the `Contributor`. |
| 84 | /// \param Contributor |
| 85 | /// \param MatchActionRef a reference (view) to a "MatchAction" |
| 86 | void findMatchesIn( |
| 87 | const NamedDecl *Contributor, |
| 88 | llvm::function_ref<void(const DynTypedNode &)> MatchActionRef); |
| 89 | |
| 90 | /// The standard contributor-summary extraction procedure: |
| 91 | /// 1. Find and group all contributor decls by their canonical decls. |
| 92 | /// 2. Use \p Extract to get an EntitySummary of a contributor from all of its |
| 93 | /// decls. |
| 94 | /// 3. Insert the EntitySummary into the \p Builder. |
| 95 | /// |
| 96 | /// \param ExtractorFnT the template parameter that should be a function type |
| 97 | /// 'std::unique_ptr<SummaryT>(std::vector<const NamedDecl *>)' for different |
| 98 | /// entity summary type `SummaryT`s |
| 99 | /// \param ExtractFn The function that extracts summaries of a contributor from |
| 100 | /// its decls. |
| 101 | /// \param ExtractorName The optional information inserted into the warning |
| 102 | /// message when duplicate contributor names (EntityNames) are seen. |
| 103 | template <typename ExtractorFnT> |
| 104 | void extractAndAddSummaries(TUSummaryExtractor &, |
| 105 | TUSummaryBuilder &Builder, ASTContext &Ctx, |
| 106 | ExtractorFnT , |
| 107 | const char * = "" ) { |
| 108 | llvm::DenseMap<const NamedDecl *, std::vector<const NamedDecl *>> |
| 109 | Contributors; |
| 110 | findContributors(Ctx, Contributors); |
| 111 | for (const auto &[Cano, Decls] : Contributors) { |
| 112 | // Templates are skipped, but their instantiations are handled. The idea |
| 113 | // is that we can conclude facts about a template through all of its |
| 114 | // instantiations. |
| 115 | if (Cano->isTemplated()) |
| 116 | continue; |
| 117 | |
| 118 | auto Summary = ExtractFn(Decls); |
| 119 | assert(Summary); |
| 120 | if (Summary->empty()) |
| 121 | continue; |
| 122 | |
| 123 | if (auto Id = Extractor.addEntity(D: Cano)) { |
| 124 | if (!Builder.addSummary(*Id, std::move(Summary)).second) |
| 125 | logWarningFromError(Err: makeErrAtNode( |
| 126 | Ctx, N: Cano, Fmt: "dropping duplicate %s summary for entity %s" , |
| 127 | Args: ExtractorName, Args: Cano->getNameAsString().c_str())); |
| 128 | } else |
| 129 | logWarningFromError(Err: makeEntityNameErr(Ctx, D: Cano)); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | } // namespace clang::ssaf |
| 134 | |
| 135 | #endif // LLVM_CLANG_SCALABLESTATICANALYSIS_ANALYSES_SSAFANALYSESCOMMON_H |
| 136 | |