Argyrios Kyrtzidis | 4b562cf | 2009-06-20 08:27:14 +0000 | [diff] [blame] | 1 | //===--- 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 Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/ASTUnit.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclVisitor.h" |
Douglas Gregor | f5586f6 | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 18 | #include "clang/AST/TypeOrdering.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/StmtVisitor.h" |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 20 | #include "clang/Driver/Compilation.h" |
| 21 | #include "clang/Driver/Driver.h" |
| 22 | #include "clang/Driver/Job.h" |
| 23 | #include "clang/Driver/Tool.h" |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/CompilerInstance.h" |
| 25 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/FrontendOptions.h" |
Sebastian Redl | 6ab7cd8 | 2010-08-18 23:57:17 +0000 | [diff] [blame] | 28 | #include "clang/Serialization/ASTReader.h" |
Sebastian Redl | 7faa2ec | 2010-08-18 23:56:37 +0000 | [diff] [blame] | 29 | #include "clang/Serialization/ASTWriter.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 30 | #include "clang/Lex/HeaderSearch.h" |
| 31 | #include "clang/Lex/Preprocessor.h" |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 32 | #include "clang/Basic/TargetOptions.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 33 | #include "clang/Basic/TargetInfo.h" |
| 34 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | 349d38c | 2010-08-16 23:08:34 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/StringSet.h" |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 37 | #include "llvm/System/Host.h" |
Benjamin Kramer | 4a630d3 | 2009-10-18 11:34:14 +0000 | [diff] [blame] | 38 | #include "llvm/System/Path.h" |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 39 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Timer.h" |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 41 | #include <cstdlib> |
Zhongxing Xu | ad23ebe | 2010-07-23 02:15:08 +0000 | [diff] [blame] | 42 | #include <cstdio> |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 43 | #include <sys/stat.h> |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 44 | using namespace clang; |
| 45 | |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 46 | /// \brief After failing to build a precompiled preamble (due to |
| 47 | /// errors in the source that occurs in the preamble), the number of |
| 48 | /// reparses during which we'll skip even trying to precompile the |
| 49 | /// preamble. |
| 50 | const unsigned DefaultPreambleRebuildInterval = 5; |
| 51 | |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 52 | ASTUnit::ASTUnit(bool _MainFileIsAST) |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 53 | : CaptureDiagnostics(false), MainFileIsAST(_MainFileIsAST), |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 54 | CompleteTranslationUnit(true), ConcurrencyCheckValue(CheckUnlocked), |
Douglas Gregor | 671947b | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 55 | PreambleRebuildCounter(0), SavedMainFileBuffer(0), PreambleBuffer(0), |
Douglas Gregor | 727d93e | 2010-08-17 00:40:40 +0000 | [diff] [blame] | 56 | ShouldCacheCodeCompletionResults(false), |
| 57 | NumTopLevelDeclsAtLastCompletionCache(0), |
Douglas Gregor | 8b1540c | 2010-08-19 00:45:44 +0000 | [diff] [blame] | 58 | CacheCodeCompletionCoolDown(0), |
| 59 | UnsafeToFree(false) { |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 60 | } |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 61 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 62 | ASTUnit::~ASTUnit() { |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 63 | ConcurrencyCheckValue = CheckLocked; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 64 | CleanTemporaryFiles(); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 65 | if (!PreambleFile.empty()) |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 66 | llvm::sys::Path(PreambleFile).eraseFromDisk(); |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 67 | |
| 68 | // Free the buffers associated with remapped files. We are required to |
| 69 | // perform this operation here because we explicitly request that the |
| 70 | // compiler instance *not* free these buffers for each invocation of the |
| 71 | // parser. |
| 72 | if (Invocation.get()) { |
| 73 | PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); |
| 74 | for (PreprocessorOptions::remapped_file_buffer_iterator |
| 75 | FB = PPOpts.remapped_file_buffer_begin(), |
| 76 | FBEnd = PPOpts.remapped_file_buffer_end(); |
| 77 | FB != FBEnd; |
| 78 | ++FB) |
| 79 | delete FB->second; |
| 80 | } |
Douglas Gregor | 2823342 | 2010-07-27 14:52:07 +0000 | [diff] [blame] | 81 | |
| 82 | delete SavedMainFileBuffer; |
Douglas Gregor | 671947b | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 83 | delete PreambleBuffer; |
| 84 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 85 | ClearCachedCompletionResults(); |
| 86 | |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 87 | for (unsigned I = 0, N = Timers.size(); I != N; ++I) |
| 88 | delete Timers[I]; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void ASTUnit::CleanTemporaryFiles() { |
Douglas Gregor | 313e26c | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 92 | for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) |
| 93 | TemporaryFiles[I].eraseFromDisk(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 94 | TemporaryFiles.clear(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 95 | } |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 96 | |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 97 | /// \brief Determine the set of code-completion contexts in which this |
| 98 | /// declaration should be shown. |
| 99 | static unsigned getDeclShowContexts(NamedDecl *ND, |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 100 | const LangOptions &LangOpts, |
| 101 | bool &IsNestedNameSpecifier) { |
| 102 | IsNestedNameSpecifier = false; |
| 103 | |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 104 | if (isa<UsingShadowDecl>(ND)) |
| 105 | ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 106 | if (!ND) |
| 107 | return 0; |
| 108 | |
| 109 | unsigned Contexts = 0; |
| 110 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) || |
| 111 | isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) { |
| 112 | // Types can appear in these contexts. |
| 113 | if (LangOpts.CPlusPlus || !isa<TagDecl>(ND)) |
| 114 | Contexts |= (1 << (CodeCompletionContext::CCC_TopLevel - 1)) |
| 115 | | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1)) |
| 116 | | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1)) |
| 117 | | (1 << (CodeCompletionContext::CCC_Statement - 1)) |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 118 | | (1 << (CodeCompletionContext::CCC_Type - 1)) |
| 119 | | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1)); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 120 | |
| 121 | // In C++, types can appear in expressions contexts (for functional casts). |
| 122 | if (LangOpts.CPlusPlus) |
| 123 | Contexts |= (1 << (CodeCompletionContext::CCC_Expression - 1)); |
| 124 | |
| 125 | // In Objective-C, message sends can send interfaces. In Objective-C++, |
| 126 | // all types are available due to functional casts. |
| 127 | if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND)) |
| 128 | Contexts |= (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)); |
| 129 | |
| 130 | // Deal with tag names. |
| 131 | if (isa<EnumDecl>(ND)) { |
| 132 | Contexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1)); |
| 133 | |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 134 | // Part of the nested-name-specifier in C++0x. |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 135 | if (LangOpts.CPlusPlus0x) |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 136 | IsNestedNameSpecifier = true; |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 137 | } else if (RecordDecl *Record = dyn_cast<RecordDecl>(ND)) { |
| 138 | if (Record->isUnion()) |
| 139 | Contexts |= (1 << (CodeCompletionContext::CCC_UnionTag - 1)); |
| 140 | else |
| 141 | Contexts |= (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1)); |
| 142 | |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 143 | if (LangOpts.CPlusPlus) |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 144 | IsNestedNameSpecifier = true; |
| 145 | } else if (isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) |
| 146 | IsNestedNameSpecifier = true; |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 147 | } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) { |
| 148 | // Values can appear in these contexts. |
| 149 | Contexts = (1 << (CodeCompletionContext::CCC_Statement - 1)) |
| 150 | | (1 << (CodeCompletionContext::CCC_Expression - 1)) |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 151 | | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1)) |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 152 | | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)); |
| 153 | } else if (isa<ObjCProtocolDecl>(ND)) { |
| 154 | Contexts = (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1)); |
| 155 | } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) { |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 156 | Contexts = (1 << (CodeCompletionContext::CCC_Namespace - 1)); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 157 | |
| 158 | // Part of the nested-name-specifier. |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 159 | IsNestedNameSpecifier = true; |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | return Contexts; |
| 163 | } |
| 164 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 165 | void ASTUnit::CacheCodeCompletionResults() { |
| 166 | if (!TheSema) |
| 167 | return; |
| 168 | |
| 169 | llvm::Timer *CachingTimer = 0; |
| 170 | if (TimerGroup.get()) { |
| 171 | CachingTimer = new llvm::Timer("Cache global code completions", |
| 172 | *TimerGroup); |
| 173 | CachingTimer->startTimer(); |
| 174 | Timers.push_back(CachingTimer); |
| 175 | } |
| 176 | |
| 177 | // Clear out the previous results. |
| 178 | ClearCachedCompletionResults(); |
| 179 | |
| 180 | // Gather the set of global code completions. |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 181 | typedef CodeCompletionResult Result; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 182 | llvm::SmallVector<Result, 8> Results; |
| 183 | TheSema->GatherGlobalCodeCompletions(Results); |
| 184 | |
| 185 | // Translate global code completions into cached completions. |
Douglas Gregor | f5586f6 | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 186 | llvm::DenseMap<CanQualType, unsigned> CompletionTypes; |
| 187 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 188 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 189 | switch (Results[I].Kind) { |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 190 | case Result::RK_Declaration: { |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 191 | bool IsNestedNameSpecifier = false; |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 192 | CachedCodeCompletionResult CachedResult; |
| 193 | CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema); |
| 194 | CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration, |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 195 | Ctx->getLangOptions(), |
| 196 | IsNestedNameSpecifier); |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 197 | CachedResult.Priority = Results[I].Priority; |
| 198 | CachedResult.Kind = Results[I].CursorKind; |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 199 | CachedResult.Availability = Results[I].Availability; |
Douglas Gregor | c4421e9 | 2010-08-16 16:46:30 +0000 | [diff] [blame] | 200 | |
Douglas Gregor | f5586f6 | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 201 | // Keep track of the type of this completion in an ASTContext-agnostic |
| 202 | // way. |
Douglas Gregor | c4421e9 | 2010-08-16 16:46:30 +0000 | [diff] [blame] | 203 | QualType UsageType = getDeclUsageType(*Ctx, Results[I].Declaration); |
Douglas Gregor | f5586f6 | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 204 | if (UsageType.isNull()) { |
Douglas Gregor | c4421e9 | 2010-08-16 16:46:30 +0000 | [diff] [blame] | 205 | CachedResult.TypeClass = STC_Void; |
Douglas Gregor | f5586f6 | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 206 | CachedResult.Type = 0; |
| 207 | } else { |
| 208 | CanQualType CanUsageType |
| 209 | = Ctx->getCanonicalType(UsageType.getUnqualifiedType()); |
| 210 | CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType); |
| 211 | |
| 212 | // Determine whether we have already seen this type. If so, we save |
| 213 | // ourselves the work of formatting the type string by using the |
| 214 | // temporary, CanQualType-based hash table to find the associated value. |
| 215 | unsigned &TypeValue = CompletionTypes[CanUsageType]; |
| 216 | if (TypeValue == 0) { |
| 217 | TypeValue = CompletionTypes.size(); |
| 218 | CachedCompletionTypes[QualType(CanUsageType).getAsString()] |
| 219 | = TypeValue; |
| 220 | } |
| 221 | |
| 222 | CachedResult.Type = TypeValue; |
Douglas Gregor | c4421e9 | 2010-08-16 16:46:30 +0000 | [diff] [blame] | 223 | } |
Douglas Gregor | f5586f6 | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 224 | |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 225 | CachedCompletionResults.push_back(CachedResult); |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 226 | |
| 227 | /// Handle nested-name-specifiers in C++. |
| 228 | if (TheSema->Context.getLangOptions().CPlusPlus && |
| 229 | IsNestedNameSpecifier && !Results[I].StartsNestedNameSpecifier) { |
| 230 | // The contexts in which a nested-name-specifier can appear in C++. |
| 231 | unsigned NNSContexts |
| 232 | = (1 << (CodeCompletionContext::CCC_TopLevel - 1)) |
| 233 | | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1)) |
| 234 | | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1)) |
| 235 | | (1 << (CodeCompletionContext::CCC_Statement - 1)) |
| 236 | | (1 << (CodeCompletionContext::CCC_Expression - 1)) |
| 237 | | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)) |
| 238 | | (1 << (CodeCompletionContext::CCC_EnumTag - 1)) |
| 239 | | (1 << (CodeCompletionContext::CCC_UnionTag - 1)) |
| 240 | | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1)) |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 241 | | (1 << (CodeCompletionContext::CCC_Type - 1)) |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 242 | | (1 << (CodeCompletionContext::CCC_PotentiallyQualifiedName - 1)) |
| 243 | | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1)); |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 244 | |
| 245 | if (isa<NamespaceDecl>(Results[I].Declaration) || |
| 246 | isa<NamespaceAliasDecl>(Results[I].Declaration)) |
| 247 | NNSContexts |= (1 << (CodeCompletionContext::CCC_Namespace - 1)); |
| 248 | |
| 249 | if (unsigned RemainingContexts |
| 250 | = NNSContexts & ~CachedResult.ShowInContexts) { |
| 251 | // If there any contexts where this completion can be a |
| 252 | // nested-name-specifier but isn't already an option, create a |
| 253 | // nested-name-specifier completion. |
| 254 | Results[I].StartsNestedNameSpecifier = true; |
| 255 | CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema); |
| 256 | CachedResult.ShowInContexts = RemainingContexts; |
| 257 | CachedResult.Priority = CCP_NestedNameSpecifier; |
| 258 | CachedResult.TypeClass = STC_Void; |
| 259 | CachedResult.Type = 0; |
| 260 | CachedCompletionResults.push_back(CachedResult); |
| 261 | } |
| 262 | } |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 263 | break; |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 266 | case Result::RK_Keyword: |
| 267 | case Result::RK_Pattern: |
| 268 | // Ignore keywords and patterns; we don't care, since they are so |
| 269 | // easily regenerated. |
| 270 | break; |
| 271 | |
| 272 | case Result::RK_Macro: { |
| 273 | CachedCodeCompletionResult CachedResult; |
| 274 | CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema); |
| 275 | CachedResult.ShowInContexts |
| 276 | = (1 << (CodeCompletionContext::CCC_TopLevel - 1)) |
| 277 | | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1)) |
| 278 | | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 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)) |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 283 | | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)) |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 284 | | (1 << (CodeCompletionContext::CCC_MacroNameUse - 1)) |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 285 | | (1 << (CodeCompletionContext::CCC_PreprocessorExpression - 1)) |
| 286 | | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1)); |
| 287 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 288 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 289 | CachedResult.Priority = Results[I].Priority; |
| 290 | CachedResult.Kind = Results[I].CursorKind; |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 291 | CachedResult.Availability = Results[I].Availability; |
Douglas Gregor | 1827e10 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 292 | CachedResult.TypeClass = STC_Void; |
Douglas Gregor | f5586f6 | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 293 | CachedResult.Type = 0; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 294 | CachedCompletionResults.push_back(CachedResult); |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | Results[I].Destroy(); |
| 299 | } |
| 300 | |
| 301 | if (CachingTimer) |
| 302 | CachingTimer->stopTimer(); |
Douglas Gregor | 727d93e | 2010-08-17 00:40:40 +0000 | [diff] [blame] | 303 | |
| 304 | // Make a note of the state when we performed this caching. |
| 305 | NumTopLevelDeclsAtLastCompletionCache = top_level_size(); |
| 306 | CacheCodeCompletionCoolDown = 15; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | void ASTUnit::ClearCachedCompletionResults() { |
| 310 | for (unsigned I = 0, N = CachedCompletionResults.size(); I != N; ++I) |
| 311 | delete CachedCompletionResults[I].Completion; |
| 312 | CachedCompletionResults.clear(); |
Douglas Gregor | f5586f6 | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 313 | CachedCompletionTypes.clear(); |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 316 | namespace { |
| 317 | |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 318 | /// \brief Gathers information from ASTReader that will be used to initialize |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 319 | /// a Preprocessor. |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 320 | class ASTInfoCollector : public ASTReaderListener { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 321 | LangOptions &LangOpt; |
| 322 | HeaderSearch &HSI; |
| 323 | std::string &TargetTriple; |
| 324 | std::string &Predefines; |
| 325 | unsigned &Counter; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 327 | unsigned NumHeaderInfos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 329 | public: |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 330 | ASTInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI, |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 331 | std::string &TargetTriple, std::string &Predefines, |
| 332 | unsigned &Counter) |
| 333 | : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple), |
| 334 | Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 336 | virtual bool ReadLanguageOptions(const LangOptions &LangOpts) { |
| 337 | LangOpt = LangOpts; |
| 338 | return false; |
| 339 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | |
Daniel Dunbar | dc3c0d2 | 2009-11-11 00:52:11 +0000 | [diff] [blame] | 341 | virtual bool ReadTargetTriple(llvm::StringRef Triple) { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 342 | TargetTriple = Triple; |
| 343 | return false; |
| 344 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | |
Sebastian Redl | cb481aa | 2010-07-14 23:29:55 +0000 | [diff] [blame] | 346 | virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers, |
Daniel Dunbar | 7b5a121 | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 347 | llvm::StringRef OriginalFileName, |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 348 | std::string &SuggestedPredefines) { |
Sebastian Redl | cb481aa | 2010-07-14 23:29:55 +0000 | [diff] [blame] | 349 | Predefines = Buffers[0].Data; |
| 350 | for (unsigned I = 1, N = Buffers.size(); I != N; ++I) { |
| 351 | Predefines += Buffers[I].Data; |
| 352 | } |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 353 | return false; |
| 354 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | |
Douglas Gregor | ec1afbf | 2010-03-16 19:09:18 +0000 | [diff] [blame] | 356 | virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 357 | HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++); |
| 358 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 360 | virtual void ReadCounter(unsigned Value) { |
| 361 | Counter = Value; |
| 362 | } |
| 363 | }; |
| 364 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 365 | class StoredDiagnosticClient : public DiagnosticClient { |
| 366 | llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags; |
| 367 | |
| 368 | public: |
| 369 | explicit StoredDiagnosticClient( |
| 370 | llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags) |
| 371 | : StoredDiags(StoredDiags) { } |
| 372 | |
| 373 | virtual void HandleDiagnostic(Diagnostic::Level Level, |
| 374 | const DiagnosticInfo &Info); |
| 375 | }; |
| 376 | |
| 377 | /// \brief RAII object that optionally captures diagnostics, if |
| 378 | /// there is no diagnostic client to capture them already. |
| 379 | class CaptureDroppedDiagnostics { |
| 380 | Diagnostic &Diags; |
| 381 | StoredDiagnosticClient Client; |
| 382 | DiagnosticClient *PreviousClient; |
| 383 | |
| 384 | public: |
| 385 | CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags, |
| 386 | llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags) |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 387 | : Diags(Diags), Client(StoredDiags), PreviousClient(0) |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 388 | { |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 389 | if (RequestCapture || Diags.getClient() == 0) { |
| 390 | PreviousClient = Diags.takeClient(); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 391 | Diags.setClient(&Client); |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 392 | } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | ~CaptureDroppedDiagnostics() { |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 396 | if (Diags.getClient() == &Client) { |
| 397 | Diags.takeClient(); |
| 398 | Diags.setClient(PreviousClient); |
| 399 | } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 400 | } |
| 401 | }; |
| 402 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 403 | } // anonymous namespace |
| 404 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 405 | void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level, |
| 406 | const DiagnosticInfo &Info) { |
| 407 | StoredDiags.push_back(StoredDiagnostic(Level, Info)); |
| 408 | } |
| 409 | |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 410 | const std::string &ASTUnit::getOriginalSourceFileName() { |
Daniel Dunbar | 68d40e2 | 2009-12-02 08:44:16 +0000 | [diff] [blame] | 411 | return OriginalSourceFile; |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 412 | } |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 413 | |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 414 | const std::string &ASTUnit::getASTFileName() { |
| 415 | assert(isMainFileAST() && "Not an ASTUnit from an AST file!"); |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 416 | return static_cast<ASTReader *>(Ctx->getExternalSource())->getFileName(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 419 | ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename, |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 420 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags, |
Ted Kremenek | 5cf4876 | 2009-10-17 00:34:24 +0000 | [diff] [blame] | 421 | bool OnlyLocalDecls, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 422 | RemappedFile *RemappedFiles, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 423 | unsigned NumRemappedFiles, |
| 424 | bool CaptureDiagnostics) { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 425 | llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true)); |
| 426 | |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 427 | if (!Diags.getPtr()) { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 428 | // No diagnostics engine was provided, so create our own diagnostics object |
| 429 | // with the default options. |
| 430 | DiagnosticOptions DiagOpts; |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 431 | Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 432 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 433 | |
| 434 | AST->CaptureDiagnostics = CaptureDiagnostics; |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 435 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 436 | AST->Diagnostics = Diags; |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 437 | AST->FileMgr.reset(new FileManager); |
| 438 | AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics())); |
Steve Naroff | 36c4464 | 2009-10-19 14:34:22 +0000 | [diff] [blame] | 439 | AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager())); |
Douglas Gregor | 914ed9d | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 440 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 441 | // If requested, capture diagnostics in the ASTUnit. |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 442 | CaptureDroppedDiagnostics Capture(CaptureDiagnostics, AST->getDiagnostics(), |
Douglas Gregor | 405634b | 2010-04-05 18:10:21 +0000 | [diff] [blame] | 443 | AST->StoredDiagnostics); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 444 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 445 | for (unsigned I = 0; I != NumRemappedFiles; ++I) { |
| 446 | // Create the file entry for the file that we're mapping from. |
| 447 | const FileEntry *FromFile |
| 448 | = AST->getFileManager().getVirtualFile(RemappedFiles[I].first, |
| 449 | RemappedFiles[I].second->getBufferSize(), |
| 450 | 0); |
| 451 | if (!FromFile) { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 452 | AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file) |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 453 | << RemappedFiles[I].first; |
Douglas Gregor | c8dfe5e | 2010-02-27 01:32:48 +0000 | [diff] [blame] | 454 | delete RemappedFiles[I].second; |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 455 | continue; |
| 456 | } |
| 457 | |
| 458 | // Override the contents of the "from" file with the contents of |
| 459 | // the "to" file. |
| 460 | AST->getSourceManager().overrideFileContents(FromFile, |
| 461 | RemappedFiles[I].second); |
| 462 | } |
| 463 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 464 | // Gather Info for preprocessor construction later on. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 465 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 466 | LangOptions LangInfo; |
| 467 | HeaderSearch &HeaderInfo = *AST->HeaderInfo.get(); |
| 468 | std::string TargetTriple; |
| 469 | std::string Predefines; |
| 470 | unsigned Counter; |
| 471 | |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 472 | llvm::OwningPtr<ASTReader> Reader; |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 473 | |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 474 | Reader.reset(new ASTReader(AST->getSourceManager(), AST->getFileManager(), |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 475 | AST->getDiagnostics())); |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 476 | Reader->setListener(new ASTInfoCollector(LangInfo, HeaderInfo, TargetTriple, |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 477 | Predefines, Counter)); |
| 478 | |
Sebastian Redl | 571db7f | 2010-08-18 23:56:56 +0000 | [diff] [blame] | 479 | switch (Reader->ReadAST(Filename)) { |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 480 | case ASTReader::Success: |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 481 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 483 | case ASTReader::Failure: |
| 484 | case ASTReader::IgnorePCH: |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 485 | AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 486 | return NULL; |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 487 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 | |
Daniel Dunbar | 68d40e2 | 2009-12-02 08:44:16 +0000 | [diff] [blame] | 489 | AST->OriginalSourceFile = Reader->getOriginalSourceFile(); |
| 490 | |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 491 | // AST file loaded successfully. Now create the preprocessor. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 493 | // Get information about the target being compiled for. |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 494 | // |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 495 | // FIXME: This is broken, we should store the TargetOptions in the AST file. |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 496 | TargetOptions TargetOpts; |
| 497 | TargetOpts.ABI = ""; |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 498 | TargetOpts.CXXABI = ""; |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 499 | TargetOpts.CPU = ""; |
| 500 | TargetOpts.Features.clear(); |
| 501 | TargetOpts.Triple = TargetTriple; |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 502 | AST->Target.reset(TargetInfo::CreateTargetInfo(AST->getDiagnostics(), |
| 503 | TargetOpts)); |
| 504 | AST->PP.reset(new Preprocessor(AST->getDiagnostics(), LangInfo, |
| 505 | *AST->Target.get(), |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 506 | AST->getSourceManager(), HeaderInfo)); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 507 | Preprocessor &PP = *AST->PP.get(); |
| 508 | |
Daniel Dunbar | d5b6126 | 2009-09-21 03:03:47 +0000 | [diff] [blame] | 509 | PP.setPredefines(Reader->getSuggestedPredefines()); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 510 | PP.setCounterValue(Counter); |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 511 | Reader->setPreprocessor(PP); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 513 | // Create and initialize the ASTContext. |
| 514 | |
| 515 | AST->Ctx.reset(new ASTContext(LangInfo, |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 516 | AST->getSourceManager(), |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 517 | *AST->Target.get(), |
| 518 | PP.getIdentifierTable(), |
| 519 | PP.getSelectorTable(), |
| 520 | PP.getBuiltinInfo(), |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 521 | /* size_reserve = */0)); |
| 522 | ASTContext &Context = *AST->Ctx.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 523 | |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 524 | Reader->InitializeContext(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 525 | |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 526 | // Attach the AST reader to the AST context as an external AST |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 527 | // source, so that declarations will be deserialized from the |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 528 | // AST file as needed. |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 529 | ASTReader *ReaderPtr = Reader.get(); |
Douglas Gregor | 914ed9d | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 530 | llvm::OwningPtr<ExternalASTSource> Source(Reader.take()); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 531 | Context.setExternalSource(Source); |
| 532 | |
Douglas Gregor | 914ed9d | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 533 | // Create an AST consumer, even though it isn't used. |
| 534 | AST->Consumer.reset(new ASTConsumer); |
| 535 | |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 536 | // Create a semantic analysis object and tell the AST reader about it. |
Douglas Gregor | 914ed9d | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 537 | AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer)); |
| 538 | AST->TheSema->Initialize(); |
| 539 | ReaderPtr->InitializeSema(*AST->TheSema); |
| 540 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 541 | return AST.take(); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 542 | } |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 543 | |
| 544 | namespace { |
| 545 | |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 546 | class TopLevelDeclTrackerConsumer : public ASTConsumer { |
| 547 | ASTUnit &Unit; |
| 548 | |
| 549 | public: |
| 550 | TopLevelDeclTrackerConsumer(ASTUnit &_Unit) : Unit(_Unit) {} |
| 551 | |
| 552 | void HandleTopLevelDecl(DeclGroupRef D) { |
Ted Kremenek | da5a428 | 2010-05-03 20:16:35 +0000 | [diff] [blame] | 553 | for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) { |
| 554 | Decl *D = *it; |
| 555 | // FIXME: Currently ObjC method declarations are incorrectly being |
| 556 | // reported as top-level declarations, even though their DeclContext |
| 557 | // is the containing ObjC @interface/@implementation. This is a |
| 558 | // fundamental problem in the parser right now. |
| 559 | if (isa<ObjCMethodDecl>(D)) |
| 560 | continue; |
Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 561 | Unit.addTopLevelDecl(D); |
Ted Kremenek | da5a428 | 2010-05-03 20:16:35 +0000 | [diff] [blame] | 562 | } |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 563 | } |
Sebastian Redl | 27372b4 | 2010-08-11 18:52:41 +0000 | [diff] [blame] | 564 | |
| 565 | // We're not interested in "interesting" decls. |
| 566 | void HandleInterestingDecl(DeclGroupRef) {} |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 567 | }; |
| 568 | |
| 569 | class TopLevelDeclTrackerAction : public ASTFrontendAction { |
| 570 | public: |
| 571 | ASTUnit &Unit; |
| 572 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 573 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
| 574 | llvm::StringRef InFile) { |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 575 | return new TopLevelDeclTrackerConsumer(Unit); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | public: |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 579 | TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {} |
| 580 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 581 | virtual bool hasCodeCompletionSupport() const { return false; } |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 582 | virtual bool usesCompleteTranslationUnit() { |
| 583 | return Unit.isCompleteTranslationUnit(); |
| 584 | } |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 585 | }; |
| 586 | |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 587 | class PrecompilePreambleConsumer : public PCHGenerator { |
| 588 | ASTUnit &Unit; |
Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 589 | std::vector<Decl *> TopLevelDecls; |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 590 | |
| 591 | public: |
| 592 | PrecompilePreambleConsumer(ASTUnit &Unit, |
| 593 | const Preprocessor &PP, bool Chaining, |
| 594 | const char *isysroot, llvm::raw_ostream *Out) |
| 595 | : PCHGenerator(PP, Chaining, isysroot, Out), Unit(Unit) { } |
| 596 | |
Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 597 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 598 | for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) { |
| 599 | Decl *D = *it; |
| 600 | // FIXME: Currently ObjC method declarations are incorrectly being |
| 601 | // reported as top-level declarations, even though their DeclContext |
| 602 | // is the containing ObjC @interface/@implementation. This is a |
| 603 | // fundamental problem in the parser right now. |
| 604 | if (isa<ObjCMethodDecl>(D)) |
| 605 | continue; |
Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 606 | TopLevelDecls.push_back(D); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | virtual void HandleTranslationUnit(ASTContext &Ctx) { |
| 611 | PCHGenerator::HandleTranslationUnit(Ctx); |
| 612 | if (!Unit.getDiagnostics().hasErrorOccurred()) { |
| 613 | // Translate the top-level declarations we captured during |
| 614 | // parsing into declaration IDs in the precompiled |
| 615 | // preamble. This will allow us to deserialize those top-level |
| 616 | // declarations when requested. |
| 617 | for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I) |
| 618 | Unit.addTopLevelDeclFromPreamble( |
| 619 | getWriter().getDeclID(TopLevelDecls[I])); |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | }; |
| 623 | |
| 624 | class PrecompilePreambleAction : public ASTFrontendAction { |
| 625 | ASTUnit &Unit; |
| 626 | |
| 627 | public: |
| 628 | explicit PrecompilePreambleAction(ASTUnit &Unit) : Unit(Unit) {} |
| 629 | |
| 630 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
| 631 | llvm::StringRef InFile) { |
| 632 | std::string Sysroot; |
| 633 | llvm::raw_ostream *OS = 0; |
| 634 | bool Chaining; |
| 635 | if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot, |
| 636 | OS, Chaining)) |
| 637 | return 0; |
| 638 | |
| 639 | const char *isysroot = CI.getFrontendOpts().RelocatablePCH ? |
| 640 | Sysroot.c_str() : 0; |
| 641 | return new PrecompilePreambleConsumer(Unit, CI.getPreprocessor(), Chaining, |
| 642 | isysroot, OS); |
| 643 | } |
| 644 | |
| 645 | virtual bool hasCodeCompletionSupport() const { return false; } |
| 646 | virtual bool hasASTFileSupport() const { return false; } |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 647 | virtual bool usesCompleteTranslationUnit() { return false; } |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 648 | }; |
| 649 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 652 | /// Parse the source file into a translation unit using the given compiler |
| 653 | /// invocation, replacing the current translation unit. |
| 654 | /// |
| 655 | /// \returns True if a failure occurred that causes the ASTUnit not to |
| 656 | /// contain any translation-unit information, false otherwise. |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 657 | bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) { |
Douglas Gregor | 2823342 | 2010-07-27 14:52:07 +0000 | [diff] [blame] | 658 | delete SavedMainFileBuffer; |
| 659 | SavedMainFileBuffer = 0; |
| 660 | |
Douglas Gregor | 671947b | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 661 | if (!Invocation.get()) { |
| 662 | delete OverrideMainBuffer; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 663 | return true; |
Douglas Gregor | 671947b | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 664 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 665 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 666 | // Create the compiler instance to use for building the AST. |
Daniel Dunbar | cb6dda1 | 2009-12-02 08:43:56 +0000 | [diff] [blame] | 667 | CompilerInstance Clang; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 668 | Clang.setInvocation(Invocation.take()); |
| 669 | OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second; |
| 670 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 671 | // Set up diagnostics, capturing any diagnostics that would |
| 672 | // otherwise be dropped. |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 673 | Clang.setDiagnostics(&getDiagnostics()); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 674 | CaptureDroppedDiagnostics Capture(CaptureDiagnostics, |
| 675 | getDiagnostics(), |
| 676 | StoredDiagnostics); |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 677 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 678 | // Create the target instance. |
| 679 | Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), |
| 680 | Clang.getTargetOpts())); |
Douglas Gregor | 671947b | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 681 | if (!Clang.hasTarget()) { |
| 682 | delete OverrideMainBuffer; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 683 | return true; |
Douglas Gregor | 671947b | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 686 | // Inform the target of the language options. |
| 687 | // |
| 688 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 689 | // created. This complexity should be lifted elsewhere. |
| 690 | Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 691 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 692 | assert(Clang.getFrontendOpts().Inputs.size() == 1 && |
| 693 | "Invocation must have exactly one source file!"); |
Daniel Dunbar | c34ce3f | 2010-06-07 23:22:09 +0000 | [diff] [blame] | 694 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST && |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 695 | "FIXME: AST inputs not yet supported here!"); |
Daniel Dunbar | faddc3e | 2010-06-07 23:26:47 +0000 | [diff] [blame] | 696 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR && |
| 697 | "IR inputs not support here!"); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 698 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 699 | // Configure the various subsystems. |
| 700 | // FIXME: Should we retain the previous file manager? |
| 701 | FileMgr.reset(new FileManager); |
| 702 | SourceMgr.reset(new SourceManager(getDiagnostics())); |
Douglas Gregor | 914ed9d | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 703 | TheSema.reset(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 704 | Ctx.reset(); |
| 705 | PP.reset(); |
| 706 | |
| 707 | // Clear out old caches and data. |
| 708 | TopLevelDecls.clear(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 709 | CleanTemporaryFiles(); |
| 710 | PreprocessedEntitiesByFile.clear(); |
Douglas Gregor | c0659ec | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 711 | |
Douglas Gregor | f128fed | 2010-08-20 00:02:33 +0000 | [diff] [blame] | 712 | if (!OverrideMainBuffer) { |
Douglas Gregor | c0659ec | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 713 | StoredDiagnostics.clear(); |
Douglas Gregor | f128fed | 2010-08-20 00:02:33 +0000 | [diff] [blame] | 714 | TopLevelDeclsInPreamble.clear(); |
| 715 | } |
| 716 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 717 | // Create a file manager object to provide access to and cache the filesystem. |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 718 | Clang.setFileManager(&getFileManager()); |
| 719 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 720 | // Create the source manager. |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 721 | Clang.setSourceManager(&getSourceManager()); |
| 722 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 723 | // If the main file has been overridden due to the use of a preamble, |
| 724 | // make that override happen and introduce the preamble. |
| 725 | PreprocessorOptions &PreprocessorOpts = Clang.getPreprocessorOpts(); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 726 | std::string PriorImplicitPCHInclude; |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 727 | if (OverrideMainBuffer) { |
| 728 | PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer); |
| 729 | PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size(); |
| 730 | PreprocessorOpts.PrecompiledPreambleBytes.second |
| 731 | = PreambleEndsAtStartOfLine; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 732 | PriorImplicitPCHInclude = PreprocessorOpts.ImplicitPCHInclude; |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 733 | PreprocessorOpts.ImplicitPCHInclude = PreambleFile; |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 734 | PreprocessorOpts.DisablePCHValidation = true; |
Douglas Gregor | 2823342 | 2010-07-27 14:52:07 +0000 | [diff] [blame] | 735 | |
| 736 | // Keep track of the override buffer; |
| 737 | SavedMainFileBuffer = OverrideMainBuffer; |
Douglas Gregor | c0659ec | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 738 | |
| 739 | // The stored diagnostic has the old source manager in it; update |
| 740 | // the locations to refer into the new source manager. Since we've |
| 741 | // been careful to make sure that the source manager's state |
| 742 | // before and after are identical, so that we can reuse the source |
| 743 | // location itself. |
| 744 | for (unsigned I = 0, N = StoredDiagnostics.size(); I != N; ++I) { |
| 745 | FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), |
| 746 | getSourceManager()); |
| 747 | StoredDiagnostics[I].setLocation(Loc); |
| 748 | } |
Douglas Gregor | f128fed | 2010-08-20 00:02:33 +0000 | [diff] [blame] | 749 | } else { |
| 750 | PreprocessorOpts.PrecompiledPreambleBytes.first = 0; |
| 751 | PreprocessorOpts.PrecompiledPreambleBytes.second = false; |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 754 | llvm::OwningPtr<TopLevelDeclTrackerAction> Act; |
| 755 | Act.reset(new TopLevelDeclTrackerAction(*this)); |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 756 | if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second, |
Daniel Dunbar | d3598a6 | 2010-06-07 23:23:06 +0000 | [diff] [blame] | 757 | Clang.getFrontendOpts().Inputs[0].first)) |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 758 | goto error; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 759 | |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 760 | Act->Execute(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 761 | |
Daniel Dunbar | 64a32ba | 2009-12-01 21:57:33 +0000 | [diff] [blame] | 762 | // Steal the created target, context, and preprocessor, and take back the |
| 763 | // source and file managers. |
Douglas Gregor | 914ed9d | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 764 | TheSema.reset(Clang.takeSema()); |
| 765 | Consumer.reset(Clang.takeASTConsumer()); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 766 | Ctx.reset(Clang.takeASTContext()); |
| 767 | PP.reset(Clang.takePreprocessor()); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 768 | Clang.takeSourceManager(); |
| 769 | Clang.takeFileManager(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 770 | Target.reset(Clang.takeTarget()); |
| 771 | |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 772 | Act->EndSourceFile(); |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 773 | |
| 774 | // Remove the overridden buffer we used for the preamble. |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 775 | if (OverrideMainBuffer) { |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 776 | PreprocessorOpts.eraseRemappedFile( |
| 777 | PreprocessorOpts.remapped_file_buffer_end() - 1); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 778 | PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude; |
| 779 | } |
| 780 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 781 | Invocation.reset(Clang.takeInvocation()); |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 782 | |
| 783 | // If we were asked to cache code-completion results and don't have any |
| 784 | // results yet, do so now. |
| 785 | if (ShouldCacheCodeCompletionResults && CachedCompletionResults.empty()) |
| 786 | CacheCodeCompletionResults(); |
| 787 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 788 | return false; |
| 789 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 790 | error: |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 791 | // Remove the overridden buffer we used for the preamble. |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 792 | if (OverrideMainBuffer) { |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 793 | PreprocessorOpts.eraseRemappedFile( |
| 794 | PreprocessorOpts.remapped_file_buffer_end() - 1); |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 795 | PreprocessorOpts.DisablePCHValidation = true; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 796 | PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude; |
Douglas Gregor | 671947b | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 797 | delete OverrideMainBuffer; |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 798 | } |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 799 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 800 | Clang.takeSourceManager(); |
| 801 | Clang.takeFileManager(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 802 | Invocation.reset(Clang.takeInvocation()); |
| 803 | return true; |
| 804 | } |
| 805 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 806 | /// \brief Simple function to retrieve a path for a preamble precompiled header. |
| 807 | static std::string GetPreamblePCHPath() { |
| 808 | // FIXME: This is lame; sys::Path should provide this function (in particular, |
| 809 | // it should know how to find the temporary files dir). |
| 810 | // FIXME: This is really lame. I copied this code from the Driver! |
Douglas Gregor | 424668c | 2010-09-11 18:05:19 +0000 | [diff] [blame] | 811 | // FIXME: This is a hack so that we can override the preamble file during |
| 812 | // crash-recovery testing, which is the only case where the preamble files |
| 813 | // are not necessarily cleaned up. |
| 814 | const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE"); |
| 815 | if (TmpFile) |
| 816 | return TmpFile; |
| 817 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 818 | std::string Error; |
| 819 | const char *TmpDir = ::getenv("TMPDIR"); |
| 820 | if (!TmpDir) |
| 821 | TmpDir = ::getenv("TEMP"); |
| 822 | if (!TmpDir) |
| 823 | TmpDir = ::getenv("TMP"); |
Douglas Gregor | c6cb2b0 | 2010-09-11 17:51:16 +0000 | [diff] [blame] | 824 | #ifdef LLVM_ON_WIN32 |
| 825 | if (!TmpDir) |
| 826 | TmpDir = ::getenv("USERPROFILE"); |
| 827 | #endif |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 828 | if (!TmpDir) |
| 829 | TmpDir = "/tmp"; |
| 830 | llvm::sys::Path P(TmpDir); |
Douglas Gregor | c6cb2b0 | 2010-09-11 17:51:16 +0000 | [diff] [blame] | 831 | P.createDirectoryOnDisk(true); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 832 | P.appendComponent("preamble"); |
Douglas Gregor | 6bf1830 | 2010-08-11 13:06:56 +0000 | [diff] [blame] | 833 | P.appendSuffix("pch"); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 834 | if (P.createTemporaryFileOnDisk()) |
| 835 | return std::string(); |
| 836 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 837 | return P.str(); |
| 838 | } |
| 839 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 840 | /// \brief Compute the preamble for the main file, providing the source buffer |
| 841 | /// that corresponds to the main file along with a pair (bytes, start-of-line) |
| 842 | /// that describes the preamble. |
| 843 | std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 844 | ASTUnit::ComputePreamble(CompilerInvocation &Invocation, |
| 845 | unsigned MaxLines, bool &CreatedBuffer) { |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 846 | FrontendOptions &FrontendOpts = Invocation.getFrontendOpts(); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 847 | PreprocessorOptions &PreprocessorOpts |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 848 | = Invocation.getPreprocessorOpts(); |
| 849 | CreatedBuffer = false; |
| 850 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 851 | // Try to determine if the main file has been remapped, either from the |
| 852 | // command line (to another file) or directly through the compiler invocation |
| 853 | // (to a memory buffer). |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 854 | llvm::MemoryBuffer *Buffer = 0; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 855 | llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second); |
| 856 | if (const llvm::sys::FileStatus *MainFileStatus = MainFilePath.getFileStatus()) { |
| 857 | // Check whether there is a file-file remapping of the main file |
| 858 | for (PreprocessorOptions::remapped_file_iterator |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 859 | M = PreprocessorOpts.remapped_file_begin(), |
| 860 | E = PreprocessorOpts.remapped_file_end(); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 861 | M != E; |
| 862 | ++M) { |
| 863 | llvm::sys::PathWithStatus MPath(M->first); |
| 864 | if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) { |
| 865 | if (MainFileStatus->uniqueID == MStatus->uniqueID) { |
| 866 | // We found a remapping. Try to load the resulting, remapped source. |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 867 | if (CreatedBuffer) { |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 868 | delete Buffer; |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 869 | CreatedBuffer = false; |
| 870 | } |
| 871 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 872 | Buffer = llvm::MemoryBuffer::getFile(M->second); |
| 873 | if (!Buffer) |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 874 | return std::make_pair((llvm::MemoryBuffer*)0, |
| 875 | std::make_pair(0, true)); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 876 | CreatedBuffer = true; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 877 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 878 | // Remove this remapping. We've captured the buffer already. |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 879 | M = PreprocessorOpts.eraseRemappedFile(M); |
| 880 | E = PreprocessorOpts.remapped_file_end(); |
Daniel Dunbar | c1cf158 | 2010-08-19 19:40:40 +0000 | [diff] [blame] | 881 | if (M == E) |
| 882 | break; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | // Check whether there is a file-buffer remapping. It supercedes the |
| 888 | // file-file remapping. |
| 889 | for (PreprocessorOptions::remapped_file_buffer_iterator |
| 890 | M = PreprocessorOpts.remapped_file_buffer_begin(), |
| 891 | E = PreprocessorOpts.remapped_file_buffer_end(); |
| 892 | M != E; |
| 893 | ++M) { |
| 894 | llvm::sys::PathWithStatus MPath(M->first); |
| 895 | if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) { |
| 896 | if (MainFileStatus->uniqueID == MStatus->uniqueID) { |
| 897 | // We found a remapping. |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 898 | if (CreatedBuffer) { |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 899 | delete Buffer; |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 900 | CreatedBuffer = false; |
| 901 | } |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 902 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 903 | Buffer = const_cast<llvm::MemoryBuffer *>(M->second); |
| 904 | |
| 905 | // Remove this remapping. We've captured the buffer already. |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 906 | M = PreprocessorOpts.eraseRemappedFile(M); |
| 907 | E = PreprocessorOpts.remapped_file_buffer_end(); |
Daniel Dunbar | c1cf158 | 2010-08-19 19:40:40 +0000 | [diff] [blame] | 908 | if (M == E) |
| 909 | break; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 910 | } |
| 911 | } |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 912 | } |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | // If the main source file was not remapped, load it now. |
| 916 | if (!Buffer) { |
| 917 | Buffer = llvm::MemoryBuffer::getFile(FrontendOpts.Inputs[0].second); |
| 918 | if (!Buffer) |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 919 | return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true)); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 920 | |
| 921 | CreatedBuffer = true; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 924 | return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer, MaxLines)); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 927 | static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old, |
| 928 | bool DeleteOld, |
| 929 | unsigned NewSize, |
| 930 | llvm::StringRef NewName) { |
| 931 | llvm::MemoryBuffer *Result |
| 932 | = llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName); |
| 933 | memcpy(const_cast<char*>(Result->getBufferStart()), |
| 934 | Old->getBufferStart(), Old->getBufferSize()); |
| 935 | memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(), |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 936 | ' ', NewSize - Old->getBufferSize() - 1); |
| 937 | const_cast<char*>(Result->getBufferEnd())[-1] = '\n'; |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 938 | |
| 939 | if (DeleteOld) |
| 940 | delete Old; |
| 941 | |
| 942 | return Result; |
| 943 | } |
| 944 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 945 | /// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing |
| 946 | /// the source file. |
| 947 | /// |
| 948 | /// This routine will compute the preamble of the main source file. If a |
| 949 | /// non-trivial preamble is found, it will precompile that preamble into a |
| 950 | /// precompiled header so that the precompiled preamble can be used to reduce |
| 951 | /// reparsing time. If a precompiled preamble has already been constructed, |
| 952 | /// this routine will determine if it is still valid and, if so, avoid |
| 953 | /// rebuilding the precompiled preamble. |
| 954 | /// |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 955 | /// \param AllowRebuild When true (the default), this routine is |
| 956 | /// allowed to rebuild the precompiled preamble if it is found to be |
| 957 | /// out-of-date. |
| 958 | /// |
| 959 | /// \param MaxLines When non-zero, the maximum number of lines that |
| 960 | /// can occur within the preamble. |
| 961 | /// |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 962 | /// \returns If the precompiled preamble can be used, returns a newly-allocated |
| 963 | /// buffer that should be used in place of the main file when doing so. |
| 964 | /// Otherwise, returns a NULL pointer. |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 965 | llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble( |
Douglas Gregor | 2283d79 | 2010-08-20 00:59:43 +0000 | [diff] [blame] | 966 | CompilerInvocation PreambleInvocation, |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 967 | bool AllowRebuild, |
| 968 | unsigned MaxLines) { |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 969 | FrontendOptions &FrontendOpts = PreambleInvocation.getFrontendOpts(); |
| 970 | PreprocessorOptions &PreprocessorOpts |
| 971 | = PreambleInvocation.getPreprocessorOpts(); |
| 972 | |
| 973 | bool CreatedPreambleBuffer = false; |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 974 | std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 975 | = ComputePreamble(PreambleInvocation, MaxLines, CreatedPreambleBuffer); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 976 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 977 | if (!NewPreamble.second.first) { |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 978 | // We couldn't find a preamble in the main source. Clear out the current |
| 979 | // preamble, if we have one. It's obviously no good any more. |
| 980 | Preamble.clear(); |
| 981 | if (!PreambleFile.empty()) { |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 982 | llvm::sys::Path(PreambleFile).eraseFromDisk(); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 983 | PreambleFile.clear(); |
| 984 | } |
| 985 | if (CreatedPreambleBuffer) |
| 986 | delete NewPreamble.first; |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 987 | |
| 988 | // The next time we actually see a preamble, precompile it. |
| 989 | PreambleRebuildCounter = 1; |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 990 | return 0; |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | if (!Preamble.empty()) { |
| 994 | // We've previously computed a preamble. Check whether we have the same |
| 995 | // preamble now that we did before, and that there's enough space in |
| 996 | // the main-file buffer within the precompiled preamble to fit the |
| 997 | // new main file. |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 998 | if (Preamble.size() == NewPreamble.second.first && |
| 999 | PreambleEndsAtStartOfLine == NewPreamble.second.second && |
Douglas Gregor | 592508e | 2010-07-24 00:42:07 +0000 | [diff] [blame] | 1000 | NewPreamble.first->getBufferSize() < PreambleReservedSize-2 && |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1001 | memcmp(&Preamble[0], NewPreamble.first->getBufferStart(), |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1002 | NewPreamble.second.first) == 0) { |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1003 | // The preamble has not changed. We may be able to re-use the precompiled |
| 1004 | // preamble. |
Douglas Gregor | c0659ec | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1005 | |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1006 | // Check that none of the files used by the preamble have changed. |
| 1007 | bool AnyFileChanged = false; |
| 1008 | |
| 1009 | // First, make a record of those files that have been overridden via |
| 1010 | // remapping or unsaved_files. |
| 1011 | llvm::StringMap<std::pair<off_t, time_t> > OverriddenFiles; |
| 1012 | for (PreprocessorOptions::remapped_file_iterator |
| 1013 | R = PreprocessorOpts.remapped_file_begin(), |
| 1014 | REnd = PreprocessorOpts.remapped_file_end(); |
| 1015 | !AnyFileChanged && R != REnd; |
| 1016 | ++R) { |
| 1017 | struct stat StatBuf; |
| 1018 | if (stat(R->second.c_str(), &StatBuf)) { |
| 1019 | // If we can't stat the file we're remapping to, assume that something |
| 1020 | // horrible happened. |
| 1021 | AnyFileChanged = true; |
| 1022 | break; |
| 1023 | } |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1024 | |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1025 | OverriddenFiles[R->first] = std::make_pair(StatBuf.st_size, |
| 1026 | StatBuf.st_mtime); |
| 1027 | } |
| 1028 | for (PreprocessorOptions::remapped_file_buffer_iterator |
| 1029 | R = PreprocessorOpts.remapped_file_buffer_begin(), |
| 1030 | REnd = PreprocessorOpts.remapped_file_buffer_end(); |
| 1031 | !AnyFileChanged && R != REnd; |
| 1032 | ++R) { |
| 1033 | // FIXME: Should we actually compare the contents of file->buffer |
| 1034 | // remappings? |
| 1035 | OverriddenFiles[R->first] = std::make_pair(R->second->getBufferSize(), |
| 1036 | 0); |
| 1037 | } |
| 1038 | |
| 1039 | // Check whether anything has changed. |
| 1040 | for (llvm::StringMap<std::pair<off_t, time_t> >::iterator |
| 1041 | F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end(); |
| 1042 | !AnyFileChanged && F != FEnd; |
| 1043 | ++F) { |
| 1044 | llvm::StringMap<std::pair<off_t, time_t> >::iterator Overridden |
| 1045 | = OverriddenFiles.find(F->first()); |
| 1046 | if (Overridden != OverriddenFiles.end()) { |
| 1047 | // This file was remapped; check whether the newly-mapped file |
| 1048 | // matches up with the previous mapping. |
| 1049 | if (Overridden->second != F->second) |
| 1050 | AnyFileChanged = true; |
| 1051 | continue; |
| 1052 | } |
| 1053 | |
| 1054 | // The file was not remapped; check whether it has changed on disk. |
| 1055 | struct stat StatBuf; |
| 1056 | if (stat(F->first(), &StatBuf)) { |
| 1057 | // If we can't stat the file, assume that something horrible happened. |
| 1058 | AnyFileChanged = true; |
| 1059 | } else if (StatBuf.st_size != F->second.first || |
| 1060 | StatBuf.st_mtime != F->second.second) |
| 1061 | AnyFileChanged = true; |
| 1062 | } |
| 1063 | |
| 1064 | if (!AnyFileChanged) { |
Douglas Gregor | c0659ec | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1065 | // Okay! We can re-use the precompiled preamble. |
| 1066 | |
| 1067 | // Set the state of the diagnostic object to mimic its state |
| 1068 | // after parsing the preamble. |
| 1069 | getDiagnostics().Reset(); |
| 1070 | getDiagnostics().setNumWarnings(NumWarningsInPreamble); |
| 1071 | if (StoredDiagnostics.size() > NumStoredDiagnosticsInPreamble) |
| 1072 | StoredDiagnostics.erase( |
| 1073 | StoredDiagnostics.begin() + NumStoredDiagnosticsInPreamble, |
| 1074 | StoredDiagnostics.end()); |
| 1075 | |
| 1076 | // Create a version of the main file buffer that is padded to |
| 1077 | // buffer size we reserved when creating the preamble. |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1078 | return CreatePaddedMainFileBuffer(NewPreamble.first, |
| 1079 | CreatedPreambleBuffer, |
| 1080 | PreambleReservedSize, |
| 1081 | FrontendOpts.Inputs[0].second); |
| 1082 | } |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1083 | } |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1084 | |
| 1085 | // If we aren't allowed to rebuild the precompiled preamble, just |
| 1086 | // return now. |
| 1087 | if (!AllowRebuild) |
| 1088 | return 0; |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1089 | |
| 1090 | // We can't reuse the previously-computed preamble. Build a new one. |
| 1091 | Preamble.clear(); |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1092 | llvm::sys::Path(PreambleFile).eraseFromDisk(); |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1093 | PreambleRebuildCounter = 1; |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1094 | } else if (!AllowRebuild) { |
| 1095 | // We aren't allowed to rebuild the precompiled preamble; just |
| 1096 | // return now. |
| 1097 | return 0; |
| 1098 | } |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1099 | |
| 1100 | // If the preamble rebuild counter > 1, it's because we previously |
| 1101 | // failed to build a preamble and we're not yet ready to try |
| 1102 | // again. Decrement the counter and return a failure. |
| 1103 | if (PreambleRebuildCounter > 1) { |
| 1104 | --PreambleRebuildCounter; |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
Douglas Gregor | 2cd4fd4 | 2010-09-11 17:56:52 +0000 | [diff] [blame] | 1108 | // Create a temporary file for the precompiled preamble. In rare |
| 1109 | // circumstances, this can fail. |
| 1110 | std::string PreamblePCHPath = GetPreamblePCHPath(); |
| 1111 | if (PreamblePCHPath.empty()) { |
| 1112 | // Try again next time. |
| 1113 | PreambleRebuildCounter = 1; |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1117 | // We did not previously compute a preamble, or it can't be reused anyway. |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1118 | llvm::Timer *PreambleTimer = 0; |
| 1119 | if (TimerGroup.get()) { |
| 1120 | PreambleTimer = new llvm::Timer("Precompiling preamble", *TimerGroup); |
| 1121 | PreambleTimer->startTimer(); |
| 1122 | Timers.push_back(PreambleTimer); |
| 1123 | } |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1124 | |
| 1125 | // Create a new buffer that stores the preamble. The buffer also contains |
| 1126 | // extra space for the original contents of the file (which will be present |
| 1127 | // when we actually parse the file) along with more room in case the file |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1128 | // grows. |
| 1129 | PreambleReservedSize = NewPreamble.first->getBufferSize(); |
| 1130 | if (PreambleReservedSize < 4096) |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1131 | PreambleReservedSize = 8191; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1132 | else |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1133 | PreambleReservedSize *= 2; |
| 1134 | |
Douglas Gregor | c0659ec | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1135 | // Save the preamble text for later; we'll need to compare against it for |
| 1136 | // subsequent reparses. |
| 1137 | Preamble.assign(NewPreamble.first->getBufferStart(), |
| 1138 | NewPreamble.first->getBufferStart() |
| 1139 | + NewPreamble.second.first); |
| 1140 | PreambleEndsAtStartOfLine = NewPreamble.second.second; |
| 1141 | |
Douglas Gregor | 671947b | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 1142 | delete PreambleBuffer; |
| 1143 | PreambleBuffer |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1144 | = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize, |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1145 | FrontendOpts.Inputs[0].second); |
| 1146 | memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()), |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1147 | NewPreamble.first->getBufferStart(), Preamble.size()); |
| 1148 | memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(), |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1149 | ' ', PreambleReservedSize - Preamble.size() - 1); |
| 1150 | const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n'; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1151 | |
| 1152 | // Remap the main source file to the preamble buffer. |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1153 | llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1154 | PreprocessorOpts.addRemappedFile(MainFilePath.str(), PreambleBuffer); |
| 1155 | |
| 1156 | // Tell the compiler invocation to generate a temporary precompiled header. |
| 1157 | FrontendOpts.ProgramAction = frontend::GeneratePCH; |
Sebastian Redl | f65339e | 2010-08-06 00:35:11 +0000 | [diff] [blame] | 1158 | // FIXME: Set ChainedPCH unconditionally, once it is ready. |
| 1159 | if (::getenv("LIBCLANG_CHAINING")) |
| 1160 | FrontendOpts.ChainedPCH = true; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1161 | // FIXME: Generate the precompiled header into memory? |
Douglas Gregor | 2cd4fd4 | 2010-09-11 17:56:52 +0000 | [diff] [blame] | 1162 | FrontendOpts.OutputFile = PreamblePCHPath; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1163 | |
| 1164 | // Create the compiler instance to use for building the precompiled preamble. |
| 1165 | CompilerInstance Clang; |
| 1166 | Clang.setInvocation(&PreambleInvocation); |
| 1167 | OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second; |
| 1168 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1169 | // Set up diagnostics, capturing all of the diagnostics produced. |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1170 | Clang.setDiagnostics(&getDiagnostics()); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1171 | CaptureDroppedDiagnostics Capture(CaptureDiagnostics, |
| 1172 | getDiagnostics(), |
| 1173 | StoredDiagnostics); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1174 | |
| 1175 | // Create the target instance. |
| 1176 | Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), |
| 1177 | Clang.getTargetOpts())); |
| 1178 | if (!Clang.hasTarget()) { |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1179 | llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); |
| 1180 | Preamble.clear(); |
| 1181 | if (CreatedPreambleBuffer) |
| 1182 | delete NewPreamble.first; |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1183 | if (PreambleTimer) |
| 1184 | PreambleTimer->stopTimer(); |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1185 | PreambleRebuildCounter = DefaultPreambleRebuildInterval; |
Douglas Gregor | 671947b | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 1186 | PreprocessorOpts.eraseRemappedFile( |
| 1187 | PreprocessorOpts.remapped_file_buffer_end() - 1); |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1188 | return 0; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | // Inform the target of the language options. |
| 1192 | // |
| 1193 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 1194 | // created. This complexity should be lifted elsewhere. |
| 1195 | Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); |
| 1196 | |
| 1197 | assert(Clang.getFrontendOpts().Inputs.size() == 1 && |
| 1198 | "Invocation must have exactly one source file!"); |
| 1199 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST && |
| 1200 | "FIXME: AST inputs not yet supported here!"); |
| 1201 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR && |
| 1202 | "IR inputs not support here!"); |
| 1203 | |
| 1204 | // Clear out old caches and data. |
| 1205 | StoredDiagnostics.clear(); |
Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1206 | TopLevelDecls.clear(); |
| 1207 | TopLevelDeclsInPreamble.clear(); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1208 | |
| 1209 | // Create a file manager object to provide access to and cache the filesystem. |
| 1210 | Clang.setFileManager(new FileManager); |
| 1211 | |
| 1212 | // Create the source manager. |
| 1213 | Clang.setSourceManager(new SourceManager(getDiagnostics())); |
| 1214 | |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 1215 | llvm::OwningPtr<PrecompilePreambleAction> Act; |
| 1216 | Act.reset(new PrecompilePreambleAction(*this)); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1217 | if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second, |
| 1218 | Clang.getFrontendOpts().Inputs[0].first)) { |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1219 | Clang.takeInvocation(); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1220 | llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); |
| 1221 | Preamble.clear(); |
| 1222 | if (CreatedPreambleBuffer) |
| 1223 | delete NewPreamble.first; |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1224 | if (PreambleTimer) |
| 1225 | PreambleTimer->stopTimer(); |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1226 | PreambleRebuildCounter = DefaultPreambleRebuildInterval; |
Douglas Gregor | 671947b | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 1227 | PreprocessorOpts.eraseRemappedFile( |
| 1228 | PreprocessorOpts.remapped_file_buffer_end() - 1); |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1229 | return 0; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | Act->Execute(); |
| 1233 | Act->EndSourceFile(); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1234 | Clang.takeInvocation(); |
| 1235 | |
Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1236 | if (Diagnostics->hasErrorOccurred()) { |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1237 | // There were errors parsing the preamble, so no precompiled header was |
| 1238 | // generated. Forget that we even tried. |
| 1239 | // FIXME: Should we leave a note for ourselves to try again? |
| 1240 | llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); |
| 1241 | Preamble.clear(); |
| 1242 | if (CreatedPreambleBuffer) |
| 1243 | delete NewPreamble.first; |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1244 | if (PreambleTimer) |
| 1245 | PreambleTimer->stopTimer(); |
Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1246 | TopLevelDeclsInPreamble.clear(); |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1247 | PreambleRebuildCounter = DefaultPreambleRebuildInterval; |
Douglas Gregor | 671947b | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 1248 | PreprocessorOpts.eraseRemappedFile( |
| 1249 | PreprocessorOpts.remapped_file_buffer_end() - 1); |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1250 | return 0; |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | // Keep track of the preamble we precompiled. |
| 1254 | PreambleFile = FrontendOpts.OutputFile; |
Douglas Gregor | c0659ec | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1255 | NumStoredDiagnosticsInPreamble = StoredDiagnostics.size(); |
| 1256 | NumWarningsInPreamble = getDiagnostics().getNumWarnings(); |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1257 | |
| 1258 | // Keep track of all of the files that the source manager knows about, |
| 1259 | // so we can verify whether they have changed or not. |
| 1260 | FilesInPreamble.clear(); |
| 1261 | SourceManager &SourceMgr = Clang.getSourceManager(); |
| 1262 | const llvm::MemoryBuffer *MainFileBuffer |
| 1263 | = SourceMgr.getBuffer(SourceMgr.getMainFileID()); |
| 1264 | for (SourceManager::fileinfo_iterator F = SourceMgr.fileinfo_begin(), |
| 1265 | FEnd = SourceMgr.fileinfo_end(); |
| 1266 | F != FEnd; |
| 1267 | ++F) { |
| 1268 | const FileEntry *File = F->second->Entry; |
| 1269 | if (!File || F->second->getRawBuffer() == MainFileBuffer) |
| 1270 | continue; |
| 1271 | |
| 1272 | FilesInPreamble[File->getName()] |
| 1273 | = std::make_pair(F->second->getSize(), File->getModificationTime()); |
| 1274 | } |
| 1275 | |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1276 | if (PreambleTimer) |
| 1277 | PreambleTimer->stopTimer(); |
| 1278 | |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1279 | PreambleRebuildCounter = 1; |
Douglas Gregor | 671947b | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 1280 | PreprocessorOpts.eraseRemappedFile( |
| 1281 | PreprocessorOpts.remapped_file_buffer_end() - 1); |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1282 | return CreatePaddedMainFileBuffer(NewPreamble.first, |
| 1283 | CreatedPreambleBuffer, |
| 1284 | PreambleReservedSize, |
| 1285 | FrontendOpts.Inputs[0].second); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1286 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1287 | |
Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1288 | void ASTUnit::RealizeTopLevelDeclsFromPreamble() { |
| 1289 | std::vector<Decl *> Resolved; |
| 1290 | Resolved.reserve(TopLevelDeclsInPreamble.size()); |
| 1291 | ExternalASTSource &Source = *getASTContext().getExternalSource(); |
| 1292 | for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) { |
| 1293 | // Resolve the declaration ID to an actual declaration, possibly |
| 1294 | // deserializing the declaration in the process. |
| 1295 | Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]); |
| 1296 | if (D) |
| 1297 | Resolved.push_back(D); |
| 1298 | } |
| 1299 | TopLevelDeclsInPreamble.clear(); |
| 1300 | TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end()); |
| 1301 | } |
| 1302 | |
| 1303 | unsigned ASTUnit::getMaxPCHLevel() const { |
| 1304 | if (!getOnlyLocalDecls()) |
| 1305 | return Decl::MaxPCHLevel; |
| 1306 | |
| 1307 | unsigned Result = 0; |
| 1308 | if (isMainFileAST() || SavedMainFileBuffer) |
| 1309 | ++Result; |
| 1310 | return Result; |
| 1311 | } |
| 1312 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1313 | ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI, |
| 1314 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags, |
| 1315 | bool OnlyLocalDecls, |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1316 | bool CaptureDiagnostics, |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1317 | bool PrecompilePreamble, |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1318 | bool CompleteTranslationUnit, |
| 1319 | bool CacheCodeCompletionResults) { |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1320 | if (!Diags.getPtr()) { |
| 1321 | // No diagnostics engine was provided, so create our own diagnostics object |
| 1322 | // with the default options. |
| 1323 | DiagnosticOptions DiagOpts; |
| 1324 | Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); |
| 1325 | } |
| 1326 | |
| 1327 | // Create the AST unit. |
| 1328 | llvm::OwningPtr<ASTUnit> AST; |
| 1329 | AST.reset(new ASTUnit(false)); |
| 1330 | AST->Diagnostics = Diags; |
| 1331 | AST->CaptureDiagnostics = CaptureDiagnostics; |
| 1332 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1333 | AST->CompleteTranslationUnit = CompleteTranslationUnit; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1334 | AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1335 | AST->Invocation.reset(CI); |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1336 | CI->getPreprocessorOpts().RetainRemappedFileBuffers = true; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1337 | |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1338 | if (getenv("LIBCLANG_TIMING")) |
| 1339 | AST->TimerGroup.reset( |
| 1340 | new llvm::TimerGroup(CI->getFrontendOpts().Inputs[0].second)); |
| 1341 | |
| 1342 | |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1343 | llvm::MemoryBuffer *OverrideMainBuffer = 0; |
Douglas Gregor | fd0b870 | 2010-07-28 22:12:37 +0000 | [diff] [blame] | 1344 | // FIXME: When C++ PCH is ready, allow use of it for a precompiled preamble. |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1345 | if (PrecompilePreamble && !CI->getLangOpts().CPlusPlus) { |
| 1346 | AST->PreambleRebuildCounter = 1; |
Douglas Gregor | 2283d79 | 2010-08-20 00:59:43 +0000 | [diff] [blame] | 1347 | OverrideMainBuffer |
| 1348 | = AST->getMainBufferWithPrecompiledPreamble(*AST->Invocation); |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1349 | } |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1350 | |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1351 | llvm::Timer *ParsingTimer = 0; |
| 1352 | if (AST->TimerGroup.get()) { |
| 1353 | ParsingTimer = new llvm::Timer("Initial parse", *AST->TimerGroup); |
| 1354 | ParsingTimer->startTimer(); |
| 1355 | AST->Timers.push_back(ParsingTimer); |
| 1356 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1357 | |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1358 | bool Failed = AST->Parse(OverrideMainBuffer); |
| 1359 | if (ParsingTimer) |
| 1360 | ParsingTimer->stopTimer(); |
| 1361 | |
| 1362 | return Failed? 0 : AST.take(); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1363 | } |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1364 | |
| 1365 | ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin, |
| 1366 | const char **ArgEnd, |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 1367 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags, |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 1368 | llvm::StringRef ResourceFilesPath, |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1369 | bool OnlyLocalDecls, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1370 | RemappedFile *RemappedFiles, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1371 | unsigned NumRemappedFiles, |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1372 | bool CaptureDiagnostics, |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1373 | bool PrecompilePreamble, |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1374 | bool CompleteTranslationUnit, |
| 1375 | bool CacheCodeCompletionResults) { |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 1376 | bool CreatedDiagnosticsObject = false; |
| 1377 | |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 1378 | if (!Diags.getPtr()) { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1379 | // No diagnostics engine was provided, so create our own diagnostics object |
| 1380 | // with the default options. |
| 1381 | DiagnosticOptions DiagOpts; |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 1382 | Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 1383 | CreatedDiagnosticsObject = true; |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1386 | llvm::SmallVector<const char *, 16> Args; |
| 1387 | Args.push_back("<clang>"); // FIXME: Remove dummy argument. |
| 1388 | Args.insert(Args.end(), ArgBegin, ArgEnd); |
| 1389 | |
| 1390 | // FIXME: Find a cleaner way to force the driver into restricted modes. We |
| 1391 | // also want to force it to use clang. |
| 1392 | Args.push_back("-fsyntax-only"); |
| 1393 | |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 1394 | // FIXME: We shouldn't have to pass in the path info. |
Daniel Dunbar | 0bbad51 | 2010-07-19 00:44:04 +0000 | [diff] [blame] | 1395 | driver::Driver TheDriver("clang", llvm::sys::getHostTriple(), |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1396 | "a.out", false, false, *Diags); |
Daniel Dunbar | 3bd54cc | 2010-01-25 00:44:02 +0000 | [diff] [blame] | 1397 | |
| 1398 | // Don't check that inputs exist, they have been remapped. |
| 1399 | TheDriver.setCheckInputsExist(false); |
| 1400 | |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1401 | llvm::OwningPtr<driver::Compilation> C( |
| 1402 | TheDriver.BuildCompilation(Args.size(), Args.data())); |
| 1403 | |
| 1404 | // We expect to get back exactly one command job, if we didn't something |
| 1405 | // failed. |
| 1406 | const driver::JobList &Jobs = C->getJobs(); |
| 1407 | if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) { |
| 1408 | llvm::SmallString<256> Msg; |
| 1409 | llvm::raw_svector_ostream OS(Msg); |
| 1410 | C->PrintJob(OS, C->getJobs(), "; ", true); |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1411 | Diags->Report(diag::err_fe_expected_compiler_job) << OS.str(); |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1412 | return 0; |
| 1413 | } |
| 1414 | |
| 1415 | const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin()); |
| 1416 | if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1417 | Diags->Report(diag::err_fe_expected_clang_command); |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1418 | return 0; |
| 1419 | } |
| 1420 | |
| 1421 | const driver::ArgStringList &CCArgs = Cmd->getArguments(); |
Daniel Dunbar | 807b061 | 2010-01-30 21:47:16 +0000 | [diff] [blame] | 1422 | llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation); |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 1423 | CompilerInvocation::CreateFromArgs(*CI, |
| 1424 | const_cast<const char **>(CCArgs.data()), |
| 1425 | const_cast<const char **>(CCArgs.data()) + |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1426 | CCArgs.size(), |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1427 | *Diags); |
Daniel Dunbar | 1e69fe3 | 2009-12-13 03:45:58 +0000 | [diff] [blame] | 1428 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1429 | // Override any files that need remapping |
| 1430 | for (unsigned I = 0; I != NumRemappedFiles; ++I) |
Daniel Dunbar | 807b061 | 2010-01-30 21:47:16 +0000 | [diff] [blame] | 1431 | CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, |
Daniel Dunbar | b26d483 | 2010-02-16 01:55:04 +0000 | [diff] [blame] | 1432 | RemappedFiles[I].second); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1433 | |
Daniel Dunbar | 8b9adfe | 2009-12-15 00:06:45 +0000 | [diff] [blame] | 1434 | // Override the resources path. |
Daniel Dunbar | 807b061 | 2010-01-30 21:47:16 +0000 | [diff] [blame] | 1435 | CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath; |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1436 | |
Douglas Gregor | f421089 | 2010-08-18 23:38:21 +0000 | [diff] [blame] | 1437 | CI->getFrontendOpts().DisableFree = false; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1438 | return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls, |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1439 | CaptureDiagnostics, PrecompilePreamble, |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1440 | CompleteTranslationUnit, |
| 1441 | CacheCodeCompletionResults); |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1442 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1443 | |
| 1444 | bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) { |
| 1445 | if (!Invocation.get()) |
| 1446 | return true; |
| 1447 | |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1448 | llvm::Timer *ReparsingTimer = 0; |
| 1449 | if (TimerGroup.get()) { |
| 1450 | ReparsingTimer = new llvm::Timer("Reparse", *TimerGroup); |
| 1451 | ReparsingTimer->startTimer(); |
| 1452 | Timers.push_back(ReparsingTimer); |
| 1453 | } |
| 1454 | |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1455 | // Remap files. |
Douglas Gregor | f128fed | 2010-08-20 00:02:33 +0000 | [diff] [blame] | 1456 | PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); |
| 1457 | for (PreprocessorOptions::remapped_file_buffer_iterator |
| 1458 | R = PPOpts.remapped_file_buffer_begin(), |
| 1459 | REnd = PPOpts.remapped_file_buffer_end(); |
| 1460 | R != REnd; |
| 1461 | ++R) { |
| 1462 | delete R->second; |
| 1463 | } |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1464 | Invocation->getPreprocessorOpts().clearRemappedFiles(); |
| 1465 | for (unsigned I = 0; I != NumRemappedFiles; ++I) |
| 1466 | Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, |
| 1467 | RemappedFiles[I].second); |
| 1468 | |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1469 | // If we have a preamble file lying around, or if we might try to |
| 1470 | // build a precompiled preamble, do so now. |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1471 | llvm::MemoryBuffer *OverrideMainBuffer = 0; |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1472 | if (!PreambleFile.empty() || PreambleRebuildCounter > 0) |
Douglas Gregor | 2283d79 | 2010-08-20 00:59:43 +0000 | [diff] [blame] | 1473 | OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1474 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1475 | // Clear out the diagnostics state. |
Douglas Gregor | c0659ec | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1476 | if (!OverrideMainBuffer) |
| 1477 | getDiagnostics().Reset(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1478 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1479 | // Parse the sources |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1480 | bool Result = Parse(OverrideMainBuffer); |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1481 | if (ReparsingTimer) |
| 1482 | ReparsingTimer->stopTimer(); |
Douglas Gregor | 727d93e | 2010-08-17 00:40:40 +0000 | [diff] [blame] | 1483 | |
| 1484 | if (ShouldCacheCodeCompletionResults) { |
| 1485 | if (CacheCodeCompletionCoolDown > 0) |
| 1486 | --CacheCodeCompletionCoolDown; |
| 1487 | else if (top_level_size() != NumTopLevelDeclsAtLastCompletionCache) |
| 1488 | CacheCodeCompletionResults(); |
| 1489 | } |
| 1490 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1491 | return Result; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1492 | } |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1493 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1494 | //----------------------------------------------------------------------------// |
| 1495 | // Code completion |
| 1496 | //----------------------------------------------------------------------------// |
| 1497 | |
| 1498 | namespace { |
| 1499 | /// \brief Code completion consumer that combines the cached code-completion |
| 1500 | /// results from an ASTUnit with the code-completion results provided to it, |
| 1501 | /// then passes the result on to |
| 1502 | class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer { |
| 1503 | unsigned NormalContexts; |
| 1504 | ASTUnit &AST; |
| 1505 | CodeCompleteConsumer &Next; |
| 1506 | |
| 1507 | public: |
| 1508 | AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 1509 | bool IncludeMacros, bool IncludeCodePatterns, |
| 1510 | bool IncludeGlobals) |
| 1511 | : CodeCompleteConsumer(IncludeMacros, IncludeCodePatterns, IncludeGlobals, |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1512 | Next.isOutputBinary()), AST(AST), Next(Next) |
| 1513 | { |
| 1514 | // Compute the set of contexts in which we will look when we don't have |
| 1515 | // any information about the specific context. |
| 1516 | NormalContexts |
| 1517 | = (1 << (CodeCompletionContext::CCC_TopLevel - 1)) |
| 1518 | | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1)) |
| 1519 | | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1)) |
| 1520 | | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1)) |
| 1521 | | (1 << (CodeCompletionContext::CCC_Statement - 1)) |
| 1522 | | (1 << (CodeCompletionContext::CCC_Expression - 1)) |
| 1523 | | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)) |
| 1524 | | (1 << (CodeCompletionContext::CCC_MemberAccess - 1)) |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1525 | | (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1)) |
| 1526 | | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1)); |
| 1527 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1528 | if (AST.getASTContext().getLangOptions().CPlusPlus) |
| 1529 | NormalContexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1)) |
| 1530 | | (1 << (CodeCompletionContext::CCC_UnionTag - 1)) |
| 1531 | | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1)); |
| 1532 | } |
| 1533 | |
| 1534 | virtual void ProcessCodeCompleteResults(Sema &S, |
| 1535 | CodeCompletionContext Context, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1536 | CodeCompletionResult *Results, |
Douglas Gregor | 697ca6d | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1537 | unsigned NumResults); |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1538 | |
| 1539 | virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, |
| 1540 | OverloadCandidate *Candidates, |
| 1541 | unsigned NumCandidates) { |
| 1542 | Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates); |
| 1543 | } |
| 1544 | }; |
| 1545 | } |
Douglas Gregor | 697ca6d | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1546 | |
Douglas Gregor | 5f808c2 | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1547 | /// \brief Helper function that computes which global names are hidden by the |
| 1548 | /// local code-completion results. |
| 1549 | void CalculateHiddenNames(const CodeCompletionContext &Context, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1550 | CodeCompletionResult *Results, |
Douglas Gregor | 5f808c2 | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1551 | unsigned NumResults, |
| 1552 | ASTContext &Ctx, |
| 1553 | llvm::StringSet<> &HiddenNames) { |
| 1554 | bool OnlyTagNames = false; |
| 1555 | switch (Context.getKind()) { |
| 1556 | case CodeCompletionContext::CCC_Other: |
| 1557 | case CodeCompletionContext::CCC_TopLevel: |
| 1558 | case CodeCompletionContext::CCC_ObjCInterface: |
| 1559 | case CodeCompletionContext::CCC_ObjCImplementation: |
| 1560 | case CodeCompletionContext::CCC_ObjCIvarList: |
| 1561 | case CodeCompletionContext::CCC_ClassStructUnion: |
| 1562 | case CodeCompletionContext::CCC_Statement: |
| 1563 | case CodeCompletionContext::CCC_Expression: |
| 1564 | case CodeCompletionContext::CCC_ObjCMessageReceiver: |
| 1565 | case CodeCompletionContext::CCC_MemberAccess: |
| 1566 | case CodeCompletionContext::CCC_Namespace: |
| 1567 | case CodeCompletionContext::CCC_Type: |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1568 | case CodeCompletionContext::CCC_Name: |
| 1569 | case CodeCompletionContext::CCC_PotentiallyQualifiedName: |
Douglas Gregor | 0268810 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1570 | case CodeCompletionContext::CCC_ParenthesizedExpression: |
Douglas Gregor | 5f808c2 | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1571 | break; |
| 1572 | |
| 1573 | case CodeCompletionContext::CCC_EnumTag: |
| 1574 | case CodeCompletionContext::CCC_UnionTag: |
| 1575 | case CodeCompletionContext::CCC_ClassOrStructTag: |
| 1576 | OnlyTagNames = true; |
| 1577 | break; |
| 1578 | |
| 1579 | case CodeCompletionContext::CCC_ObjCProtocolName: |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 1580 | case CodeCompletionContext::CCC_MacroName: |
| 1581 | case CodeCompletionContext::CCC_MacroNameUse: |
Douglas Gregor | f29c523 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 1582 | case CodeCompletionContext::CCC_PreprocessorExpression: |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 1583 | case CodeCompletionContext::CCC_PreprocessorDirective: |
Douglas Gregor | 59a6694 | 2010-08-25 18:04:30 +0000 | [diff] [blame] | 1584 | case CodeCompletionContext::CCC_NaturalLanguage: |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 1585 | case CodeCompletionContext::CCC_SelectorName: |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 1586 | case CodeCompletionContext::CCC_TypeQualifiers: |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 1587 | // We're looking for nothing, or we're looking for names that cannot |
| 1588 | // be hidden. |
Douglas Gregor | 5f808c2 | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1589 | return; |
| 1590 | } |
| 1591 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1592 | typedef CodeCompletionResult Result; |
Douglas Gregor | 5f808c2 | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1593 | for (unsigned I = 0; I != NumResults; ++I) { |
| 1594 | if (Results[I].Kind != Result::RK_Declaration) |
| 1595 | continue; |
| 1596 | |
| 1597 | unsigned IDNS |
| 1598 | = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace(); |
| 1599 | |
| 1600 | bool Hiding = false; |
| 1601 | if (OnlyTagNames) |
| 1602 | Hiding = (IDNS & Decl::IDNS_Tag); |
| 1603 | else { |
| 1604 | unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member | |
Douglas Gregor | a5fb7c3 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 1605 | Decl::IDNS_Namespace | Decl::IDNS_Ordinary | |
| 1606 | Decl::IDNS_NonMemberOperator); |
Douglas Gregor | 5f808c2 | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1607 | if (Ctx.getLangOptions().CPlusPlus) |
| 1608 | HiddenIDNS |= Decl::IDNS_Tag; |
| 1609 | Hiding = (IDNS & HiddenIDNS); |
| 1610 | } |
| 1611 | |
| 1612 | if (!Hiding) |
| 1613 | continue; |
| 1614 | |
| 1615 | DeclarationName Name = Results[I].Declaration->getDeclName(); |
| 1616 | if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo()) |
| 1617 | HiddenNames.insert(Identifier->getName()); |
| 1618 | else |
| 1619 | HiddenNames.insert(Name.getAsString()); |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | |
Douglas Gregor | 697ca6d | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1624 | void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S, |
| 1625 | CodeCompletionContext Context, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1626 | CodeCompletionResult *Results, |
Douglas Gregor | 697ca6d | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1627 | unsigned NumResults) { |
| 1628 | // Merge the results we were given with the results we cached. |
| 1629 | bool AddedResult = false; |
Douglas Gregor | 5f808c2 | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1630 | unsigned InContexts |
| 1631 | = (Context.getKind() == CodeCompletionContext::CCC_Other? NormalContexts |
| 1632 | : (1 << (Context.getKind() - 1))); |
| 1633 | |
| 1634 | // Contains the set of names that are hidden by "local" completion results. |
| 1635 | llvm::StringSet<> HiddenNames; |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 1636 | llvm::SmallVector<CodeCompletionString *, 4> StringsToDestroy; |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1637 | typedef CodeCompletionResult Result; |
Douglas Gregor | 697ca6d | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1638 | llvm::SmallVector<Result, 8> AllResults; |
| 1639 | for (ASTUnit::cached_completion_iterator |
Douglas Gregor | 5535d57 | 2010-08-16 21:23:13 +0000 | [diff] [blame] | 1640 | C = AST.cached_completion_begin(), |
| 1641 | CEnd = AST.cached_completion_end(); |
Douglas Gregor | 697ca6d | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1642 | C != CEnd; ++C) { |
| 1643 | // If the context we are in matches any of the contexts we are |
| 1644 | // interested in, we'll add this result. |
| 1645 | if ((C->ShowInContexts & InContexts) == 0) |
| 1646 | continue; |
| 1647 | |
| 1648 | // If we haven't added any results previously, do so now. |
| 1649 | if (!AddedResult) { |
Douglas Gregor | 5f808c2 | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1650 | CalculateHiddenNames(Context, Results, NumResults, S.Context, |
| 1651 | HiddenNames); |
Douglas Gregor | 697ca6d | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1652 | AllResults.insert(AllResults.end(), Results, Results + NumResults); |
| 1653 | AddedResult = true; |
| 1654 | } |
| 1655 | |
Douglas Gregor | 5f808c2 | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1656 | // Determine whether this global completion result is hidden by a local |
| 1657 | // completion result. If so, skip it. |
| 1658 | if (C->Kind != CXCursor_MacroDefinition && |
| 1659 | HiddenNames.count(C->Completion->getTypedText())) |
| 1660 | continue; |
| 1661 | |
Douglas Gregor | 697ca6d | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1662 | // Adjust priority based on similar type classes. |
| 1663 | unsigned Priority = C->Priority; |
Douglas Gregor | 4125c37 | 2010-08-25 18:03:13 +0000 | [diff] [blame] | 1664 | CXCursorKind CursorKind = C->Kind; |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 1665 | CodeCompletionString *Completion = C->Completion; |
Douglas Gregor | 697ca6d | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1666 | if (!Context.getPreferredType().isNull()) { |
| 1667 | if (C->Kind == CXCursor_MacroDefinition) { |
| 1668 | Priority = getMacroUsagePriority(C->Completion->getTypedText(), |
Douglas Gregor | b05496d | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 1669 | S.getLangOptions(), |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 1670 | Context.getPreferredType()->isAnyPointerType()); |
Douglas Gregor | 697ca6d | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1671 | } else if (C->Type) { |
| 1672 | CanQualType Expected |
Douglas Gregor | 5535d57 | 2010-08-16 21:23:13 +0000 | [diff] [blame] | 1673 | = S.Context.getCanonicalType( |
Douglas Gregor | 697ca6d | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1674 | Context.getPreferredType().getUnqualifiedType()); |
| 1675 | SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected); |
| 1676 | if (ExpectedSTC == C->TypeClass) { |
| 1677 | // We know this type is similar; check for an exact match. |
| 1678 | llvm::StringMap<unsigned> &CachedCompletionTypes |
Douglas Gregor | 5535d57 | 2010-08-16 21:23:13 +0000 | [diff] [blame] | 1679 | = AST.getCachedCompletionTypes(); |
Douglas Gregor | 697ca6d | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1680 | llvm::StringMap<unsigned>::iterator Pos |
Douglas Gregor | 5535d57 | 2010-08-16 21:23:13 +0000 | [diff] [blame] | 1681 | = CachedCompletionTypes.find(QualType(Expected).getAsString()); |
Douglas Gregor | 697ca6d | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1682 | if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type) |
| 1683 | Priority /= CCF_ExactTypeMatch; |
| 1684 | else |
| 1685 | Priority /= CCF_SimilarTypeMatch; |
| 1686 | } |
| 1687 | } |
| 1688 | } |
| 1689 | |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 1690 | // Adjust the completion string, if required. |
| 1691 | if (C->Kind == CXCursor_MacroDefinition && |
| 1692 | Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) { |
| 1693 | // Create a new code-completion string that just contains the |
| 1694 | // macro name, without its arguments. |
| 1695 | Completion = new CodeCompletionString; |
| 1696 | Completion->AddTypedTextChunk(C->Completion->getTypedText()); |
| 1697 | StringsToDestroy.push_back(Completion); |
Douglas Gregor | 4125c37 | 2010-08-25 18:03:13 +0000 | [diff] [blame] | 1698 | CursorKind = CXCursor_NotImplemented; |
| 1699 | Priority = CCP_CodePattern; |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Douglas Gregor | 4125c37 | 2010-08-25 18:03:13 +0000 | [diff] [blame] | 1702 | AllResults.push_back(Result(Completion, Priority, CursorKind, |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 1703 | C->Availability)); |
Douglas Gregor | 697ca6d | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | // If we did not add any cached completion results, just forward the |
| 1707 | // results we were given to the next consumer. |
| 1708 | if (!AddedResult) { |
| 1709 | Next.ProcessCodeCompleteResults(S, Context, Results, NumResults); |
| 1710 | return; |
| 1711 | } |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 1712 | |
Douglas Gregor | 697ca6d | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1713 | Next.ProcessCodeCompleteResults(S, Context, AllResults.data(), |
| 1714 | AllResults.size()); |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 1715 | |
| 1716 | for (unsigned I = 0, N = StringsToDestroy.size(); I != N; ++I) |
| 1717 | delete StringsToDestroy[I]; |
Douglas Gregor | 697ca6d | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | |
| 1721 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1722 | void ASTUnit::CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column, |
| 1723 | RemappedFile *RemappedFiles, |
| 1724 | unsigned NumRemappedFiles, |
Douglas Gregor | cee235c | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 1725 | bool IncludeMacros, |
| 1726 | bool IncludeCodePatterns, |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1727 | CodeCompleteConsumer &Consumer, |
| 1728 | Diagnostic &Diag, LangOptions &LangOpts, |
| 1729 | SourceManager &SourceMgr, FileManager &FileMgr, |
Douglas Gregor | 2283d79 | 2010-08-20 00:59:43 +0000 | [diff] [blame] | 1730 | llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics, |
| 1731 | llvm::SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) { |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1732 | if (!Invocation.get()) |
| 1733 | return; |
| 1734 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1735 | llvm::Timer *CompletionTimer = 0; |
| 1736 | if (TimerGroup.get()) { |
| 1737 | llvm::SmallString<128> TimerName; |
| 1738 | llvm::raw_svector_ostream TimerNameOut(TimerName); |
| 1739 | TimerNameOut << "Code completion @ " << File << ":" << Line << ":" |
| 1740 | << Column; |
| 1741 | CompletionTimer = new llvm::Timer(TimerNameOut.str(), *TimerGroup); |
| 1742 | CompletionTimer->startTimer(); |
| 1743 | Timers.push_back(CompletionTimer); |
| 1744 | } |
| 1745 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1746 | CompilerInvocation CCInvocation(*Invocation); |
| 1747 | FrontendOptions &FrontendOpts = CCInvocation.getFrontendOpts(); |
| 1748 | PreprocessorOptions &PreprocessorOpts = CCInvocation.getPreprocessorOpts(); |
Douglas Gregor | cee235c | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 1749 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1750 | FrontendOpts.ShowMacrosInCodeCompletion |
| 1751 | = IncludeMacros && CachedCompletionResults.empty(); |
Douglas Gregor | cee235c | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 1752 | FrontendOpts.ShowCodePatternsInCodeCompletion = IncludeCodePatterns; |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 1753 | FrontendOpts.ShowGlobalSymbolsInCodeCompletion |
| 1754 | = CachedCompletionResults.empty(); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1755 | FrontendOpts.CodeCompletionAt.FileName = File; |
| 1756 | FrontendOpts.CodeCompletionAt.Line = Line; |
| 1757 | FrontendOpts.CodeCompletionAt.Column = Column; |
| 1758 | |
Douglas Gregor | cee235c | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 1759 | // Turn on spell-checking when performing code completion. It leads |
| 1760 | // to better results. |
| 1761 | unsigned SpellChecking = CCInvocation.getLangOpts().SpellChecking; |
| 1762 | CCInvocation.getLangOpts().SpellChecking = 1; |
| 1763 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1764 | // Set the language options appropriately. |
| 1765 | LangOpts = CCInvocation.getLangOpts(); |
| 1766 | |
| 1767 | CompilerInstance Clang; |
| 1768 | Clang.setInvocation(&CCInvocation); |
| 1769 | OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second; |
| 1770 | |
| 1771 | // Set up diagnostics, capturing any diagnostics produced. |
| 1772 | Clang.setDiagnostics(&Diag); |
| 1773 | CaptureDroppedDiagnostics Capture(true, |
| 1774 | Clang.getDiagnostics(), |
| 1775 | StoredDiagnostics); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1776 | |
| 1777 | // Create the target instance. |
| 1778 | Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), |
| 1779 | Clang.getTargetOpts())); |
| 1780 | if (!Clang.hasTarget()) { |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1781 | Clang.takeInvocation(); |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 1782 | CCInvocation.getLangOpts().SpellChecking = SpellChecking; |
| 1783 | return; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | // Inform the target of the language options. |
| 1787 | // |
| 1788 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 1789 | // created. This complexity should be lifted elsewhere. |
| 1790 | Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); |
| 1791 | |
| 1792 | assert(Clang.getFrontendOpts().Inputs.size() == 1 && |
| 1793 | "Invocation must have exactly one source file!"); |
| 1794 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST && |
| 1795 | "FIXME: AST inputs not yet supported here!"); |
| 1796 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR && |
| 1797 | "IR inputs not support here!"); |
| 1798 | |
| 1799 | |
| 1800 | // Use the source and file managers that we were given. |
| 1801 | Clang.setFileManager(&FileMgr); |
| 1802 | Clang.setSourceManager(&SourceMgr); |
| 1803 | |
| 1804 | // Remap files. |
| 1805 | PreprocessorOpts.clearRemappedFiles(); |
Douglas Gregor | b75d3df | 2010-08-04 17:07:00 +0000 | [diff] [blame] | 1806 | PreprocessorOpts.RetainRemappedFileBuffers = true; |
Douglas Gregor | 2283d79 | 2010-08-20 00:59:43 +0000 | [diff] [blame] | 1807 | for (unsigned I = 0; I != NumRemappedFiles; ++I) { |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1808 | PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, |
| 1809 | RemappedFiles[I].second); |
Douglas Gregor | 2283d79 | 2010-08-20 00:59:43 +0000 | [diff] [blame] | 1810 | OwnedBuffers.push_back(RemappedFiles[I].second); |
| 1811 | } |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1812 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1813 | // Use the code completion consumer we were given, but adding any cached |
| 1814 | // code-completion results. |
| 1815 | AugmentedCodeCompleteConsumer |
| 1816 | AugmentedConsumer(*this, Consumer, FrontendOpts.ShowMacrosInCodeCompletion, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 1817 | FrontendOpts.ShowCodePatternsInCodeCompletion, |
| 1818 | FrontendOpts.ShowGlobalSymbolsInCodeCompletion); |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1819 | Clang.setCodeCompletionConsumer(&AugmentedConsumer); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1820 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1821 | // If we have a precompiled preamble, try to use it. We only allow |
| 1822 | // the use of the precompiled preamble if we're if the completion |
| 1823 | // point is within the main file, after the end of the precompiled |
| 1824 | // preamble. |
| 1825 | llvm::MemoryBuffer *OverrideMainBuffer = 0; |
| 1826 | if (!PreambleFile.empty()) { |
| 1827 | using llvm::sys::FileStatus; |
| 1828 | llvm::sys::PathWithStatus CompleteFilePath(File); |
| 1829 | llvm::sys::PathWithStatus MainPath(OriginalSourceFile); |
| 1830 | if (const FileStatus *CompleteFileStatus = CompleteFilePath.getFileStatus()) |
| 1831 | if (const FileStatus *MainStatus = MainPath.getFileStatus()) |
| 1832 | if (CompleteFileStatus->getUniqueID() == MainStatus->getUniqueID()) |
Douglas Gregor | 2283d79 | 2010-08-20 00:59:43 +0000 | [diff] [blame] | 1833 | OverrideMainBuffer |
Douglas Gregor | c9c29a8 | 2010-08-25 18:04:15 +0000 | [diff] [blame] | 1834 | = getMainBufferWithPrecompiledPreamble(CCInvocation, false, |
| 1835 | Line - 1); |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | // If the main file has been overridden due to the use of a preamble, |
| 1839 | // make that override happen and introduce the preamble. |
| 1840 | if (OverrideMainBuffer) { |
| 1841 | PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer); |
| 1842 | PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size(); |
| 1843 | PreprocessorOpts.PrecompiledPreambleBytes.second |
| 1844 | = PreambleEndsAtStartOfLine; |
| 1845 | PreprocessorOpts.ImplicitPCHInclude = PreambleFile; |
| 1846 | PreprocessorOpts.DisablePCHValidation = true; |
| 1847 | |
| 1848 | // The stored diagnostics have the old source manager. Copy them |
| 1849 | // to our output set of stored diagnostics, updating the source |
| 1850 | // manager to the one we were given. |
| 1851 | for (unsigned I = 0, N = this->StoredDiagnostics.size(); I != N; ++I) { |
| 1852 | StoredDiagnostics.push_back(this->StoredDiagnostics[I]); |
| 1853 | FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), SourceMgr); |
| 1854 | StoredDiagnostics[I].setLocation(Loc); |
| 1855 | } |
Douglas Gregor | 2283d79 | 2010-08-20 00:59:43 +0000 | [diff] [blame] | 1856 | |
| 1857 | OwnedBuffers.push_back(OverrideMainBuffer); |
Douglas Gregor | f128fed | 2010-08-20 00:02:33 +0000 | [diff] [blame] | 1858 | } else { |
| 1859 | PreprocessorOpts.PrecompiledPreambleBytes.first = 0; |
| 1860 | PreprocessorOpts.PrecompiledPreambleBytes.second = false; |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1863 | llvm::OwningPtr<SyntaxOnlyAction> Act; |
| 1864 | Act.reset(new SyntaxOnlyAction); |
| 1865 | if (Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second, |
| 1866 | Clang.getFrontendOpts().Inputs[0].first)) { |
| 1867 | Act->Execute(); |
| 1868 | Act->EndSourceFile(); |
| 1869 | } |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1870 | |
| 1871 | if (CompletionTimer) |
| 1872 | CompletionTimer->stopTimer(); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1873 | |
| 1874 | // Steal back our resources. |
| 1875 | Clang.takeFileManager(); |
| 1876 | Clang.takeSourceManager(); |
| 1877 | Clang.takeInvocation(); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1878 | Clang.takeCodeCompletionConsumer(); |
Douglas Gregor | cee235c | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 1879 | CCInvocation.getLangOpts().SpellChecking = SpellChecking; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1880 | } |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 1881 | |
| 1882 | bool ASTUnit::Save(llvm::StringRef File) { |
| 1883 | if (getDiagnostics().hasErrorOccurred()) |
| 1884 | return true; |
| 1885 | |
| 1886 | // FIXME: Can we somehow regenerate the stat cache here, or do we need to |
| 1887 | // unconditionally create a stat cache when we parse the file? |
| 1888 | std::string ErrorInfo; |
Benjamin Kramer | 1395c5d | 2010-08-15 16:54:31 +0000 | [diff] [blame] | 1889 | llvm::raw_fd_ostream Out(File.str().c_str(), ErrorInfo, |
| 1890 | llvm::raw_fd_ostream::F_Binary); |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 1891 | if (!ErrorInfo.empty() || Out.has_error()) |
| 1892 | return true; |
| 1893 | |
| 1894 | std::vector<unsigned char> Buffer; |
| 1895 | llvm::BitstreamWriter Stream(Buffer); |
Sebastian Redl | a4232eb | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 1896 | ASTWriter Writer(Stream); |
| 1897 | Writer.WriteAST(getSema(), 0, 0); |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 1898 | |
| 1899 | // Write the generated bitstream to "Out". |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 1900 | if (!Buffer.empty()) |
| 1901 | Out.write((char *)&Buffer.front(), Buffer.size()); |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 1902 | Out.close(); |
| 1903 | return Out.has_error(); |
| 1904 | } |