150
|
1 //===- USRGeneration.cpp - Routines for USR generation --------------------===//
|
|
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 #include "clang/Index/USRGeneration.h"
|
|
10 #include "clang/AST/ASTContext.h"
|
|
11 #include "clang/AST/Attr.h"
|
|
12 #include "clang/AST/DeclTemplate.h"
|
|
13 #include "clang/AST/DeclVisitor.h"
|
|
14 #include "clang/Lex/PreprocessingRecord.h"
|
|
15 #include "llvm/Support/Path.h"
|
|
16 #include "llvm/Support/raw_ostream.h"
|
|
17
|
|
18 using namespace clang;
|
|
19 using namespace clang::index;
|
|
20
|
|
21 //===----------------------------------------------------------------------===//
|
|
22 // USR generation.
|
|
23 //===----------------------------------------------------------------------===//
|
|
24
|
|
25 /// \returns true on error.
|
|
26 static bool printLoc(llvm::raw_ostream &OS, SourceLocation Loc,
|
|
27 const SourceManager &SM, bool IncludeOffset) {
|
|
28 if (Loc.isInvalid()) {
|
|
29 return true;
|
|
30 }
|
|
31 Loc = SM.getExpansionLoc(Loc);
|
|
32 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(Loc);
|
|
33 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
|
|
34 if (FE) {
|
|
35 OS << llvm::sys::path::filename(FE->getName());
|
|
36 } else {
|
|
37 // This case really isn't interesting.
|
|
38 return true;
|
|
39 }
|
|
40 if (IncludeOffset) {
|
|
41 // Use the offest into the FileID to represent the location. Using
|
|
42 // a line/column can cause us to look back at the original source file,
|
|
43 // which is expensive.
|
|
44 OS << '@' << Decomposed.second;
|
|
45 }
|
|
46 return false;
|
|
47 }
|
|
48
|
|
49 static StringRef GetExternalSourceContainer(const NamedDecl *D) {
|
|
50 if (!D)
|
|
51 return StringRef();
|
|
52 if (auto *attr = D->getExternalSourceSymbolAttr()) {
|
|
53 return attr->getDefinedIn();
|
|
54 }
|
|
55 return StringRef();
|
|
56 }
|
|
57
|
|
58 namespace {
|
|
59 class USRGenerator : public ConstDeclVisitor<USRGenerator> {
|
|
60 SmallVectorImpl<char> &Buf;
|
|
61 llvm::raw_svector_ostream Out;
|
|
62 bool IgnoreResults;
|
|
63 ASTContext *Context;
|
|
64 bool generatedLoc;
|
|
65
|
|
66 llvm::DenseMap<const Type *, unsigned> TypeSubstitutions;
|
|
67
|
|
68 public:
|
|
69 explicit USRGenerator(ASTContext *Ctx, SmallVectorImpl<char> &Buf)
|
|
70 : Buf(Buf),
|
|
71 Out(Buf),
|
|
72 IgnoreResults(false),
|
|
73 Context(Ctx),
|
|
74 generatedLoc(false)
|
|
75 {
|
|
76 // Add the USR space prefix.
|
|
77 Out << getUSRSpacePrefix();
|
|
78 }
|
|
79
|
|
80 bool ignoreResults() const { return IgnoreResults; }
|
|
81
|
|
82 // Visitation methods from generating USRs from AST elements.
|
|
83 void VisitDeclContext(const DeclContext *D);
|
|
84 void VisitFieldDecl(const FieldDecl *D);
|
|
85 void VisitFunctionDecl(const FunctionDecl *D);
|
|
86 void VisitNamedDecl(const NamedDecl *D);
|
|
87 void VisitNamespaceDecl(const NamespaceDecl *D);
|
|
88 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
|
|
89 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
|
|
90 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
|
|
91 void VisitObjCContainerDecl(const ObjCContainerDecl *CD,
|
|
92 const ObjCCategoryDecl *CatD = nullptr);
|
|
93 void VisitObjCMethodDecl(const ObjCMethodDecl *MD);
|
|
94 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
|
|
95 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
|
|
96 void VisitTagDecl(const TagDecl *D);
|
|
97 void VisitTypedefDecl(const TypedefDecl *D);
|
|
98 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
|
|
99 void VisitVarDecl(const VarDecl *D);
|
|
100 void VisitBindingDecl(const BindingDecl *D);
|
|
101 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
|
|
102 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
|
|
103 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
|
|
104 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
|
|
105
|
|
106 void VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
|
|
107 IgnoreResults = true; // No USRs for linkage specs themselves.
|
|
108 }
|
|
109
|
|
110 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
|
|
111 IgnoreResults = true;
|
|
112 }
|
|
113
|
|
114 void VisitUsingDecl(const UsingDecl *D) {
|
|
115 VisitDeclContext(D->getDeclContext());
|
|
116 Out << "@UD@";
|
|
117
|
|
118 bool EmittedDeclName = !EmitDeclName(D);
|
|
119 assert(EmittedDeclName && "EmitDeclName can not fail for UsingDecls");
|
|
120 (void)EmittedDeclName;
|
|
121 }
|
|
122
|
|
123 bool ShouldGenerateLocation(const NamedDecl *D);
|
|
124
|
|
125 bool isLocal(const NamedDecl *D) {
|
|
126 return D->getParentFunctionOrMethod() != nullptr;
|
|
127 }
|
|
128
|
|
129 void GenExtSymbolContainer(const NamedDecl *D);
|
|
130
|
|
131 /// Generate the string component containing the location of the
|
|
132 /// declaration.
|
|
133 bool GenLoc(const Decl *D, bool IncludeOffset);
|
|
134
|
|
135 /// String generation methods used both by the visitation methods
|
|
136 /// and from other clients that want to directly generate USRs. These
|
|
137 /// methods do not construct complete USRs (which incorporate the parents
|
|
138 /// of an AST element), but only the fragments concerning the AST element
|
|
139 /// itself.
|
|
140
|
|
141 /// Generate a USR for an Objective-C class.
|
|
142 void GenObjCClass(StringRef cls, StringRef ExtSymDefinedIn,
|
|
143 StringRef CategoryContextExtSymbolDefinedIn) {
|
|
144 generateUSRForObjCClass(cls, Out, ExtSymDefinedIn,
|
|
145 CategoryContextExtSymbolDefinedIn);
|
|
146 }
|
|
147
|
|
148 /// Generate a USR for an Objective-C class category.
|
|
149 void GenObjCCategory(StringRef cls, StringRef cat,
|
|
150 StringRef clsExt, StringRef catExt) {
|
|
151 generateUSRForObjCCategory(cls, cat, Out, clsExt, catExt);
|
|
152 }
|
|
153
|
|
154 /// Generate a USR fragment for an Objective-C property.
|
|
155 void GenObjCProperty(StringRef prop, bool isClassProp) {
|
|
156 generateUSRForObjCProperty(prop, isClassProp, Out);
|
|
157 }
|
|
158
|
|
159 /// Generate a USR for an Objective-C protocol.
|
|
160 void GenObjCProtocol(StringRef prot, StringRef ext) {
|
|
161 generateUSRForObjCProtocol(prot, Out, ext);
|
|
162 }
|
|
163
|
|
164 void VisitType(QualType T);
|
|
165 void VisitTemplateParameterList(const TemplateParameterList *Params);
|
|
166 void VisitTemplateName(TemplateName Name);
|
|
167 void VisitTemplateArgument(const TemplateArgument &Arg);
|
|
168
|
|
169 /// Emit a Decl's name using NamedDecl::printName() and return true if
|
|
170 /// the decl had no name.
|
|
171 bool EmitDeclName(const NamedDecl *D);
|
|
172 };
|
|
173 } // end anonymous namespace
|
|
174
|
|
175 //===----------------------------------------------------------------------===//
|
|
176 // Generating USRs from ASTS.
|
|
177 //===----------------------------------------------------------------------===//
|
|
178
|
|
179 bool USRGenerator::EmitDeclName(const NamedDecl *D) {
|
|
180 const unsigned startSize = Buf.size();
|
|
181 D->printName(Out);
|
|
182 const unsigned endSize = Buf.size();
|
|
183 return startSize == endSize;
|
|
184 }
|
|
185
|
|
186 bool USRGenerator::ShouldGenerateLocation(const NamedDecl *D) {
|
|
187 if (D->isExternallyVisible())
|
|
188 return false;
|
|
189 if (D->getParentFunctionOrMethod())
|
|
190 return true;
|
|
191 SourceLocation Loc = D->getLocation();
|
|
192 if (Loc.isInvalid())
|
|
193 return false;
|
|
194 const SourceManager &SM = Context->getSourceManager();
|
|
195 return !SM.isInSystemHeader(Loc);
|
|
196 }
|
|
197
|
|
198 void USRGenerator::VisitDeclContext(const DeclContext *DC) {
|
|
199 if (const NamedDecl *D = dyn_cast<NamedDecl>(DC))
|
|
200 Visit(D);
|
|
201 else if (isa<LinkageSpecDecl>(DC)) // Linkage specs are transparent in USRs.
|
|
202 VisitDeclContext(DC->getParent());
|
|
203 }
|
|
204
|
|
205 void USRGenerator::VisitFieldDecl(const FieldDecl *D) {
|
|
206 // The USR for an ivar declared in a class extension is based on the
|
|
207 // ObjCInterfaceDecl, not the ObjCCategoryDecl.
|
|
208 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
|
|
209 Visit(ID);
|
|
210 else
|
|
211 VisitDeclContext(D->getDeclContext());
|
|
212 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
|
|
213 if (EmitDeclName(D)) {
|
|
214 // Bit fields can be anonymous.
|
|
215 IgnoreResults = true;
|
|
216 return;
|
|
217 }
|
|
218 }
|
|
219
|
|
220 void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
|
|
221 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
|
|
222 return;
|
|
223
|
|
224 const unsigned StartSize = Buf.size();
|
|
225 VisitDeclContext(D->getDeclContext());
|
|
226 if (Buf.size() == StartSize)
|
|
227 GenExtSymbolContainer(D);
|
|
228
|
|
229 bool IsTemplate = false;
|
|
230 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
|
|
231 IsTemplate = true;
|
|
232 Out << "@FT@";
|
|
233 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
|
|
234 } else
|
|
235 Out << "@F@";
|
|
236
|
|
237 PrintingPolicy Policy(Context->getLangOpts());
|
|
238 // Forward references can have different template argument names. Suppress the
|
|
239 // template argument names in constructors to make their USR more stable.
|
|
240 Policy.SuppressTemplateArgsInCXXConstructors = true;
|
|
241 D->getDeclName().print(Out, Policy);
|
|
242
|
|
243 ASTContext &Ctx = *Context;
|
|
244 if ((!Ctx.getLangOpts().CPlusPlus || D->isExternC()) &&
|
|
245 !D->hasAttr<OverloadableAttr>())
|
|
246 return;
|
|
247
|
|
248 if (const TemplateArgumentList *
|
|
249 SpecArgs = D->getTemplateSpecializationArgs()) {
|
|
250 Out << '<';
|
|
251 for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) {
|
|
252 Out << '#';
|
|
253 VisitTemplateArgument(SpecArgs->get(I));
|
|
254 }
|
|
255 Out << '>';
|
|
256 }
|
|
257
|
|
258 // Mangle in type information for the arguments.
|
|
259 for (auto PD : D->parameters()) {
|
|
260 Out << '#';
|
|
261 VisitType(PD->getType());
|
|
262 }
|
|
263 if (D->isVariadic())
|
|
264 Out << '.';
|
|
265 if (IsTemplate) {
|
|
266 // Function templates can be overloaded by return type, for example:
|
|
267 // \code
|
|
268 // template <class T> typename T::A foo() {}
|
|
269 // template <class T> typename T::B foo() {}
|
|
270 // \endcode
|
|
271 Out << '#';
|
|
272 VisitType(D->getReturnType());
|
|
273 }
|
|
274 Out << '#';
|
|
275 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
|
|
276 if (MD->isStatic())
|
|
277 Out << 'S';
|
|
278 // FIXME: OpenCL: Need to consider address spaces
|
|
279 if (unsigned quals = MD->getMethodQualifiers().getCVRUQualifiers())
|
|
280 Out << (char)('0' + quals);
|
|
281 switch (MD->getRefQualifier()) {
|
|
282 case RQ_None: break;
|
|
283 case RQ_LValue: Out << '&'; break;
|
|
284 case RQ_RValue: Out << "&&"; break;
|
|
285 }
|
|
286 }
|
|
287 }
|
|
288
|
|
289 void USRGenerator::VisitNamedDecl(const NamedDecl *D) {
|
|
290 VisitDeclContext(D->getDeclContext());
|
|
291 Out << "@";
|
|
292
|
|
293 if (EmitDeclName(D)) {
|
|
294 // The string can be empty if the declaration has no name; e.g., it is
|
|
295 // the ParmDecl with no name for declaration of a function pointer type,
|
|
296 // e.g.: void (*f)(void *);
|
|
297 // In this case, don't generate a USR.
|
|
298 IgnoreResults = true;
|
|
299 }
|
|
300 }
|
|
301
|
|
302 void USRGenerator::VisitVarDecl(const VarDecl *D) {
|
|
303 // VarDecls can be declared 'extern' within a function or method body,
|
|
304 // but their enclosing DeclContext is the function, not the TU. We need
|
|
305 // to check the storage class to correctly generate the USR.
|
|
306 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
|
|
307 return;
|
|
308
|
|
309 VisitDeclContext(D->getDeclContext());
|
|
310
|
|
311 if (VarTemplateDecl *VarTmpl = D->getDescribedVarTemplate()) {
|
|
312 Out << "@VT";
|
|
313 VisitTemplateParameterList(VarTmpl->getTemplateParameters());
|
|
314 } else if (const VarTemplatePartialSpecializationDecl *PartialSpec
|
|
315 = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
|
|
316 Out << "@VP";
|
|
317 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
|
|
318 }
|
|
319
|
|
320 // Variables always have simple names.
|
|
321 StringRef s = D->getName();
|
|
322
|
|
323 // The string can be empty if the declaration has no name; e.g., it is
|
|
324 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
|
|
325 // void (*f)(void *);
|
|
326 // In this case, don't generate a USR.
|
|
327 if (s.empty())
|
|
328 IgnoreResults = true;
|
|
329 else
|
|
330 Out << '@' << s;
|
|
331
|
|
332 // For a template specialization, mangle the template arguments.
|
|
333 if (const VarTemplateSpecializationDecl *Spec
|
|
334 = dyn_cast<VarTemplateSpecializationDecl>(D)) {
|
|
335 const TemplateArgumentList &Args = Spec->getTemplateArgs();
|
|
336 Out << '>';
|
|
337 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
|
|
338 Out << '#';
|
|
339 VisitTemplateArgument(Args.get(I));
|
|
340 }
|
|
341 }
|
|
342 }
|
|
343
|
|
344 void USRGenerator::VisitBindingDecl(const BindingDecl *D) {
|
|
345 if (isLocal(D) && GenLoc(D, /*IncludeOffset=*/true))
|
|
346 return;
|
|
347 VisitNamedDecl(D);
|
|
348 }
|
|
349
|
|
350 void USRGenerator::VisitNonTypeTemplateParmDecl(
|
|
351 const NonTypeTemplateParmDecl *D) {
|
|
352 GenLoc(D, /*IncludeOffset=*/true);
|
|
353 }
|
|
354
|
|
355 void USRGenerator::VisitTemplateTemplateParmDecl(
|
|
356 const TemplateTemplateParmDecl *D) {
|
|
357 GenLoc(D, /*IncludeOffset=*/true);
|
|
358 }
|
|
359
|
|
360 void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) {
|
|
361 if (D->isAnonymousNamespace()) {
|
|
362 Out << "@aN";
|
|
363 return;
|
|
364 }
|
|
365
|
|
366 VisitDeclContext(D->getDeclContext());
|
|
367 if (!IgnoreResults)
|
|
368 Out << "@N@" << D->getName();
|
|
369 }
|
|
370
|
|
371 void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
|
|
372 VisitFunctionDecl(D->getTemplatedDecl());
|
|
373 }
|
|
374
|
|
375 void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
|
|
376 VisitTagDecl(D->getTemplatedDecl());
|
|
377 }
|
|
378
|
|
379 void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
|
|
380 VisitDeclContext(D->getDeclContext());
|
|
381 if (!IgnoreResults)
|
|
382 Out << "@NA@" << D->getName();
|
|
383 }
|
|
384
|
|
385 void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
|
|
386 const DeclContext *container = D->getDeclContext();
|
|
387 if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) {
|
|
388 Visit(pd);
|
|
389 }
|
|
390 else {
|
|
391 // The USR for a method declared in a class extension or category is based on
|
|
392 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
|
|
393 const ObjCInterfaceDecl *ID = D->getClassInterface();
|
|
394 if (!ID) {
|
|
395 IgnoreResults = true;
|
|
396 return;
|
|
397 }
|
|
398 auto getCategoryContext = [](const ObjCMethodDecl *D) ->
|
|
399 const ObjCCategoryDecl * {
|
|
400 if (auto *CD = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
|
|
401 return CD;
|
|
402 if (auto *ICD = dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
|
|
403 return ICD->getCategoryDecl();
|
|
404 return nullptr;
|
|
405 };
|
|
406 auto *CD = getCategoryContext(D);
|
|
407 VisitObjCContainerDecl(ID, CD);
|
|
408 }
|
|
409 // Ideally we would use 'GenObjCMethod', but this is such a hot path
|
|
410 // for Objective-C code that we don't want to use
|
|
411 // DeclarationName::getAsString().
|
|
412 Out << (D->isInstanceMethod() ? "(im)" : "(cm)")
|
|
413 << DeclarationName(D->getSelector());
|
|
414 }
|
|
415
|
|
416 void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D,
|
|
417 const ObjCCategoryDecl *CatD) {
|
|
418 switch (D->getKind()) {
|
|
419 default:
|
|
420 llvm_unreachable("Invalid ObjC container.");
|
|
421 case Decl::ObjCInterface:
|
|
422 case Decl::ObjCImplementation:
|
|
423 GenObjCClass(D->getName(), GetExternalSourceContainer(D),
|
|
424 GetExternalSourceContainer(CatD));
|
|
425 break;
|
|
426 case Decl::ObjCCategory: {
|
|
427 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
|
|
428 const ObjCInterfaceDecl *ID = CD->getClassInterface();
|
|
429 if (!ID) {
|
|
430 // Handle invalid code where the @interface might not
|
|
431 // have been specified.
|
|
432 // FIXME: We should be able to generate this USR even if the
|
|
433 // @interface isn't available.
|
|
434 IgnoreResults = true;
|
|
435 return;
|
|
436 }
|
|
437 // Specially handle class extensions, which are anonymous categories.
|
|
438 // We want to mangle in the location to uniquely distinguish them.
|
|
439 if (CD->IsClassExtension()) {
|
|
440 Out << "objc(ext)" << ID->getName() << '@';
|
|
441 GenLoc(CD, /*IncludeOffset=*/true);
|
|
442 }
|
|
443 else
|
|
444 GenObjCCategory(ID->getName(), CD->getName(),
|
|
445 GetExternalSourceContainer(ID),
|
|
446 GetExternalSourceContainer(CD));
|
|
447
|
|
448 break;
|
|
449 }
|
|
450 case Decl::ObjCCategoryImpl: {
|
|
451 const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
|
|
452 const ObjCInterfaceDecl *ID = CD->getClassInterface();
|
|
453 if (!ID) {
|
|
454 // Handle invalid code where the @interface might not
|
|
455 // have been specified.
|
|
456 // FIXME: We should be able to generate this USR even if the
|
|
457 // @interface isn't available.
|
|
458 IgnoreResults = true;
|
|
459 return;
|
|
460 }
|
|
461 GenObjCCategory(ID->getName(), CD->getName(),
|
|
462 GetExternalSourceContainer(ID),
|
|
463 GetExternalSourceContainer(CD));
|
|
464 break;
|
|
465 }
|
|
466 case Decl::ObjCProtocol: {
|
|
467 const ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D);
|
|
468 GenObjCProtocol(PD->getName(), GetExternalSourceContainer(PD));
|
|
469 break;
|
|
470 }
|
|
471 }
|
|
472 }
|
|
473
|
|
474 void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
|
|
475 // The USR for a property declared in a class extension or category is based
|
|
476 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl.
|
|
477 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
|
|
478 Visit(ID);
|
|
479 else
|
|
480 Visit(cast<Decl>(D->getDeclContext()));
|
|
481 GenObjCProperty(D->getName(), D->isClassProperty());
|
|
482 }
|
|
483
|
|
484 void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
|
|
485 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
|
|
486 VisitObjCPropertyDecl(PD);
|
|
487 return;
|
|
488 }
|
|
489
|
|
490 IgnoreResults = true;
|
|
491 }
|
|
492
|
|
493 void USRGenerator::VisitTagDecl(const TagDecl *D) {
|
|
494 // Add the location of the tag decl to handle resolution across
|
|
495 // translation units.
|
|
496 if (!isa<EnumDecl>(D) &&
|
|
497 ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
|
|
498 return;
|
|
499
|
|
500 GenExtSymbolContainer(D);
|
|
501
|
|
502 D = D->getCanonicalDecl();
|
|
503 VisitDeclContext(D->getDeclContext());
|
|
504
|
|
505 bool AlreadyStarted = false;
|
|
506 if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
|
|
507 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
|
|
508 AlreadyStarted = true;
|
|
509
|
|
510 switch (D->getTagKind()) {
|
|
511 case TTK_Interface:
|
|
512 case TTK_Class:
|
|
513 case TTK_Struct: Out << "@ST"; break;
|
|
514 case TTK_Union: Out << "@UT"; break;
|
|
515 case TTK_Enum: llvm_unreachable("enum template");
|
|
516 }
|
|
517 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
|
|
518 } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec
|
|
519 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
|
|
520 AlreadyStarted = true;
|
|
521
|
|
522 switch (D->getTagKind()) {
|
|
523 case TTK_Interface:
|
|
524 case TTK_Class:
|
|
525 case TTK_Struct: Out << "@SP"; break;
|
|
526 case TTK_Union: Out << "@UP"; break;
|
|
527 case TTK_Enum: llvm_unreachable("enum partial specialization");
|
|
528 }
|
|
529 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
|
|
530 }
|
|
531 }
|
|
532
|
|
533 if (!AlreadyStarted) {
|
|
534 switch (D->getTagKind()) {
|
|
535 case TTK_Interface:
|
|
536 case TTK_Class:
|
|
537 case TTK_Struct: Out << "@S"; break;
|
|
538 case TTK_Union: Out << "@U"; break;
|
|
539 case TTK_Enum: Out << "@E"; break;
|
|
540 }
|
|
541 }
|
|
542
|
|
543 Out << '@';
|
|
544 assert(Buf.size() > 0);
|
|
545 const unsigned off = Buf.size() - 1;
|
|
546
|
|
547 if (EmitDeclName(D)) {
|
|
548 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) {
|
|
549 Buf[off] = 'A';
|
|
550 Out << '@' << *TD;
|
|
551 }
|
|
552 else {
|
|
553 if (D->isEmbeddedInDeclarator() && !D->isFreeStanding()) {
|
|
554 printLoc(Out, D->getLocation(), Context->getSourceManager(), true);
|
|
555 } else {
|
|
556 Buf[off] = 'a';
|
|
557 if (auto *ED = dyn_cast<EnumDecl>(D)) {
|
|
558 // Distinguish USRs of anonymous enums by using their first enumerator.
|
|
559 auto enum_range = ED->enumerators();
|
|
560 if (enum_range.begin() != enum_range.end()) {
|
|
561 Out << '@' << **enum_range.begin();
|
|
562 }
|
|
563 }
|
|
564 }
|
|
565 }
|
|
566 }
|
|
567
|
|
568 // For a class template specialization, mangle the template arguments.
|
|
569 if (const ClassTemplateSpecializationDecl *Spec
|
|
570 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
|
|
571 const TemplateArgumentList &Args = Spec->getTemplateArgs();
|
|
572 Out << '>';
|
|
573 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
|
|
574 Out << '#';
|
|
575 VisitTemplateArgument(Args.get(I));
|
|
576 }
|
|
577 }
|
|
578 }
|
|
579
|
|
580 void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) {
|
|
581 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
|
|
582 return;
|
|
583 const DeclContext *DC = D->getDeclContext();
|
|
584 if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
|
|
585 Visit(DCN);
|
|
586 Out << "@T@";
|
|
587 Out << D->getName();
|
|
588 }
|
|
589
|
|
590 void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
|
|
591 GenLoc(D, /*IncludeOffset=*/true);
|
|
592 }
|
|
593
|
|
594 void USRGenerator::GenExtSymbolContainer(const NamedDecl *D) {
|
|
595 StringRef Container = GetExternalSourceContainer(D);
|
|
596 if (!Container.empty())
|
|
597 Out << "@M@" << Container;
|
|
598 }
|
|
599
|
|
600 bool USRGenerator::GenLoc(const Decl *D, bool IncludeOffset) {
|
|
601 if (generatedLoc)
|
|
602 return IgnoreResults;
|
|
603 generatedLoc = true;
|
|
604
|
|
605 // Guard against null declarations in invalid code.
|
|
606 if (!D) {
|
|
607 IgnoreResults = true;
|
|
608 return true;
|
|
609 }
|
|
610
|
|
611 // Use the location of canonical decl.
|
|
612 D = D->getCanonicalDecl();
|
|
613
|
|
614 IgnoreResults =
|
|
615 IgnoreResults || printLoc(Out, D->getBeginLoc(),
|
|
616 Context->getSourceManager(), IncludeOffset);
|
|
617
|
|
618 return IgnoreResults;
|
|
619 }
|
|
620
|
|
621 static void printQualifier(llvm::raw_ostream &Out, ASTContext &Ctx, NestedNameSpecifier *NNS) {
|
|
622 // FIXME: Encode the qualifier, don't just print it.
|
|
623 PrintingPolicy PO(Ctx.getLangOpts());
|
|
624 PO.SuppressTagKeyword = true;
|
|
625 PO.SuppressUnwrittenScope = true;
|
|
626 PO.ConstantArraySizeAsWritten = false;
|
|
627 PO.AnonymousTagLocations = false;
|
|
628 NNS->print(Out, PO);
|
|
629 }
|
|
630
|
|
631 void USRGenerator::VisitType(QualType T) {
|
|
632 // This method mangles in USR information for types. It can possibly
|
|
633 // just reuse the naming-mangling logic used by codegen, although the
|
|
634 // requirements for USRs might not be the same.
|
|
635 ASTContext &Ctx = *Context;
|
|
636
|
|
637 do {
|
|
638 T = Ctx.getCanonicalType(T);
|
|
639 Qualifiers Q = T.getQualifiers();
|
|
640 unsigned qVal = 0;
|
|
641 if (Q.hasConst())
|
|
642 qVal |= 0x1;
|
|
643 if (Q.hasVolatile())
|
|
644 qVal |= 0x2;
|
|
645 if (Q.hasRestrict())
|
|
646 qVal |= 0x4;
|
|
647 if(qVal)
|
|
648 Out << ((char) ('0' + qVal));
|
|
649
|
|
650 // Mangle in ObjC GC qualifiers?
|
|
651
|
|
652 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) {
|
|
653 Out << 'P';
|
|
654 T = Expansion->getPattern();
|
|
655 }
|
|
656
|
|
657 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
|
|
658 unsigned char c = '\0';
|
|
659 switch (BT->getKind()) {
|
|
660 case BuiltinType::Void:
|
152
|
661 #ifndef noCbC
|
|
662 case BuiltinType::__Code:
|
|
663 #endif
|
150
|
664 c = 'v'; break;
|
|
665 case BuiltinType::Bool:
|
|
666 c = 'b'; break;
|
|
667 case BuiltinType::UChar:
|
|
668 c = 'c'; break;
|
|
669 case BuiltinType::Char8:
|
|
670 c = 'u'; break; // FIXME: Check this doesn't collide
|
|
671 case BuiltinType::Char16:
|
|
672 c = 'q'; break;
|
|
673 case BuiltinType::Char32:
|
|
674 c = 'w'; break;
|
|
675 case BuiltinType::UShort:
|
|
676 c = 's'; break;
|
|
677 case BuiltinType::UInt:
|
|
678 c = 'i'; break;
|
|
679 case BuiltinType::ULong:
|
|
680 c = 'l'; break;
|
|
681 case BuiltinType::ULongLong:
|
|
682 c = 'k'; break;
|
|
683 case BuiltinType::UInt128:
|
|
684 c = 'j'; break;
|
|
685 case BuiltinType::Char_U:
|
|
686 case BuiltinType::Char_S:
|
|
687 c = 'C'; break;
|
|
688 case BuiltinType::SChar:
|
|
689 c = 'r'; break;
|
|
690 case BuiltinType::WChar_S:
|
|
691 case BuiltinType::WChar_U:
|
|
692 c = 'W'; break;
|
|
693 case BuiltinType::Short:
|
|
694 c = 'S'; break;
|
|
695 case BuiltinType::Int:
|
|
696 c = 'I'; break;
|
|
697 case BuiltinType::Long:
|
|
698 c = 'L'; break;
|
|
699 case BuiltinType::LongLong:
|
|
700 c = 'K'; break;
|
|
701 case BuiltinType::Int128:
|
|
702 c = 'J'; break;
|
|
703 case BuiltinType::Float16:
|
|
704 case BuiltinType::Half:
|
|
705 c = 'h'; break;
|
|
706 case BuiltinType::Float:
|
|
707 c = 'f'; break;
|
|
708 case BuiltinType::Double:
|
|
709 c = 'd'; break;
|
|
710 case BuiltinType::LongDouble:
|
|
711 c = 'D'; break;
|
|
712 case BuiltinType::Float128:
|
|
713 c = 'Q'; break;
|
|
714 case BuiltinType::NullPtr:
|
|
715 c = 'n'; break;
|
|
716 #define BUILTIN_TYPE(Id, SingletonId)
|
|
717 #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
|
|
718 #include "clang/AST/BuiltinTypes.def"
|
|
719 case BuiltinType::Dependent:
|
|
720 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
|
|
721 case BuiltinType::Id:
|
|
722 #include "clang/Basic/OpenCLImageTypes.def"
|
|
723 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
|
|
724 case BuiltinType::Id:
|
|
725 #include "clang/Basic/OpenCLExtensionTypes.def"
|
|
726 case BuiltinType::OCLEvent:
|
|
727 case BuiltinType::OCLClkEvent:
|
|
728 case BuiltinType::OCLQueue:
|
|
729 case BuiltinType::OCLReserveID:
|
|
730 case BuiltinType::OCLSampler:
|
|
731 #define SVE_TYPE(Name, Id, SingletonId) \
|
|
732 case BuiltinType::Id:
|
|
733 #include "clang/Basic/AArch64SVEACLETypes.def"
|
|
734 case BuiltinType::ShortAccum:
|
|
735 case BuiltinType::Accum:
|
|
736 case BuiltinType::LongAccum:
|
|
737 case BuiltinType::UShortAccum:
|
|
738 case BuiltinType::UAccum:
|
|
739 case BuiltinType::ULongAccum:
|
|
740 case BuiltinType::ShortFract:
|
|
741 case BuiltinType::Fract:
|
|
742 case BuiltinType::LongFract:
|
|
743 case BuiltinType::UShortFract:
|
|
744 case BuiltinType::UFract:
|
|
745 case BuiltinType::ULongFract:
|
|
746 case BuiltinType::SatShortAccum:
|
|
747 case BuiltinType::SatAccum:
|
|
748 case BuiltinType::SatLongAccum:
|
|
749 case BuiltinType::SatUShortAccum:
|
|
750 case BuiltinType::SatUAccum:
|
|
751 case BuiltinType::SatULongAccum:
|
|
752 case BuiltinType::SatShortFract:
|
|
753 case BuiltinType::SatFract:
|
|
754 case BuiltinType::SatLongFract:
|
|
755 case BuiltinType::SatUShortFract:
|
|
756 case BuiltinType::SatUFract:
|
|
757 case BuiltinType::SatULongFract:
|
|
758 IgnoreResults = true;
|
|
759 return;
|
|
760 case BuiltinType::ObjCId:
|
|
761 c = 'o'; break;
|
|
762 case BuiltinType::ObjCClass:
|
|
763 c = 'O'; break;
|
|
764 case BuiltinType::ObjCSel:
|
|
765 c = 'e'; break;
|
|
766 }
|
|
767 Out << c;
|
|
768 return;
|
|
769 }
|
|
770
|
|
771 // If we have already seen this (non-built-in) type, use a substitution
|
|
772 // encoding.
|
|
773 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
|
|
774 = TypeSubstitutions.find(T.getTypePtr());
|
|
775 if (Substitution != TypeSubstitutions.end()) {
|
|
776 Out << 'S' << Substitution->second << '_';
|
|
777 return;
|
|
778 } else {
|
|
779 // Record this as a substitution.
|
|
780 unsigned Number = TypeSubstitutions.size();
|
|
781 TypeSubstitutions[T.getTypePtr()] = Number;
|
|
782 }
|
|
783
|
|
784 if (const PointerType *PT = T->getAs<PointerType>()) {
|
|
785 Out << '*';
|
|
786 T = PT->getPointeeType();
|
|
787 continue;
|
|
788 }
|
|
789 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
|
|
790 Out << '*';
|
|
791 T = OPT->getPointeeType();
|
|
792 continue;
|
|
793 }
|
|
794 if (const RValueReferenceType *RT = T->getAs<RValueReferenceType>()) {
|
|
795 Out << "&&";
|
|
796 T = RT->getPointeeType();
|
|
797 continue;
|
|
798 }
|
|
799 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
|
|
800 Out << '&';
|
|
801 T = RT->getPointeeType();
|
|
802 continue;
|
|
803 }
|
|
804 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
|
|
805 Out << 'F';
|
|
806 VisitType(FT->getReturnType());
|
|
807 Out << '(';
|
|
808 for (const auto &I : FT->param_types()) {
|
|
809 Out << '#';
|
|
810 VisitType(I);
|
|
811 }
|
|
812 Out << ')';
|
|
813 if (FT->isVariadic())
|
|
814 Out << '.';
|
|
815 return;
|
|
816 }
|
|
817 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
|
|
818 Out << 'B';
|
|
819 T = BT->getPointeeType();
|
|
820 continue;
|
|
821 }
|
|
822 if (const ComplexType *CT = T->getAs<ComplexType>()) {
|
|
823 Out << '<';
|
|
824 T = CT->getElementType();
|
|
825 continue;
|
|
826 }
|
|
827 if (const TagType *TT = T->getAs<TagType>()) {
|
|
828 Out << '$';
|
|
829 VisitTagDecl(TT->getDecl());
|
|
830 return;
|
|
831 }
|
|
832 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
|
|
833 Out << '$';
|
|
834 VisitObjCInterfaceDecl(OIT->getDecl());
|
|
835 return;
|
|
836 }
|
|
837 if (const ObjCObjectType *OIT = T->getAs<ObjCObjectType>()) {
|
|
838 Out << 'Q';
|
|
839 VisitType(OIT->getBaseType());
|
|
840 for (auto *Prot : OIT->getProtocols())
|
|
841 VisitObjCProtocolDecl(Prot);
|
|
842 return;
|
|
843 }
|
|
844 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
|
|
845 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
|
|
846 return;
|
|
847 }
|
|
848 if (const TemplateSpecializationType *Spec
|
|
849 = T->getAs<TemplateSpecializationType>()) {
|
|
850 Out << '>';
|
|
851 VisitTemplateName(Spec->getTemplateName());
|
|
852 Out << Spec->getNumArgs();
|
|
853 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
|
|
854 VisitTemplateArgument(Spec->getArg(I));
|
|
855 return;
|
|
856 }
|
|
857 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
|
|
858 Out << '^';
|
|
859 printQualifier(Out, Ctx, DNT->getQualifier());
|
|
860 Out << ':' << DNT->getIdentifier()->getName();
|
|
861 return;
|
|
862 }
|
|
863 if (const InjectedClassNameType *InjT = T->getAs<InjectedClassNameType>()) {
|
|
864 T = InjT->getInjectedSpecializationType();
|
|
865 continue;
|
|
866 }
|
|
867 if (const auto *VT = T->getAs<VectorType>()) {
|
|
868 Out << (T->isExtVectorType() ? ']' : '[');
|
|
869 Out << VT->getNumElements();
|
|
870 T = VT->getElementType();
|
|
871 continue;
|
|
872 }
|
|
873 if (const auto *const AT = dyn_cast<ArrayType>(T)) {
|
|
874 Out << '{';
|
|
875 switch (AT->getSizeModifier()) {
|
|
876 case ArrayType::Static:
|
|
877 Out << 's';
|
|
878 break;
|
|
879 case ArrayType::Star:
|
|
880 Out << '*';
|
|
881 break;
|
|
882 case ArrayType::Normal:
|
|
883 Out << 'n';
|
|
884 break;
|
|
885 }
|
|
886 if (const auto *const CAT = dyn_cast<ConstantArrayType>(T))
|
|
887 Out << CAT->getSize();
|
|
888
|
|
889 T = AT->getElementType();
|
|
890 continue;
|
|
891 }
|
|
892
|
|
893 // Unhandled type.
|
|
894 Out << ' ';
|
|
895 break;
|
|
896 } while (true);
|
|
897 }
|
|
898
|
|
899 void USRGenerator::VisitTemplateParameterList(
|
|
900 const TemplateParameterList *Params) {
|
|
901 if (!Params)
|
|
902 return;
|
|
903 Out << '>' << Params->size();
|
|
904 for (TemplateParameterList::const_iterator P = Params->begin(),
|
|
905 PEnd = Params->end();
|
|
906 P != PEnd; ++P) {
|
|
907 Out << '#';
|
|
908 if (isa<TemplateTypeParmDecl>(*P)) {
|
|
909 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
|
|
910 Out<< 'p';
|
|
911 Out << 'T';
|
|
912 continue;
|
|
913 }
|
|
914
|
|
915 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
|
|
916 if (NTTP->isParameterPack())
|
|
917 Out << 'p';
|
|
918 Out << 'N';
|
|
919 VisitType(NTTP->getType());
|
|
920 continue;
|
|
921 }
|
|
922
|
|
923 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
|
|
924 if (TTP->isParameterPack())
|
|
925 Out << 'p';
|
|
926 Out << 't';
|
|
927 VisitTemplateParameterList(TTP->getTemplateParameters());
|
|
928 }
|
|
929 }
|
|
930
|
|
931 void USRGenerator::VisitTemplateName(TemplateName Name) {
|
|
932 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
|
|
933 if (TemplateTemplateParmDecl *TTP
|
|
934 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
|
|
935 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
|
|
936 return;
|
|
937 }
|
|
938
|
|
939 Visit(Template);
|
|
940 return;
|
|
941 }
|
|
942
|
|
943 // FIXME: Visit dependent template names.
|
|
944 }
|
|
945
|
|
946 void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
|
|
947 switch (Arg.getKind()) {
|
|
948 case TemplateArgument::Null:
|
|
949 break;
|
|
950
|
|
951 case TemplateArgument::Declaration:
|
|
952 Visit(Arg.getAsDecl());
|
|
953 break;
|
|
954
|
|
955 case TemplateArgument::NullPtr:
|
|
956 break;
|
|
957
|
|
958 case TemplateArgument::TemplateExpansion:
|
|
959 Out << 'P'; // pack expansion of...
|
|
960 LLVM_FALLTHROUGH;
|
|
961 case TemplateArgument::Template:
|
|
962 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
|
|
963 break;
|
|
964
|
|
965 case TemplateArgument::Expression:
|
|
966 // FIXME: Visit expressions.
|
|
967 break;
|
|
968
|
|
969 case TemplateArgument::Pack:
|
|
970 Out << 'p' << Arg.pack_size();
|
|
971 for (const auto &P : Arg.pack_elements())
|
|
972 VisitTemplateArgument(P);
|
|
973 break;
|
|
974
|
|
975 case TemplateArgument::Type:
|
|
976 VisitType(Arg.getAsType());
|
|
977 break;
|
|
978
|
|
979 case TemplateArgument::Integral:
|
|
980 Out << 'V';
|
|
981 VisitType(Arg.getIntegralType());
|
|
982 Out << Arg.getAsIntegral();
|
|
983 break;
|
|
984 }
|
|
985 }
|
|
986
|
|
987 void USRGenerator::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
|
|
988 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
|
|
989 return;
|
|
990 VisitDeclContext(D->getDeclContext());
|
|
991 Out << "@UUV@";
|
|
992 printQualifier(Out, D->getASTContext(), D->getQualifier());
|
|
993 EmitDeclName(D);
|
|
994 }
|
|
995
|
|
996 void USRGenerator::VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) {
|
|
997 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
|
|
998 return;
|
|
999 VisitDeclContext(D->getDeclContext());
|
|
1000 Out << "@UUT@";
|
|
1001 printQualifier(Out, D->getASTContext(), D->getQualifier());
|
|
1002 Out << D->getName(); // Simple name.
|
|
1003 }
|
|
1004
|
|
1005
|
|
1006
|
|
1007 //===----------------------------------------------------------------------===//
|
|
1008 // USR generation functions.
|
|
1009 //===----------------------------------------------------------------------===//
|
|
1010
|
|
1011 static void combineClassAndCategoryExtContainers(StringRef ClsSymDefinedIn,
|
|
1012 StringRef CatSymDefinedIn,
|
|
1013 raw_ostream &OS) {
|
|
1014 if (ClsSymDefinedIn.empty() && CatSymDefinedIn.empty())
|
|
1015 return;
|
|
1016 if (CatSymDefinedIn.empty()) {
|
|
1017 OS << "@M@" << ClsSymDefinedIn << '@';
|
|
1018 return;
|
|
1019 }
|
|
1020 OS << "@CM@" << CatSymDefinedIn << '@';
|
|
1021 if (ClsSymDefinedIn != CatSymDefinedIn) {
|
|
1022 OS << ClsSymDefinedIn << '@';
|
|
1023 }
|
|
1024 }
|
|
1025
|
|
1026 void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS,
|
|
1027 StringRef ExtSymDefinedIn,
|
|
1028 StringRef CategoryContextExtSymbolDefinedIn) {
|
|
1029 combineClassAndCategoryExtContainers(ExtSymDefinedIn,
|
|
1030 CategoryContextExtSymbolDefinedIn, OS);
|
|
1031 OS << "objc(cs)" << Cls;
|
|
1032 }
|
|
1033
|
|
1034 void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat,
|
|
1035 raw_ostream &OS,
|
|
1036 StringRef ClsSymDefinedIn,
|
|
1037 StringRef CatSymDefinedIn) {
|
|
1038 combineClassAndCategoryExtContainers(ClsSymDefinedIn, CatSymDefinedIn, OS);
|
|
1039 OS << "objc(cy)" << Cls << '@' << Cat;
|
|
1040 }
|
|
1041
|
|
1042 void clang::index::generateUSRForObjCIvar(StringRef Ivar, raw_ostream &OS) {
|
|
1043 OS << '@' << Ivar;
|
|
1044 }
|
|
1045
|
|
1046 void clang::index::generateUSRForObjCMethod(StringRef Sel,
|
|
1047 bool IsInstanceMethod,
|
|
1048 raw_ostream &OS) {
|
|
1049 OS << (IsInstanceMethod ? "(im)" : "(cm)") << Sel;
|
|
1050 }
|
|
1051
|
|
1052 void clang::index::generateUSRForObjCProperty(StringRef Prop, bool isClassProp,
|
|
1053 raw_ostream &OS) {
|
|
1054 OS << (isClassProp ? "(cpy)" : "(py)") << Prop;
|
|
1055 }
|
|
1056
|
|
1057 void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS,
|
|
1058 StringRef ExtSymDefinedIn) {
|
|
1059 if (!ExtSymDefinedIn.empty())
|
|
1060 OS << "@M@" << ExtSymDefinedIn << '@';
|
|
1061 OS << "objc(pl)" << Prot;
|
|
1062 }
|
|
1063
|
|
1064 void clang::index::generateUSRForGlobalEnum(StringRef EnumName, raw_ostream &OS,
|
|
1065 StringRef ExtSymDefinedIn) {
|
|
1066 if (!ExtSymDefinedIn.empty())
|
|
1067 OS << "@M@" << ExtSymDefinedIn;
|
|
1068 OS << "@E@" << EnumName;
|
|
1069 }
|
|
1070
|
|
1071 void clang::index::generateUSRForEnumConstant(StringRef EnumConstantName,
|
|
1072 raw_ostream &OS) {
|
|
1073 OS << '@' << EnumConstantName;
|
|
1074 }
|
|
1075
|
|
1076 bool clang::index::generateUSRForDecl(const Decl *D,
|
|
1077 SmallVectorImpl<char> &Buf) {
|
|
1078 if (!D)
|
|
1079 return true;
|
|
1080 // We don't ignore decls with invalid source locations. Implicit decls, like
|
|
1081 // C++'s operator new function, can have invalid locations but it is fine to
|
|
1082 // create USRs that can identify them.
|
|
1083
|
|
1084 USRGenerator UG(&D->getASTContext(), Buf);
|
|
1085 UG.Visit(D);
|
|
1086 return UG.ignoreResults();
|
|
1087 }
|
|
1088
|
|
1089 bool clang::index::generateUSRForMacro(const MacroDefinitionRecord *MD,
|
|
1090 const SourceManager &SM,
|
|
1091 SmallVectorImpl<char> &Buf) {
|
|
1092 if (!MD)
|
|
1093 return true;
|
|
1094 return generateUSRForMacro(MD->getName()->getName(), MD->getLocation(),
|
|
1095 SM, Buf);
|
|
1096
|
|
1097 }
|
|
1098
|
|
1099 bool clang::index::generateUSRForMacro(StringRef MacroName, SourceLocation Loc,
|
|
1100 const SourceManager &SM,
|
|
1101 SmallVectorImpl<char> &Buf) {
|
|
1102 // Don't generate USRs for things with invalid locations.
|
|
1103 if (MacroName.empty() || Loc.isInvalid())
|
|
1104 return true;
|
|
1105
|
|
1106 llvm::raw_svector_ostream Out(Buf);
|
|
1107
|
|
1108 // Assume that system headers are sane. Don't put source location
|
|
1109 // information into the USR if the macro comes from a system header.
|
|
1110 bool ShouldGenerateLocation = !SM.isInSystemHeader(Loc);
|
|
1111
|
|
1112 Out << getUSRSpacePrefix();
|
|
1113 if (ShouldGenerateLocation)
|
|
1114 printLoc(Out, Loc, SM, /*IncludeOffset=*/true);
|
|
1115 Out << "@macro@";
|
|
1116 Out << MacroName;
|
|
1117 return false;
|
|
1118 }
|
|
1119
|
|
1120 bool clang::index::generateUSRForType(QualType T, ASTContext &Ctx,
|
|
1121 SmallVectorImpl<char> &Buf) {
|
|
1122 if (T.isNull())
|
|
1123 return true;
|
|
1124 T = T.getCanonicalType();
|
|
1125
|
|
1126 USRGenerator UG(&Ctx, Buf);
|
|
1127 UG.VisitType(T);
|
|
1128 return UG.ignoreResults();
|
|
1129 }
|
|
1130
|
|
1131 bool clang::index::generateFullUSRForModule(const Module *Mod,
|
|
1132 raw_ostream &OS) {
|
|
1133 if (!Mod->Parent)
|
|
1134 return generateFullUSRForTopLevelModuleName(Mod->Name, OS);
|
|
1135 if (generateFullUSRForModule(Mod->Parent, OS))
|
|
1136 return true;
|
|
1137 return generateUSRFragmentForModule(Mod, OS);
|
|
1138 }
|
|
1139
|
|
1140 bool clang::index::generateFullUSRForTopLevelModuleName(StringRef ModName,
|
|
1141 raw_ostream &OS) {
|
|
1142 OS << getUSRSpacePrefix();
|
|
1143 return generateUSRFragmentForModuleName(ModName, OS);
|
|
1144 }
|
|
1145
|
|
1146 bool clang::index::generateUSRFragmentForModule(const Module *Mod,
|
|
1147 raw_ostream &OS) {
|
|
1148 return generateUSRFragmentForModuleName(Mod->Name, OS);
|
|
1149 }
|
|
1150
|
|
1151 bool clang::index::generateUSRFragmentForModuleName(StringRef ModName,
|
|
1152 raw_ostream &OS) {
|
|
1153 OS << "@M@" << ModName;
|
|
1154 return false;
|
|
1155 }
|