blob: a02e80987b1ff1c43f1d14feb2d821540d6c09e0 [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"
Steve Naroff50398192009-08-28 15:28:48 +000015#include "clang/Index/Program.h"
16#include "clang/Index/Indexer.h"
Steve Naroff50398192009-08-28 15:28:48 +000017#include "clang/AST/DeclVisitor.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000018#include "clang/Basic/FileManager.h"
19#include "clang/Frontend/ASTUnit.h"
20#include <cstdio>
Steve Naroff50398192009-08-28 15:28:48 +000021using namespace clang;
22using namespace idx;
23
Steve Naroff600866c2009-08-27 19:51:58 +000024extern "C" {
Ted Kremenekd2fa5662009-08-26 22:36:44 +000025
Steve Naroff600866c2009-08-27 19:51:58 +000026CXIndex clang_createIndex()
Steve Naroff50398192009-08-28 15:28:48 +000027{
28 return new Indexer(*new Program(), *new FileManager());
Steve Naroff600866c2009-08-27 19:51:58 +000029}
30
Steve Naroff50398192009-08-28 15:28:48 +000031// FIXME: need to pass back error info.
32CXTranslationUnit clang_createTranslationUnit(
33 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +000034{
Steve Naroff50398192009-08-28 15:28:48 +000035 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 Naroff600866c2009-08-27 19:51:58 +000041}
42
Daniel Dunbar1eb79b52009-08-28 16:30:07 +000043namespace {
44
Steve Naroff50398192009-08-28 15:28:48 +000045class IdxVisitor : public DeclVisitor<IdxVisitor> {
46public:
Steve Naroff50398192009-08-28 15:28:48 +000047 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 Dunbar1eb79b52009-08-28 16:30:07 +000056}
57
Steve Naroff600866c2009-08-27 19:51:58 +000058void clang_loadTranslationUnit(
Steve Naroff50398192009-08-28 15:28:48 +000059 CXTranslationUnit CTUnit, void (*callback)(CXTranslationUnit, CXCursor))
Steve Naroff600866c2009-08-27 19:51:58 +000060{
Steve Naroff50398192009-08-28 15:28:48 +000061 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 Naroff600866c2009-08-27 19:51:58 +000067}
68
69void clang_loadDeclaration(CXDecl, void (*callback)(CXDecl, CXCursor))
70{
71}
72
Steve Naroff7e8f8182009-08-28 12:07:44 +000073// 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 Naroff600866c2009-08-27 19:51:58 +000090const char *clang_getDeclarationName(CXEntity)
91{
92 return "";
93}
94const char *clang_getURI(CXEntity)
95{
96 return "";
97}
98
99CXEntity clang_getEntity(const char *URI)
100{
101 return 0;
102}
103
104//
105// CXDecl Operations.
106//
107CXCursor clang_getCursorFromDecl(CXDecl)
108{
109 return 0;
110}
111CXEntity clang_getEntityFromDecl(CXDecl)
112{
113 return 0;
114}
115enum CXDeclKind clang_getDeclKind(CXDecl)
116{
117 return CXDecl_any;
118}
119const char *clang_getDeclSpelling(CXDecl)
120{
121 return "";
122}
123//
124// CXCursor Operations.
125//
126CXCursor clang_getCursor(CXTranslationUnit, const char *source_name,
127 unsigned line, unsigned column)
128{
129 return 0;
130}
131
132CXCursorKind clang_getCursorKind(CXCursor)
133{
Steve Naroff7e8f8182009-08-28 12:07:44 +0000134 return CXCursor_Declaration;
Steve Naroff600866c2009-08-27 19:51:58 +0000135}
136
137unsigned clang_getCursorLine(CXCursor)
138{
139 return 0;
140}
141unsigned clang_getCursorColumn(CXCursor)
142{
143 return 0;
144}
145const 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.
152CXDecl clang_getCursorDecl(CXCursor)
153{
154 return 0;
155}
156
157} // end extern "C"