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 | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 56 | |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 57 | /// Generate the string component containing the location of the |
| 58 | /// declaration. |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame^] | 59 | bool GenLoc(const Decl *D); |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 61 | /// String generation methods used both by the visitation methods |
| 62 | /// and from other clients that want to directly generate USRs. These |
| 63 | /// methods do not construct complete USRs (which incorporate the parents |
| 64 | /// of an AST element), but only the fragments concerning the AST element |
| 65 | /// itself. |
| 66 | |
| 67 | /// Generate a USR fragment for a named declaration. This does |
| 68 | /// not include the USR component for the parent. |
| 69 | void GenNamedDecl(llvm::StringRef name); |
| 70 | |
| 71 | /// Generate a USR for an Objective-C class. |
| 72 | void GenObjCClass(llvm::StringRef cls); |
| 73 | /// Generate a USR for an Objective-C class category. |
| 74 | void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat); |
| 75 | /// Generate a USR fragment for an Objective-C instance variable. The |
| 76 | /// complete USR can be created by concatenating the USR for the |
| 77 | /// encompassing class with this USR fragment. |
| 78 | void GenObjCIvar(llvm::StringRef ivar); |
| 79 | /// Generate a USR fragment for an Objective-C method. |
| 80 | void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod); |
| 81 | /// Generate a USR fragment for an Objective-C property. |
| 82 | void GenObjCProperty(llvm::StringRef prop); |
| 83 | /// Generate a USR for an Objective-C protocol. |
| 84 | void GenObjCProtocol(llvm::StringRef prot); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 85 | }; |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 86 | |
| 87 | class StringUSRGenerator { |
| 88 | private: |
| 89 | llvm::SmallString<1024> StrBuf; |
| 90 | llvm::raw_svector_ostream Out; |
| 91 | USRGenerator UG; |
| 92 | public: |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 93 | StringUSRGenerator(const CXCursor *C = 0) |
| 94 | : Out(StrBuf), UG(C ? cxcursor::getCursorASTUnit(*C) : 0, Out) { |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 95 | // Add the USR space prefix. |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 96 | Out << "c:"; |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 97 | } |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 98 | |
| 99 | llvm::StringRef str() { |
| 100 | return Out.str(); |
| 101 | } |
| 102 | |
| 103 | USRGenerator* operator->() { return &UG; } |
| 104 | |
| 105 | template <typename T> |
| 106 | llvm::raw_svector_ostream &operator<<(const T &x) { |
| 107 | Out << x; |
| 108 | return Out; |
| 109 | } |
| 110 | }; |
| 111 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 112 | } // end anonymous namespace |
| 113 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 114 | //===----------------------------------------------------------------------===// |
| 115 | // Generating USRs from ASTS. |
| 116 | //===----------------------------------------------------------------------===// |
| 117 | |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame^] | 118 | static bool InAnonymousNamespace(const Decl *D) { |
| 119 | if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext())) |
| 120 | return ND->isAnonymousNamespace(); |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | static inline bool ShouldGenerateLocation(const NamedDecl *D) { |
| 125 | return D->getLinkage() != ExternalLinkage && !InAnonymousNamespace(D); |
| 126 | } |
| 127 | |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 128 | void USRGenerator::VisitDeclContext(DeclContext *DC) { |
| 129 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) |
| 130 | Visit(D); |
| 131 | } |
| 132 | |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 133 | void USRGenerator::VisitFieldDecl(FieldDecl *D) { |
| 134 | const std::string &s = D->getNameAsString(); |
| 135 | if (s.empty()) { |
| 136 | // Bit fields can be anonymous. |
| 137 | IgnoreResults = true; |
| 138 | return; |
| 139 | } |
| 140 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 141 | Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@") << s; |
Ted Kremenek | 3adca6d | 2010-01-18 22:02:49 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 144 | void USRGenerator::VisitFunctionDecl(FunctionDecl *D) { |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame^] | 145 | if (ShouldGenerateLocation(D) && GenLoc(D)) |
| 146 | return; |
Ted Kremenek | cf99910 | 2010-04-29 17:43:29 +0000 | [diff] [blame] | 147 | |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame^] | 148 | VisitDeclContext(D->getDeclContext()); |
| 149 | Out << "@F@" << D->getNameAsString(); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 150 | } |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 151 | |
| 152 | void USRGenerator::VisitNamedDecl(NamedDecl *D) { |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 153 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 154 | const std::string &s = D->getNameAsString(); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 155 | // The string can be empty if the declaration has no name; e.g., it is |
| 156 | // the ParmDecl with no name for declaration of a function pointer type, e.g.: |
| 157 | // void (*f)(void *); |
| 158 | // In this case, don't generate a USR. |
| 159 | if (s.empty()) |
| 160 | IgnoreResults = true; |
| 161 | else |
| 162 | GenNamedDecl(s); |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 165 | void USRGenerator::VisitVarDecl(VarDecl *D) { |
| 166 | // VarDecls can be declared 'extern' within a function or method body, |
| 167 | // but their enclosing DeclContext is the function, not the TU. We need |
| 168 | // to check the storage class to correctly generate the USR. |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame^] | 169 | if (ShouldGenerateLocation(D) && GenLoc(D)) |
| 170 | return; |
| 171 | |
| 172 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 173 | |
Ted Kremenek | cf99910 | 2010-04-29 17:43:29 +0000 | [diff] [blame] | 174 | // Variables always have simple names. |
| 175 | llvm::StringRef s = D->getName(); |
| 176 | |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 177 | // The string can be empty if the declaration has no name; e.g., it is |
| 178 | // the ParmDecl with no name for declaration of a function pointer type, e.g.: |
| 179 | // void (*f)(void *); |
| 180 | // In this case, don't generate a USR. |
| 181 | if (s.empty()) |
| 182 | IgnoreResults = true; |
| 183 | else |
| 184 | GenNamedDecl(s); |
| 185 | } |
| 186 | |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 187 | void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) { |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame^] | 188 | if (D->isAnonymousNamespace()) { |
| 189 | Out << "@aN"; |
| 190 | return; |
| 191 | } |
| 192 | |
Ted Kremenek | 2fee4e6 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 193 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame^] | 194 | if (!IgnoreResults) |
| 195 | Out << "@N@" << D->getName(); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 198 | void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 199 | Visit(cast<Decl>(D->getDeclContext())); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 200 | GenObjCMethod(DeclarationName(D->getSelector()).getAsString(), |
| 201 | D->isInstanceMethod()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 204 | void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) { |
| 205 | // FIXME: @class declarations can refer to multiple classes. We need |
| 206 | // to be able to traverse these. |
| 207 | IgnoreResults = true; |
| 208 | } |
| 209 | |
| 210 | void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 211 | // FIXME: @protocol declarations can refer to multiple protocols. We need |
| 212 | // to be able to traverse these. |
| 213 | IgnoreResults = true; |
| 214 | } |
| 215 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 216 | void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
| 217 | switch (D->getKind()) { |
| 218 | default: |
| 219 | assert(false && "Invalid ObjC container."); |
| 220 | case Decl::ObjCInterface: |
| 221 | case Decl::ObjCImplementation: |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 222 | GenObjCClass(D->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 223 | break; |
| 224 | case Decl::ObjCCategory: { |
| 225 | ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D); |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 226 | ObjCInterfaceDecl *ID = CD->getClassInterface(); |
| 227 | if (!ID) { |
| 228 | // Handle invalid code where the @interface might not |
| 229 | // have been specified. |
| 230 | // FIXME: We should be able to generate this USR even if the |
| 231 | // @interface isn't available. |
| 232 | IgnoreResults = true; |
| 233 | return; |
| 234 | } |
| 235 | GenObjCCategory(ID->getName(), CD->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 236 | break; |
| 237 | } |
| 238 | case Decl::ObjCCategoryImpl: { |
| 239 | ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D); |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 240 | ObjCInterfaceDecl *ID = CD->getClassInterface(); |
| 241 | if (!ID) { |
| 242 | // Handle invalid code where the @interface might not |
| 243 | // have been specified. |
| 244 | // FIXME: We should be able to generate this USR even if the |
| 245 | // @interface isn't available. |
| 246 | IgnoreResults = true; |
| 247 | return; |
| 248 | } |
| 249 | GenObjCCategory(ID->getName(), CD->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 250 | break; |
| 251 | } |
| 252 | case Decl::ObjCProtocol: |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 253 | GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 254 | break; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 259 | Visit(cast<Decl>(D->getDeclContext())); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 260 | GenObjCProperty(D->getName()); |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 263 | void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { |
| 264 | if (ObjCPropertyDecl *PD = D->getPropertyDecl()) { |
| 265 | VisitObjCPropertyDecl(PD); |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | IgnoreResults = true; |
| 270 | } |
| 271 | |
Ted Kremenek | b82b3be | 2010-01-18 22:42:20 +0000 | [diff] [blame] | 272 | void USRGenerator::VisitTagDecl(TagDecl *D) { |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame^] | 273 | // Add the location of the tag decl to handle resolution across |
| 274 | // translation units. |
| 275 | if (ShouldGenerateLocation(D) && GenLoc(D)) |
| 276 | return; |
| 277 | |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 278 | D = D->getCanonicalDecl(); |
Ted Kremenek | b82b3be | 2010-01-18 22:42:20 +0000 | [diff] [blame] | 279 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame^] | 280 | |
Ted Kremenek | b82b3be | 2010-01-18 22:42:20 +0000 | [diff] [blame] | 281 | switch (D->getTagKind()) { |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 282 | case TagDecl::TK_struct: Out << "@S"; break; |
| 283 | case TagDecl::TK_class: Out << "@C"; break; |
| 284 | case TagDecl::TK_union: Out << "@U"; break; |
| 285 | case TagDecl::TK_enum: Out << "@E"; break; |
| 286 | } |
| 287 | |
| 288 | const std::string &s = D->getNameAsString(); |
| 289 | const TypedefDecl *TD = 0; |
| 290 | if (s.empty()) { |
| 291 | TD = D->getTypedefForAnonDecl(); |
| 292 | Out << (TD ? 'A' : 'a'); |
Ted Kremenek | b82b3be | 2010-01-18 22:42:20 +0000 | [diff] [blame] | 293 | } |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 294 | |
Ted Kremenek | c5b48b3 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 295 | if (s.empty()) { |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 296 | if (TD) |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 297 | Out << '@' << TD; |
Ted Kremenek | c5b48b3 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 298 | } |
| 299 | else |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 300 | Out << '@' << s; |
Ted Kremenek | c5b48b3 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 303 | void USRGenerator::VisitTypedefDecl(TypedefDecl *D) { |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame^] | 304 | if (ShouldGenerateLocation(D) && GenLoc(D)) |
| 305 | return; |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 306 | DeclContext *DC = D->getDeclContext(); |
| 307 | if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC)) |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 308 | Visit(DCN); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 309 | Out << "@T@"; |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 310 | Out << D->getName(); |
| 311 | } |
| 312 | |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame^] | 313 | bool USRGenerator::GenLoc(const Decl *D) { |
| 314 | if (generatedLoc) |
| 315 | return IgnoreResults; |
| 316 | generatedLoc = true; |
| 317 | |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 318 | const SourceManager &SM = AU->getSourceManager(); |
| 319 | SourceLocation L = D->getLocStart(); |
| 320 | if (L.isInvalid()) { |
| 321 | IgnoreResults = true; |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame^] | 322 | return true; |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 323 | } |
| 324 | L = SM.getInstantiationLoc(L); |
| 325 | const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L); |
| 326 | const FileEntry *FE = SM.getFileEntryForID(Decomposed.first); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 327 | if (FE) { |
| 328 | llvm::sys::Path P(FE->getName()); |
| 329 | Out << P.getLast(); |
| 330 | } |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 331 | else { |
| 332 | // This case really isn't interesting. |
| 333 | IgnoreResults = true; |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame^] | 334 | return true; |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 335 | } |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 336 | Out << '@' |
| 337 | << SM.getLineNumber(Decomposed.first, Decomposed.second) << ':' |
| 338 | << SM.getColumnNumber(Decomposed.first, Decomposed.second); |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame^] | 339 | |
| 340 | return IgnoreResults; |
Ted Kremenek | c50277f | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 343 | //===----------------------------------------------------------------------===// |
| 344 | // General purpose USR generation methods. |
| 345 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 346 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 347 | void USRGenerator::GenNamedDecl(llvm::StringRef name) { |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 348 | Out << "@" << name; |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void USRGenerator::GenObjCClass(llvm::StringRef cls) { |
| 352 | Out << "objc(cs)" << cls; |
| 353 | } |
| 354 | |
| 355 | void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) { |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 356 | Out << "objc(cy)" << cls << '@' << cat; |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | void USRGenerator::GenObjCIvar(llvm::StringRef ivar) { |
| 360 | GenNamedDecl(ivar); |
| 361 | } |
| 362 | |
| 363 | void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) { |
| 364 | Out << (isInstanceMethod ? "(im)" : "(cm)") << meth; |
| 365 | } |
| 366 | |
| 367 | void USRGenerator::GenObjCProperty(llvm::StringRef prop) { |
| 368 | Out << "(py)" << prop; |
| 369 | } |
| 370 | |
| 371 | void USRGenerator::GenObjCProtocol(llvm::StringRef prot) { |
| 372 | Out << "objc(pl)" << prot; |
| 373 | } |
| 374 | |
| 375 | //===----------------------------------------------------------------------===// |
| 376 | // API hooks. |
| 377 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 378 | |
Benjamin Kramer | cfb51b6 | 2010-04-08 15:54:07 +0000 | [diff] [blame] | 379 | static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) { |
| 380 | return s.startswith("c:") ? s.substr(2) : ""; |
| 381 | } |
| 382 | |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 383 | static CXString getDeclCursorUSR(const CXCursor &C) { |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 384 | Decl *D = cxcursor::getCursorDecl(C); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 385 | |
| 386 | // Don't generate USRs for things with invalid locations. |
| 387 | if (!D || D->getLocStart().isInvalid()) |
Ted Kremenek | 1af0a2a | 2010-04-17 00:21:38 +0000 | [diff] [blame] | 388 | return createCXString(""); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 389 | |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 390 | // Check if the cursor has 'NoLinkage'. |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 391 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 392 | switch (ND->getLinkage()) { |
| 393 | case ExternalLinkage: |
| 394 | // Generate USRs for all entities with external linkage. |
| 395 | break; |
| 396 | case NoLinkage: |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame^] | 397 | case UniqueExternalLinkage: |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 398 | // We allow enums, typedefs, and structs that have no linkage to |
| 399 | // have USRs that are anchored to the file they were defined in |
| 400 | // (e.g., the header). This is a little gross, but in principal |
| 401 | // enums/anonymous structs/etc. defined in a common header file |
| 402 | // are referred to across multiple translation units. |
Ted Kremenek | 6f15395 | 2010-04-15 21:51:13 +0000 | [diff] [blame] | 403 | if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) || |
Ted Kremenek | cf99910 | 2010-04-29 17:43:29 +0000 | [diff] [blame] | 404 | isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) || |
Ted Kremenek | cbd66f0 | 2010-05-06 23:38:28 +0000 | [diff] [blame^] | 405 | isa<VarDecl>(ND) || isa<NamespaceDecl>(ND)) |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 406 | break; |
| 407 | // Fall-through. |
| 408 | case InternalLinkage: |
Ted Kremenek | cf99910 | 2010-04-29 17:43:29 +0000 | [diff] [blame] | 409 | if (isa<FunctionDecl>(ND)) |
| 410 | break; |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | StringUSRGenerator SUG(&C); |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 414 | SUG->Visit(D); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 415 | |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 416 | if (SUG->ignoreResults()) |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 417 | return createCXString(""); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 418 | |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 419 | // For development testing. |
| 420 | // assert(SUG.str().size() > 2); |
| 421 | |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 422 | // 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] | 423 | return createCXString(SUG.str(), true); |
| 424 | } |
| 425 | |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 426 | extern "C" { |
| 427 | |
| 428 | CXString clang_getCursorUSR(CXCursor C) { |
Ted Kremenek | fa8231d | 2010-04-11 22:20:34 +0000 | [diff] [blame] | 429 | const CXCursorKind &K = clang_getCursorKind(C); |
| 430 | |
| 431 | if (clang_isDeclaration(K)) |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 432 | return getDeclCursorUSR(C); |
Ted Kremenek | fa8231d | 2010-04-11 22:20:34 +0000 | [diff] [blame] | 433 | |
| 434 | if (K == CXCursor_MacroDefinition) { |
Ted Kremenek | 1865cfe | 2010-04-15 21:04:25 +0000 | [diff] [blame] | 435 | StringUSRGenerator SUG(&C); |
Ted Kremenek | fa8231d | 2010-04-11 22:20:34 +0000 | [diff] [blame] | 436 | SUG << "macro@" |
| 437 | << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart(); |
| 438 | return createCXString(SUG.str(), true); |
| 439 | } |
| 440 | |
Ted Kremenek | c3ef91d | 2010-04-11 22:20:26 +0000 | [diff] [blame] | 441 | return createCXString(""); |
| 442 | } |
| 443 | |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 444 | CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) { |
| 445 | StringUSRGenerator SUG; |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 446 | SUG << extractUSRSuffix(clang_getCString(classUSR)); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 447 | SUG->GenObjCIvar(name); |
| 448 | return createCXString(SUG.str(), true); |
| 449 | } |
| 450 | |
| 451 | CXString clang_constructUSR_ObjCMethod(const char *name, |
| 452 | unsigned isInstanceMethod, |
| 453 | CXString classUSR) { |
| 454 | StringUSRGenerator SUG; |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 455 | SUG << extractUSRSuffix(clang_getCString(classUSR)); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 456 | SUG->GenObjCMethod(name, isInstanceMethod); |
| 457 | return createCXString(SUG.str(), true); |
| 458 | } |
| 459 | |
| 460 | CXString clang_constructUSR_ObjCClass(const char *name) { |
| 461 | StringUSRGenerator SUG; |
| 462 | SUG->GenObjCClass(name); |
| 463 | return createCXString(SUG.str(), true); |
| 464 | } |
| 465 | |
| 466 | CXString clang_constructUSR_ObjCProtocol(const char *name) { |
| 467 | StringUSRGenerator SUG; |
| 468 | SUG->GenObjCProtocol(name); |
| 469 | return createCXString(SUG.str(), true); |
| 470 | } |
| 471 | |
Ted Kremenek | 66ccaec | 2010-03-15 17:38:58 +0000 | [diff] [blame] | 472 | CXString clang_constructUSR_ObjCCategory(const char *class_name, |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 473 | const char *category_name) { |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 474 | StringUSRGenerator SUG; |
| 475 | SUG->GenObjCCategory(class_name, category_name); |
| 476 | return createCXString(SUG.str(), true); |
| 477 | } |
| 478 | |
| 479 | CXString clang_constructUSR_ObjCProperty(const char *property, |
| 480 | CXString classUSR) { |
| 481 | StringUSRGenerator SUG; |
Ted Kremenek | 0c0fb41 | 2010-03-25 02:00:36 +0000 | [diff] [blame] | 482 | SUG << extractUSRSuffix(clang_getCString(classUSR)); |
Ted Kremenek | 896b70f | 2010-03-13 02:50:34 +0000 | [diff] [blame] | 483 | SUG->GenObjCProperty(property); |
| 484 | return createCXString(SUG.str(), true); |
Ted Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 485 | } |
Ted Kremenek | 1b6869a | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 486 | |
Ted Kremenek | 1b6869a | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 487 | } // end extern "C" |