Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1 | //===- CIndexHigh.cpp - 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 "IndexingContext.h" |
| 11 | #include "CXCursor.h" |
| 12 | #include "CXSourceLocation.h" |
| 13 | #include "CXTranslationUnit.h" |
| 14 | #include "CXString.h" |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 15 | #include "CIndexDiagnostic.h" |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 16 | #include "CIndexer.h" |
| 17 | |
| 18 | #include "clang/Frontend/ASTUnit.h" |
| 19 | #include "clang/Frontend/CompilerInvocation.h" |
| 20 | #include "clang/Frontend/CompilerInstance.h" |
Douglas Gregor | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/FrontendAction.h" |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/Utils.h" |
| 23 | #include "clang/Sema/SemaConsumer.h" |
| 24 | #include "clang/AST/ASTConsumer.h" |
| 25 | #include "clang/AST/DeclVisitor.h" |
| 26 | #include "clang/Lex/Preprocessor.h" |
| 27 | #include "clang/Lex/PPCallbacks.h" |
| 28 | #include "llvm/Support/MemoryBuffer.h" |
| 29 | #include "llvm/Support/CrashRecoveryContext.h" |
| 30 | |
| 31 | using namespace clang; |
| 32 | using namespace cxstring; |
| 33 | using namespace cxtu; |
| 34 | using namespace cxindex; |
| 35 | |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 36 | static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx); |
| 37 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 38 | namespace { |
| 39 | |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // IndexPPCallbacks |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
| 44 | class IndexPPCallbacks : public PPCallbacks { |
| 45 | Preprocessor &PP; |
| 46 | IndexingContext &IndexCtx; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 47 | bool IsMainFileEntered; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 48 | |
| 49 | public: |
| 50 | IndexPPCallbacks(Preprocessor &PP, IndexingContext &indexCtx) |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 51 | : PP(PP), IndexCtx(indexCtx), IsMainFileEntered(false) { } |
| 52 | |
| 53 | virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 54 | SrcMgr::CharacteristicKind FileType, FileID PrevFID) { |
| 55 | if (IsMainFileEntered) |
| 56 | return; |
| 57 | |
| 58 | SourceManager &SM = PP.getSourceManager(); |
| 59 | SourceLocation MainFileLoc = SM.getLocForStartOfFile(SM.getMainFileID()); |
| 60 | |
| 61 | if (Loc == MainFileLoc && Reason == PPCallbacks::EnterFile) { |
| 62 | IsMainFileEntered = true; |
| 63 | IndexCtx.enteredMainFile(SM.getFileEntryForID(SM.getMainFileID())); |
| 64 | } |
| 65 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 66 | |
| 67 | virtual void InclusionDirective(SourceLocation HashLoc, |
| 68 | const Token &IncludeTok, |
| 69 | StringRef FileName, |
| 70 | bool IsAngled, |
| 71 | const FileEntry *File, |
| 72 | SourceLocation EndLoc, |
| 73 | StringRef SearchPath, |
| 74 | StringRef RelativePath) { |
| 75 | bool isImport = (IncludeTok.is(tok::identifier) && |
| 76 | IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import); |
| 77 | IndexCtx.ppIncludedFile(HashLoc, FileName, File, isImport, IsAngled); |
| 78 | } |
| 79 | |
| 80 | /// MacroDefined - This hook is called whenever a macro definition is seen. |
| 81 | virtual void MacroDefined(const Token &Id, const MacroInfo *MI) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /// MacroUndefined - This hook is called whenever a macro #undef is seen. |
| 85 | /// MI is released immediately following this callback. |
| 86 | virtual void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /// MacroExpands - This is called by when a macro invocation is found. |
| 90 | virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI, |
| 91 | SourceRange Range) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /// SourceRangeSkipped - This hook is called when a source range is skipped. |
| 95 | /// \param Range The SourceRange that was skipped. The range begins at the |
| 96 | /// #if/#else directive and ends after the #endif/#else directive. |
| 97 | virtual void SourceRangeSkipped(SourceRange Range) { |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | //===----------------------------------------------------------------------===// |
| 102 | // IndexingConsumer |
| 103 | //===----------------------------------------------------------------------===// |
| 104 | |
| 105 | class IndexingConsumer : public ASTConsumer { |
| 106 | IndexingContext &IndexCtx; |
| 107 | |
| 108 | public: |
| 109 | explicit IndexingConsumer(IndexingContext &indexCtx) |
| 110 | : IndexCtx(indexCtx) { } |
| 111 | |
| 112 | // ASTConsumer Implementation |
| 113 | |
| 114 | virtual void Initialize(ASTContext &Context) { |
| 115 | IndexCtx.setASTContext(Context); |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 116 | IndexCtx.startedTranslationUnit(); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | virtual void HandleTranslationUnit(ASTContext &Ctx) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 122 | virtual bool HandleTopLevelDecl(DeclGroupRef DG) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 123 | IndexCtx.indexDeclGroupRef(DG); |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 124 | return !IndexCtx.shouldAbort(); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /// \brief Handle the specified top-level declaration that occurred inside |
| 128 | /// and ObjC container. |
| 129 | virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) { |
| 130 | // They will be handled after the interface is seen first. |
| 131 | IndexCtx.addTUDeclInObjCContainer(D); |
| 132 | } |
| 133 | |
| 134 | /// \brief This is called by the AST reader when deserializing things. |
| 135 | /// The default implementation forwards to HandleTopLevelDecl but we don't |
| 136 | /// care about them when indexing, so have an empty definition. |
| 137 | virtual void HandleInterestingDecl(DeclGroupRef D) {} |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 138 | |
| 139 | virtual void HandleTagDeclDefinition(TagDecl *D) { |
Argyrios Kyrtzidis | 58d2dbe | 2012-02-14 22:23:11 +0000 | [diff] [blame] | 140 | if (!IndexCtx.shouldIndexImplicitTemplateInsts()) |
| 141 | return; |
| 142 | |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 143 | if (IndexCtx.isTemplateImplicitInstantiation(D)) |
| 144 | IndexCtx.indexDecl(D); |
| 145 | } |
| 146 | |
| 147 | virtual void HandleCXXImplicitFunctionInstantiation(FunctionDecl *D) { |
Argyrios Kyrtzidis | 58d2dbe | 2012-02-14 22:23:11 +0000 | [diff] [blame] | 148 | if (!IndexCtx.shouldIndexImplicitTemplateInsts()) |
| 149 | return; |
| 150 | |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 151 | IndexCtx.indexDecl(D); |
| 152 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 156 | // CaptureDiagnosticConsumer |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 157 | //===----------------------------------------------------------------------===// |
| 158 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 159 | class CaptureDiagnosticConsumer : public DiagnosticConsumer { |
| 160 | SmallVector<StoredDiagnostic, 4> Errors; |
| 161 | public: |
| 162 | |
| 163 | virtual void HandleDiagnostic(DiagnosticsEngine::Level level, |
| 164 | const Diagnostic &Info) { |
| 165 | if (level >= DiagnosticsEngine::Error) |
| 166 | Errors.push_back(StoredDiagnostic(level, Info)); |
| 167 | } |
| 168 | |
| 169 | DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const { |
| 170 | return new IgnoringDiagConsumer(); |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | //===----------------------------------------------------------------------===// |
| 175 | // IndexingFrontendAction |
| 176 | //===----------------------------------------------------------------------===// |
| 177 | |
| 178 | class IndexingFrontendAction : public ASTFrontendAction { |
| 179 | IndexingContext IndexCtx; |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 180 | CXTranslationUnit CXTU; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 181 | |
| 182 | public: |
| 183 | IndexingFrontendAction(CXClientData clientData, |
| 184 | IndexerCallbacks &indexCallbacks, |
| 185 | unsigned indexOptions, |
| 186 | CXTranslationUnit cxTU) |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 187 | : IndexCtx(clientData, indexCallbacks, indexOptions, cxTU), |
| 188 | CXTU(cxTU) { } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 189 | |
| 190 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
| 191 | StringRef InFile) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 192 | IndexCtx.setASTContext(CI.getASTContext()); |
| 193 | Preprocessor &PP = CI.getPreprocessor(); |
| 194 | PP.addPPCallbacks(new IndexPPCallbacks(PP, IndexCtx)); |
Argyrios Kyrtzidis | 7fe90f3 | 2012-01-17 18:48:07 +0000 | [diff] [blame] | 195 | IndexCtx.setPreprocessor(PP); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 196 | return new IndexingConsumer(IndexCtx); |
| 197 | } |
| 198 | |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 199 | virtual void EndSourceFileAction() { |
| 200 | indexDiagnostics(CXTU, IndexCtx); |
| 201 | } |
| 202 | |
Argyrios Kyrtzidis | 58d2dbe | 2012-02-14 22:23:11 +0000 | [diff] [blame] | 203 | virtual TranslationUnitKind getTranslationUnitKind() { |
| 204 | if (IndexCtx.shouldIndexImplicitTemplateInsts()) |
| 205 | return TU_Complete; |
| 206 | else |
| 207 | return TU_Prefix; |
| 208 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 209 | virtual bool hasCodeCompletionSupport() const { return false; } |
| 210 | }; |
| 211 | |
| 212 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 213 | // clang_indexSourceFileUnit Implementation |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 214 | //===----------------------------------------------------------------------===// |
| 215 | |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 216 | struct IndexSourceFileInfo { |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 217 | CXIndexAction idxAction; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 218 | CXClientData client_data; |
| 219 | IndexerCallbacks *index_callbacks; |
| 220 | unsigned index_callbacks_size; |
| 221 | unsigned index_options; |
| 222 | const char *source_filename; |
| 223 | const char *const *command_line_args; |
| 224 | int num_command_line_args; |
| 225 | struct CXUnsavedFile *unsaved_files; |
| 226 | unsigned num_unsaved_files; |
| 227 | CXTranslationUnit *out_TU; |
| 228 | unsigned TU_options; |
| 229 | int result; |
| 230 | }; |
| 231 | |
| 232 | struct MemBufferOwner { |
| 233 | SmallVector<const llvm::MemoryBuffer *, 8> Buffers; |
| 234 | |
| 235 | ~MemBufferOwner() { |
| 236 | for (SmallVectorImpl<const llvm::MemoryBuffer *>::iterator |
| 237 | I = Buffers.begin(), E = Buffers.end(); I != E; ++I) |
| 238 | delete *I; |
| 239 | } |
| 240 | }; |
| 241 | |
| 242 | } // anonymous namespace |
| 243 | |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 244 | static void clang_indexSourceFile_Impl(void *UserData) { |
| 245 | IndexSourceFileInfo *ITUI = |
| 246 | static_cast<IndexSourceFileInfo*>(UserData); |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 247 | CXIndex CIdx = (CXIndex)ITUI->idxAction; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 248 | CXClientData client_data = ITUI->client_data; |
| 249 | IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks; |
| 250 | unsigned index_callbacks_size = ITUI->index_callbacks_size; |
| 251 | unsigned index_options = ITUI->index_options; |
| 252 | const char *source_filename = ITUI->source_filename; |
| 253 | const char * const *command_line_args = ITUI->command_line_args; |
| 254 | int num_command_line_args = ITUI->num_command_line_args; |
| 255 | struct CXUnsavedFile *unsaved_files = ITUI->unsaved_files; |
| 256 | unsigned num_unsaved_files = ITUI->num_unsaved_files; |
| 257 | CXTranslationUnit *out_TU = ITUI->out_TU; |
| 258 | unsigned TU_options = ITUI->TU_options; |
| 259 | ITUI->result = 1; // init as error. |
| 260 | |
| 261 | if (out_TU) |
| 262 | *out_TU = 0; |
| 263 | bool requestedToGetTU = (out_TU != 0); |
| 264 | |
| 265 | if (!CIdx) |
| 266 | return; |
| 267 | if (!client_index_callbacks || index_callbacks_size == 0) |
| 268 | return; |
| 269 | |
| 270 | IndexerCallbacks CB; |
| 271 | memset(&CB, 0, sizeof(CB)); |
| 272 | unsigned ClientCBSize = index_callbacks_size < sizeof(CB) |
| 273 | ? index_callbacks_size : sizeof(CB); |
| 274 | memcpy(&CB, client_index_callbacks, ClientCBSize); |
| 275 | |
| 276 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 277 | |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 278 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
Argyrios Kyrtzidis | 81b5ac3 | 2012-03-28 02:49:54 +0000 | [diff] [blame] | 279 | setThreadBackgroundPriority(); |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 280 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 281 | CaptureDiagnosticConsumer *CaptureDiag = new CaptureDiagnosticConsumer(); |
| 282 | |
| 283 | // Configure the diagnostics. |
| 284 | DiagnosticOptions DiagOpts; |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 285 | IntrusiveRefCntPtr<DiagnosticsEngine> |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 286 | Diags(CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args, |
| 287 | command_line_args, |
| 288 | CaptureDiag, |
Argyrios Kyrtzidis | 7383550 | 2011-11-29 08:14:50 +0000 | [diff] [blame] | 289 | /*ShouldOwnClient=*/true, |
| 290 | /*ShouldCloneClient=*/false)); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 291 | |
| 292 | // Recover resources if we crash before exiting this function. |
| 293 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 294 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
| 295 | DiagCleanup(Diags.getPtr()); |
| 296 | |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 297 | OwningPtr<std::vector<const char *> > |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 298 | Args(new std::vector<const char*>()); |
| 299 | |
| 300 | // Recover resources if we crash before exiting this method. |
| 301 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > |
| 302 | ArgsCleanup(Args.get()); |
| 303 | |
| 304 | Args->insert(Args->end(), command_line_args, |
| 305 | command_line_args + num_command_line_args); |
| 306 | |
| 307 | // The 'source_filename' argument is optional. If the caller does not |
| 308 | // specify it then it is assumed that the source file is specified |
| 309 | // in the actual argument list. |
| 310 | // Put the source file after command_line_args otherwise if '-x' flag is |
| 311 | // present it will be unused. |
| 312 | if (source_filename) |
| 313 | Args->push_back(source_filename); |
| 314 | |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 315 | IntrusiveRefCntPtr<CompilerInvocation> |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 316 | CInvok(createInvocationFromCommandLine(*Args, Diags)); |
| 317 | |
| 318 | if (!CInvok) |
| 319 | return; |
| 320 | |
| 321 | // Recover resources if we crash before exiting this function. |
| 322 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation, |
| 323 | llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> > |
| 324 | CInvokCleanup(CInvok.getPtr()); |
| 325 | |
| 326 | if (CInvok->getFrontendOpts().Inputs.empty()) |
| 327 | return; |
| 328 | |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 329 | OwningPtr<MemBufferOwner> BufOwner(new MemBufferOwner()); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 330 | |
| 331 | // Recover resources if we crash before exiting this method. |
| 332 | llvm::CrashRecoveryContextCleanupRegistrar<MemBufferOwner> |
| 333 | BufOwnerCleanup(BufOwner.get()); |
| 334 | |
| 335 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
| 336 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
| 337 | const llvm::MemoryBuffer *Buffer |
| 338 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
| 339 | CInvok->getPreprocessorOpts().addRemappedFile(unsaved_files[I].Filename, Buffer); |
| 340 | BufOwner->Buffers.push_back(Buffer); |
| 341 | } |
| 342 | |
| 343 | // Since libclang is primarily used by batch tools dealing with |
| 344 | // (often very broken) source code, where spell-checking can have a |
| 345 | // significant negative impact on performance (particularly when |
| 346 | // precompiled headers are involved), we disable it. |
Ted Kremenek | d3b74d9 | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 347 | CInvok->getLangOpts()->SpellChecking = false; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 348 | |
| 349 | if (!requestedToGetTU) |
| 350 | CInvok->getPreprocessorOpts().DetailedRecord = false; |
| 351 | |
Argyrios Kyrtzidis | b49a29f | 2012-03-27 21:38:03 +0000 | [diff] [blame] | 352 | if (index_options & CXIndexOpt_SuppressWarnings) |
| 353 | CInvok->getDiagnosticOpts().IgnoreWarnings = true; |
| 354 | |
Argyrios Kyrtzidis | 991bf49 | 2011-11-28 04:55:55 +0000 | [diff] [blame] | 355 | ASTUnit *Unit = ASTUnit::create(CInvok.getPtr(), Diags, |
| 356 | /*CaptureDiagnostics=*/true); |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 357 | OwningPtr<CXTUOwner> CXTU(new CXTUOwner(MakeCXTranslationUnit(CXXIdx, Unit))); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 358 | |
| 359 | // Recover resources if we crash before exiting this method. |
| 360 | llvm::CrashRecoveryContextCleanupRegistrar<CXTUOwner> |
| 361 | CXTUCleanup(CXTU.get()); |
| 362 | |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 363 | OwningPtr<IndexingFrontendAction> IndexAction; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 364 | IndexAction.reset(new IndexingFrontendAction(client_data, CB, |
| 365 | index_options, CXTU->getTU())); |
| 366 | |
| 367 | // Recover resources if we crash before exiting this method. |
| 368 | llvm::CrashRecoveryContextCleanupRegistrar<IndexingFrontendAction> |
| 369 | IndexActionCleanup(IndexAction.get()); |
| 370 | |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 371 | bool Persistent = requestedToGetTU; |
| 372 | StringRef ResourceFilesPath = CXXIdx->getClangResourcesPath(); |
| 373 | bool OnlyLocalDecls = false; |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 374 | bool PrecompilePreamble = false; |
| 375 | bool CacheCodeCompletionResults = false; |
| 376 | PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts(); |
| 377 | PPOpts.DetailedRecord = false; |
Argyrios Kyrtzidis | bef35c9 | 2012-03-07 01:51:17 +0000 | [diff] [blame] | 378 | PPOpts.AllowPCHWithCompilerErrors = true; |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 379 | |
| 380 | if (requestedToGetTU) { |
| 381 | OnlyLocalDecls = CXXIdx->getOnlyLocalDecls(); |
| 382 | PrecompilePreamble = TU_options & CXTranslationUnit_PrecompiledPreamble; |
| 383 | // FIXME: Add a flag for modules. |
| 384 | CacheCodeCompletionResults |
| 385 | = TU_options & CXTranslationUnit_CacheCompletionResults; |
| 386 | if (TU_options & CXTranslationUnit_DetailedPreprocessingRecord) { |
| 387 | PPOpts.DetailedRecord = true; |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Argyrios Kyrtzidis | e722ed6 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 391 | DiagnosticErrorTrap DiagTrap(*Diags); |
| 392 | bool Success = ASTUnit::LoadFromCompilerInvocationAction(CInvok.getPtr(), Diags, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 393 | IndexAction.get(), |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 394 | Unit, |
| 395 | Persistent, |
| 396 | ResourceFilesPath, |
| 397 | OnlyLocalDecls, |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 398 | /*CaptureDiagnostics=*/true, |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 399 | PrecompilePreamble, |
| 400 | CacheCodeCompletionResults); |
Argyrios Kyrtzidis | e722ed6 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 401 | if (DiagTrap.hasErrorOccurred() && CXXIdx->getDisplayDiagnostics()) |
| 402 | printDiagsToStderr(Unit); |
| 403 | |
| 404 | if (!Success) |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 405 | return; |
| 406 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 407 | if (out_TU) |
| 408 | *out_TU = CXTU->takeTU(); |
| 409 | |
| 410 | ITUI->result = 0; // success. |
| 411 | } |
| 412 | |
| 413 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 414 | // clang_indexTranslationUnit Implementation |
| 415 | //===----------------------------------------------------------------------===// |
| 416 | |
| 417 | namespace { |
| 418 | |
| 419 | struct IndexTranslationUnitInfo { |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 420 | CXIndexAction idxAction; |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 421 | CXClientData client_data; |
| 422 | IndexerCallbacks *index_callbacks; |
| 423 | unsigned index_callbacks_size; |
| 424 | unsigned index_options; |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 425 | CXTranslationUnit TU; |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 426 | int result; |
| 427 | }; |
| 428 | |
| 429 | } // anonymous namespace |
| 430 | |
| 431 | static void indexPreprocessingRecord(ASTUnit &Unit, IndexingContext &IdxCtx) { |
| 432 | Preprocessor &PP = Unit.getPreprocessor(); |
| 433 | if (!PP.getPreprocessingRecord()) |
| 434 | return; |
| 435 | |
| 436 | PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); |
| 437 | |
| 438 | // FIXME: Only deserialize inclusion directives. |
| 439 | // FIXME: Only deserialize stuff from the last chained PCH, not the PCH/Module |
| 440 | // that it depends on. |
| 441 | |
| 442 | bool OnlyLocal = !Unit.isMainFileAST() && Unit.getOnlyLocalDecls(); |
| 443 | PreprocessingRecord::iterator I, E; |
| 444 | if (OnlyLocal) { |
| 445 | I = PPRec.local_begin(); |
| 446 | E = PPRec.local_end(); |
| 447 | } else { |
| 448 | I = PPRec.begin(); |
| 449 | E = PPRec.end(); |
| 450 | } |
| 451 | |
| 452 | for (; I != E; ++I) { |
| 453 | PreprocessedEntity *PPE = *I; |
| 454 | |
| 455 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) { |
| 456 | IdxCtx.ppIncludedFile(ID->getSourceRange().getBegin(), ID->getFileName(), |
| 457 | ID->getFile(), ID->getKind() == InclusionDirective::Import, |
| 458 | !ID->wasInQuotes()); |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IdxCtx) { |
| 464 | // FIXME: Only deserialize stuff from the last chained PCH, not the PCH/Module |
| 465 | // that it depends on. |
| 466 | |
| 467 | bool OnlyLocal = !Unit.isMainFileAST() && Unit.getOnlyLocalDecls(); |
| 468 | |
| 469 | if (OnlyLocal) { |
| 470 | for (ASTUnit::top_level_iterator TL = Unit.top_level_begin(), |
| 471 | TLEnd = Unit.top_level_end(); |
| 472 | TL != TLEnd; ++TL) { |
| 473 | IdxCtx.indexTopLevelDecl(*TL); |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 474 | if (IdxCtx.shouldAbort()) |
| 475 | return; |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | } else { |
| 479 | TranslationUnitDecl *TUDecl = Unit.getASTContext().getTranslationUnitDecl(); |
| 480 | for (TranslationUnitDecl::decl_iterator |
| 481 | I = TUDecl->decls_begin(), E = TUDecl->decls_end(); I != E; ++I) { |
| 482 | IdxCtx.indexTopLevelDecl(*I); |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 483 | if (IdxCtx.shouldAbort()) |
| 484 | return; |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx) { |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 490 | if (!IdxCtx.hasDiagnosticCallback()) |
| 491 | return; |
| 492 | |
| 493 | CXDiagnosticSetImpl *DiagSet = cxdiag::lazyCreateDiags(TU); |
| 494 | IdxCtx.handleDiagnosticSet(DiagSet); |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | static void clang_indexTranslationUnit_Impl(void *UserData) { |
| 498 | IndexTranslationUnitInfo *ITUI = |
| 499 | static_cast<IndexTranslationUnitInfo*>(UserData); |
| 500 | CXTranslationUnit TU = ITUI->TU; |
| 501 | CXClientData client_data = ITUI->client_data; |
| 502 | IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks; |
| 503 | unsigned index_callbacks_size = ITUI->index_callbacks_size; |
| 504 | unsigned index_options = ITUI->index_options; |
| 505 | ITUI->result = 1; // init as error. |
| 506 | |
| 507 | if (!TU) |
| 508 | return; |
| 509 | if (!client_index_callbacks || index_callbacks_size == 0) |
| 510 | return; |
| 511 | |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 512 | CIndexer *CXXIdx = (CIndexer*)TU->CIdx; |
| 513 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
Argyrios Kyrtzidis | 81b5ac3 | 2012-03-28 02:49:54 +0000 | [diff] [blame] | 514 | setThreadBackgroundPriority(); |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 515 | |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 516 | IndexerCallbacks CB; |
| 517 | memset(&CB, 0, sizeof(CB)); |
| 518 | unsigned ClientCBSize = index_callbacks_size < sizeof(CB) |
| 519 | ? index_callbacks_size : sizeof(CB); |
| 520 | memcpy(&CB, client_index_callbacks, ClientCBSize); |
| 521 | |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 522 | OwningPtr<IndexingContext> IndexCtx; |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 523 | IndexCtx.reset(new IndexingContext(client_data, CB, index_options, TU)); |
| 524 | |
| 525 | // Recover resources if we crash before exiting this method. |
| 526 | llvm::CrashRecoveryContextCleanupRegistrar<IndexingContext> |
| 527 | IndexCtxCleanup(IndexCtx.get()); |
| 528 | |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 529 | OwningPtr<IndexingConsumer> IndexConsumer; |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 530 | IndexConsumer.reset(new IndexingConsumer(*IndexCtx)); |
| 531 | |
| 532 | // Recover resources if we crash before exiting this method. |
| 533 | llvm::CrashRecoveryContextCleanupRegistrar<IndexingConsumer> |
| 534 | IndexConsumerCleanup(IndexConsumer.get()); |
| 535 | |
| 536 | ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData); |
| 537 | if (!Unit) |
| 538 | return; |
| 539 | |
| 540 | FileManager &FileMgr = Unit->getFileManager(); |
| 541 | |
| 542 | if (Unit->getOriginalSourceFileName().empty()) |
| 543 | IndexCtx->enteredMainFile(0); |
| 544 | else |
| 545 | IndexCtx->enteredMainFile(FileMgr.getFile(Unit->getOriginalSourceFileName())); |
| 546 | |
| 547 | IndexConsumer->Initialize(Unit->getASTContext()); |
| 548 | |
| 549 | indexPreprocessingRecord(*Unit, *IndexCtx); |
| 550 | indexTranslationUnit(*Unit, *IndexCtx); |
| 551 | indexDiagnostics(TU, *IndexCtx); |
| 552 | |
| 553 | ITUI->result = 0; |
| 554 | } |
| 555 | |
| 556 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 557 | // libclang public APIs. |
| 558 | //===----------------------------------------------------------------------===// |
| 559 | |
| 560 | extern "C" { |
| 561 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 562 | int clang_index_isEntityObjCContainerKind(CXIdxEntityKind K) { |
| 563 | return CXIdxEntity_ObjCClass <= K && K <= CXIdxEntity_ObjCCategory; |
| 564 | } |
| 565 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 566 | const CXIdxObjCContainerDeclInfo * |
| 567 | clang_index_getObjCContainerDeclInfo(const CXIdxDeclInfo *DInfo) { |
| 568 | if (!DInfo) |
| 569 | return 0; |
| 570 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 571 | const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); |
| 572 | if (const ObjCContainerDeclInfo * |
| 573 | ContInfo = dyn_cast<ObjCContainerDeclInfo>(DI)) |
| 574 | return &ContInfo->ObjCContDeclInfo; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 575 | |
| 576 | return 0; |
| 577 | } |
| 578 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 579 | const CXIdxObjCInterfaceDeclInfo * |
| 580 | clang_index_getObjCInterfaceDeclInfo(const CXIdxDeclInfo *DInfo) { |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 581 | if (!DInfo) |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 582 | return 0; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 583 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 584 | const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); |
| 585 | if (const ObjCInterfaceDeclInfo * |
| 586 | InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI)) |
| 587 | return &InterInfo->ObjCInterDeclInfo; |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | const CXIdxObjCCategoryDeclInfo * |
| 593 | clang_index_getObjCCategoryDeclInfo(const CXIdxDeclInfo *DInfo){ |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 594 | if (!DInfo) |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 595 | return 0; |
| 596 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 597 | const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); |
| 598 | if (const ObjCCategoryDeclInfo * |
| 599 | CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI)) |
| 600 | return &CatInfo->ObjCCatDeclInfo; |
| 601 | |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | const CXIdxObjCProtocolRefListInfo * |
| 606 | clang_index_getObjCProtocolRefListInfo(const CXIdxDeclInfo *DInfo) { |
| 607 | if (!DInfo) |
| 608 | return 0; |
| 609 | |
| 610 | const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); |
| 611 | |
| 612 | if (const ObjCInterfaceDeclInfo * |
| 613 | InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI)) |
| 614 | return InterInfo->ObjCInterDeclInfo.protocols; |
| 615 | |
| 616 | if (const ObjCProtocolDeclInfo * |
| 617 | ProtInfo = dyn_cast<ObjCProtocolDeclInfo>(DI)) |
| 618 | return &ProtInfo->ObjCProtoRefListInfo; |
| 619 | |
Argyrios Kyrtzidis | c10a4c8 | 2011-12-13 18:47:45 +0000 | [diff] [blame] | 620 | if (const ObjCCategoryDeclInfo *CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI)) |
| 621 | return CatInfo->ObjCCatDeclInfo.protocols; |
| 622 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 623 | return 0; |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Argyrios Kyrtzidis | 792db26 | 2012-02-28 17:50:33 +0000 | [diff] [blame] | 626 | const CXIdxObjCPropertyDeclInfo * |
| 627 | clang_index_getObjCPropertyDeclInfo(const CXIdxDeclInfo *DInfo) { |
| 628 | if (!DInfo) |
| 629 | return 0; |
| 630 | |
| 631 | const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); |
| 632 | if (const ObjCPropertyDeclInfo *PropInfo = dyn_cast<ObjCPropertyDeclInfo>(DI)) |
| 633 | return &PropInfo->ObjCPropDeclInfo; |
| 634 | |
| 635 | return 0; |
| 636 | } |
| 637 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 638 | const CXIdxIBOutletCollectionAttrInfo * |
| 639 | clang_index_getIBOutletCollectionAttrInfo(const CXIdxAttrInfo *AInfo) { |
| 640 | if (!AInfo) |
| 641 | return 0; |
| 642 | |
| 643 | const AttrInfo *DI = static_cast<const AttrInfo *>(AInfo); |
| 644 | if (const IBOutletCollectionInfo * |
| 645 | IBInfo = dyn_cast<IBOutletCollectionInfo>(DI)) |
| 646 | return &IBInfo->IBCollInfo; |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 651 | const CXIdxCXXClassDeclInfo * |
| 652 | clang_index_getCXXClassDeclInfo(const CXIdxDeclInfo *DInfo) { |
| 653 | if (!DInfo) |
| 654 | return 0; |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 655 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 656 | const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); |
| 657 | if (const CXXClassDeclInfo *ClassInfo = dyn_cast<CXXClassDeclInfo>(DI)) |
| 658 | return &ClassInfo->CXXClassInfo; |
| 659 | |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | CXIdxClientContainer |
| 664 | clang_index_getClientContainer(const CXIdxContainerInfo *info) { |
| 665 | if (!info) |
| 666 | return 0; |
Benjamin Kramer | 3267e11 | 2011-11-29 12:31:20 +0000 | [diff] [blame] | 667 | const ContainerInfo *Container = static_cast<const ContainerInfo *>(info); |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 668 | return Container->IndexCtx->getClientContainerForDC(Container->DC); |
| 669 | } |
| 670 | |
| 671 | void clang_index_setClientContainer(const CXIdxContainerInfo *info, |
| 672 | CXIdxClientContainer client) { |
| 673 | if (!info) |
| 674 | return; |
Benjamin Kramer | 3267e11 | 2011-11-29 12:31:20 +0000 | [diff] [blame] | 675 | const ContainerInfo *Container = static_cast<const ContainerInfo *>(info); |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 676 | Container->IndexCtx->addContainerInMap(Container->DC, client); |
| 677 | } |
| 678 | |
| 679 | CXIdxClientEntity clang_index_getClientEntity(const CXIdxEntityInfo *info) { |
| 680 | if (!info) |
| 681 | return 0; |
Benjamin Kramer | 3267e11 | 2011-11-29 12:31:20 +0000 | [diff] [blame] | 682 | const EntityInfo *Entity = static_cast<const EntityInfo *>(info); |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 683 | return Entity->IndexCtx->getClientEntity(Entity->Dcl); |
| 684 | } |
| 685 | |
| 686 | void clang_index_setClientEntity(const CXIdxEntityInfo *info, |
| 687 | CXIdxClientEntity client) { |
| 688 | if (!info) |
| 689 | return; |
Benjamin Kramer | 3267e11 | 2011-11-29 12:31:20 +0000 | [diff] [blame] | 690 | const EntityInfo *Entity = static_cast<const EntityInfo *>(info); |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 691 | Entity->IndexCtx->setClientEntity(Entity->Dcl, client); |
| 692 | } |
| 693 | |
| 694 | CXIndexAction clang_IndexAction_create(CXIndex CIdx) { |
| 695 | // For now, CXIndexAction is featureless. |
| 696 | return CIdx; |
| 697 | } |
| 698 | |
| 699 | void clang_IndexAction_dispose(CXIndexAction idxAction) { |
| 700 | // For now, CXIndexAction is featureless. |
| 701 | } |
| 702 | |
| 703 | int clang_indexSourceFile(CXIndexAction idxAction, |
| 704 | CXClientData client_data, |
| 705 | IndexerCallbacks *index_callbacks, |
| 706 | unsigned index_callbacks_size, |
| 707 | unsigned index_options, |
| 708 | const char *source_filename, |
| 709 | const char * const *command_line_args, |
| 710 | int num_command_line_args, |
| 711 | struct CXUnsavedFile *unsaved_files, |
| 712 | unsigned num_unsaved_files, |
| 713 | CXTranslationUnit *out_TU, |
| 714 | unsigned TU_options) { |
| 715 | |
| 716 | IndexSourceFileInfo ITUI = { idxAction, client_data, index_callbacks, |
| 717 | index_callbacks_size, index_options, |
| 718 | source_filename, command_line_args, |
| 719 | num_command_line_args, unsaved_files, |
| 720 | num_unsaved_files, out_TU, TU_options, 0 }; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 721 | |
Argyrios Kyrtzidis | e7de9b4 | 2011-10-29 19:32:39 +0000 | [diff] [blame] | 722 | if (getenv("LIBCLANG_NOTHREADS")) { |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 723 | clang_indexSourceFile_Impl(&ITUI); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 724 | return ITUI.result; |
| 725 | } |
| 726 | |
| 727 | llvm::CrashRecoveryContext CRC; |
| 728 | |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 729 | if (!RunSafely(CRC, clang_indexSourceFile_Impl, &ITUI)) { |
| 730 | fprintf(stderr, "libclang: crash detected during indexing source file: {\n"); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 731 | fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); |
| 732 | fprintf(stderr, " 'command_line_args' : ["); |
| 733 | for (int i = 0; i != num_command_line_args; ++i) { |
| 734 | if (i) |
| 735 | fprintf(stderr, ", "); |
| 736 | fprintf(stderr, "'%s'", command_line_args[i]); |
| 737 | } |
| 738 | fprintf(stderr, "],\n"); |
| 739 | fprintf(stderr, " 'unsaved_files' : ["); |
| 740 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 741 | if (i) |
| 742 | fprintf(stderr, ", "); |
| 743 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, |
| 744 | unsaved_files[i].Length); |
| 745 | } |
| 746 | fprintf(stderr, "],\n"); |
| 747 | fprintf(stderr, " 'options' : %d,\n", TU_options); |
| 748 | fprintf(stderr, "}\n"); |
| 749 | |
| 750 | return 1; |
| 751 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
| 752 | if (out_TU) |
| 753 | PrintLibclangResourceUsage(*out_TU); |
| 754 | } |
| 755 | |
| 756 | return ITUI.result; |
| 757 | } |
| 758 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 759 | int clang_indexTranslationUnit(CXIndexAction idxAction, |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 760 | CXClientData client_data, |
| 761 | IndexerCallbacks *index_callbacks, |
| 762 | unsigned index_callbacks_size, |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 763 | unsigned index_options, |
| 764 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 765 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 766 | IndexTranslationUnitInfo ITUI = { idxAction, client_data, index_callbacks, |
| 767 | index_callbacks_size, index_options, TU, |
| 768 | 0 }; |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 769 | |
| 770 | if (getenv("LIBCLANG_NOTHREADS")) { |
| 771 | clang_indexTranslationUnit_Impl(&ITUI); |
| 772 | return ITUI.result; |
| 773 | } |
| 774 | |
| 775 | llvm::CrashRecoveryContext CRC; |
| 776 | |
| 777 | if (!RunSafely(CRC, clang_indexTranslationUnit_Impl, &ITUI)) { |
| 778 | fprintf(stderr, "libclang: crash detected during indexing TU\n"); |
| 779 | |
| 780 | return 1; |
| 781 | } |
| 782 | |
| 783 | return ITUI.result; |
| 784 | } |
| 785 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 786 | void clang_indexLoc_getFileLocation(CXIdxLoc location, |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 787 | CXIdxClientFile *indexFile, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 788 | CXFile *file, |
| 789 | unsigned *line, |
| 790 | unsigned *column, |
| 791 | unsigned *offset) { |
| 792 | if (indexFile) *indexFile = 0; |
| 793 | if (file) *file = 0; |
| 794 | if (line) *line = 0; |
| 795 | if (column) *column = 0; |
| 796 | if (offset) *offset = 0; |
| 797 | |
| 798 | SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); |
| 799 | if (!location.ptr_data[0] || Loc.isInvalid()) |
| 800 | return; |
| 801 | |
| 802 | IndexingContext &IndexCtx = |
| 803 | *static_cast<IndexingContext*>(location.ptr_data[0]); |
| 804 | IndexCtx.translateLoc(Loc, indexFile, file, line, column, offset); |
| 805 | } |
| 806 | |
| 807 | CXSourceLocation clang_indexLoc_getCXSourceLocation(CXIdxLoc location) { |
| 808 | SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); |
| 809 | if (!location.ptr_data[0] || Loc.isInvalid()) |
| 810 | return clang_getNullLocation(); |
| 811 | |
| 812 | IndexingContext &IndexCtx = |
| 813 | *static_cast<IndexingContext*>(location.ptr_data[0]); |
| 814 | return cxloc::translateSourceLocation(IndexCtx.getASTContext(), Loc); |
| 815 | } |
| 816 | |
| 817 | } // end: extern "C" |
| 818 | |