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