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/ASTWriter.h"
31 #include "clang/Lex/HeaderSearch.h"
32 #include "clang/Lex/Preprocessor.h"
33 #include "clang/Basic/TargetOptions.h"
34 #include "clang/Basic/TargetInfo.h"
35 #include "clang/Basic/Diagnostic.h"
36 #include "llvm/ADT/StringSet.h"
37 #include "llvm/Support/MemoryBuffer.h"
38 #include "llvm/System/Host.h"
39 #include "llvm/System/Path.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include "llvm/Support/Timer.h"
45 using namespace clang
;
47 using llvm::TimeRecord
;
56 explicit SimpleTimer(bool WantTiming
) : WantTiming(WantTiming
) {
58 Start
= TimeRecord::getCurrentTime();
61 void setOutput(const llvm::Twine
&Output
) {
63 this->Output
= Output
.str();
68 TimeRecord Elapsed
= TimeRecord::getCurrentTime();
70 llvm::errs() << Output
<< ':';
71 Elapsed
.print(Elapsed
, llvm::errs());
78 /// \brief After failing to build a precompiled preamble (due to
79 /// errors in the source that occurs in the preamble), the number of
80 /// reparses during which we'll skip even trying to precompile the
82 const unsigned DefaultPreambleRebuildInterval
= 5;
84 /// \brief Tracks the number of ASTUnit objects that are currently active.
86 /// Used for debugging purposes only.
87 static unsigned ActiveASTUnitObjects
;
89 ASTUnit::ASTUnit(bool _MainFileIsAST
)
90 : CaptureDiagnostics(false), MainFileIsAST(_MainFileIsAST
),
91 CompleteTranslationUnit(true), WantTiming(getenv("LIBCLANG_TIMING")),
92 NumStoredDiagnosticsFromDriver(0),
93 ConcurrencyCheckValue(CheckUnlocked
),
94 PreambleRebuildCounter(0), SavedMainFileBuffer(0), PreambleBuffer(0),
95 ShouldCacheCodeCompletionResults(false),
96 NumTopLevelDeclsAtLastCompletionCache(0),
97 CacheCodeCompletionCoolDown(0),
99 if (getenv("LIBCLANG_OBJTRACKING")) {
100 ++ActiveASTUnitObjects
;
101 fprintf(stderr
, "+++ %d translation units\n", ActiveASTUnitObjects
);
105 ASTUnit::~ASTUnit() {
106 ConcurrencyCheckValue
= CheckLocked
;
107 CleanTemporaryFiles();
108 if (!PreambleFile
.empty())
109 llvm::sys::Path(PreambleFile
).eraseFromDisk();
111 // Free the buffers associated with remapped files. We are required to
112 // perform this operation here because we explicitly request that the
113 // compiler instance *not* free these buffers for each invocation of the
115 if (Invocation
.get()) {
116 PreprocessorOptions
&PPOpts
= Invocation
->getPreprocessorOpts();
117 for (PreprocessorOptions::remapped_file_buffer_iterator
118 FB
= PPOpts
.remapped_file_buffer_begin(),
119 FBEnd
= PPOpts
.remapped_file_buffer_end();
125 delete SavedMainFileBuffer
;
126 delete PreambleBuffer
;
128 ClearCachedCompletionResults();
130 if (getenv("LIBCLANG_OBJTRACKING")) {
131 --ActiveASTUnitObjects
;
132 fprintf(stderr
, "--- %d translation units\n", ActiveASTUnitObjects
);
136 void ASTUnit::CleanTemporaryFiles() {
137 for (unsigned I
= 0, N
= TemporaryFiles
.size(); I
!= N
; ++I
)
138 TemporaryFiles
[I
].eraseFromDisk();
139 TemporaryFiles
.clear();
142 /// \brief Determine the set of code-completion contexts in which this
143 /// declaration should be shown.
144 static unsigned getDeclShowContexts(NamedDecl
*ND
,
145 const LangOptions
&LangOpts
,
146 bool &IsNestedNameSpecifier
) {
147 IsNestedNameSpecifier
= false;
149 if (isa
<UsingShadowDecl
>(ND
))
150 ND
= dyn_cast
<NamedDecl
>(ND
->getUnderlyingDecl());
154 unsigned Contexts
= 0;
155 if (isa
<TypeDecl
>(ND
) || isa
<ObjCInterfaceDecl
>(ND
) ||
156 isa
<ClassTemplateDecl
>(ND
) || isa
<TemplateTemplateParmDecl
>(ND
)) {
157 // Types can appear in these contexts.
158 if (LangOpts
.CPlusPlus
|| !isa
<TagDecl
>(ND
))
159 Contexts
|= (1 << (CodeCompletionContext::CCC_TopLevel
- 1))
160 | (1 << (CodeCompletionContext::CCC_ObjCIvarList
- 1))
161 | (1 << (CodeCompletionContext::CCC_ClassStructUnion
- 1))
162 | (1 << (CodeCompletionContext::CCC_Statement
- 1))
163 | (1 << (CodeCompletionContext::CCC_Type
- 1))
164 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression
- 1));
166 // In C++, types can appear in expressions contexts (for functional casts).
167 if (LangOpts
.CPlusPlus
)
168 Contexts
|= (1 << (CodeCompletionContext::CCC_Expression
- 1));
170 // In Objective-C, message sends can send interfaces. In Objective-C++,
171 // all types are available due to functional casts.
172 if (LangOpts
.CPlusPlus
|| isa
<ObjCInterfaceDecl
>(ND
))
173 Contexts
|= (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver
- 1));
175 // Deal with tag names.
176 if (isa
<EnumDecl
>(ND
)) {
177 Contexts
|= (1 << (CodeCompletionContext::CCC_EnumTag
- 1));
179 // Part of the nested-name-specifier in C++0x.
180 if (LangOpts
.CPlusPlus0x
)
181 IsNestedNameSpecifier
= true;
182 } else if (RecordDecl
*Record
= dyn_cast
<RecordDecl
>(ND
)) {
183 if (Record
->isUnion())
184 Contexts
|= (1 << (CodeCompletionContext::CCC_UnionTag
- 1));
186 Contexts
|= (1 << (CodeCompletionContext::CCC_ClassOrStructTag
- 1));
188 if (LangOpts
.CPlusPlus
)
189 IsNestedNameSpecifier
= true;
190 } else if (isa
<ClassTemplateDecl
>(ND
))
191 IsNestedNameSpecifier
= true;
192 } else if (isa
<ValueDecl
>(ND
) || isa
<FunctionTemplateDecl
>(ND
)) {
193 // Values can appear in these contexts.
194 Contexts
= (1 << (CodeCompletionContext::CCC_Statement
- 1))
195 | (1 << (CodeCompletionContext::CCC_Expression
- 1))
196 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression
- 1))
197 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver
- 1));
198 } else if (isa
<ObjCProtocolDecl
>(ND
)) {
199 Contexts
= (1 << (CodeCompletionContext::CCC_ObjCProtocolName
- 1));
200 } else if (isa
<NamespaceDecl
>(ND
) || isa
<NamespaceAliasDecl
>(ND
)) {
201 Contexts
= (1 << (CodeCompletionContext::CCC_Namespace
- 1));
203 // Part of the nested-name-specifier.
204 IsNestedNameSpecifier
= true;
210 void ASTUnit::CacheCodeCompletionResults() {
214 SimpleTimer
Timer(WantTiming
);
215 Timer
.setOutput("Cache global code completions for " + getMainFileName());
217 // Clear out the previous results.
218 ClearCachedCompletionResults();
220 // Gather the set of global code completions.
221 typedef CodeCompletionResult Result
;
222 llvm::SmallVector
<Result
, 8> Results
;
223 TheSema
->GatherGlobalCodeCompletions(Results
);
225 // Translate global code completions into cached completions.
226 llvm::DenseMap
<CanQualType
, unsigned> CompletionTypes
;
228 for (unsigned I
= 0, N
= Results
.size(); I
!= N
; ++I
) {
229 switch (Results
[I
].Kind
) {
230 case Result::RK_Declaration
: {
231 bool IsNestedNameSpecifier
= false;
232 CachedCodeCompletionResult CachedResult
;
233 CachedResult
.Completion
= Results
[I
].CreateCodeCompletionString(*TheSema
);
234 CachedResult
.ShowInContexts
= getDeclShowContexts(Results
[I
].Declaration
,
235 Ctx
->getLangOptions(),
236 IsNestedNameSpecifier
);
237 CachedResult
.Priority
= Results
[I
].Priority
;
238 CachedResult
.Kind
= Results
[I
].CursorKind
;
239 CachedResult
.Availability
= Results
[I
].Availability
;
241 // Keep track of the type of this completion in an ASTContext-agnostic
243 QualType UsageType
= getDeclUsageType(*Ctx
, Results
[I
].Declaration
);
244 if (UsageType
.isNull()) {
245 CachedResult
.TypeClass
= STC_Void
;
246 CachedResult
.Type
= 0;
248 CanQualType CanUsageType
249 = Ctx
->getCanonicalType(UsageType
.getUnqualifiedType());
250 CachedResult
.TypeClass
= getSimplifiedTypeClass(CanUsageType
);
252 // Determine whether we have already seen this type. If so, we save
253 // ourselves the work of formatting the type string by using the
254 // temporary, CanQualType-based hash table to find the associated value.
255 unsigned &TypeValue
= CompletionTypes
[CanUsageType
];
256 if (TypeValue
== 0) {
257 TypeValue
= CompletionTypes
.size();
258 CachedCompletionTypes
[QualType(CanUsageType
).getAsString()]
262 CachedResult
.Type
= TypeValue
;
265 CachedCompletionResults
.push_back(CachedResult
);
267 /// Handle nested-name-specifiers in C++.
268 if (TheSema
->Context
.getLangOptions().CPlusPlus
&&
269 IsNestedNameSpecifier
&& !Results
[I
].StartsNestedNameSpecifier
) {
270 // The contexts in which a nested-name-specifier can appear in C++.
272 = (1 << (CodeCompletionContext::CCC_TopLevel
- 1))
273 | (1 << (CodeCompletionContext::CCC_ObjCIvarList
- 1))
274 | (1 << (CodeCompletionContext::CCC_ClassStructUnion
- 1))
275 | (1 << (CodeCompletionContext::CCC_Statement
- 1))
276 | (1 << (CodeCompletionContext::CCC_Expression
- 1))
277 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver
- 1))
278 | (1 << (CodeCompletionContext::CCC_EnumTag
- 1))
279 | (1 << (CodeCompletionContext::CCC_UnionTag
- 1))
280 | (1 << (CodeCompletionContext::CCC_ClassOrStructTag
- 1))
281 | (1 << (CodeCompletionContext::CCC_Type
- 1))
282 | (1 << (CodeCompletionContext::CCC_PotentiallyQualifiedName
- 1))
283 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression
- 1));
285 if (isa
<NamespaceDecl
>(Results
[I
].Declaration
) ||
286 isa
<NamespaceAliasDecl
>(Results
[I
].Declaration
))
287 NNSContexts
|= (1 << (CodeCompletionContext::CCC_Namespace
- 1));
289 if (unsigned RemainingContexts
290 = NNSContexts
& ~CachedResult
.ShowInContexts
) {
291 // If there any contexts where this completion can be a
292 // nested-name-specifier but isn't already an option, create a
293 // nested-name-specifier completion.
294 Results
[I
].StartsNestedNameSpecifier
= true;
295 CachedResult
.Completion
= Results
[I
].CreateCodeCompletionString(*TheSema
);
296 CachedResult
.ShowInContexts
= RemainingContexts
;
297 CachedResult
.Priority
= CCP_NestedNameSpecifier
;
298 CachedResult
.TypeClass
= STC_Void
;
299 CachedResult
.Type
= 0;
300 CachedCompletionResults
.push_back(CachedResult
);
306 case Result::RK_Keyword
:
307 case Result::RK_Pattern
:
308 // Ignore keywords and patterns; we don't care, since they are so
309 // easily regenerated.
312 case Result::RK_Macro
: {
313 CachedCodeCompletionResult CachedResult
;
314 CachedResult
.Completion
= Results
[I
].CreateCodeCompletionString(*TheSema
);
315 CachedResult
.ShowInContexts
316 = (1 << (CodeCompletionContext::CCC_TopLevel
- 1))
317 | (1 << (CodeCompletionContext::CCC_ObjCInterface
- 1))
318 | (1 << (CodeCompletionContext::CCC_ObjCImplementation
- 1))
319 | (1 << (CodeCompletionContext::CCC_ObjCIvarList
- 1))
320 | (1 << (CodeCompletionContext::CCC_ClassStructUnion
- 1))
321 | (1 << (CodeCompletionContext::CCC_Statement
- 1))
322 | (1 << (CodeCompletionContext::CCC_Expression
- 1))
323 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver
- 1))
324 | (1 << (CodeCompletionContext::CCC_MacroNameUse
- 1))
325 | (1 << (CodeCompletionContext::CCC_PreprocessorExpression
- 1))
326 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression
- 1));
329 CachedResult
.Priority
= Results
[I
].Priority
;
330 CachedResult
.Kind
= Results
[I
].CursorKind
;
331 CachedResult
.Availability
= Results
[I
].Availability
;
332 CachedResult
.TypeClass
= STC_Void
;
333 CachedResult
.Type
= 0;
334 CachedCompletionResults
.push_back(CachedResult
);
338 Results
[I
].Destroy();
341 // Make a note of the state when we performed this caching.
342 NumTopLevelDeclsAtLastCompletionCache
= top_level_size();
345 void ASTUnit::ClearCachedCompletionResults() {
346 for (unsigned I
= 0, N
= CachedCompletionResults
.size(); I
!= N
; ++I
)
347 delete CachedCompletionResults
[I
].Completion
;
348 CachedCompletionResults
.clear();
349 CachedCompletionTypes
.clear();
354 /// \brief Gathers information from ASTReader that will be used to initialize
356 class ASTInfoCollector
: public ASTReaderListener
{
357 LangOptions
&LangOpt
;
359 std::string
&TargetTriple
;
360 std::string
&Predefines
;
363 unsigned NumHeaderInfos
;
366 ASTInfoCollector(LangOptions
&LangOpt
, HeaderSearch
&HSI
,
367 std::string
&TargetTriple
, std::string
&Predefines
,
369 : LangOpt(LangOpt
), HSI(HSI
), TargetTriple(TargetTriple
),
370 Predefines(Predefines
), Counter(Counter
), NumHeaderInfos(0) {}
372 virtual bool ReadLanguageOptions(const LangOptions
&LangOpts
) {
377 virtual bool ReadTargetTriple(llvm::StringRef Triple
) {
378 TargetTriple
= Triple
;
382 virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks
&Buffers
,
383 llvm::StringRef OriginalFileName
,
384 std::string
&SuggestedPredefines
) {
385 Predefines
= Buffers
[0].Data
;
386 for (unsigned I
= 1, N
= Buffers
.size(); I
!= N
; ++I
) {
387 Predefines
+= Buffers
[I
].Data
;
392 virtual void ReadHeaderFileInfo(const HeaderFileInfo
&HFI
, unsigned ID
) {
393 HSI
.setHeaderFileInfoForUID(HFI
, NumHeaderInfos
++);
396 virtual void ReadCounter(unsigned Value
) {
401 class StoredDiagnosticClient
: public DiagnosticClient
{
402 llvm::SmallVectorImpl
<StoredDiagnostic
> &StoredDiags
;
405 explicit StoredDiagnosticClient(
406 llvm::SmallVectorImpl
<StoredDiagnostic
> &StoredDiags
)
407 : StoredDiags(StoredDiags
) { }
409 virtual void HandleDiagnostic(Diagnostic::Level Level
,
410 const DiagnosticInfo
&Info
);
413 /// \brief RAII object that optionally captures diagnostics, if
414 /// there is no diagnostic client to capture them already.
415 class CaptureDroppedDiagnostics
{
417 StoredDiagnosticClient Client
;
418 DiagnosticClient
*PreviousClient
;
421 CaptureDroppedDiagnostics(bool RequestCapture
, Diagnostic
&Diags
,
422 llvm::SmallVectorImpl
<StoredDiagnostic
> &StoredDiags
)
423 : Diags(Diags
), Client(StoredDiags
), PreviousClient(0)
425 if (RequestCapture
|| Diags
.getClient() == 0) {
426 PreviousClient
= Diags
.takeClient();
427 Diags
.setClient(&Client
);
431 ~CaptureDroppedDiagnostics() {
432 if (Diags
.getClient() == &Client
) {
434 Diags
.setClient(PreviousClient
);
439 } // anonymous namespace
441 void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level
,
442 const DiagnosticInfo
&Info
) {
443 // Default implementation (Warnings/errors count).
444 DiagnosticClient::HandleDiagnostic(Level
, Info
);
446 StoredDiags
.push_back(StoredDiagnostic(Level
, Info
));
449 const std::string
&ASTUnit::getOriginalSourceFileName() {
450 return OriginalSourceFile
;
453 const std::string
&ASTUnit::getASTFileName() {
454 assert(isMainFileAST() && "Not an ASTUnit from an AST file!");
455 return static_cast<ASTReader
*>(Ctx
->getExternalSource())->getFileName();
458 llvm::MemoryBuffer
*ASTUnit::getBufferForFile(llvm::StringRef Filename
,
459 std::string
*ErrorStr
,
461 return FileMgr
->getBufferForFile(Filename
, FileSystemOpts
,
465 /// \brief Configure the diagnostics object for use with ASTUnit.
466 void ASTUnit::ConfigureDiags(llvm::IntrusiveRefCntPtr
<Diagnostic
> &Diags
,
467 ASTUnit
&AST
, bool CaptureDiagnostics
) {
468 if (!Diags
.getPtr()) {
469 // No diagnostics engine was provided, so create our own diagnostics object
470 // with the default options.
471 DiagnosticOptions DiagOpts
;
472 DiagnosticClient
*Client
= 0;
473 if (CaptureDiagnostics
)
474 Client
= new StoredDiagnosticClient(AST
.StoredDiagnostics
);
475 Diags
= CompilerInstance::createDiagnostics(DiagOpts
, 0, 0, Client
);
476 } else if (CaptureDiagnostics
) {
477 Diags
->setClient(new StoredDiagnosticClient(AST
.StoredDiagnostics
));
481 ASTUnit
*ASTUnit::LoadFromASTFile(const std::string
&Filename
,
482 llvm::IntrusiveRefCntPtr
<Diagnostic
> Diags
,
483 const FileSystemOptions
&FileSystemOpts
,
485 RemappedFile
*RemappedFiles
,
486 unsigned NumRemappedFiles
,
487 bool CaptureDiagnostics
) {
488 llvm::OwningPtr
<ASTUnit
> AST(new ASTUnit(true));
489 ConfigureDiags(Diags
, *AST
, CaptureDiagnostics
);
491 AST
->OnlyLocalDecls
= OnlyLocalDecls
;
492 AST
->CaptureDiagnostics
= CaptureDiagnostics
;
493 AST
->Diagnostics
= Diags
;
494 AST
->FileSystemOpts
= FileSystemOpts
;
495 AST
->FileMgr
.reset(new FileManager(FileSystemOpts
));
496 AST
->SourceMgr
.reset(new SourceManager(AST
->getDiagnostics(),
497 AST
->getFileManager(),
498 AST
->getFileSystemOpts()));
499 AST
->HeaderInfo
.reset(new HeaderSearch(AST
->getFileManager(),
500 AST
->getFileSystemOpts()));
502 for (unsigned I
= 0; I
!= NumRemappedFiles
; ++I
) {
503 // Create the file entry for the file that we're mapping from.
504 const FileEntry
*FromFile
505 = AST
->getFileManager().getVirtualFile(RemappedFiles
[I
].first
,
506 RemappedFiles
[I
].second
->getBufferSize(),
508 AST
->getFileSystemOpts());
510 AST
->getDiagnostics().Report(diag::err_fe_remap_missing_from_file
)
511 << RemappedFiles
[I
].first
;
512 delete RemappedFiles
[I
].second
;
516 // Override the contents of the "from" file with the contents of
518 AST
->getSourceManager().overrideFileContents(FromFile
,
519 RemappedFiles
[I
].second
);
522 // Gather Info for preprocessor construction later on.
524 LangOptions LangInfo
;
525 HeaderSearch
&HeaderInfo
= *AST
->HeaderInfo
.get();
526 std::string TargetTriple
;
527 std::string Predefines
;
530 llvm::OwningPtr
<ASTReader
> Reader
;
532 Reader
.reset(new ASTReader(AST
->getSourceManager(), AST
->getFileManager(),
533 AST
->getFileSystemOpts(), AST
->getDiagnostics()));
534 Reader
->setListener(new ASTInfoCollector(LangInfo
, HeaderInfo
, TargetTriple
,
535 Predefines
, Counter
));
537 switch (Reader
->ReadAST(Filename
, ASTReader::MainFile
)) {
538 case ASTReader::Success
:
541 case ASTReader::Failure
:
542 case ASTReader::IgnorePCH
:
543 AST
->getDiagnostics().Report(diag::err_fe_unable_to_load_pch
);
547 AST
->OriginalSourceFile
= Reader
->getOriginalSourceFile();
549 // AST file loaded successfully. Now create the preprocessor.
551 // Get information about the target being compiled for.
553 // FIXME: This is broken, we should store the TargetOptions in the AST file.
554 TargetOptions TargetOpts
;
556 TargetOpts
.CXXABI
= "";
558 TargetOpts
.Features
.clear();
559 TargetOpts
.Triple
= TargetTriple
;
560 AST
->Target
.reset(TargetInfo::CreateTargetInfo(AST
->getDiagnostics(),
562 AST
->PP
.reset(new Preprocessor(AST
->getDiagnostics(), LangInfo
,
564 AST
->getSourceManager(), HeaderInfo
));
565 Preprocessor
&PP
= *AST
->PP
.get();
567 PP
.setPredefines(Reader
->getSuggestedPredefines());
568 PP
.setCounterValue(Counter
);
569 Reader
->setPreprocessor(PP
);
571 // Create and initialize the ASTContext.
573 AST
->Ctx
.reset(new ASTContext(LangInfo
,
574 AST
->getSourceManager(),
576 PP
.getIdentifierTable(),
577 PP
.getSelectorTable(),
579 /* size_reserve = */0));
580 ASTContext
&Context
= *AST
->Ctx
.get();
582 Reader
->InitializeContext(Context
);
584 // Attach the AST reader to the AST context as an external AST
585 // source, so that declarations will be deserialized from the
586 // AST file as needed.
587 ASTReader
*ReaderPtr
= Reader
.get();
588 llvm::OwningPtr
<ExternalASTSource
> Source(Reader
.take());
589 Context
.setExternalSource(Source
);
591 // Create an AST consumer, even though it isn't used.
592 AST
->Consumer
.reset(new ASTConsumer
);
594 // Create a semantic analysis object and tell the AST reader about it.
595 AST
->TheSema
.reset(new Sema(PP
, Context
, *AST
->Consumer
));
596 AST
->TheSema
->Initialize();
597 ReaderPtr
->InitializeSema(*AST
->TheSema
);
604 class TopLevelDeclTrackerConsumer
: public ASTConsumer
{
608 TopLevelDeclTrackerConsumer(ASTUnit
&_Unit
) : Unit(_Unit
) {}
610 void HandleTopLevelDecl(DeclGroupRef D
) {
611 for (DeclGroupRef::iterator it
= D
.begin(), ie
= D
.end(); it
!= ie
; ++it
) {
613 // FIXME: Currently ObjC method declarations are incorrectly being
614 // reported as top-level declarations, even though their DeclContext
615 // is the containing ObjC @interface/@implementation. This is a
616 // fundamental problem in the parser right now.
617 if (isa
<ObjCMethodDecl
>(D
))
619 Unit
.addTopLevelDecl(D
);
623 // We're not interested in "interesting" decls.
624 void HandleInterestingDecl(DeclGroupRef
) {}
627 class TopLevelDeclTrackerAction
: public ASTFrontendAction
{
631 virtual ASTConsumer
*CreateASTConsumer(CompilerInstance
&CI
,
632 llvm::StringRef InFile
) {
633 return new TopLevelDeclTrackerConsumer(Unit
);
637 TopLevelDeclTrackerAction(ASTUnit
&_Unit
) : Unit(_Unit
) {}
639 virtual bool hasCodeCompletionSupport() const { return false; }
640 virtual bool usesCompleteTranslationUnit() {
641 return Unit
.isCompleteTranslationUnit();
645 class PrecompilePreambleConsumer
: public PCHGenerator
{
647 std::vector
<Decl
*> TopLevelDecls
;
650 PrecompilePreambleConsumer(ASTUnit
&Unit
,
651 const Preprocessor
&PP
, bool Chaining
,
652 const char *isysroot
, llvm::raw_ostream
*Out
)
653 : PCHGenerator(PP
, Chaining
, isysroot
, Out
), Unit(Unit
) { }
655 virtual void HandleTopLevelDecl(DeclGroupRef D
) {
656 for (DeclGroupRef::iterator it
= D
.begin(), ie
= D
.end(); it
!= ie
; ++it
) {
658 // FIXME: Currently ObjC method declarations are incorrectly being
659 // reported as top-level declarations, even though their DeclContext
660 // is the containing ObjC @interface/@implementation. This is a
661 // fundamental problem in the parser right now.
662 if (isa
<ObjCMethodDecl
>(D
))
664 TopLevelDecls
.push_back(D
);
668 virtual void HandleTranslationUnit(ASTContext
&Ctx
) {
669 PCHGenerator::HandleTranslationUnit(Ctx
);
670 if (!Unit
.getDiagnostics().hasErrorOccurred()) {
671 // Translate the top-level declarations we captured during
672 // parsing into declaration IDs in the precompiled
673 // preamble. This will allow us to deserialize those top-level
674 // declarations when requested.
675 for (unsigned I
= 0, N
= TopLevelDecls
.size(); I
!= N
; ++I
)
676 Unit
.addTopLevelDeclFromPreamble(
677 getWriter().getDeclID(TopLevelDecls
[I
]));
682 class PrecompilePreambleAction
: public ASTFrontendAction
{
686 explicit PrecompilePreambleAction(ASTUnit
&Unit
) : Unit(Unit
) {}
688 virtual ASTConsumer
*CreateASTConsumer(CompilerInstance
&CI
,
689 llvm::StringRef InFile
) {
691 llvm::raw_ostream
*OS
= 0;
693 if (GeneratePCHAction::ComputeASTConsumerArguments(CI
, InFile
, Sysroot
,
697 const char *isysroot
= CI
.getFrontendOpts().RelocatablePCH
?
699 return new PrecompilePreambleConsumer(Unit
, CI
.getPreprocessor(), Chaining
,
703 virtual bool hasCodeCompletionSupport() const { return false; }
704 virtual bool hasASTFileSupport() const { return false; }
705 virtual bool usesCompleteTranslationUnit() { return false; }
710 /// Parse the source file into a translation unit using the given compiler
711 /// invocation, replacing the current translation unit.
713 /// \returns True if a failure occurred that causes the ASTUnit not to
714 /// contain any translation-unit information, false otherwise.
715 bool ASTUnit::Parse(llvm::MemoryBuffer
*OverrideMainBuffer
) {
716 delete SavedMainFileBuffer
;
717 SavedMainFileBuffer
= 0;
719 if (!Invocation
.get()) {
720 delete OverrideMainBuffer
;
724 // Create the compiler instance to use for building the AST.
725 CompilerInstance Clang
;
726 Clang
.setInvocation(Invocation
.take());
727 OriginalSourceFile
= Clang
.getFrontendOpts().Inputs
[0].second
;
729 // Set up diagnostics, capturing any diagnostics that would
730 // otherwise be dropped.
731 Clang
.setDiagnostics(&getDiagnostics());
733 // Create the target instance.
734 Clang
.setTarget(TargetInfo::CreateTargetInfo(Clang
.getDiagnostics(),
735 Clang
.getTargetOpts()));
736 if (!Clang
.hasTarget()) {
737 delete OverrideMainBuffer
;
741 // Inform the target of the language options.
743 // FIXME: We shouldn't need to do this, the target should be immutable once
744 // created. This complexity should be lifted elsewhere.
745 Clang
.getTarget().setForcedLangOptions(Clang
.getLangOpts());
747 assert(Clang
.getFrontendOpts().Inputs
.size() == 1 &&
748 "Invocation must have exactly one source file!");
749 assert(Clang
.getFrontendOpts().Inputs
[0].first
!= IK_AST
&&
750 "FIXME: AST inputs not yet supported here!");
751 assert(Clang
.getFrontendOpts().Inputs
[0].first
!= IK_LLVM_IR
&&
752 "IR inputs not support here!");
754 // Configure the various subsystems.
755 // FIXME: Should we retain the previous file manager?
756 FileMgr
.reset(new FileManager(Clang
.getFileSystemOpts()));
757 FileSystemOpts
= Clang
.getFileSystemOpts();
758 SourceMgr
.reset(new SourceManager(getDiagnostics(), *FileMgr
, FileSystemOpts
));
763 // Clear out old caches and data.
764 TopLevelDecls
.clear();
765 CleanTemporaryFiles();
766 PreprocessedEntitiesByFile
.clear();
768 if (!OverrideMainBuffer
) {
769 StoredDiagnostics
.erase(
770 StoredDiagnostics
.begin() + NumStoredDiagnosticsFromDriver
,
771 StoredDiagnostics
.end());
772 TopLevelDeclsInPreamble
.clear();
775 // Create a file manager object to provide access to and cache the filesystem.
776 Clang
.setFileManager(&getFileManager());
778 // Create the source manager.
779 Clang
.setSourceManager(&getSourceManager());
781 // If the main file has been overridden due to the use of a preamble,
782 // make that override happen and introduce the preamble.
783 PreprocessorOptions
&PreprocessorOpts
= Clang
.getPreprocessorOpts();
784 std::string PriorImplicitPCHInclude
;
785 if (OverrideMainBuffer
) {
786 PreprocessorOpts
.addRemappedFile(OriginalSourceFile
, OverrideMainBuffer
);
787 PreprocessorOpts
.PrecompiledPreambleBytes
.first
= Preamble
.size();
788 PreprocessorOpts
.PrecompiledPreambleBytes
.second
789 = PreambleEndsAtStartOfLine
;
790 PriorImplicitPCHInclude
= PreprocessorOpts
.ImplicitPCHInclude
;
791 PreprocessorOpts
.ImplicitPCHInclude
= PreambleFile
;
792 PreprocessorOpts
.DisablePCHValidation
= true;
794 // The stored diagnostic has the old source manager in it; update
795 // the locations to refer into the new source manager. Since we've
796 // been careful to make sure that the source manager's state
797 // before and after are identical, so that we can reuse the source
799 for (unsigned I
= NumStoredDiagnosticsFromDriver
,
800 N
= StoredDiagnostics
.size();
802 FullSourceLoc
Loc(StoredDiagnostics
[I
].getLocation(),
804 StoredDiagnostics
[I
].setLocation(Loc
);
807 // Keep track of the override buffer;
808 SavedMainFileBuffer
= OverrideMainBuffer
;
810 PreprocessorOpts
.PrecompiledPreambleBytes
.first
= 0;
811 PreprocessorOpts
.PrecompiledPreambleBytes
.second
= false;
814 llvm::OwningPtr
<TopLevelDeclTrackerAction
> Act
;
815 Act
.reset(new TopLevelDeclTrackerAction(*this));
816 if (!Act
->BeginSourceFile(Clang
, Clang
.getFrontendOpts().Inputs
[0].second
,
817 Clang
.getFrontendOpts().Inputs
[0].first
))
822 // Steal the created target, context, and preprocessor, and take back the
823 // source and file managers.
824 TheSema
.reset(Clang
.takeSema());
825 Consumer
.reset(Clang
.takeASTConsumer());
826 Ctx
.reset(Clang
.takeASTContext());
827 PP
.reset(Clang
.takePreprocessor());
828 Clang
.takeSourceManager();
829 Clang
.takeFileManager();
830 Target
.reset(Clang
.takeTarget());
832 Act
->EndSourceFile();
834 // Remove the overridden buffer we used for the preamble.
835 if (OverrideMainBuffer
) {
836 PreprocessorOpts
.eraseRemappedFile(
837 PreprocessorOpts
.remapped_file_buffer_end() - 1);
838 PreprocessorOpts
.ImplicitPCHInclude
= PriorImplicitPCHInclude
;
841 Invocation
.reset(Clang
.takeInvocation());
845 // Remove the overridden buffer we used for the preamble.
846 if (OverrideMainBuffer
) {
847 PreprocessorOpts
.eraseRemappedFile(
848 PreprocessorOpts
.remapped_file_buffer_end() - 1);
849 PreprocessorOpts
.ImplicitPCHInclude
= PriorImplicitPCHInclude
;
850 delete OverrideMainBuffer
;
851 SavedMainFileBuffer
= 0;
854 StoredDiagnostics
.clear();
855 Clang
.takeSourceManager();
856 Clang
.takeFileManager();
857 Invocation
.reset(Clang
.takeInvocation());
861 /// \brief Simple function to retrieve a path for a preamble precompiled header.
862 static std::string
GetPreamblePCHPath() {
863 // FIXME: This is lame; sys::Path should provide this function (in particular,
864 // it should know how to find the temporary files dir).
865 // FIXME: This is really lame. I copied this code from the Driver!
866 // FIXME: This is a hack so that we can override the preamble file during
867 // crash-recovery testing, which is the only case where the preamble files
868 // are not necessarily cleaned up.
869 const char *TmpFile
= ::getenv("CINDEXTEST_PREAMBLE_FILE");
874 const char *TmpDir
= ::getenv("TMPDIR");
876 TmpDir
= ::getenv("TEMP");
878 TmpDir
= ::getenv("TMP");
881 TmpDir
= ::getenv("USERPROFILE");
885 llvm::sys::Path
P(TmpDir
);
886 P
.createDirectoryOnDisk(true);
887 P
.appendComponent("preamble");
888 P
.appendSuffix("pch");
889 if (P
.createTemporaryFileOnDisk())
890 return std::string();
895 /// \brief Compute the preamble for the main file, providing the source buffer
896 /// that corresponds to the main file along with a pair (bytes, start-of-line)
897 /// that describes the preamble.
898 std::pair
<llvm::MemoryBuffer
*, std::pair
<unsigned, bool> >
899 ASTUnit::ComputePreamble(CompilerInvocation
&Invocation
,
900 unsigned MaxLines
, bool &CreatedBuffer
) {
901 FrontendOptions
&FrontendOpts
= Invocation
.getFrontendOpts();
902 PreprocessorOptions
&PreprocessorOpts
903 = Invocation
.getPreprocessorOpts();
904 CreatedBuffer
= false;
906 // Try to determine if the main file has been remapped, either from the
907 // command line (to another file) or directly through the compiler invocation
908 // (to a memory buffer).
909 llvm::MemoryBuffer
*Buffer
= 0;
910 llvm::sys::PathWithStatus
MainFilePath(FrontendOpts
.Inputs
[0].second
);
911 if (const llvm::sys::FileStatus
*MainFileStatus
= MainFilePath
.getFileStatus()) {
912 // Check whether there is a file-file remapping of the main file
913 for (PreprocessorOptions::remapped_file_iterator
914 M
= PreprocessorOpts
.remapped_file_begin(),
915 E
= PreprocessorOpts
.remapped_file_end();
918 llvm::sys::PathWithStatus
MPath(M
->first
);
919 if (const llvm::sys::FileStatus
*MStatus
= MPath
.getFileStatus()) {
920 if (MainFileStatus
->uniqueID
== MStatus
->uniqueID
) {
921 // We found a remapping. Try to load the resulting, remapped source.
924 CreatedBuffer
= false;
927 Buffer
= getBufferForFile(M
->second
);
929 return std::make_pair((llvm::MemoryBuffer
*)0,
930 std::make_pair(0, true));
931 CreatedBuffer
= true;
936 // Check whether there is a file-buffer remapping. It supercedes the
937 // file-file remapping.
938 for (PreprocessorOptions::remapped_file_buffer_iterator
939 M
= PreprocessorOpts
.remapped_file_buffer_begin(),
940 E
= PreprocessorOpts
.remapped_file_buffer_end();
943 llvm::sys::PathWithStatus
MPath(M
->first
);
944 if (const llvm::sys::FileStatus
*MStatus
= MPath
.getFileStatus()) {
945 if (MainFileStatus
->uniqueID
== MStatus
->uniqueID
) {
946 // We found a remapping.
949 CreatedBuffer
= false;
952 Buffer
= const_cast<llvm::MemoryBuffer
*>(M
->second
);
958 // If the main source file was not remapped, load it now.
960 Buffer
= getBufferForFile(FrontendOpts
.Inputs
[0].second
);
962 return std::make_pair((llvm::MemoryBuffer
*)0, std::make_pair(0, true));
964 CreatedBuffer
= true;
967 return std::make_pair(Buffer
, Lexer::ComputePreamble(Buffer
, MaxLines
));
970 static llvm::MemoryBuffer
*CreatePaddedMainFileBuffer(llvm::MemoryBuffer
*Old
,
972 llvm::StringRef NewName
) {
973 llvm::MemoryBuffer
*Result
974 = llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize
, NewName
);
975 memcpy(const_cast<char*>(Result
->getBufferStart()),
976 Old
->getBufferStart(), Old
->getBufferSize());
977 memset(const_cast<char*>(Result
->getBufferStart()) + Old
->getBufferSize(),
978 ' ', NewSize
- Old
->getBufferSize() - 1);
979 const_cast<char*>(Result
->getBufferEnd())[-1] = '\n';
984 /// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing
987 /// This routine will compute the preamble of the main source file. If a
988 /// non-trivial preamble is found, it will precompile that preamble into a
989 /// precompiled header so that the precompiled preamble can be used to reduce
990 /// reparsing time. If a precompiled preamble has already been constructed,
991 /// this routine will determine if it is still valid and, if so, avoid
992 /// rebuilding the precompiled preamble.
994 /// \param AllowRebuild When true (the default), this routine is
995 /// allowed to rebuild the precompiled preamble if it is found to be
998 /// \param MaxLines When non-zero, the maximum number of lines that
999 /// can occur within the preamble.
1001 /// \returns If the precompiled preamble can be used, returns a newly-allocated
1002 /// buffer that should be used in place of the main file when doing so.
1003 /// Otherwise, returns a NULL pointer.
1004 llvm::MemoryBuffer
*ASTUnit::getMainBufferWithPrecompiledPreamble(
1005 CompilerInvocation PreambleInvocation
,
1007 unsigned MaxLines
) {
1008 FrontendOptions
&FrontendOpts
= PreambleInvocation
.getFrontendOpts();
1009 PreprocessorOptions
&PreprocessorOpts
1010 = PreambleInvocation
.getPreprocessorOpts();
1012 bool CreatedPreambleBuffer
= false;
1013 std::pair
<llvm::MemoryBuffer
*, std::pair
<unsigned, bool> > NewPreamble
1014 = ComputePreamble(PreambleInvocation
, MaxLines
, CreatedPreambleBuffer
);
1016 // If ComputePreamble() Take ownership of the
1017 llvm::OwningPtr
<llvm::MemoryBuffer
> OwnedPreambleBuffer
;
1018 if (CreatedPreambleBuffer
)
1019 OwnedPreambleBuffer
.reset(NewPreamble
.first
);
1021 if (!NewPreamble
.second
.first
) {
1022 // We couldn't find a preamble in the main source. Clear out the current
1023 // preamble, if we have one. It's obviously no good any more.
1025 if (!PreambleFile
.empty()) {
1026 llvm::sys::Path(PreambleFile
).eraseFromDisk();
1027 PreambleFile
.clear();
1030 // The next time we actually see a preamble, precompile it.
1031 PreambleRebuildCounter
= 1;
1035 if (!Preamble
.empty()) {
1036 // We've previously computed a preamble. Check whether we have the same
1037 // preamble now that we did before, and that there's enough space in
1038 // the main-file buffer within the precompiled preamble to fit the
1040 if (Preamble
.size() == NewPreamble
.second
.first
&&
1041 PreambleEndsAtStartOfLine
== NewPreamble
.second
.second
&&
1042 NewPreamble
.first
->getBufferSize() < PreambleReservedSize
-2 &&
1043 memcmp(&Preamble
[0], NewPreamble
.first
->getBufferStart(),
1044 NewPreamble
.second
.first
) == 0) {
1045 // The preamble has not changed. We may be able to re-use the precompiled
1048 // Check that none of the files used by the preamble have changed.
1049 bool AnyFileChanged
= false;
1051 // First, make a record of those files that have been overridden via
1052 // remapping or unsaved_files.
1053 llvm::StringMap
<std::pair
<off_t
, time_t> > OverriddenFiles
;
1054 for (PreprocessorOptions::remapped_file_iterator
1055 R
= PreprocessorOpts
.remapped_file_begin(),
1056 REnd
= PreprocessorOpts
.remapped_file_end();
1057 !AnyFileChanged
&& R
!= REnd
;
1059 struct stat StatBuf
;
1060 if (stat(R
->second
.c_str(), &StatBuf
)) {
1061 // If we can't stat the file we're remapping to, assume that something
1062 // horrible happened.
1063 AnyFileChanged
= true;
1067 OverriddenFiles
[R
->first
] = std::make_pair(StatBuf
.st_size
,
1070 for (PreprocessorOptions::remapped_file_buffer_iterator
1071 R
= PreprocessorOpts
.remapped_file_buffer_begin(),
1072 REnd
= PreprocessorOpts
.remapped_file_buffer_end();
1073 !AnyFileChanged
&& R
!= REnd
;
1075 // FIXME: Should we actually compare the contents of file->buffer
1077 OverriddenFiles
[R
->first
] = std::make_pair(R
->second
->getBufferSize(),
1081 // Check whether anything has changed.
1082 for (llvm::StringMap
<std::pair
<off_t
, time_t> >::iterator
1083 F
= FilesInPreamble
.begin(), FEnd
= FilesInPreamble
.end();
1084 !AnyFileChanged
&& F
!= FEnd
;
1086 llvm::StringMap
<std::pair
<off_t
, time_t> >::iterator Overridden
1087 = OverriddenFiles
.find(F
->first());
1088 if (Overridden
!= OverriddenFiles
.end()) {
1089 // This file was remapped; check whether the newly-mapped file
1090 // matches up with the previous mapping.
1091 if (Overridden
->second
!= F
->second
)
1092 AnyFileChanged
= true;
1096 // The file was not remapped; check whether it has changed on disk.
1097 struct stat StatBuf
;
1098 if (stat(F
->first(), &StatBuf
)) {
1099 // If we can't stat the file, assume that something horrible happened.
1100 AnyFileChanged
= true;
1101 } else if (StatBuf
.st_size
!= F
->second
.first
||
1102 StatBuf
.st_mtime
!= F
->second
.second
)
1103 AnyFileChanged
= true;
1106 if (!AnyFileChanged
) {
1107 // Okay! We can re-use the precompiled preamble.
1109 // Set the state of the diagnostic object to mimic its state
1110 // after parsing the preamble.
1111 // FIXME: This won't catch any #pragma push warning changes that
1112 // have occurred in the preamble.
1113 getDiagnostics().Reset();
1114 ProcessWarningOptions(getDiagnostics(),
1115 PreambleInvocation
.getDiagnosticOpts());
1116 getDiagnostics().setNumWarnings(NumWarningsInPreamble
);
1117 if (StoredDiagnostics
.size() > NumStoredDiagnosticsInPreamble
)
1118 StoredDiagnostics
.erase(
1119 StoredDiagnostics
.begin() + NumStoredDiagnosticsInPreamble
,
1120 StoredDiagnostics
.end());
1122 // Create a version of the main file buffer that is padded to
1123 // buffer size we reserved when creating the preamble.
1124 return CreatePaddedMainFileBuffer(NewPreamble
.first
,
1125 PreambleReservedSize
,
1126 FrontendOpts
.Inputs
[0].second
);
1130 // If we aren't allowed to rebuild the precompiled preamble, just
1135 // We can't reuse the previously-computed preamble. Build a new one.
1137 llvm::sys::Path(PreambleFile
).eraseFromDisk();
1138 PreambleRebuildCounter
= 1;
1139 } else if (!AllowRebuild
) {
1140 // We aren't allowed to rebuild the precompiled preamble; just
1145 // If the preamble rebuild counter > 1, it's because we previously
1146 // failed to build a preamble and we're not yet ready to try
1147 // again. Decrement the counter and return a failure.
1148 if (PreambleRebuildCounter
> 1) {
1149 --PreambleRebuildCounter
;
1153 // Create a temporary file for the precompiled preamble. In rare
1154 // circumstances, this can fail.
1155 std::string PreamblePCHPath
= GetPreamblePCHPath();
1156 if (PreamblePCHPath
.empty()) {
1157 // Try again next time.
1158 PreambleRebuildCounter
= 1;
1162 // We did not previously compute a preamble, or it can't be reused anyway.
1163 SimpleTimer
PreambleTimer(WantTiming
);
1164 PreambleTimer
.setOutput("Precompiling preamble");
1166 // Create a new buffer that stores the preamble. The buffer also contains
1167 // extra space for the original contents of the file (which will be present
1168 // when we actually parse the file) along with more room in case the file
1170 PreambleReservedSize
= NewPreamble
.first
->getBufferSize();
1171 if (PreambleReservedSize
< 4096)
1172 PreambleReservedSize
= 8191;
1174 PreambleReservedSize
*= 2;
1176 // Save the preamble text for later; we'll need to compare against it for
1177 // subsequent reparses.
1178 Preamble
.assign(NewPreamble
.first
->getBufferStart(),
1179 NewPreamble
.first
->getBufferStart()
1180 + NewPreamble
.second
.first
);
1181 PreambleEndsAtStartOfLine
= NewPreamble
.second
.second
;
1183 delete PreambleBuffer
;
1185 = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize
,
1186 FrontendOpts
.Inputs
[0].second
);
1187 memcpy(const_cast<char*>(PreambleBuffer
->getBufferStart()),
1188 NewPreamble
.first
->getBufferStart(), Preamble
.size());
1189 memset(const_cast<char*>(PreambleBuffer
->getBufferStart()) + Preamble
.size(),
1190 ' ', PreambleReservedSize
- Preamble
.size() - 1);
1191 const_cast<char*>(PreambleBuffer
->getBufferEnd())[-1] = '\n';
1193 // Remap the main source file to the preamble buffer.
1194 llvm::sys::PathWithStatus
MainFilePath(FrontendOpts
.Inputs
[0].second
);
1195 PreprocessorOpts
.addRemappedFile(MainFilePath
.str(), PreambleBuffer
);
1197 // Tell the compiler invocation to generate a temporary precompiled header.
1198 FrontendOpts
.ProgramAction
= frontend::GeneratePCH
;
1199 FrontendOpts
.ChainedPCH
= true;
1200 // FIXME: Generate the precompiled header into memory?
1201 FrontendOpts
.OutputFile
= PreamblePCHPath
;
1202 PreprocessorOpts
.PrecompiledPreambleBytes
.first
= 0;
1203 PreprocessorOpts
.PrecompiledPreambleBytes
.second
= false;
1205 // Create the compiler instance to use for building the precompiled preamble.
1206 CompilerInstance Clang
;
1207 Clang
.setInvocation(&PreambleInvocation
);
1208 OriginalSourceFile
= Clang
.getFrontendOpts().Inputs
[0].second
;
1210 // Set up diagnostics, capturing all of the diagnostics produced.
1211 Clang
.setDiagnostics(&getDiagnostics());
1213 // Create the target instance.
1214 Clang
.setTarget(TargetInfo::CreateTargetInfo(Clang
.getDiagnostics(),
1215 Clang
.getTargetOpts()));
1216 if (!Clang
.hasTarget()) {
1217 llvm::sys::Path(FrontendOpts
.OutputFile
).eraseFromDisk();
1219 PreambleRebuildCounter
= DefaultPreambleRebuildInterval
;
1220 PreprocessorOpts
.eraseRemappedFile(
1221 PreprocessorOpts
.remapped_file_buffer_end() - 1);
1225 // Inform the target of the language options.
1227 // FIXME: We shouldn't need to do this, the target should be immutable once
1228 // created. This complexity should be lifted elsewhere.
1229 Clang
.getTarget().setForcedLangOptions(Clang
.getLangOpts());
1231 assert(Clang
.getFrontendOpts().Inputs
.size() == 1 &&
1232 "Invocation must have exactly one source file!");
1233 assert(Clang
.getFrontendOpts().Inputs
[0].first
!= IK_AST
&&
1234 "FIXME: AST inputs not yet supported here!");
1235 assert(Clang
.getFrontendOpts().Inputs
[0].first
!= IK_LLVM_IR
&&
1236 "IR inputs not support here!");
1238 // Clear out old caches and data.
1239 getDiagnostics().Reset();
1240 ProcessWarningOptions(getDiagnostics(), Clang
.getDiagnosticOpts());
1241 StoredDiagnostics
.erase(
1242 StoredDiagnostics
.begin() + NumStoredDiagnosticsFromDriver
,
1243 StoredDiagnostics
.end());
1244 TopLevelDecls
.clear();
1245 TopLevelDeclsInPreamble
.clear();
1247 // Create a file manager object to provide access to and cache the filesystem.
1248 Clang
.setFileManager(new FileManager(Clang
.getFileSystemOpts()));
1250 // Create the source manager.
1251 Clang
.setSourceManager(new SourceManager(getDiagnostics(),
1252 Clang
.getFileManager(),
1253 Clang
.getFileSystemOpts()));
1255 llvm::OwningPtr
<PrecompilePreambleAction
> Act
;
1256 Act
.reset(new PrecompilePreambleAction(*this));
1257 if (!Act
->BeginSourceFile(Clang
, Clang
.getFrontendOpts().Inputs
[0].second
,
1258 Clang
.getFrontendOpts().Inputs
[0].first
)) {
1259 Clang
.takeInvocation();
1260 llvm::sys::Path(FrontendOpts
.OutputFile
).eraseFromDisk();
1262 PreambleRebuildCounter
= DefaultPreambleRebuildInterval
;
1263 PreprocessorOpts
.eraseRemappedFile(
1264 PreprocessorOpts
.remapped_file_buffer_end() - 1);
1269 Act
->EndSourceFile();
1270 Clang
.takeInvocation();
1272 if (Diagnostics
->hasErrorOccurred()) {
1273 // There were errors parsing the preamble, so no precompiled header was
1274 // generated. Forget that we even tried.
1275 // FIXME: Should we leave a note for ourselves to try again?
1276 llvm::sys::Path(FrontendOpts
.OutputFile
).eraseFromDisk();
1278 TopLevelDeclsInPreamble
.clear();
1279 PreambleRebuildCounter
= DefaultPreambleRebuildInterval
;
1280 PreprocessorOpts
.eraseRemappedFile(
1281 PreprocessorOpts
.remapped_file_buffer_end() - 1);
1285 // Keep track of the preamble we precompiled.
1286 PreambleFile
= FrontendOpts
.OutputFile
;
1287 NumStoredDiagnosticsInPreamble
= StoredDiagnostics
.size();
1288 NumWarningsInPreamble
= getDiagnostics().getNumWarnings();
1290 // Keep track of all of the files that the source manager knows about,
1291 // so we can verify whether they have changed or not.
1292 FilesInPreamble
.clear();
1293 SourceManager
&SourceMgr
= Clang
.getSourceManager();
1294 const llvm::MemoryBuffer
*MainFileBuffer
1295 = SourceMgr
.getBuffer(SourceMgr
.getMainFileID());
1296 for (SourceManager::fileinfo_iterator F
= SourceMgr
.fileinfo_begin(),
1297 FEnd
= SourceMgr
.fileinfo_end();
1300 const FileEntry
*File
= F
->second
->Entry
;
1301 if (!File
|| F
->second
->getRawBuffer() == MainFileBuffer
)
1304 FilesInPreamble
[File
->getName()]
1305 = std::make_pair(F
->second
->getSize(), File
->getModificationTime());
1308 PreambleRebuildCounter
= 1;
1309 PreprocessorOpts
.eraseRemappedFile(
1310 PreprocessorOpts
.remapped_file_buffer_end() - 1);
1311 return CreatePaddedMainFileBuffer(NewPreamble
.first
,
1312 PreambleReservedSize
,
1313 FrontendOpts
.Inputs
[0].second
);
1316 void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1317 std::vector
<Decl
*> Resolved
;
1318 Resolved
.reserve(TopLevelDeclsInPreamble
.size());
1319 ExternalASTSource
&Source
= *getASTContext().getExternalSource();
1320 for (unsigned I
= 0, N
= TopLevelDeclsInPreamble
.size(); I
!= N
; ++I
) {
1321 // Resolve the declaration ID to an actual declaration, possibly
1322 // deserializing the declaration in the process.
1323 Decl
*D
= Source
.GetExternalDecl(TopLevelDeclsInPreamble
[I
]);
1325 Resolved
.push_back(D
);
1327 TopLevelDeclsInPreamble
.clear();
1328 TopLevelDecls
.insert(TopLevelDecls
.begin(), Resolved
.begin(), Resolved
.end());
1331 unsigned ASTUnit::getMaxPCHLevel() const {
1332 if (!getOnlyLocalDecls())
1333 return Decl::MaxPCHLevel
;
1338 llvm::StringRef
ASTUnit::getMainFileName() const {
1339 return Invocation
->getFrontendOpts().Inputs
[0].second
;
1342 bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble
) {
1346 // We'll manage file buffers ourselves.
1347 Invocation
->getPreprocessorOpts().RetainRemappedFileBuffers
= true;
1348 Invocation
->getFrontendOpts().DisableFree
= false;
1350 llvm::MemoryBuffer
*OverrideMainBuffer
= 0;
1351 if (PrecompilePreamble
) {
1352 PreambleRebuildCounter
= 2;
1354 = getMainBufferWithPrecompiledPreamble(*Invocation
);
1357 SimpleTimer
ParsingTimer(WantTiming
);
1358 ParsingTimer
.setOutput("Parsing " + getMainFileName());
1360 return Parse(OverrideMainBuffer
);
1363 ASTUnit
*ASTUnit::LoadFromCompilerInvocation(CompilerInvocation
*CI
,
1364 llvm::IntrusiveRefCntPtr
<Diagnostic
> Diags
,
1365 bool OnlyLocalDecls
,
1366 bool CaptureDiagnostics
,
1367 bool PrecompilePreamble
,
1368 bool CompleteTranslationUnit
,
1369 bool CacheCodeCompletionResults
) {
1370 // Create the AST unit.
1371 llvm::OwningPtr
<ASTUnit
> AST
;
1372 AST
.reset(new ASTUnit(false));
1373 ConfigureDiags(Diags
, *AST
, CaptureDiagnostics
);
1374 AST
->Diagnostics
= Diags
;
1375 AST
->OnlyLocalDecls
= OnlyLocalDecls
;
1376 AST
->CaptureDiagnostics
= CaptureDiagnostics
;
1377 AST
->CompleteTranslationUnit
= CompleteTranslationUnit
;
1378 AST
->ShouldCacheCodeCompletionResults
= CacheCodeCompletionResults
;
1379 AST
->CacheCodeCompletionCoolDown
= 1;
1380 AST
->Invocation
.reset(CI
);
1382 return AST
->LoadFromCompilerInvocation(PrecompilePreamble
)? 0 : AST
.take();
1385 ASTUnit
*ASTUnit::LoadFromCommandLine(const char **ArgBegin
,
1386 const char **ArgEnd
,
1387 llvm::IntrusiveRefCntPtr
<Diagnostic
> Diags
,
1388 llvm::StringRef ResourceFilesPath
,
1389 bool OnlyLocalDecls
,
1390 bool CaptureDiagnostics
,
1391 RemappedFile
*RemappedFiles
,
1392 unsigned NumRemappedFiles
,
1393 bool PrecompilePreamble
,
1394 bool CompleteTranslationUnit
,
1395 bool CacheCodeCompletionResults
,
1396 bool CXXPrecompilePreamble
,
1397 bool CXXChainedPCH
) {
1398 if (!Diags
.getPtr()) {
1399 // No diagnostics engine was provided, so create our own diagnostics object
1400 // with the default options.
1401 DiagnosticOptions DiagOpts
;
1402 Diags
= CompilerInstance::createDiagnostics(DiagOpts
, 0, 0);
1405 llvm::SmallVector
<const char *, 16> Args
;
1406 Args
.push_back("<clang>"); // FIXME: Remove dummy argument.
1407 Args
.insert(Args
.end(), ArgBegin
, ArgEnd
);
1409 // FIXME: Find a cleaner way to force the driver into restricted modes. We
1410 // also want to force it to use clang.
1411 Args
.push_back("-fsyntax-only");
1413 llvm::SmallVector
<StoredDiagnostic
, 4> StoredDiagnostics
;
1415 llvm::OwningPtr
<CompilerInvocation
> CI
;
1418 CaptureDroppedDiagnostics
Capture(CaptureDiagnostics
, *Diags
,
1421 // FIXME: We shouldn't have to pass in the path info.
1422 driver::Driver
TheDriver("clang", llvm::sys::getHostTriple(),
1423 "a.out", false, false, *Diags
);
1425 // Don't check that inputs exist, they have been remapped.
1426 TheDriver
.setCheckInputsExist(false);
1428 llvm::OwningPtr
<driver::Compilation
> C(
1429 TheDriver
.BuildCompilation(Args
.size(), Args
.data()));
1431 // We expect to get back exactly one command job, if we didn't something
1433 const driver::JobList
&Jobs
= C
->getJobs();
1434 if (Jobs
.size() != 1 || !isa
<driver::Command
>(Jobs
.begin())) {
1435 llvm::SmallString
<256> Msg
;
1436 llvm::raw_svector_ostream
OS(Msg
);
1437 C
->PrintJob(OS
, C
->getJobs(), "; ", true);
1438 Diags
->Report(diag::err_fe_expected_compiler_job
) << OS
.str();
1442 const driver::Command
*Cmd
= cast
<driver::Command
>(*Jobs
.begin());
1443 if (llvm::StringRef(Cmd
->getCreator().getName()) != "clang") {
1444 Diags
->Report(diag::err_fe_expected_clang_command
);
1448 const driver::ArgStringList
&CCArgs
= Cmd
->getArguments();
1449 CI
.reset(new CompilerInvocation
);
1450 CompilerInvocation::CreateFromArgs(*CI
,
1451 const_cast<const char **>(CCArgs
.data()),
1452 const_cast<const char **>(CCArgs
.data()) +
1457 // Override any files that need remapping
1458 for (unsigned I
= 0; I
!= NumRemappedFiles
; ++I
)
1459 CI
->getPreprocessorOpts().addRemappedFile(RemappedFiles
[I
].first
,
1460 RemappedFiles
[I
].second
);
1462 // Override the resources path.
1463 CI
->getHeaderSearchOpts().ResourceDir
= ResourceFilesPath
;
1465 // Check whether we should precompile the preamble and/or use chained PCH.
1466 // FIXME: This is a temporary hack while we debug C++ chained PCH.
1467 if (CI
->getLangOpts().CPlusPlus
) {
1468 PrecompilePreamble
= PrecompilePreamble
&& CXXPrecompilePreamble
;
1470 if (PrecompilePreamble
&& !CXXChainedPCH
&&
1471 !CI
->getPreprocessorOpts().ImplicitPCHInclude
.empty())
1472 PrecompilePreamble
= false;
1475 // Create the AST unit.
1476 llvm::OwningPtr
<ASTUnit
> AST
;
1477 AST
.reset(new ASTUnit(false));
1478 ConfigureDiags(Diags
, *AST
, CaptureDiagnostics
);
1479 AST
->Diagnostics
= Diags
;
1480 AST
->OnlyLocalDecls
= OnlyLocalDecls
;
1481 AST
->CaptureDiagnostics
= CaptureDiagnostics
;
1482 AST
->CompleteTranslationUnit
= CompleteTranslationUnit
;
1483 AST
->ShouldCacheCodeCompletionResults
= CacheCodeCompletionResults
;
1484 AST
->CacheCodeCompletionCoolDown
= 1;
1485 AST
->NumStoredDiagnosticsFromDriver
= StoredDiagnostics
.size();
1486 AST
->NumStoredDiagnosticsInPreamble
= StoredDiagnostics
.size();
1487 AST
->StoredDiagnostics
.swap(StoredDiagnostics
);
1488 AST
->Invocation
.reset(CI
.take());
1489 return AST
->LoadFromCompilerInvocation(PrecompilePreamble
)? 0 : AST
.take();
1492 bool ASTUnit::Reparse(RemappedFile
*RemappedFiles
, unsigned NumRemappedFiles
) {
1493 if (!Invocation
.get())
1496 SimpleTimer
ParsingTimer(WantTiming
);
1497 ParsingTimer
.setOutput("Reparsing " + getMainFileName());
1500 PreprocessorOptions
&PPOpts
= Invocation
->getPreprocessorOpts();
1501 for (PreprocessorOptions::remapped_file_buffer_iterator
1502 R
= PPOpts
.remapped_file_buffer_begin(),
1503 REnd
= PPOpts
.remapped_file_buffer_end();
1508 Invocation
->getPreprocessorOpts().clearRemappedFiles();
1509 for (unsigned I
= 0; I
!= NumRemappedFiles
; ++I
)
1510 Invocation
->getPreprocessorOpts().addRemappedFile(RemappedFiles
[I
].first
,
1511 RemappedFiles
[I
].second
);
1513 // If we have a preamble file lying around, or if we might try to
1514 // build a precompiled preamble, do so now.
1515 llvm::MemoryBuffer
*OverrideMainBuffer
= 0;
1516 if (!PreambleFile
.empty() || PreambleRebuildCounter
> 0)
1517 OverrideMainBuffer
= getMainBufferWithPrecompiledPreamble(*Invocation
);
1519 // Clear out the diagnostics state.
1520 if (!OverrideMainBuffer
) {
1521 getDiagnostics().Reset();
1522 ProcessWarningOptions(getDiagnostics(), Invocation
->getDiagnosticOpts());
1525 // Parse the sources
1526 bool Result
= Parse(OverrideMainBuffer
);
1528 if (ShouldCacheCodeCompletionResults
) {
1529 if (CacheCodeCompletionCoolDown
> 0)
1530 --CacheCodeCompletionCoolDown
;
1531 else if (top_level_size() != NumTopLevelDeclsAtLastCompletionCache
)
1532 CacheCodeCompletionResults();
1538 //----------------------------------------------------------------------------//
1540 //----------------------------------------------------------------------------//
1543 /// \brief Code completion consumer that combines the cached code-completion
1544 /// results from an ASTUnit with the code-completion results provided to it,
1545 /// then passes the result on to
1546 class AugmentedCodeCompleteConsumer
: public CodeCompleteConsumer
{
1547 unsigned NormalContexts
;
1549 CodeCompleteConsumer
&Next
;
1552 AugmentedCodeCompleteConsumer(ASTUnit
&AST
, CodeCompleteConsumer
&Next
,
1553 bool IncludeMacros
, bool IncludeCodePatterns
,
1554 bool IncludeGlobals
)
1555 : CodeCompleteConsumer(IncludeMacros
, IncludeCodePatterns
, IncludeGlobals
,
1556 Next
.isOutputBinary()), AST(AST
), Next(Next
)
1558 // Compute the set of contexts in which we will look when we don't have
1559 // any information about the specific context.
1561 = (1 << (CodeCompletionContext::CCC_TopLevel
- 1))
1562 | (1 << (CodeCompletionContext::CCC_ObjCInterface
- 1))
1563 | (1 << (CodeCompletionContext::CCC_ObjCImplementation
- 1))
1564 | (1 << (CodeCompletionContext::CCC_ObjCIvarList
- 1))
1565 | (1 << (CodeCompletionContext::CCC_Statement
- 1))
1566 | (1 << (CodeCompletionContext::CCC_Expression
- 1))
1567 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver
- 1))
1568 | (1 << (CodeCompletionContext::CCC_MemberAccess
- 1))
1569 | (1 << (CodeCompletionContext::CCC_ObjCProtocolName
- 1))
1570 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression
- 1))
1571 | (1 << (CodeCompletionContext::CCC_Recovery
- 1));
1573 if (AST
.getASTContext().getLangOptions().CPlusPlus
)
1574 NormalContexts
|= (1 << (CodeCompletionContext::CCC_EnumTag
- 1))
1575 | (1 << (CodeCompletionContext::CCC_UnionTag
- 1))
1576 | (1 << (CodeCompletionContext::CCC_ClassOrStructTag
- 1));
1579 virtual void ProcessCodeCompleteResults(Sema
&S
,
1580 CodeCompletionContext Context
,
1581 CodeCompletionResult
*Results
,
1582 unsigned NumResults
);
1584 virtual void ProcessOverloadCandidates(Sema
&S
, unsigned CurrentArg
,
1585 OverloadCandidate
*Candidates
,
1586 unsigned NumCandidates
) {
1587 Next
.ProcessOverloadCandidates(S
, CurrentArg
, Candidates
, NumCandidates
);
1592 /// \brief Helper function that computes which global names are hidden by the
1593 /// local code-completion results.
1594 static void CalculateHiddenNames(const CodeCompletionContext
&Context
,
1595 CodeCompletionResult
*Results
,
1596 unsigned NumResults
,
1598 llvm::StringSet
<llvm::BumpPtrAllocator
> &HiddenNames
){
1599 bool OnlyTagNames
= false;
1600 switch (Context
.getKind()) {
1601 case CodeCompletionContext::CCC_Recovery
:
1602 case CodeCompletionContext::CCC_TopLevel
:
1603 case CodeCompletionContext::CCC_ObjCInterface
:
1604 case CodeCompletionContext::CCC_ObjCImplementation
:
1605 case CodeCompletionContext::CCC_ObjCIvarList
:
1606 case CodeCompletionContext::CCC_ClassStructUnion
:
1607 case CodeCompletionContext::CCC_Statement
:
1608 case CodeCompletionContext::CCC_Expression
:
1609 case CodeCompletionContext::CCC_ObjCMessageReceiver
:
1610 case CodeCompletionContext::CCC_MemberAccess
:
1611 case CodeCompletionContext::CCC_Namespace
:
1612 case CodeCompletionContext::CCC_Type
:
1613 case CodeCompletionContext::CCC_Name
:
1614 case CodeCompletionContext::CCC_PotentiallyQualifiedName
:
1615 case CodeCompletionContext::CCC_ParenthesizedExpression
:
1618 case CodeCompletionContext::CCC_EnumTag
:
1619 case CodeCompletionContext::CCC_UnionTag
:
1620 case CodeCompletionContext::CCC_ClassOrStructTag
:
1621 OnlyTagNames
= true;
1624 case CodeCompletionContext::CCC_ObjCProtocolName
:
1625 case CodeCompletionContext::CCC_MacroName
:
1626 case CodeCompletionContext::CCC_MacroNameUse
:
1627 case CodeCompletionContext::CCC_PreprocessorExpression
:
1628 case CodeCompletionContext::CCC_PreprocessorDirective
:
1629 case CodeCompletionContext::CCC_NaturalLanguage
:
1630 case CodeCompletionContext::CCC_SelectorName
:
1631 case CodeCompletionContext::CCC_TypeQualifiers
:
1632 case CodeCompletionContext::CCC_Other
:
1633 // We're looking for nothing, or we're looking for names that cannot
1638 typedef CodeCompletionResult Result
;
1639 for (unsigned I
= 0; I
!= NumResults
; ++I
) {
1640 if (Results
[I
].Kind
!= Result::RK_Declaration
)
1644 = Results
[I
].Declaration
->getUnderlyingDecl()->getIdentifierNamespace();
1646 bool Hiding
= false;
1648 Hiding
= (IDNS
& Decl::IDNS_Tag
);
1650 unsigned HiddenIDNS
= (Decl::IDNS_Type
| Decl::IDNS_Member
|
1651 Decl::IDNS_Namespace
| Decl::IDNS_Ordinary
|
1652 Decl::IDNS_NonMemberOperator
);
1653 if (Ctx
.getLangOptions().CPlusPlus
)
1654 HiddenIDNS
|= Decl::IDNS_Tag
;
1655 Hiding
= (IDNS
& HiddenIDNS
);
1661 DeclarationName Name
= Results
[I
].Declaration
->getDeclName();
1662 if (IdentifierInfo
*Identifier
= Name
.getAsIdentifierInfo())
1663 HiddenNames
.insert(Identifier
->getName());
1665 HiddenNames
.insert(Name
.getAsString());
1670 void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema
&S
,
1671 CodeCompletionContext Context
,
1672 CodeCompletionResult
*Results
,
1673 unsigned NumResults
) {
1674 // Merge the results we were given with the results we cached.
1675 bool AddedResult
= false;
1677 = (Context
.getKind() == CodeCompletionContext::CCC_Recovery
? NormalContexts
1678 : (1 << (Context
.getKind() - 1)));
1680 // Contains the set of names that are hidden by "local" completion results.
1681 llvm::StringSet
<llvm::BumpPtrAllocator
> HiddenNames
;
1682 llvm::SmallVector
<CodeCompletionString
*, 4> StringsToDestroy
;
1683 typedef CodeCompletionResult Result
;
1684 llvm::SmallVector
<Result
, 8> AllResults
;
1685 for (ASTUnit::cached_completion_iterator
1686 C
= AST
.cached_completion_begin(),
1687 CEnd
= AST
.cached_completion_end();
1689 // If the context we are in matches any of the contexts we are
1690 // interested in, we'll add this result.
1691 if ((C
->ShowInContexts
& InContexts
) == 0)
1694 // If we haven't added any results previously, do so now.
1696 CalculateHiddenNames(Context
, Results
, NumResults
, S
.Context
,
1698 AllResults
.insert(AllResults
.end(), Results
, Results
+ NumResults
);
1702 // Determine whether this global completion result is hidden by a local
1703 // completion result. If so, skip it.
1704 if (C
->Kind
!= CXCursor_MacroDefinition
&&
1705 HiddenNames
.count(C
->Completion
->getTypedText()))
1708 // Adjust priority based on similar type classes.
1709 unsigned Priority
= C
->Priority
;
1710 CXCursorKind CursorKind
= C
->Kind
;
1711 CodeCompletionString
*Completion
= C
->Completion
;
1712 if (!Context
.getPreferredType().isNull()) {
1713 if (C
->Kind
== CXCursor_MacroDefinition
) {
1714 Priority
= getMacroUsagePriority(C
->Completion
->getTypedText(),
1716 Context
.getPreferredType()->isAnyPointerType());
1717 } else if (C
->Type
) {
1718 CanQualType Expected
1719 = S
.Context
.getCanonicalType(
1720 Context
.getPreferredType().getUnqualifiedType());
1721 SimplifiedTypeClass ExpectedSTC
= getSimplifiedTypeClass(Expected
);
1722 if (ExpectedSTC
== C
->TypeClass
) {
1723 // We know this type is similar; check for an exact match.
1724 llvm::StringMap
<unsigned> &CachedCompletionTypes
1725 = AST
.getCachedCompletionTypes();
1726 llvm::StringMap
<unsigned>::iterator Pos
1727 = CachedCompletionTypes
.find(QualType(Expected
).getAsString());
1728 if (Pos
!= CachedCompletionTypes
.end() && Pos
->second
== C
->Type
)
1729 Priority
/= CCF_ExactTypeMatch
;
1731 Priority
/= CCF_SimilarTypeMatch
;
1736 // Adjust the completion string, if required.
1737 if (C
->Kind
== CXCursor_MacroDefinition
&&
1738 Context
.getKind() == CodeCompletionContext::CCC_MacroNameUse
) {
1739 // Create a new code-completion string that just contains the
1740 // macro name, without its arguments.
1741 Completion
= new CodeCompletionString
;
1742 Completion
->AddTypedTextChunk(C
->Completion
->getTypedText());
1743 StringsToDestroy
.push_back(Completion
);
1744 CursorKind
= CXCursor_NotImplemented
;
1745 Priority
= CCP_CodePattern
;
1748 AllResults
.push_back(Result(Completion
, Priority
, CursorKind
,
1752 // If we did not add any cached completion results, just forward the
1753 // results we were given to the next consumer.
1755 Next
.ProcessCodeCompleteResults(S
, Context
, Results
, NumResults
);
1759 Next
.ProcessCodeCompleteResults(S
, Context
, AllResults
.data(),
1762 for (unsigned I
= 0, N
= StringsToDestroy
.size(); I
!= N
; ++I
)
1763 delete StringsToDestroy
[I
];
1768 void ASTUnit::CodeComplete(llvm::StringRef File
, unsigned Line
, unsigned Column
,
1769 RemappedFile
*RemappedFiles
,
1770 unsigned NumRemappedFiles
,
1772 bool IncludeCodePatterns
,
1773 CodeCompleteConsumer
&Consumer
,
1774 Diagnostic
&Diag
, LangOptions
&LangOpts
,
1775 SourceManager
&SourceMgr
, FileManager
&FileMgr
,
1776 llvm::SmallVectorImpl
<StoredDiagnostic
> &StoredDiagnostics
,
1777 llvm::SmallVectorImpl
<const llvm::MemoryBuffer
*> &OwnedBuffers
) {
1778 if (!Invocation
.get())
1781 SimpleTimer
CompletionTimer(WantTiming
);
1782 CompletionTimer
.setOutput("Code completion @ " + File
+ ":" +
1783 llvm::Twine(Line
) + ":" + llvm::Twine(Column
));
1785 CompilerInvocation
CCInvocation(*Invocation
);
1786 FrontendOptions
&FrontendOpts
= CCInvocation
.getFrontendOpts();
1787 PreprocessorOptions
&PreprocessorOpts
= CCInvocation
.getPreprocessorOpts();
1789 FrontendOpts
.ShowMacrosInCodeCompletion
1790 = IncludeMacros
&& CachedCompletionResults
.empty();
1791 FrontendOpts
.ShowCodePatternsInCodeCompletion
= IncludeCodePatterns
;
1792 FrontendOpts
.ShowGlobalSymbolsInCodeCompletion
1793 = CachedCompletionResults
.empty();
1794 FrontendOpts
.CodeCompletionAt
.FileName
= File
;
1795 FrontendOpts
.CodeCompletionAt
.Line
= Line
;
1796 FrontendOpts
.CodeCompletionAt
.Column
= Column
;
1798 // Set the language options appropriately.
1799 LangOpts
= CCInvocation
.getLangOpts();
1801 CompilerInstance Clang
;
1802 Clang
.setInvocation(&CCInvocation
);
1803 OriginalSourceFile
= Clang
.getFrontendOpts().Inputs
[0].second
;
1805 // Set up diagnostics, capturing any diagnostics produced.
1806 Clang
.setDiagnostics(&Diag
);
1807 ProcessWarningOptions(Diag
, CCInvocation
.getDiagnosticOpts());
1808 CaptureDroppedDiagnostics
Capture(true,
1809 Clang
.getDiagnostics(),
1812 // Create the target instance.
1813 Clang
.setTarget(TargetInfo::CreateTargetInfo(Clang
.getDiagnostics(),
1814 Clang
.getTargetOpts()));
1815 if (!Clang
.hasTarget()) {
1816 Clang
.takeInvocation();
1820 // Inform the target of the language options.
1822 // FIXME: We shouldn't need to do this, the target should be immutable once
1823 // created. This complexity should be lifted elsewhere.
1824 Clang
.getTarget().setForcedLangOptions(Clang
.getLangOpts());
1826 assert(Clang
.getFrontendOpts().Inputs
.size() == 1 &&
1827 "Invocation must have exactly one source file!");
1828 assert(Clang
.getFrontendOpts().Inputs
[0].first
!= IK_AST
&&
1829 "FIXME: AST inputs not yet supported here!");
1830 assert(Clang
.getFrontendOpts().Inputs
[0].first
!= IK_LLVM_IR
&&
1831 "IR inputs not support here!");
1834 // Use the source and file managers that we were given.
1835 Clang
.setFileManager(&FileMgr
);
1836 Clang
.setSourceManager(&SourceMgr
);
1839 PreprocessorOpts
.clearRemappedFiles();
1840 PreprocessorOpts
.RetainRemappedFileBuffers
= true;
1841 for (unsigned I
= 0; I
!= NumRemappedFiles
; ++I
) {
1842 PreprocessorOpts
.addRemappedFile(RemappedFiles
[I
].first
,
1843 RemappedFiles
[I
].second
);
1844 OwnedBuffers
.push_back(RemappedFiles
[I
].second
);
1847 // Use the code completion consumer we were given, but adding any cached
1848 // code-completion results.
1849 AugmentedCodeCompleteConsumer
1850 AugmentedConsumer(*this, Consumer
, FrontendOpts
.ShowMacrosInCodeCompletion
,
1851 FrontendOpts
.ShowCodePatternsInCodeCompletion
,
1852 FrontendOpts
.ShowGlobalSymbolsInCodeCompletion
);
1853 Clang
.setCodeCompletionConsumer(&AugmentedConsumer
);
1855 // If we have a precompiled preamble, try to use it. We only allow
1856 // the use of the precompiled preamble if we're if the completion
1857 // point is within the main file, after the end of the precompiled
1859 llvm::MemoryBuffer
*OverrideMainBuffer
= 0;
1860 if (!PreambleFile
.empty()) {
1861 using llvm::sys::FileStatus
;
1862 llvm::sys::PathWithStatus
CompleteFilePath(File
);
1863 llvm::sys::PathWithStatus
MainPath(OriginalSourceFile
);
1864 if (const FileStatus
*CompleteFileStatus
= CompleteFilePath
.getFileStatus())
1865 if (const FileStatus
*MainStatus
= MainPath
.getFileStatus())
1866 if (CompleteFileStatus
->getUniqueID() == MainStatus
->getUniqueID())
1868 = getMainBufferWithPrecompiledPreamble(CCInvocation
, false,
1872 // If the main file has been overridden due to the use of a preamble,
1873 // make that override happen and introduce the preamble.
1874 StoredDiagnostics
.insert(StoredDiagnostics
.end(),
1875 this->StoredDiagnostics
.begin(),
1876 this->StoredDiagnostics
.begin() + NumStoredDiagnosticsFromDriver
);
1877 if (OverrideMainBuffer
) {
1878 PreprocessorOpts
.addRemappedFile(OriginalSourceFile
, OverrideMainBuffer
);
1879 PreprocessorOpts
.PrecompiledPreambleBytes
.first
= Preamble
.size();
1880 PreprocessorOpts
.PrecompiledPreambleBytes
.second
1881 = PreambleEndsAtStartOfLine
;
1882 PreprocessorOpts
.ImplicitPCHInclude
= PreambleFile
;
1883 PreprocessorOpts
.DisablePCHValidation
= true;
1885 // The stored diagnostics have the old source manager. Copy them
1886 // to our output set of stored diagnostics, updating the source
1887 // manager to the one we were given.
1888 for (unsigned I
= NumStoredDiagnosticsFromDriver
,
1889 N
= this->StoredDiagnostics
.size();
1891 StoredDiagnostics
.push_back(this->StoredDiagnostics
[I
]);
1892 FullSourceLoc
Loc(StoredDiagnostics
[I
].getLocation(), SourceMgr
);
1893 StoredDiagnostics
[I
].setLocation(Loc
);
1896 OwnedBuffers
.push_back(OverrideMainBuffer
);
1898 PreprocessorOpts
.PrecompiledPreambleBytes
.first
= 0;
1899 PreprocessorOpts
.PrecompiledPreambleBytes
.second
= false;
1902 llvm::OwningPtr
<SyntaxOnlyAction
> Act
;
1903 Act
.reset(new SyntaxOnlyAction
);
1904 if (Act
->BeginSourceFile(Clang
, Clang
.getFrontendOpts().Inputs
[0].second
,
1905 Clang
.getFrontendOpts().Inputs
[0].first
)) {
1907 Act
->EndSourceFile();
1910 // Steal back our resources.
1911 Clang
.takeFileManager();
1912 Clang
.takeSourceManager();
1913 Clang
.takeInvocation();
1914 Clang
.takeCodeCompletionConsumer();
1917 bool ASTUnit::Save(llvm::StringRef File
) {
1918 if (getDiagnostics().hasErrorOccurred())
1921 // FIXME: Can we somehow regenerate the stat cache here, or do we need to
1922 // unconditionally create a stat cache when we parse the file?
1923 std::string ErrorInfo
;
1924 llvm::raw_fd_ostream
Out(File
.str().c_str(), ErrorInfo
,
1925 llvm::raw_fd_ostream::F_Binary
);
1926 if (!ErrorInfo
.empty() || Out
.has_error())
1929 std::vector
<unsigned char> Buffer
;
1930 llvm::BitstreamWriter
Stream(Buffer
);
1931 ASTWriter
Writer(Stream
);
1932 Writer
.WriteAST(getSema(), 0, 0);
1934 // Write the generated bitstream to "Out".
1935 if (!Buffer
.empty())
1936 Out
.write((char *)&Buffer
.front(), Buffer
.size());
1938 return Out
.has_error();