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