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