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