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