blob: e90d965a1cd31674fa4835864d986a462dd807e3 [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 Kremenek87763822010-01-12 02:07:58 +000017#include "llvm/ADT/SmallString.h"
Benjamin Kramer9895c6a2010-01-12 11:32:40 +000018#include "llvm/Support/raw_ostream.h"
Ted Kremenek1b6869a2010-01-05 22:06:45 +000019
Ted Kremenekee4db4f2010-02-17 00:41:08 +000020using namespace clang::cxstring;
21
Ted Kremenekc50277f2010-01-12 23:33:42 +000022//===----------------------------------------------------------------------===//
23// USR generation.
24//===----------------------------------------------------------------------===//
25
26namespace {
Ted Kremenek2fee4e62010-01-14 01:50:21 +000027class USRGenerator : public DeclVisitor<USRGenerator> {
28 llvm::raw_ostream &Out;
Ted Kremenek3adca6d2010-01-18 22:02:49 +000029 bool IgnoreResults;
Ted Kremenek2fee4e62010-01-14 01:50:21 +000030public:
Ted Kremenek3adca6d2010-01-18 22:02:49 +000031 USRGenerator(llvm::raw_ostream &out) : Out(out), IgnoreResults(false) {}
Ted Kremenek896b70f2010-03-13 02:50:34 +000032
Ted Kremenek3adca6d2010-01-18 22:02:49 +000033 bool ignoreResults() const { return IgnoreResults; }
Ted Kremenek896b70f2010-03-13 02:50:34 +000034
35 // Visitation methods from generating USRs from AST elements.
Ted Kremenek2fee4e62010-01-14 01:50:21 +000036 void VisitBlockDecl(BlockDecl *D);
37 void VisitDeclContext(DeclContext *D);
Ted Kremenek3adca6d2010-01-18 22:02:49 +000038 void VisitFieldDecl(FieldDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000039 void VisitFunctionDecl(FunctionDecl *D);
40 void VisitNamedDecl(NamedDecl *D);
41 void VisitNamespaceDecl(NamespaceDecl *D);
Ted Kremenek896b70f2010-03-13 02:50:34 +000042 void VisitObjCContainerDecl(ObjCContainerDecl *CD);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000043 void VisitObjCMethodDecl(ObjCMethodDecl *MD);
44 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Ted Kremenekb82b3be2010-01-18 22:42:20 +000045 void VisitTagDecl(TagDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000046 void VisitTypedefDecl(TypedefDecl *D);
Ted Kremenek896b70f2010-03-13 02:50:34 +000047
48
49 /// String generation methods used both by the visitation methods
50 /// and from other clients that want to directly generate USRs. These
51 /// methods do not construct complete USRs (which incorporate the parents
52 /// of an AST element), but only the fragments concerning the AST element
53 /// itself.
54
55 /// Generate a USR fragment for a named declaration. This does
56 /// not include the USR component for the parent.
57 void GenNamedDecl(llvm::StringRef name);
58
59 /// Generate a USR for an Objective-C class.
60 void GenObjCClass(llvm::StringRef cls);
61 /// Generate a USR for an Objective-C class category.
62 void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat);
63 /// Generate a USR fragment for an Objective-C instance variable. The
64 /// complete USR can be created by concatenating the USR for the
65 /// encompassing class with this USR fragment.
66 void GenObjCIvar(llvm::StringRef ivar);
67 /// Generate a USR fragment for an Objective-C method.
68 void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod);
69 /// Generate a USR fragment for an Objective-C property.
70 void GenObjCProperty(llvm::StringRef prop);
71 /// Generate a USR for an Objective-C protocol.
72 void GenObjCProtocol(llvm::StringRef prot);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000073};
Ted Kremenek896b70f2010-03-13 02:50:34 +000074
75class StringUSRGenerator {
76private:
77 llvm::SmallString<1024> StrBuf;
78 llvm::raw_svector_ostream Out;
79 USRGenerator UG;
80public:
81 StringUSRGenerator()
Ted Kremenek0c0fb412010-03-25 02:00:36 +000082 : Out(StrBuf), UG(Out) {
83 // Add the USR space prefix.
84 Out << "c:";
85 }
Ted Kremenek896b70f2010-03-13 02:50:34 +000086
87 llvm::StringRef str() {
88 return Out.str();
89 }
90
91 USRGenerator* operator->() { return &UG; }
92
93 template <typename T>
94 llvm::raw_svector_ostream &operator<<(const T &x) {
95 Out << x;
96 return Out;
97 }
98};
99
Ted Kremenekc50277f2010-01-12 23:33:42 +0000100} // end anonymous namespace
101
Ted Kremenek896b70f2010-03-13 02:50:34 +0000102//===----------------------------------------------------------------------===//
103// Generating USRs from ASTS.
104//===----------------------------------------------------------------------===//
105
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000106void USRGenerator::VisitBlockDecl(BlockDecl *D) {
107 VisitDeclContext(D->getDeclContext());
108 // FIXME: Better support for anonymous blocks.
109 Out << "@B^anon";
110}
111
112void USRGenerator::VisitDeclContext(DeclContext *DC) {
113 if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
114 Visit(D);
115}
116
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000117void USRGenerator::VisitFieldDecl(FieldDecl *D) {
118 const std::string &s = D->getNameAsString();
119 if (s.empty()) {
120 // Bit fields can be anonymous.
121 IgnoreResults = true;
122 return;
123 }
124 VisitDeclContext(D->getDeclContext());
125 Out << "@^FI^" << s;
126}
127
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000128void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
129 VisitDeclContext(D->getDeclContext());
130 Out << "@F^" << D->getNameAsString();
131}
Ted Kremenekc50277f2010-01-12 23:33:42 +0000132
133void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000134 VisitDeclContext(D->getDeclContext());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000135 const std::string &s = D->getNameAsString();
Ted Kremenek896b70f2010-03-13 02:50:34 +0000136 // assert(!s.empty());
137 GenNamedDecl(s);
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000138}
139
140void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
141 VisitDeclContext(D->getDeclContext());
142 Out << "@N^" << D->getNameAsString();
Ted Kremenekc50277f2010-01-12 23:33:42 +0000143}
144
Ted Kremenekc50277f2010-01-12 23:33:42 +0000145void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
146 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000147 GenObjCMethod(DeclarationName(D->getSelector()).getAsString(),
148 D->isInstanceMethod());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000149}
150
151void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
152 switch (D->getKind()) {
153 default:
154 assert(false && "Invalid ObjC container.");
155 case Decl::ObjCInterface:
156 case Decl::ObjCImplementation:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000157 GenObjCClass(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000158 break;
159 case Decl::ObjCCategory: {
160 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000161 ObjCInterfaceDecl *ID = CD->getClassInterface();
162 if (!ID) {
163 // Handle invalid code where the @interface might not
164 // have been specified.
165 // FIXME: We should be able to generate this USR even if the
166 // @interface isn't available.
167 IgnoreResults = true;
168 return;
169 }
170 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000171 break;
172 }
173 case Decl::ObjCCategoryImpl: {
174 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000175 ObjCInterfaceDecl *ID = CD->getClassInterface();
176 if (!ID) {
177 // Handle invalid code where the @interface might not
178 // have been specified.
179 // FIXME: We should be able to generate this USR even if the
180 // @interface isn't available.
181 IgnoreResults = true;
182 return;
183 }
184 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000185 break;
186 }
187 case Decl::ObjCProtocol:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000188 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000189 break;
190 }
191}
192
193void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
194 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000195 GenObjCProperty(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000196}
197
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000198void USRGenerator::VisitTagDecl(TagDecl *D) {
199 VisitDeclContext(D->getDeclContext());
200 switch (D->getTagKind()) {
201 case TagDecl::TK_struct: Out << "@S^"; break;
202 case TagDecl::TK_class: Out << "@C^"; break;
203 case TagDecl::TK_union: Out << "@U^"; break;
204 case TagDecl::TK_enum: Out << "@E^"; break;
205 }
Ted Kremenek896b70f2010-03-13 02:50:34 +0000206
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000207 // FIXME: Better support for anonymous structures and enums.
208 const std::string &s = D->getNameAsString();
209 if (s.empty()) {
210 if (TypedefDecl *TD = D->getTypedefForAnonDecl())
Ted Kremenek896b70f2010-03-13 02:50:34 +0000211 Out << "^anontd^" << TD->getNameAsString();
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000212 else
213 Out << "^anon";
214 }
215 else
216 Out << s;
217}
218
Ted Kremenekc50277f2010-01-12 23:33:42 +0000219void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
220 DeclContext *DC = D->getDeclContext();
221 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Ted Kremenek896b70f2010-03-13 02:50:34 +0000222 Visit(DCN);
Ted Kremenekc50277f2010-01-12 23:33:42 +0000223 Out << "typedef@" << D->getName();
224}
225
Ted Kremenek896b70f2010-03-13 02:50:34 +0000226//===----------------------------------------------------------------------===//
227// General purpose USR generation methods.
228//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000229
Ted Kremenek896b70f2010-03-13 02:50:34 +0000230void USRGenerator::GenNamedDecl(llvm::StringRef name) {
231 Out << "@^" << name;
232}
233
234void USRGenerator::GenObjCClass(llvm::StringRef cls) {
235 Out << "objc(cs)" << cls;
236}
237
238void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) {
239 Out << "objc(cy)" << cls << '^' << cat;
240}
241
242void USRGenerator::GenObjCIvar(llvm::StringRef ivar) {
243 GenNamedDecl(ivar);
244}
245
246void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) {
247 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
248}
249
250void USRGenerator::GenObjCProperty(llvm::StringRef prop) {
251 Out << "(py)" << prop;
252}
253
254void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
255 Out << "objc(pl)" << prot;
256}
257
258//===----------------------------------------------------------------------===//
259// API hooks.
260//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000261
Benjamin Kramercfb51b62010-04-08 15:54:07 +0000262static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) {
263 return s.startswith("c:") ? s.substr(2) : "";
264}
265
Benjamin Kramer9895c6a2010-01-12 11:32:40 +0000266extern "C" {
267
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000268CXString clang_getCursorUSR(CXCursor C) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000269 Decl *D = cxcursor::getCursorDecl(C);
270 if (!D)
271 return createCXString(NULL);
272
273 StringUSRGenerator SUG;
274 SUG->Visit(static_cast<Decl*>(D));
275
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000276 if (SUG->ignoreResults())
Ted Kremenekebfa3392010-03-19 20:39:03 +0000277 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000278
279 // Return a copy of the string that must be disposed by the caller.
280 return createCXString(SUG.str(), true);
281}
282
283CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
284 StringUSRGenerator SUG;
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000285 SUG << extractUSRSuffix(clang_getCString(classUSR));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000286 SUG->GenObjCIvar(name);
287 return createCXString(SUG.str(), true);
288}
289
290CXString clang_constructUSR_ObjCMethod(const char *name,
291 unsigned isInstanceMethod,
292 CXString classUSR) {
293 StringUSRGenerator SUG;
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000294 SUG << extractUSRSuffix(clang_getCString(classUSR));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000295 SUG->GenObjCMethod(name, isInstanceMethod);
296 return createCXString(SUG.str(), true);
297}
298
299CXString clang_constructUSR_ObjCClass(const char *name) {
300 StringUSRGenerator SUG;
301 SUG->GenObjCClass(name);
302 return createCXString(SUG.str(), true);
303}
304
305CXString clang_constructUSR_ObjCProtocol(const char *name) {
306 StringUSRGenerator SUG;
307 SUG->GenObjCProtocol(name);
308 return createCXString(SUG.str(), true);
309}
310
Ted Kremenek66ccaec2010-03-15 17:38:58 +0000311CXString clang_constructUSR_ObjCCategory(const char *class_name,
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000312 const char *category_name) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000313 StringUSRGenerator SUG;
314 SUG->GenObjCCategory(class_name, category_name);
315 return createCXString(SUG.str(), true);
316}
317
318CXString clang_constructUSR_ObjCProperty(const char *property,
319 CXString classUSR) {
320 StringUSRGenerator SUG;
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000321 SUG << extractUSRSuffix(clang_getCString(classUSR));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000322 SUG->GenObjCProperty(property);
323 return createCXString(SUG.str(), true);
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000324}
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000325
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000326} // end extern "C"