blob: 46f5b5a48e749d693f033e51b22ac5d760d013d3 [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"
23#include "clang/Driver/Tool.h"
Daniel Dunbar764c0822009-12-01 09:51:01 +000024#include "clang/Frontend/CompilerInstance.h"
25#include "clang/Frontend/FrontendActions.h"
Daniel Dunbar55a17b62009-12-02 03:23:45 +000026#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar764c0822009-12-01 09:51:01 +000027#include "clang/Frontend/FrontendOptions.h"
Douglas Gregor36e3b5c2010-10-11 21:37:58 +000028#include "clang/Frontend/Utils.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000029#include "clang/Serialization/ASTReader.h"
Douglas Gregorf88e35b2010-11-30 06:16:57 +000030#include "clang/Serialization/ASTSerializationListener.h"
Sebastian Redl1914c6f2010-08-18 23:56:37 +000031#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000032#include "clang/Lex/HeaderSearch.h"
33#include "clang/Lex/Preprocessor.h"
Daniel Dunbarb9bbd542009-11-15 06:48:46 +000034#include "clang/Basic/TargetOptions.h"
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000035#include "clang/Basic/TargetInfo.h"
36#include "clang/Basic/Diagnostic.h"
Douglas Gregordf7a79a2011-02-16 18:16:54 +000037#include "llvm/ADT/StringExtras.h"
Douglas Gregor40a5a7d2010-08-16 23:08:34 +000038#include "llvm/ADT/StringSet.h"
Douglas Gregor9aeaa4d2010-12-07 00:05:48 +000039#include "llvm/Support/Atomic.h"
Douglas Gregoraa98ed92010-01-23 00:14:00 +000040#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000041#include "llvm/Support/Host.h"
42#include "llvm/Support/Path.h"
Douglas Gregor028d3e42010-08-09 20:45:32 +000043#include "llvm/Support/raw_ostream.h"
Douglas Gregor15ba0b32010-07-30 20:58:08 +000044#include "llvm/Support/Timer.h"
Douglas Gregorbe2d8c62010-07-23 00:33:23 +000045#include <cstdlib>
Zhongxing Xu318e4032010-07-23 02:15:08 +000046#include <cstdio>
Douglas Gregor0e119552010-07-31 00:40:00 +000047#include <sys/stat.h>
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000048using namespace clang;
49
Douglas Gregor16896c42010-10-28 15:44:59 +000050using llvm::TimeRecord;
51
52namespace {
53 class SimpleTimer {
54 bool WantTiming;
55 TimeRecord Start;
56 std::string Output;
57
Benjamin Kramerf2e5a912010-11-09 20:00:56 +000058 public:
Douglas Gregor1cbdd952010-11-01 13:48:43 +000059 explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
Douglas Gregor16896c42010-10-28 15:44:59 +000060 if (WantTiming)
Benjamin Kramerf2e5a912010-11-09 20:00:56 +000061 Start = TimeRecord::getCurrentTime();
Douglas Gregor16896c42010-10-28 15:44:59 +000062 }
63
Benjamin Kramerf2e5a912010-11-09 20:00:56 +000064 void setOutput(const llvm::Twine &Output) {
Douglas Gregor16896c42010-10-28 15:44:59 +000065 if (WantTiming)
Benjamin Kramerf2e5a912010-11-09 20:00:56 +000066 this->Output = Output.str();
Douglas Gregor16896c42010-10-28 15:44:59 +000067 }
68
Douglas Gregor16896c42010-10-28 15:44:59 +000069 ~SimpleTimer() {
70 if (WantTiming) {
71 TimeRecord Elapsed = TimeRecord::getCurrentTime();
72 Elapsed -= Start;
73 llvm::errs() << Output << ':';
74 Elapsed.print(Elapsed, llvm::errs());
75 llvm::errs() << '\n';
76 }
77 }
78 };
79}
80
Douglas Gregorbb420ab2010-08-04 05:53:38 +000081/// \brief After failing to build a precompiled preamble (due to
82/// errors in the source that occurs in the preamble), the number of
83/// reparses during which we'll skip even trying to precompile the
84/// preamble.
85const unsigned DefaultPreambleRebuildInterval = 5;
86
Douglas Gregor68dbaea2010-11-17 00:13:31 +000087/// \brief Tracks the number of ASTUnit objects that are currently active.
88///
89/// Used for debugging purposes only.
Douglas Gregor9aeaa4d2010-12-07 00:05:48 +000090static llvm::sys::cas_flag ActiveASTUnitObjects;
Douglas Gregor68dbaea2010-11-17 00:13:31 +000091
Douglas Gregord03e8232010-04-05 21:10:19 +000092ASTUnit::ASTUnit(bool _MainFileIsAST)
Douglas Gregoraa21cc42010-07-19 21:46:24 +000093 : CaptureDiagnostics(false), MainFileIsAST(_MainFileIsAST),
Douglas Gregor16896c42010-10-28 15:44:59 +000094 CompleteTranslationUnit(true), WantTiming(getenv("LIBCLANG_TIMING")),
95 NumStoredDiagnosticsFromDriver(0),
Douglas Gregor7bb8af62010-10-12 00:50:20 +000096 ConcurrencyCheckValue(CheckUnlocked),
Douglas Gregora0734c52010-08-19 01:33:06 +000097 PreambleRebuildCounter(0), SavedMainFileBuffer(0), PreambleBuffer(0),
Douglas Gregor2c8bd472010-08-17 00:40:40 +000098 ShouldCacheCodeCompletionResults(false),
Douglas Gregordf7a79a2011-02-16 18:16:54 +000099 CompletionCacheTopLevelHashValue(0),
100 PreambleTopLevelHashValue(0),
101 CurrentTopLevelHashValue(0),
Douglas Gregor4740c452010-08-19 00:45:44 +0000102 UnsafeToFree(false) {
Douglas Gregor68dbaea2010-11-17 00:13:31 +0000103 if (getenv("LIBCLANG_OBJTRACKING")) {
Douglas Gregor9aeaa4d2010-12-07 00:05:48 +0000104 llvm::sys::AtomicIncrement(&ActiveASTUnitObjects);
Douglas Gregor68dbaea2010-11-17 00:13:31 +0000105 fprintf(stderr, "+++ %d translation units\n", ActiveASTUnitObjects);
106 }
Douglas Gregor15ba0b32010-07-30 20:58:08 +0000107}
Douglas Gregord03e8232010-04-05 21:10:19 +0000108
Daniel Dunbar764c0822009-12-01 09:51:01 +0000109ASTUnit::~ASTUnit() {
Douglas Gregor0c7c2f82010-03-05 21:16:25 +0000110 ConcurrencyCheckValue = CheckLocked;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000111 CleanTemporaryFiles();
Douglas Gregor4dde7492010-07-23 23:58:40 +0000112 if (!PreambleFile.empty())
Douglas Gregor15ba0b32010-07-30 20:58:08 +0000113 llvm::sys::Path(PreambleFile).eraseFromDisk();
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000114
115 // Free the buffers associated with remapped files. We are required to
116 // perform this operation here because we explicitly request that the
117 // compiler instance *not* free these buffers for each invocation of the
118 // parser.
119 if (Invocation.get()) {
120 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
121 for (PreprocessorOptions::remapped_file_buffer_iterator
122 FB = PPOpts.remapped_file_buffer_begin(),
123 FBEnd = PPOpts.remapped_file_buffer_end();
124 FB != FBEnd;
125 ++FB)
126 delete FB->second;
127 }
Douglas Gregor96c04262010-07-27 14:52:07 +0000128
129 delete SavedMainFileBuffer;
Douglas Gregora0734c52010-08-19 01:33:06 +0000130 delete PreambleBuffer;
131
Douglas Gregor16896c42010-10-28 15:44:59 +0000132 ClearCachedCompletionResults();
Douglas Gregor68dbaea2010-11-17 00:13:31 +0000133
134 if (getenv("LIBCLANG_OBJTRACKING")) {
Douglas Gregor9aeaa4d2010-12-07 00:05:48 +0000135 llvm::sys::AtomicDecrement(&ActiveASTUnitObjects);
Douglas Gregor68dbaea2010-11-17 00:13:31 +0000136 fprintf(stderr, "--- %d translation units\n", ActiveASTUnitObjects);
137 }
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000138}
139
140void ASTUnit::CleanTemporaryFiles() {
Douglas Gregor6cb5ba42010-02-18 23:35:40 +0000141 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
142 TemporaryFiles[I].eraseFromDisk();
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000143 TemporaryFiles.clear();
Steve Naroff44cd60e2009-10-15 22:23:48 +0000144}
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000145
Douglas Gregor39982192010-08-15 06:18:01 +0000146/// \brief Determine the set of code-completion contexts in which this
147/// declaration should be shown.
148static unsigned getDeclShowContexts(NamedDecl *ND,
Douglas Gregor59cab552010-08-16 23:05:20 +0000149 const LangOptions &LangOpts,
150 bool &IsNestedNameSpecifier) {
151 IsNestedNameSpecifier = false;
152
Douglas Gregor39982192010-08-15 06:18:01 +0000153 if (isa<UsingShadowDecl>(ND))
154 ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl());
155 if (!ND)
156 return 0;
157
158 unsigned Contexts = 0;
159 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) ||
160 isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) {
161 // Types can appear in these contexts.
162 if (LangOpts.CPlusPlus || !isa<TagDecl>(ND))
163 Contexts |= (1 << (CodeCompletionContext::CCC_TopLevel - 1))
164 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
165 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
166 | (1 << (CodeCompletionContext::CCC_Statement - 1))
Douglas Gregor5e35d592010-09-14 23:59:36 +0000167 | (1 << (CodeCompletionContext::CCC_Type - 1))
168 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1));
Douglas Gregor39982192010-08-15 06:18:01 +0000169
170 // In C++, types can appear in expressions contexts (for functional casts).
171 if (LangOpts.CPlusPlus)
172 Contexts |= (1 << (CodeCompletionContext::CCC_Expression - 1));
173
174 // In Objective-C, message sends can send interfaces. In Objective-C++,
175 // all types are available due to functional casts.
176 if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND))
177 Contexts |= (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1));
178
179 // Deal with tag names.
180 if (isa<EnumDecl>(ND)) {
181 Contexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1));
182
Douglas Gregor59cab552010-08-16 23:05:20 +0000183 // Part of the nested-name-specifier in C++0x.
Douglas Gregor39982192010-08-15 06:18:01 +0000184 if (LangOpts.CPlusPlus0x)
Douglas Gregor59cab552010-08-16 23:05:20 +0000185 IsNestedNameSpecifier = true;
Douglas Gregor39982192010-08-15 06:18:01 +0000186 } else if (RecordDecl *Record = dyn_cast<RecordDecl>(ND)) {
187 if (Record->isUnion())
188 Contexts |= (1 << (CodeCompletionContext::CCC_UnionTag - 1));
189 else
190 Contexts |= (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1));
191
Douglas Gregor39982192010-08-15 06:18:01 +0000192 if (LangOpts.CPlusPlus)
Douglas Gregor59cab552010-08-16 23:05:20 +0000193 IsNestedNameSpecifier = true;
Douglas Gregor0ac41382010-09-23 23:01:17 +0000194 } else if (isa<ClassTemplateDecl>(ND))
Douglas Gregor59cab552010-08-16 23:05:20 +0000195 IsNestedNameSpecifier = true;
Douglas Gregor39982192010-08-15 06:18:01 +0000196 } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) {
197 // Values can appear in these contexts.
198 Contexts = (1 << (CodeCompletionContext::CCC_Statement - 1))
199 | (1 << (CodeCompletionContext::CCC_Expression - 1))
Douglas Gregor5e35d592010-09-14 23:59:36 +0000200 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
Douglas Gregor39982192010-08-15 06:18:01 +0000201 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1));
202 } else if (isa<ObjCProtocolDecl>(ND)) {
203 Contexts = (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1));
204 } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
Douglas Gregor59cab552010-08-16 23:05:20 +0000205 Contexts = (1 << (CodeCompletionContext::CCC_Namespace - 1));
Douglas Gregor39982192010-08-15 06:18:01 +0000206
207 // Part of the nested-name-specifier.
Douglas Gregor59cab552010-08-16 23:05:20 +0000208 IsNestedNameSpecifier = true;
Douglas Gregor39982192010-08-15 06:18:01 +0000209 }
210
211 return Contexts;
212}
213
Douglas Gregorb14904c2010-08-13 22:48:40 +0000214void ASTUnit::CacheCodeCompletionResults() {
215 if (!TheSema)
216 return;
217
Douglas Gregor16896c42010-10-28 15:44:59 +0000218 SimpleTimer Timer(WantTiming);
Benjamin Kramerf2e5a912010-11-09 20:00:56 +0000219 Timer.setOutput("Cache global code completions for " + getMainFileName());
Douglas Gregorb14904c2010-08-13 22:48:40 +0000220
221 // Clear out the previous results.
222 ClearCachedCompletionResults();
223
224 // Gather the set of global code completions.
John McCall276321a2010-08-25 06:19:51 +0000225 typedef CodeCompletionResult Result;
Douglas Gregorb14904c2010-08-13 22:48:40 +0000226 llvm::SmallVector<Result, 8> Results;
Douglas Gregor162b7122011-02-16 19:08:06 +0000227 CachedCompletionAllocator = new GlobalCodeCompletionAllocator;
228 TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator, Results);
Douglas Gregorb14904c2010-08-13 22:48:40 +0000229
230 // Translate global code completions into cached completions.
Douglas Gregorb61c07a2010-08-16 18:08:11 +0000231 llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
232
Douglas Gregorb14904c2010-08-13 22:48:40 +0000233 for (unsigned I = 0, N = Results.size(); I != N; ++I) {
234 switch (Results[I].Kind) {
Douglas Gregor39982192010-08-15 06:18:01 +0000235 case Result::RK_Declaration: {
Douglas Gregor59cab552010-08-16 23:05:20 +0000236 bool IsNestedNameSpecifier = false;
Douglas Gregor39982192010-08-15 06:18:01 +0000237 CachedCodeCompletionResult CachedResult;
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000238 CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema,
Douglas Gregor162b7122011-02-16 19:08:06 +0000239 *CachedCompletionAllocator);
Douglas Gregor39982192010-08-15 06:18:01 +0000240 CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration,
Douglas Gregor59cab552010-08-16 23:05:20 +0000241 Ctx->getLangOptions(),
242 IsNestedNameSpecifier);
Douglas Gregor39982192010-08-15 06:18:01 +0000243 CachedResult.Priority = Results[I].Priority;
244 CachedResult.Kind = Results[I].CursorKind;
Douglas Gregorf757a122010-08-23 23:00:57 +0000245 CachedResult.Availability = Results[I].Availability;
Douglas Gregor24747402010-08-16 16:46:30 +0000246
Douglas Gregorb61c07a2010-08-16 18:08:11 +0000247 // Keep track of the type of this completion in an ASTContext-agnostic
248 // way.
Douglas Gregor24747402010-08-16 16:46:30 +0000249 QualType UsageType = getDeclUsageType(*Ctx, Results[I].Declaration);
Douglas Gregorb61c07a2010-08-16 18:08:11 +0000250 if (UsageType.isNull()) {
Douglas Gregor24747402010-08-16 16:46:30 +0000251 CachedResult.TypeClass = STC_Void;
Douglas Gregorb61c07a2010-08-16 18:08:11 +0000252 CachedResult.Type = 0;
253 } else {
254 CanQualType CanUsageType
255 = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
256 CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
257
258 // Determine whether we have already seen this type. If so, we save
259 // ourselves the work of formatting the type string by using the
260 // temporary, CanQualType-based hash table to find the associated value.
261 unsigned &TypeValue = CompletionTypes[CanUsageType];
262 if (TypeValue == 0) {
263 TypeValue = CompletionTypes.size();
264 CachedCompletionTypes[QualType(CanUsageType).getAsString()]
265 = TypeValue;
266 }
267
268 CachedResult.Type = TypeValue;
Douglas Gregor24747402010-08-16 16:46:30 +0000269 }
Douglas Gregorb61c07a2010-08-16 18:08:11 +0000270
Douglas Gregor39982192010-08-15 06:18:01 +0000271 CachedCompletionResults.push_back(CachedResult);
Douglas Gregor59cab552010-08-16 23:05:20 +0000272
273 /// Handle nested-name-specifiers in C++.
274 if (TheSema->Context.getLangOptions().CPlusPlus &&
275 IsNestedNameSpecifier && !Results[I].StartsNestedNameSpecifier) {
276 // The contexts in which a nested-name-specifier can appear in C++.
277 unsigned NNSContexts
278 = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
279 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
280 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
281 | (1 << (CodeCompletionContext::CCC_Statement - 1))
282 | (1 << (CodeCompletionContext::CCC_Expression - 1))
283 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
284 | (1 << (CodeCompletionContext::CCC_EnumTag - 1))
285 | (1 << (CodeCompletionContext::CCC_UnionTag - 1))
286 | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1))
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000287 | (1 << (CodeCompletionContext::CCC_Type - 1))
Douglas Gregor5e35d592010-09-14 23:59:36 +0000288 | (1 << (CodeCompletionContext::CCC_PotentiallyQualifiedName - 1))
289 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1));
Douglas Gregor59cab552010-08-16 23:05:20 +0000290
291 if (isa<NamespaceDecl>(Results[I].Declaration) ||
292 isa<NamespaceAliasDecl>(Results[I].Declaration))
293 NNSContexts |= (1 << (CodeCompletionContext::CCC_Namespace - 1));
294
295 if (unsigned RemainingContexts
296 = NNSContexts & ~CachedResult.ShowInContexts) {
297 // If there any contexts where this completion can be a
298 // nested-name-specifier but isn't already an option, create a
299 // nested-name-specifier completion.
300 Results[I].StartsNestedNameSpecifier = true;
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000301 CachedResult.Completion
302 = Results[I].CreateCodeCompletionString(*TheSema,
Douglas Gregor162b7122011-02-16 19:08:06 +0000303 *CachedCompletionAllocator);
Douglas Gregor59cab552010-08-16 23:05:20 +0000304 CachedResult.ShowInContexts = RemainingContexts;
305 CachedResult.Priority = CCP_NestedNameSpecifier;
306 CachedResult.TypeClass = STC_Void;
307 CachedResult.Type = 0;
308 CachedCompletionResults.push_back(CachedResult);
309 }
310 }
Douglas Gregorb14904c2010-08-13 22:48:40 +0000311 break;
Douglas Gregor39982192010-08-15 06:18:01 +0000312 }
313
Douglas Gregorb14904c2010-08-13 22:48:40 +0000314 case Result::RK_Keyword:
315 case Result::RK_Pattern:
316 // Ignore keywords and patterns; we don't care, since they are so
317 // easily regenerated.
318 break;
319
320 case Result::RK_Macro: {
321 CachedCodeCompletionResult CachedResult;
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000322 CachedResult.Completion
323 = Results[I].CreateCodeCompletionString(*TheSema,
Douglas Gregor162b7122011-02-16 19:08:06 +0000324 *CachedCompletionAllocator);
Douglas Gregorb14904c2010-08-13 22:48:40 +0000325 CachedResult.ShowInContexts
326 = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
327 | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1))
328 | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1))
329 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
330 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
331 | (1 << (CodeCompletionContext::CCC_Statement - 1))
332 | (1 << (CodeCompletionContext::CCC_Expression - 1))
Douglas Gregor12785102010-08-24 20:21:13 +0000333 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
Douglas Gregorec00a262010-08-24 22:20:20 +0000334 | (1 << (CodeCompletionContext::CCC_MacroNameUse - 1))
Douglas Gregor5e35d592010-09-14 23:59:36 +0000335 | (1 << (CodeCompletionContext::CCC_PreprocessorExpression - 1))
336 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1));
337
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000338
Douglas Gregorb14904c2010-08-13 22:48:40 +0000339 CachedResult.Priority = Results[I].Priority;
340 CachedResult.Kind = Results[I].CursorKind;
Douglas Gregorf757a122010-08-23 23:00:57 +0000341 CachedResult.Availability = Results[I].Availability;
Douglas Gregor6e240332010-08-16 16:18:59 +0000342 CachedResult.TypeClass = STC_Void;
Douglas Gregorb61c07a2010-08-16 18:08:11 +0000343 CachedResult.Type = 0;
Douglas Gregorb14904c2010-08-13 22:48:40 +0000344 CachedCompletionResults.push_back(CachedResult);
345 break;
346 }
347 }
Douglas Gregorb14904c2010-08-13 22:48:40 +0000348 }
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000349
350 // Save the current top-level hash value.
351 CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
Douglas Gregorb14904c2010-08-13 22:48:40 +0000352}
353
354void ASTUnit::ClearCachedCompletionResults() {
Douglas Gregorb14904c2010-08-13 22:48:40 +0000355 CachedCompletionResults.clear();
Douglas Gregorb61c07a2010-08-16 18:08:11 +0000356 CachedCompletionTypes.clear();
Douglas Gregor162b7122011-02-16 19:08:06 +0000357 CachedCompletionAllocator = 0;
Douglas Gregorb14904c2010-08-13 22:48:40 +0000358}
359
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000360namespace {
361
Sebastian Redl2c499f62010-08-18 23:56:43 +0000362/// \brief Gathers information from ASTReader that will be used to initialize
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000363/// a Preprocessor.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000364class ASTInfoCollector : public ASTReaderListener {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000365 LangOptions &LangOpt;
366 HeaderSearch &HSI;
367 std::string &TargetTriple;
368 std::string &Predefines;
369 unsigned &Counter;
Mike Stump11289f42009-09-09 15:08:12 +0000370
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000371 unsigned NumHeaderInfos;
Mike Stump11289f42009-09-09 15:08:12 +0000372
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000373public:
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000374 ASTInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000375 std::string &TargetTriple, std::string &Predefines,
376 unsigned &Counter)
377 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
378 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
Mike Stump11289f42009-09-09 15:08:12 +0000379
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000380 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
381 LangOpt = LangOpts;
382 return false;
383 }
Mike Stump11289f42009-09-09 15:08:12 +0000384
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000385 virtual bool ReadTargetTriple(llvm::StringRef Triple) {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000386 TargetTriple = Triple;
387 return false;
388 }
Mike Stump11289f42009-09-09 15:08:12 +0000389
Sebastian Redl8b41f302010-07-14 23:29:55 +0000390 virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000391 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000392 std::string &SuggestedPredefines) {
Sebastian Redl8b41f302010-07-14 23:29:55 +0000393 Predefines = Buffers[0].Data;
394 for (unsigned I = 1, N = Buffers.size(); I != N; ++I) {
395 Predefines += Buffers[I].Data;
396 }
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000397 return false;
398 }
Mike Stump11289f42009-09-09 15:08:12 +0000399
Douglas Gregora2f49452010-03-16 19:09:18 +0000400 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000401 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
402 }
Mike Stump11289f42009-09-09 15:08:12 +0000403
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000404 virtual void ReadCounter(unsigned Value) {
405 Counter = Value;
406 }
407};
408
Douglas Gregor33cdd812010-02-18 18:08:43 +0000409class StoredDiagnosticClient : public DiagnosticClient {
410 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags;
411
412public:
413 explicit StoredDiagnosticClient(
414 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
415 : StoredDiags(StoredDiags) { }
416
417 virtual void HandleDiagnostic(Diagnostic::Level Level,
418 const DiagnosticInfo &Info);
419};
420
421/// \brief RAII object that optionally captures diagnostics, if
422/// there is no diagnostic client to capture them already.
423class CaptureDroppedDiagnostics {
424 Diagnostic &Diags;
425 StoredDiagnosticClient Client;
426 DiagnosticClient *PreviousClient;
427
428public:
429 CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags,
Douglas Gregor44c6ee72010-11-11 00:39:14 +0000430 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000431 : Diags(Diags), Client(StoredDiags), PreviousClient(0)
Douglas Gregor33cdd812010-02-18 18:08:43 +0000432 {
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000433 if (RequestCapture || Diags.getClient() == 0) {
434 PreviousClient = Diags.takeClient();
Douglas Gregor33cdd812010-02-18 18:08:43 +0000435 Diags.setClient(&Client);
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000436 }
Douglas Gregor33cdd812010-02-18 18:08:43 +0000437 }
438
439 ~CaptureDroppedDiagnostics() {
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000440 if (Diags.getClient() == &Client) {
441 Diags.takeClient();
442 Diags.setClient(PreviousClient);
443 }
Douglas Gregor33cdd812010-02-18 18:08:43 +0000444 }
445};
446
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000447} // anonymous namespace
448
Douglas Gregor33cdd812010-02-18 18:08:43 +0000449void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level,
450 const DiagnosticInfo &Info) {
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000451 // Default implementation (Warnings/errors count).
452 DiagnosticClient::HandleDiagnostic(Level, Info);
453
Douglas Gregor33cdd812010-02-18 18:08:43 +0000454 StoredDiags.push_back(StoredDiagnostic(Level, Info));
455}
456
Steve Naroffc0683b92009-09-03 18:19:54 +0000457const std::string &ASTUnit::getOriginalSourceFileName() {
Daniel Dunbara8a50932009-12-02 08:44:16 +0000458 return OriginalSourceFile;
Steve Naroffc0683b92009-09-03 18:19:54 +0000459}
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000460
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000461const std::string &ASTUnit::getASTFileName() {
462 assert(isMainFileAST() && "Not an ASTUnit from an AST file!");
Sebastian Redl2c499f62010-08-18 23:56:43 +0000463 return static_cast<ASTReader *>(Ctx->getExternalSource())->getFileName();
Steve Naroff44cd60e2009-10-15 22:23:48 +0000464}
465
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000466llvm::MemoryBuffer *ASTUnit::getBufferForFile(llvm::StringRef Filename,
Chris Lattner26b5c192010-11-23 09:19:42 +0000467 std::string *ErrorStr) {
Chris Lattner5159f612010-11-23 08:35:12 +0000468 assert(FileMgr);
Chris Lattner26b5c192010-11-23 09:19:42 +0000469 return FileMgr->getBufferForFile(Filename, ErrorStr);
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000470}
471
Douglas Gregor44c6ee72010-11-11 00:39:14 +0000472/// \brief Configure the diagnostics object for use with ASTUnit.
473void ASTUnit::ConfigureDiags(llvm::IntrusiveRefCntPtr<Diagnostic> &Diags,
Douglas Gregor345c1bc2011-01-19 01:02:47 +0000474 const char **ArgBegin, const char **ArgEnd,
Douglas Gregor44c6ee72010-11-11 00:39:14 +0000475 ASTUnit &AST, bool CaptureDiagnostics) {
476 if (!Diags.getPtr()) {
477 // No diagnostics engine was provided, so create our own diagnostics object
478 // with the default options.
479 DiagnosticOptions DiagOpts;
480 DiagnosticClient *Client = 0;
481 if (CaptureDiagnostics)
482 Client = new StoredDiagnosticClient(AST.StoredDiagnostics);
Douglas Gregor345c1bc2011-01-19 01:02:47 +0000483 Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd- ArgBegin,
484 ArgBegin, Client);
Douglas Gregor44c6ee72010-11-11 00:39:14 +0000485 } else if (CaptureDiagnostics) {
486 Diags->setClient(new StoredDiagnosticClient(AST.StoredDiagnostics));
487 }
488}
489
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000490ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename,
Douglas Gregor7f95d262010-04-05 23:52:57 +0000491 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000492 const FileSystemOptions &FileSystemOpts,
Ted Kremenek8bcb1c62009-10-17 00:34:24 +0000493 bool OnlyLocalDecls,
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000494 RemappedFile *RemappedFiles,
Douglas Gregor33cdd812010-02-18 18:08:43 +0000495 unsigned NumRemappedFiles,
496 bool CaptureDiagnostics) {
Douglas Gregord03e8232010-04-05 21:10:19 +0000497 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
Douglas Gregor345c1bc2011-01-19 01:02:47 +0000498 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000499
Douglas Gregor16bef852009-10-16 20:01:17 +0000500 AST->OnlyLocalDecls = OnlyLocalDecls;
Douglas Gregor44c6ee72010-11-11 00:39:14 +0000501 AST->CaptureDiagnostics = CaptureDiagnostics;
Douglas Gregor7f95d262010-04-05 23:52:57 +0000502 AST->Diagnostics = Diags;
Chris Lattner3f5a9ef2010-11-23 07:51:02 +0000503 AST->FileMgr.reset(new FileManager(FileSystemOpts));
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000504 AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics(),
Chris Lattner5159f612010-11-23 08:35:12 +0000505 AST->getFileManager()));
506 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
Douglas Gregor6fd55e02010-08-13 03:15:25 +0000507
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000508 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
509 // Create the file entry for the file that we're mapping from.
510 const FileEntry *FromFile
511 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
512 RemappedFiles[I].second->getBufferSize(),
Chris Lattner5159f612010-11-23 08:35:12 +0000513 0);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000514 if (!FromFile) {
Douglas Gregord03e8232010-04-05 21:10:19 +0000515 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000516 << RemappedFiles[I].first;
Douglas Gregor89a56c52010-02-27 01:32:48 +0000517 delete RemappedFiles[I].second;
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000518 continue;
519 }
520
521 // Override the contents of the "from" file with the contents of
522 // the "to" file.
523 AST->getSourceManager().overrideFileContents(FromFile,
524 RemappedFiles[I].second);
525 }
526
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000527 // Gather Info for preprocessor construction later on.
Mike Stump11289f42009-09-09 15:08:12 +0000528
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000529 LangOptions LangInfo;
530 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
531 std::string TargetTriple;
532 std::string Predefines;
533 unsigned Counter;
534
Sebastian Redl2c499f62010-08-18 23:56:43 +0000535 llvm::OwningPtr<ASTReader> Reader;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000536
Sebastian Redl2c499f62010-08-18 23:56:43 +0000537 Reader.reset(new ASTReader(AST->getSourceManager(), AST->getFileManager(),
Chris Lattner5159f612010-11-23 08:35:12 +0000538 AST->getDiagnostics()));
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000539 Reader->setListener(new ASTInfoCollector(LangInfo, HeaderInfo, TargetTriple,
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000540 Predefines, Counter));
541
Sebastian Redl009e7f22010-10-05 16:15:19 +0000542 switch (Reader->ReadAST(Filename, ASTReader::MainFile)) {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000543 case ASTReader::Success:
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000544 break;
Mike Stump11289f42009-09-09 15:08:12 +0000545
Sebastian Redl2c499f62010-08-18 23:56:43 +0000546 case ASTReader::Failure:
547 case ASTReader::IgnorePCH:
Douglas Gregord03e8232010-04-05 21:10:19 +0000548 AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000549 return NULL;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000550 }
Mike Stump11289f42009-09-09 15:08:12 +0000551
Daniel Dunbara8a50932009-12-02 08:44:16 +0000552 AST->OriginalSourceFile = Reader->getOriginalSourceFile();
553
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000554 // AST file loaded successfully. Now create the preprocessor.
Mike Stump11289f42009-09-09 15:08:12 +0000555
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000556 // Get information about the target being compiled for.
Daniel Dunbarb9bbd542009-11-15 06:48:46 +0000557 //
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000558 // FIXME: This is broken, we should store the TargetOptions in the AST file.
Daniel Dunbarb9bbd542009-11-15 06:48:46 +0000559 TargetOptions TargetOpts;
560 TargetOpts.ABI = "";
John McCall1c456c82010-08-22 06:43:33 +0000561 TargetOpts.CXXABI = "";
Daniel Dunbarb9bbd542009-11-15 06:48:46 +0000562 TargetOpts.CPU = "";
563 TargetOpts.Features.clear();
564 TargetOpts.Triple = TargetTriple;
Douglas Gregord03e8232010-04-05 21:10:19 +0000565 AST->Target.reset(TargetInfo::CreateTargetInfo(AST->getDiagnostics(),
566 TargetOpts));
567 AST->PP.reset(new Preprocessor(AST->getDiagnostics(), LangInfo,
568 *AST->Target.get(),
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000569 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000570 Preprocessor &PP = *AST->PP.get();
571
Daniel Dunbarb7bbfdd2009-09-21 03:03:47 +0000572 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000573 PP.setCounterValue(Counter);
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000574 Reader->setPreprocessor(PP);
Mike Stump11289f42009-09-09 15:08:12 +0000575
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000576 // Create and initialize the ASTContext.
577
578 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000579 AST->getSourceManager(),
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000580 *AST->Target.get(),
581 PP.getIdentifierTable(),
582 PP.getSelectorTable(),
583 PP.getBuiltinInfo(),
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000584 /* size_reserve = */0));
585 ASTContext &Context = *AST->Ctx.get();
Mike Stump11289f42009-09-09 15:08:12 +0000586
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000587 Reader->InitializeContext(Context);
Mike Stump11289f42009-09-09 15:08:12 +0000588
Sebastian Redl2c499f62010-08-18 23:56:43 +0000589 // Attach the AST reader to the AST context as an external AST
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000590 // source, so that declarations will be deserialized from the
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000591 // AST file as needed.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000592 ASTReader *ReaderPtr = Reader.get();
Douglas Gregor6fd55e02010-08-13 03:15:25 +0000593 llvm::OwningPtr<ExternalASTSource> Source(Reader.take());
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000594 Context.setExternalSource(Source);
595
Douglas Gregor6fd55e02010-08-13 03:15:25 +0000596 // Create an AST consumer, even though it isn't used.
597 AST->Consumer.reset(new ASTConsumer);
598
Sebastian Redl2c499f62010-08-18 23:56:43 +0000599 // Create a semantic analysis object and tell the AST reader about it.
Douglas Gregor6fd55e02010-08-13 03:15:25 +0000600 AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer));
601 AST->TheSema->Initialize();
602 ReaderPtr->InitializeSema(*AST->TheSema);
603
Mike Stump11289f42009-09-09 15:08:12 +0000604 return AST.take();
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000605}
Daniel Dunbar764c0822009-12-01 09:51:01 +0000606
607namespace {
608
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000609/// \brief Preprocessor callback class that updates a hash value with the names
610/// of all macros that have been defined by the translation unit.
611class MacroDefinitionTrackerPPCallbacks : public PPCallbacks {
612 unsigned &Hash;
613
614public:
615 explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { }
616
617 virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
618 Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash);
619 }
620};
621
622/// \brief Add the given declaration to the hash of all top-level entities.
623void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) {
624 if (!D)
625 return;
626
627 DeclContext *DC = D->getDeclContext();
628 if (!DC)
629 return;
630
631 if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit()))
632 return;
633
634 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
635 if (ND->getIdentifier())
636 Hash = llvm::HashString(ND->getIdentifier()->getName(), Hash);
637 else if (DeclarationName Name = ND->getDeclName()) {
638 std::string NameStr = Name.getAsString();
639 Hash = llvm::HashString(NameStr, Hash);
640 }
641 return;
642 }
643
644 if (ObjCForwardProtocolDecl *Forward
645 = dyn_cast<ObjCForwardProtocolDecl>(D)) {
646 for (ObjCForwardProtocolDecl::protocol_iterator
647 P = Forward->protocol_begin(),
648 PEnd = Forward->protocol_end();
649 P != PEnd; ++P)
650 AddTopLevelDeclarationToHash(*P, Hash);
651 return;
652 }
653
654 if (ObjCClassDecl *Class = llvm::dyn_cast<ObjCClassDecl>(D)) {
655 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
656 I != IEnd; ++I)
657 AddTopLevelDeclarationToHash(I->getInterface(), Hash);
658 return;
659 }
660}
661
Daniel Dunbar644dca02009-12-04 08:17:33 +0000662class TopLevelDeclTrackerConsumer : public ASTConsumer {
663 ASTUnit &Unit;
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000664 unsigned &Hash;
665
Daniel Dunbar644dca02009-12-04 08:17:33 +0000666public:
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000667 TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
668 : Unit(_Unit), Hash(Hash) {
669 Hash = 0;
670 }
671
Daniel Dunbar644dca02009-12-04 08:17:33 +0000672 void HandleTopLevelDecl(DeclGroupRef D) {
Ted Kremenekacc59c32010-05-03 20:16:35 +0000673 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
674 Decl *D = *it;
675 // FIXME: Currently ObjC method declarations are incorrectly being
676 // reported as top-level declarations, even though their DeclContext
677 // is the containing ObjC @interface/@implementation. This is a
678 // fundamental problem in the parser right now.
679 if (isa<ObjCMethodDecl>(D))
680 continue;
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000681
682 AddTopLevelDeclarationToHash(D, Hash);
Douglas Gregore9db88f2010-08-03 19:06:41 +0000683 Unit.addTopLevelDecl(D);
Ted Kremenekacc59c32010-05-03 20:16:35 +0000684 }
Daniel Dunbar644dca02009-12-04 08:17:33 +0000685 }
Sebastian Redleaa4ade2010-08-11 18:52:41 +0000686
687 // We're not interested in "interesting" decls.
688 void HandleInterestingDecl(DeclGroupRef) {}
Daniel Dunbar644dca02009-12-04 08:17:33 +0000689};
690
691class TopLevelDeclTrackerAction : public ASTFrontendAction {
692public:
693 ASTUnit &Unit;
694
Daniel Dunbar764c0822009-12-01 09:51:01 +0000695 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
696 llvm::StringRef InFile) {
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000697 CI.getPreprocessor().addPPCallbacks(
698 new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
699 return new TopLevelDeclTrackerConsumer(Unit,
700 Unit.getCurrentTopLevelHashValue());
Daniel Dunbar764c0822009-12-01 09:51:01 +0000701 }
702
703public:
Daniel Dunbar644dca02009-12-04 08:17:33 +0000704 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
705
Daniel Dunbar764c0822009-12-01 09:51:01 +0000706 virtual bool hasCodeCompletionSupport() const { return false; }
Douglas Gregor028d3e42010-08-09 20:45:32 +0000707 virtual bool usesCompleteTranslationUnit() {
708 return Unit.isCompleteTranslationUnit();
709 }
Daniel Dunbar764c0822009-12-01 09:51:01 +0000710};
711
Douglas Gregorf88e35b2010-11-30 06:16:57 +0000712class PrecompilePreambleConsumer : public PCHGenerator,
713 public ASTSerializationListener {
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000714 ASTUnit &Unit;
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000715 unsigned &Hash;
Douglas Gregore9db88f2010-08-03 19:06:41 +0000716 std::vector<Decl *> TopLevelDecls;
Douglas Gregorf88e35b2010-11-30 06:16:57 +0000717
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000718public:
719 PrecompilePreambleConsumer(ASTUnit &Unit,
720 const Preprocessor &PP, bool Chaining,
721 const char *isysroot, llvm::raw_ostream *Out)
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000722 : PCHGenerator(PP, "", Chaining, isysroot, Out), Unit(Unit),
723 Hash(Unit.getCurrentTopLevelHashValue()) {
724 Hash = 0;
725 }
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000726
Douglas Gregore9db88f2010-08-03 19:06:41 +0000727 virtual void HandleTopLevelDecl(DeclGroupRef D) {
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000728 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
729 Decl *D = *it;
730 // FIXME: Currently ObjC method declarations are incorrectly being
731 // reported as top-level declarations, even though their DeclContext
732 // is the containing ObjC @interface/@implementation. This is a
733 // fundamental problem in the parser right now.
734 if (isa<ObjCMethodDecl>(D))
735 continue;
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000736 AddTopLevelDeclarationToHash(D, Hash);
Douglas Gregore9db88f2010-08-03 19:06:41 +0000737 TopLevelDecls.push_back(D);
738 }
739 }
740
741 virtual void HandleTranslationUnit(ASTContext &Ctx) {
742 PCHGenerator::HandleTranslationUnit(Ctx);
743 if (!Unit.getDiagnostics().hasErrorOccurred()) {
744 // Translate the top-level declarations we captured during
745 // parsing into declaration IDs in the precompiled
746 // preamble. This will allow us to deserialize those top-level
747 // declarations when requested.
748 for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I)
749 Unit.addTopLevelDeclFromPreamble(
750 getWriter().getDeclID(TopLevelDecls[I]));
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000751 }
752 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +0000753
754 virtual void SerializedPreprocessedEntity(PreprocessedEntity *Entity,
755 uint64_t Offset) {
756 Unit.addPreprocessedEntityFromPreamble(Offset);
757 }
758
759 virtual ASTSerializationListener *GetASTSerializationListener() {
760 return this;
761 }
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000762};
763
764class PrecompilePreambleAction : public ASTFrontendAction {
765 ASTUnit &Unit;
766
767public:
768 explicit PrecompilePreambleAction(ASTUnit &Unit) : Unit(Unit) {}
769
770 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
771 llvm::StringRef InFile) {
772 std::string Sysroot;
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000773 std::string OutputFile;
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000774 llvm::raw_ostream *OS = 0;
775 bool Chaining;
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000776 if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot,
777 OutputFile,
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000778 OS, Chaining))
779 return 0;
780
781 const char *isysroot = CI.getFrontendOpts().RelocatablePCH ?
782 Sysroot.c_str() : 0;
Douglas Gregordf7a79a2011-02-16 18:16:54 +0000783 CI.getPreprocessor().addPPCallbacks(
784 new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000785 return new PrecompilePreambleConsumer(Unit, CI.getPreprocessor(), Chaining,
786 isysroot, OS);
787 }
788
789 virtual bool hasCodeCompletionSupport() const { return false; }
790 virtual bool hasASTFileSupport() const { return false; }
Douglas Gregor028d3e42010-08-09 20:45:32 +0000791 virtual bool usesCompleteTranslationUnit() { return false; }
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000792};
793
Daniel Dunbar764c0822009-12-01 09:51:01 +0000794}
795
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000796/// Parse the source file into a translation unit using the given compiler
797/// invocation, replacing the current translation unit.
798///
799/// \returns True if a failure occurred that causes the ASTUnit not to
800/// contain any translation-unit information, false otherwise.
Douglas Gregor6481ef12010-07-24 00:38:13 +0000801bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) {
Douglas Gregor96c04262010-07-27 14:52:07 +0000802 delete SavedMainFileBuffer;
803 SavedMainFileBuffer = 0;
804
Douglas Gregora0734c52010-08-19 01:33:06 +0000805 if (!Invocation.get()) {
806 delete OverrideMainBuffer;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000807 return true;
Douglas Gregora0734c52010-08-19 01:33:06 +0000808 }
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000809
Daniel Dunbar764c0822009-12-01 09:51:01 +0000810 // Create the compiler instance to use for building the AST.
Daniel Dunbar7afbb8c2009-12-02 08:43:56 +0000811 CompilerInstance Clang;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000812 Clang.setInvocation(Invocation.take());
813 OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
814
Douglas Gregor8e984da2010-08-04 16:47:14 +0000815 // Set up diagnostics, capturing any diagnostics that would
816 // otherwise be dropped.
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000817 Clang.setDiagnostics(&getDiagnostics());
Douglas Gregord03e8232010-04-05 21:10:19 +0000818
Daniel Dunbar764c0822009-12-01 09:51:01 +0000819 // Create the target instance.
Douglas Gregorffd6dc42011-01-27 18:02:58 +0000820 Clang.getTargetOpts().Features = TargetFeatures;
Daniel Dunbar764c0822009-12-01 09:51:01 +0000821 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
822 Clang.getTargetOpts()));
Douglas Gregora0734c52010-08-19 01:33:06 +0000823 if (!Clang.hasTarget()) {
824 delete OverrideMainBuffer;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000825 return true;
Douglas Gregora0734c52010-08-19 01:33:06 +0000826 }
827
Daniel Dunbar764c0822009-12-01 09:51:01 +0000828 // Inform the target of the language options.
829 //
830 // FIXME: We shouldn't need to do this, the target should be immutable once
831 // created. This complexity should be lifted elsewhere.
832 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts());
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000833
Daniel Dunbar764c0822009-12-01 09:51:01 +0000834 assert(Clang.getFrontendOpts().Inputs.size() == 1 &&
835 "Invocation must have exactly one source file!");
Daniel Dunbar9b491e72010-06-07 23:22:09 +0000836 assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST &&
Daniel Dunbar764c0822009-12-01 09:51:01 +0000837 "FIXME: AST inputs not yet supported here!");
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000838 assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
839 "IR inputs not support here!");
Daniel Dunbar764c0822009-12-01 09:51:01 +0000840
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000841 // Configure the various subsystems.
842 // FIXME: Should we retain the previous file manager?
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000843 FileSystemOpts = Clang.getFileSystemOpts();
Chris Lattner5159f612010-11-23 08:35:12 +0000844 FileMgr.reset(new FileManager(Clang.getFileSystemOpts()));
845 SourceMgr.reset(new SourceManager(getDiagnostics(), *FileMgr));
Douglas Gregor6fd55e02010-08-13 03:15:25 +0000846 TheSema.reset();
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000847 Ctx.reset();
848 PP.reset();
849
850 // Clear out old caches and data.
851 TopLevelDecls.clear();
Douglas Gregorf88e35b2010-11-30 06:16:57 +0000852 PreprocessedEntities.clear();
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000853 CleanTemporaryFiles();
854 PreprocessedEntitiesByFile.clear();
Douglas Gregord9a30af2010-08-02 20:51:39 +0000855
Douglas Gregor7b02b582010-08-20 00:02:33 +0000856 if (!OverrideMainBuffer) {
Douglas Gregor7bb8af62010-10-12 00:50:20 +0000857 StoredDiagnostics.erase(
858 StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver,
859 StoredDiagnostics.end());
Douglas Gregor7b02b582010-08-20 00:02:33 +0000860 TopLevelDeclsInPreamble.clear();
Douglas Gregorf88e35b2010-11-30 06:16:57 +0000861 PreprocessedEntitiesInPreamble.clear();
Douglas Gregor7b02b582010-08-20 00:02:33 +0000862 }
863
Daniel Dunbar764c0822009-12-01 09:51:01 +0000864 // Create a file manager object to provide access to and cache the filesystem.
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000865 Clang.setFileManager(&getFileManager());
866
Daniel Dunbar764c0822009-12-01 09:51:01 +0000867 // Create the source manager.
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000868 Clang.setSourceManager(&getSourceManager());
869
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000870 // If the main file has been overridden due to the use of a preamble,
871 // make that override happen and introduce the preamble.
872 PreprocessorOptions &PreprocessorOpts = Clang.getPreprocessorOpts();
Douglas Gregor8e984da2010-08-04 16:47:14 +0000873 std::string PriorImplicitPCHInclude;
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000874 if (OverrideMainBuffer) {
875 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
876 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
877 PreprocessorOpts.PrecompiledPreambleBytes.second
878 = PreambleEndsAtStartOfLine;
Douglas Gregor8e984da2010-08-04 16:47:14 +0000879 PriorImplicitPCHInclude = PreprocessorOpts.ImplicitPCHInclude;
Douglas Gregor15ba0b32010-07-30 20:58:08 +0000880 PreprocessorOpts.ImplicitPCHInclude = PreambleFile;
Douglas Gregorce3a8292010-07-27 00:27:13 +0000881 PreprocessorOpts.DisablePCHValidation = true;
Douglas Gregor96c04262010-07-27 14:52:07 +0000882
Douglas Gregord9a30af2010-08-02 20:51:39 +0000883 // The stored diagnostic has the old source manager in it; update
884 // the locations to refer into the new source manager. Since we've
885 // been careful to make sure that the source manager's state
886 // before and after are identical, so that we can reuse the source
887 // location itself.
Douglas Gregor7bb8af62010-10-12 00:50:20 +0000888 for (unsigned I = NumStoredDiagnosticsFromDriver,
889 N = StoredDiagnostics.size();
890 I < N; ++I) {
Douglas Gregord9a30af2010-08-02 20:51:39 +0000891 FullSourceLoc Loc(StoredDiagnostics[I].getLocation(),
892 getSourceManager());
893 StoredDiagnostics[I].setLocation(Loc);
894 }
Douglas Gregor7bb8af62010-10-12 00:50:20 +0000895
896 // Keep track of the override buffer;
897 SavedMainFileBuffer = OverrideMainBuffer;
Douglas Gregor7b02b582010-08-20 00:02:33 +0000898 } else {
899 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
900 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000901 }
902
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000903 llvm::OwningPtr<TopLevelDeclTrackerAction> Act;
904 Act.reset(new TopLevelDeclTrackerAction(*this));
Daniel Dunbar644dca02009-12-04 08:17:33 +0000905 if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
Daniel Dunbar86546382010-06-07 23:23:06 +0000906 Clang.getFrontendOpts().Inputs[0].first))
Daniel Dunbar764c0822009-12-01 09:51:01 +0000907 goto error;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000908
Daniel Dunbar644dca02009-12-04 08:17:33 +0000909 Act->Execute();
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000910
Daniel Dunbard2f8be32009-12-01 21:57:33 +0000911 // Steal the created target, context, and preprocessor, and take back the
912 // source and file managers.
Douglas Gregor6fd55e02010-08-13 03:15:25 +0000913 TheSema.reset(Clang.takeSema());
914 Consumer.reset(Clang.takeASTConsumer());
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000915 Ctx.reset(Clang.takeASTContext());
916 PP.reset(Clang.takePreprocessor());
Daniel Dunbar764c0822009-12-01 09:51:01 +0000917 Clang.takeSourceManager();
918 Clang.takeFileManager();
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000919 Target.reset(Clang.takeTarget());
920
Daniel Dunbar644dca02009-12-04 08:17:33 +0000921 Act->EndSourceFile();
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000922
923 // Remove the overridden buffer we used for the preamble.
Douglas Gregor8e984da2010-08-04 16:47:14 +0000924 if (OverrideMainBuffer) {
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000925 PreprocessorOpts.eraseRemappedFile(
926 PreprocessorOpts.remapped_file_buffer_end() - 1);
Douglas Gregor8e984da2010-08-04 16:47:14 +0000927 PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude;
928 }
929
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000930 Invocation.reset(Clang.takeInvocation());
931 return false;
932
Daniel Dunbar764c0822009-12-01 09:51:01 +0000933error:
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000934 // Remove the overridden buffer we used for the preamble.
Douglas Gregorce3a8292010-07-27 00:27:13 +0000935 if (OverrideMainBuffer) {
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000936 PreprocessorOpts.eraseRemappedFile(
937 PreprocessorOpts.remapped_file_buffer_end() - 1);
Douglas Gregor8e984da2010-08-04 16:47:14 +0000938 PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude;
Douglas Gregora0734c52010-08-19 01:33:06 +0000939 delete OverrideMainBuffer;
Douglas Gregora3d3ba12010-10-06 21:11:08 +0000940 SavedMainFileBuffer = 0;
Douglas Gregorce3a8292010-07-27 00:27:13 +0000941 }
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000942
Douglas Gregorefc46952010-10-12 16:25:54 +0000943 StoredDiagnostics.clear();
Daniel Dunbar764c0822009-12-01 09:51:01 +0000944 Clang.takeSourceManager();
945 Clang.takeFileManager();
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000946 Invocation.reset(Clang.takeInvocation());
947 return true;
948}
949
Douglas Gregorbe2d8c62010-07-23 00:33:23 +0000950/// \brief Simple function to retrieve a path for a preamble precompiled header.
951static std::string GetPreamblePCHPath() {
952 // FIXME: This is lame; sys::Path should provide this function (in particular,
953 // it should know how to find the temporary files dir).
954 // FIXME: This is really lame. I copied this code from the Driver!
Douglas Gregor250ab1d2010-09-11 18:05:19 +0000955 // FIXME: This is a hack so that we can override the preamble file during
956 // crash-recovery testing, which is the only case where the preamble files
957 // are not necessarily cleaned up.
958 const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
959 if (TmpFile)
960 return TmpFile;
961
Douglas Gregorbe2d8c62010-07-23 00:33:23 +0000962 std::string Error;
963 const char *TmpDir = ::getenv("TMPDIR");
964 if (!TmpDir)
965 TmpDir = ::getenv("TEMP");
966 if (!TmpDir)
967 TmpDir = ::getenv("TMP");
Douglas Gregorce3449f2010-09-11 17:51:16 +0000968#ifdef LLVM_ON_WIN32
969 if (!TmpDir)
970 TmpDir = ::getenv("USERPROFILE");
971#endif
Douglas Gregorbe2d8c62010-07-23 00:33:23 +0000972 if (!TmpDir)
973 TmpDir = "/tmp";
974 llvm::sys::Path P(TmpDir);
Douglas Gregorce3449f2010-09-11 17:51:16 +0000975 P.createDirectoryOnDisk(true);
Douglas Gregorbe2d8c62010-07-23 00:33:23 +0000976 P.appendComponent("preamble");
Douglas Gregor20975b22010-08-11 13:06:56 +0000977 P.appendSuffix("pch");
Douglas Gregorbe2d8c62010-07-23 00:33:23 +0000978 if (P.createTemporaryFileOnDisk())
979 return std::string();
980
Douglas Gregorbe2d8c62010-07-23 00:33:23 +0000981 return P.str();
982}
983
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000984/// \brief Compute the preamble for the main file, providing the source buffer
985/// that corresponds to the main file along with a pair (bytes, start-of-line)
986/// that describes the preamble.
987std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
Douglas Gregor028d3e42010-08-09 20:45:32 +0000988ASTUnit::ComputePreamble(CompilerInvocation &Invocation,
989 unsigned MaxLines, bool &CreatedBuffer) {
Douglas Gregor4dde7492010-07-23 23:58:40 +0000990 FrontendOptions &FrontendOpts = Invocation.getFrontendOpts();
Chris Lattner5159f612010-11-23 08:35:12 +0000991 PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts();
Douglas Gregor4dde7492010-07-23 23:58:40 +0000992 CreatedBuffer = false;
993
Douglas Gregorbe2d8c62010-07-23 00:33:23 +0000994 // Try to determine if the main file has been remapped, either from the
995 // command line (to another file) or directly through the compiler invocation
996 // (to a memory buffer).
Douglas Gregor4dde7492010-07-23 23:58:40 +0000997 llvm::MemoryBuffer *Buffer = 0;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +0000998 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second);
999 if (const llvm::sys::FileStatus *MainFileStatus = MainFilePath.getFileStatus()) {
1000 // Check whether there is a file-file remapping of the main file
1001 for (PreprocessorOptions::remapped_file_iterator
Douglas Gregor4dde7492010-07-23 23:58:40 +00001002 M = PreprocessorOpts.remapped_file_begin(),
1003 E = PreprocessorOpts.remapped_file_end();
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001004 M != E;
1005 ++M) {
1006 llvm::sys::PathWithStatus MPath(M->first);
1007 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1008 if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1009 // We found a remapping. Try to load the resulting, remapped source.
Douglas Gregor4dde7492010-07-23 23:58:40 +00001010 if (CreatedBuffer) {
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001011 delete Buffer;
Douglas Gregor4dde7492010-07-23 23:58:40 +00001012 CreatedBuffer = false;
1013 }
1014
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00001015 Buffer = getBufferForFile(M->second);
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001016 if (!Buffer)
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001017 return std::make_pair((llvm::MemoryBuffer*)0,
1018 std::make_pair(0, true));
Douglas Gregor4dde7492010-07-23 23:58:40 +00001019 CreatedBuffer = true;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001020 }
1021 }
1022 }
1023
1024 // Check whether there is a file-buffer remapping. It supercedes the
1025 // file-file remapping.
1026 for (PreprocessorOptions::remapped_file_buffer_iterator
1027 M = PreprocessorOpts.remapped_file_buffer_begin(),
1028 E = PreprocessorOpts.remapped_file_buffer_end();
1029 M != E;
1030 ++M) {
1031 llvm::sys::PathWithStatus MPath(M->first);
1032 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1033 if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1034 // We found a remapping.
Douglas Gregor4dde7492010-07-23 23:58:40 +00001035 if (CreatedBuffer) {
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001036 delete Buffer;
Douglas Gregor4dde7492010-07-23 23:58:40 +00001037 CreatedBuffer = false;
1038 }
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001039
Douglas Gregor4dde7492010-07-23 23:58:40 +00001040 Buffer = const_cast<llvm::MemoryBuffer *>(M->second);
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001041 }
1042 }
Douglas Gregor4dde7492010-07-23 23:58:40 +00001043 }
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001044 }
1045
1046 // If the main source file was not remapped, load it now.
1047 if (!Buffer) {
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00001048 Buffer = getBufferForFile(FrontendOpts.Inputs[0].second);
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001049 if (!Buffer)
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001050 return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true));
Douglas Gregor4dde7492010-07-23 23:58:40 +00001051
1052 CreatedBuffer = true;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001053 }
1054
Douglas Gregor028d3e42010-08-09 20:45:32 +00001055 return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer, MaxLines));
Douglas Gregor4dde7492010-07-23 23:58:40 +00001056}
1057
Douglas Gregor6481ef12010-07-24 00:38:13 +00001058static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old,
Douglas Gregor6481ef12010-07-24 00:38:13 +00001059 unsigned NewSize,
1060 llvm::StringRef NewName) {
1061 llvm::MemoryBuffer *Result
1062 = llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName);
1063 memcpy(const_cast<char*>(Result->getBufferStart()),
1064 Old->getBufferStart(), Old->getBufferSize());
1065 memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(),
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001066 ' ', NewSize - Old->getBufferSize() - 1);
1067 const_cast<char*>(Result->getBufferEnd())[-1] = '\n';
Douglas Gregor6481ef12010-07-24 00:38:13 +00001068
Douglas Gregor6481ef12010-07-24 00:38:13 +00001069 return Result;
1070}
1071
Douglas Gregor4dde7492010-07-23 23:58:40 +00001072/// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing
1073/// the source file.
1074///
1075/// This routine will compute the preamble of the main source file. If a
1076/// non-trivial preamble is found, it will precompile that preamble into a
1077/// precompiled header so that the precompiled preamble can be used to reduce
1078/// reparsing time. If a precompiled preamble has already been constructed,
1079/// this routine will determine if it is still valid and, if so, avoid
1080/// rebuilding the precompiled preamble.
1081///
Douglas Gregor028d3e42010-08-09 20:45:32 +00001082/// \param AllowRebuild When true (the default), this routine is
1083/// allowed to rebuild the precompiled preamble if it is found to be
1084/// out-of-date.
1085///
1086/// \param MaxLines When non-zero, the maximum number of lines that
1087/// can occur within the preamble.
1088///
Douglas Gregor6481ef12010-07-24 00:38:13 +00001089/// \returns If the precompiled preamble can be used, returns a newly-allocated
1090/// buffer that should be used in place of the main file when doing so.
1091/// Otherwise, returns a NULL pointer.
Douglas Gregor028d3e42010-08-09 20:45:32 +00001092llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
Douglas Gregorb97b6662010-08-20 00:59:43 +00001093 CompilerInvocation PreambleInvocation,
Douglas Gregor028d3e42010-08-09 20:45:32 +00001094 bool AllowRebuild,
1095 unsigned MaxLines) {
Douglas Gregor4dde7492010-07-23 23:58:40 +00001096 FrontendOptions &FrontendOpts = PreambleInvocation.getFrontendOpts();
1097 PreprocessorOptions &PreprocessorOpts
1098 = PreambleInvocation.getPreprocessorOpts();
1099
1100 bool CreatedPreambleBuffer = false;
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001101 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble
Douglas Gregor028d3e42010-08-09 20:45:32 +00001102 = ComputePreamble(PreambleInvocation, MaxLines, CreatedPreambleBuffer);
Douglas Gregor4dde7492010-07-23 23:58:40 +00001103
Douglas Gregor3edb1672010-11-16 20:45:51 +00001104 // If ComputePreamble() Take ownership of the
1105 llvm::OwningPtr<llvm::MemoryBuffer> OwnedPreambleBuffer;
1106 if (CreatedPreambleBuffer)
1107 OwnedPreambleBuffer.reset(NewPreamble.first);
1108
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001109 if (!NewPreamble.second.first) {
Douglas Gregor4dde7492010-07-23 23:58:40 +00001110 // We couldn't find a preamble in the main source. Clear out the current
1111 // preamble, if we have one. It's obviously no good any more.
1112 Preamble.clear();
1113 if (!PreambleFile.empty()) {
Douglas Gregor15ba0b32010-07-30 20:58:08 +00001114 llvm::sys::Path(PreambleFile).eraseFromDisk();
Douglas Gregor4dde7492010-07-23 23:58:40 +00001115 PreambleFile.clear();
1116 }
Douglas Gregorbb420ab2010-08-04 05:53:38 +00001117
1118 // The next time we actually see a preamble, precompile it.
1119 PreambleRebuildCounter = 1;
Douglas Gregor6481ef12010-07-24 00:38:13 +00001120 return 0;
Douglas Gregor4dde7492010-07-23 23:58:40 +00001121 }
1122
1123 if (!Preamble.empty()) {
1124 // We've previously computed a preamble. Check whether we have the same
1125 // preamble now that we did before, and that there's enough space in
1126 // the main-file buffer within the precompiled preamble to fit the
1127 // new main file.
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001128 if (Preamble.size() == NewPreamble.second.first &&
1129 PreambleEndsAtStartOfLine == NewPreamble.second.second &&
Douglas Gregorf5275a82010-07-24 00:42:07 +00001130 NewPreamble.first->getBufferSize() < PreambleReservedSize-2 &&
Douglas Gregor4dde7492010-07-23 23:58:40 +00001131 memcmp(&Preamble[0], NewPreamble.first->getBufferStart(),
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001132 NewPreamble.second.first) == 0) {
Douglas Gregor4dde7492010-07-23 23:58:40 +00001133 // The preamble has not changed. We may be able to re-use the precompiled
1134 // preamble.
Douglas Gregord9a30af2010-08-02 20:51:39 +00001135
Douglas Gregor0e119552010-07-31 00:40:00 +00001136 // Check that none of the files used by the preamble have changed.
1137 bool AnyFileChanged = false;
1138
1139 // First, make a record of those files that have been overridden via
1140 // remapping or unsaved_files.
1141 llvm::StringMap<std::pair<off_t, time_t> > OverriddenFiles;
1142 for (PreprocessorOptions::remapped_file_iterator
1143 R = PreprocessorOpts.remapped_file_begin(),
1144 REnd = PreprocessorOpts.remapped_file_end();
1145 !AnyFileChanged && R != REnd;
1146 ++R) {
1147 struct stat StatBuf;
1148 if (stat(R->second.c_str(), &StatBuf)) {
1149 // If we can't stat the file we're remapping to, assume that something
1150 // horrible happened.
1151 AnyFileChanged = true;
1152 break;
1153 }
Douglas Gregor6481ef12010-07-24 00:38:13 +00001154
Douglas Gregor0e119552010-07-31 00:40:00 +00001155 OverriddenFiles[R->first] = std::make_pair(StatBuf.st_size,
1156 StatBuf.st_mtime);
1157 }
1158 for (PreprocessorOptions::remapped_file_buffer_iterator
1159 R = PreprocessorOpts.remapped_file_buffer_begin(),
1160 REnd = PreprocessorOpts.remapped_file_buffer_end();
1161 !AnyFileChanged && R != REnd;
1162 ++R) {
1163 // FIXME: Should we actually compare the contents of file->buffer
1164 // remappings?
1165 OverriddenFiles[R->first] = std::make_pair(R->second->getBufferSize(),
1166 0);
1167 }
1168
1169 // Check whether anything has changed.
1170 for (llvm::StringMap<std::pair<off_t, time_t> >::iterator
1171 F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end();
1172 !AnyFileChanged && F != FEnd;
1173 ++F) {
1174 llvm::StringMap<std::pair<off_t, time_t> >::iterator Overridden
1175 = OverriddenFiles.find(F->first());
1176 if (Overridden != OverriddenFiles.end()) {
1177 // This file was remapped; check whether the newly-mapped file
1178 // matches up with the previous mapping.
1179 if (Overridden->second != F->second)
1180 AnyFileChanged = true;
1181 continue;
1182 }
1183
1184 // The file was not remapped; check whether it has changed on disk.
1185 struct stat StatBuf;
1186 if (stat(F->first(), &StatBuf)) {
1187 // If we can't stat the file, assume that something horrible happened.
1188 AnyFileChanged = true;
1189 } else if (StatBuf.st_size != F->second.first ||
1190 StatBuf.st_mtime != F->second.second)
1191 AnyFileChanged = true;
1192 }
1193
1194 if (!AnyFileChanged) {
Douglas Gregord9a30af2010-08-02 20:51:39 +00001195 // Okay! We can re-use the precompiled preamble.
1196
1197 // Set the state of the diagnostic object to mimic its state
1198 // after parsing the preamble.
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001199 // FIXME: This won't catch any #pragma push warning changes that
1200 // have occurred in the preamble.
Douglas Gregord9a30af2010-08-02 20:51:39 +00001201 getDiagnostics().Reset();
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001202 ProcessWarningOptions(getDiagnostics(),
1203 PreambleInvocation.getDiagnosticOpts());
Douglas Gregord9a30af2010-08-02 20:51:39 +00001204 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1205 if (StoredDiagnostics.size() > NumStoredDiagnosticsInPreamble)
1206 StoredDiagnostics.erase(
1207 StoredDiagnostics.begin() + NumStoredDiagnosticsInPreamble,
1208 StoredDiagnostics.end());
1209
1210 // Create a version of the main file buffer that is padded to
1211 // buffer size we reserved when creating the preamble.
Douglas Gregor0e119552010-07-31 00:40:00 +00001212 return CreatePaddedMainFileBuffer(NewPreamble.first,
Douglas Gregor0e119552010-07-31 00:40:00 +00001213 PreambleReservedSize,
1214 FrontendOpts.Inputs[0].second);
1215 }
Douglas Gregor4dde7492010-07-23 23:58:40 +00001216 }
Douglas Gregor028d3e42010-08-09 20:45:32 +00001217
1218 // If we aren't allowed to rebuild the precompiled preamble, just
1219 // return now.
1220 if (!AllowRebuild)
1221 return 0;
Douglas Gregorbb6a8812010-10-08 04:03:57 +00001222
Douglas Gregor4dde7492010-07-23 23:58:40 +00001223 // We can't reuse the previously-computed preamble. Build a new one.
1224 Preamble.clear();
Douglas Gregor15ba0b32010-07-30 20:58:08 +00001225 llvm::sys::Path(PreambleFile).eraseFromDisk();
Douglas Gregorbb420ab2010-08-04 05:53:38 +00001226 PreambleRebuildCounter = 1;
Douglas Gregor028d3e42010-08-09 20:45:32 +00001227 } else if (!AllowRebuild) {
1228 // We aren't allowed to rebuild the precompiled preamble; just
1229 // return now.
1230 return 0;
1231 }
Douglas Gregorbb420ab2010-08-04 05:53:38 +00001232
1233 // If the preamble rebuild counter > 1, it's because we previously
1234 // failed to build a preamble and we're not yet ready to try
1235 // again. Decrement the counter and return a failure.
1236 if (PreambleRebuildCounter > 1) {
1237 --PreambleRebuildCounter;
1238 return 0;
1239 }
1240
Douglas Gregore10f0e52010-09-11 17:56:52 +00001241 // Create a temporary file for the precompiled preamble. In rare
1242 // circumstances, this can fail.
1243 std::string PreamblePCHPath = GetPreamblePCHPath();
1244 if (PreamblePCHPath.empty()) {
1245 // Try again next time.
1246 PreambleRebuildCounter = 1;
1247 return 0;
1248 }
1249
Douglas Gregor4dde7492010-07-23 23:58:40 +00001250 // We did not previously compute a preamble, or it can't be reused anyway.
Douglas Gregor16896c42010-10-28 15:44:59 +00001251 SimpleTimer PreambleTimer(WantTiming);
Benjamin Kramerf2e5a912010-11-09 20:00:56 +00001252 PreambleTimer.setOutput("Precompiling preamble");
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001253
1254 // Create a new buffer that stores the preamble. The buffer also contains
1255 // extra space for the original contents of the file (which will be present
1256 // when we actually parse the file) along with more room in case the file
Douglas Gregor4dde7492010-07-23 23:58:40 +00001257 // grows.
1258 PreambleReservedSize = NewPreamble.first->getBufferSize();
1259 if (PreambleReservedSize < 4096)
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001260 PreambleReservedSize = 8191;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001261 else
Douglas Gregor4dde7492010-07-23 23:58:40 +00001262 PreambleReservedSize *= 2;
1263
Douglas Gregord9a30af2010-08-02 20:51:39 +00001264 // Save the preamble text for later; we'll need to compare against it for
1265 // subsequent reparses.
1266 Preamble.assign(NewPreamble.first->getBufferStart(),
1267 NewPreamble.first->getBufferStart()
1268 + NewPreamble.second.first);
1269 PreambleEndsAtStartOfLine = NewPreamble.second.second;
1270
Douglas Gregora0734c52010-08-19 01:33:06 +00001271 delete PreambleBuffer;
1272 PreambleBuffer
Douglas Gregor4dde7492010-07-23 23:58:40 +00001273 = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize,
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001274 FrontendOpts.Inputs[0].second);
1275 memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()),
Douglas Gregor4dde7492010-07-23 23:58:40 +00001276 NewPreamble.first->getBufferStart(), Preamble.size());
1277 memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(),
Douglas Gregor3f4bea02010-07-26 21:36:20 +00001278 ' ', PreambleReservedSize - Preamble.size() - 1);
1279 const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n';
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001280
1281 // Remap the main source file to the preamble buffer.
Douglas Gregor4dde7492010-07-23 23:58:40 +00001282 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second);
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001283 PreprocessorOpts.addRemappedFile(MainFilePath.str(), PreambleBuffer);
1284
1285 // Tell the compiler invocation to generate a temporary precompiled header.
1286 FrontendOpts.ProgramAction = frontend::GeneratePCH;
Douglas Gregor9e136b52010-10-01 01:05:22 +00001287 FrontendOpts.ChainedPCH = true;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001288 // FIXME: Generate the precompiled header into memory?
Douglas Gregore10f0e52010-09-11 17:56:52 +00001289 FrontendOpts.OutputFile = PreamblePCHPath;
Douglas Gregorbb6a8812010-10-08 04:03:57 +00001290 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
1291 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001292
1293 // Create the compiler instance to use for building the precompiled preamble.
1294 CompilerInstance Clang;
1295 Clang.setInvocation(&PreambleInvocation);
1296 OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
1297
Douglas Gregor8e984da2010-08-04 16:47:14 +00001298 // Set up diagnostics, capturing all of the diagnostics produced.
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001299 Clang.setDiagnostics(&getDiagnostics());
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001300
1301 // Create the target instance.
Douglas Gregorffd6dc42011-01-27 18:02:58 +00001302 Clang.getTargetOpts().Features = TargetFeatures;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001303 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
1304 Clang.getTargetOpts()));
1305 if (!Clang.hasTarget()) {
Douglas Gregor4dde7492010-07-23 23:58:40 +00001306 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1307 Preamble.clear();
Douglas Gregorbb420ab2010-08-04 05:53:38 +00001308 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
Douglas Gregora0734c52010-08-19 01:33:06 +00001309 PreprocessorOpts.eraseRemappedFile(
1310 PreprocessorOpts.remapped_file_buffer_end() - 1);
Douglas Gregor6481ef12010-07-24 00:38:13 +00001311 return 0;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001312 }
1313
1314 // Inform the target of the language options.
1315 //
1316 // FIXME: We shouldn't need to do this, the target should be immutable once
1317 // created. This complexity should be lifted elsewhere.
1318 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts());
1319
1320 assert(Clang.getFrontendOpts().Inputs.size() == 1 &&
1321 "Invocation must have exactly one source file!");
1322 assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST &&
1323 "FIXME: AST inputs not yet supported here!");
1324 assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
1325 "IR inputs not support here!");
1326
1327 // Clear out old caches and data.
Douglas Gregorbb6a8812010-10-08 04:03:57 +00001328 getDiagnostics().Reset();
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001329 ProcessWarningOptions(getDiagnostics(), Clang.getDiagnosticOpts());
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001330 StoredDiagnostics.erase(
1331 StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver,
1332 StoredDiagnostics.end());
Douglas Gregore9db88f2010-08-03 19:06:41 +00001333 TopLevelDecls.clear();
1334 TopLevelDeclsInPreamble.clear();
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001335 PreprocessedEntities.clear();
1336 PreprocessedEntitiesInPreamble.clear();
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001337
1338 // Create a file manager object to provide access to and cache the filesystem.
Chris Lattner3f5a9ef2010-11-23 07:51:02 +00001339 Clang.setFileManager(new FileManager(Clang.getFileSystemOpts()));
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001340
1341 // Create the source manager.
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00001342 Clang.setSourceManager(new SourceManager(getDiagnostics(),
Chris Lattner5159f612010-11-23 08:35:12 +00001343 Clang.getFileManager()));
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001344
Douglas Gregor48c8cd32010-08-03 08:14:03 +00001345 llvm::OwningPtr<PrecompilePreambleAction> Act;
1346 Act.reset(new PrecompilePreambleAction(*this));
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001347 if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
1348 Clang.getFrontendOpts().Inputs[0].first)) {
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001349 Clang.takeInvocation();
Douglas Gregor4dde7492010-07-23 23:58:40 +00001350 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1351 Preamble.clear();
Douglas Gregorbb420ab2010-08-04 05:53:38 +00001352 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
Douglas Gregora0734c52010-08-19 01:33:06 +00001353 PreprocessorOpts.eraseRemappedFile(
1354 PreprocessorOpts.remapped_file_buffer_end() - 1);
Douglas Gregor6481ef12010-07-24 00:38:13 +00001355 return 0;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001356 }
1357
1358 Act->Execute();
1359 Act->EndSourceFile();
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001360 Clang.takeInvocation();
1361
Douglas Gregore9db88f2010-08-03 19:06:41 +00001362 if (Diagnostics->hasErrorOccurred()) {
Douglas Gregor4dde7492010-07-23 23:58:40 +00001363 // There were errors parsing the preamble, so no precompiled header was
1364 // generated. Forget that we even tried.
Douglas Gregora6f74e22010-09-27 16:43:25 +00001365 // FIXME: Should we leave a note for ourselves to try again?
Douglas Gregor4dde7492010-07-23 23:58:40 +00001366 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1367 Preamble.clear();
Douglas Gregore9db88f2010-08-03 19:06:41 +00001368 TopLevelDeclsInPreamble.clear();
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001369 PreprocessedEntities.clear();
1370 PreprocessedEntitiesInPreamble.clear();
Douglas Gregorbb420ab2010-08-04 05:53:38 +00001371 PreambleRebuildCounter = DefaultPreambleRebuildInterval;
Douglas Gregora0734c52010-08-19 01:33:06 +00001372 PreprocessorOpts.eraseRemappedFile(
1373 PreprocessorOpts.remapped_file_buffer_end() - 1);
Douglas Gregor6481ef12010-07-24 00:38:13 +00001374 return 0;
Douglas Gregor4dde7492010-07-23 23:58:40 +00001375 }
1376
1377 // Keep track of the preamble we precompiled.
1378 PreambleFile = FrontendOpts.OutputFile;
Douglas Gregord9a30af2010-08-02 20:51:39 +00001379 NumStoredDiagnosticsInPreamble = StoredDiagnostics.size();
1380 NumWarningsInPreamble = getDiagnostics().getNumWarnings();
Douglas Gregor0e119552010-07-31 00:40:00 +00001381
1382 // Keep track of all of the files that the source manager knows about,
1383 // so we can verify whether they have changed or not.
1384 FilesInPreamble.clear();
1385 SourceManager &SourceMgr = Clang.getSourceManager();
1386 const llvm::MemoryBuffer *MainFileBuffer
1387 = SourceMgr.getBuffer(SourceMgr.getMainFileID());
1388 for (SourceManager::fileinfo_iterator F = SourceMgr.fileinfo_begin(),
1389 FEnd = SourceMgr.fileinfo_end();
1390 F != FEnd;
1391 ++F) {
1392 const FileEntry *File = F->second->Entry;
1393 if (!File || F->second->getRawBuffer() == MainFileBuffer)
1394 continue;
1395
1396 FilesInPreamble[File->getName()]
1397 = std::make_pair(F->second->getSize(), File->getModificationTime());
1398 }
1399
Douglas Gregorbb420ab2010-08-04 05:53:38 +00001400 PreambleRebuildCounter = 1;
Douglas Gregora0734c52010-08-19 01:33:06 +00001401 PreprocessorOpts.eraseRemappedFile(
1402 PreprocessorOpts.remapped_file_buffer_end() - 1);
Douglas Gregordf7a79a2011-02-16 18:16:54 +00001403
1404 // If the hash of top-level entities differs from the hash of the top-level
1405 // entities the last time we rebuilt the preamble, clear out the completion
1406 // cache.
1407 if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1408 CompletionCacheTopLevelHashValue = 0;
1409 PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1410 }
1411
Douglas Gregor6481ef12010-07-24 00:38:13 +00001412 return CreatePaddedMainFileBuffer(NewPreamble.first,
Douglas Gregor6481ef12010-07-24 00:38:13 +00001413 PreambleReservedSize,
1414 FrontendOpts.Inputs[0].second);
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001415}
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001416
Douglas Gregore9db88f2010-08-03 19:06:41 +00001417void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1418 std::vector<Decl *> Resolved;
1419 Resolved.reserve(TopLevelDeclsInPreamble.size());
1420 ExternalASTSource &Source = *getASTContext().getExternalSource();
1421 for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) {
1422 // Resolve the declaration ID to an actual declaration, possibly
1423 // deserializing the declaration in the process.
1424 Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]);
1425 if (D)
1426 Resolved.push_back(D);
1427 }
1428 TopLevelDeclsInPreamble.clear();
1429 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1430}
1431
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001432void ASTUnit::RealizePreprocessedEntitiesFromPreamble() {
1433 if (!PP)
1434 return;
1435
1436 PreprocessingRecord *PPRec = PP->getPreprocessingRecord();
1437 if (!PPRec)
1438 return;
1439
1440 ExternalPreprocessingRecordSource *External = PPRec->getExternalSource();
1441 if (!External)
1442 return;
1443
1444 for (unsigned I = 0, N = PreprocessedEntitiesInPreamble.size(); I != N; ++I) {
1445 if (PreprocessedEntity *PE
Douglas Gregor46c50012011-02-11 19:46:30 +00001446 = External->ReadPreprocessedEntityAtOffset(
1447 PreprocessedEntitiesInPreamble[I]))
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001448 PreprocessedEntities.push_back(PE);
1449 }
1450
1451 if (PreprocessedEntities.empty())
1452 return;
1453
1454 PreprocessedEntities.insert(PreprocessedEntities.end(),
1455 PPRec->begin(true), PPRec->end(true));
1456}
1457
1458ASTUnit::pp_entity_iterator ASTUnit::pp_entity_begin() {
1459 if (!PreprocessedEntitiesInPreamble.empty() &&
1460 PreprocessedEntities.empty())
1461 RealizePreprocessedEntitiesFromPreamble();
1462
1463 if (PreprocessedEntities.empty())
1464 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
1465 return PPRec->begin(true);
1466
1467 return PreprocessedEntities.begin();
1468}
1469
1470ASTUnit::pp_entity_iterator ASTUnit::pp_entity_end() {
1471 if (!PreprocessedEntitiesInPreamble.empty() &&
1472 PreprocessedEntities.empty())
1473 RealizePreprocessedEntitiesFromPreamble();
1474
1475 if (PreprocessedEntities.empty())
1476 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
1477 return PPRec->end(true);
1478
1479 return PreprocessedEntities.end();
1480}
1481
Douglas Gregore9db88f2010-08-03 19:06:41 +00001482unsigned ASTUnit::getMaxPCHLevel() const {
1483 if (!getOnlyLocalDecls())
1484 return Decl::MaxPCHLevel;
1485
Sebastian Redl009e7f22010-10-05 16:15:19 +00001486 return 0;
Douglas Gregore9db88f2010-08-03 19:06:41 +00001487}
1488
Douglas Gregor16896c42010-10-28 15:44:59 +00001489llvm::StringRef ASTUnit::getMainFileName() const {
1490 return Invocation->getFrontendOpts().Inputs[0].second;
1491}
1492
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001493bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) {
1494 if (!Invocation)
1495 return true;
1496
1497 // We'll manage file buffers ourselves.
1498 Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1499 Invocation->getFrontendOpts().DisableFree = false;
Douglas Gregor345c1bc2011-01-19 01:02:47 +00001500 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001501
Douglas Gregorffd6dc42011-01-27 18:02:58 +00001502 // Save the target features.
1503 TargetFeatures = Invocation->getTargetOpts().Features;
1504
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001505 llvm::MemoryBuffer *OverrideMainBuffer = 0;
Douglas Gregorf5a18542010-10-27 17:24:53 +00001506 if (PrecompilePreamble) {
Douglas Gregorc6592922010-11-15 23:00:34 +00001507 PreambleRebuildCounter = 2;
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001508 OverrideMainBuffer
1509 = getMainBufferWithPrecompiledPreamble(*Invocation);
1510 }
1511
Douglas Gregor16896c42010-10-28 15:44:59 +00001512 SimpleTimer ParsingTimer(WantTiming);
Benjamin Kramerf2e5a912010-11-09 20:00:56 +00001513 ParsingTimer.setOutput("Parsing " + getMainFileName());
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001514
Douglas Gregor16896c42010-10-28 15:44:59 +00001515 return Parse(OverrideMainBuffer);
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001516}
1517
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001518ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
1519 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
1520 bool OnlyLocalDecls,
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001521 bool CaptureDiagnostics,
Douglas Gregor028d3e42010-08-09 20:45:32 +00001522 bool PrecompilePreamble,
Douglas Gregorb14904c2010-08-13 22:48:40 +00001523 bool CompleteTranslationUnit,
Douglas Gregor44c6ee72010-11-11 00:39:14 +00001524 bool CacheCodeCompletionResults) {
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001525 // Create the AST unit.
1526 llvm::OwningPtr<ASTUnit> AST;
1527 AST.reset(new ASTUnit(false));
Douglas Gregor345c1bc2011-01-19 01:02:47 +00001528 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001529 AST->Diagnostics = Diags;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001530 AST->OnlyLocalDecls = OnlyLocalDecls;
Douglas Gregor44c6ee72010-11-11 00:39:14 +00001531 AST->CaptureDiagnostics = CaptureDiagnostics;
Douglas Gregor028d3e42010-08-09 20:45:32 +00001532 AST->CompleteTranslationUnit = CompleteTranslationUnit;
Douglas Gregorb14904c2010-08-13 22:48:40 +00001533 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001534 AST->Invocation.reset(CI);
1535
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001536 return AST->LoadFromCompilerInvocation(PrecompilePreamble)? 0 : AST.take();
Daniel Dunbar764c0822009-12-01 09:51:01 +00001537}
Daniel Dunbar55a17b62009-12-02 03:23:45 +00001538
1539ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
1540 const char **ArgEnd,
Douglas Gregor7f95d262010-04-05 23:52:57 +00001541 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
Daniel Dunbar8d4a2022009-12-13 03:46:13 +00001542 llvm::StringRef ResourceFilesPath,
Daniel Dunbar55a17b62009-12-02 03:23:45 +00001543 bool OnlyLocalDecls,
Douglas Gregor44c6ee72010-11-11 00:39:14 +00001544 bool CaptureDiagnostics,
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001545 RemappedFile *RemappedFiles,
Douglas Gregor33cdd812010-02-18 18:08:43 +00001546 unsigned NumRemappedFiles,
Douglas Gregor028d3e42010-08-09 20:45:32 +00001547 bool PrecompilePreamble,
Douglas Gregorb14904c2010-08-13 22:48:40 +00001548 bool CompleteTranslationUnit,
Douglas Gregorf5a18542010-10-27 17:24:53 +00001549 bool CacheCodeCompletionResults,
1550 bool CXXPrecompilePreamble,
1551 bool CXXChainedPCH) {
Douglas Gregor7f95d262010-04-05 23:52:57 +00001552 if (!Diags.getPtr()) {
Douglas Gregord03e8232010-04-05 21:10:19 +00001553 // No diagnostics engine was provided, so create our own diagnostics object
1554 // with the default options.
1555 DiagnosticOptions DiagOpts;
Douglas Gregor345c1bc2011-01-19 01:02:47 +00001556 Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd - ArgBegin,
1557 ArgBegin);
Douglas Gregord03e8232010-04-05 21:10:19 +00001558 }
1559
Daniel Dunbar55a17b62009-12-02 03:23:45 +00001560 llvm::SmallVector<const char *, 16> Args;
1561 Args.push_back("<clang>"); // FIXME: Remove dummy argument.
1562 Args.insert(Args.end(), ArgBegin, ArgEnd);
1563
1564 // FIXME: Find a cleaner way to force the driver into restricted modes. We
1565 // also want to force it to use clang.
1566 Args.push_back("-fsyntax-only");
1567
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001568 llvm::SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1569
1570 llvm::OwningPtr<CompilerInvocation> CI;
Douglas Gregor44c6ee72010-11-11 00:39:14 +00001571
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001572 {
Douglas Gregor44c6ee72010-11-11 00:39:14 +00001573 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001574 StoredDiagnostics);
Daniel Dunbarfcf2d422010-01-25 00:44:02 +00001575
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001576 // FIXME: We shouldn't have to pass in the path info.
1577 driver::Driver TheDriver("clang", llvm::sys::getHostTriple(),
1578 "a.out", false, false, *Diags);
Daniel Dunbarfcf2d422010-01-25 00:44:02 +00001579
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001580 // Don't check that inputs exist, they have been remapped.
1581 TheDriver.setCheckInputsExist(false);
Daniel Dunbar55a17b62009-12-02 03:23:45 +00001582
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001583 llvm::OwningPtr<driver::Compilation> C(
1584 TheDriver.BuildCompilation(Args.size(), Args.data()));
1585
1586 // We expect to get back exactly one command job, if we didn't something
1587 // failed.
1588 const driver::JobList &Jobs = C->getJobs();
1589 if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) {
1590 llvm::SmallString<256> Msg;
1591 llvm::raw_svector_ostream OS(Msg);
1592 C->PrintJob(OS, C->getJobs(), "; ", true);
1593 Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
1594 return 0;
1595 }
1596
1597 const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
1598 if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") {
1599 Diags->Report(diag::err_fe_expected_clang_command);
1600 return 0;
1601 }
1602
1603 const driver::ArgStringList &CCArgs = Cmd->getArguments();
1604 CI.reset(new CompilerInvocation);
1605 CompilerInvocation::CreateFromArgs(*CI,
Douglas Gregor44c6ee72010-11-11 00:39:14 +00001606 const_cast<const char **>(CCArgs.data()),
1607 const_cast<const char **>(CCArgs.data()) +
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001608 CCArgs.size(),
1609 *Diags);
Daniel Dunbar55a17b62009-12-02 03:23:45 +00001610 }
Douglas Gregor44c6ee72010-11-11 00:39:14 +00001611
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001612 // Override any files that need remapping
1613 for (unsigned I = 0; I != NumRemappedFiles; ++I)
Daniel Dunbar6b03ece2010-01-30 21:47:16 +00001614 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
Daniel Dunbar19511922010-02-16 01:55:04 +00001615 RemappedFiles[I].second);
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001616
Daniel Dunbara5a166d2009-12-15 00:06:45 +00001617 // Override the resources path.
Daniel Dunbar6b03ece2010-01-30 21:47:16 +00001618 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
Daniel Dunbar55a17b62009-12-02 03:23:45 +00001619
Douglas Gregorf5a18542010-10-27 17:24:53 +00001620 // Check whether we should precompile the preamble and/or use chained PCH.
1621 // FIXME: This is a temporary hack while we debug C++ chained PCH.
1622 if (CI->getLangOpts().CPlusPlus) {
1623 PrecompilePreamble = PrecompilePreamble && CXXPrecompilePreamble;
1624
1625 if (PrecompilePreamble && !CXXChainedPCH &&
1626 !CI->getPreprocessorOpts().ImplicitPCHInclude.empty())
1627 PrecompilePreamble = false;
1628 }
1629
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001630 // Create the AST unit.
1631 llvm::OwningPtr<ASTUnit> AST;
1632 AST.reset(new ASTUnit(false));
Douglas Gregor345c1bc2011-01-19 01:02:47 +00001633 ConfigureDiags(Diags, ArgBegin, ArgEnd, *AST, CaptureDiagnostics);
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001634 AST->Diagnostics = Diags;
Chris Lattner5159f612010-11-23 08:35:12 +00001635
1636 AST->FileMgr.reset(new FileManager(FileSystemOptions()));
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001637 AST->OnlyLocalDecls = OnlyLocalDecls;
Douglas Gregor44c6ee72010-11-11 00:39:14 +00001638 AST->CaptureDiagnostics = CaptureDiagnostics;
Douglas Gregor7bb8af62010-10-12 00:50:20 +00001639 AST->CompleteTranslationUnit = CompleteTranslationUnit;
1640 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1641 AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1642 AST->NumStoredDiagnosticsInPreamble = StoredDiagnostics.size();
1643 AST->StoredDiagnostics.swap(StoredDiagnostics);
1644 AST->Invocation.reset(CI.take());
Chris Lattner5159f612010-11-23 08:35:12 +00001645 return AST->LoadFromCompilerInvocation(PrecompilePreamble) ? 0 : AST.take();
Daniel Dunbar55a17b62009-12-02 03:23:45 +00001646}
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001647
1648bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) {
1649 if (!Invocation.get())
1650 return true;
1651
Douglas Gregor16896c42010-10-28 15:44:59 +00001652 SimpleTimer ParsingTimer(WantTiming);
Benjamin Kramerf2e5a912010-11-09 20:00:56 +00001653 ParsingTimer.setOutput("Reparsing " + getMainFileName());
Douglas Gregor16896c42010-10-28 15:44:59 +00001654
Douglas Gregor0e119552010-07-31 00:40:00 +00001655 // Remap files.
Douglas Gregor7b02b582010-08-20 00:02:33 +00001656 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
Douglas Gregor606c4ac2011-02-05 19:42:43 +00001657 PPOpts.DisableStatCache = true;
Douglas Gregor7b02b582010-08-20 00:02:33 +00001658 for (PreprocessorOptions::remapped_file_buffer_iterator
1659 R = PPOpts.remapped_file_buffer_begin(),
1660 REnd = PPOpts.remapped_file_buffer_end();
1661 R != REnd;
1662 ++R) {
1663 delete R->second;
1664 }
Douglas Gregor0e119552010-07-31 00:40:00 +00001665 Invocation->getPreprocessorOpts().clearRemappedFiles();
1666 for (unsigned I = 0; I != NumRemappedFiles; ++I)
1667 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1668 RemappedFiles[I].second);
1669
Douglas Gregorbb420ab2010-08-04 05:53:38 +00001670 // If we have a preamble file lying around, or if we might try to
1671 // build a precompiled preamble, do so now.
Douglas Gregor6481ef12010-07-24 00:38:13 +00001672 llvm::MemoryBuffer *OverrideMainBuffer = 0;
Douglas Gregorbb420ab2010-08-04 05:53:38 +00001673 if (!PreambleFile.empty() || PreambleRebuildCounter > 0)
Douglas Gregorb97b6662010-08-20 00:59:43 +00001674 OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation);
Douglas Gregor4dde7492010-07-23 23:58:40 +00001675
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001676 // Clear out the diagnostics state.
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001677 if (!OverrideMainBuffer) {
Douglas Gregord9a30af2010-08-02 20:51:39 +00001678 getDiagnostics().Reset();
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001679 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1680 }
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001681
Douglas Gregor4dde7492010-07-23 23:58:40 +00001682 // Parse the sources
Douglas Gregordf7a79a2011-02-16 18:16:54 +00001683 bool Result = Parse(OverrideMainBuffer);
1684
1685 // If we're caching global code-completion results, and the top-level
1686 // declarations have changed, clear out the code-completion cache.
1687 if (!Result && ShouldCacheCodeCompletionResults &&
1688 CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
1689 CacheCodeCompletionResults();
1690
Douglas Gregor4dde7492010-07-23 23:58:40 +00001691 return Result;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001692}
Douglas Gregor8e984da2010-08-04 16:47:14 +00001693
Douglas Gregorb14904c2010-08-13 22:48:40 +00001694//----------------------------------------------------------------------------//
1695// Code completion
1696//----------------------------------------------------------------------------//
1697
1698namespace {
1699 /// \brief Code completion consumer that combines the cached code-completion
1700 /// results from an ASTUnit with the code-completion results provided to it,
1701 /// then passes the result on to
1702 class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
1703 unsigned NormalContexts;
1704 ASTUnit &AST;
1705 CodeCompleteConsumer &Next;
1706
1707 public:
1708 AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
Douglas Gregor39982192010-08-15 06:18:01 +00001709 bool IncludeMacros, bool IncludeCodePatterns,
1710 bool IncludeGlobals)
1711 : CodeCompleteConsumer(IncludeMacros, IncludeCodePatterns, IncludeGlobals,
Douglas Gregorb14904c2010-08-13 22:48:40 +00001712 Next.isOutputBinary()), AST(AST), Next(Next)
1713 {
1714 // Compute the set of contexts in which we will look when we don't have
1715 // any information about the specific context.
1716 NormalContexts
1717 = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
1718 | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1))
1719 | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1))
1720 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
1721 | (1 << (CodeCompletionContext::CCC_Statement - 1))
1722 | (1 << (CodeCompletionContext::CCC_Expression - 1))
1723 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
1724 | (1 << (CodeCompletionContext::CCC_MemberAccess - 1))
Douglas Gregor5e35d592010-09-14 23:59:36 +00001725 | (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1))
Douglas Gregor0ac41382010-09-23 23:01:17 +00001726 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
1727 | (1 << (CodeCompletionContext::CCC_Recovery - 1));
Douglas Gregor5e35d592010-09-14 23:59:36 +00001728
Douglas Gregorb14904c2010-08-13 22:48:40 +00001729 if (AST.getASTContext().getLangOptions().CPlusPlus)
1730 NormalContexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1))
1731 | (1 << (CodeCompletionContext::CCC_UnionTag - 1))
1732 | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1));
1733 }
1734
1735 virtual void ProcessCodeCompleteResults(Sema &S,
1736 CodeCompletionContext Context,
John McCall276321a2010-08-25 06:19:51 +00001737 CodeCompletionResult *Results,
Douglas Gregord46cf182010-08-16 20:01:48 +00001738 unsigned NumResults);
Douglas Gregorb14904c2010-08-13 22:48:40 +00001739
1740 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
1741 OverloadCandidate *Candidates,
1742 unsigned NumCandidates) {
1743 Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
1744 }
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001745
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00001746 virtual CodeCompletionAllocator &getAllocator() {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001747 return Next.getAllocator();
1748 }
Douglas Gregorb14904c2010-08-13 22:48:40 +00001749 };
1750}
Douglas Gregord46cf182010-08-16 20:01:48 +00001751
Douglas Gregor6199f2d2010-08-16 21:18:39 +00001752/// \brief Helper function that computes which global names are hidden by the
1753/// local code-completion results.
Ted Kremenek6a153372010-11-07 06:11:36 +00001754static void CalculateHiddenNames(const CodeCompletionContext &Context,
1755 CodeCompletionResult *Results,
1756 unsigned NumResults,
1757 ASTContext &Ctx,
1758 llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
Douglas Gregor6199f2d2010-08-16 21:18:39 +00001759 bool OnlyTagNames = false;
1760 switch (Context.getKind()) {
Douglas Gregor0ac41382010-09-23 23:01:17 +00001761 case CodeCompletionContext::CCC_Recovery:
Douglas Gregor6199f2d2010-08-16 21:18:39 +00001762 case CodeCompletionContext::CCC_TopLevel:
1763 case CodeCompletionContext::CCC_ObjCInterface:
1764 case CodeCompletionContext::CCC_ObjCImplementation:
1765 case CodeCompletionContext::CCC_ObjCIvarList:
1766 case CodeCompletionContext::CCC_ClassStructUnion:
1767 case CodeCompletionContext::CCC_Statement:
1768 case CodeCompletionContext::CCC_Expression:
1769 case CodeCompletionContext::CCC_ObjCMessageReceiver:
1770 case CodeCompletionContext::CCC_MemberAccess:
1771 case CodeCompletionContext::CCC_Namespace:
1772 case CodeCompletionContext::CCC_Type:
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001773 case CodeCompletionContext::CCC_Name:
1774 case CodeCompletionContext::CCC_PotentiallyQualifiedName:
Douglas Gregor5e35d592010-09-14 23:59:36 +00001775 case CodeCompletionContext::CCC_ParenthesizedExpression:
Douglas Gregor6199f2d2010-08-16 21:18:39 +00001776 break;
1777
1778 case CodeCompletionContext::CCC_EnumTag:
1779 case CodeCompletionContext::CCC_UnionTag:
1780 case CodeCompletionContext::CCC_ClassOrStructTag:
1781 OnlyTagNames = true;
1782 break;
1783
1784 case CodeCompletionContext::CCC_ObjCProtocolName:
Douglas Gregor12785102010-08-24 20:21:13 +00001785 case CodeCompletionContext::CCC_MacroName:
1786 case CodeCompletionContext::CCC_MacroNameUse:
Douglas Gregorec00a262010-08-24 22:20:20 +00001787 case CodeCompletionContext::CCC_PreprocessorExpression:
Douglas Gregor0de55ce2010-08-25 18:41:16 +00001788 case CodeCompletionContext::CCC_PreprocessorDirective:
Douglas Gregorea147052010-08-25 18:04:30 +00001789 case CodeCompletionContext::CCC_NaturalLanguage:
Douglas Gregor67c692c2010-08-26 15:07:07 +00001790 case CodeCompletionContext::CCC_SelectorName:
Douglas Gregor28c78432010-08-27 17:35:51 +00001791 case CodeCompletionContext::CCC_TypeQualifiers:
Douglas Gregor0ac41382010-09-23 23:01:17 +00001792 case CodeCompletionContext::CCC_Other:
Douglas Gregor0de55ce2010-08-25 18:41:16 +00001793 // We're looking for nothing, or we're looking for names that cannot
1794 // be hidden.
Douglas Gregor6199f2d2010-08-16 21:18:39 +00001795 return;
1796 }
1797
John McCall276321a2010-08-25 06:19:51 +00001798 typedef CodeCompletionResult Result;
Douglas Gregor6199f2d2010-08-16 21:18:39 +00001799 for (unsigned I = 0; I != NumResults; ++I) {
1800 if (Results[I].Kind != Result::RK_Declaration)
1801 continue;
1802
1803 unsigned IDNS
1804 = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
1805
1806 bool Hiding = false;
1807 if (OnlyTagNames)
1808 Hiding = (IDNS & Decl::IDNS_Tag);
1809 else {
1810 unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
Douglas Gregor59cab552010-08-16 23:05:20 +00001811 Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
1812 Decl::IDNS_NonMemberOperator);
Douglas Gregor6199f2d2010-08-16 21:18:39 +00001813 if (Ctx.getLangOptions().CPlusPlus)
1814 HiddenIDNS |= Decl::IDNS_Tag;
1815 Hiding = (IDNS & HiddenIDNS);
1816 }
1817
1818 if (!Hiding)
1819 continue;
1820
1821 DeclarationName Name = Results[I].Declaration->getDeclName();
1822 if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
1823 HiddenNames.insert(Identifier->getName());
1824 else
1825 HiddenNames.insert(Name.getAsString());
1826 }
1827}
1828
1829
Douglas Gregord46cf182010-08-16 20:01:48 +00001830void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
1831 CodeCompletionContext Context,
John McCall276321a2010-08-25 06:19:51 +00001832 CodeCompletionResult *Results,
Douglas Gregord46cf182010-08-16 20:01:48 +00001833 unsigned NumResults) {
1834 // Merge the results we were given with the results we cached.
1835 bool AddedResult = false;
Douglas Gregor6199f2d2010-08-16 21:18:39 +00001836 unsigned InContexts
Douglas Gregor0ac41382010-09-23 23:01:17 +00001837 = (Context.getKind() == CodeCompletionContext::CCC_Recovery? NormalContexts
Douglas Gregor6199f2d2010-08-16 21:18:39 +00001838 : (1 << (Context.getKind() - 1)));
1839
1840 // Contains the set of names that are hidden by "local" completion results.
Ted Kremenek6a153372010-11-07 06:11:36 +00001841 llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
John McCall276321a2010-08-25 06:19:51 +00001842 typedef CodeCompletionResult Result;
Douglas Gregord46cf182010-08-16 20:01:48 +00001843 llvm::SmallVector<Result, 8> AllResults;
1844 for (ASTUnit::cached_completion_iterator
Douglas Gregordf239672010-08-16 21:23:13 +00001845 C = AST.cached_completion_begin(),
1846 CEnd = AST.cached_completion_end();
Douglas Gregord46cf182010-08-16 20:01:48 +00001847 C != CEnd; ++C) {
1848 // If the context we are in matches any of the contexts we are
1849 // interested in, we'll add this result.
1850 if ((C->ShowInContexts & InContexts) == 0)
1851 continue;
1852
1853 // If we haven't added any results previously, do so now.
1854 if (!AddedResult) {
Douglas Gregor6199f2d2010-08-16 21:18:39 +00001855 CalculateHiddenNames(Context, Results, NumResults, S.Context,
1856 HiddenNames);
Douglas Gregord46cf182010-08-16 20:01:48 +00001857 AllResults.insert(AllResults.end(), Results, Results + NumResults);
1858 AddedResult = true;
1859 }
1860
Douglas Gregor6199f2d2010-08-16 21:18:39 +00001861 // Determine whether this global completion result is hidden by a local
1862 // completion result. If so, skip it.
1863 if (C->Kind != CXCursor_MacroDefinition &&
1864 HiddenNames.count(C->Completion->getTypedText()))
1865 continue;
1866
Douglas Gregord46cf182010-08-16 20:01:48 +00001867 // Adjust priority based on similar type classes.
1868 unsigned Priority = C->Priority;
Douglas Gregor8850aa32010-08-25 18:03:13 +00001869 CXCursorKind CursorKind = C->Kind;
Douglas Gregor12785102010-08-24 20:21:13 +00001870 CodeCompletionString *Completion = C->Completion;
Douglas Gregord46cf182010-08-16 20:01:48 +00001871 if (!Context.getPreferredType().isNull()) {
1872 if (C->Kind == CXCursor_MacroDefinition) {
1873 Priority = getMacroUsagePriority(C->Completion->getTypedText(),
Douglas Gregor9dcf58a2010-09-20 21:11:48 +00001874 S.getLangOptions(),
Douglas Gregor12785102010-08-24 20:21:13 +00001875 Context.getPreferredType()->isAnyPointerType());
Douglas Gregord46cf182010-08-16 20:01:48 +00001876 } else if (C->Type) {
1877 CanQualType Expected
Douglas Gregordf239672010-08-16 21:23:13 +00001878 = S.Context.getCanonicalType(
Douglas Gregord46cf182010-08-16 20:01:48 +00001879 Context.getPreferredType().getUnqualifiedType());
1880 SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
1881 if (ExpectedSTC == C->TypeClass) {
1882 // We know this type is similar; check for an exact match.
1883 llvm::StringMap<unsigned> &CachedCompletionTypes
Douglas Gregordf239672010-08-16 21:23:13 +00001884 = AST.getCachedCompletionTypes();
Douglas Gregord46cf182010-08-16 20:01:48 +00001885 llvm::StringMap<unsigned>::iterator Pos
Douglas Gregordf239672010-08-16 21:23:13 +00001886 = CachedCompletionTypes.find(QualType(Expected).getAsString());
Douglas Gregord46cf182010-08-16 20:01:48 +00001887 if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
1888 Priority /= CCF_ExactTypeMatch;
1889 else
1890 Priority /= CCF_SimilarTypeMatch;
1891 }
1892 }
1893 }
1894
Douglas Gregor12785102010-08-24 20:21:13 +00001895 // Adjust the completion string, if required.
1896 if (C->Kind == CXCursor_MacroDefinition &&
1897 Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
1898 // Create a new code-completion string that just contains the
1899 // macro name, without its arguments.
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001900 CodeCompletionBuilder Builder(getAllocator(), CCP_CodePattern,
1901 C->Availability);
1902 Builder.AddTypedTextChunk(C->Completion->getTypedText());
Douglas Gregor8850aa32010-08-25 18:03:13 +00001903 CursorKind = CXCursor_NotImplemented;
1904 Priority = CCP_CodePattern;
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001905 Completion = Builder.TakeString();
Douglas Gregor12785102010-08-24 20:21:13 +00001906 }
1907
Douglas Gregor8850aa32010-08-25 18:03:13 +00001908 AllResults.push_back(Result(Completion, Priority, CursorKind,
Douglas Gregorf757a122010-08-23 23:00:57 +00001909 C->Availability));
Douglas Gregord46cf182010-08-16 20:01:48 +00001910 }
1911
1912 // If we did not add any cached completion results, just forward the
1913 // results we were given to the next consumer.
1914 if (!AddedResult) {
1915 Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
1916 return;
1917 }
Douglas Gregor49f67ce2010-08-26 13:48:20 +00001918
Douglas Gregord46cf182010-08-16 20:01:48 +00001919 Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
1920 AllResults.size());
1921}
1922
1923
1924
Douglas Gregor8e984da2010-08-04 16:47:14 +00001925void ASTUnit::CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column,
1926 RemappedFile *RemappedFiles,
1927 unsigned NumRemappedFiles,
Douglas Gregorb68bc592010-08-05 09:09:23 +00001928 bool IncludeMacros,
1929 bool IncludeCodePatterns,
Douglas Gregor8e984da2010-08-04 16:47:14 +00001930 CodeCompleteConsumer &Consumer,
1931 Diagnostic &Diag, LangOptions &LangOpts,
1932 SourceManager &SourceMgr, FileManager &FileMgr,
Douglas Gregorb97b6662010-08-20 00:59:43 +00001933 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
1934 llvm::SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
Douglas Gregor8e984da2010-08-04 16:47:14 +00001935 if (!Invocation.get())
1936 return;
1937
Douglas Gregor16896c42010-10-28 15:44:59 +00001938 SimpleTimer CompletionTimer(WantTiming);
Benjamin Kramerf2e5a912010-11-09 20:00:56 +00001939 CompletionTimer.setOutput("Code completion @ " + File + ":" +
1940 llvm::Twine(Line) + ":" + llvm::Twine(Column));
Douglas Gregor028d3e42010-08-09 20:45:32 +00001941
Douglas Gregor8e984da2010-08-04 16:47:14 +00001942 CompilerInvocation CCInvocation(*Invocation);
1943 FrontendOptions &FrontendOpts = CCInvocation.getFrontendOpts();
1944 PreprocessorOptions &PreprocessorOpts = CCInvocation.getPreprocessorOpts();
Douglas Gregorb68bc592010-08-05 09:09:23 +00001945
Douglas Gregorb14904c2010-08-13 22:48:40 +00001946 FrontendOpts.ShowMacrosInCodeCompletion
1947 = IncludeMacros && CachedCompletionResults.empty();
Douglas Gregorb68bc592010-08-05 09:09:23 +00001948 FrontendOpts.ShowCodePatternsInCodeCompletion = IncludeCodePatterns;
Douglas Gregor39982192010-08-15 06:18:01 +00001949 FrontendOpts.ShowGlobalSymbolsInCodeCompletion
1950 = CachedCompletionResults.empty();
Douglas Gregor8e984da2010-08-04 16:47:14 +00001951 FrontendOpts.CodeCompletionAt.FileName = File;
1952 FrontendOpts.CodeCompletionAt.Line = Line;
1953 FrontendOpts.CodeCompletionAt.Column = Column;
1954
1955 // Set the language options appropriately.
1956 LangOpts = CCInvocation.getLangOpts();
1957
1958 CompilerInstance Clang;
1959 Clang.setInvocation(&CCInvocation);
1960 OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
1961
1962 // Set up diagnostics, capturing any diagnostics produced.
1963 Clang.setDiagnostics(&Diag);
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001964 ProcessWarningOptions(Diag, CCInvocation.getDiagnosticOpts());
Douglas Gregor8e984da2010-08-04 16:47:14 +00001965 CaptureDroppedDiagnostics Capture(true,
Douglas Gregor44c6ee72010-11-11 00:39:14 +00001966 Clang.getDiagnostics(),
Douglas Gregor8e984da2010-08-04 16:47:14 +00001967 StoredDiagnostics);
Douglas Gregor8e984da2010-08-04 16:47:14 +00001968
1969 // Create the target instance.
Douglas Gregorffd6dc42011-01-27 18:02:58 +00001970 Clang.getTargetOpts().Features = TargetFeatures;
Douglas Gregor8e984da2010-08-04 16:47:14 +00001971 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
1972 Clang.getTargetOpts()));
1973 if (!Clang.hasTarget()) {
Douglas Gregor8e984da2010-08-04 16:47:14 +00001974 Clang.takeInvocation();
Douglas Gregor2dd19f12010-08-18 22:29:43 +00001975 return;
Douglas Gregor8e984da2010-08-04 16:47:14 +00001976 }
1977
1978 // Inform the target of the language options.
1979 //
1980 // FIXME: We shouldn't need to do this, the target should be immutable once
1981 // created. This complexity should be lifted elsewhere.
1982 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts());
1983
1984 assert(Clang.getFrontendOpts().Inputs.size() == 1 &&
1985 "Invocation must have exactly one source file!");
1986 assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST &&
1987 "FIXME: AST inputs not yet supported here!");
1988 assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
1989 "IR inputs not support here!");
1990
1991
1992 // Use the source and file managers that we were given.
1993 Clang.setFileManager(&FileMgr);
1994 Clang.setSourceManager(&SourceMgr);
1995
1996 // Remap files.
1997 PreprocessorOpts.clearRemappedFiles();
Douglas Gregord8a5dba2010-08-04 17:07:00 +00001998 PreprocessorOpts.RetainRemappedFileBuffers = true;
Douglas Gregorb97b6662010-08-20 00:59:43 +00001999 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
Douglas Gregor8e984da2010-08-04 16:47:14 +00002000 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first,
2001 RemappedFiles[I].second);
Douglas Gregorb97b6662010-08-20 00:59:43 +00002002 OwnedBuffers.push_back(RemappedFiles[I].second);
2003 }
Douglas Gregor8e984da2010-08-04 16:47:14 +00002004
Douglas Gregorb14904c2010-08-13 22:48:40 +00002005 // Use the code completion consumer we were given, but adding any cached
2006 // code-completion results.
Douglas Gregore9186e62010-11-29 16:13:56 +00002007 AugmentedCodeCompleteConsumer *AugmentedConsumer
2008 = new AugmentedCodeCompleteConsumer(*this, Consumer,
2009 FrontendOpts.ShowMacrosInCodeCompletion,
2010 FrontendOpts.ShowCodePatternsInCodeCompletion,
2011 FrontendOpts.ShowGlobalSymbolsInCodeCompletion);
2012 Clang.setCodeCompletionConsumer(AugmentedConsumer);
Douglas Gregor8e984da2010-08-04 16:47:14 +00002013
Douglas Gregor028d3e42010-08-09 20:45:32 +00002014 // If we have a precompiled preamble, try to use it. We only allow
2015 // the use of the precompiled preamble if we're if the completion
2016 // point is within the main file, after the end of the precompiled
2017 // preamble.
2018 llvm::MemoryBuffer *OverrideMainBuffer = 0;
2019 if (!PreambleFile.empty()) {
2020 using llvm::sys::FileStatus;
2021 llvm::sys::PathWithStatus CompleteFilePath(File);
2022 llvm::sys::PathWithStatus MainPath(OriginalSourceFile);
2023 if (const FileStatus *CompleteFileStatus = CompleteFilePath.getFileStatus())
2024 if (const FileStatus *MainStatus = MainPath.getFileStatus())
2025 if (CompleteFileStatus->getUniqueID() == MainStatus->getUniqueID())
Douglas Gregorb97b6662010-08-20 00:59:43 +00002026 OverrideMainBuffer
Douglas Gregor8e817b62010-08-25 18:04:15 +00002027 = getMainBufferWithPrecompiledPreamble(CCInvocation, false,
2028 Line - 1);
Douglas Gregor028d3e42010-08-09 20:45:32 +00002029 }
2030
2031 // If the main file has been overridden due to the use of a preamble,
2032 // make that override happen and introduce the preamble.
Douglas Gregor606c4ac2011-02-05 19:42:43 +00002033 PreprocessorOpts.DisableStatCache = true;
Douglas Gregor7bb8af62010-10-12 00:50:20 +00002034 StoredDiagnostics.insert(StoredDiagnostics.end(),
2035 this->StoredDiagnostics.begin(),
2036 this->StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver);
Douglas Gregor028d3e42010-08-09 20:45:32 +00002037 if (OverrideMainBuffer) {
2038 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
2039 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
2040 PreprocessorOpts.PrecompiledPreambleBytes.second
2041 = PreambleEndsAtStartOfLine;
2042 PreprocessorOpts.ImplicitPCHInclude = PreambleFile;
2043 PreprocessorOpts.DisablePCHValidation = true;
2044
2045 // The stored diagnostics have the old source manager. Copy them
2046 // to our output set of stored diagnostics, updating the source
2047 // manager to the one we were given.
Douglas Gregor7bb8af62010-10-12 00:50:20 +00002048 for (unsigned I = NumStoredDiagnosticsFromDriver,
2049 N = this->StoredDiagnostics.size();
2050 I < N; ++I) {
Douglas Gregor028d3e42010-08-09 20:45:32 +00002051 StoredDiagnostics.push_back(this->StoredDiagnostics[I]);
2052 FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), SourceMgr);
2053 StoredDiagnostics[I].setLocation(Loc);
2054 }
Douglas Gregor7bb8af62010-10-12 00:50:20 +00002055
Douglas Gregorb97b6662010-08-20 00:59:43 +00002056 OwnedBuffers.push_back(OverrideMainBuffer);
Douglas Gregor7b02b582010-08-20 00:02:33 +00002057 } else {
2058 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2059 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
Douglas Gregor028d3e42010-08-09 20:45:32 +00002060 }
2061
Douglas Gregor8e984da2010-08-04 16:47:14 +00002062 llvm::OwningPtr<SyntaxOnlyAction> Act;
2063 Act.reset(new SyntaxOnlyAction);
2064 if (Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
2065 Clang.getFrontendOpts().Inputs[0].first)) {
2066 Act->Execute();
2067 Act->EndSourceFile();
2068 }
Douglas Gregor028d3e42010-08-09 20:45:32 +00002069
Douglas Gregor8e984da2010-08-04 16:47:14 +00002070 // Steal back our resources.
2071 Clang.takeFileManager();
2072 Clang.takeSourceManager();
2073 Clang.takeInvocation();
Douglas Gregor8e984da2010-08-04 16:47:14 +00002074}
Douglas Gregore9386682010-08-13 05:36:37 +00002075
2076bool ASTUnit::Save(llvm::StringRef File) {
2077 if (getDiagnostics().hasErrorOccurred())
2078 return true;
2079
2080 // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2081 // unconditionally create a stat cache when we parse the file?
2082 std::string ErrorInfo;
Benjamin Kramer340045b2010-08-15 16:54:31 +00002083 llvm::raw_fd_ostream Out(File.str().c_str(), ErrorInfo,
2084 llvm::raw_fd_ostream::F_Binary);
Douglas Gregore9386682010-08-13 05:36:37 +00002085 if (!ErrorInfo.empty() || Out.has_error())
2086 return true;
2087
2088 std::vector<unsigned char> Buffer;
2089 llvm::BitstreamWriter Stream(Buffer);
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002090 ASTWriter Writer(Stream);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002091 Writer.WriteAST(getSema(), 0, std::string(), 0);
Douglas Gregore9386682010-08-13 05:36:37 +00002092
2093 // Write the generated bitstream to "Out".
Douglas Gregor2dd19f12010-08-18 22:29:43 +00002094 if (!Buffer.empty())
2095 Out.write((char *)&Buffer.front(), Buffer.size());
Douglas Gregore9386682010-08-13 05:36:37 +00002096 Out.close();
2097 return Out.has_error();
2098}