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/Tool.h"
24 #include "clang/Frontend/CompilerInstance.h"
25 #include "clang/Frontend/FrontendActions.h"
26 #include "clang/Frontend/FrontendDiagnostic.h"
27 #include "clang/Frontend/FrontendOptions.h"
28 #include "clang/Frontend/Utils.h"
29 #include "clang/Serialization/ASTReader.h"
30 #include "clang/Serialization/ASTSerializationListener.h"
31 #include "clang/Serialization/ASTWriter.h"
32 #include "clang/Lex/HeaderSearch.h"
33 #include "clang/Lex/Preprocessor.h"
34 #include "clang/Basic/TargetOptions.h"
35 #include "clang/Basic/TargetInfo.h"
36 #include "clang/Basic/Diagnostic.h"
37 #include "llvm/ADT/StringExtras.h"
38 #include "llvm/ADT/StringSet.h"
39 #include "llvm/Support/Atomic.h"
40 #include "llvm/Support/MemoryBuffer.h"
41 #include "llvm/Support/Host.h"
42 #include "llvm/Support/Path.h"
43 #include "llvm/Support/raw_ostream.h"
44 #include "llvm/Support/Timer.h"
48 using namespace clang;
50 using llvm::TimeRecord;
59 explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
61 Start = TimeRecord::getCurrentTime();
64 void setOutput(const llvm::Twine &Output) {
66 this->Output = Output.str();
71 TimeRecord Elapsed = TimeRecord::getCurrentTime();
73 llvm::errs() << Output << ':';
74 Elapsed.print(Elapsed, llvm::errs());
81 /// \brief After failing to build a precompiled preamble (due to
82 /// errors in the source that occurs in the preamble), the number of
83 /// reparses during which we'll skip even trying to precompile the
85 const unsigned DefaultPreambleRebuildInterval = 5;
87 /// \brief Tracks the number of ASTUnit objects that are currently active.
89 /// Used for debugging purposes only.
90 static llvm::sys::cas_flag ActiveASTUnitObjects;
92 ASTUnit::ASTUnit(bool _MainFileIsAST)
93 : CaptureDiagnostics(false), MainFileIsAST(_MainFileIsAST),
94 CompleteTranslationUnit(true), WantTiming(getenv("LIBCLANG_TIMING")),
95 NumStoredDiagnosticsFromDriver(0),
96 ConcurrencyCheckValue(CheckUnlocked),
97 PreambleRebuildCounter(0), SavedMainFileBuffer(0), PreambleBuffer(0),
98 ShouldCacheCodeCompletionResults(false),
99 CompletionCacheTopLevelHashValue(0),
100 PreambleTopLevelHashValue(0),
101 CurrentTopLevelHashValue(0),
102 UnsafeToFree(false) {
103 if (getenv("LIBCLANG_OBJTRACKING")) {
104 llvm::sys::AtomicIncrement(&ActiveASTUnitObjects);
105 fprintf(stderr, "+++ %d translation units\n", ActiveASTUnitObjects);
109 ASTUnit::~ASTUnit() {
110 ConcurrencyCheckValue = CheckLocked;
111 CleanTemporaryFiles();
112 if (!PreambleFile.empty())
113 llvm::sys::Path(PreambleFile).eraseFromDisk();
115 // Free the buffers associated with remapped files. We are required to
116 // perform this operation here because we explicitly request that the
117 // compiler instance *not* free these buffers for each invocation of the
119 if (Invocation.get()) {
120 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
121 for (PreprocessorOptions::remapped_file_buffer_iterator
122 FB = PPOpts.remapped_file_buffer_begin(),
123 FBEnd = PPOpts.remapped_file_buffer_end();
129 delete SavedMainFileBuffer;
130 delete PreambleBuffer;
132 ClearCachedCompletionResults();
134 if (getenv("LIBCLANG_OBJTRACKING")) {
135 llvm::sys::AtomicDecrement(&ActiveASTUnitObjects);
136 fprintf(stderr, "--- %d translation units\n", ActiveASTUnitObjects);
140 void ASTUnit::CleanTemporaryFiles() {
141 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
142 TemporaryFiles[I].eraseFromDisk();
143 TemporaryFiles.clear();
146 /// \brief Determine the set of code-completion contexts in which this
147 /// declaration should be shown.
148 static unsigned getDeclShowContexts(NamedDecl *ND,
149 const LangOptions &LangOpts,
150 bool &IsNestedNameSpecifier) {
151 IsNestedNameSpecifier = false;
153 if (isa<UsingShadowDecl>(ND))
154 ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl());
158 unsigned Contexts = 0;
159 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) ||
160 isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) {
161 // Types can appear in these contexts.
162 if (LangOpts.CPlusPlus || !isa<TagDecl>(ND))
163 Contexts |= (1 << (CodeCompletionContext::CCC_TopLevel - 1))
164 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
165 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
166 | (1 << (CodeCompletionContext::CCC_Statement - 1))
167 | (1 << (CodeCompletionContext::CCC_Type - 1))
168 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1));
170 // In C++, types can appear in expressions contexts (for functional casts).
171 if (LangOpts.CPlusPlus)
172 Contexts |= (1 << (CodeCompletionContext::CCC_Expression - 1));
174 // In Objective-C, message sends can send interfaces. In Objective-C++,
175 // all types are available due to functional casts.
176 if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND))
177 Contexts |= (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1));
179 // Deal with tag names.
180 if (isa<EnumDecl>(ND)) {
181 Contexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1));
183 // Part of the nested-name-specifier in C++0x.
184 if (LangOpts.CPlusPlus0x)
185 IsNestedNameSpecifier = true;
186 } else if (RecordDecl *Record = dyn_cast<RecordDecl>(ND)) {
187 if (Record->isUnion())
188 Contexts |= (1 << (CodeCompletionContext::CCC_UnionTag - 1));
190 Contexts |= (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1));
192 if (LangOpts.CPlusPlus)
193 IsNestedNameSpecifier = true;
194 } else if (isa<ClassTemplateDecl>(ND))
195 IsNestedNameSpecifier = true;
196 } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) {
197 // Values can appear in these contexts.
198 Contexts = (1 << (CodeCompletionContext::CCC_Statement - 1))
199 | (1 << (CodeCompletionContext::CCC_Expression - 1))
200 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
201 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1));
202 } else if (isa<ObjCProtocolDecl>(ND)) {
203 Contexts = (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1));
204 } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
205 Contexts = (1 << (CodeCompletionContext::CCC_Namespace - 1));
207 // Part of the nested-name-specifier.
208 IsNestedNameSpecifier = true;
214 void ASTUnit::CacheCodeCompletionResults() {
218 SimpleTimer Timer(WantTiming);
219 Timer.setOutput("Cache global code completions for " + getMainFileName());
221 // Clear out the previous results.
222 ClearCachedCompletionResults();
224 // Gather the set of global code completions.
225 typedef CodeCompletionResult Result;
226 llvm::SmallVector<Result, 8> Results;
227 CachedCompletionAllocator = new GlobalCodeCompletionAllocator;
228 TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator, Results);
230 // Translate global code completions into cached completions.
231 llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
233 for (unsigned I = 0, N = Results.size(); I != N; ++I) {
234 switch (Results[I].Kind) {
235 case Result::RK_Declaration: {
236 bool IsNestedNameSpecifier = false;
237 CachedCodeCompletionResult CachedResult;
238 CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema,
239 *CachedCompletionAllocator);
240 CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration,
241 Ctx->getLangOptions(),
242 IsNestedNameSpecifier);
243 CachedResult.Priority = Results[I].Priority;
244 CachedResult.Kind = Results[I].CursorKind;
245 CachedResult.Availability = Results[I].Availability;
247 // Keep track of the type of this completion in an ASTContext-agnostic
249 QualType UsageType = getDeclUsageType(*Ctx, Results[I].Declaration);
250 if (UsageType.isNull()) {
251 CachedResult.TypeClass = STC_Void;
252 CachedResult.Type = 0;
254 CanQualType CanUsageType
255 = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
256 CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
258 // Determine whether we have already seen this type. If so, we save
259 // ourselves the work of formatting the type string by using the
260 // temporary, CanQualType-based hash table to find the associated value.
261 unsigned &TypeValue = CompletionTypes[CanUsageType];
262 if (TypeValue == 0) {
263 TypeValue = CompletionTypes.size();
264 CachedCompletionTypes[QualType(CanUsageType).getAsString()]
268 CachedResult.Type = TypeValue;
271 CachedCompletionResults.push_back(CachedResult);
273 /// Handle nested-name-specifiers in C++.
274 if (TheSema->Context.getLangOptions().CPlusPlus &&
275 IsNestedNameSpecifier && !Results[I].StartsNestedNameSpecifier) {
276 // The contexts in which a nested-name-specifier can appear in C++.
278 = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
279 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
280 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
281 | (1 << (CodeCompletionContext::CCC_Statement - 1))
282 | (1 << (CodeCompletionContext::CCC_Expression - 1))
283 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
284 | (1 << (CodeCompletionContext::CCC_EnumTag - 1))
285 | (1 << (CodeCompletionContext::CCC_UnionTag - 1))
286 | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1))
287 | (1 << (CodeCompletionContext::CCC_Type - 1))
288 | (1 << (CodeCompletionContext::CCC_PotentiallyQualifiedName - 1))
289 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1));
291 if (isa<NamespaceDecl>(Results[I].Declaration) ||
292 isa<NamespaceAliasDecl>(Results[I].Declaration))
293 NNSContexts |= (1 << (CodeCompletionContext::CCC_Namespace - 1));
295 if (unsigned RemainingContexts
296 = NNSContexts & ~CachedResult.ShowInContexts) {
297 // If there any contexts where this completion can be a
298 // nested-name-specifier but isn't already an option, create a
299 // nested-name-specifier completion.
300 Results[I].StartsNestedNameSpecifier = true;
301 CachedResult.Completion
302 = Results[I].CreateCodeCompletionString(*TheSema,
303 *CachedCompletionAllocator);
304 CachedResult.ShowInContexts = RemainingContexts;
305 CachedResult.Priority = CCP_NestedNameSpecifier;
306 CachedResult.TypeClass = STC_Void;
307 CachedResult.Type = 0;
308 CachedCompletionResults.push_back(CachedResult);
314 case Result::RK_Keyword:
315 case Result::RK_Pattern:
316 // Ignore keywords and patterns; we don't care, since they are so
317 // easily regenerated.
320 case Result::RK_Macro: {
321 CachedCodeCompletionResult CachedResult;
322 CachedResult.Completion
323 = Results[I].CreateCodeCompletionString(*TheSema,
324 *CachedCompletionAllocator);
325 CachedResult.ShowInContexts
326 = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
327 | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1))
328 | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1))
329 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
330 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
331 | (1 << (CodeCompletionContext::CCC_Statement - 1))
332 | (1 << (CodeCompletionContext::CCC_Expression - 1))
333 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
334 | (1 << (CodeCompletionContext::CCC_MacroNameUse - 1))
335 | (1 << (CodeCompletionContext::CCC_PreprocessorExpression - 1))
336 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
337 | (1 << (CodeCompletionContext::CCC_OtherWithMacros - 1));
339 CachedResult.Priority = Results[I].Priority;
340 CachedResult.Kind = Results[I].CursorKind;
341 CachedResult.Availability = Results[I].Availability;
342 CachedResult.TypeClass = STC_Void;
343 CachedResult.Type = 0;
344 CachedCompletionResults.push_back(CachedResult);
350 // Save the current top-level hash value.
351 CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
354 void ASTUnit::ClearCachedCompletionResults() {
355 CachedCompletionResults.clear();
356 CachedCompletionTypes.clear();
357 CachedCompletionAllocator = 0;
362 /// \brief Gathers information from ASTReader that will be used to initialize
364 class ASTInfoCollector : public ASTReaderListener {
365 LangOptions &LangOpt;
367 std::string &TargetTriple;
368 std::string &Predefines;
371 unsigned NumHeaderInfos;
374 ASTInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
375 std::string &TargetTriple, std::string &Predefines,
377 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
378 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
380 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
385 virtual bool ReadTargetTriple(llvm::StringRef Triple) {
386 TargetTriple = Triple;
390 virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
391 llvm::StringRef OriginalFileName,
392 std::string &SuggestedPredefines,
393 FileManager &FileMgr) {
394 Predefines = Buffers[0].Data;
395 for (unsigned I = 1, N = Buffers.size(); I != N; ++I) {
396 Predefines += Buffers[I].Data;
401 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {
402 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
405 virtual void ReadCounter(unsigned Value) {
410 class StoredDiagnosticClient : public DiagnosticClient {
411 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags;
414 explicit StoredDiagnosticClient(
415 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
416 : StoredDiags(StoredDiags) { }
418 virtual void HandleDiagnostic(Diagnostic::Level Level,
419 const DiagnosticInfo &Info);
422 /// \brief RAII object that optionally captures diagnostics, if
423 /// there is no diagnostic client to capture them already.
424 class CaptureDroppedDiagnostics {
426 StoredDiagnosticClient Client;
427 DiagnosticClient *PreviousClient;
430 CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags,
431 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
432 : Diags(Diags), Client(StoredDiags), PreviousClient(0)
434 if (RequestCapture || Diags.getClient() == 0) {
435 PreviousClient = Diags.takeClient();
436 Diags.setClient(&Client);
440 ~CaptureDroppedDiagnostics() {
441 if (Diags.getClient() == &Client) {
443 Diags.setClient(PreviousClient);
448 } // anonymous namespace
450 void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level,
451 const DiagnosticInfo &Info) {
452 // Default implementation (Warnings/errors count).
453 DiagnosticClient::HandleDiagnostic(Level, Info);
455 StoredDiags.push_back(StoredDiagnostic(Level, Info));
458 const std::string &ASTUnit::getOriginalSourceFileName() {
459 return OriginalSourceFile;
462 const std::string &ASTUnit::getASTFileName() {
463 assert(isMainFileAST() && "Not an ASTUnit from an AST file!");
464 return static_cast<ASTReader *>(Ctx->getExternalSource())->getFileName();
467 llvm::MemoryBuffer *ASTUnit::getBufferForFile(llvm::StringRef Filename,
468 std::string *ErrorStr) {
470 return FileMgr->getBufferForFile(Filename, ErrorStr);
473 /// \brief Configure the diagnostics object for use with ASTUnit.
474 void ASTUnit::ConfigureDiags(llvm::IntrusiveRefCntPtr<Diagnostic> &Diags,
475 const char **ArgBegin, const char **ArgEnd,
476 ASTUnit &AST, bool CaptureDiagnostics) {
477 if (!Diags.getPtr()) {
478 // No diagnostics engine was provided, so create our own diagnostics object
479 // with the default options.
480 DiagnosticOptions DiagOpts;
481 DiagnosticClient *Client = 0;
482 if (CaptureDiagnostics)
483 Client = new StoredDiagnosticClient(AST.StoredDiagnostics);
484 Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd- ArgBegin,
486 } else if (CaptureDiagnostics) {
487 Diags->setClient(new StoredDiagnosticClient(AST.StoredDiagnostics));
491 ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename,
492 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
493 const FileSystemOptions &FileSystemOpts,
495 RemappedFile *RemappedFiles,
496 unsigned NumRemappedFiles,
497 bool CaptureDiagnostics) {
498 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
499 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
501 AST->OnlyLocalDecls = OnlyLocalDecls;
502 AST->CaptureDiagnostics = CaptureDiagnostics;
503 AST->Diagnostics = Diags;
504 AST->FileMgr.reset(new FileManager(FileSystemOpts));
505 AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics(),
506 AST->getFileManager()));
507 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
509 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
510 // Create the file entry for the file that we're mapping from.
511 const FileEntry *FromFile
512 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
513 RemappedFiles[I].second->getBufferSize(),
516 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
517 << RemappedFiles[I].first;
518 delete RemappedFiles[I].second;
522 // Override the contents of the "from" file with the contents of
524 AST->getSourceManager().overrideFileContents(FromFile,
525 RemappedFiles[I].second);
528 // Gather Info for preprocessor construction later on.
530 LangOptions LangInfo;
531 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
532 std::string TargetTriple;
533 std::string Predefines;
536 llvm::OwningPtr<ASTReader> Reader;
538 Reader.reset(new ASTReader(AST->getSourceManager(), AST->getFileManager(),
539 AST->getDiagnostics()));
540 Reader->setListener(new ASTInfoCollector(LangInfo, HeaderInfo, TargetTriple,
541 Predefines, Counter));
543 switch (Reader->ReadAST(Filename, ASTReader::MainFile)) {
544 case ASTReader::Success:
547 case ASTReader::Failure:
548 case ASTReader::IgnorePCH:
549 AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
553 AST->OriginalSourceFile = Reader->getOriginalSourceFile();
555 // AST file loaded successfully. Now create the preprocessor.
557 // Get information about the target being compiled for.
559 // FIXME: This is broken, we should store the TargetOptions in the AST file.
560 TargetOptions TargetOpts;
562 TargetOpts.CXXABI = "";
564 TargetOpts.Features.clear();
565 TargetOpts.Triple = TargetTriple;
566 AST->Target.reset(TargetInfo::CreateTargetInfo(AST->getDiagnostics(),
568 AST->PP.reset(new Preprocessor(AST->getDiagnostics(), LangInfo,
570 AST->getSourceManager(), HeaderInfo));
571 Preprocessor &PP = *AST->PP.get();
573 PP.setPredefines(Reader->getSuggestedPredefines());
574 PP.setCounterValue(Counter);
575 Reader->setPreprocessor(PP);
577 // Create and initialize the ASTContext.
579 AST->Ctx.reset(new ASTContext(LangInfo,
580 AST->getSourceManager(),
582 PP.getIdentifierTable(),
583 PP.getSelectorTable(),
585 /* size_reserve = */0));
586 ASTContext &Context = *AST->Ctx.get();
588 Reader->InitializeContext(Context);
590 // Attach the AST reader to the AST context as an external AST
591 // source, so that declarations will be deserialized from the
592 // AST file as needed.
593 ASTReader *ReaderPtr = Reader.get();
594 llvm::OwningPtr<ExternalASTSource> Source(Reader.take());
595 Context.setExternalSource(Source);
597 // Create an AST consumer, even though it isn't used.
598 AST->Consumer.reset(new ASTConsumer);
600 // Create a semantic analysis object and tell the AST reader about it.
601 AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer));
602 AST->TheSema->Initialize();
603 ReaderPtr->InitializeSema(*AST->TheSema);
610 /// \brief Preprocessor callback class that updates a hash value with the names
611 /// of all macros that have been defined by the translation unit.
612 class MacroDefinitionTrackerPPCallbacks : public PPCallbacks {
616 explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { }
618 virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
619 Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash);
623 /// \brief Add the given declaration to the hash of all top-level entities.
624 void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) {
628 DeclContext *DC = D->getDeclContext();
632 if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit()))
635 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
636 if (ND->getIdentifier())
637 Hash = llvm::HashString(ND->getIdentifier()->getName(), Hash);
638 else if (DeclarationName Name = ND->getDeclName()) {
639 std::string NameStr = Name.getAsString();
640 Hash = llvm::HashString(NameStr, Hash);
645 if (ObjCForwardProtocolDecl *Forward
646 = dyn_cast<ObjCForwardProtocolDecl>(D)) {
647 for (ObjCForwardProtocolDecl::protocol_iterator
648 P = Forward->protocol_begin(),
649 PEnd = Forward->protocol_end();
651 AddTopLevelDeclarationToHash(*P, Hash);
655 if (ObjCClassDecl *Class = llvm::dyn_cast<ObjCClassDecl>(D)) {
656 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
658 AddTopLevelDeclarationToHash(I->getInterface(), Hash);
663 class TopLevelDeclTrackerConsumer : public ASTConsumer {
668 TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
669 : Unit(_Unit), Hash(Hash) {
673 void HandleTopLevelDecl(DeclGroupRef D) {
674 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
676 // FIXME: Currently ObjC method declarations are incorrectly being
677 // reported as top-level declarations, even though their DeclContext
678 // is the containing ObjC @interface/@implementation. This is a
679 // fundamental problem in the parser right now.
680 if (isa<ObjCMethodDecl>(D))
683 AddTopLevelDeclarationToHash(D, Hash);
684 Unit.addTopLevelDecl(D);
688 // We're not interested in "interesting" decls.
689 void HandleInterestingDecl(DeclGroupRef) {}
692 class TopLevelDeclTrackerAction : public ASTFrontendAction {
696 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
697 llvm::StringRef InFile) {
698 CI.getPreprocessor().addPPCallbacks(
699 new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
700 return new TopLevelDeclTrackerConsumer(Unit,
701 Unit.getCurrentTopLevelHashValue());
705 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
707 virtual bool hasCodeCompletionSupport() const { return false; }
708 virtual bool usesCompleteTranslationUnit() {
709 return Unit.isCompleteTranslationUnit();
713 class PrecompilePreambleConsumer : public PCHGenerator,
714 public ASTSerializationListener {
717 std::vector<Decl *> TopLevelDecls;
720 PrecompilePreambleConsumer(ASTUnit &Unit,
721 const Preprocessor &PP, bool Chaining,
722 const char *isysroot, llvm::raw_ostream *Out)
723 : PCHGenerator(PP, "", Chaining, isysroot, Out), Unit(Unit),
724 Hash(Unit.getCurrentTopLevelHashValue()) {
728 virtual void HandleTopLevelDecl(DeclGroupRef D) {
729 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
731 // FIXME: Currently ObjC method declarations are incorrectly being
732 // reported as top-level declarations, even though their DeclContext
733 // is the containing ObjC @interface/@implementation. This is a
734 // fundamental problem in the parser right now.
735 if (isa<ObjCMethodDecl>(D))
737 AddTopLevelDeclarationToHash(D, Hash);
738 TopLevelDecls.push_back(D);
742 virtual void HandleTranslationUnit(ASTContext &Ctx) {
743 PCHGenerator::HandleTranslationUnit(Ctx);
744 if (!Unit.getDiagnostics().hasErrorOccurred()) {
745 // Translate the top-level declarations we captured during
746 // parsing into declaration IDs in the precompiled
747 // preamble. This will allow us to deserialize those top-level
748 // declarations when requested.
749 for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I)
750 Unit.addTopLevelDeclFromPreamble(
751 getWriter().getDeclID(TopLevelDecls[I]));
755 virtual void SerializedPreprocessedEntity(PreprocessedEntity *Entity,
757 Unit.addPreprocessedEntityFromPreamble(Offset);
760 virtual ASTSerializationListener *GetASTSerializationListener() {
765 class PrecompilePreambleAction : public ASTFrontendAction {
769 explicit PrecompilePreambleAction(ASTUnit &Unit) : Unit(Unit) {}
771 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
772 llvm::StringRef InFile) {
774 std::string OutputFile;
775 llvm::raw_ostream *OS = 0;
777 if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot,
782 const char *isysroot = CI.getFrontendOpts().RelocatablePCH ?
784 CI.getPreprocessor().addPPCallbacks(
785 new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
786 return new PrecompilePreambleConsumer(Unit, CI.getPreprocessor(), Chaining,
790 virtual bool hasCodeCompletionSupport() const { return false; }
791 virtual bool hasASTFileSupport() const { return false; }
792 virtual bool usesCompleteTranslationUnit() { return false; }
797 /// Parse the source file into a translation unit using the given compiler
798 /// invocation, replacing the current translation unit.
800 /// \returns True if a failure occurred that causes the ASTUnit not to
801 /// contain any translation-unit information, false otherwise.
802 bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) {
803 delete SavedMainFileBuffer;
804 SavedMainFileBuffer = 0;
806 if (!Invocation.get()) {
807 delete OverrideMainBuffer;
811 // Create the compiler instance to use for building the AST.
812 CompilerInstance Clang;
813 Clang.setInvocation(Invocation.take());
814 OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
816 // Set up diagnostics, capturing any diagnostics that would
817 // otherwise be dropped.
818 Clang.setDiagnostics(&getDiagnostics());
820 // Create the target instance.
821 Clang.getTargetOpts().Features = TargetFeatures;
822 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
823 Clang.getTargetOpts()));
824 if (!Clang.hasTarget()) {
825 delete OverrideMainBuffer;
829 // Inform the target of the language options.
831 // FIXME: We shouldn't need to do this, the target should be immutable once
832 // created. This complexity should be lifted elsewhere.
833 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts());
835 assert(Clang.getFrontendOpts().Inputs.size() == 1 &&
836 "Invocation must have exactly one source file!");
837 assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST &&
838 "FIXME: AST inputs not yet supported here!");
839 assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
840 "IR inputs not support here!");
842 // Configure the various subsystems.
843 // FIXME: Should we retain the previous file manager?
844 FileSystemOpts = Clang.getFileSystemOpts();
845 FileMgr.reset(new FileManager(Clang.getFileSystemOpts()));
846 SourceMgr.reset(new SourceManager(getDiagnostics(), *FileMgr));
851 // Clear out old caches and data.
852 TopLevelDecls.clear();
853 PreprocessedEntities.clear();
854 CleanTemporaryFiles();
855 PreprocessedEntitiesByFile.clear();
857 if (!OverrideMainBuffer) {
858 StoredDiagnostics.erase(
859 StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver,
860 StoredDiagnostics.end());
861 TopLevelDeclsInPreamble.clear();
862 PreprocessedEntitiesInPreamble.clear();
865 // Create a file manager object to provide access to and cache the filesystem.
866 Clang.setFileManager(&getFileManager());
868 // Create the source manager.
869 Clang.setSourceManager(&getSourceManager());
871 // If the main file has been overridden due to the use of a preamble,
872 // make that override happen and introduce the preamble.
873 PreprocessorOptions &PreprocessorOpts = Clang.getPreprocessorOpts();
874 std::string PriorImplicitPCHInclude;
875 if (OverrideMainBuffer) {
876 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
877 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
878 PreprocessorOpts.PrecompiledPreambleBytes.second
879 = PreambleEndsAtStartOfLine;
880 PriorImplicitPCHInclude = PreprocessorOpts.ImplicitPCHInclude;
881 PreprocessorOpts.ImplicitPCHInclude = PreambleFile;
882 PreprocessorOpts.DisablePCHValidation = true;
884 // The stored diagnostic has the old source manager in it; update
885 // the locations to refer into the new source manager. Since we've
886 // been careful to make sure that the source manager's state
887 // before and after are identical, so that we can reuse the source
889 for (unsigned I = NumStoredDiagnosticsFromDriver,
890 N = StoredDiagnostics.size();
892 FullSourceLoc Loc(StoredDiagnostics[I].getLocation(),
894 StoredDiagnostics[I].setLocation(Loc);
897 // Keep track of the override buffer;
898 SavedMainFileBuffer = OverrideMainBuffer;
900 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
901 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
904 llvm::OwningPtr<TopLevelDeclTrackerAction> Act;
905 Act.reset(new TopLevelDeclTrackerAction(*this));
906 if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
907 Clang.getFrontendOpts().Inputs[0].first))
912 // Steal the created target, context, and preprocessor, and take back the
913 // source and file managers.
914 TheSema.reset(Clang.takeSema());
915 Consumer.reset(Clang.takeASTConsumer());
916 Ctx.reset(Clang.takeASTContext());
917 PP.reset(Clang.takePreprocessor());
918 Clang.takeSourceManager();
919 Clang.takeFileManager();
920 Target.reset(Clang.takeTarget());
922 Act->EndSourceFile();
924 // Remove the overridden buffer we used for the preamble.
925 if (OverrideMainBuffer) {
926 PreprocessorOpts.eraseRemappedFile(
927 PreprocessorOpts.remapped_file_buffer_end() - 1);
928 PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude;
931 Invocation.reset(Clang.takeInvocation());
935 // Remove the overridden buffer we used for the preamble.
936 if (OverrideMainBuffer) {
937 PreprocessorOpts.eraseRemappedFile(
938 PreprocessorOpts.remapped_file_buffer_end() - 1);
939 PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude;
940 delete OverrideMainBuffer;
941 SavedMainFileBuffer = 0;
944 StoredDiagnostics.clear();
945 Clang.takeSourceManager();
946 Clang.takeFileManager();
947 Invocation.reset(Clang.takeInvocation());
951 /// \brief Simple function to retrieve a path for a preamble precompiled header.
952 static std::string GetPreamblePCHPath() {
953 // FIXME: This is lame; sys::Path should provide this function (in particular,
954 // it should know how to find the temporary files dir).
955 // FIXME: This is really lame. I copied this code from the Driver!
956 // FIXME: This is a hack so that we can override the preamble file during
957 // crash-recovery testing, which is the only case where the preamble files
958 // are not necessarily cleaned up.
959 const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
964 const char *TmpDir = ::getenv("TMPDIR");
966 TmpDir = ::getenv("TEMP");
968 TmpDir = ::getenv("TMP");
971 TmpDir = ::getenv("USERPROFILE");
975 llvm::sys::Path P(TmpDir);
976 P.createDirectoryOnDisk(true);
977 P.appendComponent("preamble");
978 P.appendSuffix("pch");
979 if (P.createTemporaryFileOnDisk())
980 return std::string();
985 /// \brief Compute the preamble for the main file, providing the source buffer
986 /// that corresponds to the main file along with a pair (bytes, start-of-line)
987 /// that describes the preamble.
988 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
989 ASTUnit::ComputePreamble(CompilerInvocation &Invocation,
990 unsigned MaxLines, bool &CreatedBuffer) {
991 FrontendOptions &FrontendOpts = Invocation.getFrontendOpts();
992 PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts();
993 CreatedBuffer = false;
995 // Try to determine if the main file has been remapped, either from the
996 // command line (to another file) or directly through the compiler invocation
997 // (to a memory buffer).
998 llvm::MemoryBuffer *Buffer = 0;
999 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second);
1000 if (const llvm::sys::FileStatus *MainFileStatus = MainFilePath.getFileStatus()) {
1001 // Check whether there is a file-file remapping of the main file
1002 for (PreprocessorOptions::remapped_file_iterator
1003 M = PreprocessorOpts.remapped_file_begin(),
1004 E = PreprocessorOpts.remapped_file_end();
1007 llvm::sys::PathWithStatus MPath(M->first);
1008 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1009 if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1010 // We found a remapping. Try to load the resulting, remapped source.
1011 if (CreatedBuffer) {
1013 CreatedBuffer = false;
1016 Buffer = getBufferForFile(M->second);
1018 return std::make_pair((llvm::MemoryBuffer*)0,
1019 std::make_pair(0, true));
1020 CreatedBuffer = true;
1025 // Check whether there is a file-buffer remapping. It supercedes the
1026 // file-file remapping.
1027 for (PreprocessorOptions::remapped_file_buffer_iterator
1028 M = PreprocessorOpts.remapped_file_buffer_begin(),
1029 E = PreprocessorOpts.remapped_file_buffer_end();
1032 llvm::sys::PathWithStatus MPath(M->first);
1033 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1034 if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1035 // We found a remapping.
1036 if (CreatedBuffer) {
1038 CreatedBuffer = false;
1041 Buffer = const_cast<llvm::MemoryBuffer *>(M->second);
1047 // If the main source file was not remapped, load it now.
1049 Buffer = getBufferForFile(FrontendOpts.Inputs[0].second);
1051 return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true));
1053 CreatedBuffer = true;
1056 return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer, MaxLines));
1059 static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old,
1061 llvm::StringRef NewName) {
1062 llvm::MemoryBuffer *Result
1063 = llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName);
1064 memcpy(const_cast<char*>(Result->getBufferStart()),
1065 Old->getBufferStart(), Old->getBufferSize());
1066 memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(),
1067 ' ', NewSize - Old->getBufferSize() - 1);
1068 const_cast<char*>(Result->getBufferEnd())[-1] = '\n';
1073 /// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing
1074 /// the source file.
1076 /// This routine will compute the preamble of the main source file. If a
1077 /// non-trivial preamble is found, it will precompile that preamble into a
1078 /// precompiled header so that the precompiled preamble can be used to reduce
1079 /// reparsing time. If a precompiled preamble has already been constructed,
1080 /// this routine will determine if it is still valid and, if so, avoid
1081 /// rebuilding the precompiled preamble.
1083 /// \param AllowRebuild When true (the default), this routine is
1084 /// allowed to rebuild the precompiled preamble if it is found to be
1087 /// \param MaxLines When non-zero, the maximum number of lines that
1088 /// can occur within the preamble.
1090 /// \returns If the precompiled preamble can be used, returns a newly-allocated
1091 /// buffer that should be used in place of the main file when doing so.
1092 /// Otherwise, returns a NULL pointer.
1093 llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
1094 CompilerInvocation PreambleInvocation,
1096 unsigned MaxLines) {
1097 FrontendOptions &FrontendOpts = PreambleInvocation.getFrontendOpts();
1098 PreprocessorOptions &PreprocessorOpts
1099 = PreambleInvocation.getPreprocessorOpts();
1101 bool CreatedPreambleBuffer = false;
1102 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble
1103 = ComputePreamble(PreambleInvocation, MaxLines, CreatedPreambleBuffer);
1105 // If ComputePreamble() Take ownership of the
1106 llvm::OwningPtr<llvm::MemoryBuffer> OwnedPreambleBuffer;
1107 if (CreatedPreambleBuffer)
1108 OwnedPreambleBuffer.reset(NewPreamble.first);
1110 if (!NewPreamble.second.first) {
1111 // We couldn't find a preamble in the main source. Clear out the current
1112 // preamble, if we have one. It's obviously no good any more.
1114 if (!PreambleFile.empty()) {
1115 llvm::sys::Path(PreambleFile).eraseFromDisk();
1116 PreambleFile.clear();
1119 // The next time we actually see a preamble, precompile it.
1120 PreambleRebuildCounter = 1;
1124 if (!Preamble.empty()) {
1125 // We've previously computed a preamble. Check whether we have the same
1126 // preamble now that we did before, and that there's enough space in
1127 // the main-file buffer within the precompiled preamble to fit the
1129 if (Preamble.size() == NewPreamble.second.first &&
1130 PreambleEndsAtStartOfLine == NewPreamble.second.second &&
1131 NewPreamble.first->getBufferSize() < PreambleReservedSize-2 &&
1132 memcmp(&Preamble[0], NewPreamble.first->getBufferStart(),
1133 NewPreamble.second.first) == 0) {
1134 // The preamble has not changed. We may be able to re-use the precompiled
1137 // Check that none of the files used by the preamble have changed.
1138 bool AnyFileChanged = false;
1140 // First, make a record of those files that have been overridden via
1141 // remapping or unsaved_files.
1142 llvm::StringMap<std::pair<off_t, time_t> > OverriddenFiles;
1143 for (PreprocessorOptions::remapped_file_iterator
1144 R = PreprocessorOpts.remapped_file_begin(),
1145 REnd = PreprocessorOpts.remapped_file_end();
1146 !AnyFileChanged && R != REnd;
1148 struct stat StatBuf;
1149 if (stat(R->second.c_str(), &StatBuf)) {
1150 // If we can't stat the file we're remapping to, assume that something
1151 // horrible happened.
1152 AnyFileChanged = true;
1156 OverriddenFiles[R->first] = std::make_pair(StatBuf.st_size,
1159 for (PreprocessorOptions::remapped_file_buffer_iterator
1160 R = PreprocessorOpts.remapped_file_buffer_begin(),
1161 REnd = PreprocessorOpts.remapped_file_buffer_end();
1162 !AnyFileChanged && R != REnd;
1164 // FIXME: Should we actually compare the contents of file->buffer
1166 OverriddenFiles[R->first] = std::make_pair(R->second->getBufferSize(),
1170 // Check whether anything has changed.
1171 for (llvm::StringMap<std::pair<off_t, time_t> >::iterator
1172 F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end();
1173 !AnyFileChanged && F != FEnd;
1175 llvm::StringMap<std::pair<off_t, time_t> >::iterator Overridden
1176 = OverriddenFiles.find(F->first());
1177 if (Overridden != OverriddenFiles.end()) {
1178 // This file was remapped; check whether the newly-mapped file
1179 // matches up with the previous mapping.
1180 if (Overridden->second != F->second)
1181 AnyFileChanged = true;
1185 // The file was not remapped; check whether it has changed on disk.
1186 struct stat StatBuf;
1187 if (stat(F->first(), &StatBuf)) {
1188 // If we can't stat the file, assume that something horrible happened.
1189 AnyFileChanged = true;
1190 } else if (StatBuf.st_size != F->second.first ||
1191 StatBuf.st_mtime != F->second.second)
1192 AnyFileChanged = true;
1195 if (!AnyFileChanged) {
1196 // Okay! We can re-use the precompiled preamble.
1198 // Set the state of the diagnostic object to mimic its state
1199 // after parsing the preamble.
1200 // FIXME: This won't catch any #pragma push warning changes that
1201 // have occurred in the preamble.
1202 getDiagnostics().Reset();
1203 ProcessWarningOptions(getDiagnostics(),
1204 PreambleInvocation.getDiagnosticOpts());
1205 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1206 if (StoredDiagnostics.size() > NumStoredDiagnosticsInPreamble)
1207 StoredDiagnostics.erase(
1208 StoredDiagnostics.begin() + NumStoredDiagnosticsInPreamble,
1209 StoredDiagnostics.end());
1211 // Create a version of the main file buffer that is padded to
1212 // buffer size we reserved when creating the preamble.
1213 return CreatePaddedMainFileBuffer(NewPreamble.first,
1214 PreambleReservedSize,
1215 FrontendOpts.Inputs[0].second);
1219 // If we aren't allowed to rebuild the precompiled preamble, just
1224 // We can't reuse the previously-computed preamble. Build a new one.
1226 llvm::sys::Path(PreambleFile).eraseFromDisk();
1227 PreambleRebuildCounter = 1;
1228 } else if (!AllowRebuild) {
1229 // We aren't allowed to rebuild the precompiled preamble; just
1234 // If the preamble rebuild counter > 1, it's because we previously
1235 // failed to build a preamble and we're not yet ready to try
1236 // again. Decrement the counter and return a failure.
1237 if (PreambleRebuildCounter > 1) {
1238 --PreambleRebuildCounter;
1242 // Create a temporary file for the precompiled preamble. In rare
1243 // circumstances, this can fail.
1244 std::string PreamblePCHPath = GetPreamblePCHPath();
1245 if (PreamblePCHPath.empty()) {
1246 // Try again next time.
1247 PreambleRebuildCounter = 1;
1251 // We did not previously compute a preamble, or it can't be reused anyway.
1252 SimpleTimer PreambleTimer(WantTiming);
1253 PreambleTimer.setOutput("Precompiling preamble");
1255 // Create a new buffer that stores the preamble. The buffer also contains
1256 // extra space for the original contents of the file (which will be present
1257 // when we actually parse the file) along with more room in case the file
1259 PreambleReservedSize = NewPreamble.first->getBufferSize();
1260 if (PreambleReservedSize < 4096)
1261 PreambleReservedSize = 8191;
1263 PreambleReservedSize *= 2;
1265 // Save the preamble text for later; we'll need to compare against it for
1266 // subsequent reparses.
1267 Preamble.assign(NewPreamble.first->getBufferStart(),
1268 NewPreamble.first->getBufferStart()
1269 + NewPreamble.second.first);
1270 PreambleEndsAtStartOfLine = NewPreamble.second.second;
1272 delete PreambleBuffer;
1274 = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize,
1275 FrontendOpts.Inputs[0].second);
1276 memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()),
1277 NewPreamble.first->getBufferStart(), Preamble.size());
1278 memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(),
1279 ' ', PreambleReservedSize - Preamble.size() - 1);
1280 const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n';
1282 // Remap the main source file to the preamble buffer.
1283 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second);
1284 PreprocessorOpts.addRemappedFile(MainFilePath.str(), PreambleBuffer);
1286 // Tell the compiler invocation to generate a temporary precompiled header.
1287 FrontendOpts.ProgramAction = frontend::GeneratePCH;
1288 FrontendOpts.ChainedPCH = true;
1289 // FIXME: Generate the precompiled header into memory?
1290 FrontendOpts.OutputFile = PreamblePCHPath;
1291 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
1292 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
1294 // Create the compiler instance to use for building the precompiled preamble.
1295 CompilerInstance Clang;
1296 Clang.setInvocation(&PreambleInvocation);
1297 OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
1299 // Set up diagnostics, capturing all of the diagnostics produced.
1300 Clang.setDiagnostics(&getDiagnostics());
1302 // Create the target instance.
1303 Clang.getTargetOpts().Features = TargetFeatures;
1304 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
1305 Clang.getTargetOpts()));
1306 if (!Clang.hasTarget()) {
1307 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1309 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1310 PreprocessorOpts.eraseRemappedFile(
1311 PreprocessorOpts.remapped_file_buffer_end() - 1);
1315 // Inform the target of the language options.
1317 // FIXME: We shouldn't need to do this, the target should be immutable once
1318 // created. This complexity should be lifted elsewhere.
1319 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts());
1321 assert(Clang.getFrontendOpts().Inputs.size() == 1 &&
1322 "Invocation must have exactly one source file!");
1323 assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST &&
1324 "FIXME: AST inputs not yet supported here!");
1325 assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
1326 "IR inputs not support here!");
1328 // Clear out old caches and data.
1329 getDiagnostics().Reset();
1330 ProcessWarningOptions(getDiagnostics(), Clang.getDiagnosticOpts());
1331 StoredDiagnostics.erase(
1332 StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver,
1333 StoredDiagnostics.end());
1334 TopLevelDecls.clear();
1335 TopLevelDeclsInPreamble.clear();
1336 PreprocessedEntities.clear();
1337 PreprocessedEntitiesInPreamble.clear();
1339 // Create a file manager object to provide access to and cache the filesystem.
1340 Clang.setFileManager(new FileManager(Clang.getFileSystemOpts()));
1342 // Create the source manager.
1343 Clang.setSourceManager(new SourceManager(getDiagnostics(),
1344 Clang.getFileManager()));
1346 llvm::OwningPtr<PrecompilePreambleAction> Act;
1347 Act.reset(new PrecompilePreambleAction(*this));
1348 if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
1349 Clang.getFrontendOpts().Inputs[0].first)) {
1350 Clang.takeInvocation();
1351 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1353 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1354 PreprocessorOpts.eraseRemappedFile(
1355 PreprocessorOpts.remapped_file_buffer_end() - 1);
1360 Act->EndSourceFile();
1361 Clang.takeInvocation();
1363 if (Diagnostics->hasErrorOccurred()) {
1364 // There were errors parsing the preamble, so no precompiled header was
1365 // generated. Forget that we even tried.
1366 // FIXME: Should we leave a note for ourselves to try again?
1367 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1369 TopLevelDeclsInPreamble.clear();
1370 PreprocessedEntities.clear();
1371 PreprocessedEntitiesInPreamble.clear();
1372 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1373 PreprocessorOpts.eraseRemappedFile(
1374 PreprocessorOpts.remapped_file_buffer_end() - 1);
1378 // Keep track of the preamble we precompiled.
1379 PreambleFile = FrontendOpts.OutputFile;
1380 NumStoredDiagnosticsInPreamble = StoredDiagnostics.size();
1381 NumWarningsInPreamble = getDiagnostics().getNumWarnings();
1383 // Keep track of all of the files that the source manager knows about,
1384 // so we can verify whether they have changed or not.
1385 FilesInPreamble.clear();
1386 SourceManager &SourceMgr = Clang.getSourceManager();
1387 const llvm::MemoryBuffer *MainFileBuffer
1388 = SourceMgr.getBuffer(SourceMgr.getMainFileID());
1389 for (SourceManager::fileinfo_iterator F = SourceMgr.fileinfo_begin(),
1390 FEnd = SourceMgr.fileinfo_end();
1393 const FileEntry *File = F->second->Entry;
1394 if (!File || F->second->getRawBuffer() == MainFileBuffer)
1397 FilesInPreamble[File->getName()]
1398 = std::make_pair(F->second->getSize(), File->getModificationTime());
1401 PreambleRebuildCounter = 1;
1402 PreprocessorOpts.eraseRemappedFile(
1403 PreprocessorOpts.remapped_file_buffer_end() - 1);
1405 // If the hash of top-level entities differs from the hash of the top-level
1406 // entities the last time we rebuilt the preamble, clear out the completion
1408 if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1409 CompletionCacheTopLevelHashValue = 0;
1410 PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1413 return CreatePaddedMainFileBuffer(NewPreamble.first,
1414 PreambleReservedSize,
1415 FrontendOpts.Inputs[0].second);
1418 void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1419 std::vector<Decl *> Resolved;
1420 Resolved.reserve(TopLevelDeclsInPreamble.size());
1421 ExternalASTSource &Source = *getASTContext().getExternalSource();
1422 for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) {
1423 // Resolve the declaration ID to an actual declaration, possibly
1424 // deserializing the declaration in the process.
1425 Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]);
1427 Resolved.push_back(D);
1429 TopLevelDeclsInPreamble.clear();
1430 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1433 void ASTUnit::RealizePreprocessedEntitiesFromPreamble() {
1437 PreprocessingRecord *PPRec = PP->getPreprocessingRecord();
1441 ExternalPreprocessingRecordSource *External = PPRec->getExternalSource();
1445 for (unsigned I = 0, N = PreprocessedEntitiesInPreamble.size(); I != N; ++I) {
1446 if (PreprocessedEntity *PE
1447 = External->ReadPreprocessedEntityAtOffset(
1448 PreprocessedEntitiesInPreamble[I]))
1449 PreprocessedEntities.push_back(PE);
1452 if (PreprocessedEntities.empty())
1455 PreprocessedEntities.insert(PreprocessedEntities.end(),
1456 PPRec->begin(true), PPRec->end(true));
1459 ASTUnit::pp_entity_iterator ASTUnit::pp_entity_begin() {
1460 if (!PreprocessedEntitiesInPreamble.empty() &&
1461 PreprocessedEntities.empty())
1462 RealizePreprocessedEntitiesFromPreamble();
1464 if (PreprocessedEntities.empty())
1465 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
1466 return PPRec->begin(true);
1468 return PreprocessedEntities.begin();
1471 ASTUnit::pp_entity_iterator ASTUnit::pp_entity_end() {
1472 if (!PreprocessedEntitiesInPreamble.empty() &&
1473 PreprocessedEntities.empty())
1474 RealizePreprocessedEntitiesFromPreamble();
1476 if (PreprocessedEntities.empty())
1477 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
1478 return PPRec->end(true);
1480 return PreprocessedEntities.end();
1483 unsigned ASTUnit::getMaxPCHLevel() const {
1484 if (!getOnlyLocalDecls())
1485 return Decl::MaxPCHLevel;
1490 llvm::StringRef ASTUnit::getMainFileName() const {
1491 return Invocation->getFrontendOpts().Inputs[0].second;
1494 bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) {
1498 // We'll manage file buffers ourselves.
1499 Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1500 Invocation->getFrontendOpts().DisableFree = false;
1501 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1503 // Save the target features.
1504 TargetFeatures = Invocation->getTargetOpts().Features;
1506 llvm::MemoryBuffer *OverrideMainBuffer = 0;
1507 if (PrecompilePreamble) {
1508 PreambleRebuildCounter = 2;
1510 = getMainBufferWithPrecompiledPreamble(*Invocation);
1513 SimpleTimer ParsingTimer(WantTiming);
1514 ParsingTimer.setOutput("Parsing " + getMainFileName());
1516 return Parse(OverrideMainBuffer);
1519 ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
1520 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
1521 bool OnlyLocalDecls,
1522 bool CaptureDiagnostics,
1523 bool PrecompilePreamble,
1524 bool CompleteTranslationUnit,
1525 bool CacheCodeCompletionResults) {
1526 // Create the AST unit.
1527 llvm::OwningPtr<ASTUnit> AST;
1528 AST.reset(new ASTUnit(false));
1529 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
1530 AST->Diagnostics = Diags;
1531 AST->OnlyLocalDecls = OnlyLocalDecls;
1532 AST->CaptureDiagnostics = CaptureDiagnostics;
1533 AST->CompleteTranslationUnit = CompleteTranslationUnit;
1534 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1535 AST->Invocation.reset(CI);
1537 return AST->LoadFromCompilerInvocation(PrecompilePreamble)? 0 : AST.take();
1540 ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
1541 const char **ArgEnd,
1542 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
1543 llvm::StringRef ResourceFilesPath,
1544 bool OnlyLocalDecls,
1545 bool CaptureDiagnostics,
1546 RemappedFile *RemappedFiles,
1547 unsigned NumRemappedFiles,
1548 bool PrecompilePreamble,
1549 bool CompleteTranslationUnit,
1550 bool CacheCodeCompletionResults,
1551 bool CXXPrecompilePreamble,
1552 bool CXXChainedPCH) {
1553 if (!Diags.getPtr()) {
1554 // No diagnostics engine was provided, so create our own diagnostics object
1555 // with the default options.
1556 DiagnosticOptions DiagOpts;
1557 Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd - ArgBegin,
1561 llvm::SmallVector<const char *, 16> Args;
1562 Args.push_back("<clang>"); // FIXME: Remove dummy argument.
1563 Args.insert(Args.end(), ArgBegin, ArgEnd);
1565 // FIXME: Find a cleaner way to force the driver into restricted modes. We
1566 // also want to force it to use clang.
1567 Args.push_back("-fsyntax-only");
1569 llvm::SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1571 llvm::OwningPtr<CompilerInvocation> CI;
1574 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
1577 // FIXME: We shouldn't have to pass in the path info.
1578 driver::Driver TheDriver("clang", llvm::sys::getHostTriple(),
1579 "a.out", false, false, *Diags);
1581 // Don't check that inputs exist, they have been remapped.
1582 TheDriver.setCheckInputsExist(false);
1584 llvm::OwningPtr<driver::Compilation> C(
1585 TheDriver.BuildCompilation(Args.size(), Args.data()));
1587 // We expect to get back exactly one command job, if we didn't something
1589 const driver::JobList &Jobs = C->getJobs();
1590 if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) {
1591 llvm::SmallString<256> Msg;
1592 llvm::raw_svector_ostream OS(Msg);
1593 C->PrintJob(OS, C->getJobs(), "; ", true);
1594 Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
1598 const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
1599 if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") {
1600 Diags->Report(diag::err_fe_expected_clang_command);
1604 const driver::ArgStringList &CCArgs = Cmd->getArguments();
1605 CI.reset(new CompilerInvocation);
1606 CompilerInvocation::CreateFromArgs(*CI,
1607 const_cast<const char **>(CCArgs.data()),
1608 const_cast<const char **>(CCArgs.data()) +
1613 // Override any files that need remapping
1614 for (unsigned I = 0; I != NumRemappedFiles; ++I)
1615 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1616 RemappedFiles[I].second);
1618 // Override the resources path.
1619 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1621 // Check whether we should precompile the preamble and/or use chained PCH.
1622 // FIXME: This is a temporary hack while we debug C++ chained PCH.
1623 if (CI->getLangOpts().CPlusPlus) {
1624 PrecompilePreamble = PrecompilePreamble && CXXPrecompilePreamble;
1626 if (PrecompilePreamble && !CXXChainedPCH &&
1627 !CI->getPreprocessorOpts().ImplicitPCHInclude.empty())
1628 PrecompilePreamble = false;
1631 // Create the AST unit.
1632 llvm::OwningPtr<ASTUnit> AST;
1633 AST.reset(new ASTUnit(false));
1634 ConfigureDiags(Diags, ArgBegin, ArgEnd, *AST, CaptureDiagnostics);
1635 AST->Diagnostics = Diags;
1637 AST->FileMgr.reset(new FileManager(FileSystemOptions()));
1638 AST->OnlyLocalDecls = OnlyLocalDecls;
1639 AST->CaptureDiagnostics = CaptureDiagnostics;
1640 AST->CompleteTranslationUnit = CompleteTranslationUnit;
1641 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1642 AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1643 AST->NumStoredDiagnosticsInPreamble = StoredDiagnostics.size();
1644 AST->StoredDiagnostics.swap(StoredDiagnostics);
1645 AST->Invocation.reset(CI.take());
1646 return AST->LoadFromCompilerInvocation(PrecompilePreamble) ? 0 : AST.take();
1649 bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) {
1650 if (!Invocation.get())
1653 SimpleTimer ParsingTimer(WantTiming);
1654 ParsingTimer.setOutput("Reparsing " + getMainFileName());
1657 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1658 PPOpts.DisableStatCache = true;
1659 for (PreprocessorOptions::remapped_file_buffer_iterator
1660 R = PPOpts.remapped_file_buffer_begin(),
1661 REnd = PPOpts.remapped_file_buffer_end();
1666 Invocation->getPreprocessorOpts().clearRemappedFiles();
1667 for (unsigned I = 0; I != NumRemappedFiles; ++I)
1668 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1669 RemappedFiles[I].second);
1671 // If we have a preamble file lying around, or if we might try to
1672 // build a precompiled preamble, do so now.
1673 llvm::MemoryBuffer *OverrideMainBuffer = 0;
1674 if (!PreambleFile.empty() || PreambleRebuildCounter > 0)
1675 OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation);
1677 // Clear out the diagnostics state.
1678 if (!OverrideMainBuffer) {
1679 getDiagnostics().Reset();
1680 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1683 // Parse the sources
1684 bool Result = Parse(OverrideMainBuffer);
1686 // If we're caching global code-completion results, and the top-level
1687 // declarations have changed, clear out the code-completion cache.
1688 if (!Result && ShouldCacheCodeCompletionResults &&
1689 CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
1690 CacheCodeCompletionResults();
1695 //----------------------------------------------------------------------------//
1697 //----------------------------------------------------------------------------//
1700 /// \brief Code completion consumer that combines the cached code-completion
1701 /// results from an ASTUnit with the code-completion results provided to it,
1702 /// then passes the result on to
1703 class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
1704 unsigned NormalContexts;
1706 CodeCompleteConsumer &Next;
1709 AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
1710 bool IncludeMacros, bool IncludeCodePatterns,
1711 bool IncludeGlobals)
1712 : CodeCompleteConsumer(IncludeMacros, IncludeCodePatterns, IncludeGlobals,
1713 Next.isOutputBinary()), AST(AST), Next(Next)
1715 // Compute the set of contexts in which we will look when we don't have
1716 // any information about the specific context.
1718 = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
1719 | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1))
1720 | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1))
1721 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
1722 | (1 << (CodeCompletionContext::CCC_Statement - 1))
1723 | (1 << (CodeCompletionContext::CCC_Expression - 1))
1724 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
1725 | (1 << (CodeCompletionContext::CCC_MemberAccess - 1))
1726 | (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1))
1727 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
1728 | (1 << (CodeCompletionContext::CCC_Recovery - 1));
1730 if (AST.getASTContext().getLangOptions().CPlusPlus)
1731 NormalContexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1))
1732 | (1 << (CodeCompletionContext::CCC_UnionTag - 1))
1733 | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1));
1736 virtual void ProcessCodeCompleteResults(Sema &S,
1737 CodeCompletionContext Context,
1738 CodeCompletionResult *Results,
1739 unsigned NumResults);
1741 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
1742 OverloadCandidate *Candidates,
1743 unsigned NumCandidates) {
1744 Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
1747 virtual CodeCompletionAllocator &getAllocator() {
1748 return Next.getAllocator();
1753 /// \brief Helper function that computes which global names are hidden by the
1754 /// local code-completion results.
1755 static void CalculateHiddenNames(const CodeCompletionContext &Context,
1756 CodeCompletionResult *Results,
1757 unsigned NumResults,
1759 llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
1760 bool OnlyTagNames = false;
1761 switch (Context.getKind()) {
1762 case CodeCompletionContext::CCC_Recovery:
1763 case CodeCompletionContext::CCC_TopLevel:
1764 case CodeCompletionContext::CCC_ObjCInterface:
1765 case CodeCompletionContext::CCC_ObjCImplementation:
1766 case CodeCompletionContext::CCC_ObjCIvarList:
1767 case CodeCompletionContext::CCC_ClassStructUnion:
1768 case CodeCompletionContext::CCC_Statement:
1769 case CodeCompletionContext::CCC_Expression:
1770 case CodeCompletionContext::CCC_ObjCMessageReceiver:
1771 case CodeCompletionContext::CCC_MemberAccess:
1772 case CodeCompletionContext::CCC_Namespace:
1773 case CodeCompletionContext::CCC_Type:
1774 case CodeCompletionContext::CCC_Name:
1775 case CodeCompletionContext::CCC_PotentiallyQualifiedName:
1776 case CodeCompletionContext::CCC_ParenthesizedExpression:
1779 case CodeCompletionContext::CCC_EnumTag:
1780 case CodeCompletionContext::CCC_UnionTag:
1781 case CodeCompletionContext::CCC_ClassOrStructTag:
1782 OnlyTagNames = true;
1785 case CodeCompletionContext::CCC_ObjCProtocolName:
1786 case CodeCompletionContext::CCC_MacroName:
1787 case CodeCompletionContext::CCC_MacroNameUse:
1788 case CodeCompletionContext::CCC_PreprocessorExpression:
1789 case CodeCompletionContext::CCC_PreprocessorDirective:
1790 case CodeCompletionContext::CCC_NaturalLanguage:
1791 case CodeCompletionContext::CCC_SelectorName:
1792 case CodeCompletionContext::CCC_TypeQualifiers:
1793 case CodeCompletionContext::CCC_Other:
1794 case CodeCompletionContext::CCC_OtherWithMacros:
1795 // We're looking for nothing, or we're looking for names that cannot
1800 typedef CodeCompletionResult Result;
1801 for (unsigned I = 0; I != NumResults; ++I) {
1802 if (Results[I].Kind != Result::RK_Declaration)
1806 = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
1808 bool Hiding = false;
1810 Hiding = (IDNS & Decl::IDNS_Tag);
1812 unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
1813 Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
1814 Decl::IDNS_NonMemberOperator);
1815 if (Ctx.getLangOptions().CPlusPlus)
1816 HiddenIDNS |= Decl::IDNS_Tag;
1817 Hiding = (IDNS & HiddenIDNS);
1823 DeclarationName Name = Results[I].Declaration->getDeclName();
1824 if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
1825 HiddenNames.insert(Identifier->getName());
1827 HiddenNames.insert(Name.getAsString());
1832 void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
1833 CodeCompletionContext Context,
1834 CodeCompletionResult *Results,
1835 unsigned NumResults) {
1836 // Merge the results we were given with the results we cached.
1837 bool AddedResult = false;
1839 = (Context.getKind() == CodeCompletionContext::CCC_Recovery? NormalContexts
1840 : (1 << (Context.getKind() - 1)));
1842 // Contains the set of names that are hidden by "local" completion results.
1843 llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
1844 typedef CodeCompletionResult Result;
1845 llvm::SmallVector<Result, 8> AllResults;
1846 for (ASTUnit::cached_completion_iterator
1847 C = AST.cached_completion_begin(),
1848 CEnd = AST.cached_completion_end();
1850 // If the context we are in matches any of the contexts we are
1851 // interested in, we'll add this result.
1852 if ((C->ShowInContexts & InContexts) == 0)
1855 // If we haven't added any results previously, do so now.
1857 CalculateHiddenNames(Context, Results, NumResults, S.Context,
1859 AllResults.insert(AllResults.end(), Results, Results + NumResults);
1863 // Determine whether this global completion result is hidden by a local
1864 // completion result. If so, skip it.
1865 if (C->Kind != CXCursor_MacroDefinition &&
1866 HiddenNames.count(C->Completion->getTypedText()))
1869 // Adjust priority based on similar type classes.
1870 unsigned Priority = C->Priority;
1871 CXCursorKind CursorKind = C->Kind;
1872 CodeCompletionString *Completion = C->Completion;
1873 if (!Context.getPreferredType().isNull()) {
1874 if (C->Kind == CXCursor_MacroDefinition) {
1875 Priority = getMacroUsagePriority(C->Completion->getTypedText(),
1877 Context.getPreferredType()->isAnyPointerType());
1878 } else if (C->Type) {
1879 CanQualType Expected
1880 = S.Context.getCanonicalType(
1881 Context.getPreferredType().getUnqualifiedType());
1882 SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
1883 if (ExpectedSTC == C->TypeClass) {
1884 // We know this type is similar; check for an exact match.
1885 llvm::StringMap<unsigned> &CachedCompletionTypes
1886 = AST.getCachedCompletionTypes();
1887 llvm::StringMap<unsigned>::iterator Pos
1888 = CachedCompletionTypes.find(QualType(Expected).getAsString());
1889 if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
1890 Priority /= CCF_ExactTypeMatch;
1892 Priority /= CCF_SimilarTypeMatch;
1897 // Adjust the completion string, if required.
1898 if (C->Kind == CXCursor_MacroDefinition &&
1899 Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
1900 // Create a new code-completion string that just contains the
1901 // macro name, without its arguments.
1902 CodeCompletionBuilder Builder(getAllocator(), CCP_CodePattern,
1904 Builder.AddTypedTextChunk(C->Completion->getTypedText());
1905 CursorKind = CXCursor_NotImplemented;
1906 Priority = CCP_CodePattern;
1907 Completion = Builder.TakeString();
1910 AllResults.push_back(Result(Completion, Priority, CursorKind,
1914 // If we did not add any cached completion results, just forward the
1915 // results we were given to the next consumer.
1917 Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
1921 Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
1927 void ASTUnit::CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column,
1928 RemappedFile *RemappedFiles,
1929 unsigned NumRemappedFiles,
1931 bool IncludeCodePatterns,
1932 CodeCompleteConsumer &Consumer,
1933 Diagnostic &Diag, LangOptions &LangOpts,
1934 SourceManager &SourceMgr, FileManager &FileMgr,
1935 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
1936 llvm::SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
1937 if (!Invocation.get())
1940 SimpleTimer CompletionTimer(WantTiming);
1941 CompletionTimer.setOutput("Code completion @ " + File + ":" +
1942 llvm::Twine(Line) + ":" + llvm::Twine(Column));
1944 CompilerInvocation CCInvocation(*Invocation);
1945 FrontendOptions &FrontendOpts = CCInvocation.getFrontendOpts();
1946 PreprocessorOptions &PreprocessorOpts = CCInvocation.getPreprocessorOpts();
1948 FrontendOpts.ShowMacrosInCodeCompletion
1949 = IncludeMacros && CachedCompletionResults.empty();
1950 FrontendOpts.ShowCodePatternsInCodeCompletion = IncludeCodePatterns;
1951 FrontendOpts.ShowGlobalSymbolsInCodeCompletion
1952 = CachedCompletionResults.empty();
1953 FrontendOpts.CodeCompletionAt.FileName = File;
1954 FrontendOpts.CodeCompletionAt.Line = Line;
1955 FrontendOpts.CodeCompletionAt.Column = Column;
1957 // Set the language options appropriately.
1958 LangOpts = CCInvocation.getLangOpts();
1960 CompilerInstance Clang;
1961 Clang.setInvocation(&CCInvocation);
1962 OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
1964 // Set up diagnostics, capturing any diagnostics produced.
1965 Clang.setDiagnostics(&Diag);
1966 ProcessWarningOptions(Diag, CCInvocation.getDiagnosticOpts());
1967 CaptureDroppedDiagnostics Capture(true,
1968 Clang.getDiagnostics(),
1971 // Create the target instance.
1972 Clang.getTargetOpts().Features = TargetFeatures;
1973 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
1974 Clang.getTargetOpts()));
1975 if (!Clang.hasTarget()) {
1976 Clang.takeInvocation();
1980 // Inform the target of the language options.
1982 // FIXME: We shouldn't need to do this, the target should be immutable once
1983 // created. This complexity should be lifted elsewhere.
1984 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts());
1986 assert(Clang.getFrontendOpts().Inputs.size() == 1 &&
1987 "Invocation must have exactly one source file!");
1988 assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST &&
1989 "FIXME: AST inputs not yet supported here!");
1990 assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
1991 "IR inputs not support here!");
1994 // Use the source and file managers that we were given.
1995 Clang.setFileManager(&FileMgr);
1996 Clang.setSourceManager(&SourceMgr);
1999 PreprocessorOpts.clearRemappedFiles();
2000 PreprocessorOpts.RetainRemappedFileBuffers = true;
2001 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
2002 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first,
2003 RemappedFiles[I].second);
2004 OwnedBuffers.push_back(RemappedFiles[I].second);
2007 // Use the code completion consumer we were given, but adding any cached
2008 // code-completion results.
2009 AugmentedCodeCompleteConsumer *AugmentedConsumer
2010 = new AugmentedCodeCompleteConsumer(*this, Consumer,
2011 FrontendOpts.ShowMacrosInCodeCompletion,
2012 FrontendOpts.ShowCodePatternsInCodeCompletion,
2013 FrontendOpts.ShowGlobalSymbolsInCodeCompletion);
2014 Clang.setCodeCompletionConsumer(AugmentedConsumer);
2016 // If we have a precompiled preamble, try to use it. We only allow
2017 // the use of the precompiled preamble if we're if the completion
2018 // point is within the main file, after the end of the precompiled
2020 llvm::MemoryBuffer *OverrideMainBuffer = 0;
2021 if (!PreambleFile.empty()) {
2022 using llvm::sys::FileStatus;
2023 llvm::sys::PathWithStatus CompleteFilePath(File);
2024 llvm::sys::PathWithStatus MainPath(OriginalSourceFile);
2025 if (const FileStatus *CompleteFileStatus = CompleteFilePath.getFileStatus())
2026 if (const FileStatus *MainStatus = MainPath.getFileStatus())
2027 if (CompleteFileStatus->getUniqueID() == MainStatus->getUniqueID())
2029 = getMainBufferWithPrecompiledPreamble(CCInvocation, false,
2033 // If the main file has been overridden due to the use of a preamble,
2034 // make that override happen and introduce the preamble.
2035 PreprocessorOpts.DisableStatCache = true;
2036 StoredDiagnostics.insert(StoredDiagnostics.end(),
2037 this->StoredDiagnostics.begin(),
2038 this->StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver);
2039 if (OverrideMainBuffer) {
2040 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
2041 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
2042 PreprocessorOpts.PrecompiledPreambleBytes.second
2043 = PreambleEndsAtStartOfLine;
2044 PreprocessorOpts.ImplicitPCHInclude = PreambleFile;
2045 PreprocessorOpts.DisablePCHValidation = true;
2047 // The stored diagnostics have the old source manager. Copy them
2048 // to our output set of stored diagnostics, updating the source
2049 // manager to the one we were given.
2050 for (unsigned I = NumStoredDiagnosticsFromDriver,
2051 N = this->StoredDiagnostics.size();
2053 StoredDiagnostics.push_back(this->StoredDiagnostics[I]);
2054 FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), SourceMgr);
2055 StoredDiagnostics[I].setLocation(Loc);
2058 OwnedBuffers.push_back(OverrideMainBuffer);
2060 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2061 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2064 llvm::OwningPtr<SyntaxOnlyAction> Act;
2065 Act.reset(new SyntaxOnlyAction);
2066 if (Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
2067 Clang.getFrontendOpts().Inputs[0].first)) {
2069 Act->EndSourceFile();
2072 // Steal back our resources.
2073 Clang.takeFileManager();
2074 Clang.takeSourceManager();
2075 Clang.takeInvocation();
2078 bool ASTUnit::Save(llvm::StringRef File) {
2079 if (getDiagnostics().hasErrorOccurred())
2082 // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2083 // unconditionally create a stat cache when we parse the file?
2084 std::string ErrorInfo;
2085 llvm::raw_fd_ostream Out(File.str().c_str(), ErrorInfo,
2086 llvm::raw_fd_ostream::F_Binary);
2087 if (!ErrorInfo.empty() || Out.has_error())
2090 std::vector<unsigned char> Buffer;
2091 llvm::BitstreamWriter Stream(Buffer);
2092 ASTWriter Writer(Stream);
2093 Writer.WriteAST(getSema(), 0, std::string(), 0);
2095 // Write the generated bitstream to "Out".
2096 if (!Buffer.empty())
2097 Out.write((char *)&Buffer.front(), Buffer.size());
2099 return Out.has_error();