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