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