3 <title>The Index Library
</title>
4 <link type=
"text/css" rel=
"stylesheet" href=
"../menu.css" />
5 <link type=
"text/css" rel=
"stylesheet" href=
"../content.css" />
6 <style type=
"text/css">
15 <!--#include virtual="../menu.html.incl"-->
19 <h1>The Index Library
</h1>
21 <p><b>Table of Contents
</b></p>
23 <li><a href=
"#philosophy">Design Philosophy
</a></li>
24 <li><a href=
"#classes">Classes
</a>
26 <li><a href=
"#entity">Entity
</a></li>
27 <li><a href=
"#astlocation">ASTLocation
</a></li>
28 <li><a href=
"#declreferencemap">DeclReferenceMap
</a></li>
31 <li><a href=
"#functions">Functions
</a>
33 <li><a href=
"#resolveloc">ResolveLocationInAST
</a></li>
36 <li><a href=
"#astfiles">AST Files
</a></li>
37 <li><a href=
"#indextest">index-test tool
</a>
39 <li><a href=
"#indextestusage">Usage
</a></li>
40 <li><a href=
"#indextestexamples">Examples
</a></li>
45 <h2 id=
"philosophy">Design Philosophy
</h2>
47 <p> The Index library is meant to provide the basic infrastructure for
48 cross-translation-unit analysis and is primarily focused on indexing
49 related functionality. It provides an API for clients that need to
50 accurately map the AST nodes of the ASTContext to the locations in the source files.
51 It also allows them to analyze information across multiple translation units.
</p>
53 <p>As a
"general rule", ASTContexts are considered the primary source of
54 information that a client wants about a translation unit. There will be no such class as an
55 "indexing database" that stores, for example, source locations of identifiers separately from ASTContext.
56 All the information that a client needs from a translation unit will be extracted from the ASTContext.
</p>
58 <h2 id=
"classes">Classes
</h2>
60 <h3 id=
"entity">Entity
</h3>
62 <p>To be able to reason about semantically the same Decls that are contained in multiple ASTContexts, the 'Entity' class was introduced.
63 An Entity is an ASTContext-independent
"token" that can be created from a Decl (and a typename in the future) with
64 the purpose to
"resolve" it into a Decl belonging to another ASTContext. Some examples to make the concept of Entities more clear:
</p>
68 <pre class=
"code_example">
76 <pre class=
"code_example">
83 Translation unit
<code>t1.c
</code> contains
2 Entities
<code>foo
</code> and
<code>bar
</code>, while
<code>t2.c
</code> contains
1 Entity
<code>foo
</code>.
84 Entities are uniqued in such a way that the Entity* pointer for
<code>t1.c/foo
</code> is the same as the Entity* pointer for
<code>t2.c/foo
</code>.
85 An Entity doesn't convey any information about the declaration, it is more like an opaque pointer used only to get the
86 associated Decl out of an ASTContext so that the actual information for the declaration can be accessed.
87 Another important aspect of Entities is that they can only be created/associated for declarations that are visible outside the
88 translation unit. This means that for:
92 <pre class=
"code_example">
93 static void foo(void);
97 there can be no Entity (if you ask for the Entity* of the static function
<code>foo
</code> you'll get a null pointer).
98 This is for
2 reasons:
100 <li>To preserve the invariant that the same Entity* pointers refer to the same semantic Decls.
101 In the above example
<code>t1.c/foo
</code> and
<code>t2.c/foo
</code> are the same, while
<code>t3.c/foo
</code> is different.
</li>
102 <li>The purpose of Entity is to get the same semantic Decl from multiple ASTContexts. For a Decl that is not visible
103 outside of its own translation unit, you don't need an Entity since it won't appear in another ASTContext.
</li>
107 <h3 id=
"astlocation">ASTLocation
</h3>
109 Encapsulates a
"point" in the AST tree of the ASTContext.
110 It represents either a Decl*, or a Stmt* along with its immediate Decl* parent.
111 An example for its usage is that libIndex will provide the references of
<code>foo
</code> in the form of ASTLocations,
112 "pointing" at the expressions that reference
<code>foo
</code>.
114 <h3 id=
"declreferencemap">DeclReferenceMap
</h3>
116 Accepts an ASTContext and creates a mapping from NamedDecls to the ASTLocations that reference them (in the same ASTContext).
118 <h2 id=
"functions">Functions
</h2>
120 <h3 id=
"resolveloc">ResolveLocationInAST
</h3>
122 A function that accepts an ASTContext and a SourceLocation which it resolves into an ASTLocation.
124 <h2 id=
"astfiles">AST Files
</h2>
126 The precompiled headers implementation of clang (
<a href=
"http://clang.llvm.org/docs/PCHInternals.html">PCH
</a>) is ideal for storing an ASTContext in a compact form that
127 will be loaded later for AST analysis. An
"AST file" refers to a translation unit that was
"compiled" into a precompiled header file.
129 <h2 id=
"indextest">index-test tool
</h2>
131 <h3 id=
"indextestusage">Usage
</h3>
133 A command-line tool that exercises the libIndex API, useful for testing its features.
134 As input it accepts multiple AST files (representing multiple translation units) and a few options:
137 <pre class=
"code_example">
138 -point-at [file:line:column]
140 Resolves a [file:line:column] triplet into a ASTLocation from the first AST file. If no other option is specified, it prints the ASTLocation.
141 It also prints a declaration's associated doxygen comment, if one is available.
145 <pre class=
"code_example">
148 Prints the ASTLocations that reference the declaration that was resolved out of the [file:line:column] triplet
152 <pre class=
"code_example">
155 Prints the ASTLocations that define the resolved declaration
159 <pre class=
"code_example">
162 Prints the ASTLocations that declare the resolved declaration
165 <h3 id=
"indextestexamples">Examples
</h3>
168 Here's an example of using index-test:
177 <pre class=
"code_example">
178 extern int global_var;
180 void foo_func(int param1);
185 <pre class=
"code_example">
188 void foo_func(int param1) {
189 int local_var = global_var;
190 for (int for_var =
100; for_var <
500; ++for_var) {
191 local_var = param1 + for_var;
198 <pre class=
"code_example">
203 void bar_func(void) {
205 foo_func(global_var);
211 You first get AST files out of
<code>t1.c
</code> and
<code>t2.c
</code>:
213 <pre class=
"code_example">
214 $ clang -emit-ast t1.c -o t1.ast
215 $ clang -emit-ast t2.c -o t2.ast
220 Find the ASTLocation under this position of
<code>t1.c
</code>:
221 <pre class=
"code_example">
223 void foo_func(int param1) {
224 int local_var = global_var;
229 <pre class=
"code_example">
230 $ index-test t1.ast -point-at t1.c:
4:
23
231 > [Decl: Var local_var | Stmt: DeclRefExpr global_var]
<t1.c:
4:
19, t1.c:
4:
19>
236 Find the declaration:
238 <pre class=
"code_example">
239 $ index-test t1.ast -point-at t1.c:
4:
23 -print-decls
240 > [Decl: Var global_var]
<foo.h:
1:
12, foo.h:
1:
12>
247 <pre class=
"code_example">
248 $ index-test t1.ast t2.ast -point-at t1.c:
4:
23 -print-refs
249 > [Decl: Var local_var | Stmt: DeclRefExpr global_var]
<t1.c:
4:
19, t1.c:
4:
19>
250 > [Decl: Function bar_func | Stmt: DeclRefExpr global_var]
<t2.c:
6:
3, t2.c:
6:
3>
251 > [Decl: Function bar_func | Stmt: DeclRefExpr global_var]
<t2.c:
7:
12, t2.c:
7:
12>
258 <pre class=
"code_example">
259 $ index-test t1.ast t2.ast -point-at t1.c:
4:
23 -print-defs
260 > [Decl: Var global_var]
<t2.c:
3:
5, t2.c:
3:
18>