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