Coverage Report

Created: 2021-06-15 06:44

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