blob: ed469f677a34e4cabd7329e06b709659f057065d [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);
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);
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000102
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000103 void VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
104 IgnoreResults = true;
105 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000106
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000107 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
108 IgnoreResults = true;
109 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000110
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000111 void VisitUsingDecl(const UsingDecl *D) {
112 IgnoreResults = true;
113 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000114
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000115 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
116 IgnoreResults = true;
117 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000118
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000119 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) {
120 IgnoreResults = true;
121 }
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000122
123 bool ShouldGenerateLocation(const NamedDecl *D);
124
125 bool isLocal(const NamedDecl *D) {
Craig Topper236bde32014-05-26 06:21:51 +0000126 return D->getParentFunctionOrMethod() != nullptr;
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000127 }
128
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000129 void GenExtSymbolContainer(const NamedDecl *D);
130
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000131 /// Generate the string component containing the location of the
132 /// declaration.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000133 bool GenLoc(const Decl *D, bool IncludeOffset);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000134
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.
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000142 void GenObjCClass(StringRef cls, StringRef ExtSymDefinedIn,
143 StringRef CategoryContextExtSymbolDefinedIn) {
144 generateUSRForObjCClass(cls, Out, ExtSymDefinedIn,
145 CategoryContextExtSymbolDefinedIn);
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 for an Objective-C class category.
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000149 void GenObjCCategory(StringRef cls, StringRef cat,
150 StringRef clsExt, StringRef catExt) {
151 generateUSRForObjCCategory(cls, cat, Out, clsExt, catExt);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000152 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000153
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000154 /// Generate a USR fragment for an Objective-C property.
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000155 void GenObjCProperty(StringRef prop, bool isClassProp) {
156 generateUSRForObjCProperty(prop, isClassProp, Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000157 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000158
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000159 /// Generate a USR for an Objective-C protocol.
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000160 void GenObjCProtocol(StringRef prot, StringRef ext) {
161 generateUSRForObjCProtocol(prot, Out, ext);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000162 }
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};
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000173} // end anonymous namespace
174
175//===----------------------------------------------------------------------===//
176// Generating USRs from ASTS.
177//===----------------------------------------------------------------------===//
178
179bool USRGenerator::EmitDeclName(const NamedDecl *D) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000180 const unsigned startSize = Buf.size();
181 D->printName(Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000182 const unsigned endSize = Buf.size();
183 return startSize == endSize;
184}
185
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000186bool USRGenerator::ShouldGenerateLocation(const NamedDecl *D) {
187 if (D->isExternallyVisible())
188 return false;
189 if (D->getParentFunctionOrMethod())
190 return true;
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000191 SourceLocation Loc = D->getLocation();
192 if (Loc.isInvalid())
193 return false;
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000194 const SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000195 return !SM.isInSystemHeader(Loc);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000196}
197
198void USRGenerator::VisitDeclContext(const DeclContext *DC) {
199 if (const NamedDecl *D = dyn_cast<NamedDecl>(DC))
200 Visit(D);
201}
202
203void USRGenerator::VisitFieldDecl(const FieldDecl *D) {
204 // The USR for an ivar declared in a class extension is based on the
205 // ObjCInterfaceDecl, not the ObjCCategoryDecl.
206 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
207 Visit(ID);
208 else
209 VisitDeclContext(D->getDeclContext());
210 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
211 if (EmitDeclName(D)) {
212 // Bit fields can be anonymous.
213 IgnoreResults = true;
214 return;
215 }
216}
217
218void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000219 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000220 return;
221
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000222 const unsigned StartSize = Buf.size();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000223 VisitDeclContext(D->getDeclContext());
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000224 if (Buf.size() == StartSize)
225 GenExtSymbolContainer(D);
226
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000227 bool IsTemplate = false;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000228 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000229 IsTemplate = true;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000230 Out << "@FT@";
231 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
232 } else
233 Out << "@F@";
Argyrios Kyrtzidisd5719082016-02-15 01:32:36 +0000234
235 PrintingPolicy Policy(Context->getLangOpts());
236 // Forward references can have different template argument names. Suppress the
237 // template argument names in constructors to make their USR more stable.
238 Policy.SuppressTemplateArgsInCXXConstructors = true;
239 D->getDeclName().print(Out, Policy);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000240
241 ASTContext &Ctx = *Context;
Argyrios Kyrtzidis2682f9e2016-03-04 07:17:48 +0000242 if ((!Ctx.getLangOpts().CPlusPlus || D->isExternC()) &&
243 !D->hasAttr<OverloadableAttr>())
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000244 return;
245
246 if (const TemplateArgumentList *
247 SpecArgs = D->getTemplateSpecializationArgs()) {
248 Out << '<';
249 for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) {
250 Out << '#';
251 VisitTemplateArgument(SpecArgs->get(I));
252 }
253 Out << '>';
254 }
255
256 // Mangle in type information for the arguments.
David Majnemer59f77922016-06-24 04:05:48 +0000257 for (auto PD : D->parameters()) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000258 Out << '#';
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +0000259 VisitType(PD->getType());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000260 }
261 if (D->isVariadic())
262 Out << '.';
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000263 if (IsTemplate) {
264 // Function templates can be overloaded by return type, for example:
265 // \code
266 // template <class T> typename T::A foo() {}
267 // template <class T> typename T::B foo() {}
268 // \endcode
269 Out << '#';
270 VisitType(D->getReturnType());
271 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000272 Out << '#';
273 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
274 if (MD->isStatic())
275 Out << 'S';
276 if (unsigned quals = MD->getTypeQualifiers())
277 Out << (char)('0' + quals);
Argyrios Kyrtzidisf5819092014-12-08 08:48:21 +0000278 switch (MD->getRefQualifier()) {
279 case RQ_None: break;
280 case RQ_LValue: Out << '&'; break;
281 case RQ_RValue: Out << "&&"; break;
282 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000283 }
284}
285
286void USRGenerator::VisitNamedDecl(const NamedDecl *D) {
287 VisitDeclContext(D->getDeclContext());
288 Out << "@";
289
290 if (EmitDeclName(D)) {
291 // The string can be empty if the declaration has no name; e.g., it is
292 // the ParmDecl with no name for declaration of a function pointer type,
293 // e.g.: void (*f)(void *);
294 // In this case, don't generate a USR.
295 IgnoreResults = true;
296 }
297}
298
299void USRGenerator::VisitVarDecl(const VarDecl *D) {
300 // VarDecls can be declared 'extern' within a function or method body,
301 // but their enclosing DeclContext is the function, not the TU. We need
302 // to check the storage class to correctly generate the USR.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000303 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000304 return;
305
306 VisitDeclContext(D->getDeclContext());
307
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000308 if (VarTemplateDecl *VarTmpl = D->getDescribedVarTemplate()) {
309 Out << "@VT";
310 VisitTemplateParameterList(VarTmpl->getTemplateParameters());
311 } else if (const VarTemplatePartialSpecializationDecl *PartialSpec
312 = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
313 Out << "@VP";
314 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
315 }
316
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000317 // Variables always have simple names.
318 StringRef s = D->getName();
319
320 // The string can be empty if the declaration has no name; e.g., it is
321 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
322 // void (*f)(void *);
323 // In this case, don't generate a USR.
324 if (s.empty())
325 IgnoreResults = true;
326 else
327 Out << '@' << s;
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000328
329 // For a template specialization, mangle the template arguments.
330 if (const VarTemplateSpecializationDecl *Spec
331 = dyn_cast<VarTemplateSpecializationDecl>(D)) {
Argyrios Kyrtzidis7d90ed02017-02-15 16:16:27 +0000332 const TemplateArgumentList &Args = Spec->getTemplateArgs();
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000333 Out << '>';
334 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
335 Out << '#';
336 VisitTemplateArgument(Args.get(I));
337 }
338 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000339}
340
341void USRGenerator::VisitNonTypeTemplateParmDecl(
342 const NonTypeTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000343 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000344}
345
346void USRGenerator::VisitTemplateTemplateParmDecl(
347 const TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000348 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000349}
350
351void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) {
352 if (D->isAnonymousNamespace()) {
353 Out << "@aN";
354 return;
355 }
356
357 VisitDeclContext(D->getDeclContext());
358 if (!IgnoreResults)
359 Out << "@N@" << D->getName();
360}
361
362void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
363 VisitFunctionDecl(D->getTemplatedDecl());
364}
365
366void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
367 VisitTagDecl(D->getTemplatedDecl());
368}
369
370void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
371 VisitDeclContext(D->getDeclContext());
372 if (!IgnoreResults)
373 Out << "@NA@" << D->getName();
374}
375
376void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
377 const DeclContext *container = D->getDeclContext();
378 if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) {
379 Visit(pd);
380 }
381 else {
382 // The USR for a method declared in a class extension or category is based on
383 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
384 const ObjCInterfaceDecl *ID = D->getClassInterface();
385 if (!ID) {
386 IgnoreResults = true;
387 return;
388 }
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000389 auto getCategoryContext = [](const ObjCMethodDecl *D) ->
390 const ObjCCategoryDecl * {
391 if (auto *CD = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
392 return CD;
393 if (auto *ICD = dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
394 return ICD->getCategoryDecl();
395 return nullptr;
396 };
397 auto *CD = getCategoryContext(D);
398 VisitObjCContainerDecl(ID, CD);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000399 }
400 // Ideally we would use 'GenObjCMethod', but this is such a hot path
401 // for Objective-C code that we don't want to use
402 // DeclarationName::getAsString().
403 Out << (D->isInstanceMethod() ? "(im)" : "(cm)")
404 << DeclarationName(D->getSelector());
405}
406
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000407void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D,
408 const ObjCCategoryDecl *CatD) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000409 switch (D->getKind()) {
410 default:
411 llvm_unreachable("Invalid ObjC container.");
412 case Decl::ObjCInterface:
413 case Decl::ObjCImplementation:
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000414 GenObjCClass(D->getName(), GetExternalSourceContainer(D),
415 GetExternalSourceContainer(CatD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000416 break;
417 case Decl::ObjCCategory: {
418 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
419 const ObjCInterfaceDecl *ID = CD->getClassInterface();
420 if (!ID) {
421 // Handle invalid code where the @interface might not
422 // have been specified.
423 // FIXME: We should be able to generate this USR even if the
424 // @interface isn't available.
425 IgnoreResults = true;
426 return;
427 }
428 // Specially handle class extensions, which are anonymous categories.
429 // We want to mangle in the location to uniquely distinguish them.
430 if (CD->IsClassExtension()) {
431 Out << "objc(ext)" << ID->getName() << '@';
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000432 GenLoc(CD, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000433 }
434 else
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000435 GenObjCCategory(ID->getName(), CD->getName(),
436 GetExternalSourceContainer(ID),
437 GetExternalSourceContainer(CD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000438
439 break;
440 }
441 case Decl::ObjCCategoryImpl: {
442 const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
443 const ObjCInterfaceDecl *ID = CD->getClassInterface();
444 if (!ID) {
445 // Handle invalid code where the @interface might not
446 // have been specified.
447 // FIXME: We should be able to generate this USR even if the
448 // @interface isn't available.
449 IgnoreResults = true;
450 return;
451 }
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000452 GenObjCCategory(ID->getName(), CD->getName(),
453 GetExternalSourceContainer(ID),
454 GetExternalSourceContainer(CD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000455 break;
456 }
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000457 case Decl::ObjCProtocol: {
458 const ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D);
459 GenObjCProtocol(PD->getName(), GetExternalSourceContainer(PD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000460 break;
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000461 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000462 }
463}
464
465void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
466 // The USR for a property declared in a class extension or category is based
467 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl.
468 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
469 Visit(ID);
470 else
471 Visit(cast<Decl>(D->getDeclContext()));
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000472 GenObjCProperty(D->getName(), D->isClassProperty());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000473}
474
475void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
476 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
477 VisitObjCPropertyDecl(PD);
478 return;
479 }
480
481 IgnoreResults = true;
482}
483
484void USRGenerator::VisitTagDecl(const TagDecl *D) {
485 // Add the location of the tag decl to handle resolution across
486 // translation units.
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000487 if (!isa<EnumDecl>(D) &&
488 ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000489 return;
490
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000491 GenExtSymbolContainer(D);
492
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000493 D = D->getCanonicalDecl();
494 VisitDeclContext(D->getDeclContext());
495
496 bool AlreadyStarted = false;
497 if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
498 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
499 AlreadyStarted = true;
500
501 switch (D->getTagKind()) {
502 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000503 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000504 case TTK_Struct: Out << "@ST"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000505 case TTK_Union: Out << "@UT"; break;
506 case TTK_Enum: llvm_unreachable("enum template");
507 }
508 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
509 } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec
510 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
511 AlreadyStarted = true;
512
513 switch (D->getTagKind()) {
514 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000515 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000516 case TTK_Struct: Out << "@SP"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000517 case TTK_Union: Out << "@UP"; break;
518 case TTK_Enum: llvm_unreachable("enum partial specialization");
519 }
520 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
521 }
522 }
523
524 if (!AlreadyStarted) {
525 switch (D->getTagKind()) {
526 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000527 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000528 case TTK_Struct: Out << "@S"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000529 case TTK_Union: Out << "@U"; break;
530 case TTK_Enum: Out << "@E"; break;
531 }
532 }
533
534 Out << '@';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000535 assert(Buf.size() > 0);
536 const unsigned off = Buf.size() - 1;
537
538 if (EmitDeclName(D)) {
539 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) {
540 Buf[off] = 'A';
541 Out << '@' << *TD;
542 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000543 else {
544 if (D->isEmbeddedInDeclarator() && !D->isFreeStanding()) {
545 printLoc(Out, D->getLocation(), Context->getSourceManager(), true);
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000546 } else {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000547 Buf[off] = 'a';
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000548 if (auto *ED = dyn_cast<EnumDecl>(D)) {
549 // Distinguish USRs of anonymous enums by using their first enumerator.
550 auto enum_range = ED->enumerators();
551 if (enum_range.begin() != enum_range.end()) {
552 Out << '@' << **enum_range.begin();
553 }
554 }
555 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000556 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000557 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000558
559 // For a class template specialization, mangle the template arguments.
560 if (const ClassTemplateSpecializationDecl *Spec
561 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
Argyrios Kyrtzidis7d90ed02017-02-15 16:16:27 +0000562 const TemplateArgumentList &Args = Spec->getTemplateArgs();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000563 Out << '>';
564 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
565 Out << '#';
566 VisitTemplateArgument(Args.get(I));
567 }
568 }
569}
570
571void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000572 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000573 return;
574 const DeclContext *DC = D->getDeclContext();
575 if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
576 Visit(DCN);
577 Out << "@T@";
578 Out << D->getName();
579}
580
581void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000582 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000583}
584
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000585void USRGenerator::GenExtSymbolContainer(const NamedDecl *D) {
586 StringRef Container = GetExternalSourceContainer(D);
587 if (!Container.empty())
588 Out << "@M@" << Container;
589}
590
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000591bool USRGenerator::GenLoc(const Decl *D, bool IncludeOffset) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000592 if (generatedLoc)
593 return IgnoreResults;
594 generatedLoc = true;
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000595
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000596 // Guard against null declarations in invalid code.
597 if (!D) {
598 IgnoreResults = true;
599 return true;
600 }
601
602 // Use the location of canonical decl.
603 D = D->getCanonicalDecl();
604
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000605 IgnoreResults =
606 IgnoreResults || printLoc(Out, D->getLocStart(),
607 Context->getSourceManager(), IncludeOffset);
608
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000609 return IgnoreResults;
610}
611
612void USRGenerator::VisitType(QualType T) {
613 // This method mangles in USR information for types. It can possibly
614 // just reuse the naming-mangling logic used by codegen, although the
615 // requirements for USRs might not be the same.
616 ASTContext &Ctx = *Context;
617
618 do {
619 T = Ctx.getCanonicalType(T);
620 Qualifiers Q = T.getQualifiers();
621 unsigned qVal = 0;
622 if (Q.hasConst())
623 qVal |= 0x1;
624 if (Q.hasVolatile())
625 qVal |= 0x2;
626 if (Q.hasRestrict())
627 qVal |= 0x4;
628 if(qVal)
629 Out << ((char) ('0' + qVal));
630
631 // Mangle in ObjC GC qualifiers?
632
633 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) {
634 Out << 'P';
635 T = Expansion->getPattern();
636 }
637
638 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
639 unsigned char c = '\0';
640 switch (BT->getKind()) {
641 case BuiltinType::Void:
642 c = 'v'; break;
643 case BuiltinType::Bool:
644 c = 'b'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000645 case BuiltinType::UChar:
646 c = 'c'; break;
647 case BuiltinType::Char16:
648 c = 'q'; break;
649 case BuiltinType::Char32:
650 c = 'w'; break;
651 case BuiltinType::UShort:
652 c = 's'; break;
653 case BuiltinType::UInt:
654 c = 'i'; break;
655 case BuiltinType::ULong:
656 c = 'l'; break;
657 case BuiltinType::ULongLong:
658 c = 'k'; break;
659 case BuiltinType::UInt128:
660 c = 'j'; break;
Argyrios Kyrtzidis56af7e62014-12-08 09:09:05 +0000661 case BuiltinType::Char_U:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000662 case BuiltinType::Char_S:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000663 c = 'C'; break;
Argyrios Kyrtzidisca044542014-12-08 08:48:17 +0000664 case BuiltinType::SChar:
665 c = 'r'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000666 case BuiltinType::WChar_S:
667 case BuiltinType::WChar_U:
668 c = 'W'; break;
669 case BuiltinType::Short:
670 c = 'S'; break;
671 case BuiltinType::Int:
672 c = 'I'; break;
673 case BuiltinType::Long:
674 c = 'L'; break;
675 case BuiltinType::LongLong:
676 c = 'K'; break;
677 case BuiltinType::Int128:
678 c = 'J'; break;
679 case BuiltinType::Half:
680 c = 'h'; break;
681 case BuiltinType::Float:
682 c = 'f'; break;
683 case BuiltinType::Double:
684 c = 'd'; break;
685 case BuiltinType::LongDouble:
686 c = 'D'; break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000687 case BuiltinType::Float128:
688 c = 'Q'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000689 case BuiltinType::NullPtr:
690 c = 'n'; break;
691#define BUILTIN_TYPE(Id, SingletonId)
692#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
693#include "clang/AST/BuiltinTypes.def"
694 case BuiltinType::Dependent:
Alexey Bader954ba212016-04-08 13:40:33 +0000695#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
696 case BuiltinType::Id:
Alexey Baderb62f1442016-04-13 08:33:41 +0000697#include "clang/Basic/OpenCLImageTypes.def"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000698 case BuiltinType::OCLEvent:
Alexey Bader9c8453f2015-09-15 11:18:52 +0000699 case BuiltinType::OCLClkEvent:
700 case BuiltinType::OCLQueue:
Alexey Bader9c8453f2015-09-15 11:18:52 +0000701 case BuiltinType::OCLReserveID:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000702 case BuiltinType::OCLSampler:
703 IgnoreResults = true;
704 return;
705 case BuiltinType::ObjCId:
706 c = 'o'; break;
707 case BuiltinType::ObjCClass:
708 c = 'O'; break;
709 case BuiltinType::ObjCSel:
710 c = 'e'; break;
711 }
712 Out << c;
713 return;
714 }
715
716 // If we have already seen this (non-built-in) type, use a substitution
717 // encoding.
718 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
719 = TypeSubstitutions.find(T.getTypePtr());
720 if (Substitution != TypeSubstitutions.end()) {
721 Out << 'S' << Substitution->second << '_';
722 return;
723 } else {
724 // Record this as a substitution.
725 unsigned Number = TypeSubstitutions.size();
726 TypeSubstitutions[T.getTypePtr()] = Number;
727 }
728
729 if (const PointerType *PT = T->getAs<PointerType>()) {
730 Out << '*';
731 T = PT->getPointeeType();
732 continue;
733 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000734 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
735 Out << '*';
736 T = OPT->getPointeeType();
737 continue;
738 }
Argyrios Kyrtzidis0e387dc2014-12-08 08:48:27 +0000739 if (const RValueReferenceType *RT = T->getAs<RValueReferenceType>()) {
740 Out << "&&";
741 T = RT->getPointeeType();
742 continue;
743 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000744 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
745 Out << '&';
746 T = RT->getPointeeType();
747 continue;
748 }
749 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
750 Out << 'F';
Alp Toker314cc812014-01-25 16:55:45 +0000751 VisitType(FT->getReturnType());
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000752 for (const auto &I : FT->param_types())
753 VisitType(I);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000754 if (FT->isVariadic())
755 Out << '.';
756 return;
757 }
758 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
759 Out << 'B';
760 T = BT->getPointeeType();
761 continue;
762 }
763 if (const ComplexType *CT = T->getAs<ComplexType>()) {
764 Out << '<';
765 T = CT->getElementType();
766 continue;
767 }
768 if (const TagType *TT = T->getAs<TagType>()) {
769 Out << '$';
770 VisitTagDecl(TT->getDecl());
771 return;
772 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000773 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
774 Out << '$';
775 VisitObjCInterfaceDecl(OIT->getDecl());
776 return;
777 }
778 if (const ObjCObjectType *OIT = T->getAs<ObjCObjectType>()) {
779 Out << 'Q';
780 VisitType(OIT->getBaseType());
781 for (auto *Prot : OIT->getProtocols())
782 VisitObjCProtocolDecl(Prot);
783 return;
784 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000785 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
786 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
787 return;
788 }
789 if (const TemplateSpecializationType *Spec
790 = T->getAs<TemplateSpecializationType>()) {
791 Out << '>';
792 VisitTemplateName(Spec->getTemplateName());
793 Out << Spec->getNumArgs();
794 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
795 VisitTemplateArgument(Spec->getArg(I));
796 return;
797 }
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000798 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
799 Out << '^';
800 // FIXME: Encode the qualifier, don't just print it.
801 PrintingPolicy PO(Ctx.getLangOpts());
802 PO.SuppressTagKeyword = true;
803 PO.SuppressUnwrittenScope = true;
804 PO.ConstantArraySizeAsWritten = false;
805 PO.AnonymousTagLocations = false;
806 DNT->getQualifier()->print(Out, PO);
807 Out << ':' << DNT->getIdentifier()->getName();
808 return;
809 }
Argyrios Kyrtzidis1d5e5422014-12-08 08:48:43 +0000810 if (const InjectedClassNameType *InjT = T->getAs<InjectedClassNameType>()) {
811 T = InjT->getInjectedSpecializationType();
812 continue;
813 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000814
815 // Unhandled type.
816 Out << ' ';
817 break;
818 } while (true);
819}
820
821void USRGenerator::VisitTemplateParameterList(
822 const TemplateParameterList *Params) {
823 if (!Params)
824 return;
825 Out << '>' << Params->size();
826 for (TemplateParameterList::const_iterator P = Params->begin(),
827 PEnd = Params->end();
828 P != PEnd; ++P) {
829 Out << '#';
830 if (isa<TemplateTypeParmDecl>(*P)) {
831 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
832 Out<< 'p';
833 Out << 'T';
834 continue;
835 }
836
837 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
838 if (NTTP->isParameterPack())
839 Out << 'p';
840 Out << 'N';
841 VisitType(NTTP->getType());
842 continue;
843 }
844
845 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
846 if (TTP->isParameterPack())
847 Out << 'p';
848 Out << 't';
849 VisitTemplateParameterList(TTP->getTemplateParameters());
850 }
851}
852
853void USRGenerator::VisitTemplateName(TemplateName Name) {
854 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
855 if (TemplateTemplateParmDecl *TTP
856 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
857 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
858 return;
859 }
860
861 Visit(Template);
862 return;
863 }
864
865 // FIXME: Visit dependent template names.
866}
867
868void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
869 switch (Arg.getKind()) {
870 case TemplateArgument::Null:
871 break;
872
873 case TemplateArgument::Declaration:
874 Visit(Arg.getAsDecl());
875 break;
876
877 case TemplateArgument::NullPtr:
878 break;
879
880 case TemplateArgument::TemplateExpansion:
881 Out << 'P'; // pack expansion of...
882 // Fall through
883 case TemplateArgument::Template:
884 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
885 break;
886
887 case TemplateArgument::Expression:
888 // FIXME: Visit expressions.
889 break;
890
891 case TemplateArgument::Pack:
892 Out << 'p' << Arg.pack_size();
Aaron Ballman2a89e852014-07-15 21:32:31 +0000893 for (const auto &P : Arg.pack_elements())
894 VisitTemplateArgument(P);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000895 break;
896
897 case TemplateArgument::Type:
898 VisitType(Arg.getAsType());
899 break;
900
901 case TemplateArgument::Integral:
902 Out << 'V';
903 VisitType(Arg.getIntegralType());
904 Out << Arg.getAsIntegral();
905 break;
906 }
907}
908
909//===----------------------------------------------------------------------===//
910// USR generation functions.
911//===----------------------------------------------------------------------===//
912
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000913static void combineClassAndCategoryExtContainers(StringRef ClsSymDefinedIn,
914 StringRef CatSymDefinedIn,
915 raw_ostream &OS) {
916 if (ClsSymDefinedIn.empty() && CatSymDefinedIn.empty())
917 return;
918 if (CatSymDefinedIn.empty()) {
919 OS << "@M@" << ClsSymDefinedIn << '@';
920 return;
921 }
922 OS << "@CM@" << CatSymDefinedIn << '@';
923 if (ClsSymDefinedIn != CatSymDefinedIn) {
924 OS << ClsSymDefinedIn << '@';
925 }
926}
927
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000928void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS,
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000929 StringRef ExtSymDefinedIn,
930 StringRef CategoryContextExtSymbolDefinedIn) {
931 combineClassAndCategoryExtContainers(ExtSymDefinedIn,
932 CategoryContextExtSymbolDefinedIn, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000933 OS << "objc(cs)" << Cls;
934}
935
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000936void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat,
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000937 raw_ostream &OS,
938 StringRef ClsSymDefinedIn,
939 StringRef CatSymDefinedIn) {
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000940 combineClassAndCategoryExtContainers(ClsSymDefinedIn, CatSymDefinedIn, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000941 OS << "objc(cy)" << Cls << '@' << Cat;
942}
943
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000944void clang::index::generateUSRForObjCIvar(StringRef Ivar, raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000945 OS << '@' << Ivar;
946}
947
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000948void clang::index::generateUSRForObjCMethod(StringRef Sel,
949 bool IsInstanceMethod,
950 raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000951 OS << (IsInstanceMethod ? "(im)" : "(cm)") << Sel;
952}
953
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000954void clang::index::generateUSRForObjCProperty(StringRef Prop, bool isClassProp,
955 raw_ostream &OS) {
956 OS << (isClassProp ? "(cpy)" : "(py)") << Prop;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000957}
958
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000959void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS,
960 StringRef ExtSymDefinedIn) {
961 if (!ExtSymDefinedIn.empty())
962 OS << "@M@" << ExtSymDefinedIn << '@';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000963 OS << "objc(pl)" << Prot;
964}
965
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000966void clang::index::generateUSRForGlobalEnum(StringRef EnumName, raw_ostream &OS,
967 StringRef ExtSymDefinedIn) {
968 if (!ExtSymDefinedIn.empty())
969 OS << "@M@" << ExtSymDefinedIn;
970 OS << "@E@" << EnumName;
971}
972
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000973void clang::index::generateUSRForEnumConstant(StringRef EnumConstantName,
974 raw_ostream &OS) {
975 OS << '@' << EnumConstantName;
976}
977
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000978bool clang::index::generateUSRForDecl(const Decl *D,
979 SmallVectorImpl<char> &Buf) {
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000980 if (!D)
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000981 return true;
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000982 // We don't ignore decls with invalid source locations. Implicit decls, like
983 // C++'s operator new function, can have invalid locations but it is fine to
984 // create USRs that can identify them.
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000985
986 USRGenerator UG(&D->getASTContext(), Buf);
987 UG.Visit(D);
988 return UG.ignoreResults();
989}
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000990
Richard Smith66a81862015-05-04 02:25:31 +0000991bool clang::index::generateUSRForMacro(const MacroDefinitionRecord *MD,
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000992 const SourceManager &SM,
993 SmallVectorImpl<char> &Buf) {
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +0000994 if (!MD)
995 return true;
996 return generateUSRForMacro(MD->getName()->getName(), MD->getLocation(),
997 SM, Buf);
998
999}
1000
1001bool clang::index::generateUSRForMacro(StringRef MacroName, SourceLocation Loc,
1002 const SourceManager &SM,
1003 SmallVectorImpl<char> &Buf) {
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001004 // Don't generate USRs for things with invalid locations.
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001005 if (MacroName.empty() || Loc.isInvalid())
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001006 return true;
1007
1008 llvm::raw_svector_ostream Out(Buf);
1009
1010 // Assume that system headers are sane. Don't put source location
1011 // information into the USR if the macro comes from a system header.
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001012 bool ShouldGenerateLocation = !SM.isInSystemHeader(Loc);
1013
1014 Out << getUSRSpacePrefix();
1015 if (ShouldGenerateLocation)
1016 printLoc(Out, Loc, SM, /*IncludeOffset=*/true);
1017 Out << "@macro@";
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001018 Out << MacroName;
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001019 return false;
1020}