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" |
Benjamin Kramer | 9895c6a | 2010-01-12 11:32:40 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclVisitor.h" |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/ASTUnit.h" |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 18 | #include "clang/Lex/PreprocessingRecord.h" |
Ted Kremenek | 8776382 | 2010-01-12 02:07:58 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 9895c6a | 2010-01-12 11:32:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 21 | |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 22 | using namespace clang; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 23 | using namespace clang::cxstring; |
| 24 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | // USR generation. |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | namespace { |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 30 | class USRGenerator : public DeclVisitor<USRGenerator> { |
| 31 | llvm::raw_ostream &Out; |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 32 | bool IgnoreResults; |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 33 | ASTUnit *AU; |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 34 | bool generatedLoc; |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 35 | public: |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 36 | USRGenerator(ASTUnit *au, llvm::raw_ostream &out) |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 37 | : Out(out), IgnoreResults(false), AU(au), generatedLoc(false) {} |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 39 | bool ignoreResults() const { return IgnoreResults; } |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 40 | |
| 41 | // Visitation methods from generating USRs from AST elements. |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 42 | void VisitDeclContext(DeclContext *D); |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 43 | void VisitFieldDecl(FieldDecl *D); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 44 | void VisitFunctionDecl(FunctionDecl *D); |
| 45 | void VisitNamedDecl(NamedDecl *D); |
| 46 | void VisitNamespaceDecl(NamespaceDecl *D); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 47 | void VisitObjCClassDecl(ObjCClassDecl *CD); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 48 | void VisitObjCContainerDecl(ObjCContainerDecl *CD); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 49 | void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *P); |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 50 | void VisitObjCMethodDecl(ObjCMethodDecl *MD); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 51 | void VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 52 | void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
Ted Kremenek | b82b3be | 2010-01-18 22:42:20 +0000 | [diff] [blame] | 53 | void VisitTagDecl(TagDecl *D); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 54 | void VisitTypedefDecl(TypedefDecl *D); |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 55 | void VisitVarDecl(VarDecl *D); |
Ted Kremenek | 8e67219 | 2010-05-07 01:04:32 +0000 | [diff] [blame^] | 56 | void VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 57 | IgnoreResults = true; |
| 58 | return; |
| 59 | } |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 61 | /// Generate the string component containing the location of the |
| 62 | /// declaration. |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 63 | bool GenLoc(const Decl *D); |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 64 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 65 | /// String generation methods used both by the visitation methods |
| 66 | /// and from other clients that want to directly generate USRs. These |
| 67 | /// methods do not construct complete USRs (which incorporate the parents |
| 68 | /// of an AST element), but only the fragments concerning the AST element |
| 69 | /// itself. |
| 70 | |
| 71 | /// Generate a USR fragment for a named declaration. This does |
| 72 | /// not include the USR component for the parent. |
| 73 | void GenNamedDecl(llvm::StringRef name); |
| 74 | |
| 75 | /// Generate a USR for an Objective-C class. |
| 76 | void GenObjCClass(llvm::StringRef cls); |
| 77 | /// Generate a USR for an Objective-C class category. |
| 78 | void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat); |
| 79 | /// Generate a USR fragment for an Objective-C instance variable. The |
| 80 | /// complete USR can be created by concatenating the USR for the |
| 81 | /// encompassing class with this USR fragment. |
| 82 | void GenObjCIvar(llvm::StringRef ivar); |
| 83 | /// Generate a USR fragment for an Objective-C method. |
| 84 | void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod); |
| 85 | /// Generate a USR fragment for an Objective-C property. |
| 86 | void GenObjCProperty(llvm::StringRef prop); |
| 87 | /// Generate a USR for an Objective-C protocol. |
| 88 | void GenObjCProtocol(llvm::StringRef prot); |
Ted Kremenek | 8e67219 | 2010-05-07 01:04:32 +0000 | [diff] [blame^] | 89 | |
| 90 | void VisitType(QualType T); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 91 | }; |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 92 | |
| 93 | class StringUSRGenerator { |
| 94 | private: |
| 95 | llvm::SmallString<1024> StrBuf; |
| 96 | llvm::raw_svector_ostream Out; |
| 97 | USRGenerator UG; |
| 98 | public: |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 99 | StringUSRGenerator(const CXCursor *C = 0) |
| 100 | : Out(StrBuf), UG(C ? cxcursor::getCursorASTUnit(*C) : 0, Out) { |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 101 | // Add the USR space prefix. |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 102 | Out << "c:"; |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 103 | } |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 104 | |
| 105 | llvm::StringRef str() { |
| 106 | return Out.str(); |
| 107 | } |
| 108 | |
| 109 | USRGenerator* operator->() { return &UG; } |
| 110 | |
| 111 | template <typename T> |
| 112 | llvm::raw_svector_ostream &operator<<(const T &x) { |
| 113 | Out << x; |
| 114 | return Out; |
| 115 | } |
| 116 | }; |
| 117 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 118 | } // end anonymous namespace |
| 119 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 120 | //===----------------------------------------------------------------------===// |
| 121 | // Generating USRs from ASTS. |
| 122 | //===----------------------------------------------------------------------===// |
| 123 | |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 124 | static bool InAnonymousNamespace(const Decl *D) { |
| 125 | if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext())) |
| 126 | return ND->isAnonymousNamespace(); |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | static inline bool ShouldGenerateLocation(const NamedDecl *D) { |
| 131 | return D->getLinkage() != ExternalLinkage && !InAnonymousNamespace(D); |
| 132 | } |
| 133 | |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 134 | void USRGenerator::VisitDeclContext(DeclContext *DC) { |
| 135 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) |
| 136 | Visit(D); |
| 137 | } |
| 138 | |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 139 | void USRGenerator::VisitFieldDecl(FieldDecl *D) { |
| 140 | const std::string &s = D->getNameAsString(); |
| 141 | if (s.empty()) { |
| 142 | // Bit fields can be anonymous. |
| 143 | IgnoreResults = true; |
| 144 | return; |
| 145 | } |
| 146 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 147 | Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@") << s; |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 150 | void USRGenerator::VisitFunctionDecl(FunctionDecl *D) { |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 151 | if (ShouldGenerateLocation(D) && GenLoc(D)) |
| 152 | return; |
Ted Kremenek | cf99910 | 2010-04-29 17:43:29 +0000 | [diff] [blame] | 153 | |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 154 | VisitDeclContext(D->getDeclContext()); |
| 155 | Out << "@F@" << D->getNameAsString(); |
Ted Kremenek | 8e67219 | 2010-05-07 01:04:32 +0000 | [diff] [blame^] | 156 | |
| 157 | ASTContext &Ctx = AU->getASTContext(); |
| 158 | if (!Ctx.getLangOptions().CPlusPlus || D->isExternC()) |
| 159 | return; |
| 160 | |
| 161 | // Mangle in type information for the arguments. |
| 162 | for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end(); |
| 163 | I != E; ++I) { |
| 164 | Out << '#'; |
| 165 | if (ParmVarDecl *PD = *I) |
| 166 | VisitType(PD->getType()); |
| 167 | } |
| 168 | if (D->isVariadic()) |
| 169 | Out << '.'; |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 170 | } |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 171 | |
| 172 | void USRGenerator::VisitNamedDecl(NamedDecl *D) { |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 173 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 174 | const std::string &s = D->getNameAsString(); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 175 | // The string can be empty if the declaration has no name; e.g., it is |
| 176 | // the ParmDecl with no name for declaration of a function pointer type, e.g.: |
| 177 | // void (*f)(void *); |
| 178 | // In this case, don't generate a USR. |
| 179 | if (s.empty()) |
| 180 | IgnoreResults = true; |
| 181 | else |
| 182 | GenNamedDecl(s); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 185 | void USRGenerator::VisitVarDecl(VarDecl *D) { |
| 186 | // VarDecls can be declared 'extern' within a function or method body, |
| 187 | // but their enclosing DeclContext is the function, not the TU. We need |
| 188 | // to check the storage class to correctly generate the USR. |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 189 | if (ShouldGenerateLocation(D) && GenLoc(D)) |
| 190 | return; |
| 191 | |
| 192 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 193 | |
Ted Kremenek | cf99910 | 2010-04-29 17:43:29 +0000 | [diff] [blame] | 194 | // Variables always have simple names. |
| 195 | llvm::StringRef s = D->getName(); |
| 196 | |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 197 | // The string can be empty if the declaration has no name; e.g., it is |
| 198 | // the ParmDecl with no name for declaration of a function pointer type, e.g.: |
| 199 | // void (*f)(void *); |
| 200 | // In this case, don't generate a USR. |
| 201 | if (s.empty()) |
| 202 | IgnoreResults = true; |
| 203 | else |
| 204 | GenNamedDecl(s); |
| 205 | } |
| 206 | |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 207 | void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) { |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 208 | if (D->isAnonymousNamespace()) { |
| 209 | Out << "@aN"; |
| 210 | return; |
| 211 | } |
| 212 | |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 213 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 214 | if (!IgnoreResults) |
| 215 | Out << "@N@" << D->getName(); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 218 | void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 219 | Visit(cast<Decl>(D->getDeclContext())); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 220 | GenObjCMethod(DeclarationName(D->getSelector()).getAsString(), |
| 221 | D->isInstanceMethod()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 224 | void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) { |
| 225 | // FIXME: @class declarations can refer to multiple classes. We need |
| 226 | // to be able to traverse these. |
| 227 | IgnoreResults = true; |
| 228 | } |
| 229 | |
| 230 | void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 231 | // FIXME: @protocol declarations can refer to multiple protocols. We need |
| 232 | // to be able to traverse these. |
| 233 | IgnoreResults = true; |
| 234 | } |
| 235 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 236 | void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
| 237 | switch (D->getKind()) { |
| 238 | default: |
| 239 | assert(false && "Invalid ObjC container."); |
| 240 | case Decl::ObjCInterface: |
| 241 | case Decl::ObjCImplementation: |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 242 | GenObjCClass(D->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 243 | break; |
| 244 | case Decl::ObjCCategory: { |
| 245 | ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D); |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 246 | ObjCInterfaceDecl *ID = CD->getClassInterface(); |
| 247 | if (!ID) { |
| 248 | // Handle invalid code where the @interface might not |
| 249 | // have been specified. |
| 250 | // FIXME: We should be able to generate this USR even if the |
| 251 | // @interface isn't available. |
| 252 | IgnoreResults = true; |
| 253 | return; |
| 254 | } |
| 255 | GenObjCCategory(ID->getName(), CD->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 256 | break; |
| 257 | } |
| 258 | case Decl::ObjCCategoryImpl: { |
| 259 | ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D); |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 260 | ObjCInterfaceDecl *ID = CD->getClassInterface(); |
| 261 | if (!ID) { |
| 262 | // Handle invalid code where the @interface might not |
| 263 | // have been specified. |
| 264 | // FIXME: We should be able to generate this USR even if the |
| 265 | // @interface isn't available. |
| 266 | IgnoreResults = true; |
| 267 | return; |
| 268 | } |
| 269 | GenObjCCategory(ID->getName(), CD->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 270 | break; |
| 271 | } |
| 272 | case Decl::ObjCProtocol: |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 273 | GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 274 | break; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 279 | Visit(cast<Decl>(D->getDeclContext())); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 280 | GenObjCProperty(D->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 283 | void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { |
| 284 | if (ObjCPropertyDecl *PD = D->getPropertyDecl()) { |
| 285 | VisitObjCPropertyDecl(PD); |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | IgnoreResults = true; |
| 290 | } |
| 291 | |
Ted Kremenek | b82b3be | 2010-01-18 22:42:20 +0000 | [diff] [blame] | 292 | void USRGenerator::VisitTagDecl(TagDecl *D) { |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 293 | // Add the location of the tag decl to handle resolution across |
| 294 | // translation units. |
| 295 | if (ShouldGenerateLocation(D) && GenLoc(D)) |
| 296 | return; |
Ted Kremenek | 8e67219 | 2010-05-07 01:04:32 +0000 | [diff] [blame^] | 297 | |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 298 | D = D->getCanonicalDecl(); |
Ted Kremenek | b82b3be | 2010-01-18 22:42:20 +0000 | [diff] [blame] | 299 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 300 | |
Ted Kremenek | b82b3be | 2010-01-18 22:42:20 +0000 | [diff] [blame] | 301 | switch (D->getTagKind()) { |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 302 | case TagDecl::TK_struct: Out << "@S"; break; |
| 303 | case TagDecl::TK_class: Out << "@C"; break; |
| 304 | case TagDecl::TK_union: Out << "@U"; break; |
| 305 | case TagDecl::TK_enum: Out << "@E"; break; |
| 306 | } |
| 307 | |
| 308 | const std::string &s = D->getNameAsString(); |
| 309 | const TypedefDecl *TD = 0; |
| 310 | if (s.empty()) { |
| 311 | TD = D->getTypedefForAnonDecl(); |
| 312 | Out << (TD ? 'A' : 'a'); |
Ted Kremenek | b82b3be | 2010-01-18 22:42:20 +0000 | [diff] [blame] | 313 | } |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 314 | |
Ted Kremenek | c5b48b3 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 315 | if (s.empty()) { |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 316 | if (TD) |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 317 | Out << '@' << TD; |
Ted Kremenek | c5b48b3 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 318 | } |
| 319 | else |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 320 | Out << '@' << s; |
Ted Kremenek | c5b48b3 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 323 | void USRGenerator::VisitTypedefDecl(TypedefDecl *D) { |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 324 | if (ShouldGenerateLocation(D) && GenLoc(D)) |
| 325 | return; |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 326 | DeclContext *DC = D->getDeclContext(); |
| 327 | if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC)) |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 328 | Visit(DCN); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 329 | Out << "@T@"; |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 330 | Out << D->getName(); |
| 331 | } |
| 332 | |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 333 | bool USRGenerator::GenLoc(const Decl *D) { |
| 334 | if (generatedLoc) |
| 335 | return IgnoreResults; |
| 336 | generatedLoc = true; |
| 337 | |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 338 | const SourceManager &SM = AU->getSourceManager(); |
| 339 | SourceLocation L = D->getLocStart(); |
| 340 | if (L.isInvalid()) { |
| 341 | IgnoreResults = true; |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 342 | return true; |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 343 | } |
| 344 | L = SM.getInstantiationLoc(L); |
| 345 | const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L); |
| 346 | const FileEntry *FE = SM.getFileEntryForID(Decomposed.first); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 347 | if (FE) { |
| 348 | llvm::sys::Path P(FE->getName()); |
| 349 | Out << P.getLast(); |
| 350 | } |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 351 | else { |
| 352 | // This case really isn't interesting. |
| 353 | IgnoreResults = true; |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 354 | return true; |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 355 | } |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 356 | Out << '@' |
| 357 | << SM.getLineNumber(Decomposed.first, Decomposed.second) << ':' |
| 358 | << SM.getColumnNumber(Decomposed.first, Decomposed.second); |
Ted Kremenek | 8e67219 | 2010-05-07 01:04:32 +0000 | [diff] [blame^] | 359 | |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 360 | return IgnoreResults; |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Ted Kremenek | 8e67219 | 2010-05-07 01:04:32 +0000 | [diff] [blame^] | 363 | void USRGenerator::VisitType(QualType T) { |
| 364 | // This method mangles in USR information for types. It can possibly |
| 365 | // just reuse the naming-mangling logic used by codegen, although the |
| 366 | // requirements for USRs might not be the same. |
| 367 | do { |
| 368 | T = T.getTypePtr()->getCanonicalTypeInternal(); |
| 369 | Qualifiers Q = T.getQualifiers(); |
| 370 | if (Q.hasConst()) |
| 371 | Out << '1'; |
| 372 | if (Q.hasVolatile()) |
| 373 | Out << '2'; |
| 374 | if (Q.hasRestrict()) |
| 375 | Out << '3'; |
| 376 | |
| 377 | // Mangle in ObjC GC qualifiers? |
| 378 | |
| 379 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 380 | Out << '*'; |
| 381 | T = PT->getPointeeType(); |
| 382 | continue; |
| 383 | } |
| 384 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) { |
| 385 | Out << '&'; |
| 386 | T = RT->getPointeeType(); |
| 387 | continue; |
| 388 | } |
| 389 | if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) { |
| 390 | Out << 'F'; |
| 391 | VisitType(FT->getResultType()); |
| 392 | for (FunctionProtoType::arg_type_iterator |
| 393 | I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) { |
| 394 | VisitType(*I); |
| 395 | } |
| 396 | if (FT->isVariadic()) |
| 397 | Out << '.'; |
| 398 | return; |
| 399 | } |
| 400 | if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) { |
| 401 | Out << 'B'; |
| 402 | T = BT->getPointeeType(); |
| 403 | continue; |
| 404 | } |
| 405 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 406 | unsigned char c = '\0'; |
| 407 | switch (BT->getKind()) { |
| 408 | case BuiltinType::Void: |
| 409 | c = 'v'; break; |
| 410 | case BuiltinType::Bool: |
| 411 | c = 'b'; break; |
| 412 | case BuiltinType::Char_U: |
| 413 | case BuiltinType::UChar: |
| 414 | c = 'c'; break; |
| 415 | case BuiltinType::Char16: |
| 416 | c = 'q'; break; |
| 417 | case BuiltinType::Char32: |
| 418 | c = 'w'; break; |
| 419 | case BuiltinType::UShort: |
| 420 | c = 's'; break; |
| 421 | case BuiltinType::UInt: |
| 422 | c = 'i'; break; |
| 423 | case BuiltinType::ULong: |
| 424 | c = 'l'; break; |
| 425 | case BuiltinType::ULongLong: |
| 426 | c = 'k'; break; |
| 427 | case BuiltinType::UInt128: |
| 428 | c = 'j'; break; |
| 429 | case BuiltinType::Char_S: |
| 430 | case BuiltinType::SChar: |
| 431 | c = 'C'; break; |
| 432 | case BuiltinType::WChar: |
| 433 | c = 'W'; break; |
| 434 | case BuiltinType::Short: |
| 435 | c = 'S'; break; |
| 436 | case BuiltinType::Int: |
| 437 | c = 'I'; break; |
| 438 | case BuiltinType::Long: |
| 439 | c = 'L'; break; |
| 440 | case BuiltinType::LongLong: |
| 441 | c = 'K'; break; |
| 442 | case BuiltinType::Int128: |
| 443 | c = 'J'; break; |
| 444 | case BuiltinType::Float: |
| 445 | c = 'f'; break; |
| 446 | case BuiltinType::Double: |
| 447 | c = 'd'; break; |
| 448 | case BuiltinType::LongDouble: |
| 449 | c = 'D'; break; |
| 450 | case BuiltinType::NullPtr: |
| 451 | c = 'n'; break; |
| 452 | case BuiltinType::Overload: |
| 453 | case BuiltinType::Dependent: |
| 454 | case BuiltinType::UndeducedAuto: |
| 455 | IgnoreResults = true; |
| 456 | return; |
| 457 | case BuiltinType::ObjCId: |
| 458 | c = 'o'; break; |
| 459 | case BuiltinType::ObjCClass: |
| 460 | c = 'O'; break; |
| 461 | case BuiltinType::ObjCSel: |
| 462 | c = 'e'; break; |
| 463 | } |
| 464 | Out << c; |
| 465 | return; |
| 466 | } |
| 467 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
| 468 | Out << '<'; |
| 469 | T = CT->getElementType(); |
| 470 | continue; |
| 471 | } |
| 472 | |
| 473 | // Unhandled type. |
| 474 | Out << ' '; |
| 475 | break; |
| 476 | } while (true); |
| 477 | } |
| 478 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 479 | //===----------------------------------------------------------------------===// |
| 480 | // General purpose USR generation methods. |
| 481 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 482 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 483 | void USRGenerator::GenNamedDecl(llvm::StringRef name) { |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 484 | Out << "@" << name; |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | void USRGenerator::GenObjCClass(llvm::StringRef cls) { |
| 488 | Out << "objc(cs)" << cls; |
| 489 | } |
| 490 | |
| 491 | void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) { |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 492 | Out << "objc(cy)" << cls << '@' << cat; |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | void USRGenerator::GenObjCIvar(llvm::StringRef ivar) { |
| 496 | GenNamedDecl(ivar); |
| 497 | } |
| 498 | |
| 499 | void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) { |
| 500 | Out << (isInstanceMethod ? "(im)" : "(cm)") << meth; |
| 501 | } |
| 502 | |
| 503 | void USRGenerator::GenObjCProperty(llvm::StringRef prop) { |
| 504 | Out << "(py)" << prop; |
| 505 | } |
| 506 | |
| 507 | void USRGenerator::GenObjCProtocol(llvm::StringRef prot) { |
| 508 | Out << "objc(pl)" << prot; |
| 509 | } |
| 510 | |
| 511 | //===----------------------------------------------------------------------===// |
| 512 | // API hooks. |
| 513 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 514 | |
Benjamin Kramer | cfb51b6 | 2010-04-08 15:54:07 +0000 | [diff] [blame] | 515 | static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) { |
| 516 | return s.startswith("c:") ? s.substr(2) : ""; |
| 517 | } |
| 518 | |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 519 | static CXString getDeclCursorUSR(const CXCursor &C) { |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 520 | Decl *D = cxcursor::getCursorDecl(C); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 521 | |
| 522 | // Don't generate USRs for things with invalid locations. |
| 523 | if (!D || D->getLocStart().isInvalid()) |
Ted Kremenek | 1af0a2a | 2010-04-17 00:21:38 +0000 | [diff] [blame] | 524 | return createCXString(""); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 525 | |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 526 | // Check if the cursor has 'NoLinkage'. |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 527 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 528 | switch (ND->getLinkage()) { |
| 529 | case ExternalLinkage: |
| 530 | // Generate USRs for all entities with external linkage. |
| 531 | break; |
| 532 | case NoLinkage: |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 533 | case UniqueExternalLinkage: |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 534 | // We allow enums, typedefs, and structs that have no linkage to |
| 535 | // have USRs that are anchored to the file they were defined in |
| 536 | // (e.g., the header). This is a little gross, but in principal |
| 537 | // enums/anonymous structs/etc. defined in a common header file |
| 538 | // are referred to across multiple translation units. |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 539 | if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) || |
Ted Kremenek | cf99910 | 2010-04-29 17:43:29 +0000 | [diff] [blame] | 540 | isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) || |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame] | 541 | isa<VarDecl>(ND) || isa<NamespaceDecl>(ND)) |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 542 | break; |
| 543 | // Fall-through. |
| 544 | case InternalLinkage: |
Ted Kremenek | cf99910 | 2010-04-29 17:43:29 +0000 | [diff] [blame] | 545 | if (isa<FunctionDecl>(ND)) |
| 546 | break; |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | StringUSRGenerator SUG(&C); |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 550 | SUG->Visit(D); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 551 | |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 552 | if (SUG->ignoreResults()) |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 553 | return createCXString(""); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 554 | |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 555 | // For development testing. |
| 556 | // assert(SUG.str().size() > 2); |
| 557 | |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 558 | // Return a copy of the string that must be disposed by the caller. |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 559 | return createCXString(SUG.str(), true); |
| 560 | } |
| 561 | |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 562 | extern "C" { |
| 563 | |
| 564 | CXString clang_getCursorUSR(CXCursor C) { |
Ted Kremenek | fa8231d | 2010-04-11 22:20:34 +0000 | [diff] [blame] | 565 | const CXCursorKind &K = clang_getCursorKind(C); |
| 566 | |
| 567 | if (clang_isDeclaration(K)) |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 568 | return getDeclCursorUSR(C); |
Ted Kremenek | fa8231d | 2010-04-11 22:20:34 +0000 | [diff] [blame] | 569 | |
| 570 | if (K == CXCursor_MacroDefinition) { |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 571 | StringUSRGenerator SUG(&C); |
Ted Kremenek | fa8231d | 2010-04-11 22:20:34 +0000 | [diff] [blame] | 572 | SUG << "macro@" |
| 573 | << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart(); |
| 574 | return createCXString(SUG.str(), true); |
| 575 | } |
| 576 | |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 577 | return createCXString(""); |
| 578 | } |
| 579 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 580 | CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) { |
| 581 | StringUSRGenerator SUG; |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 582 | SUG << extractUSRSuffix(clang_getCString(classUSR)); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 583 | SUG->GenObjCIvar(name); |
| 584 | return createCXString(SUG.str(), true); |
| 585 | } |
| 586 | |
| 587 | CXString clang_constructUSR_ObjCMethod(const char *name, |
| 588 | unsigned isInstanceMethod, |
| 589 | CXString classUSR) { |
| 590 | StringUSRGenerator SUG; |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 591 | SUG << extractUSRSuffix(clang_getCString(classUSR)); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 592 | SUG->GenObjCMethod(name, isInstanceMethod); |
| 593 | return createCXString(SUG.str(), true); |
| 594 | } |
| 595 | |
| 596 | CXString clang_constructUSR_ObjCClass(const char *name) { |
| 597 | StringUSRGenerator SUG; |
| 598 | SUG->GenObjCClass(name); |
| 599 | return createCXString(SUG.str(), true); |
| 600 | } |
| 601 | |
| 602 | CXString clang_constructUSR_ObjCProtocol(const char *name) { |
| 603 | StringUSRGenerator SUG; |
| 604 | SUG->GenObjCProtocol(name); |
| 605 | return createCXString(SUG.str(), true); |
| 606 | } |
| 607 | |
Ted Kremenek | 66ccaec | 2010-03-15 17:38:58 +0000 | [diff] [blame] | 608 | CXString clang_constructUSR_ObjCCategory(const char *class_name, |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 609 | const char *category_name) { |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 610 | StringUSRGenerator SUG; |
| 611 | SUG->GenObjCCategory(class_name, category_name); |
| 612 | return createCXString(SUG.str(), true); |
| 613 | } |
| 614 | |
| 615 | CXString clang_constructUSR_ObjCProperty(const char *property, |
| 616 | CXString classUSR) { |
| 617 | StringUSRGenerator SUG; |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 618 | SUG << extractUSRSuffix(clang_getCString(classUSR)); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 619 | SUG->GenObjCProperty(property); |
| 620 | return createCXString(SUG.str(), true); |
Ted Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 621 | } |
Ted Kremenek | 1b6869a | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 622 | |
Ted Kremenek | 1b6869a | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 623 | } // end extern "C" |