IndexingContext.h revision 4e7064fa7e344e8f87a5b8457e96dfdd252c4a9e
1//===- IndexingContext.h - Higher level API functions ------------------------===//
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#include "Index_Internal.h"
11#include "CXCursor.h"
12
13#include "clang/AST/Decl.h"
14#include "clang/AST/DeclGroup.h"
15#include "llvm/ADT/DenseMap.h"
16
17namespace clang {
18  class FileEntry;
19  class ObjCPropertyDecl;
20
21namespace cxindex {
22  class IndexingContext;
23
24class IndexingContext {
25  ASTContext *Ctx;
26  CXClientData ClientData;
27  IndexerCallbacks &CB;
28  unsigned IndexOptions;
29  CXTranslationUnit CXTU;
30
31  typedef llvm::DenseMap<const FileEntry *, CXIdxFile> FileMapTy;
32  typedef llvm::DenseMap<const NamedDecl *, CXIdxEntity> EntityMapTy;
33  typedef llvm::DenseMap<const void *, CXIdxMacro> MacroMapTy;
34  typedef llvm::DenseMap<const DeclContext *, CXIdxContainer> ContainerMapTy;
35  FileMapTy FileMap;
36  EntityMapTy EntityMap;
37  MacroMapTy MacroMap;
38  ContainerMapTy ContainerMap;
39
40  SmallVector<DeclGroupRef, 8> TUDeclsInObjCContainer;
41
42  llvm::SmallString<256> StrScratch;
43
44  class StrAdapter {
45    llvm::SmallString<256> &Scratch;
46
47  public:
48    StrAdapter(IndexingContext *indexCtx)
49      : Scratch(indexCtx->StrScratch) {}
50    ~StrAdapter() { Scratch.clear(); }
51
52    const char *toCStr(StringRef Str);
53
54    unsigned getCurSize() const { return Scratch.size(); }
55
56    const char *getCStr(unsigned CharIndex) {
57      Scratch.push_back('\0');
58      return Scratch.data() + CharIndex;
59    }
60
61    SmallVectorImpl<char> &getBuffer() { return Scratch; }
62  };
63
64public:
65  IndexingContext(CXClientData clientData, IndexerCallbacks &indexCallbacks,
66                  unsigned indexOptions, CXTranslationUnit cxTU)
67    : Ctx(0), ClientData(clientData), CB(indexCallbacks),
68      IndexOptions(indexOptions), CXTU(cxTU) { }
69
70  ASTContext &getASTContext() const { return *Ctx; }
71
72  void setASTContext(ASTContext &ctx);
73
74  void ppIncludedFile(SourceLocation hashLoc,
75                      StringRef filename, const FileEntry *File,
76                      bool isImport, bool isAngled);
77
78  void ppMacroDefined(SourceLocation Loc, StringRef Name,
79                      SourceLocation DefBegin, unsigned Length,
80                      const void *OpaqueMacro);
81
82  void ppMacroUndefined(SourceLocation Loc, StringRef Name,
83                        const void *OpaqueMacro);
84
85  void ppMacroExpanded(SourceLocation Loc, StringRef Name,
86                       const void *OpaqueMacro);
87
88  void invokeStartedTranslationUnit();
89
90  void invokeFinishedTranslationUnit();
91
92  void indexDecl(const Decl *D);
93
94  void indexTagDecl(const TagDecl *D);
95
96  void indexTypeSourceInfo(TypeSourceInfo *TInfo, const NamedDecl *Parent,
97                           const DeclContext *DC = 0);
98
99  void indexTypeLoc(TypeLoc TL, const NamedDecl *Parent,
100                           const DeclContext *DC);
101
102  void indexDeclContext(const DeclContext *DC);
103
104  void indexBody(const Stmt *S, const DeclContext *DC);
105
106  void handleDiagnostic(const StoredDiagnostic &StoredDiag);
107
108  void handleFunction(const FunctionDecl *FD);
109
110  void handleVar(const VarDecl *D);
111
112  void handleField(const FieldDecl *D);
113
114  void handleEnumerator(const EnumConstantDecl *D);
115
116  void handleTagDecl(const TagDecl *D);
117
118  void handleTypedef(const TypedefDecl *D);
119
120  void handleObjCInterface(const ObjCInterfaceDecl *D);
121
122  void defineObjCInterface(const ObjCInterfaceDecl *D);
123
124  void handleObjCProtocol(const ObjCProtocolDecl *D);
125
126  void handleObjCCategory(const ObjCCategoryDecl *D);
127
128  void handleObjCMethod(const ObjCMethodDecl *D);
129
130  void handleObjCProperty(const ObjCPropertyDecl *D);
131
132  void handleReference(const NamedDecl *D, SourceLocation Loc,
133                       const NamedDecl *Parent,
134                       const DeclContext *DC,
135                       const Expr *E = 0);
136
137  void invokeStartedTagTypeDefinition(const TagDecl *D);
138
139  void invokeStartedStatementBody(const NamedDecl *D, const DeclContext *DC);
140
141  void invokeStartedObjCContainer(const ObjCContainerDecl *D);
142
143  void invokeEndedContainer(const DeclContext *DC);
144
145  bool isNotFromSourceFile(SourceLocation Loc) const;
146
147  void indexTUDeclsInObjCContainer();
148  void indexDeclGroupRef(DeclGroupRef DG);
149
150  void addTUDeclInObjCContainer(DeclGroupRef DG) {
151    TUDeclsInObjCContainer.push_back(DG);
152  }
153
154  void translateLoc(SourceLocation Loc, CXIdxFile *indexFile, CXFile *file,
155                    unsigned *line, unsigned *column, unsigned *offset);
156
157private:
158  void addEntityInMap(const NamedDecl *D, CXIdxEntity entity);
159
160  void addContainerInMap(const DeclContext *DC, CXIdxContainer container);
161
162  CXIdxEntity getIndexEntity(const NamedDecl *D);
163
164  const NamedDecl *getEntityDecl(const NamedDecl *D) const;
165
166  CXIdxContainer getIndexContainer(const NamedDecl *D) const {
167    return getIndexContainerForDC(D->getDeclContext());
168  }
169
170  const DeclContext *getScopedContext(const DeclContext *DC) const;
171  CXIdxContainer getIndexContainerForDC(const DeclContext *DC) const;
172
173  CXIdxFile getIndexFile(const FileEntry *File);
174
175  CXIdxLoc getIndexLoc(SourceLocation Loc) const;
176
177  void getIndexedEntityInfo(const NamedDecl *D,
178                            CXIdxIndexedEntityInfo &IdxEntityInfo,
179                            CXIdxEntityInfo &EntityInfo,
180                            CXIdxIndexedDeclInfo &IdxDeclInfo,
181                            StrAdapter &SA);
182
183  void getIndexedDeclInfo(const NamedDecl *D,
184                          CXIdxIndexedDeclInfo &IdxDeclInfo);
185
186  void getIndexedRedeclInfo(const NamedDecl *D,
187                            CXIdxIndexedRedeclInfo &RedeclInfo,
188                            CXIdxIndexedDeclInfo &IdxDeclInfo);
189
190  void getContainerInfo(const NamedDecl *D,
191                        CXIdxContainerInfo &ContainerInfo);
192
193  void getEntityInfo(const NamedDecl *D,
194                     CXIdxEntityInfo &EntityInfo,
195                     StrAdapter &SA);
196
197  CXCursor getCursor(const NamedDecl *D) {
198    return cxcursor::MakeCXCursor(const_cast<NamedDecl*>(D), CXTU);
199  }
200
201  CXCursor getRefCursor(const NamedDecl *D, SourceLocation Loc);
202};
203
204}} // end clang::cxindex
205