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