1 //===--- ASTUnit.cpp - ASTUnit utility ------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // ASTUnit Implementation.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Frontend/ASTUnit.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/AST/DeclVisitor.h"
18 #include "clang/AST/TypeOrdering.h"
19 #include "clang/AST/StmtVisitor.h"
20 #include "clang/Driver/Compilation.h"
21 #include "clang/Driver/Driver.h"
22 #include "clang/Driver/Job.h"
23 #include "clang/Driver/ArgList.h"
24 #include "clang/Driver/Options.h"
25 #include "clang/Driver/Tool.h"
26 #include "clang/Frontend/CompilerInstance.h"
27 #include "clang/Frontend/FrontendActions.h"
28 #include "clang/Frontend/FrontendDiagnostic.h"
29 #include "clang/Frontend/FrontendOptions.h"
30 #include "clang/Frontend/Utils.h"
31 #include "clang/Serialization/ASTReader.h"
32 #include "clang/Serialization/ASTWriter.h"
33 #include "clang/Lex/HeaderSearch.h"
34 #include "clang/Lex/Preprocessor.h"
35 #include "clang/Basic/TargetOptions.h"
36 #include "clang/Basic/TargetInfo.h"
37 #include "clang/Basic/Diagnostic.h"
38 #include "llvm/ADT/ArrayRef.h"
39 #include "llvm/ADT/StringExtras.h"
40 #include "llvm/ADT/StringSet.h"
41 #include "llvm/Support/Atomic.h"
42 #include "llvm/Support/MemoryBuffer.h"
43 #include "llvm/Support/Host.h"
44 #include "llvm/Support/Path.h"
45 #include "llvm/Support/raw_ostream.h"
46 #include "llvm/Support/Timer.h"
47 #include "llvm/Support/FileSystem.h"
48 #include "llvm/Support/Mutex.h"
49 #include "llvm/Support/CrashRecoveryContext.h"
53 using namespace clang;
55 using llvm::TimeRecord;
64 explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
66 Start = TimeRecord::getCurrentTime();
69 void setOutput(const Twine &Output) {
71 this->Output = Output.str();
76 TimeRecord Elapsed = TimeRecord::getCurrentTime();
78 llvm::errs() << Output << ':';
79 Elapsed.print(Elapsed, llvm::errs());
86 /// \brief After failing to build a precompiled preamble (due to
87 /// errors in the source that occurs in the preamble), the number of
88 /// reparses during which we'll skip even trying to precompile the
90 const unsigned DefaultPreambleRebuildInterval = 5;
92 /// \brief Tracks the number of ASTUnit objects that are currently active.
94 /// Used for debugging purposes only.
95 static llvm::sys::cas_flag ActiveASTUnitObjects;
97 ASTUnit::ASTUnit(bool _MainFileIsAST)
98 : OnlyLocalDecls(false), CaptureDiagnostics(false),
99 MainFileIsAST(_MainFileIsAST),
100 TUKind(TU_Complete), WantTiming(getenv("LIBCLANG_TIMING")),
101 OwnsRemappedFileBuffers(true),
102 NumStoredDiagnosticsFromDriver(0),
103 PreambleRebuildCounter(0), SavedMainFileBuffer(0), PreambleBuffer(0),
104 ShouldCacheCodeCompletionResults(false),
105 NestedMacroExpansions(true),
106 CompletionCacheTopLevelHashValue(0),
107 PreambleTopLevelHashValue(0),
108 CurrentTopLevelHashValue(0),
109 UnsafeToFree(false) {
110 if (getenv("LIBCLANG_OBJTRACKING")) {
111 llvm::sys::AtomicIncrement(&ActiveASTUnitObjects);
112 fprintf(stderr, "+++ %d translation units\n", ActiveASTUnitObjects);
116 ASTUnit::~ASTUnit() {
117 CleanTemporaryFiles();
118 if (!PreambleFile.empty())
119 llvm::sys::Path(PreambleFile).eraseFromDisk();
121 // Free the buffers associated with remapped files. We are required to
122 // perform this operation here because we explicitly request that the
123 // compiler instance *not* free these buffers for each invocation of the
125 if (Invocation.getPtr() && OwnsRemappedFileBuffers) {
126 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
127 for (PreprocessorOptions::remapped_file_buffer_iterator
128 FB = PPOpts.remapped_file_buffer_begin(),
129 FBEnd = PPOpts.remapped_file_buffer_end();
135 delete SavedMainFileBuffer;
136 delete PreambleBuffer;
138 ClearCachedCompletionResults();
140 if (getenv("LIBCLANG_OBJTRACKING")) {
141 llvm::sys::AtomicDecrement(&ActiveASTUnitObjects);
142 fprintf(stderr, "--- %d translation units\n", ActiveASTUnitObjects);
146 void ASTUnit::CleanTemporaryFiles() {
147 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
148 TemporaryFiles[I].eraseFromDisk();
149 TemporaryFiles.clear();
152 /// \brief Determine the set of code-completion contexts in which this
153 /// declaration should be shown.
154 static unsigned getDeclShowContexts(NamedDecl *ND,
155 const LangOptions &LangOpts,
156 bool &IsNestedNameSpecifier) {
157 IsNestedNameSpecifier = false;
159 if (isa<UsingShadowDecl>(ND))
160 ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl());
164 unsigned Contexts = 0;
165 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) ||
166 isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) {
167 // Types can appear in these contexts.
168 if (LangOpts.CPlusPlus || !isa<TagDecl>(ND))
169 Contexts |= (1 << (CodeCompletionContext::CCC_TopLevel - 1))
170 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
171 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
172 | (1 << (CodeCompletionContext::CCC_Statement - 1))
173 | (1 << (CodeCompletionContext::CCC_Type - 1))
174 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1));
176 // In C++, types can appear in expressions contexts (for functional casts).
177 if (LangOpts.CPlusPlus)
178 Contexts |= (1 << (CodeCompletionContext::CCC_Expression - 1));
180 // In Objective-C, message sends can send interfaces. In Objective-C++,
181 // all types are available due to functional casts.
182 if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND))
183 Contexts |= (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1));
185 // In Objective-C, you can only be a subclass of another Objective-C class
186 if (isa<ObjCInterfaceDecl>(ND))
187 Contexts |= (1 << (CodeCompletionContext::CCC_ObjCInterfaceName - 1));
189 // Deal with tag names.
190 if (isa<EnumDecl>(ND)) {
191 Contexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1));
193 // Part of the nested-name-specifier in C++0x.
194 if (LangOpts.CPlusPlus0x)
195 IsNestedNameSpecifier = true;
196 } else if (RecordDecl *Record = dyn_cast<RecordDecl>(ND)) {
197 if (Record->isUnion())
198 Contexts |= (1 << (CodeCompletionContext::CCC_UnionTag - 1));
200 Contexts |= (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1));
202 if (LangOpts.CPlusPlus)
203 IsNestedNameSpecifier = true;
204 } else if (isa<ClassTemplateDecl>(ND))
205 IsNestedNameSpecifier = true;
206 } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) {
207 // Values can appear in these contexts.
208 Contexts = (1 << (CodeCompletionContext::CCC_Statement - 1))
209 | (1 << (CodeCompletionContext::CCC_Expression - 1))
210 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
211 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1));
212 } else if (isa<ObjCProtocolDecl>(ND)) {
213 Contexts = (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1));
214 } else if (isa<ObjCCategoryDecl>(ND)) {
215 Contexts = (1 << (CodeCompletionContext::CCC_ObjCCategoryName - 1));
216 } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
217 Contexts = (1 << (CodeCompletionContext::CCC_Namespace - 1));
219 // Part of the nested-name-specifier.
220 IsNestedNameSpecifier = true;
226 void ASTUnit::CacheCodeCompletionResults() {
230 SimpleTimer Timer(WantTiming);
231 Timer.setOutput("Cache global code completions for " + getMainFileName());
233 // Clear out the previous results.
234 ClearCachedCompletionResults();
236 // Gather the set of global code completions.
237 typedef CodeCompletionResult Result;
238 SmallVector<Result, 8> Results;
239 CachedCompletionAllocator = new GlobalCodeCompletionAllocator;
240 TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator, Results);
242 // Translate global code completions into cached completions.
243 llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
245 for (unsigned I = 0, N = Results.size(); I != N; ++I) {
246 switch (Results[I].Kind) {
247 case Result::RK_Declaration: {
248 bool IsNestedNameSpecifier = false;
249 CachedCodeCompletionResult CachedResult;
250 CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema,
251 *CachedCompletionAllocator);
252 CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration,
253 Ctx->getLangOptions(),
254 IsNestedNameSpecifier);
255 CachedResult.Priority = Results[I].Priority;
256 CachedResult.Kind = Results[I].CursorKind;
257 CachedResult.Availability = Results[I].Availability;
259 // Keep track of the type of this completion in an ASTContext-agnostic
261 QualType UsageType = getDeclUsageType(*Ctx, Results[I].Declaration);
262 if (UsageType.isNull()) {
263 CachedResult.TypeClass = STC_Void;
264 CachedResult.Type = 0;
266 CanQualType CanUsageType
267 = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
268 CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
270 // Determine whether we have already seen this type. If so, we save
271 // ourselves the work of formatting the type string by using the
272 // temporary, CanQualType-based hash table to find the associated value.
273 unsigned &TypeValue = CompletionTypes[CanUsageType];
274 if (TypeValue == 0) {
275 TypeValue = CompletionTypes.size();
276 CachedCompletionTypes[QualType(CanUsageType).getAsString()]
280 CachedResult.Type = TypeValue;
283 CachedCompletionResults.push_back(CachedResult);
285 /// Handle nested-name-specifiers in C++.
286 if (TheSema->Context.getLangOptions().CPlusPlus &&
287 IsNestedNameSpecifier && !Results[I].StartsNestedNameSpecifier) {
288 // The contexts in which a nested-name-specifier can appear in C++.
290 = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
291 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
292 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
293 | (1 << (CodeCompletionContext::CCC_Statement - 1))
294 | (1 << (CodeCompletionContext::CCC_Expression - 1))
295 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
296 | (1 << (CodeCompletionContext::CCC_EnumTag - 1))
297 | (1 << (CodeCompletionContext::CCC_UnionTag - 1))
298 | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1))
299 | (1 << (CodeCompletionContext::CCC_Type - 1))
300 | (1 << (CodeCompletionContext::CCC_PotentiallyQualifiedName - 1))
301 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1));
303 if (isa<NamespaceDecl>(Results[I].Declaration) ||
304 isa<NamespaceAliasDecl>(Results[I].Declaration))
305 NNSContexts |= (1 << (CodeCompletionContext::CCC_Namespace - 1));
307 if (unsigned RemainingContexts
308 = NNSContexts & ~CachedResult.ShowInContexts) {
309 // If there any contexts where this completion can be a
310 // nested-name-specifier but isn't already an option, create a
311 // nested-name-specifier completion.
312 Results[I].StartsNestedNameSpecifier = true;
313 CachedResult.Completion
314 = Results[I].CreateCodeCompletionString(*TheSema,
315 *CachedCompletionAllocator);
316 CachedResult.ShowInContexts = RemainingContexts;
317 CachedResult.Priority = CCP_NestedNameSpecifier;
318 CachedResult.TypeClass = STC_Void;
319 CachedResult.Type = 0;
320 CachedCompletionResults.push_back(CachedResult);
326 case Result::RK_Keyword:
327 case Result::RK_Pattern:
328 // Ignore keywords and patterns; we don't care, since they are so
329 // easily regenerated.
332 case Result::RK_Macro: {
333 CachedCodeCompletionResult CachedResult;
334 CachedResult.Completion
335 = Results[I].CreateCodeCompletionString(*TheSema,
336 *CachedCompletionAllocator);
337 CachedResult.ShowInContexts
338 = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
339 | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1))
340 | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1))
341 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
342 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
343 | (1 << (CodeCompletionContext::CCC_Statement - 1))
344 | (1 << (CodeCompletionContext::CCC_Expression - 1))
345 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
346 | (1 << (CodeCompletionContext::CCC_MacroNameUse - 1))
347 | (1 << (CodeCompletionContext::CCC_PreprocessorExpression - 1))
348 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
349 | (1 << (CodeCompletionContext::CCC_OtherWithMacros - 1));
351 CachedResult.Priority = Results[I].Priority;
352 CachedResult.Kind = Results[I].CursorKind;
353 CachedResult.Availability = Results[I].Availability;
354 CachedResult.TypeClass = STC_Void;
355 CachedResult.Type = 0;
356 CachedCompletionResults.push_back(CachedResult);
362 // Save the current top-level hash value.
363 CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
366 void ASTUnit::ClearCachedCompletionResults() {
367 CachedCompletionResults.clear();
368 CachedCompletionTypes.clear();
369 CachedCompletionAllocator = 0;
374 /// \brief Gathers information from ASTReader that will be used to initialize
376 class ASTInfoCollector : public ASTReaderListener {
379 LangOptions &LangOpt;
381 llvm::IntrusiveRefCntPtr<TargetInfo> &Target;
382 std::string &Predefines;
385 unsigned NumHeaderInfos;
387 bool InitializedLanguage;
389 ASTInfoCollector(Preprocessor &PP, ASTContext &Context, LangOptions &LangOpt,
391 llvm::IntrusiveRefCntPtr<TargetInfo> &Target,
392 std::string &Predefines,
394 : PP(PP), Context(Context), LangOpt(LangOpt), HSI(HSI), Target(Target),
395 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0),
396 InitializedLanguage(false) {}
398 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
399 if (InitializedLanguage)
404 // Initialize the preprocessor.
405 PP.Initialize(*Target);
407 // Initialize the ASTContext
408 Context.InitBuiltinTypes(*Target);
410 InitializedLanguage = true;
414 virtual bool ReadTargetTriple(StringRef Triple) {
415 // If we've already initialized the target, don't do it again.
419 // FIXME: This is broken, we should store the TargetOptions in the AST file.
420 TargetOptions TargetOpts;
422 TargetOpts.CXXABI = "";
424 TargetOpts.Features.clear();
425 TargetOpts.Triple = Triple;
426 Target = TargetInfo::CreateTargetInfo(PP.getDiagnostics(), TargetOpts);
430 virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
431 StringRef OriginalFileName,
432 std::string &SuggestedPredefines,
433 FileManager &FileMgr) {
434 Predefines = Buffers[0].Data;
435 for (unsigned I = 1, N = Buffers.size(); I != N; ++I) {
436 Predefines += Buffers[I].Data;
441 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {
442 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
445 virtual void ReadCounter(unsigned Value) {
450 class StoredDiagnosticConsumer : public DiagnosticConsumer {
451 SmallVectorImpl<StoredDiagnostic> &StoredDiags;
454 explicit StoredDiagnosticConsumer(
455 SmallVectorImpl<StoredDiagnostic> &StoredDiags)
456 : StoredDiags(StoredDiags) { }
458 virtual void HandleDiagnostic(DiagnosticsEngine::Level Level,
459 const Diagnostic &Info);
461 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
462 // Just drop any diagnostics that come from cloned consumers; they'll
463 // have different source managers anyway.
464 return new IgnoringDiagConsumer();
468 /// \brief RAII object that optionally captures diagnostics, if
469 /// there is no diagnostic client to capture them already.
470 class CaptureDroppedDiagnostics {
471 DiagnosticsEngine &Diags;
472 StoredDiagnosticConsumer Client;
473 DiagnosticConsumer *PreviousClient;
476 CaptureDroppedDiagnostics(bool RequestCapture, DiagnosticsEngine &Diags,
477 SmallVectorImpl<StoredDiagnostic> &StoredDiags)
478 : Diags(Diags), Client(StoredDiags), PreviousClient(0)
480 if (RequestCapture || Diags.getClient() == 0) {
481 PreviousClient = Diags.takeClient();
482 Diags.setClient(&Client);
486 ~CaptureDroppedDiagnostics() {
487 if (Diags.getClient() == &Client) {
489 Diags.setClient(PreviousClient);
494 } // anonymous namespace
496 void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level,
497 const Diagnostic &Info) {
498 // Default implementation (Warnings/errors count).
499 DiagnosticConsumer::HandleDiagnostic(Level, Info);
501 StoredDiags.push_back(StoredDiagnostic(Level, Info));
504 const std::string &ASTUnit::getOriginalSourceFileName() {
505 return OriginalSourceFile;
508 llvm::MemoryBuffer *ASTUnit::getBufferForFile(StringRef Filename,
509 std::string *ErrorStr) {
511 return FileMgr->getBufferForFile(Filename, ErrorStr);
514 /// \brief Configure the diagnostics object for use with ASTUnit.
515 void ASTUnit::ConfigureDiags(llvm::IntrusiveRefCntPtr<DiagnosticsEngine> &Diags,
516 const char **ArgBegin, const char **ArgEnd,
517 ASTUnit &AST, bool CaptureDiagnostics) {
518 if (!Diags.getPtr()) {
519 // No diagnostics engine was provided, so create our own diagnostics object
520 // with the default options.
521 DiagnosticOptions DiagOpts;
522 DiagnosticConsumer *Client = 0;
523 if (CaptureDiagnostics)
524 Client = new StoredDiagnosticConsumer(AST.StoredDiagnostics);
525 Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd- ArgBegin,
527 } else if (CaptureDiagnostics) {
528 Diags->setClient(new StoredDiagnosticConsumer(AST.StoredDiagnostics));
532 ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename,
533 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
534 const FileSystemOptions &FileSystemOpts,
536 RemappedFile *RemappedFiles,
537 unsigned NumRemappedFiles,
538 bool CaptureDiagnostics) {
539 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
541 // Recover resources if we crash before exiting this method.
542 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
543 ASTUnitCleanup(AST.get());
544 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
545 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
546 DiagCleanup(Diags.getPtr());
548 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
550 AST->OnlyLocalDecls = OnlyLocalDecls;
551 AST->CaptureDiagnostics = CaptureDiagnostics;
552 AST->Diagnostics = Diags;
553 AST->FileMgr = new FileManager(FileSystemOpts);
554 AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
555 AST->getFileManager());
556 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
558 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
559 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
560 if (const llvm::MemoryBuffer *
561 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
562 // Create the file entry for the file that we're mapping from.
563 const FileEntry *FromFile
564 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
565 memBuf->getBufferSize(),
568 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
569 << RemappedFiles[I].first;
574 // Override the contents of the "from" file with the contents of
576 AST->getSourceManager().overrideFileContents(FromFile, memBuf);
579 const char *fname = fileOrBuf.get<const char *>();
580 const FileEntry *ToFile = AST->FileMgr->getFile(fname);
582 AST->getDiagnostics().Report(diag::err_fe_remap_missing_to_file)
583 << RemappedFiles[I].first << fname;
587 // Create the file entry for the file that we're mapping from.
588 const FileEntry *FromFile
589 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
593 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
594 << RemappedFiles[I].first;
599 // Override the contents of the "from" file with the contents of
601 AST->getSourceManager().overrideFileContents(FromFile, ToFile);
605 // Gather Info for preprocessor construction later on.
607 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
608 std::string Predefines;
611 llvm::OwningPtr<ASTReader> Reader;
613 AST->PP = new Preprocessor(AST->getDiagnostics(), AST->ASTFileLangOpts,
614 /*Target=*/0, AST->getSourceManager(), HeaderInfo,
617 /*OwnsHeaderSearch=*/false,
618 /*DelayInitialization=*/true);
619 Preprocessor &PP = *AST->PP;
621 AST->Ctx = new ASTContext(AST->ASTFileLangOpts,
622 AST->getSourceManager(),
624 PP.getIdentifierTable(),
625 PP.getSelectorTable(),
627 /* size_reserve = */0,
628 /*DelayInitialization=*/true);
629 ASTContext &Context = *AST->Ctx;
631 Reader.reset(new ASTReader(PP, Context));
633 // Recover resources if we crash before exiting this method.
634 llvm::CrashRecoveryContextCleanupRegistrar<ASTReader>
635 ReaderCleanup(Reader.get());
637 Reader->setListener(new ASTInfoCollector(*AST->PP, Context,
638 AST->ASTFileLangOpts, HeaderInfo,
639 AST->Target, Predefines, Counter));
641 switch (Reader->ReadAST(Filename, serialization::MK_MainFile)) {
642 case ASTReader::Success:
645 case ASTReader::Failure:
646 case ASTReader::IgnorePCH:
647 AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
651 AST->OriginalSourceFile = Reader->getOriginalSourceFile();
653 PP.setPredefines(Reader->getSuggestedPredefines());
654 PP.setCounterValue(Counter);
656 // Attach the AST reader to the AST context as an external AST
657 // source, so that declarations will be deserialized from the
658 // AST file as needed.
659 ASTReader *ReaderPtr = Reader.get();
660 llvm::OwningPtr<ExternalASTSource> Source(Reader.take());
662 // Unregister the cleanup for ASTReader. It will get cleaned up
663 // by the ASTUnit cleanup.
664 ReaderCleanup.unregister();
666 Context.setExternalSource(Source);
668 // Create an AST consumer, even though it isn't used.
669 AST->Consumer.reset(new ASTConsumer);
671 // Create a semantic analysis object and tell the AST reader about it.
672 AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer));
673 AST->TheSema->Initialize();
674 ReaderPtr->InitializeSema(*AST->TheSema);
681 /// \brief Preprocessor callback class that updates a hash value with the names
682 /// of all macros that have been defined by the translation unit.
683 class MacroDefinitionTrackerPPCallbacks : public PPCallbacks {
687 explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { }
689 virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
690 Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash);
694 /// \brief Add the given declaration to the hash of all top-level entities.
695 void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) {
699 DeclContext *DC = D->getDeclContext();
703 if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit()))
706 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
707 if (ND->getIdentifier())
708 Hash = llvm::HashString(ND->getIdentifier()->getName(), Hash);
709 else if (DeclarationName Name = ND->getDeclName()) {
710 std::string NameStr = Name.getAsString();
711 Hash = llvm::HashString(NameStr, Hash);
716 if (ObjCForwardProtocolDecl *Forward
717 = dyn_cast<ObjCForwardProtocolDecl>(D)) {
718 for (ObjCForwardProtocolDecl::protocol_iterator
719 P = Forward->protocol_begin(),
720 PEnd = Forward->protocol_end();
722 AddTopLevelDeclarationToHash(*P, Hash);
726 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(D)) {
727 AddTopLevelDeclarationToHash(Class->getForwardInterfaceDecl(), Hash);
732 class TopLevelDeclTrackerConsumer : public ASTConsumer {
737 TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
738 : Unit(_Unit), Hash(Hash) {
742 void HandleTopLevelDecl(DeclGroupRef D) {
743 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
745 // FIXME: Currently ObjC method declarations are incorrectly being
746 // reported as top-level declarations, even though their DeclContext
747 // is the containing ObjC @interface/@implementation. This is a
748 // fundamental problem in the parser right now.
749 if (isa<ObjCMethodDecl>(D))
752 AddTopLevelDeclarationToHash(D, Hash);
753 Unit.addTopLevelDecl(D);
757 // We're not interested in "interesting" decls.
758 void HandleInterestingDecl(DeclGroupRef) {}
761 class TopLevelDeclTrackerAction : public ASTFrontendAction {
765 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
767 CI.getPreprocessor().addPPCallbacks(
768 new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
769 return new TopLevelDeclTrackerConsumer(Unit,
770 Unit.getCurrentTopLevelHashValue());
774 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
776 virtual bool hasCodeCompletionSupport() const { return false; }
777 virtual TranslationUnitKind getTranslationUnitKind() {
778 return Unit.getTranslationUnitKind();
782 class PrecompilePreambleConsumer : public PCHGenerator {
785 std::vector<Decl *> TopLevelDecls;
788 PrecompilePreambleConsumer(ASTUnit &Unit, const Preprocessor &PP,
789 StringRef isysroot, raw_ostream *Out)
790 : PCHGenerator(PP, "", /*IsModule=*/false, isysroot, Out), Unit(Unit),
791 Hash(Unit.getCurrentTopLevelHashValue()) {
795 virtual void HandleTopLevelDecl(DeclGroupRef D) {
796 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
798 // FIXME: Currently ObjC method declarations are incorrectly being
799 // reported as top-level declarations, even though their DeclContext
800 // is the containing ObjC @interface/@implementation. This is a
801 // fundamental problem in the parser right now.
802 if (isa<ObjCMethodDecl>(D))
804 AddTopLevelDeclarationToHash(D, Hash);
805 TopLevelDecls.push_back(D);
809 virtual void HandleTranslationUnit(ASTContext &Ctx) {
810 PCHGenerator::HandleTranslationUnit(Ctx);
811 if (!Unit.getDiagnostics().hasErrorOccurred()) {
812 // Translate the top-level declarations we captured during
813 // parsing into declaration IDs in the precompiled
814 // preamble. This will allow us to deserialize those top-level
815 // declarations when requested.
816 for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I)
817 Unit.addTopLevelDeclFromPreamble(
818 getWriter().getDeclID(TopLevelDecls[I]));
823 class PrecompilePreambleAction : public ASTFrontendAction {
827 explicit PrecompilePreambleAction(ASTUnit &Unit) : Unit(Unit) {}
829 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
832 std::string OutputFile;
834 if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot,
839 if (!CI.getFrontendOpts().RelocatablePCH)
842 CI.getPreprocessor().addPPCallbacks(
843 new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
844 return new PrecompilePreambleConsumer(Unit, CI.getPreprocessor(), Sysroot,
848 virtual bool hasCodeCompletionSupport() const { return false; }
849 virtual bool hasASTFileSupport() const { return false; }
850 virtual TranslationUnitKind getTranslationUnitKind() { return TU_Prefix; }
855 /// Parse the source file into a translation unit using the given compiler
856 /// invocation, replacing the current translation unit.
858 /// \returns True if a failure occurred that causes the ASTUnit not to
859 /// contain any translation-unit information, false otherwise.
860 bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) {
861 delete SavedMainFileBuffer;
862 SavedMainFileBuffer = 0;
865 delete OverrideMainBuffer;
869 // Create the compiler instance to use for building the AST.
870 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
872 // Recover resources if we crash before exiting this method.
873 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
874 CICleanup(Clang.get());
876 llvm::IntrusiveRefCntPtr<CompilerInvocation>
877 CCInvocation(new CompilerInvocation(*Invocation));
879 Clang->setInvocation(CCInvocation.getPtr());
880 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
882 // Set up diagnostics, capturing any diagnostics that would
883 // otherwise be dropped.
884 Clang->setDiagnostics(&getDiagnostics());
886 // Create the target instance.
887 Clang->getTargetOpts().Features = TargetFeatures;
888 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
889 Clang->getTargetOpts()));
890 if (!Clang->hasTarget()) {
891 delete OverrideMainBuffer;
895 // Inform the target of the language options.
897 // FIXME: We shouldn't need to do this, the target should be immutable once
898 // created. This complexity should be lifted elsewhere.
899 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
901 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
902 "Invocation must have exactly one source file!");
903 assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
904 "FIXME: AST inputs not yet supported here!");
905 assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
906 "IR inputs not support here!");
908 // Configure the various subsystems.
909 // FIXME: Should we retain the previous file manager?
910 FileSystemOpts = Clang->getFileSystemOpts();
911 FileMgr = new FileManager(FileSystemOpts);
912 SourceMgr = new SourceManager(getDiagnostics(), *FileMgr);
917 // Clear out old caches and data.
918 TopLevelDecls.clear();
919 CleanTemporaryFiles();
921 if (!OverrideMainBuffer) {
922 StoredDiagnostics.erase(
923 StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver,
924 StoredDiagnostics.end());
925 TopLevelDeclsInPreamble.clear();
928 // Create a file manager object to provide access to and cache the filesystem.
929 Clang->setFileManager(&getFileManager());
931 // Create the source manager.
932 Clang->setSourceManager(&getSourceManager());
934 // If the main file has been overridden due to the use of a preamble,
935 // make that override happen and introduce the preamble.
936 PreprocessorOptions &PreprocessorOpts = Clang->getPreprocessorOpts();
937 PreprocessorOpts.DetailedRecordIncludesNestedMacroExpansions
938 = NestedMacroExpansions;
939 if (OverrideMainBuffer) {
940 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
941 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
942 PreprocessorOpts.PrecompiledPreambleBytes.second
943 = PreambleEndsAtStartOfLine;
944 PreprocessorOpts.ImplicitPCHInclude = PreambleFile;
945 PreprocessorOpts.DisablePCHValidation = true;
947 // The stored diagnostic has the old source manager in it; update
948 // the locations to refer into the new source manager. Since we've
949 // been careful to make sure that the source manager's state
950 // before and after are identical, so that we can reuse the source
952 for (unsigned I = NumStoredDiagnosticsFromDriver,
953 N = StoredDiagnostics.size();
955 FullSourceLoc Loc(StoredDiagnostics[I].getLocation(),
957 StoredDiagnostics[I].setLocation(Loc);
960 // Keep track of the override buffer;
961 SavedMainFileBuffer = OverrideMainBuffer;
964 llvm::OwningPtr<TopLevelDeclTrackerAction> Act(
965 new TopLevelDeclTrackerAction(*this));
967 // Recover resources if we crash before exiting this method.
968 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
969 ActCleanup(Act.get());
971 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
972 Clang->getFrontendOpts().Inputs[0].first))
975 if (OverrideMainBuffer) {
976 std::string ModName = PreambleFile;
977 TranslateStoredDiagnostics(Clang->getModuleManager(), ModName,
978 getSourceManager(), PreambleDiagnostics,
984 // Steal the created target, context, and preprocessor.
985 TheSema.reset(Clang->takeSema());
986 Consumer.reset(Clang->takeASTConsumer());
987 Ctx = &Clang->getASTContext();
988 PP = &Clang->getPreprocessor();
989 Clang->setSourceManager(0);
990 Clang->setFileManager(0);
991 Target = &Clang->getTarget();
993 Act->EndSourceFile();
998 // Remove the overridden buffer we used for the preamble.
999 if (OverrideMainBuffer) {
1000 delete OverrideMainBuffer;
1001 SavedMainFileBuffer = 0;
1004 StoredDiagnostics.clear();
1008 /// \brief Simple function to retrieve a path for a preamble precompiled header.
1009 static std::string GetPreamblePCHPath() {
1010 // FIXME: This is lame; sys::Path should provide this function (in particular,
1011 // it should know how to find the temporary files dir).
1012 // FIXME: This is really lame. I copied this code from the Driver!
1013 // FIXME: This is a hack so that we can override the preamble file during
1014 // crash-recovery testing, which is the only case where the preamble files
1015 // are not necessarily cleaned up.
1016 const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
1021 const char *TmpDir = ::getenv("TMPDIR");
1023 TmpDir = ::getenv("TEMP");
1025 TmpDir = ::getenv("TMP");
1026 #ifdef LLVM_ON_WIN32
1028 TmpDir = ::getenv("USERPROFILE");
1032 llvm::sys::Path P(TmpDir);
1033 P.createDirectoryOnDisk(true);
1034 P.appendComponent("preamble");
1035 P.appendSuffix("pch");
1036 if (P.makeUnique(/*reuse_current=*/false, /*ErrMsg*/0))
1037 return std::string();
1042 /// \brief Compute the preamble for the main file, providing the source buffer
1043 /// that corresponds to the main file along with a pair (bytes, start-of-line)
1044 /// that describes the preamble.
1045 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
1046 ASTUnit::ComputePreamble(CompilerInvocation &Invocation,
1047 unsigned MaxLines, bool &CreatedBuffer) {
1048 FrontendOptions &FrontendOpts = Invocation.getFrontendOpts();
1049 PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts();
1050 CreatedBuffer = false;
1052 // Try to determine if the main file has been remapped, either from the
1053 // command line (to another file) or directly through the compiler invocation
1054 // (to a memory buffer).
1055 llvm::MemoryBuffer *Buffer = 0;
1056 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second);
1057 if (const llvm::sys::FileStatus *MainFileStatus = MainFilePath.getFileStatus()) {
1058 // Check whether there is a file-file remapping of the main file
1059 for (PreprocessorOptions::remapped_file_iterator
1060 M = PreprocessorOpts.remapped_file_begin(),
1061 E = PreprocessorOpts.remapped_file_end();
1064 llvm::sys::PathWithStatus MPath(M->first);
1065 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1066 if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1067 // We found a remapping. Try to load the resulting, remapped source.
1068 if (CreatedBuffer) {
1070 CreatedBuffer = false;
1073 Buffer = getBufferForFile(M->second);
1075 return std::make_pair((llvm::MemoryBuffer*)0,
1076 std::make_pair(0, true));
1077 CreatedBuffer = true;
1082 // Check whether there is a file-buffer remapping. It supercedes the
1083 // file-file remapping.
1084 for (PreprocessorOptions::remapped_file_buffer_iterator
1085 M = PreprocessorOpts.remapped_file_buffer_begin(),
1086 E = PreprocessorOpts.remapped_file_buffer_end();
1089 llvm::sys::PathWithStatus MPath(M->first);
1090 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1091 if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1092 // We found a remapping.
1093 if (CreatedBuffer) {
1095 CreatedBuffer = false;
1098 Buffer = const_cast<llvm::MemoryBuffer *>(M->second);
1104 // If the main source file was not remapped, load it now.
1106 Buffer = getBufferForFile(FrontendOpts.Inputs[0].second);
1108 return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true));
1110 CreatedBuffer = true;
1113 return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer,
1114 Invocation.getLangOpts(),
1118 static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old,
1120 StringRef NewName) {
1121 llvm::MemoryBuffer *Result
1122 = llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName);
1123 memcpy(const_cast<char*>(Result->getBufferStart()),
1124 Old->getBufferStart(), Old->getBufferSize());
1125 memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(),
1126 ' ', NewSize - Old->getBufferSize() - 1);
1127 const_cast<char*>(Result->getBufferEnd())[-1] = '\n';
1132 /// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing
1133 /// the source file.
1135 /// This routine will compute the preamble of the main source file. If a
1136 /// non-trivial preamble is found, it will precompile that preamble into a
1137 /// precompiled header so that the precompiled preamble can be used to reduce
1138 /// reparsing time. If a precompiled preamble has already been constructed,
1139 /// this routine will determine if it is still valid and, if so, avoid
1140 /// rebuilding the precompiled preamble.
1142 /// \param AllowRebuild When true (the default), this routine is
1143 /// allowed to rebuild the precompiled preamble if it is found to be
1146 /// \param MaxLines When non-zero, the maximum number of lines that
1147 /// can occur within the preamble.
1149 /// \returns If the precompiled preamble can be used, returns a newly-allocated
1150 /// buffer that should be used in place of the main file when doing so.
1151 /// Otherwise, returns a NULL pointer.
1152 llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
1153 const CompilerInvocation &PreambleInvocationIn,
1155 unsigned MaxLines) {
1157 llvm::IntrusiveRefCntPtr<CompilerInvocation>
1158 PreambleInvocation(new CompilerInvocation(PreambleInvocationIn));
1159 FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts();
1160 PreprocessorOptions &PreprocessorOpts
1161 = PreambleInvocation->getPreprocessorOpts();
1163 bool CreatedPreambleBuffer = false;
1164 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble
1165 = ComputePreamble(*PreambleInvocation, MaxLines, CreatedPreambleBuffer);
1167 // If ComputePreamble() Take ownership of the preamble buffer.
1168 llvm::OwningPtr<llvm::MemoryBuffer> OwnedPreambleBuffer;
1169 if (CreatedPreambleBuffer)
1170 OwnedPreambleBuffer.reset(NewPreamble.first);
1172 if (!NewPreamble.second.first) {
1173 // We couldn't find a preamble in the main source. Clear out the current
1174 // preamble, if we have one. It's obviously no good any more.
1176 if (!PreambleFile.empty()) {
1177 llvm::sys::Path(PreambleFile).eraseFromDisk();
1178 PreambleFile.clear();
1181 // The next time we actually see a preamble, precompile it.
1182 PreambleRebuildCounter = 1;
1186 if (!Preamble.empty()) {
1187 // We've previously computed a preamble. Check whether we have the same
1188 // preamble now that we did before, and that there's enough space in
1189 // the main-file buffer within the precompiled preamble to fit the
1191 if (Preamble.size() == NewPreamble.second.first &&
1192 PreambleEndsAtStartOfLine == NewPreamble.second.second &&
1193 NewPreamble.first->getBufferSize() < PreambleReservedSize-2 &&
1194 memcmp(Preamble.getBufferStart(), NewPreamble.first->getBufferStart(),
1195 NewPreamble.second.first) == 0) {
1196 // The preamble has not changed. We may be able to re-use the precompiled
1199 // Check that none of the files used by the preamble have changed.
1200 bool AnyFileChanged = false;
1202 // First, make a record of those files that have been overridden via
1203 // remapping or unsaved_files.
1204 llvm::StringMap<std::pair<off_t, time_t> > OverriddenFiles;
1205 for (PreprocessorOptions::remapped_file_iterator
1206 R = PreprocessorOpts.remapped_file_begin(),
1207 REnd = PreprocessorOpts.remapped_file_end();
1208 !AnyFileChanged && R != REnd;
1210 struct stat StatBuf;
1211 if (FileMgr->getNoncachedStatValue(R->second, StatBuf)) {
1212 // If we can't stat the file we're remapping to, assume that something
1213 // horrible happened.
1214 AnyFileChanged = true;
1218 OverriddenFiles[R->first] = std::make_pair(StatBuf.st_size,
1221 for (PreprocessorOptions::remapped_file_buffer_iterator
1222 R = PreprocessorOpts.remapped_file_buffer_begin(),
1223 REnd = PreprocessorOpts.remapped_file_buffer_end();
1224 !AnyFileChanged && R != REnd;
1226 // FIXME: Should we actually compare the contents of file->buffer
1228 OverriddenFiles[R->first] = std::make_pair(R->second->getBufferSize(),
1232 // Check whether anything has changed.
1233 for (llvm::StringMap<std::pair<off_t, time_t> >::iterator
1234 F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end();
1235 !AnyFileChanged && F != FEnd;
1237 llvm::StringMap<std::pair<off_t, time_t> >::iterator Overridden
1238 = OverriddenFiles.find(F->first());
1239 if (Overridden != OverriddenFiles.end()) {
1240 // This file was remapped; check whether the newly-mapped file
1241 // matches up with the previous mapping.
1242 if (Overridden->second != F->second)
1243 AnyFileChanged = true;
1247 // The file was not remapped; check whether it has changed on disk.
1248 struct stat StatBuf;
1249 if (FileMgr->getNoncachedStatValue(F->first(), StatBuf)) {
1250 // If we can't stat the file, assume that something horrible happened.
1251 AnyFileChanged = true;
1252 } else if (StatBuf.st_size != F->second.first ||
1253 StatBuf.st_mtime != F->second.second)
1254 AnyFileChanged = true;
1257 if (!AnyFileChanged) {
1258 // Okay! We can re-use the precompiled preamble.
1260 // Set the state of the diagnostic object to mimic its state
1261 // after parsing the preamble.
1262 // FIXME: This won't catch any #pragma push warning changes that
1263 // have occurred in the preamble.
1264 getDiagnostics().Reset();
1265 ProcessWarningOptions(getDiagnostics(),
1266 PreambleInvocation->getDiagnosticOpts());
1267 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1269 // Create a version of the main file buffer that is padded to
1270 // buffer size we reserved when creating the preamble.
1271 return CreatePaddedMainFileBuffer(NewPreamble.first,
1272 PreambleReservedSize,
1273 FrontendOpts.Inputs[0].second);
1277 // If we aren't allowed to rebuild the precompiled preamble, just
1282 // We can't reuse the previously-computed preamble. Build a new one.
1284 PreambleDiagnostics.clear();
1285 llvm::sys::Path(PreambleFile).eraseFromDisk();
1286 PreambleRebuildCounter = 1;
1287 } else if (!AllowRebuild) {
1288 // We aren't allowed to rebuild the precompiled preamble; just
1293 // If the preamble rebuild counter > 1, it's because we previously
1294 // failed to build a preamble and we're not yet ready to try
1295 // again. Decrement the counter and return a failure.
1296 if (PreambleRebuildCounter > 1) {
1297 --PreambleRebuildCounter;
1301 // Create a temporary file for the precompiled preamble. In rare
1302 // circumstances, this can fail.
1303 std::string PreamblePCHPath = GetPreamblePCHPath();
1304 if (PreamblePCHPath.empty()) {
1305 // Try again next time.
1306 PreambleRebuildCounter = 1;
1310 // We did not previously compute a preamble, or it can't be reused anyway.
1311 SimpleTimer PreambleTimer(WantTiming);
1312 PreambleTimer.setOutput("Precompiling preamble");
1314 // Create a new buffer that stores the preamble. The buffer also contains
1315 // extra space for the original contents of the file (which will be present
1316 // when we actually parse the file) along with more room in case the file
1318 PreambleReservedSize = NewPreamble.first->getBufferSize();
1319 if (PreambleReservedSize < 4096)
1320 PreambleReservedSize = 8191;
1322 PreambleReservedSize *= 2;
1324 // Save the preamble text for later; we'll need to compare against it for
1325 // subsequent reparses.
1326 StringRef MainFilename = PreambleInvocation->getFrontendOpts().Inputs[0].second;
1327 Preamble.assign(FileMgr->getFile(MainFilename),
1328 NewPreamble.first->getBufferStart(),
1329 NewPreamble.first->getBufferStart()
1330 + NewPreamble.second.first);
1331 PreambleEndsAtStartOfLine = NewPreamble.second.second;
1333 delete PreambleBuffer;
1335 = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize,
1336 FrontendOpts.Inputs[0].second);
1337 memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()),
1338 NewPreamble.first->getBufferStart(), Preamble.size());
1339 memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(),
1340 ' ', PreambleReservedSize - Preamble.size() - 1);
1341 const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n';
1343 // Remap the main source file to the preamble buffer.
1344 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second);
1345 PreprocessorOpts.addRemappedFile(MainFilePath.str(), PreambleBuffer);
1347 // Tell the compiler invocation to generate a temporary precompiled header.
1348 FrontendOpts.ProgramAction = frontend::GeneratePCH;
1349 // FIXME: Generate the precompiled header into memory?
1350 FrontendOpts.OutputFile = PreamblePCHPath;
1351 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
1352 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
1354 // Create the compiler instance to use for building the precompiled preamble.
1355 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1357 // Recover resources if we crash before exiting this method.
1358 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1359 CICleanup(Clang.get());
1361 Clang->setInvocation(&*PreambleInvocation);
1362 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
1364 // Set up diagnostics, capturing all of the diagnostics produced.
1365 Clang->setDiagnostics(&getDiagnostics());
1367 // Create the target instance.
1368 Clang->getTargetOpts().Features = TargetFeatures;
1369 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1370 Clang->getTargetOpts()));
1371 if (!Clang->hasTarget()) {
1372 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1374 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1375 PreprocessorOpts.eraseRemappedFile(
1376 PreprocessorOpts.remapped_file_buffer_end() - 1);
1380 // Inform the target of the language options.
1382 // FIXME: We shouldn't need to do this, the target should be immutable once
1383 // created. This complexity should be lifted elsewhere.
1384 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1386 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1387 "Invocation must have exactly one source file!");
1388 assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
1389 "FIXME: AST inputs not yet supported here!");
1390 assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
1391 "IR inputs not support here!");
1393 // Clear out old caches and data.
1394 getDiagnostics().Reset();
1395 ProcessWarningOptions(getDiagnostics(), Clang->getDiagnosticOpts());
1396 StoredDiagnostics.erase(
1397 StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver,
1398 StoredDiagnostics.end());
1399 TopLevelDecls.clear();
1400 TopLevelDeclsInPreamble.clear();
1402 // Create a file manager object to provide access to and cache the filesystem.
1403 Clang->setFileManager(new FileManager(Clang->getFileSystemOpts()));
1405 // Create the source manager.
1406 Clang->setSourceManager(new SourceManager(getDiagnostics(),
1407 Clang->getFileManager()));
1409 llvm::OwningPtr<PrecompilePreambleAction> Act;
1410 Act.reset(new PrecompilePreambleAction(*this));
1411 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
1412 Clang->getFrontendOpts().Inputs[0].first)) {
1413 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1415 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1416 PreprocessorOpts.eraseRemappedFile(
1417 PreprocessorOpts.remapped_file_buffer_end() - 1);
1422 Act->EndSourceFile();
1424 if (Diagnostics->hasErrorOccurred()) {
1425 // There were errors parsing the preamble, so no precompiled header was
1426 // generated. Forget that we even tried.
1427 // FIXME: Should we leave a note for ourselves to try again?
1428 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1430 TopLevelDeclsInPreamble.clear();
1431 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1432 PreprocessorOpts.eraseRemappedFile(
1433 PreprocessorOpts.remapped_file_buffer_end() - 1);
1437 // Transfer any diagnostics generated when parsing the preamble into the set
1438 // of preamble diagnostics.
1439 PreambleDiagnostics.clear();
1440 PreambleDiagnostics.insert(PreambleDiagnostics.end(),
1441 StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver,
1442 StoredDiagnostics.end());
1443 StoredDiagnostics.erase(
1444 StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver,
1445 StoredDiagnostics.end());
1447 // Keep track of the preamble we precompiled.
1448 PreambleFile = FrontendOpts.OutputFile;
1449 NumWarningsInPreamble = getDiagnostics().getNumWarnings();
1451 // Keep track of all of the files that the source manager knows about,
1452 // so we can verify whether they have changed or not.
1453 FilesInPreamble.clear();
1454 SourceManager &SourceMgr = Clang->getSourceManager();
1455 const llvm::MemoryBuffer *MainFileBuffer
1456 = SourceMgr.getBuffer(SourceMgr.getMainFileID());
1457 for (SourceManager::fileinfo_iterator F = SourceMgr.fileinfo_begin(),
1458 FEnd = SourceMgr.fileinfo_end();
1461 const FileEntry *File = F->second->OrigEntry;
1462 if (!File || F->second->getRawBuffer() == MainFileBuffer)
1465 FilesInPreamble[File->getName()]
1466 = std::make_pair(F->second->getSize(), File->getModificationTime());
1469 PreambleRebuildCounter = 1;
1470 PreprocessorOpts.eraseRemappedFile(
1471 PreprocessorOpts.remapped_file_buffer_end() - 1);
1473 // If the hash of top-level entities differs from the hash of the top-level
1474 // entities the last time we rebuilt the preamble, clear out the completion
1476 if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1477 CompletionCacheTopLevelHashValue = 0;
1478 PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1481 return CreatePaddedMainFileBuffer(NewPreamble.first,
1482 PreambleReservedSize,
1483 FrontendOpts.Inputs[0].second);
1486 void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1487 std::vector<Decl *> Resolved;
1488 Resolved.reserve(TopLevelDeclsInPreamble.size());
1489 ExternalASTSource &Source = *getASTContext().getExternalSource();
1490 for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) {
1491 // Resolve the declaration ID to an actual declaration, possibly
1492 // deserializing the declaration in the process.
1493 Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]);
1495 Resolved.push_back(D);
1497 TopLevelDeclsInPreamble.clear();
1498 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1501 StringRef ASTUnit::getMainFileName() const {
1502 return Invocation->getFrontendOpts().Inputs[0].second;
1505 ASTUnit *ASTUnit::create(CompilerInvocation *CI,
1506 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags) {
1507 llvm::OwningPtr<ASTUnit> AST;
1508 AST.reset(new ASTUnit(false));
1509 ConfigureDiags(Diags, 0, 0, *AST, /*CaptureDiagnostics=*/false);
1510 AST->Diagnostics = Diags;
1511 AST->Invocation = CI;
1512 AST->FileSystemOpts = CI->getFileSystemOpts();
1513 AST->FileMgr = new FileManager(AST->FileSystemOpts);
1514 AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr);
1519 ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(CompilerInvocation *CI,
1520 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1521 ASTFrontendAction *Action,
1523 assert(CI && "A CompilerInvocation is required");
1525 llvm::OwningPtr<ASTUnit> OwnAST;
1526 ASTUnit *AST = Unit;
1528 // Create the AST unit.
1529 OwnAST.reset(create(CI, Diags));
1533 AST->OnlyLocalDecls = false;
1534 AST->CaptureDiagnostics = false;
1535 AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete;
1536 AST->ShouldCacheCodeCompletionResults = false;
1538 // Recover resources if we crash before exiting this method.
1539 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1540 ASTUnitCleanup(OwnAST.get());
1541 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1542 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
1543 DiagCleanup(Diags.getPtr());
1545 // We'll manage file buffers ourselves.
1546 CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1547 CI->getFrontendOpts().DisableFree = false;
1548 ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
1550 // Save the target features.
1551 AST->TargetFeatures = CI->getTargetOpts().Features;
1553 // Create the compiler instance to use for building the AST.
1554 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1556 // Recover resources if we crash before exiting this method.
1557 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1558 CICleanup(Clang.get());
1560 Clang->setInvocation(CI);
1561 AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
1563 // Set up diagnostics, capturing any diagnostics that would
1564 // otherwise be dropped.
1565 Clang->setDiagnostics(&AST->getDiagnostics());
1567 // Create the target instance.
1568 Clang->getTargetOpts().Features = AST->TargetFeatures;
1569 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1570 Clang->getTargetOpts()));
1571 if (!Clang->hasTarget())
1574 // Inform the target of the language options.
1576 // FIXME: We shouldn't need to do this, the target should be immutable once
1577 // created. This complexity should be lifted elsewhere.
1578 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1580 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1581 "Invocation must have exactly one source file!");
1582 assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
1583 "FIXME: AST inputs not yet supported here!");
1584 assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
1585 "IR inputs not supported here!");
1587 // Configure the various subsystems.
1588 AST->TheSema.reset();
1592 // Create a file manager object to provide access to and cache the filesystem.
1593 Clang->setFileManager(&AST->getFileManager());
1595 // Create the source manager.
1596 Clang->setSourceManager(&AST->getSourceManager());
1598 ASTFrontendAction *Act = Action;
1600 llvm::OwningPtr<TopLevelDeclTrackerAction> TrackerAct;
1602 TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
1603 Act = TrackerAct.get();
1606 // Recover resources if we crash before exiting this method.
1607 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1608 ActCleanup(TrackerAct.get());
1610 if (!Act->BeginSourceFile(*Clang.get(),
1611 Clang->getFrontendOpts().Inputs[0].second,
1612 Clang->getFrontendOpts().Inputs[0].first))
1617 // Steal the created target, context, and preprocessor.
1618 AST->TheSema.reset(Clang->takeSema());
1619 AST->Consumer.reset(Clang->takeASTConsumer());
1620 AST->Ctx = &Clang->getASTContext();
1621 AST->PP = &Clang->getPreprocessor();
1622 Clang->setSourceManager(0);
1623 Clang->setFileManager(0);
1624 AST->Target = &Clang->getTarget();
1626 Act->EndSourceFile();
1629 return OwnAST.take();
1634 bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) {
1638 // We'll manage file buffers ourselves.
1639 Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1640 Invocation->getFrontendOpts().DisableFree = false;
1641 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1643 // Save the target features.
1644 TargetFeatures = Invocation->getTargetOpts().Features;
1646 llvm::MemoryBuffer *OverrideMainBuffer = 0;
1647 if (PrecompilePreamble) {
1648 PreambleRebuildCounter = 2;
1650 = getMainBufferWithPrecompiledPreamble(*Invocation);
1653 SimpleTimer ParsingTimer(WantTiming);
1654 ParsingTimer.setOutput("Parsing " + getMainFileName());
1656 // Recover resources if we crash before exiting this method.
1657 llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1658 MemBufferCleanup(OverrideMainBuffer);
1660 return Parse(OverrideMainBuffer);
1663 ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
1664 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1665 bool OnlyLocalDecls,
1666 bool CaptureDiagnostics,
1667 bool PrecompilePreamble,
1668 TranslationUnitKind TUKind,
1669 bool CacheCodeCompletionResults,
1670 bool NestedMacroExpansions) {
1671 // Create the AST unit.
1672 llvm::OwningPtr<ASTUnit> AST;
1673 AST.reset(new ASTUnit(false));
1674 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
1675 AST->Diagnostics = Diags;
1676 AST->OnlyLocalDecls = OnlyLocalDecls;
1677 AST->CaptureDiagnostics = CaptureDiagnostics;
1678 AST->TUKind = TUKind;
1679 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1680 AST->Invocation = CI;
1681 AST->NestedMacroExpansions = NestedMacroExpansions;
1683 // Recover resources if we crash before exiting this method.
1684 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1685 ASTUnitCleanup(AST.get());
1686 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1687 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
1688 DiagCleanup(Diags.getPtr());
1690 return AST->LoadFromCompilerInvocation(PrecompilePreamble)? 0 : AST.take();
1693 ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
1694 const char **ArgEnd,
1695 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1696 StringRef ResourceFilesPath,
1697 bool OnlyLocalDecls,
1698 bool CaptureDiagnostics,
1699 RemappedFile *RemappedFiles,
1700 unsigned NumRemappedFiles,
1701 bool RemappedFilesKeepOriginalName,
1702 bool PrecompilePreamble,
1703 TranslationUnitKind TUKind,
1704 bool CacheCodeCompletionResults,
1705 bool NestedMacroExpansions) {
1706 if (!Diags.getPtr()) {
1707 // No diagnostics engine was provided, so create our own diagnostics object
1708 // with the default options.
1709 DiagnosticOptions DiagOpts;
1710 Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd - ArgBegin,
1714 SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1716 llvm::IntrusiveRefCntPtr<CompilerInvocation> CI;
1720 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
1723 CI = clang::createInvocationFromCommandLine(
1724 llvm::makeArrayRef(ArgBegin, ArgEnd),
1730 // Override any files that need remapping
1731 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
1732 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
1733 if (const llvm::MemoryBuffer *
1734 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
1735 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, memBuf);
1737 const char *fname = fileOrBuf.get<const char *>();
1738 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, fname);
1741 CI->getPreprocessorOpts().RemappedFilesKeepOriginalName =
1742 RemappedFilesKeepOriginalName;
1744 // Override the resources path.
1745 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1747 // Create the AST unit.
1748 llvm::OwningPtr<ASTUnit> AST;
1749 AST.reset(new ASTUnit(false));
1750 ConfigureDiags(Diags, ArgBegin, ArgEnd, *AST, CaptureDiagnostics);
1751 AST->Diagnostics = Diags;
1753 AST->FileSystemOpts = CI->getFileSystemOpts();
1754 AST->FileMgr = new FileManager(AST->FileSystemOpts);
1755 AST->OnlyLocalDecls = OnlyLocalDecls;
1756 AST->CaptureDiagnostics = CaptureDiagnostics;
1757 AST->TUKind = TUKind;
1758 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1759 AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1760 AST->StoredDiagnostics.swap(StoredDiagnostics);
1761 AST->Invocation = CI;
1762 AST->NestedMacroExpansions = NestedMacroExpansions;
1764 // Recover resources if we crash before exiting this method.
1765 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1766 ASTUnitCleanup(AST.get());
1767 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation,
1768 llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> >
1769 CICleanup(CI.getPtr());
1770 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1771 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
1772 DiagCleanup(Diags.getPtr());
1774 return AST->LoadFromCompilerInvocation(PrecompilePreamble) ? 0 : AST.take();
1777 bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) {
1781 SimpleTimer ParsingTimer(WantTiming);
1782 ParsingTimer.setOutput("Reparsing " + getMainFileName());
1785 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1786 PPOpts.DisableStatCache = true;
1787 for (PreprocessorOptions::remapped_file_buffer_iterator
1788 R = PPOpts.remapped_file_buffer_begin(),
1789 REnd = PPOpts.remapped_file_buffer_end();
1794 Invocation->getPreprocessorOpts().clearRemappedFiles();
1795 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
1796 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
1797 if (const llvm::MemoryBuffer *
1798 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
1799 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1802 const char *fname = fileOrBuf.get<const char *>();
1803 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1808 // If we have a preamble file lying around, or if we might try to
1809 // build a precompiled preamble, do so now.
1810 llvm::MemoryBuffer *OverrideMainBuffer = 0;
1811 if (!PreambleFile.empty() || PreambleRebuildCounter > 0)
1812 OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation);
1814 // Clear out the diagnostics state.
1815 if (!OverrideMainBuffer) {
1816 getDiagnostics().Reset();
1817 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1820 // Parse the sources
1821 bool Result = Parse(OverrideMainBuffer);
1823 // If we're caching global code-completion results, and the top-level
1824 // declarations have changed, clear out the code-completion cache.
1825 if (!Result && ShouldCacheCodeCompletionResults &&
1826 CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
1827 CacheCodeCompletionResults();
1829 // We now need to clear out the completion allocator for
1830 // clang_getCursorCompletionString; it'll be recreated if necessary.
1831 CursorCompletionAllocator = 0;
1836 //----------------------------------------------------------------------------//
1838 //----------------------------------------------------------------------------//
1841 /// \brief Code completion consumer that combines the cached code-completion
1842 /// results from an ASTUnit with the code-completion results provided to it,
1843 /// then passes the result on to
1844 class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
1845 unsigned long long NormalContexts;
1847 CodeCompleteConsumer &Next;
1850 AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
1851 bool IncludeMacros, bool IncludeCodePatterns,
1852 bool IncludeGlobals)
1853 : CodeCompleteConsumer(IncludeMacros, IncludeCodePatterns, IncludeGlobals,
1854 Next.isOutputBinary()), AST(AST), Next(Next)
1856 // Compute the set of contexts in which we will look when we don't have
1857 // any information about the specific context.
1859 = (1LL << (CodeCompletionContext::CCC_TopLevel - 1))
1860 | (1LL << (CodeCompletionContext::CCC_ObjCInterface - 1))
1861 | (1LL << (CodeCompletionContext::CCC_ObjCImplementation - 1))
1862 | (1LL << (CodeCompletionContext::CCC_ObjCIvarList - 1))
1863 | (1LL << (CodeCompletionContext::CCC_Statement - 1))
1864 | (1LL << (CodeCompletionContext::CCC_Expression - 1))
1865 | (1LL << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
1866 | (1LL << (CodeCompletionContext::CCC_DotMemberAccess - 1))
1867 | (1LL << (CodeCompletionContext::CCC_ArrowMemberAccess - 1))
1868 | (1LL << (CodeCompletionContext::CCC_ObjCPropertyAccess - 1))
1869 | (1LL << (CodeCompletionContext::CCC_ObjCProtocolName - 1))
1870 | (1LL << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
1871 | (1LL << (CodeCompletionContext::CCC_Recovery - 1));
1873 if (AST.getASTContext().getLangOptions().CPlusPlus)
1874 NormalContexts |= (1LL << (CodeCompletionContext::CCC_EnumTag - 1))
1875 | (1LL << (CodeCompletionContext::CCC_UnionTag - 1))
1876 | (1LL << (CodeCompletionContext::CCC_ClassOrStructTag - 1));
1879 virtual void ProcessCodeCompleteResults(Sema &S,
1880 CodeCompletionContext Context,
1881 CodeCompletionResult *Results,
1882 unsigned NumResults);
1884 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
1885 OverloadCandidate *Candidates,
1886 unsigned NumCandidates) {
1887 Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
1890 virtual CodeCompletionAllocator &getAllocator() {
1891 return Next.getAllocator();
1896 /// \brief Helper function that computes which global names are hidden by the
1897 /// local code-completion results.
1898 static void CalculateHiddenNames(const CodeCompletionContext &Context,
1899 CodeCompletionResult *Results,
1900 unsigned NumResults,
1902 llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
1903 bool OnlyTagNames = false;
1904 switch (Context.getKind()) {
1905 case CodeCompletionContext::CCC_Recovery:
1906 case CodeCompletionContext::CCC_TopLevel:
1907 case CodeCompletionContext::CCC_ObjCInterface:
1908 case CodeCompletionContext::CCC_ObjCImplementation:
1909 case CodeCompletionContext::CCC_ObjCIvarList:
1910 case CodeCompletionContext::CCC_ClassStructUnion:
1911 case CodeCompletionContext::CCC_Statement:
1912 case CodeCompletionContext::CCC_Expression:
1913 case CodeCompletionContext::CCC_ObjCMessageReceiver:
1914 case CodeCompletionContext::CCC_DotMemberAccess:
1915 case CodeCompletionContext::CCC_ArrowMemberAccess:
1916 case CodeCompletionContext::CCC_ObjCPropertyAccess:
1917 case CodeCompletionContext::CCC_Namespace:
1918 case CodeCompletionContext::CCC_Type:
1919 case CodeCompletionContext::CCC_Name:
1920 case CodeCompletionContext::CCC_PotentiallyQualifiedName:
1921 case CodeCompletionContext::CCC_ParenthesizedExpression:
1922 case CodeCompletionContext::CCC_ObjCInterfaceName:
1925 case CodeCompletionContext::CCC_EnumTag:
1926 case CodeCompletionContext::CCC_UnionTag:
1927 case CodeCompletionContext::CCC_ClassOrStructTag:
1928 OnlyTagNames = true;
1931 case CodeCompletionContext::CCC_ObjCProtocolName:
1932 case CodeCompletionContext::CCC_MacroName:
1933 case CodeCompletionContext::CCC_MacroNameUse:
1934 case CodeCompletionContext::CCC_PreprocessorExpression:
1935 case CodeCompletionContext::CCC_PreprocessorDirective:
1936 case CodeCompletionContext::CCC_NaturalLanguage:
1937 case CodeCompletionContext::CCC_SelectorName:
1938 case CodeCompletionContext::CCC_TypeQualifiers:
1939 case CodeCompletionContext::CCC_Other:
1940 case CodeCompletionContext::CCC_OtherWithMacros:
1941 case CodeCompletionContext::CCC_ObjCInstanceMessage:
1942 case CodeCompletionContext::CCC_ObjCClassMessage:
1943 case CodeCompletionContext::CCC_ObjCCategoryName:
1944 // We're looking for nothing, or we're looking for names that cannot
1949 typedef CodeCompletionResult Result;
1950 for (unsigned I = 0; I != NumResults; ++I) {
1951 if (Results[I].Kind != Result::RK_Declaration)
1955 = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
1957 bool Hiding = false;
1959 Hiding = (IDNS & Decl::IDNS_Tag);
1961 unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
1962 Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
1963 Decl::IDNS_NonMemberOperator);
1964 if (Ctx.getLangOptions().CPlusPlus)
1965 HiddenIDNS |= Decl::IDNS_Tag;
1966 Hiding = (IDNS & HiddenIDNS);
1972 DeclarationName Name = Results[I].Declaration->getDeclName();
1973 if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
1974 HiddenNames.insert(Identifier->getName());
1976 HiddenNames.insert(Name.getAsString());
1981 void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
1982 CodeCompletionContext Context,
1983 CodeCompletionResult *Results,
1984 unsigned NumResults) {
1985 // Merge the results we were given with the results we cached.
1986 bool AddedResult = false;
1988 = (Context.getKind() == CodeCompletionContext::CCC_Recovery? NormalContexts
1989 : (1ULL << (Context.getKind() - 1)));
1990 // Contains the set of names that are hidden by "local" completion results.
1991 llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
1992 typedef CodeCompletionResult Result;
1993 SmallVector<Result, 8> AllResults;
1994 for (ASTUnit::cached_completion_iterator
1995 C = AST.cached_completion_begin(),
1996 CEnd = AST.cached_completion_end();
1998 // If the context we are in matches any of the contexts we are
1999 // interested in, we'll add this result.
2000 if ((C->ShowInContexts & InContexts) == 0)
2003 // If we haven't added any results previously, do so now.
2005 CalculateHiddenNames(Context, Results, NumResults, S.Context,
2007 AllResults.insert(AllResults.end(), Results, Results + NumResults);
2011 // Determine whether this global completion result is hidden by a local
2012 // completion result. If so, skip it.
2013 if (C->Kind != CXCursor_MacroDefinition &&
2014 HiddenNames.count(C->Completion->getTypedText()))
2017 // Adjust priority based on similar type classes.
2018 unsigned Priority = C->Priority;
2019 CXCursorKind CursorKind = C->Kind;
2020 CodeCompletionString *Completion = C->Completion;
2021 if (!Context.getPreferredType().isNull()) {
2022 if (C->Kind == CXCursor_MacroDefinition) {
2023 Priority = getMacroUsagePriority(C->Completion->getTypedText(),
2025 Context.getPreferredType()->isAnyPointerType());
2026 } else if (C->Type) {
2027 CanQualType Expected
2028 = S.Context.getCanonicalType(
2029 Context.getPreferredType().getUnqualifiedType());
2030 SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
2031 if (ExpectedSTC == C->TypeClass) {
2032 // We know this type is similar; check for an exact match.
2033 llvm::StringMap<unsigned> &CachedCompletionTypes
2034 = AST.getCachedCompletionTypes();
2035 llvm::StringMap<unsigned>::iterator Pos
2036 = CachedCompletionTypes.find(QualType(Expected).getAsString());
2037 if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
2038 Priority /= CCF_ExactTypeMatch;
2040 Priority /= CCF_SimilarTypeMatch;
2045 // Adjust the completion string, if required.
2046 if (C->Kind == CXCursor_MacroDefinition &&
2047 Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
2048 // Create a new code-completion string that just contains the
2049 // macro name, without its arguments.
2050 CodeCompletionBuilder Builder(getAllocator(), CCP_CodePattern,
2052 Builder.AddTypedTextChunk(C->Completion->getTypedText());
2053 CursorKind = CXCursor_NotImplemented;
2054 Priority = CCP_CodePattern;
2055 Completion = Builder.TakeString();
2058 AllResults.push_back(Result(Completion, Priority, CursorKind,
2062 // If we did not add any cached completion results, just forward the
2063 // results we were given to the next consumer.
2065 Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
2069 Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
2075 void ASTUnit::CodeComplete(StringRef File, unsigned Line, unsigned Column,
2076 RemappedFile *RemappedFiles,
2077 unsigned NumRemappedFiles,
2079 bool IncludeCodePatterns,
2080 CodeCompleteConsumer &Consumer,
2081 DiagnosticsEngine &Diag, LangOptions &LangOpts,
2082 SourceManager &SourceMgr, FileManager &FileMgr,
2083 SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2084 SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
2088 SimpleTimer CompletionTimer(WantTiming);
2089 CompletionTimer.setOutput("Code completion @ " + File + ":" +
2090 Twine(Line) + ":" + Twine(Column));
2092 llvm::IntrusiveRefCntPtr<CompilerInvocation>
2093 CCInvocation(new CompilerInvocation(*Invocation));
2095 FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2096 PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2098 FrontendOpts.ShowMacrosInCodeCompletion
2099 = IncludeMacros && CachedCompletionResults.empty();
2100 FrontendOpts.ShowCodePatternsInCodeCompletion = IncludeCodePatterns;
2101 FrontendOpts.ShowGlobalSymbolsInCodeCompletion
2102 = CachedCompletionResults.empty();
2103 FrontendOpts.CodeCompletionAt.FileName = File;
2104 FrontendOpts.CodeCompletionAt.Line = Line;
2105 FrontendOpts.CodeCompletionAt.Column = Column;
2107 // Set the language options appropriately.
2108 LangOpts = CCInvocation->getLangOpts();
2110 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
2112 // Recover resources if we crash before exiting this method.
2113 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2114 CICleanup(Clang.get());
2116 Clang->setInvocation(&*CCInvocation);
2117 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
2119 // Set up diagnostics, capturing any diagnostics produced.
2120 Clang->setDiagnostics(&Diag);
2121 ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts());
2122 CaptureDroppedDiagnostics Capture(true,
2123 Clang->getDiagnostics(),
2126 // Create the target instance.
2127 Clang->getTargetOpts().Features = TargetFeatures;
2128 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
2129 Clang->getTargetOpts()));
2130 if (!Clang->hasTarget()) {
2131 Clang->setInvocation(0);
2135 // Inform the target of the language options.
2137 // FIXME: We shouldn't need to do this, the target should be immutable once
2138 // created. This complexity should be lifted elsewhere.
2139 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
2141 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2142 "Invocation must have exactly one source file!");
2143 assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
2144 "FIXME: AST inputs not yet supported here!");
2145 assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
2146 "IR inputs not support here!");
2149 // Use the source and file managers that we were given.
2150 Clang->setFileManager(&FileMgr);
2151 Clang->setSourceManager(&SourceMgr);
2154 PreprocessorOpts.clearRemappedFiles();
2155 PreprocessorOpts.RetainRemappedFileBuffers = true;
2156 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
2157 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
2158 if (const llvm::MemoryBuffer *
2159 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
2160 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, memBuf);
2161 OwnedBuffers.push_back(memBuf);
2163 const char *fname = fileOrBuf.get<const char *>();
2164 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, fname);
2168 // Use the code completion consumer we were given, but adding any cached
2169 // code-completion results.
2170 AugmentedCodeCompleteConsumer *AugmentedConsumer
2171 = new AugmentedCodeCompleteConsumer(*this, Consumer,
2172 FrontendOpts.ShowMacrosInCodeCompletion,
2173 FrontendOpts.ShowCodePatternsInCodeCompletion,
2174 FrontendOpts.ShowGlobalSymbolsInCodeCompletion);
2175 Clang->setCodeCompletionConsumer(AugmentedConsumer);
2177 // If we have a precompiled preamble, try to use it. We only allow
2178 // the use of the precompiled preamble if we're if the completion
2179 // point is within the main file, after the end of the precompiled
2181 llvm::MemoryBuffer *OverrideMainBuffer = 0;
2182 if (!PreambleFile.empty()) {
2183 using llvm::sys::FileStatus;
2184 llvm::sys::PathWithStatus CompleteFilePath(File);
2185 llvm::sys::PathWithStatus MainPath(OriginalSourceFile);
2186 if (const FileStatus *CompleteFileStatus = CompleteFilePath.getFileStatus())
2187 if (const FileStatus *MainStatus = MainPath.getFileStatus())
2188 if (CompleteFileStatus->getUniqueID() == MainStatus->getUniqueID() &&
2191 = getMainBufferWithPrecompiledPreamble(*CCInvocation, false,
2195 // If the main file has been overridden due to the use of a preamble,
2196 // make that override happen and introduce the preamble.
2197 PreprocessorOpts.DisableStatCache = true;
2198 StoredDiagnostics.insert(StoredDiagnostics.end(),
2199 this->StoredDiagnostics.begin(),
2200 this->StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver);
2201 if (OverrideMainBuffer) {
2202 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
2203 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
2204 PreprocessorOpts.PrecompiledPreambleBytes.second
2205 = PreambleEndsAtStartOfLine;
2206 PreprocessorOpts.ImplicitPCHInclude = PreambleFile;
2207 PreprocessorOpts.DisablePCHValidation = true;
2209 OwnedBuffers.push_back(OverrideMainBuffer);
2211 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2212 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2215 // Disable the preprocessing record
2216 PreprocessorOpts.DetailedRecord = false;
2218 llvm::OwningPtr<SyntaxOnlyAction> Act;
2219 Act.reset(new SyntaxOnlyAction);
2220 if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
2221 Clang->getFrontendOpts().Inputs[0].first)) {
2222 if (OverrideMainBuffer) {
2223 std::string ModName = PreambleFile;
2224 TranslateStoredDiagnostics(Clang->getModuleManager(), ModName,
2225 getSourceManager(), PreambleDiagnostics,
2229 Act->EndSourceFile();
2233 CXSaveError ASTUnit::Save(StringRef File) {
2234 if (getDiagnostics().hasUnrecoverableErrorOccurred())
2235 return CXSaveError_TranslationErrors;
2237 // Write to a temporary file and later rename it to the actual file, to avoid
2238 // possible race conditions.
2239 llvm::SmallString<128> TempPath;
2241 TempPath += "-%%%%%%%%";
2243 if (llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath,
2244 /*makeAbsolute=*/false))
2245 return CXSaveError_Unknown;
2247 // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2248 // unconditionally create a stat cache when we parse the file?
2249 llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true);
2253 if (Out.has_error())
2254 return CXSaveError_Unknown;
2256 if (llvm::error_code ec = llvm::sys::fs::rename(TempPath.str(), File)) {
2258 llvm::sys::fs::remove(TempPath.str(), exists);
2259 return CXSaveError_Unknown;
2262 return CXSaveError_None;
2265 bool ASTUnit::serialize(raw_ostream &OS) {
2266 if (getDiagnostics().hasErrorOccurred())
2269 std::vector<unsigned char> Buffer;
2270 llvm::BitstreamWriter Stream(Buffer);
2271 ASTWriter Writer(Stream);
2272 // FIXME: Handle modules
2273 Writer.WriteAST(getSema(), 0, std::string(), /*IsModule=*/false, "");
2275 // Write the generated bitstream to "Out".
2276 if (!Buffer.empty())
2277 OS.write((char *)&Buffer.front(), Buffer.size());
2282 typedef ContinuousRangeMap<unsigned, int, 2> SLocRemap;
2284 static void TranslateSLoc(SourceLocation &L, SLocRemap &Remap) {
2285 unsigned Raw = L.getRawEncoding();
2286 const unsigned MacroBit = 1U << 31;
2287 L = SourceLocation::getFromRawEncoding((Raw & MacroBit) |
2288 ((Raw & ~MacroBit) + Remap.find(Raw & ~MacroBit)->second));
2291 void ASTUnit::TranslateStoredDiagnostics(
2294 SourceManager &SrcMgr,
2295 const SmallVectorImpl<StoredDiagnostic> &Diags,
2296 SmallVectorImpl<StoredDiagnostic> &Out) {
2297 // The stored diagnostic has the old source manager in it; update
2298 // the locations to refer into the new source manager. We also need to remap
2299 // all the locations to the new view. This includes the diag location, any
2300 // associated source ranges, and the source ranges of associated fix-its.
2301 // FIXME: There should be a cleaner way to do this.
2303 SmallVector<StoredDiagnostic, 4> Result;
2304 Result.reserve(Diags.size());
2305 assert(MMan && "Don't have a module manager");
2306 serialization::Module *Mod = MMan->ModuleMgr.lookup(ModName);
2307 assert(Mod && "Don't have preamble module");
2308 SLocRemap &Remap = Mod->SLocRemap;
2309 for (unsigned I = 0, N = Diags.size(); I != N; ++I) {
2310 // Rebuild the StoredDiagnostic.
2311 const StoredDiagnostic &SD = Diags[I];
2312 SourceLocation L = SD.getLocation();
2313 TranslateSLoc(L, Remap);
2314 FullSourceLoc Loc(L, SrcMgr);
2316 SmallVector<CharSourceRange, 4> Ranges;
2317 Ranges.reserve(SD.range_size());
2318 for (StoredDiagnostic::range_iterator I = SD.range_begin(),
2321 SourceLocation BL = I->getBegin();
2322 TranslateSLoc(BL, Remap);
2323 SourceLocation EL = I->getEnd();
2324 TranslateSLoc(EL, Remap);
2325 Ranges.push_back(CharSourceRange(SourceRange(BL, EL), I->isTokenRange()));
2328 SmallVector<FixItHint, 2> FixIts;
2329 FixIts.reserve(SD.fixit_size());
2330 for (StoredDiagnostic::fixit_iterator I = SD.fixit_begin(),
2333 FixIts.push_back(FixItHint());
2334 FixItHint &FH = FixIts.back();
2335 FH.CodeToInsert = I->CodeToInsert;
2336 SourceLocation BL = I->RemoveRange.getBegin();
2337 TranslateSLoc(BL, Remap);
2338 SourceLocation EL = I->RemoveRange.getEnd();
2339 TranslateSLoc(EL, Remap);
2340 FH.RemoveRange = CharSourceRange(SourceRange(BL, EL),
2341 I->RemoveRange.isTokenRange());
2344 Result.push_back(StoredDiagnostic(SD.getLevel(), SD.getID(),
2345 SD.getMessage(), Loc, Ranges, FixIts));
2350 SourceLocation ASTUnit::getLocation(const FileEntry *File,
2351 unsigned Line, unsigned Col) const {
2352 const SourceManager &SM = getSourceManager();
2353 SourceLocation Loc = SM.translateFileLineCol(File, Line, Col);
2354 return SM.getMacroArgExpandedLocation(Loc);
2357 SourceLocation ASTUnit::getLocation(const FileEntry *File,
2358 unsigned Offset) const {
2359 const SourceManager &SM = getSourceManager();
2360 SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1);
2361 return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
2364 /// \brief If \arg Loc is a loaded location from the preamble, returns
2365 /// the corresponding local location of the main file, otherwise it returns
2367 SourceLocation ASTUnit::mapLocationFromPreamble(SourceLocation Loc) {
2370 PreambleID = SourceMgr->getPreambleFileID();
2372 if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
2376 if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble.size()) {
2377 SourceLocation FileLoc
2378 = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID());
2379 return FileLoc.getLocWithOffset(Offs);
2385 /// \brief If \arg Loc is a local location of the main file but inside the
2386 /// preamble chunk, returns the corresponding loaded location from the
2387 /// preamble, otherwise it returns \arg Loc.
2388 SourceLocation ASTUnit::mapLocationToPreamble(SourceLocation Loc) {
2391 PreambleID = SourceMgr->getPreambleFileID();
2393 if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
2397 if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) &&
2398 Offs < Preamble.size()) {
2399 SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID);
2400 return FileLoc.getLocWithOffset(Offs);
2406 void ASTUnit::PreambleData::countLines() const {
2411 for (std::vector<char>::const_iterator
2412 I = Buffer.begin(), E = Buffer.end(); I != E; ++I) {
2416 if (Buffer.back() != '\n')
2421 ASTUnit::ConcurrencyState::ConcurrencyState() {
2422 Mutex = new llvm::sys::MutexImpl(/*recursive=*/true);
2425 ASTUnit::ConcurrencyState::~ConcurrencyState() {
2426 delete static_cast<llvm::sys::MutexImpl *>(Mutex);
2429 void ASTUnit::ConcurrencyState::start() {
2430 bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire();
2431 assert(acquired && "Concurrent access to ASTUnit!");
2434 void ASTUnit::ConcurrencyState::finish() {
2435 static_cast<llvm::sys::MutexImpl *>(Mutex)->release();
2440 ASTUnit::ConcurrencyState::ConcurrencyState() {}
2441 ASTUnit::ConcurrencyState::~ConcurrencyState() {}
2442 void ASTUnit::ConcurrencyState::start() {}
2443 void ASTUnit::ConcurrencyState::finish() {}