blob: cc481d230e8bc5c6afeb3141ca6dc62f8f6c99c0 [file] [log] [blame]
Ted Kremenek1b6869a2010-01-05 22:06:45 +00001//===- CIndexUSR.cpp - Clang-C Source Indexing Library --------------------===//
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//
10// This file implements the generation and use of USRs from CXEntities.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CIndexer.h"
Ted Kremenekcf84aa42010-01-18 20:23:29 +000015#include "CXCursor.h"
Benjamin Kramer9895c6a2010-01-12 11:32:40 +000016#include "clang/AST/DeclVisitor.h"
Ted Kremenek6f153952010-04-15 21:51:13 +000017#include "clang/Frontend/ASTUnit.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000018#include "clang/Lex/PreprocessingRecord.h"
Ted Kremenek87763822010-01-12 02:07:58 +000019#include "llvm/ADT/SmallString.h"
Benjamin Kramer9895c6a2010-01-12 11:32:40 +000020#include "llvm/Support/raw_ostream.h"
Ted Kremenek6f153952010-04-15 21:51:13 +000021
Benjamin Kramerb846deb2010-04-12 19:45:50 +000022using namespace clang;
Ted Kremenekee4db4f2010-02-17 00:41:08 +000023using namespace clang::cxstring;
24
Ted Kremenekc50277f2010-01-12 23:33:42 +000025//===----------------------------------------------------------------------===//
26// USR generation.
27//===----------------------------------------------------------------------===//
28
29namespace {
Ted Kremenek2fee4e62010-01-14 01:50:21 +000030class USRGenerator : public DeclVisitor<USRGenerator> {
31 llvm::raw_ostream &Out;
Ted Kremenek3adca6d2010-01-18 22:02:49 +000032 bool IgnoreResults;
Ted Kremenek1865cfe2010-04-15 21:04:25 +000033 ASTUnit *AU;
Ted Kremenekcbd66f02010-05-06 23:38:28 +000034 bool generatedLoc;
Ted Kremenek2fee4e62010-01-14 01:50:21 +000035public:
Ted Kremenek1865cfe2010-04-15 21:04:25 +000036 USRGenerator(ASTUnit *au, llvm::raw_ostream &out)
Ted Kremenekcbd66f02010-05-06 23:38:28 +000037 : Out(out), IgnoreResults(false), AU(au), generatedLoc(false) {}
Ted Kremenek896b70f2010-03-13 02:50:34 +000038
Ted Kremenek3adca6d2010-01-18 22:02:49 +000039 bool ignoreResults() const { return IgnoreResults; }
Ted Kremenek896b70f2010-03-13 02:50:34 +000040
41 // Visitation methods from generating USRs from AST elements.
Ted Kremenek2fee4e62010-01-14 01:50:21 +000042 void VisitDeclContext(DeclContext *D);
Ted Kremenek3adca6d2010-01-18 22:02:49 +000043 void VisitFieldDecl(FieldDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000044 void VisitFunctionDecl(FunctionDecl *D);
45 void VisitNamedDecl(NamedDecl *D);
46 void VisitNamespaceDecl(NamespaceDecl *D);
Ted Kremeneke74ef122010-04-16 21:31:52 +000047 void VisitObjCClassDecl(ObjCClassDecl *CD);
Ted Kremenek896b70f2010-03-13 02:50:34 +000048 void VisitObjCContainerDecl(ObjCContainerDecl *CD);
Ted Kremeneke74ef122010-04-16 21:31:52 +000049 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *P);
Ted Kremeneke542f772010-04-20 23:15:40 +000050 void VisitObjCMethodDecl(ObjCMethodDecl *MD);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000051 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000052 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Ted Kremenekb82b3be2010-01-18 22:42:20 +000053 void VisitTagDecl(TagDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000054 void VisitTypedefDecl(TypedefDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000055 void VisitVarDecl(VarDecl *D);
Ted Kremenek896b70f2010-03-13 02:50:34 +000056
Ted Kremenek6f153952010-04-15 21:51:13 +000057 /// Generate the string component containing the location of the
58 /// declaration.
Ted Kremenekcbd66f02010-05-06 23:38:28 +000059 bool GenLoc(const Decl *D);
Ted Kremenek6f153952010-04-15 21:51:13 +000060
Ted Kremenek896b70f2010-03-13 02:50:34 +000061 /// String generation methods used both by the visitation methods
62 /// and from other clients that want to directly generate USRs. These
63 /// methods do not construct complete USRs (which incorporate the parents
64 /// of an AST element), but only the fragments concerning the AST element
65 /// itself.
66
67 /// Generate a USR fragment for a named declaration. This does
68 /// not include the USR component for the parent.
69 void GenNamedDecl(llvm::StringRef name);
70
71 /// Generate a USR for an Objective-C class.
72 void GenObjCClass(llvm::StringRef cls);
73 /// Generate a USR for an Objective-C class category.
74 void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat);
75 /// Generate a USR fragment for an Objective-C instance variable. The
76 /// complete USR can be created by concatenating the USR for the
77 /// encompassing class with this USR fragment.
78 void GenObjCIvar(llvm::StringRef ivar);
79 /// Generate a USR fragment for an Objective-C method.
80 void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod);
81 /// Generate a USR fragment for an Objective-C property.
82 void GenObjCProperty(llvm::StringRef prop);
83 /// Generate a USR for an Objective-C protocol.
84 void GenObjCProtocol(llvm::StringRef prot);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000085};
Ted Kremenek896b70f2010-03-13 02:50:34 +000086
87class StringUSRGenerator {
88private:
89 llvm::SmallString<1024> StrBuf;
90 llvm::raw_svector_ostream Out;
91 USRGenerator UG;
92public:
Ted Kremenek1865cfe2010-04-15 21:04:25 +000093 StringUSRGenerator(const CXCursor *C = 0)
94 : Out(StrBuf), UG(C ? cxcursor::getCursorASTUnit(*C) : 0, Out) {
Ted Kremenek0c0fb412010-03-25 02:00:36 +000095 // Add the USR space prefix.
Ted Kremenek1865cfe2010-04-15 21:04:25 +000096 Out << "c:";
Ted Kremenek0c0fb412010-03-25 02:00:36 +000097 }
Ted Kremenek896b70f2010-03-13 02:50:34 +000098
99 llvm::StringRef str() {
100 return Out.str();
101 }
102
103 USRGenerator* operator->() { return &UG; }
104
105 template <typename T>
106 llvm::raw_svector_ostream &operator<<(const T &x) {
107 Out << x;
108 return Out;
109 }
110};
111
Ted Kremenekc50277f2010-01-12 23:33:42 +0000112} // end anonymous namespace
113
Ted Kremenek896b70f2010-03-13 02:50:34 +0000114//===----------------------------------------------------------------------===//
115// Generating USRs from ASTS.
116//===----------------------------------------------------------------------===//
117
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000118static bool InAnonymousNamespace(const Decl *D) {
119 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext()))
120 return ND->isAnonymousNamespace();
121 return false;
122}
123
124static inline bool ShouldGenerateLocation(const NamedDecl *D) {
125 return D->getLinkage() != ExternalLinkage && !InAnonymousNamespace(D);
126}
127
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000128void USRGenerator::VisitDeclContext(DeclContext *DC) {
129 if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
130 Visit(D);
131}
132
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000133void USRGenerator::VisitFieldDecl(FieldDecl *D) {
134 const std::string &s = D->getNameAsString();
135 if (s.empty()) {
136 // Bit fields can be anonymous.
137 IgnoreResults = true;
138 return;
139 }
140 VisitDeclContext(D->getDeclContext());
Ted Kremeneke542f772010-04-20 23:15:40 +0000141 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@") << s;
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000142}
143
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000144void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000145 if (ShouldGenerateLocation(D) && GenLoc(D))
146 return;
Ted Kremenekcf999102010-04-29 17:43:29 +0000147
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000148 VisitDeclContext(D->getDeclContext());
149 Out << "@F@" << D->getNameAsString();
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000150}
Ted Kremenekc50277f2010-01-12 23:33:42 +0000151
152void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000153 VisitDeclContext(D->getDeclContext());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000154 const std::string &s = D->getNameAsString();
Ted Kremeneke74ef122010-04-16 21:31:52 +0000155 // The string can be empty if the declaration has no name; e.g., it is
156 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
157 // void (*f)(void *);
158 // In this case, don't generate a USR.
159 if (s.empty())
160 IgnoreResults = true;
161 else
162 GenNamedDecl(s);
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000163}
164
Ted Kremeneke542f772010-04-20 23:15:40 +0000165void USRGenerator::VisitVarDecl(VarDecl *D) {
166 // VarDecls can be declared 'extern' within a function or method body,
167 // but their enclosing DeclContext is the function, not the TU. We need
168 // to check the storage class to correctly generate the USR.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000169 if (ShouldGenerateLocation(D) && GenLoc(D))
170 return;
171
172 VisitDeclContext(D->getDeclContext());
Ted Kremeneke542f772010-04-20 23:15:40 +0000173
Ted Kremenekcf999102010-04-29 17:43:29 +0000174 // Variables always have simple names.
175 llvm::StringRef s = D->getName();
176
Ted Kremeneke542f772010-04-20 23:15:40 +0000177 // The string can be empty if the declaration has no name; e.g., it is
178 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
179 // void (*f)(void *);
180 // In this case, don't generate a USR.
181 if (s.empty())
182 IgnoreResults = true;
183 else
184 GenNamedDecl(s);
185}
186
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000187void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000188 if (D->isAnonymousNamespace()) {
189 Out << "@aN";
190 return;
191 }
192
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000193 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000194 if (!IgnoreResults)
195 Out << "@N@" << D->getName();
Ted Kremenekc50277f2010-01-12 23:33:42 +0000196}
197
Ted Kremenekc50277f2010-01-12 23:33:42 +0000198void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
199 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000200 GenObjCMethod(DeclarationName(D->getSelector()).getAsString(),
201 D->isInstanceMethod());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000202}
203
Ted Kremeneke74ef122010-04-16 21:31:52 +0000204void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) {
205 // FIXME: @class declarations can refer to multiple classes. We need
206 // to be able to traverse these.
207 IgnoreResults = true;
208}
209
210void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
211 // FIXME: @protocol declarations can refer to multiple protocols. We need
212 // to be able to traverse these.
213 IgnoreResults = true;
214}
215
Ted Kremenekc50277f2010-01-12 23:33:42 +0000216void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
217 switch (D->getKind()) {
218 default:
219 assert(false && "Invalid ObjC container.");
220 case Decl::ObjCInterface:
221 case Decl::ObjCImplementation:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000222 GenObjCClass(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000223 break;
224 case Decl::ObjCCategory: {
225 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000226 ObjCInterfaceDecl *ID = CD->getClassInterface();
227 if (!ID) {
228 // Handle invalid code where the @interface might not
229 // have been specified.
230 // FIXME: We should be able to generate this USR even if the
231 // @interface isn't available.
232 IgnoreResults = true;
233 return;
234 }
235 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000236 break;
237 }
238 case Decl::ObjCCategoryImpl: {
239 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000240 ObjCInterfaceDecl *ID = CD->getClassInterface();
241 if (!ID) {
242 // Handle invalid code where the @interface might not
243 // have been specified.
244 // FIXME: We should be able to generate this USR even if the
245 // @interface isn't available.
246 IgnoreResults = true;
247 return;
248 }
249 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000250 break;
251 }
252 case Decl::ObjCProtocol:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000253 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000254 break;
255 }
256}
257
258void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
259 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000260 GenObjCProperty(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000261}
262
Ted Kremeneke542f772010-04-20 23:15:40 +0000263void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
264 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
265 VisitObjCPropertyDecl(PD);
266 return;
267 }
268
269 IgnoreResults = true;
270}
271
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000272void USRGenerator::VisitTagDecl(TagDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000273 // Add the location of the tag decl to handle resolution across
274 // translation units.
275 if (ShouldGenerateLocation(D) && GenLoc(D))
276 return;
277
Ted Kremenek6f153952010-04-15 21:51:13 +0000278 D = D->getCanonicalDecl();
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000279 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000280
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000281 switch (D->getTagKind()) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000282 case TagDecl::TK_struct: Out << "@S"; break;
283 case TagDecl::TK_class: Out << "@C"; break;
284 case TagDecl::TK_union: Out << "@U"; break;
285 case TagDecl::TK_enum: Out << "@E"; break;
286 }
287
288 const std::string &s = D->getNameAsString();
289 const TypedefDecl *TD = 0;
290 if (s.empty()) {
291 TD = D->getTypedefForAnonDecl();
292 Out << (TD ? 'A' : 'a');
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000293 }
Ted Kremenek896b70f2010-03-13 02:50:34 +0000294
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000295 if (s.empty()) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000296 if (TD)
Benjamin Kramer900fc632010-04-17 09:33:03 +0000297 Out << '@' << TD;
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000298 }
299 else
Ted Kremeneke74ef122010-04-16 21:31:52 +0000300 Out << '@' << s;
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000301}
302
Ted Kremenekc50277f2010-01-12 23:33:42 +0000303void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000304 if (ShouldGenerateLocation(D) && GenLoc(D))
305 return;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000306 DeclContext *DC = D->getDeclContext();
307 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Ted Kremenek896b70f2010-03-13 02:50:34 +0000308 Visit(DCN);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000309 Out << "@T@";
Ted Kremenek6f153952010-04-15 21:51:13 +0000310 Out << D->getName();
311}
312
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000313bool USRGenerator::GenLoc(const Decl *D) {
314 if (generatedLoc)
315 return IgnoreResults;
316 generatedLoc = true;
317
Ted Kremenek6f153952010-04-15 21:51:13 +0000318 const SourceManager &SM = AU->getSourceManager();
319 SourceLocation L = D->getLocStart();
320 if (L.isInvalid()) {
321 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000322 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000323 }
324 L = SM.getInstantiationLoc(L);
325 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
326 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000327 if (FE) {
328 llvm::sys::Path P(FE->getName());
329 Out << P.getLast();
330 }
Ted Kremenek6f153952010-04-15 21:51:13 +0000331 else {
332 // This case really isn't interesting.
333 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000334 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000335 }
Ted Kremeneke74ef122010-04-16 21:31:52 +0000336 Out << '@'
337 << SM.getLineNumber(Decomposed.first, Decomposed.second) << ':'
338 << SM.getColumnNumber(Decomposed.first, Decomposed.second);
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000339
340 return IgnoreResults;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000341}
342
Ted Kremenek896b70f2010-03-13 02:50:34 +0000343//===----------------------------------------------------------------------===//
344// General purpose USR generation methods.
345//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000346
Ted Kremenek896b70f2010-03-13 02:50:34 +0000347void USRGenerator::GenNamedDecl(llvm::StringRef name) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000348 Out << "@" << name;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000349}
350
351void USRGenerator::GenObjCClass(llvm::StringRef cls) {
352 Out << "objc(cs)" << cls;
353}
354
355void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000356 Out << "objc(cy)" << cls << '@' << cat;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000357}
358
359void USRGenerator::GenObjCIvar(llvm::StringRef ivar) {
360 GenNamedDecl(ivar);
361}
362
363void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) {
364 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
365}
366
367void USRGenerator::GenObjCProperty(llvm::StringRef prop) {
368 Out << "(py)" << prop;
369}
370
371void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
372 Out << "objc(pl)" << prot;
373}
374
375//===----------------------------------------------------------------------===//
376// API hooks.
377//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000378
Benjamin Kramercfb51b62010-04-08 15:54:07 +0000379static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) {
380 return s.startswith("c:") ? s.substr(2) : "";
381}
382
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000383static CXString getDeclCursorUSR(const CXCursor &C) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000384 Decl *D = cxcursor::getCursorDecl(C);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000385
386 // Don't generate USRs for things with invalid locations.
387 if (!D || D->getLocStart().isInvalid())
Ted Kremenek1af0a2a2010-04-17 00:21:38 +0000388 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000389
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000390 // Check if the cursor has 'NoLinkage'.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000391 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000392 switch (ND->getLinkage()) {
393 case ExternalLinkage:
394 // Generate USRs for all entities with external linkage.
395 break;
396 case NoLinkage:
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000397 case UniqueExternalLinkage:
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000398 // We allow enums, typedefs, and structs that have no linkage to
399 // have USRs that are anchored to the file they were defined in
400 // (e.g., the header). This is a little gross, but in principal
401 // enums/anonymous structs/etc. defined in a common header file
402 // are referred to across multiple translation units.
Ted Kremenek6f153952010-04-15 21:51:13 +0000403 if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) ||
Ted Kremenekcf999102010-04-29 17:43:29 +0000404 isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) ||
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000405 isa<VarDecl>(ND) || isa<NamespaceDecl>(ND))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000406 break;
407 // Fall-through.
408 case InternalLinkage:
Ted Kremenekcf999102010-04-29 17:43:29 +0000409 if (isa<FunctionDecl>(ND))
410 break;
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000411 }
412
413 StringUSRGenerator SUG(&C);
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000414 SUG->Visit(D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000415
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000416 if (SUG->ignoreResults())
Ted Kremenekebfa3392010-03-19 20:39:03 +0000417 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000418
Ted Kremeneke542f772010-04-20 23:15:40 +0000419 // For development testing.
420 // assert(SUG.str().size() > 2);
421
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000422 // Return a copy of the string that must be disposed by the caller.
Ted Kremenek896b70f2010-03-13 02:50:34 +0000423 return createCXString(SUG.str(), true);
424}
425
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000426extern "C" {
427
428CXString clang_getCursorUSR(CXCursor C) {
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000429 const CXCursorKind &K = clang_getCursorKind(C);
430
431 if (clang_isDeclaration(K))
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000432 return getDeclCursorUSR(C);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000433
434 if (K == CXCursor_MacroDefinition) {
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000435 StringUSRGenerator SUG(&C);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000436 SUG << "macro@"
437 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
438 return createCXString(SUG.str(), true);
439 }
440
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000441 return createCXString("");
442}
443
Ted Kremenek896b70f2010-03-13 02:50:34 +0000444CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
445 StringUSRGenerator SUG;
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000446 SUG << extractUSRSuffix(clang_getCString(classUSR));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000447 SUG->GenObjCIvar(name);
448 return createCXString(SUG.str(), true);
449}
450
451CXString clang_constructUSR_ObjCMethod(const char *name,
452 unsigned isInstanceMethod,
453 CXString classUSR) {
454 StringUSRGenerator SUG;
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000455 SUG << extractUSRSuffix(clang_getCString(classUSR));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000456 SUG->GenObjCMethod(name, isInstanceMethod);
457 return createCXString(SUG.str(), true);
458}
459
460CXString clang_constructUSR_ObjCClass(const char *name) {
461 StringUSRGenerator SUG;
462 SUG->GenObjCClass(name);
463 return createCXString(SUG.str(), true);
464}
465
466CXString clang_constructUSR_ObjCProtocol(const char *name) {
467 StringUSRGenerator SUG;
468 SUG->GenObjCProtocol(name);
469 return createCXString(SUG.str(), true);
470}
471
Ted Kremenek66ccaec2010-03-15 17:38:58 +0000472CXString clang_constructUSR_ObjCCategory(const char *class_name,
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000473 const char *category_name) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000474 StringUSRGenerator SUG;
475 SUG->GenObjCCategory(class_name, category_name);
476 return createCXString(SUG.str(), true);
477}
478
479CXString clang_constructUSR_ObjCProperty(const char *property,
480 CXString classUSR) {
481 StringUSRGenerator SUG;
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000482 SUG << extractUSRSuffix(clang_getCString(classUSR));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000483 SUG->GenObjCProperty(property);
484 return createCXString(SUG.str(), true);
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000485}
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000486
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000487} // end extern "C"