Ted Kremenek | 1b6869a | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 1 | //===- 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 Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 15 | #include "CXCursor.h" |
Benjamin Kramer | 9895c6a | 2010-01-12 11:32:40 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclVisitor.h" |
Ted Kremenek | 8776382 | 2010-01-12 02:07:58 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 9895c6a | 2010-01-12 11:32:40 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 1b6869a | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 19 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 20 | using namespace clang::cxstring; |
| 21 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 22 | //===----------------------------------------------------------------------===// |
| 23 | // USR generation. |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | namespace { |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 27 | class USRGenerator : public DeclVisitor<USRGenerator> { |
| 28 | llvm::raw_ostream &Out; |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 29 | bool IgnoreResults; |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 30 | public: |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 31 | USRGenerator(llvm::raw_ostream &out) : Out(out), IgnoreResults(false) {} |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 32 | |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 33 | bool ignoreResults() const { return IgnoreResults; } |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 34 | |
| 35 | // Visitation methods from generating USRs from AST elements. |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 36 | void VisitBlockDecl(BlockDecl *D); |
| 37 | void VisitDeclContext(DeclContext *D); |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 38 | void VisitFieldDecl(FieldDecl *D); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 39 | void VisitFunctionDecl(FunctionDecl *D); |
| 40 | void VisitNamedDecl(NamedDecl *D); |
| 41 | void VisitNamespaceDecl(NamespaceDecl *D); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 42 | void VisitObjCContainerDecl(ObjCContainerDecl *CD); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 43 | void VisitObjCMethodDecl(ObjCMethodDecl *MD); |
| 44 | void VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
Ted Kremenek | b82b3be | 2010-01-18 22:42:20 +0000 | [diff] [blame] | 45 | void VisitTagDecl(TagDecl *D); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 46 | void VisitTypedefDecl(TypedefDecl *D); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 47 | |
| 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 Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 73 | }; |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 74 | |
| 75 | class StringUSRGenerator { |
| 76 | private: |
| 77 | llvm::SmallString<1024> StrBuf; |
| 78 | llvm::raw_svector_ostream Out; |
| 79 | USRGenerator UG; |
| 80 | public: |
| 81 | StringUSRGenerator() |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 82 | : Out(StrBuf), UG(Out) { |
| 83 | // Add the USR space prefix. |
| 84 | Out << "c:"; |
| 85 | } |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 86 | |
| 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 Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 100 | } // end anonymous namespace |
| 101 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 102 | //===----------------------------------------------------------------------===// |
| 103 | // Generating USRs from ASTS. |
| 104 | //===----------------------------------------------------------------------===// |
| 105 | |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 106 | void USRGenerator::VisitBlockDecl(BlockDecl *D) { |
| 107 | VisitDeclContext(D->getDeclContext()); |
| 108 | // FIXME: Better support for anonymous blocks. |
| 109 | Out << "@B^anon"; |
| 110 | } |
| 111 | |
| 112 | void USRGenerator::VisitDeclContext(DeclContext *DC) { |
| 113 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) |
| 114 | Visit(D); |
| 115 | } |
| 116 | |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 117 | void 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 Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 128 | void USRGenerator::VisitFunctionDecl(FunctionDecl *D) { |
| 129 | VisitDeclContext(D->getDeclContext()); |
| 130 | Out << "@F^" << D->getNameAsString(); |
| 131 | } |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 132 | |
| 133 | void USRGenerator::VisitNamedDecl(NamedDecl *D) { |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 134 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 135 | const std::string &s = D->getNameAsString(); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 136 | // assert(!s.empty()); |
| 137 | GenNamedDecl(s); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) { |
| 141 | VisitDeclContext(D->getDeclContext()); |
| 142 | Out << "@N^" << D->getNameAsString(); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 145 | void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 146 | Visit(cast<Decl>(D->getDeclContext())); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 147 | GenObjCMethod(DeclarationName(D->getSelector()).getAsString(), |
| 148 | D->isInstanceMethod()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void 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 Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 157 | GenObjCClass(D->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 158 | break; |
| 159 | case Decl::ObjCCategory: { |
| 160 | ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D); |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 161 | 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 Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 171 | break; |
| 172 | } |
| 173 | case Decl::ObjCCategoryImpl: { |
| 174 | ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D); |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 175 | 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 Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 185 | break; |
| 186 | } |
| 187 | case Decl::ObjCProtocol: |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 188 | GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 189 | break; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 194 | Visit(cast<Decl>(D->getDeclContext())); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 195 | GenObjCProperty(D->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Ted Kremenek | b82b3be | 2010-01-18 22:42:20 +0000 | [diff] [blame] | 198 | void 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 Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 206 | |
Ted Kremenek | c5b48b3 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 207 | // 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 Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 211 | Out << "^anontd^" << TD->getNameAsString(); |
Ted Kremenek | c5b48b3 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 212 | else |
| 213 | Out << "^anon"; |
| 214 | } |
| 215 | else |
| 216 | Out << s; |
| 217 | } |
| 218 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 219 | void USRGenerator::VisitTypedefDecl(TypedefDecl *D) { |
| 220 | DeclContext *DC = D->getDeclContext(); |
| 221 | if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC)) |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 222 | Visit(DCN); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 223 | Out << "typedef@" << D->getName(); |
| 224 | } |
| 225 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 226 | //===----------------------------------------------------------------------===// |
| 227 | // General purpose USR generation methods. |
| 228 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 229 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 230 | void USRGenerator::GenNamedDecl(llvm::StringRef name) { |
| 231 | Out << "@^" << name; |
| 232 | } |
| 233 | |
| 234 | void USRGenerator::GenObjCClass(llvm::StringRef cls) { |
| 235 | Out << "objc(cs)" << cls; |
| 236 | } |
| 237 | |
| 238 | void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) { |
| 239 | Out << "objc(cy)" << cls << '^' << cat; |
| 240 | } |
| 241 | |
| 242 | void USRGenerator::GenObjCIvar(llvm::StringRef ivar) { |
| 243 | GenNamedDecl(ivar); |
| 244 | } |
| 245 | |
| 246 | void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) { |
| 247 | Out << (isInstanceMethod ? "(im)" : "(cm)") << meth; |
| 248 | } |
| 249 | |
| 250 | void USRGenerator::GenObjCProperty(llvm::StringRef prop) { |
| 251 | Out << "(py)" << prop; |
| 252 | } |
| 253 | |
| 254 | void USRGenerator::GenObjCProtocol(llvm::StringRef prot) { |
| 255 | Out << "objc(pl)" << prot; |
| 256 | } |
| 257 | |
| 258 | //===----------------------------------------------------------------------===// |
| 259 | // API hooks. |
| 260 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 261 | |
Benjamin Kramer | cfb51b6 | 2010-04-08 15:54:07 +0000 | [diff] [blame^] | 262 | static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) { |
| 263 | return s.startswith("c:") ? s.substr(2) : ""; |
| 264 | } |
| 265 | |
Benjamin Kramer | 9895c6a | 2010-01-12 11:32:40 +0000 | [diff] [blame] | 266 | extern "C" { |
| 267 | |
Ted Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 268 | CXString clang_getCursorUSR(CXCursor C) { |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 269 | 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 Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 276 | if (SUG->ignoreResults()) |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 277 | return createCXString(""); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 278 | |
| 279 | // Return a copy of the string that must be disposed by the caller. |
| 280 | return createCXString(SUG.str(), true); |
| 281 | } |
| 282 | |
| 283 | CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) { |
| 284 | StringUSRGenerator SUG; |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 285 | SUG << extractUSRSuffix(clang_getCString(classUSR)); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 286 | SUG->GenObjCIvar(name); |
| 287 | return createCXString(SUG.str(), true); |
| 288 | } |
| 289 | |
| 290 | CXString clang_constructUSR_ObjCMethod(const char *name, |
| 291 | unsigned isInstanceMethod, |
| 292 | CXString classUSR) { |
| 293 | StringUSRGenerator SUG; |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 294 | SUG << extractUSRSuffix(clang_getCString(classUSR)); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 295 | SUG->GenObjCMethod(name, isInstanceMethod); |
| 296 | return createCXString(SUG.str(), true); |
| 297 | } |
| 298 | |
| 299 | CXString clang_constructUSR_ObjCClass(const char *name) { |
| 300 | StringUSRGenerator SUG; |
| 301 | SUG->GenObjCClass(name); |
| 302 | return createCXString(SUG.str(), true); |
| 303 | } |
| 304 | |
| 305 | CXString clang_constructUSR_ObjCProtocol(const char *name) { |
| 306 | StringUSRGenerator SUG; |
| 307 | SUG->GenObjCProtocol(name); |
| 308 | return createCXString(SUG.str(), true); |
| 309 | } |
| 310 | |
Ted Kremenek | 66ccaec | 2010-03-15 17:38:58 +0000 | [diff] [blame] | 311 | CXString clang_constructUSR_ObjCCategory(const char *class_name, |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 312 | const char *category_name) { |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 313 | StringUSRGenerator SUG; |
| 314 | SUG->GenObjCCategory(class_name, category_name); |
| 315 | return createCXString(SUG.str(), true); |
| 316 | } |
| 317 | |
| 318 | CXString clang_constructUSR_ObjCProperty(const char *property, |
| 319 | CXString classUSR) { |
| 320 | StringUSRGenerator SUG; |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 321 | SUG << extractUSRSuffix(clang_getCString(classUSR)); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 322 | SUG->GenObjCProperty(property); |
| 323 | return createCXString(SUG.str(), true); |
Ted Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 324 | } |
Ted Kremenek | 1b6869a | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 325 | |
Ted Kremenek | 1b6869a | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 326 | } // end extern "C" |