blob: 3196fd74633a606612096b76cf1779044bca6764 [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 Kremenek2fee4e62010-01-14 01:50:21 +000034public:
Ted Kremenek1865cfe2010-04-15 21:04:25 +000035 USRGenerator(ASTUnit *au, llvm::raw_ostream &out)
36 : Out(out), IgnoreResults(false), AU(au) {}
Ted Kremenek896b70f2010-03-13 02:50:34 +000037
Ted Kremenek3adca6d2010-01-18 22:02:49 +000038 bool ignoreResults() const { return IgnoreResults; }
Ted Kremenek896b70f2010-03-13 02:50:34 +000039
40 // Visitation methods from generating USRs from AST elements.
Ted Kremenek2fee4e62010-01-14 01:50:21 +000041 void VisitBlockDecl(BlockDecl *D);
42 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 Kremenek896b70f2010-03-13 02:50:34 +000047 void VisitObjCContainerDecl(ObjCContainerDecl *CD);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000048 void VisitObjCMethodDecl(ObjCMethodDecl *MD);
49 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Ted Kremenekb82b3be2010-01-18 22:42:20 +000050 void VisitTagDecl(TagDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000051 void VisitTypedefDecl(TypedefDecl *D);
Ted Kremenek896b70f2010-03-13 02:50:34 +000052
Ted Kremenek6f153952010-04-15 21:51:13 +000053 /// Generate the string component containing the location of the
54 /// declaration.
55 void GenLoc(const Decl *D);
56
Ted Kremenek896b70f2010-03-13 02:50:34 +000057 /// String generation methods used both by the visitation methods
58 /// and from other clients that want to directly generate USRs. These
59 /// methods do not construct complete USRs (which incorporate the parents
60 /// of an AST element), but only the fragments concerning the AST element
61 /// itself.
62
63 /// Generate a USR fragment for a named declaration. This does
64 /// not include the USR component for the parent.
65 void GenNamedDecl(llvm::StringRef name);
66
67 /// Generate a USR for an Objective-C class.
68 void GenObjCClass(llvm::StringRef cls);
69 /// Generate a USR for an Objective-C class category.
70 void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat);
71 /// Generate a USR fragment for an Objective-C instance variable. The
72 /// complete USR can be created by concatenating the USR for the
73 /// encompassing class with this USR fragment.
74 void GenObjCIvar(llvm::StringRef ivar);
75 /// Generate a USR fragment for an Objective-C method.
76 void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod);
77 /// Generate a USR fragment for an Objective-C property.
78 void GenObjCProperty(llvm::StringRef prop);
79 /// Generate a USR for an Objective-C protocol.
80 void GenObjCProtocol(llvm::StringRef prot);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000081};
Ted Kremenek896b70f2010-03-13 02:50:34 +000082
83class StringUSRGenerator {
84private:
85 llvm::SmallString<1024> StrBuf;
86 llvm::raw_svector_ostream Out;
87 USRGenerator UG;
88public:
Ted Kremenek1865cfe2010-04-15 21:04:25 +000089 StringUSRGenerator(const CXCursor *C = 0)
90 : Out(StrBuf), UG(C ? cxcursor::getCursorASTUnit(*C) : 0, Out) {
Ted Kremenek0c0fb412010-03-25 02:00:36 +000091 // Add the USR space prefix.
Ted Kremenek1865cfe2010-04-15 21:04:25 +000092 Out << "c:";
Ted Kremenek0c0fb412010-03-25 02:00:36 +000093 }
Ted Kremenek896b70f2010-03-13 02:50:34 +000094
95 llvm::StringRef str() {
96 return Out.str();
97 }
98
99 USRGenerator* operator->() { return &UG; }
100
101 template <typename T>
102 llvm::raw_svector_ostream &operator<<(const T &x) {
103 Out << x;
104 return Out;
105 }
106};
107
Ted Kremenekc50277f2010-01-12 23:33:42 +0000108} // end anonymous namespace
109
Ted Kremenek896b70f2010-03-13 02:50:34 +0000110//===----------------------------------------------------------------------===//
111// Generating USRs from ASTS.
112//===----------------------------------------------------------------------===//
113
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000114void USRGenerator::VisitBlockDecl(BlockDecl *D) {
115 VisitDeclContext(D->getDeclContext());
116 // FIXME: Better support for anonymous blocks.
117 Out << "@B^anon";
118}
119
120void USRGenerator::VisitDeclContext(DeclContext *DC) {
121 if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
122 Visit(D);
123}
124
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000125void USRGenerator::VisitFieldDecl(FieldDecl *D) {
126 const std::string &s = D->getNameAsString();
127 if (s.empty()) {
128 // Bit fields can be anonymous.
129 IgnoreResults = true;
130 return;
131 }
132 VisitDeclContext(D->getDeclContext());
133 Out << "@^FI^" << s;
134}
135
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000136void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
137 VisitDeclContext(D->getDeclContext());
138 Out << "@F^" << D->getNameAsString();
139}
Ted Kremenekc50277f2010-01-12 23:33:42 +0000140
141void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000142 VisitDeclContext(D->getDeclContext());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000143 const std::string &s = D->getNameAsString();
Ted Kremenek896b70f2010-03-13 02:50:34 +0000144 // assert(!s.empty());
145 GenNamedDecl(s);
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000146}
147
148void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
149 VisitDeclContext(D->getDeclContext());
150 Out << "@N^" << D->getNameAsString();
Ted Kremenekc50277f2010-01-12 23:33:42 +0000151}
152
Ted Kremenekc50277f2010-01-12 23:33:42 +0000153void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
154 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000155 GenObjCMethod(DeclarationName(D->getSelector()).getAsString(),
156 D->isInstanceMethod());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000157}
158
159void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
160 switch (D->getKind()) {
161 default:
162 assert(false && "Invalid ObjC container.");
163 case Decl::ObjCInterface:
164 case Decl::ObjCImplementation:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000165 GenObjCClass(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000166 break;
167 case Decl::ObjCCategory: {
168 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000169 ObjCInterfaceDecl *ID = CD->getClassInterface();
170 if (!ID) {
171 // Handle invalid code where the @interface might not
172 // have been specified.
173 // FIXME: We should be able to generate this USR even if the
174 // @interface isn't available.
175 IgnoreResults = true;
176 return;
177 }
178 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000179 break;
180 }
181 case Decl::ObjCCategoryImpl: {
182 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000183 ObjCInterfaceDecl *ID = CD->getClassInterface();
184 if (!ID) {
185 // Handle invalid code where the @interface might not
186 // have been specified.
187 // FIXME: We should be able to generate this USR even if the
188 // @interface isn't available.
189 IgnoreResults = true;
190 return;
191 }
192 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000193 break;
194 }
195 case Decl::ObjCProtocol:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000196 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000197 break;
198 }
199}
200
201void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
202 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000203 GenObjCProperty(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000204}
205
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000206void USRGenerator::VisitTagDecl(TagDecl *D) {
Ted Kremenek6f153952010-04-15 21:51:13 +0000207 D = D->getCanonicalDecl();
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000208 VisitDeclContext(D->getDeclContext());
209 switch (D->getTagKind()) {
210 case TagDecl::TK_struct: Out << "@S^"; break;
211 case TagDecl::TK_class: Out << "@C^"; break;
212 case TagDecl::TK_union: Out << "@U^"; break;
213 case TagDecl::TK_enum: Out << "@E^"; break;
214 }
Ted Kremenek896b70f2010-03-13 02:50:34 +0000215
Ted Kremenek6f153952010-04-15 21:51:13 +0000216 // Add the location of the tag decl to handle resolution across
217 // translation units.
218 if (D->getLinkage() == NoLinkage) {
219 GenLoc(D);
220 if (IgnoreResults)
221 return;
222 }
223
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000224 const std::string &s = D->getNameAsString();
225 if (s.empty()) {
226 if (TypedefDecl *TD = D->getTypedefForAnonDecl())
Ted Kremenek896b70f2010-03-13 02:50:34 +0000227 Out << "^anontd^" << TD->getNameAsString();
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000228 else
229 Out << "^anon";
230 }
231 else
232 Out << s;
233}
234
Ted Kremenekc50277f2010-01-12 23:33:42 +0000235void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
236 DeclContext *DC = D->getDeclContext();
237 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Ted Kremenek896b70f2010-03-13 02:50:34 +0000238 Visit(DCN);
Ted Kremenek6f153952010-04-15 21:51:13 +0000239 Out << "typedef@";
240 GenLoc(D);
241 Out << D->getName();
242}
243
244void USRGenerator::GenLoc(const Decl *D) {
245 const SourceManager &SM = AU->getSourceManager();
246 SourceLocation L = D->getLocStart();
247 if (L.isInvalid()) {
248 IgnoreResults = true;
249 return;
250 }
251 L = SM.getInstantiationLoc(L);
252 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
253 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
254 if (FE)
255 Out << FE->getName();
256 else {
257 // This case really isn't interesting.
258 IgnoreResults = true;
259 return;
260 }
261 Out << SM.getLineNumber(Decomposed.first, Decomposed.second) << ':'
262 << SM.getColumnNumber(Decomposed.first, Decomposed.second)
263 << '^';
Ted Kremenekc50277f2010-01-12 23:33:42 +0000264}
265
Ted Kremenek896b70f2010-03-13 02:50:34 +0000266//===----------------------------------------------------------------------===//
267// General purpose USR generation methods.
268//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000269
Ted Kremenek896b70f2010-03-13 02:50:34 +0000270void USRGenerator::GenNamedDecl(llvm::StringRef name) {
271 Out << "@^" << name;
272}
273
274void USRGenerator::GenObjCClass(llvm::StringRef cls) {
275 Out << "objc(cs)" << cls;
276}
277
278void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) {
279 Out << "objc(cy)" << cls << '^' << cat;
280}
281
282void USRGenerator::GenObjCIvar(llvm::StringRef ivar) {
283 GenNamedDecl(ivar);
284}
285
286void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) {
287 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
288}
289
290void USRGenerator::GenObjCProperty(llvm::StringRef prop) {
291 Out << "(py)" << prop;
292}
293
294void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
295 Out << "objc(pl)" << prot;
296}
297
298//===----------------------------------------------------------------------===//
299// API hooks.
300//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000301
Benjamin Kramercfb51b62010-04-08 15:54:07 +0000302static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) {
303 return s.startswith("c:") ? s.substr(2) : "";
304}
305
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000306static CXString getDeclCursorUSR(const CXCursor &C) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000307 Decl *D = cxcursor::getCursorDecl(C);
308 if (!D)
309 return createCXString(NULL);
310
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000311 // Check if the cursor has 'NoLinkage'.
312 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D))
313 switch (ND->getLinkage()) {
314 case ExternalLinkage:
315 // Generate USRs for all entities with external linkage.
316 break;
317 case NoLinkage:
318 // We allow enums, typedefs, and structs that have no linkage to
319 // have USRs that are anchored to the file they were defined in
320 // (e.g., the header). This is a little gross, but in principal
321 // enums/anonymous structs/etc. defined in a common header file
322 // are referred to across multiple translation units.
Ted Kremenek6f153952010-04-15 21:51:13 +0000323 if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) ||
324 isa<EnumConstantDecl>(ND))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000325 break;
326 // Fall-through.
327 case InternalLinkage:
328 case UniqueExternalLinkage:
329 return createCXString("");
330 }
331
332 StringUSRGenerator SUG(&C);
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000333 SUG->Visit(D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000334
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000335 if (SUG->ignoreResults())
Ted Kremenekebfa3392010-03-19 20:39:03 +0000336 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000337
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000338 // Return a copy of the string that must be disposed by the caller.
Ted Kremenek896b70f2010-03-13 02:50:34 +0000339 return createCXString(SUG.str(), true);
340}
341
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000342extern "C" {
343
344CXString clang_getCursorUSR(CXCursor C) {
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000345 const CXCursorKind &K = clang_getCursorKind(C);
346
347 if (clang_isDeclaration(K))
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000348 return getDeclCursorUSR(C);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000349
350 if (K == CXCursor_MacroDefinition) {
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000351 StringUSRGenerator SUG(&C);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000352 SUG << "macro@"
353 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
354 return createCXString(SUG.str(), true);
355 }
356
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000357 return createCXString("");
358}
359
Ted Kremenek896b70f2010-03-13 02:50:34 +0000360CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
361 StringUSRGenerator SUG;
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000362 SUG << extractUSRSuffix(clang_getCString(classUSR));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000363 SUG->GenObjCIvar(name);
364 return createCXString(SUG.str(), true);
365}
366
367CXString clang_constructUSR_ObjCMethod(const char *name,
368 unsigned isInstanceMethod,
369 CXString classUSR) {
370 StringUSRGenerator SUG;
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000371 SUG << extractUSRSuffix(clang_getCString(classUSR));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000372 SUG->GenObjCMethod(name, isInstanceMethod);
373 return createCXString(SUG.str(), true);
374}
375
376CXString clang_constructUSR_ObjCClass(const char *name) {
377 StringUSRGenerator SUG;
378 SUG->GenObjCClass(name);
379 return createCXString(SUG.str(), true);
380}
381
382CXString clang_constructUSR_ObjCProtocol(const char *name) {
383 StringUSRGenerator SUG;
384 SUG->GenObjCProtocol(name);
385 return createCXString(SUG.str(), true);
386}
387
Ted Kremenek66ccaec2010-03-15 17:38:58 +0000388CXString clang_constructUSR_ObjCCategory(const char *class_name,
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000389 const char *category_name) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000390 StringUSRGenerator SUG;
391 SUG->GenObjCCategory(class_name, category_name);
392 return createCXString(SUG.str(), true);
393}
394
395CXString clang_constructUSR_ObjCProperty(const char *property,
396 CXString classUSR) {
397 StringUSRGenerator SUG;
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000398 SUG << extractUSRSuffix(clang_getCString(classUSR));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000399 SUG->GenObjCProperty(property);
400 return createCXString(SUG.str(), true);
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000401}
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000402
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000403} // end extern "C"