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