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