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