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