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