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