blob: b049c96b482bf033abfae50b8292505aa3242118 [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"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000017#include "clang/Lex/PreprocessingRecord.h"
Ted Kremenek87763822010-01-12 02:07:58 +000018#include "llvm/ADT/SmallString.h"
Benjamin Kramer9895c6a2010-01-12 11:32:40 +000019#include "llvm/Support/raw_ostream.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000020using namespace clang;
Ted Kremenekee4db4f2010-02-17 00:41:08 +000021using namespace clang::cxstring;
22
Ted Kremenekc50277f2010-01-12 23:33:42 +000023//===----------------------------------------------------------------------===//
24// USR generation.
25//===----------------------------------------------------------------------===//
26
27namespace {
Ted Kremenek2fee4e62010-01-14 01:50:21 +000028class USRGenerator : public DeclVisitor<USRGenerator> {
29 llvm::raw_ostream &Out;
Ted Kremenek3adca6d2010-01-18 22:02:49 +000030 bool IgnoreResults;
Ted Kremenek2fee4e62010-01-14 01:50:21 +000031public:
Ted Kremenek3adca6d2010-01-18 22:02:49 +000032 USRGenerator(llvm::raw_ostream &out) : Out(out), IgnoreResults(false) {}
Ted Kremenek896b70f2010-03-13 02:50:34 +000033
Ted Kremenek3adca6d2010-01-18 22:02:49 +000034 bool ignoreResults() const { return IgnoreResults; }
Ted Kremenek896b70f2010-03-13 02:50:34 +000035
36 // Visitation methods from generating USRs from AST elements.
Ted Kremenek2fee4e62010-01-14 01:50:21 +000037 void VisitBlockDecl(BlockDecl *D);
38 void VisitDeclContext(DeclContext *D);
Ted Kremenek3adca6d2010-01-18 22:02:49 +000039 void VisitFieldDecl(FieldDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000040 void VisitFunctionDecl(FunctionDecl *D);
41 void VisitNamedDecl(NamedDecl *D);
42 void VisitNamespaceDecl(NamespaceDecl *D);
Ted Kremenek896b70f2010-03-13 02:50:34 +000043 void VisitObjCContainerDecl(ObjCContainerDecl *CD);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000044 void VisitObjCMethodDecl(ObjCMethodDecl *MD);
45 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Ted Kremenekb82b3be2010-01-18 22:42:20 +000046 void VisitTagDecl(TagDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000047 void VisitTypedefDecl(TypedefDecl *D);
Ted Kremenek896b70f2010-03-13 02:50:34 +000048
49
50 /// String generation methods used both by the visitation methods
51 /// and from other clients that want to directly generate USRs. These
52 /// methods do not construct complete USRs (which incorporate the parents
53 /// of an AST element), but only the fragments concerning the AST element
54 /// itself.
55
56 /// Generate a USR fragment for a named declaration. This does
57 /// not include the USR component for the parent.
58 void GenNamedDecl(llvm::StringRef name);
59
60 /// Generate a USR for an Objective-C class.
61 void GenObjCClass(llvm::StringRef cls);
62 /// Generate a USR for an Objective-C class category.
63 void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat);
64 /// Generate a USR fragment for an Objective-C instance variable. The
65 /// complete USR can be created by concatenating the USR for the
66 /// encompassing class with this USR fragment.
67 void GenObjCIvar(llvm::StringRef ivar);
68 /// Generate a USR fragment for an Objective-C method.
69 void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod);
70 /// Generate a USR fragment for an Objective-C property.
71 void GenObjCProperty(llvm::StringRef prop);
72 /// Generate a USR for an Objective-C protocol.
73 void GenObjCProtocol(llvm::StringRef prot);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000074};
Ted Kremenek896b70f2010-03-13 02:50:34 +000075
76class StringUSRGenerator {
77private:
78 llvm::SmallString<1024> StrBuf;
79 llvm::raw_svector_ostream Out;
80 USRGenerator UG;
81public:
82 StringUSRGenerator()
Ted Kremenek0c0fb412010-03-25 02:00:36 +000083 : Out(StrBuf), UG(Out) {
84 // Add the USR space prefix.
85 Out << "c:";
86 }
Ted Kremenek896b70f2010-03-13 02:50:34 +000087
88 llvm::StringRef str() {
89 return Out.str();
90 }
91
92 USRGenerator* operator->() { return &UG; }
93
94 template <typename T>
95 llvm::raw_svector_ostream &operator<<(const T &x) {
96 Out << x;
97 return Out;
98 }
99};
100
Ted Kremenekc50277f2010-01-12 23:33:42 +0000101} // end anonymous namespace
102
Ted Kremenek896b70f2010-03-13 02:50:34 +0000103//===----------------------------------------------------------------------===//
104// Generating USRs from ASTS.
105//===----------------------------------------------------------------------===//
106
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000107void USRGenerator::VisitBlockDecl(BlockDecl *D) {
108 VisitDeclContext(D->getDeclContext());
109 // FIXME: Better support for anonymous blocks.
110 Out << "@B^anon";
111}
112
113void USRGenerator::VisitDeclContext(DeclContext *DC) {
114 if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
115 Visit(D);
116}
117
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000118void USRGenerator::VisitFieldDecl(FieldDecl *D) {
119 const std::string &s = D->getNameAsString();
120 if (s.empty()) {
121 // Bit fields can be anonymous.
122 IgnoreResults = true;
123 return;
124 }
125 VisitDeclContext(D->getDeclContext());
126 Out << "@^FI^" << s;
127}
128
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000129void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
130 VisitDeclContext(D->getDeclContext());
131 Out << "@F^" << D->getNameAsString();
132}
Ted Kremenekc50277f2010-01-12 23:33:42 +0000133
134void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000135 VisitDeclContext(D->getDeclContext());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000136 const std::string &s = D->getNameAsString();
Ted Kremenek896b70f2010-03-13 02:50:34 +0000137 // assert(!s.empty());
138 GenNamedDecl(s);
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000139}
140
141void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
142 VisitDeclContext(D->getDeclContext());
143 Out << "@N^" << D->getNameAsString();
Ted Kremenekc50277f2010-01-12 23:33:42 +0000144}
145
Ted Kremenekc50277f2010-01-12 23:33:42 +0000146void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
147 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000148 GenObjCMethod(DeclarationName(D->getSelector()).getAsString(),
149 D->isInstanceMethod());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000150}
151
152void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
153 switch (D->getKind()) {
154 default:
155 assert(false && "Invalid ObjC container.");
156 case Decl::ObjCInterface:
157 case Decl::ObjCImplementation:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000158 GenObjCClass(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000159 break;
160 case Decl::ObjCCategory: {
161 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000162 ObjCInterfaceDecl *ID = CD->getClassInterface();
163 if (!ID) {
164 // Handle invalid code where the @interface might not
165 // have been specified.
166 // FIXME: We should be able to generate this USR even if the
167 // @interface isn't available.
168 IgnoreResults = true;
169 return;
170 }
171 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000172 break;
173 }
174 case Decl::ObjCCategoryImpl: {
175 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000176 ObjCInterfaceDecl *ID = CD->getClassInterface();
177 if (!ID) {
178 // Handle invalid code where the @interface might not
179 // have been specified.
180 // FIXME: We should be able to generate this USR even if the
181 // @interface isn't available.
182 IgnoreResults = true;
183 return;
184 }
185 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000186 break;
187 }
188 case Decl::ObjCProtocol:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000189 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000190 break;
191 }
192}
193
194void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
195 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000196 GenObjCProperty(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000197}
198
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000199void USRGenerator::VisitTagDecl(TagDecl *D) {
200 VisitDeclContext(D->getDeclContext());
201 switch (D->getTagKind()) {
202 case TagDecl::TK_struct: Out << "@S^"; break;
203 case TagDecl::TK_class: Out << "@C^"; break;
204 case TagDecl::TK_union: Out << "@U^"; break;
205 case TagDecl::TK_enum: Out << "@E^"; break;
206 }
Ted Kremenek896b70f2010-03-13 02:50:34 +0000207
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000208 // FIXME: Better support for anonymous structures and enums.
209 const std::string &s = D->getNameAsString();
210 if (s.empty()) {
211 if (TypedefDecl *TD = D->getTypedefForAnonDecl())
Ted Kremenek896b70f2010-03-13 02:50:34 +0000212 Out << "^anontd^" << TD->getNameAsString();
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000213 else
214 Out << "^anon";
215 }
216 else
217 Out << s;
218}
219
Ted Kremenekc50277f2010-01-12 23:33:42 +0000220void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
221 DeclContext *DC = D->getDeclContext();
222 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Ted Kremenek896b70f2010-03-13 02:50:34 +0000223 Visit(DCN);
Ted Kremenekc50277f2010-01-12 23:33:42 +0000224 Out << "typedef@" << D->getName();
225}
226
Ted Kremenek896b70f2010-03-13 02:50:34 +0000227//===----------------------------------------------------------------------===//
228// General purpose USR generation methods.
229//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000230
Ted Kremenek896b70f2010-03-13 02:50:34 +0000231void USRGenerator::GenNamedDecl(llvm::StringRef name) {
232 Out << "@^" << name;
233}
234
235void USRGenerator::GenObjCClass(llvm::StringRef cls) {
236 Out << "objc(cs)" << cls;
237}
238
239void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) {
240 Out << "objc(cy)" << cls << '^' << cat;
241}
242
243void USRGenerator::GenObjCIvar(llvm::StringRef ivar) {
244 GenNamedDecl(ivar);
245}
246
247void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) {
248 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
249}
250
251void USRGenerator::GenObjCProperty(llvm::StringRef prop) {
252 Out << "(py)" << prop;
253}
254
255void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
256 Out << "objc(pl)" << prot;
257}
258
259//===----------------------------------------------------------------------===//
260// API hooks.
261//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000262
Benjamin Kramercfb51b62010-04-08 15:54:07 +0000263static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) {
264 return s.startswith("c:") ? s.substr(2) : "";
265}
266
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000267static CXString getDeclCursorUSR(const CXCursor &C) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000268 Decl *D = cxcursor::getCursorDecl(C);
269 if (!D)
270 return createCXString(NULL);
271
272 StringUSRGenerator SUG;
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000273 SUG->Visit(D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000274
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000275 if (SUG->ignoreResults())
Ted Kremenekebfa3392010-03-19 20:39:03 +0000276 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000277
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000278 // Return a copy of the string that must be disposed by the caller.
Ted Kremenek896b70f2010-03-13 02:50:34 +0000279 return createCXString(SUG.str(), true);
280}
281
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000282extern "C" {
283
284CXString clang_getCursorUSR(CXCursor C) {
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000285 const CXCursorKind &K = clang_getCursorKind(C);
286
287 if (clang_isDeclaration(K))
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000288 return getDeclCursorUSR(C);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000289
290 if (K == CXCursor_MacroDefinition) {
291 StringUSRGenerator SUG;
292 SUG << "macro@"
293 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
294 return createCXString(SUG.str(), true);
295 }
296
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000297 return createCXString("");
298}
299
Ted Kremenek896b70f2010-03-13 02:50:34 +0000300CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
301 StringUSRGenerator SUG;
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000302 SUG << extractUSRSuffix(clang_getCString(classUSR));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000303 SUG->GenObjCIvar(name);
304 return createCXString(SUG.str(), true);
305}
306
307CXString clang_constructUSR_ObjCMethod(const char *name,
308 unsigned isInstanceMethod,
309 CXString classUSR) {
310 StringUSRGenerator SUG;
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000311 SUG << extractUSRSuffix(clang_getCString(classUSR));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000312 SUG->GenObjCMethod(name, isInstanceMethod);
313 return createCXString(SUG.str(), true);
314}
315
316CXString clang_constructUSR_ObjCClass(const char *name) {
317 StringUSRGenerator SUG;
318 SUG->GenObjCClass(name);
319 return createCXString(SUG.str(), true);
320}
321
322CXString clang_constructUSR_ObjCProtocol(const char *name) {
323 StringUSRGenerator SUG;
324 SUG->GenObjCProtocol(name);
325 return createCXString(SUG.str(), true);
326}
327
Ted Kremenek66ccaec2010-03-15 17:38:58 +0000328CXString clang_constructUSR_ObjCCategory(const char *class_name,
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000329 const char *category_name) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000330 StringUSRGenerator SUG;
331 SUG->GenObjCCategory(class_name, category_name);
332 return createCXString(SUG.str(), true);
333}
334
335CXString clang_constructUSR_ObjCProperty(const char *property,
336 CXString classUSR) {
337 StringUSRGenerator SUG;
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000338 SUG << extractUSRSuffix(clang_getCString(classUSR));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000339 SUG->GenObjCProperty(property);
340 return createCXString(SUG.str(), true);
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000341}
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000342
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000343} // end extern "C"