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"
15 #include "CIndexer.h"
16 
17 #include "clang/Frontend/ASTUnit.h"
18 #include "clang/Frontend/CompilerInvocation.h"
19 #include "clang/Frontend/CompilerInstance.h"
20 #include "clang/Frontend/Utils.h"
21 #include "clang/Sema/SemaConsumer.h"
22 #include "clang/AST/ASTConsumer.h"
23 #include "clang/AST/DeclVisitor.h"
24 #include "clang/Lex/Preprocessor.h"
25 #include "clang/Lex/PPCallbacks.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/CrashRecoveryContext.h"
28 
29 using namespace clang;
30 using namespace cxstring;
31 using namespace cxtu;
32 using namespace cxindex;
33 
34 namespace {
35 
36 //===----------------------------------------------------------------------===//
37 // IndexPPCallbacks
38 //===----------------------------------------------------------------------===//
39 
40 class IndexPPCallbacks : public PPCallbacks {
41   Preprocessor &PP;
42   IndexingContext &IndexCtx;
43 
44 public:
45   IndexPPCallbacks(Preprocessor &PP, IndexingContext &indexCtx)
46     : PP(PP), IndexCtx(indexCtx) { }
47 
48   virtual void InclusionDirective(SourceLocation HashLoc,
49                                   const Token &IncludeTok,
50                                   StringRef FileName,
51                                   bool IsAngled,
52                                   const FileEntry *File,
53                                   SourceLocation EndLoc,
54                                   StringRef SearchPath,
55                                   StringRef RelativePath) {
56     bool isImport = (IncludeTok.is(tok::identifier) &&
57             IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import);
58     IndexCtx.ppIncludedFile(HashLoc, FileName, File, isImport, IsAngled);
59   }
60 
61   /// MacroDefined - This hook is called whenever a macro definition is seen.
62   virtual void MacroDefined(const Token &Id, const MacroInfo *MI) {
63     if (MI->isBuiltinMacro())
64       return;
65     if (IndexCtx.isNotFromSourceFile(MI->getDefinitionLoc()))
66       return;
67 
68     SourceLocation Loc = MI->getDefinitionLoc();
69     SourceLocation DefBegin = MI->tokens_empty() ? Loc
70                                      : MI->getReplacementToken(0).getLocation();
71     IndexCtx.ppMacroDefined(Loc,
72                             Id.getIdentifierInfo()->getName(),
73                             DefBegin,
74                             MI->getDefinitionLength(PP.getSourceManager()),
75                             MI);
76   }
77 
78   /// MacroUndefined - This hook is called whenever a macro #undef is seen.
79   /// MI is released immediately following this callback.
80   virtual void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI) {
81     if (MI->isBuiltinMacro())
82       return;
83     if (IndexCtx.isNotFromSourceFile(MI->getDefinitionLoc()))
84       return;
85 
86     SourceLocation Loc = MacroNameTok.getLocation();
87     IndexCtx.ppMacroUndefined(Loc,
88                             MacroNameTok.getIdentifierInfo()->getName(),
89                             MI);
90   }
91 
92   /// MacroExpands - This is called by when a macro invocation is found.
93   virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI,
94                             SourceRange Range) {
95     if (MI->isBuiltinMacro())
96       return;
97     if (IndexCtx.isNotFromSourceFile(MI->getDefinitionLoc()))
98       return;
99 
100     SourceLocation Loc = MacroNameTok.getLocation();
101     IndexCtx.ppMacroExpanded(Loc,
102                              MacroNameTok.getIdentifierInfo()->getName(),
103                              MI);
104   }
105 
106   /// SourceRangeSkipped - This hook is called when a source range is skipped.
107   /// \param Range The SourceRange that was skipped. The range begins at the
108   /// #if/#else directive and ends after the #endif/#else directive.
109   virtual void SourceRangeSkipped(SourceRange Range) {
110   }
111 };
112 
113 //===----------------------------------------------------------------------===//
114 // IndexingConsumer
115 //===----------------------------------------------------------------------===//
116 
117 class IndexingConsumer : public ASTConsumer {
118   IndexingContext &IndexCtx;
119 
120 public:
121   explicit IndexingConsumer(IndexingContext &indexCtx)
122     : IndexCtx(indexCtx) { }
123 
124   // ASTConsumer Implementation
125 
126   virtual void Initialize(ASTContext &Context) {
127     IndexCtx.setASTContext(Context);
128     IndexCtx.invokeStartedTranslationUnit();
129   }
130 
131   virtual void HandleTranslationUnit(ASTContext &Ctx) {
132     IndexCtx.invokeFinishedTranslationUnit();
133   }
134 
135   virtual void HandleTopLevelDecl(DeclGroupRef DG) {
136     IndexCtx.indexDeclGroupRef(DG);
137   }
138 
139   /// \brief Handle the specified top-level declaration that occurred inside
140   /// and ObjC container.
141   virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) {
142     // They will be handled after the interface is seen first.
143     IndexCtx.addTUDeclInObjCContainer(D);
144   }
145 
146   /// \brief This is called by the AST reader when deserializing things.
147   /// The default implementation forwards to HandleTopLevelDecl but we don't
148   /// care about them when indexing, so have an empty definition.
149   virtual void HandleInterestingDecl(DeclGroupRef D) {}
150 };
151 
152 //===----------------------------------------------------------------------===//
153 // IndexingDiagnosticConsumer
154 //===----------------------------------------------------------------------===//
155 
156 class IndexingDiagnosticConsumer : public DiagnosticConsumer {
157   IndexingContext &IndexCtx;
158 
159 public:
160   explicit IndexingDiagnosticConsumer(IndexingContext &indexCtx)
161     : IndexCtx(indexCtx) {}
162 
163   virtual void HandleDiagnostic(DiagnosticsEngine::Level Level,
164                                 const Diagnostic &Info) {
165     // Default implementation (Warnings/errors count).
166     DiagnosticConsumer::HandleDiagnostic(Level, Info);
167 
168     IndexCtx.handleDiagnostic(StoredDiagnostic(Level, Info));
169   }
170 
171   DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
172     return new IgnoringDiagConsumer();
173   }
174 };
175 
176 class CaptureDiagnosticConsumer : public DiagnosticConsumer {
177   SmallVector<StoredDiagnostic, 4> Errors;
178 public:
179 
180   virtual void HandleDiagnostic(DiagnosticsEngine::Level level,
181                                 const Diagnostic &Info) {
182     if (level >= DiagnosticsEngine::Error)
183       Errors.push_back(StoredDiagnostic(level, Info));
184   }
185 
186   DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
187     return new IgnoringDiagConsumer();
188   }
189 };
190 
191 //===----------------------------------------------------------------------===//
192 // IndexingFrontendAction
193 //===----------------------------------------------------------------------===//
194 
195 class IndexingFrontendAction : public ASTFrontendAction {
196   IndexingContext IndexCtx;
197 
198 public:
199   IndexingFrontendAction(CXClientData clientData,
200                          IndexerCallbacks &indexCallbacks,
201                          unsigned indexOptions,
202                          CXTranslationUnit cxTU)
203     : IndexCtx(clientData, indexCallbacks, indexOptions, cxTU) { }
204 
205   virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
206                                          StringRef InFile) {
207     CI.getDiagnostics().setClient(new IndexingDiagnosticConsumer(IndexCtx),
208                                   /*own=*/true);
209     IndexCtx.setASTContext(CI.getASTContext());
210     Preprocessor &PP = CI.getPreprocessor();
211     PP.addPPCallbacks(new IndexPPCallbacks(PP, IndexCtx));
212     return new IndexingConsumer(IndexCtx);
213   }
214 
215   virtual TranslationUnitKind getTranslationUnitKind() { return TU_Prefix; }
216   virtual bool hasCodeCompletionSupport() const { return false; }
217 };
218 
219 //===----------------------------------------------------------------------===//
220 // clang_indexTranslationUnit Implementation
221 //===----------------------------------------------------------------------===//
222 
223 struct IndexTranslationUnitInfo {
224   CXIndex CIdx;
225   CXClientData client_data;
226   IndexerCallbacks *index_callbacks;
227   unsigned index_callbacks_size;
228   unsigned index_options;
229   const char *source_filename;
230   const char *const *command_line_args;
231   int num_command_line_args;
232   struct CXUnsavedFile *unsaved_files;
233   unsigned num_unsaved_files;
234   CXTranslationUnit *out_TU;
235   unsigned TU_options;
236   int result;
237 };
238 
239 struct MemBufferOwner {
240   SmallVector<const llvm::MemoryBuffer *, 8> Buffers;
241 
242   ~MemBufferOwner() {
243     for (SmallVectorImpl<const llvm::MemoryBuffer *>::iterator
244            I = Buffers.begin(), E = Buffers.end(); I != E; ++I)
245       delete *I;
246   }
247 };
248 
249 } // anonymous namespace
250 
251 static void clang_indexTranslationUnit_Impl(void *UserData) {
252   IndexTranslationUnitInfo *ITUI =
253     static_cast<IndexTranslationUnitInfo*>(UserData);
254   CXIndex CIdx = ITUI->CIdx;
255   CXClientData client_data = ITUI->client_data;
256   IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks;
257   unsigned index_callbacks_size = ITUI->index_callbacks_size;
258   unsigned index_options = ITUI->index_options;
259   const char *source_filename = ITUI->source_filename;
260   const char * const *command_line_args = ITUI->command_line_args;
261   int num_command_line_args = ITUI->num_command_line_args;
262   struct CXUnsavedFile *unsaved_files = ITUI->unsaved_files;
263   unsigned num_unsaved_files = ITUI->num_unsaved_files;
264   CXTranslationUnit *out_TU  = ITUI->out_TU;
265   unsigned TU_options = ITUI->TU_options;
266   ITUI->result = 1; // init as error.
267 
268   if (out_TU)
269     *out_TU = 0;
270   bool requestedToGetTU = (out_TU != 0);
271 
272   if (!CIdx)
273     return;
274   if (!client_index_callbacks || index_callbacks_size == 0)
275     return;
276 
277   IndexerCallbacks CB;
278   memset(&CB, 0, sizeof(CB));
279   unsigned ClientCBSize = index_callbacks_size < sizeof(CB)
280                                   ? index_callbacks_size : sizeof(CB);
281   memcpy(&CB, client_index_callbacks, ClientCBSize);
282 
283   CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
284 
285   (void)CXXIdx;
286   (void)TU_options;
287 
288   CaptureDiagnosticConsumer *CaptureDiag = new CaptureDiagnosticConsumer();
289 
290   // Configure the diagnostics.
291   DiagnosticOptions DiagOpts;
292   llvm::IntrusiveRefCntPtr<DiagnosticsEngine>
293     Diags(CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args,
294                                                 command_line_args,
295                                                 CaptureDiag,
296                                                 /*ShouldOwnClient=*/true));
297 
298   // Recover resources if we crash before exiting this function.
299   llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
300     llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
301     DiagCleanup(Diags.getPtr());
302 
303   llvm::OwningPtr<std::vector<const char *> >
304     Args(new std::vector<const char*>());
305 
306   // Recover resources if we crash before exiting this method.
307   llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> >
308     ArgsCleanup(Args.get());
309 
310   Args->insert(Args->end(), command_line_args,
311                command_line_args + num_command_line_args);
312 
313   // The 'source_filename' argument is optional.  If the caller does not
314   // specify it then it is assumed that the source file is specified
315   // in the actual argument list.
316   // Put the source file after command_line_args otherwise if '-x' flag is
317   // present it will be unused.
318   if (source_filename)
319     Args->push_back(source_filename);
320 
321   llvm::IntrusiveRefCntPtr<CompilerInvocation>
322     CInvok(createInvocationFromCommandLine(*Args, Diags));
323 
324   if (!CInvok)
325     return;
326 
327   // Recover resources if we crash before exiting this function.
328   llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation,
329     llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> >
330     CInvokCleanup(CInvok.getPtr());
331 
332   if (CInvok->getFrontendOpts().Inputs.empty())
333     return;
334 
335   llvm::OwningPtr<MemBufferOwner> BufOwner(new MemBufferOwner());
336 
337   // Recover resources if we crash before exiting this method.
338   llvm::CrashRecoveryContextCleanupRegistrar<MemBufferOwner>
339     BufOwnerCleanup(BufOwner.get());
340 
341   for (unsigned I = 0; I != num_unsaved_files; ++I) {
342     StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
343     const llvm::MemoryBuffer *Buffer
344       = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
345     CInvok->getPreprocessorOpts().addRemappedFile(unsaved_files[I].Filename, Buffer);
346     BufOwner->Buffers.push_back(Buffer);
347   }
348 
349   // Since libclang is primarily used by batch tools dealing with
350   // (often very broken) source code, where spell-checking can have a
351   // significant negative impact on performance (particularly when
352   // precompiled headers are involved), we disable it.
353   CInvok->getLangOpts().SpellChecking = false;
354 
355   if (!requestedToGetTU)
356     CInvok->getPreprocessorOpts().DetailedRecord = false;
357 
358   ASTUnit *Unit = ASTUnit::create(CInvok.getPtr(), Diags);
359   llvm::OwningPtr<CXTUOwner> CXTU(new CXTUOwner(MakeCXTranslationUnit(Unit)));
360 
361   // Recover resources if we crash before exiting this method.
362   llvm::CrashRecoveryContextCleanupRegistrar<CXTUOwner>
363     CXTUCleanup(CXTU.get());
364 
365   llvm::OwningPtr<IndexingFrontendAction> IndexAction;
366   IndexAction.reset(new IndexingFrontendAction(client_data, CB,
367                                                index_options, CXTU->getTU()));
368 
369   // Recover resources if we crash before exiting this method.
370   llvm::CrashRecoveryContextCleanupRegistrar<IndexingFrontendAction>
371     IndexActionCleanup(IndexAction.get());
372 
373   Unit = ASTUnit::LoadFromCompilerInvocationAction(CInvok.getPtr(), Diags,
374                                                        IndexAction.get(),
375                                                        Unit);
376   if (!Unit)
377     return;
378 
379   // FIXME: Set state of the ASTUnit according to the TU_options.
380   if (out_TU)
381     *out_TU = CXTU->takeTU();
382 
383   ITUI->result = 0; // success.
384 }
385 
386 //===----------------------------------------------------------------------===//
387 // libclang public APIs.
388 //===----------------------------------------------------------------------===//
389 
390 extern "C" {
391 
392 int clang_indexTranslationUnit(CXIndex CIdx,
393                                 CXClientData client_data,
394                                 IndexerCallbacks *index_callbacks,
395                                 unsigned index_callbacks_size,
396                                 unsigned index_options,
397                                 const char *source_filename,
398                                 const char * const *command_line_args,
399                                 int num_command_line_args,
400                                 struct CXUnsavedFile *unsaved_files,
401                                 unsigned num_unsaved_files,
402                                 CXTranslationUnit *out_TU,
403                                 unsigned TU_options) {
404   IndexTranslationUnitInfo ITUI = { CIdx, client_data, index_callbacks,
405                                     index_callbacks_size, index_options,
406                                     source_filename, command_line_args,
407                                     num_command_line_args, unsaved_files,
408                                     num_unsaved_files, out_TU, TU_options, 0 };
409 
410   if (getenv("LIBCLANG_NOTHREADS")) {
411     clang_indexTranslationUnit_Impl(&ITUI);
412     return ITUI.result;
413   }
414 
415   llvm::CrashRecoveryContext CRC;
416 
417   if (!RunSafely(CRC, clang_indexTranslationUnit_Impl, &ITUI)) {
418     fprintf(stderr, "libclang: crash detected during parsing: {\n");
419     fprintf(stderr, "  'source_filename' : '%s'\n", source_filename);
420     fprintf(stderr, "  'command_line_args' : [");
421     for (int i = 0; i != num_command_line_args; ++i) {
422       if (i)
423         fprintf(stderr, ", ");
424       fprintf(stderr, "'%s'", command_line_args[i]);
425     }
426     fprintf(stderr, "],\n");
427     fprintf(stderr, "  'unsaved_files' : [");
428     for (unsigned i = 0; i != num_unsaved_files; ++i) {
429       if (i)
430         fprintf(stderr, ", ");
431       fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename,
432               unsaved_files[i].Length);
433     }
434     fprintf(stderr, "],\n");
435     fprintf(stderr, "  'options' : %d,\n", TU_options);
436     fprintf(stderr, "}\n");
437 
438     return 1;
439   } else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
440     if (out_TU)
441       PrintLibclangResourceUsage(*out_TU);
442   }
443 
444   return ITUI.result;
445 }
446 
447 void clang_indexLoc_getFileLocation(CXIdxLoc location,
448                                     CXIdxFile *indexFile,
449                                     CXFile *file,
450                                     unsigned *line,
451                                     unsigned *column,
452                                     unsigned *offset) {
453   if (indexFile) *indexFile = 0;
454   if (file)   *file = 0;
455   if (line)   *line = 0;
456   if (column) *column = 0;
457   if (offset) *offset = 0;
458 
459   SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
460   if (!location.ptr_data[0] || Loc.isInvalid())
461     return;
462 
463   IndexingContext &IndexCtx =
464       *static_cast<IndexingContext*>(location.ptr_data[0]);
465   IndexCtx.translateLoc(Loc, indexFile, file, line, column, offset);
466 }
467 
468 CXSourceLocation clang_indexLoc_getCXSourceLocation(CXIdxLoc location) {
469   SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
470   if (!location.ptr_data[0] || Loc.isInvalid())
471     return clang_getNullLocation();
472 
473   IndexingContext &IndexCtx =
474       *static_cast<IndexingContext*>(location.ptr_data[0]);
475   return cxloc::translateSourceLocation(IndexCtx.getASTContext(), Loc);
476 }
477 
478 } // end: extern "C"
479 
480