blob: 8827116f2e40f604be3e47e0481fecda562c2176 [file] [log] [blame]
Argyrios Kyrtzidis3a08ec12009-06-20 08:27:14 +00001//===--- ASTUnit.cpp - ASTUnit utility ------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// ASTUnit Implementation.
11//
12//===----------------------------------------------------------------------===//
13
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000014#include "clang/Frontend/ASTUnit.h"
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000015#include "clang/AST/ASTContext.h"
Daniel Dunbar764c0822009-12-01 09:51:01 +000016#include "clang/AST/ASTConsumer.h"
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000017#include "clang/AST/DeclVisitor.h"
Douglas Gregorb61c07a2010-08-16 18:08:11 +000018#include "clang/AST/TypeOrdering.h"
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000019#include "clang/AST/StmtVisitor.h"
Daniel Dunbar55a17b62009-12-02 03:23:45 +000020#include "clang/Driver/Compilation.h"
21#include "clang/Driver/Driver.h"
22#include "clang/Driver/Job.h"
Argyrios Kyrtzidisbc1f48f2011-03-07 22:45:01 +000023#include "clang/Driver/ArgList.h"
24#include "clang/Driver/Options.h"
Daniel Dunbar55a17b62009-12-02 03:23:45 +000025#include "clang/Driver/Tool.h"
Daniel Dunbar764c0822009-12-01 09:51:01 +000026#include "clang/Frontend/CompilerInstance.h"
27#include "clang/Frontend/FrontendActions.h"
Daniel Dunbar55a17b62009-12-02 03:23:45 +000028#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar764c0822009-12-01 09:51:01 +000029#include "clang/Frontend/FrontendOptions.h"
Douglas Gregor36e3b5c2010-10-11 21:37:58 +000030#include "clang/Frontend/Utils.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000031#include "clang/Serialization/ASTReader.h"
Douglas Gregorf88e35b2010-11-30 06:16:57 +000032#include "clang/Serialization/ASTSerializationListener.h"
Sebastian Redl1914c6f2010-08-18 23:56:37 +000033#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000034#include "clang/Lex/HeaderSearch.h"
35#include "clang/Lex/Preprocessor.h"
Daniel Dunbarb9bbd542009-11-15 06:48:46 +000036#include "clang/Basic/TargetOptions.h"
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000037#include "clang/Basic/TargetInfo.h"
38#include "clang/Basic/Diagnostic.h"
Chris Lattnerce6c42f2011-03-23 04:04:01 +000039#include "llvm/ADT/ArrayRef.h"
Douglas Gregordf7a79a2011-02-16 18:16:54 +000040#include "llvm/ADT/StringExtras.h"
Douglas Gregor40a5a7d2010-08-16 23:08:34 +000041#include "llvm/ADT/StringSet.h"
Douglas Gregor9aeaa4d2010-12-07 00:05:48 +000042#include "llvm/Support/Atomic.h"
Douglas Gregoraa98ed92010-01-23 00:14:00 +000043#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000044#include "llvm/Support/Host.h"
45#include "llvm/Support/Path.h"
Douglas Gregor028d3e42010-08-09 20:45:32 +000046#include "llvm/Support/raw_ostream.h"
Douglas Gregor15ba0b32010-07-30 20:58:08 +000047#include "llvm/Support/Timer.h"
Ted Kremenek4422bfe2011-03-18 02:06:56 +000048#include "llvm/Support/CrashRecoveryContext.h"
Douglas Gregorbe2d8c62010-07-23 00:33:23 +000049#include <cstdlib>
Zhongxing Xu318e4032010-07-23 02:15:08 +000050#include <cstdio>
Douglas Gregor0e119552010-07-31 00:40:00 +000051#include <sys/stat.h>
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000052using namespace clang;
53
Douglas Gregor16896c42010-10-28 15:44:59 +000054using llvm::TimeRecord;
55
56namespace {
57 class SimpleTimer {
58 bool WantTiming;
59 TimeRecord Start;
60 std::string Output;
61
Benjamin Kramerf2e5a912010-11-09 20:00:56 +000062 public:
Douglas Gregor1cbdd952010-11-01 13:48:43 +000063 explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
Douglas Gregor16896c42010-10-28 15:44:59 +000064 if (WantTiming)
Benjamin Kramerf2e5a912010-11-09 20:00:56 +000065 Start = TimeRecord::getCurrentTime();
Douglas Gregor16896c42010-10-28 15:44:59 +000066 }
67
Benjamin Kramerf2e5a912010-11-09 20:00:56 +000068 void setOutput(const llvm::Twine &Output) {
Douglas Gregor16896c42010-10-28 15:44:59 +000069 if (WantTiming)
Benjamin Kramerf2e5a912010-11-09 20:00:56 +000070 this->Output = Output.str();
Douglas Gregor16896c42010-10-28 15:44:59 +000071 }
72
Douglas Gregor16896c42010-10-28 15:44:59 +000073 ~SimpleTimer() {
74 if (WantTiming) {
75 TimeRecord Elapsed = TimeRecord::getCurrentTime();
76 Elapsed -= Start;
77 llvm::errs() << Output << ':';
78 Elapsed.print(Elapsed, llvm::errs());
79 llvm::errs() << '\n';
80 }
81 }
82 };
83}
84
Douglas Gregorbb420ab2010-08-04 05:53:38 +000085/// \brief After failing to build a precompiled preamble (due to
86/// errors in the source that occurs in the preamble), the number of
87/// reparses during which we'll skip even trying to precompile the
88/// preamble.
89const unsigned DefaultPreambleRebuildInterval = 5;
90
Douglas Gregor68dbaea2010-11-17 00:13:31 +000091/// \brief Tracks the number of ASTUnit objects that are currently active.
92///
93/// Used for debugging purposes only.
Douglas Gregor9aeaa4d2010-12-07 00:05:48 +000094static llvm::sys::cas_flag ActiveASTUnitObjects;
Douglas Gregor68dbaea2010-11-17 00:13:31 +000095
Douglas Gregord03e8232010-04-05 21:10:19 +000096ASTUnit::ASTUnit(bool _MainFileIsAST)
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +000097 : OnlyLocalDecls(false), CaptureDiagnostics(false),
98 MainFileIsAST(_MainFileIsAST),
Douglas Gregor16896c42010-10-28 15:44:59 +000099 CompleteTranslationUnit(true), WantTiming(getenv("LIBCLANG_TIMING")),
Argyrios Kyrtzidis4954bc12011-03-05 01:03:48 +0000100 OwnsRemappedFileBuffers(true),
Douglas Gregor16896c42010-10-28 15:44:59 +0000101 NumStoredDiagnosticsFromDriver(0),
Douglas Gregor7bb8af62010-10-12 00:50:20 +0000102 ConcurrencyCheckValue(CheckUnlocked),
Douglas Gregora0734c52010-08-19 01:33:06 +0000103 PreambleRebuildCounter(0), SavedMainFileBuffer(0), PreambleBuffer(0),
Douglas Gregor2c8bd472010-08-17 00:40:40 +0000104 ShouldCacheCodeCompletionResults(false),
Douglas Gregor998caea2011-05-06 16:33:08 +0000105 NestedMacroInstantiations(true),
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000106 CompletionCacheTopLevelHashValue(0),
107 PreambleTopLevelHashValue(0),
108 CurrentTopLevelHashValue(0),
Douglas Gregor4740c452010-08-19 00:45:44 +0000109 UnsafeToFree(false) {
Douglas Gregor68dbaea2010-11-17 00:13:31 +0000110 if (getenv("LIBCLANG_OBJTRACKING")) {
Douglas Gregor9aeaa4d2010-12-07 00:05:48 +0000111 llvm::sys::AtomicIncrement(&ActiveASTUnitObjects);
Douglas Gregor68dbaea2010-11-17 00:13:31 +0000112 fprintf(stderr, "+++ %d translation units\n", ActiveASTUnitObjects);
113 }
Douglas Gregor15ba0b32010-07-30 20:58:08 +0000114}
Douglas Gregord03e8232010-04-05 21:10:19 +0000115
Daniel Dunbar764c0822009-12-01 09:51:01 +0000116ASTUnit::~ASTUnit() {
Douglas Gregor0c7c2f82010-03-05 21:16:25 +0000117 ConcurrencyCheckValue = CheckLocked;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000118 CleanTemporaryFiles();
Douglas Gregor4dde7492010-07-23 23:58:40 +0000119 if (!PreambleFile.empty())
Douglas Gregor15ba0b32010-07-30 20:58:08 +0000120 llvm::sys::Path(PreambleFile).eraseFromDisk();
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000121
122 // Free the buffers associated with remapped files. We are required to
123 // perform this operation here because we explicitly request that the
124 // compiler instance *not* free these buffers for each invocation of the
125 // parser.
Ted Kremenek5e14d392011-03-21 18:40:17 +0000126 if (Invocation.getPtr() && OwnsRemappedFileBuffers) {
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000127 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
128 for (PreprocessorOptions::remapped_file_buffer_iterator
129 FB = PPOpts.remapped_file_buffer_begin(),
130 FBEnd = PPOpts.remapped_file_buffer_end();
131 FB != FBEnd;
132 ++FB)
133 delete FB->second;
134 }
Douglas Gregor96c04262010-07-27 14:52:07 +0000135
136 delete SavedMainFileBuffer;
Douglas Gregora0734c52010-08-19 01:33:06 +0000137 delete PreambleBuffer;
138
Douglas Gregor16896c42010-10-28 15:44:59 +0000139 ClearCachedCompletionResults();
Douglas Gregor68dbaea2010-11-17 00:13:31 +0000140
141 if (getenv("LIBCLANG_OBJTRACKING")) {
Douglas Gregor9aeaa4d2010-12-07 00:05:48 +0000142 llvm::sys::AtomicDecrement(&ActiveASTUnitObjects);
Douglas Gregor68dbaea2010-11-17 00:13:31 +0000143 fprintf(stderr, "--- %d translation units\n", ActiveASTUnitObjects);
144 }
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000145}
146
147void ASTUnit::CleanTemporaryFiles() {
Douglas Gregor6cb5ba42010-02-18 23:35:40 +0000148 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
149 TemporaryFiles[I].eraseFromDisk();
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000150 TemporaryFiles.clear();
Steve Naroff44cd60e2009-10-15 22:23:48 +0000151}
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000152
Douglas Gregor39982192010-08-15 06:18:01 +0000153/// \brief Determine the set of code-completion contexts in which this
154/// declaration should be shown.
155static unsigned getDeclShowContexts(NamedDecl *ND,
Douglas Gregor59cab552010-08-16 23:05:20 +0000156 const LangOptions &LangOpts,
157 bool &IsNestedNameSpecifier) {
158 IsNestedNameSpecifier = false;
159
Douglas Gregor39982192010-08-15 06:18:01 +0000160 if (isa<UsingShadowDecl>(ND))
161 ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl());
162 if (!ND)
163 return 0;
164
165 unsigned Contexts = 0;
166 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) ||
167 isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) {
168 // Types can appear in these contexts.
169 if (LangOpts.CPlusPlus || !isa<TagDecl>(ND))
170 Contexts |= (1 << (CodeCompletionContext::CCC_TopLevel - 1))
171 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
172 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
173 | (1 << (CodeCompletionContext::CCC_Statement - 1))
Douglas Gregor5e35d592010-09-14 23:59:36 +0000174 | (1 << (CodeCompletionContext::CCC_Type - 1))
175 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1));
Douglas Gregor39982192010-08-15 06:18:01 +0000176
177 // In C++, types can appear in expressions contexts (for functional casts).
178 if (LangOpts.CPlusPlus)
179 Contexts |= (1 << (CodeCompletionContext::CCC_Expression - 1));
180
181 // In Objective-C, message sends can send interfaces. In Objective-C++,
182 // all types are available due to functional casts.
183 if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND))
184 Contexts |= (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1));
185
186 // Deal with tag names.
187 if (isa<EnumDecl>(ND)) {
188 Contexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1));
189
Douglas Gregor59cab552010-08-16 23:05:20 +0000190 // Part of the nested-name-specifier in C++0x.
Douglas Gregor39982192010-08-15 06:18:01 +0000191 if (LangOpts.CPlusPlus0x)
Douglas Gregor59cab552010-08-16 23:05:20 +0000192 IsNestedNameSpecifier = true;
Douglas Gregor39982192010-08-15 06:18:01 +0000193 } else if (RecordDecl *Record = dyn_cast<RecordDecl>(ND)) {
194 if (Record->isUnion())
195 Contexts |= (1 << (CodeCompletionContext::CCC_UnionTag - 1));
196 else
197 Contexts |= (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1));
198
Douglas Gregor39982192010-08-15 06:18:01 +0000199 if (LangOpts.CPlusPlus)
Douglas Gregor59cab552010-08-16 23:05:20 +0000200 IsNestedNameSpecifier = true;
Douglas Gregor0ac41382010-09-23 23:01:17 +0000201 } else if (isa<ClassTemplateDecl>(ND))
Douglas Gregor59cab552010-08-16 23:05:20 +0000202 IsNestedNameSpecifier = true;
Douglas Gregor39982192010-08-15 06:18:01 +0000203 } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) {
204 // Values can appear in these contexts.
205 Contexts = (1 << (CodeCompletionContext::CCC_Statement - 1))
206 | (1 << (CodeCompletionContext::CCC_Expression - 1))
Douglas Gregor5e35d592010-09-14 23:59:36 +0000207 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
Douglas Gregor39982192010-08-15 06:18:01 +0000208 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1));
209 } else if (isa<ObjCProtocolDecl>(ND)) {
210 Contexts = (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1));
211 } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
Douglas Gregor59cab552010-08-16 23:05:20 +0000212 Contexts = (1 << (CodeCompletionContext::CCC_Namespace - 1));
Douglas Gregor39982192010-08-15 06:18:01 +0000213
214 // Part of the nested-name-specifier.
Douglas Gregor59cab552010-08-16 23:05:20 +0000215 IsNestedNameSpecifier = true;
Douglas Gregor39982192010-08-15 06:18:01 +0000216 }
217
218 return Contexts;
219}
220
Douglas Gregorb14904c2010-08-13 22:48:40 +0000221void ASTUnit::CacheCodeCompletionResults() {
222 if (!TheSema)
223 return;
224
Douglas Gregor16896c42010-10-28 15:44:59 +0000225 SimpleTimer Timer(WantTiming);
Benjamin Kramerf2e5a912010-11-09 20:00:56 +0000226 Timer.setOutput("Cache global code completions for " + getMainFileName());
Douglas Gregorb14904c2010-08-13 22:48:40 +0000227
228 // Clear out the previous results.
229 ClearCachedCompletionResults();
230
231 // Gather the set of global code completions.
John McCall276321a2010-08-25 06:19:51 +0000232 typedef CodeCompletionResult Result;
Douglas Gregorb14904c2010-08-13 22:48:40 +0000233 llvm::SmallVector<Result, 8> Results;
Douglas Gregor162b7122011-02-16 19:08:06 +0000234 CachedCompletionAllocator = new GlobalCodeCompletionAllocator;
235 TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator, Results);
Douglas Gregorb14904c2010-08-13 22:48:40 +0000236
237 // Translate global code completions into cached completions.
Douglas Gregorb61c07a2010-08-16 18:08:11 +0000238 llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
239
Douglas Gregorb14904c2010-08-13 22:48:40 +0000240 for (unsigned I = 0, N = Results.size(); I != N; ++I) {
241 switch (Results[I].Kind) {
Douglas Gregor39982192010-08-15 06:18:01 +0000242 case Result::RK_Declaration: {
Douglas Gregor59cab552010-08-16 23:05:20 +0000243 bool IsNestedNameSpecifier = false;
Douglas Gregor39982192010-08-15 06:18:01 +0000244 CachedCodeCompletionResult CachedResult;
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000245 CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema,
Douglas Gregor162b7122011-02-16 19:08:06 +0000246 *CachedCompletionAllocator);
Douglas Gregor39982192010-08-15 06:18:01 +0000247 CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration,
Douglas Gregor59cab552010-08-16 23:05:20 +0000248 Ctx->getLangOptions(),
249 IsNestedNameSpecifier);
Douglas Gregor39982192010-08-15 06:18:01 +0000250 CachedResult.Priority = Results[I].Priority;
251 CachedResult.Kind = Results[I].CursorKind;
Douglas Gregorf757a122010-08-23 23:00:57 +0000252 CachedResult.Availability = Results[I].Availability;
Douglas Gregor24747402010-08-16 16:46:30 +0000253
Douglas Gregorb61c07a2010-08-16 18:08:11 +0000254 // Keep track of the type of this completion in an ASTContext-agnostic
255 // way.
Douglas Gregor24747402010-08-16 16:46:30 +0000256 QualType UsageType = getDeclUsageType(*Ctx, Results[I].Declaration);
Douglas Gregorb61c07a2010-08-16 18:08:11 +0000257 if (UsageType.isNull()) {
Douglas Gregor24747402010-08-16 16:46:30 +0000258 CachedResult.TypeClass = STC_Void;
Douglas Gregorb61c07a2010-08-16 18:08:11 +0000259 CachedResult.Type = 0;
260 } else {
261 CanQualType CanUsageType
262 = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
263 CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
264
265 // Determine whether we have already seen this type. If so, we save
266 // ourselves the work of formatting the type string by using the
267 // temporary, CanQualType-based hash table to find the associated value.
268 unsigned &TypeValue = CompletionTypes[CanUsageType];
269 if (TypeValue == 0) {
270 TypeValue = CompletionTypes.size();
271 CachedCompletionTypes[QualType(CanUsageType).getAsString()]
272 = TypeValue;
273 }
274
275 CachedResult.Type = TypeValue;
Douglas Gregor24747402010-08-16 16:46:30 +0000276 }
Douglas Gregorb61c07a2010-08-16 18:08:11 +0000277
Douglas Gregor39982192010-08-15 06:18:01 +0000278 CachedCompletionResults.push_back(CachedResult);
Douglas Gregor59cab552010-08-16 23:05:20 +0000279
280 /// Handle nested-name-specifiers in C++.
281 if (TheSema->Context.getLangOptions().CPlusPlus &&
282 IsNestedNameSpecifier && !Results[I].StartsNestedNameSpecifier) {
283 // The contexts in which a nested-name-specifier can appear in C++.
284 unsigned NNSContexts
285 = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
286 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
287 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
288 | (1 << (CodeCompletionContext::CCC_Statement - 1))
289 | (1 << (CodeCompletionContext::CCC_Expression - 1))
290 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
291 | (1 << (CodeCompletionContext::CCC_EnumTag - 1))
292 | (1 << (CodeCompletionContext::CCC_UnionTag - 1))
293 | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1))
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000294 | (1 << (CodeCompletionContext::CCC_Type - 1))
Douglas Gregor5e35d592010-09-14 23:59:36 +0000295 | (1 << (CodeCompletionContext::CCC_PotentiallyQualifiedName - 1))
296 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1));
Douglas Gregor59cab552010-08-16 23:05:20 +0000297
298 if (isa<NamespaceDecl>(Results[I].Declaration) ||
299 isa<NamespaceAliasDecl>(Results[I].Declaration))
300 NNSContexts |= (1 << (CodeCompletionContext::CCC_Namespace - 1));
301
302 if (unsigned RemainingContexts
303 = NNSContexts & ~CachedResult.ShowInContexts) {
304 // If there any contexts where this completion can be a
305 // nested-name-specifier but isn't already an option, create a
306 // nested-name-specifier completion.
307 Results[I].StartsNestedNameSpecifier = true;
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000308 CachedResult.Completion
309 = Results[I].CreateCodeCompletionString(*TheSema,
Douglas Gregor162b7122011-02-16 19:08:06 +0000310 *CachedCompletionAllocator);
Douglas Gregor59cab552010-08-16 23:05:20 +0000311 CachedResult.ShowInContexts = RemainingContexts;
312 CachedResult.Priority = CCP_NestedNameSpecifier;
313 CachedResult.TypeClass = STC_Void;
314 CachedResult.Type = 0;
315 CachedCompletionResults.push_back(CachedResult);
316 }
317 }
Douglas Gregorb14904c2010-08-13 22:48:40 +0000318 break;
Douglas Gregor39982192010-08-15 06:18:01 +0000319 }
320
Douglas Gregorb14904c2010-08-13 22:48:40 +0000321 case Result::RK_Keyword:
322 case Result::RK_Pattern:
323 // Ignore keywords and patterns; we don't care, since they are so
324 // easily regenerated.
325 break;
326
327 case Result::RK_Macro: {
328 CachedCodeCompletionResult CachedResult;
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000329 CachedResult.Completion
330 = Results[I].CreateCodeCompletionString(*TheSema,
Douglas Gregor162b7122011-02-16 19:08:06 +0000331 *CachedCompletionAllocator);
Douglas Gregorb14904c2010-08-13 22:48:40 +0000332 CachedResult.ShowInContexts
333 = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
334 | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1))
335 | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1))
336 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
337 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
338 | (1 << (CodeCompletionContext::CCC_Statement - 1))
339 | (1 << (CodeCompletionContext::CCC_Expression - 1))
Douglas Gregor12785102010-08-24 20:21:13 +0000340 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
Douglas Gregorec00a262010-08-24 22:20:20 +0000341 | (1 << (CodeCompletionContext::CCC_MacroNameUse - 1))
Douglas Gregor5e35d592010-09-14 23:59:36 +0000342 | (1 << (CodeCompletionContext::CCC_PreprocessorExpression - 1))
Douglas Gregor3a69eaf2011-02-18 23:30:37 +0000343 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
344 | (1 << (CodeCompletionContext::CCC_OtherWithMacros - 1));
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000345
Douglas Gregorb14904c2010-08-13 22:48:40 +0000346 CachedResult.Priority = Results[I].Priority;
347 CachedResult.Kind = Results[I].CursorKind;
Douglas Gregorf757a122010-08-23 23:00:57 +0000348 CachedResult.Availability = Results[I].Availability;
Douglas Gregor6e240332010-08-16 16:18:59 +0000349 CachedResult.TypeClass = STC_Void;
Douglas Gregorb61c07a2010-08-16 18:08:11 +0000350 CachedResult.Type = 0;
Douglas Gregorb14904c2010-08-13 22:48:40 +0000351 CachedCompletionResults.push_back(CachedResult);
352 break;
353 }
354 }
Douglas Gregorb14904c2010-08-13 22:48:40 +0000355 }
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000356
357 // Save the current top-level hash value.
358 CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
Douglas Gregorb14904c2010-08-13 22:48:40 +0000359}
360
361void ASTUnit::ClearCachedCompletionResults() {
Douglas Gregorb14904c2010-08-13 22:48:40 +0000362 CachedCompletionResults.clear();
Douglas Gregorb61c07a2010-08-16 18:08:11 +0000363 CachedCompletionTypes.clear();
Douglas Gregor162b7122011-02-16 19:08:06 +0000364 CachedCompletionAllocator = 0;
Douglas Gregorb14904c2010-08-13 22:48:40 +0000365}
366
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000367namespace {
368
Sebastian Redl2c499f62010-08-18 23:56:43 +0000369/// \brief Gathers information from ASTReader that will be used to initialize
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000370/// a Preprocessor.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000371class ASTInfoCollector : public ASTReaderListener {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000372 LangOptions &LangOpt;
373 HeaderSearch &HSI;
374 std::string &TargetTriple;
375 std::string &Predefines;
376 unsigned &Counter;
Mike Stump11289f42009-09-09 15:08:12 +0000377
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000378 unsigned NumHeaderInfos;
Mike Stump11289f42009-09-09 15:08:12 +0000379
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000380public:
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000381 ASTInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000382 std::string &TargetTriple, std::string &Predefines,
383 unsigned &Counter)
384 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
385 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
Mike Stump11289f42009-09-09 15:08:12 +0000386
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000387 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
388 LangOpt = LangOpts;
389 return false;
390 }
Mike Stump11289f42009-09-09 15:08:12 +0000391
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000392 virtual bool ReadTargetTriple(llvm::StringRef Triple) {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000393 TargetTriple = Triple;
394 return false;
395 }
Mike Stump11289f42009-09-09 15:08:12 +0000396
Sebastian Redl8b41f302010-07-14 23:29:55 +0000397 virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000398 llvm::StringRef OriginalFileName,
Nick Lewycky36079892011-02-23 21:16:44 +0000399 std::string &SuggestedPredefines,
400 FileManager &FileMgr) {
Sebastian Redl8b41f302010-07-14 23:29:55 +0000401 Predefines = Buffers[0].Data;
402 for (unsigned I = 1, N = Buffers.size(); I != N; ++I) {
403 Predefines += Buffers[I].Data;
404 }
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000405 return false;
406 }
Mike Stump11289f42009-09-09 15:08:12 +0000407
Douglas Gregora2f49452010-03-16 19:09:18 +0000408 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000409 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
410 }
Mike Stump11289f42009-09-09 15:08:12 +0000411
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000412 virtual void ReadCounter(unsigned Value) {
413 Counter = Value;
414 }
415};
416
Douglas Gregor33cdd812010-02-18 18:08:43 +0000417class StoredDiagnosticClient : public DiagnosticClient {
418 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags;
419
420public:
421 explicit StoredDiagnosticClient(
422 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
423 : StoredDiags(StoredDiags) { }
424
425 virtual void HandleDiagnostic(Diagnostic::Level Level,
426 const DiagnosticInfo &Info);
427};
428
429/// \brief RAII object that optionally captures diagnostics, if
430/// there is no diagnostic client to capture them already.
431class CaptureDroppedDiagnostics {
432 Diagnostic &Diags;
433 StoredDiagnosticClient Client;
434 DiagnosticClient *PreviousClient;
435
436public:
437 CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags,
Douglas Gregor44c6ee72010-11-11 00:39:14 +0000438 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000439 : Diags(Diags), Client(StoredDiags), PreviousClient(0)
Douglas Gregor33cdd812010-02-18 18:08:43 +0000440 {
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000441 if (RequestCapture || Diags.getClient() == 0) {
442 PreviousClient = Diags.takeClient();
Douglas Gregor33cdd812010-02-18 18:08:43 +0000443 Diags.setClient(&Client);
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000444 }
Douglas Gregor33cdd812010-02-18 18:08:43 +0000445 }
446
447 ~CaptureDroppedDiagnostics() {
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000448 if (Diags.getClient() == &Client) {
449 Diags.takeClient();
450 Diags.setClient(PreviousClient);
451 }
Douglas Gregor33cdd812010-02-18 18:08:43 +0000452 }
453};
454
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000455} // anonymous namespace
456
Douglas Gregor33cdd812010-02-18 18:08:43 +0000457void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level,
458 const DiagnosticInfo &Info) {
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000459 // Default implementation (Warnings/errors count).
460 DiagnosticClient::HandleDiagnostic(Level, Info);
461
Douglas Gregor33cdd812010-02-18 18:08:43 +0000462 StoredDiags.push_back(StoredDiagnostic(Level, Info));
463}
464
Steve Naroffc0683b92009-09-03 18:19:54 +0000465const std::string &ASTUnit::getOriginalSourceFileName() {
Daniel Dunbara8a50932009-12-02 08:44:16 +0000466 return OriginalSourceFile;
Steve Naroffc0683b92009-09-03 18:19:54 +0000467}
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000468
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000469const std::string &ASTUnit::getASTFileName() {
470 assert(isMainFileAST() && "Not an ASTUnit from an AST file!");
Sebastian Redl2c499f62010-08-18 23:56:43 +0000471 return static_cast<ASTReader *>(Ctx->getExternalSource())->getFileName();
Steve Naroff44cd60e2009-10-15 22:23:48 +0000472}
473
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000474llvm::MemoryBuffer *ASTUnit::getBufferForFile(llvm::StringRef Filename,
Chris Lattner26b5c192010-11-23 09:19:42 +0000475 std::string *ErrorStr) {
Chris Lattner5159f612010-11-23 08:35:12 +0000476 assert(FileMgr);
Chris Lattner26b5c192010-11-23 09:19:42 +0000477 return FileMgr->getBufferForFile(Filename, ErrorStr);
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000478}
479
Douglas Gregor44c6ee72010-11-11 00:39:14 +0000480/// \brief Configure the diagnostics object for use with ASTUnit.
481void ASTUnit::ConfigureDiags(llvm::IntrusiveRefCntPtr<Diagnostic> &Diags,
Douglas Gregor345c1bc2011-01-19 01:02:47 +0000482 const char **ArgBegin, const char **ArgEnd,
Douglas Gregor44c6ee72010-11-11 00:39:14 +0000483 ASTUnit &AST, bool CaptureDiagnostics) {
484 if (!Diags.getPtr()) {
485 // No diagnostics engine was provided, so create our own diagnostics object
486 // with the default options.
487 DiagnosticOptions DiagOpts;
488 DiagnosticClient *Client = 0;
489 if (CaptureDiagnostics)
490 Client = new StoredDiagnosticClient(AST.StoredDiagnostics);
Douglas Gregor345c1bc2011-01-19 01:02:47 +0000491 Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd- ArgBegin,
492 ArgBegin, Client);
Douglas Gregor44c6ee72010-11-11 00:39:14 +0000493 } else if (CaptureDiagnostics) {
494 Diags->setClient(new StoredDiagnosticClient(AST.StoredDiagnostics));
495 }
496}
497
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000498ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename,
Douglas Gregor7f95d262010-04-05 23:52:57 +0000499 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000500 const FileSystemOptions &FileSystemOpts,
Ted Kremenek8bcb1c62009-10-17 00:34:24 +0000501 bool OnlyLocalDecls,
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000502 RemappedFile *RemappedFiles,
Douglas Gregor33cdd812010-02-18 18:08:43 +0000503 unsigned NumRemappedFiles,
504 bool CaptureDiagnostics) {
Douglas Gregord03e8232010-04-05 21:10:19 +0000505 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
Ted Kremenek4422bfe2011-03-18 02:06:56 +0000506
507 // Recover resources if we crash before exiting this method.
Ted Kremenek022a4902011-03-22 01:15:24 +0000508 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
509 ASTUnitCleanup(AST.get());
510 llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
511 llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
512 DiagCleanup(Diags.getPtr());
Ted Kremenek4422bfe2011-03-18 02:06:56 +0000513
Douglas Gregor345c1bc2011-01-19 01:02:47 +0000514 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000515
Douglas Gregor16bef852009-10-16 20:01:17 +0000516 AST->OnlyLocalDecls = OnlyLocalDecls;
Douglas Gregor44c6ee72010-11-11 00:39:14 +0000517 AST->CaptureDiagnostics = CaptureDiagnostics;
Douglas Gregor7f95d262010-04-05 23:52:57 +0000518 AST->Diagnostics = Diags;
Ted Kremenek5e14d392011-03-21 18:40:17 +0000519 AST->FileMgr = new FileManager(FileSystemOpts);
520 AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
521 AST->getFileManager());
Chris Lattner5159f612010-11-23 08:35:12 +0000522 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
Douglas Gregor6fd55e02010-08-13 03:15:25 +0000523
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000524 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +0000525 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
526 if (const llvm::MemoryBuffer *
527 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
528 // Create the file entry for the file that we're mapping from.
529 const FileEntry *FromFile
530 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
531 memBuf->getBufferSize(),
532 0);
533 if (!FromFile) {
534 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
535 << RemappedFiles[I].first;
536 delete memBuf;
537 continue;
538 }
539
540 // Override the contents of the "from" file with the contents of
541 // the "to" file.
542 AST->getSourceManager().overrideFileContents(FromFile, memBuf);
543
544 } else {
545 const char *fname = fileOrBuf.get<const char *>();
546 const FileEntry *ToFile = AST->FileMgr->getFile(fname);
547 if (!ToFile) {
548 AST->getDiagnostics().Report(diag::err_fe_remap_missing_to_file)
549 << RemappedFiles[I].first << fname;
550 continue;
551 }
552
553 // Create the file entry for the file that we're mapping from.
554 const FileEntry *FromFile
555 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
556 ToFile->getSize(),
557 0);
558 if (!FromFile) {
559 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
560 << RemappedFiles[I].first;
561 delete memBuf;
562 continue;
563 }
564
565 // Override the contents of the "from" file with the contents of
566 // the "to" file.
567 AST->getSourceManager().overrideFileContents(FromFile, ToFile);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000568 }
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000569 }
570
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000571 // Gather Info for preprocessor construction later on.
Mike Stump11289f42009-09-09 15:08:12 +0000572
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000573 LangOptions LangInfo;
574 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
575 std::string TargetTriple;
576 std::string Predefines;
577 unsigned Counter;
578
Sebastian Redl2c499f62010-08-18 23:56:43 +0000579 llvm::OwningPtr<ASTReader> Reader;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000580
Sebastian Redl2c499f62010-08-18 23:56:43 +0000581 Reader.reset(new ASTReader(AST->getSourceManager(), AST->getFileManager(),
Chris Lattner5159f612010-11-23 08:35:12 +0000582 AST->getDiagnostics()));
Ted Kremenek2159b8d2011-05-04 23:27:12 +0000583
584 // Recover resources if we crash before exiting this method.
585 llvm::CrashRecoveryContextCleanupRegistrar<ASTReader>
586 ReaderCleanup(Reader.get());
587
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000588 Reader->setListener(new ASTInfoCollector(LangInfo, HeaderInfo, TargetTriple,
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000589 Predefines, Counter));
590
Sebastian Redl009e7f22010-10-05 16:15:19 +0000591 switch (Reader->ReadAST(Filename, ASTReader::MainFile)) {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000592 case ASTReader::Success:
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000593 break;
Mike Stump11289f42009-09-09 15:08:12 +0000594
Sebastian Redl2c499f62010-08-18 23:56:43 +0000595 case ASTReader::Failure:
596 case ASTReader::IgnorePCH:
Douglas Gregord03e8232010-04-05 21:10:19 +0000597 AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000598 return NULL;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000599 }
Mike Stump11289f42009-09-09 15:08:12 +0000600
Daniel Dunbara8a50932009-12-02 08:44:16 +0000601 AST->OriginalSourceFile = Reader->getOriginalSourceFile();
602
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000603 // AST file loaded successfully. Now create the preprocessor.
Mike Stump11289f42009-09-09 15:08:12 +0000604
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000605 // Get information about the target being compiled for.
Daniel Dunbarb9bbd542009-11-15 06:48:46 +0000606 //
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000607 // FIXME: This is broken, we should store the TargetOptions in the AST file.
Daniel Dunbarb9bbd542009-11-15 06:48:46 +0000608 TargetOptions TargetOpts;
609 TargetOpts.ABI = "";
John McCall1c456c82010-08-22 06:43:33 +0000610 TargetOpts.CXXABI = "";
Daniel Dunbarb9bbd542009-11-15 06:48:46 +0000611 TargetOpts.CPU = "";
612 TargetOpts.Features.clear();
613 TargetOpts.Triple = TargetTriple;
Ted Kremenek5e14d392011-03-21 18:40:17 +0000614 AST->Target = TargetInfo::CreateTargetInfo(AST->getDiagnostics(),
615 TargetOpts);
616 AST->PP = new Preprocessor(AST->getDiagnostics(), LangInfo, *AST->Target,
617 AST->getSourceManager(), HeaderInfo);
618 Preprocessor &PP = *AST->PP;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000619
Daniel Dunbarb7bbfdd2009-09-21 03:03:47 +0000620 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000621 PP.setCounterValue(Counter);
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000622 Reader->setPreprocessor(PP);
Mike Stump11289f42009-09-09 15:08:12 +0000623
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000624 // Create and initialize the ASTContext.
625
Ted Kremenek5e14d392011-03-21 18:40:17 +0000626 AST->Ctx = new ASTContext(LangInfo,
627 AST->getSourceManager(),
628 *AST->Target,
629 PP.getIdentifierTable(),
630 PP.getSelectorTable(),
631 PP.getBuiltinInfo(),
632 /* size_reserve = */0);
633 ASTContext &Context = *AST->Ctx;
Mike Stump11289f42009-09-09 15:08:12 +0000634
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000635 Reader->InitializeContext(Context);
Mike Stump11289f42009-09-09 15:08:12 +0000636
Sebastian Redl2c499f62010-08-18 23:56:43 +0000637 // Attach the AST reader to the AST context as an external AST
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000638 // source, so that declarations will be deserialized from the
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000639 // AST file as needed.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000640 ASTReader *ReaderPtr = Reader.get();
Douglas Gregor6fd55e02010-08-13 03:15:25 +0000641 llvm::OwningPtr<ExternalASTSource> Source(Reader.take());
Ted Kremenek2159b8d2011-05-04 23:27:12 +0000642
643 // Unregister the cleanup for ASTReader. It will get cleaned up
644 // by the ASTUnit cleanup.
645 ReaderCleanup.unregister();
646
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000647 Context.setExternalSource(Source);
648
Douglas Gregor6fd55e02010-08-13 03:15:25 +0000649 // Create an AST consumer, even though it isn't used.
650 AST->Consumer.reset(new ASTConsumer);
651
Sebastian Redl2c499f62010-08-18 23:56:43 +0000652 // Create a semantic analysis object and tell the AST reader about it.
Douglas Gregor6fd55e02010-08-13 03:15:25 +0000653 AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer));
654 AST->TheSema->Initialize();
655 ReaderPtr->InitializeSema(*AST->TheSema);
656
Mike Stump11289f42009-09-09 15:08:12 +0000657 return AST.take();
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000658}
Daniel Dunbar764c0822009-12-01 09:51:01 +0000659
660namespace {
661
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000662/// \brief Preprocessor callback class that updates a hash value with the names
663/// of all macros that have been defined by the translation unit.
664class MacroDefinitionTrackerPPCallbacks : public PPCallbacks {
665 unsigned &Hash;
666
667public:
668 explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { }
669
670 virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
671 Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash);
672 }
673};
674
675/// \brief Add the given declaration to the hash of all top-level entities.
676void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) {
677 if (!D)
678 return;
679
680 DeclContext *DC = D->getDeclContext();
681 if (!DC)
682 return;
683
684 if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit()))
685 return;
686
687 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
688 if (ND->getIdentifier())
689 Hash = llvm::HashString(ND->getIdentifier()->getName(), Hash);
690 else if (DeclarationName Name = ND->getDeclName()) {
691 std::string NameStr = Name.getAsString();
692 Hash = llvm::HashString(NameStr, Hash);
693 }
694 return;
695 }
696
697 if (ObjCForwardProtocolDecl *Forward
698 = dyn_cast<ObjCForwardProtocolDecl>(D)) {
699 for (ObjCForwardProtocolDecl::protocol_iterator
700 P = Forward->protocol_begin(),
701 PEnd = Forward->protocol_end();
702 P != PEnd; ++P)
703 AddTopLevelDeclarationToHash(*P, Hash);
704 return;
705 }
706
707 if (ObjCClassDecl *Class = llvm::dyn_cast<ObjCClassDecl>(D)) {
708 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
709 I != IEnd; ++I)
710 AddTopLevelDeclarationToHash(I->getInterface(), Hash);
711 return;
712 }
713}
714
Daniel Dunbar644dca02009-12-04 08:17:33 +0000715class TopLevelDeclTrackerConsumer : public ASTConsumer {
716 ASTUnit &Unit;
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000717 unsigned &Hash;
718
Daniel Dunbar644dca02009-12-04 08:17:33 +0000719public:
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000720 TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
721 : Unit(_Unit), Hash(Hash) {
722 Hash = 0;
723 }
724
Daniel Dunbar644dca02009-12-04 08:17:33 +0000725 void HandleTopLevelDecl(DeclGroupRef D) {
Ted Kremenekacc59c32010-05-03 20:16:35 +0000726 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
727 Decl *D = *it;
728 // FIXME: Currently ObjC method declarations are incorrectly being
729 // reported as top-level declarations, even though their DeclContext
730 // is the containing ObjC @interface/@implementation. This is a
731 // fundamental problem in the parser right now.
732 if (isa<ObjCMethodDecl>(D))
733 continue;
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000734
735 AddTopLevelDeclarationToHash(D, Hash);
Douglas Gregore9db88f2010-08-03 19:06:41 +0000736 Unit.addTopLevelDecl(D);
Ted Kremenekacc59c32010-05-03 20:16:35 +0000737 }
Daniel Dunbar644dca02009-12-04 08:17:33 +0000738 }
Sebastian Redleaa4ade2010-08-11 18:52:41 +0000739
740 // We're not interested in "interesting" decls.
741 void HandleInterestingDecl(DeclGroupRef) {}
Daniel Dunbar644dca02009-12-04 08:17:33 +0000742};
743
744class TopLevelDeclTrackerAction : public ASTFrontendAction {
745public:
746 ASTUnit &Unit;
747
Daniel Dunbar764c0822009-12-01 09:51:01 +0000748 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
749 llvm::StringRef InFile) {
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000750 CI.getPreprocessor().addPPCallbacks(
751 new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
752 return new TopLevelDeclTrackerConsumer(Unit,
753 Unit.getCurrentTopLevelHashValue());
Daniel Dunbar764c0822009-12-01 09:51:01 +0000754 }
755
756public:
Daniel Dunbar644dca02009-12-04 08:17:33 +0000757 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
758
Daniel Dunbar764c0822009-12-01 09:51:01 +0000759 virtual bool hasCodeCompletionSupport() const { return false; }
Douglas Gregor028d3e42010-08-09 20:45:32 +0000760 virtual bool usesCompleteTranslationUnit() {
761 return Unit.isCompleteTranslationUnit();
762 }
Daniel Dunbar764c0822009-12-01 09:51:01 +0000763};
764
Douglas Gregorf88e35b2010-11-30 06:16:57 +0000765class PrecompilePreambleConsumer : public PCHGenerator,
766 public ASTSerializationListener {
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000767 ASTUnit &Unit;
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000768 unsigned &Hash;
Douglas Gregore9db88f2010-08-03 19:06:41 +0000769 std::vector<Decl *> TopLevelDecls;
Douglas Gregorf88e35b2010-11-30 06:16:57 +0000770
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000771public:
772 PrecompilePreambleConsumer(ASTUnit &Unit,
773 const Preprocessor &PP, bool Chaining,
774 const char *isysroot, llvm::raw_ostream *Out)
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000775 : PCHGenerator(PP, "", Chaining, isysroot, Out), Unit(Unit),
776 Hash(Unit.getCurrentTopLevelHashValue()) {
777 Hash = 0;
778 }
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000779
Douglas Gregore9db88f2010-08-03 19:06:41 +0000780 virtual void HandleTopLevelDecl(DeclGroupRef D) {
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000781 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
782 Decl *D = *it;
783 // FIXME: Currently ObjC method declarations are incorrectly being
784 // reported as top-level declarations, even though their DeclContext
785 // is the containing ObjC @interface/@implementation. This is a
786 // fundamental problem in the parser right now.
787 if (isa<ObjCMethodDecl>(D))
788 continue;
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000789 AddTopLevelDeclarationToHash(D, Hash);
Douglas Gregore9db88f2010-08-03 19:06:41 +0000790 TopLevelDecls.push_back(D);
791 }
792 }
793
794 virtual void HandleTranslationUnit(ASTContext &Ctx) {
795 PCHGenerator::HandleTranslationUnit(Ctx);
796 if (!Unit.getDiagnostics().hasErrorOccurred()) {
797 // Translate the top-level declarations we captured during
798 // parsing into declaration IDs in the precompiled
799 // preamble. This will allow us to deserialize those top-level
800 // declarations when requested.
801 for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I)
802 Unit.addTopLevelDeclFromPreamble(
803 getWriter().getDeclID(TopLevelDecls[I]));
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000804 }
805 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +0000806
807 virtual void SerializedPreprocessedEntity(PreprocessedEntity *Entity,
808 uint64_t Offset) {
809 Unit.addPreprocessedEntityFromPreamble(Offset);
810 }
811
812 virtual ASTSerializationListener *GetASTSerializationListener() {
813 return this;
814 }
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000815};
816
817class PrecompilePreambleAction : public ASTFrontendAction {
818 ASTUnit &Unit;
819
820public:
821 explicit PrecompilePreambleAction(ASTUnit &Unit) : Unit(Unit) {}
822
823 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
824 llvm::StringRef InFile) {
825 std::string Sysroot;
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000826 std::string OutputFile;
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000827 llvm::raw_ostream *OS = 0;
828 bool Chaining;
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000829 if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot,
830 OutputFile,
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000831 OS, Chaining))
832 return 0;
833
834 const char *isysroot = CI.getFrontendOpts().RelocatablePCH ?
835 Sysroot.c_str() : 0;
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000836 CI.getPreprocessor().addPPCallbacks(
837 new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000838 return new PrecompilePreambleConsumer(Unit, CI.getPreprocessor(), Chaining,
839 isysroot, OS);
840 }
841
842 virtual bool hasCodeCompletionSupport() const { return false; }
843 virtual bool hasASTFileSupport() const { return false; }
Douglas Gregor028d3e42010-08-09 20:45:32 +0000844 virtual bool usesCompleteTranslationUnit() { return false; }
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000845};
846
Daniel Dunbar764c0822009-12-01 09:51:01 +0000847}
848
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000849/// Parse the source file into a translation unit using the given compiler
850/// invocation, replacing the current translation unit.
851///
852/// \returns True if a failure occurred that causes the ASTUnit not to
853/// contain any translation-unit information, false otherwise.
Douglas Gregor6481ef12010-07-24 00:38:13 +0000854bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) {
Douglas Gregor96c04262010-07-27 14:52:07 +0000855 delete SavedMainFileBuffer;
856 SavedMainFileBuffer = 0;
857
Ted Kremenek5e14d392011-03-21 18:40:17 +0000858 if (!Invocation) {
Douglas Gregora0734c52010-08-19 01:33:06 +0000859 delete OverrideMainBuffer;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000860 return true;
Douglas Gregora0734c52010-08-19 01:33:06 +0000861 }
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000862
Daniel Dunbar764c0822009-12-01 09:51:01 +0000863 // Create the compiler instance to use for building the AST.
Ted Kremenek84de4a12011-03-21 18:40:07 +0000864 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
865
866 // Recover resources if we crash before exiting this method.
Ted Kremenek022a4902011-03-22 01:15:24 +0000867 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
868 CICleanup(Clang.get());
Ted Kremenek84de4a12011-03-21 18:40:07 +0000869
Ted Kremenek5e14d392011-03-21 18:40:17 +0000870 Clang->setInvocation(&*Invocation);
Ted Kremenek84de4a12011-03-21 18:40:07 +0000871 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000872
Douglas Gregor8e984da2010-08-04 16:47:14 +0000873 // Set up diagnostics, capturing any diagnostics that would
874 // otherwise be dropped.
Ted Kremenek84de4a12011-03-21 18:40:07 +0000875 Clang->setDiagnostics(&getDiagnostics());
Douglas Gregord03e8232010-04-05 21:10:19 +0000876
Daniel Dunbar764c0822009-12-01 09:51:01 +0000877 // Create the target instance.
Ted Kremenek84de4a12011-03-21 18:40:07 +0000878 Clang->getTargetOpts().Features = TargetFeatures;
879 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
Ted Kremenek5e14d392011-03-21 18:40:17 +0000880 Clang->getTargetOpts()));
Ted Kremenek84de4a12011-03-21 18:40:07 +0000881 if (!Clang->hasTarget()) {
Douglas Gregora0734c52010-08-19 01:33:06 +0000882 delete OverrideMainBuffer;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000883 return true;
Douglas Gregora0734c52010-08-19 01:33:06 +0000884 }
885
Daniel Dunbar764c0822009-12-01 09:51:01 +0000886 // Inform the target of the language options.
887 //
888 // FIXME: We shouldn't need to do this, the target should be immutable once
889 // created. This complexity should be lifted elsewhere.
Ted Kremenek84de4a12011-03-21 18:40:07 +0000890 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000891
Ted Kremenek84de4a12011-03-21 18:40:07 +0000892 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
Daniel Dunbar764c0822009-12-01 09:51:01 +0000893 "Invocation must have exactly one source file!");
Ted Kremenek84de4a12011-03-21 18:40:07 +0000894 assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
Daniel Dunbar764c0822009-12-01 09:51:01 +0000895 "FIXME: AST inputs not yet supported here!");
Ted Kremenek84de4a12011-03-21 18:40:07 +0000896 assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000897 "IR inputs not support here!");
Daniel Dunbar764c0822009-12-01 09:51:01 +0000898
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000899 // Configure the various subsystems.
900 // FIXME: Should we retain the previous file manager?
Ted Kremenek84de4a12011-03-21 18:40:07 +0000901 FileSystemOpts = Clang->getFileSystemOpts();
Ted Kremenek5e14d392011-03-21 18:40:17 +0000902 FileMgr = new FileManager(FileSystemOpts);
903 SourceMgr = new SourceManager(getDiagnostics(), *FileMgr);
Douglas Gregor6fd55e02010-08-13 03:15:25 +0000904 TheSema.reset();
Ted Kremenek5e14d392011-03-21 18:40:17 +0000905 Ctx = 0;
906 PP = 0;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000907
908 // Clear out old caches and data.
909 TopLevelDecls.clear();
Douglas Gregorf88e35b2010-11-30 06:16:57 +0000910 PreprocessedEntities.clear();
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000911 CleanTemporaryFiles();
912 PreprocessedEntitiesByFile.clear();
Douglas Gregord9a30af2010-08-02 20:51:39 +0000913
Douglas Gregor7b02b582010-08-20 00:02:33 +0000914 if (!OverrideMainBuffer) {
Douglas Gregor7bb8af62010-10-12 00:50:20 +0000915 StoredDiagnostics.erase(
916 StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver,
917 StoredDiagnostics.end());
Douglas Gregor7b02b582010-08-20 00:02:33 +0000918 TopLevelDeclsInPreamble.clear();
Douglas Gregorf88e35b2010-11-30 06:16:57 +0000919 PreprocessedEntitiesInPreamble.clear();
Douglas Gregor7b02b582010-08-20 00:02:33 +0000920 }
921
Daniel Dunbar764c0822009-12-01 09:51:01 +0000922 // Create a file manager object to provide access to and cache the filesystem.
Ted Kremenek84de4a12011-03-21 18:40:07 +0000923 Clang->setFileManager(&getFileManager());
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000924
Daniel Dunbar764c0822009-12-01 09:51:01 +0000925 // Create the source manager.
Ted Kremenek84de4a12011-03-21 18:40:07 +0000926 Clang->setSourceManager(&getSourceManager());
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000927
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000928 // If the main file has been overridden due to the use of a preamble,
929 // make that override happen and introduce the preamble.
Ted Kremenek84de4a12011-03-21 18:40:07 +0000930 PreprocessorOptions &PreprocessorOpts = Clang->getPreprocessorOpts();
Douglas Gregor998caea2011-05-06 16:33:08 +0000931 PreprocessorOpts.DetailedRecordIncludesNestedMacroInstantiations
932 = NestedMacroInstantiations;
Douglas Gregor8e984da2010-08-04 16:47:14 +0000933 std::string PriorImplicitPCHInclude;
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000934 if (OverrideMainBuffer) {
935 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
936 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
937 PreprocessorOpts.PrecompiledPreambleBytes.second
938 = PreambleEndsAtStartOfLine;
Douglas Gregor8e984da2010-08-04 16:47:14 +0000939 PriorImplicitPCHInclude = PreprocessorOpts.ImplicitPCHInclude;
Douglas Gregor15ba0b32010-07-30 20:58:08 +0000940 PreprocessorOpts.ImplicitPCHInclude = PreambleFile;
Douglas Gregorce3a8292010-07-27 00:27:13 +0000941 PreprocessorOpts.DisablePCHValidation = true;
Douglas Gregor96c04262010-07-27 14:52:07 +0000942
Douglas Gregord9a30af2010-08-02 20:51:39 +0000943 // The stored diagnostic has the old source manager in it; update
944 // the locations to refer into the new source manager. Since we've
945 // been careful to make sure that the source manager's state
946 // before and after are identical, so that we can reuse the source
947 // location itself.
Douglas Gregor7bb8af62010-10-12 00:50:20 +0000948 for (unsigned I = NumStoredDiagnosticsFromDriver,
949 N = StoredDiagnostics.size();
950 I < N; ++I) {
Douglas Gregord9a30af2010-08-02 20:51:39 +0000951 FullSourceLoc Loc(StoredDiagnostics[I].getLocation(),
952 getSourceManager());
953 StoredDiagnostics[I].setLocation(Loc);
954 }
Douglas Gregor7bb8af62010-10-12 00:50:20 +0000955
956 // Keep track of the override buffer;
957 SavedMainFileBuffer = OverrideMainBuffer;
Douglas Gregor7b02b582010-08-20 00:02:33 +0000958 } else {
959 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
960 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000961 }
962
Ted Kremenek022a4902011-03-22 01:15:24 +0000963 llvm::OwningPtr<TopLevelDeclTrackerAction> Act(
964 new TopLevelDeclTrackerAction(*this));
965
966 // Recover resources if we crash before exiting this method.
967 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
968 ActCleanup(Act.get());
969
Ted Kremenek84de4a12011-03-21 18:40:07 +0000970 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
971 Clang->getFrontendOpts().Inputs[0].first))
Daniel Dunbar764c0822009-12-01 09:51:01 +0000972 goto error;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000973
Daniel Dunbar644dca02009-12-04 08:17:33 +0000974 Act->Execute();
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000975
Ted Kremenek5e14d392011-03-21 18:40:17 +0000976 // Steal the created target, context, and preprocessor.
Ted Kremenek84de4a12011-03-21 18:40:07 +0000977 TheSema.reset(Clang->takeSema());
978 Consumer.reset(Clang->takeASTConsumer());
Ted Kremenek5e14d392011-03-21 18:40:17 +0000979 Ctx = &Clang->getASTContext();
980 PP = &Clang->getPreprocessor();
981 Clang->setSourceManager(0);
982 Clang->setFileManager(0);
983 Target = &Clang->getTarget();
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000984
Daniel Dunbar644dca02009-12-04 08:17:33 +0000985 Act->EndSourceFile();
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000986
987 // Remove the overridden buffer we used for the preamble.
Douglas Gregor8e984da2010-08-04 16:47:14 +0000988 if (OverrideMainBuffer) {
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000989 PreprocessorOpts.eraseRemappedFile(
990 PreprocessorOpts.remapped_file_buffer_end() - 1);
Douglas Gregor8e984da2010-08-04 16:47:14 +0000991 PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude;
992 }
993
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000994 return false;
Ted Kremenek5e14d392011-03-21 18:40:17 +0000995
Daniel Dunbar764c0822009-12-01 09:51:01 +0000996error:
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000997 // Remove the overridden buffer we used for the preamble.
Douglas Gregorce3a8292010-07-27 00:27:13 +0000998 if (OverrideMainBuffer) {
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000999 PreprocessorOpts.eraseRemappedFile(
1000 PreprocessorOpts.remapped_file_buffer_end() - 1);
Douglas Gregor8e984da2010-08-04 16:47:14 +00001001 PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude;
Douglas Gregora0734c52010-08-19 01:33:06 +00001002 delete OverrideMainBuffer;
Douglas Gregora3d3ba12010-10-06 21:11:08 +00001003 SavedMainFileBuffer = 0;
Douglas Gregorce3a8292010-07-27 00:27:13 +00001004 }
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001005
Douglas Gregorefc46952010-10-12 16:25:54 +00001006 StoredDiagnostics.clear();
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001007 return true;
1008}
1009
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001010/// \brief Simple function to retrieve a path for a preamble precompiled header.
1011static std::string GetPreamblePCHPath() {
1012 // FIXME: This is lame; sys::Path should provide this function (in particular,
1013 // it should know how to find the temporary files dir).
1014 // FIXME: This is really lame. I copied this code from the Driver!
Douglas Gregor250ab1d2010-09-11 18:05:19 +00001015 // FIXME: This is a hack so that we can override the preamble file during
1016 // crash-recovery testing, which is the only case where the preamble files
1017 // are not necessarily cleaned up.
1018 const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
1019 if (TmpFile)
1020 return TmpFile;
1021
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001022 std::string Error;
1023 const char *TmpDir = ::getenv("TMPDIR");
1024 if (!TmpDir)
1025 TmpDir = ::getenv("TEMP");
1026 if (!TmpDir)
1027 TmpDir = ::getenv("TMP");
Douglas Gregorce3449f2010-09-11 17:51:16 +00001028#ifdef LLVM_ON_WIN32
1029 if (!TmpDir)
1030 TmpDir = ::getenv("USERPROFILE");
1031#endif
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001032 if (!TmpDir)
1033 TmpDir = "/tmp";
1034 llvm::sys::Path P(TmpDir);
Douglas Gregorce3449f2010-09-11 17:51:16 +00001035 P.createDirectoryOnDisk(true);
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001036 P.appendComponent("preamble");
Douglas Gregor20975b22010-08-11 13:06:56 +00001037 P.appendSuffix("pch");
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001038 if (P.createTemporaryFileOnDisk())
1039 return std::string();
1040
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001041 return P.str();
1042}
1043
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001044/// \brief Compute the preamble for the main file, providing the source buffer
1045/// that corresponds to the main file along with a pair (bytes, start-of-line)
1046/// that describes the preamble.
1047std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
Douglas Gregor028d3e42010-08-09 20:45:32 +00001048ASTUnit::ComputePreamble(CompilerInvocation &Invocation,
1049 unsigned MaxLines, bool &CreatedBuffer) {
Douglas Gregor4dde7492010-07-23 23:58:40 +00001050 FrontendOptions &FrontendOpts = Invocation.getFrontendOpts();
Chris Lattner5159f612010-11-23 08:35:12 +00001051 PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts();
Douglas Gregor4dde7492010-07-23 23:58:40 +00001052 CreatedBuffer = false;
1053
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001054 // Try to determine if the main file has been remapped, either from the
1055 // command line (to another file) or directly through the compiler invocation
1056 // (to a memory buffer).
Douglas Gregor4dde7492010-07-23 23:58:40 +00001057 llvm::MemoryBuffer *Buffer = 0;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001058 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second);
1059 if (const llvm::sys::FileStatus *MainFileStatus = MainFilePath.getFileStatus()) {
1060 // Check whether there is a file-file remapping of the main file
1061 for (PreprocessorOptions::remapped_file_iterator
Douglas Gregor4dde7492010-07-23 23:58:40 +00001062 M = PreprocessorOpts.remapped_file_begin(),
1063 E = PreprocessorOpts.remapped_file_end();
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001064 M != E;
1065 ++M) {
1066 llvm::sys::PathWithStatus MPath(M->first);
1067 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1068 if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1069 // We found a remapping. Try to load the resulting, remapped source.
Douglas Gregor4dde7492010-07-23 23:58:40 +00001070 if (CreatedBuffer) {
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001071 delete Buffer;
Douglas Gregor4dde7492010-07-23 23:58:40 +00001072 CreatedBuffer = false;
1073 }
1074
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00001075 Buffer = getBufferForFile(M->second);
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001076 if (!Buffer)
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001077 return std::make_pair((llvm::MemoryBuffer*)0,
1078 std::make_pair(0, true));
Douglas Gregor4dde7492010-07-23 23:58:40 +00001079 CreatedBuffer = true;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001080 }
1081 }
1082 }
1083
1084 // Check whether there is a file-buffer remapping. It supercedes the
1085 // file-file remapping.
1086 for (PreprocessorOptions::remapped_file_buffer_iterator
1087 M = PreprocessorOpts.remapped_file_buffer_begin(),
1088 E = PreprocessorOpts.remapped_file_buffer_end();
1089 M != E;
1090 ++M) {
1091 llvm::sys::PathWithStatus MPath(M->first);
1092 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1093 if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1094 // We found a remapping.
Douglas Gregor4dde7492010-07-23 23:58:40 +00001095 if (CreatedBuffer) {
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001096 delete Buffer;
Douglas Gregor4dde7492010-07-23 23:58:40 +00001097 CreatedBuffer = false;
1098 }
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001099
Douglas Gregor4dde7492010-07-23 23:58:40 +00001100 Buffer = const_cast<llvm::MemoryBuffer *>(M->second);
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001101 }
1102 }
Douglas Gregor4dde7492010-07-23 23:58:40 +00001103 }
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001104 }
1105
1106 // If the main source file was not remapped, load it now.
1107 if (!Buffer) {
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00001108 Buffer = getBufferForFile(FrontendOpts.Inputs[0].second);
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001109 if (!Buffer)
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001110 return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true));
Douglas Gregor4dde7492010-07-23 23:58:40 +00001111
1112 CreatedBuffer = true;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001113 }
1114
Douglas Gregor028d3e42010-08-09 20:45:32 +00001115 return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer, MaxLines));
Douglas Gregor4dde7492010-07-23 23:58:40 +00001116}
1117
Douglas Gregor6481ef12010-07-24 00:38:13 +00001118static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old,
Douglas Gregor6481ef12010-07-24 00:38:13 +00001119 unsigned NewSize,
1120 llvm::StringRef NewName) {
1121 llvm::MemoryBuffer *Result
1122 = llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName);
1123 memcpy(const_cast<char*>(Result->getBufferStart()),
1124 Old->getBufferStart(), Old->getBufferSize());
1125 memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(),
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001126 ' ', NewSize - Old->getBufferSize() - 1);
1127 const_cast<char*>(Result->getBufferEnd())[-1] = '\n';
Douglas Gregor6481ef12010-07-24 00:38:13 +00001128
Douglas Gregor6481ef12010-07-24 00:38:13 +00001129 return Result;
1130}
1131
Douglas Gregor4dde7492010-07-23 23:58:40 +00001132/// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing
1133/// the source file.
1134///
1135/// This routine will compute the preamble of the main source file. If a
1136/// non-trivial preamble is found, it will precompile that preamble into a
1137/// precompiled header so that the precompiled preamble can be used to reduce
1138/// reparsing time. If a precompiled preamble has already been constructed,
1139/// this routine will determine if it is still valid and, if so, avoid
1140/// rebuilding the precompiled preamble.
1141///
Douglas Gregor028d3e42010-08-09 20:45:32 +00001142/// \param AllowRebuild When true (the default), this routine is
1143/// allowed to rebuild the precompiled preamble if it is found to be
1144/// out-of-date.
1145///
1146/// \param MaxLines When non-zero, the maximum number of lines that
1147/// can occur within the preamble.
1148///
Douglas Gregor6481ef12010-07-24 00:38:13 +00001149/// \returns If the precompiled preamble can be used, returns a newly-allocated
1150/// buffer that should be used in place of the main file when doing so.
1151/// Otherwise, returns a NULL pointer.
Douglas Gregor028d3e42010-08-09 20:45:32 +00001152llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
Douglas Gregorb97b6662010-08-20 00:59:43 +00001153 CompilerInvocation PreambleInvocation,
Douglas Gregor028d3e42010-08-09 20:45:32 +00001154 bool AllowRebuild,
1155 unsigned MaxLines) {
Douglas Gregor4dde7492010-07-23 23:58:40 +00001156 FrontendOptions &FrontendOpts = PreambleInvocation.getFrontendOpts();
1157 PreprocessorOptions &PreprocessorOpts
1158 = PreambleInvocation.getPreprocessorOpts();
1159
1160 bool CreatedPreambleBuffer = false;
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001161 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble
Douglas Gregor028d3e42010-08-09 20:45:32 +00001162 = ComputePreamble(PreambleInvocation, MaxLines, CreatedPreambleBuffer);
Douglas Gregor4dde7492010-07-23 23:58:40 +00001163
Douglas Gregor3edb1672010-11-16 20:45:51 +00001164 // If ComputePreamble() Take ownership of the
1165 llvm::OwningPtr<llvm::MemoryBuffer> OwnedPreambleBuffer;
1166 if (CreatedPreambleBuffer)
1167 OwnedPreambleBuffer.reset(NewPreamble.first);
1168
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001169 if (!NewPreamble.second.first) {
Douglas Gregor4dde7492010-07-23 23:58:40 +00001170 // We couldn't find a preamble in the main source. Clear out the current
1171 // preamble, if we have one. It's obviously no good any more.
1172 Preamble.clear();
1173 if (!PreambleFile.empty()) {
Douglas Gregor15ba0b32010-07-30 20:58:08 +00001174 llvm::sys::Path(PreambleFile).eraseFromDisk();
Douglas Gregor4dde7492010-07-23 23:58:40 +00001175 PreambleFile.clear();
1176 }
Douglas Gregorbb420ab2010-08-04 05:53:38 +00001177
1178 // The next time we actually see a preamble, precompile it.
1179 PreambleRebuildCounter = 1;
Douglas Gregor6481ef12010-07-24 00:38:13 +00001180 return 0;
Douglas Gregor4dde7492010-07-23 23:58:40 +00001181 }
1182
1183 if (!Preamble.empty()) {
1184 // We've previously computed a preamble. Check whether we have the same
1185 // preamble now that we did before, and that there's enough space in
1186 // the main-file buffer within the precompiled preamble to fit the
1187 // new main file.
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001188 if (Preamble.size() == NewPreamble.second.first &&
1189 PreambleEndsAtStartOfLine == NewPreamble.second.second &&
Douglas Gregorf5275a82010-07-24 00:42:07 +00001190 NewPreamble.first->getBufferSize() < PreambleReservedSize-2 &&
Douglas Gregor4dde7492010-07-23 23:58:40 +00001191 memcmp(&Preamble[0], NewPreamble.first->getBufferStart(),
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001192 NewPreamble.second.first) == 0) {
Douglas Gregor4dde7492010-07-23 23:58:40 +00001193 // The preamble has not changed. We may be able to re-use the precompiled
1194 // preamble.
Douglas Gregord9a30af2010-08-02 20:51:39 +00001195
Douglas Gregor0e119552010-07-31 00:40:00 +00001196 // Check that none of the files used by the preamble have changed.
1197 bool AnyFileChanged = false;
1198
1199 // First, make a record of those files that have been overridden via
1200 // remapping or unsaved_files.
1201 llvm::StringMap<std::pair<off_t, time_t> > OverriddenFiles;
1202 for (PreprocessorOptions::remapped_file_iterator
1203 R = PreprocessorOpts.remapped_file_begin(),
1204 REnd = PreprocessorOpts.remapped_file_end();
1205 !AnyFileChanged && R != REnd;
1206 ++R) {
1207 struct stat StatBuf;
Anders Carlsson9583f792011-03-18 19:23:38 +00001208 if (FileMgr->getNoncachedStatValue(R->second, StatBuf)) {
Douglas Gregor0e119552010-07-31 00:40:00 +00001209 // If we can't stat the file we're remapping to, assume that something
1210 // horrible happened.
1211 AnyFileChanged = true;
1212 break;
1213 }
Douglas Gregor6481ef12010-07-24 00:38:13 +00001214
Douglas Gregor0e119552010-07-31 00:40:00 +00001215 OverriddenFiles[R->first] = std::make_pair(StatBuf.st_size,
1216 StatBuf.st_mtime);
1217 }
1218 for (PreprocessorOptions::remapped_file_buffer_iterator
1219 R = PreprocessorOpts.remapped_file_buffer_begin(),
1220 REnd = PreprocessorOpts.remapped_file_buffer_end();
1221 !AnyFileChanged && R != REnd;
1222 ++R) {
1223 // FIXME: Should we actually compare the contents of file->buffer
1224 // remappings?
1225 OverriddenFiles[R->first] = std::make_pair(R->second->getBufferSize(),
1226 0);
1227 }
1228
1229 // Check whether anything has changed.
1230 for (llvm::StringMap<std::pair<off_t, time_t> >::iterator
1231 F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end();
1232 !AnyFileChanged && F != FEnd;
1233 ++F) {
1234 llvm::StringMap<std::pair<off_t, time_t> >::iterator Overridden
1235 = OverriddenFiles.find(F->first());
1236 if (Overridden != OverriddenFiles.end()) {
1237 // This file was remapped; check whether the newly-mapped file
1238 // matches up with the previous mapping.
1239 if (Overridden->second != F->second)
1240 AnyFileChanged = true;
1241 continue;
1242 }
1243
1244 // The file was not remapped; check whether it has changed on disk.
1245 struct stat StatBuf;
Anders Carlsson9583f792011-03-18 19:23:38 +00001246 if (FileMgr->getNoncachedStatValue(F->first(), StatBuf)) {
Douglas Gregor0e119552010-07-31 00:40:00 +00001247 // If we can't stat the file, assume that something horrible happened.
1248 AnyFileChanged = true;
1249 } else if (StatBuf.st_size != F->second.first ||
1250 StatBuf.st_mtime != F->second.second)
1251 AnyFileChanged = true;
1252 }
1253
1254 if (!AnyFileChanged) {
Douglas Gregord9a30af2010-08-02 20:51:39 +00001255 // Okay! We can re-use the precompiled preamble.
1256
1257 // Set the state of the diagnostic object to mimic its state
1258 // after parsing the preamble.
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001259 // FIXME: This won't catch any #pragma push warning changes that
1260 // have occurred in the preamble.
Douglas Gregord9a30af2010-08-02 20:51:39 +00001261 getDiagnostics().Reset();
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001262 ProcessWarningOptions(getDiagnostics(),
1263 PreambleInvocation.getDiagnosticOpts());
Douglas Gregord9a30af2010-08-02 20:51:39 +00001264 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1265 if (StoredDiagnostics.size() > NumStoredDiagnosticsInPreamble)
1266 StoredDiagnostics.erase(
1267 StoredDiagnostics.begin() + NumStoredDiagnosticsInPreamble,
1268 StoredDiagnostics.end());
1269
1270 // Create a version of the main file buffer that is padded to
1271 // buffer size we reserved when creating the preamble.
Douglas Gregor0e119552010-07-31 00:40:00 +00001272 return CreatePaddedMainFileBuffer(NewPreamble.first,
Douglas Gregor0e119552010-07-31 00:40:00 +00001273 PreambleReservedSize,
1274 FrontendOpts.Inputs[0].second);
1275 }
Douglas Gregor4dde7492010-07-23 23:58:40 +00001276 }
Douglas Gregor028d3e42010-08-09 20:45:32 +00001277
1278 // If we aren't allowed to rebuild the precompiled preamble, just
1279 // return now.
1280 if (!AllowRebuild)
1281 return 0;
Douglas Gregorbb6a8812010-10-08 04:03:57 +00001282
Douglas Gregor4dde7492010-07-23 23:58:40 +00001283 // We can't reuse the previously-computed preamble. Build a new one.
1284 Preamble.clear();
Douglas Gregor15ba0b32010-07-30 20:58:08 +00001285 llvm::sys::Path(PreambleFile).eraseFromDisk();
Douglas Gregorbb420ab2010-08-04 05:53:38 +00001286 PreambleRebuildCounter = 1;
Douglas Gregor028d3e42010-08-09 20:45:32 +00001287 } else if (!AllowRebuild) {
1288 // We aren't allowed to rebuild the precompiled preamble; just
1289 // return now.
1290 return 0;
1291 }
Douglas Gregorbb420ab2010-08-04 05:53:38 +00001292
1293 // If the preamble rebuild counter > 1, it's because we previously
1294 // failed to build a preamble and we're not yet ready to try
1295 // again. Decrement the counter and return a failure.
1296 if (PreambleRebuildCounter > 1) {
1297 --PreambleRebuildCounter;
1298 return 0;
1299 }
1300
Douglas Gregore10f0e52010-09-11 17:56:52 +00001301 // Create a temporary file for the precompiled preamble. In rare
1302 // circumstances, this can fail.
1303 std::string PreamblePCHPath = GetPreamblePCHPath();
1304 if (PreamblePCHPath.empty()) {
1305 // Try again next time.
1306 PreambleRebuildCounter = 1;
1307 return 0;
1308 }
1309
Douglas Gregor4dde7492010-07-23 23:58:40 +00001310 // We did not previously compute a preamble, or it can't be reused anyway.
Douglas Gregor16896c42010-10-28 15:44:59 +00001311 SimpleTimer PreambleTimer(WantTiming);
Benjamin Kramerf2e5a912010-11-09 20:00:56 +00001312 PreambleTimer.setOutput("Precompiling preamble");
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001313
1314 // Create a new buffer that stores the preamble. The buffer also contains
1315 // extra space for the original contents of the file (which will be present
1316 // when we actually parse the file) along with more room in case the file
Douglas Gregor4dde7492010-07-23 23:58:40 +00001317 // grows.
1318 PreambleReservedSize = NewPreamble.first->getBufferSize();
1319 if (PreambleReservedSize < 4096)
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001320 PreambleReservedSize = 8191;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001321 else
Douglas Gregor4dde7492010-07-23 23:58:40 +00001322 PreambleReservedSize *= 2;
1323
Douglas Gregord9a30af2010-08-02 20:51:39 +00001324 // Save the preamble text for later; we'll need to compare against it for
1325 // subsequent reparses.
1326 Preamble.assign(NewPreamble.first->getBufferStart(),
1327 NewPreamble.first->getBufferStart()
1328 + NewPreamble.second.first);
1329 PreambleEndsAtStartOfLine = NewPreamble.second.second;
1330
Douglas Gregora0734c52010-08-19 01:33:06 +00001331 delete PreambleBuffer;
1332 PreambleBuffer
Douglas Gregor4dde7492010-07-23 23:58:40 +00001333 = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize,
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001334 FrontendOpts.Inputs[0].second);
1335 memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()),
Douglas Gregor4dde7492010-07-23 23:58:40 +00001336 NewPreamble.first->getBufferStart(), Preamble.size());
1337 memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(),
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001338 ' ', PreambleReservedSize - Preamble.size() - 1);
1339 const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n';
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001340
1341 // Remap the main source file to the preamble buffer.
Douglas Gregor4dde7492010-07-23 23:58:40 +00001342 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second);
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001343 PreprocessorOpts.addRemappedFile(MainFilePath.str(), PreambleBuffer);
1344
1345 // Tell the compiler invocation to generate a temporary precompiled header.
1346 FrontendOpts.ProgramAction = frontend::GeneratePCH;
Douglas Gregor9e136b52010-10-01 01:05:22 +00001347 FrontendOpts.ChainedPCH = true;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001348 // FIXME: Generate the precompiled header into memory?
Douglas Gregore10f0e52010-09-11 17:56:52 +00001349 FrontendOpts.OutputFile = PreamblePCHPath;
Douglas Gregorbb6a8812010-10-08 04:03:57 +00001350 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
1351 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001352
1353 // Create the compiler instance to use for building the precompiled preamble.
Ted Kremenek84de4a12011-03-21 18:40:07 +00001354 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1355
1356 // Recover resources if we crash before exiting this method.
Ted Kremenek022a4902011-03-22 01:15:24 +00001357 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1358 CICleanup(Clang.get());
Ted Kremenek84de4a12011-03-21 18:40:07 +00001359
1360 Clang->setInvocation(&PreambleInvocation);
1361 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001362
Douglas Gregor8e984da2010-08-04 16:47:14 +00001363 // Set up diagnostics, capturing all of the diagnostics produced.
Ted Kremenek84de4a12011-03-21 18:40:07 +00001364 Clang->setDiagnostics(&getDiagnostics());
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001365
1366 // Create the target instance.
Ted Kremenek84de4a12011-03-21 18:40:07 +00001367 Clang->getTargetOpts().Features = TargetFeatures;
1368 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1369 Clang->getTargetOpts()));
1370 if (!Clang->hasTarget()) {
Douglas Gregor4dde7492010-07-23 23:58:40 +00001371 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1372 Preamble.clear();
Douglas Gregorbb420ab2010-08-04 05:53:38 +00001373 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
Douglas Gregora0734c52010-08-19 01:33:06 +00001374 PreprocessorOpts.eraseRemappedFile(
1375 PreprocessorOpts.remapped_file_buffer_end() - 1);
Douglas Gregor6481ef12010-07-24 00:38:13 +00001376 return 0;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001377 }
1378
1379 // Inform the target of the language options.
1380 //
1381 // FIXME: We shouldn't need to do this, the target should be immutable once
1382 // created. This complexity should be lifted elsewhere.
Ted Kremenek84de4a12011-03-21 18:40:07 +00001383 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001384
Ted Kremenek84de4a12011-03-21 18:40:07 +00001385 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001386 "Invocation must have exactly one source file!");
Ted Kremenek84de4a12011-03-21 18:40:07 +00001387 assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001388 "FIXME: AST inputs not yet supported here!");
Ted Kremenek84de4a12011-03-21 18:40:07 +00001389 assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001390 "IR inputs not support here!");
1391
1392 // Clear out old caches and data.
Douglas Gregorbb6a8812010-10-08 04:03:57 +00001393 getDiagnostics().Reset();
Ted Kremenek84de4a12011-03-21 18:40:07 +00001394 ProcessWarningOptions(getDiagnostics(), Clang->getDiagnosticOpts());
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001395 StoredDiagnostics.erase(
1396 StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver,
1397 StoredDiagnostics.end());
Douglas Gregore9db88f2010-08-03 19:06:41 +00001398 TopLevelDecls.clear();
1399 TopLevelDeclsInPreamble.clear();
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001400 PreprocessedEntities.clear();
1401 PreprocessedEntitiesInPreamble.clear();
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001402
1403 // Create a file manager object to provide access to and cache the filesystem.
Ted Kremenek84de4a12011-03-21 18:40:07 +00001404 Clang->setFileManager(new FileManager(Clang->getFileSystemOpts()));
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001405
1406 // Create the source manager.
Ted Kremenek84de4a12011-03-21 18:40:07 +00001407 Clang->setSourceManager(new SourceManager(getDiagnostics(),
Ted Kremenek5e14d392011-03-21 18:40:17 +00001408 Clang->getFileManager()));
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001409
Douglas Gregor48c8cd32010-08-03 08:14:03 +00001410 llvm::OwningPtr<PrecompilePreambleAction> Act;
1411 Act.reset(new PrecompilePreambleAction(*this));
Ted Kremenek84de4a12011-03-21 18:40:07 +00001412 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
1413 Clang->getFrontendOpts().Inputs[0].first)) {
Douglas Gregor4dde7492010-07-23 23:58:40 +00001414 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1415 Preamble.clear();
Douglas Gregorbb420ab2010-08-04 05:53:38 +00001416 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
Douglas Gregora0734c52010-08-19 01:33:06 +00001417 PreprocessorOpts.eraseRemappedFile(
1418 PreprocessorOpts.remapped_file_buffer_end() - 1);
Douglas Gregor6481ef12010-07-24 00:38:13 +00001419 return 0;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001420 }
1421
1422 Act->Execute();
1423 Act->EndSourceFile();
Ted Kremenek5e14d392011-03-21 18:40:17 +00001424
Douglas Gregore9db88f2010-08-03 19:06:41 +00001425 if (Diagnostics->hasErrorOccurred()) {
Douglas Gregor4dde7492010-07-23 23:58:40 +00001426 // There were errors parsing the preamble, so no precompiled header was
1427 // generated. Forget that we even tried.
Douglas Gregora6f74e22010-09-27 16:43:25 +00001428 // FIXME: Should we leave a note for ourselves to try again?
Douglas Gregor4dde7492010-07-23 23:58:40 +00001429 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1430 Preamble.clear();
Douglas Gregore9db88f2010-08-03 19:06:41 +00001431 TopLevelDeclsInPreamble.clear();
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001432 PreprocessedEntities.clear();
1433 PreprocessedEntitiesInPreamble.clear();
Douglas Gregorbb420ab2010-08-04 05:53:38 +00001434 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
Douglas Gregora0734c52010-08-19 01:33:06 +00001435 PreprocessorOpts.eraseRemappedFile(
1436 PreprocessorOpts.remapped_file_buffer_end() - 1);
Douglas Gregor6481ef12010-07-24 00:38:13 +00001437 return 0;
Douglas Gregor4dde7492010-07-23 23:58:40 +00001438 }
1439
1440 // Keep track of the preamble we precompiled.
1441 PreambleFile = FrontendOpts.OutputFile;
Douglas Gregord9a30af2010-08-02 20:51:39 +00001442 NumStoredDiagnosticsInPreamble = StoredDiagnostics.size();
1443 NumWarningsInPreamble = getDiagnostics().getNumWarnings();
Douglas Gregor0e119552010-07-31 00:40:00 +00001444
1445 // Keep track of all of the files that the source manager knows about,
1446 // so we can verify whether they have changed or not.
1447 FilesInPreamble.clear();
Ted Kremenek84de4a12011-03-21 18:40:07 +00001448 SourceManager &SourceMgr = Clang->getSourceManager();
Douglas Gregor0e119552010-07-31 00:40:00 +00001449 const llvm::MemoryBuffer *MainFileBuffer
1450 = SourceMgr.getBuffer(SourceMgr.getMainFileID());
1451 for (SourceManager::fileinfo_iterator F = SourceMgr.fileinfo_begin(),
1452 FEnd = SourceMgr.fileinfo_end();
1453 F != FEnd;
1454 ++F) {
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001455 const FileEntry *File = F->second->OrigEntry;
Douglas Gregor0e119552010-07-31 00:40:00 +00001456 if (!File || F->second->getRawBuffer() == MainFileBuffer)
1457 continue;
1458
1459 FilesInPreamble[File->getName()]
1460 = std::make_pair(F->second->getSize(), File->getModificationTime());
1461 }
1462
Douglas Gregorbb420ab2010-08-04 05:53:38 +00001463 PreambleRebuildCounter = 1;
Douglas Gregora0734c52010-08-19 01:33:06 +00001464 PreprocessorOpts.eraseRemappedFile(
1465 PreprocessorOpts.remapped_file_buffer_end() - 1);
Douglas Gregordf7a79a2011-02-16 18:16:54 +00001466
1467 // If the hash of top-level entities differs from the hash of the top-level
1468 // entities the last time we rebuilt the preamble, clear out the completion
1469 // cache.
1470 if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1471 CompletionCacheTopLevelHashValue = 0;
1472 PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1473 }
1474
Douglas Gregor6481ef12010-07-24 00:38:13 +00001475 return CreatePaddedMainFileBuffer(NewPreamble.first,
Douglas Gregor6481ef12010-07-24 00:38:13 +00001476 PreambleReservedSize,
1477 FrontendOpts.Inputs[0].second);
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001478}
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001479
Douglas Gregore9db88f2010-08-03 19:06:41 +00001480void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1481 std::vector<Decl *> Resolved;
1482 Resolved.reserve(TopLevelDeclsInPreamble.size());
1483 ExternalASTSource &Source = *getASTContext().getExternalSource();
1484 for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) {
1485 // Resolve the declaration ID to an actual declaration, possibly
1486 // deserializing the declaration in the process.
1487 Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]);
1488 if (D)
1489 Resolved.push_back(D);
1490 }
1491 TopLevelDeclsInPreamble.clear();
1492 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1493}
1494
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001495void ASTUnit::RealizePreprocessedEntitiesFromPreamble() {
1496 if (!PP)
1497 return;
1498
1499 PreprocessingRecord *PPRec = PP->getPreprocessingRecord();
1500 if (!PPRec)
1501 return;
1502
1503 ExternalPreprocessingRecordSource *External = PPRec->getExternalSource();
1504 if (!External)
1505 return;
1506
1507 for (unsigned I = 0, N = PreprocessedEntitiesInPreamble.size(); I != N; ++I) {
1508 if (PreprocessedEntity *PE
Douglas Gregor46c50012011-02-11 19:46:30 +00001509 = External->ReadPreprocessedEntityAtOffset(
1510 PreprocessedEntitiesInPreamble[I]))
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001511 PreprocessedEntities.push_back(PE);
1512 }
1513
1514 if (PreprocessedEntities.empty())
1515 return;
1516
1517 PreprocessedEntities.insert(PreprocessedEntities.end(),
1518 PPRec->begin(true), PPRec->end(true));
1519}
1520
1521ASTUnit::pp_entity_iterator ASTUnit::pp_entity_begin() {
1522 if (!PreprocessedEntitiesInPreamble.empty() &&
1523 PreprocessedEntities.empty())
1524 RealizePreprocessedEntitiesFromPreamble();
1525
1526 if (PreprocessedEntities.empty())
1527 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
1528 return PPRec->begin(true);
1529
1530 return PreprocessedEntities.begin();
1531}
1532
1533ASTUnit::pp_entity_iterator ASTUnit::pp_entity_end() {
1534 if (!PreprocessedEntitiesInPreamble.empty() &&
1535 PreprocessedEntities.empty())
1536 RealizePreprocessedEntitiesFromPreamble();
1537
1538 if (PreprocessedEntities.empty())
1539 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
1540 return PPRec->end(true);
1541
1542 return PreprocessedEntities.end();
1543}
1544
Douglas Gregore9db88f2010-08-03 19:06:41 +00001545unsigned ASTUnit::getMaxPCHLevel() const {
1546 if (!getOnlyLocalDecls())
1547 return Decl::MaxPCHLevel;
1548
Sebastian Redl009e7f22010-10-05 16:15:19 +00001549 return 0;
Douglas Gregore9db88f2010-08-03 19:06:41 +00001550}
1551
Douglas Gregor16896c42010-10-28 15:44:59 +00001552llvm::StringRef ASTUnit::getMainFileName() const {
1553 return Invocation->getFrontendOpts().Inputs[0].second;
1554}
1555
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +00001556ASTUnit *ASTUnit::create(CompilerInvocation *CI,
1557 llvm::IntrusiveRefCntPtr<Diagnostic> Diags) {
1558 llvm::OwningPtr<ASTUnit> AST;
1559 AST.reset(new ASTUnit(false));
1560 ConfigureDiags(Diags, 0, 0, *AST, /*CaptureDiagnostics=*/false);
1561 AST->Diagnostics = Diags;
Ted Kremenek5e14d392011-03-21 18:40:17 +00001562 AST->Invocation = CI;
Anders Carlssonc30dcec2011-03-18 18:22:40 +00001563 AST->FileSystemOpts = CI->getFileSystemOpts();
Ted Kremenek5e14d392011-03-21 18:40:17 +00001564 AST->FileMgr = new FileManager(AST->FileSystemOpts);
1565 AST->SourceMgr = new SourceManager(*Diags, *AST->FileMgr);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +00001566
1567 return AST.take();
1568}
1569
Argyrios Kyrtzidisf1f67592011-05-03 23:26:34 +00001570ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(CompilerInvocation *CI,
1571 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
1572 ASTFrontendAction *Action) {
1573 assert(CI && "A CompilerInvocation is required");
1574
1575 // Create the AST unit.
1576 llvm::OwningPtr<ASTUnit> AST;
1577 AST.reset(new ASTUnit(false));
1578 ConfigureDiags(Diags, 0, 0, *AST, /*CaptureDiagnostics*/false);
1579 AST->Diagnostics = Diags;
1580 AST->OnlyLocalDecls = false;
1581 AST->CaptureDiagnostics = false;
1582 AST->CompleteTranslationUnit = Action ? Action->usesCompleteTranslationUnit()
1583 : true;
1584 AST->ShouldCacheCodeCompletionResults = false;
1585 AST->Invocation = CI;
1586
1587 // Recover resources if we crash before exiting this method.
1588 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1589 ASTUnitCleanup(AST.get());
1590 llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
1591 llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
1592 DiagCleanup(Diags.getPtr());
1593
1594 // We'll manage file buffers ourselves.
1595 CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1596 CI->getFrontendOpts().DisableFree = false;
1597 ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
1598
1599 // Save the target features.
1600 AST->TargetFeatures = CI->getTargetOpts().Features;
1601
1602 // Create the compiler instance to use for building the AST.
1603 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1604
1605 // Recover resources if we crash before exiting this method.
1606 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1607 CICleanup(Clang.get());
1608
1609 Clang->setInvocation(CI);
1610 AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
1611
1612 // Set up diagnostics, capturing any diagnostics that would
1613 // otherwise be dropped.
1614 Clang->setDiagnostics(&AST->getDiagnostics());
1615
1616 // Create the target instance.
1617 Clang->getTargetOpts().Features = AST->TargetFeatures;
1618 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1619 Clang->getTargetOpts()));
1620 if (!Clang->hasTarget())
1621 return 0;
1622
1623 // Inform the target of the language options.
1624 //
1625 // FIXME: We shouldn't need to do this, the target should be immutable once
1626 // created. This complexity should be lifted elsewhere.
1627 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1628
1629 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1630 "Invocation must have exactly one source file!");
1631 assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
1632 "FIXME: AST inputs not yet supported here!");
1633 assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
1634 "IR inputs not supported here!");
1635
1636 // Configure the various subsystems.
1637 AST->FileSystemOpts = Clang->getFileSystemOpts();
1638 AST->FileMgr = new FileManager(AST->FileSystemOpts);
1639 AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr);
1640 AST->TheSema.reset();
1641 AST->Ctx = 0;
1642 AST->PP = 0;
1643
1644 // Create a file manager object to provide access to and cache the filesystem.
1645 Clang->setFileManager(&AST->getFileManager());
1646
1647 // Create the source manager.
1648 Clang->setSourceManager(&AST->getSourceManager());
1649
1650 ASTFrontendAction *Act = Action;
1651
1652 llvm::OwningPtr<TopLevelDeclTrackerAction> TrackerAct;
1653 if (!Act) {
1654 TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
1655 Act = TrackerAct.get();
1656 }
1657
1658 // Recover resources if we crash before exiting this method.
1659 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1660 ActCleanup(TrackerAct.get());
1661
1662 if (!Act->BeginSourceFile(*Clang.get(),
1663 Clang->getFrontendOpts().Inputs[0].second,
1664 Clang->getFrontendOpts().Inputs[0].first))
1665 return 0;
1666
1667 Act->Execute();
1668
1669 // Steal the created target, context, and preprocessor.
1670 AST->TheSema.reset(Clang->takeSema());
1671 AST->Consumer.reset(Clang->takeASTConsumer());
1672 AST->Ctx = &Clang->getASTContext();
1673 AST->PP = &Clang->getPreprocessor();
1674 Clang->setSourceManager(0);
1675 Clang->setFileManager(0);
1676 AST->Target = &Clang->getTarget();
1677
1678 Act->EndSourceFile();
1679
1680 return AST.take();
1681}
1682
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001683bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) {
1684 if (!Invocation)
1685 return true;
1686
1687 // We'll manage file buffers ourselves.
1688 Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1689 Invocation->getFrontendOpts().DisableFree = false;
Douglas Gregor345c1bc2011-01-19 01:02:47 +00001690 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001691
Douglas Gregorffd6dc42011-01-27 18:02:58 +00001692 // Save the target features.
1693 TargetFeatures = Invocation->getTargetOpts().Features;
1694
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001695 llvm::MemoryBuffer *OverrideMainBuffer = 0;
Douglas Gregorf5a18542010-10-27 17:24:53 +00001696 if (PrecompilePreamble) {
Douglas Gregorc6592922010-11-15 23:00:34 +00001697 PreambleRebuildCounter = 2;
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001698 OverrideMainBuffer
1699 = getMainBufferWithPrecompiledPreamble(*Invocation);
1700 }
1701
Douglas Gregor16896c42010-10-28 15:44:59 +00001702 SimpleTimer ParsingTimer(WantTiming);
Benjamin Kramerf2e5a912010-11-09 20:00:56 +00001703 ParsingTimer.setOutput("Parsing " + getMainFileName());
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001704
Ted Kremenek022a4902011-03-22 01:15:24 +00001705 // Recover resources if we crash before exiting this method.
1706 llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1707 MemBufferCleanup(OverrideMainBuffer);
1708
Douglas Gregor16896c42010-10-28 15:44:59 +00001709 return Parse(OverrideMainBuffer);
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001710}
1711
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001712ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
1713 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
1714 bool OnlyLocalDecls,
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001715 bool CaptureDiagnostics,
Douglas Gregor028d3e42010-08-09 20:45:32 +00001716 bool PrecompilePreamble,
Douglas Gregorb14904c2010-08-13 22:48:40 +00001717 bool CompleteTranslationUnit,
Douglas Gregor998caea2011-05-06 16:33:08 +00001718 bool CacheCodeCompletionResults,
1719 bool NestedMacroInstantiations) {
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001720 // Create the AST unit.
1721 llvm::OwningPtr<ASTUnit> AST;
1722 AST.reset(new ASTUnit(false));
Douglas Gregor345c1bc2011-01-19 01:02:47 +00001723 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001724 AST->Diagnostics = Diags;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001725 AST->OnlyLocalDecls = OnlyLocalDecls;
Douglas Gregor44c6ee72010-11-11 00:39:14 +00001726 AST->CaptureDiagnostics = CaptureDiagnostics;
Douglas Gregor028d3e42010-08-09 20:45:32 +00001727 AST->CompleteTranslationUnit = CompleteTranslationUnit;
Douglas Gregorb14904c2010-08-13 22:48:40 +00001728 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
Ted Kremenek5e14d392011-03-21 18:40:17 +00001729 AST->Invocation = CI;
Douglas Gregor998caea2011-05-06 16:33:08 +00001730 AST->NestedMacroInstantiations = NestedMacroInstantiations;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001731
Ted Kremenek4422bfe2011-03-18 02:06:56 +00001732 // Recover resources if we crash before exiting this method.
Ted Kremenek022a4902011-03-22 01:15:24 +00001733 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1734 ASTUnitCleanup(AST.get());
1735 llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
1736 llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
1737 DiagCleanup(Diags.getPtr());
Ted Kremenek4422bfe2011-03-18 02:06:56 +00001738
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001739 return AST->LoadFromCompilerInvocation(PrecompilePreamble)? 0 : AST.take();
Daniel Dunbar764c0822009-12-01 09:51:01 +00001740}
Daniel Dunbar55a17b62009-12-02 03:23:45 +00001741
1742ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
1743 const char **ArgEnd,
Douglas Gregor7f95d262010-04-05 23:52:57 +00001744 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
Daniel Dunbar8d4a2022009-12-13 03:46:13 +00001745 llvm::StringRef ResourceFilesPath,
Daniel Dunbar55a17b62009-12-02 03:23:45 +00001746 bool OnlyLocalDecls,
Douglas Gregor44c6ee72010-11-11 00:39:14 +00001747 bool CaptureDiagnostics,
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001748 RemappedFile *RemappedFiles,
Douglas Gregor33cdd812010-02-18 18:08:43 +00001749 unsigned NumRemappedFiles,
Argyrios Kyrtzidis97d3a382011-03-08 23:35:24 +00001750 bool RemappedFilesKeepOriginalName,
Douglas Gregor028d3e42010-08-09 20:45:32 +00001751 bool PrecompilePreamble,
Douglas Gregorb14904c2010-08-13 22:48:40 +00001752 bool CompleteTranslationUnit,
Douglas Gregorf5a18542010-10-27 17:24:53 +00001753 bool CacheCodeCompletionResults,
1754 bool CXXPrecompilePreamble,
Douglas Gregor998caea2011-05-06 16:33:08 +00001755 bool CXXChainedPCH,
1756 bool NestedMacroInstantiations) {
Douglas Gregor7f95d262010-04-05 23:52:57 +00001757 if (!Diags.getPtr()) {
Douglas Gregord03e8232010-04-05 21:10:19 +00001758 // No diagnostics engine was provided, so create our own diagnostics object
1759 // with the default options.
1760 DiagnosticOptions DiagOpts;
Douglas Gregor345c1bc2011-01-19 01:02:47 +00001761 Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd - ArgBegin,
1762 ArgBegin);
Douglas Gregord03e8232010-04-05 21:10:19 +00001763 }
Daniel Dunbar55a17b62009-12-02 03:23:45 +00001764
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001765 llvm::SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1766
Ted Kremenek5e14d392011-03-21 18:40:17 +00001767 llvm::IntrusiveRefCntPtr<CompilerInvocation> CI;
Douglas Gregor44c6ee72010-11-11 00:39:14 +00001768
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001769 {
Douglas Gregor44c6ee72010-11-11 00:39:14 +00001770 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001771 StoredDiagnostics);
Daniel Dunbarfcf2d422010-01-25 00:44:02 +00001772
Argyrios Kyrtzidis5cf423e2011-04-04 23:11:45 +00001773 CI = clang::createInvocationFromCommandLine(
Argyrios Kyrtzidisf606b822011-04-04 21:38:51 +00001774 llvm::ArrayRef<const char *>(ArgBegin, ArgEnd-ArgBegin),
1775 Diags);
1776 if (!CI)
Argyrios Kyrtzidisbc1f48f2011-03-07 22:45:01 +00001777 return 0;
Daniel Dunbar55a17b62009-12-02 03:23:45 +00001778 }
Douglas Gregor44c6ee72010-11-11 00:39:14 +00001779
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001780 // Override any files that need remapping
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001781 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
1782 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
1783 if (const llvm::MemoryBuffer *
1784 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
1785 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, memBuf);
1786 } else {
1787 const char *fname = fileOrBuf.get<const char *>();
1788 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, fname);
1789 }
1790 }
Argyrios Kyrtzidis97d3a382011-03-08 23:35:24 +00001791 CI->getPreprocessorOpts().RemappedFilesKeepOriginalName =
1792 RemappedFilesKeepOriginalName;
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001793
Daniel Dunbara5a166d2009-12-15 00:06:45 +00001794 // Override the resources path.
Daniel Dunbar6b03ece2010-01-30 21:47:16 +00001795 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
Daniel Dunbar55a17b62009-12-02 03:23:45 +00001796
Douglas Gregorf5a18542010-10-27 17:24:53 +00001797 // Check whether we should precompile the preamble and/or use chained PCH.
1798 // FIXME: This is a temporary hack while we debug C++ chained PCH.
1799 if (CI->getLangOpts().CPlusPlus) {
1800 PrecompilePreamble = PrecompilePreamble && CXXPrecompilePreamble;
1801
1802 if (PrecompilePreamble && !CXXChainedPCH &&
1803 !CI->getPreprocessorOpts().ImplicitPCHInclude.empty())
1804 PrecompilePreamble = false;
1805 }
1806
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001807 // Create the AST unit.
1808 llvm::OwningPtr<ASTUnit> AST;
1809 AST.reset(new ASTUnit(false));
Douglas Gregor345c1bc2011-01-19 01:02:47 +00001810 ConfigureDiags(Diags, ArgBegin, ArgEnd, *AST, CaptureDiagnostics);
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001811 AST->Diagnostics = Diags;
Anders Carlssonc30dcec2011-03-18 18:22:40 +00001812
1813 AST->FileSystemOpts = CI->getFileSystemOpts();
Ted Kremenek5e14d392011-03-21 18:40:17 +00001814 AST->FileMgr = new FileManager(AST->FileSystemOpts);
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001815 AST->OnlyLocalDecls = OnlyLocalDecls;
Douglas Gregor44c6ee72010-11-11 00:39:14 +00001816 AST->CaptureDiagnostics = CaptureDiagnostics;
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001817 AST->CompleteTranslationUnit = CompleteTranslationUnit;
1818 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1819 AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1820 AST->NumStoredDiagnosticsInPreamble = StoredDiagnostics.size();
1821 AST->StoredDiagnostics.swap(StoredDiagnostics);
Ted Kremenek5e14d392011-03-21 18:40:17 +00001822 AST->Invocation = CI;
Douglas Gregor998caea2011-05-06 16:33:08 +00001823 AST->NestedMacroInstantiations = NestedMacroInstantiations;
Ted Kremenek4422bfe2011-03-18 02:06:56 +00001824
1825 // Recover resources if we crash before exiting this method.
Ted Kremenek022a4902011-03-22 01:15:24 +00001826 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1827 ASTUnitCleanup(AST.get());
1828 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation,
1829 llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> >
1830 CICleanup(CI.getPtr());
1831 llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
1832 llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
1833 DiagCleanup(Diags.getPtr());
Ted Kremenek4422bfe2011-03-18 02:06:56 +00001834
Chris Lattner5159f612010-11-23 08:35:12 +00001835 return AST->LoadFromCompilerInvocation(PrecompilePreamble) ? 0 : AST.take();
Daniel Dunbar55a17b62009-12-02 03:23:45 +00001836}
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001837
1838bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) {
Ted Kremenek5e14d392011-03-21 18:40:17 +00001839 if (!Invocation)
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001840 return true;
1841
Douglas Gregor16896c42010-10-28 15:44:59 +00001842 SimpleTimer ParsingTimer(WantTiming);
Benjamin Kramerf2e5a912010-11-09 20:00:56 +00001843 ParsingTimer.setOutput("Reparsing " + getMainFileName());
Douglas Gregor16896c42010-10-28 15:44:59 +00001844
Douglas Gregor0e119552010-07-31 00:40:00 +00001845 // Remap files.
Douglas Gregor7b02b582010-08-20 00:02:33 +00001846 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
Douglas Gregor606c4ac2011-02-05 19:42:43 +00001847 PPOpts.DisableStatCache = true;
Douglas Gregor7b02b582010-08-20 00:02:33 +00001848 for (PreprocessorOptions::remapped_file_buffer_iterator
1849 R = PPOpts.remapped_file_buffer_begin(),
1850 REnd = PPOpts.remapped_file_buffer_end();
1851 R != REnd;
1852 ++R) {
1853 delete R->second;
1854 }
Douglas Gregor0e119552010-07-31 00:40:00 +00001855 Invocation->getPreprocessorOpts().clearRemappedFiles();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001856 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
1857 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
1858 if (const llvm::MemoryBuffer *
1859 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
1860 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1861 memBuf);
1862 } else {
1863 const char *fname = fileOrBuf.get<const char *>();
1864 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1865 fname);
1866 }
1867 }
Douglas Gregor0e119552010-07-31 00:40:00 +00001868
Douglas Gregorbb420ab2010-08-04 05:53:38 +00001869 // If we have a preamble file lying around, or if we might try to
1870 // build a precompiled preamble, do so now.
Douglas Gregor6481ef12010-07-24 00:38:13 +00001871 llvm::MemoryBuffer *OverrideMainBuffer = 0;
Douglas Gregorbb420ab2010-08-04 05:53:38 +00001872 if (!PreambleFile.empty() || PreambleRebuildCounter > 0)
Douglas Gregorb97b6662010-08-20 00:59:43 +00001873 OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation);
Douglas Gregor4dde7492010-07-23 23:58:40 +00001874
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001875 // Clear out the diagnostics state.
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001876 if (!OverrideMainBuffer) {
Douglas Gregord9a30af2010-08-02 20:51:39 +00001877 getDiagnostics().Reset();
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001878 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1879 }
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001880
Douglas Gregor4dde7492010-07-23 23:58:40 +00001881 // Parse the sources
Douglas Gregordf7a79a2011-02-16 18:16:54 +00001882 bool Result = Parse(OverrideMainBuffer);
1883
1884 // If we're caching global code-completion results, and the top-level
1885 // declarations have changed, clear out the code-completion cache.
1886 if (!Result && ShouldCacheCodeCompletionResults &&
1887 CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
1888 CacheCodeCompletionResults();
1889
Douglas Gregor4dde7492010-07-23 23:58:40 +00001890 return Result;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001891}
Douglas Gregor8e984da2010-08-04 16:47:14 +00001892
Douglas Gregorb14904c2010-08-13 22:48:40 +00001893//----------------------------------------------------------------------------//
1894// Code completion
1895//----------------------------------------------------------------------------//
1896
1897namespace {
1898 /// \brief Code completion consumer that combines the cached code-completion
1899 /// results from an ASTUnit with the code-completion results provided to it,
1900 /// then passes the result on to
1901 class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
1902 unsigned NormalContexts;
1903 ASTUnit &AST;
1904 CodeCompleteConsumer &Next;
1905
1906 public:
1907 AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
Douglas Gregor39982192010-08-15 06:18:01 +00001908 bool IncludeMacros, bool IncludeCodePatterns,
1909 bool IncludeGlobals)
1910 : CodeCompleteConsumer(IncludeMacros, IncludeCodePatterns, IncludeGlobals,
Douglas Gregorb14904c2010-08-13 22:48:40 +00001911 Next.isOutputBinary()), AST(AST), Next(Next)
1912 {
1913 // Compute the set of contexts in which we will look when we don't have
1914 // any information about the specific context.
1915 NormalContexts
1916 = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
1917 | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1))
1918 | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1))
1919 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
1920 | (1 << (CodeCompletionContext::CCC_Statement - 1))
1921 | (1 << (CodeCompletionContext::CCC_Expression - 1))
1922 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
1923 | (1 << (CodeCompletionContext::CCC_MemberAccess - 1))
Douglas Gregor5e35d592010-09-14 23:59:36 +00001924 | (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1))
Douglas Gregor0ac41382010-09-23 23:01:17 +00001925 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
1926 | (1 << (CodeCompletionContext::CCC_Recovery - 1));
Douglas Gregor5e35d592010-09-14 23:59:36 +00001927
Douglas Gregorb14904c2010-08-13 22:48:40 +00001928 if (AST.getASTContext().getLangOptions().CPlusPlus)
1929 NormalContexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1))
1930 | (1 << (CodeCompletionContext::CCC_UnionTag - 1))
1931 | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1));
1932 }
1933
1934 virtual void ProcessCodeCompleteResults(Sema &S,
1935 CodeCompletionContext Context,
John McCall276321a2010-08-25 06:19:51 +00001936 CodeCompletionResult *Results,
Douglas Gregord46cf182010-08-16 20:01:48 +00001937 unsigned NumResults);
Douglas Gregorb14904c2010-08-13 22:48:40 +00001938
1939 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
1940 OverloadCandidate *Candidates,
1941 unsigned NumCandidates) {
1942 Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
1943 }
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001944
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00001945 virtual CodeCompletionAllocator &getAllocator() {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001946 return Next.getAllocator();
1947 }
Douglas Gregorb14904c2010-08-13 22:48:40 +00001948 };
1949}
Douglas Gregord46cf182010-08-16 20:01:48 +00001950
Douglas Gregor6199f2d2010-08-16 21:18:39 +00001951/// \brief Helper function that computes which global names are hidden by the
1952/// local code-completion results.
Ted Kremenek6a153372010-11-07 06:11:36 +00001953static void CalculateHiddenNames(const CodeCompletionContext &Context,
1954 CodeCompletionResult *Results,
1955 unsigned NumResults,
1956 ASTContext &Ctx,
1957 llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
Douglas Gregor6199f2d2010-08-16 21:18:39 +00001958 bool OnlyTagNames = false;
1959 switch (Context.getKind()) {
Douglas Gregor0ac41382010-09-23 23:01:17 +00001960 case CodeCompletionContext::CCC_Recovery:
Douglas Gregor6199f2d2010-08-16 21:18:39 +00001961 case CodeCompletionContext::CCC_TopLevel:
1962 case CodeCompletionContext::CCC_ObjCInterface:
1963 case CodeCompletionContext::CCC_ObjCImplementation:
1964 case CodeCompletionContext::CCC_ObjCIvarList:
1965 case CodeCompletionContext::CCC_ClassStructUnion:
1966 case CodeCompletionContext::CCC_Statement:
1967 case CodeCompletionContext::CCC_Expression:
1968 case CodeCompletionContext::CCC_ObjCMessageReceiver:
1969 case CodeCompletionContext::CCC_MemberAccess:
1970 case CodeCompletionContext::CCC_Namespace:
1971 case CodeCompletionContext::CCC_Type:
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001972 case CodeCompletionContext::CCC_Name:
1973 case CodeCompletionContext::CCC_PotentiallyQualifiedName:
Douglas Gregor5e35d592010-09-14 23:59:36 +00001974 case CodeCompletionContext::CCC_ParenthesizedExpression:
Douglas Gregor6199f2d2010-08-16 21:18:39 +00001975 break;
1976
1977 case CodeCompletionContext::CCC_EnumTag:
1978 case CodeCompletionContext::CCC_UnionTag:
1979 case CodeCompletionContext::CCC_ClassOrStructTag:
1980 OnlyTagNames = true;
1981 break;
1982
1983 case CodeCompletionContext::CCC_ObjCProtocolName:
Douglas Gregor12785102010-08-24 20:21:13 +00001984 case CodeCompletionContext::CCC_MacroName:
1985 case CodeCompletionContext::CCC_MacroNameUse:
Douglas Gregorec00a262010-08-24 22:20:20 +00001986 case CodeCompletionContext::CCC_PreprocessorExpression:
Douglas Gregor0de55ce2010-08-25 18:41:16 +00001987 case CodeCompletionContext::CCC_PreprocessorDirective:
Douglas Gregorea147052010-08-25 18:04:30 +00001988 case CodeCompletionContext::CCC_NaturalLanguage:
Douglas Gregor67c692c2010-08-26 15:07:07 +00001989 case CodeCompletionContext::CCC_SelectorName:
Douglas Gregor28c78432010-08-27 17:35:51 +00001990 case CodeCompletionContext::CCC_TypeQualifiers:
Douglas Gregor0ac41382010-09-23 23:01:17 +00001991 case CodeCompletionContext::CCC_Other:
Douglas Gregor3a69eaf2011-02-18 23:30:37 +00001992 case CodeCompletionContext::CCC_OtherWithMacros:
Douglas Gregor0de55ce2010-08-25 18:41:16 +00001993 // We're looking for nothing, or we're looking for names that cannot
1994 // be hidden.
Douglas Gregor6199f2d2010-08-16 21:18:39 +00001995 return;
1996 }
1997
John McCall276321a2010-08-25 06:19:51 +00001998 typedef CodeCompletionResult Result;
Douglas Gregor6199f2d2010-08-16 21:18:39 +00001999 for (unsigned I = 0; I != NumResults; ++I) {
2000 if (Results[I].Kind != Result::RK_Declaration)
2001 continue;
2002
2003 unsigned IDNS
2004 = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
2005
2006 bool Hiding = false;
2007 if (OnlyTagNames)
2008 Hiding = (IDNS & Decl::IDNS_Tag);
2009 else {
2010 unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
Douglas Gregor59cab552010-08-16 23:05:20 +00002011 Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
2012 Decl::IDNS_NonMemberOperator);
Douglas Gregor6199f2d2010-08-16 21:18:39 +00002013 if (Ctx.getLangOptions().CPlusPlus)
2014 HiddenIDNS |= Decl::IDNS_Tag;
2015 Hiding = (IDNS & HiddenIDNS);
2016 }
2017
2018 if (!Hiding)
2019 continue;
2020
2021 DeclarationName Name = Results[I].Declaration->getDeclName();
2022 if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
2023 HiddenNames.insert(Identifier->getName());
2024 else
2025 HiddenNames.insert(Name.getAsString());
2026 }
2027}
2028
2029
Douglas Gregord46cf182010-08-16 20:01:48 +00002030void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
2031 CodeCompletionContext Context,
John McCall276321a2010-08-25 06:19:51 +00002032 CodeCompletionResult *Results,
Douglas Gregord46cf182010-08-16 20:01:48 +00002033 unsigned NumResults) {
2034 // Merge the results we were given with the results we cached.
2035 bool AddedResult = false;
Douglas Gregor6199f2d2010-08-16 21:18:39 +00002036 unsigned InContexts
Douglas Gregor0ac41382010-09-23 23:01:17 +00002037 = (Context.getKind() == CodeCompletionContext::CCC_Recovery? NormalContexts
Douglas Gregor6199f2d2010-08-16 21:18:39 +00002038 : (1 << (Context.getKind() - 1)));
2039
2040 // Contains the set of names that are hidden by "local" completion results.
Ted Kremenek6a153372010-11-07 06:11:36 +00002041 llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
John McCall276321a2010-08-25 06:19:51 +00002042 typedef CodeCompletionResult Result;
Douglas Gregord46cf182010-08-16 20:01:48 +00002043 llvm::SmallVector<Result, 8> AllResults;
2044 for (ASTUnit::cached_completion_iterator
Douglas Gregordf239672010-08-16 21:23:13 +00002045 C = AST.cached_completion_begin(),
2046 CEnd = AST.cached_completion_end();
Douglas Gregord46cf182010-08-16 20:01:48 +00002047 C != CEnd; ++C) {
2048 // If the context we are in matches any of the contexts we are
2049 // interested in, we'll add this result.
2050 if ((C->ShowInContexts & InContexts) == 0)
2051 continue;
2052
2053 // If we haven't added any results previously, do so now.
2054 if (!AddedResult) {
Douglas Gregor6199f2d2010-08-16 21:18:39 +00002055 CalculateHiddenNames(Context, Results, NumResults, S.Context,
2056 HiddenNames);
Douglas Gregord46cf182010-08-16 20:01:48 +00002057 AllResults.insert(AllResults.end(), Results, Results + NumResults);
2058 AddedResult = true;
2059 }
2060
Douglas Gregor6199f2d2010-08-16 21:18:39 +00002061 // Determine whether this global completion result is hidden by a local
2062 // completion result. If so, skip it.
2063 if (C->Kind != CXCursor_MacroDefinition &&
2064 HiddenNames.count(C->Completion->getTypedText()))
2065 continue;
2066
Douglas Gregord46cf182010-08-16 20:01:48 +00002067 // Adjust priority based on similar type classes.
2068 unsigned Priority = C->Priority;
Douglas Gregor8850aa32010-08-25 18:03:13 +00002069 CXCursorKind CursorKind = C->Kind;
Douglas Gregor12785102010-08-24 20:21:13 +00002070 CodeCompletionString *Completion = C->Completion;
Douglas Gregord46cf182010-08-16 20:01:48 +00002071 if (!Context.getPreferredType().isNull()) {
2072 if (C->Kind == CXCursor_MacroDefinition) {
2073 Priority = getMacroUsagePriority(C->Completion->getTypedText(),
Douglas Gregor9dcf58a2010-09-20 21:11:48 +00002074 S.getLangOptions(),
Douglas Gregor12785102010-08-24 20:21:13 +00002075 Context.getPreferredType()->isAnyPointerType());
Douglas Gregord46cf182010-08-16 20:01:48 +00002076 } else if (C->Type) {
2077 CanQualType Expected
Douglas Gregordf239672010-08-16 21:23:13 +00002078 = S.Context.getCanonicalType(
Douglas Gregord46cf182010-08-16 20:01:48 +00002079 Context.getPreferredType().getUnqualifiedType());
2080 SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
2081 if (ExpectedSTC == C->TypeClass) {
2082 // We know this type is similar; check for an exact match.
2083 llvm::StringMap<unsigned> &CachedCompletionTypes
Douglas Gregordf239672010-08-16 21:23:13 +00002084 = AST.getCachedCompletionTypes();
Douglas Gregord46cf182010-08-16 20:01:48 +00002085 llvm::StringMap<unsigned>::iterator Pos
Douglas Gregordf239672010-08-16 21:23:13 +00002086 = CachedCompletionTypes.find(QualType(Expected).getAsString());
Douglas Gregord46cf182010-08-16 20:01:48 +00002087 if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
2088 Priority /= CCF_ExactTypeMatch;
2089 else
2090 Priority /= CCF_SimilarTypeMatch;
2091 }
2092 }
2093 }
2094
Douglas Gregor12785102010-08-24 20:21:13 +00002095 // Adjust the completion string, if required.
2096 if (C->Kind == CXCursor_MacroDefinition &&
2097 Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
2098 // Create a new code-completion string that just contains the
2099 // macro name, without its arguments.
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002100 CodeCompletionBuilder Builder(getAllocator(), CCP_CodePattern,
2101 C->Availability);
2102 Builder.AddTypedTextChunk(C->Completion->getTypedText());
Douglas Gregor8850aa32010-08-25 18:03:13 +00002103 CursorKind = CXCursor_NotImplemented;
2104 Priority = CCP_CodePattern;
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002105 Completion = Builder.TakeString();
Douglas Gregor12785102010-08-24 20:21:13 +00002106 }
2107
Douglas Gregor8850aa32010-08-25 18:03:13 +00002108 AllResults.push_back(Result(Completion, Priority, CursorKind,
Douglas Gregorf757a122010-08-23 23:00:57 +00002109 C->Availability));
Douglas Gregord46cf182010-08-16 20:01:48 +00002110 }
2111
2112 // If we did not add any cached completion results, just forward the
2113 // results we were given to the next consumer.
2114 if (!AddedResult) {
2115 Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
2116 return;
2117 }
Douglas Gregor49f67ce2010-08-26 13:48:20 +00002118
Douglas Gregord46cf182010-08-16 20:01:48 +00002119 Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
2120 AllResults.size());
2121}
2122
2123
2124
Douglas Gregor8e984da2010-08-04 16:47:14 +00002125void ASTUnit::CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column,
2126 RemappedFile *RemappedFiles,
2127 unsigned NumRemappedFiles,
Douglas Gregorb68bc592010-08-05 09:09:23 +00002128 bool IncludeMacros,
2129 bool IncludeCodePatterns,
Douglas Gregor8e984da2010-08-04 16:47:14 +00002130 CodeCompleteConsumer &Consumer,
2131 Diagnostic &Diag, LangOptions &LangOpts,
2132 SourceManager &SourceMgr, FileManager &FileMgr,
Douglas Gregorb97b6662010-08-20 00:59:43 +00002133 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2134 llvm::SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
Ted Kremenek5e14d392011-03-21 18:40:17 +00002135 if (!Invocation)
Douglas Gregor8e984da2010-08-04 16:47:14 +00002136 return;
2137
Douglas Gregor16896c42010-10-28 15:44:59 +00002138 SimpleTimer CompletionTimer(WantTiming);
Benjamin Kramerf2e5a912010-11-09 20:00:56 +00002139 CompletionTimer.setOutput("Code completion @ " + File + ":" +
2140 llvm::Twine(Line) + ":" + llvm::Twine(Column));
Douglas Gregor028d3e42010-08-09 20:45:32 +00002141
Ted Kremenek5e14d392011-03-21 18:40:17 +00002142 llvm::IntrusiveRefCntPtr<CompilerInvocation>
2143 CCInvocation(new CompilerInvocation(*Invocation));
2144
2145 FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2146 PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
Douglas Gregorb68bc592010-08-05 09:09:23 +00002147
Douglas Gregorb14904c2010-08-13 22:48:40 +00002148 FrontendOpts.ShowMacrosInCodeCompletion
2149 = IncludeMacros && CachedCompletionResults.empty();
Douglas Gregorb68bc592010-08-05 09:09:23 +00002150 FrontendOpts.ShowCodePatternsInCodeCompletion = IncludeCodePatterns;
Douglas Gregor39982192010-08-15 06:18:01 +00002151 FrontendOpts.ShowGlobalSymbolsInCodeCompletion
2152 = CachedCompletionResults.empty();
Douglas Gregor8e984da2010-08-04 16:47:14 +00002153 FrontendOpts.CodeCompletionAt.FileName = File;
2154 FrontendOpts.CodeCompletionAt.Line = Line;
2155 FrontendOpts.CodeCompletionAt.Column = Column;
2156
2157 // Set the language options appropriately.
Ted Kremenek5e14d392011-03-21 18:40:17 +00002158 LangOpts = CCInvocation->getLangOpts();
Douglas Gregor8e984da2010-08-04 16:47:14 +00002159
Ted Kremenek84de4a12011-03-21 18:40:07 +00002160 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
2161
2162 // Recover resources if we crash before exiting this method.
Ted Kremenek022a4902011-03-22 01:15:24 +00002163 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2164 CICleanup(Clang.get());
Ted Kremenek84de4a12011-03-21 18:40:07 +00002165
Ted Kremenek5e14d392011-03-21 18:40:17 +00002166 Clang->setInvocation(&*CCInvocation);
Ted Kremenek84de4a12011-03-21 18:40:07 +00002167 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
Douglas Gregor8e984da2010-08-04 16:47:14 +00002168
2169 // Set up diagnostics, capturing any diagnostics produced.
Ted Kremenek84de4a12011-03-21 18:40:07 +00002170 Clang->setDiagnostics(&Diag);
Ted Kremenek5e14d392011-03-21 18:40:17 +00002171 ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts());
Douglas Gregor8e984da2010-08-04 16:47:14 +00002172 CaptureDroppedDiagnostics Capture(true,
Ted Kremenek84de4a12011-03-21 18:40:07 +00002173 Clang->getDiagnostics(),
Douglas Gregor8e984da2010-08-04 16:47:14 +00002174 StoredDiagnostics);
Douglas Gregor8e984da2010-08-04 16:47:14 +00002175
2176 // Create the target instance.
Ted Kremenek84de4a12011-03-21 18:40:07 +00002177 Clang->getTargetOpts().Features = TargetFeatures;
2178 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
2179 Clang->getTargetOpts()));
2180 if (!Clang->hasTarget()) {
Ted Kremenek5e14d392011-03-21 18:40:17 +00002181 Clang->setInvocation(0);
Douglas Gregor2dd19f12010-08-18 22:29:43 +00002182 return;
Douglas Gregor8e984da2010-08-04 16:47:14 +00002183 }
2184
2185 // Inform the target of the language options.
2186 //
2187 // FIXME: We shouldn't need to do this, the target should be immutable once
2188 // created. This complexity should be lifted elsewhere.
Ted Kremenek84de4a12011-03-21 18:40:07 +00002189 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
Douglas Gregor8e984da2010-08-04 16:47:14 +00002190
Ted Kremenek84de4a12011-03-21 18:40:07 +00002191 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
Douglas Gregor8e984da2010-08-04 16:47:14 +00002192 "Invocation must have exactly one source file!");
Ted Kremenek84de4a12011-03-21 18:40:07 +00002193 assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
Douglas Gregor8e984da2010-08-04 16:47:14 +00002194 "FIXME: AST inputs not yet supported here!");
Ted Kremenek84de4a12011-03-21 18:40:07 +00002195 assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
Douglas Gregor8e984da2010-08-04 16:47:14 +00002196 "IR inputs not support here!");
2197
2198
2199 // Use the source and file managers that we were given.
Ted Kremenek84de4a12011-03-21 18:40:07 +00002200 Clang->setFileManager(&FileMgr);
2201 Clang->setSourceManager(&SourceMgr);
Douglas Gregor8e984da2010-08-04 16:47:14 +00002202
2203 // Remap files.
2204 PreprocessorOpts.clearRemappedFiles();
Douglas Gregord8a5dba2010-08-04 17:07:00 +00002205 PreprocessorOpts.RetainRemappedFileBuffers = true;
Douglas Gregorb97b6662010-08-20 00:59:43 +00002206 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002207 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
2208 if (const llvm::MemoryBuffer *
2209 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
2210 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, memBuf);
2211 OwnedBuffers.push_back(memBuf);
2212 } else {
2213 const char *fname = fileOrBuf.get<const char *>();
2214 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, fname);
2215 }
Douglas Gregorb97b6662010-08-20 00:59:43 +00002216 }
Douglas Gregor8e984da2010-08-04 16:47:14 +00002217
Douglas Gregorb14904c2010-08-13 22:48:40 +00002218 // Use the code completion consumer we were given, but adding any cached
2219 // code-completion results.
Douglas Gregore9186e62010-11-29 16:13:56 +00002220 AugmentedCodeCompleteConsumer *AugmentedConsumer
2221 = new AugmentedCodeCompleteConsumer(*this, Consumer,
2222 FrontendOpts.ShowMacrosInCodeCompletion,
2223 FrontendOpts.ShowCodePatternsInCodeCompletion,
2224 FrontendOpts.ShowGlobalSymbolsInCodeCompletion);
Ted Kremenek84de4a12011-03-21 18:40:07 +00002225 Clang->setCodeCompletionConsumer(AugmentedConsumer);
Douglas Gregor8e984da2010-08-04 16:47:14 +00002226
Douglas Gregor028d3e42010-08-09 20:45:32 +00002227 // If we have a precompiled preamble, try to use it. We only allow
2228 // the use of the precompiled preamble if we're if the completion
2229 // point is within the main file, after the end of the precompiled
2230 // preamble.
2231 llvm::MemoryBuffer *OverrideMainBuffer = 0;
2232 if (!PreambleFile.empty()) {
2233 using llvm::sys::FileStatus;
2234 llvm::sys::PathWithStatus CompleteFilePath(File);
2235 llvm::sys::PathWithStatus MainPath(OriginalSourceFile);
2236 if (const FileStatus *CompleteFileStatus = CompleteFilePath.getFileStatus())
2237 if (const FileStatus *MainStatus = MainPath.getFileStatus())
2238 if (CompleteFileStatus->getUniqueID() == MainStatus->getUniqueID())
Douglas Gregorb97b6662010-08-20 00:59:43 +00002239 OverrideMainBuffer
Ted Kremenek5e14d392011-03-21 18:40:17 +00002240 = getMainBufferWithPrecompiledPreamble(*CCInvocation, false,
Douglas Gregor8e817b62010-08-25 18:04:15 +00002241 Line - 1);
Douglas Gregor028d3e42010-08-09 20:45:32 +00002242 }
2243
2244 // If the main file has been overridden due to the use of a preamble,
2245 // make that override happen and introduce the preamble.
Douglas Gregor606c4ac2011-02-05 19:42:43 +00002246 PreprocessorOpts.DisableStatCache = true;
Douglas Gregor7bb8af62010-10-12 00:50:20 +00002247 StoredDiagnostics.insert(StoredDiagnostics.end(),
2248 this->StoredDiagnostics.begin(),
2249 this->StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver);
Douglas Gregor028d3e42010-08-09 20:45:32 +00002250 if (OverrideMainBuffer) {
2251 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
2252 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
2253 PreprocessorOpts.PrecompiledPreambleBytes.second
2254 = PreambleEndsAtStartOfLine;
2255 PreprocessorOpts.ImplicitPCHInclude = PreambleFile;
2256 PreprocessorOpts.DisablePCHValidation = true;
2257
2258 // The stored diagnostics have the old source manager. Copy them
2259 // to our output set of stored diagnostics, updating the source
2260 // manager to the one we were given.
Douglas Gregor7bb8af62010-10-12 00:50:20 +00002261 for (unsigned I = NumStoredDiagnosticsFromDriver,
2262 N = this->StoredDiagnostics.size();
2263 I < N; ++I) {
Douglas Gregor028d3e42010-08-09 20:45:32 +00002264 StoredDiagnostics.push_back(this->StoredDiagnostics[I]);
2265 FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), SourceMgr);
2266 StoredDiagnostics[I].setLocation(Loc);
2267 }
Douglas Gregor7bb8af62010-10-12 00:50:20 +00002268
Douglas Gregorb97b6662010-08-20 00:59:43 +00002269 OwnedBuffers.push_back(OverrideMainBuffer);
Douglas Gregor7b02b582010-08-20 00:02:33 +00002270 } else {
2271 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2272 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
Douglas Gregor028d3e42010-08-09 20:45:32 +00002273 }
2274
Douglas Gregor998caea2011-05-06 16:33:08 +00002275 // Disable the preprocessing record
2276 PreprocessorOpts.DetailedRecord = false;
2277
Douglas Gregor8e984da2010-08-04 16:47:14 +00002278 llvm::OwningPtr<SyntaxOnlyAction> Act;
2279 Act.reset(new SyntaxOnlyAction);
Ted Kremenek84de4a12011-03-21 18:40:07 +00002280 if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
2281 Clang->getFrontendOpts().Inputs[0].first)) {
Douglas Gregor8e984da2010-08-04 16:47:14 +00002282 Act->Execute();
2283 Act->EndSourceFile();
2284 }
Douglas Gregor8e984da2010-08-04 16:47:14 +00002285}
Douglas Gregore9386682010-08-13 05:36:37 +00002286
2287bool ASTUnit::Save(llvm::StringRef File) {
2288 if (getDiagnostics().hasErrorOccurred())
2289 return true;
2290
2291 // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2292 // unconditionally create a stat cache when we parse the file?
2293 std::string ErrorInfo;
Benjamin Kramer340045b2010-08-15 16:54:31 +00002294 llvm::raw_fd_ostream Out(File.str().c_str(), ErrorInfo,
2295 llvm::raw_fd_ostream::F_Binary);
Douglas Gregore9386682010-08-13 05:36:37 +00002296 if (!ErrorInfo.empty() || Out.has_error())
2297 return true;
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +00002298
2299 serialize(Out);
2300 Out.close();
2301 return Out.has_error();
2302}
2303
2304bool ASTUnit::serialize(llvm::raw_ostream &OS) {
2305 if (getDiagnostics().hasErrorOccurred())
2306 return true;
2307
Douglas Gregore9386682010-08-13 05:36:37 +00002308 std::vector<unsigned char> Buffer;
2309 llvm::BitstreamWriter Stream(Buffer);
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002310 ASTWriter Writer(Stream);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002311 Writer.WriteAST(getSema(), 0, std::string(), 0);
Douglas Gregore9386682010-08-13 05:36:37 +00002312
2313 // Write the generated bitstream to "Out".
Douglas Gregor2dd19f12010-08-18 22:29:43 +00002314 if (!Buffer.empty())
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +00002315 OS.write((char *)&Buffer.front(), Buffer.size());
2316
2317 return false;
Douglas Gregore9386682010-08-13 05:36:37 +00002318}