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