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