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