[NFC][SSAF] Extract common code in Analyses to a shared file (#191932)
Created SSAFAnalysesCommon.h for the SSAF analyses implementation. --------- Co-authored-by: Balázs Benics <benicsbalazs@gmail.com>
This commit is contained in:
@@ -12,16 +12,6 @@
|
||||
#include "clang/ScalableStaticAnalysisFramework/Analyses/EntityPointerLevel/EntityPointerLevel.h"
|
||||
#include "clang/ScalableStaticAnalysisFramework/Core/Serialization/JSONFormat.h"
|
||||
|
||||
template <typename... Ts>
|
||||
llvm::Error makeSawButExpectedError(const llvm::json::Value &Saw,
|
||||
llvm::StringRef Expected,
|
||||
const Ts &...ExpectedArgs) {
|
||||
std::string Fmt = ("saw %s but expected " + Expected).str();
|
||||
std::string SawStr = llvm::formatv("{0:2}", Saw).str();
|
||||
|
||||
return llvm::createStringError(Fmt.c_str(), SawStr.c_str(), ExpectedArgs...);
|
||||
}
|
||||
|
||||
namespace clang::ssaf {
|
||||
llvm::json::Value
|
||||
entityPointerLevelToJSON(const EntityPointerLevel &EPL,
|
||||
|
||||
@@ -6,6 +6,7 @@ add_clang_library(clangScalableStaticAnalysisFrameworkAnalyses
|
||||
CallGraph/CallGraphExtractor.cpp
|
||||
CallGraph/CallGraphJSONFormat.cpp
|
||||
EntityPointerLevel/EntityPointerLevel.cpp
|
||||
SSAFAnalysesCommon.cpp
|
||||
UnsafeBufferUsage/UnsafeBufferUsage.cpp
|
||||
UnsafeBufferUsage/UnsafeBufferUsageExtractor.cpp
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "clang/ScalableStaticAnalysisFramework/Analyses/EntityPointerLevel/EntityPointerLevel.h"
|
||||
#include "SSAFAnalysesCommon.h"
|
||||
#include "clang/AST/ASTContext.h"
|
||||
#include "clang/AST/Decl.h"
|
||||
#include "clang/AST/StmtVisitor.h"
|
||||
@@ -18,25 +19,6 @@
|
||||
using namespace clang;
|
||||
using namespace ssaf;
|
||||
|
||||
namespace {
|
||||
template <typename DeclOrExpr> bool hasPtrOrArrType(const DeclOrExpr *E) {
|
||||
return llvm::isa<PointerType, ArrayType>(E->getType().getCanonicalType());
|
||||
}
|
||||
|
||||
template <typename NodeTy, typename... Ts>
|
||||
llvm::Error makeErrAtNode(ASTContext &Ctx, const NodeTy &N, StringRef Fmt,
|
||||
const Ts &...Args) {
|
||||
std::string LocStr = N.getBeginLoc().printToString(Ctx.getSourceManager());
|
||||
return llvm::createStringError((Fmt + " at %s").str().c_str(), Args...,
|
||||
LocStr.c_str());
|
||||
}
|
||||
|
||||
llvm::Error makeEntityNameErr(ASTContext &Ctx, const NamedDecl &D) {
|
||||
return makeErrAtNode(Ctx, D, "failed to create entity name for %s",
|
||||
D.getNameAsString().data());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace clang::ssaf {
|
||||
// Translate a pointer type expression 'E' to a (set of) EntityPointerLevel(s)
|
||||
// associated with the declared type of the base address of `E`. If the base
|
||||
@@ -64,7 +46,7 @@ class EntityPointerLevelTranslator
|
||||
|
||||
// Fallback method for all unsupported expression kind:
|
||||
llvm::Error fallback(const Stmt *E) {
|
||||
return makeErrAtNode(Ctx, *E,
|
||||
return makeErrAtNode(Ctx, E,
|
||||
"attempt to translate %s to EntityPointerLevels",
|
||||
E->getStmtClassName());
|
||||
}
|
||||
@@ -98,7 +80,7 @@ public:
|
||||
Expected<EntityPointerLevel> translate(const NamedDecl *D, bool IsRet) {
|
||||
if (IsRet && !isa<FunctionDecl>(D))
|
||||
return makeErrAtNode(
|
||||
Ctx, *D,
|
||||
Ctx, D,
|
||||
"attempt to call getEntityNameForReturn on a NamedDecl of %s kind",
|
||||
D->getDeclKindName());
|
||||
|
||||
@@ -107,7 +89,7 @@ public:
|
||||
: getEntityName(D);
|
||||
if (EN)
|
||||
return createEntityPointerLevelFor(*EN);
|
||||
return makeEntityNameErr(Ctx, *D);
|
||||
return makeEntityNameErr(Ctx, D);
|
||||
}
|
||||
|
||||
static EntityPointerLevel incrementPointerLevel(const EntityPointerLevel &E) {
|
||||
@@ -174,7 +156,7 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
// Translate((T*)base) -> Translate(p) if p has pointer type
|
||||
// Translate((T*)base) -> Translate(base) if base has pointer type
|
||||
// -> {} otherwise
|
||||
Expected<EntityPointerLevelSet> VisitCastExpr(const CastExpr *E) {
|
||||
if (hasPtrOrArrType(E->getSubExpr()))
|
||||
@@ -230,14 +212,14 @@ private:
|
||||
Expected<EntityPointerLevelSet> VisitDeclRefExpr(const DeclRefExpr *E) {
|
||||
if (auto EntityName = getEntityName(E->getDecl()))
|
||||
return EntityPointerLevelSet{createEntityPointerLevelFor(*EntityName)};
|
||||
return makeEntityNameErr(Ctx, *E->getDecl());
|
||||
return makeEntityNameErr(Ctx, E->getDecl());
|
||||
}
|
||||
|
||||
// Translate({., ->}f) -> {(MemberDecl, 1)}
|
||||
Expected<EntityPointerLevelSet> VisitMemberExpr(const MemberExpr *E) {
|
||||
if (auto EntityName = getEntityName(E->getMemberDecl()))
|
||||
return EntityPointerLevelSet{createEntityPointerLevelFor(*EntityName)};
|
||||
return makeEntityNameErr(Ctx, *E->getMemberDecl());
|
||||
return makeEntityNameErr(Ctx, E->getMemberDecl());
|
||||
}
|
||||
|
||||
Expected<EntityPointerLevelSet>
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
//===- SSAFAnalysesCommon.cpp ---------------------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "SSAFAnalysesCommon.h"
|
||||
#include "clang/AST/ASTContext.h"
|
||||
#include "clang/AST/Decl.h"
|
||||
|
||||
llvm::Error clang::ssaf::makeEntityNameErr(clang::ASTContext &Ctx,
|
||||
const clang::NamedDecl *D) {
|
||||
return makeErrAtNode(Ctx, D, "failed to create entity name for %s",
|
||||
D->getNameAsString().data());
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
//===- SSAFAnalysesCommon.h -------------------------------------*- C++ -*-===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Common code in SSAF analyses implementations
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#ifndef LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_ANALYSES_SSAFANALYSESCOMMON_H
|
||||
#define LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_ANALYSES_SSAFANALYSESCOMMON_H
|
||||
|
||||
#include "clang/AST/ASTContext.h"
|
||||
#include "clang/AST/Decl.h"
|
||||
#include "llvm/Support/JSON.h"
|
||||
|
||||
namespace clang::ssaf {
|
||||
template <typename NodeTy, typename... Ts>
|
||||
llvm::Error makeErrAtNode(clang::ASTContext &Ctx, const NodeTy *N,
|
||||
llvm::StringRef Fmt, const Ts &...Args) {
|
||||
std::string LocStr = N->getBeginLoc().printToString(Ctx.getSourceManager());
|
||||
return llvm::createStringError((Fmt + " at %s").str().c_str(), Args...,
|
||||
LocStr.c_str());
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
llvm::Error makeSawButExpectedError(const llvm::json::Value &Saw,
|
||||
llvm::StringRef Expected,
|
||||
const Ts &...ExpectedArgs) {
|
||||
std::string Fmt = ("saw %s but expected " + Expected).str();
|
||||
std::string SawStr = llvm::formatv("{0:2}", Saw).str();
|
||||
|
||||
return llvm::createStringError(Fmt.c_str(), SawStr.c_str(), ExpectedArgs...);
|
||||
}
|
||||
|
||||
template <typename DeclOrExpr> bool hasPtrOrArrType(const DeclOrExpr *E) {
|
||||
return llvm::isa<clang::PointerType, clang::ArrayType>(
|
||||
E->getType().getCanonicalType());
|
||||
}
|
||||
|
||||
llvm::Error makeEntityNameErr(clang::ASTContext &Ctx,
|
||||
const clang::NamedDecl *D);
|
||||
} // namespace clang::ssaf
|
||||
|
||||
#endif // LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_ANALYSES_SSAFANALYSESCOMMON_H
|
||||
@@ -7,6 +7,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "clang/ScalableStaticAnalysisFramework/Analyses/UnsafeBufferUsage/UnsafeBufferUsage.h"
|
||||
#include "SSAFAnalysesCommon.h"
|
||||
#include "clang/ScalableStaticAnalysisFramework/Analyses/EntityPointerLevel/EntityPointerLevel.h"
|
||||
#include "clang/ScalableStaticAnalysisFramework/Analyses/EntityPointerLevel/EntityPointerLevelFormat.h"
|
||||
#include "clang/ScalableStaticAnalysisFramework/Analyses/UnsafeBufferUsage/UnsafeBufferUsageTest.h"
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "SSAFAnalysesCommon.h"
|
||||
#include "clang/AST/ASTConsumer.h"
|
||||
#include "clang/AST/ASTContext.h"
|
||||
#include "clang/AST/DynamicRecursiveASTVisitor.h"
|
||||
@@ -26,15 +27,6 @@ namespace {
|
||||
using namespace clang;
|
||||
using namespace ssaf;
|
||||
|
||||
llvm::Error makeCreateEntityNameError(const NamedDecl *FailedDecl,
|
||||
ASTContext &Ctx) {
|
||||
std::string LocStr = FailedDecl->getSourceRange().getBegin().printToString(
|
||||
Ctx.getSourceManager());
|
||||
return llvm::createStringError(
|
||||
"failed to create entity name for %s declared at %s",
|
||||
FailedDecl->getNameAsString().c_str(), LocStr.c_str());
|
||||
}
|
||||
|
||||
Expected<EntityPointerLevelSet>
|
||||
buildEntityPointerLevels(std::set<const Expr *> &&UnsafePointers,
|
||||
ASTContext &Ctx,
|
||||
@@ -147,7 +139,7 @@ void clang::ssaf::UnsafeBufferUsageTUSummaryExtractor::HandleTranslationUnit(
|
||||
auto ContributorName = getEntityName(CD);
|
||||
|
||||
if (!ContributorName)
|
||||
llvm::reportFatalInternalError(makeCreateEntityNameError(CD, Ctx));
|
||||
llvm::reportFatalInternalError(makeEntityNameErr(Ctx, CD));
|
||||
|
||||
auto [Ignored, InsertionSucceeded] = SummaryBuilder.addSummary(
|
||||
addEntity(*ContributorName), std::move(*EntitySummary));
|
||||
|
||||
Reference in New Issue
Block a user