Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 1 | //===- CIndex.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 Clang-C Source Indexing library. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang-c/Index.h" |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 15 | #include "clang/Index/Program.h" |
| 16 | #include "clang/Index/Indexer.h" |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclVisitor.h" |
Benjamin Kramer | d01a0bc | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 18 | #include "clang/Basic/FileManager.h" |
| 19 | #include "clang/Frontend/ASTUnit.h" |
| 20 | #include <cstdio> |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | using namespace idx; |
| 23 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 24 | extern "C" { |
Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 25 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 26 | CXIndex clang_createIndex() |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 27 | { |
| 28 | return new Indexer(*new Program(), *new FileManager()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 31 | // FIXME: need to pass back error info. |
| 32 | CXTranslationUnit clang_createTranslationUnit( |
| 33 | CXIndex CIdx, const char *ast_filename) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 34 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 35 | assert(CIdx && "Passed null CXIndex"); |
| 36 | Indexer *CXXIdx = static_cast<Indexer *>(CIdx); |
| 37 | std::string astName(ast_filename); |
| 38 | std::string ErrMsg; |
| 39 | |
| 40 | return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getFileManager(), &ErrMsg); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 43 | namespace { |
| 44 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 45 | class IdxVisitor : public DeclVisitor<IdxVisitor> { |
| 46 | public: |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 47 | void VisitNamedDecl(NamedDecl *ND) { |
| 48 | printf("NamedDecl (%s:", ND->getDeclKindName()); |
| 49 | if (ND->getIdentifier()) |
| 50 | printf("%s)\n", ND->getIdentifier()->getName()); |
| 51 | else |
| 52 | printf("<no name>)\n"); |
| 53 | } |
| 54 | }; |
| 55 | |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 58 | void clang_loadTranslationUnit( |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 59 | CXTranslationUnit CTUnit, void (*callback)(CXTranslationUnit, CXCursor)) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 60 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 61 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 62 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 63 | ASTContext &Ctx = CXXUnit->getASTContext(); |
| 64 | |
| 65 | IdxVisitor DVisit; |
| 66 | DVisit.Visit(Ctx.getTranslationUnitDecl()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void clang_loadDeclaration(CXDecl, void (*callback)(CXDecl, CXCursor)) |
| 70 | { |
| 71 | } |
| 72 | |
Steve Naroff | 7e8f818 | 2009-08-28 12:07:44 +0000 | [diff] [blame] | 73 | // Some notes on CXEntity: |
| 74 | // |
| 75 | // - Since the 'ordinary' namespace includes functions, data, typedefs, |
| 76 | // ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one |
| 77 | // entity for 2 different types). For example: |
| 78 | // |
| 79 | // module1.m: @interface Foo @end Foo *x; |
| 80 | // module2.m: void Foo(int); |
| 81 | // |
| 82 | // - Since the unique name spans translation units, static data/functions |
| 83 | // within a CXTranslationUnit are *not* currently represented by entities. |
| 84 | // As a result, there will be no entity for the following: |
| 85 | // |
| 86 | // module.m: static void Foo() { } |
| 87 | // |
| 88 | |
| 89 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 90 | const char *clang_getDeclarationName(CXEntity) |
| 91 | { |
| 92 | return ""; |
| 93 | } |
| 94 | const char *clang_getURI(CXEntity) |
| 95 | { |
| 96 | return ""; |
| 97 | } |
| 98 | |
| 99 | CXEntity clang_getEntity(const char *URI) |
| 100 | { |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | // |
| 105 | // CXDecl Operations. |
| 106 | // |
| 107 | CXCursor clang_getCursorFromDecl(CXDecl) |
| 108 | { |
| 109 | return 0; |
| 110 | } |
| 111 | CXEntity clang_getEntityFromDecl(CXDecl) |
| 112 | { |
| 113 | return 0; |
| 114 | } |
| 115 | enum CXDeclKind clang_getDeclKind(CXDecl) |
| 116 | { |
| 117 | return CXDecl_any; |
| 118 | } |
| 119 | const char *clang_getDeclSpelling(CXDecl) |
| 120 | { |
| 121 | return ""; |
| 122 | } |
| 123 | // |
| 124 | // CXCursor Operations. |
| 125 | // |
| 126 | CXCursor clang_getCursor(CXTranslationUnit, const char *source_name, |
| 127 | unsigned line, unsigned column) |
| 128 | { |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | CXCursorKind clang_getCursorKind(CXCursor) |
| 133 | { |
Steve Naroff | 7e8f818 | 2009-08-28 12:07:44 +0000 | [diff] [blame] | 134 | return CXCursor_Declaration; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | unsigned clang_getCursorLine(CXCursor) |
| 138 | { |
| 139 | return 0; |
| 140 | } |
| 141 | unsigned clang_getCursorColumn(CXCursor) |
| 142 | { |
| 143 | return 0; |
| 144 | } |
| 145 | const char *clang_getCursorSource(CXCursor) |
| 146 | { |
| 147 | return ""; |
| 148 | } |
| 149 | |
| 150 | // If CXCursorKind == Cursor_Reference, then this will return the referenced declaration. |
| 151 | // If CXCursorKind == Cursor_Declaration, then this will return the declaration. |
| 152 | CXDecl clang_getCursorDecl(CXCursor) |
| 153 | { |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | } // end extern "C" |