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" |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame^] | 16 | #include "clang/AST/DeclTemplate.h" |
Benjamin Kramer | 9895c6a | 2010-01-12 11:32:40 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclVisitor.h" |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 18 | #include "clang/Frontend/ASTUnit.h" |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 19 | #include "clang/Lex/PreprocessingRecord.h" |
Ted Kremenek | 8776382 | 2010-01-12 02:07:58 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 9895c6a | 2010-01-12 11:32:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 22 | |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 23 | using namespace clang; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 24 | using namespace clang::cxstring; |
| 25 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | // USR generation. |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | namespace { |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 31 | class USRGenerator : public DeclVisitor<USRGenerator> { |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 32 | llvm::SmallString<1024> Buf; |
| 33 | llvm::raw_svector_ostream Out; |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 34 | bool IgnoreResults; |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 35 | ASTUnit *AU; |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 36 | bool generatedLoc; |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 37 | public: |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 38 | USRGenerator(const CXCursor *C = 0) |
| 39 | : Out(Buf), |
| 40 | IgnoreResults(false), |
| 41 | AU(C ? cxcursor::getCursorASTUnit(*C) : 0), |
| 42 | generatedLoc(false) |
| 43 | { |
| 44 | // Add the USR space prefix. |
| 45 | Out << "c:"; |
| 46 | } |
| 47 | |
| 48 | llvm::StringRef str() { |
| 49 | return Out.str(); |
| 50 | } |
| 51 | |
| 52 | USRGenerator* operator->() { return this; } |
| 53 | |
| 54 | template <typename T> |
| 55 | llvm::raw_svector_ostream &operator<<(const T &x) { |
| 56 | Out << x; |
| 57 | return Out; |
| 58 | } |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 59 | |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 60 | bool ignoreResults() const { return IgnoreResults; } |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 61 | |
| 62 | // Visitation methods from generating USRs from AST elements. |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 63 | void VisitDeclContext(DeclContext *D); |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 64 | void VisitFieldDecl(FieldDecl *D); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 65 | void VisitFunctionDecl(FunctionDecl *D); |
| 66 | void VisitNamedDecl(NamedDecl *D); |
| 67 | void VisitNamespaceDecl(NamespaceDecl *D); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame^] | 68 | void VisitFunctionTemplateDecl(FunctionTemplateDecl *D); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 69 | void VisitObjCClassDecl(ObjCClassDecl *CD); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 70 | void VisitObjCContainerDecl(ObjCContainerDecl *CD); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 71 | void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *P); |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 72 | void VisitObjCMethodDecl(ObjCMethodDecl *MD); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 73 | void VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 74 | void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
Ted Kremenek | b82b3be | 2010-01-18 22:42:20 +0000 | [diff] [blame] | 75 | void VisitTagDecl(TagDecl *D); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 76 | void VisitTypedefDecl(TypedefDecl *D); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame^] | 77 | void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 78 | void VisitVarDecl(VarDecl *D); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame^] | 79 | void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
| 80 | void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
Ted Kremenek | 8e67219 | 2010-05-07 01:04:32 +0000 | [diff] [blame] | 81 | void VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 82 | IgnoreResults = true; |
| 83 | return; |
| 84 | } |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 85 | |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 86 | /// Generate the string component containing the location of the |
| 87 | /// declaration. |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 88 | bool GenLoc(const Decl *D); |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 89 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 90 | /// String generation methods used both by the visitation methods |
| 91 | /// and from other clients that want to directly generate USRs. These |
| 92 | /// methods do not construct complete USRs (which incorporate the parents |
| 93 | /// of an AST element), but only the fragments concerning the AST element |
| 94 | /// itself. |
| 95 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 96 | /// Generate a USR for an Objective-C class. |
| 97 | void GenObjCClass(llvm::StringRef cls); |
| 98 | /// Generate a USR for an Objective-C class category. |
| 99 | void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat); |
| 100 | /// Generate a USR fragment for an Objective-C instance variable. The |
| 101 | /// complete USR can be created by concatenating the USR for the |
| 102 | /// encompassing class with this USR fragment. |
| 103 | void GenObjCIvar(llvm::StringRef ivar); |
| 104 | /// Generate a USR fragment for an Objective-C method. |
| 105 | void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod); |
| 106 | /// Generate a USR fragment for an Objective-C property. |
| 107 | void GenObjCProperty(llvm::StringRef prop); |
| 108 | /// Generate a USR for an Objective-C protocol. |
| 109 | void GenObjCProtocol(llvm::StringRef prot); |
Ted Kremenek | 8e67219 | 2010-05-07 01:04:32 +0000 | [diff] [blame] | 110 | |
| 111 | void VisitType(QualType T); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame^] | 112 | void VisitTemplateParameterList(const TemplateParameterList *Params); |
| 113 | void VisitTemplateName(TemplateName Name); |
| 114 | void VisitTemplateArgument(const TemplateArgument &Arg); |
| 115 | |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 116 | /// Emit a Decl's name using NamedDecl::printName() and return true if |
| 117 | /// the decl had no name. |
| 118 | bool EmitDeclName(const NamedDecl *D); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 121 | } // end anonymous namespace |
| 122 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 123 | //===----------------------------------------------------------------------===// |
| 124 | // Generating USRs from ASTS. |
| 125 | //===----------------------------------------------------------------------===// |
| 126 | |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 127 | bool USRGenerator::EmitDeclName(const NamedDecl *D) { |
| 128 | Out.flush(); |
| 129 | const unsigned startSize = Buf.size(); |
| 130 | D->printName(Out); |
| 131 | Out.flush(); |
| 132 | const unsigned endSize = Buf.size(); |
| 133 | return startSize == endSize; |
| 134 | } |
| 135 | |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 136 | static bool InAnonymousNamespace(const Decl *D) { |
| 137 | if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext())) |
| 138 | return ND->isAnonymousNamespace(); |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | static inline bool ShouldGenerateLocation(const NamedDecl *D) { |
| 143 | return D->getLinkage() != ExternalLinkage && !InAnonymousNamespace(D); |
| 144 | } |
| 145 | |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 146 | void USRGenerator::VisitDeclContext(DeclContext *DC) { |
| 147 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) |
| 148 | Visit(D); |
| 149 | } |
| 150 | |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 151 | void USRGenerator::VisitFieldDecl(FieldDecl *D) { |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 152 | VisitDeclContext(D->getDeclContext()); |
| 153 | Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@"); |
| 154 | if (EmitDeclName(D)) { |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 155 | // Bit fields can be anonymous. |
| 156 | IgnoreResults = true; |
| 157 | return; |
| 158 | } |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 161 | void USRGenerator::VisitFunctionDecl(FunctionDecl *D) { |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 162 | if (ShouldGenerateLocation(D) && GenLoc(D)) |
| 163 | return; |
Ted Kremenek | cf99910 | 2010-04-29 17:43:29 +0000 | [diff] [blame] | 164 | |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 165 | VisitDeclContext(D->getDeclContext()); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame^] | 166 | if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) { |
| 167 | Out << "@FT@"; |
| 168 | VisitTemplateParameterList(FunTmpl->getTemplateParameters()); |
| 169 | } else |
| 170 | Out << "@F@"; |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 171 | D->printName(Out); |
Ted Kremenek | 8e67219 | 2010-05-07 01:04:32 +0000 | [diff] [blame] | 172 | |
| 173 | ASTContext &Ctx = AU->getASTContext(); |
| 174 | if (!Ctx.getLangOptions().CPlusPlus || D->isExternC()) |
| 175 | return; |
| 176 | |
| 177 | // Mangle in type information for the arguments. |
| 178 | for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end(); |
| 179 | I != E; ++I) { |
| 180 | Out << '#'; |
| 181 | if (ParmVarDecl *PD = *I) |
| 182 | VisitType(PD->getType()); |
| 183 | } |
| 184 | if (D->isVariadic()) |
| 185 | Out << '.'; |
Ted Kremenek | 2ea5baf | 2010-05-07 20:39:40 +0000 | [diff] [blame] | 186 | Out << '#'; |
| 187 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 188 | if (MD->isStatic()) |
| 189 | Out << 'S'; |
| 190 | if (unsigned quals = MD->getTypeQualifiers()) |
| 191 | Out << (char)('0' + quals); |
| 192 | } |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 193 | } |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 194 | |
| 195 | void USRGenerator::VisitNamedDecl(NamedDecl *D) { |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 196 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 197 | Out << "@"; |
| 198 | |
| 199 | if (EmitDeclName(D)) { |
| 200 | // The string can be empty if the declaration has no name; e.g., it is |
| 201 | // the ParmDecl with no name for declaration of a function pointer type, |
| 202 | // e.g.: void (*f)(void *); |
| 203 | // In this case, don't generate a USR. |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 204 | IgnoreResults = true; |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 205 | } |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 208 | void USRGenerator::VisitVarDecl(VarDecl *D) { |
| 209 | // VarDecls can be declared 'extern' within a function or method body, |
| 210 | // but their enclosing DeclContext is the function, not the TU. We need |
| 211 | // to check the storage class to correctly generate the USR. |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 212 | if (ShouldGenerateLocation(D) && GenLoc(D)) |
| 213 | return; |
| 214 | |
| 215 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 216 | |
Ted Kremenek | cf99910 | 2010-04-29 17:43:29 +0000 | [diff] [blame] | 217 | // Variables always have simple names. |
| 218 | llvm::StringRef s = D->getName(); |
| 219 | |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 220 | // The string can be empty if the declaration has no name; e.g., it is |
| 221 | // the ParmDecl with no name for declaration of a function pointer type, e.g.: |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 222 | // void (*f)(void *); |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 223 | // In this case, don't generate a USR. |
| 224 | if (s.empty()) |
| 225 | IgnoreResults = true; |
| 226 | else |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 227 | Out << '@' << s; |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame^] | 230 | void USRGenerator::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 231 | GenLoc(D); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | void USRGenerator::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 236 | GenLoc(D); |
| 237 | return; |
| 238 | } |
| 239 | |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 240 | void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) { |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 241 | if (D->isAnonymousNamespace()) { |
| 242 | Out << "@aN"; |
| 243 | return; |
| 244 | } |
| 245 | |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 246 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 247 | if (!IgnoreResults) |
| 248 | Out << "@N@" << D->getName(); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame^] | 251 | void USRGenerator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 252 | VisitFunctionDecl(D->getTemplatedDecl()); |
| 253 | } |
| 254 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 255 | void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
Ted Kremenek | 28a7f25 | 2010-08-24 23:13:41 +0000 | [diff] [blame] | 256 | Decl *container = cast<Decl>(D->getDeclContext()); |
| 257 | |
| 258 | // The USR for a method declared in a class extension is based on |
| 259 | // the ObjCInterfaceDecl, not the ObjCCategoryDecl. |
| 260 | do { |
| 261 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(container)) |
| 262 | if (CD->IsClassExtension()) { |
| 263 | Visit(CD->getClassInterface()); |
| 264 | break; |
| 265 | } |
| 266 | Visit(cast<Decl>(D->getDeclContext())); |
| 267 | } |
| 268 | while (false); |
| 269 | |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 270 | // Ideally we would use 'GenObjCMethod', but this is such a hot path |
| 271 | // for Objective-C code that we don't want to use |
| 272 | // DeclarationName::getAsString(). |
| 273 | Out << (D->isInstanceMethod() ? "(im)" : "(cm)"); |
| 274 | DeclarationName N(D->getSelector()); |
| 275 | N.printName(Out); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 278 | void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) { |
| 279 | // FIXME: @class declarations can refer to multiple classes. We need |
| 280 | // to be able to traverse these. |
| 281 | IgnoreResults = true; |
| 282 | } |
| 283 | |
| 284 | void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 285 | // FIXME: @protocol declarations can refer to multiple protocols. We need |
| 286 | // to be able to traverse these. |
| 287 | IgnoreResults = true; |
| 288 | } |
| 289 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 290 | void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
| 291 | switch (D->getKind()) { |
| 292 | default: |
| 293 | assert(false && "Invalid ObjC container."); |
| 294 | case Decl::ObjCInterface: |
| 295 | case Decl::ObjCImplementation: |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 296 | GenObjCClass(D->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 297 | break; |
| 298 | case Decl::ObjCCategory: { |
| 299 | ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D); |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 300 | ObjCInterfaceDecl *ID = CD->getClassInterface(); |
| 301 | if (!ID) { |
| 302 | // Handle invalid code where the @interface might not |
| 303 | // have been specified. |
| 304 | // FIXME: We should be able to generate this USR even if the |
| 305 | // @interface isn't available. |
| 306 | IgnoreResults = true; |
| 307 | return; |
| 308 | } |
Ted Kremenek | 28a7f25 | 2010-08-24 23:13:41 +0000 | [diff] [blame] | 309 | // Specially handle class extensions, which are anonymous categories. |
| 310 | // We want to mangle in the location to uniquely distinguish them. |
| 311 | if (CD->IsClassExtension()) { |
| 312 | Out << "objc(ext)" << ID->getName() << '@'; |
| 313 | GenLoc(CD); |
| 314 | } |
| 315 | else |
| 316 | GenObjCCategory(ID->getName(), CD->getName()); |
| 317 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 318 | break; |
| 319 | } |
| 320 | case Decl::ObjCCategoryImpl: { |
| 321 | ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D); |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 322 | ObjCInterfaceDecl *ID = CD->getClassInterface(); |
| 323 | if (!ID) { |
| 324 | // Handle invalid code where the @interface might not |
| 325 | // have been specified. |
| 326 | // FIXME: We should be able to generate this USR even if the |
| 327 | // @interface isn't available. |
| 328 | IgnoreResults = true; |
| 329 | return; |
| 330 | } |
| 331 | GenObjCCategory(ID->getName(), CD->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 332 | break; |
| 333 | } |
| 334 | case Decl::ObjCProtocol: |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 335 | GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 336 | break; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 341 | Visit(cast<Decl>(D->getDeclContext())); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 342 | GenObjCProperty(D->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 345 | void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { |
| 346 | if (ObjCPropertyDecl *PD = D->getPropertyDecl()) { |
| 347 | VisitObjCPropertyDecl(PD); |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | IgnoreResults = true; |
| 352 | } |
| 353 | |
Ted Kremenek | b82b3be | 2010-01-18 22:42:20 +0000 | [diff] [blame] | 354 | void USRGenerator::VisitTagDecl(TagDecl *D) { |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 355 | // Add the location of the tag decl to handle resolution across |
| 356 | // translation units. |
| 357 | if (ShouldGenerateLocation(D) && GenLoc(D)) |
| 358 | return; |
Ted Kremenek | 8e67219 | 2010-05-07 01:04:32 +0000 | [diff] [blame] | 359 | |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 360 | D = D->getCanonicalDecl(); |
Ted Kremenek | b82b3be | 2010-01-18 22:42:20 +0000 | [diff] [blame] | 361 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 362 | |
Ted Kremenek | b82b3be | 2010-01-18 22:42:20 +0000 | [diff] [blame] | 363 | switch (D->getTagKind()) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 364 | case TTK_Struct: Out << "@S"; break; |
| 365 | case TTK_Class: Out << "@C"; break; |
| 366 | case TTK_Union: Out << "@U"; break; |
| 367 | case TTK_Enum: Out << "@E"; break; |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 370 | Out << '@'; |
| 371 | Out.flush(); |
| 372 | assert(Buf.size() > 0); |
| 373 | const unsigned off = Buf.size() - 1; |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 374 | |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 375 | if (EmitDeclName(D)) { |
| 376 | if (const TypedefDecl *TD = D->getTypedefForAnonDecl()) { |
| 377 | Buf[off] = 'A'; |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 378 | Out << '@' << TD; |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 379 | } |
| 380 | else |
| 381 | Buf[off] = 'a'; |
Ted Kremenek | c5b48b3 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 382 | } |
Ted Kremenek | c5b48b3 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 385 | void USRGenerator::VisitTypedefDecl(TypedefDecl *D) { |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 386 | if (ShouldGenerateLocation(D) && GenLoc(D)) |
| 387 | return; |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 388 | DeclContext *DC = D->getDeclContext(); |
| 389 | if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC)) |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 390 | Visit(DCN); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 391 | Out << "@T@"; |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 392 | Out << D->getName(); |
| 393 | } |
| 394 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame^] | 395 | void USRGenerator::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
| 396 | GenLoc(D); |
| 397 | return; |
| 398 | } |
| 399 | |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 400 | bool USRGenerator::GenLoc(const Decl *D) { |
| 401 | if (generatedLoc) |
| 402 | return IgnoreResults; |
| 403 | generatedLoc = true; |
| 404 | |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 405 | const SourceManager &SM = AU->getSourceManager(); |
| 406 | SourceLocation L = D->getLocStart(); |
| 407 | if (L.isInvalid()) { |
| 408 | IgnoreResults = true; |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 409 | return true; |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 410 | } |
| 411 | L = SM.getInstantiationLoc(L); |
| 412 | const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L); |
| 413 | const FileEntry *FE = SM.getFileEntryForID(Decomposed.first); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 414 | if (FE) { |
| 415 | llvm::sys::Path P(FE->getName()); |
| 416 | Out << P.getLast(); |
| 417 | } |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 418 | else { |
| 419 | // This case really isn't interesting. |
| 420 | IgnoreResults = true; |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 421 | return true; |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 422 | } |
Ted Kremenek | f48b531 | 2010-07-22 11:14:15 +0000 | [diff] [blame] | 423 | // Use the offest into the FileID to represent the location. Using |
| 424 | // a line/column can cause us to look back at the original source file, |
| 425 | // which is expensive. |
| 426 | Out << '@' << Decomposed.second; |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 427 | return IgnoreResults; |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Ted Kremenek | 8e67219 | 2010-05-07 01:04:32 +0000 | [diff] [blame] | 430 | void USRGenerator::VisitType(QualType T) { |
| 431 | // This method mangles in USR information for types. It can possibly |
| 432 | // just reuse the naming-mangling logic used by codegen, although the |
| 433 | // requirements for USRs might not be the same. |
Ted Kremenek | 2ea5baf | 2010-05-07 20:39:40 +0000 | [diff] [blame] | 434 | ASTContext &Ctx = AU->getASTContext(); |
| 435 | |
Ted Kremenek | 8e67219 | 2010-05-07 01:04:32 +0000 | [diff] [blame] | 436 | do { |
Ted Kremenek | 2ea5baf | 2010-05-07 20:39:40 +0000 | [diff] [blame] | 437 | T = Ctx.getCanonicalType(T); |
Ted Kremenek | 8e67219 | 2010-05-07 01:04:32 +0000 | [diff] [blame] | 438 | Qualifiers Q = T.getQualifiers(); |
Ted Kremenek | 2ea5baf | 2010-05-07 20:39:40 +0000 | [diff] [blame] | 439 | unsigned qVal = 0; |
Ted Kremenek | 8e67219 | 2010-05-07 01:04:32 +0000 | [diff] [blame] | 440 | if (Q.hasConst()) |
Ted Kremenek | 2ea5baf | 2010-05-07 20:39:40 +0000 | [diff] [blame] | 441 | qVal |= 0x1; |
Ted Kremenek | 8e67219 | 2010-05-07 01:04:32 +0000 | [diff] [blame] | 442 | if (Q.hasVolatile()) |
Ted Kremenek | 2ea5baf | 2010-05-07 20:39:40 +0000 | [diff] [blame] | 443 | qVal |= 0x2; |
Ted Kremenek | 8e67219 | 2010-05-07 01:04:32 +0000 | [diff] [blame] | 444 | if (Q.hasRestrict()) |
Ted Kremenek | 2ea5baf | 2010-05-07 20:39:40 +0000 | [diff] [blame] | 445 | qVal |= 0x4; |
| 446 | if(qVal) |
| 447 | Out << ((char) ('0' + qVal)); |
Ted Kremenek | 8e67219 | 2010-05-07 01:04:32 +0000 | [diff] [blame] | 448 | |
| 449 | // Mangle in ObjC GC qualifiers? |
| 450 | |
| 451 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 452 | Out << '*'; |
| 453 | T = PT->getPointeeType(); |
| 454 | continue; |
| 455 | } |
| 456 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) { |
| 457 | Out << '&'; |
| 458 | T = RT->getPointeeType(); |
| 459 | continue; |
| 460 | } |
| 461 | if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) { |
| 462 | Out << 'F'; |
| 463 | VisitType(FT->getResultType()); |
| 464 | for (FunctionProtoType::arg_type_iterator |
| 465 | I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) { |
| 466 | VisitType(*I); |
| 467 | } |
| 468 | if (FT->isVariadic()) |
| 469 | Out << '.'; |
| 470 | return; |
| 471 | } |
| 472 | if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) { |
| 473 | Out << 'B'; |
| 474 | T = BT->getPointeeType(); |
| 475 | continue; |
| 476 | } |
| 477 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 478 | unsigned char c = '\0'; |
| 479 | switch (BT->getKind()) { |
| 480 | case BuiltinType::Void: |
| 481 | c = 'v'; break; |
| 482 | case BuiltinType::Bool: |
| 483 | c = 'b'; break; |
| 484 | case BuiltinType::Char_U: |
| 485 | case BuiltinType::UChar: |
| 486 | c = 'c'; break; |
| 487 | case BuiltinType::Char16: |
| 488 | c = 'q'; break; |
| 489 | case BuiltinType::Char32: |
| 490 | c = 'w'; break; |
| 491 | case BuiltinType::UShort: |
| 492 | c = 's'; break; |
| 493 | case BuiltinType::UInt: |
| 494 | c = 'i'; break; |
| 495 | case BuiltinType::ULong: |
| 496 | c = 'l'; break; |
| 497 | case BuiltinType::ULongLong: |
| 498 | c = 'k'; break; |
| 499 | case BuiltinType::UInt128: |
| 500 | c = 'j'; break; |
| 501 | case BuiltinType::Char_S: |
| 502 | case BuiltinType::SChar: |
| 503 | c = 'C'; break; |
| 504 | case BuiltinType::WChar: |
| 505 | c = 'W'; break; |
| 506 | case BuiltinType::Short: |
| 507 | c = 'S'; break; |
| 508 | case BuiltinType::Int: |
| 509 | c = 'I'; break; |
| 510 | case BuiltinType::Long: |
| 511 | c = 'L'; break; |
| 512 | case BuiltinType::LongLong: |
| 513 | c = 'K'; break; |
| 514 | case BuiltinType::Int128: |
| 515 | c = 'J'; break; |
| 516 | case BuiltinType::Float: |
| 517 | c = 'f'; break; |
| 518 | case BuiltinType::Double: |
| 519 | c = 'd'; break; |
| 520 | case BuiltinType::LongDouble: |
| 521 | c = 'D'; break; |
| 522 | case BuiltinType::NullPtr: |
| 523 | c = 'n'; break; |
| 524 | case BuiltinType::Overload: |
| 525 | case BuiltinType::Dependent: |
| 526 | case BuiltinType::UndeducedAuto: |
| 527 | IgnoreResults = true; |
| 528 | return; |
| 529 | case BuiltinType::ObjCId: |
| 530 | c = 'o'; break; |
| 531 | case BuiltinType::ObjCClass: |
| 532 | c = 'O'; break; |
| 533 | case BuiltinType::ObjCSel: |
| 534 | c = 'e'; break; |
| 535 | } |
| 536 | Out << c; |
| 537 | return; |
| 538 | } |
| 539 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
| 540 | Out << '<'; |
| 541 | T = CT->getElementType(); |
| 542 | continue; |
| 543 | } |
Ted Kremenek | 2ea5baf | 2010-05-07 20:39:40 +0000 | [diff] [blame] | 544 | if (const TagType *TT = T->getAs<TagType>()) { |
| 545 | Out << '$'; |
| 546 | VisitTagDecl(TT->getDecl()); |
| 547 | return; |
| 548 | } |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame^] | 549 | if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) { |
| 550 | Out << 't' << TTP->getDepth() << '.' << TTP->getIndex(); |
| 551 | return; |
| 552 | } |
| 553 | if (const TemplateSpecializationType *Spec |
| 554 | = T->getAs<TemplateSpecializationType>()) { |
| 555 | Out << '>'; |
| 556 | VisitTemplateName(Spec->getTemplateName()); |
| 557 | Out << Spec->getNumArgs(); |
| 558 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
| 559 | VisitTemplateArgument(Spec->getArg(I)); |
| 560 | return; |
| 561 | } |
| 562 | |
Ted Kremenek | 8e67219 | 2010-05-07 01:04:32 +0000 | [diff] [blame] | 563 | // Unhandled type. |
| 564 | Out << ' '; |
| 565 | break; |
| 566 | } while (true); |
| 567 | } |
| 568 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame^] | 569 | void USRGenerator::VisitTemplateParameterList( |
| 570 | const TemplateParameterList *Params) { |
| 571 | if (!Params) |
| 572 | return; |
| 573 | Out << '>' << Params->size(); |
| 574 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 575 | PEnd = Params->end(); |
| 576 | P != PEnd; ++P) { |
| 577 | Out << '#'; |
| 578 | if (isa<TemplateTypeParmDecl>(*P)) { |
| 579 | Out << 'T'; |
| 580 | continue; |
| 581 | } |
| 582 | |
| 583 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 584 | Out << 'N'; |
| 585 | VisitType(NTTP->getType()); |
| 586 | continue; |
| 587 | } |
| 588 | |
| 589 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 590 | Out << 't'; |
| 591 | VisitTemplateParameterList(TTP->getTemplateParameters()); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | void USRGenerator::VisitTemplateName(TemplateName Name) { |
| 596 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 597 | if (TemplateTemplateParmDecl *TTP |
| 598 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
| 599 | Out << 't' << TTP->getDepth() << '.' << TTP->getIndex(); |
| 600 | return; |
| 601 | } |
| 602 | |
| 603 | Visit(Template); |
| 604 | return; |
| 605 | } |
| 606 | |
| 607 | // FIXME: Visit dependent template names. |
| 608 | } |
| 609 | |
| 610 | void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) { |
| 611 | switch (Arg.getKind()) { |
| 612 | case TemplateArgument::Null: |
| 613 | break; |
| 614 | |
| 615 | case TemplateArgument::Declaration: |
| 616 | Visit(Arg.getAsDecl()); |
| 617 | break; |
| 618 | |
| 619 | case TemplateArgument::Template: |
| 620 | VisitTemplateName(Arg.getAsTemplate()); |
| 621 | break; |
| 622 | |
| 623 | case TemplateArgument::Expression: |
| 624 | // FIXME: Visit expressions. |
| 625 | break; |
| 626 | |
| 627 | case TemplateArgument::Pack: |
| 628 | // FIXME: Variadic templates |
| 629 | break; |
| 630 | |
| 631 | case TemplateArgument::Type: |
| 632 | VisitType(Arg.getAsType()); |
| 633 | break; |
| 634 | |
| 635 | case TemplateArgument::Integral: |
| 636 | Out << 'V'; |
| 637 | VisitType(Arg.getIntegralType()); |
| 638 | Out << *Arg.getAsIntegral(); |
| 639 | break; |
| 640 | } |
| 641 | } |
| 642 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 643 | //===----------------------------------------------------------------------===// |
| 644 | // General purpose USR generation methods. |
| 645 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 646 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 647 | void USRGenerator::GenObjCClass(llvm::StringRef cls) { |
| 648 | Out << "objc(cs)" << cls; |
| 649 | } |
| 650 | |
| 651 | void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) { |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 652 | Out << "objc(cy)" << cls << '@' << cat; |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | void USRGenerator::GenObjCIvar(llvm::StringRef ivar) { |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 656 | Out << '@' << ivar; |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) { |
| 660 | Out << (isInstanceMethod ? "(im)" : "(cm)") << meth; |
| 661 | } |
| 662 | |
| 663 | void USRGenerator::GenObjCProperty(llvm::StringRef prop) { |
| 664 | Out << "(py)" << prop; |
| 665 | } |
| 666 | |
| 667 | void USRGenerator::GenObjCProtocol(llvm::StringRef prot) { |
| 668 | Out << "objc(pl)" << prot; |
| 669 | } |
| 670 | |
| 671 | //===----------------------------------------------------------------------===// |
| 672 | // API hooks. |
| 673 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 674 | |
Benjamin Kramer | cfb51b6 | 2010-04-08 15:54:07 +0000 | [diff] [blame] | 675 | static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) { |
| 676 | return s.startswith("c:") ? s.substr(2) : ""; |
| 677 | } |
| 678 | |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 679 | static CXString getDeclCursorUSR(const CXCursor &C) { |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 680 | Decl *D = cxcursor::getCursorDecl(C); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 681 | |
| 682 | // Don't generate USRs for things with invalid locations. |
| 683 | if (!D || D->getLocStart().isInvalid()) |
Ted Kremenek | 1af0a2a | 2010-04-17 00:21:38 +0000 | [diff] [blame] | 684 | return createCXString(""); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 685 | |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 686 | // Check if the cursor has 'NoLinkage'. |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 687 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 688 | switch (ND->getLinkage()) { |
| 689 | case ExternalLinkage: |
| 690 | // Generate USRs for all entities with external linkage. |
| 691 | break; |
| 692 | case NoLinkage: |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 693 | case UniqueExternalLinkage: |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 694 | // We allow enums, typedefs, and structs that have no linkage to |
| 695 | // have USRs that are anchored to the file they were defined in |
| 696 | // (e.g., the header). This is a little gross, but in principal |
| 697 | // enums/anonymous structs/etc. defined in a common header file |
| 698 | // are referred to across multiple translation units. |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 699 | if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) || |
Ted Kremenek | cf99910 | 2010-04-29 17:43:29 +0000 | [diff] [blame] | 700 | isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) || |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 701 | isa<VarDecl>(ND) || isa<NamespaceDecl>(ND)) |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 702 | break; |
| 703 | // Fall-through. |
| 704 | case InternalLinkage: |
Ted Kremenek | cf99910 | 2010-04-29 17:43:29 +0000 | [diff] [blame] | 705 | if (isa<FunctionDecl>(ND)) |
| 706 | break; |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 709 | USRGenerator UG(&C); |
| 710 | UG->Visit(D); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 711 | |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 712 | if (UG->ignoreResults()) |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 713 | return createCXString(""); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 714 | |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 715 | #if 0 |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 716 | // For development testing. |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 717 | assert(UG.str().size() > 2); |
| 718 | #endif |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 719 | |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 720 | // Return a copy of the string that must be disposed by the caller. |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 721 | return createCXString(UG.str(), true); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 724 | extern "C" { |
| 725 | |
| 726 | CXString clang_getCursorUSR(CXCursor C) { |
Ted Kremenek | fa8231d | 2010-04-11 22:20:34 +0000 | [diff] [blame] | 727 | const CXCursorKind &K = clang_getCursorKind(C); |
| 728 | |
| 729 | if (clang_isDeclaration(K)) |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 730 | return getDeclCursorUSR(C); |
Ted Kremenek | fa8231d | 2010-04-11 22:20:34 +0000 | [diff] [blame] | 731 | |
| 732 | if (K == CXCursor_MacroDefinition) { |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 733 | USRGenerator UG(&C); |
| 734 | UG << "macro@" |
| 735 | << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart(); |
| 736 | return createCXString(UG.str(), true); |
Ted Kremenek | fa8231d | 2010-04-11 22:20:34 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 739 | return createCXString(""); |
| 740 | } |
| 741 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 742 | CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) { |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 743 | USRGenerator UG; |
| 744 | UG << extractUSRSuffix(clang_getCString(classUSR)); |
| 745 | UG->GenObjCIvar(name); |
| 746 | return createCXString(UG.str(), true); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | CXString clang_constructUSR_ObjCMethod(const char *name, |
| 750 | unsigned isInstanceMethod, |
| 751 | CXString classUSR) { |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 752 | USRGenerator UG; |
| 753 | UG << extractUSRSuffix(clang_getCString(classUSR)); |
| 754 | UG->GenObjCMethod(name, isInstanceMethod); |
| 755 | return createCXString(UG.str(), true); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | CXString clang_constructUSR_ObjCClass(const char *name) { |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 759 | USRGenerator UG; |
| 760 | UG->GenObjCClass(name); |
| 761 | return createCXString(UG.str(), true); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | CXString clang_constructUSR_ObjCProtocol(const char *name) { |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 765 | USRGenerator UG; |
| 766 | UG->GenObjCProtocol(name); |
| 767 | return createCXString(UG.str(), true); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Ted Kremenek | 66ccaec | 2010-03-15 17:38:58 +0000 | [diff] [blame] | 770 | CXString clang_constructUSR_ObjCCategory(const char *class_name, |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 771 | const char *category_name) { |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 772 | USRGenerator UG; |
| 773 | UG->GenObjCCategory(class_name, category_name); |
| 774 | return createCXString(UG.str(), true); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | CXString clang_constructUSR_ObjCProperty(const char *property, |
| 778 | CXString classUSR) { |
Ted Kremenek | 3ebd8dc | 2010-05-07 20:07:23 +0000 | [diff] [blame] | 779 | USRGenerator UG; |
| 780 | UG << extractUSRSuffix(clang_getCString(classUSR)); |
| 781 | UG->GenObjCProperty(property); |
| 782 | return createCXString(UG.str(), true); |
Ted Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 783 | } |
Ted Kremenek | 1b6869a | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 784 | |
Ted Kremenek | 1b6869a | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 785 | } // end extern "C" |