clang 23.0.0git
SSAFAnalysesCommon.h
Go to the documentation of this file.
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_SCALABLESTATICANALYSISFRAMEWORK_ANALYSES_SSAFANALYSESCOMMON_H
13#define LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_ANALYSES_SSAFANALYSESCOMMON_H
14
17#include "clang/AST/Decl.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
29namespace clang::ssaf {
30///\return a short descriptions of a json::Value
31std::string describeJSONValue(const llvm::json::Value &V);
32///\return a short descriptions of a json::Array
33std::string describeJSONValue(const llvm::json::Array &A);
34///\return a short descriptions of a json::Object
35std::string describeJSONValue(const llvm::json::Object &O);
36
37template <typename NodeTy, typename... Ts>
38llvm::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
45template <typename JSONTy, typename... Ts>
46llvm::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.
55inline bool hasPtrOrArrType(const Expr *E) {
56 return llvm::isa<clang::PointerType, clang::ArrayType>(
58}
59
60///\return true iff Decl `D` has (reference-to) pointer or array type.
61inline bool hasPtrOrArrType(const ValueDecl *D) {
62 return llvm::isa<clang::PointerType, clang::ArrayType>(
64}
65
67 const clang::NamedDecl *D);
68
69/// Log a warning from an llvm::Error
70inline void logWarningFromError(llvm::Error Err) {
71 DEBUG_WITH_TYPE("ssaf-analyses", llvm::errs() << Err);
72 llvm::consumeError(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.
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"
86void 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.
103template <typename ExtractorFnT>
105 TUSummaryBuilder &Builder, ASTContext &Ctx,
106 ExtractorFnT ExtractFn,
107 const char *ExtractorName = "") {
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(Cano)) {
124 if (!Builder.addSummary(*Id, std::move(Summary)).second)
126 Ctx, Cano, "dropping duplicate %s summary for entity %s",
127 ExtractorName, Cano->getNameAsString().c_str()));
128 } else
130 }
131}
132
133} // namespace clang::ssaf
134
135#endif // LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_ANALYSES_SSAFANALYSESCOMMON_H
Defines the clang::ASTContext interface.
#define V(N, I)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
SourceManager & getSourceManager()
Definition ASTContext.h:863
A dynamically typed AST node container.
This represents one expression.
Definition Expr.h:112
QualType getType() const
Definition Expr.h:144
This represents a decl that may have a name.
Definition Decl.h:274
QualType getNonReferenceType() const
If Type is a reference type (e.g., const int&), returns the type that the reference refers to ("const...
Definition TypeBase.h:8632
QualType getCanonicalType() const
Definition TypeBase.h:8499
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
QualType getType() const
Definition Decl.h:723
std::optional< EntityId > addEntity(const NamedDecl *D)
Creates EntityName from the Decl, registers the entity, and sets its linkage atomically.
bool hasPtrOrArrType(const Expr *E)
void findContributors(ASTContext &Ctx, llvm::DenseMap< const NamedDecl *, std::vector< const NamedDecl * > > &Contributors)
Find all contributors in an AST.
void logWarningFromError(llvm::Error Err)
Log a warning from an llvm::Error.
llvm::Error makeErrAtNode(clang::ASTContext &Ctx, const NodeTy *N, llvm::StringRef Fmt, const Ts &...Args)
llvm::Error makeEntityNameErr(clang::ASTContext &Ctx, const clang::NamedDecl *D)
void findMatchesIn(const NamedDecl *Contributor, llvm::function_ref< void(const DynTypedNode &)> MatchActionRef)
Perform "MatchAction" on each Stmt and Decl belonging to the Contributor.
llvm::Error makeSawButExpectedError(const JSONTy &Saw, llvm::StringRef Expected, const Ts &...ExpectedArgs)
std::string describeJSONValue(const llvm::json::Value &V)
void extractAndAddSummaries(TUSummaryExtractor &Extractor, TUSummaryBuilder &Builder, ASTContext &Ctx, ExtractorFnT ExtractFn, const char *ExtractorName="")
The standard contributor-summary extraction procedure: