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