Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
20
namespace clang {
21
namespace ast_type_traits {
22
23
const ASTNodeKind::KindInfo ASTNodeKind::AllKindInfo[] = {
24
  { NKI_None, "<None>" },
25
  { NKI_None, "TemplateArgument" },
26
  { NKI_None, "TemplateName" },
27
  { NKI_None, "NestedNameSpecifierLoc" },
28
  { NKI_None, "QualType" },
29
  { NKI_None, "TypeLoc" },
30
  { NKI_None, "CXXCtorInitializer" },
31
  { NKI_None, "NestedNameSpecifier" },
32
  { NKI_None, "Decl" },
33
#define DECL(DERIVED, BASE) { NKI_##BASE, #DERIVED "Decl" },
34
#include "clang/AST/DeclNodes.inc"
35
  { NKI_None, "Stmt" },
36
#define STMT(DERIVED, BASE) { NKI_##BASE, #DERIVED },
37
#include "clang/AST/StmtNodes.inc"
38
  { NKI_None, "Type" },
39
#define TYPE(DERIVED, BASE) { NKI_##BASE, #DERIVED "Type" },
40
#include "clang/AST/TypeNodes.def"
41
  { NKI_None, "OMPClause" },
42
#define OPENMP_CLAUSE(TextualSpelling, Class) {NKI_OMPClause, #Class},
43
#include "clang/Basic/OpenMPKinds.def"
44
};
45
46
1.28M
bool ASTNodeKind::isBaseOf(ASTNodeKind Other, unsigned *Distance) const {
47
1.28M
  return isBaseOf(KindId, Other.KindId, Distance);
48
1.28M
}
49
50
bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived,
51
1.28M
                           unsigned *Distance) {
52
1.28M
  if (Base == NKI_None || 
Derived == NKI_None1.28M
)
return false2
;
53
1.28M
  unsigned Dist = 0;
54
4.05M
  while (Derived != Base && 
Derived != NKI_None3.15M
) {
55
2.77M
    Derived = AllKindInfo[Derived].ParentId;
56
2.77M
    ++Dist;
57
2.77M
  }
58
1.28M
  if (Distance)
59
12.9k
    *Distance = Dist;
60
1.28M
  return Derived == Base;
61
1.28M
}
62
63
2.06k
StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; }
64
65
ASTNodeKind ASTNodeKind::getMostDerivedType(ASTNodeKind Kind1,
66
714k
                                            ASTNodeKind Kind2) {
67
714k
  if (Kind1.isBaseOf(Kind2)) 
return Kind2644k
;
68
69.4k
  if (Kind2.isBaseOf(Kind1)) 
return Kind169.4k
;
69
2
  return ASTNodeKind();
70
2
}
71
72
ASTNodeKind ASTNodeKind::getMostDerivedCommonAncestor(ASTNodeKind Kind1,
73
5
                                                      ASTNodeKind Kind2) {
74
5
  NodeKindId Parent = Kind1.KindId;
75
12
  while (!isBaseOf(Parent, Kind2.KindId, nullptr) && 
Parent != NKI_None8
) {
76
7
    Parent = AllKindInfo[Parent].ParentId;
77
7
  }
78
5
  return ASTNodeKind(Parent);
79
5
}
80
81
123k
ASTNodeKind ASTNodeKind::getFromNode(const Decl &D) {
82
123k
  switch (D.getKind()) {
83
123k
#define DECL(DERIVED, BASE)                                                    \
84
123k
    case Decl::DERIVED: return ASTNodeKind(NKI_##DERIVED##Decl);
85
123k
#define ABSTRACT_DECL(D)
86
123k
#include 
"clang/AST/DeclNodes.inc"319
87
123k
  };
88
0
  llvm_unreachable("invalid decl kind");
89
123k
}
90
91
133k
ASTNodeKind ASTNodeKind::getFromNode(const Stmt &S) {
92
133k
  switch (S.getStmtClass()) {
93
133k
    
case Stmt::NoStmtClass: return NKI_None0
;
94
133k
#define STMT(CLASS, PARENT)                                                    \
95
133k
    case Stmt::CLASS##Class: return ASTNodeKind(NKI_##CLASS);
96
133k
#define ABSTRACT_STMT(S)
97
133k
#include 
"clang/AST/StmtNodes.inc"77
98
133k
  }
99
133k
  
llvm_unreachable0
("invalid stmt kind");
100
133k
}
101
102
19.4k
ASTNodeKind ASTNodeKind::getFromNode(const Type &T) {
103
19.4k
  switch (T.getTypeClass()) {
104
19.4k
#define TYPE(Class, Base)                                                      \
105
19.4k
    case Type::Class: return ASTNodeKind(NKI_##Class##Type);
106
19.4k
#define ABSTRACT_TYPE(Class, Base)
107
19.4k
#include 
"clang/AST/TypeNodes.def"7.25k
108
19.4k
  }
109
19.4k
  
llvm_unreachable0
("invalid type kind");
110
19.4k
 }
111
112
24
ASTNodeKind ASTNodeKind::getFromNode(const OMPClause &C) {
113
24
  switch (C.getClauseKind()) {
114
24
#define OPENMP_CLAUSE(Name, Class)                                             \
115
24
    case OMPC_##Name: return ASTNodeKind(NKI_##Class);
116
24
#include "clang/Basic/OpenMPKinds.def"
117
24
  case OMPC_threadprivate:
118
0
  case OMPC_uniform:
119
0
  case OMPC_unknown:
120
0
    llvm_unreachable("unexpected OpenMP clause kind");
121
0
  }
122
0
  llvm_unreachable("invalid stmt kind");
123
0
}
124
125
void DynTypedNode::print(llvm::raw_ostream &OS,
126
2
                         const PrintingPolicy &PP) const {
127
2
  if (const TemplateArgument *TA = get<TemplateArgument>())
128
0
    TA->print(PP, OS);
129
2
  else if (const TemplateName *TN = get<TemplateName>())
130
0
    TN->print(OS, PP);
131
2
  else if (const NestedNameSpecifier *NNS = get<NestedNameSpecifier>())
132
0
    NNS->print(OS, PP);
133
2
  else if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>()) {
134
0
    if (const NestedNameSpecifier *NNS = NNSL->getNestedNameSpecifier())
135
0
      NNS->print(OS, PP);
136
0
    else
137
0
      OS << "(empty NestedNameSpecifierLoc)";
138
2
  } else if (const QualType *QT = get<QualType>())
139
0
    QT->print(OS, PP);
140
2
  else if (const TypeLoc *TL = get<TypeLoc>())
141
0
    TL->getType().print(OS, PP);
142
2
  else if (const Decl *D = get<Decl>())
143
1
    D->print(OS, PP);
144
1
  else if (const Stmt *S = get<Stmt>())
145
1
    S->printPretty(OS, nullptr, PP);
146
0
  else if (const Type *T = get<Type>())
147
0
    QualType(T, 0).print(OS, PP);
148
0
  else
149
0
    OS << "Unable to print values of type " << NodeKind.asStringRef() << "\n";
150
2
}
151
152
2
void DynTypedNode::dump(llvm::raw_ostream &OS, SourceManager &SM) const {
153
2
  if (const Decl *D = get<Decl>())
154
1
    D->dump(OS);
155
1
  else if (const Stmt *S = get<Stmt>())
156
1
    S->dump(OS, SM);
157
0
  else if (const Type *T = get<Type>())
158
0
    T->dump(OS);
159
0
  else
160
0
    OS << "Unable to dump values of type " << NodeKind.asStringRef() << "\n";
161
2
}
162
163
241
SourceRange DynTypedNode::getSourceRange() const {
164
241
  if (const CXXCtorInitializer *CCI = get<CXXCtorInitializer>())
165
0
    return CCI->getSourceRange();
166
241
  if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
167
1
    return NNSL->getSourceRange();
168
240
  if (const TypeLoc *TL = get<TypeLoc>())
169
1
    return TL->getSourceRange();
170
239
  if (const Decl *D = get<Decl>())
171
57
    return D->getSourceRange();
172
182
  if (const Stmt *S = get<Stmt>())
173
182
    return S->getSourceRange();
174
0
  if (const auto *C = get<OMPClause>())
175
0
    return SourceRange(C->getBeginLoc(), C->getEndLoc());
176
0
  return SourceRange();
177
0
}
178
179
} // end namespace ast_type_traits
180
} // end namespace clang