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 NestedMacroInstantiations(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 // Deal with tag names.
187 if (isa<EnumDecl>(ND)) {
188 Contexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1));
190 // Part of the nested-name-specifier in C++0x.
191 if (LangOpts.CPlusPlus0x)
192 IsNestedNameSpecifier = true;
193 } else if (RecordDecl *Record = dyn_cast<RecordDecl>(ND)) {
194 if (Record->isUnion())
195 Contexts |= (1 << (CodeCompletionContext::CCC_UnionTag - 1));
197 Contexts |= (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1));
199 if (LangOpts.CPlusPlus)
200 IsNestedNameSpecifier = true;
201 } else if (isa<ClassTemplateDecl>(ND))
202 IsNestedNameSpecifier = true;
203 } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) {
204 // Values can appear in these contexts.
205 Contexts = (1 << (CodeCompletionContext::CCC_Statement - 1))
206 | (1 << (CodeCompletionContext::CCC_Expression - 1))
207 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
208 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1));
209 } else if (isa<ObjCProtocolDecl>(ND)) {
210 Contexts = (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1));
211 } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
212 Contexts = (1 << (CodeCompletionContext::CCC_Namespace - 1));
214 // Part of the nested-name-specifier.
215 IsNestedNameSpecifier = true;
221 void ASTUnit::CacheCodeCompletionResults() {
225 SimpleTimer Timer(WantTiming);
226 Timer.setOutput("Cache global code completions for " + getMainFileName());
228 // Clear out the previous results.
229 ClearCachedCompletionResults();
231 // Gather the set of global code completions.
232 typedef CodeCompletionResult Result;
233 llvm::SmallVector<Result, 8> Results;
234 CachedCompletionAllocator = new GlobalCodeCompletionAllocator;
235 TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator, Results);
237 // Translate global code completions into cached completions.
238 llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
240 for (unsigned I = 0, N = Results.size(); I != N; ++I) {
241 switch (Results[I].Kind) {
242 case Result::RK_Declaration: {
243 bool IsNestedNameSpecifier = false;
244 CachedCodeCompletionResult CachedResult;
245 CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema,
246 *CachedCompletionAllocator);
247 CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration,
248 Ctx->getLangOptions(),
249 IsNestedNameSpecifier);
250 CachedResult.Priority = Results[I].Priority;
251 CachedResult.Kind = Results[I].CursorKind;
252 CachedResult.Availability = Results[I].Availability;
254 // Keep track of the type of this completion in an ASTContext-agnostic
256 QualType UsageType = getDeclUsageType(*Ctx, Results[I].Declaration);
257 if (UsageType.isNull()) {
258 CachedResult.TypeClass = STC_Void;
259 CachedResult.Type = 0;
261 CanQualType CanUsageType
262 = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
263 CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
265 // Determine whether we have already seen this type. If so, we save
266 // ourselves the work of formatting the type string by using the
267 // temporary, CanQualType-based hash table to find the associated value.
268 unsigned &TypeValue = CompletionTypes[CanUsageType];
269 if (TypeValue == 0) {
270 TypeValue = CompletionTypes.size();
271 CachedCompletionTypes[QualType(CanUsageType).getAsString()]
275 CachedResult.Type = TypeValue;
278 CachedCompletionResults.push_back(CachedResult);
280 /// Handle nested-name-specifiers in C++.
281 if (TheSema->Context.getLangOptions().CPlusPlus &&
282 IsNestedNameSpecifier && !Results[I].StartsNestedNameSpecifier) {
283 // The contexts in which a nested-name-specifier can appear in C++.
285 = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
286 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
287 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
288 | (1 << (CodeCompletionContext::CCC_Statement - 1))
289 | (1 << (CodeCompletionContext::CCC_Expression - 1))
290 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
291 | (1 << (CodeCompletionContext::CCC_EnumTag - 1))
292 | (1 << (CodeCompletionContext::CCC_UnionTag - 1))
293 | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1))
294 | (1 << (CodeCompletionContext::CCC_Type - 1))
295 | (1 << (CodeCompletionContext::CCC_PotentiallyQualifiedName - 1))
296 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1));
298 if (isa<NamespaceDecl>(Results[I].Declaration) ||
299 isa<NamespaceAliasDecl>(Results[I].Declaration))
300 NNSContexts |= (1 << (CodeCompletionContext::CCC_Namespace - 1));
302 if (unsigned RemainingContexts
303 = NNSContexts & ~CachedResult.ShowInContexts) {
304 // If there any contexts where this completion can be a
305 // nested-name-specifier but isn't already an option, create a
306 // nested-name-specifier completion.
307 Results[I].StartsNestedNameSpecifier = true;
308 CachedResult.Completion
309 = Results[I].CreateCodeCompletionString(*TheSema,
310 *CachedCompletionAllocator);
311 CachedResult.ShowInContexts = RemainingContexts;
312 CachedResult.Priority = CCP_NestedNameSpecifier;
313 CachedResult.TypeClass = STC_Void;
314 CachedResult.Type = 0;
315 CachedCompletionResults.push_back(CachedResult);
321 case Result::RK_Keyword:
322 case Result::RK_Pattern:
323 // Ignore keywords and patterns; we don't care, since they are so
324 // easily regenerated.
327 case Result::RK_Macro: {
328 CachedCodeCompletionResult CachedResult;
329 CachedResult.Completion
330 = Results[I].CreateCodeCompletionString(*TheSema,
331 *CachedCompletionAllocator);
332 CachedResult.ShowInContexts
333 = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
334 | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1))
335 | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1))
336 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
337 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
338 | (1 << (CodeCompletionContext::CCC_Statement - 1))
339 | (1 << (CodeCompletionContext::CCC_Expression - 1))
340 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
341 | (1 << (CodeCompletionContext::CCC_MacroNameUse - 1))
342 | (1 << (CodeCompletionContext::CCC_PreprocessorExpression - 1))
343 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
344 | (1 << (CodeCompletionContext::CCC_OtherWithMacros - 1));
346 CachedResult.Priority = Results[I].Priority;
347 CachedResult.Kind = Results[I].CursorKind;
348 CachedResult.Availability = Results[I].Availability;
349 CachedResult.TypeClass = STC_Void;
350 CachedResult.Type = 0;
351 CachedCompletionResults.push_back(CachedResult);
357 // Save the current top-level hash value.
358 CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
361 void ASTUnit::ClearCachedCompletionResults() {
362 CachedCompletionResults.clear();
363 CachedCompletionTypes.clear();
364 CachedCompletionAllocator = 0;
369 /// \brief Gathers information from ASTReader that will be used to initialize
371 class ASTInfoCollector : public ASTReaderListener {
372 LangOptions &LangOpt;
374 std::string &TargetTriple;
375 std::string &Predefines;
378 unsigned NumHeaderInfos;
381 ASTInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
382 std::string &TargetTriple, std::string &Predefines,
384 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
385 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
387 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
392 virtual bool ReadTargetTriple(llvm::StringRef Triple) {
393 TargetTriple = Triple;
397 virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
398 llvm::StringRef OriginalFileName,
399 std::string &SuggestedPredefines,
400 FileManager &FileMgr) {
401 Predefines = Buffers[0].Data;
402 for (unsigned I = 1, N = Buffers.size(); I != N; ++I) {
403 Predefines += Buffers[I].Data;
408 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {
409 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
412 virtual void ReadCounter(unsigned Value) {
417 class StoredDiagnosticClient : public DiagnosticClient {
418 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags;
421 explicit StoredDiagnosticClient(
422 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
423 : StoredDiags(StoredDiags) { }
425 virtual void HandleDiagnostic(Diagnostic::Level Level,
426 const DiagnosticInfo &Info);
429 /// \brief RAII object that optionally captures diagnostics, if
430 /// there is no diagnostic client to capture them already.
431 class CaptureDroppedDiagnostics {
433 StoredDiagnosticClient Client;
434 DiagnosticClient *PreviousClient;
437 CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags,
438 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
439 : Diags(Diags), Client(StoredDiags), PreviousClient(0)
441 if (RequestCapture || Diags.getClient() == 0) {
442 PreviousClient = Diags.takeClient();
443 Diags.setClient(&Client);
447 ~CaptureDroppedDiagnostics() {
448 if (Diags.getClient() == &Client) {
450 Diags.setClient(PreviousClient);
455 } // anonymous namespace
457 void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level,
458 const DiagnosticInfo &Info) {
459 // Default implementation (Warnings/errors count).
460 DiagnosticClient::HandleDiagnostic(Level, Info);
462 StoredDiags.push_back(StoredDiagnostic(Level, Info));
465 const std::string &ASTUnit::getOriginalSourceFileName() {
466 return OriginalSourceFile;
469 const std::string &ASTUnit::getASTFileName() {
470 assert(isMainFileAST() && "Not an ASTUnit from an AST file!");
471 return static_cast<ASTReader *>(Ctx->getExternalSource())->getFileName();
474 llvm::MemoryBuffer *ASTUnit::getBufferForFile(llvm::StringRef Filename,
475 std::string *ErrorStr) {
477 return FileMgr->getBufferForFile(Filename, ErrorStr);
480 /// \brief Configure the diagnostics object for use with ASTUnit.
481 void ASTUnit::ConfigureDiags(llvm::IntrusiveRefCntPtr<Diagnostic> &Diags,
482 const char **ArgBegin, const char **ArgEnd,
483 ASTUnit &AST, bool CaptureDiagnostics) {
484 if (!Diags.getPtr()) {
485 // No diagnostics engine was provided, so create our own diagnostics object
486 // with the default options.
487 DiagnosticOptions DiagOpts;
488 DiagnosticClient *Client = 0;
489 if (CaptureDiagnostics)
490 Client = new StoredDiagnosticClient(AST.StoredDiagnostics);
491 Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd- ArgBegin,
493 } else if (CaptureDiagnostics) {
494 Diags->setClient(new StoredDiagnosticClient(AST.StoredDiagnostics));
498 ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename,
499 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
500 const FileSystemOptions &FileSystemOpts,
502 RemappedFile *RemappedFiles,
503 unsigned NumRemappedFiles,
504 bool CaptureDiagnostics) {
505 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
507 // Recover resources if we crash before exiting this method.
508 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
509 ASTUnitCleanup(AST.get());
510 llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
511 llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
512 DiagCleanup(Diags.getPtr());
514 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
516 AST->OnlyLocalDecls = OnlyLocalDecls;
517 AST->CaptureDiagnostics = CaptureDiagnostics;
518 AST->Diagnostics = Diags;
519 AST->FileMgr = new FileManager(FileSystemOpts);
520 AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
521 AST->getFileManager());
522 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
524 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
525 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
526 if (const llvm::MemoryBuffer *
527 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
528 // Create the file entry for the file that we're mapping from.
529 const FileEntry *FromFile
530 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
531 memBuf->getBufferSize(),
534 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
535 << RemappedFiles[I].first;
540 // Override the contents of the "from" file with the contents of
542 AST->getSourceManager().overrideFileContents(FromFile, memBuf);
545 const char *fname = fileOrBuf.get<const char *>();
546 const FileEntry *ToFile = AST->FileMgr->getFile(fname);
548 AST->getDiagnostics().Report(diag::err_fe_remap_missing_to_file)
549 << RemappedFiles[I].first << fname;
553 // Create the file entry for the file that we're mapping from.
554 const FileEntry *FromFile
555 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
559 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
560 << RemappedFiles[I].first;
565 // Override the contents of the "from" file with the contents of
567 AST->getSourceManager().overrideFileContents(FromFile, ToFile);
571 // Gather Info for preprocessor construction later on.
573 LangOptions LangInfo;
574 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
575 std::string TargetTriple;
576 std::string Predefines;
579 llvm::OwningPtr<ASTReader> Reader;
581 Reader.reset(new ASTReader(AST->getSourceManager(), AST->getFileManager(),
582 AST->getDiagnostics()));
584 // Recover resources if we crash before exiting this method.
585 llvm::CrashRecoveryContextCleanupRegistrar<ASTReader>
586 ReaderCleanup(Reader.get());
588 Reader->setListener(new ASTInfoCollector(LangInfo, HeaderInfo, TargetTriple,
589 Predefines, Counter));
591 switch (Reader->ReadAST(Filename, ASTReader::MainFile)) {
592 case ASTReader::Success:
595 case ASTReader::Failure:
596 case ASTReader::IgnorePCH:
597 AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
601 AST->OriginalSourceFile = Reader->getOriginalSourceFile();
603 // AST file loaded successfully. Now create the preprocessor.
605 // Get information about the target being compiled for.
607 // FIXME: This is broken, we should store the TargetOptions in the AST file.
608 TargetOptions TargetOpts;
610 TargetOpts.CXXABI = "";
612 TargetOpts.Features.clear();
613 TargetOpts.Triple = TargetTriple;
614 AST->Target = TargetInfo::CreateTargetInfo(AST->getDiagnostics(),
616 AST->PP = new Preprocessor(AST->getDiagnostics(), LangInfo, *AST->Target,
617 AST->getSourceManager(), HeaderInfo);
618 Preprocessor &PP = *AST->PP;
620 PP.setPredefines(Reader->getSuggestedPredefines());
621 PP.setCounterValue(Counter);
622 Reader->setPreprocessor(PP);
624 // Create and initialize the ASTContext.
626 AST->Ctx = new ASTContext(LangInfo,
627 AST->getSourceManager(),
629 PP.getIdentifierTable(),
630 PP.getSelectorTable(),
632 /* size_reserve = */0);
633 ASTContext &Context = *AST->Ctx;
635 Reader->InitializeContext(Context);
637 // Attach the AST reader to the AST context as an external AST
638 // source, so that declarations will be deserialized from the
639 // AST file as needed.
640 ASTReader *ReaderPtr = Reader.get();
641 llvm::OwningPtr<ExternalASTSource> Source(Reader.take());
643 // Unregister the cleanup for ASTReader. It will get cleaned up
644 // by the ASTUnit cleanup.
645 ReaderCleanup.unregister();
647 Context.setExternalSource(Source);
649 // Create an AST consumer, even though it isn't used.
650 AST->Consumer.reset(new ASTConsumer);
652 // Create a semantic analysis object and tell the AST reader about it.
653 AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer));
654 AST->TheSema->Initialize();
655 ReaderPtr->InitializeSema(*AST->TheSema);
662 /// \brief Preprocessor callback class that updates a hash value with the names
663 /// of all macros that have been defined by the translation unit.
664 class MacroDefinitionTrackerPPCallbacks : public PPCallbacks {
668 explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { }
670 virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
671 Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash);
675 /// \brief Add the given declaration to the hash of all top-level entities.
676 void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) {
680 DeclContext *DC = D->getDeclContext();
684 if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit()))
687 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
688 if (ND->getIdentifier())
689 Hash = llvm::HashString(ND->getIdentifier()->getName(), Hash);
690 else if (DeclarationName Name = ND->getDeclName()) {
691 std::string NameStr = Name.getAsString();
692 Hash = llvm::HashString(NameStr, Hash);
697 if (ObjCForwardProtocolDecl *Forward
698 = dyn_cast<ObjCForwardProtocolDecl>(D)) {
699 for (ObjCForwardProtocolDecl::protocol_iterator
700 P = Forward->protocol_begin(),
701 PEnd = Forward->protocol_end();
703 AddTopLevelDeclarationToHash(*P, Hash);
707 if (ObjCClassDecl *Class = llvm::dyn_cast<ObjCClassDecl>(D)) {
708 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
710 AddTopLevelDeclarationToHash(I->getInterface(), Hash);
715 class TopLevelDeclTrackerConsumer : public ASTConsumer {
720 TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
721 : Unit(_Unit), Hash(Hash) {
725 void HandleTopLevelDecl(DeclGroupRef D) {
726 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
728 // FIXME: Currently ObjC method declarations are incorrectly being
729 // reported as top-level declarations, even though their DeclContext
730 // is the containing ObjC @interface/@implementation. This is a
731 // fundamental problem in the parser right now.
732 if (isa<ObjCMethodDecl>(D))
735 AddTopLevelDeclarationToHash(D, Hash);
736 Unit.addTopLevelDecl(D);
740 // We're not interested in "interesting" decls.
741 void HandleInterestingDecl(DeclGroupRef) {}
744 class TopLevelDeclTrackerAction : public ASTFrontendAction {
748 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
749 llvm::StringRef InFile) {
750 CI.getPreprocessor().addPPCallbacks(
751 new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
752 return new TopLevelDeclTrackerConsumer(Unit,
753 Unit.getCurrentTopLevelHashValue());
757 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
759 virtual bool hasCodeCompletionSupport() const { return false; }
760 virtual bool usesCompleteTranslationUnit() {
761 return Unit.isCompleteTranslationUnit();
765 class PrecompilePreambleConsumer : public PCHGenerator,
766 public ASTSerializationListener {
769 std::vector<Decl *> TopLevelDecls;
772 PrecompilePreambleConsumer(ASTUnit &Unit,
773 const Preprocessor &PP, bool Chaining,
774 const char *isysroot, llvm::raw_ostream *Out)
775 : PCHGenerator(PP, "", Chaining, isysroot, Out), Unit(Unit),
776 Hash(Unit.getCurrentTopLevelHashValue()) {
780 virtual void HandleTopLevelDecl(DeclGroupRef D) {
781 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
783 // FIXME: Currently ObjC method declarations are incorrectly being
784 // reported as top-level declarations, even though their DeclContext
785 // is the containing ObjC @interface/@implementation. This is a
786 // fundamental problem in the parser right now.
787 if (isa<ObjCMethodDecl>(D))
789 AddTopLevelDeclarationToHash(D, Hash);
790 TopLevelDecls.push_back(D);
794 virtual void HandleTranslationUnit(ASTContext &Ctx) {
795 PCHGenerator::HandleTranslationUnit(Ctx);
796 if (!Unit.getDiagnostics().hasErrorOccurred()) {
797 // Translate the top-level declarations we captured during
798 // parsing into declaration IDs in the precompiled
799 // preamble. This will allow us to deserialize those top-level
800 // declarations when requested.
801 for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I)
802 Unit.addTopLevelDeclFromPreamble(
803 getWriter().getDeclID(TopLevelDecls[I]));
807 virtual void SerializedPreprocessedEntity(PreprocessedEntity *Entity,
809 Unit.addPreprocessedEntityFromPreamble(Offset);
812 virtual ASTSerializationListener *GetASTSerializationListener() {
817 class PrecompilePreambleAction : public ASTFrontendAction {
821 explicit PrecompilePreambleAction(ASTUnit &Unit) : Unit(Unit) {}
823 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
824 llvm::StringRef InFile) {
826 std::string OutputFile;
827 llvm::raw_ostream *OS = 0;
829 if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot,
834 const char *isysroot = CI.getFrontendOpts().RelocatablePCH ?
836 CI.getPreprocessor().addPPCallbacks(
837 new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
838 return new PrecompilePreambleConsumer(Unit, CI.getPreprocessor(), Chaining,
842 virtual bool hasCodeCompletionSupport() const { return false; }
843 virtual bool hasASTFileSupport() const { return false; }
844 virtual bool usesCompleteTranslationUnit() { return false; }
849 /// Parse the source file into a translation unit using the given compiler
850 /// invocation, replacing the current translation unit.
852 /// \returns True if a failure occurred that causes the ASTUnit not to
853 /// contain any translation-unit information, false otherwise.
854 bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) {
855 delete SavedMainFileBuffer;
856 SavedMainFileBuffer = 0;
859 delete OverrideMainBuffer;
863 // Create the compiler instance to use for building the AST.
864 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
866 // Recover resources if we crash before exiting this method.
867 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
868 CICleanup(Clang.get());
870 Clang->setInvocation(&*Invocation);
871 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
873 // Set up diagnostics, capturing any diagnostics that would
874 // otherwise be dropped.
875 Clang->setDiagnostics(&getDiagnostics());
877 // Create the target instance.
878 Clang->getTargetOpts().Features = TargetFeatures;
879 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
880 Clang->getTargetOpts()));
881 if (!Clang->hasTarget()) {
882 delete OverrideMainBuffer;
886 // Inform the target of the language options.
888 // FIXME: We shouldn't need to do this, the target should be immutable once
889 // created. This complexity should be lifted elsewhere.
890 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
892 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
893 "Invocation must have exactly one source file!");
894 assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
895 "FIXME: AST inputs not yet supported here!");
896 assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
897 "IR inputs not support here!");
899 // Configure the various subsystems.
900 // FIXME: Should we retain the previous file manager?
901 FileSystemOpts = Clang->getFileSystemOpts();
902 FileMgr = new FileManager(FileSystemOpts);
903 SourceMgr = new SourceManager(getDiagnostics(), *FileMgr);
908 // Clear out old caches and data.
909 TopLevelDecls.clear();
910 PreprocessedEntities.clear();
911 CleanTemporaryFiles();
912 PreprocessedEntitiesByFile.clear();
914 if (!OverrideMainBuffer) {
915 StoredDiagnostics.erase(
916 StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver,
917 StoredDiagnostics.end());
918 TopLevelDeclsInPreamble.clear();
919 PreprocessedEntitiesInPreamble.clear();
922 // Create a file manager object to provide access to and cache the filesystem.
923 Clang->setFileManager(&getFileManager());
925 // Create the source manager.
926 Clang->setSourceManager(&getSourceManager());
928 // If the main file has been overridden due to the use of a preamble,
929 // make that override happen and introduce the preamble.
930 PreprocessorOptions &PreprocessorOpts = Clang->getPreprocessorOpts();
931 PreprocessorOpts.DetailedRecordIncludesNestedMacroInstantiations
932 = NestedMacroInstantiations;
933 std::string PriorImplicitPCHInclude;
934 if (OverrideMainBuffer) {
935 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
936 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
937 PreprocessorOpts.PrecompiledPreambleBytes.second
938 = PreambleEndsAtStartOfLine;
939 PriorImplicitPCHInclude = PreprocessorOpts.ImplicitPCHInclude;
940 PreprocessorOpts.ImplicitPCHInclude = PreambleFile;
941 PreprocessorOpts.DisablePCHValidation = true;
943 // The stored diagnostic has the old source manager in it; update
944 // the locations to refer into the new source manager. Since we've
945 // been careful to make sure that the source manager's state
946 // before and after are identical, so that we can reuse the source
948 for (unsigned I = NumStoredDiagnosticsFromDriver,
949 N = StoredDiagnostics.size();
951 FullSourceLoc Loc(StoredDiagnostics[I].getLocation(),
953 StoredDiagnostics[I].setLocation(Loc);
956 // Keep track of the override buffer;
957 SavedMainFileBuffer = OverrideMainBuffer;
959 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
960 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
963 llvm::OwningPtr<TopLevelDeclTrackerAction> Act(
964 new TopLevelDeclTrackerAction(*this));
966 // Recover resources if we crash before exiting this method.
967 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
968 ActCleanup(Act.get());
970 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
971 Clang->getFrontendOpts().Inputs[0].first))
976 // Steal the created target, context, and preprocessor.
977 TheSema.reset(Clang->takeSema());
978 Consumer.reset(Clang->takeASTConsumer());
979 Ctx = &Clang->getASTContext();
980 PP = &Clang->getPreprocessor();
981 Clang->setSourceManager(0);
982 Clang->setFileManager(0);
983 Target = &Clang->getTarget();
985 Act->EndSourceFile();
987 // Remove the overridden buffer we used for the preamble.
988 if (OverrideMainBuffer) {
989 PreprocessorOpts.eraseRemappedFile(
990 PreprocessorOpts.remapped_file_buffer_end() - 1);
991 PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude;
997 // Remove the overridden buffer we used for the preamble.
998 if (OverrideMainBuffer) {
999 PreprocessorOpts.eraseRemappedFile(
1000 PreprocessorOpts.remapped_file_buffer_end() - 1);
1001 PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude;
1002 delete OverrideMainBuffer;
1003 SavedMainFileBuffer = 0;
1006 StoredDiagnostics.clear();
1010 /// \brief Simple function to retrieve a path for a preamble precompiled header.
1011 static std::string GetPreamblePCHPath() {
1012 // FIXME: This is lame; sys::Path should provide this function (in particular,
1013 // it should know how to find the temporary files dir).
1014 // FIXME: This is really lame. I copied this code from the Driver!
1015 // FIXME: This is a hack so that we can override the preamble file during
1016 // crash-recovery testing, which is the only case where the preamble files
1017 // are not necessarily cleaned up.
1018 const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
1023 const char *TmpDir = ::getenv("TMPDIR");
1025 TmpDir = ::getenv("TEMP");
1027 TmpDir = ::getenv("TMP");
1028 #ifdef LLVM_ON_WIN32
1030 TmpDir = ::getenv("USERPROFILE");
1034 llvm::sys::Path P(TmpDir);
1035 P.createDirectoryOnDisk(true);
1036 P.appendComponent("preamble");
1037 P.appendSuffix("pch");
1038 if (P.createTemporaryFileOnDisk())
1039 return std::string();
1044 /// \brief Compute the preamble for the main file, providing the source buffer
1045 /// that corresponds to the main file along with a pair (bytes, start-of-line)
1046 /// that describes the preamble.
1047 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
1048 ASTUnit::ComputePreamble(CompilerInvocation &Invocation,
1049 unsigned MaxLines, bool &CreatedBuffer) {
1050 FrontendOptions &FrontendOpts = Invocation.getFrontendOpts();
1051 PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts();
1052 CreatedBuffer = false;
1054 // Try to determine if the main file has been remapped, either from the
1055 // command line (to another file) or directly through the compiler invocation
1056 // (to a memory buffer).
1057 llvm::MemoryBuffer *Buffer = 0;
1058 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second);
1059 if (const llvm::sys::FileStatus *MainFileStatus = MainFilePath.getFileStatus()) {
1060 // Check whether there is a file-file remapping of the main file
1061 for (PreprocessorOptions::remapped_file_iterator
1062 M = PreprocessorOpts.remapped_file_begin(),
1063 E = PreprocessorOpts.remapped_file_end();
1066 llvm::sys::PathWithStatus MPath(M->first);
1067 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1068 if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1069 // We found a remapping. Try to load the resulting, remapped source.
1070 if (CreatedBuffer) {
1072 CreatedBuffer = false;
1075 Buffer = getBufferForFile(M->second);
1077 return std::make_pair((llvm::MemoryBuffer*)0,
1078 std::make_pair(0, true));
1079 CreatedBuffer = true;
1084 // Check whether there is a file-buffer remapping. It supercedes the
1085 // file-file remapping.
1086 for (PreprocessorOptions::remapped_file_buffer_iterator
1087 M = PreprocessorOpts.remapped_file_buffer_begin(),
1088 E = PreprocessorOpts.remapped_file_buffer_end();
1091 llvm::sys::PathWithStatus MPath(M->first);
1092 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1093 if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1094 // We found a remapping.
1095 if (CreatedBuffer) {
1097 CreatedBuffer = false;
1100 Buffer = const_cast<llvm::MemoryBuffer *>(M->second);
1106 // If the main source file was not remapped, load it now.
1108 Buffer = getBufferForFile(FrontendOpts.Inputs[0].second);
1110 return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true));
1112 CreatedBuffer = true;
1115 return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer, MaxLines));
1118 static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old,
1120 llvm::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 CompilerInvocation PreambleInvocation,
1155 unsigned MaxLines) {
1156 FrontendOptions &FrontendOpts = PreambleInvocation.getFrontendOpts();
1157 PreprocessorOptions &PreprocessorOpts
1158 = PreambleInvocation.getPreprocessorOpts();
1160 bool CreatedPreambleBuffer = false;
1161 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble
1162 = ComputePreamble(PreambleInvocation, MaxLines, CreatedPreambleBuffer);
1164 // If ComputePreamble() Take ownership of the
1165 llvm::OwningPtr<llvm::MemoryBuffer> OwnedPreambleBuffer;
1166 if (CreatedPreambleBuffer)
1167 OwnedPreambleBuffer.reset(NewPreamble.first);
1169 if (!NewPreamble.second.first) {
1170 // We couldn't find a preamble in the main source. Clear out the current
1171 // preamble, if we have one. It's obviously no good any more.
1173 if (!PreambleFile.empty()) {
1174 llvm::sys::Path(PreambleFile).eraseFromDisk();
1175 PreambleFile.clear();
1178 // The next time we actually see a preamble, precompile it.
1179 PreambleRebuildCounter = 1;
1183 if (!Preamble.empty()) {
1184 // We've previously computed a preamble. Check whether we have the same
1185 // preamble now that we did before, and that there's enough space in
1186 // the main-file buffer within the precompiled preamble to fit the
1188 if (Preamble.size() == NewPreamble.second.first &&
1189 PreambleEndsAtStartOfLine == NewPreamble.second.second &&
1190 NewPreamble.first->getBufferSize() < PreambleReservedSize-2 &&
1191 memcmp(&Preamble[0], NewPreamble.first->getBufferStart(),
1192 NewPreamble.second.first) == 0) {
1193 // The preamble has not changed. We may be able to re-use the precompiled
1196 // Check that none of the files used by the preamble have changed.
1197 bool AnyFileChanged = false;
1199 // First, make a record of those files that have been overridden via
1200 // remapping or unsaved_files.
1201 llvm::StringMap<std::pair<off_t, time_t> > OverriddenFiles;
1202 for (PreprocessorOptions::remapped_file_iterator
1203 R = PreprocessorOpts.remapped_file_begin(),
1204 REnd = PreprocessorOpts.remapped_file_end();
1205 !AnyFileChanged && R != REnd;
1207 struct stat StatBuf;
1208 if (FileMgr->getNoncachedStatValue(R->second, StatBuf)) {
1209 // If we can't stat the file we're remapping to, assume that something
1210 // horrible happened.
1211 AnyFileChanged = true;
1215 OverriddenFiles[R->first] = std::make_pair(StatBuf.st_size,
1218 for (PreprocessorOptions::remapped_file_buffer_iterator
1219 R = PreprocessorOpts.remapped_file_buffer_begin(),
1220 REnd = PreprocessorOpts.remapped_file_buffer_end();
1221 !AnyFileChanged && R != REnd;
1223 // FIXME: Should we actually compare the contents of file->buffer
1225 OverriddenFiles[R->first] = std::make_pair(R->second->getBufferSize(),
1229 // Check whether anything has changed.
1230 for (llvm::StringMap<std::pair<off_t, time_t> >::iterator
1231 F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end();
1232 !AnyFileChanged && F != FEnd;
1234 llvm::StringMap<std::pair<off_t, time_t> >::iterator Overridden
1235 = OverriddenFiles.find(F->first());
1236 if (Overridden != OverriddenFiles.end()) {
1237 // This file was remapped; check whether the newly-mapped file
1238 // matches up with the previous mapping.
1239 if (Overridden->second != F->second)
1240 AnyFileChanged = true;
1244 // The file was not remapped; check whether it has changed on disk.
1245 struct stat StatBuf;
1246 if (FileMgr->getNoncachedStatValue(F->first(), StatBuf)) {
1247 // If we can't stat the file, assume that something horrible happened.
1248 AnyFileChanged = true;
1249 } else if (StatBuf.st_size != F->second.first ||
1250 StatBuf.st_mtime != F->second.second)
1251 AnyFileChanged = true;
1254 if (!AnyFileChanged) {
1255 // Okay! We can re-use the precompiled preamble.
1257 // Set the state of the diagnostic object to mimic its state
1258 // after parsing the preamble.
1259 // FIXME: This won't catch any #pragma push warning changes that
1260 // have occurred in the preamble.
1261 getDiagnostics().Reset();
1262 ProcessWarningOptions(getDiagnostics(),
1263 PreambleInvocation.getDiagnosticOpts());
1264 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1265 if (StoredDiagnostics.size() > NumStoredDiagnosticsInPreamble)
1266 StoredDiagnostics.erase(
1267 StoredDiagnostics.begin() + NumStoredDiagnosticsInPreamble,
1268 StoredDiagnostics.end());
1270 // Create a version of the main file buffer that is padded to
1271 // buffer size we reserved when creating the preamble.
1272 return CreatePaddedMainFileBuffer(NewPreamble.first,
1273 PreambleReservedSize,
1274 FrontendOpts.Inputs[0].second);
1278 // If we aren't allowed to rebuild the precompiled preamble, just
1283 // We can't reuse the previously-computed preamble. Build a new one.
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 Preamble.assign(NewPreamble.first->getBufferStart(),
1327 NewPreamble.first->getBufferStart()
1328 + NewPreamble.second.first);
1329 PreambleEndsAtStartOfLine = NewPreamble.second.second;
1331 delete PreambleBuffer;
1333 = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize,
1334 FrontendOpts.Inputs[0].second);
1335 memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()),
1336 NewPreamble.first->getBufferStart(), Preamble.size());
1337 memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(),
1338 ' ', PreambleReservedSize - Preamble.size() - 1);
1339 const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n';
1341 // Remap the main source file to the preamble buffer.
1342 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second);
1343 PreprocessorOpts.addRemappedFile(MainFilePath.str(), PreambleBuffer);
1345 // Tell the compiler invocation to generate a temporary precompiled header.
1346 FrontendOpts.ProgramAction = frontend::GeneratePCH;
1347 FrontendOpts.ChainedPCH = true;
1348 // FIXME: Generate the precompiled header into memory?
1349 FrontendOpts.OutputFile = PreamblePCHPath;
1350 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
1351 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
1353 // Create the compiler instance to use for building the precompiled preamble.
1354 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1356 // Recover resources if we crash before exiting this method.
1357 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1358 CICleanup(Clang.get());
1360 Clang->setInvocation(&PreambleInvocation);
1361 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
1363 // Set up diagnostics, capturing all of the diagnostics produced.
1364 Clang->setDiagnostics(&getDiagnostics());
1366 // Create the target instance.
1367 Clang->getTargetOpts().Features = TargetFeatures;
1368 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1369 Clang->getTargetOpts()));
1370 if (!Clang->hasTarget()) {
1371 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1373 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1374 PreprocessorOpts.eraseRemappedFile(
1375 PreprocessorOpts.remapped_file_buffer_end() - 1);
1379 // Inform the target of the language options.
1381 // FIXME: We shouldn't need to do this, the target should be immutable once
1382 // created. This complexity should be lifted elsewhere.
1383 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1385 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1386 "Invocation must have exactly one source file!");
1387 assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
1388 "FIXME: AST inputs not yet supported here!");
1389 assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
1390 "IR inputs not support here!");
1392 // Clear out old caches and data.
1393 getDiagnostics().Reset();
1394 ProcessWarningOptions(getDiagnostics(), Clang->getDiagnosticOpts());
1395 StoredDiagnostics.erase(
1396 StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver,
1397 StoredDiagnostics.end());
1398 TopLevelDecls.clear();
1399 TopLevelDeclsInPreamble.clear();
1400 PreprocessedEntities.clear();
1401 PreprocessedEntitiesInPreamble.clear();
1403 // Create a file manager object to provide access to and cache the filesystem.
1404 Clang->setFileManager(new FileManager(Clang->getFileSystemOpts()));
1406 // Create the source manager.
1407 Clang->setSourceManager(new SourceManager(getDiagnostics(),
1408 Clang->getFileManager()));
1410 llvm::OwningPtr<PrecompilePreambleAction> Act;
1411 Act.reset(new PrecompilePreambleAction(*this));
1412 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
1413 Clang->getFrontendOpts().Inputs[0].first)) {
1414 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1416 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1417 PreprocessorOpts.eraseRemappedFile(
1418 PreprocessorOpts.remapped_file_buffer_end() - 1);
1423 Act->EndSourceFile();
1425 if (Diagnostics->hasErrorOccurred()) {
1426 // There were errors parsing the preamble, so no precompiled header was
1427 // generated. Forget that we even tried.
1428 // FIXME: Should we leave a note for ourselves to try again?
1429 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1431 TopLevelDeclsInPreamble.clear();
1432 PreprocessedEntities.clear();
1433 PreprocessedEntitiesInPreamble.clear();
1434 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1435 PreprocessorOpts.eraseRemappedFile(
1436 PreprocessorOpts.remapped_file_buffer_end() - 1);
1440 // Keep track of the preamble we precompiled.
1441 PreambleFile = FrontendOpts.OutputFile;
1442 NumStoredDiagnosticsInPreamble = StoredDiagnostics.size();
1443 NumWarningsInPreamble = getDiagnostics().getNumWarnings();
1445 // Keep track of all of the files that the source manager knows about,
1446 // so we can verify whether they have changed or not.
1447 FilesInPreamble.clear();
1448 SourceManager &SourceMgr = Clang->getSourceManager();
1449 const llvm::MemoryBuffer *MainFileBuffer
1450 = SourceMgr.getBuffer(SourceMgr.getMainFileID());
1451 for (SourceManager::fileinfo_iterator F = SourceMgr.fileinfo_begin(),
1452 FEnd = SourceMgr.fileinfo_end();
1455 const FileEntry *File = F->second->OrigEntry;
1456 if (!File || F->second->getRawBuffer() == MainFileBuffer)
1459 FilesInPreamble[File->getName()]
1460 = std::make_pair(F->second->getSize(), File->getModificationTime());
1463 PreambleRebuildCounter = 1;
1464 PreprocessorOpts.eraseRemappedFile(
1465 PreprocessorOpts.remapped_file_buffer_end() - 1);
1467 // If the hash of top-level entities differs from the hash of the top-level
1468 // entities the last time we rebuilt the preamble, clear out the completion
1470 if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1471 CompletionCacheTopLevelHashValue = 0;
1472 PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1475 return CreatePaddedMainFileBuffer(NewPreamble.first,
1476 PreambleReservedSize,
1477 FrontendOpts.Inputs[0].second);
1480 void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1481 std::vector<Decl *> Resolved;
1482 Resolved.reserve(TopLevelDeclsInPreamble.size());
1483 ExternalASTSource &Source = *getASTContext().getExternalSource();
1484 for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) {
1485 // Resolve the declaration ID to an actual declaration, possibly
1486 // deserializing the declaration in the process.
1487 Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]);
1489 Resolved.push_back(D);
1491 TopLevelDeclsInPreamble.clear();
1492 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1495 void ASTUnit::RealizePreprocessedEntitiesFromPreamble() {
1499 PreprocessingRecord *PPRec = PP->getPreprocessingRecord();
1503 ExternalPreprocessingRecordSource *External = PPRec->getExternalSource();
1507 for (unsigned I = 0, N = PreprocessedEntitiesInPreamble.size(); I != N; ++I) {
1508 if (PreprocessedEntity *PE
1509 = External->ReadPreprocessedEntityAtOffset(
1510 PreprocessedEntitiesInPreamble[I]))
1511 PreprocessedEntities.push_back(PE);
1514 if (PreprocessedEntities.empty())
1517 PreprocessedEntities.insert(PreprocessedEntities.end(),
1518 PPRec->begin(true), PPRec->end(true));
1521 ASTUnit::pp_entity_iterator ASTUnit::pp_entity_begin() {
1522 if (!PreprocessedEntitiesInPreamble.empty() &&
1523 PreprocessedEntities.empty())
1524 RealizePreprocessedEntitiesFromPreamble();
1526 if (PreprocessedEntities.empty())
1527 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
1528 return PPRec->begin(true);
1530 return PreprocessedEntities.begin();
1533 ASTUnit::pp_entity_iterator ASTUnit::pp_entity_end() {
1534 if (!PreprocessedEntitiesInPreamble.empty() &&
1535 PreprocessedEntities.empty())
1536 RealizePreprocessedEntitiesFromPreamble();
1538 if (PreprocessedEntities.empty())
1539 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
1540 return PPRec->end(true);
1542 return PreprocessedEntities.end();
1545 unsigned ASTUnit::getMaxPCHLevel() const {
1546 if (!getOnlyLocalDecls())
1547 return Decl::MaxPCHLevel;
1552 llvm::StringRef ASTUnit::getMainFileName() const {
1553 return Invocation->getFrontendOpts().Inputs[0].second;
1556 ASTUnit *ASTUnit::create(CompilerInvocation *CI,
1557 llvm::IntrusiveRefCntPtr<Diagnostic> Diags) {
1558 llvm::OwningPtr<ASTUnit> AST;
1559 AST.reset(new ASTUnit(false));
1560 ConfigureDiags(Diags, 0, 0, *AST, /*CaptureDiagnostics=*/false);
1561 AST->Diagnostics = Diags;
1562 AST->Invocation = CI;
1563 AST->FileSystemOpts = CI->getFileSystemOpts();
1564 AST->FileMgr = new FileManager(AST->FileSystemOpts);
1565 AST->SourceMgr = new SourceManager(*Diags, *AST->FileMgr);
1570 ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(CompilerInvocation *CI,
1571 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
1572 ASTFrontendAction *Action) {
1573 assert(CI && "A CompilerInvocation is required");
1575 // Create the AST unit.
1576 llvm::OwningPtr<ASTUnit> AST;
1577 AST.reset(new ASTUnit(false));
1578 ConfigureDiags(Diags, 0, 0, *AST, /*CaptureDiagnostics*/false);
1579 AST->Diagnostics = Diags;
1580 AST->OnlyLocalDecls = false;
1581 AST->CaptureDiagnostics = false;
1582 AST->CompleteTranslationUnit = Action ? Action->usesCompleteTranslationUnit()
1584 AST->ShouldCacheCodeCompletionResults = false;
1585 AST->Invocation = CI;
1587 // Recover resources if we crash before exiting this method.
1588 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1589 ASTUnitCleanup(AST.get());
1590 llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
1591 llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
1592 DiagCleanup(Diags.getPtr());
1594 // We'll manage file buffers ourselves.
1595 CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1596 CI->getFrontendOpts().DisableFree = false;
1597 ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
1599 // Save the target features.
1600 AST->TargetFeatures = CI->getTargetOpts().Features;
1602 // Create the compiler instance to use for building the AST.
1603 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1605 // Recover resources if we crash before exiting this method.
1606 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1607 CICleanup(Clang.get());
1609 Clang->setInvocation(CI);
1610 AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
1612 // Set up diagnostics, capturing any diagnostics that would
1613 // otherwise be dropped.
1614 Clang->setDiagnostics(&AST->getDiagnostics());
1616 // Create the target instance.
1617 Clang->getTargetOpts().Features = AST->TargetFeatures;
1618 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1619 Clang->getTargetOpts()));
1620 if (!Clang->hasTarget())
1623 // Inform the target of the language options.
1625 // FIXME: We shouldn't need to do this, the target should be immutable once
1626 // created. This complexity should be lifted elsewhere.
1627 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1629 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1630 "Invocation must have exactly one source file!");
1631 assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
1632 "FIXME: AST inputs not yet supported here!");
1633 assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
1634 "IR inputs not supported here!");
1636 // Configure the various subsystems.
1637 AST->FileSystemOpts = Clang->getFileSystemOpts();
1638 AST->FileMgr = new FileManager(AST->FileSystemOpts);
1639 AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr);
1640 AST->TheSema.reset();
1644 // Create a file manager object to provide access to and cache the filesystem.
1645 Clang->setFileManager(&AST->getFileManager());
1647 // Create the source manager.
1648 Clang->setSourceManager(&AST->getSourceManager());
1650 ASTFrontendAction *Act = Action;
1652 llvm::OwningPtr<TopLevelDeclTrackerAction> TrackerAct;
1654 TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
1655 Act = TrackerAct.get();
1658 // Recover resources if we crash before exiting this method.
1659 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1660 ActCleanup(TrackerAct.get());
1662 if (!Act->BeginSourceFile(*Clang.get(),
1663 Clang->getFrontendOpts().Inputs[0].second,
1664 Clang->getFrontendOpts().Inputs[0].first))
1669 // Steal the created target, context, and preprocessor.
1670 AST->TheSema.reset(Clang->takeSema());
1671 AST->Consumer.reset(Clang->takeASTConsumer());
1672 AST->Ctx = &Clang->getASTContext();
1673 AST->PP = &Clang->getPreprocessor();
1674 Clang->setSourceManager(0);
1675 Clang->setFileManager(0);
1676 AST->Target = &Clang->getTarget();
1678 Act->EndSourceFile();
1683 bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) {
1687 // We'll manage file buffers ourselves.
1688 Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1689 Invocation->getFrontendOpts().DisableFree = false;
1690 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1692 // Save the target features.
1693 TargetFeatures = Invocation->getTargetOpts().Features;
1695 llvm::MemoryBuffer *OverrideMainBuffer = 0;
1696 if (PrecompilePreamble) {
1697 PreambleRebuildCounter = 2;
1699 = getMainBufferWithPrecompiledPreamble(*Invocation);
1702 SimpleTimer ParsingTimer(WantTiming);
1703 ParsingTimer.setOutput("Parsing " + getMainFileName());
1705 // Recover resources if we crash before exiting this method.
1706 llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1707 MemBufferCleanup(OverrideMainBuffer);
1709 return Parse(OverrideMainBuffer);
1712 ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
1713 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
1714 bool OnlyLocalDecls,
1715 bool CaptureDiagnostics,
1716 bool PrecompilePreamble,
1717 bool CompleteTranslationUnit,
1718 bool CacheCodeCompletionResults,
1719 bool NestedMacroInstantiations) {
1720 // Create the AST unit.
1721 llvm::OwningPtr<ASTUnit> AST;
1722 AST.reset(new ASTUnit(false));
1723 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
1724 AST->Diagnostics = Diags;
1725 AST->OnlyLocalDecls = OnlyLocalDecls;
1726 AST->CaptureDiagnostics = CaptureDiagnostics;
1727 AST->CompleteTranslationUnit = CompleteTranslationUnit;
1728 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1729 AST->Invocation = CI;
1730 AST->NestedMacroInstantiations = NestedMacroInstantiations;
1732 // Recover resources if we crash before exiting this method.
1733 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1734 ASTUnitCleanup(AST.get());
1735 llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
1736 llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
1737 DiagCleanup(Diags.getPtr());
1739 return AST->LoadFromCompilerInvocation(PrecompilePreamble)? 0 : AST.take();
1742 ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
1743 const char **ArgEnd,
1744 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
1745 llvm::StringRef ResourceFilesPath,
1746 bool OnlyLocalDecls,
1747 bool CaptureDiagnostics,
1748 RemappedFile *RemappedFiles,
1749 unsigned NumRemappedFiles,
1750 bool RemappedFilesKeepOriginalName,
1751 bool PrecompilePreamble,
1752 bool CompleteTranslationUnit,
1753 bool CacheCodeCompletionResults,
1754 bool CXXPrecompilePreamble,
1756 bool NestedMacroInstantiations) {
1757 if (!Diags.getPtr()) {
1758 // No diagnostics engine was provided, so create our own diagnostics object
1759 // with the default options.
1760 DiagnosticOptions DiagOpts;
1761 Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd - ArgBegin,
1765 llvm::SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1767 llvm::IntrusiveRefCntPtr<CompilerInvocation> CI;
1770 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
1773 CI = clang::createInvocationFromCommandLine(
1774 llvm::ArrayRef<const char *>(ArgBegin, ArgEnd-ArgBegin),
1780 // Override any files that need remapping
1781 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
1782 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
1783 if (const llvm::MemoryBuffer *
1784 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
1785 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, memBuf);
1787 const char *fname = fileOrBuf.get<const char *>();
1788 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, fname);
1791 CI->getPreprocessorOpts().RemappedFilesKeepOriginalName =
1792 RemappedFilesKeepOriginalName;
1794 // Override the resources path.
1795 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1797 // Check whether we should precompile the preamble and/or use chained PCH.
1798 // FIXME: This is a temporary hack while we debug C++ chained PCH.
1799 if (CI->getLangOpts().CPlusPlus) {
1800 PrecompilePreamble = PrecompilePreamble && CXXPrecompilePreamble;
1802 if (PrecompilePreamble && !CXXChainedPCH &&
1803 !CI->getPreprocessorOpts().ImplicitPCHInclude.empty())
1804 PrecompilePreamble = false;
1807 // Create the AST unit.
1808 llvm::OwningPtr<ASTUnit> AST;
1809 AST.reset(new ASTUnit(false));
1810 ConfigureDiags(Diags, ArgBegin, ArgEnd, *AST, CaptureDiagnostics);
1811 AST->Diagnostics = Diags;
1813 AST->FileSystemOpts = CI->getFileSystemOpts();
1814 AST->FileMgr = new FileManager(AST->FileSystemOpts);
1815 AST->OnlyLocalDecls = OnlyLocalDecls;
1816 AST->CaptureDiagnostics = CaptureDiagnostics;
1817 AST->CompleteTranslationUnit = CompleteTranslationUnit;
1818 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1819 AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1820 AST->NumStoredDiagnosticsInPreamble = StoredDiagnostics.size();
1821 AST->StoredDiagnostics.swap(StoredDiagnostics);
1822 AST->Invocation = CI;
1823 AST->NestedMacroInstantiations = NestedMacroInstantiations;
1825 // Recover resources if we crash before exiting this method.
1826 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1827 ASTUnitCleanup(AST.get());
1828 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation,
1829 llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> >
1830 CICleanup(CI.getPtr());
1831 llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
1832 llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
1833 DiagCleanup(Diags.getPtr());
1835 return AST->LoadFromCompilerInvocation(PrecompilePreamble) ? 0 : AST.take();
1838 bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) {
1842 SimpleTimer ParsingTimer(WantTiming);
1843 ParsingTimer.setOutput("Reparsing " + getMainFileName());
1846 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1847 PPOpts.DisableStatCache = true;
1848 for (PreprocessorOptions::remapped_file_buffer_iterator
1849 R = PPOpts.remapped_file_buffer_begin(),
1850 REnd = PPOpts.remapped_file_buffer_end();
1855 Invocation->getPreprocessorOpts().clearRemappedFiles();
1856 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
1857 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
1858 if (const llvm::MemoryBuffer *
1859 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
1860 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1863 const char *fname = fileOrBuf.get<const char *>();
1864 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1869 // If we have a preamble file lying around, or if we might try to
1870 // build a precompiled preamble, do so now.
1871 llvm::MemoryBuffer *OverrideMainBuffer = 0;
1872 if (!PreambleFile.empty() || PreambleRebuildCounter > 0)
1873 OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation);
1875 // Clear out the diagnostics state.
1876 if (!OverrideMainBuffer) {
1877 getDiagnostics().Reset();
1878 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1881 // Parse the sources
1882 bool Result = Parse(OverrideMainBuffer);
1884 // If we're caching global code-completion results, and the top-level
1885 // declarations have changed, clear out the code-completion cache.
1886 if (!Result && ShouldCacheCodeCompletionResults &&
1887 CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
1888 CacheCodeCompletionResults();
1893 //----------------------------------------------------------------------------//
1895 //----------------------------------------------------------------------------//
1898 /// \brief Code completion consumer that combines the cached code-completion
1899 /// results from an ASTUnit with the code-completion results provided to it,
1900 /// then passes the result on to
1901 class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
1902 unsigned NormalContexts;
1904 CodeCompleteConsumer &Next;
1907 AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
1908 bool IncludeMacros, bool IncludeCodePatterns,
1909 bool IncludeGlobals)
1910 : CodeCompleteConsumer(IncludeMacros, IncludeCodePatterns, IncludeGlobals,
1911 Next.isOutputBinary()), AST(AST), Next(Next)
1913 // Compute the set of contexts in which we will look when we don't have
1914 // any information about the specific context.
1916 = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
1917 | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1))
1918 | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1))
1919 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
1920 | (1 << (CodeCompletionContext::CCC_Statement - 1))
1921 | (1 << (CodeCompletionContext::CCC_Expression - 1))
1922 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
1923 | (1 << (CodeCompletionContext::CCC_MemberAccess - 1))
1924 | (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1))
1925 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
1926 | (1 << (CodeCompletionContext::CCC_Recovery - 1));
1928 if (AST.getASTContext().getLangOptions().CPlusPlus)
1929 NormalContexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1))
1930 | (1 << (CodeCompletionContext::CCC_UnionTag - 1))
1931 | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1));
1934 virtual void ProcessCodeCompleteResults(Sema &S,
1935 CodeCompletionContext Context,
1936 CodeCompletionResult *Results,
1937 unsigned NumResults);
1939 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
1940 OverloadCandidate *Candidates,
1941 unsigned NumCandidates) {
1942 Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
1945 virtual CodeCompletionAllocator &getAllocator() {
1946 return Next.getAllocator();
1951 /// \brief Helper function that computes which global names are hidden by the
1952 /// local code-completion results.
1953 static void CalculateHiddenNames(const CodeCompletionContext &Context,
1954 CodeCompletionResult *Results,
1955 unsigned NumResults,
1957 llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
1958 bool OnlyTagNames = false;
1959 switch (Context.getKind()) {
1960 case CodeCompletionContext::CCC_Recovery:
1961 case CodeCompletionContext::CCC_TopLevel:
1962 case CodeCompletionContext::CCC_ObjCInterface:
1963 case CodeCompletionContext::CCC_ObjCImplementation:
1964 case CodeCompletionContext::CCC_ObjCIvarList:
1965 case CodeCompletionContext::CCC_ClassStructUnion:
1966 case CodeCompletionContext::CCC_Statement:
1967 case CodeCompletionContext::CCC_Expression:
1968 case CodeCompletionContext::CCC_ObjCMessageReceiver:
1969 case CodeCompletionContext::CCC_MemberAccess:
1970 case CodeCompletionContext::CCC_Namespace:
1971 case CodeCompletionContext::CCC_Type:
1972 case CodeCompletionContext::CCC_Name:
1973 case CodeCompletionContext::CCC_PotentiallyQualifiedName:
1974 case CodeCompletionContext::CCC_ParenthesizedExpression:
1977 case CodeCompletionContext::CCC_EnumTag:
1978 case CodeCompletionContext::CCC_UnionTag:
1979 case CodeCompletionContext::CCC_ClassOrStructTag:
1980 OnlyTagNames = true;
1983 case CodeCompletionContext::CCC_ObjCProtocolName:
1984 case CodeCompletionContext::CCC_MacroName:
1985 case CodeCompletionContext::CCC_MacroNameUse:
1986 case CodeCompletionContext::CCC_PreprocessorExpression:
1987 case CodeCompletionContext::CCC_PreprocessorDirective:
1988 case CodeCompletionContext::CCC_NaturalLanguage:
1989 case CodeCompletionContext::CCC_SelectorName:
1990 case CodeCompletionContext::CCC_TypeQualifiers:
1991 case CodeCompletionContext::CCC_Other:
1992 case CodeCompletionContext::CCC_OtherWithMacros:
1993 // We're looking for nothing, or we're looking for names that cannot
1998 typedef CodeCompletionResult Result;
1999 for (unsigned I = 0; I != NumResults; ++I) {
2000 if (Results[I].Kind != Result::RK_Declaration)
2004 = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
2006 bool Hiding = false;
2008 Hiding = (IDNS & Decl::IDNS_Tag);
2010 unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
2011 Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
2012 Decl::IDNS_NonMemberOperator);
2013 if (Ctx.getLangOptions().CPlusPlus)
2014 HiddenIDNS |= Decl::IDNS_Tag;
2015 Hiding = (IDNS & HiddenIDNS);
2021 DeclarationName Name = Results[I].Declaration->getDeclName();
2022 if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
2023 HiddenNames.insert(Identifier->getName());
2025 HiddenNames.insert(Name.getAsString());
2030 void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
2031 CodeCompletionContext Context,
2032 CodeCompletionResult *Results,
2033 unsigned NumResults) {
2034 // Merge the results we were given with the results we cached.
2035 bool AddedResult = false;
2037 = (Context.getKind() == CodeCompletionContext::CCC_Recovery? NormalContexts
2038 : (1 << (Context.getKind() - 1)));
2040 // Contains the set of names that are hidden by "local" completion results.
2041 llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
2042 typedef CodeCompletionResult Result;
2043 llvm::SmallVector<Result, 8> AllResults;
2044 for (ASTUnit::cached_completion_iterator
2045 C = AST.cached_completion_begin(),
2046 CEnd = AST.cached_completion_end();
2048 // If the context we are in matches any of the contexts we are
2049 // interested in, we'll add this result.
2050 if ((C->ShowInContexts & InContexts) == 0)
2053 // If we haven't added any results previously, do so now.
2055 CalculateHiddenNames(Context, Results, NumResults, S.Context,
2057 AllResults.insert(AllResults.end(), Results, Results + NumResults);
2061 // Determine whether this global completion result is hidden by a local
2062 // completion result. If so, skip it.
2063 if (C->Kind != CXCursor_MacroDefinition &&
2064 HiddenNames.count(C->Completion->getTypedText()))
2067 // Adjust priority based on similar type classes.
2068 unsigned Priority = C->Priority;
2069 CXCursorKind CursorKind = C->Kind;
2070 CodeCompletionString *Completion = C->Completion;
2071 if (!Context.getPreferredType().isNull()) {
2072 if (C->Kind == CXCursor_MacroDefinition) {
2073 Priority = getMacroUsagePriority(C->Completion->getTypedText(),
2075 Context.getPreferredType()->isAnyPointerType());
2076 } else if (C->Type) {
2077 CanQualType Expected
2078 = S.Context.getCanonicalType(
2079 Context.getPreferredType().getUnqualifiedType());
2080 SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
2081 if (ExpectedSTC == C->TypeClass) {
2082 // We know this type is similar; check for an exact match.
2083 llvm::StringMap<unsigned> &CachedCompletionTypes
2084 = AST.getCachedCompletionTypes();
2085 llvm::StringMap<unsigned>::iterator Pos
2086 = CachedCompletionTypes.find(QualType(Expected).getAsString());
2087 if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
2088 Priority /= CCF_ExactTypeMatch;
2090 Priority /= CCF_SimilarTypeMatch;
2095 // Adjust the completion string, if required.
2096 if (C->Kind == CXCursor_MacroDefinition &&
2097 Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
2098 // Create a new code-completion string that just contains the
2099 // macro name, without its arguments.
2100 CodeCompletionBuilder Builder(getAllocator(), CCP_CodePattern,
2102 Builder.AddTypedTextChunk(C->Completion->getTypedText());
2103 CursorKind = CXCursor_NotImplemented;
2104 Priority = CCP_CodePattern;
2105 Completion = Builder.TakeString();
2108 AllResults.push_back(Result(Completion, Priority, CursorKind,
2112 // If we did not add any cached completion results, just forward the
2113 // results we were given to the next consumer.
2115 Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
2119 Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
2125 void ASTUnit::CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column,
2126 RemappedFile *RemappedFiles,
2127 unsigned NumRemappedFiles,
2129 bool IncludeCodePatterns,
2130 CodeCompleteConsumer &Consumer,
2131 Diagnostic &Diag, LangOptions &LangOpts,
2132 SourceManager &SourceMgr, FileManager &FileMgr,
2133 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2134 llvm::SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
2138 SimpleTimer CompletionTimer(WantTiming);
2139 CompletionTimer.setOutput("Code completion @ " + File + ":" +
2140 llvm::Twine(Line) + ":" + llvm::Twine(Column));
2142 llvm::IntrusiveRefCntPtr<CompilerInvocation>
2143 CCInvocation(new CompilerInvocation(*Invocation));
2145 FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2146 PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2148 FrontendOpts.ShowMacrosInCodeCompletion
2149 = IncludeMacros && CachedCompletionResults.empty();
2150 FrontendOpts.ShowCodePatternsInCodeCompletion = IncludeCodePatterns;
2151 FrontendOpts.ShowGlobalSymbolsInCodeCompletion
2152 = CachedCompletionResults.empty();
2153 FrontendOpts.CodeCompletionAt.FileName = File;
2154 FrontendOpts.CodeCompletionAt.Line = Line;
2155 FrontendOpts.CodeCompletionAt.Column = Column;
2157 // Set the language options appropriately.
2158 LangOpts = CCInvocation->getLangOpts();
2160 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
2162 // Recover resources if we crash before exiting this method.
2163 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2164 CICleanup(Clang.get());
2166 Clang->setInvocation(&*CCInvocation);
2167 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
2169 // Set up diagnostics, capturing any diagnostics produced.
2170 Clang->setDiagnostics(&Diag);
2171 ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts());
2172 CaptureDroppedDiagnostics Capture(true,
2173 Clang->getDiagnostics(),
2176 // Create the target instance.
2177 Clang->getTargetOpts().Features = TargetFeatures;
2178 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
2179 Clang->getTargetOpts()));
2180 if (!Clang->hasTarget()) {
2181 Clang->setInvocation(0);
2185 // Inform the target of the language options.
2187 // FIXME: We shouldn't need to do this, the target should be immutable once
2188 // created. This complexity should be lifted elsewhere.
2189 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
2191 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2192 "Invocation must have exactly one source file!");
2193 assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
2194 "FIXME: AST inputs not yet supported here!");
2195 assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
2196 "IR inputs not support here!");
2199 // Use the source and file managers that we were given.
2200 Clang->setFileManager(&FileMgr);
2201 Clang->setSourceManager(&SourceMgr);
2204 PreprocessorOpts.clearRemappedFiles();
2205 PreprocessorOpts.RetainRemappedFileBuffers = true;
2206 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
2207 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
2208 if (const llvm::MemoryBuffer *
2209 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
2210 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, memBuf);
2211 OwnedBuffers.push_back(memBuf);
2213 const char *fname = fileOrBuf.get<const char *>();
2214 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, fname);
2218 // Use the code completion consumer we were given, but adding any cached
2219 // code-completion results.
2220 AugmentedCodeCompleteConsumer *AugmentedConsumer
2221 = new AugmentedCodeCompleteConsumer(*this, Consumer,
2222 FrontendOpts.ShowMacrosInCodeCompletion,
2223 FrontendOpts.ShowCodePatternsInCodeCompletion,
2224 FrontendOpts.ShowGlobalSymbolsInCodeCompletion);
2225 Clang->setCodeCompletionConsumer(AugmentedConsumer);
2227 // If we have a precompiled preamble, try to use it. We only allow
2228 // the use of the precompiled preamble if we're if the completion
2229 // point is within the main file, after the end of the precompiled
2231 llvm::MemoryBuffer *OverrideMainBuffer = 0;
2232 if (!PreambleFile.empty()) {
2233 using llvm::sys::FileStatus;
2234 llvm::sys::PathWithStatus CompleteFilePath(File);
2235 llvm::sys::PathWithStatus MainPath(OriginalSourceFile);
2236 if (const FileStatus *CompleteFileStatus = CompleteFilePath.getFileStatus())
2237 if (const FileStatus *MainStatus = MainPath.getFileStatus())
2238 if (CompleteFileStatus->getUniqueID() == MainStatus->getUniqueID())
2240 = getMainBufferWithPrecompiledPreamble(*CCInvocation, false,
2244 // If the main file has been overridden due to the use of a preamble,
2245 // make that override happen and introduce the preamble.
2246 PreprocessorOpts.DisableStatCache = true;
2247 StoredDiagnostics.insert(StoredDiagnostics.end(),
2248 this->StoredDiagnostics.begin(),
2249 this->StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver);
2250 if (OverrideMainBuffer) {
2251 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
2252 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
2253 PreprocessorOpts.PrecompiledPreambleBytes.second
2254 = PreambleEndsAtStartOfLine;
2255 PreprocessorOpts.ImplicitPCHInclude = PreambleFile;
2256 PreprocessorOpts.DisablePCHValidation = true;
2258 // The stored diagnostics have the old source manager. Copy them
2259 // to our output set of stored diagnostics, updating the source
2260 // manager to the one we were given.
2261 for (unsigned I = NumStoredDiagnosticsFromDriver,
2262 N = this->StoredDiagnostics.size();
2264 StoredDiagnostics.push_back(this->StoredDiagnostics[I]);
2265 FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), SourceMgr);
2266 StoredDiagnostics[I].setLocation(Loc);
2269 OwnedBuffers.push_back(OverrideMainBuffer);
2271 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2272 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2275 // Disable the preprocessing record
2276 PreprocessorOpts.DetailedRecord = false;
2278 llvm::OwningPtr<SyntaxOnlyAction> Act;
2279 Act.reset(new SyntaxOnlyAction);
2280 if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
2281 Clang->getFrontendOpts().Inputs[0].first)) {
2283 Act->EndSourceFile();
2287 bool ASTUnit::Save(llvm::StringRef File) {
2288 if (getDiagnostics().hasErrorOccurred())
2291 // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2292 // unconditionally create a stat cache when we parse the file?
2293 std::string ErrorInfo;
2294 llvm::raw_fd_ostream Out(File.str().c_str(), ErrorInfo,
2295 llvm::raw_fd_ostream::F_Binary);
2296 if (!ErrorInfo.empty() || Out.has_error())
2301 return Out.has_error();
2304 bool ASTUnit::serialize(llvm::raw_ostream &OS) {
2305 if (getDiagnostics().hasErrorOccurred())
2308 std::vector<unsigned char> Buffer;
2309 llvm::BitstreamWriter Stream(Buffer);
2310 ASTWriter Writer(Stream);
2311 Writer.WriteAST(getSema(), 0, std::string(), 0);
2313 // Write the generated bitstream to "Out".
2314 if (!Buffer.empty())
2315 OS.write((char *)&Buffer.front(), Buffer.size());