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