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/Serialization/ASTReader.h"
33 #include "clang/Serialization/ASTWriter.h"
34 #include "llvm/ADT/ArrayRef.h"
35 #include "llvm/ADT/StringExtras.h"
36 #include "llvm/ADT/StringSet.h"
37 #include "llvm/Support/Atomic.h"
38 #include "llvm/Support/CrashRecoveryContext.h"
39 #include "llvm/Support/FileSystem.h"
40 #include "llvm/Support/Host.h"
41 #include "llvm/Support/MemoryBuffer.h"
42 #include "llvm/Support/Mutex.h"
43 #include "llvm/Support/MutexGuard.h"
44 #include "llvm/Support/Path.h"
45 #include "llvm/Support/Timer.h"
46 #include "llvm/Support/raw_ostream.h"
50 using namespace clang;
52 using llvm::TimeRecord;
61 explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
63 Start = TimeRecord::getCurrentTime();
66 void setOutput(const Twine &Output) {
68 this->Output = Output.str();
73 TimeRecord Elapsed = TimeRecord::getCurrentTime();
75 llvm::errs() << Output << ':';
76 Elapsed.print(Elapsed, llvm::errs());
83 /// \brief The file in which the precompiled preamble is stored.
84 std::string PreambleFile;
86 /// \brief Temporary files that should be removed when the ASTUnit is
88 SmallVector<llvm::sys::Path, 4> TemporaryFiles;
90 /// \brief Erase temporary files.
91 void CleanTemporaryFiles();
93 /// \brief Erase the preamble file.
94 void CleanPreambleFile();
96 /// \brief Erase temporary files and the preamble file.
101 static llvm::sys::SmartMutex<false> &getOnDiskMutex() {
102 static llvm::sys::SmartMutex<false> M(/* recursive = */ true);
106 static void cleanupOnDiskMapAtExit();
108 typedef llvm::DenseMap<const ASTUnit *, OnDiskData *> OnDiskDataMap;
109 static OnDiskDataMap &getOnDiskDataMap() {
110 static OnDiskDataMap M;
111 static bool hasRegisteredAtExit = false;
112 if (!hasRegisteredAtExit) {
113 hasRegisteredAtExit = true;
114 atexit(cleanupOnDiskMapAtExit);
119 static void cleanupOnDiskMapAtExit() {
120 // Use the mutex because there can be an alive thread destroying an ASTUnit.
121 llvm::MutexGuard Guard(getOnDiskMutex());
122 OnDiskDataMap &M = getOnDiskDataMap();
123 for (OnDiskDataMap::iterator I = M.begin(), E = M.end(); I != E; ++I) {
124 // We don't worry about freeing the memory associated with OnDiskDataMap.
125 // All we care about is erasing stale files.
126 I->second->Cleanup();
130 static OnDiskData &getOnDiskData(const ASTUnit *AU) {
131 // We require the mutex since we are modifying the structure of the
133 llvm::MutexGuard Guard(getOnDiskMutex());
134 OnDiskDataMap &M = getOnDiskDataMap();
135 OnDiskData *&D = M[AU];
137 D = new OnDiskData();
141 static void erasePreambleFile(const ASTUnit *AU) {
142 getOnDiskData(AU).CleanPreambleFile();
145 static void removeOnDiskEntry(const ASTUnit *AU) {
146 // We require the mutex since we are modifying the structure of the
148 llvm::MutexGuard Guard(getOnDiskMutex());
149 OnDiskDataMap &M = getOnDiskDataMap();
150 OnDiskDataMap::iterator I = M.find(AU);
152 I->second->Cleanup();
158 static void setPreambleFile(const ASTUnit *AU, StringRef preambleFile) {
159 getOnDiskData(AU).PreambleFile = preambleFile;
162 static const std::string &getPreambleFile(const ASTUnit *AU) {
163 return getOnDiskData(AU).PreambleFile;
166 void OnDiskData::CleanTemporaryFiles() {
167 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
168 TemporaryFiles[I].eraseFromDisk();
169 TemporaryFiles.clear();
172 void OnDiskData::CleanPreambleFile() {
173 if (!PreambleFile.empty()) {
174 llvm::sys::Path(PreambleFile).eraseFromDisk();
175 PreambleFile.clear();
179 void OnDiskData::Cleanup() {
180 CleanTemporaryFiles();
184 struct ASTUnit::ASTWriterData {
185 SmallString<128> Buffer;
186 llvm::BitstreamWriter Stream;
189 ASTWriterData() : Stream(Buffer), Writer(Stream) { }
192 void ASTUnit::clearFileLevelDecls() {
193 for (FileDeclsTy::iterator
194 I = FileDecls.begin(), E = FileDecls.end(); I != E; ++I)
199 void ASTUnit::CleanTemporaryFiles() {
200 getOnDiskData(this).CleanTemporaryFiles();
203 void ASTUnit::addTemporaryFile(const llvm::sys::Path &TempFile) {
204 getOnDiskData(this).TemporaryFiles.push_back(TempFile);
207 /// \brief After failing to build a precompiled preamble (due to
208 /// errors in the source that occurs in the preamble), the number of
209 /// reparses during which we'll skip even trying to precompile the
211 const unsigned DefaultPreambleRebuildInterval = 5;
213 /// \brief Tracks the number of ASTUnit objects that are currently active.
215 /// Used for debugging purposes only.
216 static llvm::sys::cas_flag ActiveASTUnitObjects;
218 ASTUnit::ASTUnit(bool _MainFileIsAST)
219 : Reader(0), OnlyLocalDecls(false), CaptureDiagnostics(false),
220 MainFileIsAST(_MainFileIsAST),
221 TUKind(TU_Complete), WantTiming(getenv("LIBCLANG_TIMING")),
222 OwnsRemappedFileBuffers(true),
223 NumStoredDiagnosticsFromDriver(0),
224 PreambleRebuildCounter(0), SavedMainFileBuffer(0), PreambleBuffer(0),
225 NumWarningsInPreamble(0),
226 ShouldCacheCodeCompletionResults(false),
227 IncludeBriefCommentsInCodeCompletion(false), UserFilesAreVolatile(false),
228 CompletionCacheTopLevelHashValue(0),
229 PreambleTopLevelHashValue(0),
230 CurrentTopLevelHashValue(0),
231 UnsafeToFree(false) {
232 if (getenv("LIBCLANG_OBJTRACKING")) {
233 llvm::sys::AtomicIncrement(&ActiveASTUnitObjects);
234 fprintf(stderr, "+++ %d translation units\n", ActiveASTUnitObjects);
238 ASTUnit::~ASTUnit() {
239 // If we loaded from an AST file, balance out the BeginSourceFile call.
240 if (MainFileIsAST && getDiagnostics().getClient()) {
241 getDiagnostics().getClient()->EndSourceFile();
244 clearFileLevelDecls();
246 // Clean up the temporary files and the preamble file.
247 removeOnDiskEntry(this);
249 // Free the buffers associated with remapped files. We are required to
250 // perform this operation here because we explicitly request that the
251 // compiler instance *not* free these buffers for each invocation of the
253 if (Invocation.getPtr() && OwnsRemappedFileBuffers) {
254 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
255 for (PreprocessorOptions::remapped_file_buffer_iterator
256 FB = PPOpts.remapped_file_buffer_begin(),
257 FBEnd = PPOpts.remapped_file_buffer_end();
263 delete SavedMainFileBuffer;
264 delete PreambleBuffer;
266 ClearCachedCompletionResults();
268 if (getenv("LIBCLANG_OBJTRACKING")) {
269 llvm::sys::AtomicDecrement(&ActiveASTUnitObjects);
270 fprintf(stderr, "--- %d translation units\n", ActiveASTUnitObjects);
274 void ASTUnit::setPreprocessor(Preprocessor *pp) { PP = pp; }
276 /// \brief Determine the set of code-completion contexts in which this
277 /// declaration should be shown.
278 static unsigned getDeclShowContexts(const NamedDecl *ND,
279 const LangOptions &LangOpts,
280 bool &IsNestedNameSpecifier) {
281 IsNestedNameSpecifier = false;
283 if (isa<UsingShadowDecl>(ND))
284 ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl());
288 uint64_t Contexts = 0;
289 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) ||
290 isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) {
291 // Types can appear in these contexts.
292 if (LangOpts.CPlusPlus || !isa<TagDecl>(ND))
293 Contexts |= (1LL << CodeCompletionContext::CCC_TopLevel)
294 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
295 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
296 | (1LL << CodeCompletionContext::CCC_Statement)
297 | (1LL << CodeCompletionContext::CCC_Type)
298 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
300 // In C++, types can appear in expressions contexts (for functional casts).
301 if (LangOpts.CPlusPlus)
302 Contexts |= (1LL << CodeCompletionContext::CCC_Expression);
304 // In Objective-C, message sends can send interfaces. In Objective-C++,
305 // all types are available due to functional casts.
306 if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND))
307 Contexts |= (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver);
309 // In Objective-C, you can only be a subclass of another Objective-C class
310 if (isa<ObjCInterfaceDecl>(ND))
311 Contexts |= (1LL << CodeCompletionContext::CCC_ObjCInterfaceName);
313 // Deal with tag names.
314 if (isa<EnumDecl>(ND)) {
315 Contexts |= (1LL << CodeCompletionContext::CCC_EnumTag);
317 // Part of the nested-name-specifier in C++0x.
318 if (LangOpts.CPlusPlus11)
319 IsNestedNameSpecifier = true;
320 } else if (const RecordDecl *Record = dyn_cast<RecordDecl>(ND)) {
321 if (Record->isUnion())
322 Contexts |= (1LL << CodeCompletionContext::CCC_UnionTag);
324 Contexts |= (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
326 if (LangOpts.CPlusPlus)
327 IsNestedNameSpecifier = true;
328 } else if (isa<ClassTemplateDecl>(ND))
329 IsNestedNameSpecifier = true;
330 } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) {
331 // Values can appear in these contexts.
332 Contexts = (1LL << CodeCompletionContext::CCC_Statement)
333 | (1LL << CodeCompletionContext::CCC_Expression)
334 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
335 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver);
336 } else if (isa<ObjCProtocolDecl>(ND)) {
337 Contexts = (1LL << CodeCompletionContext::CCC_ObjCProtocolName);
338 } else if (isa<ObjCCategoryDecl>(ND)) {
339 Contexts = (1LL << CodeCompletionContext::CCC_ObjCCategoryName);
340 } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
341 Contexts = (1LL << CodeCompletionContext::CCC_Namespace);
343 // Part of the nested-name-specifier.
344 IsNestedNameSpecifier = true;
350 void ASTUnit::CacheCodeCompletionResults() {
354 SimpleTimer Timer(WantTiming);
355 Timer.setOutput("Cache global code completions for " + getMainFileName());
357 // Clear out the previous results.
358 ClearCachedCompletionResults();
360 // Gather the set of global code completions.
361 typedef CodeCompletionResult Result;
362 SmallVector<Result, 8> Results;
363 CachedCompletionAllocator = new GlobalCodeCompletionAllocator;
364 CodeCompletionTUInfo CCTUInfo(CachedCompletionAllocator);
365 TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator,
368 // Translate global code completions into cached completions.
369 llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
371 for (unsigned I = 0, N = Results.size(); I != N; ++I) {
372 switch (Results[I].Kind) {
373 case Result::RK_Declaration: {
374 bool IsNestedNameSpecifier = false;
375 CachedCodeCompletionResult CachedResult;
376 CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema,
377 *CachedCompletionAllocator,
379 IncludeBriefCommentsInCodeCompletion);
380 CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration,
382 IsNestedNameSpecifier);
383 CachedResult.Priority = Results[I].Priority;
384 CachedResult.Kind = Results[I].CursorKind;
385 CachedResult.Availability = Results[I].Availability;
387 // Keep track of the type of this completion in an ASTContext-agnostic
389 QualType UsageType = getDeclUsageType(*Ctx, Results[I].Declaration);
390 if (UsageType.isNull()) {
391 CachedResult.TypeClass = STC_Void;
392 CachedResult.Type = 0;
394 CanQualType CanUsageType
395 = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
396 CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
398 // Determine whether we have already seen this type. If so, we save
399 // ourselves the work of formatting the type string by using the
400 // temporary, CanQualType-based hash table to find the associated value.
401 unsigned &TypeValue = CompletionTypes[CanUsageType];
402 if (TypeValue == 0) {
403 TypeValue = CompletionTypes.size();
404 CachedCompletionTypes[QualType(CanUsageType).getAsString()]
408 CachedResult.Type = TypeValue;
411 CachedCompletionResults.push_back(CachedResult);
413 /// Handle nested-name-specifiers in C++.
414 if (TheSema->Context.getLangOpts().CPlusPlus &&
415 IsNestedNameSpecifier && !Results[I].StartsNestedNameSpecifier) {
416 // The contexts in which a nested-name-specifier can appear in C++.
418 = (1LL << CodeCompletionContext::CCC_TopLevel)
419 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
420 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
421 | (1LL << CodeCompletionContext::CCC_Statement)
422 | (1LL << CodeCompletionContext::CCC_Expression)
423 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
424 | (1LL << CodeCompletionContext::CCC_EnumTag)
425 | (1LL << CodeCompletionContext::CCC_UnionTag)
426 | (1LL << CodeCompletionContext::CCC_ClassOrStructTag)
427 | (1LL << CodeCompletionContext::CCC_Type)
428 | (1LL << CodeCompletionContext::CCC_PotentiallyQualifiedName)
429 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
431 if (isa<NamespaceDecl>(Results[I].Declaration) ||
432 isa<NamespaceAliasDecl>(Results[I].Declaration))
433 NNSContexts |= (1LL << CodeCompletionContext::CCC_Namespace);
435 if (unsigned RemainingContexts
436 = NNSContexts & ~CachedResult.ShowInContexts) {
437 // If there any contexts where this completion can be a
438 // nested-name-specifier but isn't already an option, create a
439 // nested-name-specifier completion.
440 Results[I].StartsNestedNameSpecifier = true;
441 CachedResult.Completion
442 = Results[I].CreateCodeCompletionString(*TheSema,
443 *CachedCompletionAllocator,
445 IncludeBriefCommentsInCodeCompletion);
446 CachedResult.ShowInContexts = RemainingContexts;
447 CachedResult.Priority = CCP_NestedNameSpecifier;
448 CachedResult.TypeClass = STC_Void;
449 CachedResult.Type = 0;
450 CachedCompletionResults.push_back(CachedResult);
456 case Result::RK_Keyword:
457 case Result::RK_Pattern:
458 // Ignore keywords and patterns; we don't care, since they are so
459 // easily regenerated.
462 case Result::RK_Macro: {
463 CachedCodeCompletionResult CachedResult;
464 CachedResult.Completion
465 = Results[I].CreateCodeCompletionString(*TheSema,
466 *CachedCompletionAllocator,
468 IncludeBriefCommentsInCodeCompletion);
469 CachedResult.ShowInContexts
470 = (1LL << CodeCompletionContext::CCC_TopLevel)
471 | (1LL << CodeCompletionContext::CCC_ObjCInterface)
472 | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
473 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
474 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
475 | (1LL << CodeCompletionContext::CCC_Statement)
476 | (1LL << CodeCompletionContext::CCC_Expression)
477 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
478 | (1LL << CodeCompletionContext::CCC_MacroNameUse)
479 | (1LL << CodeCompletionContext::CCC_PreprocessorExpression)
480 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
481 | (1LL << CodeCompletionContext::CCC_OtherWithMacros);
483 CachedResult.Priority = Results[I].Priority;
484 CachedResult.Kind = Results[I].CursorKind;
485 CachedResult.Availability = Results[I].Availability;
486 CachedResult.TypeClass = STC_Void;
487 CachedResult.Type = 0;
488 CachedCompletionResults.push_back(CachedResult);
494 // Save the current top-level hash value.
495 CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
498 void ASTUnit::ClearCachedCompletionResults() {
499 CachedCompletionResults.clear();
500 CachedCompletionTypes.clear();
501 CachedCompletionAllocator = 0;
506 /// \brief Gathers information from ASTReader that will be used to initialize
508 class ASTInfoCollector : public ASTReaderListener {
511 LangOptions &LangOpt;
513 IntrusiveRefCntPtr<TargetOptions> &TargetOpts;
514 IntrusiveRefCntPtr<TargetInfo> &Target;
517 unsigned NumHeaderInfos;
519 bool InitializedLanguage;
521 ASTInfoCollector(Preprocessor &PP, ASTContext &Context, LangOptions &LangOpt,
523 IntrusiveRefCntPtr<TargetOptions> &TargetOpts,
524 IntrusiveRefCntPtr<TargetInfo> &Target,
526 : PP(PP), Context(Context), LangOpt(LangOpt), HSI(HSI),
527 TargetOpts(TargetOpts), Target(Target),
528 Counter(Counter), NumHeaderInfos(0),
529 InitializedLanguage(false) {}
531 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
533 if (InitializedLanguage)
537 InitializedLanguage = true;
543 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
545 // If we've already initialized the target, don't do it again.
549 this->TargetOpts = new TargetOptions(TargetOpts);
550 Target = TargetInfo::CreateTargetInfo(PP.getDiagnostics(),
557 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {
558 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
561 virtual void ReadCounter(const serialization::ModuleFile &M, unsigned Value) {
567 if (!Target || !InitializedLanguage)
570 // Inform the target of the language options.
572 // FIXME: We shouldn't need to do this, the target should be immutable once
573 // created. This complexity should be lifted elsewhere.
574 Target->setForcedLangOptions(LangOpt);
576 // Initialize the preprocessor.
577 PP.Initialize(*Target);
579 // Initialize the ASTContext
580 Context.InitBuiltinTypes(*Target);
582 // We didn't have access to the comment options when the ASTContext was
583 // constructed, so register them now.
584 Context.getCommentCommandTraits().registerCommentOptions(
585 LangOpt.CommentOpts);
589 /// \brief Diagnostic consumer that saves each diagnostic it is given.
590 class StoredDiagnosticConsumer : public DiagnosticConsumer {
591 SmallVectorImpl<StoredDiagnostic> &StoredDiags;
592 SourceManager *SourceMgr;
595 explicit StoredDiagnosticConsumer(
596 SmallVectorImpl<StoredDiagnostic> &StoredDiags)
597 : StoredDiags(StoredDiags), SourceMgr(0) { }
599 virtual void BeginSourceFile(const LangOptions &LangOpts,
600 const Preprocessor *PP = 0) {
602 SourceMgr = &PP->getSourceManager();
605 virtual void HandleDiagnostic(DiagnosticsEngine::Level Level,
606 const Diagnostic &Info);
609 /// \brief RAII object that optionally captures diagnostics, if
610 /// there is no diagnostic client to capture them already.
611 class CaptureDroppedDiagnostics {
612 DiagnosticsEngine &Diags;
613 StoredDiagnosticConsumer Client;
614 DiagnosticConsumer *PreviousClient;
617 CaptureDroppedDiagnostics(bool RequestCapture, DiagnosticsEngine &Diags,
618 SmallVectorImpl<StoredDiagnostic> &StoredDiags)
619 : Diags(Diags), Client(StoredDiags), PreviousClient(0)
621 if (RequestCapture || Diags.getClient() == 0) {
622 PreviousClient = Diags.takeClient();
623 Diags.setClient(&Client);
627 ~CaptureDroppedDiagnostics() {
628 if (Diags.getClient() == &Client) {
630 Diags.setClient(PreviousClient);
635 } // anonymous namespace
637 void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level,
638 const Diagnostic &Info) {
639 // Default implementation (Warnings/errors count).
640 DiagnosticConsumer::HandleDiagnostic(Level, Info);
642 // Only record the diagnostic if it's part of the source manager we know
643 // about. This effectively drops diagnostics from modules we're building.
644 // FIXME: In the long run, ee don't want to drop source managers from modules.
645 if (!Info.hasSourceManager() || &Info.getSourceManager() == SourceMgr)
646 StoredDiags.push_back(StoredDiagnostic(Level, Info));
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->getFileManager(),
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, HeaderInfo,
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 (ND->getIdentifier())
881 Hash = llvm::HashString(ND->getIdentifier()->getName(), Hash);
882 else if (DeclarationName Name = ND->getDeclName()) {
883 std::string NameStr = Name.getAsString();
884 Hash = llvm::HashString(NameStr, Hash);
890 class TopLevelDeclTrackerConsumer : public ASTConsumer {
895 TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
896 : Unit(_Unit), Hash(Hash) {
900 void handleTopLevelDecl(Decl *D) {
904 // FIXME: Currently ObjC method declarations are incorrectly being
905 // reported as top-level declarations, even though their DeclContext
906 // is the containing ObjC @interface/@implementation. This is a
907 // fundamental problem in the parser right now.
908 if (isa<ObjCMethodDecl>(D))
911 AddTopLevelDeclarationToHash(D, Hash);
912 Unit.addTopLevelDecl(D);
914 handleFileLevelDecl(D);
917 void handleFileLevelDecl(Decl *D) {
918 Unit.addFileLevelDecl(D);
919 if (NamespaceDecl *NSD = dyn_cast<NamespaceDecl>(D)) {
920 for (NamespaceDecl::decl_iterator
921 I = NSD->decls_begin(), E = NSD->decls_end(); I != E; ++I)
922 handleFileLevelDecl(*I);
926 bool HandleTopLevelDecl(DeclGroupRef D) {
927 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
928 handleTopLevelDecl(*it);
932 // We're not interested in "interesting" decls.
933 void HandleInterestingDecl(DeclGroupRef) {}
935 void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) {
936 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
937 handleTopLevelDecl(*it);
940 virtual ASTDeserializationListener *GetASTDeserializationListener() {
941 return Unit.getDeserializationListener();
945 class TopLevelDeclTrackerAction : public ASTFrontendAction {
949 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
951 CI.getPreprocessor().addPPCallbacks(
952 new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
953 return new TopLevelDeclTrackerConsumer(Unit,
954 Unit.getCurrentTopLevelHashValue());
958 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
960 virtual bool hasCodeCompletionSupport() const { return false; }
961 virtual TranslationUnitKind getTranslationUnitKind() {
962 return Unit.getTranslationUnitKind();
966 class PrecompilePreambleConsumer : public PCHGenerator {
969 std::vector<Decl *> TopLevelDecls;
972 PrecompilePreambleConsumer(ASTUnit &Unit, const Preprocessor &PP,
973 StringRef isysroot, raw_ostream *Out)
974 : PCHGenerator(PP, "", 0, isysroot, Out), Unit(Unit),
975 Hash(Unit.getCurrentTopLevelHashValue()) {
979 virtual bool HandleTopLevelDecl(DeclGroupRef D) {
980 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
982 // FIXME: Currently ObjC method declarations are incorrectly being
983 // reported as top-level declarations, even though their DeclContext
984 // is the containing ObjC @interface/@implementation. This is a
985 // fundamental problem in the parser right now.
986 if (isa<ObjCMethodDecl>(D))
988 AddTopLevelDeclarationToHash(D, Hash);
989 TopLevelDecls.push_back(D);
994 virtual void HandleTranslationUnit(ASTContext &Ctx) {
995 PCHGenerator::HandleTranslationUnit(Ctx);
996 if (!Unit.getDiagnostics().hasErrorOccurred()) {
997 // Translate the top-level declarations we captured during
998 // parsing into declaration IDs in the precompiled
999 // preamble. This will allow us to deserialize those top-level
1000 // declarations when requested.
1001 for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I)
1002 Unit.addTopLevelDeclFromPreamble(
1003 getWriter().getDeclID(TopLevelDecls[I]));
1008 class PrecompilePreambleAction : public ASTFrontendAction {
1012 explicit PrecompilePreambleAction(ASTUnit &Unit) : Unit(Unit) {}
1014 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
1016 std::string Sysroot;
1017 std::string OutputFile;
1018 raw_ostream *OS = 0;
1019 if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot,
1024 if (!CI.getFrontendOpts().RelocatablePCH)
1027 CI.getPreprocessor().addPPCallbacks(
1028 new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
1029 return new PrecompilePreambleConsumer(Unit, CI.getPreprocessor(), Sysroot,
1033 virtual bool hasCodeCompletionSupport() const { return false; }
1034 virtual bool hasASTFileSupport() const { return false; }
1035 virtual TranslationUnitKind getTranslationUnitKind() { return TU_Prefix; }
1040 static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag) {
1041 return StoredDiag.getLocation().isValid();
1045 checkAndRemoveNonDriverDiags(SmallVectorImpl<StoredDiagnostic> &StoredDiags) {
1046 // Get rid of stored diagnostics except the ones from the driver which do not
1047 // have a source location.
1049 std::remove_if(StoredDiags.begin(), StoredDiags.end(), isNonDriverDiag),
1053 static void checkAndSanitizeDiags(SmallVectorImpl<StoredDiagnostic> &
1055 SourceManager &SM) {
1056 // The stored diagnostic has the old source manager in it; update
1057 // the locations to refer into the new source manager. Since we've
1058 // been careful to make sure that the source manager's state
1059 // before and after are identical, so that we can reuse the source
1061 for (unsigned I = 0, N = StoredDiagnostics.size(); I < N; ++I) {
1062 if (StoredDiagnostics[I].getLocation().isValid()) {
1063 FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), SM);
1064 StoredDiagnostics[I].setLocation(Loc);
1069 /// Parse the source file into a translation unit using the given compiler
1070 /// invocation, replacing the current translation unit.
1072 /// \returns True if a failure occurred that causes the ASTUnit not to
1073 /// contain any translation-unit information, false otherwise.
1074 bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) {
1075 delete SavedMainFileBuffer;
1076 SavedMainFileBuffer = 0;
1079 delete OverrideMainBuffer;
1083 // Create the compiler instance to use for building the AST.
1084 OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1086 // Recover resources if we crash before exiting this method.
1087 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1088 CICleanup(Clang.get());
1090 IntrusiveRefCntPtr<CompilerInvocation>
1091 CCInvocation(new CompilerInvocation(*Invocation));
1093 Clang->setInvocation(CCInvocation.getPtr());
1094 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1096 // Set up diagnostics, capturing any diagnostics that would
1097 // otherwise be dropped.
1098 Clang->setDiagnostics(&getDiagnostics());
1100 // Create the target instance.
1101 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1102 &Clang->getTargetOpts()));
1103 if (!Clang->hasTarget()) {
1104 delete OverrideMainBuffer;
1108 // Inform the target of the language options.
1110 // FIXME: We shouldn't need to do this, the target should be immutable once
1111 // created. This complexity should be lifted elsewhere.
1112 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1114 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1115 "Invocation must have exactly one source file!");
1116 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
1117 "FIXME: AST inputs not yet supported here!");
1118 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
1119 "IR inputs not support here!");
1121 // Configure the various subsystems.
1122 // FIXME: Should we retain the previous file manager?
1123 LangOpts = &Clang->getLangOpts();
1124 FileSystemOpts = Clang->getFileSystemOpts();
1125 FileMgr = new FileManager(FileSystemOpts);
1126 SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
1127 UserFilesAreVolatile);
1133 // Clear out old caches and data.
1134 TopLevelDecls.clear();
1135 clearFileLevelDecls();
1136 CleanTemporaryFiles();
1138 if (!OverrideMainBuffer) {
1139 checkAndRemoveNonDriverDiags(StoredDiagnostics);
1140 TopLevelDeclsInPreamble.clear();
1143 // Create a file manager object to provide access to and cache the filesystem.
1144 Clang->setFileManager(&getFileManager());
1146 // Create the source manager.
1147 Clang->setSourceManager(&getSourceManager());
1149 // If the main file has been overridden due to the use of a preamble,
1150 // make that override happen and introduce the preamble.
1151 PreprocessorOptions &PreprocessorOpts = Clang->getPreprocessorOpts();
1152 if (OverrideMainBuffer) {
1153 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
1154 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
1155 PreprocessorOpts.PrecompiledPreambleBytes.second
1156 = PreambleEndsAtStartOfLine;
1157 PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this);
1158 PreprocessorOpts.DisablePCHValidation = true;
1160 // The stored diagnostic has the old source manager in it; update
1161 // the locations to refer into the new source manager. Since we've
1162 // been careful to make sure that the source manager's state
1163 // before and after are identical, so that we can reuse the source
1165 checkAndSanitizeDiags(StoredDiagnostics, getSourceManager());
1167 // Keep track of the override buffer;
1168 SavedMainFileBuffer = OverrideMainBuffer;
1171 OwningPtr<TopLevelDeclTrackerAction> Act(
1172 new TopLevelDeclTrackerAction(*this));
1174 // Recover resources if we crash before exiting this method.
1175 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1176 ActCleanup(Act.get());
1178 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
1181 if (OverrideMainBuffer) {
1182 std::string ModName = getPreambleFile(this);
1183 TranslateStoredDiagnostics(Clang->getModuleManager(), ModName,
1184 getSourceManager(), PreambleDiagnostics,
1188 if (!Act->Execute())
1191 transferASTDataFromCompilerInstance(*Clang);
1193 Act->EndSourceFile();
1195 FailedParseDiagnostics.clear();
1200 // Remove the overridden buffer we used for the preamble.
1201 if (OverrideMainBuffer) {
1202 delete OverrideMainBuffer;
1203 SavedMainFileBuffer = 0;
1206 // Keep the ownership of the data in the ASTUnit because the client may
1207 // want to see the diagnostics.
1208 transferASTDataFromCompilerInstance(*Clang);
1209 FailedParseDiagnostics.swap(StoredDiagnostics);
1210 StoredDiagnostics.clear();
1211 NumStoredDiagnosticsFromDriver = 0;
1215 /// \brief Simple function to retrieve a path for a preamble precompiled header.
1216 static std::string GetPreamblePCHPath() {
1217 // FIXME: This is lame; sys::Path should provide this function (in particular,
1218 // it should know how to find the temporary files dir).
1219 // FIXME: This is really lame. I copied this code from the Driver!
1220 // FIXME: This is a hack so that we can override the preamble file during
1221 // crash-recovery testing, which is the only case where the preamble files
1222 // are not necessarily cleaned up.
1223 const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
1228 const char *TmpDir = ::getenv("TMPDIR");
1230 TmpDir = ::getenv("TEMP");
1232 TmpDir = ::getenv("TMP");
1233 #ifdef LLVM_ON_WIN32
1235 TmpDir = ::getenv("USERPROFILE");
1239 llvm::sys::Path P(TmpDir);
1240 P.createDirectoryOnDisk(true);
1241 P.appendComponent("preamble");
1242 P.appendSuffix("pch");
1243 if (P.makeUnique(/*reuse_current=*/false, /*ErrMsg*/0))
1244 return std::string();
1249 /// \brief Compute the preamble for the main file, providing the source buffer
1250 /// that corresponds to the main file along with a pair (bytes, start-of-line)
1251 /// that describes the preamble.
1252 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
1253 ASTUnit::ComputePreamble(CompilerInvocation &Invocation,
1254 unsigned MaxLines, bool &CreatedBuffer) {
1255 FrontendOptions &FrontendOpts = Invocation.getFrontendOpts();
1256 PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts();
1257 CreatedBuffer = false;
1259 // Try to determine if the main file has been remapped, either from the
1260 // command line (to another file) or directly through the compiler invocation
1261 // (to a memory buffer).
1262 llvm::MemoryBuffer *Buffer = 0;
1263 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].getFile());
1264 if (const llvm::sys::FileStatus *MainFileStatus = MainFilePath.getFileStatus()) {
1265 // Check whether there is a file-file remapping of the main file
1266 for (PreprocessorOptions::remapped_file_iterator
1267 M = PreprocessorOpts.remapped_file_begin(),
1268 E = PreprocessorOpts.remapped_file_end();
1271 llvm::sys::PathWithStatus MPath(M->first);
1272 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1273 if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1274 // We found a remapping. Try to load the resulting, remapped source.
1275 if (CreatedBuffer) {
1277 CreatedBuffer = false;
1280 Buffer = getBufferForFile(M->second);
1282 return std::make_pair((llvm::MemoryBuffer*)0,
1283 std::make_pair(0, true));
1284 CreatedBuffer = true;
1289 // Check whether there is a file-buffer remapping. It supercedes the
1290 // file-file remapping.
1291 for (PreprocessorOptions::remapped_file_buffer_iterator
1292 M = PreprocessorOpts.remapped_file_buffer_begin(),
1293 E = PreprocessorOpts.remapped_file_buffer_end();
1296 llvm::sys::PathWithStatus MPath(M->first);
1297 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1298 if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1299 // We found a remapping.
1300 if (CreatedBuffer) {
1302 CreatedBuffer = false;
1305 Buffer = const_cast<llvm::MemoryBuffer *>(M->second);
1311 // If the main source file was not remapped, load it now.
1313 Buffer = getBufferForFile(FrontendOpts.Inputs[0].getFile());
1315 return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true));
1317 CreatedBuffer = true;
1320 return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer,
1321 *Invocation.getLangOpts(),
1325 static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old,
1327 StringRef NewName) {
1328 llvm::MemoryBuffer *Result
1329 = llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName);
1330 memcpy(const_cast<char*>(Result->getBufferStart()),
1331 Old->getBufferStart(), Old->getBufferSize());
1332 memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(),
1333 ' ', NewSize - Old->getBufferSize() - 1);
1334 const_cast<char*>(Result->getBufferEnd())[-1] = '\n';
1339 /// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing
1340 /// the source file.
1342 /// This routine will compute the preamble of the main source file. If a
1343 /// non-trivial preamble is found, it will precompile that preamble into a
1344 /// precompiled header so that the precompiled preamble can be used to reduce
1345 /// reparsing time. If a precompiled preamble has already been constructed,
1346 /// this routine will determine if it is still valid and, if so, avoid
1347 /// rebuilding the precompiled preamble.
1349 /// \param AllowRebuild When true (the default), this routine is
1350 /// allowed to rebuild the precompiled preamble if it is found to be
1353 /// \param MaxLines When non-zero, the maximum number of lines that
1354 /// can occur within the preamble.
1356 /// \returns If the precompiled preamble can be used, returns a newly-allocated
1357 /// buffer that should be used in place of the main file when doing so.
1358 /// Otherwise, returns a NULL pointer.
1359 llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
1360 const CompilerInvocation &PreambleInvocationIn,
1362 unsigned MaxLines) {
1364 IntrusiveRefCntPtr<CompilerInvocation>
1365 PreambleInvocation(new CompilerInvocation(PreambleInvocationIn));
1366 FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts();
1367 PreprocessorOptions &PreprocessorOpts
1368 = PreambleInvocation->getPreprocessorOpts();
1370 bool CreatedPreambleBuffer = false;
1371 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble
1372 = ComputePreamble(*PreambleInvocation, MaxLines, CreatedPreambleBuffer);
1374 // If ComputePreamble() Take ownership of the preamble buffer.
1375 OwningPtr<llvm::MemoryBuffer> OwnedPreambleBuffer;
1376 if (CreatedPreambleBuffer)
1377 OwnedPreambleBuffer.reset(NewPreamble.first);
1379 if (!NewPreamble.second.first) {
1380 // We couldn't find a preamble in the main source. Clear out the current
1381 // preamble, if we have one. It's obviously no good any more.
1383 erasePreambleFile(this);
1385 // The next time we actually see a preamble, precompile it.
1386 PreambleRebuildCounter = 1;
1390 if (!Preamble.empty()) {
1391 // We've previously computed a preamble. Check whether we have the same
1392 // preamble now that we did before, and that there's enough space in
1393 // the main-file buffer within the precompiled preamble to fit the
1395 if (Preamble.size() == NewPreamble.second.first &&
1396 PreambleEndsAtStartOfLine == NewPreamble.second.second &&
1397 NewPreamble.first->getBufferSize() < PreambleReservedSize-2 &&
1398 memcmp(Preamble.getBufferStart(), NewPreamble.first->getBufferStart(),
1399 NewPreamble.second.first) == 0) {
1400 // The preamble has not changed. We may be able to re-use the precompiled
1403 // Check that none of the files used by the preamble have changed.
1404 bool AnyFileChanged = false;
1406 // First, make a record of those files that have been overridden via
1407 // remapping or unsaved_files.
1408 llvm::StringMap<std::pair<off_t, time_t> > OverriddenFiles;
1409 for (PreprocessorOptions::remapped_file_iterator
1410 R = PreprocessorOpts.remapped_file_begin(),
1411 REnd = PreprocessorOpts.remapped_file_end();
1412 !AnyFileChanged && R != REnd;
1414 struct stat StatBuf;
1415 if (FileMgr->getNoncachedStatValue(R->second, StatBuf)) {
1416 // If we can't stat the file we're remapping to, assume that something
1417 // horrible happened.
1418 AnyFileChanged = true;
1422 OverriddenFiles[R->first] = std::make_pair(StatBuf.st_size,
1425 for (PreprocessorOptions::remapped_file_buffer_iterator
1426 R = PreprocessorOpts.remapped_file_buffer_begin(),
1427 REnd = PreprocessorOpts.remapped_file_buffer_end();
1428 !AnyFileChanged && R != REnd;
1430 // FIXME: Should we actually compare the contents of file->buffer
1432 OverriddenFiles[R->first] = std::make_pair(R->second->getBufferSize(),
1436 // Check whether anything has changed.
1437 for (llvm::StringMap<std::pair<off_t, time_t> >::iterator
1438 F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end();
1439 !AnyFileChanged && F != FEnd;
1441 llvm::StringMap<std::pair<off_t, time_t> >::iterator Overridden
1442 = OverriddenFiles.find(F->first());
1443 if (Overridden != OverriddenFiles.end()) {
1444 // This file was remapped; check whether the newly-mapped file
1445 // matches up with the previous mapping.
1446 if (Overridden->second != F->second)
1447 AnyFileChanged = true;
1451 // The file was not remapped; check whether it has changed on disk.
1452 struct stat StatBuf;
1453 if (FileMgr->getNoncachedStatValue(F->first(), StatBuf)) {
1454 // If we can't stat the file, assume that something horrible happened.
1455 AnyFileChanged = true;
1456 } else if (StatBuf.st_size != F->second.first ||
1457 StatBuf.st_mtime != F->second.second)
1458 AnyFileChanged = true;
1461 if (!AnyFileChanged) {
1462 // Okay! We can re-use the precompiled preamble.
1464 // Set the state of the diagnostic object to mimic its state
1465 // after parsing the preamble.
1466 getDiagnostics().Reset();
1467 ProcessWarningOptions(getDiagnostics(),
1468 PreambleInvocation->getDiagnosticOpts());
1469 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1471 // Create a version of the main file buffer that is padded to
1472 // buffer size we reserved when creating the preamble.
1473 return CreatePaddedMainFileBuffer(NewPreamble.first,
1474 PreambleReservedSize,
1475 FrontendOpts.Inputs[0].getFile());
1479 // If we aren't allowed to rebuild the precompiled preamble, just
1484 // We can't reuse the previously-computed preamble. Build a new one.
1486 PreambleDiagnostics.clear();
1487 erasePreambleFile(this);
1488 PreambleRebuildCounter = 1;
1489 } else if (!AllowRebuild) {
1490 // We aren't allowed to rebuild the precompiled preamble; just
1495 // If the preamble rebuild counter > 1, it's because we previously
1496 // failed to build a preamble and we're not yet ready to try
1497 // again. Decrement the counter and return a failure.
1498 if (PreambleRebuildCounter > 1) {
1499 --PreambleRebuildCounter;
1503 // Create a temporary file for the precompiled preamble. In rare
1504 // circumstances, this can fail.
1505 std::string PreamblePCHPath = GetPreamblePCHPath();
1506 if (PreamblePCHPath.empty()) {
1507 // Try again next time.
1508 PreambleRebuildCounter = 1;
1512 // We did not previously compute a preamble, or it can't be reused anyway.
1513 SimpleTimer PreambleTimer(WantTiming);
1514 PreambleTimer.setOutput("Precompiling preamble");
1516 // Create a new buffer that stores the preamble. The buffer also contains
1517 // extra space for the original contents of the file (which will be present
1518 // when we actually parse the file) along with more room in case the file
1520 PreambleReservedSize = NewPreamble.first->getBufferSize();
1521 if (PreambleReservedSize < 4096)
1522 PreambleReservedSize = 8191;
1524 PreambleReservedSize *= 2;
1526 // Save the preamble text for later; we'll need to compare against it for
1527 // subsequent reparses.
1528 StringRef MainFilename = PreambleInvocation->getFrontendOpts().Inputs[0].getFile();
1529 Preamble.assign(FileMgr->getFile(MainFilename),
1530 NewPreamble.first->getBufferStart(),
1531 NewPreamble.first->getBufferStart()
1532 + NewPreamble.second.first);
1533 PreambleEndsAtStartOfLine = NewPreamble.second.second;
1535 delete PreambleBuffer;
1537 = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize,
1538 FrontendOpts.Inputs[0].getFile());
1539 memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()),
1540 NewPreamble.first->getBufferStart(), Preamble.size());
1541 memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(),
1542 ' ', PreambleReservedSize - Preamble.size() - 1);
1543 const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n';
1545 // Remap the main source file to the preamble buffer.
1546 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].getFile());
1547 PreprocessorOpts.addRemappedFile(MainFilePath.str(), PreambleBuffer);
1549 // Tell the compiler invocation to generate a temporary precompiled header.
1550 FrontendOpts.ProgramAction = frontend::GeneratePCH;
1551 // FIXME: Generate the precompiled header into memory?
1552 FrontendOpts.OutputFile = PreamblePCHPath;
1553 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
1554 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
1556 // Create the compiler instance to use for building the precompiled preamble.
1557 OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1559 // Recover resources if we crash before exiting this method.
1560 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1561 CICleanup(Clang.get());
1563 Clang->setInvocation(&*PreambleInvocation);
1564 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1566 // Set up diagnostics, capturing all of the diagnostics produced.
1567 Clang->setDiagnostics(&getDiagnostics());
1569 // Create the target instance.
1570 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1571 &Clang->getTargetOpts()));
1572 if (!Clang->hasTarget()) {
1573 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1575 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1576 PreprocessorOpts.eraseRemappedFile(
1577 PreprocessorOpts.remapped_file_buffer_end() - 1);
1581 // Inform the target of the language options.
1583 // FIXME: We shouldn't need to do this, the target should be immutable once
1584 // created. This complexity should be lifted elsewhere.
1585 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1587 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1588 "Invocation must have exactly one source file!");
1589 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
1590 "FIXME: AST inputs not yet supported here!");
1591 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
1592 "IR inputs not support here!");
1594 // Clear out old caches and data.
1595 getDiagnostics().Reset();
1596 ProcessWarningOptions(getDiagnostics(), Clang->getDiagnosticOpts());
1597 checkAndRemoveNonDriverDiags(StoredDiagnostics);
1598 TopLevelDecls.clear();
1599 TopLevelDeclsInPreamble.clear();
1601 // Create a file manager object to provide access to and cache the filesystem.
1602 Clang->setFileManager(new FileManager(Clang->getFileSystemOpts()));
1604 // Create the source manager.
1605 Clang->setSourceManager(new SourceManager(getDiagnostics(),
1606 Clang->getFileManager()));
1608 OwningPtr<PrecompilePreambleAction> Act;
1609 Act.reset(new PrecompilePreambleAction(*this));
1610 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
1611 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1613 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1614 PreprocessorOpts.eraseRemappedFile(
1615 PreprocessorOpts.remapped_file_buffer_end() - 1);
1620 Act->EndSourceFile();
1622 if (Diagnostics->hasErrorOccurred()) {
1623 // There were errors parsing the preamble, so no precompiled header was
1624 // generated. Forget that we even tried.
1625 // FIXME: Should we leave a note for ourselves to try again?
1626 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1628 TopLevelDeclsInPreamble.clear();
1629 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1630 PreprocessorOpts.eraseRemappedFile(
1631 PreprocessorOpts.remapped_file_buffer_end() - 1);
1635 // Transfer any diagnostics generated when parsing the preamble into the set
1636 // of preamble diagnostics.
1637 PreambleDiagnostics.clear();
1638 PreambleDiagnostics.insert(PreambleDiagnostics.end(),
1639 stored_diag_afterDriver_begin(), stored_diag_end());
1640 checkAndRemoveNonDriverDiags(StoredDiagnostics);
1642 // Keep track of the preamble we precompiled.
1643 setPreambleFile(this, FrontendOpts.OutputFile);
1644 NumWarningsInPreamble = getDiagnostics().getNumWarnings();
1646 // Keep track of all of the files that the source manager knows about,
1647 // so we can verify whether they have changed or not.
1648 FilesInPreamble.clear();
1649 SourceManager &SourceMgr = Clang->getSourceManager();
1650 const llvm::MemoryBuffer *MainFileBuffer
1651 = SourceMgr.getBuffer(SourceMgr.getMainFileID());
1652 for (SourceManager::fileinfo_iterator F = SourceMgr.fileinfo_begin(),
1653 FEnd = SourceMgr.fileinfo_end();
1656 const FileEntry *File = F->second->OrigEntry;
1657 if (!File || F->second->getRawBuffer() == MainFileBuffer)
1660 FilesInPreamble[File->getName()]
1661 = std::make_pair(F->second->getSize(), File->getModificationTime());
1664 PreambleRebuildCounter = 1;
1665 PreprocessorOpts.eraseRemappedFile(
1666 PreprocessorOpts.remapped_file_buffer_end() - 1);
1668 // If the hash of top-level entities differs from the hash of the top-level
1669 // entities the last time we rebuilt the preamble, clear out the completion
1671 if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1672 CompletionCacheTopLevelHashValue = 0;
1673 PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1676 return CreatePaddedMainFileBuffer(NewPreamble.first,
1677 PreambleReservedSize,
1678 FrontendOpts.Inputs[0].getFile());
1681 void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1682 std::vector<Decl *> Resolved;
1683 Resolved.reserve(TopLevelDeclsInPreamble.size());
1684 ExternalASTSource &Source = *getASTContext().getExternalSource();
1685 for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) {
1686 // Resolve the declaration ID to an actual declaration, possibly
1687 // deserializing the declaration in the process.
1688 Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]);
1690 Resolved.push_back(D);
1692 TopLevelDeclsInPreamble.clear();
1693 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1696 void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
1697 // Steal the created target, context, and preprocessor.
1698 TheSema.reset(CI.takeSema());
1699 Consumer.reset(CI.takeASTConsumer());
1700 Ctx = &CI.getASTContext();
1701 PP = &CI.getPreprocessor();
1702 CI.setSourceManager(0);
1703 CI.setFileManager(0);
1704 Target = &CI.getTarget();
1705 Reader = CI.getModuleManager();
1708 StringRef ASTUnit::getMainFileName() const {
1709 if (Invocation && !Invocation->getFrontendOpts().Inputs.empty()) {
1710 const FrontendInputFile &Input = Invocation->getFrontendOpts().Inputs[0];
1712 return Input.getFile();
1714 return Input.getBuffer()->getBufferIdentifier();
1718 if (const FileEntry *
1719 FE = SourceMgr->getFileEntryForID(SourceMgr->getMainFileID()))
1720 return FE->getName();
1726 StringRef ASTUnit::getASTFileName() const {
1727 if (!isMainFileAST())
1730 serialization::ModuleFile &
1731 Mod = Reader->getModuleManager().getPrimaryModule();
1732 return Mod.FileName;
1735 ASTUnit *ASTUnit::create(CompilerInvocation *CI,
1736 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1737 bool CaptureDiagnostics,
1738 bool UserFilesAreVolatile) {
1739 OwningPtr<ASTUnit> AST;
1740 AST.reset(new ASTUnit(false));
1741 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
1742 AST->Diagnostics = Diags;
1743 AST->Invocation = CI;
1744 AST->FileSystemOpts = CI->getFileSystemOpts();
1745 AST->FileMgr = new FileManager(AST->FileSystemOpts);
1746 AST->UserFilesAreVolatile = UserFilesAreVolatile;
1747 AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
1748 UserFilesAreVolatile);
1753 ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(CompilerInvocation *CI,
1754 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1755 ASTFrontendAction *Action,
1758 StringRef ResourceFilesPath,
1759 bool OnlyLocalDecls,
1760 bool CaptureDiagnostics,
1761 bool PrecompilePreamble,
1762 bool CacheCodeCompletionResults,
1763 bool IncludeBriefCommentsInCodeCompletion,
1764 bool UserFilesAreVolatile,
1765 OwningPtr<ASTUnit> *ErrAST) {
1766 assert(CI && "A CompilerInvocation is required");
1768 OwningPtr<ASTUnit> OwnAST;
1769 ASTUnit *AST = Unit;
1771 // Create the AST unit.
1772 OwnAST.reset(create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile));
1776 if (!ResourceFilesPath.empty()) {
1777 // Override the resources path.
1778 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1780 AST->OnlyLocalDecls = OnlyLocalDecls;
1781 AST->CaptureDiagnostics = CaptureDiagnostics;
1782 if (PrecompilePreamble)
1783 AST->PreambleRebuildCounter = 2;
1784 AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete;
1785 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1786 AST->IncludeBriefCommentsInCodeCompletion
1787 = IncludeBriefCommentsInCodeCompletion;
1789 // Recover resources if we crash before exiting this method.
1790 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1791 ASTUnitCleanup(OwnAST.get());
1792 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1793 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
1794 DiagCleanup(Diags.getPtr());
1796 // We'll manage file buffers ourselves.
1797 CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1798 CI->getFrontendOpts().DisableFree = false;
1799 ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
1801 // Create the compiler instance to use for building the AST.
1802 OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1804 // Recover resources if we crash before exiting this method.
1805 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1806 CICleanup(Clang.get());
1808 Clang->setInvocation(CI);
1809 AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1811 // Set up diagnostics, capturing any diagnostics that would
1812 // otherwise be dropped.
1813 Clang->setDiagnostics(&AST->getDiagnostics());
1815 // Create the target instance.
1816 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1817 &Clang->getTargetOpts()));
1818 if (!Clang->hasTarget())
1821 // Inform the target of the language options.
1823 // FIXME: We shouldn't need to do this, the target should be immutable once
1824 // created. This complexity should be lifted elsewhere.
1825 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1827 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1828 "Invocation must have exactly one source file!");
1829 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
1830 "FIXME: AST inputs not yet supported here!");
1831 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
1832 "IR inputs not supported here!");
1834 // Configure the various subsystems.
1835 AST->TheSema.reset();
1840 // Create a file manager object to provide access to and cache the filesystem.
1841 Clang->setFileManager(&AST->getFileManager());
1843 // Create the source manager.
1844 Clang->setSourceManager(&AST->getSourceManager());
1846 ASTFrontendAction *Act = Action;
1848 OwningPtr<TopLevelDeclTrackerAction> TrackerAct;
1850 TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
1851 Act = TrackerAct.get();
1854 // Recover resources if we crash before exiting this method.
1855 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1856 ActCleanup(TrackerAct.get());
1858 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
1859 AST->transferASTDataFromCompilerInstance(*Clang);
1860 if (OwnAST && ErrAST)
1861 ErrAST->swap(OwnAST);
1866 if (Persistent && !TrackerAct) {
1867 Clang->getPreprocessor().addPPCallbacks(
1868 new MacroDefinitionTrackerPPCallbacks(AST->getCurrentTopLevelHashValue()));
1869 std::vector<ASTConsumer*> Consumers;
1870 if (Clang->hasASTConsumer())
1871 Consumers.push_back(Clang->takeASTConsumer());
1872 Consumers.push_back(new TopLevelDeclTrackerConsumer(*AST,
1873 AST->getCurrentTopLevelHashValue()));
1874 Clang->setASTConsumer(new MultiplexConsumer(Consumers));
1876 if (!Act->Execute()) {
1877 AST->transferASTDataFromCompilerInstance(*Clang);
1878 if (OwnAST && ErrAST)
1879 ErrAST->swap(OwnAST);
1884 // Steal the created target, context, and preprocessor.
1885 AST->transferASTDataFromCompilerInstance(*Clang);
1887 Act->EndSourceFile();
1890 return OwnAST.take();
1895 bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) {
1899 // We'll manage file buffers ourselves.
1900 Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1901 Invocation->getFrontendOpts().DisableFree = false;
1902 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1904 llvm::MemoryBuffer *OverrideMainBuffer = 0;
1905 if (PrecompilePreamble) {
1906 PreambleRebuildCounter = 2;
1908 = getMainBufferWithPrecompiledPreamble(*Invocation);
1911 SimpleTimer ParsingTimer(WantTiming);
1912 ParsingTimer.setOutput("Parsing " + getMainFileName());
1914 // Recover resources if we crash before exiting this method.
1915 llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1916 MemBufferCleanup(OverrideMainBuffer);
1918 return Parse(OverrideMainBuffer);
1921 ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
1922 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1923 bool OnlyLocalDecls,
1924 bool CaptureDiagnostics,
1925 bool PrecompilePreamble,
1926 TranslationUnitKind TUKind,
1927 bool CacheCodeCompletionResults,
1928 bool IncludeBriefCommentsInCodeCompletion,
1929 bool UserFilesAreVolatile) {
1930 // Create the AST unit.
1931 OwningPtr<ASTUnit> AST;
1932 AST.reset(new ASTUnit(false));
1933 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
1934 AST->Diagnostics = Diags;
1935 AST->OnlyLocalDecls = OnlyLocalDecls;
1936 AST->CaptureDiagnostics = CaptureDiagnostics;
1937 AST->TUKind = TUKind;
1938 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1939 AST->IncludeBriefCommentsInCodeCompletion
1940 = IncludeBriefCommentsInCodeCompletion;
1941 AST->Invocation = CI;
1942 AST->FileSystemOpts = CI->getFileSystemOpts();
1943 AST->FileMgr = new FileManager(AST->FileSystemOpts);
1944 AST->UserFilesAreVolatile = UserFilesAreVolatile;
1946 // Recover resources if we crash before exiting this method.
1947 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1948 ASTUnitCleanup(AST.get());
1949 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1950 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
1951 DiagCleanup(Diags.getPtr());
1953 return AST->LoadFromCompilerInvocation(PrecompilePreamble)? 0 : AST.take();
1956 ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
1957 const char **ArgEnd,
1958 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1959 StringRef ResourceFilesPath,
1960 bool OnlyLocalDecls,
1961 bool CaptureDiagnostics,
1962 RemappedFile *RemappedFiles,
1963 unsigned NumRemappedFiles,
1964 bool RemappedFilesKeepOriginalName,
1965 bool PrecompilePreamble,
1966 TranslationUnitKind TUKind,
1967 bool CacheCodeCompletionResults,
1968 bool IncludeBriefCommentsInCodeCompletion,
1969 bool AllowPCHWithCompilerErrors,
1970 bool SkipFunctionBodies,
1971 bool UserFilesAreVolatile,
1972 bool ForSerialization,
1973 OwningPtr<ASTUnit> *ErrAST) {
1974 if (!Diags.getPtr()) {
1975 // No diagnostics engine was provided, so create our own diagnostics object
1976 // with the default options.
1977 Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions());
1980 SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1982 IntrusiveRefCntPtr<CompilerInvocation> CI;
1986 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
1989 CI = clang::createInvocationFromCommandLine(
1990 llvm::makeArrayRef(ArgBegin, ArgEnd),
1996 // Override any files that need remapping
1997 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
1998 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
1999 if (const llvm::MemoryBuffer *
2000 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
2001 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, memBuf);
2003 const char *fname = fileOrBuf.get<const char *>();
2004 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, fname);
2007 PreprocessorOptions &PPOpts = CI->getPreprocessorOpts();
2008 PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName;
2009 PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors;
2011 // Override the resources path.
2012 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
2014 CI->getFrontendOpts().SkipFunctionBodies = SkipFunctionBodies;
2016 // Create the AST unit.
2017 OwningPtr<ASTUnit> AST;
2018 AST.reset(new ASTUnit(false));
2019 ConfigureDiags(Diags, ArgBegin, ArgEnd, *AST, CaptureDiagnostics);
2020 AST->Diagnostics = Diags;
2021 Diags = 0; // Zero out now to ease cleanup during crash recovery.
2022 AST->FileSystemOpts = CI->getFileSystemOpts();
2023 AST->FileMgr = new FileManager(AST->FileSystemOpts);
2024 AST->OnlyLocalDecls = OnlyLocalDecls;
2025 AST->CaptureDiagnostics = CaptureDiagnostics;
2026 AST->TUKind = TUKind;
2027 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
2028 AST->IncludeBriefCommentsInCodeCompletion
2029 = IncludeBriefCommentsInCodeCompletion;
2030 AST->UserFilesAreVolatile = UserFilesAreVolatile;
2031 AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
2032 AST->StoredDiagnostics.swap(StoredDiagnostics);
2033 AST->Invocation = CI;
2034 if (ForSerialization)
2035 AST->WriterData.reset(new ASTWriterData());
2036 CI = 0; // Zero out now to ease cleanup during crash recovery.
2038 // Recover resources if we crash before exiting this method.
2039 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
2040 ASTUnitCleanup(AST.get());
2042 if (AST->LoadFromCompilerInvocation(PrecompilePreamble)) {
2043 // Some error occurred, if caller wants to examine diagnostics, pass it the
2046 AST->StoredDiagnostics.swap(AST->FailedParseDiagnostics);
2055 bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) {
2059 clearFileLevelDecls();
2061 SimpleTimer ParsingTimer(WantTiming);
2062 ParsingTimer.setOutput("Reparsing " + getMainFileName());
2065 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
2066 for (PreprocessorOptions::remapped_file_buffer_iterator
2067 R = PPOpts.remapped_file_buffer_begin(),
2068 REnd = PPOpts.remapped_file_buffer_end();
2073 Invocation->getPreprocessorOpts().clearRemappedFiles();
2074 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
2075 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
2076 if (const llvm::MemoryBuffer *
2077 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
2078 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
2081 const char *fname = fileOrBuf.get<const char *>();
2082 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
2087 // If we have a preamble file lying around, or if we might try to
2088 // build a precompiled preamble, do so now.
2089 llvm::MemoryBuffer *OverrideMainBuffer = 0;
2090 if (!getPreambleFile(this).empty() || PreambleRebuildCounter > 0)
2091 OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation);
2093 // Clear out the diagnostics state.
2094 getDiagnostics().Reset();
2095 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
2096 if (OverrideMainBuffer)
2097 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
2099 // Parse the sources
2100 bool Result = Parse(OverrideMainBuffer);
2102 // If we're caching global code-completion results, and the top-level
2103 // declarations have changed, clear out the code-completion cache.
2104 if (!Result && ShouldCacheCodeCompletionResults &&
2105 CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
2106 CacheCodeCompletionResults();
2108 // We now need to clear out the completion info related to this translation
2109 // unit; it'll be recreated if necessary.
2115 //----------------------------------------------------------------------------//
2117 //----------------------------------------------------------------------------//
2120 /// \brief Code completion consumer that combines the cached code-completion
2121 /// results from an ASTUnit with the code-completion results provided to it,
2122 /// then passes the result on to
2123 class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
2124 uint64_t NormalContexts;
2126 CodeCompleteConsumer &Next;
2129 AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
2130 const CodeCompleteOptions &CodeCompleteOpts)
2131 : CodeCompleteConsumer(CodeCompleteOpts, Next.isOutputBinary()),
2132 AST(AST), Next(Next)
2134 // Compute the set of contexts in which we will look when we don't have
2135 // any information about the specific context.
2137 = (1LL << CodeCompletionContext::CCC_TopLevel)
2138 | (1LL << CodeCompletionContext::CCC_ObjCInterface)
2139 | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
2140 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
2141 | (1LL << CodeCompletionContext::CCC_Statement)
2142 | (1LL << CodeCompletionContext::CCC_Expression)
2143 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
2144 | (1LL << CodeCompletionContext::CCC_DotMemberAccess)
2145 | (1LL << CodeCompletionContext::CCC_ArrowMemberAccess)
2146 | (1LL << CodeCompletionContext::CCC_ObjCPropertyAccess)
2147 | (1LL << CodeCompletionContext::CCC_ObjCProtocolName)
2148 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
2149 | (1LL << CodeCompletionContext::CCC_Recovery);
2151 if (AST.getASTContext().getLangOpts().CPlusPlus)
2152 NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag)
2153 | (1LL << CodeCompletionContext::CCC_UnionTag)
2154 | (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
2157 virtual void ProcessCodeCompleteResults(Sema &S,
2158 CodeCompletionContext Context,
2159 CodeCompletionResult *Results,
2160 unsigned NumResults);
2162 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
2163 OverloadCandidate *Candidates,
2164 unsigned NumCandidates) {
2165 Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
2168 virtual CodeCompletionAllocator &getAllocator() {
2169 return Next.getAllocator();
2172 virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() {
2173 return Next.getCodeCompletionTUInfo();
2178 /// \brief Helper function that computes which global names are hidden by the
2179 /// local code-completion results.
2180 static void CalculateHiddenNames(const CodeCompletionContext &Context,
2181 CodeCompletionResult *Results,
2182 unsigned NumResults,
2184 llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
2185 bool OnlyTagNames = false;
2186 switch (Context.getKind()) {
2187 case CodeCompletionContext::CCC_Recovery:
2188 case CodeCompletionContext::CCC_TopLevel:
2189 case CodeCompletionContext::CCC_ObjCInterface:
2190 case CodeCompletionContext::CCC_ObjCImplementation:
2191 case CodeCompletionContext::CCC_ObjCIvarList:
2192 case CodeCompletionContext::CCC_ClassStructUnion:
2193 case CodeCompletionContext::CCC_Statement:
2194 case CodeCompletionContext::CCC_Expression:
2195 case CodeCompletionContext::CCC_ObjCMessageReceiver:
2196 case CodeCompletionContext::CCC_DotMemberAccess:
2197 case CodeCompletionContext::CCC_ArrowMemberAccess:
2198 case CodeCompletionContext::CCC_ObjCPropertyAccess:
2199 case CodeCompletionContext::CCC_Namespace:
2200 case CodeCompletionContext::CCC_Type:
2201 case CodeCompletionContext::CCC_Name:
2202 case CodeCompletionContext::CCC_PotentiallyQualifiedName:
2203 case CodeCompletionContext::CCC_ParenthesizedExpression:
2204 case CodeCompletionContext::CCC_ObjCInterfaceName:
2207 case CodeCompletionContext::CCC_EnumTag:
2208 case CodeCompletionContext::CCC_UnionTag:
2209 case CodeCompletionContext::CCC_ClassOrStructTag:
2210 OnlyTagNames = true;
2213 case CodeCompletionContext::CCC_ObjCProtocolName:
2214 case CodeCompletionContext::CCC_MacroName:
2215 case CodeCompletionContext::CCC_MacroNameUse:
2216 case CodeCompletionContext::CCC_PreprocessorExpression:
2217 case CodeCompletionContext::CCC_PreprocessorDirective:
2218 case CodeCompletionContext::CCC_NaturalLanguage:
2219 case CodeCompletionContext::CCC_SelectorName:
2220 case CodeCompletionContext::CCC_TypeQualifiers:
2221 case CodeCompletionContext::CCC_Other:
2222 case CodeCompletionContext::CCC_OtherWithMacros:
2223 case CodeCompletionContext::CCC_ObjCInstanceMessage:
2224 case CodeCompletionContext::CCC_ObjCClassMessage:
2225 case CodeCompletionContext::CCC_ObjCCategoryName:
2226 // We're looking for nothing, or we're looking for names that cannot
2231 typedef CodeCompletionResult Result;
2232 for (unsigned I = 0; I != NumResults; ++I) {
2233 if (Results[I].Kind != Result::RK_Declaration)
2237 = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
2239 bool Hiding = false;
2241 Hiding = (IDNS & Decl::IDNS_Tag);
2243 unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
2244 Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
2245 Decl::IDNS_NonMemberOperator);
2246 if (Ctx.getLangOpts().CPlusPlus)
2247 HiddenIDNS |= Decl::IDNS_Tag;
2248 Hiding = (IDNS & HiddenIDNS);
2254 DeclarationName Name = Results[I].Declaration->getDeclName();
2255 if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
2256 HiddenNames.insert(Identifier->getName());
2258 HiddenNames.insert(Name.getAsString());
2263 void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
2264 CodeCompletionContext Context,
2265 CodeCompletionResult *Results,
2266 unsigned NumResults) {
2267 // Merge the results we were given with the results we cached.
2268 bool AddedResult = false;
2269 uint64_t InContexts =
2270 Context.getKind() == CodeCompletionContext::CCC_Recovery
2271 ? NormalContexts : (1LL << Context.getKind());
2272 // Contains the set of names that are hidden by "local" completion results.
2273 llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
2274 typedef CodeCompletionResult Result;
2275 SmallVector<Result, 8> AllResults;
2276 for (ASTUnit::cached_completion_iterator
2277 C = AST.cached_completion_begin(),
2278 CEnd = AST.cached_completion_end();
2280 // If the context we are in matches any of the contexts we are
2281 // interested in, we'll add this result.
2282 if ((C->ShowInContexts & InContexts) == 0)
2285 // If we haven't added any results previously, do so now.
2287 CalculateHiddenNames(Context, Results, NumResults, S.Context,
2289 AllResults.insert(AllResults.end(), Results, Results + NumResults);
2293 // Determine whether this global completion result is hidden by a local
2294 // completion result. If so, skip it.
2295 if (C->Kind != CXCursor_MacroDefinition &&
2296 HiddenNames.count(C->Completion->getTypedText()))
2299 // Adjust priority based on similar type classes.
2300 unsigned Priority = C->Priority;
2301 CodeCompletionString *Completion = C->Completion;
2302 if (!Context.getPreferredType().isNull()) {
2303 if (C->Kind == CXCursor_MacroDefinition) {
2304 Priority = getMacroUsagePriority(C->Completion->getTypedText(),
2306 Context.getPreferredType()->isAnyPointerType());
2307 } else if (C->Type) {
2308 CanQualType Expected
2309 = S.Context.getCanonicalType(
2310 Context.getPreferredType().getUnqualifiedType());
2311 SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
2312 if (ExpectedSTC == C->TypeClass) {
2313 // We know this type is similar; check for an exact match.
2314 llvm::StringMap<unsigned> &CachedCompletionTypes
2315 = AST.getCachedCompletionTypes();
2316 llvm::StringMap<unsigned>::iterator Pos
2317 = CachedCompletionTypes.find(QualType(Expected).getAsString());
2318 if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
2319 Priority /= CCF_ExactTypeMatch;
2321 Priority /= CCF_SimilarTypeMatch;
2326 // Adjust the completion string, if required.
2327 if (C->Kind == CXCursor_MacroDefinition &&
2328 Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
2329 // Create a new code-completion string that just contains the
2330 // macro name, without its arguments.
2331 CodeCompletionBuilder Builder(getAllocator(), getCodeCompletionTUInfo(),
2332 CCP_CodePattern, C->Availability);
2333 Builder.AddTypedTextChunk(C->Completion->getTypedText());
2334 Priority = CCP_CodePattern;
2335 Completion = Builder.TakeString();
2338 AllResults.push_back(Result(Completion, Priority, C->Kind,
2342 // If we did not add any cached completion results, just forward the
2343 // results we were given to the next consumer.
2345 Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
2349 Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
2355 void ASTUnit::CodeComplete(StringRef File, unsigned Line, unsigned Column,
2356 RemappedFile *RemappedFiles,
2357 unsigned NumRemappedFiles,
2359 bool IncludeCodePatterns,
2360 bool IncludeBriefComments,
2361 CodeCompleteConsumer &Consumer,
2362 DiagnosticsEngine &Diag, LangOptions &LangOpts,
2363 SourceManager &SourceMgr, FileManager &FileMgr,
2364 SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2365 SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
2369 SimpleTimer CompletionTimer(WantTiming);
2370 CompletionTimer.setOutput("Code completion @ " + File + ":" +
2371 Twine(Line) + ":" + Twine(Column));
2373 IntrusiveRefCntPtr<CompilerInvocation>
2374 CCInvocation(new CompilerInvocation(*Invocation));
2376 FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2377 CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts;
2378 PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2380 CodeCompleteOpts.IncludeMacros = IncludeMacros &&
2381 CachedCompletionResults.empty();
2382 CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns;
2383 CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty();
2384 CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments;
2386 assert(IncludeBriefComments == this->IncludeBriefCommentsInCodeCompletion);
2388 FrontendOpts.CodeCompletionAt.FileName = File;
2389 FrontendOpts.CodeCompletionAt.Line = Line;
2390 FrontendOpts.CodeCompletionAt.Column = Column;
2392 // Set the language options appropriately.
2393 LangOpts = *CCInvocation->getLangOpts();
2395 OwningPtr<CompilerInstance> Clang(new CompilerInstance());
2397 // Recover resources if we crash before exiting this method.
2398 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2399 CICleanup(Clang.get());
2401 Clang->setInvocation(&*CCInvocation);
2402 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
2404 // Set up diagnostics, capturing any diagnostics produced.
2405 Clang->setDiagnostics(&Diag);
2406 ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts());
2407 CaptureDroppedDiagnostics Capture(true,
2408 Clang->getDiagnostics(),
2411 // Create the target instance.
2412 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
2413 &Clang->getTargetOpts()));
2414 if (!Clang->hasTarget()) {
2415 Clang->setInvocation(0);
2419 // Inform the target of the language options.
2421 // FIXME: We shouldn't need to do this, the target should be immutable once
2422 // created. This complexity should be lifted elsewhere.
2423 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
2425 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2426 "Invocation must have exactly one source file!");
2427 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
2428 "FIXME: AST inputs not yet supported here!");
2429 assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
2430 "IR inputs not support here!");
2433 // Use the source and file managers that we were given.
2434 Clang->setFileManager(&FileMgr);
2435 Clang->setSourceManager(&SourceMgr);
2438 PreprocessorOpts.clearRemappedFiles();
2439 PreprocessorOpts.RetainRemappedFileBuffers = true;
2440 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
2441 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
2442 if (const llvm::MemoryBuffer *
2443 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
2444 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, memBuf);
2445 OwnedBuffers.push_back(memBuf);
2447 const char *fname = fileOrBuf.get<const char *>();
2448 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, fname);
2452 // Use the code completion consumer we were given, but adding any cached
2453 // code-completion results.
2454 AugmentedCodeCompleteConsumer *AugmentedConsumer
2455 = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts);
2456 Clang->setCodeCompletionConsumer(AugmentedConsumer);
2458 // If we have a precompiled preamble, try to use it. We only allow
2459 // the use of the precompiled preamble if we're if the completion
2460 // point is within the main file, after the end of the precompiled
2462 llvm::MemoryBuffer *OverrideMainBuffer = 0;
2463 if (!getPreambleFile(this).empty()) {
2464 using llvm::sys::FileStatus;
2465 llvm::sys::PathWithStatus CompleteFilePath(File);
2466 llvm::sys::PathWithStatus MainPath(OriginalSourceFile);
2467 if (const FileStatus *CompleteFileStatus = CompleteFilePath.getFileStatus())
2468 if (const FileStatus *MainStatus = MainPath.getFileStatus())
2469 if (CompleteFileStatus->getUniqueID() == MainStatus->getUniqueID() &&
2472 = getMainBufferWithPrecompiledPreamble(*CCInvocation, false,
2476 // If the main file has been overridden due to the use of a preamble,
2477 // make that override happen and introduce the preamble.
2478 if (OverrideMainBuffer) {
2479 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
2480 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
2481 PreprocessorOpts.PrecompiledPreambleBytes.second
2482 = PreambleEndsAtStartOfLine;
2483 PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this);
2484 PreprocessorOpts.DisablePCHValidation = true;
2486 OwnedBuffers.push_back(OverrideMainBuffer);
2488 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2489 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2492 // Disable the preprocessing record if modules are not enabled.
2493 if (!Clang->getLangOpts().Modules)
2494 PreprocessorOpts.DetailedRecord = false;
2496 OwningPtr<SyntaxOnlyAction> Act;
2497 Act.reset(new SyntaxOnlyAction);
2498 if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
2500 Act->EndSourceFile();
2504 bool ASTUnit::Save(StringRef File) {
2505 // Write to a temporary file and later rename it to the actual file, to avoid
2506 // possible race conditions.
2507 SmallString<128> TempPath;
2509 TempPath += "-%%%%%%%%";
2511 if (llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath,
2512 /*makeAbsolute=*/false))
2515 // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2516 // unconditionally create a stat cache when we parse the file?
2517 llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true);
2521 if (Out.has_error()) {
2526 if (llvm::sys::fs::rename(TempPath.str(), File)) {
2528 llvm::sys::fs::remove(TempPath.str(), exists);
2535 static bool serializeUnit(ASTWriter &Writer,
2536 SmallVectorImpl<char> &Buffer,
2540 Writer.WriteAST(S, std::string(), 0, "", hasErrors);
2542 // Write the generated bitstream to "Out".
2543 if (!Buffer.empty())
2544 OS.write(Buffer.data(), Buffer.size());
2549 bool ASTUnit::serialize(raw_ostream &OS) {
2550 bool hasErrors = getDiagnostics().hasErrorOccurred();
2553 return serializeUnit(WriterData->Writer, WriterData->Buffer,
2554 getSema(), hasErrors, OS);
2556 SmallString<128> Buffer;
2557 llvm::BitstreamWriter Stream(Buffer);
2558 ASTWriter Writer(Stream);
2559 return serializeUnit(Writer, Buffer, getSema(), hasErrors, OS);
2562 typedef ContinuousRangeMap<unsigned, int, 2> SLocRemap;
2564 static void TranslateSLoc(SourceLocation &L, SLocRemap &Remap) {
2565 unsigned Raw = L.getRawEncoding();
2566 const unsigned MacroBit = 1U << 31;
2567 L = SourceLocation::getFromRawEncoding((Raw & MacroBit) |
2568 ((Raw & ~MacroBit) + Remap.find(Raw & ~MacroBit)->second));
2571 void ASTUnit::TranslateStoredDiagnostics(
2574 SourceManager &SrcMgr,
2575 const SmallVectorImpl<StoredDiagnostic> &Diags,
2576 SmallVectorImpl<StoredDiagnostic> &Out) {
2577 // The stored diagnostic has the old source manager in it; update
2578 // the locations to refer into the new source manager. We also need to remap
2579 // all the locations to the new view. This includes the diag location, any
2580 // associated source ranges, and the source ranges of associated fix-its.
2581 // FIXME: There should be a cleaner way to do this.
2583 SmallVector<StoredDiagnostic, 4> Result;
2584 Result.reserve(Diags.size());
2585 assert(MMan && "Don't have a module manager");
2586 serialization::ModuleFile *Mod = MMan->ModuleMgr.lookup(ModName);
2587 assert(Mod && "Don't have preamble module");
2588 SLocRemap &Remap = Mod->SLocRemap;
2589 for (unsigned I = 0, N = Diags.size(); I != N; ++I) {
2590 // Rebuild the StoredDiagnostic.
2591 const StoredDiagnostic &SD = Diags[I];
2592 SourceLocation L = SD.getLocation();
2593 TranslateSLoc(L, Remap);
2594 FullSourceLoc Loc(L, SrcMgr);
2596 SmallVector<CharSourceRange, 4> Ranges;
2597 Ranges.reserve(SD.range_size());
2598 for (StoredDiagnostic::range_iterator I = SD.range_begin(),
2601 SourceLocation BL = I->getBegin();
2602 TranslateSLoc(BL, Remap);
2603 SourceLocation EL = I->getEnd();
2604 TranslateSLoc(EL, Remap);
2605 Ranges.push_back(CharSourceRange(SourceRange(BL, EL), I->isTokenRange()));
2608 SmallVector<FixItHint, 2> FixIts;
2609 FixIts.reserve(SD.fixit_size());
2610 for (StoredDiagnostic::fixit_iterator I = SD.fixit_begin(),
2613 FixIts.push_back(FixItHint());
2614 FixItHint &FH = FixIts.back();
2615 FH.CodeToInsert = I->CodeToInsert;
2616 SourceLocation BL = I->RemoveRange.getBegin();
2617 TranslateSLoc(BL, Remap);
2618 SourceLocation EL = I->RemoveRange.getEnd();
2619 TranslateSLoc(EL, Remap);
2620 FH.RemoveRange = CharSourceRange(SourceRange(BL, EL),
2621 I->RemoveRange.isTokenRange());
2624 Result.push_back(StoredDiagnostic(SD.getLevel(), SD.getID(),
2625 SD.getMessage(), Loc, Ranges, FixIts));
2630 static inline bool compLocDecl(std::pair<unsigned, Decl *> L,
2631 std::pair<unsigned, Decl *> R) {
2632 return L.first < R.first;
2635 void ASTUnit::addFileLevelDecl(Decl *D) {
2638 // We only care about local declarations.
2639 if (D->isFromASTFile())
2642 SourceManager &SM = *SourceMgr;
2643 SourceLocation Loc = D->getLocation();
2644 if (Loc.isInvalid() || !SM.isLocalSourceLocation(Loc))
2647 // We only keep track of the file-level declarations of each file.
2648 if (!D->getLexicalDeclContext()->isFileContext())
2651 SourceLocation FileLoc = SM.getFileLoc(Loc);
2652 assert(SM.isLocalSourceLocation(FileLoc));
2655 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
2656 if (FID.isInvalid())
2659 LocDeclsTy *&Decls = FileDecls[FID];
2661 Decls = new LocDeclsTy();
2663 std::pair<unsigned, Decl *> LocDecl(Offset, D);
2665 if (Decls->empty() || Decls->back().first <= Offset) {
2666 Decls->push_back(LocDecl);
2670 LocDeclsTy::iterator
2671 I = std::upper_bound(Decls->begin(), Decls->end(), LocDecl, compLocDecl);
2673 Decls->insert(I, LocDecl);
2676 void ASTUnit::findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
2677 SmallVectorImpl<Decl *> &Decls) {
2678 if (File.isInvalid())
2681 if (SourceMgr->isLoadedFileID(File)) {
2682 assert(Ctx->getExternalSource() && "No external source!");
2683 return Ctx->getExternalSource()->FindFileRegionDecls(File, Offset, Length,
2687 FileDeclsTy::iterator I = FileDecls.find(File);
2688 if (I == FileDecls.end())
2691 LocDeclsTy &LocDecls = *I->second;
2692 if (LocDecls.empty())
2695 LocDeclsTy::iterator
2696 BeginIt = std::lower_bound(LocDecls.begin(), LocDecls.end(),
2697 std::make_pair(Offset, (Decl*)0), compLocDecl);
2698 if (BeginIt != LocDecls.begin())
2701 // If we are pointing at a top-level decl inside an objc container, we need
2702 // to backtrack until we find it otherwise we will fail to report that the
2703 // region overlaps with an objc container.
2704 while (BeginIt != LocDecls.begin() &&
2705 BeginIt->second->isTopLevelDeclInObjCContainer())
2708 LocDeclsTy::iterator
2709 EndIt = std::upper_bound(LocDecls.begin(), LocDecls.end(),
2710 std::make_pair(Offset+Length, (Decl*)0),
2712 if (EndIt != LocDecls.end())
2715 for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt)
2716 Decls.push_back(DIt->second);
2719 SourceLocation ASTUnit::getLocation(const FileEntry *File,
2720 unsigned Line, unsigned Col) const {
2721 const SourceManager &SM = getSourceManager();
2722 SourceLocation Loc = SM.translateFileLineCol(File, Line, Col);
2723 return SM.getMacroArgExpandedLocation(Loc);
2726 SourceLocation ASTUnit::getLocation(const FileEntry *File,
2727 unsigned Offset) const {
2728 const SourceManager &SM = getSourceManager();
2729 SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1);
2730 return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
2733 /// \brief If \arg Loc is a loaded location from the preamble, returns
2734 /// the corresponding local location of the main file, otherwise it returns
2736 SourceLocation ASTUnit::mapLocationFromPreamble(SourceLocation Loc) {
2739 PreambleID = SourceMgr->getPreambleFileID();
2741 if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
2745 if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble.size()) {
2746 SourceLocation FileLoc
2747 = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID());
2748 return FileLoc.getLocWithOffset(Offs);
2754 /// \brief If \arg Loc is a local location of the main file but inside the
2755 /// preamble chunk, returns the corresponding loaded location from the
2756 /// preamble, otherwise it returns \arg Loc.
2757 SourceLocation ASTUnit::mapLocationToPreamble(SourceLocation Loc) {
2760 PreambleID = SourceMgr->getPreambleFileID();
2762 if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
2766 if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) &&
2767 Offs < Preamble.size()) {
2768 SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID);
2769 return FileLoc.getLocWithOffset(Offs);
2775 bool ASTUnit::isInPreambleFileID(SourceLocation Loc) {
2778 FID = SourceMgr->getPreambleFileID();
2780 if (Loc.isInvalid() || FID.isInvalid())
2783 return SourceMgr->isInFileID(Loc, FID);
2786 bool ASTUnit::isInMainFileID(SourceLocation Loc) {
2789 FID = SourceMgr->getMainFileID();
2791 if (Loc.isInvalid() || FID.isInvalid())
2794 return SourceMgr->isInFileID(Loc, FID);
2797 SourceLocation ASTUnit::getEndOfPreambleFileID() {
2800 FID = SourceMgr->getPreambleFileID();
2802 if (FID.isInvalid())
2803 return SourceLocation();
2805 return SourceMgr->getLocForEndOfFile(FID);
2808 SourceLocation ASTUnit::getStartOfMainFileID() {
2811 FID = SourceMgr->getMainFileID();
2813 if (FID.isInvalid())
2814 return SourceLocation();
2816 return SourceMgr->getLocForStartOfFile(FID);
2819 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
2820 ASTUnit::getLocalPreprocessingEntities() const {
2821 if (isMainFileAST()) {
2822 serialization::ModuleFile &
2823 Mod = Reader->getModuleManager().getPrimaryModule();
2824 return Reader->getModulePreprocessedEntities(Mod);
2827 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
2828 return std::make_pair(PPRec->local_begin(), PPRec->local_end());
2830 return std::make_pair(PreprocessingRecord::iterator(),
2831 PreprocessingRecord::iterator());
2834 bool ASTUnit::visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn) {
2835 if (isMainFileAST()) {
2836 serialization::ModuleFile &
2837 Mod = Reader->getModuleManager().getPrimaryModule();
2838 ASTReader::ModuleDeclIterator MDI, MDE;
2839 llvm::tie(MDI, MDE) = Reader->getModuleFileLevelDecls(Mod);
2840 for (; MDI != MDE; ++MDI) {
2841 if (!Fn(context, *MDI))
2848 for (ASTUnit::top_level_iterator TL = top_level_begin(),
2849 TLEnd = top_level_end();
2850 TL != TLEnd; ++TL) {
2851 if (!Fn(context, *TL))
2859 struct PCHLocatorInfo {
2860 serialization::ModuleFile *Mod;
2861 PCHLocatorInfo() : Mod(0) {}
2865 static bool PCHLocator(serialization::ModuleFile &M, void *UserData) {
2866 PCHLocatorInfo &Info = *static_cast<PCHLocatorInfo*>(UserData);
2868 case serialization::MK_Module:
2869 return true; // skip dependencies.
2870 case serialization::MK_PCH:
2872 return true; // found it.
2873 case serialization::MK_Preamble:
2874 return false; // look in dependencies.
2875 case serialization::MK_MainFile:
2876 return false; // look in dependencies.
2882 const FileEntry *ASTUnit::getPCHFile() {
2886 PCHLocatorInfo Info;
2887 Reader->getModuleManager().visit(PCHLocator, &Info);
2889 return Info.Mod->File;
2894 bool ASTUnit::isModuleFile() {
2895 return isMainFileAST() && !ASTFileLangOpts.CurrentModule.empty();
2898 void ASTUnit::PreambleData::countLines() const {
2903 for (std::vector<char>::const_iterator
2904 I = Buffer.begin(), E = Buffer.end(); I != E; ++I) {
2908 if (Buffer.back() != '\n')
2913 ASTUnit::ConcurrencyState::ConcurrencyState() {
2914 Mutex = new llvm::sys::MutexImpl(/*recursive=*/true);
2917 ASTUnit::ConcurrencyState::~ConcurrencyState() {
2918 delete static_cast<llvm::sys::MutexImpl *>(Mutex);
2921 void ASTUnit::ConcurrencyState::start() {
2922 bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire();
2923 assert(acquired && "Concurrent access to ASTUnit!");
2926 void ASTUnit::ConcurrencyState::finish() {
2927 static_cast<llvm::sys::MutexImpl *>(Mutex)->release();
2932 ASTUnit::ConcurrencyState::ConcurrencyState() {}
2933 ASTUnit::ConcurrencyState::~ConcurrencyState() {}
2934 void ASTUnit::ConcurrencyState::start() {}
2935 void ASTUnit::ConcurrencyState::finish() {}