-
Notifications
You must be signed in to change notification settings - Fork 243
/
clang_utils.cc
171 lines (153 loc) · 5.75 KB
/
clang_utils.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright 2016 The Kythe Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "kythe/cxx/indexer/cxx/clang_utils.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/DeclVisitor.h"
#include "clang/Basic/CharInfo.h"
#include "clang/Lex/Lexer.h"
#include "glog/logging.h"
namespace kythe {
bool isObjCSelector(const clang::DeclarationName& DN) {
switch (DN.getNameKind()) {
case clang::DeclarationName::NameKind::ObjCZeroArgSelector:
case clang::DeclarationName::NameKind::ObjCOneArgSelector:
case clang::DeclarationName::NameKind::ObjCMultiArgSelector:
return true;
default:
return false;
}
}
clang::SourceLocation GetLocForEndOfToken(
const clang::SourceManager& source_manager,
const clang::LangOptions& lang_options,
clang::SourceLocation start_location) {
if (start_location.isMacroID()) {
start_location = source_manager.getExpansionLoc(start_location);
}
return clang::Lexer::getLocForEndOfToken(start_location,
0 /* offset from end of token */,
source_manager, lang_options);
}
namespace {
class DereferenceMemberTemplatesDeclVisitor
: public clang::ConstDeclVisitor<DereferenceMemberTemplatesDeclVisitor,
void> {
public:
DereferenceMemberTemplatesDeclVisitor() = default;
const clang::Decl* Visit(const clang::Decl* decl) {
decl_ = decl;
ConstDeclVisitor::Visit(decl);
return decl_;
}
void VisitFunctionTemplateDecl(const clang::FunctionTemplateDecl* ft) {
while ((ft = ft->getInstantiatedFromMemberTemplate())) {
if (ft->getTemplatedDecl() != nullptr) decl_ = ft->getTemplatedDecl();
}
}
void VisitClassTemplateDecl(const clang::ClassTemplateDecl* ct) {
while ((ct = ct->getInstantiatedFromMemberTemplate())) {
if (ct->getTemplatedDecl() != nullptr) decl_ = ct->getTemplatedDecl();
}
}
void VisitClassTemplatePartialSpecializationDecl(
const clang::ClassTemplatePartialSpecializationDecl* ctp) {
while ((ctp = ctp->getInstantiatedFromMemberTemplate())) {
decl_ = ctp;
}
}
void VisitVarTemplatePartialSpecializationDecl(
const clang::VarTemplatePartialSpecializationDecl* vtp) {
while ((vtp = vtp->getInstantiatedFromMember())) {
decl_ = vtp;
}
}
void VisitVarTemplateDecl(const clang::VarTemplateDecl* vt) {
while ((vt = vt->getInstantiatedFromMemberTemplate())) {
if (vt->getTemplatedDecl() != nullptr) decl_ = vt->getTemplatedDecl();
}
}
void VisitTypeAliasTemplateDecl(const clang::TypeAliasTemplateDecl* at) {
while ((at = at->getInstantiatedFromMemberTemplate())) {
if (at->getTemplatedDecl() != nullptr) decl_ = at->getTemplatedDecl();
}
}
private:
const clang::Decl* decl_;
};
} // anonymous namespace
const clang::Decl* DereferenceMemberTemplates(const clang::Decl* decl) {
return DereferenceMemberTemplatesDeclVisitor().Visit(decl);
}
const clang::Decl* FindSpecializedTemplate(const clang::Decl* decl) {
if (const auto* FD = llvm::dyn_cast<const clang::FunctionDecl>(decl)) {
if (auto* ftsi = FD->getTemplateSpecializationInfo()) {
if (!ftsi->isExplicitInstantiationOrSpecialization()) {
return DereferenceMemberTemplates(ftsi->getTemplate());
}
}
} else if (const auto* ctsd =
llvm::dyn_cast<const clang::ClassTemplateSpecializationDecl>(
decl)) {
if (!ctsd->isExplicitInstantiationOrSpecialization()) {
auto primary_or_partial = ctsd->getSpecializedTemplateOrPartial();
if (const auto* partial =
primary_or_partial
.dyn_cast<clang::ClassTemplatePartialSpecializationDecl*>()) {
return DereferenceMemberTemplates(partial);
} else if (const auto* primary =
primary_or_partial.dyn_cast<clang::ClassTemplateDecl*>()) {
return DereferenceMemberTemplates(primary);
}
}
} else if (const auto* vtsd =
llvm::dyn_cast<const clang::VarTemplateSpecializationDecl>(
decl)) {
if (!vtsd->isExplicitInstantiationOrSpecialization()) {
auto primary_or_partial = vtsd->getSpecializedTemplateOrPartial();
if (const auto* partial =
primary_or_partial
.dyn_cast<clang::VarTemplatePartialSpecializationDecl*>()) {
return DereferenceMemberTemplates(partial);
} else if (const auto* primary =
primary_or_partial.dyn_cast<clang::VarTemplateDecl*>()) {
return DereferenceMemberTemplates(primary);
}
}
}
return DereferenceMemberTemplates(decl);
}
bool ShouldHaveBlameContext(const clang::Decl* decl) {
// TODO(zarko): introduce more blameable decls.
switch (decl->getKind()) {
case clang::Decl::Kind::Field:
case clang::Decl::Kind::Var:
return true;
default:
return false;
}
}
const clang::Stmt* FindLValueHead(const clang::Stmt* stmt) {
if (stmt == nullptr) return nullptr;
switch (stmt->getStmtClass()) {
case clang::Stmt::StmtClass::DeclRefExprClass:
case clang::Stmt::StmtClass::ObjCIvarRefExprClass:
case clang::Stmt::StmtClass::MemberExprClass:
return stmt;
default:
return nullptr;
}
}
} // namespace kythe