Coverage Report

Created: 2021-09-21 08:58

/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
22
using namespace clang;
23
24
const ASTNodeKind::KindInfo ASTNodeKind::AllKindInfo[] = {
25
    {NKI_None, "<None>"},
26
    {NKI_None, "TemplateArgument"},
27
    {NKI_None, "TemplateArgumentLoc"},
28
    {NKI_None, "TemplateName"},
29
    {NKI_None, "NestedNameSpecifierLoc"},
30
    {NKI_None, "QualType"},
31
    {NKI_None, "TypeLoc"},
32
    {NKI_None, "CXXBaseSpecifier"},
33
    {NKI_None, "CXXCtorInitializer"},
34
    {NKI_None, "NestedNameSpecifier"},
35
    {NKI_None, "Decl"},
36
#define DECL(DERIVED, BASE) { NKI_##BASE, #DERIVED "Decl" },
37
#include "clang/AST/DeclNodes.inc"
38
    {NKI_None, "Stmt"},
39
#define STMT(DERIVED, BASE) { NKI_##BASE, #DERIVED },
40
#include "clang/AST/StmtNodes.inc"
41
    {NKI_None, "Type"},
42
#define TYPE(DERIVED, BASE) { NKI_##BASE, #DERIVED "Type" },
43
#include "clang/AST/TypeNodes.inc"
44
    {NKI_None, "OMPClause"},
45
#define GEN_CLANG_CLAUSE_CLASS
46
#define CLAUSE_CLASS(Enum, Str, Class) {NKI_OMPClause, #Class},
47
#include "llvm/Frontend/OpenMP/OMP.inc"
48
    {NKI_None, "Attr"},
49
#define ATTR(A) {NKI_Attr, #A "Attr"},
50
#include "clang/Basic/AttrList.inc"
51
};
52
53
4.59M
bool ASTNodeKind::isBaseOf(ASTNodeKind Other, unsigned *Distance) const {
54
4.59M
  return isBaseOf(KindId, Other.KindId, Distance);
55
4.59M
}
56
57
bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived,
58
4.59M
                           unsigned *Distance) {
59
4.59M
  if (Base == NKI_None || 
Derived == NKI_None4.59M
)
return false2
;
60
4.59M
  unsigned Dist = 0;
61
13.5M
  while (Derived != Base && 
Derived != NKI_None10.2M
) {
62
8.95M
    Derived = AllKindInfo[Derived].ParentId;
63
8.95M
    ++Dist;
64
8.95M
  }
65
4.59M
  if (Distance)
66
26.4k
    *Distance = Dist;
67
4.59M
  return Derived == Base;
68
4.59M
}
69
70
18
ASTNodeKind ASTNodeKind::getCladeKind() const {
71
18
  NodeKindId LastId = KindId;
72
49
  while (LastId) {
73
48
    NodeKindId ParentId = AllKindInfo[LastId].ParentId;
74
48
    if (ParentId == NKI_None)
75
17
      return LastId;
76
31
    LastId = ParentId;
77
31
  }
78
1
  return NKI_None;
79
18
}
80
81
2.71k
StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; }
82
83
ASTNodeKind ASTNodeKind::getMostDerivedType(ASTNodeKind Kind1,
84
1.06M
                                            ASTNodeKind Kind2) {
85
1.06M
  if (Kind1.isBaseOf(Kind2)) 
return Kind2930k
;
86
130k
  if (Kind2.isBaseOf(Kind1)) 
return Kind1130k
;
87
2
  return ASTNodeKind();
88
130k
}
89
90
ASTNodeKind ASTNodeKind::getMostDerivedCommonAncestor(ASTNodeKind Kind1,
91
5
                                                      ASTNodeKind Kind2) {
92
5
  NodeKindId Parent = Kind1.KindId;
93
12
  while (!isBaseOf(Parent, Kind2.KindId, nullptr) && 
Parent != NKI_None8
) {
94
7
    Parent = AllKindInfo[Parent].ParentId;
95
7
  }
96
5
  return ASTNodeKind(Parent);
97
5
}
98
99
379k
ASTNodeKind ASTNodeKind::getFromNode(const Decl &D) {
100
379k
  switch (D.getKind()) {
101
0
#define DECL(DERIVED, BASE)                                                    \
102
379k
    case Decl::DERIVED: return ASTNodeKind(NKI_##DERIVED##Decl);
103
0
#define ABSTRACT_DECL(D)
104
379k
#include 
"clang/AST/DeclNodes.inc"0
105
379k
  };
106
0
  llvm_unreachable("invalid decl kind");
107
0
}
108
109
679k
ASTNodeKind ASTNodeKind::getFromNode(const Stmt &S) {
110
679k
  switch (S.getStmtClass()) {
111
0
    case Stmt::NoStmtClass: return NKI_None;
112
0
#define STMT(CLASS, PARENT)                                                    \
113
679k
    case Stmt::CLASS##Class: return ASTNodeKind(NKI_##CLASS);
114
0
#define ABSTRACT_STMT(S)
115
679k
#include 
"clang/AST/StmtNodes.inc"0
116
679k
  }
117
0
  llvm_unreachable("invalid stmt kind");
118
0
}
119
120
42.9k
ASTNodeKind ASTNodeKind::getFromNode(const Type &T) {
121
42.9k
  switch (T.getTypeClass()) {
122
0
#define TYPE(Class, Base)                                                      \
123
42.9k
    case Type::Class: return ASTNodeKind(NKI_##Class##Type);
124
0
#define ABSTRACT_TYPE(Class, Base)
125
42.9k
#include 
"clang/AST/TypeNodes.inc"0
126
42.9k
  }
127
0
  llvm_unreachable("invalid type kind");
128
0
 }
129
130
352
ASTNodeKind ASTNodeKind::getFromNode(const OMPClause &C) {
131
352
  switch (C.getClauseKind()) {
132
0
#define GEN_CLANG_CLAUSE_CLASS
133
0
#define CLAUSE_CLASS(Enum, Str, Class)                                         \
134
352
  case llvm::omp::Clause::Enum:                                                \
135
352
    return ASTNodeKind(NKI_##Class);
136
0
#define CLAUSE_NO_CLASS(Enum, Str)                                             \
137
0
  case llvm::omp::Clause::Enum:                                                \
138
0
    llvm_unreachable("unexpected OpenMP clause kind");
139
352
#include 
"llvm/Frontend/OpenMP/OMP.inc"0
140
352
  }
141
0
  llvm_unreachable("invalid omp clause kind");
142
0
}
143
144
1.00k
ASTNodeKind ASTNodeKind::getFromNode(const Attr &A) {
145
1.00k
  switch (A.getKind()) {
146
0
#define ATTR(A)                                                                \
147
1.00k
  case attr::A:                                                                \
148
1.00k
    return ASTNodeKind(NKI_##A##Attr);
149
1.00k
#include 
"clang/Basic/AttrList.inc"0
150
1.00k
  }
151
0
  llvm_unreachable("invalid attr kind");
152
0
}
153
154
void DynTypedNode::print(llvm::raw_ostream &OS,
155
8
                         const PrintingPolicy &PP) const {
156
8
  if (const TemplateArgument *TA = get<TemplateArgument>())
157
0
    TA->print(PP, OS, /*IncludeType*/ true);
158
8
  else if (const TemplateArgumentLoc *TAL = get<TemplateArgumentLoc>())
159
0
    TAL->getArgument().print(PP, OS, /*IncludeType*/ true);
160
8
  else if (const TemplateName *TN = get<TemplateName>())
161
0
    TN->print(OS, PP);
162
8
  else if (const NestedNameSpecifier *NNS = get<NestedNameSpecifier>())
163
0
    NNS->print(OS, PP);
164
8
  else if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>()) {
165
0
    if (const NestedNameSpecifier *NNS = NNSL->getNestedNameSpecifier())
166
0
      NNS->print(OS, PP);
167
0
    else
168
0
      OS << "(empty NestedNameSpecifierLoc)";
169
8
  } else if (const QualType *QT = get<QualType>())
170
6
    QT->print(OS, PP);
171
2
  else if (const TypeLoc *TL = get<TypeLoc>())
172
0
    TL->getType().print(OS, PP);
173
2
  else if (const Decl *D = get<Decl>())
174
1
    D->print(OS, PP);
175
1
  else if (const Stmt *S = get<Stmt>())
176
1
    S->printPretty(OS, nullptr, PP);
177
0
  else if (const Type *T = get<Type>())
178
0
    QualType(T, 0).print(OS, PP);
179
0
  else if (const Attr *A = get<Attr>())
180
0
    A->printPretty(OS, PP);
181
0
  else
182
0
    OS << "Unable to print values of type " << NodeKind.asStringRef() << "\n";
183
8
}
184
185
void DynTypedNode::dump(llvm::raw_ostream &OS,
186
2
                        const ASTContext &Context) const {
187
2
  if (const Decl *D = get<Decl>())
188
1
    D->dump(OS);
189
1
  else if (const Stmt *S = get<Stmt>())
190
1
    S->dump(OS, Context);
191
0
  else if (const Type *T = get<Type>())
192
0
    T->dump(OS, Context);
193
0
  else
194
0
    OS << "Unable to dump values of type " << NodeKind.asStringRef() << "\n";
195
2
}
196
197
342
SourceRange DynTypedNode::getSourceRange() const {
198
342
  if (const CXXCtorInitializer *CCI = get<CXXCtorInitializer>())
199
0
    return CCI->getSourceRange();
200
342
  if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
201
1
    return NNSL->getSourceRange();
202
341
  if (const TypeLoc *TL = get<TypeLoc>())
203
5
    return TL->getSourceRange();
204
336
  if (const Decl *D = get<Decl>())
205
82
    return D->getSourceRange();
206
254
  if (const Stmt *S = get<Stmt>())
207
253
    return S->getSourceRange();
208
1
  if (const TemplateArgumentLoc *TAL = get<TemplateArgumentLoc>())
209
0
    return TAL->getSourceRange();
210
1
  if (const auto *C = get<OMPClause>())
211
0
    return SourceRange(C->getBeginLoc(), C->getEndLoc());
212
1
  if (const auto *CBS = get<CXXBaseSpecifier>())
213
0
    return CBS->getSourceRange();
214
1
  if (const auto *A = get<Attr>())
215
1
    return A->getRange();
216
0
  return SourceRange();
217
1
}