1 //===--- ASTUnit.cpp - ASTUnit utility ------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // ASTUnit Implementation.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Frontend/ASTUnit.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/AST/DeclVisitor.h"
18 #include "clang/AST/TypeOrdering.h"
19 #include "clang/AST/StmtVisitor.h"
20 #include "clang/Driver/Compilation.h"
21 #include "clang/Driver/Driver.h"
22 #include "clang/Driver/Job.h"
23 #include "clang/Driver/ArgList.h"
24 #include "clang/Driver/Options.h"
25 #include "clang/Driver/Tool.h"
26 #include "clang/Frontend/CompilerInstance.h"
27 #include "clang/Frontend/FrontendActions.h"
28 #include "clang/Frontend/FrontendDiagnostic.h"
29 #include "clang/Frontend/FrontendOptions.h"
30 #include "clang/Frontend/Utils.h"
31 #include "clang/Serialization/ASTReader.h"
32 #include "clang/Serialization/ASTSerializationListener.h"
33 #include "clang/Serialization/ASTWriter.h"
34 #include "clang/Lex/HeaderSearch.h"
35 #include "clang/Lex/Preprocessor.h"
36 #include "clang/Basic/TargetOptions.h"
37 #include "clang/Basic/TargetInfo.h"
38 #include "clang/Basic/Diagnostic.h"
39 #include "llvm/ADT/ArrayRef.h"
40 #include "llvm/ADT/StringExtras.h"
41 #include "llvm/ADT/StringSet.h"
42 #include "llvm/Support/Atomic.h"
43 #include "llvm/Support/MemoryBuffer.h"
44 #include "llvm/Support/Host.h"
45 #include "llvm/Support/Path.h"
46 #include "llvm/Support/raw_ostream.h"
47 #include "llvm/Support/Timer.h"
48 #include "llvm/Support/CrashRecoveryContext.h"
52 using namespace clang;
54 using llvm::TimeRecord;
63 explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
65 Start = TimeRecord::getCurrentTime();
68 void setOutput(const llvm::Twine &Output) {
70 this->Output = Output.str();
75 TimeRecord Elapsed = TimeRecord::getCurrentTime();
77 llvm::errs() << Output << ':';
78 Elapsed.print(Elapsed, llvm::errs());
85 /// \brief After failing to build a precompiled preamble (due to
86 /// errors in the source that occurs in the preamble), the number of
87 /// reparses during which we'll skip even trying to precompile the
89 const unsigned DefaultPreambleRebuildInterval = 5;
91 /// \brief Tracks the number of ASTUnit objects that are currently active.
93 /// Used for debugging purposes only.
94 static llvm::sys::cas_flag ActiveASTUnitObjects;
96 ASTUnit::ASTUnit(bool _MainFileIsAST)
97 : OnlyLocalDecls(false), CaptureDiagnostics(false),
98 MainFileIsAST(_MainFileIsAST),
99 CompleteTranslationUnit(true), WantTiming(getenv("LIBCLANG_TIMING")),
100 OwnsRemappedFileBuffers(true),
101 NumStoredDiagnosticsFromDriver(0),
102 ConcurrencyCheckValue(CheckUnlocked),
103 PreambleRebuildCounter(0), SavedMainFileBuffer(0), PreambleBuffer(0),
104 ShouldCacheCodeCompletionResults(false),
105 CompletionCacheTopLevelHashValue(0),
106 PreambleTopLevelHashValue(0),
107 CurrentTopLevelHashValue(0),
108 UnsafeToFree(false) {
109 if (getenv("LIBCLANG_OBJTRACKING")) {
110 llvm::sys::AtomicIncrement(&ActiveASTUnitObjects);
111 fprintf(stderr, "+++ %d translation units\n", ActiveASTUnitObjects);
115 ASTUnit::~ASTUnit() {
116 ConcurrencyCheckValue = CheckLocked;
117 CleanTemporaryFiles();
118 if (!PreambleFile.empty())
119 llvm::sys::Path(PreambleFile).eraseFromDisk();
121 // Free the buffers associated with remapped files. We are required to
122 // perform this operation here because we explicitly request that the
123 // compiler instance *not* free these buffers for each invocation of the
125 if (Invocation.getPtr() && OwnsRemappedFileBuffers) {
126 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
127 for (PreprocessorOptions::remapped_file_buffer_iterator
128 FB = PPOpts.remapped_file_buffer_begin(),
129 FBEnd = PPOpts.remapped_file_buffer_end();
135 delete SavedMainFileBuffer;
136 delete PreambleBuffer;
138 ClearCachedCompletionResults();
140 if (getenv("LIBCLANG_OBJTRACKING")) {
141 llvm::sys::AtomicDecrement(&ActiveASTUnitObjects);
142 fprintf(stderr, "--- %d translation units\n", ActiveASTUnitObjects);
146 void ASTUnit::CleanTemporaryFiles() {
147 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
148 TemporaryFiles[I].eraseFromDisk();
149 TemporaryFiles.clear();
152 /// \brief Determine the set of code-completion contexts in which this
153 /// declaration should be shown.
154 static unsigned getDeclShowContexts(NamedDecl *ND,
155 const LangOptions &LangOpts,
156 bool &IsNestedNameSpecifier) {
157 IsNestedNameSpecifier = false;
159 if (isa<UsingShadowDecl>(ND))
160 ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl());
164 unsigned Contexts = 0;
165 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) ||
166 isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) {
167 // Types can appear in these contexts.
168 if (LangOpts.CPlusPlus || !isa<TagDecl>(ND))
169 Contexts |= (1 << (CodeCompletionContext::CCC_TopLevel - 1))
170 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
171 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
172 | (1 << (CodeCompletionContext::CCC_Statement - 1))
173 | (1 << (CodeCompletionContext::CCC_Type - 1))
174 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1));
176 // In C++, types can appear in expressions contexts (for functional casts).
177 if (LangOpts.CPlusPlus)
178 Contexts |= (1 << (CodeCompletionContext::CCC_Expression - 1));
180 // In Objective-C, message sends can send interfaces. In Objective-C++,
181 // all types are available due to functional casts.
182 if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND))
183 Contexts |= (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1));
185 // Deal with tag names.
186 if (isa<EnumDecl>(ND)) {
187 Contexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1));
189 // Part of the nested-name-specifier in C++0x.
190 if (LangOpts.CPlusPlus0x)
191 IsNestedNameSpecifier = true;
192 } else if (RecordDecl *Record = dyn_cast<RecordDecl>(ND)) {
193 if (Record->isUnion())
194 Contexts |= (1 << (CodeCompletionContext::CCC_UnionTag - 1));
196 Contexts |= (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1));
198 if (LangOpts.CPlusPlus)
199 IsNestedNameSpecifier = true;
200 } else if (isa<ClassTemplateDecl>(ND))
201 IsNestedNameSpecifier = true;
202 } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) {
203 // Values can appear in these contexts.
204 Contexts = (1 << (CodeCompletionContext::CCC_Statement - 1))
205 | (1 << (CodeCompletionContext::CCC_Expression - 1))
206 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
207 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1));
208 } else if (isa<ObjCProtocolDecl>(ND)) {
209 Contexts = (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1));
210 } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
211 Contexts = (1 << (CodeCompletionContext::CCC_Namespace - 1));
213 // Part of the nested-name-specifier.
214 IsNestedNameSpecifier = true;
220 void ASTUnit::CacheCodeCompletionResults() {
224 SimpleTimer Timer(WantTiming);
225 Timer.setOutput("Cache global code completions for " + getMainFileName());
227 // Clear out the previous results.
228 ClearCachedCompletionResults();
230 // Gather the set of global code completions.
231 typedef CodeCompletionResult Result;
232 llvm::SmallVector<Result, 8> Results;
233 CachedCompletionAllocator = new GlobalCodeCompletionAllocator;
234 TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator, Results);
236 // Translate global code completions into cached completions.
237 llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
239 for (unsigned I = 0, N = Results.size(); I != N; ++I) {
240 switch (Results[I].Kind) {
241 case Result::RK_Declaration: {
242 bool IsNestedNameSpecifier = false;
243 CachedCodeCompletionResult CachedResult;
244 CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema,
245 *CachedCompletionAllocator);
246 CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration,
247 Ctx->getLangOptions(),
248 IsNestedNameSpecifier);
249 CachedResult.Priority = Results[I].Priority;
250 CachedResult.Kind = Results[I].CursorKind;
251 CachedResult.Availability = Results[I].Availability;
253 // Keep track of the type of this completion in an ASTContext-agnostic
255 QualType UsageType = getDeclUsageType(*Ctx, Results[I].Declaration);
256 if (UsageType.isNull()) {
257 CachedResult.TypeClass = STC_Void;
258 CachedResult.Type = 0;
260 CanQualType CanUsageType
261 = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
262 CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
264 // Determine whether we have already seen this type. If so, we save
265 // ourselves the work of formatting the type string by using the
266 // temporary, CanQualType-based hash table to find the associated value.
267 unsigned &TypeValue = CompletionTypes[CanUsageType];
268 if (TypeValue == 0) {
269 TypeValue = CompletionTypes.size();
270 CachedCompletionTypes[QualType(CanUsageType).getAsString()]
274 CachedResult.Type = TypeValue;
277 CachedCompletionResults.push_back(CachedResult);
279 /// Handle nested-name-specifiers in C++.
280 if (TheSema->Context.getLangOptions().CPlusPlus &&
281 IsNestedNameSpecifier && !Results[I].StartsNestedNameSpecifier) {
282 // The contexts in which a nested-name-specifier can appear in C++.
284 = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
285 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
286 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
287 | (1 << (CodeCompletionContext::CCC_Statement - 1))
288 | (1 << (CodeCompletionContext::CCC_Expression - 1))
289 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
290 | (1 << (CodeCompletionContext::CCC_EnumTag - 1))
291 | (1 << (CodeCompletionContext::CCC_UnionTag - 1))
292 | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1))
293 | (1 << (CodeCompletionContext::CCC_Type - 1))
294 | (1 << (CodeCompletionContext::CCC_PotentiallyQualifiedName - 1))
295 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1));
297 if (isa<NamespaceDecl>(Results[I].Declaration) ||
298 isa<NamespaceAliasDecl>(Results[I].Declaration))
299 NNSContexts |= (1 << (CodeCompletionContext::CCC_Namespace - 1));
301 if (unsigned RemainingContexts
302 = NNSContexts & ~CachedResult.ShowInContexts) {
303 // If there any contexts where this completion can be a
304 // nested-name-specifier but isn't already an option, create a
305 // nested-name-specifier completion.
306 Results[I].StartsNestedNameSpecifier = true;
307 CachedResult.Completion
308 = Results[I].CreateCodeCompletionString(*TheSema,
309 *CachedCompletionAllocator);
310 CachedResult.ShowInContexts = RemainingContexts;
311 CachedResult.Priority = CCP_NestedNameSpecifier;
312 CachedResult.TypeClass = STC_Void;
313 CachedResult.Type = 0;
314 CachedCompletionResults.push_back(CachedResult);
320 case Result::RK_Keyword:
321 case Result::RK_Pattern:
322 // Ignore keywords and patterns; we don't care, since they are so
323 // easily regenerated.
326 case Result::RK_Macro: {
327 CachedCodeCompletionResult CachedResult;
328 CachedResult.Completion
329 = Results[I].CreateCodeCompletionString(*TheSema,
330 *CachedCompletionAllocator);
331 CachedResult.ShowInContexts
332 = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
333 | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1))
334 | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1))
335 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
336 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
337 | (1 << (CodeCompletionContext::CCC_Statement - 1))
338 | (1 << (CodeCompletionContext::CCC_Expression - 1))
339 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
340 | (1 << (CodeCompletionContext::CCC_MacroNameUse - 1))
341 | (1 << (CodeCompletionContext::CCC_PreprocessorExpression - 1))
342 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
343 | (1 << (CodeCompletionContext::CCC_OtherWithMacros - 1));
345 CachedResult.Priority = Results[I].Priority;
346 CachedResult.Kind = Results[I].CursorKind;
347 CachedResult.Availability = Results[I].Availability;
348 CachedResult.TypeClass = STC_Void;
349 CachedResult.Type = 0;
350 CachedCompletionResults.push_back(CachedResult);
356 // Save the current top-level hash value.
357 CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
360 void ASTUnit::ClearCachedCompletionResults() {
361 CachedCompletionResults.clear();
362 CachedCompletionTypes.clear();
363 CachedCompletionAllocator = 0;
368 /// \brief Gathers information from ASTReader that will be used to initialize
370 class ASTInfoCollector : public ASTReaderListener {
371 LangOptions &LangOpt;
373 std::string &TargetTriple;
374 std::string &Predefines;
377 unsigned NumHeaderInfos;
380 ASTInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
381 std::string &TargetTriple, std::string &Predefines,
383 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
384 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
386 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
391 virtual bool ReadTargetTriple(llvm::StringRef Triple) {
392 TargetTriple = Triple;
396 virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
397 llvm::StringRef OriginalFileName,
398 std::string &SuggestedPredefines,
399 FileManager &FileMgr) {
400 Predefines = Buffers[0].Data;
401 for (unsigned I = 1, N = Buffers.size(); I != N; ++I) {
402 Predefines += Buffers[I].Data;
407 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {
408 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
411 virtual void ReadCounter(unsigned Value) {
416 class StoredDiagnosticClient : public DiagnosticClient {
417 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags;
420 explicit StoredDiagnosticClient(
421 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
422 : StoredDiags(StoredDiags) { }
424 virtual void HandleDiagnostic(Diagnostic::Level Level,
425 const DiagnosticInfo &Info);
428 /// \brief RAII object that optionally captures diagnostics, if
429 /// there is no diagnostic client to capture them already.
430 class CaptureDroppedDiagnostics {
432 StoredDiagnosticClient Client;
433 DiagnosticClient *PreviousClient;
436 CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags,
437 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
438 : Diags(Diags), Client(StoredDiags), PreviousClient(0)
440 if (RequestCapture || Diags.getClient() == 0) {
441 PreviousClient = Diags.takeClient();
442 Diags.setClient(&Client);
446 ~CaptureDroppedDiagnostics() {
447 if (Diags.getClient() == &Client) {
449 Diags.setClient(PreviousClient);
454 } // anonymous namespace
456 void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level,
457 const DiagnosticInfo &Info) {
458 // Default implementation (Warnings/errors count).
459 DiagnosticClient::HandleDiagnostic(Level, Info);
461 StoredDiags.push_back(StoredDiagnostic(Level, Info));
464 const std::string &ASTUnit::getOriginalSourceFileName() {
465 return OriginalSourceFile;
468 const std::string &ASTUnit::getASTFileName() {
469 assert(isMainFileAST() && "Not an ASTUnit from an AST file!");
470 return static_cast<ASTReader *>(Ctx->getExternalSource())->getFileName();
473 llvm::MemoryBuffer *ASTUnit::getBufferForFile(llvm::StringRef Filename,
474 std::string *ErrorStr) {
476 return FileMgr->getBufferForFile(Filename, ErrorStr);
479 /// \brief Configure the diagnostics object for use with ASTUnit.
480 void ASTUnit::ConfigureDiags(llvm::IntrusiveRefCntPtr<Diagnostic> &Diags,
481 const char **ArgBegin, const char **ArgEnd,
482 ASTUnit &AST, bool CaptureDiagnostics) {
483 if (!Diags.getPtr()) {
484 // No diagnostics engine was provided, so create our own diagnostics object
485 // with the default options.
486 DiagnosticOptions DiagOpts;
487 DiagnosticClient *Client = 0;
488 if (CaptureDiagnostics)
489 Client = new StoredDiagnosticClient(AST.StoredDiagnostics);
490 Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd- ArgBegin,
492 } else if (CaptureDiagnostics) {
493 Diags->setClient(new StoredDiagnosticClient(AST.StoredDiagnostics));
497 ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename,
498 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
499 const FileSystemOptions &FileSystemOpts,
501 RemappedFile *RemappedFiles,
502 unsigned NumRemappedFiles,
503 bool CaptureDiagnostics) {
504 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
506 // Recover resources if we crash before exiting this method.
507 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
508 ASTUnitCleanup(AST.get());
509 llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
510 llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
511 DiagCleanup(Diags.getPtr());
513 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
515 AST->OnlyLocalDecls = OnlyLocalDecls;
516 AST->CaptureDiagnostics = CaptureDiagnostics;
517 AST->Diagnostics = Diags;
518 AST->FileMgr = new FileManager(FileSystemOpts);
519 AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
520 AST->getFileManager());
521 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
523 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
524 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
525 if (const llvm::MemoryBuffer *
526 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
527 // Create the file entry for the file that we're mapping from.
528 const FileEntry *FromFile
529 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
530 memBuf->getBufferSize(),
533 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
534 << RemappedFiles[I].first;
539 // Override the contents of the "from" file with the contents of
541 AST->getSourceManager().overrideFileContents(FromFile, memBuf);
544 const char *fname = fileOrBuf.get<const char *>();
545 const FileEntry *ToFile = AST->FileMgr->getFile(fname);
547 AST->getDiagnostics().Report(diag::err_fe_remap_missing_to_file)
548 << RemappedFiles[I].first << fname;
552 // Create the file entry for the file that we're mapping from.
553 const FileEntry *FromFile
554 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
558 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
559 << RemappedFiles[I].first;
564 // Override the contents of the "from" file with the contents of
566 AST->getSourceManager().overrideFileContents(FromFile, ToFile);
570 // Gather Info for preprocessor construction later on.
572 LangOptions LangInfo;
573 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
574 std::string TargetTriple;
575 std::string Predefines;
578 llvm::OwningPtr<ASTReader> Reader;
580 Reader.reset(new ASTReader(AST->getSourceManager(), AST->getFileManager(),
581 AST->getDiagnostics()));
582 Reader->setListener(new ASTInfoCollector(LangInfo, HeaderInfo, TargetTriple,
583 Predefines, Counter));
585 switch (Reader->ReadAST(Filename, ASTReader::MainFile)) {
586 case ASTReader::Success:
589 case ASTReader::Failure:
590 case ASTReader::IgnorePCH:
591 AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
595 AST->OriginalSourceFile = Reader->getOriginalSourceFile();
597 // AST file loaded successfully. Now create the preprocessor.
599 // Get information about the target being compiled for.
601 // FIXME: This is broken, we should store the TargetOptions in the AST file.
602 TargetOptions TargetOpts;
604 TargetOpts.CXXABI = "";
606 TargetOpts.Features.clear();
607 TargetOpts.Triple = TargetTriple;
608 AST->Target = TargetInfo::CreateTargetInfo(AST->getDiagnostics(),
610 AST->PP = new Preprocessor(AST->getDiagnostics(), LangInfo, *AST->Target,
611 AST->getSourceManager(), HeaderInfo);
612 Preprocessor &PP = *AST->PP;
614 PP.setPredefines(Reader->getSuggestedPredefines());
615 PP.setCounterValue(Counter);
616 Reader->setPreprocessor(PP);
618 // Create and initialize the ASTContext.
620 AST->Ctx = new ASTContext(LangInfo,
621 AST->getSourceManager(),
623 PP.getIdentifierTable(),
624 PP.getSelectorTable(),
626 /* size_reserve = */0);
627 ASTContext &Context = *AST->Ctx;
629 Reader->InitializeContext(Context);
631 // Attach the AST reader to the AST context as an external AST
632 // source, so that declarations will be deserialized from the
633 // AST file as needed.
634 ASTReader *ReaderPtr = Reader.get();
635 llvm::OwningPtr<ExternalASTSource> Source(Reader.take());
636 Context.setExternalSource(Source);
638 // Create an AST consumer, even though it isn't used.
639 AST->Consumer.reset(new ASTConsumer);
641 // Create a semantic analysis object and tell the AST reader about it.
642 AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer));
643 AST->TheSema->Initialize();
644 ReaderPtr->InitializeSema(*AST->TheSema);
651 /// \brief Preprocessor callback class that updates a hash value with the names
652 /// of all macros that have been defined by the translation unit.
653 class MacroDefinitionTrackerPPCallbacks : public PPCallbacks {
657 explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { }
659 virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
660 Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash);
664 /// \brief Add the given declaration to the hash of all top-level entities.
665 void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) {
669 DeclContext *DC = D->getDeclContext();
673 if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit()))
676 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
677 if (ND->getIdentifier())
678 Hash = llvm::HashString(ND->getIdentifier()->getName(), Hash);
679 else if (DeclarationName Name = ND->getDeclName()) {
680 std::string NameStr = Name.getAsString();
681 Hash = llvm::HashString(NameStr, Hash);
686 if (ObjCForwardProtocolDecl *Forward
687 = dyn_cast<ObjCForwardProtocolDecl>(D)) {
688 for (ObjCForwardProtocolDecl::protocol_iterator
689 P = Forward->protocol_begin(),
690 PEnd = Forward->protocol_end();
692 AddTopLevelDeclarationToHash(*P, Hash);
696 if (ObjCClassDecl *Class = llvm::dyn_cast<ObjCClassDecl>(D)) {
697 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
699 AddTopLevelDeclarationToHash(I->getInterface(), Hash);
704 class TopLevelDeclTrackerConsumer : public ASTConsumer {
709 TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
710 : Unit(_Unit), Hash(Hash) {
714 void HandleTopLevelDecl(DeclGroupRef D) {
715 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
717 // FIXME: Currently ObjC method declarations are incorrectly being
718 // reported as top-level declarations, even though their DeclContext
719 // is the containing ObjC @interface/@implementation. This is a
720 // fundamental problem in the parser right now.
721 if (isa<ObjCMethodDecl>(D))
724 AddTopLevelDeclarationToHash(D, Hash);
725 Unit.addTopLevelDecl(D);
729 // We're not interested in "interesting" decls.
730 void HandleInterestingDecl(DeclGroupRef) {}
733 class TopLevelDeclTrackerAction : public ASTFrontendAction {
737 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
738 llvm::StringRef InFile) {
739 CI.getPreprocessor().addPPCallbacks(
740 new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
741 return new TopLevelDeclTrackerConsumer(Unit,
742 Unit.getCurrentTopLevelHashValue());
746 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
748 virtual bool hasCodeCompletionSupport() const { return false; }
749 virtual bool usesCompleteTranslationUnit() {
750 return Unit.isCompleteTranslationUnit();
754 class PrecompilePreambleConsumer : public PCHGenerator,
755 public ASTSerializationListener {
758 std::vector<Decl *> TopLevelDecls;
761 PrecompilePreambleConsumer(ASTUnit &Unit,
762 const Preprocessor &PP, bool Chaining,
763 const char *isysroot, llvm::raw_ostream *Out)
764 : PCHGenerator(PP, "", Chaining, isysroot, Out), Unit(Unit),
765 Hash(Unit.getCurrentTopLevelHashValue()) {
769 virtual void HandleTopLevelDecl(DeclGroupRef D) {
770 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
772 // FIXME: Currently ObjC method declarations are incorrectly being
773 // reported as top-level declarations, even though their DeclContext
774 // is the containing ObjC @interface/@implementation. This is a
775 // fundamental problem in the parser right now.
776 if (isa<ObjCMethodDecl>(D))
778 AddTopLevelDeclarationToHash(D, Hash);
779 TopLevelDecls.push_back(D);
783 virtual void HandleTranslationUnit(ASTContext &Ctx) {
784 PCHGenerator::HandleTranslationUnit(Ctx);
785 if (!Unit.getDiagnostics().hasErrorOccurred()) {
786 // Translate the top-level declarations we captured during
787 // parsing into declaration IDs in the precompiled
788 // preamble. This will allow us to deserialize those top-level
789 // declarations when requested.
790 for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I)
791 Unit.addTopLevelDeclFromPreamble(
792 getWriter().getDeclID(TopLevelDecls[I]));
796 virtual void SerializedPreprocessedEntity(PreprocessedEntity *Entity,
798 Unit.addPreprocessedEntityFromPreamble(Offset);
801 virtual ASTSerializationListener *GetASTSerializationListener() {
806 class PrecompilePreambleAction : public ASTFrontendAction {
810 explicit PrecompilePreambleAction(ASTUnit &Unit) : Unit(Unit) {}
812 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
813 llvm::StringRef InFile) {
815 std::string OutputFile;
816 llvm::raw_ostream *OS = 0;
818 if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot,
823 const char *isysroot = CI.getFrontendOpts().RelocatablePCH ?
825 CI.getPreprocessor().addPPCallbacks(
826 new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
827 return new PrecompilePreambleConsumer(Unit, CI.getPreprocessor(), Chaining,
831 virtual bool hasCodeCompletionSupport() const { return false; }
832 virtual bool hasASTFileSupport() const { return false; }
833 virtual bool usesCompleteTranslationUnit() { return false; }
838 /// Parse the source file into a translation unit using the given compiler
839 /// invocation, replacing the current translation unit.
841 /// \returns True if a failure occurred that causes the ASTUnit not to
842 /// contain any translation-unit information, false otherwise.
843 bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) {
844 delete SavedMainFileBuffer;
845 SavedMainFileBuffer = 0;
848 delete OverrideMainBuffer;
852 // Create the compiler instance to use for building the AST.
853 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
855 // Recover resources if we crash before exiting this method.
856 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
857 CICleanup(Clang.get());
859 Clang->setInvocation(&*Invocation);
860 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
862 // Set up diagnostics, capturing any diagnostics that would
863 // otherwise be dropped.
864 Clang->setDiagnostics(&getDiagnostics());
866 // Create the target instance.
867 Clang->getTargetOpts().Features = TargetFeatures;
868 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
869 Clang->getTargetOpts()));
870 if (!Clang->hasTarget()) {
871 delete OverrideMainBuffer;
875 // Inform the target of the language options.
877 // FIXME: We shouldn't need to do this, the target should be immutable once
878 // created. This complexity should be lifted elsewhere.
879 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
881 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
882 "Invocation must have exactly one source file!");
883 assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
884 "FIXME: AST inputs not yet supported here!");
885 assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
886 "IR inputs not support here!");
888 // Configure the various subsystems.
889 // FIXME: Should we retain the previous file manager?
890 FileSystemOpts = Clang->getFileSystemOpts();
891 FileMgr = new FileManager(FileSystemOpts);
892 SourceMgr = new SourceManager(getDiagnostics(), *FileMgr);
897 // Clear out old caches and data.
898 TopLevelDecls.clear();
899 PreprocessedEntities.clear();
900 CleanTemporaryFiles();
901 PreprocessedEntitiesByFile.clear();
903 if (!OverrideMainBuffer) {
904 StoredDiagnostics.erase(
905 StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver,
906 StoredDiagnostics.end());
907 TopLevelDeclsInPreamble.clear();
908 PreprocessedEntitiesInPreamble.clear();
911 // Create a file manager object to provide access to and cache the filesystem.
912 Clang->setFileManager(&getFileManager());
914 // Create the source manager.
915 Clang->setSourceManager(&getSourceManager());
917 // If the main file has been overridden due to the use of a preamble,
918 // make that override happen and introduce the preamble.
919 PreprocessorOptions &PreprocessorOpts = Clang->getPreprocessorOpts();
920 std::string PriorImplicitPCHInclude;
921 if (OverrideMainBuffer) {
922 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
923 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
924 PreprocessorOpts.PrecompiledPreambleBytes.second
925 = PreambleEndsAtStartOfLine;
926 PriorImplicitPCHInclude = PreprocessorOpts.ImplicitPCHInclude;
927 PreprocessorOpts.ImplicitPCHInclude = PreambleFile;
928 PreprocessorOpts.DisablePCHValidation = true;
930 // The stored diagnostic has the old source manager in it; update
931 // the locations to refer into the new source manager. Since we've
932 // been careful to make sure that the source manager's state
933 // before and after are identical, so that we can reuse the source
935 for (unsigned I = NumStoredDiagnosticsFromDriver,
936 N = StoredDiagnostics.size();
938 FullSourceLoc Loc(StoredDiagnostics[I].getLocation(),
940 StoredDiagnostics[I].setLocation(Loc);
943 // Keep track of the override buffer;
944 SavedMainFileBuffer = OverrideMainBuffer;
946 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
947 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
950 llvm::OwningPtr<TopLevelDeclTrackerAction> Act(
951 new TopLevelDeclTrackerAction(*this));
953 // Recover resources if we crash before exiting this method.
954 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
955 ActCleanup(Act.get());
957 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
958 Clang->getFrontendOpts().Inputs[0].first))
963 // Steal the created target, context, and preprocessor.
964 TheSema.reset(Clang->takeSema());
965 Consumer.reset(Clang->takeASTConsumer());
966 Ctx = &Clang->getASTContext();
967 PP = &Clang->getPreprocessor();
968 Clang->setSourceManager(0);
969 Clang->setFileManager(0);
970 Target = &Clang->getTarget();
972 Act->EndSourceFile();
974 // Remove the overridden buffer we used for the preamble.
975 if (OverrideMainBuffer) {
976 PreprocessorOpts.eraseRemappedFile(
977 PreprocessorOpts.remapped_file_buffer_end() - 1);
978 PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude;
984 // Remove the overridden buffer we used for the preamble.
985 if (OverrideMainBuffer) {
986 PreprocessorOpts.eraseRemappedFile(
987 PreprocessorOpts.remapped_file_buffer_end() - 1);
988 PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude;
989 delete OverrideMainBuffer;
990 SavedMainFileBuffer = 0;
993 StoredDiagnostics.clear();
997 /// \brief Simple function to retrieve a path for a preamble precompiled header.
998 static std::string GetPreamblePCHPath() {
999 // FIXME: This is lame; sys::Path should provide this function (in particular,
1000 // it should know how to find the temporary files dir).
1001 // FIXME: This is really lame. I copied this code from the Driver!
1002 // FIXME: This is a hack so that we can override the preamble file during
1003 // crash-recovery testing, which is the only case where the preamble files
1004 // are not necessarily cleaned up.
1005 const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
1010 const char *TmpDir = ::getenv("TMPDIR");
1012 TmpDir = ::getenv("TEMP");
1014 TmpDir = ::getenv("TMP");
1015 #ifdef LLVM_ON_WIN32
1017 TmpDir = ::getenv("USERPROFILE");
1021 llvm::sys::Path P(TmpDir);
1022 P.createDirectoryOnDisk(true);
1023 P.appendComponent("preamble");
1024 P.appendSuffix("pch");
1025 if (P.createTemporaryFileOnDisk())
1026 return std::string();
1031 /// \brief Compute the preamble for the main file, providing the source buffer
1032 /// that corresponds to the main file along with a pair (bytes, start-of-line)
1033 /// that describes the preamble.
1034 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
1035 ASTUnit::ComputePreamble(CompilerInvocation &Invocation,
1036 unsigned MaxLines, bool &CreatedBuffer) {
1037 FrontendOptions &FrontendOpts = Invocation.getFrontendOpts();
1038 PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts();
1039 CreatedBuffer = false;
1041 // Try to determine if the main file has been remapped, either from the
1042 // command line (to another file) or directly through the compiler invocation
1043 // (to a memory buffer).
1044 llvm::MemoryBuffer *Buffer = 0;
1045 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second);
1046 if (const llvm::sys::FileStatus *MainFileStatus = MainFilePath.getFileStatus()) {
1047 // Check whether there is a file-file remapping of the main file
1048 for (PreprocessorOptions::remapped_file_iterator
1049 M = PreprocessorOpts.remapped_file_begin(),
1050 E = PreprocessorOpts.remapped_file_end();
1053 llvm::sys::PathWithStatus MPath(M->first);
1054 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1055 if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1056 // We found a remapping. Try to load the resulting, remapped source.
1057 if (CreatedBuffer) {
1059 CreatedBuffer = false;
1062 Buffer = getBufferForFile(M->second);
1064 return std::make_pair((llvm::MemoryBuffer*)0,
1065 std::make_pair(0, true));
1066 CreatedBuffer = true;
1071 // Check whether there is a file-buffer remapping. It supercedes the
1072 // file-file remapping.
1073 for (PreprocessorOptions::remapped_file_buffer_iterator
1074 M = PreprocessorOpts.remapped_file_buffer_begin(),
1075 E = PreprocessorOpts.remapped_file_buffer_end();
1078 llvm::sys::PathWithStatus MPath(M->first);
1079 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1080 if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1081 // We found a remapping.
1082 if (CreatedBuffer) {
1084 CreatedBuffer = false;
1087 Buffer = const_cast<llvm::MemoryBuffer *>(M->second);
1093 // If the main source file was not remapped, load it now.
1095 Buffer = getBufferForFile(FrontendOpts.Inputs[0].second);
1097 return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true));
1099 CreatedBuffer = true;
1102 return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer, MaxLines));
1105 static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old,
1107 llvm::StringRef NewName) {
1108 llvm::MemoryBuffer *Result
1109 = llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName);
1110 memcpy(const_cast<char*>(Result->getBufferStart()),
1111 Old->getBufferStart(), Old->getBufferSize());
1112 memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(),
1113 ' ', NewSize - Old->getBufferSize() - 1);
1114 const_cast<char*>(Result->getBufferEnd())[-1] = '\n';
1119 /// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing
1120 /// the source file.
1122 /// This routine will compute the preamble of the main source file. If a
1123 /// non-trivial preamble is found, it will precompile that preamble into a
1124 /// precompiled header so that the precompiled preamble can be used to reduce
1125 /// reparsing time. If a precompiled preamble has already been constructed,
1126 /// this routine will determine if it is still valid and, if so, avoid
1127 /// rebuilding the precompiled preamble.
1129 /// \param AllowRebuild When true (the default), this routine is
1130 /// allowed to rebuild the precompiled preamble if it is found to be
1133 /// \param MaxLines When non-zero, the maximum number of lines that
1134 /// can occur within the preamble.
1136 /// \returns If the precompiled preamble can be used, returns a newly-allocated
1137 /// buffer that should be used in place of the main file when doing so.
1138 /// Otherwise, returns a NULL pointer.
1139 llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
1140 CompilerInvocation PreambleInvocation,
1142 unsigned MaxLines) {
1143 FrontendOptions &FrontendOpts = PreambleInvocation.getFrontendOpts();
1144 PreprocessorOptions &PreprocessorOpts
1145 = PreambleInvocation.getPreprocessorOpts();
1147 bool CreatedPreambleBuffer = false;
1148 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble
1149 = ComputePreamble(PreambleInvocation, MaxLines, CreatedPreambleBuffer);
1151 // If ComputePreamble() Take ownership of the
1152 llvm::OwningPtr<llvm::MemoryBuffer> OwnedPreambleBuffer;
1153 if (CreatedPreambleBuffer)
1154 OwnedPreambleBuffer.reset(NewPreamble.first);
1156 if (!NewPreamble.second.first) {
1157 // We couldn't find a preamble in the main source. Clear out the current
1158 // preamble, if we have one. It's obviously no good any more.
1160 if (!PreambleFile.empty()) {
1161 llvm::sys::Path(PreambleFile).eraseFromDisk();
1162 PreambleFile.clear();
1165 // The next time we actually see a preamble, precompile it.
1166 PreambleRebuildCounter = 1;
1170 if (!Preamble.empty()) {
1171 // We've previously computed a preamble. Check whether we have the same
1172 // preamble now that we did before, and that there's enough space in
1173 // the main-file buffer within the precompiled preamble to fit the
1175 if (Preamble.size() == NewPreamble.second.first &&
1176 PreambleEndsAtStartOfLine == NewPreamble.second.second &&
1177 NewPreamble.first->getBufferSize() < PreambleReservedSize-2 &&
1178 memcmp(&Preamble[0], NewPreamble.first->getBufferStart(),
1179 NewPreamble.second.first) == 0) {
1180 // The preamble has not changed. We may be able to re-use the precompiled
1183 // Check that none of the files used by the preamble have changed.
1184 bool AnyFileChanged = false;
1186 // First, make a record of those files that have been overridden via
1187 // remapping or unsaved_files.
1188 llvm::StringMap<std::pair<off_t, time_t> > OverriddenFiles;
1189 for (PreprocessorOptions::remapped_file_iterator
1190 R = PreprocessorOpts.remapped_file_begin(),
1191 REnd = PreprocessorOpts.remapped_file_end();
1192 !AnyFileChanged && R != REnd;
1194 struct stat StatBuf;
1195 if (FileMgr->getNoncachedStatValue(R->second, StatBuf)) {
1196 // If we can't stat the file we're remapping to, assume that something
1197 // horrible happened.
1198 AnyFileChanged = true;
1202 OverriddenFiles[R->first] = std::make_pair(StatBuf.st_size,
1205 for (PreprocessorOptions::remapped_file_buffer_iterator
1206 R = PreprocessorOpts.remapped_file_buffer_begin(),
1207 REnd = PreprocessorOpts.remapped_file_buffer_end();
1208 !AnyFileChanged && R != REnd;
1210 // FIXME: Should we actually compare the contents of file->buffer
1212 OverriddenFiles[R->first] = std::make_pair(R->second->getBufferSize(),
1216 // Check whether anything has changed.
1217 for (llvm::StringMap<std::pair<off_t, time_t> >::iterator
1218 F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end();
1219 !AnyFileChanged && F != FEnd;
1221 llvm::StringMap<std::pair<off_t, time_t> >::iterator Overridden
1222 = OverriddenFiles.find(F->first());
1223 if (Overridden != OverriddenFiles.end()) {
1224 // This file was remapped; check whether the newly-mapped file
1225 // matches up with the previous mapping.
1226 if (Overridden->second != F->second)
1227 AnyFileChanged = true;
1231 // The file was not remapped; check whether it has changed on disk.
1232 struct stat StatBuf;
1233 if (FileMgr->getNoncachedStatValue(F->first(), StatBuf)) {
1234 // If we can't stat the file, assume that something horrible happened.
1235 AnyFileChanged = true;
1236 } else if (StatBuf.st_size != F->second.first ||
1237 StatBuf.st_mtime != F->second.second)
1238 AnyFileChanged = true;
1241 if (!AnyFileChanged) {
1242 // Okay! We can re-use the precompiled preamble.
1244 // Set the state of the diagnostic object to mimic its state
1245 // after parsing the preamble.
1246 // FIXME: This won't catch any #pragma push warning changes that
1247 // have occurred in the preamble.
1248 getDiagnostics().Reset();
1249 ProcessWarningOptions(getDiagnostics(),
1250 PreambleInvocation.getDiagnosticOpts());
1251 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1252 if (StoredDiagnostics.size() > NumStoredDiagnosticsInPreamble)
1253 StoredDiagnostics.erase(
1254 StoredDiagnostics.begin() + NumStoredDiagnosticsInPreamble,
1255 StoredDiagnostics.end());
1257 // Create a version of the main file buffer that is padded to
1258 // buffer size we reserved when creating the preamble.
1259 return CreatePaddedMainFileBuffer(NewPreamble.first,
1260 PreambleReservedSize,
1261 FrontendOpts.Inputs[0].second);
1265 // If we aren't allowed to rebuild the precompiled preamble, just
1270 // We can't reuse the previously-computed preamble. Build a new one.
1272 llvm::sys::Path(PreambleFile).eraseFromDisk();
1273 PreambleRebuildCounter = 1;
1274 } else if (!AllowRebuild) {
1275 // We aren't allowed to rebuild the precompiled preamble; just
1280 // If the preamble rebuild counter > 1, it's because we previously
1281 // failed to build a preamble and we're not yet ready to try
1282 // again. Decrement the counter and return a failure.
1283 if (PreambleRebuildCounter > 1) {
1284 --PreambleRebuildCounter;
1288 // Create a temporary file for the precompiled preamble. In rare
1289 // circumstances, this can fail.
1290 std::string PreamblePCHPath = GetPreamblePCHPath();
1291 if (PreamblePCHPath.empty()) {
1292 // Try again next time.
1293 PreambleRebuildCounter = 1;
1297 // We did not previously compute a preamble, or it can't be reused anyway.
1298 SimpleTimer PreambleTimer(WantTiming);
1299 PreambleTimer.setOutput("Precompiling preamble");
1301 // Create a new buffer that stores the preamble. The buffer also contains
1302 // extra space for the original contents of the file (which will be present
1303 // when we actually parse the file) along with more room in case the file
1305 PreambleReservedSize = NewPreamble.first->getBufferSize();
1306 if (PreambleReservedSize < 4096)
1307 PreambleReservedSize = 8191;
1309 PreambleReservedSize *= 2;
1311 // Save the preamble text for later; we'll need to compare against it for
1312 // subsequent reparses.
1313 Preamble.assign(NewPreamble.first->getBufferStart(),
1314 NewPreamble.first->getBufferStart()
1315 + NewPreamble.second.first);
1316 PreambleEndsAtStartOfLine = NewPreamble.second.second;
1318 delete PreambleBuffer;
1320 = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize,
1321 FrontendOpts.Inputs[0].second);
1322 memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()),
1323 NewPreamble.first->getBufferStart(), Preamble.size());
1324 memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(),
1325 ' ', PreambleReservedSize - Preamble.size() - 1);
1326 const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n';
1328 // Remap the main source file to the preamble buffer.
1329 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second);
1330 PreprocessorOpts.addRemappedFile(MainFilePath.str(), PreambleBuffer);
1332 // Tell the compiler invocation to generate a temporary precompiled header.
1333 FrontendOpts.ProgramAction = frontend::GeneratePCH;
1334 FrontendOpts.ChainedPCH = true;
1335 // FIXME: Generate the precompiled header into memory?
1336 FrontendOpts.OutputFile = PreamblePCHPath;
1337 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
1338 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
1340 // Create the compiler instance to use for building the precompiled preamble.
1341 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1343 // Recover resources if we crash before exiting this method.
1344 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1345 CICleanup(Clang.get());
1347 Clang->setInvocation(&PreambleInvocation);
1348 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
1350 // Set up diagnostics, capturing all of the diagnostics produced.
1351 Clang->setDiagnostics(&getDiagnostics());
1353 // Create the target instance.
1354 Clang->getTargetOpts().Features = TargetFeatures;
1355 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1356 Clang->getTargetOpts()));
1357 if (!Clang->hasTarget()) {
1358 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1360 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1361 PreprocessorOpts.eraseRemappedFile(
1362 PreprocessorOpts.remapped_file_buffer_end() - 1);
1366 // Inform the target of the language options.
1368 // FIXME: We shouldn't need to do this, the target should be immutable once
1369 // created. This complexity should be lifted elsewhere.
1370 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1372 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1373 "Invocation must have exactly one source file!");
1374 assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
1375 "FIXME: AST inputs not yet supported here!");
1376 assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
1377 "IR inputs not support here!");
1379 // Clear out old caches and data.
1380 getDiagnostics().Reset();
1381 ProcessWarningOptions(getDiagnostics(), Clang->getDiagnosticOpts());
1382 StoredDiagnostics.erase(
1383 StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver,
1384 StoredDiagnostics.end());
1385 TopLevelDecls.clear();
1386 TopLevelDeclsInPreamble.clear();
1387 PreprocessedEntities.clear();
1388 PreprocessedEntitiesInPreamble.clear();
1390 // Create a file manager object to provide access to and cache the filesystem.
1391 Clang->setFileManager(new FileManager(Clang->getFileSystemOpts()));
1393 // Create the source manager.
1394 Clang->setSourceManager(new SourceManager(getDiagnostics(),
1395 Clang->getFileManager()));
1397 llvm::OwningPtr<PrecompilePreambleAction> Act;
1398 Act.reset(new PrecompilePreambleAction(*this));
1399 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
1400 Clang->getFrontendOpts().Inputs[0].first)) {
1401 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1403 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1404 PreprocessorOpts.eraseRemappedFile(
1405 PreprocessorOpts.remapped_file_buffer_end() - 1);
1410 Act->EndSourceFile();
1412 if (Diagnostics->hasErrorOccurred()) {
1413 // There were errors parsing the preamble, so no precompiled header was
1414 // generated. Forget that we even tried.
1415 // FIXME: Should we leave a note for ourselves to try again?
1416 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1418 TopLevelDeclsInPreamble.clear();
1419 PreprocessedEntities.clear();
1420 PreprocessedEntitiesInPreamble.clear();
1421 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1422 PreprocessorOpts.eraseRemappedFile(
1423 PreprocessorOpts.remapped_file_buffer_end() - 1);
1427 // Keep track of the preamble we precompiled.
1428 PreambleFile = FrontendOpts.OutputFile;
1429 NumStoredDiagnosticsInPreamble = StoredDiagnostics.size();
1430 NumWarningsInPreamble = getDiagnostics().getNumWarnings();
1432 // Keep track of all of the files that the source manager knows about,
1433 // so we can verify whether they have changed or not.
1434 FilesInPreamble.clear();
1435 SourceManager &SourceMgr = Clang->getSourceManager();
1436 const llvm::MemoryBuffer *MainFileBuffer
1437 = SourceMgr.getBuffer(SourceMgr.getMainFileID());
1438 for (SourceManager::fileinfo_iterator F = SourceMgr.fileinfo_begin(),
1439 FEnd = SourceMgr.fileinfo_end();
1442 const FileEntry *File = F->second->OrigEntry;
1443 if (!File || F->second->getRawBuffer() == MainFileBuffer)
1446 FilesInPreamble[File->getName()]
1447 = std::make_pair(F->second->getSize(), File->getModificationTime());
1450 PreambleRebuildCounter = 1;
1451 PreprocessorOpts.eraseRemappedFile(
1452 PreprocessorOpts.remapped_file_buffer_end() - 1);
1454 // If the hash of top-level entities differs from the hash of the top-level
1455 // entities the last time we rebuilt the preamble, clear out the completion
1457 if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1458 CompletionCacheTopLevelHashValue = 0;
1459 PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1462 return CreatePaddedMainFileBuffer(NewPreamble.first,
1463 PreambleReservedSize,
1464 FrontendOpts.Inputs[0].second);
1467 void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1468 std::vector<Decl *> Resolved;
1469 Resolved.reserve(TopLevelDeclsInPreamble.size());
1470 ExternalASTSource &Source = *getASTContext().getExternalSource();
1471 for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) {
1472 // Resolve the declaration ID to an actual declaration, possibly
1473 // deserializing the declaration in the process.
1474 Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]);
1476 Resolved.push_back(D);
1478 TopLevelDeclsInPreamble.clear();
1479 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1482 void ASTUnit::RealizePreprocessedEntitiesFromPreamble() {
1486 PreprocessingRecord *PPRec = PP->getPreprocessingRecord();
1490 ExternalPreprocessingRecordSource *External = PPRec->getExternalSource();
1494 for (unsigned I = 0, N = PreprocessedEntitiesInPreamble.size(); I != N; ++I) {
1495 if (PreprocessedEntity *PE
1496 = External->ReadPreprocessedEntityAtOffset(
1497 PreprocessedEntitiesInPreamble[I]))
1498 PreprocessedEntities.push_back(PE);
1501 if (PreprocessedEntities.empty())
1504 PreprocessedEntities.insert(PreprocessedEntities.end(),
1505 PPRec->begin(true), PPRec->end(true));
1508 ASTUnit::pp_entity_iterator ASTUnit::pp_entity_begin() {
1509 if (!PreprocessedEntitiesInPreamble.empty() &&
1510 PreprocessedEntities.empty())
1511 RealizePreprocessedEntitiesFromPreamble();
1513 if (PreprocessedEntities.empty())
1514 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
1515 return PPRec->begin(true);
1517 return PreprocessedEntities.begin();
1520 ASTUnit::pp_entity_iterator ASTUnit::pp_entity_end() {
1521 if (!PreprocessedEntitiesInPreamble.empty() &&
1522 PreprocessedEntities.empty())
1523 RealizePreprocessedEntitiesFromPreamble();
1525 if (PreprocessedEntities.empty())
1526 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
1527 return PPRec->end(true);
1529 return PreprocessedEntities.end();
1532 unsigned ASTUnit::getMaxPCHLevel() const {
1533 if (!getOnlyLocalDecls())
1534 return Decl::MaxPCHLevel;
1539 llvm::StringRef ASTUnit::getMainFileName() const {
1540 return Invocation->getFrontendOpts().Inputs[0].second;
1543 ASTUnit *ASTUnit::create(CompilerInvocation *CI,
1544 llvm::IntrusiveRefCntPtr<Diagnostic> Diags) {
1545 llvm::OwningPtr<ASTUnit> AST;
1546 AST.reset(new ASTUnit(false));
1547 ConfigureDiags(Diags, 0, 0, *AST, /*CaptureDiagnostics=*/false);
1548 AST->Diagnostics = Diags;
1549 AST->Invocation = CI;
1550 AST->FileSystemOpts = CI->getFileSystemOpts();
1551 AST->FileMgr = new FileManager(AST->FileSystemOpts);
1552 AST->SourceMgr = new SourceManager(*Diags, *AST->FileMgr);
1557 bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) {
1561 // We'll manage file buffers ourselves.
1562 Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1563 Invocation->getFrontendOpts().DisableFree = false;
1564 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1566 // Save the target features.
1567 TargetFeatures = Invocation->getTargetOpts().Features;
1569 llvm::MemoryBuffer *OverrideMainBuffer = 0;
1570 if (PrecompilePreamble) {
1571 PreambleRebuildCounter = 2;
1573 = getMainBufferWithPrecompiledPreamble(*Invocation);
1576 SimpleTimer ParsingTimer(WantTiming);
1577 ParsingTimer.setOutput("Parsing " + getMainFileName());
1579 // Recover resources if we crash before exiting this method.
1580 llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1581 MemBufferCleanup(OverrideMainBuffer);
1583 return Parse(OverrideMainBuffer);
1586 ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
1587 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
1588 bool OnlyLocalDecls,
1589 bool CaptureDiagnostics,
1590 bool PrecompilePreamble,
1591 bool CompleteTranslationUnit,
1592 bool CacheCodeCompletionResults) {
1593 // Create the AST unit.
1594 llvm::OwningPtr<ASTUnit> AST;
1595 AST.reset(new ASTUnit(false));
1596 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
1597 AST->Diagnostics = Diags;
1598 AST->OnlyLocalDecls = OnlyLocalDecls;
1599 AST->CaptureDiagnostics = CaptureDiagnostics;
1600 AST->CompleteTranslationUnit = CompleteTranslationUnit;
1601 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1602 AST->Invocation = CI;
1604 // Recover resources if we crash before exiting this method.
1605 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1606 ASTUnitCleanup(AST.get());
1607 llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
1608 llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
1609 DiagCleanup(Diags.getPtr());
1611 return AST->LoadFromCompilerInvocation(PrecompilePreamble)? 0 : AST.take();
1614 ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
1615 const char **ArgEnd,
1616 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
1617 llvm::StringRef ResourceFilesPath,
1618 bool OnlyLocalDecls,
1619 bool CaptureDiagnostics,
1620 RemappedFile *RemappedFiles,
1621 unsigned NumRemappedFiles,
1622 bool RemappedFilesKeepOriginalName,
1623 bool PrecompilePreamble,
1624 bool CompleteTranslationUnit,
1625 bool CacheCodeCompletionResults,
1626 bool CXXPrecompilePreamble,
1627 bool CXXChainedPCH) {
1628 if (!Diags.getPtr()) {
1629 // No diagnostics engine was provided, so create our own diagnostics object
1630 // with the default options.
1631 DiagnosticOptions DiagOpts;
1632 Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd - ArgBegin,
1636 llvm::SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1638 llvm::IntrusiveRefCntPtr<CompilerInvocation> CI;
1641 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
1644 CI = clang::createInvocationFromCommandLine(
1645 llvm::ArrayRef<const char *>(ArgBegin, ArgEnd-ArgBegin),
1651 // Override any files that need remapping
1652 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
1653 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
1654 if (const llvm::MemoryBuffer *
1655 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
1656 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, memBuf);
1658 const char *fname = fileOrBuf.get<const char *>();
1659 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, fname);
1662 CI->getPreprocessorOpts().RemappedFilesKeepOriginalName =
1663 RemappedFilesKeepOriginalName;
1665 // Override the resources path.
1666 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1668 // Check whether we should precompile the preamble and/or use chained PCH.
1669 // FIXME: This is a temporary hack while we debug C++ chained PCH.
1670 if (CI->getLangOpts().CPlusPlus) {
1671 PrecompilePreamble = PrecompilePreamble && CXXPrecompilePreamble;
1673 if (PrecompilePreamble && !CXXChainedPCH &&
1674 !CI->getPreprocessorOpts().ImplicitPCHInclude.empty())
1675 PrecompilePreamble = false;
1678 // Create the AST unit.
1679 llvm::OwningPtr<ASTUnit> AST;
1680 AST.reset(new ASTUnit(false));
1681 ConfigureDiags(Diags, ArgBegin, ArgEnd, *AST, CaptureDiagnostics);
1682 AST->Diagnostics = Diags;
1684 AST->FileSystemOpts = CI->getFileSystemOpts();
1685 AST->FileMgr = new FileManager(AST->FileSystemOpts);
1686 AST->OnlyLocalDecls = OnlyLocalDecls;
1687 AST->CaptureDiagnostics = CaptureDiagnostics;
1688 AST->CompleteTranslationUnit = CompleteTranslationUnit;
1689 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1690 AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1691 AST->NumStoredDiagnosticsInPreamble = StoredDiagnostics.size();
1692 AST->StoredDiagnostics.swap(StoredDiagnostics);
1693 AST->Invocation = CI;
1695 // Recover resources if we crash before exiting this method.
1696 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1697 ASTUnitCleanup(AST.get());
1698 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation,
1699 llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> >
1700 CICleanup(CI.getPtr());
1701 llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
1702 llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
1703 DiagCleanup(Diags.getPtr());
1705 return AST->LoadFromCompilerInvocation(PrecompilePreamble) ? 0 : AST.take();
1708 bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) {
1712 SimpleTimer ParsingTimer(WantTiming);
1713 ParsingTimer.setOutput("Reparsing " + getMainFileName());
1716 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1717 PPOpts.DisableStatCache = true;
1718 for (PreprocessorOptions::remapped_file_buffer_iterator
1719 R = PPOpts.remapped_file_buffer_begin(),
1720 REnd = PPOpts.remapped_file_buffer_end();
1725 Invocation->getPreprocessorOpts().clearRemappedFiles();
1726 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
1727 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
1728 if (const llvm::MemoryBuffer *
1729 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
1730 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1733 const char *fname = fileOrBuf.get<const char *>();
1734 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1739 // If we have a preamble file lying around, or if we might try to
1740 // build a precompiled preamble, do so now.
1741 llvm::MemoryBuffer *OverrideMainBuffer = 0;
1742 if (!PreambleFile.empty() || PreambleRebuildCounter > 0)
1743 OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation);
1745 // Clear out the diagnostics state.
1746 if (!OverrideMainBuffer) {
1747 getDiagnostics().Reset();
1748 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1751 // Parse the sources
1752 bool Result = Parse(OverrideMainBuffer);
1754 // If we're caching global code-completion results, and the top-level
1755 // declarations have changed, clear out the code-completion cache.
1756 if (!Result && ShouldCacheCodeCompletionResults &&
1757 CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
1758 CacheCodeCompletionResults();
1763 //----------------------------------------------------------------------------//
1765 //----------------------------------------------------------------------------//
1768 /// \brief Code completion consumer that combines the cached code-completion
1769 /// results from an ASTUnit with the code-completion results provided to it,
1770 /// then passes the result on to
1771 class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
1772 unsigned NormalContexts;
1774 CodeCompleteConsumer &Next;
1777 AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
1778 bool IncludeMacros, bool IncludeCodePatterns,
1779 bool IncludeGlobals)
1780 : CodeCompleteConsumer(IncludeMacros, IncludeCodePatterns, IncludeGlobals,
1781 Next.isOutputBinary()), AST(AST), Next(Next)
1783 // Compute the set of contexts in which we will look when we don't have
1784 // any information about the specific context.
1786 = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
1787 | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1))
1788 | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1))
1789 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
1790 | (1 << (CodeCompletionContext::CCC_Statement - 1))
1791 | (1 << (CodeCompletionContext::CCC_Expression - 1))
1792 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
1793 | (1 << (CodeCompletionContext::CCC_MemberAccess - 1))
1794 | (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1))
1795 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
1796 | (1 << (CodeCompletionContext::CCC_Recovery - 1));
1798 if (AST.getASTContext().getLangOptions().CPlusPlus)
1799 NormalContexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1))
1800 | (1 << (CodeCompletionContext::CCC_UnionTag - 1))
1801 | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1));
1804 virtual void ProcessCodeCompleteResults(Sema &S,
1805 CodeCompletionContext Context,
1806 CodeCompletionResult *Results,
1807 unsigned NumResults);
1809 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
1810 OverloadCandidate *Candidates,
1811 unsigned NumCandidates) {
1812 Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
1815 virtual CodeCompletionAllocator &getAllocator() {
1816 return Next.getAllocator();
1821 /// \brief Helper function that computes which global names are hidden by the
1822 /// local code-completion results.
1823 static void CalculateHiddenNames(const CodeCompletionContext &Context,
1824 CodeCompletionResult *Results,
1825 unsigned NumResults,
1827 llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
1828 bool OnlyTagNames = false;
1829 switch (Context.getKind()) {
1830 case CodeCompletionContext::CCC_Recovery:
1831 case CodeCompletionContext::CCC_TopLevel:
1832 case CodeCompletionContext::CCC_ObjCInterface:
1833 case CodeCompletionContext::CCC_ObjCImplementation:
1834 case CodeCompletionContext::CCC_ObjCIvarList:
1835 case CodeCompletionContext::CCC_ClassStructUnion:
1836 case CodeCompletionContext::CCC_Statement:
1837 case CodeCompletionContext::CCC_Expression:
1838 case CodeCompletionContext::CCC_ObjCMessageReceiver:
1839 case CodeCompletionContext::CCC_MemberAccess:
1840 case CodeCompletionContext::CCC_Namespace:
1841 case CodeCompletionContext::CCC_Type:
1842 case CodeCompletionContext::CCC_Name:
1843 case CodeCompletionContext::CCC_PotentiallyQualifiedName:
1844 case CodeCompletionContext::CCC_ParenthesizedExpression:
1847 case CodeCompletionContext::CCC_EnumTag:
1848 case CodeCompletionContext::CCC_UnionTag:
1849 case CodeCompletionContext::CCC_ClassOrStructTag:
1850 OnlyTagNames = true;
1853 case CodeCompletionContext::CCC_ObjCProtocolName:
1854 case CodeCompletionContext::CCC_MacroName:
1855 case CodeCompletionContext::CCC_MacroNameUse:
1856 case CodeCompletionContext::CCC_PreprocessorExpression:
1857 case CodeCompletionContext::CCC_PreprocessorDirective:
1858 case CodeCompletionContext::CCC_NaturalLanguage:
1859 case CodeCompletionContext::CCC_SelectorName:
1860 case CodeCompletionContext::CCC_TypeQualifiers:
1861 case CodeCompletionContext::CCC_Other:
1862 case CodeCompletionContext::CCC_OtherWithMacros:
1863 // We're looking for nothing, or we're looking for names that cannot
1868 typedef CodeCompletionResult Result;
1869 for (unsigned I = 0; I != NumResults; ++I) {
1870 if (Results[I].Kind != Result::RK_Declaration)
1874 = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
1876 bool Hiding = false;
1878 Hiding = (IDNS & Decl::IDNS_Tag);
1880 unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
1881 Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
1882 Decl::IDNS_NonMemberOperator);
1883 if (Ctx.getLangOptions().CPlusPlus)
1884 HiddenIDNS |= Decl::IDNS_Tag;
1885 Hiding = (IDNS & HiddenIDNS);
1891 DeclarationName Name = Results[I].Declaration->getDeclName();
1892 if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
1893 HiddenNames.insert(Identifier->getName());
1895 HiddenNames.insert(Name.getAsString());
1900 void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
1901 CodeCompletionContext Context,
1902 CodeCompletionResult *Results,
1903 unsigned NumResults) {
1904 // Merge the results we were given with the results we cached.
1905 bool AddedResult = false;
1907 = (Context.getKind() == CodeCompletionContext::CCC_Recovery? NormalContexts
1908 : (1 << (Context.getKind() - 1)));
1910 // Contains the set of names that are hidden by "local" completion results.
1911 llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
1912 typedef CodeCompletionResult Result;
1913 llvm::SmallVector<Result, 8> AllResults;
1914 for (ASTUnit::cached_completion_iterator
1915 C = AST.cached_completion_begin(),
1916 CEnd = AST.cached_completion_end();
1918 // If the context we are in matches any of the contexts we are
1919 // interested in, we'll add this result.
1920 if ((C->ShowInContexts & InContexts) == 0)
1923 // If we haven't added any results previously, do so now.
1925 CalculateHiddenNames(Context, Results, NumResults, S.Context,
1927 AllResults.insert(AllResults.end(), Results, Results + NumResults);
1931 // Determine whether this global completion result is hidden by a local
1932 // completion result. If so, skip it.
1933 if (C->Kind != CXCursor_MacroDefinition &&
1934 HiddenNames.count(C->Completion->getTypedText()))
1937 // Adjust priority based on similar type classes.
1938 unsigned Priority = C->Priority;
1939 CXCursorKind CursorKind = C->Kind;
1940 CodeCompletionString *Completion = C->Completion;
1941 if (!Context.getPreferredType().isNull()) {
1942 if (C->Kind == CXCursor_MacroDefinition) {
1943 Priority = getMacroUsagePriority(C->Completion->getTypedText(),
1945 Context.getPreferredType()->isAnyPointerType());
1946 } else if (C->Type) {
1947 CanQualType Expected
1948 = S.Context.getCanonicalType(
1949 Context.getPreferredType().getUnqualifiedType());
1950 SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
1951 if (ExpectedSTC == C->TypeClass) {
1952 // We know this type is similar; check for an exact match.
1953 llvm::StringMap<unsigned> &CachedCompletionTypes
1954 = AST.getCachedCompletionTypes();
1955 llvm::StringMap<unsigned>::iterator Pos
1956 = CachedCompletionTypes.find(QualType(Expected).getAsString());
1957 if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
1958 Priority /= CCF_ExactTypeMatch;
1960 Priority /= CCF_SimilarTypeMatch;
1965 // Adjust the completion string, if required.
1966 if (C->Kind == CXCursor_MacroDefinition &&
1967 Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
1968 // Create a new code-completion string that just contains the
1969 // macro name, without its arguments.
1970 CodeCompletionBuilder Builder(getAllocator(), CCP_CodePattern,
1972 Builder.AddTypedTextChunk(C->Completion->getTypedText());
1973 CursorKind = CXCursor_NotImplemented;
1974 Priority = CCP_CodePattern;
1975 Completion = Builder.TakeString();
1978 AllResults.push_back(Result(Completion, Priority, CursorKind,
1982 // If we did not add any cached completion results, just forward the
1983 // results we were given to the next consumer.
1985 Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
1989 Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
1995 void ASTUnit::CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column,
1996 RemappedFile *RemappedFiles,
1997 unsigned NumRemappedFiles,
1999 bool IncludeCodePatterns,
2000 CodeCompleteConsumer &Consumer,
2001 Diagnostic &Diag, LangOptions &LangOpts,
2002 SourceManager &SourceMgr, FileManager &FileMgr,
2003 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2004 llvm::SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
2008 SimpleTimer CompletionTimer(WantTiming);
2009 CompletionTimer.setOutput("Code completion @ " + File + ":" +
2010 llvm::Twine(Line) + ":" + llvm::Twine(Column));
2012 llvm::IntrusiveRefCntPtr<CompilerInvocation>
2013 CCInvocation(new CompilerInvocation(*Invocation));
2015 FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2016 PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2018 FrontendOpts.ShowMacrosInCodeCompletion
2019 = IncludeMacros && CachedCompletionResults.empty();
2020 FrontendOpts.ShowCodePatternsInCodeCompletion = IncludeCodePatterns;
2021 FrontendOpts.ShowGlobalSymbolsInCodeCompletion
2022 = CachedCompletionResults.empty();
2023 FrontendOpts.CodeCompletionAt.FileName = File;
2024 FrontendOpts.CodeCompletionAt.Line = Line;
2025 FrontendOpts.CodeCompletionAt.Column = Column;
2027 // Set the language options appropriately.
2028 LangOpts = CCInvocation->getLangOpts();
2030 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
2032 // Recover resources if we crash before exiting this method.
2033 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2034 CICleanup(Clang.get());
2036 Clang->setInvocation(&*CCInvocation);
2037 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
2039 // Set up diagnostics, capturing any diagnostics produced.
2040 Clang->setDiagnostics(&Diag);
2041 ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts());
2042 CaptureDroppedDiagnostics Capture(true,
2043 Clang->getDiagnostics(),
2046 // Create the target instance.
2047 Clang->getTargetOpts().Features = TargetFeatures;
2048 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
2049 Clang->getTargetOpts()));
2050 if (!Clang->hasTarget()) {
2051 Clang->setInvocation(0);
2055 // Inform the target of the language options.
2057 // FIXME: We shouldn't need to do this, the target should be immutable once
2058 // created. This complexity should be lifted elsewhere.
2059 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
2061 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2062 "Invocation must have exactly one source file!");
2063 assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
2064 "FIXME: AST inputs not yet supported here!");
2065 assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
2066 "IR inputs not support here!");
2069 // Use the source and file managers that we were given.
2070 Clang->setFileManager(&FileMgr);
2071 Clang->setSourceManager(&SourceMgr);
2074 PreprocessorOpts.clearRemappedFiles();
2075 PreprocessorOpts.RetainRemappedFileBuffers = true;
2076 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
2077 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
2078 if (const llvm::MemoryBuffer *
2079 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
2080 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, memBuf);
2081 OwnedBuffers.push_back(memBuf);
2083 const char *fname = fileOrBuf.get<const char *>();
2084 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, fname);
2088 // Use the code completion consumer we were given, but adding any cached
2089 // code-completion results.
2090 AugmentedCodeCompleteConsumer *AugmentedConsumer
2091 = new AugmentedCodeCompleteConsumer(*this, Consumer,
2092 FrontendOpts.ShowMacrosInCodeCompletion,
2093 FrontendOpts.ShowCodePatternsInCodeCompletion,
2094 FrontendOpts.ShowGlobalSymbolsInCodeCompletion);
2095 Clang->setCodeCompletionConsumer(AugmentedConsumer);
2097 // If we have a precompiled preamble, try to use it. We only allow
2098 // the use of the precompiled preamble if we're if the completion
2099 // point is within the main file, after the end of the precompiled
2101 llvm::MemoryBuffer *OverrideMainBuffer = 0;
2102 if (!PreambleFile.empty()) {
2103 using llvm::sys::FileStatus;
2104 llvm::sys::PathWithStatus CompleteFilePath(File);
2105 llvm::sys::PathWithStatus MainPath(OriginalSourceFile);
2106 if (const FileStatus *CompleteFileStatus = CompleteFilePath.getFileStatus())
2107 if (const FileStatus *MainStatus = MainPath.getFileStatus())
2108 if (CompleteFileStatus->getUniqueID() == MainStatus->getUniqueID())
2110 = getMainBufferWithPrecompiledPreamble(*CCInvocation, false,
2114 // If the main file has been overridden due to the use of a preamble,
2115 // make that override happen and introduce the preamble.
2116 PreprocessorOpts.DisableStatCache = true;
2117 StoredDiagnostics.insert(StoredDiagnostics.end(),
2118 this->StoredDiagnostics.begin(),
2119 this->StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver);
2120 if (OverrideMainBuffer) {
2121 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
2122 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
2123 PreprocessorOpts.PrecompiledPreambleBytes.second
2124 = PreambleEndsAtStartOfLine;
2125 PreprocessorOpts.ImplicitPCHInclude = PreambleFile;
2126 PreprocessorOpts.DisablePCHValidation = true;
2128 // The stored diagnostics have the old source manager. Copy them
2129 // to our output set of stored diagnostics, updating the source
2130 // manager to the one we were given.
2131 for (unsigned I = NumStoredDiagnosticsFromDriver,
2132 N = this->StoredDiagnostics.size();
2134 StoredDiagnostics.push_back(this->StoredDiagnostics[I]);
2135 FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), SourceMgr);
2136 StoredDiagnostics[I].setLocation(Loc);
2139 OwnedBuffers.push_back(OverrideMainBuffer);
2141 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2142 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2145 llvm::OwningPtr<SyntaxOnlyAction> Act;
2146 Act.reset(new SyntaxOnlyAction);
2147 if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
2148 Clang->getFrontendOpts().Inputs[0].first)) {
2150 Act->EndSourceFile();
2154 bool ASTUnit::Save(llvm::StringRef File) {
2155 if (getDiagnostics().hasErrorOccurred())
2158 // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2159 // unconditionally create a stat cache when we parse the file?
2160 std::string ErrorInfo;
2161 llvm::raw_fd_ostream Out(File.str().c_str(), ErrorInfo,
2162 llvm::raw_fd_ostream::F_Binary);
2163 if (!ErrorInfo.empty() || Out.has_error())
2168 return Out.has_error();
2171 bool ASTUnit::serialize(llvm::raw_ostream &OS) {
2172 if (getDiagnostics().hasErrorOccurred())
2175 std::vector<unsigned char> Buffer;
2176 llvm::BitstreamWriter Stream(Buffer);
2177 ASTWriter Writer(Stream);
2178 Writer.WriteAST(getSema(), 0, std::string(), 0);
2180 // Write the generated bitstream to "Out".
2181 if (!Buffer.empty())
2182 OS.write((char *)&Buffer.front(), Buffer.size());