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/ASTConsumer.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/DeclVisitor.h"
18 #include "clang/AST/StmtVisitor.h"
19 #include "clang/AST/TypeOrdering.h"
20 #include "clang/Basic/Diagnostic.h"
21 #include "clang/Basic/TargetInfo.h"
22 #include "clang/Basic/TargetOptions.h"
23 #include "clang/Frontend/CompilerInstance.h"
24 #include "clang/Frontend/FrontendActions.h"
25 #include "clang/Frontend/FrontendDiagnostic.h"
26 #include "clang/Frontend/FrontendOptions.h"
27 #include "clang/Frontend/MultiplexConsumer.h"
28 #include "clang/Frontend/Utils.h"
29 #include "clang/Lex/HeaderSearch.h"
30 #include "clang/Lex/Preprocessor.h"
31 #include "clang/Lex/PreprocessorOptions.h"
32 #include "clang/Sema/Sema.h"
33 #include "clang/Serialization/ASTReader.h"
34 #include "clang/Serialization/ASTWriter.h"
35 #include "llvm/ADT/ArrayRef.h"
36 #include "llvm/ADT/StringExtras.h"
37 #include "llvm/ADT/StringSet.h"
38 #include "llvm/Support/Atomic.h"
39 #include "llvm/Support/CrashRecoveryContext.h"
40 #include "llvm/Support/FileSystem.h"
41 #include "llvm/Support/Host.h"
42 #include "llvm/Support/MemoryBuffer.h"
43 #include "llvm/Support/Mutex.h"
44 #include "llvm/Support/MutexGuard.h"
45 #include "llvm/Support/Path.h"
46 #include "llvm/Support/Timer.h"
47 #include "llvm/Support/raw_ostream.h"
51 using namespace clang;
53 using llvm::TimeRecord;
62 explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
64 Start = TimeRecord::getCurrentTime();
67 void setOutput(const Twine &Output) {
69 this->Output = Output.str();
74 TimeRecord Elapsed = TimeRecord::getCurrentTime();
76 llvm::errs() << Output << ':';
77 Elapsed.print(Elapsed, llvm::errs());
84 /// \brief The file in which the precompiled preamble is stored.
85 std::string PreambleFile;
87 /// \brief Temporary files that should be removed when the ASTUnit is
89 SmallVector<std::string, 4> TemporaryFiles;
91 /// \brief Erase temporary files.
92 void CleanTemporaryFiles();
94 /// \brief Erase the preamble file.
95 void CleanPreambleFile();
97 /// \brief Erase temporary files and the preamble file.
102 static llvm::sys::SmartMutex<false> &getOnDiskMutex() {
103 static llvm::sys::SmartMutex<false> M(/* recursive = */ true);
107 static void cleanupOnDiskMapAtExit();
109 typedef llvm::DenseMap<const ASTUnit *, OnDiskData *> OnDiskDataMap;
110 static OnDiskDataMap &getOnDiskDataMap() {
111 static OnDiskDataMap M;
112 static bool hasRegisteredAtExit = false;
113 if (!hasRegisteredAtExit) {
114 hasRegisteredAtExit = true;
115 atexit(cleanupOnDiskMapAtExit);
120 static void cleanupOnDiskMapAtExit() {
121 // Use the mutex because there can be an alive thread destroying an ASTUnit.
122 llvm::MutexGuard Guard(getOnDiskMutex());
123 OnDiskDataMap &M = getOnDiskDataMap();
124 for (OnDiskDataMap::iterator I = M.begin(), E = M.end(); I != E; ++I) {
125 // We don't worry about freeing the memory associated with OnDiskDataMap.
126 // All we care about is erasing stale files.
127 I->second->Cleanup();
131 static OnDiskData &getOnDiskData(const ASTUnit *AU) {
132 // We require the mutex since we are modifying the structure of the
134 llvm::MutexGuard Guard(getOnDiskMutex());
135 OnDiskDataMap &M = getOnDiskDataMap();
136 OnDiskData *&D = M[AU];
138 D = new OnDiskData();
142 static void erasePreambleFile(const ASTUnit *AU) {
143 getOnDiskData(AU).CleanPreambleFile();
146 static void removeOnDiskEntry(const ASTUnit *AU) {
147 // We require the mutex since we are modifying the structure of the
149 llvm::MutexGuard Guard(getOnDiskMutex());
150 OnDiskDataMap &M = getOnDiskDataMap();
151 OnDiskDataMap::iterator I = M.find(AU);
153 I->second->Cleanup();
159 static void setPreambleFile(const ASTUnit *AU, StringRef preambleFile) {
160 getOnDiskData(AU).PreambleFile = preambleFile;
163 static const std::string &getPreambleFile(const ASTUnit *AU) {
164 return getOnDiskData(AU).PreambleFile;
167 void OnDiskData::CleanTemporaryFiles() {
168 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
169 llvm::sys::fs::remove(TemporaryFiles[I]);
170 TemporaryFiles.clear();
173 void OnDiskData::CleanPreambleFile() {
174 if (!PreambleFile.empty()) {
175 llvm::sys::fs::remove(PreambleFile);
176 PreambleFile.clear();
180 void OnDiskData::Cleanup() {
181 CleanTemporaryFiles();
185 struct ASTUnit::ASTWriterData {
186 SmallString<128> Buffer;
187 llvm::BitstreamWriter Stream;
190 ASTWriterData() : Stream(Buffer), Writer(Stream) { }
193 void ASTUnit::clearFileLevelDecls() {
194 for (FileDeclsTy::iterator
195 I = FileDecls.begin(), E = FileDecls.end(); I != E; ++I)
200 void ASTUnit::CleanTemporaryFiles() {
201 getOnDiskData(this).CleanTemporaryFiles();
204 void ASTUnit::addTemporaryFile(StringRef TempFile) {
205 getOnDiskData(this).TemporaryFiles.push_back(TempFile);
208 /// \brief After failing to build a precompiled preamble (due to
209 /// errors in the source that occurs in the preamble), the number of
210 /// reparses during which we'll skip even trying to precompile the
212 const unsigned DefaultPreambleRebuildInterval = 5;
214 /// \brief Tracks the number of ASTUnit objects that are currently active.
216 /// Used for debugging purposes only.
217 static llvm::sys::cas_flag ActiveASTUnitObjects;
219 ASTUnit::ASTUnit(bool _MainFileIsAST)
220 : Reader(0), HadModuleLoaderFatalFailure(false),
221 OnlyLocalDecls(false), CaptureDiagnostics(false),
222 MainFileIsAST(_MainFileIsAST),
223 TUKind(TU_Complete), WantTiming(getenv("LIBCLANG_TIMING")),
224 OwnsRemappedFileBuffers(true),
225 NumStoredDiagnosticsFromDriver(0),
226 PreambleRebuildCounter(0), SavedMainFileBuffer(0), PreambleBuffer(0),
227 NumWarningsInPreamble(0),
228 ShouldCacheCodeCompletionResults(false),
229 IncludeBriefCommentsInCodeCompletion(false), UserFilesAreVolatile(false),
230 CompletionCacheTopLevelHashValue(0),
231 PreambleTopLevelHashValue(0),
232 CurrentTopLevelHashValue(0),
233 UnsafeToFree(false) {
234 if (getenv("LIBCLANG_OBJTRACKING")) {
235 llvm::sys::AtomicIncrement(&ActiveASTUnitObjects);
236 fprintf(stderr, "+++ %d translation units\n", ActiveASTUnitObjects);
240 ASTUnit::~ASTUnit() {
241 // If we loaded from an AST file, balance out the BeginSourceFile call.
242 if (MainFileIsAST && getDiagnostics().getClient()) {
243 getDiagnostics().getClient()->EndSourceFile();
246 clearFileLevelDecls();
248 // Clean up the temporary files and the preamble file.
249 removeOnDiskEntry(this);
251 // Free the buffers associated with remapped files. We are required to
252 // perform this operation here because we explicitly request that the
253 // compiler instance *not* free these buffers for each invocation of the
255 if (Invocation.getPtr() && OwnsRemappedFileBuffers) {
256 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
257 for (PreprocessorOptions::remapped_file_buffer_iterator
258 FB = PPOpts.remapped_file_buffer_begin(),
259 FBEnd = PPOpts.remapped_file_buffer_end();
265 delete SavedMainFileBuffer;
266 delete PreambleBuffer;
268 ClearCachedCompletionResults();
270 if (getenv("LIBCLANG_OBJTRACKING")) {
271 llvm::sys::AtomicDecrement(&ActiveASTUnitObjects);
272 fprintf(stderr, "--- %d translation units\n", ActiveASTUnitObjects);
276 void ASTUnit::setPreprocessor(Preprocessor *pp) { PP = pp; }
278 /// \brief Determine the set of code-completion contexts in which this
279 /// declaration should be shown.
280 static unsigned getDeclShowContexts(const NamedDecl *ND,
281 const LangOptions &LangOpts,
282 bool &IsNestedNameSpecifier) {
283 IsNestedNameSpecifier = false;
285 if (isa<UsingShadowDecl>(ND))
286 ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl());
290 uint64_t Contexts = 0;
291 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) ||
292 isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) {
293 // Types can appear in these contexts.
294 if (LangOpts.CPlusPlus || !isa<TagDecl>(ND))
295 Contexts |= (1LL << CodeCompletionContext::CCC_TopLevel)
296 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
297 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
298 | (1LL << CodeCompletionContext::CCC_Statement)
299 | (1LL << CodeCompletionContext::CCC_Type)
300 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
302 // In C++, types can appear in expressions contexts (for functional casts).
303 if (LangOpts.CPlusPlus)
304 Contexts |= (1LL << CodeCompletionContext::CCC_Expression);
306 // In Objective-C, message sends can send interfaces. In Objective-C++,
307 // all types are available due to functional casts.
308 if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND))
309 Contexts |= (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver);
311 // In Objective-C, you can only be a subclass of another Objective-C class
312 if (isa<ObjCInterfaceDecl>(ND))
313 Contexts |= (1LL << CodeCompletionContext::CCC_ObjCInterfaceName);
315 // Deal with tag names.
316 if (isa<EnumDecl>(ND)) {
317 Contexts |= (1LL << CodeCompletionContext::CCC_EnumTag);
319 // Part of the nested-name-specifier in C++0x.
320 if (LangOpts.CPlusPlus11)
321 IsNestedNameSpecifier = true;
322 } else if (const RecordDecl *Record = dyn_cast<RecordDecl>(ND)) {
323 if (Record->isUnion())
324 Contexts |= (1LL << CodeCompletionContext::CCC_UnionTag);
326 Contexts |= (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
328 if (LangOpts.CPlusPlus)
329 IsNestedNameSpecifier = true;
330 } else if (isa<ClassTemplateDecl>(ND))
331 IsNestedNameSpecifier = true;
332 } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) {
333 // Values can appear in these contexts.
334 Contexts = (1LL << CodeCompletionContext::CCC_Statement)
335 | (1LL << CodeCompletionContext::CCC_Expression)
336 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
337 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver);
338 } else if (isa<ObjCProtocolDecl>(ND)) {
339 Contexts = (1LL << CodeCompletionContext::CCC_ObjCProtocolName);
340 } else if (isa<ObjCCategoryDecl>(ND)) {
341 Contexts = (1LL << CodeCompletionContext::CCC_ObjCCategoryName);
342 } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
343 Contexts = (1LL << CodeCompletionContext::CCC_Namespace);
345 // Part of the nested-name-specifier.
346 IsNestedNameSpecifier = true;
352 void ASTUnit::CacheCodeCompletionResults() {
356 SimpleTimer Timer(WantTiming);
357 Timer.setOutput("Cache global code completions for " + getMainFileName());
359 // Clear out the previous results.
360 ClearCachedCompletionResults();
362 // Gather the set of global code completions.
363 typedef CodeCompletionResult Result;
364 SmallVector<Result, 8> Results;
365 CachedCompletionAllocator = new GlobalCodeCompletionAllocator;
366 CodeCompletionTUInfo CCTUInfo(CachedCompletionAllocator);
367 TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator,
370 // Translate global code completions into cached completions.
371 llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
373 for (unsigned I = 0, N = Results.size(); I != N; ++I) {
374 switch (Results[I].Kind) {
375 case Result::RK_Declaration: {
376 bool IsNestedNameSpecifier = false;
377 CachedCodeCompletionResult CachedResult;
378 CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema,
379 *CachedCompletionAllocator,
381 IncludeBriefCommentsInCodeCompletion);
382 CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration,
384 IsNestedNameSpecifier);
385 CachedResult.Priority = Results[I].Priority;
386 CachedResult.Kind = Results[I].CursorKind;
387 CachedResult.Availability = Results[I].Availability;
389 // Keep track of the type of this completion in an ASTContext-agnostic
391 QualType UsageType = getDeclUsageType(*Ctx, Results[I].Declaration);
392 if (UsageType.isNull()) {
393 CachedResult.TypeClass = STC_Void;
394 CachedResult.Type = 0;
396 CanQualType CanUsageType
397 = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
398 CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
400 // Determine whether we have already seen this type. If so, we save
401 // ourselves the work of formatting the type string by using the
402 // temporary, CanQualType-based hash table to find the associated value.
403 unsigned &TypeValue = CompletionTypes[CanUsageType];
404 if (TypeValue == 0) {
405 TypeValue = CompletionTypes.size();
406 CachedCompletionTypes[QualType(CanUsageType).getAsString()]
410 CachedResult.Type = TypeValue;
413 CachedCompletionResults.push_back(CachedResult);
415 /// Handle nested-name-specifiers in C++.
416 if (TheSema->Context.getLangOpts().CPlusPlus &&
417 IsNestedNameSpecifier && !Results[I].StartsNestedNameSpecifier) {
418 // The contexts in which a nested-name-specifier can appear in C++.
420 = (1LL << CodeCompletionContext::CCC_TopLevel)
421 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
422 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
423 | (1LL << CodeCompletionContext::CCC_Statement)
424 | (1LL << CodeCompletionContext::CCC_Expression)
425 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
426 | (1LL << CodeCompletionContext::CCC_EnumTag)
427 | (1LL << CodeCompletionContext::CCC_UnionTag)
428 | (1LL << CodeCompletionContext::CCC_ClassOrStructTag)
429 | (1LL << CodeCompletionContext::CCC_Type)
430 | (1LL << CodeCompletionContext::CCC_PotentiallyQualifiedName)
431 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
433 if (isa<NamespaceDecl>(Results[I].Declaration) ||
434 isa<NamespaceAliasDecl>(Results[I].Declaration))
435 NNSContexts |= (1LL << CodeCompletionContext::CCC_Namespace);
437 if (unsigned RemainingContexts
438 = NNSContexts & ~CachedResult.ShowInContexts) {
439 // If there any contexts where this completion can be a
440 // nested-name-specifier but isn't already an option, create a
441 // nested-name-specifier completion.
442 Results[I].StartsNestedNameSpecifier = true;
443 CachedResult.Completion
444 = Results[I].CreateCodeCompletionString(*TheSema,
445 *CachedCompletionAllocator,
447 IncludeBriefCommentsInCodeCompletion);
448 CachedResult.ShowInContexts = RemainingContexts;
449 CachedResult.Priority = CCP_NestedNameSpecifier;
450 CachedResult.TypeClass = STC_Void;
451 CachedResult.Type = 0;
452 CachedCompletionResults.push_back(CachedResult);
458 case Result::RK_Keyword:
459 case Result::RK_Pattern:
460 // Ignore keywords and patterns; we don't care, since they are so
461 // easily regenerated.
464 case Result::RK_Macro: {
465 CachedCodeCompletionResult CachedResult;
466 CachedResult.Completion
467 = Results[I].CreateCodeCompletionString(*TheSema,
468 *CachedCompletionAllocator,
470 IncludeBriefCommentsInCodeCompletion);
471 CachedResult.ShowInContexts
472 = (1LL << CodeCompletionContext::CCC_TopLevel)
473 | (1LL << CodeCompletionContext::CCC_ObjCInterface)
474 | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
475 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
476 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
477 | (1LL << CodeCompletionContext::CCC_Statement)
478 | (1LL << CodeCompletionContext::CCC_Expression)
479 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
480 | (1LL << CodeCompletionContext::CCC_MacroNameUse)
481 | (1LL << CodeCompletionContext::CCC_PreprocessorExpression)
482 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
483 | (1LL << CodeCompletionContext::CCC_OtherWithMacros);
485 CachedResult.Priority = Results[I].Priority;
486 CachedResult.Kind = Results[I].CursorKind;
487 CachedResult.Availability = Results[I].Availability;
488 CachedResult.TypeClass = STC_Void;
489 CachedResult.Type = 0;
490 CachedCompletionResults.push_back(CachedResult);
496 // Save the current top-level hash value.
497 CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
500 void ASTUnit::ClearCachedCompletionResults() {
501 CachedCompletionResults.clear();
502 CachedCompletionTypes.clear();
503 CachedCompletionAllocator = 0;
508 /// \brief Gathers information from ASTReader that will be used to initialize
510 class ASTInfoCollector : public ASTReaderListener {
513 LangOptions &LangOpt;
514 IntrusiveRefCntPtr<TargetOptions> &TargetOpts;
515 IntrusiveRefCntPtr<TargetInfo> &Target;
518 bool InitializedLanguage;
520 ASTInfoCollector(Preprocessor &PP, ASTContext &Context, LangOptions &LangOpt,
521 IntrusiveRefCntPtr<TargetOptions> &TargetOpts,
522 IntrusiveRefCntPtr<TargetInfo> &Target,
524 : PP(PP), Context(Context), LangOpt(LangOpt),
525 TargetOpts(TargetOpts), Target(Target),
527 InitializedLanguage(false) {}
529 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
531 if (InitializedLanguage)
535 InitializedLanguage = true;
541 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
543 // If we've already initialized the target, don't do it again.
547 this->TargetOpts = new TargetOptions(TargetOpts);
548 Target = TargetInfo::CreateTargetInfo(PP.getDiagnostics(),
555 virtual void ReadCounter(const serialization::ModuleFile &M, unsigned Value) {
561 if (!Target || !InitializedLanguage)
564 // Inform the target of the language options.
566 // FIXME: We shouldn't need to do this, the target should be immutable once
567 // created. This complexity should be lifted elsewhere.
568 Target->setForcedLangOptions(LangOpt);
570 // Initialize the preprocessor.
571 PP.Initialize(*Target);
573 // Initialize the ASTContext
574 Context.InitBuiltinTypes(*Target);
576 // We didn't have access to the comment options when the ASTContext was
577 // constructed, so register them now.
578 Context.getCommentCommandTraits().registerCommentOptions(
579 LangOpt.CommentOpts);
583 /// \brief Diagnostic consumer that saves each diagnostic it is given.
584 class StoredDiagnosticConsumer : public DiagnosticConsumer {
585 SmallVectorImpl<StoredDiagnostic> &StoredDiags;
586 SourceManager *SourceMgr;
589 explicit StoredDiagnosticConsumer(
590 SmallVectorImpl<StoredDiagnostic> &StoredDiags)
591 : StoredDiags(StoredDiags), SourceMgr(0) { }
593 virtual void BeginSourceFile(const LangOptions &LangOpts,
594 const Preprocessor *PP = 0) {
596 SourceMgr = &PP->getSourceManager();
599 virtual void HandleDiagnostic(DiagnosticsEngine::Level Level,
600 const Diagnostic &Info);
603 /// \brief RAII object that optionally captures diagnostics, if
604 /// there is no diagnostic client to capture them already.
605 class CaptureDroppedDiagnostics {
606 DiagnosticsEngine &Diags;
607 StoredDiagnosticConsumer Client;
608 DiagnosticConsumer *PreviousClient;
611 CaptureDroppedDiagnostics(bool RequestCapture, DiagnosticsEngine &Diags,
612 SmallVectorImpl<StoredDiagnostic> &StoredDiags)
613 : Diags(Diags), Client(StoredDiags), PreviousClient(0)
615 if (RequestCapture || Diags.getClient() == 0) {
616 PreviousClient = Diags.takeClient();
617 Diags.setClient(&Client);
621 ~CaptureDroppedDiagnostics() {
622 if (Diags.getClient() == &Client) {
624 Diags.setClient(PreviousClient);
629 } // anonymous namespace
631 void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level,
632 const Diagnostic &Info) {
633 // Default implementation (Warnings/errors count).
634 DiagnosticConsumer::HandleDiagnostic(Level, Info);
636 // Only record the diagnostic if it's part of the source manager we know
637 // about. This effectively drops diagnostics from modules we're building.
638 // FIXME: In the long run, ee don't want to drop source managers from modules.
639 if (!Info.hasSourceManager() || &Info.getSourceManager() == SourceMgr)
640 StoredDiags.push_back(StoredDiagnostic(Level, Info));
643 ASTMutationListener *ASTUnit::getASTMutationListener() {
645 return &WriterData->Writer;
649 ASTDeserializationListener *ASTUnit::getDeserializationListener() {
651 return &WriterData->Writer;
655 llvm::MemoryBuffer *ASTUnit::getBufferForFile(StringRef Filename,
656 std::string *ErrorStr) {
658 return FileMgr->getBufferForFile(Filename, ErrorStr);
661 /// \brief Configure the diagnostics object for use with ASTUnit.
662 void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> &Diags,
663 const char **ArgBegin, const char **ArgEnd,
664 ASTUnit &AST, bool CaptureDiagnostics) {
665 if (!Diags.getPtr()) {
666 // No diagnostics engine was provided, so create our own diagnostics object
667 // with the default options.
668 DiagnosticConsumer *Client = 0;
669 if (CaptureDiagnostics)
670 Client = new StoredDiagnosticConsumer(AST.StoredDiagnostics);
671 Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions(),
673 /*ShouldOwnClient=*/true);
674 } else if (CaptureDiagnostics) {
675 Diags->setClient(new StoredDiagnosticConsumer(AST.StoredDiagnostics));
679 ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename,
680 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
681 const FileSystemOptions &FileSystemOpts,
683 RemappedFile *RemappedFiles,
684 unsigned NumRemappedFiles,
685 bool CaptureDiagnostics,
686 bool AllowPCHWithCompilerErrors,
687 bool UserFilesAreVolatile) {
688 OwningPtr<ASTUnit> AST(new ASTUnit(true));
690 // Recover resources if we crash before exiting this method.
691 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
692 ASTUnitCleanup(AST.get());
693 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
694 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
695 DiagCleanup(Diags.getPtr());
697 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
699 AST->OnlyLocalDecls = OnlyLocalDecls;
700 AST->CaptureDiagnostics = CaptureDiagnostics;
701 AST->Diagnostics = Diags;
702 AST->FileMgr = new FileManager(FileSystemOpts);
703 AST->UserFilesAreVolatile = UserFilesAreVolatile;
704 AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
705 AST->getFileManager(),
706 UserFilesAreVolatile);
707 AST->HSOpts = new HeaderSearchOptions();
709 AST->HeaderInfo.reset(new HeaderSearch(AST->HSOpts,
710 AST->getSourceManager(),
711 AST->getDiagnostics(),
712 AST->ASTFileLangOpts,
715 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
716 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
717 if (const llvm::MemoryBuffer *
718 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
719 // Create the file entry for the file that we're mapping from.
720 const FileEntry *FromFile
721 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
722 memBuf->getBufferSize(),
725 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
726 << RemappedFiles[I].first;
731 // Override the contents of the "from" file with the contents of
733 AST->getSourceManager().overrideFileContents(FromFile, memBuf);
736 const char *fname = fileOrBuf.get<const char *>();
737 const FileEntry *ToFile = AST->FileMgr->getFile(fname);
739 AST->getDiagnostics().Report(diag::err_fe_remap_missing_to_file)
740 << RemappedFiles[I].first << fname;
744 // Create the file entry for the file that we're mapping from.
745 const FileEntry *FromFile
746 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
750 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
751 << RemappedFiles[I].first;
756 // Override the contents of the "from" file with the contents of
758 AST->getSourceManager().overrideFileContents(FromFile, ToFile);
762 // Gather Info for preprocessor construction later on.
764 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
767 OwningPtr<ASTReader> Reader;
769 AST->PP = new Preprocessor(new PreprocessorOptions(),
770 AST->getDiagnostics(), AST->ASTFileLangOpts,
771 /*Target=*/0, AST->getSourceManager(), HeaderInfo,
774 /*OwnsHeaderSearch=*/false,
775 /*DelayInitialization=*/true);
776 Preprocessor &PP = *AST->PP;
778 AST->Ctx = new ASTContext(AST->ASTFileLangOpts,
779 AST->getSourceManager(),
781 PP.getIdentifierTable(),
782 PP.getSelectorTable(),
784 /* size_reserve = */0,
785 /*DelayInitialization=*/true);
786 ASTContext &Context = *AST->Ctx;
788 bool disableValid = false;
789 if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION"))
791 Reader.reset(new ASTReader(PP, Context,
793 /*DisableValidation=*/disableValid,
794 AllowPCHWithCompilerErrors));
796 // Recover resources if we crash before exiting this method.
797 llvm::CrashRecoveryContextCleanupRegistrar<ASTReader>
798 ReaderCleanup(Reader.get());
800 Reader->setListener(new ASTInfoCollector(*AST->PP, Context,
801 AST->ASTFileLangOpts,
802 AST->TargetOpts, AST->Target,
805 switch (Reader->ReadAST(Filename, serialization::MK_MainFile,
806 SourceLocation(), ASTReader::ARR_None)) {
807 case ASTReader::Success:
810 case ASTReader::Failure:
811 case ASTReader::Missing:
812 case ASTReader::OutOfDate:
813 case ASTReader::VersionMismatch:
814 case ASTReader::ConfigurationMismatch:
815 case ASTReader::HadErrors:
816 AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
820 AST->OriginalSourceFile = Reader->getOriginalSourceFile();
822 PP.setCounterValue(Counter);
824 // Attach the AST reader to the AST context as an external AST
825 // source, so that declarations will be deserialized from the
826 // AST file as needed.
827 ASTReader *ReaderPtr = Reader.get();
828 OwningPtr<ExternalASTSource> Source(Reader.take());
830 // Unregister the cleanup for ASTReader. It will get cleaned up
831 // by the ASTUnit cleanup.
832 ReaderCleanup.unregister();
834 Context.setExternalSource(Source);
836 // Create an AST consumer, even though it isn't used.
837 AST->Consumer.reset(new ASTConsumer);
839 // Create a semantic analysis object and tell the AST reader about it.
840 AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer));
841 AST->TheSema->Initialize();
842 ReaderPtr->InitializeSema(*AST->TheSema);
843 AST->Reader = ReaderPtr;
845 // Tell the diagnostic client that we have started a source file.
846 AST->getDiagnostics().getClient()->BeginSourceFile(Context.getLangOpts(),&PP);
853 /// \brief Preprocessor callback class that updates a hash value with the names
854 /// of all macros that have been defined by the translation unit.
855 class MacroDefinitionTrackerPPCallbacks : public PPCallbacks {
859 explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { }
861 virtual void MacroDefined(const Token &MacroNameTok,
862 const MacroDirective *MD) {
863 Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash);
867 /// \brief Add the given declaration to the hash of all top-level entities.
868 void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) {
872 DeclContext *DC = D->getDeclContext();
876 if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit()))
879 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
880 if (EnumDecl *EnumD = dyn_cast<EnumDecl>(D)) {
881 // For an unscoped enum include the enumerators in the hash since they
882 // enter the top-level namespace.
883 if (!EnumD->isScoped()) {
884 for (EnumDecl::enumerator_iterator EI = EnumD->enumerator_begin(),
885 EE = EnumD->enumerator_end(); EI != EE; ++EI) {
886 if ((*EI)->getIdentifier())
887 Hash = llvm::HashString((*EI)->getIdentifier()->getName(), Hash);
892 if (ND->getIdentifier())
893 Hash = llvm::HashString(ND->getIdentifier()->getName(), Hash);
894 else if (DeclarationName Name = ND->getDeclName()) {
895 std::string NameStr = Name.getAsString();
896 Hash = llvm::HashString(NameStr, Hash);
901 if (ImportDecl *ImportD = dyn_cast<ImportDecl>(D)) {
902 if (Module *Mod = ImportD->getImportedModule()) {
903 std::string ModName = Mod->getFullModuleName();
904 Hash = llvm::HashString(ModName, Hash);
910 class TopLevelDeclTrackerConsumer : public ASTConsumer {
915 TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
916 : Unit(_Unit), Hash(Hash) {
920 void handleTopLevelDecl(Decl *D) {
924 // FIXME: Currently ObjC method declarations are incorrectly being
925 // reported as top-level declarations, even though their DeclContext
926 // is the containing ObjC @interface/@implementation. This is a
927 // fundamental problem in the parser right now.
928 if (isa<ObjCMethodDecl>(D))
931 AddTopLevelDeclarationToHash(D, Hash);
932 Unit.addTopLevelDecl(D);
934 handleFileLevelDecl(D);
937 void handleFileLevelDecl(Decl *D) {
938 Unit.addFileLevelDecl(D);
939 if (NamespaceDecl *NSD = dyn_cast<NamespaceDecl>(D)) {
940 for (NamespaceDecl::decl_iterator
941 I = NSD->decls_begin(), E = NSD->decls_end(); I != E; ++I)
942 handleFileLevelDecl(*I);
946 bool HandleTopLevelDecl(DeclGroupRef D) {
947 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
948 handleTopLevelDecl(*it);
952 // We're not interested in "interesting" decls.
953 void HandleInterestingDecl(DeclGroupRef) {}
955 void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) {
956 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
957 handleTopLevelDecl(*it);
960 virtual ASTMutationListener *GetASTMutationListener() {
961 return Unit.getASTMutationListener();
964 virtual ASTDeserializationListener *GetASTDeserializationListener() {
965 return Unit.getDeserializationListener();
969 class TopLevelDeclTrackerAction : public ASTFrontendAction {
973 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
975 CI.getPreprocessor().addPPCallbacks(
976 new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
977 return new TopLevelDeclTrackerConsumer(Unit,
978 Unit.getCurrentTopLevelHashValue());
982 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
984 virtual bool hasCodeCompletionSupport() const { return false; }
985 virtual TranslationUnitKind getTranslationUnitKind() {
986 return Unit.getTranslationUnitKind();
990 class PrecompilePreambleAction : public ASTFrontendAction {
992 bool HasEmittedPreamblePCH;
995 explicit PrecompilePreambleAction(ASTUnit &Unit)
996 : Unit(Unit), HasEmittedPreamblePCH(false) {}
998 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
1000 bool hasEmittedPreamblePCH() const { return HasEmittedPreamblePCH; }
1001 void setHasEmittedPreamblePCH() { HasEmittedPreamblePCH = true; }
1002 virtual bool shouldEraseOutputFiles() { return !hasEmittedPreamblePCH(); }
1004 virtual bool hasCodeCompletionSupport() const { return false; }
1005 virtual bool hasASTFileSupport() const { return false; }
1006 virtual TranslationUnitKind getTranslationUnitKind() { return TU_Prefix; }
1009 class PrecompilePreambleConsumer : public PCHGenerator {
1012 std::vector<Decl *> TopLevelDecls;
1013 PrecompilePreambleAction *Action;
1016 PrecompilePreambleConsumer(ASTUnit &Unit, PrecompilePreambleAction *Action,
1017 const Preprocessor &PP, StringRef isysroot,
1019 : PCHGenerator(PP, "", 0, isysroot, Out, /*AllowASTWithErrors=*/true),
1020 Unit(Unit), Hash(Unit.getCurrentTopLevelHashValue()), Action(Action) {
1024 virtual bool HandleTopLevelDecl(DeclGroupRef D) {
1025 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
1027 // FIXME: Currently ObjC method declarations are incorrectly being
1028 // reported as top-level declarations, even though their DeclContext
1029 // is the containing ObjC @interface/@implementation. This is a
1030 // fundamental problem in the parser right now.
1031 if (isa<ObjCMethodDecl>(D))
1033 AddTopLevelDeclarationToHash(D, Hash);
1034 TopLevelDecls.push_back(D);
1039 virtual void HandleTranslationUnit(ASTContext &Ctx) {
1040 PCHGenerator::HandleTranslationUnit(Ctx);
1041 if (hasEmittedPCH()) {
1042 // Translate the top-level declarations we captured during
1043 // parsing into declaration IDs in the precompiled
1044 // preamble. This will allow us to deserialize those top-level
1045 // declarations when requested.
1046 for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I) {
1047 Decl *D = TopLevelDecls[I];
1048 // Invalid top-level decls may not have been serialized.
1049 if (D->isInvalidDecl())
1051 Unit.addTopLevelDeclFromPreamble(getWriter().getDeclID(D));
1054 Action->setHasEmittedPreamblePCH();
1061 ASTConsumer *PrecompilePreambleAction::CreateASTConsumer(CompilerInstance &CI,
1063 std::string Sysroot;
1064 std::string OutputFile;
1065 raw_ostream *OS = 0;
1066 if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot,
1070 if (!CI.getFrontendOpts().RelocatablePCH)
1073 CI.getPreprocessor().addPPCallbacks(new MacroDefinitionTrackerPPCallbacks(
1074 Unit.getCurrentTopLevelHashValue()));
1075 return new PrecompilePreambleConsumer(Unit, this, CI.getPreprocessor(),
1079 static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag) {
1080 return StoredDiag.getLocation().isValid();
1084 checkAndRemoveNonDriverDiags(SmallVectorImpl<StoredDiagnostic> &StoredDiags) {
1085 // Get rid of stored diagnostics except the ones from the driver which do not
1086 // have a source location.
1088 std::remove_if(StoredDiags.begin(), StoredDiags.end(), isNonDriverDiag),
1092 static void checkAndSanitizeDiags(SmallVectorImpl<StoredDiagnostic> &
1094 SourceManager &SM) {
1095 // The stored diagnostic has the old source manager in it; update
1096 // the locations to refer into the new source manager. Since we've
1097 // been careful to make sure that the source manager's state
1098 // before and after are identical, so that we can reuse the source
1100 for (unsigned I = 0, N = StoredDiagnostics.size(); I < N; ++I) {
1101 if (StoredDiagnostics[I].getLocation().isValid()) {
1102 FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), SM);
1103 StoredDiagnostics[I].setLocation(Loc);
1108 /// Parse the source file into a translation unit using the given compiler
1109 /// invocation, replacing the current translation unit.
1111 /// \returns True if a failure occurred that causes the ASTUnit not to
1112 /// contain any translation-unit information, false otherwise.
1113 bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) {
1114 delete SavedMainFileBuffer;
1115 SavedMainFileBuffer = 0;
1118 delete OverrideMainBuffer;
1122 // Create the compiler instance to use for building the AST.
1123 OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1125 // Recover resources if we crash before exiting this method.
1126 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1127 CICleanup(Clang.get());
1129 IntrusiveRefCntPtr<CompilerInvocation>
1130 CCInvocation(new CompilerInvocation(*Invocation));
1132 Clang->setInvocation(CCInvocation.getPtr());
1133 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1135 // Set up diagnostics, capturing any diagnostics that would
1136 // otherwise be dropped.
1137 Clang->setDiagnostics(&getDiagnostics());
1139 // Create the target instance.
1140 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1141 &Clang->getTargetOpts()));
1142 if (!Clang->hasTarget()) {
1143 delete OverrideMainBuffer;
1147 // Inform the target of the language options.
1149 // FIXME: We shouldn't need to do this, the target should be immutable once
1150 // created. This complexity should be lifted elsewhere.
1151 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1153 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1154 "Invocation must have exactly one source file!");
1155 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
1156 "FIXME: AST inputs not yet supported here!");
1157 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
1158 "IR inputs not support here!");
1160 // Configure the various subsystems.
1161 // FIXME: Should we retain the previous file manager?
1162 LangOpts = &Clang->getLangOpts();
1163 FileSystemOpts = Clang->getFileSystemOpts();
1164 FileMgr = new FileManager(FileSystemOpts);
1165 SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
1166 UserFilesAreVolatile);
1172 // Clear out old caches and data.
1173 TopLevelDecls.clear();
1174 clearFileLevelDecls();
1175 CleanTemporaryFiles();
1177 if (!OverrideMainBuffer) {
1178 checkAndRemoveNonDriverDiags(StoredDiagnostics);
1179 TopLevelDeclsInPreamble.clear();
1182 // Create a file manager object to provide access to and cache the filesystem.
1183 Clang->setFileManager(&getFileManager());
1185 // Create the source manager.
1186 Clang->setSourceManager(&getSourceManager());
1188 // If the main file has been overridden due to the use of a preamble,
1189 // make that override happen and introduce the preamble.
1190 PreprocessorOptions &PreprocessorOpts = Clang->getPreprocessorOpts();
1191 if (OverrideMainBuffer) {
1192 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
1193 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
1194 PreprocessorOpts.PrecompiledPreambleBytes.second
1195 = PreambleEndsAtStartOfLine;
1196 PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this);
1197 PreprocessorOpts.DisablePCHValidation = true;
1199 // The stored diagnostic has the old source manager in it; update
1200 // the locations to refer into the new source manager. Since we've
1201 // been careful to make sure that the source manager's state
1202 // before and after are identical, so that we can reuse the source
1204 checkAndSanitizeDiags(StoredDiagnostics, getSourceManager());
1206 // Keep track of the override buffer;
1207 SavedMainFileBuffer = OverrideMainBuffer;
1210 OwningPtr<TopLevelDeclTrackerAction> Act(
1211 new TopLevelDeclTrackerAction(*this));
1213 // Recover resources if we crash before exiting this method.
1214 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1215 ActCleanup(Act.get());
1217 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
1220 if (OverrideMainBuffer) {
1221 std::string ModName = getPreambleFile(this);
1222 TranslateStoredDiagnostics(Clang->getModuleManager(), ModName,
1223 getSourceManager(), PreambleDiagnostics,
1227 if (!Act->Execute())
1230 transferASTDataFromCompilerInstance(*Clang);
1232 Act->EndSourceFile();
1234 FailedParseDiagnostics.clear();
1239 // Remove the overridden buffer we used for the preamble.
1240 if (OverrideMainBuffer) {
1241 delete OverrideMainBuffer;
1242 SavedMainFileBuffer = 0;
1245 // Keep the ownership of the data in the ASTUnit because the client may
1246 // want to see the diagnostics.
1247 transferASTDataFromCompilerInstance(*Clang);
1248 FailedParseDiagnostics.swap(StoredDiagnostics);
1249 StoredDiagnostics.clear();
1250 NumStoredDiagnosticsFromDriver = 0;
1254 /// \brief Simple function to retrieve a path for a preamble precompiled header.
1255 static std::string GetPreamblePCHPath() {
1256 // FIXME: This is a hack so that we can override the preamble file during
1257 // crash-recovery testing, which is the only case where the preamble files
1258 // are not necessarily cleaned up.
1259 const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
1263 SmallString<128> Path;
1264 llvm::sys::fs::createTemporaryFile("preamble", "pch", Path);
1269 /// \brief Compute the preamble for the main file, providing the source buffer
1270 /// that corresponds to the main file along with a pair (bytes, start-of-line)
1271 /// that describes the preamble.
1272 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
1273 ASTUnit::ComputePreamble(CompilerInvocation &Invocation,
1274 unsigned MaxLines, bool &CreatedBuffer) {
1275 FrontendOptions &FrontendOpts = Invocation.getFrontendOpts();
1276 PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts();
1277 CreatedBuffer = false;
1279 // Try to determine if the main file has been remapped, either from the
1280 // command line (to another file) or directly through the compiler invocation
1281 // (to a memory buffer).
1282 llvm::MemoryBuffer *Buffer = 0;
1283 std::string MainFilePath(FrontendOpts.Inputs[0].getFile());
1284 llvm::sys::fs::UniqueID MainFileID;
1285 if (!llvm::sys::fs::getUniqueID(MainFilePath, MainFileID)) {
1286 // Check whether there is a file-file remapping of the main file
1287 for (PreprocessorOptions::remapped_file_iterator
1288 M = PreprocessorOpts.remapped_file_begin(),
1289 E = PreprocessorOpts.remapped_file_end();
1292 std::string MPath(M->first);
1293 llvm::sys::fs::UniqueID MID;
1294 if (!llvm::sys::fs::getUniqueID(MPath, MID)) {
1295 if (MainFileID == MID) {
1296 // We found a remapping. Try to load the resulting, remapped source.
1297 if (CreatedBuffer) {
1299 CreatedBuffer = false;
1302 Buffer = getBufferForFile(M->second);
1304 return std::make_pair((llvm::MemoryBuffer*)0,
1305 std::make_pair(0, true));
1306 CreatedBuffer = true;
1311 // Check whether there is a file-buffer remapping. It supercedes the
1312 // file-file remapping.
1313 for (PreprocessorOptions::remapped_file_buffer_iterator
1314 M = PreprocessorOpts.remapped_file_buffer_begin(),
1315 E = PreprocessorOpts.remapped_file_buffer_end();
1318 std::string MPath(M->first);
1319 llvm::sys::fs::UniqueID MID;
1320 if (!llvm::sys::fs::getUniqueID(MPath, MID)) {
1321 if (MainFileID == MID) {
1322 // We found a remapping.
1323 if (CreatedBuffer) {
1325 CreatedBuffer = false;
1328 Buffer = const_cast<llvm::MemoryBuffer *>(M->second);
1334 // If the main source file was not remapped, load it now.
1336 Buffer = getBufferForFile(FrontendOpts.Inputs[0].getFile());
1338 return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true));
1340 CreatedBuffer = true;
1343 return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer,
1344 *Invocation.getLangOpts(),
1348 static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old,
1350 StringRef NewName) {
1351 llvm::MemoryBuffer *Result
1352 = llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName);
1353 memcpy(const_cast<char*>(Result->getBufferStart()),
1354 Old->getBufferStart(), Old->getBufferSize());
1355 memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(),
1356 ' ', NewSize - Old->getBufferSize() - 1);
1357 const_cast<char*>(Result->getBufferEnd())[-1] = '\n';
1362 /// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing
1363 /// the source file.
1365 /// This routine will compute the preamble of the main source file. If a
1366 /// non-trivial preamble is found, it will precompile that preamble into a
1367 /// precompiled header so that the precompiled preamble can be used to reduce
1368 /// reparsing time. If a precompiled preamble has already been constructed,
1369 /// this routine will determine if it is still valid and, if so, avoid
1370 /// rebuilding the precompiled preamble.
1372 /// \param AllowRebuild When true (the default), this routine is
1373 /// allowed to rebuild the precompiled preamble if it is found to be
1376 /// \param MaxLines When non-zero, the maximum number of lines that
1377 /// can occur within the preamble.
1379 /// \returns If the precompiled preamble can be used, returns a newly-allocated
1380 /// buffer that should be used in place of the main file when doing so.
1381 /// Otherwise, returns a NULL pointer.
1382 llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
1383 const CompilerInvocation &PreambleInvocationIn,
1385 unsigned MaxLines) {
1387 IntrusiveRefCntPtr<CompilerInvocation>
1388 PreambleInvocation(new CompilerInvocation(PreambleInvocationIn));
1389 FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts();
1390 PreprocessorOptions &PreprocessorOpts
1391 = PreambleInvocation->getPreprocessorOpts();
1393 bool CreatedPreambleBuffer = false;
1394 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble
1395 = ComputePreamble(*PreambleInvocation, MaxLines, CreatedPreambleBuffer);
1397 // If ComputePreamble() Take ownership of the preamble buffer.
1398 OwningPtr<llvm::MemoryBuffer> OwnedPreambleBuffer;
1399 if (CreatedPreambleBuffer)
1400 OwnedPreambleBuffer.reset(NewPreamble.first);
1402 if (!NewPreamble.second.first) {
1403 // We couldn't find a preamble in the main source. Clear out the current
1404 // preamble, if we have one. It's obviously no good any more.
1406 erasePreambleFile(this);
1408 // The next time we actually see a preamble, precompile it.
1409 PreambleRebuildCounter = 1;
1413 if (!Preamble.empty()) {
1414 // We've previously computed a preamble. Check whether we have the same
1415 // preamble now that we did before, and that there's enough space in
1416 // the main-file buffer within the precompiled preamble to fit the
1418 if (Preamble.size() == NewPreamble.second.first &&
1419 PreambleEndsAtStartOfLine == NewPreamble.second.second &&
1420 NewPreamble.first->getBufferSize() < PreambleReservedSize-2 &&
1421 memcmp(Preamble.getBufferStart(), NewPreamble.first->getBufferStart(),
1422 NewPreamble.second.first) == 0) {
1423 // The preamble has not changed. We may be able to re-use the precompiled
1426 // Check that none of the files used by the preamble have changed.
1427 bool AnyFileChanged = false;
1429 // First, make a record of those files that have been overridden via
1430 // remapping or unsaved_files.
1431 llvm::StringMap<std::pair<off_t, time_t> > OverriddenFiles;
1432 for (PreprocessorOptions::remapped_file_iterator
1433 R = PreprocessorOpts.remapped_file_begin(),
1434 REnd = PreprocessorOpts.remapped_file_end();
1435 !AnyFileChanged && R != REnd;
1437 llvm::sys::fs::file_status Status;
1438 if (FileMgr->getNoncachedStatValue(R->second, Status)) {
1439 // If we can't stat the file we're remapping to, assume that something
1440 // horrible happened.
1441 AnyFileChanged = true;
1445 OverriddenFiles[R->first] = std::make_pair(
1446 Status.getSize(), Status.getLastModificationTime().toEpochTime());
1448 for (PreprocessorOptions::remapped_file_buffer_iterator
1449 R = PreprocessorOpts.remapped_file_buffer_begin(),
1450 REnd = PreprocessorOpts.remapped_file_buffer_end();
1451 !AnyFileChanged && R != REnd;
1453 // FIXME: Should we actually compare the contents of file->buffer
1455 OverriddenFiles[R->first] = std::make_pair(R->second->getBufferSize(),
1459 // Check whether anything has changed.
1460 for (llvm::StringMap<std::pair<off_t, time_t> >::iterator
1461 F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end();
1462 !AnyFileChanged && F != FEnd;
1464 llvm::StringMap<std::pair<off_t, time_t> >::iterator Overridden
1465 = OverriddenFiles.find(F->first());
1466 if (Overridden != OverriddenFiles.end()) {
1467 // This file was remapped; check whether the newly-mapped file
1468 // matches up with the previous mapping.
1469 if (Overridden->second != F->second)
1470 AnyFileChanged = true;
1474 // The file was not remapped; check whether it has changed on disk.
1475 llvm::sys::fs::file_status Status;
1476 if (FileMgr->getNoncachedStatValue(F->first(), Status)) {
1477 // If we can't stat the file, assume that something horrible happened.
1478 AnyFileChanged = true;
1479 } else if (Status.getSize() != uint64_t(F->second.first) ||
1480 Status.getLastModificationTime().toEpochTime() !=
1481 uint64_t(F->second.second))
1482 AnyFileChanged = true;
1485 if (!AnyFileChanged) {
1486 // Okay! We can re-use the precompiled preamble.
1488 // Set the state of the diagnostic object to mimic its state
1489 // after parsing the preamble.
1490 getDiagnostics().Reset();
1491 ProcessWarningOptions(getDiagnostics(),
1492 PreambleInvocation->getDiagnosticOpts());
1493 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1495 // Create a version of the main file buffer that is padded to
1496 // buffer size we reserved when creating the preamble.
1497 return CreatePaddedMainFileBuffer(NewPreamble.first,
1498 PreambleReservedSize,
1499 FrontendOpts.Inputs[0].getFile());
1503 // If we aren't allowed to rebuild the precompiled preamble, just
1508 // We can't reuse the previously-computed preamble. Build a new one.
1510 PreambleDiagnostics.clear();
1511 erasePreambleFile(this);
1512 PreambleRebuildCounter = 1;
1513 } else if (!AllowRebuild) {
1514 // We aren't allowed to rebuild the precompiled preamble; just
1519 // If the preamble rebuild counter > 1, it's because we previously
1520 // failed to build a preamble and we're not yet ready to try
1521 // again. Decrement the counter and return a failure.
1522 if (PreambleRebuildCounter > 1) {
1523 --PreambleRebuildCounter;
1527 // Create a temporary file for the precompiled preamble. In rare
1528 // circumstances, this can fail.
1529 std::string PreamblePCHPath = GetPreamblePCHPath();
1530 if (PreamblePCHPath.empty()) {
1531 // Try again next time.
1532 PreambleRebuildCounter = 1;
1536 // We did not previously compute a preamble, or it can't be reused anyway.
1537 SimpleTimer PreambleTimer(WantTiming);
1538 PreambleTimer.setOutput("Precompiling preamble");
1540 // Create a new buffer that stores the preamble. The buffer also contains
1541 // extra space for the original contents of the file (which will be present
1542 // when we actually parse the file) along with more room in case the file
1544 PreambleReservedSize = NewPreamble.first->getBufferSize();
1545 if (PreambleReservedSize < 4096)
1546 PreambleReservedSize = 8191;
1548 PreambleReservedSize *= 2;
1550 // Save the preamble text for later; we'll need to compare against it for
1551 // subsequent reparses.
1552 StringRef MainFilename = PreambleInvocation->getFrontendOpts().Inputs[0].getFile();
1553 Preamble.assign(FileMgr->getFile(MainFilename),
1554 NewPreamble.first->getBufferStart(),
1555 NewPreamble.first->getBufferStart()
1556 + NewPreamble.second.first);
1557 PreambleEndsAtStartOfLine = NewPreamble.second.second;
1559 delete PreambleBuffer;
1561 = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize,
1562 FrontendOpts.Inputs[0].getFile());
1563 memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()),
1564 NewPreamble.first->getBufferStart(), Preamble.size());
1565 memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(),
1566 ' ', PreambleReservedSize - Preamble.size() - 1);
1567 const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n';
1569 // Remap the main source file to the preamble buffer.
1570 StringRef MainFilePath = FrontendOpts.Inputs[0].getFile();
1571 PreprocessorOpts.addRemappedFile(MainFilePath, PreambleBuffer);
1573 // Tell the compiler invocation to generate a temporary precompiled header.
1574 FrontendOpts.ProgramAction = frontend::GeneratePCH;
1575 // FIXME: Generate the precompiled header into memory?
1576 FrontendOpts.OutputFile = PreamblePCHPath;
1577 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
1578 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
1580 // Create the compiler instance to use for building the precompiled preamble.
1581 OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1583 // Recover resources if we crash before exiting this method.
1584 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1585 CICleanup(Clang.get());
1587 Clang->setInvocation(&*PreambleInvocation);
1588 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1590 // Set up diagnostics, capturing all of the diagnostics produced.
1591 Clang->setDiagnostics(&getDiagnostics());
1593 // Create the target instance.
1594 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1595 &Clang->getTargetOpts()));
1596 if (!Clang->hasTarget()) {
1597 llvm::sys::fs::remove(FrontendOpts.OutputFile);
1599 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1600 PreprocessorOpts.eraseRemappedFile(
1601 PreprocessorOpts.remapped_file_buffer_end() - 1);
1605 // Inform the target of the language options.
1607 // FIXME: We shouldn't need to do this, the target should be immutable once
1608 // created. This complexity should be lifted elsewhere.
1609 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1611 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1612 "Invocation must have exactly one source file!");
1613 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
1614 "FIXME: AST inputs not yet supported here!");
1615 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
1616 "IR inputs not support here!");
1618 // Clear out old caches and data.
1619 getDiagnostics().Reset();
1620 ProcessWarningOptions(getDiagnostics(), Clang->getDiagnosticOpts());
1621 checkAndRemoveNonDriverDiags(StoredDiagnostics);
1622 TopLevelDecls.clear();
1623 TopLevelDeclsInPreamble.clear();
1625 // Create a file manager object to provide access to and cache the filesystem.
1626 Clang->setFileManager(new FileManager(Clang->getFileSystemOpts()));
1628 // Create the source manager.
1629 Clang->setSourceManager(new SourceManager(getDiagnostics(),
1630 Clang->getFileManager()));
1632 OwningPtr<PrecompilePreambleAction> Act;
1633 Act.reset(new PrecompilePreambleAction(*this));
1634 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
1635 llvm::sys::fs::remove(FrontendOpts.OutputFile);
1637 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1638 PreprocessorOpts.eraseRemappedFile(
1639 PreprocessorOpts.remapped_file_buffer_end() - 1);
1644 Act->EndSourceFile();
1646 if (!Act->hasEmittedPreamblePCH()) {
1647 // The preamble PCH failed (e.g. there was a module loading fatal error),
1648 // so no precompiled header was generated. Forget that we even tried.
1649 // FIXME: Should we leave a note for ourselves to try again?
1650 llvm::sys::fs::remove(FrontendOpts.OutputFile);
1652 TopLevelDeclsInPreamble.clear();
1653 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1654 PreprocessorOpts.eraseRemappedFile(
1655 PreprocessorOpts.remapped_file_buffer_end() - 1);
1659 // Transfer any diagnostics generated when parsing the preamble into the set
1660 // of preamble diagnostics.
1661 PreambleDiagnostics.clear();
1662 PreambleDiagnostics.insert(PreambleDiagnostics.end(),
1663 stored_diag_afterDriver_begin(), stored_diag_end());
1664 checkAndRemoveNonDriverDiags(StoredDiagnostics);
1666 // Keep track of the preamble we precompiled.
1667 setPreambleFile(this, FrontendOpts.OutputFile);
1668 NumWarningsInPreamble = getDiagnostics().getNumWarnings();
1670 // Keep track of all of the files that the source manager knows about,
1671 // so we can verify whether they have changed or not.
1672 FilesInPreamble.clear();
1673 SourceManager &SourceMgr = Clang->getSourceManager();
1674 const llvm::MemoryBuffer *MainFileBuffer
1675 = SourceMgr.getBuffer(SourceMgr.getMainFileID());
1676 for (SourceManager::fileinfo_iterator F = SourceMgr.fileinfo_begin(),
1677 FEnd = SourceMgr.fileinfo_end();
1680 const FileEntry *File = F->second->OrigEntry;
1681 if (!File || F->second->getRawBuffer() == MainFileBuffer)
1684 FilesInPreamble[File->getName()]
1685 = std::make_pair(F->second->getSize(), File->getModificationTime());
1688 PreambleRebuildCounter = 1;
1689 PreprocessorOpts.eraseRemappedFile(
1690 PreprocessorOpts.remapped_file_buffer_end() - 1);
1692 // If the hash of top-level entities differs from the hash of the top-level
1693 // entities the last time we rebuilt the preamble, clear out the completion
1695 if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1696 CompletionCacheTopLevelHashValue = 0;
1697 PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1700 return CreatePaddedMainFileBuffer(NewPreamble.first,
1701 PreambleReservedSize,
1702 FrontendOpts.Inputs[0].getFile());
1705 void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1706 std::vector<Decl *> Resolved;
1707 Resolved.reserve(TopLevelDeclsInPreamble.size());
1708 ExternalASTSource &Source = *getASTContext().getExternalSource();
1709 for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) {
1710 // Resolve the declaration ID to an actual declaration, possibly
1711 // deserializing the declaration in the process.
1712 Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]);
1714 Resolved.push_back(D);
1716 TopLevelDeclsInPreamble.clear();
1717 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1720 void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
1721 // Steal the created target, context, and preprocessor.
1722 TheSema.reset(CI.takeSema());
1723 Consumer.reset(CI.takeASTConsumer());
1724 Ctx = &CI.getASTContext();
1725 PP = &CI.getPreprocessor();
1726 CI.setSourceManager(0);
1727 CI.setFileManager(0);
1728 Target = &CI.getTarget();
1729 Reader = CI.getModuleManager();
1730 HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure();
1733 StringRef ASTUnit::getMainFileName() const {
1734 if (Invocation && !Invocation->getFrontendOpts().Inputs.empty()) {
1735 const FrontendInputFile &Input = Invocation->getFrontendOpts().Inputs[0];
1737 return Input.getFile();
1739 return Input.getBuffer()->getBufferIdentifier();
1743 if (const FileEntry *
1744 FE = SourceMgr->getFileEntryForID(SourceMgr->getMainFileID()))
1745 return FE->getName();
1751 StringRef ASTUnit::getASTFileName() const {
1752 if (!isMainFileAST())
1755 serialization::ModuleFile &
1756 Mod = Reader->getModuleManager().getPrimaryModule();
1757 return Mod.FileName;
1760 ASTUnit *ASTUnit::create(CompilerInvocation *CI,
1761 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1762 bool CaptureDiagnostics,
1763 bool UserFilesAreVolatile) {
1764 OwningPtr<ASTUnit> AST;
1765 AST.reset(new ASTUnit(false));
1766 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
1767 AST->Diagnostics = Diags;
1768 AST->Invocation = CI;
1769 AST->FileSystemOpts = CI->getFileSystemOpts();
1770 AST->FileMgr = new FileManager(AST->FileSystemOpts);
1771 AST->UserFilesAreVolatile = UserFilesAreVolatile;
1772 AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
1773 UserFilesAreVolatile);
1778 ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(CompilerInvocation *CI,
1779 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1780 ASTFrontendAction *Action,
1783 StringRef ResourceFilesPath,
1784 bool OnlyLocalDecls,
1785 bool CaptureDiagnostics,
1786 bool PrecompilePreamble,
1787 bool CacheCodeCompletionResults,
1788 bool IncludeBriefCommentsInCodeCompletion,
1789 bool UserFilesAreVolatile,
1790 OwningPtr<ASTUnit> *ErrAST) {
1791 assert(CI && "A CompilerInvocation is required");
1793 OwningPtr<ASTUnit> OwnAST;
1794 ASTUnit *AST = Unit;
1796 // Create the AST unit.
1797 OwnAST.reset(create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile));
1801 if (!ResourceFilesPath.empty()) {
1802 // Override the resources path.
1803 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1805 AST->OnlyLocalDecls = OnlyLocalDecls;
1806 AST->CaptureDiagnostics = CaptureDiagnostics;
1807 if (PrecompilePreamble)
1808 AST->PreambleRebuildCounter = 2;
1809 AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete;
1810 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1811 AST->IncludeBriefCommentsInCodeCompletion
1812 = IncludeBriefCommentsInCodeCompletion;
1814 // Recover resources if we crash before exiting this method.
1815 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1816 ASTUnitCleanup(OwnAST.get());
1817 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1818 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
1819 DiagCleanup(Diags.getPtr());
1821 // We'll manage file buffers ourselves.
1822 CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1823 CI->getFrontendOpts().DisableFree = false;
1824 ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
1826 // Create the compiler instance to use for building the AST.
1827 OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1829 // Recover resources if we crash before exiting this method.
1830 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1831 CICleanup(Clang.get());
1833 Clang->setInvocation(CI);
1834 AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1836 // Set up diagnostics, capturing any diagnostics that would
1837 // otherwise be dropped.
1838 Clang->setDiagnostics(&AST->getDiagnostics());
1840 // Create the target instance.
1841 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1842 &Clang->getTargetOpts()));
1843 if (!Clang->hasTarget())
1846 // Inform the target of the language options.
1848 // FIXME: We shouldn't need to do this, the target should be immutable once
1849 // created. This complexity should be lifted elsewhere.
1850 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1852 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1853 "Invocation must have exactly one source file!");
1854 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
1855 "FIXME: AST inputs not yet supported here!");
1856 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
1857 "IR inputs not supported here!");
1859 // Configure the various subsystems.
1860 AST->TheSema.reset();
1865 // Create a file manager object to provide access to and cache the filesystem.
1866 Clang->setFileManager(&AST->getFileManager());
1868 // Create the source manager.
1869 Clang->setSourceManager(&AST->getSourceManager());
1871 ASTFrontendAction *Act = Action;
1873 OwningPtr<TopLevelDeclTrackerAction> TrackerAct;
1875 TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
1876 Act = TrackerAct.get();
1879 // Recover resources if we crash before exiting this method.
1880 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1881 ActCleanup(TrackerAct.get());
1883 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
1884 AST->transferASTDataFromCompilerInstance(*Clang);
1885 if (OwnAST && ErrAST)
1886 ErrAST->swap(OwnAST);
1891 if (Persistent && !TrackerAct) {
1892 Clang->getPreprocessor().addPPCallbacks(
1893 new MacroDefinitionTrackerPPCallbacks(AST->getCurrentTopLevelHashValue()));
1894 std::vector<ASTConsumer*> Consumers;
1895 if (Clang->hasASTConsumer())
1896 Consumers.push_back(Clang->takeASTConsumer());
1897 Consumers.push_back(new TopLevelDeclTrackerConsumer(*AST,
1898 AST->getCurrentTopLevelHashValue()));
1899 Clang->setASTConsumer(new MultiplexConsumer(Consumers));
1901 if (!Act->Execute()) {
1902 AST->transferASTDataFromCompilerInstance(*Clang);
1903 if (OwnAST && ErrAST)
1904 ErrAST->swap(OwnAST);
1909 // Steal the created target, context, and preprocessor.
1910 AST->transferASTDataFromCompilerInstance(*Clang);
1912 Act->EndSourceFile();
1915 return OwnAST.take();
1920 bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) {
1924 // We'll manage file buffers ourselves.
1925 Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1926 Invocation->getFrontendOpts().DisableFree = false;
1927 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1929 llvm::MemoryBuffer *OverrideMainBuffer = 0;
1930 if (PrecompilePreamble) {
1931 PreambleRebuildCounter = 2;
1933 = getMainBufferWithPrecompiledPreamble(*Invocation);
1936 SimpleTimer ParsingTimer(WantTiming);
1937 ParsingTimer.setOutput("Parsing " + getMainFileName());
1939 // Recover resources if we crash before exiting this method.
1940 llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1941 MemBufferCleanup(OverrideMainBuffer);
1943 return Parse(OverrideMainBuffer);
1946 ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
1947 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1948 bool OnlyLocalDecls,
1949 bool CaptureDiagnostics,
1950 bool PrecompilePreamble,
1951 TranslationUnitKind TUKind,
1952 bool CacheCodeCompletionResults,
1953 bool IncludeBriefCommentsInCodeCompletion,
1954 bool UserFilesAreVolatile) {
1955 // Create the AST unit.
1956 OwningPtr<ASTUnit> AST;
1957 AST.reset(new ASTUnit(false));
1958 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
1959 AST->Diagnostics = Diags;
1960 AST->OnlyLocalDecls = OnlyLocalDecls;
1961 AST->CaptureDiagnostics = CaptureDiagnostics;
1962 AST->TUKind = TUKind;
1963 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1964 AST->IncludeBriefCommentsInCodeCompletion
1965 = IncludeBriefCommentsInCodeCompletion;
1966 AST->Invocation = CI;
1967 AST->FileSystemOpts = CI->getFileSystemOpts();
1968 AST->FileMgr = new FileManager(AST->FileSystemOpts);
1969 AST->UserFilesAreVolatile = UserFilesAreVolatile;
1971 // Recover resources if we crash before exiting this method.
1972 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1973 ASTUnitCleanup(AST.get());
1974 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1975 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
1976 DiagCleanup(Diags.getPtr());
1978 return AST->LoadFromCompilerInvocation(PrecompilePreamble)? 0 : AST.take();
1981 ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
1982 const char **ArgEnd,
1983 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1984 StringRef ResourceFilesPath,
1985 bool OnlyLocalDecls,
1986 bool CaptureDiagnostics,
1987 RemappedFile *RemappedFiles,
1988 unsigned NumRemappedFiles,
1989 bool RemappedFilesKeepOriginalName,
1990 bool PrecompilePreamble,
1991 TranslationUnitKind TUKind,
1992 bool CacheCodeCompletionResults,
1993 bool IncludeBriefCommentsInCodeCompletion,
1994 bool AllowPCHWithCompilerErrors,
1995 bool SkipFunctionBodies,
1996 bool UserFilesAreVolatile,
1997 bool ForSerialization,
1998 OwningPtr<ASTUnit> *ErrAST) {
1999 if (!Diags.getPtr()) {
2000 // No diagnostics engine was provided, so create our own diagnostics object
2001 // with the default options.
2002 Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions());
2005 SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
2007 IntrusiveRefCntPtr<CompilerInvocation> CI;
2011 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
2014 CI = clang::createInvocationFromCommandLine(
2015 llvm::makeArrayRef(ArgBegin, ArgEnd),
2021 // Override any files that need remapping
2022 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
2023 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
2024 if (const llvm::MemoryBuffer *
2025 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
2026 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, memBuf);
2028 const char *fname = fileOrBuf.get<const char *>();
2029 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, fname);
2032 PreprocessorOptions &PPOpts = CI->getPreprocessorOpts();
2033 PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName;
2034 PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors;
2036 // Override the resources path.
2037 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
2039 CI->getFrontendOpts().SkipFunctionBodies = SkipFunctionBodies;
2041 // Create the AST unit.
2042 OwningPtr<ASTUnit> AST;
2043 AST.reset(new ASTUnit(false));
2044 ConfigureDiags(Diags, ArgBegin, ArgEnd, *AST, CaptureDiagnostics);
2045 AST->Diagnostics = Diags;
2046 Diags = 0; // Zero out now to ease cleanup during crash recovery.
2047 AST->FileSystemOpts = CI->getFileSystemOpts();
2048 AST->FileMgr = new FileManager(AST->FileSystemOpts);
2049 AST->OnlyLocalDecls = OnlyLocalDecls;
2050 AST->CaptureDiagnostics = CaptureDiagnostics;
2051 AST->TUKind = TUKind;
2052 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
2053 AST->IncludeBriefCommentsInCodeCompletion
2054 = IncludeBriefCommentsInCodeCompletion;
2055 AST->UserFilesAreVolatile = UserFilesAreVolatile;
2056 AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
2057 AST->StoredDiagnostics.swap(StoredDiagnostics);
2058 AST->Invocation = CI;
2059 if (ForSerialization)
2060 AST->WriterData.reset(new ASTWriterData());
2061 CI = 0; // Zero out now to ease cleanup during crash recovery.
2063 // Recover resources if we crash before exiting this method.
2064 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
2065 ASTUnitCleanup(AST.get());
2067 if (AST->LoadFromCompilerInvocation(PrecompilePreamble)) {
2068 // Some error occurred, if caller wants to examine diagnostics, pass it the
2071 AST->StoredDiagnostics.swap(AST->FailedParseDiagnostics);
2080 bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) {
2084 clearFileLevelDecls();
2086 SimpleTimer ParsingTimer(WantTiming);
2087 ParsingTimer.setOutput("Reparsing " + getMainFileName());
2090 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
2091 for (PreprocessorOptions::remapped_file_buffer_iterator
2092 R = PPOpts.remapped_file_buffer_begin(),
2093 REnd = PPOpts.remapped_file_buffer_end();
2098 Invocation->getPreprocessorOpts().clearRemappedFiles();
2099 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
2100 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
2101 if (const llvm::MemoryBuffer *
2102 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
2103 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
2106 const char *fname = fileOrBuf.get<const char *>();
2107 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
2112 // If we have a preamble file lying around, or if we might try to
2113 // build a precompiled preamble, do so now.
2114 llvm::MemoryBuffer *OverrideMainBuffer = 0;
2115 if (!getPreambleFile(this).empty() || PreambleRebuildCounter > 0)
2116 OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation);
2118 // Clear out the diagnostics state.
2119 getDiagnostics().Reset();
2120 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
2121 if (OverrideMainBuffer)
2122 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
2124 // Parse the sources
2125 bool Result = Parse(OverrideMainBuffer);
2127 // If we're caching global code-completion results, and the top-level
2128 // declarations have changed, clear out the code-completion cache.
2129 if (!Result && ShouldCacheCodeCompletionResults &&
2130 CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
2131 CacheCodeCompletionResults();
2133 // We now need to clear out the completion info related to this translation
2134 // unit; it'll be recreated if necessary.
2140 //----------------------------------------------------------------------------//
2142 //----------------------------------------------------------------------------//
2145 /// \brief Code completion consumer that combines the cached code-completion
2146 /// results from an ASTUnit with the code-completion results provided to it,
2147 /// then passes the result on to
2148 class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
2149 uint64_t NormalContexts;
2151 CodeCompleteConsumer &Next;
2154 AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
2155 const CodeCompleteOptions &CodeCompleteOpts)
2156 : CodeCompleteConsumer(CodeCompleteOpts, Next.isOutputBinary()),
2157 AST(AST), Next(Next)
2159 // Compute the set of contexts in which we will look when we don't have
2160 // any information about the specific context.
2162 = (1LL << CodeCompletionContext::CCC_TopLevel)
2163 | (1LL << CodeCompletionContext::CCC_ObjCInterface)
2164 | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
2165 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
2166 | (1LL << CodeCompletionContext::CCC_Statement)
2167 | (1LL << CodeCompletionContext::CCC_Expression)
2168 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
2169 | (1LL << CodeCompletionContext::CCC_DotMemberAccess)
2170 | (1LL << CodeCompletionContext::CCC_ArrowMemberAccess)
2171 | (1LL << CodeCompletionContext::CCC_ObjCPropertyAccess)
2172 | (1LL << CodeCompletionContext::CCC_ObjCProtocolName)
2173 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
2174 | (1LL << CodeCompletionContext::CCC_Recovery);
2176 if (AST.getASTContext().getLangOpts().CPlusPlus)
2177 NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag)
2178 | (1LL << CodeCompletionContext::CCC_UnionTag)
2179 | (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
2182 virtual void ProcessCodeCompleteResults(Sema &S,
2183 CodeCompletionContext Context,
2184 CodeCompletionResult *Results,
2185 unsigned NumResults);
2187 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
2188 OverloadCandidate *Candidates,
2189 unsigned NumCandidates) {
2190 Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
2193 virtual CodeCompletionAllocator &getAllocator() {
2194 return Next.getAllocator();
2197 virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() {
2198 return Next.getCodeCompletionTUInfo();
2203 /// \brief Helper function that computes which global names are hidden by the
2204 /// local code-completion results.
2205 static void CalculateHiddenNames(const CodeCompletionContext &Context,
2206 CodeCompletionResult *Results,
2207 unsigned NumResults,
2209 llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
2210 bool OnlyTagNames = false;
2211 switch (Context.getKind()) {
2212 case CodeCompletionContext::CCC_Recovery:
2213 case CodeCompletionContext::CCC_TopLevel:
2214 case CodeCompletionContext::CCC_ObjCInterface:
2215 case CodeCompletionContext::CCC_ObjCImplementation:
2216 case CodeCompletionContext::CCC_ObjCIvarList:
2217 case CodeCompletionContext::CCC_ClassStructUnion:
2218 case CodeCompletionContext::CCC_Statement:
2219 case CodeCompletionContext::CCC_Expression:
2220 case CodeCompletionContext::CCC_ObjCMessageReceiver:
2221 case CodeCompletionContext::CCC_DotMemberAccess:
2222 case CodeCompletionContext::CCC_ArrowMemberAccess:
2223 case CodeCompletionContext::CCC_ObjCPropertyAccess:
2224 case CodeCompletionContext::CCC_Namespace:
2225 case CodeCompletionContext::CCC_Type:
2226 case CodeCompletionContext::CCC_Name:
2227 case CodeCompletionContext::CCC_PotentiallyQualifiedName:
2228 case CodeCompletionContext::CCC_ParenthesizedExpression:
2229 case CodeCompletionContext::CCC_ObjCInterfaceName:
2232 case CodeCompletionContext::CCC_EnumTag:
2233 case CodeCompletionContext::CCC_UnionTag:
2234 case CodeCompletionContext::CCC_ClassOrStructTag:
2235 OnlyTagNames = true;
2238 case CodeCompletionContext::CCC_ObjCProtocolName:
2239 case CodeCompletionContext::CCC_MacroName:
2240 case CodeCompletionContext::CCC_MacroNameUse:
2241 case CodeCompletionContext::CCC_PreprocessorExpression:
2242 case CodeCompletionContext::CCC_PreprocessorDirective:
2243 case CodeCompletionContext::CCC_NaturalLanguage:
2244 case CodeCompletionContext::CCC_SelectorName:
2245 case CodeCompletionContext::CCC_TypeQualifiers:
2246 case CodeCompletionContext::CCC_Other:
2247 case CodeCompletionContext::CCC_OtherWithMacros:
2248 case CodeCompletionContext::CCC_ObjCInstanceMessage:
2249 case CodeCompletionContext::CCC_ObjCClassMessage:
2250 case CodeCompletionContext::CCC_ObjCCategoryName:
2251 // We're looking for nothing, or we're looking for names that cannot
2256 typedef CodeCompletionResult Result;
2257 for (unsigned I = 0; I != NumResults; ++I) {
2258 if (Results[I].Kind != Result::RK_Declaration)
2262 = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
2264 bool Hiding = false;
2266 Hiding = (IDNS & Decl::IDNS_Tag);
2268 unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
2269 Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
2270 Decl::IDNS_NonMemberOperator);
2271 if (Ctx.getLangOpts().CPlusPlus)
2272 HiddenIDNS |= Decl::IDNS_Tag;
2273 Hiding = (IDNS & HiddenIDNS);
2279 DeclarationName Name = Results[I].Declaration->getDeclName();
2280 if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
2281 HiddenNames.insert(Identifier->getName());
2283 HiddenNames.insert(Name.getAsString());
2288 void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
2289 CodeCompletionContext Context,
2290 CodeCompletionResult *Results,
2291 unsigned NumResults) {
2292 // Merge the results we were given with the results we cached.
2293 bool AddedResult = false;
2294 uint64_t InContexts =
2295 Context.getKind() == CodeCompletionContext::CCC_Recovery
2296 ? NormalContexts : (1LL << Context.getKind());
2297 // Contains the set of names that are hidden by "local" completion results.
2298 llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
2299 typedef CodeCompletionResult Result;
2300 SmallVector<Result, 8> AllResults;
2301 for (ASTUnit::cached_completion_iterator
2302 C = AST.cached_completion_begin(),
2303 CEnd = AST.cached_completion_end();
2305 // If the context we are in matches any of the contexts we are
2306 // interested in, we'll add this result.
2307 if ((C->ShowInContexts & InContexts) == 0)
2310 // If we haven't added any results previously, do so now.
2312 CalculateHiddenNames(Context, Results, NumResults, S.Context,
2314 AllResults.insert(AllResults.end(), Results, Results + NumResults);
2318 // Determine whether this global completion result is hidden by a local
2319 // completion result. If so, skip it.
2320 if (C->Kind != CXCursor_MacroDefinition &&
2321 HiddenNames.count(C->Completion->getTypedText()))
2324 // Adjust priority based on similar type classes.
2325 unsigned Priority = C->Priority;
2326 CodeCompletionString *Completion = C->Completion;
2327 if (!Context.getPreferredType().isNull()) {
2328 if (C->Kind == CXCursor_MacroDefinition) {
2329 Priority = getMacroUsagePriority(C->Completion->getTypedText(),
2331 Context.getPreferredType()->isAnyPointerType());
2332 } else if (C->Type) {
2333 CanQualType Expected
2334 = S.Context.getCanonicalType(
2335 Context.getPreferredType().getUnqualifiedType());
2336 SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
2337 if (ExpectedSTC == C->TypeClass) {
2338 // We know this type is similar; check for an exact match.
2339 llvm::StringMap<unsigned> &CachedCompletionTypes
2340 = AST.getCachedCompletionTypes();
2341 llvm::StringMap<unsigned>::iterator Pos
2342 = CachedCompletionTypes.find(QualType(Expected).getAsString());
2343 if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
2344 Priority /= CCF_ExactTypeMatch;
2346 Priority /= CCF_SimilarTypeMatch;
2351 // Adjust the completion string, if required.
2352 if (C->Kind == CXCursor_MacroDefinition &&
2353 Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
2354 // Create a new code-completion string that just contains the
2355 // macro name, without its arguments.
2356 CodeCompletionBuilder Builder(getAllocator(), getCodeCompletionTUInfo(),
2357 CCP_CodePattern, C->Availability);
2358 Builder.AddTypedTextChunk(C->Completion->getTypedText());
2359 Priority = CCP_CodePattern;
2360 Completion = Builder.TakeString();
2363 AllResults.push_back(Result(Completion, Priority, C->Kind,
2367 // If we did not add any cached completion results, just forward the
2368 // results we were given to the next consumer.
2370 Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
2374 Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
2380 void ASTUnit::CodeComplete(StringRef File, unsigned Line, unsigned Column,
2381 RemappedFile *RemappedFiles,
2382 unsigned NumRemappedFiles,
2384 bool IncludeCodePatterns,
2385 bool IncludeBriefComments,
2386 CodeCompleteConsumer &Consumer,
2387 DiagnosticsEngine &Diag, LangOptions &LangOpts,
2388 SourceManager &SourceMgr, FileManager &FileMgr,
2389 SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2390 SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
2394 SimpleTimer CompletionTimer(WantTiming);
2395 CompletionTimer.setOutput("Code completion @ " + File + ":" +
2396 Twine(Line) + ":" + Twine(Column));
2398 IntrusiveRefCntPtr<CompilerInvocation>
2399 CCInvocation(new CompilerInvocation(*Invocation));
2401 FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2402 CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts;
2403 PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2405 CodeCompleteOpts.IncludeMacros = IncludeMacros &&
2406 CachedCompletionResults.empty();
2407 CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns;
2408 CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty();
2409 CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments;
2411 assert(IncludeBriefComments == this->IncludeBriefCommentsInCodeCompletion);
2413 FrontendOpts.CodeCompletionAt.FileName = File;
2414 FrontendOpts.CodeCompletionAt.Line = Line;
2415 FrontendOpts.CodeCompletionAt.Column = Column;
2417 // Set the language options appropriately.
2418 LangOpts = *CCInvocation->getLangOpts();
2420 OwningPtr<CompilerInstance> Clang(new CompilerInstance());
2422 // Recover resources if we crash before exiting this method.
2423 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2424 CICleanup(Clang.get());
2426 Clang->setInvocation(&*CCInvocation);
2427 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
2429 // Set up diagnostics, capturing any diagnostics produced.
2430 Clang->setDiagnostics(&Diag);
2431 CaptureDroppedDiagnostics Capture(true,
2432 Clang->getDiagnostics(),
2434 ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts());
2436 // Create the target instance.
2437 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
2438 &Clang->getTargetOpts()));
2439 if (!Clang->hasTarget()) {
2440 Clang->setInvocation(0);
2444 // Inform the target of the language options.
2446 // FIXME: We shouldn't need to do this, the target should be immutable once
2447 // created. This complexity should be lifted elsewhere.
2448 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
2450 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2451 "Invocation must have exactly one source file!");
2452 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
2453 "FIXME: AST inputs not yet supported here!");
2454 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
2455 "IR inputs not support here!");
2458 // Use the source and file managers that we were given.
2459 Clang->setFileManager(&FileMgr);
2460 Clang->setSourceManager(&SourceMgr);
2463 PreprocessorOpts.clearRemappedFiles();
2464 PreprocessorOpts.RetainRemappedFileBuffers = true;
2465 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
2466 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
2467 if (const llvm::MemoryBuffer *
2468 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
2469 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, memBuf);
2470 OwnedBuffers.push_back(memBuf);
2472 const char *fname = fileOrBuf.get<const char *>();
2473 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, fname);
2477 // Use the code completion consumer we were given, but adding any cached
2478 // code-completion results.
2479 AugmentedCodeCompleteConsumer *AugmentedConsumer
2480 = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts);
2481 Clang->setCodeCompletionConsumer(AugmentedConsumer);
2483 // If we have a precompiled preamble, try to use it. We only allow
2484 // the use of the precompiled preamble if we're if the completion
2485 // point is within the main file, after the end of the precompiled
2487 llvm::MemoryBuffer *OverrideMainBuffer = 0;
2488 if (!getPreambleFile(this).empty()) {
2489 std::string CompleteFilePath(File);
2490 llvm::sys::fs::UniqueID CompleteFileID;
2492 if (!llvm::sys::fs::getUniqueID(CompleteFilePath, CompleteFileID)) {
2493 std::string MainPath(OriginalSourceFile);
2494 llvm::sys::fs::UniqueID MainID;
2495 if (!llvm::sys::fs::getUniqueID(MainPath, MainID)) {
2496 if (CompleteFileID == MainID && Line > 1)
2498 = getMainBufferWithPrecompiledPreamble(*CCInvocation, false,
2504 // If the main file has been overridden due to the use of a preamble,
2505 // make that override happen and introduce the preamble.
2506 if (OverrideMainBuffer) {
2507 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
2508 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
2509 PreprocessorOpts.PrecompiledPreambleBytes.second
2510 = PreambleEndsAtStartOfLine;
2511 PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this);
2512 PreprocessorOpts.DisablePCHValidation = true;
2514 OwnedBuffers.push_back(OverrideMainBuffer);
2516 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2517 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2520 // Disable the preprocessing record if modules are not enabled.
2521 if (!Clang->getLangOpts().Modules)
2522 PreprocessorOpts.DetailedRecord = false;
2524 OwningPtr<SyntaxOnlyAction> Act;
2525 Act.reset(new SyntaxOnlyAction);
2526 if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
2528 Act->EndSourceFile();
2532 bool ASTUnit::Save(StringRef File) {
2533 if (HadModuleLoaderFatalFailure)
2536 // Write to a temporary file and later rename it to the actual file, to avoid
2537 // possible race conditions.
2538 SmallString<128> TempPath;
2540 TempPath += "-%%%%%%%%";
2542 if (llvm::sys::fs::createUniqueFile(TempPath.str(), fd, TempPath))
2545 // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2546 // unconditionally create a stat cache when we parse the file?
2547 llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true);
2551 if (Out.has_error()) {
2556 if (llvm::sys::fs::rename(TempPath.str(), File)) {
2558 llvm::sys::fs::remove(TempPath.str(), exists);
2565 static bool serializeUnit(ASTWriter &Writer,
2566 SmallVectorImpl<char> &Buffer,
2570 Writer.WriteAST(S, std::string(), 0, "", hasErrors);
2572 // Write the generated bitstream to "Out".
2573 if (!Buffer.empty())
2574 OS.write(Buffer.data(), Buffer.size());
2579 bool ASTUnit::serialize(raw_ostream &OS) {
2580 bool hasErrors = getDiagnostics().hasErrorOccurred();
2583 return serializeUnit(WriterData->Writer, WriterData->Buffer,
2584 getSema(), hasErrors, OS);
2586 SmallString<128> Buffer;
2587 llvm::BitstreamWriter Stream(Buffer);
2588 ASTWriter Writer(Stream);
2589 return serializeUnit(Writer, Buffer, getSema(), hasErrors, OS);
2592 typedef ContinuousRangeMap<unsigned, int, 2> SLocRemap;
2594 static void TranslateSLoc(SourceLocation &L, SLocRemap &Remap) {
2595 unsigned Raw = L.getRawEncoding();
2596 const unsigned MacroBit = 1U << 31;
2597 L = SourceLocation::getFromRawEncoding((Raw & MacroBit) |
2598 ((Raw & ~MacroBit) + Remap.find(Raw & ~MacroBit)->second));
2601 void ASTUnit::TranslateStoredDiagnostics(
2604 SourceManager &SrcMgr,
2605 const SmallVectorImpl<StoredDiagnostic> &Diags,
2606 SmallVectorImpl<StoredDiagnostic> &Out) {
2607 // The stored diagnostic has the old source manager in it; update
2608 // the locations to refer into the new source manager. We also need to remap
2609 // all the locations to the new view. This includes the diag location, any
2610 // associated source ranges, and the source ranges of associated fix-its.
2611 // FIXME: There should be a cleaner way to do this.
2613 SmallVector<StoredDiagnostic, 4> Result;
2614 Result.reserve(Diags.size());
2615 assert(MMan && "Don't have a module manager");
2616 serialization::ModuleFile *Mod = MMan->ModuleMgr.lookup(ModName);
2617 assert(Mod && "Don't have preamble module");
2618 SLocRemap &Remap = Mod->SLocRemap;
2619 for (unsigned I = 0, N = Diags.size(); I != N; ++I) {
2620 // Rebuild the StoredDiagnostic.
2621 const StoredDiagnostic &SD = Diags[I];
2622 SourceLocation L = SD.getLocation();
2623 TranslateSLoc(L, Remap);
2624 FullSourceLoc Loc(L, SrcMgr);
2626 SmallVector<CharSourceRange, 4> Ranges;
2627 Ranges.reserve(SD.range_size());
2628 for (StoredDiagnostic::range_iterator I = SD.range_begin(),
2631 SourceLocation BL = I->getBegin();
2632 TranslateSLoc(BL, Remap);
2633 SourceLocation EL = I->getEnd();
2634 TranslateSLoc(EL, Remap);
2635 Ranges.push_back(CharSourceRange(SourceRange(BL, EL), I->isTokenRange()));
2638 SmallVector<FixItHint, 2> FixIts;
2639 FixIts.reserve(SD.fixit_size());
2640 for (StoredDiagnostic::fixit_iterator I = SD.fixit_begin(),
2643 FixIts.push_back(FixItHint());
2644 FixItHint &FH = FixIts.back();
2645 FH.CodeToInsert = I->CodeToInsert;
2646 SourceLocation BL = I->RemoveRange.getBegin();
2647 TranslateSLoc(BL, Remap);
2648 SourceLocation EL = I->RemoveRange.getEnd();
2649 TranslateSLoc(EL, Remap);
2650 FH.RemoveRange = CharSourceRange(SourceRange(BL, EL),
2651 I->RemoveRange.isTokenRange());
2654 Result.push_back(StoredDiagnostic(SD.getLevel(), SD.getID(),
2655 SD.getMessage(), Loc, Ranges, FixIts));
2660 void ASTUnit::addFileLevelDecl(Decl *D) {
2663 // We only care about local declarations.
2664 if (D->isFromASTFile())
2667 SourceManager &SM = *SourceMgr;
2668 SourceLocation Loc = D->getLocation();
2669 if (Loc.isInvalid() || !SM.isLocalSourceLocation(Loc))
2672 // We only keep track of the file-level declarations of each file.
2673 if (!D->getLexicalDeclContext()->isFileContext())
2676 SourceLocation FileLoc = SM.getFileLoc(Loc);
2677 assert(SM.isLocalSourceLocation(FileLoc));
2680 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
2681 if (FID.isInvalid())
2684 LocDeclsTy *&Decls = FileDecls[FID];
2686 Decls = new LocDeclsTy();
2688 std::pair<unsigned, Decl *> LocDecl(Offset, D);
2690 if (Decls->empty() || Decls->back().first <= Offset) {
2691 Decls->push_back(LocDecl);
2695 LocDeclsTy::iterator I = std::upper_bound(Decls->begin(), Decls->end(),
2696 LocDecl, llvm::less_first());
2698 Decls->insert(I, LocDecl);
2701 void ASTUnit::findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
2702 SmallVectorImpl<Decl *> &Decls) {
2703 if (File.isInvalid())
2706 if (SourceMgr->isLoadedFileID(File)) {
2707 assert(Ctx->getExternalSource() && "No external source!");
2708 return Ctx->getExternalSource()->FindFileRegionDecls(File, Offset, Length,
2712 FileDeclsTy::iterator I = FileDecls.find(File);
2713 if (I == FileDecls.end())
2716 LocDeclsTy &LocDecls = *I->second;
2717 if (LocDecls.empty())
2720 LocDeclsTy::iterator BeginIt =
2721 std::lower_bound(LocDecls.begin(), LocDecls.end(),
2722 std::make_pair(Offset, (Decl *)0), llvm::less_first());
2723 if (BeginIt != LocDecls.begin())
2726 // If we are pointing at a top-level decl inside an objc container, we need
2727 // to backtrack until we find it otherwise we will fail to report that the
2728 // region overlaps with an objc container.
2729 while (BeginIt != LocDecls.begin() &&
2730 BeginIt->second->isTopLevelDeclInObjCContainer())
2733 LocDeclsTy::iterator EndIt = std::upper_bound(
2734 LocDecls.begin(), LocDecls.end(),
2735 std::make_pair(Offset + Length, (Decl *)0), llvm::less_first());
2736 if (EndIt != LocDecls.end())
2739 for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt)
2740 Decls.push_back(DIt->second);
2743 SourceLocation ASTUnit::getLocation(const FileEntry *File,
2744 unsigned Line, unsigned Col) const {
2745 const SourceManager &SM = getSourceManager();
2746 SourceLocation Loc = SM.translateFileLineCol(File, Line, Col);
2747 return SM.getMacroArgExpandedLocation(Loc);
2750 SourceLocation ASTUnit::getLocation(const FileEntry *File,
2751 unsigned Offset) const {
2752 const SourceManager &SM = getSourceManager();
2753 SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1);
2754 return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
2757 /// \brief If \arg Loc is a loaded location from the preamble, returns
2758 /// the corresponding local location of the main file, otherwise it returns
2760 SourceLocation ASTUnit::mapLocationFromPreamble(SourceLocation Loc) {
2763 PreambleID = SourceMgr->getPreambleFileID();
2765 if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
2769 if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble.size()) {
2770 SourceLocation FileLoc
2771 = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID());
2772 return FileLoc.getLocWithOffset(Offs);
2778 /// \brief If \arg Loc is a local location of the main file but inside the
2779 /// preamble chunk, returns the corresponding loaded location from the
2780 /// preamble, otherwise it returns \arg Loc.
2781 SourceLocation ASTUnit::mapLocationToPreamble(SourceLocation Loc) {
2784 PreambleID = SourceMgr->getPreambleFileID();
2786 if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
2790 if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) &&
2791 Offs < Preamble.size()) {
2792 SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID);
2793 return FileLoc.getLocWithOffset(Offs);
2799 bool ASTUnit::isInPreambleFileID(SourceLocation Loc) {
2802 FID = SourceMgr->getPreambleFileID();
2804 if (Loc.isInvalid() || FID.isInvalid())
2807 return SourceMgr->isInFileID(Loc, FID);
2810 bool ASTUnit::isInMainFileID(SourceLocation Loc) {
2813 FID = SourceMgr->getMainFileID();
2815 if (Loc.isInvalid() || FID.isInvalid())
2818 return SourceMgr->isInFileID(Loc, FID);
2821 SourceLocation ASTUnit::getEndOfPreambleFileID() {
2824 FID = SourceMgr->getPreambleFileID();
2826 if (FID.isInvalid())
2827 return SourceLocation();
2829 return SourceMgr->getLocForEndOfFile(FID);
2832 SourceLocation ASTUnit::getStartOfMainFileID() {
2835 FID = SourceMgr->getMainFileID();
2837 if (FID.isInvalid())
2838 return SourceLocation();
2840 return SourceMgr->getLocForStartOfFile(FID);
2843 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
2844 ASTUnit::getLocalPreprocessingEntities() const {
2845 if (isMainFileAST()) {
2846 serialization::ModuleFile &
2847 Mod = Reader->getModuleManager().getPrimaryModule();
2848 return Reader->getModulePreprocessedEntities(Mod);
2851 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
2852 return std::make_pair(PPRec->local_begin(), PPRec->local_end());
2854 return std::make_pair(PreprocessingRecord::iterator(),
2855 PreprocessingRecord::iterator());
2858 bool ASTUnit::visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn) {
2859 if (isMainFileAST()) {
2860 serialization::ModuleFile &
2861 Mod = Reader->getModuleManager().getPrimaryModule();
2862 ASTReader::ModuleDeclIterator MDI, MDE;
2863 llvm::tie(MDI, MDE) = Reader->getModuleFileLevelDecls(Mod);
2864 for (; MDI != MDE; ++MDI) {
2865 if (!Fn(context, *MDI))
2872 for (ASTUnit::top_level_iterator TL = top_level_begin(),
2873 TLEnd = top_level_end();
2874 TL != TLEnd; ++TL) {
2875 if (!Fn(context, *TL))
2883 struct PCHLocatorInfo {
2884 serialization::ModuleFile *Mod;
2885 PCHLocatorInfo() : Mod(0) {}
2889 static bool PCHLocator(serialization::ModuleFile &M, void *UserData) {
2890 PCHLocatorInfo &Info = *static_cast<PCHLocatorInfo*>(UserData);
2892 case serialization::MK_Module:
2893 return true; // skip dependencies.
2894 case serialization::MK_PCH:
2896 return true; // found it.
2897 case serialization::MK_Preamble:
2898 return false; // look in dependencies.
2899 case serialization::MK_MainFile:
2900 return false; // look in dependencies.
2906 const FileEntry *ASTUnit::getPCHFile() {
2910 PCHLocatorInfo Info;
2911 Reader->getModuleManager().visit(PCHLocator, &Info);
2913 return Info.Mod->File;
2918 bool ASTUnit::isModuleFile() {
2919 return isMainFileAST() && !ASTFileLangOpts.CurrentModule.empty();
2922 void ASTUnit::PreambleData::countLines() const {
2927 for (std::vector<char>::const_iterator
2928 I = Buffer.begin(), E = Buffer.end(); I != E; ++I) {
2932 if (Buffer.back() != '\n')
2937 ASTUnit::ConcurrencyState::ConcurrencyState() {
2938 Mutex = new llvm::sys::MutexImpl(/*recursive=*/true);
2941 ASTUnit::ConcurrencyState::~ConcurrencyState() {
2942 delete static_cast<llvm::sys::MutexImpl *>(Mutex);
2945 void ASTUnit::ConcurrencyState::start() {
2946 bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire();
2947 assert(acquired && "Concurrent access to ASTUnit!");
2950 void ASTUnit::ConcurrencyState::finish() {
2951 static_cast<llvm::sys::MutexImpl *>(Mutex)->release();
2956 ASTUnit::ConcurrencyState::ConcurrencyState() {}
2957 ASTUnit::ConcurrencyState::~ConcurrencyState() {}
2958 void ASTUnit::ConcurrencyState::start() {}
2959 void ASTUnit::ConcurrencyState::finish() {}