blob: 3a06554b256c6c2cbd3b475e075f54b0574b25a0 [file] [log] [blame]
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001//===- USRGeneration.cpp - Routines for USR generation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Argyrios Kyrtzidis15a2fcc2013-08-17 00:40:41 +000010#include "clang/Index/USRGeneration.h"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000011#include "clang/AST/ASTContext.h"
12#include "clang/AST/DeclTemplate.h"
13#include "clang/AST/DeclVisitor.h"
Dmitri Gribenko237769e2014-03-28 22:21:26 +000014#include "clang/Lex/PreprocessingRecord.h"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000015#include "llvm/Support/Path.h"
16#include "llvm/Support/raw_ostream.h"
17
18using namespace clang;
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +000019using namespace clang::index;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000020
21//===----------------------------------------------------------------------===//
22// USR generation.
23//===----------------------------------------------------------------------===//
24
Dmitri Gribenko237769e2014-03-28 22:21:26 +000025/// \returns true on error.
26static 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
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +000049static StringRef GetExternalSourceContainer(const NamedDecl *D) {
50 if (!D)
51 return StringRef();
Argyrios Kyrtzidis11d70482017-05-20 04:11:33 +000052 if (auto *attr = D->getExternalSourceSymbolAttr()) {
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +000053 return attr->getDefinedIn();
54 }
55 return StringRef();
56}
57
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000058namespace {
59class 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
68public:
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.
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +000077 Out << getUSRSpacePrefix();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000078 }
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);
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +000091 void VisitObjCContainerDecl(const ObjCContainerDecl *CD,
92 const ObjCCategoryDecl *CatD = nullptr);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000093 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 VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
101 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000102 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
103 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000104
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000105 void VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
106 IgnoreResults = true;
107 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000108
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000109 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
110 IgnoreResults = true;
111 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000112
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000113 void VisitUsingDecl(const UsingDecl *D) {
114 IgnoreResults = true;
115 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000116
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000117 bool ShouldGenerateLocation(const NamedDecl *D);
118
119 bool isLocal(const NamedDecl *D) {
Craig Topper236bde32014-05-26 06:21:51 +0000120 return D->getParentFunctionOrMethod() != nullptr;
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000121 }
122
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000123 void GenExtSymbolContainer(const NamedDecl *D);
124
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000125 /// Generate the string component containing the location of the
126 /// declaration.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000127 bool GenLoc(const Decl *D, bool IncludeOffset);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000128
129 /// String generation methods used both by the visitation methods
130 /// and from other clients that want to directly generate USRs. These
131 /// methods do not construct complete USRs (which incorporate the parents
132 /// of an AST element), but only the fragments concerning the AST element
133 /// itself.
134
135 /// Generate a USR for an Objective-C class.
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000136 void GenObjCClass(StringRef cls, StringRef ExtSymDefinedIn,
137 StringRef CategoryContextExtSymbolDefinedIn) {
138 generateUSRForObjCClass(cls, Out, ExtSymDefinedIn,
139 CategoryContextExtSymbolDefinedIn);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000140 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000141
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000142 /// Generate a USR for an Objective-C class category.
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000143 void GenObjCCategory(StringRef cls, StringRef cat,
144 StringRef clsExt, StringRef catExt) {
145 generateUSRForObjCCategory(cls, cat, Out, clsExt, catExt);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000146 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000147
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000148 /// Generate a USR fragment for an Objective-C property.
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000149 void GenObjCProperty(StringRef prop, bool isClassProp) {
150 generateUSRForObjCProperty(prop, isClassProp, Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000151 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000152
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000153 /// Generate a USR for an Objective-C protocol.
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000154 void GenObjCProtocol(StringRef prot, StringRef ext) {
155 generateUSRForObjCProtocol(prot, Out, ext);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000156 }
157
158 void VisitType(QualType T);
159 void VisitTemplateParameterList(const TemplateParameterList *Params);
160 void VisitTemplateName(TemplateName Name);
161 void VisitTemplateArgument(const TemplateArgument &Arg);
162
163 /// Emit a Decl's name using NamedDecl::printName() and return true if
164 /// the decl had no name.
165 bool EmitDeclName(const NamedDecl *D);
166};
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000167} // end anonymous namespace
168
169//===----------------------------------------------------------------------===//
170// Generating USRs from ASTS.
171//===----------------------------------------------------------------------===//
172
173bool USRGenerator::EmitDeclName(const NamedDecl *D) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000174 const unsigned startSize = Buf.size();
175 D->printName(Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000176 const unsigned endSize = Buf.size();
177 return startSize == endSize;
178}
179
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000180bool USRGenerator::ShouldGenerateLocation(const NamedDecl *D) {
181 if (D->isExternallyVisible())
182 return false;
183 if (D->getParentFunctionOrMethod())
184 return true;
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000185 SourceLocation Loc = D->getLocation();
186 if (Loc.isInvalid())
187 return false;
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000188 const SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000189 return !SM.isInSystemHeader(Loc);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000190}
191
192void USRGenerator::VisitDeclContext(const DeclContext *DC) {
193 if (const NamedDecl *D = dyn_cast<NamedDecl>(DC))
194 Visit(D);
195}
196
197void USRGenerator::VisitFieldDecl(const FieldDecl *D) {
198 // The USR for an ivar declared in a class extension is based on the
199 // ObjCInterfaceDecl, not the ObjCCategoryDecl.
200 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
201 Visit(ID);
202 else
203 VisitDeclContext(D->getDeclContext());
204 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
205 if (EmitDeclName(D)) {
206 // Bit fields can be anonymous.
207 IgnoreResults = true;
208 return;
209 }
210}
211
212void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000213 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000214 return;
215
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000216 const unsigned StartSize = Buf.size();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000217 VisitDeclContext(D->getDeclContext());
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000218 if (Buf.size() == StartSize)
219 GenExtSymbolContainer(D);
220
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000221 bool IsTemplate = false;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000222 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000223 IsTemplate = true;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000224 Out << "@FT@";
225 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
226 } else
227 Out << "@F@";
Argyrios Kyrtzidisd5719082016-02-15 01:32:36 +0000228
229 PrintingPolicy Policy(Context->getLangOpts());
230 // Forward references can have different template argument names. Suppress the
231 // template argument names in constructors to make their USR more stable.
232 Policy.SuppressTemplateArgsInCXXConstructors = true;
233 D->getDeclName().print(Out, Policy);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000234
235 ASTContext &Ctx = *Context;
Argyrios Kyrtzidis2682f9e2016-03-04 07:17:48 +0000236 if ((!Ctx.getLangOpts().CPlusPlus || D->isExternC()) &&
237 !D->hasAttr<OverloadableAttr>())
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000238 return;
239
240 if (const TemplateArgumentList *
241 SpecArgs = D->getTemplateSpecializationArgs()) {
242 Out << '<';
243 for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) {
244 Out << '#';
245 VisitTemplateArgument(SpecArgs->get(I));
246 }
247 Out << '>';
248 }
249
250 // Mangle in type information for the arguments.
David Majnemer59f77922016-06-24 04:05:48 +0000251 for (auto PD : D->parameters()) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000252 Out << '#';
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +0000253 VisitType(PD->getType());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000254 }
255 if (D->isVariadic())
256 Out << '.';
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000257 if (IsTemplate) {
258 // Function templates can be overloaded by return type, for example:
259 // \code
260 // template <class T> typename T::A foo() {}
261 // template <class T> typename T::B foo() {}
262 // \endcode
263 Out << '#';
264 VisitType(D->getReturnType());
265 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000266 Out << '#';
267 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
268 if (MD->isStatic())
269 Out << 'S';
270 if (unsigned quals = MD->getTypeQualifiers())
271 Out << (char)('0' + quals);
Argyrios Kyrtzidisf5819092014-12-08 08:48:21 +0000272 switch (MD->getRefQualifier()) {
273 case RQ_None: break;
274 case RQ_LValue: Out << '&'; break;
275 case RQ_RValue: Out << "&&"; break;
276 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000277 }
278}
279
280void USRGenerator::VisitNamedDecl(const NamedDecl *D) {
281 VisitDeclContext(D->getDeclContext());
282 Out << "@";
283
284 if (EmitDeclName(D)) {
285 // The string can be empty if the declaration has no name; e.g., it is
286 // the ParmDecl with no name for declaration of a function pointer type,
287 // e.g.: void (*f)(void *);
288 // In this case, don't generate a USR.
289 IgnoreResults = true;
290 }
291}
292
293void USRGenerator::VisitVarDecl(const VarDecl *D) {
294 // VarDecls can be declared 'extern' within a function or method body,
295 // but their enclosing DeclContext is the function, not the TU. We need
296 // to check the storage class to correctly generate the USR.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000297 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000298 return;
299
300 VisitDeclContext(D->getDeclContext());
301
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000302 if (VarTemplateDecl *VarTmpl = D->getDescribedVarTemplate()) {
303 Out << "@VT";
304 VisitTemplateParameterList(VarTmpl->getTemplateParameters());
305 } else if (const VarTemplatePartialSpecializationDecl *PartialSpec
306 = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
307 Out << "@VP";
308 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
309 }
310
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000311 // Variables always have simple names.
312 StringRef s = D->getName();
313
314 // The string can be empty if the declaration has no name; e.g., it is
315 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
316 // void (*f)(void *);
317 // In this case, don't generate a USR.
318 if (s.empty())
319 IgnoreResults = true;
320 else
321 Out << '@' << s;
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000322
323 // For a template specialization, mangle the template arguments.
324 if (const VarTemplateSpecializationDecl *Spec
325 = dyn_cast<VarTemplateSpecializationDecl>(D)) {
Argyrios Kyrtzidis7d90ed02017-02-15 16:16:27 +0000326 const TemplateArgumentList &Args = Spec->getTemplateArgs();
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000327 Out << '>';
328 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
329 Out << '#';
330 VisitTemplateArgument(Args.get(I));
331 }
332 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000333}
334
335void USRGenerator::VisitNonTypeTemplateParmDecl(
336 const NonTypeTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000337 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000338}
339
340void USRGenerator::VisitTemplateTemplateParmDecl(
341 const TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000342 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000343}
344
345void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) {
346 if (D->isAnonymousNamespace()) {
347 Out << "@aN";
348 return;
349 }
350
351 VisitDeclContext(D->getDeclContext());
352 if (!IgnoreResults)
353 Out << "@N@" << D->getName();
354}
355
356void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
357 VisitFunctionDecl(D->getTemplatedDecl());
358}
359
360void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
361 VisitTagDecl(D->getTemplatedDecl());
362}
363
364void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
365 VisitDeclContext(D->getDeclContext());
366 if (!IgnoreResults)
367 Out << "@NA@" << D->getName();
368}
369
370void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
371 const DeclContext *container = D->getDeclContext();
372 if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) {
373 Visit(pd);
374 }
375 else {
376 // The USR for a method declared in a class extension or category is based on
377 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
378 const ObjCInterfaceDecl *ID = D->getClassInterface();
379 if (!ID) {
380 IgnoreResults = true;
381 return;
382 }
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000383 auto getCategoryContext = [](const ObjCMethodDecl *D) ->
384 const ObjCCategoryDecl * {
385 if (auto *CD = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
386 return CD;
387 if (auto *ICD = dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
388 return ICD->getCategoryDecl();
389 return nullptr;
390 };
391 auto *CD = getCategoryContext(D);
392 VisitObjCContainerDecl(ID, CD);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000393 }
394 // Ideally we would use 'GenObjCMethod', but this is such a hot path
395 // for Objective-C code that we don't want to use
396 // DeclarationName::getAsString().
397 Out << (D->isInstanceMethod() ? "(im)" : "(cm)")
398 << DeclarationName(D->getSelector());
399}
400
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000401void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D,
402 const ObjCCategoryDecl *CatD) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000403 switch (D->getKind()) {
404 default:
405 llvm_unreachable("Invalid ObjC container.");
406 case Decl::ObjCInterface:
407 case Decl::ObjCImplementation:
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000408 GenObjCClass(D->getName(), GetExternalSourceContainer(D),
409 GetExternalSourceContainer(CatD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000410 break;
411 case Decl::ObjCCategory: {
412 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
413 const ObjCInterfaceDecl *ID = CD->getClassInterface();
414 if (!ID) {
415 // Handle invalid code where the @interface might not
416 // have been specified.
417 // FIXME: We should be able to generate this USR even if the
418 // @interface isn't available.
419 IgnoreResults = true;
420 return;
421 }
422 // Specially handle class extensions, which are anonymous categories.
423 // We want to mangle in the location to uniquely distinguish them.
424 if (CD->IsClassExtension()) {
425 Out << "objc(ext)" << ID->getName() << '@';
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000426 GenLoc(CD, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000427 }
428 else
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000429 GenObjCCategory(ID->getName(), CD->getName(),
430 GetExternalSourceContainer(ID),
431 GetExternalSourceContainer(CD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000432
433 break;
434 }
435 case Decl::ObjCCategoryImpl: {
436 const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
437 const ObjCInterfaceDecl *ID = CD->getClassInterface();
438 if (!ID) {
439 // Handle invalid code where the @interface might not
440 // have been specified.
441 // FIXME: We should be able to generate this USR even if the
442 // @interface isn't available.
443 IgnoreResults = true;
444 return;
445 }
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000446 GenObjCCategory(ID->getName(), CD->getName(),
447 GetExternalSourceContainer(ID),
448 GetExternalSourceContainer(CD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000449 break;
450 }
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000451 case Decl::ObjCProtocol: {
452 const ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D);
453 GenObjCProtocol(PD->getName(), GetExternalSourceContainer(PD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000454 break;
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000455 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000456 }
457}
458
459void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
460 // The USR for a property declared in a class extension or category is based
461 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl.
462 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
463 Visit(ID);
464 else
465 Visit(cast<Decl>(D->getDeclContext()));
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000466 GenObjCProperty(D->getName(), D->isClassProperty());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000467}
468
469void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
470 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
471 VisitObjCPropertyDecl(PD);
472 return;
473 }
474
475 IgnoreResults = true;
476}
477
478void USRGenerator::VisitTagDecl(const TagDecl *D) {
479 // Add the location of the tag decl to handle resolution across
480 // translation units.
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000481 if (!isa<EnumDecl>(D) &&
482 ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000483 return;
484
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000485 GenExtSymbolContainer(D);
486
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000487 D = D->getCanonicalDecl();
488 VisitDeclContext(D->getDeclContext());
489
490 bool AlreadyStarted = false;
491 if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
492 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
493 AlreadyStarted = true;
494
495 switch (D->getTagKind()) {
496 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000497 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000498 case TTK_Struct: Out << "@ST"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000499 case TTK_Union: Out << "@UT"; break;
500 case TTK_Enum: llvm_unreachable("enum template");
501 }
502 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
503 } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec
504 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
505 AlreadyStarted = true;
506
507 switch (D->getTagKind()) {
508 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000509 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000510 case TTK_Struct: Out << "@SP"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000511 case TTK_Union: Out << "@UP"; break;
512 case TTK_Enum: llvm_unreachable("enum partial specialization");
513 }
514 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
515 }
516 }
517
518 if (!AlreadyStarted) {
519 switch (D->getTagKind()) {
520 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000521 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000522 case TTK_Struct: Out << "@S"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000523 case TTK_Union: Out << "@U"; break;
524 case TTK_Enum: Out << "@E"; break;
525 }
526 }
527
528 Out << '@';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000529 assert(Buf.size() > 0);
530 const unsigned off = Buf.size() - 1;
531
532 if (EmitDeclName(D)) {
533 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) {
534 Buf[off] = 'A';
535 Out << '@' << *TD;
536 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000537 else {
538 if (D->isEmbeddedInDeclarator() && !D->isFreeStanding()) {
539 printLoc(Out, D->getLocation(), Context->getSourceManager(), true);
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000540 } else {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000541 Buf[off] = 'a';
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000542 if (auto *ED = dyn_cast<EnumDecl>(D)) {
543 // Distinguish USRs of anonymous enums by using their first enumerator.
544 auto enum_range = ED->enumerators();
545 if (enum_range.begin() != enum_range.end()) {
546 Out << '@' << **enum_range.begin();
547 }
548 }
549 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000550 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000551 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000552
553 // For a class template specialization, mangle the template arguments.
554 if (const ClassTemplateSpecializationDecl *Spec
555 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
Argyrios Kyrtzidis7d90ed02017-02-15 16:16:27 +0000556 const TemplateArgumentList &Args = Spec->getTemplateArgs();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000557 Out << '>';
558 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
559 Out << '#';
560 VisitTemplateArgument(Args.get(I));
561 }
562 }
563}
564
565void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000566 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000567 return;
568 const DeclContext *DC = D->getDeclContext();
569 if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
570 Visit(DCN);
571 Out << "@T@";
572 Out << D->getName();
573}
574
575void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000576 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000577}
578
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000579void USRGenerator::GenExtSymbolContainer(const NamedDecl *D) {
580 StringRef Container = GetExternalSourceContainer(D);
581 if (!Container.empty())
582 Out << "@M@" << Container;
583}
584
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000585bool USRGenerator::GenLoc(const Decl *D, bool IncludeOffset) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000586 if (generatedLoc)
587 return IgnoreResults;
588 generatedLoc = true;
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000589
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000590 // Guard against null declarations in invalid code.
591 if (!D) {
592 IgnoreResults = true;
593 return true;
594 }
595
596 // Use the location of canonical decl.
597 D = D->getCanonicalDecl();
598
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000599 IgnoreResults =
600 IgnoreResults || printLoc(Out, D->getLocStart(),
601 Context->getSourceManager(), IncludeOffset);
602
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000603 return IgnoreResults;
604}
605
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000606static void printQualifier(llvm::raw_ostream &Out, ASTContext &Ctx, NestedNameSpecifier *NNS) {
607 // FIXME: Encode the qualifier, don't just print it.
608 PrintingPolicy PO(Ctx.getLangOpts());
609 PO.SuppressTagKeyword = true;
610 PO.SuppressUnwrittenScope = true;
611 PO.ConstantArraySizeAsWritten = false;
612 PO.AnonymousTagLocations = false;
613 NNS->print(Out, PO);
614}
615
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000616void USRGenerator::VisitType(QualType T) {
617 // This method mangles in USR information for types. It can possibly
618 // just reuse the naming-mangling logic used by codegen, although the
619 // requirements for USRs might not be the same.
620 ASTContext &Ctx = *Context;
621
622 do {
623 T = Ctx.getCanonicalType(T);
624 Qualifiers Q = T.getQualifiers();
625 unsigned qVal = 0;
626 if (Q.hasConst())
627 qVal |= 0x1;
628 if (Q.hasVolatile())
629 qVal |= 0x2;
630 if (Q.hasRestrict())
631 qVal |= 0x4;
632 if(qVal)
633 Out << ((char) ('0' + qVal));
634
635 // Mangle in ObjC GC qualifiers?
636
637 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) {
638 Out << 'P';
639 T = Expansion->getPattern();
640 }
641
642 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
643 unsigned char c = '\0';
644 switch (BT->getKind()) {
645 case BuiltinType::Void:
646 c = 'v'; break;
647 case BuiltinType::Bool:
648 c = 'b'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000649 case BuiltinType::UChar:
650 c = 'c'; break;
651 case BuiltinType::Char16:
652 c = 'q'; break;
653 case BuiltinType::Char32:
654 c = 'w'; break;
655 case BuiltinType::UShort:
656 c = 's'; break;
657 case BuiltinType::UInt:
658 c = 'i'; break;
659 case BuiltinType::ULong:
660 c = 'l'; break;
661 case BuiltinType::ULongLong:
662 c = 'k'; break;
663 case BuiltinType::UInt128:
664 c = 'j'; break;
Argyrios Kyrtzidis56af7e62014-12-08 09:09:05 +0000665 case BuiltinType::Char_U:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000666 case BuiltinType::Char_S:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000667 c = 'C'; break;
Argyrios Kyrtzidisca044542014-12-08 08:48:17 +0000668 case BuiltinType::SChar:
669 c = 'r'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000670 case BuiltinType::WChar_S:
671 case BuiltinType::WChar_U:
672 c = 'W'; break;
673 case BuiltinType::Short:
674 c = 'S'; break;
675 case BuiltinType::Int:
676 c = 'I'; break;
677 case BuiltinType::Long:
678 c = 'L'; break;
679 case BuiltinType::LongLong:
680 c = 'K'; break;
681 case BuiltinType::Int128:
682 c = 'J'; break;
Sjoerd Meijercc623ad2017-09-08 15:15:00 +0000683 case BuiltinType::Float16:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000684 case BuiltinType::Half:
685 c = 'h'; break;
686 case BuiltinType::Float:
687 c = 'f'; break;
688 case BuiltinType::Double:
689 c = 'd'; break;
690 case BuiltinType::LongDouble:
691 c = 'D'; break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000692 case BuiltinType::Float128:
693 c = 'Q'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000694 case BuiltinType::NullPtr:
695 c = 'n'; break;
696#define BUILTIN_TYPE(Id, SingletonId)
697#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
698#include "clang/AST/BuiltinTypes.def"
699 case BuiltinType::Dependent:
Alexey Bader954ba212016-04-08 13:40:33 +0000700#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
701 case BuiltinType::Id:
Alexey Baderb62f1442016-04-13 08:33:41 +0000702#include "clang/Basic/OpenCLImageTypes.def"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000703 case BuiltinType::OCLEvent:
Alexey Bader9c8453f2015-09-15 11:18:52 +0000704 case BuiltinType::OCLClkEvent:
705 case BuiltinType::OCLQueue:
Alexey Bader9c8453f2015-09-15 11:18:52 +0000706 case BuiltinType::OCLReserveID:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000707 case BuiltinType::OCLSampler:
708 IgnoreResults = true;
709 return;
710 case BuiltinType::ObjCId:
711 c = 'o'; break;
712 case BuiltinType::ObjCClass:
713 c = 'O'; break;
714 case BuiltinType::ObjCSel:
715 c = 'e'; break;
716 }
717 Out << c;
718 return;
719 }
720
721 // If we have already seen this (non-built-in) type, use a substitution
722 // encoding.
723 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
724 = TypeSubstitutions.find(T.getTypePtr());
725 if (Substitution != TypeSubstitutions.end()) {
726 Out << 'S' << Substitution->second << '_';
727 return;
728 } else {
729 // Record this as a substitution.
730 unsigned Number = TypeSubstitutions.size();
731 TypeSubstitutions[T.getTypePtr()] = Number;
732 }
733
734 if (const PointerType *PT = T->getAs<PointerType>()) {
735 Out << '*';
736 T = PT->getPointeeType();
737 continue;
738 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000739 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
740 Out << '*';
741 T = OPT->getPointeeType();
742 continue;
743 }
Argyrios Kyrtzidis0e387dc2014-12-08 08:48:27 +0000744 if (const RValueReferenceType *RT = T->getAs<RValueReferenceType>()) {
745 Out << "&&";
746 T = RT->getPointeeType();
747 continue;
748 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000749 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
750 Out << '&';
751 T = RT->getPointeeType();
752 continue;
753 }
754 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
755 Out << 'F';
Alp Toker314cc812014-01-25 16:55:45 +0000756 VisitType(FT->getReturnType());
Jan Korouse6a02422017-10-10 00:35:16 +0000757 Out << '(';
758 for (const auto &I : FT->param_types()) {
759 Out << '#';
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000760 VisitType(I);
Jan Korouse6a02422017-10-10 00:35:16 +0000761 }
762 Out << ')';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000763 if (FT->isVariadic())
764 Out << '.';
765 return;
766 }
767 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
768 Out << 'B';
769 T = BT->getPointeeType();
770 continue;
771 }
772 if (const ComplexType *CT = T->getAs<ComplexType>()) {
773 Out << '<';
774 T = CT->getElementType();
775 continue;
776 }
777 if (const TagType *TT = T->getAs<TagType>()) {
778 Out << '$';
779 VisitTagDecl(TT->getDecl());
780 return;
781 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000782 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
783 Out << '$';
784 VisitObjCInterfaceDecl(OIT->getDecl());
785 return;
786 }
787 if (const ObjCObjectType *OIT = T->getAs<ObjCObjectType>()) {
788 Out << 'Q';
789 VisitType(OIT->getBaseType());
790 for (auto *Prot : OIT->getProtocols())
791 VisitObjCProtocolDecl(Prot);
792 return;
793 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000794 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
795 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
796 return;
797 }
798 if (const TemplateSpecializationType *Spec
799 = T->getAs<TemplateSpecializationType>()) {
800 Out << '>';
801 VisitTemplateName(Spec->getTemplateName());
802 Out << Spec->getNumArgs();
803 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
804 VisitTemplateArgument(Spec->getArg(I));
805 return;
806 }
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000807 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
808 Out << '^';
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000809 printQualifier(Out, Ctx, DNT->getQualifier());
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000810 Out << ':' << DNT->getIdentifier()->getName();
811 return;
812 }
Argyrios Kyrtzidis1d5e5422014-12-08 08:48:43 +0000813 if (const InjectedClassNameType *InjT = T->getAs<InjectedClassNameType>()) {
814 T = InjT->getInjectedSpecializationType();
815 continue;
816 }
Alex Lorenz45c423b2017-04-28 09:46:36 +0000817 if (const auto *VT = T->getAs<VectorType>()) {
818 Out << (T->isExtVectorType() ? ']' : '[');
819 Out << VT->getNumElements();
820 T = VT->getElementType();
821 continue;
822 }
Jan Korous663ba152017-10-09 19:51:33 +0000823 if (const auto *const AT = dyn_cast<ArrayType>(T)) {
824 Out << '{';
825 switch (AT->getSizeModifier()) {
826 case ArrayType::Static:
827 Out << 's';
828 break;
829 case ArrayType::Star:
830 Out << '*';
831 break;
832 case ArrayType::Normal:
833 Out << 'n';
834 break;
835 }
836 if (const auto *const CAT = dyn_cast<ConstantArrayType>(T))
837 Out << CAT->getSize();
838
839 T = AT->getElementType();
840 continue;
841 }
Alex Lorenz45c423b2017-04-28 09:46:36 +0000842
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000843 // Unhandled type.
844 Out << ' ';
845 break;
846 } while (true);
847}
848
849void USRGenerator::VisitTemplateParameterList(
850 const TemplateParameterList *Params) {
851 if (!Params)
852 return;
853 Out << '>' << Params->size();
854 for (TemplateParameterList::const_iterator P = Params->begin(),
855 PEnd = Params->end();
856 P != PEnd; ++P) {
857 Out << '#';
858 if (isa<TemplateTypeParmDecl>(*P)) {
859 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
860 Out<< 'p';
861 Out << 'T';
862 continue;
863 }
864
865 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
866 if (NTTP->isParameterPack())
867 Out << 'p';
868 Out << 'N';
869 VisitType(NTTP->getType());
870 continue;
871 }
872
873 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
874 if (TTP->isParameterPack())
875 Out << 'p';
876 Out << 't';
877 VisitTemplateParameterList(TTP->getTemplateParameters());
878 }
879}
880
881void USRGenerator::VisitTemplateName(TemplateName Name) {
882 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
883 if (TemplateTemplateParmDecl *TTP
884 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
885 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
886 return;
887 }
888
889 Visit(Template);
890 return;
891 }
892
893 // FIXME: Visit dependent template names.
894}
895
896void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
897 switch (Arg.getKind()) {
898 case TemplateArgument::Null:
899 break;
900
901 case TemplateArgument::Declaration:
902 Visit(Arg.getAsDecl());
903 break;
904
905 case TemplateArgument::NullPtr:
906 break;
907
908 case TemplateArgument::TemplateExpansion:
909 Out << 'P'; // pack expansion of...
910 // Fall through
911 case TemplateArgument::Template:
912 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
913 break;
914
915 case TemplateArgument::Expression:
916 // FIXME: Visit expressions.
917 break;
918
919 case TemplateArgument::Pack:
920 Out << 'p' << Arg.pack_size();
Aaron Ballman2a89e852014-07-15 21:32:31 +0000921 for (const auto &P : Arg.pack_elements())
922 VisitTemplateArgument(P);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000923 break;
924
925 case TemplateArgument::Type:
926 VisitType(Arg.getAsType());
927 break;
928
929 case TemplateArgument::Integral:
930 Out << 'V';
931 VisitType(Arg.getIntegralType());
932 Out << Arg.getAsIntegral();
933 break;
934 }
935}
936
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000937void USRGenerator::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
938 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
939 return;
940 VisitDeclContext(D->getDeclContext());
941 Out << "@UUV@";
942 printQualifier(Out, D->getASTContext(), D->getQualifier());
943 EmitDeclName(D);
944}
945
946void USRGenerator::VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) {
947 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
948 return;
949 VisitDeclContext(D->getDeclContext());
950 Out << "@UUT@";
951 printQualifier(Out, D->getASTContext(), D->getQualifier());
952 Out << D->getName(); // Simple name.
953}
954
955
956
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000957//===----------------------------------------------------------------------===//
958// USR generation functions.
959//===----------------------------------------------------------------------===//
960
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000961static void combineClassAndCategoryExtContainers(StringRef ClsSymDefinedIn,
962 StringRef CatSymDefinedIn,
963 raw_ostream &OS) {
964 if (ClsSymDefinedIn.empty() && CatSymDefinedIn.empty())
965 return;
966 if (CatSymDefinedIn.empty()) {
967 OS << "@M@" << ClsSymDefinedIn << '@';
968 return;
969 }
970 OS << "@CM@" << CatSymDefinedIn << '@';
971 if (ClsSymDefinedIn != CatSymDefinedIn) {
972 OS << ClsSymDefinedIn << '@';
973 }
974}
975
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000976void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS,
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000977 StringRef ExtSymDefinedIn,
978 StringRef CategoryContextExtSymbolDefinedIn) {
979 combineClassAndCategoryExtContainers(ExtSymDefinedIn,
980 CategoryContextExtSymbolDefinedIn, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000981 OS << "objc(cs)" << Cls;
982}
983
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000984void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat,
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000985 raw_ostream &OS,
986 StringRef ClsSymDefinedIn,
987 StringRef CatSymDefinedIn) {
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000988 combineClassAndCategoryExtContainers(ClsSymDefinedIn, CatSymDefinedIn, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000989 OS << "objc(cy)" << Cls << '@' << Cat;
990}
991
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000992void clang::index::generateUSRForObjCIvar(StringRef Ivar, raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000993 OS << '@' << Ivar;
994}
995
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000996void clang::index::generateUSRForObjCMethod(StringRef Sel,
997 bool IsInstanceMethod,
998 raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000999 OS << (IsInstanceMethod ? "(im)" : "(cm)") << Sel;
1000}
1001
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +00001002void clang::index::generateUSRForObjCProperty(StringRef Prop, bool isClassProp,
1003 raw_ostream &OS) {
1004 OS << (isClassProp ? "(cpy)" : "(py)") << Prop;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001005}
1006
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001007void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS,
1008 StringRef ExtSymDefinedIn) {
1009 if (!ExtSymDefinedIn.empty())
1010 OS << "@M@" << ExtSymDefinedIn << '@';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001011 OS << "objc(pl)" << Prot;
1012}
1013
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001014void clang::index::generateUSRForGlobalEnum(StringRef EnumName, raw_ostream &OS,
1015 StringRef ExtSymDefinedIn) {
1016 if (!ExtSymDefinedIn.empty())
1017 OS << "@M@" << ExtSymDefinedIn;
1018 OS << "@E@" << EnumName;
1019}
1020
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +00001021void clang::index::generateUSRForEnumConstant(StringRef EnumConstantName,
1022 raw_ostream &OS) {
1023 OS << '@' << EnumConstantName;
1024}
1025
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001026bool clang::index::generateUSRForDecl(const Decl *D,
1027 SmallVectorImpl<char> &Buf) {
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +00001028 if (!D)
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001029 return true;
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +00001030 // We don't ignore decls with invalid source locations. Implicit decls, like
1031 // C++'s operator new function, can have invalid locations but it is fine to
1032 // create USRs that can identify them.
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001033
1034 USRGenerator UG(&D->getASTContext(), Buf);
1035 UG.Visit(D);
1036 return UG.ignoreResults();
1037}
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001038
Richard Smith66a81862015-05-04 02:25:31 +00001039bool clang::index::generateUSRForMacro(const MacroDefinitionRecord *MD,
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001040 const SourceManager &SM,
1041 SmallVectorImpl<char> &Buf) {
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001042 if (!MD)
1043 return true;
1044 return generateUSRForMacro(MD->getName()->getName(), MD->getLocation(),
1045 SM, Buf);
1046
1047}
1048
1049bool clang::index::generateUSRForMacro(StringRef MacroName, SourceLocation Loc,
1050 const SourceManager &SM,
1051 SmallVectorImpl<char> &Buf) {
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001052 // Don't generate USRs for things with invalid locations.
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001053 if (MacroName.empty() || Loc.isInvalid())
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001054 return true;
1055
1056 llvm::raw_svector_ostream Out(Buf);
1057
1058 // Assume that system headers are sane. Don't put source location
1059 // information into the USR if the macro comes from a system header.
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001060 bool ShouldGenerateLocation = !SM.isInSystemHeader(Loc);
1061
1062 Out << getUSRSpacePrefix();
1063 if (ShouldGenerateLocation)
1064 printLoc(Out, Loc, SM, /*IncludeOffset=*/true);
1065 Out << "@macro@";
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001066 Out << MacroName;
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001067 return false;
1068}