Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/ASTTypeTraits.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- ASTTypeTraits.cpp --------------------------------------*- 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
//  Provides a dynamic type identifier and a dynamically typed node container
10
//  that can be used to store an AST base node at runtime in the same storage in
11
//  a type safe way.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "clang/AST/ASTTypeTraits.h"
16
#include "clang/AST/ASTContext.h"
17
#include "clang/AST/Attr.h"
18
#include "clang/AST/DeclCXX.h"
19
#include "clang/AST/NestedNameSpecifier.h"
20
#include "clang/AST/OpenMPClause.h"
21
#include "clang/AST/TypeLoc.h"
22
23
using namespace clang;
24
25
const ASTNodeKind::KindInfo ASTNodeKind::AllKindInfo[] = {
26
    {NKI_None, "<None>"},
27
    {NKI_None, "TemplateArgument"},
28
    {NKI_None, "TemplateArgumentLoc"},
29
    {NKI_None, "LambdaCapture"},
30
    {NKI_None, "TemplateName"},
31
    {NKI_None, "NestedNameSpecifierLoc"},
32
    {NKI_None, "QualType"},
33
#define TYPELOC(CLASS, PARENT) {NKI_##PARENT, #CLASS "TypeLoc"},
34
#include "clang/AST/TypeLocNodes.def"
35
    {NKI_None, "TypeLoc"},
36
    {NKI_None, "CXXBaseSpecifier"},
37
    {NKI_None, "CXXCtorInitializer"},
38
    {NKI_None, "NestedNameSpecifier"},
39
    {NKI_None, "Decl"},
40
#define DECL(DERIVED, BASE) { NKI_##BASE, #DERIVED "Decl" },
41
#include "clang/AST/DeclNodes.inc"
42
    {NKI_None, "Stmt"},
43
#define STMT(DERIVED, BASE) { NKI_##BASE, #DERIVED },
44
#include "clang/AST/StmtNodes.inc"
45
    {NKI_None, "Type"},
46
#define TYPE(DERIVED, BASE) { NKI_##BASE, #DERIVED "Type" },
47
#include "clang/AST/TypeNodes.inc"
48
    {NKI_None, "OMPClause"},
49
#define GEN_CLANG_CLAUSE_CLASS
50
#define CLAUSE_CLASS(Enum, Str, Class) {NKI_OMPClause, #Class},
51
#include "llvm/Frontend/OpenMP/OMP.inc"
52
    {NKI_None, "Attr"},
53
#define ATTR(A) {NKI_Attr, #A "Attr"},
54
#include "clang/Basic/AttrList.inc"
55
};
56
57
5.07M
bool ASTNodeKind::isBaseOf(ASTNodeKind Other, unsigned *Distance) const {
58
5.07M
  return isBaseOf(KindId, Other.KindId, Distance);
59
5.07M
}
60
61
bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived,
62
5.07M
                           unsigned *Distance) {
63
5.07M
  if (Base == NKI_None || 
Derived == NKI_None5.07M
)
return false2
;
64
5.07M
  unsigned Dist = 0;
65
15.8M
  while (Derived != Base && 
Derived != NKI_None12.3M
) {
66
10.7M
    Derived = AllKindInfo[Derived].ParentId;
67
10.7M
    ++Dist;
68
10.7M
  }
69
5.07M
  if (Distance)
70
27.1k
    *Distance = Dist;
71
5.07M
  return Derived == Base;
72
5.07M
}
73
74
18
ASTNodeKind ASTNodeKind::getCladeKind() const {
75
18
  NodeKindId LastId = KindId;
76
49
  while (LastId) {
77
48
    NodeKindId ParentId = AllKindInfo[LastId].ParentId;
78
48
    if (ParentId == NKI_None)
79
17
      return LastId;
80
31
    LastId = ParentId;
81
31
  }
82
1
  return NKI_None;
83
18
}
84
85
2.75k
StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; }
86
87
ASTNodeKind ASTNodeKind::getMostDerivedType(ASTNodeKind Kind1,
88
1.08M
                                            ASTNodeKind Kind2) {
89
1.08M
  if (Kind1.isBaseOf(Kind2)) 
return Kind2948k
;
90
131k
  if (Kind2.isBaseOf(Kind1)) 
return Kind1131k
;
91
2
  return ASTNodeKind();
92
131k
}
93
94
ASTNodeKind ASTNodeKind::getMostDerivedCommonAncestor(ASTNodeKind Kind1,
95
5
                                                      ASTNodeKind Kind2) {
96
5
  NodeKindId Parent = Kind1.KindId;
97
12
  while (!isBaseOf(Parent, Kind2.KindId, nullptr) && 
Parent != NKI_None8
) {
98
7
    Parent = AllKindInfo[Parent].ParentId;
99
7
  }
100
5
  return ASTNodeKind(Parent);
101
5
}
102
103
416k
ASTNodeKind ASTNodeKind::getFromNode(const Decl &D) {
104
416k
  switch (D.getKind()) {
105
0
#define DECL(DERIVED, BASE)                                                    \
106
416k
    case Decl::DERIVED: return ASTNodeKind(NKI_##DERIVED##Decl);
107
0
#define ABSTRACT_DECL(D)
108
416k
#include 
"clang/AST/DeclNodes.inc"0
109
416k
  };
110
0
  llvm_unreachable("invalid decl kind");
111
0
}
112
113
693k
ASTNodeKind ASTNodeKind::getFromNode(const Stmt &S) {
114
693k
  switch (S.getStmtClass()) {
115
0
    case Stmt::NoStmtClass: return NKI_None;
116
0
#define STMT(CLASS, PARENT)                                                    \
117
693k
    case Stmt::CLASS##Class: return ASTNodeKind(NKI_##CLASS);
118
0
#define ABSTRACT_STMT(S)
119
693k
#include 
"clang/AST/StmtNodes.inc"0
120
693k
  }
121
0
  llvm_unreachable("invalid stmt kind");
122
0
}
123
124
43.2k
ASTNodeKind ASTNodeKind::getFromNode(const Type &T) {
125
43.2k
  switch (T.getTypeClass()) {
126
0
#define TYPE(Class, Base)                                                      \
127
43.2k
    case Type::Class: return ASTNodeKind(NKI_##Class##Type);
128
0
#define ABSTRACT_TYPE(Class, Base)
129
43.2k
#include 
"clang/AST/TypeNodes.inc"0
130
43.2k
  }
131
0
  llvm_unreachable("invalid type kind");
132
0
 }
133
134
79.9k
 ASTNodeKind ASTNodeKind::getFromNode(const TypeLoc &T) {
135
79.9k
   switch (T.getTypeLocClass()) {
136
0
#define ABSTRACT_TYPELOC(CLASS, PARENT)
137
0
#define TYPELOC(CLASS, PARENT)                                                 \
138
79.9k
  case TypeLoc::CLASS:                                                         \
139
79.9k
    return ASTNodeKind(NKI_##CLASS##TypeLoc);
140
79.9k
#include 
"clang/AST/TypeLocNodes.def"0
141
79.9k
   }
142
0
   llvm_unreachable("invalid typeloc kind");
143
0
 }
144
145
352
ASTNodeKind ASTNodeKind::getFromNode(const OMPClause &C) {
146
352
  switch (C.getClauseKind()) {
147
0
#define GEN_CLANG_CLAUSE_CLASS
148
0
#define CLAUSE_CLASS(Enum, Str, Class)                                         \
149
352
  case llvm::omp::Clause::Enum:                                                \
150
352
    return ASTNodeKind(NKI_##Class);
151
0
#define CLAUSE_NO_CLASS(Enum, Str)                                             \
152
0
  case llvm::omp::Clause::Enum:                                                \
153
0
    llvm_unreachable("unexpected OpenMP clause kind");
154
352
#include 
"llvm/Frontend/OpenMP/OMP.inc"0
155
352
  }
156
0
  llvm_unreachable("invalid omp clause kind");
157
0
}
158
159
1.03k
ASTNodeKind ASTNodeKind::getFromNode(const Attr &A) {
160
1.03k
  switch (A.getKind()) {
161
0
#define ATTR(A)                                                                \
162
1.03k
  case attr::A:                                                                \
163
1.03k
    return ASTNodeKind(NKI_##A##Attr);
164
1.03k
#include 
"clang/Basic/AttrList.inc"0
165
1.03k
  }
166
0
  llvm_unreachable("invalid attr kind");
167
0
}
168
169
void DynTypedNode::print(llvm::raw_ostream &OS,
170
8
                         const PrintingPolicy &PP) const {
171
8
  if (const TemplateArgument *TA = get<TemplateArgument>())
172
0
    TA->print(PP, OS, /*IncludeType*/ true);
173
8
  else if (const TemplateArgumentLoc *TAL = get<TemplateArgumentLoc>())
174
0
    TAL->getArgument().print(PP, OS, /*IncludeType*/ true);
175
8
  else if (const TemplateName *TN = get<TemplateName>())
176
0
    TN->print(OS, PP);
177
8
  else if (const NestedNameSpecifier *NNS = get<NestedNameSpecifier>())
178
0
    NNS->print(OS, PP);
179
8
  else if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>()) {
180
0
    if (const NestedNameSpecifier *NNS = NNSL->getNestedNameSpecifier())
181
0
      NNS->print(OS, PP);
182
0
    else
183
0
      OS << "(empty NestedNameSpecifierLoc)";
184
8
  } else if (const QualType *QT = get<QualType>())
185
6
    QT->print(OS, PP);
186
2
  else if (const TypeLoc *TL = get<TypeLoc>())
187
0
    TL->getType().print(OS, PP);
188
2
  else if (const Decl *D = get<Decl>())
189
1
    D->print(OS, PP);
190
1
  else if (const Stmt *S = get<Stmt>())
191
1
    S->printPretty(OS, nullptr, PP);
192
0
  else if (const Type *T = get<Type>())
193
0
    QualType(T, 0).print(OS, PP);
194
0
  else if (const Attr *A = get<Attr>())
195
0
    A->printPretty(OS, PP);
196
0
  else
197
0
    OS << "Unable to print values of type " << NodeKind.asStringRef() << "\n";
198
8
}
199
200
void DynTypedNode::dump(llvm::raw_ostream &OS,
201
2
                        const ASTContext &Context) const {
202
2
  if (const Decl *D = get<Decl>())
203
1
    D->dump(OS);
204
1
  else if (const Stmt *S = get<Stmt>())
205
1
    S->dump(OS, Context);
206
0
  else if (const Type *T = get<Type>())
207
0
    T->dump(OS, Context);
208
0
  else
209
0
    OS << "Unable to dump values of type " << NodeKind.asStringRef() << "\n";
210
2
}
211
212
342
SourceRange DynTypedNode::getSourceRange() const {
213
342
  if (const CXXCtorInitializer *CCI = get<CXXCtorInitializer>())
214
0
    return CCI->getSourceRange();
215
342
  if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
216
1
    return NNSL->getSourceRange();
217
341
  if (const TypeLoc *TL = get<TypeLoc>())
218
5
    return TL->getSourceRange();
219
336
  if (const Decl *D = get<Decl>())
220
82
    return D->getSourceRange();
221
254
  if (const Stmt *S = get<Stmt>())
222
253
    return S->getSourceRange();
223
1
  if (const TemplateArgumentLoc *TAL = get<TemplateArgumentLoc>())
224
0
    return TAL->getSourceRange();
225
1
  if (const auto *C = get<OMPClause>())
226
0
    return SourceRange(C->getBeginLoc(), C->getEndLoc());
227
1
  if (const auto *CBS = get<CXXBaseSpecifier>())
228
0
    return CBS->getSourceRange();
229
1
  if (const auto *A = get<Attr>())
230
1
    return A->getRange();
231
0
  return SourceRange();
232
1
}