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