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