Indexing.cpp revision c58b75628c3860775fc3914aa6d08e84de82233f
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 "CIndexDiagnostic.h"
12#include "CIndexer.h"
13#include "CLog.h"
14#include "CXCursor.h"
15#include "CXSourceLocation.h"
16#include "CXString.h"
17#include "CXTranslationUnit.h"
18#include "clang/AST/ASTConsumer.h"
19#include "clang/AST/DeclVisitor.h"
20#include "clang/Frontend/ASTUnit.h"
21#include "clang/Frontend/CompilerInstance.h"
22#include "clang/Frontend/CompilerInvocation.h"
23#include "clang/Frontend/FrontendAction.h"
24#include "clang/Frontend/Utils.h"
25#include "clang/Lex/HeaderSearch.h"
26#include "clang/Lex/PPCallbacks.h"
27#include "clang/Lex/PPConditionalDirectiveRecord.h"
28#include "clang/Lex/Preprocessor.h"
29#include "clang/Sema/SemaConsumer.h"
30#include "llvm/Support/CrashRecoveryContext.h"
31#include "llvm/Support/MemoryBuffer.h"
32#include "llvm/Support/Mutex.h"
33#include "llvm/Support/MutexGuard.h"
34
35using namespace clang;
36using namespace cxtu;
37using namespace cxindex;
38
39static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx);
40
41namespace {
42
43//===----------------------------------------------------------------------===//
44// Skip Parsed Bodies
45//===----------------------------------------------------------------------===//
46
47#ifdef LLVM_ON_WIN32
48
49// FIXME: On windows it is disabled since current implementation depends on
50// file inodes.
51
52class SessionSkipBodyData { };
53
54class TUSkipBodyControl {
55public:
56  TUSkipBodyControl(SessionSkipBodyData &sessionData,
57                    PPConditionalDirectiveRecord &ppRec,
58                    Preprocessor &pp) { }
59  bool isParsed(SourceLocation Loc, FileID FID, const FileEntry *FE) {
60    return false;
61  }
62  void finished() { }
63};
64
65#else
66
67/// \brief A "region" in source code identified by the file/offset of the
68/// preprocessor conditional directive that it belongs to.
69/// Multiple, non-consecutive ranges can be parts of the same region.
70///
71/// As an example of different regions separated by preprocessor directives:
72///
73/// \code
74///   #1
75/// #ifdef BLAH
76///   #2
77/// #ifdef CAKE
78///   #3
79/// #endif
80///   #2
81/// #endif
82///   #1
83/// \endcode
84///
85/// There are 3 regions, with non-consecutive parts:
86///   #1 is identified as the beginning of the file
87///   #2 is identified as the location of "#ifdef BLAH"
88///   #3 is identified as the location of "#ifdef CAKE"
89///
90class PPRegion {
91  ino_t ino;
92  time_t ModTime;
93  dev_t dev;
94  unsigned Offset;
95public:
96  PPRegion() : ino(), ModTime(), dev(), Offset() {}
97  PPRegion(dev_t dev, ino_t ino, unsigned offset, time_t modTime)
98    : ino(ino), ModTime(modTime), dev(dev), Offset(offset) {}
99
100  ino_t getIno() const { return ino; }
101  dev_t getDev() const { return dev; }
102  unsigned getOffset() const { return Offset; }
103  time_t getModTime() const { return ModTime; }
104
105  bool isInvalid() const { return *this == PPRegion(); }
106
107  friend bool operator==(const PPRegion &lhs, const PPRegion &rhs) {
108    return lhs.dev == rhs.dev && lhs.ino == rhs.ino &&
109        lhs.Offset == rhs.Offset && lhs.ModTime == rhs.ModTime;
110  }
111};
112
113typedef llvm::DenseSet<PPRegion> PPRegionSetTy;
114
115} // end anonymous namespace
116
117namespace llvm {
118  template <> struct isPodLike<PPRegion> {
119    static const bool value = true;
120  };
121
122  template <>
123  struct DenseMapInfo<PPRegion> {
124    static inline PPRegion getEmptyKey() {
125      return PPRegion(0, 0, unsigned(-1), 0);
126    }
127    static inline PPRegion getTombstoneKey() {
128      return PPRegion(0, 0, unsigned(-2), 0);
129    }
130
131    static unsigned getHashValue(const PPRegion &S) {
132      llvm::FoldingSetNodeID ID;
133      ID.AddInteger(S.getIno());
134      ID.AddInteger(S.getDev());
135      ID.AddInteger(S.getOffset());
136      ID.AddInteger(S.getModTime());
137      return ID.ComputeHash();
138    }
139
140    static bool isEqual(const PPRegion &LHS, const PPRegion &RHS) {
141      return LHS == RHS;
142    }
143  };
144}
145
146namespace {
147
148class SessionSkipBodyData {
149  llvm::sys::Mutex Mux;
150  PPRegionSetTy ParsedRegions;
151
152public:
153  SessionSkipBodyData() : Mux(/*recursive=*/false) {}
154  ~SessionSkipBodyData() {
155    //llvm::errs() << "RegionData: " << Skipped.size() << " - " << Skipped.getMemorySize() << "\n";
156  }
157
158  void copyTo(PPRegionSetTy &Set) {
159    llvm::MutexGuard MG(Mux);
160    Set = ParsedRegions;
161  }
162
163  void update(ArrayRef<PPRegion> Regions) {
164    llvm::MutexGuard MG(Mux);
165    ParsedRegions.insert(Regions.begin(), Regions.end());
166  }
167};
168
169class TUSkipBodyControl {
170  SessionSkipBodyData &SessionData;
171  PPConditionalDirectiveRecord &PPRec;
172  Preprocessor &PP;
173
174  PPRegionSetTy ParsedRegions;
175  SmallVector<PPRegion, 32> NewParsedRegions;
176  PPRegion LastRegion;
177  bool LastIsParsed;
178
179public:
180  TUSkipBodyControl(SessionSkipBodyData &sessionData,
181                    PPConditionalDirectiveRecord &ppRec,
182                    Preprocessor &pp)
183    : SessionData(sessionData), PPRec(ppRec), PP(pp) {
184    SessionData.copyTo(ParsedRegions);
185  }
186
187  bool isParsed(SourceLocation Loc, FileID FID, const FileEntry *FE) {
188    PPRegion region = getRegion(Loc, FID, FE);
189    if (region.isInvalid())
190      return false;
191
192    // Check common case, consecutive functions in the same region.
193    if (LastRegion == region)
194      return LastIsParsed;
195
196    LastRegion = region;
197    LastIsParsed = ParsedRegions.count(region);
198    if (!LastIsParsed)
199      NewParsedRegions.push_back(region);
200    return LastIsParsed;
201  }
202
203  void finished() {
204    SessionData.update(NewParsedRegions);
205  }
206
207private:
208  PPRegion getRegion(SourceLocation Loc, FileID FID, const FileEntry *FE) {
209    SourceLocation RegionLoc = PPRec.findConditionalDirectiveRegionLoc(Loc);
210    if (RegionLoc.isInvalid()) {
211      if (isParsedOnceInclude(FE))
212        return PPRegion(FE->getDevice(), FE->getInode(), 0,
213                        FE->getModificationTime());
214      return PPRegion();
215    }
216
217    const SourceManager &SM = PPRec.getSourceManager();
218    assert(RegionLoc.isFileID());
219    FileID RegionFID;
220    unsigned RegionOffset;
221    llvm::tie(RegionFID, RegionOffset) = SM.getDecomposedLoc(RegionLoc);
222
223    if (RegionFID != FID) {
224      if (isParsedOnceInclude(FE))
225        return PPRegion(FE->getDevice(), FE->getInode(), 0,
226                        FE->getModificationTime());
227      return PPRegion();
228    }
229
230    return PPRegion(FE->getDevice(), FE->getInode(), RegionOffset,
231                    FE->getModificationTime());
232  }
233
234  bool isParsedOnceInclude(const FileEntry *FE) {
235    return PP.getHeaderSearchInfo().isFileMultipleIncludeGuarded(FE);
236  }
237};
238
239#endif
240
241//===----------------------------------------------------------------------===//
242// IndexPPCallbacks
243//===----------------------------------------------------------------------===//
244
245class IndexPPCallbacks : public PPCallbacks {
246  Preprocessor &PP;
247  IndexingContext &IndexCtx;
248  bool IsMainFileEntered;
249
250public:
251  IndexPPCallbacks(Preprocessor &PP, IndexingContext &indexCtx)
252    : PP(PP), IndexCtx(indexCtx), IsMainFileEntered(false) { }
253
254  virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
255                          SrcMgr::CharacteristicKind FileType, FileID PrevFID) {
256    if (IsMainFileEntered)
257      return;
258
259    SourceManager &SM = PP.getSourceManager();
260    SourceLocation MainFileLoc = SM.getLocForStartOfFile(SM.getMainFileID());
261
262    if (Loc == MainFileLoc && Reason == PPCallbacks::EnterFile) {
263      IsMainFileEntered = true;
264      IndexCtx.enteredMainFile(SM.getFileEntryForID(SM.getMainFileID()));
265    }
266  }
267
268  virtual void InclusionDirective(SourceLocation HashLoc,
269                                  const Token &IncludeTok,
270                                  StringRef FileName,
271                                  bool IsAngled,
272                                  CharSourceRange FilenameRange,
273                                  const FileEntry *File,
274                                  StringRef SearchPath,
275                                  StringRef RelativePath,
276                                  const Module *Imported) {
277    bool isImport = (IncludeTok.is(tok::identifier) &&
278            IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import);
279    IndexCtx.ppIncludedFile(HashLoc, FileName, File, isImport, IsAngled,
280                            Imported);
281  }
282
283  /// MacroDefined - This hook is called whenever a macro definition is seen.
284  virtual void MacroDefined(const Token &Id, const MacroDirective *MD) {
285  }
286
287  /// MacroUndefined - This hook is called whenever a macro #undef is seen.
288  /// MI is released immediately following this callback.
289  virtual void MacroUndefined(const Token &MacroNameTok,
290                              const MacroDirective *MD) {
291  }
292
293  /// MacroExpands - This is called by when a macro invocation is found.
294  virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
295                            SourceRange Range) {
296  }
297
298  /// SourceRangeSkipped - This hook is called when a source range is skipped.
299  /// \param Range The SourceRange that was skipped. The range begins at the
300  /// #if/#else directive and ends after the #endif/#else directive.
301  virtual void SourceRangeSkipped(SourceRange Range) {
302  }
303};
304
305//===----------------------------------------------------------------------===//
306// IndexingConsumer
307//===----------------------------------------------------------------------===//
308
309class IndexingConsumer : public ASTConsumer {
310  IndexingContext &IndexCtx;
311  TUSkipBodyControl *SKCtrl;
312
313public:
314  IndexingConsumer(IndexingContext &indexCtx, TUSkipBodyControl *skCtrl)
315    : IndexCtx(indexCtx), SKCtrl(skCtrl) { }
316
317  // ASTConsumer Implementation
318
319  virtual void Initialize(ASTContext &Context) {
320    IndexCtx.setASTContext(Context);
321    IndexCtx.startedTranslationUnit();
322  }
323
324  virtual void HandleTranslationUnit(ASTContext &Ctx) {
325    if (SKCtrl)
326      SKCtrl->finished();
327  }
328
329  virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
330    IndexCtx.indexDeclGroupRef(DG);
331    return !IndexCtx.shouldAbort();
332  }
333
334  /// \brief Handle the specified top-level declaration that occurred inside
335  /// and ObjC container.
336  virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) {
337    // They will be handled after the interface is seen first.
338    IndexCtx.addTUDeclInObjCContainer(D);
339  }
340
341  /// \brief This is called by the AST reader when deserializing things.
342  /// The default implementation forwards to HandleTopLevelDecl but we don't
343  /// care about them when indexing, so have an empty definition.
344  virtual void HandleInterestingDecl(DeclGroupRef D) {}
345
346  virtual void HandleTagDeclDefinition(TagDecl *D) {
347    if (!IndexCtx.shouldIndexImplicitTemplateInsts())
348      return;
349
350    if (IndexCtx.isTemplateImplicitInstantiation(D))
351      IndexCtx.indexDecl(D);
352  }
353
354  virtual void HandleCXXImplicitFunctionInstantiation(FunctionDecl *D) {
355    if (!IndexCtx.shouldIndexImplicitTemplateInsts())
356      return;
357
358    IndexCtx.indexDecl(D);
359  }
360
361  virtual bool shouldSkipFunctionBody(Decl *D) {
362    if (!SKCtrl) {
363      // Always skip bodies.
364      return true;
365    }
366
367    const SourceManager &SM = IndexCtx.getASTContext().getSourceManager();
368    SourceLocation Loc = D->getLocation();
369    if (Loc.isMacroID())
370      return false;
371    if (SM.isInSystemHeader(Loc))
372      return true; // always skip bodies from system headers.
373
374    FileID FID;
375    unsigned Offset;
376    llvm::tie(FID, Offset) = SM.getDecomposedLoc(Loc);
377    // Don't skip bodies from main files; this may be revisited.
378    if (SM.getMainFileID() == FID)
379      return false;
380    const FileEntry *FE = SM.getFileEntryForID(FID);
381    if (!FE)
382      return false;
383
384    return SKCtrl->isParsed(Loc, FID, FE);
385  }
386};
387
388//===----------------------------------------------------------------------===//
389// CaptureDiagnosticConsumer
390//===----------------------------------------------------------------------===//
391
392class CaptureDiagnosticConsumer : public DiagnosticConsumer {
393  SmallVector<StoredDiagnostic, 4> Errors;
394public:
395
396  virtual void HandleDiagnostic(DiagnosticsEngine::Level level,
397                                const Diagnostic &Info) {
398    if (level >= DiagnosticsEngine::Error)
399      Errors.push_back(StoredDiagnostic(level, Info));
400  }
401
402  DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
403    return new IgnoringDiagConsumer();
404  }
405};
406
407//===----------------------------------------------------------------------===//
408// IndexingFrontendAction
409//===----------------------------------------------------------------------===//
410
411class IndexingFrontendAction : public ASTFrontendAction {
412  IndexingContext IndexCtx;
413  CXTranslationUnit CXTU;
414
415  SessionSkipBodyData *SKData;
416  OwningPtr<TUSkipBodyControl> SKCtrl;
417
418public:
419  IndexingFrontendAction(CXClientData clientData,
420                         IndexerCallbacks &indexCallbacks,
421                         unsigned indexOptions,
422                         CXTranslationUnit cxTU,
423                         SessionSkipBodyData *skData)
424    : IndexCtx(clientData, indexCallbacks, indexOptions, cxTU),
425      CXTU(cxTU), SKData(skData) { }
426
427  virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
428                                         StringRef InFile) {
429    PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
430
431    if (!PPOpts.ImplicitPCHInclude.empty()) {
432      IndexCtx.importedPCH(
433                        CI.getFileManager().getFile(PPOpts.ImplicitPCHInclude));
434    }
435
436    IndexCtx.setASTContext(CI.getASTContext());
437    Preprocessor &PP = CI.getPreprocessor();
438    PP.addPPCallbacks(new IndexPPCallbacks(PP, IndexCtx));
439    IndexCtx.setPreprocessor(PP);
440
441    if (SKData) {
442      PPConditionalDirectiveRecord *
443        PPRec = new PPConditionalDirectiveRecord(PP.getSourceManager());
444      PP.addPPCallbacks(PPRec);
445      SKCtrl.reset(new TUSkipBodyControl(*SKData, *PPRec, PP));
446    }
447
448    return new IndexingConsumer(IndexCtx, SKCtrl.get());
449  }
450
451  virtual void EndSourceFileAction() {
452    indexDiagnostics(CXTU, IndexCtx);
453  }
454
455  virtual TranslationUnitKind getTranslationUnitKind() {
456    if (IndexCtx.shouldIndexImplicitTemplateInsts())
457      return TU_Complete;
458    else
459      return TU_Prefix;
460  }
461  virtual bool hasCodeCompletionSupport() const { return false; }
462};
463
464//===----------------------------------------------------------------------===//
465// clang_indexSourceFileUnit Implementation
466//===----------------------------------------------------------------------===//
467
468struct IndexSessionData {
469  CXIndex CIdx;
470  OwningPtr<SessionSkipBodyData> SkipBodyData;
471
472  explicit IndexSessionData(CXIndex cIdx)
473    : CIdx(cIdx), SkipBodyData(new SessionSkipBodyData) {}
474};
475
476struct IndexSourceFileInfo {
477  CXIndexAction idxAction;
478  CXClientData client_data;
479  IndexerCallbacks *index_callbacks;
480  unsigned index_callbacks_size;
481  unsigned index_options;
482  const char *source_filename;
483  const char *const *command_line_args;
484  int num_command_line_args;
485  struct CXUnsavedFile *unsaved_files;
486  unsigned num_unsaved_files;
487  CXTranslationUnit *out_TU;
488  unsigned TU_options;
489  int result;
490};
491
492struct MemBufferOwner {
493  SmallVector<const llvm::MemoryBuffer *, 8> Buffers;
494
495  ~MemBufferOwner() {
496    for (SmallVectorImpl<const llvm::MemoryBuffer *>::iterator
497           I = Buffers.begin(), E = Buffers.end(); I != E; ++I)
498      delete *I;
499  }
500};
501
502} // anonymous namespace
503
504static void clang_indexSourceFile_Impl(void *UserData) {
505  IndexSourceFileInfo *ITUI =
506    static_cast<IndexSourceFileInfo*>(UserData);
507  CXIndexAction cxIdxAction = ITUI->idxAction;
508  CXClientData client_data = ITUI->client_data;
509  IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks;
510  unsigned index_callbacks_size = ITUI->index_callbacks_size;
511  unsigned index_options = ITUI->index_options;
512  const char *source_filename = ITUI->source_filename;
513  const char * const *command_line_args = ITUI->command_line_args;
514  int num_command_line_args = ITUI->num_command_line_args;
515  struct CXUnsavedFile *unsaved_files = ITUI->unsaved_files;
516  unsigned num_unsaved_files = ITUI->num_unsaved_files;
517  CXTranslationUnit *out_TU  = ITUI->out_TU;
518  unsigned TU_options = ITUI->TU_options;
519  ITUI->result = 1; // init as error.
520
521  if (out_TU)
522    *out_TU = 0;
523  bool requestedToGetTU = (out_TU != 0);
524
525  if (!cxIdxAction)
526    return;
527  if (!client_index_callbacks || index_callbacks_size == 0)
528    return;
529
530  IndexerCallbacks CB;
531  memset(&CB, 0, sizeof(CB));
532  unsigned ClientCBSize = index_callbacks_size < sizeof(CB)
533                                  ? index_callbacks_size : sizeof(CB);
534  memcpy(&CB, client_index_callbacks, ClientCBSize);
535
536  IndexSessionData *IdxSession = static_cast<IndexSessionData *>(cxIdxAction);
537  CIndexer *CXXIdx = static_cast<CIndexer *>(IdxSession->CIdx);
538
539  if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
540    setThreadBackgroundPriority();
541
542  bool CaptureDiagnostics = !Logger::isLoggingEnabled();
543
544  CaptureDiagnosticConsumer *CaptureDiag = 0;
545  if (CaptureDiagnostics)
546    CaptureDiag = new CaptureDiagnosticConsumer();
547
548  // Configure the diagnostics.
549  IntrusiveRefCntPtr<DiagnosticsEngine>
550    Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions,
551                                              CaptureDiag,
552                                              /*ShouldOwnClient=*/true,
553                                              /*ShouldCloneClient=*/false));
554
555  // Recover resources if we crash before exiting this function.
556  llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
557    llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
558    DiagCleanup(Diags.getPtr());
559
560  OwningPtr<std::vector<const char *> >
561    Args(new std::vector<const char*>());
562
563  // Recover resources if we crash before exiting this method.
564  llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> >
565    ArgsCleanup(Args.get());
566
567  Args->insert(Args->end(), command_line_args,
568               command_line_args + num_command_line_args);
569
570  // The 'source_filename' argument is optional.  If the caller does not
571  // specify it then it is assumed that the source file is specified
572  // in the actual argument list.
573  // Put the source file after command_line_args otherwise if '-x' flag is
574  // present it will be unused.
575  if (source_filename)
576    Args->push_back(source_filename);
577
578  IntrusiveRefCntPtr<CompilerInvocation>
579    CInvok(createInvocationFromCommandLine(*Args, Diags));
580
581  if (!CInvok)
582    return;
583
584  // Recover resources if we crash before exiting this function.
585  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation,
586    llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> >
587    CInvokCleanup(CInvok.getPtr());
588
589  if (CInvok->getFrontendOpts().Inputs.empty())
590    return;
591
592  OwningPtr<MemBufferOwner> BufOwner(new MemBufferOwner());
593
594  // Recover resources if we crash before exiting this method.
595  llvm::CrashRecoveryContextCleanupRegistrar<MemBufferOwner>
596    BufOwnerCleanup(BufOwner.get());
597
598  for (unsigned I = 0; I != num_unsaved_files; ++I) {
599    StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
600    const llvm::MemoryBuffer *Buffer
601      = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
602    CInvok->getPreprocessorOpts().addRemappedFile(unsaved_files[I].Filename, Buffer);
603    BufOwner->Buffers.push_back(Buffer);
604  }
605
606  // Since libclang is primarily used by batch tools dealing with
607  // (often very broken) source code, where spell-checking can have a
608  // significant negative impact on performance (particularly when
609  // precompiled headers are involved), we disable it.
610  CInvok->getLangOpts()->SpellChecking = false;
611
612  if (index_options & CXIndexOpt_SuppressWarnings)
613    CInvok->getDiagnosticOpts().IgnoreWarnings = true;
614
615  ASTUnit *Unit = ASTUnit::create(CInvok.getPtr(), Diags,
616                                  CaptureDiagnostics,
617                                  /*UserFilesAreVolatile=*/true);
618  OwningPtr<CXTUOwner> CXTU(new CXTUOwner(MakeCXTranslationUnit(CXXIdx, Unit)));
619
620  // Recover resources if we crash before exiting this method.
621  llvm::CrashRecoveryContextCleanupRegistrar<CXTUOwner>
622    CXTUCleanup(CXTU.get());
623
624  // Enable the skip-parsed-bodies optimization only for C++; this may be
625  // revisited.
626  bool SkipBodies = (index_options & CXIndexOpt_SkipParsedBodiesInSession) &&
627      CInvok->getLangOpts()->CPlusPlus;
628  if (SkipBodies)
629    CInvok->getFrontendOpts().SkipFunctionBodies = true;
630
631  OwningPtr<IndexingFrontendAction> IndexAction;
632  IndexAction.reset(new IndexingFrontendAction(client_data, CB,
633                                               index_options, CXTU->getTU(),
634                              SkipBodies ? IdxSession->SkipBodyData.get() : 0));
635
636  // Recover resources if we crash before exiting this method.
637  llvm::CrashRecoveryContextCleanupRegistrar<IndexingFrontendAction>
638    IndexActionCleanup(IndexAction.get());
639
640  bool Persistent = requestedToGetTU;
641  bool OnlyLocalDecls = false;
642  bool PrecompilePreamble = false;
643  bool CacheCodeCompletionResults = false;
644  PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts();
645  PPOpts.AllowPCHWithCompilerErrors = true;
646
647  if (requestedToGetTU) {
648    OnlyLocalDecls = CXXIdx->getOnlyLocalDecls();
649    PrecompilePreamble = TU_options & CXTranslationUnit_PrecompiledPreamble;
650    // FIXME: Add a flag for modules.
651    CacheCodeCompletionResults
652      = TU_options & CXTranslationUnit_CacheCompletionResults;
653  }
654
655  if (TU_options & CXTranslationUnit_DetailedPreprocessingRecord) {
656    PPOpts.DetailedRecord = true;
657  }
658
659  if (!requestedToGetTU && !CInvok->getLangOpts()->Modules)
660    PPOpts.DetailedRecord = false;
661
662  DiagnosticErrorTrap DiagTrap(*Diags);
663  bool Success = ASTUnit::LoadFromCompilerInvocationAction(CInvok.getPtr(), Diags,
664                                                       IndexAction.get(),
665                                                       Unit,
666                                                       Persistent,
667                                                CXXIdx->getClangResourcesPath(),
668                                                       OnlyLocalDecls,
669                                                       CaptureDiagnostics,
670                                                       PrecompilePreamble,
671                                                    CacheCodeCompletionResults,
672                                 /*IncludeBriefCommentsInCodeCompletion=*/false,
673                                                 /*UserFilesAreVolatile=*/true);
674  if (DiagTrap.hasErrorOccurred() && CXXIdx->getDisplayDiagnostics())
675    printDiagsToStderr(Unit);
676
677  if (!Success)
678    return;
679
680  if (out_TU)
681    *out_TU = CXTU->takeTU();
682
683  ITUI->result = 0; // success.
684}
685
686//===----------------------------------------------------------------------===//
687// clang_indexTranslationUnit Implementation
688//===----------------------------------------------------------------------===//
689
690namespace {
691
692struct IndexTranslationUnitInfo {
693  CXIndexAction idxAction;
694  CXClientData client_data;
695  IndexerCallbacks *index_callbacks;
696  unsigned index_callbacks_size;
697  unsigned index_options;
698  CXTranslationUnit TU;
699  int result;
700};
701
702} // anonymous namespace
703
704static void indexPreprocessingRecord(ASTUnit &Unit, IndexingContext &IdxCtx) {
705  Preprocessor &PP = Unit.getPreprocessor();
706  if (!PP.getPreprocessingRecord())
707    return;
708
709  // FIXME: Only deserialize inclusion directives.
710
711  PreprocessingRecord::iterator I, E;
712  llvm::tie(I, E) = Unit.getLocalPreprocessingEntities();
713
714  bool isModuleFile = Unit.isModuleFile();
715  for (; I != E; ++I) {
716    PreprocessedEntity *PPE = *I;
717
718    if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) {
719      SourceLocation Loc = ID->getSourceRange().getBegin();
720      // Modules have synthetic main files as input, give an invalid location
721      // if the location points to such a file.
722      if (isModuleFile && Unit.isInMainFileID(Loc))
723        Loc = SourceLocation();
724      IdxCtx.ppIncludedFile(Loc, ID->getFileName(),
725                            ID->getFile(),
726                            ID->getKind() == InclusionDirective::Import,
727                            !ID->wasInQuotes(), ID->importedModule());
728    }
729  }
730}
731
732static bool topLevelDeclVisitor(void *context, const Decl *D) {
733  IndexingContext &IdxCtx = *static_cast<IndexingContext*>(context);
734  IdxCtx.indexTopLevelDecl(D);
735  if (IdxCtx.shouldAbort())
736    return false;
737  return true;
738}
739
740static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IdxCtx) {
741  Unit.visitLocalTopLevelDecls(&IdxCtx, topLevelDeclVisitor);
742}
743
744static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx) {
745  if (!IdxCtx.hasDiagnosticCallback())
746    return;
747
748  CXDiagnosticSetImpl *DiagSet = cxdiag::lazyCreateDiags(TU);
749  IdxCtx.handleDiagnosticSet(DiagSet);
750}
751
752static void clang_indexTranslationUnit_Impl(void *UserData) {
753  IndexTranslationUnitInfo *ITUI =
754    static_cast<IndexTranslationUnitInfo*>(UserData);
755  CXTranslationUnit TU = ITUI->TU;
756  CXClientData client_data = ITUI->client_data;
757  IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks;
758  unsigned index_callbacks_size = ITUI->index_callbacks_size;
759  unsigned index_options = ITUI->index_options;
760  ITUI->result = 1; // init as error.
761
762  if (!TU)
763    return;
764  if (!client_index_callbacks || index_callbacks_size == 0)
765    return;
766
767  CIndexer *CXXIdx = TU->CIdx;
768  if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
769    setThreadBackgroundPriority();
770
771  IndexerCallbacks CB;
772  memset(&CB, 0, sizeof(CB));
773  unsigned ClientCBSize = index_callbacks_size < sizeof(CB)
774                                  ? index_callbacks_size : sizeof(CB);
775  memcpy(&CB, client_index_callbacks, ClientCBSize);
776
777  OwningPtr<IndexingContext> IndexCtx;
778  IndexCtx.reset(new IndexingContext(client_data, CB, index_options, TU));
779
780  // Recover resources if we crash before exiting this method.
781  llvm::CrashRecoveryContextCleanupRegistrar<IndexingContext>
782    IndexCtxCleanup(IndexCtx.get());
783
784  OwningPtr<IndexingConsumer> IndexConsumer;
785  IndexConsumer.reset(new IndexingConsumer(*IndexCtx, 0));
786
787  // Recover resources if we crash before exiting this method.
788  llvm::CrashRecoveryContextCleanupRegistrar<IndexingConsumer>
789    IndexConsumerCleanup(IndexConsumer.get());
790
791  ASTUnit *Unit = cxtu::getASTUnit(TU);
792  if (!Unit)
793    return;
794
795  ASTUnit::ConcurrencyCheck Check(*Unit);
796
797  if (const FileEntry *PCHFile = Unit->getPCHFile())
798    IndexCtx->importedPCH(PCHFile);
799
800  FileManager &FileMgr = Unit->getFileManager();
801
802  if (Unit->getOriginalSourceFileName().empty())
803    IndexCtx->enteredMainFile(0);
804  else
805    IndexCtx->enteredMainFile(FileMgr.getFile(Unit->getOriginalSourceFileName()));
806
807  IndexConsumer->Initialize(Unit->getASTContext());
808
809  indexPreprocessingRecord(*Unit, *IndexCtx);
810  indexTranslationUnit(*Unit, *IndexCtx);
811  indexDiagnostics(TU, *IndexCtx);
812
813  ITUI->result = 0;
814}
815
816//===----------------------------------------------------------------------===//
817// libclang public APIs.
818//===----------------------------------------------------------------------===//
819
820extern "C" {
821
822int clang_index_isEntityObjCContainerKind(CXIdxEntityKind K) {
823  return CXIdxEntity_ObjCClass <= K && K <= CXIdxEntity_ObjCCategory;
824}
825
826const CXIdxObjCContainerDeclInfo *
827clang_index_getObjCContainerDeclInfo(const CXIdxDeclInfo *DInfo) {
828  if (!DInfo)
829    return 0;
830
831  const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
832  if (const ObjCContainerDeclInfo *
833        ContInfo = dyn_cast<ObjCContainerDeclInfo>(DI))
834    return &ContInfo->ObjCContDeclInfo;
835
836  return 0;
837}
838
839const CXIdxObjCInterfaceDeclInfo *
840clang_index_getObjCInterfaceDeclInfo(const CXIdxDeclInfo *DInfo) {
841  if (!DInfo)
842    return 0;
843
844  const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
845  if (const ObjCInterfaceDeclInfo *
846        InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI))
847    return &InterInfo->ObjCInterDeclInfo;
848
849  return 0;
850}
851
852const CXIdxObjCCategoryDeclInfo *
853clang_index_getObjCCategoryDeclInfo(const CXIdxDeclInfo *DInfo){
854  if (!DInfo)
855    return 0;
856
857  const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
858  if (const ObjCCategoryDeclInfo *
859        CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI))
860    return &CatInfo->ObjCCatDeclInfo;
861
862  return 0;
863}
864
865const CXIdxObjCProtocolRefListInfo *
866clang_index_getObjCProtocolRefListInfo(const CXIdxDeclInfo *DInfo) {
867  if (!DInfo)
868    return 0;
869
870  const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
871
872  if (const ObjCInterfaceDeclInfo *
873        InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI))
874    return InterInfo->ObjCInterDeclInfo.protocols;
875
876  if (const ObjCProtocolDeclInfo *
877        ProtInfo = dyn_cast<ObjCProtocolDeclInfo>(DI))
878    return &ProtInfo->ObjCProtoRefListInfo;
879
880  if (const ObjCCategoryDeclInfo *CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI))
881    return CatInfo->ObjCCatDeclInfo.protocols;
882
883  return 0;
884}
885
886const CXIdxObjCPropertyDeclInfo *
887clang_index_getObjCPropertyDeclInfo(const CXIdxDeclInfo *DInfo) {
888  if (!DInfo)
889    return 0;
890
891  const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
892  if (const ObjCPropertyDeclInfo *PropInfo = dyn_cast<ObjCPropertyDeclInfo>(DI))
893    return &PropInfo->ObjCPropDeclInfo;
894
895  return 0;
896}
897
898const CXIdxIBOutletCollectionAttrInfo *
899clang_index_getIBOutletCollectionAttrInfo(const CXIdxAttrInfo *AInfo) {
900  if (!AInfo)
901    return 0;
902
903  const AttrInfo *DI = static_cast<const AttrInfo *>(AInfo);
904  if (const IBOutletCollectionInfo *
905        IBInfo = dyn_cast<IBOutletCollectionInfo>(DI))
906    return &IBInfo->IBCollInfo;
907
908  return 0;
909}
910
911const CXIdxCXXClassDeclInfo *
912clang_index_getCXXClassDeclInfo(const CXIdxDeclInfo *DInfo) {
913  if (!DInfo)
914    return 0;
915
916  const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
917  if (const CXXClassDeclInfo *ClassInfo = dyn_cast<CXXClassDeclInfo>(DI))
918    return &ClassInfo->CXXClassInfo;
919
920  return 0;
921}
922
923CXIdxClientContainer
924clang_index_getClientContainer(const CXIdxContainerInfo *info) {
925  if (!info)
926    return 0;
927  const ContainerInfo *Container = static_cast<const ContainerInfo *>(info);
928  return Container->IndexCtx->getClientContainerForDC(Container->DC);
929}
930
931void clang_index_setClientContainer(const CXIdxContainerInfo *info,
932                                    CXIdxClientContainer client) {
933  if (!info)
934    return;
935  const ContainerInfo *Container = static_cast<const ContainerInfo *>(info);
936  Container->IndexCtx->addContainerInMap(Container->DC, client);
937}
938
939CXIdxClientEntity clang_index_getClientEntity(const CXIdxEntityInfo *info) {
940  if (!info)
941    return 0;
942  const EntityInfo *Entity = static_cast<const EntityInfo *>(info);
943  return Entity->IndexCtx->getClientEntity(Entity->Dcl);
944}
945
946void clang_index_setClientEntity(const CXIdxEntityInfo *info,
947                                 CXIdxClientEntity client) {
948  if (!info)
949    return;
950  const EntityInfo *Entity = static_cast<const EntityInfo *>(info);
951  Entity->IndexCtx->setClientEntity(Entity->Dcl, client);
952}
953
954CXIndexAction clang_IndexAction_create(CXIndex CIdx) {
955  return new IndexSessionData(CIdx);
956}
957
958void clang_IndexAction_dispose(CXIndexAction idxAction) {
959  if (idxAction)
960    delete static_cast<IndexSessionData *>(idxAction);
961}
962
963int clang_indexSourceFile(CXIndexAction idxAction,
964                          CXClientData client_data,
965                          IndexerCallbacks *index_callbacks,
966                          unsigned index_callbacks_size,
967                          unsigned index_options,
968                          const char *source_filename,
969                          const char * const *command_line_args,
970                          int num_command_line_args,
971                          struct CXUnsavedFile *unsaved_files,
972                          unsigned num_unsaved_files,
973                          CXTranslationUnit *out_TU,
974                          unsigned TU_options) {
975  LOG_FUNC_SECTION {
976    *Log << source_filename << ": ";
977    for (int i = 0; i != num_command_line_args; ++i)
978      *Log << command_line_args[i] << " ";
979  }
980
981  IndexSourceFileInfo ITUI = { idxAction, client_data, index_callbacks,
982                               index_callbacks_size, index_options,
983                               source_filename, command_line_args,
984                               num_command_line_args, unsaved_files,
985                               num_unsaved_files, out_TU, TU_options, 0 };
986
987  if (getenv("LIBCLANG_NOTHREADS")) {
988    clang_indexSourceFile_Impl(&ITUI);
989    return ITUI.result;
990  }
991
992  llvm::CrashRecoveryContext CRC;
993
994  if (!RunSafely(CRC, clang_indexSourceFile_Impl, &ITUI)) {
995    fprintf(stderr, "libclang: crash detected during indexing source file: {\n");
996    fprintf(stderr, "  'source_filename' : '%s'\n", source_filename);
997    fprintf(stderr, "  'command_line_args' : [");
998    for (int i = 0; i != num_command_line_args; ++i) {
999      if (i)
1000        fprintf(stderr, ", ");
1001      fprintf(stderr, "'%s'", command_line_args[i]);
1002    }
1003    fprintf(stderr, "],\n");
1004    fprintf(stderr, "  'unsaved_files' : [");
1005    for (unsigned i = 0; i != num_unsaved_files; ++i) {
1006      if (i)
1007        fprintf(stderr, ", ");
1008      fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename,
1009              unsaved_files[i].Length);
1010    }
1011    fprintf(stderr, "],\n");
1012    fprintf(stderr, "  'options' : %d,\n", TU_options);
1013    fprintf(stderr, "}\n");
1014
1015    return 1;
1016  } else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
1017    if (out_TU)
1018      PrintLibclangResourceUsage(*out_TU);
1019  }
1020
1021  return ITUI.result;
1022}
1023
1024int clang_indexTranslationUnit(CXIndexAction idxAction,
1025                               CXClientData client_data,
1026                               IndexerCallbacks *index_callbacks,
1027                               unsigned index_callbacks_size,
1028                               unsigned index_options,
1029                               CXTranslationUnit TU) {
1030  LOG_FUNC_SECTION {
1031    *Log << TU;
1032  }
1033
1034  IndexTranslationUnitInfo ITUI = { idxAction, client_data, index_callbacks,
1035                                    index_callbacks_size, index_options, TU,
1036                                    0 };
1037
1038  if (getenv("LIBCLANG_NOTHREADS")) {
1039    clang_indexTranslationUnit_Impl(&ITUI);
1040    return ITUI.result;
1041  }
1042
1043  llvm::CrashRecoveryContext CRC;
1044
1045  if (!RunSafely(CRC, clang_indexTranslationUnit_Impl, &ITUI)) {
1046    fprintf(stderr, "libclang: crash detected during indexing TU\n");
1047
1048    return 1;
1049  }
1050
1051  return ITUI.result;
1052}
1053
1054void clang_indexLoc_getFileLocation(CXIdxLoc location,
1055                                    CXIdxClientFile *indexFile,
1056                                    CXFile *file,
1057                                    unsigned *line,
1058                                    unsigned *column,
1059                                    unsigned *offset) {
1060  if (indexFile) *indexFile = 0;
1061  if (file)   *file = 0;
1062  if (line)   *line = 0;
1063  if (column) *column = 0;
1064  if (offset) *offset = 0;
1065
1066  SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
1067  if (!location.ptr_data[0] || Loc.isInvalid())
1068    return;
1069
1070  IndexingContext &IndexCtx =
1071      *static_cast<IndexingContext*>(location.ptr_data[0]);
1072  IndexCtx.translateLoc(Loc, indexFile, file, line, column, offset);
1073}
1074
1075CXSourceLocation clang_indexLoc_getCXSourceLocation(CXIdxLoc location) {
1076  SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
1077  if (!location.ptr_data[0] || Loc.isInvalid())
1078    return clang_getNullLocation();
1079
1080  IndexingContext &IndexCtx =
1081      *static_cast<IndexingContext*>(location.ptr_data[0]);
1082  return cxloc::translateSourceLocation(IndexCtx.getASTContext(), Loc);
1083}
1084
1085} // end: extern "C"
1086
1087