blob: c373eda98d46a55807fb08143e30f5c5be42a2f4 [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();
52 if (auto *attr = D->getAttr<ExternalSourceSymbolAttr>()) {
53 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);
91 void VisitObjCContainerDecl(const ObjCContainerDecl *CD);
92 void VisitObjCMethodDecl(const ObjCMethodDecl *MD);
93 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
94 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
95 void VisitTagDecl(const TagDecl *D);
96 void VisitTypedefDecl(const TypedefDecl *D);
97 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
98 void VisitVarDecl(const VarDecl *D);
99 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
100 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000101
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000102 void VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
103 IgnoreResults = true;
104 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000105
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000106 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
107 IgnoreResults = true;
108 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000109
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000110 void VisitUsingDecl(const UsingDecl *D) {
111 IgnoreResults = true;
112 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000113
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000114 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
115 IgnoreResults = true;
116 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000117
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000118 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) {
119 IgnoreResults = true;
120 }
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000121
122 bool ShouldGenerateLocation(const NamedDecl *D);
123
124 bool isLocal(const NamedDecl *D) {
Craig Topper236bde32014-05-26 06:21:51 +0000125 return D->getParentFunctionOrMethod() != nullptr;
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000126 }
127
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000128 void GenExtSymbolContainer(const NamedDecl *D);
129
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000130 /// Generate the string component containing the location of the
131 /// declaration.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000132 bool GenLoc(const Decl *D, bool IncludeOffset);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000133
134 /// String generation methods used both by the visitation methods
135 /// and from other clients that want to directly generate USRs. These
136 /// methods do not construct complete USRs (which incorporate the parents
137 /// of an AST element), but only the fragments concerning the AST element
138 /// itself.
139
140 /// Generate a USR for an Objective-C class.
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000141 void GenObjCClass(StringRef cls, StringRef ext) {
142 generateUSRForObjCClass(cls, Out, ext);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000143 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000144
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000145 /// Generate a USR for an Objective-C class category.
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000146 void GenObjCCategory(StringRef cls, StringRef cat,
147 StringRef clsExt, StringRef catExt) {
148 generateUSRForObjCCategory(cls, cat, Out, clsExt, catExt);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000149 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000150
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000151 /// Generate a USR fragment for an Objective-C property.
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000152 void GenObjCProperty(StringRef prop, bool isClassProp) {
153 generateUSRForObjCProperty(prop, isClassProp, Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000154 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000155
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000156 /// Generate a USR for an Objective-C protocol.
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000157 void GenObjCProtocol(StringRef prot, StringRef ext) {
158 generateUSRForObjCProtocol(prot, Out, ext);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000159 }
160
161 void VisitType(QualType T);
162 void VisitTemplateParameterList(const TemplateParameterList *Params);
163 void VisitTemplateName(TemplateName Name);
164 void VisitTemplateArgument(const TemplateArgument &Arg);
165
166 /// Emit a Decl's name using NamedDecl::printName() and return true if
167 /// the decl had no name.
168 bool EmitDeclName(const NamedDecl *D);
169};
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000170} // end anonymous namespace
171
172//===----------------------------------------------------------------------===//
173// Generating USRs from ASTS.
174//===----------------------------------------------------------------------===//
175
176bool USRGenerator::EmitDeclName(const NamedDecl *D) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000177 const unsigned startSize = Buf.size();
178 D->printName(Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000179 const unsigned endSize = Buf.size();
180 return startSize == endSize;
181}
182
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000183bool USRGenerator::ShouldGenerateLocation(const NamedDecl *D) {
184 if (D->isExternallyVisible())
185 return false;
186 if (D->getParentFunctionOrMethod())
187 return true;
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000188 SourceLocation Loc = D->getLocation();
189 if (Loc.isInvalid())
190 return false;
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000191 const SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000192 return !SM.isInSystemHeader(Loc);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000193}
194
195void USRGenerator::VisitDeclContext(const DeclContext *DC) {
196 if (const NamedDecl *D = dyn_cast<NamedDecl>(DC))
197 Visit(D);
198}
199
200void USRGenerator::VisitFieldDecl(const FieldDecl *D) {
201 // The USR for an ivar declared in a class extension is based on the
202 // ObjCInterfaceDecl, not the ObjCCategoryDecl.
203 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
204 Visit(ID);
205 else
206 VisitDeclContext(D->getDeclContext());
207 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
208 if (EmitDeclName(D)) {
209 // Bit fields can be anonymous.
210 IgnoreResults = true;
211 return;
212 }
213}
214
215void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000216 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000217 return;
218
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000219 const unsigned StartSize = Buf.size();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000220 VisitDeclContext(D->getDeclContext());
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000221 if (Buf.size() == StartSize)
222 GenExtSymbolContainer(D);
223
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000224 bool IsTemplate = false;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000225 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000226 IsTemplate = true;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000227 Out << "@FT@";
228 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
229 } else
230 Out << "@F@";
Argyrios Kyrtzidisd5719082016-02-15 01:32:36 +0000231
232 PrintingPolicy Policy(Context->getLangOpts());
233 // Forward references can have different template argument names. Suppress the
234 // template argument names in constructors to make their USR more stable.
235 Policy.SuppressTemplateArgsInCXXConstructors = true;
236 D->getDeclName().print(Out, Policy);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000237
238 ASTContext &Ctx = *Context;
Argyrios Kyrtzidis2682f9e2016-03-04 07:17:48 +0000239 if ((!Ctx.getLangOpts().CPlusPlus || D->isExternC()) &&
240 !D->hasAttr<OverloadableAttr>())
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000241 return;
242
243 if (const TemplateArgumentList *
244 SpecArgs = D->getTemplateSpecializationArgs()) {
245 Out << '<';
246 for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) {
247 Out << '#';
248 VisitTemplateArgument(SpecArgs->get(I));
249 }
250 Out << '>';
251 }
252
253 // Mangle in type information for the arguments.
David Majnemer59f77922016-06-24 04:05:48 +0000254 for (auto PD : D->parameters()) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000255 Out << '#';
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +0000256 VisitType(PD->getType());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000257 }
258 if (D->isVariadic())
259 Out << '.';
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000260 if (IsTemplate) {
261 // Function templates can be overloaded by return type, for example:
262 // \code
263 // template <class T> typename T::A foo() {}
264 // template <class T> typename T::B foo() {}
265 // \endcode
266 Out << '#';
267 VisitType(D->getReturnType());
268 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000269 Out << '#';
270 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
271 if (MD->isStatic())
272 Out << 'S';
273 if (unsigned quals = MD->getTypeQualifiers())
274 Out << (char)('0' + quals);
Argyrios Kyrtzidisf5819092014-12-08 08:48:21 +0000275 switch (MD->getRefQualifier()) {
276 case RQ_None: break;
277 case RQ_LValue: Out << '&'; break;
278 case RQ_RValue: Out << "&&"; break;
279 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000280 }
281}
282
283void USRGenerator::VisitNamedDecl(const NamedDecl *D) {
284 VisitDeclContext(D->getDeclContext());
285 Out << "@";
286
287 if (EmitDeclName(D)) {
288 // The string can be empty if the declaration has no name; e.g., it is
289 // the ParmDecl with no name for declaration of a function pointer type,
290 // e.g.: void (*f)(void *);
291 // In this case, don't generate a USR.
292 IgnoreResults = true;
293 }
294}
295
296void USRGenerator::VisitVarDecl(const VarDecl *D) {
297 // VarDecls can be declared 'extern' within a function or method body,
298 // but their enclosing DeclContext is the function, not the TU. We need
299 // to check the storage class to correctly generate the USR.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000300 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000301 return;
302
303 VisitDeclContext(D->getDeclContext());
304
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000305 if (VarTemplateDecl *VarTmpl = D->getDescribedVarTemplate()) {
306 Out << "@VT";
307 VisitTemplateParameterList(VarTmpl->getTemplateParameters());
308 } else if (const VarTemplatePartialSpecializationDecl *PartialSpec
309 = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
310 Out << "@VP";
311 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
312 }
313
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000314 // Variables always have simple names.
315 StringRef s = D->getName();
316
317 // The string can be empty if the declaration has no name; e.g., it is
318 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
319 // void (*f)(void *);
320 // In this case, don't generate a USR.
321 if (s.empty())
322 IgnoreResults = true;
323 else
324 Out << '@' << s;
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000325
326 // For a template specialization, mangle the template arguments.
327 if (const VarTemplateSpecializationDecl *Spec
328 = dyn_cast<VarTemplateSpecializationDecl>(D)) {
Argyrios Kyrtzidis7d90ed02017-02-15 16:16:27 +0000329 const TemplateArgumentList &Args = Spec->getTemplateArgs();
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000330 Out << '>';
331 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
332 Out << '#';
333 VisitTemplateArgument(Args.get(I));
334 }
335 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000336}
337
338void USRGenerator::VisitNonTypeTemplateParmDecl(
339 const NonTypeTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000340 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000341}
342
343void USRGenerator::VisitTemplateTemplateParmDecl(
344 const TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000345 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000346}
347
348void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) {
349 if (D->isAnonymousNamespace()) {
350 Out << "@aN";
351 return;
352 }
353
354 VisitDeclContext(D->getDeclContext());
355 if (!IgnoreResults)
356 Out << "@N@" << D->getName();
357}
358
359void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
360 VisitFunctionDecl(D->getTemplatedDecl());
361}
362
363void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
364 VisitTagDecl(D->getTemplatedDecl());
365}
366
367void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
368 VisitDeclContext(D->getDeclContext());
369 if (!IgnoreResults)
370 Out << "@NA@" << D->getName();
371}
372
373void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
374 const DeclContext *container = D->getDeclContext();
375 if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) {
376 Visit(pd);
377 }
378 else {
379 // The USR for a method declared in a class extension or category is based on
380 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
381 const ObjCInterfaceDecl *ID = D->getClassInterface();
382 if (!ID) {
383 IgnoreResults = true;
384 return;
385 }
386 Visit(ID);
387 }
388 // Ideally we would use 'GenObjCMethod', but this is such a hot path
389 // for Objective-C code that we don't want to use
390 // DeclarationName::getAsString().
391 Out << (D->isInstanceMethod() ? "(im)" : "(cm)")
392 << DeclarationName(D->getSelector());
393}
394
395void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D) {
396 switch (D->getKind()) {
397 default:
398 llvm_unreachable("Invalid ObjC container.");
399 case Decl::ObjCInterface:
400 case Decl::ObjCImplementation:
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000401 GenObjCClass(D->getName(), GetExternalSourceContainer(D));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000402 break;
403 case Decl::ObjCCategory: {
404 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
405 const ObjCInterfaceDecl *ID = CD->getClassInterface();
406 if (!ID) {
407 // Handle invalid code where the @interface might not
408 // have been specified.
409 // FIXME: We should be able to generate this USR even if the
410 // @interface isn't available.
411 IgnoreResults = true;
412 return;
413 }
414 // Specially handle class extensions, which are anonymous categories.
415 // We want to mangle in the location to uniquely distinguish them.
416 if (CD->IsClassExtension()) {
417 Out << "objc(ext)" << ID->getName() << '@';
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000418 GenLoc(CD, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000419 }
420 else
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000421 GenObjCCategory(ID->getName(), CD->getName(),
422 GetExternalSourceContainer(ID),
423 GetExternalSourceContainer(CD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000424
425 break;
426 }
427 case Decl::ObjCCategoryImpl: {
428 const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
429 const ObjCInterfaceDecl *ID = CD->getClassInterface();
430 if (!ID) {
431 // Handle invalid code where the @interface might not
432 // have been specified.
433 // FIXME: We should be able to generate this USR even if the
434 // @interface isn't available.
435 IgnoreResults = true;
436 return;
437 }
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000438 GenObjCCategory(ID->getName(), CD->getName(),
439 GetExternalSourceContainer(ID),
440 GetExternalSourceContainer(CD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000441 break;
442 }
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000443 case Decl::ObjCProtocol: {
444 const ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D);
445 GenObjCProtocol(PD->getName(), GetExternalSourceContainer(PD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000446 break;
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000447 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000448 }
449}
450
451void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
452 // The USR for a property declared in a class extension or category is based
453 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl.
454 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
455 Visit(ID);
456 else
457 Visit(cast<Decl>(D->getDeclContext()));
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000458 GenObjCProperty(D->getName(), D->isClassProperty());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000459}
460
461void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
462 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
463 VisitObjCPropertyDecl(PD);
464 return;
465 }
466
467 IgnoreResults = true;
468}
469
470void USRGenerator::VisitTagDecl(const TagDecl *D) {
471 // Add the location of the tag decl to handle resolution across
472 // translation units.
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000473 if (!isa<EnumDecl>(D) &&
474 ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000475 return;
476
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000477 GenExtSymbolContainer(D);
478
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000479 D = D->getCanonicalDecl();
480 VisitDeclContext(D->getDeclContext());
481
482 bool AlreadyStarted = false;
483 if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
484 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
485 AlreadyStarted = true;
486
487 switch (D->getTagKind()) {
488 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000489 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000490 case TTK_Struct: Out << "@ST"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000491 case TTK_Union: Out << "@UT"; break;
492 case TTK_Enum: llvm_unreachable("enum template");
493 }
494 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
495 } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec
496 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
497 AlreadyStarted = true;
498
499 switch (D->getTagKind()) {
500 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000501 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000502 case TTK_Struct: Out << "@SP"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000503 case TTK_Union: Out << "@UP"; break;
504 case TTK_Enum: llvm_unreachable("enum partial specialization");
505 }
506 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
507 }
508 }
509
510 if (!AlreadyStarted) {
511 switch (D->getTagKind()) {
512 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000513 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000514 case TTK_Struct: Out << "@S"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000515 case TTK_Union: Out << "@U"; break;
516 case TTK_Enum: Out << "@E"; break;
517 }
518 }
519
520 Out << '@';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000521 assert(Buf.size() > 0);
522 const unsigned off = Buf.size() - 1;
523
524 if (EmitDeclName(D)) {
525 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) {
526 Buf[off] = 'A';
527 Out << '@' << *TD;
528 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000529 else {
530 if (D->isEmbeddedInDeclarator() && !D->isFreeStanding()) {
531 printLoc(Out, D->getLocation(), Context->getSourceManager(), true);
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000532 } else {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000533 Buf[off] = 'a';
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000534 if (auto *ED = dyn_cast<EnumDecl>(D)) {
535 // Distinguish USRs of anonymous enums by using their first enumerator.
536 auto enum_range = ED->enumerators();
537 if (enum_range.begin() != enum_range.end()) {
538 Out << '@' << **enum_range.begin();
539 }
540 }
541 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000542 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000543 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000544
545 // For a class template specialization, mangle the template arguments.
546 if (const ClassTemplateSpecializationDecl *Spec
547 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
Argyrios Kyrtzidis7d90ed02017-02-15 16:16:27 +0000548 const TemplateArgumentList &Args = Spec->getTemplateArgs();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000549 Out << '>';
550 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
551 Out << '#';
552 VisitTemplateArgument(Args.get(I));
553 }
554 }
555}
556
557void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000558 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000559 return;
560 const DeclContext *DC = D->getDeclContext();
561 if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
562 Visit(DCN);
563 Out << "@T@";
564 Out << D->getName();
565}
566
567void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000568 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000569}
570
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000571void USRGenerator::GenExtSymbolContainer(const NamedDecl *D) {
572 StringRef Container = GetExternalSourceContainer(D);
573 if (!Container.empty())
574 Out << "@M@" << Container;
575}
576
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000577bool USRGenerator::GenLoc(const Decl *D, bool IncludeOffset) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000578 if (generatedLoc)
579 return IgnoreResults;
580 generatedLoc = true;
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000581
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000582 // Guard against null declarations in invalid code.
583 if (!D) {
584 IgnoreResults = true;
585 return true;
586 }
587
588 // Use the location of canonical decl.
589 D = D->getCanonicalDecl();
590
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000591 IgnoreResults =
592 IgnoreResults || printLoc(Out, D->getLocStart(),
593 Context->getSourceManager(), IncludeOffset);
594
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000595 return IgnoreResults;
596}
597
598void USRGenerator::VisitType(QualType T) {
599 // This method mangles in USR information for types. It can possibly
600 // just reuse the naming-mangling logic used by codegen, although the
601 // requirements for USRs might not be the same.
602 ASTContext &Ctx = *Context;
603
604 do {
605 T = Ctx.getCanonicalType(T);
606 Qualifiers Q = T.getQualifiers();
607 unsigned qVal = 0;
608 if (Q.hasConst())
609 qVal |= 0x1;
610 if (Q.hasVolatile())
611 qVal |= 0x2;
612 if (Q.hasRestrict())
613 qVal |= 0x4;
614 if(qVal)
615 Out << ((char) ('0' + qVal));
616
617 // Mangle in ObjC GC qualifiers?
618
619 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) {
620 Out << 'P';
621 T = Expansion->getPattern();
622 }
623
624 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
625 unsigned char c = '\0';
626 switch (BT->getKind()) {
627 case BuiltinType::Void:
628 c = 'v'; break;
629 case BuiltinType::Bool:
630 c = 'b'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000631 case BuiltinType::UChar:
632 c = 'c'; break;
633 case BuiltinType::Char16:
634 c = 'q'; break;
635 case BuiltinType::Char32:
636 c = 'w'; break;
637 case BuiltinType::UShort:
638 c = 's'; break;
639 case BuiltinType::UInt:
640 c = 'i'; break;
641 case BuiltinType::ULong:
642 c = 'l'; break;
643 case BuiltinType::ULongLong:
644 c = 'k'; break;
645 case BuiltinType::UInt128:
646 c = 'j'; break;
Argyrios Kyrtzidis56af7e62014-12-08 09:09:05 +0000647 case BuiltinType::Char_U:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000648 case BuiltinType::Char_S:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000649 c = 'C'; break;
Argyrios Kyrtzidisca044542014-12-08 08:48:17 +0000650 case BuiltinType::SChar:
651 c = 'r'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000652 case BuiltinType::WChar_S:
653 case BuiltinType::WChar_U:
654 c = 'W'; break;
655 case BuiltinType::Short:
656 c = 'S'; break;
657 case BuiltinType::Int:
658 c = 'I'; break;
659 case BuiltinType::Long:
660 c = 'L'; break;
661 case BuiltinType::LongLong:
662 c = 'K'; break;
663 case BuiltinType::Int128:
664 c = 'J'; break;
665 case BuiltinType::Half:
666 c = 'h'; break;
667 case BuiltinType::Float:
668 c = 'f'; break;
669 case BuiltinType::Double:
670 c = 'd'; break;
671 case BuiltinType::LongDouble:
672 c = 'D'; break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000673 case BuiltinType::Float128:
674 c = 'Q'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000675 case BuiltinType::NullPtr:
676 c = 'n'; break;
677#define BUILTIN_TYPE(Id, SingletonId)
678#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
679#include "clang/AST/BuiltinTypes.def"
680 case BuiltinType::Dependent:
Alexey Bader954ba212016-04-08 13:40:33 +0000681#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
682 case BuiltinType::Id:
Alexey Baderb62f1442016-04-13 08:33:41 +0000683#include "clang/Basic/OpenCLImageTypes.def"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000684 case BuiltinType::OCLEvent:
Alexey Bader9c8453f2015-09-15 11:18:52 +0000685 case BuiltinType::OCLClkEvent:
686 case BuiltinType::OCLQueue:
Alexey Bader9c8453f2015-09-15 11:18:52 +0000687 case BuiltinType::OCLReserveID:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000688 case BuiltinType::OCLSampler:
689 IgnoreResults = true;
690 return;
691 case BuiltinType::ObjCId:
692 c = 'o'; break;
693 case BuiltinType::ObjCClass:
694 c = 'O'; break;
695 case BuiltinType::ObjCSel:
696 c = 'e'; break;
697 }
698 Out << c;
699 return;
700 }
701
702 // If we have already seen this (non-built-in) type, use a substitution
703 // encoding.
704 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
705 = TypeSubstitutions.find(T.getTypePtr());
706 if (Substitution != TypeSubstitutions.end()) {
707 Out << 'S' << Substitution->second << '_';
708 return;
709 } else {
710 // Record this as a substitution.
711 unsigned Number = TypeSubstitutions.size();
712 TypeSubstitutions[T.getTypePtr()] = Number;
713 }
714
715 if (const PointerType *PT = T->getAs<PointerType>()) {
716 Out << '*';
717 T = PT->getPointeeType();
718 continue;
719 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000720 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
721 Out << '*';
722 T = OPT->getPointeeType();
723 continue;
724 }
Argyrios Kyrtzidis0e387dc2014-12-08 08:48:27 +0000725 if (const RValueReferenceType *RT = T->getAs<RValueReferenceType>()) {
726 Out << "&&";
727 T = RT->getPointeeType();
728 continue;
729 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000730 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
731 Out << '&';
732 T = RT->getPointeeType();
733 continue;
734 }
735 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
736 Out << 'F';
Alp Toker314cc812014-01-25 16:55:45 +0000737 VisitType(FT->getReturnType());
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000738 for (const auto &I : FT->param_types())
739 VisitType(I);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000740 if (FT->isVariadic())
741 Out << '.';
742 return;
743 }
744 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
745 Out << 'B';
746 T = BT->getPointeeType();
747 continue;
748 }
749 if (const ComplexType *CT = T->getAs<ComplexType>()) {
750 Out << '<';
751 T = CT->getElementType();
752 continue;
753 }
754 if (const TagType *TT = T->getAs<TagType>()) {
755 Out << '$';
756 VisitTagDecl(TT->getDecl());
757 return;
758 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000759 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
760 Out << '$';
761 VisitObjCInterfaceDecl(OIT->getDecl());
762 return;
763 }
764 if (const ObjCObjectType *OIT = T->getAs<ObjCObjectType>()) {
765 Out << 'Q';
766 VisitType(OIT->getBaseType());
767 for (auto *Prot : OIT->getProtocols())
768 VisitObjCProtocolDecl(Prot);
769 return;
770 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000771 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
772 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
773 return;
774 }
775 if (const TemplateSpecializationType *Spec
776 = T->getAs<TemplateSpecializationType>()) {
777 Out << '>';
778 VisitTemplateName(Spec->getTemplateName());
779 Out << Spec->getNumArgs();
780 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
781 VisitTemplateArgument(Spec->getArg(I));
782 return;
783 }
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000784 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
785 Out << '^';
786 // FIXME: Encode the qualifier, don't just print it.
787 PrintingPolicy PO(Ctx.getLangOpts());
788 PO.SuppressTagKeyword = true;
789 PO.SuppressUnwrittenScope = true;
790 PO.ConstantArraySizeAsWritten = false;
791 PO.AnonymousTagLocations = false;
792 DNT->getQualifier()->print(Out, PO);
793 Out << ':' << DNT->getIdentifier()->getName();
794 return;
795 }
Argyrios Kyrtzidis1d5e5422014-12-08 08:48:43 +0000796 if (const InjectedClassNameType *InjT = T->getAs<InjectedClassNameType>()) {
797 T = InjT->getInjectedSpecializationType();
798 continue;
799 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000800
801 // Unhandled type.
802 Out << ' ';
803 break;
804 } while (true);
805}
806
807void USRGenerator::VisitTemplateParameterList(
808 const TemplateParameterList *Params) {
809 if (!Params)
810 return;
811 Out << '>' << Params->size();
812 for (TemplateParameterList::const_iterator P = Params->begin(),
813 PEnd = Params->end();
814 P != PEnd; ++P) {
815 Out << '#';
816 if (isa<TemplateTypeParmDecl>(*P)) {
817 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
818 Out<< 'p';
819 Out << 'T';
820 continue;
821 }
822
823 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
824 if (NTTP->isParameterPack())
825 Out << 'p';
826 Out << 'N';
827 VisitType(NTTP->getType());
828 continue;
829 }
830
831 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
832 if (TTP->isParameterPack())
833 Out << 'p';
834 Out << 't';
835 VisitTemplateParameterList(TTP->getTemplateParameters());
836 }
837}
838
839void USRGenerator::VisitTemplateName(TemplateName Name) {
840 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
841 if (TemplateTemplateParmDecl *TTP
842 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
843 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
844 return;
845 }
846
847 Visit(Template);
848 return;
849 }
850
851 // FIXME: Visit dependent template names.
852}
853
854void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
855 switch (Arg.getKind()) {
856 case TemplateArgument::Null:
857 break;
858
859 case TemplateArgument::Declaration:
860 Visit(Arg.getAsDecl());
861 break;
862
863 case TemplateArgument::NullPtr:
864 break;
865
866 case TemplateArgument::TemplateExpansion:
867 Out << 'P'; // pack expansion of...
868 // Fall through
869 case TemplateArgument::Template:
870 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
871 break;
872
873 case TemplateArgument::Expression:
874 // FIXME: Visit expressions.
875 break;
876
877 case TemplateArgument::Pack:
878 Out << 'p' << Arg.pack_size();
Aaron Ballman2a89e852014-07-15 21:32:31 +0000879 for (const auto &P : Arg.pack_elements())
880 VisitTemplateArgument(P);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000881 break;
882
883 case TemplateArgument::Type:
884 VisitType(Arg.getAsType());
885 break;
886
887 case TemplateArgument::Integral:
888 Out << 'V';
889 VisitType(Arg.getIntegralType());
890 Out << Arg.getAsIntegral();
891 break;
892 }
893}
894
895//===----------------------------------------------------------------------===//
896// USR generation functions.
897//===----------------------------------------------------------------------===//
898
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000899void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS,
900 StringRef ExtSymDefinedIn) {
901 if (!ExtSymDefinedIn.empty())
902 OS << "@M@" << ExtSymDefinedIn << '@';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000903 OS << "objc(cs)" << Cls;
904}
905
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000906void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat,
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000907 raw_ostream &OS,
908 StringRef ClsSymDefinedIn,
909 StringRef CatSymDefinedIn) {
910 if (!CatSymDefinedIn.empty() || !ClsSymDefinedIn.empty()) {
911 OS << "@M@";
912 if (!CatSymDefinedIn.empty() && !ClsSymDefinedIn.empty())
913 OS << CatSymDefinedIn << '-' << ClsSymDefinedIn;
914 else if (!CatSymDefinedIn.empty())
915 OS << CatSymDefinedIn;
916 else
917 OS << ClsSymDefinedIn;
918 OS << '@';
919 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000920 OS << "objc(cy)" << Cls << '@' << Cat;
921}
922
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000923void clang::index::generateUSRForObjCIvar(StringRef Ivar, raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000924 OS << '@' << Ivar;
925}
926
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000927void clang::index::generateUSRForObjCMethod(StringRef Sel,
928 bool IsInstanceMethod,
929 raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000930 OS << (IsInstanceMethod ? "(im)" : "(cm)") << Sel;
931}
932
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000933void clang::index::generateUSRForObjCProperty(StringRef Prop, bool isClassProp,
934 raw_ostream &OS) {
935 OS << (isClassProp ? "(cpy)" : "(py)") << Prop;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000936}
937
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000938void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS,
939 StringRef ExtSymDefinedIn) {
940 if (!ExtSymDefinedIn.empty())
941 OS << "@M@" << ExtSymDefinedIn << '@';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000942 OS << "objc(pl)" << Prot;
943}
944
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000945void clang::index::generateUSRForGlobalEnum(StringRef EnumName, raw_ostream &OS,
946 StringRef ExtSymDefinedIn) {
947 if (!ExtSymDefinedIn.empty())
948 OS << "@M@" << ExtSymDefinedIn;
949 OS << "@E@" << EnumName;
950}
951
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000952bool clang::index::generateUSRForDecl(const Decl *D,
953 SmallVectorImpl<char> &Buf) {
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000954 if (!D)
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000955 return true;
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000956 // We don't ignore decls with invalid source locations. Implicit decls, like
957 // C++'s operator new function, can have invalid locations but it is fine to
958 // create USRs that can identify them.
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000959
960 USRGenerator UG(&D->getASTContext(), Buf);
961 UG.Visit(D);
962 return UG.ignoreResults();
963}
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000964
Richard Smith66a81862015-05-04 02:25:31 +0000965bool clang::index::generateUSRForMacro(const MacroDefinitionRecord *MD,
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000966 const SourceManager &SM,
967 SmallVectorImpl<char> &Buf) {
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +0000968 if (!MD)
969 return true;
970 return generateUSRForMacro(MD->getName()->getName(), MD->getLocation(),
971 SM, Buf);
972
973}
974
975bool clang::index::generateUSRForMacro(StringRef MacroName, SourceLocation Loc,
976 const SourceManager &SM,
977 SmallVectorImpl<char> &Buf) {
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000978 // Don't generate USRs for things with invalid locations.
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +0000979 if (MacroName.empty() || Loc.isInvalid())
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000980 return true;
981
982 llvm::raw_svector_ostream Out(Buf);
983
984 // Assume that system headers are sane. Don't put source location
985 // information into the USR if the macro comes from a system header.
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000986 bool ShouldGenerateLocation = !SM.isInSystemHeader(Loc);
987
988 Out << getUSRSpacePrefix();
989 if (ShouldGenerateLocation)
990 printLoc(Out, Loc, SM, /*IncludeOffset=*/true);
991 Out << "@macro@";
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +0000992 Out << MacroName;
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000993 return false;
994}