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; |
| 73 | public: |
| 74 | USRGenerator(llvm::raw_ostream &out) : Out(out) {} |
| 75 | |
| 76 | void VisitBlockDecl(BlockDecl *D); |
| 77 | void VisitDeclContext(DeclContext *D); |
Ted Kremenek | 8433d1d | 2010-01-15 20:04:31 +0000 | [diff] [blame] | 78 | void VisitEnumDecl(EnumDecl *D); |
Ted Kremenek | e1b5525 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 79 | void VisitFunctionDecl(FunctionDecl *D); |
| 80 | void VisitNamedDecl(NamedDecl *D); |
| 81 | void VisitNamespaceDecl(NamespaceDecl *D); |
| 82 | void VisitObjCContainerDecl(ObjCContainerDecl *CD); |
| 83 | void VisitObjCMethodDecl(ObjCMethodDecl *MD); |
| 84 | void VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
| 85 | void VisitRecordDecl(RecordDecl *D); |
Ted Kremenek | 6b1d550 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 86 | void VisitTagDeclCommon(TagDecl *D); |
Ted Kremenek | e1b5525 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 87 | void VisitTypedefDecl(TypedefDecl *D); |
| 88 | }; |
Ted Kremenek | cb674f9 | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 89 | } // end anonymous namespace |
| 90 | |
Ted Kremenek | e1b5525 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 91 | void USRGenerator::VisitBlockDecl(BlockDecl *D) { |
| 92 | VisitDeclContext(D->getDeclContext()); |
| 93 | // FIXME: Better support for anonymous blocks. |
| 94 | Out << "@B^anon"; |
| 95 | } |
| 96 | |
| 97 | void USRGenerator::VisitDeclContext(DeclContext *DC) { |
| 98 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) |
| 99 | Visit(D); |
| 100 | } |
| 101 | |
Ted Kremenek | 8433d1d | 2010-01-15 20:04:31 +0000 | [diff] [blame] | 102 | void USRGenerator::VisitEnumDecl(EnumDecl *D) { |
| 103 | VisitDeclContext(D->getDeclContext()); |
| 104 | Out << "@E^"; |
Ted Kremenek | 6b1d550 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 105 | VisitTagDeclCommon(D); |
Ted Kremenek | 8433d1d | 2010-01-15 20:04:31 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Ted Kremenek | e1b5525 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 108 | void USRGenerator::VisitFunctionDecl(FunctionDecl *D) { |
| 109 | VisitDeclContext(D->getDeclContext()); |
| 110 | Out << "@F^" << D->getNameAsString(); |
| 111 | } |
Ted Kremenek | cb674f9 | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 112 | |
| 113 | void USRGenerator::VisitNamedDecl(NamedDecl *D) { |
Ted Kremenek | e1b5525 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 114 | VisitDeclContext(D->getDeclContext()); |
Ted Kremenek | cb674f9 | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 115 | const std::string &s = D->getNameAsString(); |
| 116 | assert(!s.empty()); |
Ted Kremenek | e1b5525 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 117 | Out << "@^" << s; |
| 118 | } |
| 119 | |
| 120 | void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) { |
| 121 | VisitDeclContext(D->getDeclContext()); |
| 122 | Out << "@N^" << D->getNameAsString(); |
Ted Kremenek | cb674f9 | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void USRGenerator::VisitRecordDecl(RecordDecl *D) { |
Ted Kremenek | e1b5525 | 2010-01-14 01:50:21 +0000 | [diff] [blame] | 126 | VisitDeclContext(D->getDeclContext()); |
| 127 | Out << "@S^"; |
Ted Kremenek | 6b1d550 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 128 | VisitTagDeclCommon(D); |
Ted Kremenek | cb674f9 | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 132 | Visit(cast<Decl>(D->getDeclContext())); |
| 133 | Out << (D->isInstanceMethod() ? "(im)" : "(cm)"); |
| 134 | Out << DeclarationName(D->getSelector()).getAsString(); |
| 135 | } |
| 136 | |
| 137 | void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
| 138 | switch (D->getKind()) { |
| 139 | default: |
| 140 | assert(false && "Invalid ObjC container."); |
| 141 | case Decl::ObjCInterface: |
| 142 | case Decl::ObjCImplementation: |
| 143 | Out << "objc(cs)" << D->getName(); |
| 144 | break; |
| 145 | case Decl::ObjCCategory: { |
| 146 | ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D); |
| 147 | Out << "objc(cy)" << CD->getClassInterface()->getName() |
| 148 | << '_' << CD->getName(); |
| 149 | break; |
| 150 | } |
| 151 | case Decl::ObjCCategoryImpl: { |
| 152 | ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D); |
| 153 | Out << "objc(cy)" << CD->getClassInterface()->getName() |
| 154 | << '_' << CD->getName(); |
| 155 | break; |
| 156 | } |
| 157 | case Decl::ObjCProtocol: |
| 158 | Out << "objc(pl)" << cast<ObjCProtocolDecl>(D)->getName(); |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 164 | Visit(cast<Decl>(D->getDeclContext())); |
| 165 | Out << "(py)" << D->getName(); |
| 166 | } |
| 167 | |
Ted Kremenek | 6b1d550 | 2010-01-15 23:34:31 +0000 | [diff] [blame] | 168 | void USRGenerator::VisitTagDeclCommon(TagDecl *D) { |
| 169 | // FIXME: Better support for anonymous structures and enums. |
| 170 | const std::string &s = D->getNameAsString(); |
| 171 | if (s.empty()) { |
| 172 | if (TypedefDecl *TD = D->getTypedefForAnonDecl()) |
| 173 | Out << "^anontd^" << TD->getNameAsString(); |
| 174 | else |
| 175 | Out << "^anon"; |
| 176 | } |
| 177 | else |
| 178 | Out << s; |
| 179 | } |
| 180 | |
Ted Kremenek | cb674f9 | 2010-01-12 23:33:42 +0000 | [diff] [blame] | 181 | void USRGenerator::VisitTypedefDecl(TypedefDecl *D) { |
| 182 | DeclContext *DC = D->getDeclContext(); |
| 183 | if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC)) |
| 184 | Visit(DCN); |
| 185 | Out << "typedef@" << D->getName(); |
| 186 | } |
| 187 | |
Ted Kremenek | 473c7a7 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 188 | // FIXME: This is a skeleton implementation. It will be overhauled. |
| 189 | static CXString ConstructUSR(Decl *D) { |
| 190 | llvm::SmallString<1024> StrBuf; |
| 191 | { |
| 192 | llvm::raw_svector_ostream Out(StrBuf); |
| 193 | USRGenerator UG(Out); |
| 194 | UG.Visit(static_cast<Decl*>(D)); |
| 195 | } |
| 196 | |
| 197 | if (StrBuf.empty()) |
| 198 | return CIndexer::createCXString(NULL); |
| 199 | |
| 200 | // Return a copy of the string that must be disposed by the caller. |
| 201 | return CIndexer::createCXString(StrBuf.c_str(), true); |
| 202 | } |
| 203 | |
| 204 | |
Benjamin Kramer | 59617be | 2010-01-12 11:32:40 +0000 | [diff] [blame] | 205 | extern "C" { |
| 206 | |
Ted Kremenek | e5f86be | 2010-01-11 23:56:39 +0000 | [diff] [blame] | 207 | /// clang_getDeclaration() maps from a CXEntity to the matching CXDecl (if any) |
| 208 | /// in a specified translation unit. |
| 209 | CXDecl clang_getDeclaration(CXEntity CE, CXTranslationUnit TU) { |
| 210 | return (CXDecl) GetEntity(CE).getDecl(GetASTContext(TU)); |
| 211 | } |
| 212 | |
| 213 | |
| 214 | CXEntity clang_getEntityFromDecl(CXIndex CIdx, CXDecl CE) { |
| 215 | if (Decl *D = (Decl *) CE) |
| 216 | return MakeEntity(CIdx, Entity::get(D, GetProgram(CIdx))); |
| 217 | return NullCXEntity(); |
| 218 | } |
Ted Kremenek | 5631d2d | 2010-01-12 02:07:58 +0000 | [diff] [blame] | 219 | |
Ted Kremenek | 473c7a7 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 220 | CXString clang_getCursorUSR(CXCursor C) { |
| 221 | if (Decl *D = cxcursor::getCursorDecl(C)) |
| 222 | return ConstructUSR(D); |
| 223 | |
| 224 | |
| 225 | return CIndexer::createCXString(NULL); |
| 226 | } |
Ted Kremenek | 9cd9f6d | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 227 | |
Ted Kremenek | 9cd9f6d | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 228 | } // end extern "C" |