clang 23.0.0git
TUSummaryExtractor.cpp
Go to the documentation of this file.
1//===- TUSummaryExtractor.cpp ---------------------------------------------===//
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
10#include "clang/AST/Decl.h"
11#include "clang/AST/DeclBase.h"
16#include "llvm/Support/Casting.h"
17#include <optional>
18
19using namespace clang;
20using namespace ssaf;
21
23 const auto *ND = dyn_cast<NamedDecl>(D);
24 if (!ND)
25 return EntityLinkageType::None;
26
27 // Parameters have no linkage in C++, but SSAF needs them to inherit
28 // the external linkage from their parent functions.
29 // Here is why:
30 // SSAF treats parameters as entities and may not always associate them back
31 // to their parent functions. Therefore, it needs to identify parameters of
32 // functions with external linkage across different TUs. Treating them as
33 // having no linkage (as in C++) causes the same parameter in different TUs
34 // to be assigned different EntityIDs. As a result, the behavior of the
35 // parameter across multiple TUs cannot be correlated.
36 if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) {
37 if (const auto *FD = llvm::dyn_cast_or_null<FunctionDecl>(
38 PVD->getParentFunctionOrMethod())) {
39 return getLinkageForDecl(FD);
40 }
41 }
42
43 switch (ND->getFormalLinkage()) {
44 case Linkage::Invalid: {
45 llvm_unreachable("Shouldn't be invalid");
46 }
47 case Linkage::None:
48 return EntityLinkageType::None;
50 return EntityLinkageType::Internal;
52 return EntityLinkageType::Internal;
54 return EntityLinkageType::Internal;
55 case Linkage::Module:
56 return EntityLinkageType::External;
58 return EntityLinkageType::External;
59 }
60 llvm_unreachable("Unhandled clang::Linkage kind");
61}
62
63std::optional<EntityId> TUSummaryExtractor::addEntity(const NamedDecl *D) {
64 auto Name = getEntityName(D);
65 if (!Name)
66 return std::nullopt;
67 return SummaryBuilder.addEntity(*Name, getLinkageForDecl(D));
68}
69
70std::optional<EntityId>
72 auto Name = getEntityNameForReturn(FD);
73 if (!Name)
74 return std::nullopt;
75 return SummaryBuilder.addEntity(*Name, getLinkageForDecl(FD));
76}
static EntityLinkageType getLinkageForDecl(const Decl *D)
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
Represents a function declaration or definition.
Definition Decl.h:2018
This represents a decl that may have a name.
Definition Decl.h:274
std::optional< EntityId > addEntityForReturn(const FunctionDecl *FD)
Creates EntityName for the return value of FD, registers the entity, and sets its linkage atomically.
std::optional< EntityId > addEntity(const NamedDecl *D)
Creates EntityName from the Decl, registers the entity, and sets its linkage atomically.
std::optional< EntityName > getEntityNameForReturn(const FunctionDecl *FD)
Maps return entity of a function to an EntityName.
std::optional< EntityName > getEntityName(const Decl *D)
Maps a declaration to an EntityName.
The JSON file list parser is used to communicate input to InstallAPI.
@ VisibleNone
No linkage according to the standard, but is visible from other translation units because of types de...
Definition Linkage.h:48
@ None
No linkage, which means that the entity is unique and can only be referred to from within its scope.
Definition Linkage.h:30
@ UniqueExternal
External linkage within a unique namespace.
Definition Linkage.h:44
@ Internal
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
Definition Linkage.h:35
@ External
External linkage, which indicates that the entity can be referred to from other translation units.
Definition Linkage.h:58
@ Module
Module linkage, which indicates that the entity can be referred to from other translation units withi...
Definition Linkage.h:54