Coverage Report

Created: 2020-09-15 12:33

/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 OMP_CLAUSE_CLASS(Enum, Str, Class) {NKI_OMPClause, #Class},
45
#include "llvm/Frontend/OpenMP/OMPKinds.def"
46
};
47
48
3.20M
bool ASTNodeKind::isBaseOf(ASTNodeKind Other, unsigned *Distance) const {
49
3.20M
  return isBaseOf(KindId, Other.KindId, Distance);
50
3.20M
}
51
52
bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived,
53
3.20M
                           unsigned *Distance) {
54
3.20M
  if (Base == NKI_None || 
Derived == NKI_None3.20M
)
return false2
;
55
3.20M
  unsigned Dist = 0;
56
9.63M
  while (Derived != Base && 
Derived != NKI_None7.36M
) {
57
6.43M
    Derived = AllKindInfo[Derived].ParentId;
58
6.43M
    ++Dist;
59
6.43M
  }
60
3.20M
  if (Distance)
61
13.6k
    *Distance = Dist;
62
3.20M
  return Derived == Base;
63
3.20M
}
64
65
2.09k
StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; }
66
67
ASTNodeKind ASTNodeKind::getMostDerivedType(ASTNodeKind Kind1,
68
781k
                                            ASTNodeKind Kind2) {
69
781k
  if (Kind1.isBaseOf(Kind2)) 
return Kind2704k
;
70
77.3k
  if (Kind2.isBaseOf(Kind1)) 
return Kind177.3k
;
71
2
  return ASTNodeKind();
72
2
}
73
74
ASTNodeKind ASTNodeKind::getMostDerivedCommonAncestor(ASTNodeKind Kind1,
75
5
                                                      ASTNodeKind Kind2) {
76
5
  NodeKindId Parent = Kind1.KindId;
77
12
  while (!isBaseOf(Parent, Kind2.KindId, nullptr) && 
Parent != NKI_None8
) {
78
7
    Parent = AllKindInfo[Parent].ParentId;
79
7
  }
80
5
  return ASTNodeKind(Parent);
81
5
}
82
83
331k
ASTNodeKind ASTNodeKind::getFromNode(const Decl &D) {
84
331k
  switch (D.getKind()) {
85
0
#define DECL(DERIVED, BASE)                                                    \
86
331k
    case Decl::DERIVED: return ASTNodeKind(NKI_##DERIVED##Decl);
87
0
#define ABSTRACT_DECL(D)
88
0
#include "clang/AST/DeclNodes.inc"
89
0
  };
90
0
  llvm_unreachable("invalid decl kind");
91
331k
}
92
93
405k
ASTNodeKind ASTNodeKind::getFromNode(const Stmt &S) {
94
405k
  switch (S.getStmtClass()) {
95
0
    case Stmt::NoStmtClass: return NKI_None;
96
0
#define STMT(CLASS, PARENT)                                                    \
97
405k
    case Stmt::CLASS##Class: return ASTNodeKind(NKI_##CLASS);
98
0
#define ABSTRACT_STMT(S)
99
0
#include "clang/AST/StmtNodes.inc"
100
405k
  }
101
405k
  
llvm_unreachable0
("invalid stmt kind");
102
405k
}
103
104
38.6k
ASTNodeKind ASTNodeKind::getFromNode(const Type &T) {
105
38.6k
  switch (T.getTypeClass()) {
106
0
#define TYPE(Class, Base)                                                      \
107
38.6k
    case Type::Class: return ASTNodeKind(NKI_##Class##Type);
108
0
#define ABSTRACT_TYPE(Class, Base)
109
0
#include "clang/AST/TypeNodes.inc"
110
38.6k
  }
111
38.6k
  
llvm_unreachable0
("invalid type kind");
112
38.6k
 }
113
114
352
ASTNodeKind ASTNodeKind::getFromNode(const OMPClause &C) {
115
352
  switch (C.getClauseKind()) {
116
0
#define OMP_CLAUSE_CLASS(Enum, Str, Class)                                     \
117
352
  case llvm::omp::Clause::Enum:                                                \
118
352
    return ASTNodeKind(NKI_##Class);
119
0
#define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
120
0
  case llvm::omp::Clause::Enum:                                                \
121
0
    llvm_unreachable("unexpected OpenMP clause kind");
122
0
  default:
123
0
    break;
124
0
#include "llvm/Frontend/OpenMP/OMPKinds.def"
125
352
  }
126
352
  
llvm_unreachable0
("invalid stmt kind");
127
352
}
128
129
void DynTypedNode::print(llvm::raw_ostream &OS,
130
2
                         const PrintingPolicy &PP) const {
131
2
  if (const TemplateArgument *TA = get<TemplateArgument>())
132
0
    TA->print(PP, OS);
133
2
  else if (const TemplateArgumentLoc *TAL = get<TemplateArgumentLoc>())
134
0
    TAL->getArgument().print(PP, OS);
135
2
  else if (const TemplateName *TN = get<TemplateName>())
136
0
    TN->print(OS, PP);
137
2
  else if (const NestedNameSpecifier *NNS = get<NestedNameSpecifier>())
138
0
    NNS->print(OS, PP);
139
2
  else if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>()) {
140
0
    if (const NestedNameSpecifier *NNS = NNSL->getNestedNameSpecifier())
141
0
      NNS->print(OS, PP);
142
0
    else
143
0
      OS << "(empty NestedNameSpecifierLoc)";
144
2
  } else if (const QualType *QT = get<QualType>())
145
0
    QT->print(OS, PP);
146
2
  else if (const TypeLoc *TL = get<TypeLoc>())
147
0
    TL->getType().print(OS, PP);
148
2
  else if (const Decl *D = get<Decl>())
149
1
    D->print(OS, PP);
150
1
  else if (const Stmt *S = get<Stmt>())
151
1
    S->printPretty(OS, nullptr, PP);
152
0
  else if (const Type *T = get<Type>())
153
0
    QualType(T, 0).print(OS, PP);
154
0
  else
155
0
    OS << "Unable to print values of type " << NodeKind.asStringRef() << "\n";
156
2
}
157
158
void DynTypedNode::dump(llvm::raw_ostream &OS,
159
2
                        const ASTContext &Context) const {
160
2
  if (const Decl *D = get<Decl>())
161
1
    D->dump(OS);
162
1
  else if (const Stmt *S = get<Stmt>())
163
1
    S->dump(OS, Context);
164
0
  else if (const Type *T = get<Type>())
165
0
    T->dump(OS, Context);
166
0
  else
167
0
    OS << "Unable to dump values of type " << NodeKind.asStringRef() << "\n";
168
2
}
169
170
285
SourceRange DynTypedNode::getSourceRange() const {
171
285
  if (const CXXCtorInitializer *CCI = get<CXXCtorInitializer>())
172
0
    return CCI->getSourceRange();
173
285
  if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
174
1
    return NNSL->getSourceRange();
175
284
  if (const TypeLoc *TL = get<TypeLoc>())
176
5
    return TL->getSourceRange();
177
279
  if (const Decl *D = get<Decl>())
178
60
    return D->getSourceRange();
179
219
  if (const Stmt *S = get<Stmt>())
180
219
    return S->getSourceRange();
181
0
  if (const TemplateArgumentLoc *TAL = get<TemplateArgumentLoc>())
182
0
    return TAL->getSourceRange();
183
0
  if (const auto *C = get<OMPClause>())
184
0
    return SourceRange(C->getBeginLoc(), C->getEndLoc());
185
0
  return SourceRange();
186
0
}