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/ASTSerializationListener.h"
33 #include "clang/Serialization/ASTWriter.h"
34 #include "clang/Lex/HeaderSearch.h"
35 #include "clang/Lex/Preprocessor.h"
36 #include "clang/Basic/TargetOptions.h"
37 #include "clang/Basic/TargetInfo.h"
38 #include "clang/Basic/Diagnostic.h"
39 #include "llvm/ADT/ArrayRef.h"
40 #include "llvm/ADT/StringExtras.h"
41 #include "llvm/ADT/StringSet.h"
42 #include "llvm/Support/Atomic.h"
43 #include "llvm/Support/MemoryBuffer.h"
44 #include "llvm/Support/Host.h"
45 #include "llvm/Support/Path.h"
46 #include "llvm/Support/raw_ostream.h"
47 #include "llvm/Support/Timer.h"
48 #include "llvm/Support/CrashRecoveryContext.h"
52 using namespace clang;
54 using llvm::TimeRecord;
63 explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
65 Start = TimeRecord::getCurrentTime();
68 void setOutput(const llvm::Twine &Output) {
70 this->Output = Output.str();
75 TimeRecord Elapsed = TimeRecord::getCurrentTime();
77 llvm::errs() << Output << ':';
78 Elapsed.print(Elapsed, llvm::errs());
85 /// \brief After failing to build a precompiled preamble (due to
86 /// errors in the source that occurs in the preamble), the number of
87 /// reparses during which we'll skip even trying to precompile the
89 const unsigned DefaultPreambleRebuildInterval = 5;
91 /// \brief Tracks the number of ASTUnit objects that are currently active.
93 /// Used for debugging purposes only.
94 static llvm::sys::cas_flag ActiveASTUnitObjects;
96 ASTUnit::ASTUnit(bool _MainFileIsAST)
97 : OnlyLocalDecls(false), CaptureDiagnostics(false),
98 MainFileIsAST(_MainFileIsAST),
99 CompleteTranslationUnit(true), WantTiming(getenv("LIBCLANG_TIMING")),
100 OwnsRemappedFileBuffers(true),
101 NumStoredDiagnosticsFromDriver(0),
102 ConcurrencyCheckValue(CheckUnlocked),
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 ConcurrencyCheckValue = CheckLocked;
118 CleanTemporaryFiles();
119 if (!PreambleFile.empty())
120 llvm::sys::Path(PreambleFile).eraseFromDisk();
122 // Free the buffers associated with remapped files. We are required to
123 // perform this operation here because we explicitly request that the
124 // compiler instance *not* free these buffers for each invocation of the
126 if (Invocation.getPtr() && OwnsRemappedFileBuffers) {
127 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
128 for (PreprocessorOptions::remapped_file_buffer_iterator
129 FB = PPOpts.remapped_file_buffer_begin(),
130 FBEnd = PPOpts.remapped_file_buffer_end();
136 delete SavedMainFileBuffer;
137 delete PreambleBuffer;
139 ClearCachedCompletionResults();
141 if (getenv("LIBCLANG_OBJTRACKING")) {
142 llvm::sys::AtomicDecrement(&ActiveASTUnitObjects);
143 fprintf(stderr, "--- %d translation units\n", ActiveASTUnitObjects);
147 void ASTUnit::CleanTemporaryFiles() {
148 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
149 TemporaryFiles[I].eraseFromDisk();
150 TemporaryFiles.clear();
153 /// \brief Determine the set of code-completion contexts in which this
154 /// declaration should be shown.
155 static unsigned getDeclShowContexts(NamedDecl *ND,
156 const LangOptions &LangOpts,
157 bool &IsNestedNameSpecifier) {
158 IsNestedNameSpecifier = false;
160 if (isa<UsingShadowDecl>(ND))
161 ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl());
165 unsigned Contexts = 0;
166 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) ||
167 isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) {
168 // Types can appear in these contexts.
169 if (LangOpts.CPlusPlus || !isa<TagDecl>(ND))
170 Contexts |= (1 << (CodeCompletionContext::CCC_TopLevel - 1))
171 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
172 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
173 | (1 << (CodeCompletionContext::CCC_Statement - 1))
174 | (1 << (CodeCompletionContext::CCC_Type - 1))
175 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1));
177 // In C++, types can appear in expressions contexts (for functional casts).
178 if (LangOpts.CPlusPlus)
179 Contexts |= (1 << (CodeCompletionContext::CCC_Expression - 1));
181 // In Objective-C, message sends can send interfaces. In Objective-C++,
182 // all types are available due to functional casts.
183 if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND))
184 Contexts |= (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1));
186 // In Objective-C, you can only be a subclass of another Objective-C class
187 if (isa<ObjCInterfaceDecl>(ND))
188 Contexts |= (1 << (CodeCompletionContext::CCC_ObjCSuperclass - 1));
190 // Deal with tag names.
191 if (isa<EnumDecl>(ND)) {
192 Contexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1));
194 // Part of the nested-name-specifier in C++0x.
195 if (LangOpts.CPlusPlus0x)
196 IsNestedNameSpecifier = true;
197 } else if (RecordDecl *Record = dyn_cast<RecordDecl>(ND)) {
198 if (Record->isUnion())
199 Contexts |= (1 << (CodeCompletionContext::CCC_UnionTag - 1));
201 Contexts |= (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1));
203 if (LangOpts.CPlusPlus)
204 IsNestedNameSpecifier = true;
205 } else if (isa<ClassTemplateDecl>(ND))
206 IsNestedNameSpecifier = true;
207 } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) {
208 // Values can appear in these contexts.
209 Contexts = (1 << (CodeCompletionContext::CCC_Statement - 1))
210 | (1 << (CodeCompletionContext::CCC_Expression - 1))
211 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
212 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1));
213 } else if (isa<ObjCProtocolDecl>(ND)) {
214 Contexts = (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1));
215 } else if (isa<ObjCCategoryDecl>(ND)) {
216 Contexts = (1 << (CodeCompletionContext::CCC_ObjCCategoryName - 1));
217 } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
218 Contexts = (1 << (CodeCompletionContext::CCC_Namespace - 1));
220 // Part of the nested-name-specifier.
221 IsNestedNameSpecifier = true;
227 void ASTUnit::CacheCodeCompletionResults() {
231 SimpleTimer Timer(WantTiming);
232 Timer.setOutput("Cache global code completions for " + getMainFileName());
234 // Clear out the previous results.
235 ClearCachedCompletionResults();
237 // Gather the set of global code completions.
238 typedef CodeCompletionResult Result;
239 llvm::SmallVector<Result, 8> Results;
240 CachedCompletionAllocator = new GlobalCodeCompletionAllocator;
241 TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator, Results);
243 // Translate global code completions into cached completions.
244 llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
246 for (unsigned I = 0, N = Results.size(); I != N; ++I) {
247 switch (Results[I].Kind) {
248 case Result::RK_Declaration: {
249 bool IsNestedNameSpecifier = false;
250 CachedCodeCompletionResult CachedResult;
251 CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema,
252 *CachedCompletionAllocator);
253 CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration,
254 Ctx->getLangOptions(),
255 IsNestedNameSpecifier);
256 CachedResult.Priority = Results[I].Priority;
257 CachedResult.Kind = Results[I].CursorKind;
258 CachedResult.Availability = Results[I].Availability;
260 // Keep track of the type of this completion in an ASTContext-agnostic
262 QualType UsageType = getDeclUsageType(*Ctx, Results[I].Declaration);
263 if (UsageType.isNull()) {
264 CachedResult.TypeClass = STC_Void;
265 CachedResult.Type = 0;
267 CanQualType CanUsageType
268 = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
269 CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
271 // Determine whether we have already seen this type. If so, we save
272 // ourselves the work of formatting the type string by using the
273 // temporary, CanQualType-based hash table to find the associated value.
274 unsigned &TypeValue = CompletionTypes[CanUsageType];
275 if (TypeValue == 0) {
276 TypeValue = CompletionTypes.size();
277 CachedCompletionTypes[QualType(CanUsageType).getAsString()]
281 CachedResult.Type = TypeValue;
284 CachedCompletionResults.push_back(CachedResult);
286 /// Handle nested-name-specifiers in C++.
287 if (TheSema->Context.getLangOptions().CPlusPlus &&
288 IsNestedNameSpecifier && !Results[I].StartsNestedNameSpecifier) {
289 // The contexts in which a nested-name-specifier can appear in C++.
291 = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
292 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
293 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
294 | (1 << (CodeCompletionContext::CCC_Statement - 1))
295 | (1 << (CodeCompletionContext::CCC_Expression - 1))
296 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
297 | (1 << (CodeCompletionContext::CCC_EnumTag - 1))
298 | (1 << (CodeCompletionContext::CCC_UnionTag - 1))
299 | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1))
300 | (1 << (CodeCompletionContext::CCC_Type - 1))
301 | (1 << (CodeCompletionContext::CCC_PotentiallyQualifiedName - 1))
302 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1));
304 if (isa<NamespaceDecl>(Results[I].Declaration) ||
305 isa<NamespaceAliasDecl>(Results[I].Declaration))
306 NNSContexts |= (1 << (CodeCompletionContext::CCC_Namespace - 1));
308 if (unsigned RemainingContexts
309 = NNSContexts & ~CachedResult.ShowInContexts) {
310 // If there any contexts where this completion can be a
311 // nested-name-specifier but isn't already an option, create a
312 // nested-name-specifier completion.
313 Results[I].StartsNestedNameSpecifier = true;
314 CachedResult.Completion
315 = Results[I].CreateCodeCompletionString(*TheSema,
316 *CachedCompletionAllocator);
317 CachedResult.ShowInContexts = RemainingContexts;
318 CachedResult.Priority = CCP_NestedNameSpecifier;
319 CachedResult.TypeClass = STC_Void;
320 CachedResult.Type = 0;
321 CachedCompletionResults.push_back(CachedResult);
327 case Result::RK_Keyword:
328 case Result::RK_Pattern:
329 // Ignore keywords and patterns; we don't care, since they are so
330 // easily regenerated.
333 case Result::RK_Macro: {
334 CachedCodeCompletionResult CachedResult;
335 CachedResult.Completion
336 = Results[I].CreateCodeCompletionString(*TheSema,
337 *CachedCompletionAllocator);
338 CachedResult.ShowInContexts
339 = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
340 | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1))
341 | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1))
342 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
343 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
344 | (1 << (CodeCompletionContext::CCC_Statement - 1))
345 | (1 << (CodeCompletionContext::CCC_Expression - 1))
346 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
347 | (1 << (CodeCompletionContext::CCC_MacroNameUse - 1))
348 | (1 << (CodeCompletionContext::CCC_PreprocessorExpression - 1))
349 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
350 | (1 << (CodeCompletionContext::CCC_OtherWithMacros - 1));
352 CachedResult.Priority = Results[I].Priority;
353 CachedResult.Kind = Results[I].CursorKind;
354 CachedResult.Availability = Results[I].Availability;
355 CachedResult.TypeClass = STC_Void;
356 CachedResult.Type = 0;
357 CachedCompletionResults.push_back(CachedResult);
363 // Save the current top-level hash value.
364 CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
367 void ASTUnit::ClearCachedCompletionResults() {
368 CachedCompletionResults.clear();
369 CachedCompletionTypes.clear();
370 CachedCompletionAllocator = 0;
375 /// \brief Gathers information from ASTReader that will be used to initialize
377 class ASTInfoCollector : public ASTReaderListener {
378 LangOptions &LangOpt;
380 std::string &TargetTriple;
381 std::string &Predefines;
384 unsigned NumHeaderInfos;
387 ASTInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
388 std::string &TargetTriple, std::string &Predefines,
390 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
391 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
393 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
398 virtual bool ReadTargetTriple(llvm::StringRef Triple) {
399 TargetTriple = Triple;
403 virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
404 llvm::StringRef OriginalFileName,
405 std::string &SuggestedPredefines,
406 FileManager &FileMgr) {
407 Predefines = Buffers[0].Data;
408 for (unsigned I = 1, N = Buffers.size(); I != N; ++I) {
409 Predefines += Buffers[I].Data;
414 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {
415 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
418 virtual void ReadCounter(unsigned Value) {
423 class StoredDiagnosticClient : public DiagnosticClient {
424 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags;
427 explicit StoredDiagnosticClient(
428 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
429 : StoredDiags(StoredDiags) { }
431 virtual void HandleDiagnostic(Diagnostic::Level Level,
432 const DiagnosticInfo &Info);
435 /// \brief RAII object that optionally captures diagnostics, if
436 /// there is no diagnostic client to capture them already.
437 class CaptureDroppedDiagnostics {
439 StoredDiagnosticClient Client;
440 DiagnosticClient *PreviousClient;
443 CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags,
444 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
445 : Diags(Diags), Client(StoredDiags), PreviousClient(0)
447 if (RequestCapture || Diags.getClient() == 0) {
448 PreviousClient = Diags.takeClient();
449 Diags.setClient(&Client);
453 ~CaptureDroppedDiagnostics() {
454 if (Diags.getClient() == &Client) {
456 Diags.setClient(PreviousClient);
461 } // anonymous namespace
463 void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level,
464 const DiagnosticInfo &Info) {
465 // Default implementation (Warnings/errors count).
466 DiagnosticClient::HandleDiagnostic(Level, Info);
468 StoredDiags.push_back(StoredDiagnostic(Level, Info));
471 const std::string &ASTUnit::getOriginalSourceFileName() {
472 return OriginalSourceFile;
475 const std::string &ASTUnit::getASTFileName() {
476 assert(isMainFileAST() && "Not an ASTUnit from an AST file!");
477 return static_cast<ASTReader *>(Ctx->getExternalSource())->getFileName();
480 llvm::MemoryBuffer *ASTUnit::getBufferForFile(llvm::StringRef Filename,
481 std::string *ErrorStr) {
483 return FileMgr->getBufferForFile(Filename, ErrorStr);
486 /// \brief Configure the diagnostics object for use with ASTUnit.
487 void ASTUnit::ConfigureDiags(llvm::IntrusiveRefCntPtr<Diagnostic> &Diags,
488 const char **ArgBegin, const char **ArgEnd,
489 ASTUnit &AST, bool CaptureDiagnostics) {
490 if (!Diags.getPtr()) {
491 // No diagnostics engine was provided, so create our own diagnostics object
492 // with the default options.
493 DiagnosticOptions DiagOpts;
494 DiagnosticClient *Client = 0;
495 if (CaptureDiagnostics)
496 Client = new StoredDiagnosticClient(AST.StoredDiagnostics);
497 Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd- ArgBegin,
499 } else if (CaptureDiagnostics) {
500 Diags->setClient(new StoredDiagnosticClient(AST.StoredDiagnostics));
504 ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename,
505 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
506 const FileSystemOptions &FileSystemOpts,
508 RemappedFile *RemappedFiles,
509 unsigned NumRemappedFiles,
510 bool CaptureDiagnostics) {
511 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
513 // Recover resources if we crash before exiting this method.
514 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
515 ASTUnitCleanup(AST.get());
516 llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
517 llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
518 DiagCleanup(Diags.getPtr());
520 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
522 AST->OnlyLocalDecls = OnlyLocalDecls;
523 AST->CaptureDiagnostics = CaptureDiagnostics;
524 AST->Diagnostics = Diags;
525 AST->FileMgr = new FileManager(FileSystemOpts);
526 AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
527 AST->getFileManager());
528 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
530 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
531 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
532 if (const llvm::MemoryBuffer *
533 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
534 // Create the file entry for the file that we're mapping from.
535 const FileEntry *FromFile
536 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
537 memBuf->getBufferSize(),
540 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
541 << RemappedFiles[I].first;
546 // Override the contents of the "from" file with the contents of
548 AST->getSourceManager().overrideFileContents(FromFile, memBuf);
551 const char *fname = fileOrBuf.get<const char *>();
552 const FileEntry *ToFile = AST->FileMgr->getFile(fname);
554 AST->getDiagnostics().Report(diag::err_fe_remap_missing_to_file)
555 << RemappedFiles[I].first << fname;
559 // Create the file entry for the file that we're mapping from.
560 const FileEntry *FromFile
561 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
565 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
566 << RemappedFiles[I].first;
571 // Override the contents of the "from" file with the contents of
573 AST->getSourceManager().overrideFileContents(FromFile, ToFile);
577 // Gather Info for preprocessor construction later on.
579 LangOptions LangInfo;
580 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
581 std::string TargetTriple;
582 std::string Predefines;
585 llvm::OwningPtr<ASTReader> Reader;
587 Reader.reset(new ASTReader(AST->getSourceManager(), AST->getFileManager(),
588 AST->getDiagnostics()));
590 // Recover resources if we crash before exiting this method.
591 llvm::CrashRecoveryContextCleanupRegistrar<ASTReader>
592 ReaderCleanup(Reader.get());
594 Reader->setListener(new ASTInfoCollector(LangInfo, HeaderInfo, TargetTriple,
595 Predefines, Counter));
597 switch (Reader->ReadAST(Filename, ASTReader::MainFile)) {
598 case ASTReader::Success:
601 case ASTReader::Failure:
602 case ASTReader::IgnorePCH:
603 AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
607 AST->OriginalSourceFile = Reader->getOriginalSourceFile();
609 // AST file loaded successfully. Now create the preprocessor.
611 // Get information about the target being compiled for.
613 // FIXME: This is broken, we should store the TargetOptions in the AST file.
614 TargetOptions TargetOpts;
616 TargetOpts.CXXABI = "";
618 TargetOpts.Features.clear();
619 TargetOpts.Triple = TargetTriple;
620 AST->Target = TargetInfo::CreateTargetInfo(AST->getDiagnostics(),
622 AST->PP = new Preprocessor(AST->getDiagnostics(), LangInfo, *AST->Target,
623 AST->getSourceManager(), HeaderInfo);
624 Preprocessor &PP = *AST->PP;
626 PP.setPredefines(Reader->getSuggestedPredefines());
627 PP.setCounterValue(Counter);
628 Reader->setPreprocessor(PP);
630 // Create and initialize the ASTContext.
632 AST->Ctx = new ASTContext(LangInfo,
633 AST->getSourceManager(),
635 PP.getIdentifierTable(),
636 PP.getSelectorTable(),
638 /* size_reserve = */0);
639 ASTContext &Context = *AST->Ctx;
641 Reader->InitializeContext(Context);
643 // Attach the AST reader to the AST context as an external AST
644 // source, so that declarations will be deserialized from the
645 // AST file as needed.
646 ASTReader *ReaderPtr = Reader.get();
647 llvm::OwningPtr<ExternalASTSource> Source(Reader.take());
649 // Unregister the cleanup for ASTReader. It will get cleaned up
650 // by the ASTUnit cleanup.
651 ReaderCleanup.unregister();
653 Context.setExternalSource(Source);
655 // Create an AST consumer, even though it isn't used.
656 AST->Consumer.reset(new ASTConsumer);
658 // Create a semantic analysis object and tell the AST reader about it.
659 AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer));
660 AST->TheSema->Initialize();
661 ReaderPtr->InitializeSema(*AST->TheSema);
668 /// \brief Preprocessor callback class that updates a hash value with the names
669 /// of all macros that have been defined by the translation unit.
670 class MacroDefinitionTrackerPPCallbacks : public PPCallbacks {
674 explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { }
676 virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
677 Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash);
681 /// \brief Add the given declaration to the hash of all top-level entities.
682 void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) {
686 DeclContext *DC = D->getDeclContext();
690 if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit()))
693 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
694 if (ND->getIdentifier())
695 Hash = llvm::HashString(ND->getIdentifier()->getName(), Hash);
696 else if (DeclarationName Name = ND->getDeclName()) {
697 std::string NameStr = Name.getAsString();
698 Hash = llvm::HashString(NameStr, Hash);
703 if (ObjCForwardProtocolDecl *Forward
704 = dyn_cast<ObjCForwardProtocolDecl>(D)) {
705 for (ObjCForwardProtocolDecl::protocol_iterator
706 P = Forward->protocol_begin(),
707 PEnd = Forward->protocol_end();
709 AddTopLevelDeclarationToHash(*P, Hash);
713 if (ObjCClassDecl *Class = llvm::dyn_cast<ObjCClassDecl>(D)) {
714 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
716 AddTopLevelDeclarationToHash(I->getInterface(), Hash);
721 class TopLevelDeclTrackerConsumer : public ASTConsumer {
726 TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
727 : Unit(_Unit), Hash(Hash) {
731 void HandleTopLevelDecl(DeclGroupRef D) {
732 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
734 // FIXME: Currently ObjC method declarations are incorrectly being
735 // reported as top-level declarations, even though their DeclContext
736 // is the containing ObjC @interface/@implementation. This is a
737 // fundamental problem in the parser right now.
738 if (isa<ObjCMethodDecl>(D))
741 AddTopLevelDeclarationToHash(D, Hash);
742 Unit.addTopLevelDecl(D);
746 // We're not interested in "interesting" decls.
747 void HandleInterestingDecl(DeclGroupRef) {}
750 class TopLevelDeclTrackerAction : public ASTFrontendAction {
754 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
755 llvm::StringRef InFile) {
756 CI.getPreprocessor().addPPCallbacks(
757 new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
758 return new TopLevelDeclTrackerConsumer(Unit,
759 Unit.getCurrentTopLevelHashValue());
763 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
765 virtual bool hasCodeCompletionSupport() const { return false; }
766 virtual bool usesCompleteTranslationUnit() {
767 return Unit.isCompleteTranslationUnit();
771 class PrecompilePreambleConsumer : public PCHGenerator,
772 public ASTSerializationListener {
775 std::vector<Decl *> TopLevelDecls;
778 PrecompilePreambleConsumer(ASTUnit &Unit,
779 const Preprocessor &PP, bool Chaining,
780 const char *isysroot, llvm::raw_ostream *Out)
781 : PCHGenerator(PP, "", Chaining, isysroot, Out), Unit(Unit),
782 Hash(Unit.getCurrentTopLevelHashValue()) {
786 virtual void HandleTopLevelDecl(DeclGroupRef D) {
787 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
789 // FIXME: Currently ObjC method declarations are incorrectly being
790 // reported as top-level declarations, even though their DeclContext
791 // is the containing ObjC @interface/@implementation. This is a
792 // fundamental problem in the parser right now.
793 if (isa<ObjCMethodDecl>(D))
795 AddTopLevelDeclarationToHash(D, Hash);
796 TopLevelDecls.push_back(D);
800 virtual void HandleTranslationUnit(ASTContext &Ctx) {
801 PCHGenerator::HandleTranslationUnit(Ctx);
802 if (!Unit.getDiagnostics().hasErrorOccurred()) {
803 // Translate the top-level declarations we captured during
804 // parsing into declaration IDs in the precompiled
805 // preamble. This will allow us to deserialize those top-level
806 // declarations when requested.
807 for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I)
808 Unit.addTopLevelDeclFromPreamble(
809 getWriter().getDeclID(TopLevelDecls[I]));
813 virtual void SerializedPreprocessedEntity(PreprocessedEntity *Entity,
815 Unit.addPreprocessedEntityFromPreamble(Offset);
818 virtual ASTSerializationListener *GetASTSerializationListener() {
823 class PrecompilePreambleAction : public ASTFrontendAction {
827 explicit PrecompilePreambleAction(ASTUnit &Unit) : Unit(Unit) {}
829 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
830 llvm::StringRef InFile) {
832 std::string OutputFile;
833 llvm::raw_ostream *OS = 0;
835 if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot,
840 const char *isysroot = CI.getFrontendOpts().RelocatablePCH ?
842 CI.getPreprocessor().addPPCallbacks(
843 new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
844 return new PrecompilePreambleConsumer(Unit, CI.getPreprocessor(), Chaining,
848 virtual bool hasCodeCompletionSupport() const { return false; }
849 virtual bool hasASTFileSupport() const { return false; }
850 virtual bool usesCompleteTranslationUnit() { return false; }
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 Clang->setInvocation(&*Invocation);
877 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
879 // Set up diagnostics, capturing any diagnostics that would
880 // otherwise be dropped.
881 Clang->setDiagnostics(&getDiagnostics());
883 // Create the target instance.
884 Clang->getTargetOpts().Features = TargetFeatures;
885 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
886 Clang->getTargetOpts()));
887 if (!Clang->hasTarget()) {
888 delete OverrideMainBuffer;
892 // Inform the target of the language options.
894 // FIXME: We shouldn't need to do this, the target should be immutable once
895 // created. This complexity should be lifted elsewhere.
896 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
898 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
899 "Invocation must have exactly one source file!");
900 assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
901 "FIXME: AST inputs not yet supported here!");
902 assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
903 "IR inputs not support here!");
905 // Configure the various subsystems.
906 // FIXME: Should we retain the previous file manager?
907 FileSystemOpts = Clang->getFileSystemOpts();
908 FileMgr = new FileManager(FileSystemOpts);
909 SourceMgr = new SourceManager(getDiagnostics(), *FileMgr);
914 // Clear out old caches and data.
915 TopLevelDecls.clear();
916 PreprocessedEntities.clear();
917 CleanTemporaryFiles();
918 PreprocessedEntitiesByFile.clear();
920 if (!OverrideMainBuffer) {
921 StoredDiagnostics.erase(
922 StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver,
923 StoredDiagnostics.end());
924 TopLevelDeclsInPreamble.clear();
925 PreprocessedEntitiesInPreamble.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 std::string PriorImplicitPCHInclude;
940 if (OverrideMainBuffer) {
941 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
942 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
943 PreprocessorOpts.PrecompiledPreambleBytes.second
944 = PreambleEndsAtStartOfLine;
945 PriorImplicitPCHInclude = PreprocessorOpts.ImplicitPCHInclude;
946 PreprocessorOpts.ImplicitPCHInclude = PreambleFile;
947 PreprocessorOpts.DisablePCHValidation = true;
949 // The stored diagnostic has the old source manager in it; update
950 // the locations to refer into the new source manager. Since we've
951 // been careful to make sure that the source manager's state
952 // before and after are identical, so that we can reuse the source
954 for (unsigned I = NumStoredDiagnosticsFromDriver,
955 N = StoredDiagnostics.size();
957 FullSourceLoc Loc(StoredDiagnostics[I].getLocation(),
959 StoredDiagnostics[I].setLocation(Loc);
962 // Keep track of the override buffer;
963 SavedMainFileBuffer = OverrideMainBuffer;
965 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
966 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
969 llvm::OwningPtr<TopLevelDeclTrackerAction> Act(
970 new TopLevelDeclTrackerAction(*this));
972 // Recover resources if we crash before exiting this method.
973 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
974 ActCleanup(Act.get());
976 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
977 Clang->getFrontendOpts().Inputs[0].first))
982 // Steal the created target, context, and preprocessor.
983 TheSema.reset(Clang->takeSema());
984 Consumer.reset(Clang->takeASTConsumer());
985 Ctx = &Clang->getASTContext();
986 PP = &Clang->getPreprocessor();
987 Clang->setSourceManager(0);
988 Clang->setFileManager(0);
989 Target = &Clang->getTarget();
991 Act->EndSourceFile();
993 // Remove the overridden buffer we used for the preamble.
994 if (OverrideMainBuffer) {
995 PreprocessorOpts.eraseRemappedFile(
996 PreprocessorOpts.remapped_file_buffer_end() - 1);
997 PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude;
1003 // Remove the overridden buffer we used for the preamble.
1004 if (OverrideMainBuffer) {
1005 PreprocessorOpts.eraseRemappedFile(
1006 PreprocessorOpts.remapped_file_buffer_end() - 1);
1007 PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude;
1008 delete OverrideMainBuffer;
1009 SavedMainFileBuffer = 0;
1012 StoredDiagnostics.clear();
1016 /// \brief Simple function to retrieve a path for a preamble precompiled header.
1017 static std::string GetPreamblePCHPath() {
1018 // FIXME: This is lame; sys::Path should provide this function (in particular,
1019 // it should know how to find the temporary files dir).
1020 // FIXME: This is really lame. I copied this code from the Driver!
1021 // FIXME: This is a hack so that we can override the preamble file during
1022 // crash-recovery testing, which is the only case where the preamble files
1023 // are not necessarily cleaned up.
1024 const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
1029 const char *TmpDir = ::getenv("TMPDIR");
1031 TmpDir = ::getenv("TEMP");
1033 TmpDir = ::getenv("TMP");
1034 #ifdef LLVM_ON_WIN32
1036 TmpDir = ::getenv("USERPROFILE");
1040 llvm::sys::Path P(TmpDir);
1041 P.createDirectoryOnDisk(true);
1042 P.appendComponent("preamble");
1043 P.appendSuffix("pch");
1044 if (P.createTemporaryFileOnDisk())
1045 return std::string();
1050 /// \brief Compute the preamble for the main file, providing the source buffer
1051 /// that corresponds to the main file along with a pair (bytes, start-of-line)
1052 /// that describes the preamble.
1053 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
1054 ASTUnit::ComputePreamble(CompilerInvocation &Invocation,
1055 unsigned MaxLines, bool &CreatedBuffer) {
1056 FrontendOptions &FrontendOpts = Invocation.getFrontendOpts();
1057 PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts();
1058 CreatedBuffer = false;
1060 // Try to determine if the main file has been remapped, either from the
1061 // command line (to another file) or directly through the compiler invocation
1062 // (to a memory buffer).
1063 llvm::MemoryBuffer *Buffer = 0;
1064 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second);
1065 if (const llvm::sys::FileStatus *MainFileStatus = MainFilePath.getFileStatus()) {
1066 // Check whether there is a file-file remapping of the main file
1067 for (PreprocessorOptions::remapped_file_iterator
1068 M = PreprocessorOpts.remapped_file_begin(),
1069 E = PreprocessorOpts.remapped_file_end();
1072 llvm::sys::PathWithStatus MPath(M->first);
1073 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1074 if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1075 // We found a remapping. Try to load the resulting, remapped source.
1076 if (CreatedBuffer) {
1078 CreatedBuffer = false;
1081 Buffer = getBufferForFile(M->second);
1083 return std::make_pair((llvm::MemoryBuffer*)0,
1084 std::make_pair(0, true));
1085 CreatedBuffer = true;
1090 // Check whether there is a file-buffer remapping. It supercedes the
1091 // file-file remapping.
1092 for (PreprocessorOptions::remapped_file_buffer_iterator
1093 M = PreprocessorOpts.remapped_file_buffer_begin(),
1094 E = PreprocessorOpts.remapped_file_buffer_end();
1097 llvm::sys::PathWithStatus MPath(M->first);
1098 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1099 if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1100 // We found a remapping.
1101 if (CreatedBuffer) {
1103 CreatedBuffer = false;
1106 Buffer = const_cast<llvm::MemoryBuffer *>(M->second);
1112 // If the main source file was not remapped, load it now.
1114 Buffer = getBufferForFile(FrontendOpts.Inputs[0].second);
1116 return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true));
1118 CreatedBuffer = true;
1121 return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer, MaxLines));
1124 static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old,
1126 llvm::StringRef NewName) {
1127 llvm::MemoryBuffer *Result
1128 = llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName);
1129 memcpy(const_cast<char*>(Result->getBufferStart()),
1130 Old->getBufferStart(), Old->getBufferSize());
1131 memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(),
1132 ' ', NewSize - Old->getBufferSize() - 1);
1133 const_cast<char*>(Result->getBufferEnd())[-1] = '\n';
1138 /// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing
1139 /// the source file.
1141 /// This routine will compute the preamble of the main source file. If a
1142 /// non-trivial preamble is found, it will precompile that preamble into a
1143 /// precompiled header so that the precompiled preamble can be used to reduce
1144 /// reparsing time. If a precompiled preamble has already been constructed,
1145 /// this routine will determine if it is still valid and, if so, avoid
1146 /// rebuilding the precompiled preamble.
1148 /// \param AllowRebuild When true (the default), this routine is
1149 /// allowed to rebuild the precompiled preamble if it is found to be
1152 /// \param MaxLines When non-zero, the maximum number of lines that
1153 /// can occur within the preamble.
1155 /// \returns If the precompiled preamble can be used, returns a newly-allocated
1156 /// buffer that should be used in place of the main file when doing so.
1157 /// Otherwise, returns a NULL pointer.
1158 llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
1159 const CompilerInvocation &PreambleInvocationIn,
1161 unsigned MaxLines) {
1163 llvm::IntrusiveRefCntPtr<CompilerInvocation>
1164 PreambleInvocation(new CompilerInvocation(PreambleInvocationIn));
1165 FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts();
1166 PreprocessorOptions &PreprocessorOpts
1167 = PreambleInvocation->getPreprocessorOpts();
1169 bool CreatedPreambleBuffer = false;
1170 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble
1171 = ComputePreamble(*PreambleInvocation, MaxLines, CreatedPreambleBuffer);
1173 // If ComputePreamble() Take ownership of the
1174 llvm::OwningPtr<llvm::MemoryBuffer> OwnedPreambleBuffer;
1175 if (CreatedPreambleBuffer)
1176 OwnedPreambleBuffer.reset(NewPreamble.first);
1178 if (!NewPreamble.second.first) {
1179 // We couldn't find a preamble in the main source. Clear out the current
1180 // preamble, if we have one. It's obviously no good any more.
1182 if (!PreambleFile.empty()) {
1183 llvm::sys::Path(PreambleFile).eraseFromDisk();
1184 PreambleFile.clear();
1187 // The next time we actually see a preamble, precompile it.
1188 PreambleRebuildCounter = 1;
1192 if (!Preamble.empty()) {
1193 // We've previously computed a preamble. Check whether we have the same
1194 // preamble now that we did before, and that there's enough space in
1195 // the main-file buffer within the precompiled preamble to fit the
1197 if (Preamble.size() == NewPreamble.second.first &&
1198 PreambleEndsAtStartOfLine == NewPreamble.second.second &&
1199 NewPreamble.first->getBufferSize() < PreambleReservedSize-2 &&
1200 memcmp(&Preamble[0], NewPreamble.first->getBufferStart(),
1201 NewPreamble.second.first) == 0) {
1202 // The preamble has not changed. We may be able to re-use the precompiled
1205 // Check that none of the files used by the preamble have changed.
1206 bool AnyFileChanged = false;
1208 // First, make a record of those files that have been overridden via
1209 // remapping or unsaved_files.
1210 llvm::StringMap<std::pair<off_t, time_t> > OverriddenFiles;
1211 for (PreprocessorOptions::remapped_file_iterator
1212 R = PreprocessorOpts.remapped_file_begin(),
1213 REnd = PreprocessorOpts.remapped_file_end();
1214 !AnyFileChanged && R != REnd;
1216 struct stat StatBuf;
1217 if (FileMgr->getNoncachedStatValue(R->second, StatBuf)) {
1218 // If we can't stat the file we're remapping to, assume that something
1219 // horrible happened.
1220 AnyFileChanged = true;
1224 OverriddenFiles[R->first] = std::make_pair(StatBuf.st_size,
1227 for (PreprocessorOptions::remapped_file_buffer_iterator
1228 R = PreprocessorOpts.remapped_file_buffer_begin(),
1229 REnd = PreprocessorOpts.remapped_file_buffer_end();
1230 !AnyFileChanged && R != REnd;
1232 // FIXME: Should we actually compare the contents of file->buffer
1234 OverriddenFiles[R->first] = std::make_pair(R->second->getBufferSize(),
1238 // Check whether anything has changed.
1239 for (llvm::StringMap<std::pair<off_t, time_t> >::iterator
1240 F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end();
1241 !AnyFileChanged && F != FEnd;
1243 llvm::StringMap<std::pair<off_t, time_t> >::iterator Overridden
1244 = OverriddenFiles.find(F->first());
1245 if (Overridden != OverriddenFiles.end()) {
1246 // This file was remapped; check whether the newly-mapped file
1247 // matches up with the previous mapping.
1248 if (Overridden->second != F->second)
1249 AnyFileChanged = true;
1253 // The file was not remapped; check whether it has changed on disk.
1254 struct stat StatBuf;
1255 if (FileMgr->getNoncachedStatValue(F->first(), StatBuf)) {
1256 // If we can't stat the file, assume that something horrible happened.
1257 AnyFileChanged = true;
1258 } else if (StatBuf.st_size != F->second.first ||
1259 StatBuf.st_mtime != F->second.second)
1260 AnyFileChanged = true;
1263 if (!AnyFileChanged) {
1264 // Okay! We can re-use the precompiled preamble.
1266 // Set the state of the diagnostic object to mimic its state
1267 // after parsing the preamble.
1268 // FIXME: This won't catch any #pragma push warning changes that
1269 // have occurred in the preamble.
1270 getDiagnostics().Reset();
1271 ProcessWarningOptions(getDiagnostics(),
1272 PreambleInvocation->getDiagnosticOpts());
1273 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1274 if (StoredDiagnostics.size() > NumStoredDiagnosticsInPreamble)
1275 StoredDiagnostics.erase(
1276 StoredDiagnostics.begin() + NumStoredDiagnosticsInPreamble,
1277 StoredDiagnostics.end());
1279 // Create a version of the main file buffer that is padded to
1280 // buffer size we reserved when creating the preamble.
1281 return CreatePaddedMainFileBuffer(NewPreamble.first,
1282 PreambleReservedSize,
1283 FrontendOpts.Inputs[0].second);
1287 // If we aren't allowed to rebuild the precompiled preamble, just
1292 // We can't reuse the previously-computed preamble. Build a new one.
1294 llvm::sys::Path(PreambleFile).eraseFromDisk();
1295 PreambleRebuildCounter = 1;
1296 } else if (!AllowRebuild) {
1297 // We aren't allowed to rebuild the precompiled preamble; just
1302 // If the preamble rebuild counter > 1, it's because we previously
1303 // failed to build a preamble and we're not yet ready to try
1304 // again. Decrement the counter and return a failure.
1305 if (PreambleRebuildCounter > 1) {
1306 --PreambleRebuildCounter;
1310 // Create a temporary file for the precompiled preamble. In rare
1311 // circumstances, this can fail.
1312 std::string PreamblePCHPath = GetPreamblePCHPath();
1313 if (PreamblePCHPath.empty()) {
1314 // Try again next time.
1315 PreambleRebuildCounter = 1;
1319 // We did not previously compute a preamble, or it can't be reused anyway.
1320 SimpleTimer PreambleTimer(WantTiming);
1321 PreambleTimer.setOutput("Precompiling preamble");
1323 // Create a new buffer that stores the preamble. The buffer also contains
1324 // extra space for the original contents of the file (which will be present
1325 // when we actually parse the file) along with more room in case the file
1327 PreambleReservedSize = NewPreamble.first->getBufferSize();
1328 if (PreambleReservedSize < 4096)
1329 PreambleReservedSize = 8191;
1331 PreambleReservedSize *= 2;
1333 // Save the preamble text for later; we'll need to compare against it for
1334 // subsequent reparses.
1335 Preamble.assign(NewPreamble.first->getBufferStart(),
1336 NewPreamble.first->getBufferStart()
1337 + NewPreamble.second.first);
1338 PreambleEndsAtStartOfLine = NewPreamble.second.second;
1340 delete PreambleBuffer;
1342 = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize,
1343 FrontendOpts.Inputs[0].second);
1344 memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()),
1345 NewPreamble.first->getBufferStart(), Preamble.size());
1346 memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(),
1347 ' ', PreambleReservedSize - Preamble.size() - 1);
1348 const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n';
1350 // Remap the main source file to the preamble buffer.
1351 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second);
1352 PreprocessorOpts.addRemappedFile(MainFilePath.str(), PreambleBuffer);
1354 // Tell the compiler invocation to generate a temporary precompiled header.
1355 FrontendOpts.ProgramAction = frontend::GeneratePCH;
1356 FrontendOpts.ChainedPCH = true;
1357 // FIXME: Generate the precompiled header into memory?
1358 FrontendOpts.OutputFile = PreamblePCHPath;
1359 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
1360 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
1362 // Create the compiler instance to use for building the precompiled preamble.
1363 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1365 // Recover resources if we crash before exiting this method.
1366 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1367 CICleanup(Clang.get());
1369 Clang->setInvocation(&*PreambleInvocation);
1370 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
1372 // Set up diagnostics, capturing all of the diagnostics produced.
1373 Clang->setDiagnostics(&getDiagnostics());
1375 // Create the target instance.
1376 Clang->getTargetOpts().Features = TargetFeatures;
1377 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1378 Clang->getTargetOpts()));
1379 if (!Clang->hasTarget()) {
1380 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1382 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1383 PreprocessorOpts.eraseRemappedFile(
1384 PreprocessorOpts.remapped_file_buffer_end() - 1);
1388 // Inform the target of the language options.
1390 // FIXME: We shouldn't need to do this, the target should be immutable once
1391 // created. This complexity should be lifted elsewhere.
1392 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1394 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1395 "Invocation must have exactly one source file!");
1396 assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
1397 "FIXME: AST inputs not yet supported here!");
1398 assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
1399 "IR inputs not support here!");
1401 // Clear out old caches and data.
1402 getDiagnostics().Reset();
1403 ProcessWarningOptions(getDiagnostics(), Clang->getDiagnosticOpts());
1404 StoredDiagnostics.erase(
1405 StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver,
1406 StoredDiagnostics.end());
1407 TopLevelDecls.clear();
1408 TopLevelDeclsInPreamble.clear();
1409 PreprocessedEntities.clear();
1410 PreprocessedEntitiesInPreamble.clear();
1412 // Create a file manager object to provide access to and cache the filesystem.
1413 Clang->setFileManager(new FileManager(Clang->getFileSystemOpts()));
1415 // Create the source manager.
1416 Clang->setSourceManager(new SourceManager(getDiagnostics(),
1417 Clang->getFileManager()));
1419 llvm::OwningPtr<PrecompilePreambleAction> Act;
1420 Act.reset(new PrecompilePreambleAction(*this));
1421 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
1422 Clang->getFrontendOpts().Inputs[0].first)) {
1423 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1425 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1426 PreprocessorOpts.eraseRemappedFile(
1427 PreprocessorOpts.remapped_file_buffer_end() - 1);
1432 Act->EndSourceFile();
1434 if (Diagnostics->hasErrorOccurred()) {
1435 // There were errors parsing the preamble, so no precompiled header was
1436 // generated. Forget that we even tried.
1437 // FIXME: Should we leave a note for ourselves to try again?
1438 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1440 TopLevelDeclsInPreamble.clear();
1441 PreprocessedEntities.clear();
1442 PreprocessedEntitiesInPreamble.clear();
1443 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1444 PreprocessorOpts.eraseRemappedFile(
1445 PreprocessorOpts.remapped_file_buffer_end() - 1);
1449 // Keep track of the preamble we precompiled.
1450 PreambleFile = FrontendOpts.OutputFile;
1451 NumStoredDiagnosticsInPreamble = StoredDiagnostics.size();
1452 NumWarningsInPreamble = getDiagnostics().getNumWarnings();
1454 // Keep track of all of the files that the source manager knows about,
1455 // so we can verify whether they have changed or not.
1456 FilesInPreamble.clear();
1457 SourceManager &SourceMgr = Clang->getSourceManager();
1458 const llvm::MemoryBuffer *MainFileBuffer
1459 = SourceMgr.getBuffer(SourceMgr.getMainFileID());
1460 for (SourceManager::fileinfo_iterator F = SourceMgr.fileinfo_begin(),
1461 FEnd = SourceMgr.fileinfo_end();
1464 const FileEntry *File = F->second->OrigEntry;
1465 if (!File || F->second->getRawBuffer() == MainFileBuffer)
1468 FilesInPreamble[File->getName()]
1469 = std::make_pair(F->second->getSize(), File->getModificationTime());
1472 PreambleRebuildCounter = 1;
1473 PreprocessorOpts.eraseRemappedFile(
1474 PreprocessorOpts.remapped_file_buffer_end() - 1);
1476 // If the hash of top-level entities differs from the hash of the top-level
1477 // entities the last time we rebuilt the preamble, clear out the completion
1479 if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1480 CompletionCacheTopLevelHashValue = 0;
1481 PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1484 return CreatePaddedMainFileBuffer(NewPreamble.first,
1485 PreambleReservedSize,
1486 FrontendOpts.Inputs[0].second);
1489 void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1490 std::vector<Decl *> Resolved;
1491 Resolved.reserve(TopLevelDeclsInPreamble.size());
1492 ExternalASTSource &Source = *getASTContext().getExternalSource();
1493 for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) {
1494 // Resolve the declaration ID to an actual declaration, possibly
1495 // deserializing the declaration in the process.
1496 Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]);
1498 Resolved.push_back(D);
1500 TopLevelDeclsInPreamble.clear();
1501 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1504 void ASTUnit::RealizePreprocessedEntitiesFromPreamble() {
1508 PreprocessingRecord *PPRec = PP->getPreprocessingRecord();
1512 ExternalPreprocessingRecordSource *External = PPRec->getExternalSource();
1516 for (unsigned I = 0, N = PreprocessedEntitiesInPreamble.size(); I != N; ++I) {
1517 if (PreprocessedEntity *PE
1518 = External->ReadPreprocessedEntityAtOffset(
1519 PreprocessedEntitiesInPreamble[I]))
1520 PreprocessedEntities.push_back(PE);
1523 if (PreprocessedEntities.empty())
1526 PreprocessedEntities.insert(PreprocessedEntities.end(),
1527 PPRec->begin(true), PPRec->end(true));
1530 ASTUnit::pp_entity_iterator ASTUnit::pp_entity_begin() {
1531 if (!PreprocessedEntitiesInPreamble.empty() &&
1532 PreprocessedEntities.empty())
1533 RealizePreprocessedEntitiesFromPreamble();
1535 if (PreprocessedEntities.empty())
1536 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
1537 return PPRec->begin(true);
1539 return PreprocessedEntities.begin();
1542 ASTUnit::pp_entity_iterator ASTUnit::pp_entity_end() {
1543 if (!PreprocessedEntitiesInPreamble.empty() &&
1544 PreprocessedEntities.empty())
1545 RealizePreprocessedEntitiesFromPreamble();
1547 if (PreprocessedEntities.empty())
1548 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
1549 return PPRec->end(true);
1551 return PreprocessedEntities.end();
1554 unsigned ASTUnit::getMaxPCHLevel() const {
1555 if (!getOnlyLocalDecls())
1556 return Decl::MaxPCHLevel;
1561 llvm::StringRef ASTUnit::getMainFileName() const {
1562 return Invocation->getFrontendOpts().Inputs[0].second;
1565 ASTUnit *ASTUnit::create(CompilerInvocation *CI,
1566 llvm::IntrusiveRefCntPtr<Diagnostic> Diags) {
1567 llvm::OwningPtr<ASTUnit> AST;
1568 AST.reset(new ASTUnit(false));
1569 ConfigureDiags(Diags, 0, 0, *AST, /*CaptureDiagnostics=*/false);
1570 AST->Diagnostics = Diags;
1571 AST->Invocation = CI;
1572 AST->FileSystemOpts = CI->getFileSystemOpts();
1573 AST->FileMgr = new FileManager(AST->FileSystemOpts);
1574 AST->SourceMgr = new SourceManager(*Diags, *AST->FileMgr);
1579 ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(CompilerInvocation *CI,
1580 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
1581 ASTFrontendAction *Action) {
1582 assert(CI && "A CompilerInvocation is required");
1584 // Create the AST unit.
1585 llvm::OwningPtr<ASTUnit> AST;
1586 AST.reset(new ASTUnit(false));
1587 ConfigureDiags(Diags, 0, 0, *AST, /*CaptureDiagnostics*/false);
1588 AST->Diagnostics = Diags;
1589 AST->OnlyLocalDecls = false;
1590 AST->CaptureDiagnostics = false;
1591 AST->CompleteTranslationUnit = Action ? Action->usesCompleteTranslationUnit()
1593 AST->ShouldCacheCodeCompletionResults = false;
1594 AST->Invocation = CI;
1596 // Recover resources if we crash before exiting this method.
1597 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1598 ASTUnitCleanup(AST.get());
1599 llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
1600 llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
1601 DiagCleanup(Diags.getPtr());
1603 // We'll manage file buffers ourselves.
1604 CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1605 CI->getFrontendOpts().DisableFree = false;
1606 ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
1608 // Save the target features.
1609 AST->TargetFeatures = CI->getTargetOpts().Features;
1611 // Create the compiler instance to use for building the AST.
1612 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1614 // Recover resources if we crash before exiting this method.
1615 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1616 CICleanup(Clang.get());
1618 Clang->setInvocation(CI);
1619 AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
1621 // Set up diagnostics, capturing any diagnostics that would
1622 // otherwise be dropped.
1623 Clang->setDiagnostics(&AST->getDiagnostics());
1625 // Create the target instance.
1626 Clang->getTargetOpts().Features = AST->TargetFeatures;
1627 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1628 Clang->getTargetOpts()));
1629 if (!Clang->hasTarget())
1632 // Inform the target of the language options.
1634 // FIXME: We shouldn't need to do this, the target should be immutable once
1635 // created. This complexity should be lifted elsewhere.
1636 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1638 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1639 "Invocation must have exactly one source file!");
1640 assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
1641 "FIXME: AST inputs not yet supported here!");
1642 assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
1643 "IR inputs not supported here!");
1645 // Configure the various subsystems.
1646 AST->FileSystemOpts = Clang->getFileSystemOpts();
1647 AST->FileMgr = new FileManager(AST->FileSystemOpts);
1648 AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr);
1649 AST->TheSema.reset();
1653 // Create a file manager object to provide access to and cache the filesystem.
1654 Clang->setFileManager(&AST->getFileManager());
1656 // Create the source manager.
1657 Clang->setSourceManager(&AST->getSourceManager());
1659 ASTFrontendAction *Act = Action;
1661 llvm::OwningPtr<TopLevelDeclTrackerAction> TrackerAct;
1663 TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
1664 Act = TrackerAct.get();
1667 // Recover resources if we crash before exiting this method.
1668 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1669 ActCleanup(TrackerAct.get());
1671 if (!Act->BeginSourceFile(*Clang.get(),
1672 Clang->getFrontendOpts().Inputs[0].second,
1673 Clang->getFrontendOpts().Inputs[0].first))
1678 // Steal the created target, context, and preprocessor.
1679 AST->TheSema.reset(Clang->takeSema());
1680 AST->Consumer.reset(Clang->takeASTConsumer());
1681 AST->Ctx = &Clang->getASTContext();
1682 AST->PP = &Clang->getPreprocessor();
1683 Clang->setSourceManager(0);
1684 Clang->setFileManager(0);
1685 AST->Target = &Clang->getTarget();
1687 Act->EndSourceFile();
1692 bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) {
1696 // We'll manage file buffers ourselves.
1697 Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1698 Invocation->getFrontendOpts().DisableFree = false;
1699 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1701 // Save the target features.
1702 TargetFeatures = Invocation->getTargetOpts().Features;
1704 llvm::MemoryBuffer *OverrideMainBuffer = 0;
1705 if (PrecompilePreamble) {
1706 PreambleRebuildCounter = 2;
1708 = getMainBufferWithPrecompiledPreamble(*Invocation);
1711 SimpleTimer ParsingTimer(WantTiming);
1712 ParsingTimer.setOutput("Parsing " + getMainFileName());
1714 // Recover resources if we crash before exiting this method.
1715 llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1716 MemBufferCleanup(OverrideMainBuffer);
1718 return Parse(OverrideMainBuffer);
1721 ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
1722 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
1723 bool OnlyLocalDecls,
1724 bool CaptureDiagnostics,
1725 bool PrecompilePreamble,
1726 bool CompleteTranslationUnit,
1727 bool CacheCodeCompletionResults,
1728 bool NestedMacroExpansions) {
1729 // Create the AST unit.
1730 llvm::OwningPtr<ASTUnit> AST;
1731 AST.reset(new ASTUnit(false));
1732 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
1733 AST->Diagnostics = Diags;
1734 AST->OnlyLocalDecls = OnlyLocalDecls;
1735 AST->CaptureDiagnostics = CaptureDiagnostics;
1736 AST->CompleteTranslationUnit = CompleteTranslationUnit;
1737 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1738 AST->Invocation = CI;
1739 AST->NestedMacroExpansions = NestedMacroExpansions;
1741 // Recover resources if we crash before exiting this method.
1742 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1743 ASTUnitCleanup(AST.get());
1744 llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
1745 llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
1746 DiagCleanup(Diags.getPtr());
1748 return AST->LoadFromCompilerInvocation(PrecompilePreamble)? 0 : AST.take();
1751 ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
1752 const char **ArgEnd,
1753 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
1754 llvm::StringRef ResourceFilesPath,
1755 bool OnlyLocalDecls,
1756 bool CaptureDiagnostics,
1757 RemappedFile *RemappedFiles,
1758 unsigned NumRemappedFiles,
1759 bool RemappedFilesKeepOriginalName,
1760 bool PrecompilePreamble,
1761 bool CompleteTranslationUnit,
1762 bool CacheCodeCompletionResults,
1763 bool CXXPrecompilePreamble,
1765 bool NestedMacroExpansions) {
1766 if (!Diags.getPtr()) {
1767 // No diagnostics engine was provided, so create our own diagnostics object
1768 // with the default options.
1769 DiagnosticOptions DiagOpts;
1770 Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd - ArgBegin,
1774 llvm::SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1776 llvm::IntrusiveRefCntPtr<CompilerInvocation> CI;
1779 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
1782 CI = clang::createInvocationFromCommandLine(
1783 llvm::ArrayRef<const char *>(ArgBegin, ArgEnd-ArgBegin),
1789 // Override any files that need remapping
1790 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
1791 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
1792 if (const llvm::MemoryBuffer *
1793 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
1794 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, memBuf);
1796 const char *fname = fileOrBuf.get<const char *>();
1797 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, fname);
1800 CI->getPreprocessorOpts().RemappedFilesKeepOriginalName =
1801 RemappedFilesKeepOriginalName;
1803 // Override the resources path.
1804 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1806 // Check whether we should precompile the preamble and/or use chained PCH.
1807 // FIXME: This is a temporary hack while we debug C++ chained PCH.
1808 if (CI->getLangOpts().CPlusPlus) {
1809 PrecompilePreamble = PrecompilePreamble && CXXPrecompilePreamble;
1811 if (PrecompilePreamble && !CXXChainedPCH &&
1812 !CI->getPreprocessorOpts().ImplicitPCHInclude.empty())
1813 PrecompilePreamble = false;
1816 // Create the AST unit.
1817 llvm::OwningPtr<ASTUnit> AST;
1818 AST.reset(new ASTUnit(false));
1819 ConfigureDiags(Diags, ArgBegin, ArgEnd, *AST, CaptureDiagnostics);
1820 AST->Diagnostics = Diags;
1822 AST->FileSystemOpts = CI->getFileSystemOpts();
1823 AST->FileMgr = new FileManager(AST->FileSystemOpts);
1824 AST->OnlyLocalDecls = OnlyLocalDecls;
1825 AST->CaptureDiagnostics = CaptureDiagnostics;
1826 AST->CompleteTranslationUnit = CompleteTranslationUnit;
1827 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1828 AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1829 AST->NumStoredDiagnosticsInPreamble = StoredDiagnostics.size();
1830 AST->StoredDiagnostics.swap(StoredDiagnostics);
1831 AST->Invocation = CI;
1832 AST->NestedMacroExpansions = NestedMacroExpansions;
1834 // Recover resources if we crash before exiting this method.
1835 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1836 ASTUnitCleanup(AST.get());
1837 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation,
1838 llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> >
1839 CICleanup(CI.getPtr());
1840 llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
1841 llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
1842 DiagCleanup(Diags.getPtr());
1844 return AST->LoadFromCompilerInvocation(PrecompilePreamble) ? 0 : AST.take();
1847 bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) {
1851 SimpleTimer ParsingTimer(WantTiming);
1852 ParsingTimer.setOutput("Reparsing " + getMainFileName());
1855 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1856 PPOpts.DisableStatCache = true;
1857 for (PreprocessorOptions::remapped_file_buffer_iterator
1858 R = PPOpts.remapped_file_buffer_begin(),
1859 REnd = PPOpts.remapped_file_buffer_end();
1864 Invocation->getPreprocessorOpts().clearRemappedFiles();
1865 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
1866 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
1867 if (const llvm::MemoryBuffer *
1868 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
1869 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1872 const char *fname = fileOrBuf.get<const char *>();
1873 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1878 // If we have a preamble file lying around, or if we might try to
1879 // build a precompiled preamble, do so now.
1880 llvm::MemoryBuffer *OverrideMainBuffer = 0;
1881 if (!PreambleFile.empty() || PreambleRebuildCounter > 0)
1882 OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation);
1884 // Clear out the diagnostics state.
1885 if (!OverrideMainBuffer) {
1886 getDiagnostics().Reset();
1887 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1890 // Parse the sources
1891 bool Result = Parse(OverrideMainBuffer);
1893 // If we're caching global code-completion results, and the top-level
1894 // declarations have changed, clear out the code-completion cache.
1895 if (!Result && ShouldCacheCodeCompletionResults &&
1896 CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
1897 CacheCodeCompletionResults();
1902 //----------------------------------------------------------------------------//
1904 //----------------------------------------------------------------------------//
1907 /// \brief Code completion consumer that combines the cached code-completion
1908 /// results from an ASTUnit with the code-completion results provided to it,
1909 /// then passes the result on to
1910 class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
1911 unsigned long long NormalContexts;
1913 CodeCompleteConsumer &Next;
1916 AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
1917 bool IncludeMacros, bool IncludeCodePatterns,
1918 bool IncludeGlobals)
1919 : CodeCompleteConsumer(IncludeMacros, IncludeCodePatterns, IncludeGlobals,
1920 Next.isOutputBinary()), AST(AST), Next(Next)
1922 // Compute the set of contexts in which we will look when we don't have
1923 // any information about the specific context.
1925 = (1LL << (CodeCompletionContext::CCC_TopLevel - 1))
1926 | (1LL << (CodeCompletionContext::CCC_ObjCInterface - 1))
1927 | (1LL << (CodeCompletionContext::CCC_ObjCImplementation - 1))
1928 | (1LL << (CodeCompletionContext::CCC_ObjCIvarList - 1))
1929 | (1LL << (CodeCompletionContext::CCC_Statement - 1))
1930 | (1LL << (CodeCompletionContext::CCC_Expression - 1))
1931 | (1LL << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
1932 | (1LL << (CodeCompletionContext::CCC_DotMemberAccess - 1))
1933 | (1LL << (CodeCompletionContext::CCC_ArrowMemberAccess - 1))
1934 | (1LL << (CodeCompletionContext::CCC_ObjCPropertyAccess - 1))
1935 | (1LL << (CodeCompletionContext::CCC_ObjCProtocolName - 1))
1936 | (1LL << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
1937 | (1LL << (CodeCompletionContext::CCC_Recovery - 1));
1939 if (AST.getASTContext().getLangOptions().CPlusPlus)
1940 NormalContexts |= (1LL << (CodeCompletionContext::CCC_EnumTag - 1))
1941 | (1LL << (CodeCompletionContext::CCC_UnionTag - 1))
1942 | (1LL << (CodeCompletionContext::CCC_ClassOrStructTag - 1));
1945 virtual void ProcessCodeCompleteResults(Sema &S,
1946 CodeCompletionContext Context,
1947 CodeCompletionResult *Results,
1948 unsigned NumResults);
1950 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
1951 OverloadCandidate *Candidates,
1952 unsigned NumCandidates) {
1953 Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
1956 virtual CodeCompletionAllocator &getAllocator() {
1957 return Next.getAllocator();
1962 /// \brief Helper function that computes which global names are hidden by the
1963 /// local code-completion results.
1964 static void CalculateHiddenNames(const CodeCompletionContext &Context,
1965 CodeCompletionResult *Results,
1966 unsigned NumResults,
1968 llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
1969 bool OnlyTagNames = false;
1970 switch (Context.getKind()) {
1971 case CodeCompletionContext::CCC_Recovery:
1972 case CodeCompletionContext::CCC_TopLevel:
1973 case CodeCompletionContext::CCC_ObjCInterface:
1974 case CodeCompletionContext::CCC_ObjCImplementation:
1975 case CodeCompletionContext::CCC_ObjCIvarList:
1976 case CodeCompletionContext::CCC_ClassStructUnion:
1977 case CodeCompletionContext::CCC_Statement:
1978 case CodeCompletionContext::CCC_Expression:
1979 case CodeCompletionContext::CCC_ObjCMessageReceiver:
1980 case CodeCompletionContext::CCC_DotMemberAccess:
1981 case CodeCompletionContext::CCC_ArrowMemberAccess:
1982 case CodeCompletionContext::CCC_ObjCPropertyAccess:
1983 case CodeCompletionContext::CCC_Namespace:
1984 case CodeCompletionContext::CCC_Type:
1985 case CodeCompletionContext::CCC_Name:
1986 case CodeCompletionContext::CCC_PotentiallyQualifiedName:
1987 case CodeCompletionContext::CCC_ParenthesizedExpression:
1988 case CodeCompletionContext::CCC_ObjCSuperclass:
1991 case CodeCompletionContext::CCC_EnumTag:
1992 case CodeCompletionContext::CCC_UnionTag:
1993 case CodeCompletionContext::CCC_ClassOrStructTag:
1994 OnlyTagNames = true;
1997 case CodeCompletionContext::CCC_ObjCProtocolName:
1998 case CodeCompletionContext::CCC_MacroName:
1999 case CodeCompletionContext::CCC_MacroNameUse:
2000 case CodeCompletionContext::CCC_PreprocessorExpression:
2001 case CodeCompletionContext::CCC_PreprocessorDirective:
2002 case CodeCompletionContext::CCC_NaturalLanguage:
2003 case CodeCompletionContext::CCC_SelectorName:
2004 case CodeCompletionContext::CCC_TypeQualifiers:
2005 case CodeCompletionContext::CCC_Other:
2006 case CodeCompletionContext::CCC_OtherWithMacros:
2007 case CodeCompletionContext::CCC_ObjCInstanceMessage:
2008 case CodeCompletionContext::CCC_ObjCClassMessage:
2009 case CodeCompletionContext::CCC_ObjCCategoryName:
2010 // We're looking for nothing, or we're looking for names that cannot
2015 typedef CodeCompletionResult Result;
2016 for (unsigned I = 0; I != NumResults; ++I) {
2017 if (Results[I].Kind != Result::RK_Declaration)
2021 = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
2023 bool Hiding = false;
2025 Hiding = (IDNS & Decl::IDNS_Tag);
2027 unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
2028 Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
2029 Decl::IDNS_NonMemberOperator);
2030 if (Ctx.getLangOptions().CPlusPlus)
2031 HiddenIDNS |= Decl::IDNS_Tag;
2032 Hiding = (IDNS & HiddenIDNS);
2038 DeclarationName Name = Results[I].Declaration->getDeclName();
2039 if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
2040 HiddenNames.insert(Identifier->getName());
2042 HiddenNames.insert(Name.getAsString());
2047 void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
2048 CodeCompletionContext Context,
2049 CodeCompletionResult *Results,
2050 unsigned NumResults) {
2051 // Merge the results we were given with the results we cached.
2052 bool AddedResult = false;
2054 = (Context.getKind() == CodeCompletionContext::CCC_Recovery? NormalContexts
2055 : (1 << (Context.getKind() - 1)));
2057 // Contains the set of names that are hidden by "local" completion results.
2058 llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
2059 typedef CodeCompletionResult Result;
2060 llvm::SmallVector<Result, 8> AllResults;
2061 for (ASTUnit::cached_completion_iterator
2062 C = AST.cached_completion_begin(),
2063 CEnd = AST.cached_completion_end();
2065 // If the context we are in matches any of the contexts we are
2066 // interested in, we'll add this result.
2067 if ((C->ShowInContexts & InContexts) == 0)
2070 // If we haven't added any results previously, do so now.
2072 CalculateHiddenNames(Context, Results, NumResults, S.Context,
2074 AllResults.insert(AllResults.end(), Results, Results + NumResults);
2078 // Determine whether this global completion result is hidden by a local
2079 // completion result. If so, skip it.
2080 if (C->Kind != CXCursor_MacroDefinition &&
2081 HiddenNames.count(C->Completion->getTypedText()))
2084 // Adjust priority based on similar type classes.
2085 unsigned Priority = C->Priority;
2086 CXCursorKind CursorKind = C->Kind;
2087 CodeCompletionString *Completion = C->Completion;
2088 if (!Context.getPreferredType().isNull()) {
2089 if (C->Kind == CXCursor_MacroDefinition) {
2090 Priority = getMacroUsagePriority(C->Completion->getTypedText(),
2092 Context.getPreferredType()->isAnyPointerType());
2093 } else if (C->Type) {
2094 CanQualType Expected
2095 = S.Context.getCanonicalType(
2096 Context.getPreferredType().getUnqualifiedType());
2097 SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
2098 if (ExpectedSTC == C->TypeClass) {
2099 // We know this type is similar; check for an exact match.
2100 llvm::StringMap<unsigned> &CachedCompletionTypes
2101 = AST.getCachedCompletionTypes();
2102 llvm::StringMap<unsigned>::iterator Pos
2103 = CachedCompletionTypes.find(QualType(Expected).getAsString());
2104 if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
2105 Priority /= CCF_ExactTypeMatch;
2107 Priority /= CCF_SimilarTypeMatch;
2112 // Adjust the completion string, if required.
2113 if (C->Kind == CXCursor_MacroDefinition &&
2114 Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
2115 // Create a new code-completion string that just contains the
2116 // macro name, without its arguments.
2117 CodeCompletionBuilder Builder(getAllocator(), CCP_CodePattern,
2119 Builder.AddTypedTextChunk(C->Completion->getTypedText());
2120 CursorKind = CXCursor_NotImplemented;
2121 Priority = CCP_CodePattern;
2122 Completion = Builder.TakeString();
2125 AllResults.push_back(Result(Completion, Priority, CursorKind,
2129 // If we did not add any cached completion results, just forward the
2130 // results we were given to the next consumer.
2132 Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
2136 Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
2142 void ASTUnit::CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column,
2143 RemappedFile *RemappedFiles,
2144 unsigned NumRemappedFiles,
2146 bool IncludeCodePatterns,
2147 CodeCompleteConsumer &Consumer,
2148 Diagnostic &Diag, LangOptions &LangOpts,
2149 SourceManager &SourceMgr, FileManager &FileMgr,
2150 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2151 llvm::SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
2155 SimpleTimer CompletionTimer(WantTiming);
2156 CompletionTimer.setOutput("Code completion @ " + File + ":" +
2157 llvm::Twine(Line) + ":" + llvm::Twine(Column));
2159 llvm::IntrusiveRefCntPtr<CompilerInvocation>
2160 CCInvocation(new CompilerInvocation(*Invocation));
2162 FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2163 PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2165 FrontendOpts.ShowMacrosInCodeCompletion
2166 = IncludeMacros && CachedCompletionResults.empty();
2167 FrontendOpts.ShowCodePatternsInCodeCompletion = IncludeCodePatterns;
2168 FrontendOpts.ShowGlobalSymbolsInCodeCompletion
2169 = CachedCompletionResults.empty();
2170 FrontendOpts.CodeCompletionAt.FileName = File;
2171 FrontendOpts.CodeCompletionAt.Line = Line;
2172 FrontendOpts.CodeCompletionAt.Column = Column;
2174 // Set the language options appropriately.
2175 LangOpts = CCInvocation->getLangOpts();
2177 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
2179 // Recover resources if we crash before exiting this method.
2180 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2181 CICleanup(Clang.get());
2183 Clang->setInvocation(&*CCInvocation);
2184 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
2186 // Set up diagnostics, capturing any diagnostics produced.
2187 Clang->setDiagnostics(&Diag);
2188 ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts());
2189 CaptureDroppedDiagnostics Capture(true,
2190 Clang->getDiagnostics(),
2193 // Create the target instance.
2194 Clang->getTargetOpts().Features = TargetFeatures;
2195 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
2196 Clang->getTargetOpts()));
2197 if (!Clang->hasTarget()) {
2198 Clang->setInvocation(0);
2202 // Inform the target of the language options.
2204 // FIXME: We shouldn't need to do this, the target should be immutable once
2205 // created. This complexity should be lifted elsewhere.
2206 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
2208 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2209 "Invocation must have exactly one source file!");
2210 assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
2211 "FIXME: AST inputs not yet supported here!");
2212 assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
2213 "IR inputs not support here!");
2216 // Use the source and file managers that we were given.
2217 Clang->setFileManager(&FileMgr);
2218 Clang->setSourceManager(&SourceMgr);
2221 PreprocessorOpts.clearRemappedFiles();
2222 PreprocessorOpts.RetainRemappedFileBuffers = true;
2223 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
2224 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
2225 if (const llvm::MemoryBuffer *
2226 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
2227 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, memBuf);
2228 OwnedBuffers.push_back(memBuf);
2230 const char *fname = fileOrBuf.get<const char *>();
2231 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, fname);
2235 // Use the code completion consumer we were given, but adding any cached
2236 // code-completion results.
2237 AugmentedCodeCompleteConsumer *AugmentedConsumer
2238 = new AugmentedCodeCompleteConsumer(*this, Consumer,
2239 FrontendOpts.ShowMacrosInCodeCompletion,
2240 FrontendOpts.ShowCodePatternsInCodeCompletion,
2241 FrontendOpts.ShowGlobalSymbolsInCodeCompletion);
2242 Clang->setCodeCompletionConsumer(AugmentedConsumer);
2244 // If we have a precompiled preamble, try to use it. We only allow
2245 // the use of the precompiled preamble if we're if the completion
2246 // point is within the main file, after the end of the precompiled
2248 llvm::MemoryBuffer *OverrideMainBuffer = 0;
2249 if (!PreambleFile.empty()) {
2250 using llvm::sys::FileStatus;
2251 llvm::sys::PathWithStatus CompleteFilePath(File);
2252 llvm::sys::PathWithStatus MainPath(OriginalSourceFile);
2253 if (const FileStatus *CompleteFileStatus = CompleteFilePath.getFileStatus())
2254 if (const FileStatus *MainStatus = MainPath.getFileStatus())
2255 if (CompleteFileStatus->getUniqueID() == MainStatus->getUniqueID())
2257 = getMainBufferWithPrecompiledPreamble(*CCInvocation, false,
2261 // If the main file has been overridden due to the use of a preamble,
2262 // make that override happen and introduce the preamble.
2263 PreprocessorOpts.DisableStatCache = true;
2264 StoredDiagnostics.insert(StoredDiagnostics.end(),
2265 this->StoredDiagnostics.begin(),
2266 this->StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver);
2267 if (OverrideMainBuffer) {
2268 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
2269 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
2270 PreprocessorOpts.PrecompiledPreambleBytes.second
2271 = PreambleEndsAtStartOfLine;
2272 PreprocessorOpts.ImplicitPCHInclude = PreambleFile;
2273 PreprocessorOpts.DisablePCHValidation = true;
2275 // The stored diagnostics have the old source manager. Copy them
2276 // to our output set of stored diagnostics, updating the source
2277 // manager to the one we were given.
2278 for (unsigned I = NumStoredDiagnosticsFromDriver,
2279 N = this->StoredDiagnostics.size();
2281 StoredDiagnostics.push_back(this->StoredDiagnostics[I]);
2282 FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), SourceMgr);
2283 StoredDiagnostics[I].setLocation(Loc);
2286 OwnedBuffers.push_back(OverrideMainBuffer);
2288 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2289 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2292 // Disable the preprocessing record
2293 PreprocessorOpts.DetailedRecord = false;
2295 llvm::OwningPtr<SyntaxOnlyAction> Act;
2296 Act.reset(new SyntaxOnlyAction);
2297 if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
2298 Clang->getFrontendOpts().Inputs[0].first)) {
2300 Act->EndSourceFile();
2304 CXSaveError ASTUnit::Save(llvm::StringRef File) {
2305 if (getDiagnostics().hasUnrecoverableErrorOccurred())
2306 return CXSaveError_TranslationErrors;
2308 // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2309 // unconditionally create a stat cache when we parse the file?
2310 std::string ErrorInfo;
2311 llvm::raw_fd_ostream Out(File.str().c_str(), ErrorInfo,
2312 llvm::raw_fd_ostream::F_Binary);
2313 if (!ErrorInfo.empty() || Out.has_error())
2314 return CXSaveError_Unknown;
2318 return Out.has_error()? CXSaveError_Unknown : CXSaveError_None;
2321 bool ASTUnit::serialize(llvm::raw_ostream &OS) {
2322 if (getDiagnostics().hasErrorOccurred())
2325 std::vector<unsigned char> Buffer;
2326 llvm::BitstreamWriter Stream(Buffer);
2327 ASTWriter Writer(Stream);
2328 Writer.WriteAST(getSema(), 0, std::string(), 0);
2330 // Write the generated bitstream to "Out".
2331 if (!Buffer.empty())
2332 OS.write((char *)&Buffer.front(), Buffer.size());