Ted Kremenek | 9cd9f6d | 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 | 473c7a7 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 15 | #include "CXCursor.h" |
Benjamin Kramer | 59617be | 2010-01-12 11:32:40 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclVisitor.h" |
Ted Kremenek | 5631d2d | 2010-01-12 02:07:58 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 59617be | 2010-01-12 11:32:40 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 9cd9f6d | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 19 | |
| 20 | // Some notes on CXEntity: |
| 21 | // |
| 22 | // - Since the 'ordinary' namespace includes functions, data, typedefs, |
| 23 | // ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one |
| 24 | // entity for 2 different types). For example: |
| 25 | // |
| 26 | // module1.m: @interface Foo @end Foo *x; |
| 27 | // module2.m: void Foo(int); |
| 28 | // |
| 29 | // - Since the unique name spans translation units, static data/functions |
| 30 | // within a CXTranslationUnit are *not* currently represented by entities. |
| 31 | // As a result, there will be no entity for the following: |
| 32 | // |
| 33 | // module.m: static void Foo() { } |
| 34 | // |
Ted Kremenek | e5f86be | 2010-01-11 23:56:39 +0000 | [diff] [blame] | 35 | |
| 36 | static inline Entity GetEntity(const CXEntity &E) { |
| 37 | return Entity::getFromOpaquePtr(E.data); |
| 38 | } |
| 39 | |
| 40 | static inline ASTUnit *GetTranslationUnit(CXTranslationUnit TU) { |
| 41 | return (ASTUnit*) TU; |
Ted Kremenek | 9cd9f6d | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Ted Kremenek | e5f86be | 2010-01-11 23:56:39 +0000 | [diff] [blame] | 44 | static inline ASTContext &GetASTContext(CXTranslationUnit TU) { |
| 45 | return GetTranslationUnit(TU)->getASTContext(); |
| 46 | } |
| 47 | |
| 48 | static inline CXEntity NullCXEntity() { |
| 49 | CXEntity CE; |
| 50 | CE.index = NULL; |
| 51 | CE.data = NULL; |
| 52 | return CE; |
| 53 | } |
| 54 | |
| 55 | static inline CXEntity MakeEntity(CXIndex CIdx, const Entity &E) { |
| 56 | CXEntity CE; |
| 57 | CE.index = CIdx; |
| 58 | CE.data = E.getAsOpaquePtr(); |
| 59 | return CE; |
| 60 | } |
| 61 | |
| 62 | static inline Program &GetProgram(CXIndex CIdx) { |
| 63 | return ((CIndexer*) CIdx)->getProgram(); |
| 64 | } |
Benjamin Kramer | 59617be | 2010-01-12 11:32:40 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | cb674f9 | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 66 | //===----------------------------------------------------------------------===// |
| 67 | // USR generation. |
| 68 | //===----------------------------------------------------------------------===// |
| 69 | |
| 70 | namespace { |
Ted Kremenek | e1b5525 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 71 | class USRGenerator : public DeclVisitor<USRGenerator> { |
| 72 | llvm::raw_ostream &Out; |
Ted Kremenek | a6980af | 2010-01-18 22:02:49 +0000 | [diff] [blame^] | 73 | bool IgnoreResults; |
Ted Kremenek | e1b5525 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 74 | public: |
Ted Kremenek | a6980af | 2010-01-18 22:02:49 +0000 | [diff] [blame^] | 75 | USRGenerator(llvm::raw_ostream &out) : Out(out), IgnoreResults(false) {} |
| 76 | |
| 77 | bool ignoreResults() const { return IgnoreResults; } |
Ted Kremenek | e1b5525 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 78 | |
| 79 | void VisitBlockDecl(BlockDecl *D); |
| 80 | void VisitDeclContext(DeclContext *D); |
Ted Kremenek | 8433d1d | 2010-01-15 20:04:31 +0000 | [diff] [blame] | 81 | void VisitEnumDecl(EnumDecl *D); |
Ted Kremenek | a6980af | 2010-01-18 22:02:49 +0000 | [diff] [blame^] | 82 | void VisitFieldDecl(FieldDecl *D); |
Ted Kremenek | e1b5525 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 83 | void VisitFunctionDecl(FunctionDecl *D); |
| 84 | void VisitNamedDecl(NamedDecl *D); |
| 85 | void VisitNamespaceDecl(NamespaceDecl *D); |
| 86 | void VisitObjCContainerDecl(ObjCContainerDecl *CD); |
| 87 | void VisitObjCMethodDecl(ObjCMethodDecl *MD); |
| 88 | void VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
| 89 | void VisitRecordDecl(RecordDecl *D); |
Ted Kremenek | 6b1d550 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 90 | void VisitTagDeclCommon(TagDecl *D); |
Ted Kremenek | e1b5525 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 91 | void VisitTypedefDecl(TypedefDecl *D); |
| 92 | }; |
Ted Kremenek | cb674f9 | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 93 | } // end anonymous namespace |
| 94 | |
Ted Kremenek | e1b5525 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 95 | void USRGenerator::VisitBlockDecl(BlockDecl *D) { |
| 96 | VisitDeclContext(D->getDeclContext()); |
| 97 | // FIXME: Better support for anonymous blocks. |
| 98 | Out << "@B^anon"; |
| 99 | } |
| 100 | |
| 101 | void USRGenerator::VisitDeclContext(DeclContext *DC) { |
| 102 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) |
| 103 | Visit(D); |
| 104 | } |
| 105 | |
Ted Kremenek | 8433d1d | 2010-01-15 20:04:31 +0000 | [diff] [blame] | 106 | void USRGenerator::VisitEnumDecl(EnumDecl *D) { |
| 107 | VisitDeclContext(D->getDeclContext()); |
| 108 | Out << "@E^"; |
Ted Kremenek | 6b1d550 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 109 | VisitTagDeclCommon(D); |
Ted Kremenek | 8433d1d | 2010-01-15 20:04:31 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Ted Kremenek | a6980af | 2010-01-18 22:02:49 +0000 | [diff] [blame^] | 112 | void USRGenerator::VisitFieldDecl(FieldDecl *D) { |
| 113 | const std::string &s = D->getNameAsString(); |
| 114 | if (s.empty()) { |
| 115 | // Bit fields can be anonymous. |
| 116 | IgnoreResults = true; |
| 117 | return; |
| 118 | } |
| 119 | VisitDeclContext(D->getDeclContext()); |
| 120 | Out << "@^FI^" << s; |
| 121 | } |
| 122 | |
Ted Kremenek | e1b5525 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 123 | void USRGenerator::VisitFunctionDecl(FunctionDecl *D) { |
| 124 | VisitDeclContext(D->getDeclContext()); |
| 125 | Out << "@F^" << D->getNameAsString(); |
| 126 | } |
Ted Kremenek | cb674f9 | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 127 | |
| 128 | void USRGenerator::VisitNamedDecl(NamedDecl *D) { |
Ted Kremenek | e1b5525 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 129 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | cb674f9 | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 130 | const std::string &s = D->getNameAsString(); |
| 131 | assert(!s.empty()); |
Ted Kremenek | e1b5525 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 132 | Out << "@^" << s; |
| 133 | } |
| 134 | |
| 135 | void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) { |
| 136 | VisitDeclContext(D->getDeclContext()); |
| 137 | Out << "@N^" << D->getNameAsString(); |
Ted Kremenek | cb674f9 | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void USRGenerator::VisitRecordDecl(RecordDecl *D) { |
Ted Kremenek | e1b5525 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 141 | VisitDeclContext(D->getDeclContext()); |
| 142 | Out << "@S^"; |
Ted Kremenek | 6b1d550 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 143 | VisitTagDeclCommon(D); |
Ted Kremenek | cb674f9 | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 147 | Visit(cast<Decl>(D->getDeclContext())); |
| 148 | Out << (D->isInstanceMethod() ? "(im)" : "(cm)"); |
| 149 | Out << DeclarationName(D->getSelector()).getAsString(); |
| 150 | } |
| 151 | |
| 152 | void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
| 153 | switch (D->getKind()) { |
| 154 | default: |
| 155 | assert(false && "Invalid ObjC container."); |
| 156 | case Decl::ObjCInterface: |
| 157 | case Decl::ObjCImplementation: |
| 158 | Out << "objc(cs)" << D->getName(); |
| 159 | break; |
| 160 | case Decl::ObjCCategory: { |
| 161 | ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D); |
| 162 | Out << "objc(cy)" << CD->getClassInterface()->getName() |
| 163 | << '_' << CD->getName(); |
| 164 | break; |
| 165 | } |
| 166 | case Decl::ObjCCategoryImpl: { |
| 167 | ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D); |
| 168 | Out << "objc(cy)" << CD->getClassInterface()->getName() |
| 169 | << '_' << CD->getName(); |
| 170 | break; |
| 171 | } |
| 172 | case Decl::ObjCProtocol: |
| 173 | Out << "objc(pl)" << cast<ObjCProtocolDecl>(D)->getName(); |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 179 | Visit(cast<Decl>(D->getDeclContext())); |
| 180 | Out << "(py)" << D->getName(); |
| 181 | } |
| 182 | |
Ted Kremenek | 6b1d550 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 183 | void USRGenerator::VisitTagDeclCommon(TagDecl *D) { |
| 184 | // FIXME: Better support for anonymous structures and enums. |
| 185 | const std::string &s = D->getNameAsString(); |
| 186 | if (s.empty()) { |
| 187 | if (TypedefDecl *TD = D->getTypedefForAnonDecl()) |
| 188 | Out << "^anontd^" << TD->getNameAsString(); |
| 189 | else |
| 190 | Out << "^anon"; |
| 191 | } |
| 192 | else |
| 193 | Out << s; |
| 194 | } |
| 195 | |
Ted Kremenek | cb674f9 | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 196 | void USRGenerator::VisitTypedefDecl(TypedefDecl *D) { |
| 197 | DeclContext *DC = D->getDeclContext(); |
| 198 | if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC)) |
| 199 | Visit(DCN); |
| 200 | Out << "typedef@" << D->getName(); |
| 201 | } |
| 202 | |
Ted Kremenek | 473c7a7 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 203 | // FIXME: This is a skeleton implementation. It will be overhauled. |
| 204 | static CXString ConstructUSR(Decl *D) { |
| 205 | llvm::SmallString<1024> StrBuf; |
| 206 | { |
| 207 | llvm::raw_svector_ostream Out(StrBuf); |
| 208 | USRGenerator UG(Out); |
| 209 | UG.Visit(static_cast<Decl*>(D)); |
Ted Kremenek | a6980af | 2010-01-18 22:02:49 +0000 | [diff] [blame^] | 210 | if (UG.ignoreResults()) |
| 211 | return CIndexer::createCXString(NULL); |
Ted Kremenek | 473c7a7 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | if (StrBuf.empty()) |
| 215 | return CIndexer::createCXString(NULL); |
| 216 | |
| 217 | // Return a copy of the string that must be disposed by the caller. |
| 218 | return CIndexer::createCXString(StrBuf.c_str(), true); |
| 219 | } |
| 220 | |
| 221 | |
Benjamin Kramer | 59617be | 2010-01-12 11:32:40 +0000 | [diff] [blame] | 222 | extern "C" { |
| 223 | |
Ted Kremenek | e5f86be | 2010-01-11 23:56:39 +0000 | [diff] [blame] | 224 | /// clang_getDeclaration() maps from a CXEntity to the matching CXDecl (if any) |
| 225 | /// in a specified translation unit. |
| 226 | CXDecl clang_getDeclaration(CXEntity CE, CXTranslationUnit TU) { |
| 227 | return (CXDecl) GetEntity(CE).getDecl(GetASTContext(TU)); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | CXEntity clang_getEntityFromDecl(CXIndex CIdx, CXDecl CE) { |
| 232 | if (Decl *D = (Decl *) CE) |
| 233 | return MakeEntity(CIdx, Entity::get(D, GetProgram(CIdx))); |
| 234 | return NullCXEntity(); |
| 235 | } |
Ted Kremenek | 5631d2d | 2010-01-12 02:07:58 +0000 | [diff] [blame] | 236 | |
Ted Kremenek | 473c7a7 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 237 | CXString clang_getCursorUSR(CXCursor C) { |
| 238 | if (Decl *D = cxcursor::getCursorDecl(C)) |
| 239 | return ConstructUSR(D); |
| 240 | |
| 241 | |
| 242 | return CIndexer::createCXString(NULL); |
| 243 | } |
Ted Kremenek | 9cd9f6d | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 244 | |
Ted Kremenek | 9cd9f6d | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 245 | } // end extern "C" |