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 | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/ASTUnit.h" |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 18 | #include "clang/Lex/PreprocessingRecord.h" |
Ted Kremenek | 8776382 | 2010-01-12 02:07:58 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 9895c6a | 2010-01-12 11:32:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 21 | |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 22 | using namespace clang; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 23 | using namespace clang::cxstring; |
| 24 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | // USR generation. |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | namespace { |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 30 | class USRGenerator : public DeclVisitor<USRGenerator> { |
| 31 | llvm::raw_ostream &Out; |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 32 | bool IgnoreResults; |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 33 | ASTUnit *AU; |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 34 | public: |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 35 | USRGenerator(ASTUnit *au, llvm::raw_ostream &out) |
| 36 | : Out(out), IgnoreResults(false), AU(au) {} |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 37 | |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 38 | bool ignoreResults() const { return IgnoreResults; } |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 39 | |
| 40 | // Visitation methods from generating USRs from AST elements. |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 41 | void VisitBlockDecl(BlockDecl *D); |
| 42 | void VisitDeclContext(DeclContext *D); |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 43 | void VisitFieldDecl(FieldDecl *D); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 44 | void VisitFunctionDecl(FunctionDecl *D); |
| 45 | void VisitNamedDecl(NamedDecl *D); |
| 46 | void VisitNamespaceDecl(NamespaceDecl *D); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 47 | void VisitObjCClassDecl(ObjCClassDecl *CD); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 48 | void VisitObjCContainerDecl(ObjCContainerDecl *CD); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 49 | void VisitObjCMethodDecl(ObjCMethodDecl *MD); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 50 | void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *P); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 51 | void VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
Ted Kremenek | b82b3be | 2010-01-18 22:42:20 +0000 | [diff] [blame] | 52 | void VisitTagDecl(TagDecl *D); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 53 | void VisitTypedefDecl(TypedefDecl *D); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 54 | |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 55 | /// Generate the string component containing the location of the |
| 56 | /// declaration. |
| 57 | void GenLoc(const Decl *D); |
| 58 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 59 | /// String generation methods used both by the visitation methods |
| 60 | /// and from other clients that want to directly generate USRs. These |
| 61 | /// methods do not construct complete USRs (which incorporate the parents |
| 62 | /// of an AST element), but only the fragments concerning the AST element |
| 63 | /// itself. |
| 64 | |
| 65 | /// Generate a USR fragment for a named declaration. This does |
| 66 | /// not include the USR component for the parent. |
| 67 | void GenNamedDecl(llvm::StringRef name); |
| 68 | |
| 69 | /// Generate a USR for an Objective-C class. |
| 70 | void GenObjCClass(llvm::StringRef cls); |
| 71 | /// Generate a USR for an Objective-C class category. |
| 72 | void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat); |
| 73 | /// Generate a USR fragment for an Objective-C instance variable. The |
| 74 | /// complete USR can be created by concatenating the USR for the |
| 75 | /// encompassing class with this USR fragment. |
| 76 | void GenObjCIvar(llvm::StringRef ivar); |
| 77 | /// Generate a USR fragment for an Objective-C method. |
| 78 | void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod); |
| 79 | /// Generate a USR fragment for an Objective-C property. |
| 80 | void GenObjCProperty(llvm::StringRef prop); |
| 81 | /// Generate a USR for an Objective-C protocol. |
| 82 | void GenObjCProtocol(llvm::StringRef prot); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 83 | }; |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 84 | |
| 85 | class StringUSRGenerator { |
| 86 | private: |
| 87 | llvm::SmallString<1024> StrBuf; |
| 88 | llvm::raw_svector_ostream Out; |
| 89 | USRGenerator UG; |
| 90 | public: |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 91 | StringUSRGenerator(const CXCursor *C = 0) |
| 92 | : Out(StrBuf), UG(C ? cxcursor::getCursorASTUnit(*C) : 0, Out) { |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 93 | // Add the USR space prefix. |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 94 | Out << "c:"; |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 95 | } |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 96 | |
| 97 | llvm::StringRef str() { |
| 98 | return Out.str(); |
| 99 | } |
| 100 | |
| 101 | USRGenerator* operator->() { return &UG; } |
| 102 | |
| 103 | template <typename T> |
| 104 | llvm::raw_svector_ostream &operator<<(const T &x) { |
| 105 | Out << x; |
| 106 | return Out; |
| 107 | } |
| 108 | }; |
| 109 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 110 | } // end anonymous namespace |
| 111 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 112 | //===----------------------------------------------------------------------===// |
| 113 | // Generating USRs from ASTS. |
| 114 | //===----------------------------------------------------------------------===// |
| 115 | |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 116 | void USRGenerator::VisitBlockDecl(BlockDecl *D) { |
| 117 | VisitDeclContext(D->getDeclContext()); |
| 118 | // FIXME: Better support for anonymous blocks. |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 119 | Out << "@B@anon"; |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void USRGenerator::VisitDeclContext(DeclContext *DC) { |
| 123 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) |
| 124 | Visit(D); |
| 125 | } |
| 126 | |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 127 | void USRGenerator::VisitFieldDecl(FieldDecl *D) { |
| 128 | const std::string &s = D->getNameAsString(); |
| 129 | if (s.empty()) { |
| 130 | // Bit fields can be anonymous. |
| 131 | IgnoreResults = true; |
| 132 | return; |
| 133 | } |
| 134 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 135 | Out << "@FI@" << s; |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 138 | void USRGenerator::VisitFunctionDecl(FunctionDecl *D) { |
| 139 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 140 | Out << "@F@" << D->getNameAsString(); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 141 | } |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 142 | |
| 143 | void USRGenerator::VisitNamedDecl(NamedDecl *D) { |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 144 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 145 | const std::string &s = D->getNameAsString(); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 146 | // The string can be empty if the declaration has no name; e.g., it is |
| 147 | // the ParmDecl with no name for declaration of a function pointer type, e.g.: |
| 148 | // void (*f)(void *); |
| 149 | // In this case, don't generate a USR. |
| 150 | if (s.empty()) |
| 151 | IgnoreResults = true; |
| 152 | else |
| 153 | GenNamedDecl(s); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) { |
| 157 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 158 | Out << "@N@" << D->getNameAsString(); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 161 | void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 162 | Visit(cast<Decl>(D->getDeclContext())); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 163 | GenObjCMethod(DeclarationName(D->getSelector()).getAsString(), |
| 164 | D->isInstanceMethod()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 167 | void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) { |
| 168 | // FIXME: @class declarations can refer to multiple classes. We need |
| 169 | // to be able to traverse these. |
| 170 | IgnoreResults = true; |
| 171 | } |
| 172 | |
| 173 | void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 174 | // FIXME: @protocol declarations can refer to multiple protocols. We need |
| 175 | // to be able to traverse these. |
| 176 | IgnoreResults = true; |
| 177 | } |
| 178 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 179 | void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
| 180 | switch (D->getKind()) { |
| 181 | default: |
| 182 | assert(false && "Invalid ObjC container."); |
| 183 | case Decl::ObjCInterface: |
| 184 | case Decl::ObjCImplementation: |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 185 | GenObjCClass(D->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 186 | break; |
| 187 | case Decl::ObjCCategory: { |
| 188 | ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D); |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 189 | ObjCInterfaceDecl *ID = CD->getClassInterface(); |
| 190 | if (!ID) { |
| 191 | // Handle invalid code where the @interface might not |
| 192 | // have been specified. |
| 193 | // FIXME: We should be able to generate this USR even if the |
| 194 | // @interface isn't available. |
| 195 | IgnoreResults = true; |
| 196 | return; |
| 197 | } |
| 198 | GenObjCCategory(ID->getName(), CD->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 199 | break; |
| 200 | } |
| 201 | case Decl::ObjCCategoryImpl: { |
| 202 | ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D); |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 203 | ObjCInterfaceDecl *ID = CD->getClassInterface(); |
| 204 | if (!ID) { |
| 205 | // Handle invalid code where the @interface might not |
| 206 | // have been specified. |
| 207 | // FIXME: We should be able to generate this USR even if the |
| 208 | // @interface isn't available. |
| 209 | IgnoreResults = true; |
| 210 | return; |
| 211 | } |
| 212 | GenObjCCategory(ID->getName(), CD->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 213 | break; |
| 214 | } |
| 215 | case Decl::ObjCProtocol: |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 216 | GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 217 | break; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 222 | Visit(cast<Decl>(D->getDeclContext())); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 223 | GenObjCProperty(D->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Ted Kremenek | b82b3be | 2010-01-18 22:42:20 +0000 | [diff] [blame] | 226 | void USRGenerator::VisitTagDecl(TagDecl *D) { |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 227 | D = D->getCanonicalDecl(); |
Ted Kremenek | b82b3be | 2010-01-18 22:42:20 +0000 | [diff] [blame] | 228 | VisitDeclContext(D->getDeclContext()); |
| 229 | switch (D->getTagKind()) { |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 230 | case TagDecl::TK_struct: Out << "@S"; break; |
| 231 | case TagDecl::TK_class: Out << "@C"; break; |
| 232 | case TagDecl::TK_union: Out << "@U"; break; |
| 233 | case TagDecl::TK_enum: Out << "@E"; break; |
| 234 | } |
| 235 | |
| 236 | const std::string &s = D->getNameAsString(); |
| 237 | const TypedefDecl *TD = 0; |
| 238 | if (s.empty()) { |
| 239 | TD = D->getTypedefForAnonDecl(); |
| 240 | Out << (TD ? 'A' : 'a'); |
Ted Kremenek | b82b3be | 2010-01-18 22:42:20 +0000 | [diff] [blame] | 241 | } |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 242 | |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 243 | // Add the location of the tag decl to handle resolution across |
| 244 | // translation units. |
| 245 | if (D->getLinkage() == NoLinkage) { |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 246 | Out << '@'; |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 247 | GenLoc(D); |
| 248 | if (IgnoreResults) |
| 249 | return; |
| 250 | } |
| 251 | |
Ted Kremenek | c5b48b3 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 252 | if (s.empty()) { |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 253 | if (TD) |
| 254 | Out << '@' << TD->getNameAsString(); |
Ted Kremenek | c5b48b3 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 255 | } |
| 256 | else |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 257 | Out << '@' << s; |
Ted Kremenek | c5b48b3 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 260 | void USRGenerator::VisitTypedefDecl(TypedefDecl *D) { |
| 261 | DeclContext *DC = D->getDeclContext(); |
| 262 | if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC)) |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 263 | Visit(DCN); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 264 | Out << "@T@"; |
| 265 | if (D->getLinkage() == NoLinkage) { |
| 266 | GenLoc(D); |
| 267 | if (IgnoreResults) |
| 268 | return; |
| 269 | Out << '@'; |
| 270 | } |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 271 | Out << D->getName(); |
| 272 | } |
| 273 | |
| 274 | void USRGenerator::GenLoc(const Decl *D) { |
| 275 | const SourceManager &SM = AU->getSourceManager(); |
| 276 | SourceLocation L = D->getLocStart(); |
| 277 | if (L.isInvalid()) { |
| 278 | IgnoreResults = true; |
| 279 | return; |
| 280 | } |
| 281 | L = SM.getInstantiationLoc(L); |
| 282 | const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L); |
| 283 | const FileEntry *FE = SM.getFileEntryForID(Decomposed.first); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 284 | if (FE) { |
| 285 | llvm::sys::Path P(FE->getName()); |
| 286 | Out << P.getLast(); |
| 287 | } |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 288 | else { |
| 289 | // This case really isn't interesting. |
| 290 | IgnoreResults = true; |
| 291 | return; |
| 292 | } |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 293 | Out << '@' |
| 294 | << SM.getLineNumber(Decomposed.first, Decomposed.second) << ':' |
| 295 | << SM.getColumnNumber(Decomposed.first, Decomposed.second); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 298 | //===----------------------------------------------------------------------===// |
| 299 | // General purpose USR generation methods. |
| 300 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 301 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 302 | void USRGenerator::GenNamedDecl(llvm::StringRef name) { |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 303 | Out << "@" << name; |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void USRGenerator::GenObjCClass(llvm::StringRef cls) { |
| 307 | Out << "objc(cs)" << cls; |
| 308 | } |
| 309 | |
| 310 | void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) { |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 311 | Out << "objc(cy)" << cls << '@' << cat; |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void USRGenerator::GenObjCIvar(llvm::StringRef ivar) { |
| 315 | GenNamedDecl(ivar); |
| 316 | } |
| 317 | |
| 318 | void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) { |
| 319 | Out << (isInstanceMethod ? "(im)" : "(cm)") << meth; |
| 320 | } |
| 321 | |
| 322 | void USRGenerator::GenObjCProperty(llvm::StringRef prop) { |
| 323 | Out << "(py)" << prop; |
| 324 | } |
| 325 | |
| 326 | void USRGenerator::GenObjCProtocol(llvm::StringRef prot) { |
| 327 | Out << "objc(pl)" << prot; |
| 328 | } |
| 329 | |
| 330 | //===----------------------------------------------------------------------===// |
| 331 | // API hooks. |
| 332 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 333 | |
Benjamin Kramer | cfb51b6 | 2010-04-08 15:54:07 +0000 | [diff] [blame] | 334 | static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) { |
| 335 | return s.startswith("c:") ? s.substr(2) : ""; |
| 336 | } |
| 337 | |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 338 | static CXString getDeclCursorUSR(const CXCursor &C) { |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 339 | Decl *D = cxcursor::getCursorDecl(C); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 340 | |
| 341 | // Don't generate USRs for things with invalid locations. |
| 342 | if (!D || D->getLocStart().isInvalid()) |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 343 | return createCXString(NULL); |
| 344 | |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 345 | // Check if the cursor has 'NoLinkage'. |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 346 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 347 | switch (ND->getLinkage()) { |
| 348 | case ExternalLinkage: |
| 349 | // Generate USRs for all entities with external linkage. |
| 350 | break; |
| 351 | case NoLinkage: |
| 352 | // We allow enums, typedefs, and structs that have no linkage to |
| 353 | // have USRs that are anchored to the file they were defined in |
| 354 | // (e.g., the header). This is a little gross, but in principal |
| 355 | // enums/anonymous structs/etc. defined in a common header file |
| 356 | // are referred to across multiple translation units. |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 357 | if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) || |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 358 | isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND)) |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 359 | break; |
| 360 | // Fall-through. |
| 361 | case InternalLinkage: |
| 362 | case UniqueExternalLinkage: |
| 363 | return createCXString(""); |
| 364 | } |
| 365 | |
| 366 | StringUSRGenerator SUG(&C); |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 367 | SUG->Visit(D); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 368 | |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 369 | if (SUG->ignoreResults()) |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 370 | return createCXString(""); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 371 | |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 372 | assert(SUG.str().size() > 3); |
| 373 | |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 374 | // Return a copy of the string that must be disposed by the caller. |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 375 | return createCXString(SUG.str(), true); |
| 376 | } |
| 377 | |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 378 | extern "C" { |
| 379 | |
| 380 | CXString clang_getCursorUSR(CXCursor C) { |
Ted Kremenek | fa8231d | 2010-04-11 22:20:34 +0000 | [diff] [blame] | 381 | const CXCursorKind &K = clang_getCursorKind(C); |
| 382 | |
| 383 | if (clang_isDeclaration(K)) |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 384 | return getDeclCursorUSR(C); |
Ted Kremenek | fa8231d | 2010-04-11 22:20:34 +0000 | [diff] [blame] | 385 | |
| 386 | if (K == CXCursor_MacroDefinition) { |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 387 | StringUSRGenerator SUG(&C); |
Ted Kremenek | fa8231d | 2010-04-11 22:20:34 +0000 | [diff] [blame] | 388 | SUG << "macro@" |
| 389 | << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart(); |
| 390 | return createCXString(SUG.str(), true); |
| 391 | } |
| 392 | |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 393 | return createCXString(""); |
| 394 | } |
| 395 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 396 | CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) { |
| 397 | StringUSRGenerator SUG; |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 398 | SUG << extractUSRSuffix(clang_getCString(classUSR)); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 399 | SUG->GenObjCIvar(name); |
| 400 | return createCXString(SUG.str(), true); |
| 401 | } |
| 402 | |
| 403 | CXString clang_constructUSR_ObjCMethod(const char *name, |
| 404 | unsigned isInstanceMethod, |
| 405 | CXString classUSR) { |
| 406 | StringUSRGenerator SUG; |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 407 | SUG << extractUSRSuffix(clang_getCString(classUSR)); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 408 | SUG->GenObjCMethod(name, isInstanceMethod); |
| 409 | return createCXString(SUG.str(), true); |
| 410 | } |
| 411 | |
| 412 | CXString clang_constructUSR_ObjCClass(const char *name) { |
| 413 | StringUSRGenerator SUG; |
| 414 | SUG->GenObjCClass(name); |
| 415 | return createCXString(SUG.str(), true); |
| 416 | } |
| 417 | |
| 418 | CXString clang_constructUSR_ObjCProtocol(const char *name) { |
| 419 | StringUSRGenerator SUG; |
| 420 | SUG->GenObjCProtocol(name); |
| 421 | return createCXString(SUG.str(), true); |
| 422 | } |
| 423 | |
Ted Kremenek | 66ccaec | 2010-03-15 17:38:58 +0000 | [diff] [blame] | 424 | CXString clang_constructUSR_ObjCCategory(const char *class_name, |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 425 | const char *category_name) { |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 426 | StringUSRGenerator SUG; |
| 427 | SUG->GenObjCCategory(class_name, category_name); |
| 428 | return createCXString(SUG.str(), true); |
| 429 | } |
| 430 | |
| 431 | CXString clang_constructUSR_ObjCProperty(const char *property, |
| 432 | CXString classUSR) { |
| 433 | StringUSRGenerator SUG; |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 434 | SUG << extractUSRSuffix(clang_getCString(classUSR)); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 435 | SUG->GenObjCProperty(property); |
| 436 | return createCXString(SUG.str(), true); |
Ted Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 437 | } |
Ted Kremenek | 1b6869a | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 438 | |
Ted Kremenek | 1b6869a | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 439 | } // end extern "C" |