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