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