Previously, USRGeneration was implemented by clangIndex. However, that poses too broad library layering constraints on the ever growing set of users of a tiny component of it, USRGeneration. This PR splits that into a small library, called: clangUnifiedSymbolResolution Anyone needing USRGeneration could simply link against this without pulling in everything from clangIndex. --- Importantly, clangIndex linked against clangFrontend to define its FrontendAction, and use some ASTUnit APIs. Some users may want to use USRGeneration but NOT depend on clangFrontend. This new clangUnifiedSymbolResolution library would solve such circular dependencies. PS: There were quite a few cases where libraries could just link against clangUnifiedSymbolResolution without linking to clangIndex. I've simplified those cases in this PR, to keep link deps minimal.
84 lines
2.3 KiB
C++
84 lines
2.3 KiB
C++
//===- ASTMapping.cpp - AST to SSAF Entity mapping --------------*- 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file implements utilities for mapping AST declarations to SSAF entities.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/Analysis/Scalable/ASTEntityMapping.h"
|
|
#include "clang/AST/Decl.h"
|
|
#include "clang/Analysis/Scalable/Model/BuildNamespace.h"
|
|
#include "clang/UnifiedSymbolResolution/USRGeneration.h"
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
namespace clang::ssaf {
|
|
|
|
std::optional<EntityName> getEntityName(const Decl *D) {
|
|
if (!D)
|
|
return std::nullopt;
|
|
|
|
if (D->isImplicit())
|
|
return std::nullopt;
|
|
|
|
if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->getBuiltinID())
|
|
return std::nullopt;
|
|
|
|
if (!isa<FunctionDecl, ParmVarDecl, VarDecl, FieldDecl, RecordDecl>(D))
|
|
return std::nullopt;
|
|
|
|
llvm::SmallString<16> Suffix;
|
|
const Decl *USRDecl = D;
|
|
|
|
// For parameters, use the parent function's USR with parameter index as
|
|
// suffix
|
|
if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) {
|
|
const auto *FD =
|
|
dyn_cast_or_null<FunctionDecl>(PVD->getParentFunctionOrMethod());
|
|
if (!FD)
|
|
return std::nullopt;
|
|
USRDecl = FD;
|
|
|
|
const auto ParamIdx = PVD->getFunctionScopeIndex();
|
|
llvm::raw_svector_ostream OS(Suffix);
|
|
// Parameter uses function's USR with 1-based index as suffix
|
|
OS << (ParamIdx + 1);
|
|
}
|
|
|
|
llvm::SmallString<128> USRBuf;
|
|
if (clang::index::generateUSRForDecl(USRDecl, USRBuf))
|
|
return std::nullopt;
|
|
|
|
if (USRBuf.empty())
|
|
return std::nullopt;
|
|
|
|
return EntityName(USRBuf.str(), Suffix, {});
|
|
}
|
|
|
|
std::optional<EntityName> getEntityNameForReturn(const FunctionDecl *FD) {
|
|
if (!FD)
|
|
return std::nullopt;
|
|
|
|
if (FD->isImplicit())
|
|
return std::nullopt;
|
|
|
|
if (FD->getBuiltinID())
|
|
return std::nullopt;
|
|
|
|
llvm::SmallString<128> USRBuf;
|
|
if (clang::index::generateUSRForDecl(FD, USRBuf)) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
if (USRBuf.empty())
|
|
return std::nullopt;
|
|
|
|
return EntityName(USRBuf.str(), /*Suffix=*/"0", /*Namespace=*/{});
|
|
}
|
|
|
|
} // namespace clang::ssaf
|