Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1 | //===--- PCHReader.cpp - Precompiled Headers Reader -------------*- C++ -*-===// |
| 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 | // This file defines the PCHReader class, which reads a precompiled header. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4c6f952 | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 13 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/PCHReader.h" |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/FrontendDiagnostic.h" |
Sebastian Redl | 30c514c | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/PCHDeserializationListener.h" |
Daniel Dunbar | c716293 | 2009-11-11 23:58:53 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/Utils.h" |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 18 | #include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 | #include "clang/AST/Type.h" |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 23 | #include "clang/AST/TypeLocVisitor.h" |
Chris Lattner | 42d42b5 | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 24 | #include "clang/Lex/MacroInfo.h" |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 25 | #include "clang/Lex/PreprocessingRecord.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 26 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 27 | #include "clang/Lex/HeaderSearch.h" |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 28 | #include "clang/Basic/OnDiskHashTable.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 29 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 30 | #include "clang/Basic/SourceManagerInternals.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 31 | #include "clang/Basic/FileManager.h" |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 32 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | 445e23e | 2009-10-05 21:07:28 +0000 | [diff] [blame] | 33 | #include "clang/Basic/Version.h" |
Daniel Dunbar | 2596e42 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 35 | #include "llvm/Bitcode/BitstreamReader.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MemoryBuffer.h" |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 37 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | d5b2197 | 2009-11-18 19:50:41 +0000 | [diff] [blame] | 38 | #include "llvm/System/Path.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 39 | #include <algorithm> |
Douglas Gregor | e721f95 | 2009-04-28 18:58:38 +0000 | [diff] [blame] | 40 | #include <iterator> |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 41 | #include <cstdio> |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 42 | #include <sys/stat.h> |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 43 | using namespace clang; |
| 44 | |
| 45 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 46 | // PCH reader validator implementation |
| 47 | //===----------------------------------------------------------------------===// |
| 48 | |
| 49 | PCHReaderListener::~PCHReaderListener() {} |
| 50 | |
| 51 | bool |
| 52 | PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) { |
| 53 | const LangOptions &PPLangOpts = PP.getLangOptions(); |
| 54 | #define PARSE_LANGOPT_BENIGN(Option) |
| 55 | #define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \ |
| 56 | if (PPLangOpts.Option != LangOpts.Option) { \ |
| 57 | Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \ |
| 58 | return true; \ |
| 59 | } |
| 60 | |
| 61 | PARSE_LANGOPT_BENIGN(Trigraphs); |
| 62 | PARSE_LANGOPT_BENIGN(BCPLComment); |
| 63 | PARSE_LANGOPT_BENIGN(DollarIdents); |
| 64 | PARSE_LANGOPT_BENIGN(AsmPreprocessor); |
| 65 | PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions); |
Chandler Carruth | eb5d7b7 | 2010-04-17 20:17:31 +0000 | [diff] [blame] | 66 | PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 67 | PARSE_LANGOPT_BENIGN(ImplicitInt); |
| 68 | PARSE_LANGOPT_BENIGN(Digraphs); |
| 69 | PARSE_LANGOPT_BENIGN(HexFloats); |
| 70 | PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99); |
| 71 | PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions); |
| 72 | PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus); |
| 73 | PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x); |
| 74 | PARSE_LANGOPT_BENIGN(CXXOperatorName); |
| 75 | PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c); |
| 76 | PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2); |
| 77 | PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi); |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 78 | PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2); |
Fariborz Jahanian | 4c9d8d0 | 2010-04-22 21:01:59 +0000 | [diff] [blame] | 79 | PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings, |
| 80 | diag::warn_pch_no_constant_cfstrings); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 81 | PARSE_LANGOPT_BENIGN(PascalStrings); |
| 82 | PARSE_LANGOPT_BENIGN(WritableStrings); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | PARSE_LANGOPT_IMPORTANT(LaxVectorConversions, |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 84 | diag::warn_pch_lax_vector_conversions); |
Nate Begeman | 69cfb9b | 2009-06-25 22:57:40 +0000 | [diff] [blame] | 85 | PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 86 | PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions); |
Daniel Dunbar | 7348288 | 2010-02-10 18:48:44 +0000 | [diff] [blame] | 87 | PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 88 | PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime); |
| 89 | PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding); |
| 90 | PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics, |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 92 | diag::warn_pch_thread_safe_statics); |
Daniel Dunbar | 5345c39 | 2009-09-03 04:54:28 +0000 | [diff] [blame] | 93 | PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 94 | PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks); |
| 95 | PARSE_LANGOPT_BENIGN(EmitAllDecls); |
| 96 | PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno); |
Chris Lattner | a4d7145 | 2010-06-26 21:25:03 +0000 | [diff] [blame] | 97 | PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | PARSE_LANGOPT_IMPORTANT(HeinousExtensions, |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 99 | diag::warn_pch_heinous_extensions); |
| 100 | // FIXME: Most of the options below are benign if the macro wasn't |
| 101 | // used. Unfortunately, this means that a PCH compiled without |
| 102 | // optimization can't be used with optimization turned on, even |
| 103 | // though the only thing that changes is whether __OPTIMIZE__ was |
| 104 | // defined... but if __OPTIMIZE__ never showed up in the header, it |
| 105 | // doesn't matter. We could consider making this some special kind |
| 106 | // of check. |
| 107 | PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize); |
| 108 | PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size); |
| 109 | PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static); |
| 110 | PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level); |
| 111 | PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline); |
| 112 | PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline); |
| 113 | PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control); |
| 114 | PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed); |
John Thompson | a6fda12 | 2009-11-05 20:14:16 +0000 | [diff] [blame] | 115 | PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 116 | if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | Reader.Diag(diag::warn_pch_gc_mode) |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 118 | << LangOpts.getGCMode() << PPLangOpts.getGCMode(); |
| 119 | return true; |
| 120 | } |
| 121 | PARSE_LANGOPT_BENIGN(getVisibilityMode()); |
Daniel Dunbar | ab8e281 | 2009-09-21 04:16:19 +0000 | [diff] [blame] | 122 | PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(), |
| 123 | diag::warn_pch_stack_protector); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 124 | PARSE_LANGOPT_BENIGN(InstantiationDepth); |
Nate Begeman | 69cfb9b | 2009-06-25 22:57:40 +0000 | [diff] [blame] | 125 | PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl); |
Mike Stump | 9c276ae | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 126 | PARSE_LANGOPT_BENIGN(CatchUndefined); |
Daniel Dunbar | ab8e281 | 2009-09-21 04:16:19 +0000 | [diff] [blame] | 127 | PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors); |
Douglas Gregor | a0068fc | 2010-07-09 17:35:33 +0000 | [diff] [blame] | 128 | PARSE_LANGOPT_BENIGN(SpellChecking); |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 129 | #undef PARSE_LANGOPT_IMPORTANT |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 130 | #undef PARSE_LANGOPT_BENIGN |
| 131 | |
| 132 | return false; |
| 133 | } |
| 134 | |
Daniel Dunbar | dc3c0d2 | 2009-11-11 00:52:11 +0000 | [diff] [blame] | 135 | bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) { |
| 136 | if (Triple == PP.getTargetInfo().getTriple().str()) |
| 137 | return false; |
| 138 | |
| 139 | Reader.Diag(diag::warn_pch_target_triple) |
| 140 | << Triple << PP.getTargetInfo().getTriple().str(); |
| 141 | return true; |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Sebastian Redl | 7e9ad8b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 144 | struct EmptyStringRef { |
Benjamin Kramer | ec1b1cc | 2010-07-14 23:19:41 +0000 | [diff] [blame] | 145 | bool operator ()(llvm::StringRef r) const { return r.empty(); } |
Sebastian Redl | 7e9ad8b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 146 | }; |
| 147 | struct EmptyBlock { |
| 148 | bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); } |
| 149 | }; |
| 150 | |
| 151 | static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L, |
| 152 | PCHPredefinesBlocks R) { |
| 153 | // First, sum up the lengths. |
| 154 | unsigned LL = 0, RL = 0; |
| 155 | for (unsigned I = 0, N = L.size(); I != N; ++I) { |
| 156 | LL += L[I].size(); |
| 157 | } |
| 158 | for (unsigned I = 0, N = R.size(); I != N; ++I) { |
| 159 | RL += R[I].Data.size(); |
| 160 | } |
| 161 | if (LL != RL) |
| 162 | return false; |
| 163 | if (LL == 0 && RL == 0) |
| 164 | return true; |
| 165 | |
| 166 | // Kick out empty parts, they confuse the algorithm below. |
| 167 | L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end()); |
| 168 | R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end()); |
| 169 | |
| 170 | // Do it the hard way. At this point, both vectors must be non-empty. |
| 171 | llvm::StringRef LR = L[0], RR = R[0].Data; |
| 172 | unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size(); |
Daniel Dunbar | c76c9e0 | 2010-07-16 00:00:11 +0000 | [diff] [blame] | 173 | (void) RN; |
Sebastian Redl | 7e9ad8b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 174 | for (;;) { |
| 175 | // Compare the current pieces. |
| 176 | if (LR.size() == RR.size()) { |
| 177 | // If they're the same length, it's pretty easy. |
| 178 | if (LR != RR) |
| 179 | return false; |
| 180 | // Both pieces are done, advance. |
| 181 | ++LI; |
| 182 | ++RI; |
| 183 | // If either string is done, they're both done, since they're the same |
| 184 | // length. |
| 185 | if (LI == LN) { |
| 186 | assert(RI == RN && "Strings not the same length after all?"); |
| 187 | return true; |
| 188 | } |
| 189 | LR = L[LI]; |
| 190 | RR = R[RI].Data; |
| 191 | } else if (LR.size() < RR.size()) { |
| 192 | // Right piece is longer. |
| 193 | if (!RR.startswith(LR)) |
| 194 | return false; |
| 195 | ++LI; |
| 196 | assert(LI != LN && "Strings not the same length after all?"); |
| 197 | RR = RR.substr(LR.size()); |
| 198 | LR = L[LI]; |
| 199 | } else { |
| 200 | // Left piece is longer. |
| 201 | if (!LR.startswith(RR)) |
| 202 | return false; |
| 203 | ++RI; |
| 204 | assert(RI != RN && "Strings not the same length after all?"); |
| 205 | LR = LR.substr(RR.size()); |
| 206 | RR = R[RI].Data; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | static std::pair<FileID, llvm::StringRef::size_type> |
| 212 | FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) { |
| 213 | std::pair<FileID, llvm::StringRef::size_type> Res; |
| 214 | for (unsigned I = 0, N = Buffers.size(); I != N; ++I) { |
| 215 | Res.second = Buffers[I].Data.find(MacroDef); |
| 216 | if (Res.second != llvm::StringRef::npos) { |
| 217 | Res.first = Buffers[I].BufferID; |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | return Res; |
| 222 | } |
| 223 | |
| 224 | bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers, |
Daniel Dunbar | 7b5a121 | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 225 | llvm::StringRef OriginalFileName, |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 226 | std::string &SuggestedPredefines) { |
Daniel Dunbar | c716293 | 2009-11-11 23:58:53 +0000 | [diff] [blame] | 227 | // We are in the context of an implicit include, so the predefines buffer will |
| 228 | // have a #include entry for the PCH file itself (as normalized by the |
| 229 | // preprocessor initialization). Find it and skip over it in the checking |
| 230 | // below. |
Daniel Dunbar | 7b5a121 | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 231 | llvm::SmallString<256> PCHInclude; |
| 232 | PCHInclude += "#include \""; |
Daniel Dunbar | c716293 | 2009-11-11 23:58:53 +0000 | [diff] [blame] | 233 | PCHInclude += NormalizeDashIncludePath(OriginalFileName); |
Daniel Dunbar | 7b5a121 | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 234 | PCHInclude += "\"\n"; |
| 235 | std::pair<llvm::StringRef,llvm::StringRef> Split = |
| 236 | llvm::StringRef(PP.getPredefines()).split(PCHInclude.str()); |
| 237 | llvm::StringRef Left = Split.first, Right = Split.second; |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 238 | if (Left == PP.getPredefines()) { |
| 239 | Error("Missing PCH include entry!"); |
| 240 | return true; |
| 241 | } |
Daniel Dunbar | 7b5a121 | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 242 | |
Sebastian Redl | 7e9ad8b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 243 | // If the concatenation of all the PCH buffers is equal to the adjusted |
| 244 | // command line, we're done. |
| 245 | // We build a SmallVector of the command line here, because we'll eventually |
| 246 | // need to support an arbitrary amount of pieces anyway (when we have chained |
| 247 | // PCH reading). |
| 248 | llvm::SmallVector<llvm::StringRef, 2> CommandLine; |
| 249 | CommandLine.push_back(Left); |
| 250 | CommandLine.push_back(Right); |
| 251 | if (EqualConcatenations(CommandLine, Buffers)) |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 252 | return false; |
| 253 | |
| 254 | SourceManager &SourceMgr = PP.getSourceManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | |
Daniel Dunbar | 10014aa | 2009-11-11 03:45:59 +0000 | [diff] [blame] | 256 | // The predefines buffers are different. Determine what the differences are, |
| 257 | // and whether they require us to reject the PCH file. |
Daniel Dunbar | e675049 | 2009-11-13 16:46:11 +0000 | [diff] [blame] | 258 | llvm::SmallVector<llvm::StringRef, 8> PCHLines; |
Sebastian Redl | 7e9ad8b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 259 | for (unsigned I = 0, N = Buffers.size(); I != N; ++I) |
| 260 | Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
Daniel Dunbar | e675049 | 2009-11-13 16:46:11 +0000 | [diff] [blame] | 261 | |
| 262 | llvm::SmallVector<llvm::StringRef, 8> CmdLineLines; |
| 263 | Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
| 264 | Right.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 265 | |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 266 | // Sort both sets of predefined buffer lines, since we allow some extra |
| 267 | // definitions and they may appear at any point in the output. |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 268 | std::sort(CmdLineLines.begin(), CmdLineLines.end()); |
| 269 | std::sort(PCHLines.begin(), PCHLines.end()); |
| 270 | |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 271 | // Determine which predefines that were used to build the PCH file are missing |
| 272 | // from the command line. |
| 273 | std::vector<llvm::StringRef> MissingPredefines; |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 274 | std::set_difference(PCHLines.begin(), PCHLines.end(), |
| 275 | CmdLineLines.begin(), CmdLineLines.end(), |
| 276 | std::back_inserter(MissingPredefines)); |
| 277 | |
| 278 | bool MissingDefines = false; |
| 279 | bool ConflictingDefines = false; |
| 280 | for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) { |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 281 | llvm::StringRef Missing = MissingPredefines[I]; |
| 282 | if (!Missing.startswith("#define ")) { |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 283 | Reader.Diag(diag::warn_pch_compiler_options_mismatch); |
| 284 | return true; |
| 285 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | |
Daniel Dunbar | 10014aa | 2009-11-11 03:45:59 +0000 | [diff] [blame] | 287 | // This is a macro definition. Determine the name of the macro we're |
| 288 | // defining. |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 289 | std::string::size_type StartOfMacroName = strlen("#define "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | std::string::size_type EndOfMacroName |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 291 | = Missing.find_first_of("( \n\r", StartOfMacroName); |
| 292 | assert(EndOfMacroName != std::string::npos && |
| 293 | "Couldn't find the end of the macro name"); |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 294 | llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 295 | |
Daniel Dunbar | 10014aa | 2009-11-11 03:45:59 +0000 | [diff] [blame] | 296 | // Determine whether this macro was given a different definition on the |
| 297 | // command line. |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 298 | std::string MacroDefStart = "#define " + MacroName.str(); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 299 | std::string::size_type MacroDefLen = MacroDefStart.size(); |
Daniel Dunbar | e675049 | 2009-11-13 16:46:11 +0000 | [diff] [blame] | 300 | llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 301 | = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(), |
| 302 | MacroDefStart); |
| 303 | for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) { |
Daniel Dunbar | 10014aa | 2009-11-11 03:45:59 +0000 | [diff] [blame] | 304 | if (!ConflictPos->startswith(MacroDefStart)) { |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 305 | // Different macro; we're done. |
| 306 | ConflictPos = CmdLineLines.end(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | break; |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 308 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | |
| 310 | assert(ConflictPos->size() > MacroDefLen && |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 311 | "Invalid #define in predefines buffer?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | if ((*ConflictPos)[MacroDefLen] != ' ' && |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 313 | (*ConflictPos)[MacroDefLen] != '(') |
| 314 | continue; // Longer macro name; keep trying. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 316 | // We found a conflicting macro definition. |
| 317 | break; |
| 318 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 320 | if (ConflictPos != CmdLineLines.end()) { |
| 321 | Reader.Diag(diag::warn_cmdline_conflicting_macro_def) |
| 322 | << MacroName; |
| 323 | |
| 324 | // Show the definition of this macro within the PCH file. |
Sebastian Redl | 7e9ad8b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 325 | std::pair<FileID, llvm::StringRef::size_type> MacroLoc = |
| 326 | FindMacro(Buffers, Missing); |
| 327 | assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!"); |
| 328 | SourceLocation PCHMissingLoc = |
| 329 | SourceMgr.getLocForStartOfFile(MacroLoc.first) |
| 330 | .getFileLocWithOffset(MacroLoc.second); |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 331 | Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName; |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 332 | |
| 333 | ConflictingDefines = true; |
| 334 | continue; |
| 335 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | |
Daniel Dunbar | 10014aa | 2009-11-11 03:45:59 +0000 | [diff] [blame] | 337 | // If the macro doesn't conflict, then we'll just pick up the macro |
| 338 | // definition from the PCH file. Warn the user that they made a mistake. |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 339 | if (ConflictingDefines) |
| 340 | continue; // Don't complain if there are already conflicting defs |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 342 | if (!MissingDefines) { |
| 343 | Reader.Diag(diag::warn_cmdline_missing_macro_defs); |
| 344 | MissingDefines = true; |
| 345 | } |
| 346 | |
| 347 | // Show the definition of this macro within the PCH file. |
Sebastian Redl | 7e9ad8b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 348 | std::pair<FileID, llvm::StringRef::size_type> MacroLoc = |
| 349 | FindMacro(Buffers, Missing); |
| 350 | assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!"); |
| 351 | SourceLocation PCHMissingLoc = |
| 352 | SourceMgr.getLocForStartOfFile(MacroLoc.first) |
| 353 | .getFileLocWithOffset(MacroLoc.second); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 354 | Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch); |
| 355 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 357 | if (ConflictingDefines) |
| 358 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 360 | // Determine what predefines were introduced based on command-line |
| 361 | // parameters that were not present when building the PCH |
| 362 | // file. Extra #defines are okay, so long as the identifiers being |
| 363 | // defined were not used within the precompiled header. |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 364 | std::vector<llvm::StringRef> ExtraPredefines; |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 365 | std::set_difference(CmdLineLines.begin(), CmdLineLines.end(), |
| 366 | PCHLines.begin(), PCHLines.end(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | std::back_inserter(ExtraPredefines)); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 368 | for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) { |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 369 | llvm::StringRef &Extra = ExtraPredefines[I]; |
| 370 | if (!Extra.startswith("#define ")) { |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 371 | Reader.Diag(diag::warn_pch_compiler_options_mismatch); |
| 372 | return true; |
| 373 | } |
| 374 | |
| 375 | // This is an extra macro definition. Determine the name of the |
| 376 | // macro we're defining. |
| 377 | std::string::size_type StartOfMacroName = strlen("#define "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | std::string::size_type EndOfMacroName |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 379 | = Extra.find_first_of("( \n\r", StartOfMacroName); |
| 380 | assert(EndOfMacroName != std::string::npos && |
| 381 | "Couldn't find the end of the macro name"); |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 382 | llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 383 | |
| 384 | // Check whether this name was used somewhere in the PCH file. If |
| 385 | // so, defining it as a macro could change behavior, so we reject |
| 386 | // the PCH file. |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 387 | if (IdentifierInfo *II = Reader.get(MacroName)) { |
Daniel Dunbar | 4fda42e | 2009-11-11 00:52:00 +0000 | [diff] [blame] | 388 | Reader.Diag(diag::warn_macro_name_used_in_pch) << II; |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 389 | return true; |
| 390 | } |
| 391 | |
| 392 | // Add this definition to the suggested predefines buffer. |
| 393 | SuggestedPredefines += Extra; |
| 394 | SuggestedPredefines += '\n'; |
| 395 | } |
| 396 | |
| 397 | // If we get here, it's because the predefines buffer had compatible |
| 398 | // contents. Accept the PCH file. |
| 399 | return false; |
| 400 | } |
| 401 | |
Douglas Gregor | 12fab31 | 2010-03-16 16:35:32 +0000 | [diff] [blame] | 402 | void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI, |
| 403 | unsigned ID) { |
| 404 | PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID); |
| 405 | ++NumHeaderInfos; |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | void PCHValidator::ReadCounter(unsigned Value) { |
| 409 | PP.setCounterValue(Value); |
| 410 | } |
| 411 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 412 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 413 | // PCH reader implementation |
| 414 | //===----------------------------------------------------------------------===// |
| 415 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | PCHReader::PCHReader(Preprocessor &PP, ASTContext *Context, |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 417 | const char *isysroot, bool DisableValidation) |
Sebastian Redl | 30c514c | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 418 | : Listener(new PCHValidator(PP, *this)), DeserializationListener(0), |
| 419 | SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), |
| 420 | Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context), |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 421 | Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation), |
| 422 | NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0), |
| 423 | TotalNumSLocEntries(0), NumStatementsRead(0), TotalNumStatements(0), |
Sebastian Redl | fa78dec | 2010-08-04 21:22:45 +0000 | [diff] [blame] | 424 | NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0), |
| 425 | NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0), |
| 426 | TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0), |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 427 | TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0), |
| 428 | TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) { |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 429 | RelocatablePCH = false; |
| 430 | } |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 431 | |
| 432 | PCHReader::PCHReader(SourceManager &SourceMgr, FileManager &FileMgr, |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 433 | Diagnostic &Diags, const char *isysroot, |
| 434 | bool DisableValidation) |
Sebastian Redl | 30c514c | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 435 | : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr), |
Sebastian Redl | 9137a52 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 436 | Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0), |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 437 | isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0), |
| 438 | NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0), |
| 439 | NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0), |
Sebastian Redl | fa78dec | 2010-08-04 21:22:45 +0000 | [diff] [blame] | 440 | TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0), |
| 441 | NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0), |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 442 | NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0), |
| 443 | NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0), |
| 444 | NumCurrentElementsDeserializing(0) { |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 445 | RelocatablePCH = false; |
| 446 | } |
Chris Lattner | 4c6f952 | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 447 | |
Sebastian Redl | 9137a52 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 448 | PCHReader::~PCHReader() { |
| 449 | for (unsigned i = 0, e = Chain.size(); i != e; ++i) |
| 450 | delete Chain[e - i - 1]; |
| 451 | } |
| 452 | |
| 453 | PCHReader::PerFileData::PerFileData() |
Sebastian Redl | 12d6da0 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 454 | : StatCache(0), LocalNumSLocEntries(0), LocalNumTypes(0), TypeOffsets(0), |
Sebastian Redl | 2da08f9 | 2010-07-19 22:28:42 +0000 | [diff] [blame] | 455 | LocalNumDecls(0), DeclOffsets(0), LocalNumIdentifiers(0), |
Sebastian Redl | 04e6fd4 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 456 | IdentifierOffsets(0), IdentifierTableData(0), IdentifierLookupTable(0), |
| 457 | LocalNumMacroDefinitions(0), MacroDefinitionOffsets(0), |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 458 | NumPreallocatedPreprocessingEntities(0), SelectorLookupTable(0), |
| 459 | SelectorLookupTableData(0), SelectorOffsets(0), LocalNumSelectors(0) |
Sebastian Redl | 9137a52 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 460 | {} |
Chris Lattner | 4c6f952 | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 461 | |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 462 | void |
| 463 | PCHReader::setDeserializationListener(PCHDeserializationListener *Listener) { |
| 464 | DeserializationListener = Listener; |
| 465 | if (DeserializationListener) |
| 466 | DeserializationListener->SetReader(this); |
| 467 | } |
| 468 | |
Chris Lattner | 4c6f952 | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 469 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 470 | namespace { |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 471 | class PCHSelectorLookupTrait { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 472 | PCHReader &Reader; |
| 473 | |
| 474 | public: |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 475 | struct data_type { |
| 476 | pch::SelectorID ID; |
| 477 | ObjCMethodList Instance, Factory; |
| 478 | }; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 479 | |
| 480 | typedef Selector external_key_type; |
| 481 | typedef external_key_type internal_key_type; |
| 482 | |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 483 | explicit PCHSelectorLookupTrait(PCHReader &Reader) : Reader(Reader) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 485 | static bool EqualKey(const internal_key_type& a, |
| 486 | const internal_key_type& b) { |
| 487 | return a == b; |
| 488 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 490 | static unsigned ComputeHash(Selector Sel) { |
| 491 | unsigned N = Sel.getNumArgs(); |
| 492 | if (N == 0) |
| 493 | ++N; |
| 494 | unsigned R = 5381; |
| 495 | for (unsigned I = 0; I != N; ++I) |
| 496 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I)) |
Daniel Dunbar | 2596e42 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 497 | R = llvm::HashString(II->getName(), R); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 498 | return R; |
| 499 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 500 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 501 | // This hopefully will just get inlined and removed by the optimizer. |
| 502 | static const internal_key_type& |
| 503 | GetInternalKey(const external_key_type& x) { return x; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 504 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 505 | static std::pair<unsigned, unsigned> |
| 506 | ReadKeyDataLength(const unsigned char*& d) { |
| 507 | using namespace clang::io; |
| 508 | unsigned KeyLen = ReadUnalignedLE16(d); |
| 509 | unsigned DataLen = ReadUnalignedLE16(d); |
| 510 | return std::make_pair(KeyLen, DataLen); |
| 511 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 513 | internal_key_type ReadKey(const unsigned char* d, unsigned) { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 514 | using namespace clang::io; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 515 | SelectorTable &SelTable = Reader.getContext()->Selectors; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 516 | unsigned N = ReadUnalignedLE16(d); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | IdentifierInfo *FirstII |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 518 | = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)); |
| 519 | if (N == 0) |
| 520 | return SelTable.getNullarySelector(FirstII); |
| 521 | else if (N == 1) |
| 522 | return SelTable.getUnarySelector(FirstII); |
| 523 | |
| 524 | llvm::SmallVector<IdentifierInfo *, 16> Args; |
| 525 | Args.push_back(FirstII); |
| 526 | for (unsigned I = 1; I != N; ++I) |
| 527 | Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d))); |
| 528 | |
Douglas Gregor | 75fdb23 | 2009-05-22 22:45:36 +0000 | [diff] [blame] | 529 | return SelTable.getSelector(N, Args.data()); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 530 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 532 | data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) { |
| 533 | using namespace clang::io; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 534 | |
| 535 | data_type Result; |
| 536 | |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 537 | Result.ID = ReadUnalignedLE32(d); |
| 538 | unsigned NumInstanceMethods = ReadUnalignedLE16(d); |
| 539 | unsigned NumFactoryMethods = ReadUnalignedLE16(d); |
| 540 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 541 | // Load instance methods |
| 542 | ObjCMethodList *Prev = 0; |
| 543 | for (unsigned I = 0; I != NumInstanceMethods; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | ObjCMethodDecl *Method |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 545 | = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d))); |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 546 | if (!Result.Instance.Method) { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 547 | // This is the first method, which is the easy case. |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 548 | Result.Instance.Method = Method; |
| 549 | Prev = &Result.Instance; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 550 | continue; |
| 551 | } |
| 552 | |
Ted Kremenek | 298ed87 | 2010-02-11 00:53:01 +0000 | [diff] [blame] | 553 | ObjCMethodList *Mem = |
| 554 | Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>(); |
| 555 | Prev->Next = new (Mem) ObjCMethodList(Method, 0); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 556 | Prev = Prev->Next; |
| 557 | } |
| 558 | |
| 559 | // Load factory methods |
| 560 | Prev = 0; |
| 561 | for (unsigned I = 0; I != NumFactoryMethods; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | ObjCMethodDecl *Method |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 563 | = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d))); |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 564 | if (!Result.Factory.Method) { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 565 | // This is the first method, which is the easy case. |
Sebastian Redl | 5d05007 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 566 | Result.Factory.Method = Method; |
| 567 | Prev = &Result.Factory; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 568 | continue; |
| 569 | } |
| 570 | |
Ted Kremenek | 298ed87 | 2010-02-11 00:53:01 +0000 | [diff] [blame] | 571 | ObjCMethodList *Mem = |
| 572 | Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>(); |
| 573 | Prev->Next = new (Mem) ObjCMethodList(Method, 0); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 574 | Prev = Prev->Next; |
| 575 | } |
| 576 | |
| 577 | return Result; |
| 578 | } |
| 579 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | |
| 581 | } // end anonymous namespace |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 582 | |
| 583 | /// \brief The on-disk hash table used for the global method pool. |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 584 | typedef OnDiskChainedHashTable<PCHSelectorLookupTrait> |
| 585 | PCHSelectorLookupTable; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 586 | |
| 587 | namespace { |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 588 | class PCHIdentifierLookupTrait { |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 589 | PCHReader &Reader; |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 590 | llvm::BitstreamCursor &Stream; |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 591 | |
| 592 | // If we know the IdentifierInfo in advance, it is here and we will |
| 593 | // not build a new one. Used when deserializing information about an |
| 594 | // identifier that was constructed before the PCH file was read. |
| 595 | IdentifierInfo *KnownII; |
| 596 | |
| 597 | public: |
| 598 | typedef IdentifierInfo * data_type; |
| 599 | |
| 600 | typedef const std::pair<const char*, unsigned> external_key_type; |
| 601 | |
| 602 | typedef external_key_type internal_key_type; |
| 603 | |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 604 | PCHIdentifierLookupTrait(PCHReader &Reader, llvm::BitstreamCursor &Stream, |
| 605 | IdentifierInfo *II = 0) |
| 606 | : Reader(Reader), Stream(Stream), KnownII(II) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 607 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 608 | static bool EqualKey(const internal_key_type& a, |
| 609 | const internal_key_type& b) { |
| 610 | return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0 |
| 611 | : false; |
| 612 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 613 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 614 | static unsigned ComputeHash(const internal_key_type& a) { |
Daniel Dunbar | 2596e42 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 615 | return llvm::HashString(llvm::StringRef(a.first, a.second)); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 616 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 617 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 618 | // This hopefully will just get inlined and removed by the optimizer. |
| 619 | static const internal_key_type& |
| 620 | GetInternalKey(const external_key_type& x) { return x; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 621 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 622 | static std::pair<unsigned, unsigned> |
| 623 | ReadKeyDataLength(const unsigned char*& d) { |
| 624 | using namespace clang::io; |
Douglas Gregor | 5f8e330 | 2009-04-25 20:26:24 +0000 | [diff] [blame] | 625 | unsigned DataLen = ReadUnalignedLE16(d); |
Douglas Gregor | d6595a4 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 626 | unsigned KeyLen = ReadUnalignedLE16(d); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 627 | return std::make_pair(KeyLen, DataLen); |
| 628 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 629 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 630 | static std::pair<const char*, unsigned> |
| 631 | ReadKey(const unsigned char* d, unsigned n) { |
| 632 | assert(n >= 2 && d[n-1] == '\0'); |
| 633 | return std::make_pair((const char*) d, n-1); |
| 634 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | |
| 636 | IdentifierInfo *ReadData(const internal_key_type& k, |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 637 | const unsigned char* d, |
| 638 | unsigned DataLen) { |
| 639 | using namespace clang::io; |
Douglas Gregor | a92193e | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 640 | pch::IdentID ID = ReadUnalignedLE32(d); |
| 641 | bool IsInteresting = ID & 0x01; |
| 642 | |
| 643 | // Wipe out the "is interesting" bit. |
| 644 | ID = ID >> 1; |
| 645 | |
| 646 | if (!IsInteresting) { |
Sebastian Redl | 083abdf | 2010-07-27 23:01:28 +0000 | [diff] [blame] | 647 | // For uninteresting identifiers, just build the IdentifierInfo |
Douglas Gregor | a92193e | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 648 | // and associate it with the persistent ID. |
| 649 | IdentifierInfo *II = KnownII; |
| 650 | if (!II) |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 651 | II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second); |
Douglas Gregor | a92193e | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 652 | Reader.SetIdentifierInfo(ID, II); |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 653 | II->setIsFromPCH(); |
Douglas Gregor | a92193e | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 654 | return II; |
| 655 | } |
| 656 | |
Douglas Gregor | 5998da5 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 657 | unsigned Bits = ReadUnalignedLE16(d); |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 658 | bool CPlusPlusOperatorKeyword = Bits & 0x01; |
| 659 | Bits >>= 1; |
| 660 | bool Poisoned = Bits & 0x01; |
| 661 | Bits >>= 1; |
| 662 | bool ExtensionToken = Bits & 0x01; |
| 663 | Bits >>= 1; |
| 664 | bool hasMacroDefinition = Bits & 0x01; |
| 665 | Bits >>= 1; |
| 666 | unsigned ObjCOrBuiltinID = Bits & 0x3FF; |
| 667 | Bits >>= 10; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 668 | |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 669 | assert(Bits == 0 && "Extra bits in the identifier?"); |
Douglas Gregor | 5998da5 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 670 | DataLen -= 6; |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 671 | |
| 672 | // Build the IdentifierInfo itself and link the identifier ID with |
| 673 | // the new IdentifierInfo. |
| 674 | IdentifierInfo *II = KnownII; |
| 675 | if (!II) |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 676 | II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 677 | Reader.SetIdentifierInfo(ID, II); |
| 678 | |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 679 | // Set or check the various bits in the IdentifierInfo structure. |
| 680 | // FIXME: Load token IDs lazily, too? |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 681 | II->setObjCOrBuiltinID(ObjCOrBuiltinID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 682 | assert(II->isExtensionToken() == ExtensionToken && |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 683 | "Incorrect extension token flag"); |
| 684 | (void)ExtensionToken; |
| 685 | II->setIsPoisoned(Poisoned); |
| 686 | assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && |
| 687 | "Incorrect C++ operator keyword flag"); |
| 688 | (void)CPlusPlusOperatorKeyword; |
| 689 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 690 | // If this identifier is a macro, deserialize the macro |
| 691 | // definition. |
| 692 | if (hasMacroDefinition) { |
Douglas Gregor | 5998da5 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 693 | uint32_t Offset = ReadUnalignedLE32(d); |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 694 | Reader.ReadMacroRecord(Stream, Offset); |
Douglas Gregor | 5998da5 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 695 | DataLen -= 4; |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 696 | } |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 697 | |
| 698 | // Read all of the declarations visible at global scope with this |
| 699 | // name. |
Chris Lattner | 6bf690f | 2009-04-27 22:17:41 +0000 | [diff] [blame] | 700 | if (Reader.getContext() == 0) return II; |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 701 | if (DataLen > 0) { |
| 702 | llvm::SmallVector<uint32_t, 4> DeclIDs; |
| 703 | for (; DataLen > 0; DataLen -= 4) |
| 704 | DeclIDs.push_back(ReadUnalignedLE32(d)); |
| 705 | Reader.SetGloballyVisibleDecls(II, DeclIDs); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 706 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 707 | |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 708 | II->setIsFromPCH(); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 709 | return II; |
| 710 | } |
| 711 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | |
| 713 | } // end anonymous namespace |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 714 | |
| 715 | /// \brief The on-disk hash table used to contain information about |
| 716 | /// all of the identifiers in the program. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait> |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 718 | PCHIdentifierLookupTable; |
| 719 | |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 720 | void PCHReader::Error(const char *Msg) { |
| 721 | Diag(diag::err_fe_pch_malformed) << Msg; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Sebastian Redl | 7e9ad8b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 724 | /// \brief Check the contents of the concatenation of all predefines buffers in |
| 725 | /// the PCH chain against the contents of the predefines buffer of the current |
| 726 | /// compiler invocation. |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 727 | /// |
Sebastian Redl | 7e9ad8b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 728 | /// The contents should be the same. If not, then some command-line option |
| 729 | /// changed the preprocessor state and we must probably reject the PCH file. |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 730 | /// |
| 731 | /// \returns true if there was a mismatch (in which case the PCH file |
| 732 | /// should be ignored), or false otherwise. |
Sebastian Redl | 7e9ad8b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 733 | bool PCHReader::CheckPredefinesBuffers() { |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 734 | if (Listener) |
Sebastian Redl | 7e9ad8b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 735 | return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers, |
Daniel Dunbar | 7b5a121 | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 736 | ActualOriginalFileName, |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 737 | SuggestedPredefines); |
Douglas Gregor | e721f95 | 2009-04-28 18:58:38 +0000 | [diff] [blame] | 738 | return false; |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 741 | //===----------------------------------------------------------------------===// |
| 742 | // Source Manager Deserialization |
| 743 | //===----------------------------------------------------------------------===// |
| 744 | |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 745 | /// \brief Read the line table in the source manager block. |
| 746 | /// \returns true if ther was an error. |
Sebastian Redl | 518d8cb | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 747 | bool PCHReader::ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record) { |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 748 | unsigned Idx = 0; |
| 749 | LineTableInfo &LineTable = SourceMgr.getLineTable(); |
| 750 | |
| 751 | // Parse the file names |
Douglas Gregor | ff0a987 | 2009-04-13 17:12:42 +0000 | [diff] [blame] | 752 | std::map<int, int> FileIDs; |
| 753 | for (int I = 0, N = Record[Idx++]; I != N; ++I) { |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 754 | // Extract the file name |
| 755 | unsigned FilenameLen = Record[Idx++]; |
| 756 | std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen); |
| 757 | Idx += FilenameLen; |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 758 | MaybeAddSystemRootToFilename(Filename); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 759 | FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(), |
Douglas Gregor | ff0a987 | 2009-04-13 17:12:42 +0000 | [diff] [blame] | 760 | Filename.size()); |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | // Parse the line entries |
| 764 | std::vector<LineEntry> Entries; |
| 765 | while (Idx < Record.size()) { |
Argyrios Kyrtzidis | f52a5d2 | 2010-07-02 11:55:05 +0000 | [diff] [blame] | 766 | int FID = Record[Idx++]; |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 767 | |
| 768 | // Extract the line entries |
| 769 | unsigned NumEntries = Record[Idx++]; |
Argyrios Kyrtzidis | f52a5d2 | 2010-07-02 11:55:05 +0000 | [diff] [blame] | 770 | assert(NumEntries && "Numentries is 00000"); |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 771 | Entries.clear(); |
| 772 | Entries.reserve(NumEntries); |
| 773 | for (unsigned I = 0; I != NumEntries; ++I) { |
| 774 | unsigned FileOffset = Record[Idx++]; |
| 775 | unsigned LineNo = Record[Idx++]; |
Argyrios Kyrtzidis | f52a5d2 | 2010-07-02 11:55:05 +0000 | [diff] [blame] | 776 | int FilenameID = FileIDs[Record[Idx++]]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 777 | SrcMgr::CharacteristicKind FileKind |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 778 | = (SrcMgr::CharacteristicKind)Record[Idx++]; |
| 779 | unsigned IncludeOffset = Record[Idx++]; |
| 780 | Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, |
| 781 | FileKind, IncludeOffset)); |
| 782 | } |
| 783 | LineTable.AddEntry(FID, Entries); |
| 784 | } |
| 785 | |
| 786 | return false; |
| 787 | } |
| 788 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 789 | namespace { |
| 790 | |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 791 | class PCHStatData { |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 792 | public: |
| 793 | const bool hasStat; |
| 794 | const ino_t ino; |
| 795 | const dev_t dev; |
| 796 | const mode_t mode; |
| 797 | const time_t mtime; |
| 798 | const off_t size; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 799 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 800 | PCHStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 801 | : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {} |
| 802 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 803 | PCHStatData() |
| 804 | : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {} |
| 805 | }; |
| 806 | |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 807 | class PCHStatLookupTrait { |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 808 | public: |
| 809 | typedef const char *external_key_type; |
| 810 | typedef const char *internal_key_type; |
| 811 | |
| 812 | typedef PCHStatData data_type; |
| 813 | |
| 814 | static unsigned ComputeHash(const char *path) { |
Daniel Dunbar | 2596e42 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 815 | return llvm::HashString(path); |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | static internal_key_type GetInternalKey(const char *path) { return path; } |
| 819 | |
| 820 | static bool EqualKey(internal_key_type a, internal_key_type b) { |
| 821 | return strcmp(a, b) == 0; |
| 822 | } |
| 823 | |
| 824 | static std::pair<unsigned, unsigned> |
| 825 | ReadKeyDataLength(const unsigned char*& d) { |
| 826 | unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d); |
| 827 | unsigned DataLen = (unsigned) *d++; |
| 828 | return std::make_pair(KeyLen + 1, DataLen); |
| 829 | } |
| 830 | |
| 831 | static internal_key_type ReadKey(const unsigned char *d, unsigned) { |
| 832 | return (const char *)d; |
| 833 | } |
| 834 | |
| 835 | static data_type ReadData(const internal_key_type, const unsigned char *d, |
| 836 | unsigned /*DataLen*/) { |
| 837 | using namespace clang::io; |
| 838 | |
| 839 | if (*d++ == 1) |
| 840 | return data_type(); |
| 841 | |
| 842 | ino_t ino = (ino_t) ReadUnalignedLE32(d); |
| 843 | dev_t dev = (dev_t) ReadUnalignedLE32(d); |
| 844 | mode_t mode = (mode_t) ReadUnalignedLE16(d); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 845 | time_t mtime = (time_t) ReadUnalignedLE64(d); |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 846 | off_t size = (off_t) ReadUnalignedLE64(d); |
| 847 | return data_type(ino, dev, mode, mtime, size); |
| 848 | } |
| 849 | }; |
| 850 | |
| 851 | /// \brief stat() cache for precompiled headers. |
| 852 | /// |
| 853 | /// This cache is very similar to the stat cache used by pretokenized |
| 854 | /// headers. |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 855 | class PCHStatCache : public StatSysCallCache { |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 856 | typedef OnDiskChainedHashTable<PCHStatLookupTrait> CacheTy; |
| 857 | CacheTy *Cache; |
| 858 | |
| 859 | unsigned &NumStatHits, &NumStatMisses; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 860 | public: |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 861 | PCHStatCache(const unsigned char *Buckets, |
| 862 | const unsigned char *Base, |
| 863 | unsigned &NumStatHits, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 864 | unsigned &NumStatMisses) |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 865 | : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) { |
| 866 | Cache = CacheTy::Create(Buckets, Base); |
| 867 | } |
| 868 | |
| 869 | ~PCHStatCache() { delete Cache; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 870 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 871 | int stat(const char *path, struct stat *buf) { |
| 872 | // Do the lookup for the file's data in the PCH file. |
| 873 | CacheTy::iterator I = Cache->find(path); |
| 874 | |
| 875 | // If we don't get a hit in the PCH file just forward to 'stat'. |
| 876 | if (I == Cache->end()) { |
| 877 | ++NumStatMisses; |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 878 | return StatSysCallCache::stat(path, buf); |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 879 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 880 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 881 | ++NumStatHits; |
| 882 | PCHStatData Data = *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 883 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 884 | if (!Data.hasStat) |
| 885 | return 1; |
| 886 | |
| 887 | buf->st_ino = Data.ino; |
| 888 | buf->st_dev = Data.dev; |
| 889 | buf->st_mtime = Data.mtime; |
| 890 | buf->st_mode = Data.mode; |
| 891 | buf->st_size = Data.size; |
| 892 | return 0; |
| 893 | } |
| 894 | }; |
| 895 | } // end anonymous namespace |
| 896 | |
| 897 | |
Sebastian Redl | 93fb9ed | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 898 | /// \brief Read a source manager block |
| 899 | PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock(PerFileData &F) { |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 900 | using namespace SrcMgr; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 901 | |
Sebastian Redl | 93fb9ed | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 902 | llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; |
Sebastian Redl | 9137a52 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 903 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 904 | // Set the source-location entry cursor to the current position in |
| 905 | // the stream. This cursor will be used to read the contents of the |
| 906 | // source manager block initially, and then lazily read |
| 907 | // source-location entries as needed. |
Sebastian Redl | 93fb9ed | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 908 | SLocEntryCursor = F.Stream; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 909 | |
| 910 | // The stream itself is going to skip over the source manager block. |
Sebastian Redl | 93fb9ed | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 911 | if (F.Stream.SkipBlock()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 912 | Error("malformed block record in PCH file"); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 913 | return Failure; |
| 914 | } |
| 915 | |
| 916 | // Enter the source manager block. |
| 917 | if (SLocEntryCursor.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 918 | Error("malformed source manager block record in PCH file"); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 919 | return Failure; |
| 920 | } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 921 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 922 | RecordData Record; |
| 923 | while (true) { |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 924 | unsigned Code = SLocEntryCursor.ReadCode(); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 925 | if (Code == llvm::bitc::END_BLOCK) { |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 926 | if (SLocEntryCursor.ReadBlockEnd()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 927 | Error("error at end of Source Manager block in PCH file"); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 928 | return Failure; |
| 929 | } |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 930 | return Success; |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 931 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 932 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 933 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 934 | // No known subblocks, always skip them. |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 935 | SLocEntryCursor.ReadSubBlockID(); |
| 936 | if (SLocEntryCursor.SkipBlock()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 937 | Error("malformed block record in PCH file"); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 938 | return Failure; |
| 939 | } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 940 | continue; |
| 941 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 942 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 943 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 944 | SLocEntryCursor.ReadAbbrevRecord(); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 945 | continue; |
| 946 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 947 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 948 | // Read a record. |
| 949 | const char *BlobStart; |
| 950 | unsigned BlobLen; |
| 951 | Record.clear(); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 952 | switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 953 | default: // Default behavior: ignore. |
| 954 | break; |
| 955 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 956 | case pch::SM_LINE_TABLE: |
Sebastian Redl | 518d8cb | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 957 | if (ParseLineTable(Record)) |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 958 | return Failure; |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 959 | break; |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 960 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 961 | case pch::SM_SLOC_FILE_ENTRY: |
| 962 | case pch::SM_SLOC_BUFFER_ENTRY: |
| 963 | case pch::SM_SLOC_INSTANTIATION_ENTRY: |
| 964 | // Once we hit one of the source location entries, we're done. |
| 965 | return Success; |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | } |
| 969 | |
Sebastian Redl | 190faf7 | 2010-07-20 21:50:20 +0000 | [diff] [blame] | 970 | /// \brief Get a cursor that's correctly positioned for reading the source |
| 971 | /// location entry with the given ID. |
| 972 | llvm::BitstreamCursor &PCHReader::SLocCursorForID(unsigned ID) { |
| 973 | assert(ID != 0 && ID <= TotalNumSLocEntries && |
| 974 | "SLocCursorForID should only be called for real IDs."); |
| 975 | |
| 976 | ID -= 1; |
| 977 | PerFileData *F = 0; |
| 978 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
| 979 | F = Chain[N - I - 1]; |
| 980 | if (ID < F->LocalNumSLocEntries) |
| 981 | break; |
| 982 | ID -= F->LocalNumSLocEntries; |
| 983 | } |
| 984 | assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted"); |
| 985 | |
| 986 | F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]); |
| 987 | return F->SLocEntryCursor; |
| 988 | } |
| 989 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 990 | /// \brief Read in the source location entry with the given ID. |
| 991 | PCHReader::PCHReadResult PCHReader::ReadSLocEntryRecord(unsigned ID) { |
| 992 | if (ID == 0) |
| 993 | return Success; |
| 994 | |
| 995 | if (ID > TotalNumSLocEntries) { |
| 996 | Error("source location entry ID out-of-range for PCH file"); |
| 997 | return Failure; |
| 998 | } |
| 999 | |
Sebastian Redl | 190faf7 | 2010-07-20 21:50:20 +0000 | [diff] [blame] | 1000 | llvm::BitstreamCursor &SLocEntryCursor = SLocCursorForID(ID); |
Sebastian Redl | 9137a52 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 1001 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1002 | ++NumSLocEntriesRead; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1003 | unsigned Code = SLocEntryCursor.ReadCode(); |
| 1004 | if (Code == llvm::bitc::END_BLOCK || |
| 1005 | Code == llvm::bitc::ENTER_SUBBLOCK || |
| 1006 | Code == llvm::bitc::DEFINE_ABBREV) { |
| 1007 | Error("incorrectly-formatted source location entry in PCH file"); |
| 1008 | return Failure; |
| 1009 | } |
| 1010 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1011 | RecordData Record; |
| 1012 | const char *BlobStart; |
| 1013 | unsigned BlobLen; |
| 1014 | switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { |
| 1015 | default: |
| 1016 | Error("incorrectly-formatted source location entry in PCH file"); |
| 1017 | return Failure; |
| 1018 | |
| 1019 | case pch::SM_SLOC_FILE_ENTRY: { |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1020 | std::string Filename(BlobStart, BlobStart + BlobLen); |
| 1021 | MaybeAddSystemRootToFilename(Filename); |
| 1022 | const FileEntry *File = FileMgr.getFile(Filename); |
Chris Lattner | d3555ae | 2009-06-15 04:35:16 +0000 | [diff] [blame] | 1023 | if (File == 0) { |
| 1024 | std::string ErrorStr = "could not find file '"; |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1025 | ErrorStr += Filename; |
Chris Lattner | d3555ae | 2009-06-15 04:35:16 +0000 | [diff] [blame] | 1026 | ErrorStr += "' referenced by PCH file"; |
| 1027 | Error(ErrorStr.c_str()); |
| 1028 | return Failure; |
| 1029 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1030 | |
Douglas Gregor | 2d52be5 | 2010-03-21 22:49:54 +0000 | [diff] [blame] | 1031 | if (Record.size() < 10) { |
Ted Kremenek | 1857f62 | 2010-03-18 21:23:05 +0000 | [diff] [blame] | 1032 | Error("source location entry is incorrect"); |
| 1033 | return Failure; |
| 1034 | } |
| 1035 | |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 1036 | if (!DisableValidation && |
| 1037 | ((off_t)Record[4] != File->getSize() |
Douglas Gregor | 9f692a0 | 2010-04-09 15:54:22 +0000 | [diff] [blame] | 1038 | #if !defined(LLVM_ON_WIN32) |
| 1039 | // In our regression testing, the Windows file system seems to |
| 1040 | // have inconsistent modification times that sometimes |
| 1041 | // erroneously trigger this error-handling path. |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 1042 | || (time_t)Record[5] != File->getModificationTime() |
Douglas Gregor | 9f692a0 | 2010-04-09 15:54:22 +0000 | [diff] [blame] | 1043 | #endif |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 1044 | )) { |
Douglas Gregor | 2d52be5 | 2010-03-21 22:49:54 +0000 | [diff] [blame] | 1045 | Diag(diag::err_fe_pch_file_modified) |
| 1046 | << Filename; |
| 1047 | return Failure; |
| 1048 | } |
| 1049 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1050 | FileID FID = SourceMgr.createFileID(File, |
| 1051 | SourceLocation::getFromRawEncoding(Record[1]), |
| 1052 | (SrcMgr::CharacteristicKind)Record[2], |
| 1053 | ID, Record[0]); |
| 1054 | if (Record[3]) |
| 1055 | const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()) |
| 1056 | .setHasLineDirectives(); |
| 1057 | |
Douglas Gregor | 12fab31 | 2010-03-16 16:35:32 +0000 | [diff] [blame] | 1058 | // Reconstruct header-search information for this file. |
| 1059 | HeaderFileInfo HFI; |
Douglas Gregor | 2d52be5 | 2010-03-21 22:49:54 +0000 | [diff] [blame] | 1060 | HFI.isImport = Record[6]; |
| 1061 | HFI.DirInfo = Record[7]; |
| 1062 | HFI.NumIncludes = Record[8]; |
| 1063 | HFI.ControllingMacroID = Record[9]; |
Douglas Gregor | 12fab31 | 2010-03-16 16:35:32 +0000 | [diff] [blame] | 1064 | if (Listener) |
| 1065 | Listener->ReadHeaderFileInfo(HFI, File->getUID()); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1066 | break; |
| 1067 | } |
| 1068 | |
| 1069 | case pch::SM_SLOC_BUFFER_ENTRY: { |
| 1070 | const char *Name = BlobStart; |
| 1071 | unsigned Offset = Record[0]; |
| 1072 | unsigned Code = SLocEntryCursor.ReadCode(); |
| 1073 | Record.clear(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1074 | unsigned RecCode |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1075 | = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen); |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 1076 | |
| 1077 | if (RecCode != pch::SM_SLOC_BUFFER_BLOB) { |
| 1078 | Error("PCH record has invalid code"); |
| 1079 | return Failure; |
| 1080 | } |
| 1081 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1082 | llvm::MemoryBuffer *Buffer |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 1083 | = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1), |
| 1084 | Name); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1085 | FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | |
Douglas Gregor | 92b059e | 2009-04-28 20:33:11 +0000 | [diff] [blame] | 1087 | if (strcmp(Name, "<built-in>") == 0) { |
Sebastian Redl | 7e9ad8b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 1088 | PCHPredefinesBlock Block = { |
| 1089 | BufferID, |
| 1090 | llvm::StringRef(BlobStart, BlobLen - 1) |
| 1091 | }; |
| 1092 | PCHPredefinesBuffers.push_back(Block); |
Douglas Gregor | 92b059e | 2009-04-28 20:33:11 +0000 | [diff] [blame] | 1093 | } |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1094 | |
| 1095 | break; |
| 1096 | } |
| 1097 | |
| 1098 | case pch::SM_SLOC_INSTANTIATION_ENTRY: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1099 | SourceLocation SpellingLoc |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1100 | = SourceLocation::getFromRawEncoding(Record[1]); |
| 1101 | SourceMgr.createInstantiationLoc(SpellingLoc, |
| 1102 | SourceLocation::getFromRawEncoding(Record[2]), |
| 1103 | SourceLocation::getFromRawEncoding(Record[3]), |
| 1104 | Record[4], |
| 1105 | ID, |
| 1106 | Record[0]); |
| 1107 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1108 | } |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | return Success; |
| 1112 | } |
| 1113 | |
Chris Lattner | 6367f6d | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 1114 | /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the |
| 1115 | /// specified cursor. Read the abbreviations that are at the top of the block |
| 1116 | /// and then leave the cursor pointing into the block. |
| 1117 | bool PCHReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, |
| 1118 | unsigned BlockID) { |
| 1119 | if (Cursor.EnterSubBlock(BlockID)) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1120 | Error("malformed block record in PCH file"); |
Chris Lattner | 6367f6d | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 1121 | return Failure; |
| 1122 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 | |
Chris Lattner | 6367f6d | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 1124 | while (true) { |
| 1125 | unsigned Code = Cursor.ReadCode(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1126 | |
Chris Lattner | 6367f6d | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 1127 | // We expect all abbrevs to be at the start of the block. |
| 1128 | if (Code != llvm::bitc::DEFINE_ABBREV) |
| 1129 | return false; |
| 1130 | Cursor.ReadAbbrevRecord(); |
| 1131 | } |
| 1132 | } |
| 1133 | |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1134 | void PCHReader::ReadMacroRecord(llvm::BitstreamCursor &Stream, uint64_t Offset){ |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1135 | assert(PP && "Forgot to set Preprocessor ?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1136 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1137 | // Keep track of where we are in the stream, then jump back there |
| 1138 | // after reading this macro. |
| 1139 | SavedStreamPosition SavedPosition(Stream); |
| 1140 | |
| 1141 | Stream.JumpToBit(Offset); |
| 1142 | RecordData Record; |
| 1143 | llvm::SmallVector<IdentifierInfo*, 16> MacroArgs; |
| 1144 | MacroInfo *Macro = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1145 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1146 | while (true) { |
| 1147 | unsigned Code = Stream.ReadCode(); |
| 1148 | switch (Code) { |
| 1149 | case llvm::bitc::END_BLOCK: |
| 1150 | return; |
| 1151 | |
| 1152 | case llvm::bitc::ENTER_SUBBLOCK: |
| 1153 | // No known subblocks, always skip them. |
| 1154 | Stream.ReadSubBlockID(); |
| 1155 | if (Stream.SkipBlock()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1156 | Error("malformed block record in PCH file"); |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1157 | return; |
| 1158 | } |
| 1159 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1160 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1161 | case llvm::bitc::DEFINE_ABBREV: |
| 1162 | Stream.ReadAbbrevRecord(); |
| 1163 | continue; |
| 1164 | default: break; |
| 1165 | } |
| 1166 | |
| 1167 | // Read a record. |
| 1168 | Record.clear(); |
| 1169 | pch::PreprocessorRecordTypes RecType = |
| 1170 | (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record); |
| 1171 | switch (RecType) { |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1172 | case pch::PP_MACRO_OBJECT_LIKE: |
| 1173 | case pch::PP_MACRO_FUNCTION_LIKE: { |
| 1174 | // If we already have a macro, that means that we've hit the end |
| 1175 | // of the definition of the macro we were looking for. We're |
| 1176 | // done. |
| 1177 | if (Macro) |
| 1178 | return; |
| 1179 | |
| 1180 | IdentifierInfo *II = DecodeIdentifierInfo(Record[0]); |
| 1181 | if (II == 0) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1182 | Error("macro must have a name in PCH file"); |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1183 | return; |
| 1184 | } |
| 1185 | SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]); |
| 1186 | bool isUsed = Record[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1187 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1188 | MacroInfo *MI = PP->AllocateMacroInfo(Loc); |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1189 | MI->setIsUsed(isUsed); |
Sebastian Redl | 083abdf | 2010-07-27 23:01:28 +0000 | [diff] [blame] | 1190 | MI->setIsFromPCH(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1191 | |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1192 | unsigned NextIndex = 3; |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1193 | if (RecType == pch::PP_MACRO_FUNCTION_LIKE) { |
| 1194 | // Decode function-like macro info. |
| 1195 | bool isC99VarArgs = Record[3]; |
| 1196 | bool isGNUVarArgs = Record[4]; |
| 1197 | MacroArgs.clear(); |
| 1198 | unsigned NumArgs = Record[5]; |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1199 | NextIndex = 6 + NumArgs; |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1200 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1201 | MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i])); |
| 1202 | |
| 1203 | // Install function-like macro info. |
| 1204 | MI->setIsFunctionLike(); |
| 1205 | if (isC99VarArgs) MI->setIsC99Varargs(); |
| 1206 | if (isGNUVarArgs) MI->setIsGNUVarargs(); |
Douglas Gregor | 75fdb23 | 2009-05-22 22:45:36 +0000 | [diff] [blame] | 1207 | MI->setArgumentList(MacroArgs.data(), MacroArgs.size(), |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1208 | PP->getPreprocessorAllocator()); |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | // Finally, install the macro. |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1212 | PP->setMacroInfo(II, MI); |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1213 | |
| 1214 | // Remember that we saw this macro last so that we add the tokens that |
| 1215 | // form its body to it. |
| 1216 | Macro = MI; |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1217 | |
| 1218 | if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) { |
| 1219 | // We have a macro definition. Load it now. |
| 1220 | PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro, |
| 1221 | getMacroDefinition(Record[NextIndex])); |
| 1222 | } |
| 1223 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1224 | ++NumMacrosRead; |
| 1225 | break; |
| 1226 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1227 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1228 | case pch::PP_TOKEN: { |
| 1229 | // If we see a TOKEN before a PP_MACRO_*, then the file is |
| 1230 | // erroneous, just pretend we didn't see this. |
| 1231 | if (Macro == 0) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1232 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1233 | Token Tok; |
| 1234 | Tok.startToken(); |
| 1235 | Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0])); |
| 1236 | Tok.setLength(Record[1]); |
| 1237 | if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2])) |
| 1238 | Tok.setIdentifierInfo(II); |
| 1239 | Tok.setKind((tok::TokenKind)Record[3]); |
| 1240 | Tok.setFlag((Token::TokenFlags)Record[4]); |
| 1241 | Macro->AddTokenToBody(Tok); |
| 1242 | break; |
| 1243 | } |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1244 | |
| 1245 | case pch::PP_MACRO_INSTANTIATION: { |
| 1246 | // If we already have a macro, that means that we've hit the end |
| 1247 | // of the definition of the macro we were looking for. We're |
| 1248 | // done. |
| 1249 | if (Macro) |
| 1250 | return; |
| 1251 | |
| 1252 | if (!PP->getPreprocessingRecord()) { |
| 1253 | Error("missing preprocessing record in PCH file"); |
| 1254 | return; |
| 1255 | } |
| 1256 | |
| 1257 | PreprocessingRecord &PPRec = *PP->getPreprocessingRecord(); |
| 1258 | if (PPRec.getPreprocessedEntity(Record[0])) |
| 1259 | return; |
| 1260 | |
| 1261 | MacroInstantiation *MI |
| 1262 | = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]), |
| 1263 | SourceRange( |
| 1264 | SourceLocation::getFromRawEncoding(Record[1]), |
| 1265 | SourceLocation::getFromRawEncoding(Record[2])), |
| 1266 | getMacroDefinition(Record[4])); |
| 1267 | PPRec.SetPreallocatedEntity(Record[0], MI); |
| 1268 | return; |
| 1269 | } |
| 1270 | |
| 1271 | case pch::PP_MACRO_DEFINITION: { |
| 1272 | // If we already have a macro, that means that we've hit the end |
| 1273 | // of the definition of the macro we were looking for. We're |
| 1274 | // done. |
| 1275 | if (Macro) |
| 1276 | return; |
| 1277 | |
| 1278 | if (!PP->getPreprocessingRecord()) { |
| 1279 | Error("missing preprocessing record in PCH file"); |
| 1280 | return; |
| 1281 | } |
| 1282 | |
| 1283 | PreprocessingRecord &PPRec = *PP->getPreprocessingRecord(); |
| 1284 | if (PPRec.getPreprocessedEntity(Record[0])) |
| 1285 | return; |
| 1286 | |
| 1287 | if (Record[1] >= MacroDefinitionsLoaded.size()) { |
| 1288 | Error("out-of-bounds macro definition record"); |
| 1289 | return; |
| 1290 | } |
| 1291 | |
| 1292 | MacroDefinition *MD |
| 1293 | = new (PPRec) MacroDefinition(DecodeIdentifierInfo(Record[4]), |
| 1294 | SourceLocation::getFromRawEncoding(Record[5]), |
| 1295 | SourceRange( |
| 1296 | SourceLocation::getFromRawEncoding(Record[2]), |
| 1297 | SourceLocation::getFromRawEncoding(Record[3]))); |
| 1298 | PPRec.SetPreallocatedEntity(Record[0], MD); |
| 1299 | MacroDefinitionsLoaded[Record[1]] = MD; |
| 1300 | return; |
| 1301 | } |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 1302 | } |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1303 | } |
| 1304 | } |
| 1305 | |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1306 | void PCHReader::ReadDefinedMacros() { |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1307 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
| 1308 | llvm::BitstreamCursor &MacroCursor = Chain[N - I - 1]->MacroCursor; |
Sebastian Redl | 9137a52 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 1309 | |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1310 | // If there was no preprocessor block, skip this file. |
| 1311 | if (!MacroCursor.getBitStreamReader()) |
| 1312 | continue; |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 1313 | |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1314 | llvm::BitstreamCursor Cursor = MacroCursor; |
| 1315 | if (Cursor.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID)) { |
| 1316 | Error("malformed preprocessor block record in PCH file"); |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1317 | return; |
| 1318 | } |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 1319 | |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1320 | RecordData Record; |
| 1321 | while (true) { |
| 1322 | unsigned Code = Cursor.ReadCode(); |
| 1323 | if (Code == llvm::bitc::END_BLOCK) { |
| 1324 | if (Cursor.ReadBlockEnd()) { |
| 1325 | Error("error at end of preprocessor block in PCH file"); |
| 1326 | return; |
| 1327 | } |
| 1328 | break; |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1329 | } |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 1330 | |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1331 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 1332 | // No known subblocks, always skip them. |
| 1333 | Cursor.ReadSubBlockID(); |
| 1334 | if (Cursor.SkipBlock()) { |
| 1335 | Error("malformed block record in PCH file"); |
| 1336 | return; |
| 1337 | } |
| 1338 | continue; |
| 1339 | } |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 1340 | |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1341 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
| 1342 | Cursor.ReadAbbrevRecord(); |
| 1343 | continue; |
| 1344 | } |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 1345 | |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1346 | // Read a record. |
| 1347 | const char *BlobStart; |
| 1348 | unsigned BlobLen; |
| 1349 | Record.clear(); |
| 1350 | switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { |
| 1351 | default: // Default behavior: ignore. |
| 1352 | break; |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1353 | |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1354 | case pch::PP_MACRO_OBJECT_LIKE: |
| 1355 | case pch::PP_MACRO_FUNCTION_LIKE: |
| 1356 | DecodeIdentifierInfo(Record[0]); |
| 1357 | break; |
| 1358 | |
| 1359 | case pch::PP_TOKEN: |
| 1360 | // Ignore tokens. |
| 1361 | break; |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1362 | |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1363 | case pch::PP_MACRO_INSTANTIATION: |
| 1364 | case pch::PP_MACRO_DEFINITION: |
| 1365 | // Read the macro record. |
| 1366 | ReadMacroRecord(Chain[N - I - 1]->Stream, Cursor.GetCurrentBitNo()); |
| 1367 | break; |
| 1368 | } |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1369 | } |
| 1370 | } |
| 1371 | } |
| 1372 | |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1373 | MacroDefinition *PCHReader::getMacroDefinition(pch::IdentID ID) { |
| 1374 | if (ID == 0 || ID >= MacroDefinitionsLoaded.size()) |
| 1375 | return 0; |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1376 | |
| 1377 | if (!MacroDefinitionsLoaded[ID]) { |
| 1378 | unsigned Index = ID; |
| 1379 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
| 1380 | PerFileData &F = *Chain[N - I - 1]; |
| 1381 | if (Index < F.LocalNumMacroDefinitions) { |
| 1382 | ReadMacroRecord(F.Stream, F.MacroDefinitionOffsets[Index]); |
| 1383 | break; |
| 1384 | } |
| 1385 | Index -= F.LocalNumMacroDefinitions; |
| 1386 | } |
| 1387 | assert(MacroDefinitionsLoaded[ID] && "Broken chain"); |
| 1388 | } |
| 1389 | |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1390 | return MacroDefinitionsLoaded[ID]; |
| 1391 | } |
| 1392 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1393 | /// \brief If we are loading a relocatable PCH file, and the filename is |
| 1394 | /// not an absolute path, add the system root to the beginning of the file |
| 1395 | /// name. |
| 1396 | void PCHReader::MaybeAddSystemRootToFilename(std::string &Filename) { |
| 1397 | // If this is not a relocatable PCH file, there's nothing to do. |
| 1398 | if (!RelocatablePCH) |
| 1399 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1400 | |
Daniel Dunbar | d5b2197 | 2009-11-18 19:50:41 +0000 | [diff] [blame] | 1401 | if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute()) |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1402 | return; |
| 1403 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1404 | if (isysroot == 0) { |
| 1405 | // If no system root was given, default to '/' |
| 1406 | Filename.insert(Filename.begin(), '/'); |
| 1407 | return; |
| 1408 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1409 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1410 | unsigned Length = strlen(isysroot); |
| 1411 | if (isysroot[Length - 1] != '/') |
| 1412 | Filename.insert(Filename.begin(), '/'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1413 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1414 | Filename.insert(Filename.begin(), isysroot, isysroot + Length); |
| 1415 | } |
| 1416 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | PCHReader::PCHReadResult |
Sebastian Redl | cdf3b83 | 2010-07-16 20:41:52 +0000 | [diff] [blame] | 1418 | PCHReader::ReadPCHBlock(PerFileData &F) { |
Sebastian Redl | 9137a52 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 1419 | llvm::BitstreamCursor &Stream = F.Stream; |
| 1420 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1421 | if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1422 | Error("malformed block record in PCH file"); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1423 | return Failure; |
| 1424 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1425 | |
| 1426 | // Read all of the records and blocks for the PCH file. |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1427 | RecordData Record; |
Sebastian Redl | 93fb9ed | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 1428 | bool First = true; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1429 | while (!Stream.AtEndOfStream()) { |
| 1430 | unsigned Code = Stream.ReadCode(); |
| 1431 | if (Code == llvm::bitc::END_BLOCK) { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1432 | if (Stream.ReadBlockEnd()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1433 | Error("error at end of module block in PCH file"); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1434 | return Failure; |
| 1435 | } |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1436 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1437 | return Success; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 1441 | switch (Stream.ReadSubBlockID()) { |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 1442 | case pch::DECLTYPES_BLOCK_ID: |
Chris Lattner | 6367f6d | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 1443 | // We lazily load the decls block, but we want to set up the |
| 1444 | // DeclsCursor cursor to point into it. Clone our current bitcode |
| 1445 | // cursor to it, enter the block and read the abbrevs in that block. |
| 1446 | // With the main cursor, we just skip over it. |
Sebastian Redl | 9137a52 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 1447 | F.DeclsCursor = Stream; |
Chris Lattner | 6367f6d | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 1448 | if (Stream.SkipBlock() || // Skip with the main cursor. |
| 1449 | // Read the abbrevs. |
Sebastian Redl | 9137a52 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 1450 | ReadBlockAbbrevs(F.DeclsCursor, pch::DECLTYPES_BLOCK_ID)) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1451 | Error("malformed block record in PCH file"); |
Chris Lattner | 6367f6d | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 1452 | return Failure; |
| 1453 | } |
| 1454 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1455 | |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1456 | case pch::PREPROCESSOR_BLOCK_ID: |
Sebastian Redl | 9137a52 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 1457 | F.MacroCursor = Stream; |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1458 | if (PP) |
| 1459 | PP->setExternalSource(this); |
| 1460 | |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1461 | if (Stream.SkipBlock()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1462 | Error("malformed block record in PCH file"); |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1463 | return Failure; |
| 1464 | } |
| 1465 | break; |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 1466 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1467 | case pch::SOURCE_MANAGER_BLOCK_ID: |
Sebastian Redl | 93fb9ed | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 1468 | switch (ReadSourceManagerBlock(F)) { |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1469 | case Success: |
| 1470 | break; |
| 1471 | |
| 1472 | case Failure: |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1473 | Error("malformed source manager block in PCH file"); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1474 | return Failure; |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1475 | |
| 1476 | case IgnorePCH: |
| 1477 | return IgnorePCH; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1478 | } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1479 | break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1480 | } |
Sebastian Redl | 93fb9ed | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 1481 | First = false; |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1482 | continue; |
| 1483 | } |
| 1484 | |
| 1485 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
| 1486 | Stream.ReadAbbrevRecord(); |
| 1487 | continue; |
| 1488 | } |
| 1489 | |
| 1490 | // Read and process a record. |
| 1491 | Record.clear(); |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1492 | const char *BlobStart = 0; |
| 1493 | unsigned BlobLen = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1494 | switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record, |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1495 | &BlobStart, &BlobLen)) { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1496 | default: // Default behavior: ignore. |
| 1497 | break; |
| 1498 | |
Sebastian Redl | fbd4bf1 | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1499 | case pch::METADATA: { |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 1500 | if (Record[0] != pch::VERSION_MAJOR && !DisableValidation) { |
Sebastian Redl | fbd4bf1 | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1501 | Diag(Record[0] < pch::VERSION_MAJOR? diag::warn_pch_version_too_old |
| 1502 | : diag::warn_pch_version_too_new); |
| 1503 | return IgnorePCH; |
| 1504 | } |
| 1505 | |
| 1506 | RelocatablePCH = Record[4]; |
| 1507 | if (Listener) { |
| 1508 | std::string TargetTriple(BlobStart, BlobLen); |
| 1509 | if (Listener->ReadTargetTriple(TargetTriple)) |
| 1510 | return IgnorePCH; |
| 1511 | } |
| 1512 | break; |
| 1513 | } |
| 1514 | |
| 1515 | case pch::CHAINED_METADATA: { |
Sebastian Redl | 93fb9ed | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 1516 | if (!First) { |
| 1517 | Error("CHAINED_METADATA is not first record in block"); |
| 1518 | return Failure; |
| 1519 | } |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 1520 | if (Record[0] != pch::VERSION_MAJOR && !DisableValidation) { |
Sebastian Redl | fbd4bf1 | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1521 | Diag(Record[0] < pch::VERSION_MAJOR? diag::warn_pch_version_too_old |
| 1522 | : diag::warn_pch_version_too_new); |
| 1523 | return IgnorePCH; |
| 1524 | } |
| 1525 | |
| 1526 | // Load the chained file. |
| 1527 | switch(ReadPCHCore(llvm::StringRef(BlobStart, BlobLen))) { |
| 1528 | case Failure: return Failure; |
| 1529 | // If we have to ignore the dependency, we'll have to ignore this too. |
| 1530 | case IgnorePCH: return IgnorePCH; |
| 1531 | case Success: break; |
| 1532 | } |
| 1533 | break; |
| 1534 | } |
| 1535 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1536 | case pch::TYPE_OFFSET: |
Sebastian Redl | 12d6da0 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 1537 | if (F.LocalNumTypes != 0) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1538 | Error("duplicate TYPE_OFFSET record in PCH file"); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1539 | return Failure; |
| 1540 | } |
Sebastian Redl | 12d6da0 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 1541 | F.TypeOffsets = (const uint32_t *)BlobStart; |
| 1542 | F.LocalNumTypes = Record[0]; |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1543 | break; |
| 1544 | |
| 1545 | case pch::DECL_OFFSET: |
Sebastian Redl | 12d6da0 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 1546 | if (F.LocalNumDecls != 0) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1547 | Error("duplicate DECL_OFFSET record in PCH file"); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1548 | return Failure; |
| 1549 | } |
Sebastian Redl | 12d6da0 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 1550 | F.DeclOffsets = (const uint32_t *)BlobStart; |
| 1551 | F.LocalNumDecls = Record[0]; |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1552 | break; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1553 | |
Sebastian Redl | d692af7 | 2010-07-27 18:24:41 +0000 | [diff] [blame] | 1554 | case pch::TU_UPDATE_LEXICAL: { |
| 1555 | DeclContextInfo Info = { |
| 1556 | /* No visible information */ 0, 0, |
| 1557 | reinterpret_cast<const pch::DeclID *>(BlobStart), |
| 1558 | BlobLen / sizeof(pch::DeclID) |
| 1559 | }; |
| 1560 | DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info); |
| 1561 | break; |
| 1562 | } |
| 1563 | |
Argyrios Kyrtzidis | a865005 | 2010-08-03 17:30:10 +0000 | [diff] [blame] | 1564 | case pch::REDECLS_UPDATE_LATEST: { |
| 1565 | assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs"); |
| 1566 | for (unsigned i = 0, e = Record.size(); i < e; i += 2) { |
| 1567 | pch::DeclID First = Record[i], Latest = Record[i+1]; |
| 1568 | assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() || |
| 1569 | Latest > FirstLatestDeclIDs[First]) && |
| 1570 | "The new latest is supposed to come after the previous latest"); |
| 1571 | FirstLatestDeclIDs[First] = Latest; |
| 1572 | } |
| 1573 | break; |
| 1574 | } |
| 1575 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1576 | case pch::LANGUAGE_OPTIONS: |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 1577 | if (ParseLanguageOptions(Record) && !DisableValidation) |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1578 | return IgnorePCH; |
| 1579 | break; |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1580 | |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1581 | case pch::IDENTIFIER_TABLE: |
Sebastian Redl | 93fb9ed | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 1582 | F.IdentifierTableData = BlobStart; |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1583 | if (Record[0]) { |
Sebastian Redl | 93fb9ed | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 1584 | F.IdentifierLookupTable |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1585 | = PCHIdentifierLookupTable::Create( |
Sebastian Redl | 93fb9ed | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 1586 | (const unsigned char *)F.IdentifierTableData + Record[0], |
| 1587 | (const unsigned char *)F.IdentifierTableData, |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1588 | PCHIdentifierLookupTrait(*this, F.Stream)); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1589 | if (PP) |
| 1590 | PP->getIdentifierTable().setExternalIdentifierLookup(this); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1591 | } |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1592 | break; |
| 1593 | |
| 1594 | case pch::IDENTIFIER_OFFSET: |
Sebastian Redl | 2da08f9 | 2010-07-19 22:28:42 +0000 | [diff] [blame] | 1595 | if (F.LocalNumIdentifiers != 0) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1596 | Error("duplicate IDENTIFIER_OFFSET record in PCH file"); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1597 | return Failure; |
| 1598 | } |
Sebastian Redl | 2da08f9 | 2010-07-19 22:28:42 +0000 | [diff] [blame] | 1599 | F.IdentifierOffsets = (const uint32_t *)BlobStart; |
| 1600 | F.LocalNumIdentifiers = Record[0]; |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1601 | break; |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1602 | |
| 1603 | case pch::EXTERNAL_DEFINITIONS: |
Sebastian Redl | 518d8cb | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1604 | // Optimization for the first block. |
| 1605 | if (ExternalDefinitions.empty()) |
| 1606 | ExternalDefinitions.swap(Record); |
| 1607 | else |
| 1608 | ExternalDefinitions.insert(ExternalDefinitions.end(), |
| 1609 | Record.begin(), Record.end()); |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1610 | break; |
Douglas Gregor | 3e1af84 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 1611 | |
Douglas Gregor | ad1de00 | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 1612 | case pch::SPECIAL_TYPES: |
Sebastian Redl | 518d8cb | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1613 | // Optimization for the first block |
| 1614 | if (SpecialTypes.empty()) |
| 1615 | SpecialTypes.swap(Record); |
| 1616 | else |
| 1617 | SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end()); |
Douglas Gregor | ad1de00 | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 1618 | break; |
| 1619 | |
Douglas Gregor | 3e1af84 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 1620 | case pch::STATISTICS: |
Sebastian Redl | 518d8cb | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1621 | TotalNumStatements += Record[0]; |
| 1622 | TotalNumMacros += Record[1]; |
| 1623 | TotalLexicalDeclContexts += Record[2]; |
| 1624 | TotalVisibleDeclContexts += Record[3]; |
Douglas Gregor | 3e1af84 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 1625 | break; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1626 | |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 1627 | case pch::TENTATIVE_DEFINITIONS: |
Sebastian Redl | 518d8cb | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1628 | // Optimization for the first block. |
| 1629 | if (TentativeDefinitions.empty()) |
| 1630 | TentativeDefinitions.swap(Record); |
| 1631 | else |
| 1632 | TentativeDefinitions.insert(TentativeDefinitions.end(), |
| 1633 | Record.begin(), Record.end()); |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 1634 | break; |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 1635 | |
Tanya Lattner | e6bbc01 | 2010-02-12 00:07:30 +0000 | [diff] [blame] | 1636 | case pch::UNUSED_STATIC_FUNCS: |
Sebastian Redl | 518d8cb | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1637 | // Optimization for the first block. |
| 1638 | if (UnusedStaticFuncs.empty()) |
| 1639 | UnusedStaticFuncs.swap(Record); |
| 1640 | else |
| 1641 | UnusedStaticFuncs.insert(UnusedStaticFuncs.end(), |
| 1642 | Record.begin(), Record.end()); |
Tanya Lattner | e6bbc01 | 2010-02-12 00:07:30 +0000 | [diff] [blame] | 1643 | break; |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 1644 | |
Argyrios Kyrtzidis | 72b9057 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 1645 | case pch::WEAK_UNDECLARED_IDENTIFIERS: |
Sebastian Redl | 4056680 | 2010-08-05 18:21:25 +0000 | [diff] [blame] | 1646 | // Later blocks overwrite earlier ones. |
| 1647 | WeakUndeclaredIdentifiers.swap(Record); |
Argyrios Kyrtzidis | 72b9057 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 1648 | break; |
| 1649 | |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 1650 | case pch::LOCALLY_SCOPED_EXTERNAL_DECLS: |
Sebastian Redl | 518d8cb | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1651 | // Optimization for the first block. |
| 1652 | if (LocallyScopedExternalDecls.empty()) |
| 1653 | LocallyScopedExternalDecls.swap(Record); |
| 1654 | else |
| 1655 | LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(), |
| 1656 | Record.begin(), Record.end()); |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 1657 | break; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1658 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1659 | case pch::SELECTOR_OFFSETS: |
Sebastian Redl | 059612d | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 1660 | F.SelectorOffsets = (const uint32_t *)BlobStart; |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 1661 | F.LocalNumSelectors = Record[0]; |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1662 | break; |
| 1663 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1664 | case pch::METHOD_POOL: |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 1665 | F.SelectorLookupTableData = (const unsigned char *)BlobStart; |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1666 | if (Record[0]) |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 1667 | F.SelectorLookupTable |
| 1668 | = PCHSelectorLookupTable::Create( |
| 1669 | F.SelectorLookupTableData + Record[0], |
| 1670 | F.SelectorLookupTableData, |
| 1671 | PCHSelectorLookupTrait(*this)); |
Sebastian Redl | fa78dec | 2010-08-04 21:22:45 +0000 | [diff] [blame] | 1672 | TotalNumMethodPoolEntries += Record[1]; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1673 | break; |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 1674 | |
Fariborz Jahanian | 3201983 | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 1675 | case pch::REFERENCED_SELECTOR_POOL: { |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 1676 | ReferencedSelectorsData.insert(ReferencedSelectorsData.end(), |
| 1677 | Record.begin(), Record.end()); |
Fariborz Jahanian | 3201983 | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 1678 | break; |
Sebastian Redl | 681d723 | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 1679 | } |
Fariborz Jahanian | 3201983 | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 1680 | |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 1681 | case pch::PP_COUNTER_VALUE: |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1682 | if (!Record.empty() && Listener) |
| 1683 | Listener->ReadCounter(Record[0]); |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 1684 | break; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1685 | |
| 1686 | case pch::SOURCE_LOCATION_OFFSETS: |
Sebastian Redl | 518d8cb | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1687 | F.SLocOffsets = (const uint32_t *)BlobStart; |
| 1688 | F.LocalNumSLocEntries = Record[0]; |
| 1689 | // We cannot delay this until all PCHs are loaded, because then source |
| 1690 | // location preloads would also have to be delayed. |
| 1691 | TotalNumSLocEntries += F.LocalNumSLocEntries; |
Douglas Gregor | 445e23e | 2009-10-05 21:07:28 +0000 | [diff] [blame] | 1692 | SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, Record[1]); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1693 | break; |
| 1694 | |
| 1695 | case pch::SOURCE_LOCATION_PRELOADS: |
| 1696 | for (unsigned I = 0, N = Record.size(); I != N; ++I) { |
| 1697 | PCHReadResult Result = ReadSLocEntryRecord(Record[I]); |
| 1698 | if (Result != Success) |
| 1699 | return Result; |
| 1700 | } |
| 1701 | break; |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1702 | |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 1703 | case pch::STAT_CACHE: { |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 1704 | PCHStatCache *MyStatCache = |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 1705 | new PCHStatCache((const unsigned char *)BlobStart + Record[0], |
| 1706 | (const unsigned char *)BlobStart, |
| 1707 | NumStatHits, NumStatMisses); |
| 1708 | FileMgr.addStatCache(MyStatCache); |
Sebastian Redl | 9137a52 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 1709 | F.StatCache = MyStatCache; |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1710 | break; |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 1711 | } |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 1712 | |
Douglas Gregor | b81c170 | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 1713 | case pch::EXT_VECTOR_DECLS: |
Sebastian Redl | a9f2368 | 2010-07-28 21:38:49 +0000 | [diff] [blame] | 1714 | // Optimization for the first block. |
| 1715 | if (ExtVectorDecls.empty()) |
| 1716 | ExtVectorDecls.swap(Record); |
| 1717 | else |
| 1718 | ExtVectorDecls.insert(ExtVectorDecls.end(), |
| 1719 | Record.begin(), Record.end()); |
Douglas Gregor | b81c170 | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 1720 | break; |
| 1721 | |
Argyrios Kyrtzidis | d455add | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 1722 | case pch::VTABLE_USES: |
Sebastian Redl | 4056680 | 2010-08-05 18:21:25 +0000 | [diff] [blame] | 1723 | // Later tables overwrite earlier ones. |
Argyrios Kyrtzidis | d455add | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 1724 | VTableUses.swap(Record); |
| 1725 | break; |
| 1726 | |
| 1727 | case pch::DYNAMIC_CLASSES: |
Sebastian Redl | 4056680 | 2010-08-05 18:21:25 +0000 | [diff] [blame] | 1728 | // Optimization for the first block. |
| 1729 | if (DynamicClasses.empty()) |
| 1730 | DynamicClasses.swap(Record); |
| 1731 | else |
| 1732 | DynamicClasses.insert(DynamicClasses.end(), |
| 1733 | Record.begin(), Record.end()); |
Argyrios Kyrtzidis | d455add | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 1734 | break; |
| 1735 | |
Argyrios Kyrtzidis | 0e03638 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 1736 | case pch::PENDING_IMPLICIT_INSTANTIATIONS: |
| 1737 | // Optimization for the first block. |
| 1738 | if (PendingImplicitInstantiations.empty()) |
| 1739 | PendingImplicitInstantiations.swap(Record); |
| 1740 | else |
| 1741 | PendingImplicitInstantiations.insert( |
| 1742 | PendingImplicitInstantiations.end(), Record.begin(), Record.end()); |
| 1743 | break; |
| 1744 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 1745 | case pch::SEMA_DECL_REFS: |
Sebastian Redl | 4056680 | 2010-08-05 18:21:25 +0000 | [diff] [blame] | 1746 | // Later tables overwrite earlier ones. |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 1747 | SemaDeclRefs.swap(Record); |
| 1748 | break; |
| 1749 | |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1750 | case pch::ORIGINAL_FILE_NAME: |
Sebastian Redl | 518d8cb | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1751 | // The primary PCH will be the last to get here, so it will be the one |
| 1752 | // that's used. |
Daniel Dunbar | 7b5a121 | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 1753 | ActualOriginalFileName.assign(BlobStart, BlobLen); |
| 1754 | OriginalFileName = ActualOriginalFileName; |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1755 | MaybeAddSystemRootToFilename(OriginalFileName); |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1756 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1757 | |
Ted Kremenek | 5b4ec63 | 2010-01-22 20:59:36 +0000 | [diff] [blame] | 1758 | case pch::VERSION_CONTROL_BRANCH_REVISION: { |
Ted Kremenek | 974be4d | 2010-02-12 23:31:14 +0000 | [diff] [blame] | 1759 | const std::string &CurBranch = getClangFullRepositoryVersion(); |
Ted Kremenek | 517e676 | 2010-01-22 20:55:35 +0000 | [diff] [blame] | 1760 | llvm::StringRef PCHBranch(BlobStart, BlobLen); |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 1761 | if (llvm::StringRef(CurBranch) != PCHBranch && !DisableValidation) { |
Douglas Gregor | 445e23e | 2009-10-05 21:07:28 +0000 | [diff] [blame] | 1762 | Diag(diag::warn_pch_different_branch) << PCHBranch << CurBranch; |
| 1763 | return IgnorePCH; |
| 1764 | } |
| 1765 | break; |
| 1766 | } |
Sebastian Redl | 04e6fd4 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1767 | |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1768 | case pch::MACRO_DEFINITION_OFFSETS: |
Sebastian Redl | 04e6fd4 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1769 | F.MacroDefinitionOffsets = (const uint32_t *)BlobStart; |
| 1770 | F.NumPreallocatedPreprocessingEntities = Record[0]; |
| 1771 | F.LocalNumMacroDefinitions = Record[1]; |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1772 | break; |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1773 | } |
Sebastian Redl | 93fb9ed | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 1774 | First = false; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1775 | } |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1776 | Error("premature end of bitstream in PCH file"); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1777 | return Failure; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1780 | PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) { |
Sebastian Redl | fbd4bf1 | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1781 | switch(ReadPCHCore(FileName)) { |
Sebastian Redl | cdf3b83 | 2010-07-16 20:41:52 +0000 | [diff] [blame] | 1782 | case Failure: return Failure; |
| 1783 | case IgnorePCH: return IgnorePCH; |
| 1784 | case Success: break; |
| 1785 | } |
Sebastian Redl | fbd4bf1 | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1786 | |
| 1787 | // Here comes stuff that we only do once the entire chain is loaded. |
| 1788 | |
Sebastian Redl | 518d8cb | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1789 | // Allocate space for loaded identifiers, decls and types. |
Sebastian Redl | 04e6fd4 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1790 | unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0, |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 1791 | TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0, |
| 1792 | TotalNumSelectors = 0; |
Sebastian Redl | 12d6da0 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 1793 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
Sebastian Redl | 2da08f9 | 2010-07-19 22:28:42 +0000 | [diff] [blame] | 1794 | TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers; |
Sebastian Redl | 12d6da0 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 1795 | TotalNumTypes += Chain[I]->LocalNumTypes; |
| 1796 | TotalNumDecls += Chain[I]->LocalNumDecls; |
Sebastian Redl | 04e6fd4 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1797 | TotalNumPreallocatedPreprocessingEntities += |
| 1798 | Chain[I]->NumPreallocatedPreprocessingEntities; |
| 1799 | TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions; |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 1800 | TotalNumSelectors += Chain[I]->LocalNumSelectors; |
Sebastian Redl | 12d6da0 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 1801 | } |
Sebastian Redl | 2da08f9 | 2010-07-19 22:28:42 +0000 | [diff] [blame] | 1802 | IdentifiersLoaded.resize(TotalNumIdentifiers); |
Sebastian Redl | 12d6da0 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 1803 | TypesLoaded.resize(TotalNumTypes); |
| 1804 | DeclsLoaded.resize(TotalNumDecls); |
Sebastian Redl | 04e6fd4 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1805 | MacroDefinitionsLoaded.resize(TotalNumMacroDefs); |
| 1806 | if (PP) { |
| 1807 | if (TotalNumIdentifiers > 0) |
| 1808 | PP->getHeaderSearchInfo().SetExternalLookup(this); |
| 1809 | if (TotalNumPreallocatedPreprocessingEntities > 0) { |
| 1810 | if (!PP->getPreprocessingRecord()) |
| 1811 | PP->createPreprocessingRecord(); |
| 1812 | PP->getPreprocessingRecord()->SetExternalSource(*this, |
| 1813 | TotalNumPreallocatedPreprocessingEntities); |
| 1814 | } |
| 1815 | } |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 1816 | SelectorsLoaded.resize(TotalNumSelectors); |
Sebastian Redl | 12d6da0 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 1817 | |
Sebastian Redl | fbd4bf1 | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1818 | // Check the predefines buffers. |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 1819 | if (!DisableValidation && CheckPredefinesBuffers()) |
Sebastian Redl | fbd4bf1 | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1820 | return IgnorePCH; |
| 1821 | |
| 1822 | if (PP) { |
| 1823 | // Initialization of keywords and pragmas occurs before the |
| 1824 | // PCH file is read, so there may be some identifiers that were |
| 1825 | // loaded into the IdentifierTable before we intercepted the |
| 1826 | // creation of identifiers. Iterate through the list of known |
| 1827 | // identifiers and determine whether we have to establish |
| 1828 | // preprocessor definitions or top-level identifier declaration |
| 1829 | // chains for those identifiers. |
| 1830 | // |
| 1831 | // We copy the IdentifierInfo pointers to a small vector first, |
| 1832 | // since de-serializing declarations or macro definitions can add |
| 1833 | // new entries into the identifier table, invalidating the |
| 1834 | // iterators. |
| 1835 | llvm::SmallVector<IdentifierInfo *, 128> Identifiers; |
| 1836 | for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(), |
| 1837 | IdEnd = PP->getIdentifierTable().end(); |
| 1838 | Id != IdEnd; ++Id) |
| 1839 | Identifiers.push_back(Id->second); |
Sebastian Redl | 04e6fd4 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1840 | // We need to search the tables in all files. |
Sebastian Redl | 04e6fd4 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1841 | for (unsigned J = 0, M = Chain.size(); J != M; ++J) { |
| 1842 | PCHIdentifierLookupTable *IdTable |
| 1843 | = (PCHIdentifierLookupTable *)Chain[J]->IdentifierLookupTable; |
Sebastian Redl | 0fa7d0b | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 1844 | // Not all PCH files necessarily have identifier tables, only the useful |
| 1845 | // ones. |
| 1846 | if (!IdTable) |
| 1847 | continue; |
Sebastian Redl | 04e6fd4 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1848 | for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) { |
| 1849 | IdentifierInfo *II = Identifiers[I]; |
| 1850 | // Look in the on-disk hash tables for an entry for this identifier |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1851 | PCHIdentifierLookupTrait Info(*this, Chain[J]->Stream, II); |
Sebastian Redl | 04e6fd4 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1852 | std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength()); |
Sebastian Redl | 518d8cb | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1853 | PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info); |
| 1854 | if (Pos == IdTable->end()) |
| 1855 | continue; |
Sebastian Redl | fbd4bf1 | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1856 | |
Sebastian Redl | 518d8cb | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1857 | // Dereferencing the iterator has the effect of populating the |
| 1858 | // IdentifierInfo node with the various declarations it needs. |
| 1859 | (void)*Pos; |
| 1860 | } |
Sebastian Redl | fbd4bf1 | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | if (Context) |
| 1865 | InitializeContext(*Context); |
| 1866 | |
| 1867 | return Success; |
| 1868 | } |
| 1869 | |
| 1870 | PCHReader::PCHReadResult PCHReader::ReadPCHCore(llvm::StringRef FileName) { |
| 1871 | Chain.push_back(new PerFileData()); |
Sebastian Redl | 9137a52 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 1872 | PerFileData &F = *Chain.back(); |
Sebastian Redl | fbd4bf1 | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1873 | |
| 1874 | // Set the PCH file name. |
| 1875 | F.FileName = FileName; |
| 1876 | |
| 1877 | // Open the PCH file. |
| 1878 | // |
| 1879 | // FIXME: This shouldn't be here, we should just take a raw_ostream. |
| 1880 | std::string ErrStr; |
| 1881 | F.Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr)); |
| 1882 | if (!F.Buffer) { |
| 1883 | Error(ErrStr.c_str()); |
| 1884 | return IgnorePCH; |
| 1885 | } |
| 1886 | |
| 1887 | // Initialize the stream |
| 1888 | F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(), |
| 1889 | (const unsigned char *)F.Buffer->getBufferEnd()); |
Sebastian Redl | 9137a52 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 1890 | llvm::BitstreamCursor &Stream = F.Stream; |
Sebastian Redl | fbd4bf1 | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1891 | Stream.init(F.StreamFile); |
Sebastian Redl | 04e6fd4 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1892 | F.SizeInBits = F.Buffer->getBufferSize() * 8; |
Sebastian Redl | fbd4bf1 | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1893 | |
| 1894 | // Sniff for the signature. |
| 1895 | if (Stream.Read(8) != 'C' || |
| 1896 | Stream.Read(8) != 'P' || |
| 1897 | Stream.Read(8) != 'C' || |
| 1898 | Stream.Read(8) != 'H') { |
| 1899 | Diag(diag::err_not_a_pch_file) << FileName; |
| 1900 | return Failure; |
| 1901 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1902 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1903 | while (!Stream.AtEndOfStream()) { |
| 1904 | unsigned Code = Stream.ReadCode(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1905 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1906 | if (Code != llvm::bitc::ENTER_SUBBLOCK) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1907 | Error("invalid record at top-level of PCH file"); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1908 | return Failure; |
| 1909 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1910 | |
| 1911 | unsigned BlockID = Stream.ReadSubBlockID(); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1912 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1913 | // We only know the PCH subblock ID. |
| 1914 | switch (BlockID) { |
| 1915 | case llvm::bitc::BLOCKINFO_BLOCK_ID: |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1916 | if (Stream.ReadBlockInfoBlock()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1917 | Error("malformed BlockInfoBlock in PCH file"); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1918 | return Failure; |
| 1919 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1920 | break; |
| 1921 | case pch::PCH_BLOCK_ID: |
Sebastian Redl | cdf3b83 | 2010-07-16 20:41:52 +0000 | [diff] [blame] | 1922 | switch (ReadPCHBlock(F)) { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1923 | case Success: |
| 1924 | break; |
| 1925 | |
| 1926 | case Failure: |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1927 | return Failure; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1928 | |
| 1929 | case IgnorePCH: |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1930 | // FIXME: We could consider reading through to the end of this |
| 1931 | // PCH block, skipping subblocks, to see if there are other |
| 1932 | // PCH blocks elsewhere. |
Douglas Gregor | 2bf1eb0 | 2009-04-27 21:28:04 +0000 | [diff] [blame] | 1933 | |
| 1934 | // Clear out any preallocated source location entries, so that |
| 1935 | // the source manager does not try to resolve them later. |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1936 | SourceMgr.ClearPreallocatedSLocEntries(); |
Douglas Gregor | 2bf1eb0 | 2009-04-27 21:28:04 +0000 | [diff] [blame] | 1937 | |
| 1938 | // Remove the stat cache. |
Sebastian Redl | 9137a52 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 1939 | if (F.StatCache) |
| 1940 | FileMgr.removeStatCache((PCHStatCache*)F.StatCache); |
Douglas Gregor | 2bf1eb0 | 2009-04-27 21:28:04 +0000 | [diff] [blame] | 1941 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1942 | return IgnorePCH; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1943 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1944 | break; |
| 1945 | default: |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1946 | if (Stream.SkipBlock()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1947 | Error("malformed block record in PCH file"); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1948 | return Failure; |
| 1949 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1950 | break; |
| 1951 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Sebastian Redl | cdf3b83 | 2010-07-16 20:41:52 +0000 | [diff] [blame] | 1954 | return Success; |
| 1955 | } |
| 1956 | |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1957 | void PCHReader::setPreprocessor(Preprocessor &pp) { |
| 1958 | PP = &pp; |
Sebastian Redl | 04e6fd4 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1959 | |
| 1960 | unsigned TotalNum = 0; |
| 1961 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) |
| 1962 | TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities; |
| 1963 | if (TotalNum) { |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1964 | if (!PP->getPreprocessingRecord()) |
| 1965 | PP->createPreprocessingRecord(); |
Sebastian Redl | 04e6fd4 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1966 | PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum); |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1967 | } |
| 1968 | } |
| 1969 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1970 | void PCHReader::InitializeContext(ASTContext &Ctx) { |
| 1971 | Context = &Ctx; |
| 1972 | assert(Context && "Passed null context!"); |
| 1973 | |
| 1974 | assert(PP && "Forgot to set Preprocessor ?"); |
| 1975 | PP->getIdentifierTable().setExternalIdentifierLookup(this); |
| 1976 | PP->getHeaderSearchInfo().SetExternalLookup(this); |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1977 | PP->setExternalSource(this); |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 1978 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1979 | // Load the translation unit declaration |
Argyrios Kyrtzidis | 8871a44 | 2010-07-08 17:13:02 +0000 | [diff] [blame] | 1980 | GetTranslationUnitDecl(); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1981 | |
| 1982 | // Load the special types. |
| 1983 | Context->setBuiltinVaListType( |
| 1984 | GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST])); |
| 1985 | if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID]) |
| 1986 | Context->setObjCIdType(GetType(Id)); |
| 1987 | if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR]) |
| 1988 | Context->setObjCSelType(GetType(Sel)); |
| 1989 | if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL]) |
| 1990 | Context->setObjCProtoType(GetType(Proto)); |
| 1991 | if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS]) |
| 1992 | Context->setObjCClassType(GetType(Class)); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1993 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1994 | if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING]) |
| 1995 | Context->setCFConstantStringType(GetType(String)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1996 | if (unsigned FastEnum |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1997 | = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE]) |
| 1998 | Context->setObjCFastEnumerationStateType(GetType(FastEnum)); |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 1999 | if (unsigned File = SpecialTypes[pch::SPECIAL_TYPE_FILE]) { |
| 2000 | QualType FileType = GetType(File); |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2001 | if (FileType.isNull()) { |
| 2002 | Error("FILE type is NULL"); |
| 2003 | return; |
| 2004 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2005 | if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 2006 | Context->setFILEDecl(Typedef->getDecl()); |
| 2007 | else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2008 | const TagType *Tag = FileType->getAs<TagType>(); |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2009 | if (!Tag) { |
| 2010 | Error("Invalid FILE type in PCH file"); |
| 2011 | return; |
| 2012 | } |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 2013 | Context->setFILEDecl(Tag->getDecl()); |
| 2014 | } |
| 2015 | } |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 2016 | if (unsigned Jmp_buf = SpecialTypes[pch::SPECIAL_TYPE_jmp_buf]) { |
| 2017 | QualType Jmp_bufType = GetType(Jmp_buf); |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2018 | if (Jmp_bufType.isNull()) { |
| 2019 | Error("jmp_bug type is NULL"); |
| 2020 | return; |
| 2021 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2022 | if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 2023 | Context->setjmp_bufDecl(Typedef->getDecl()); |
| 2024 | else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2025 | const TagType *Tag = Jmp_bufType->getAs<TagType>(); |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2026 | if (!Tag) { |
| 2027 | Error("Invalid jmp_bug type in PCH file"); |
| 2028 | return; |
| 2029 | } |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 2030 | Context->setjmp_bufDecl(Tag->getDecl()); |
| 2031 | } |
| 2032 | } |
| 2033 | if (unsigned Sigjmp_buf = SpecialTypes[pch::SPECIAL_TYPE_sigjmp_buf]) { |
| 2034 | QualType Sigjmp_bufType = GetType(Sigjmp_buf); |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2035 | if (Sigjmp_bufType.isNull()) { |
| 2036 | Error("sigjmp_buf type is NULL"); |
| 2037 | return; |
| 2038 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2039 | if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 2040 | Context->setsigjmp_bufDecl(Typedef->getDecl()); |
| 2041 | else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2042 | const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 2043 | assert(Tag && "Invalid sigjmp_buf type in PCH file"); |
| 2044 | Context->setsigjmp_bufDecl(Tag->getDecl()); |
| 2045 | } |
| 2046 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2047 | if (unsigned ObjCIdRedef |
Douglas Gregor | d1571ac | 2009-08-21 00:27:50 +0000 | [diff] [blame] | 2048 | = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID_REDEFINITION]) |
| 2049 | Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2050 | if (unsigned ObjCClassRedef |
Douglas Gregor | d1571ac | 2009-08-21 00:27:50 +0000 | [diff] [blame] | 2051 | = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) |
| 2052 | Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2053 | if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_BLOCK_DESCRIPTOR]) |
| 2054 | Context->setBlockDescriptorType(GetType(String)); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2055 | if (unsigned String |
| 2056 | = SpecialTypes[pch::SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR]) |
| 2057 | Context->setBlockDescriptorExtendedType(GetType(String)); |
Fariborz Jahanian | 2bb5dda | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2058 | if (unsigned ObjCSelRedef |
| 2059 | = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) |
| 2060 | Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef); |
| 2061 | if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_NS_CONSTANT_STRING]) |
| 2062 | Context->setNSConstantStringType(GetType(String)); |
Argyrios Kyrtzidis | 0061138 | 2010-07-04 21:44:19 +0000 | [diff] [blame] | 2063 | |
| 2064 | if (SpecialTypes[pch::SPECIAL_TYPE_INT128_INSTALLED]) |
| 2065 | Context->setInt128Installed(); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2068 | /// \brief Retrieve the name of the original source file name |
| 2069 | /// directly from the PCH file, without actually loading the PCH |
| 2070 | /// file. |
Daniel Dunbar | 93ebb1b | 2009-12-03 09:13:06 +0000 | [diff] [blame] | 2071 | std::string PCHReader::getOriginalSourceFile(const std::string &PCHFileName, |
| 2072 | Diagnostic &Diags) { |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2073 | // Open the PCH file. |
| 2074 | std::string ErrStr; |
| 2075 | llvm::OwningPtr<llvm::MemoryBuffer> Buffer; |
| 2076 | Buffer.reset(llvm::MemoryBuffer::getFile(PCHFileName.c_str(), &ErrStr)); |
| 2077 | if (!Buffer) { |
Daniel Dunbar | 93ebb1b | 2009-12-03 09:13:06 +0000 | [diff] [blame] | 2078 | Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr; |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2079 | return std::string(); |
| 2080 | } |
| 2081 | |
| 2082 | // Initialize the stream |
| 2083 | llvm::BitstreamReader StreamFile; |
| 2084 | llvm::BitstreamCursor Stream; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2085 | StreamFile.init((const unsigned char *)Buffer->getBufferStart(), |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2086 | (const unsigned char *)Buffer->getBufferEnd()); |
| 2087 | Stream.init(StreamFile); |
| 2088 | |
| 2089 | // Sniff for the signature. |
| 2090 | if (Stream.Read(8) != 'C' || |
| 2091 | Stream.Read(8) != 'P' || |
| 2092 | Stream.Read(8) != 'C' || |
| 2093 | Stream.Read(8) != 'H') { |
Daniel Dunbar | 93ebb1b | 2009-12-03 09:13:06 +0000 | [diff] [blame] | 2094 | Diags.Report(diag::err_fe_not_a_pch_file) << PCHFileName; |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2095 | return std::string(); |
| 2096 | } |
| 2097 | |
| 2098 | RecordData Record; |
| 2099 | while (!Stream.AtEndOfStream()) { |
| 2100 | unsigned Code = Stream.ReadCode(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2102 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 2103 | unsigned BlockID = Stream.ReadSubBlockID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2104 | |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2105 | // We only know the PCH subblock ID. |
| 2106 | switch (BlockID) { |
| 2107 | case pch::PCH_BLOCK_ID: |
| 2108 | if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) { |
Daniel Dunbar | 93ebb1b | 2009-12-03 09:13:06 +0000 | [diff] [blame] | 2109 | Diags.Report(diag::err_fe_pch_malformed_block) << PCHFileName; |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2110 | return std::string(); |
| 2111 | } |
| 2112 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2113 | |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2114 | default: |
| 2115 | if (Stream.SkipBlock()) { |
Daniel Dunbar | 93ebb1b | 2009-12-03 09:13:06 +0000 | [diff] [blame] | 2116 | Diags.Report(diag::err_fe_pch_malformed_block) << PCHFileName; |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2117 | return std::string(); |
| 2118 | } |
| 2119 | break; |
| 2120 | } |
| 2121 | continue; |
| 2122 | } |
| 2123 | |
| 2124 | if (Code == llvm::bitc::END_BLOCK) { |
| 2125 | if (Stream.ReadBlockEnd()) { |
Daniel Dunbar | 93ebb1b | 2009-12-03 09:13:06 +0000 | [diff] [blame] | 2126 | Diags.Report(diag::err_fe_pch_error_at_end_block) << PCHFileName; |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2127 | return std::string(); |
| 2128 | } |
| 2129 | continue; |
| 2130 | } |
| 2131 | |
| 2132 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
| 2133 | Stream.ReadAbbrevRecord(); |
| 2134 | continue; |
| 2135 | } |
| 2136 | |
| 2137 | Record.clear(); |
| 2138 | const char *BlobStart = 0; |
| 2139 | unsigned BlobLen = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2140 | if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2141 | == pch::ORIGINAL_FILE_NAME) |
| 2142 | return std::string(BlobStart, BlobLen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2143 | } |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2144 | |
| 2145 | return std::string(); |
| 2146 | } |
| 2147 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 2148 | /// \brief Parse the record that corresponds to a LangOptions data |
| 2149 | /// structure. |
| 2150 | /// |
| 2151 | /// This routine compares the language options used to generate the |
| 2152 | /// PCH file against the language options set for the current |
| 2153 | /// compilation. For each option, we classify differences between the |
| 2154 | /// two compiler states as either "benign" or "important". Benign |
| 2155 | /// differences don't matter, and we accept them without complaint |
| 2156 | /// (and without modifying the language options). Differences between |
| 2157 | /// the states for important options cause the PCH file to be |
| 2158 | /// unusable, so we emit a warning and return true to indicate that |
| 2159 | /// there was an error. |
| 2160 | /// |
| 2161 | /// \returns true if the PCH file is unacceptable, false otherwise. |
| 2162 | bool PCHReader::ParseLanguageOptions( |
| 2163 | const llvm::SmallVectorImpl<uint64_t> &Record) { |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2164 | if (Listener) { |
| 2165 | LangOptions LangOpts; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2166 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2167 | #define PARSE_LANGOPT(Option) \ |
| 2168 | LangOpts.Option = Record[Idx]; \ |
| 2169 | ++Idx |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2170 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2171 | unsigned Idx = 0; |
| 2172 | PARSE_LANGOPT(Trigraphs); |
| 2173 | PARSE_LANGOPT(BCPLComment); |
| 2174 | PARSE_LANGOPT(DollarIdents); |
| 2175 | PARSE_LANGOPT(AsmPreprocessor); |
| 2176 | PARSE_LANGOPT(GNUMode); |
Chandler Carruth | eb5d7b7 | 2010-04-17 20:17:31 +0000 | [diff] [blame] | 2177 | PARSE_LANGOPT(GNUKeywords); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2178 | PARSE_LANGOPT(ImplicitInt); |
| 2179 | PARSE_LANGOPT(Digraphs); |
| 2180 | PARSE_LANGOPT(HexFloats); |
| 2181 | PARSE_LANGOPT(C99); |
| 2182 | PARSE_LANGOPT(Microsoft); |
| 2183 | PARSE_LANGOPT(CPlusPlus); |
| 2184 | PARSE_LANGOPT(CPlusPlus0x); |
| 2185 | PARSE_LANGOPT(CXXOperatorNames); |
| 2186 | PARSE_LANGOPT(ObjC1); |
| 2187 | PARSE_LANGOPT(ObjC2); |
| 2188 | PARSE_LANGOPT(ObjCNonFragileABI); |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 2189 | PARSE_LANGOPT(ObjCNonFragileABI2); |
Fariborz Jahanian | 4c9d8d0 | 2010-04-22 21:01:59 +0000 | [diff] [blame] | 2190 | PARSE_LANGOPT(NoConstantCFStrings); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2191 | PARSE_LANGOPT(PascalStrings); |
| 2192 | PARSE_LANGOPT(WritableStrings); |
| 2193 | PARSE_LANGOPT(LaxVectorConversions); |
Nate Begeman | b9e7e63 | 2009-06-25 23:01:11 +0000 | [diff] [blame] | 2194 | PARSE_LANGOPT(AltiVec); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2195 | PARSE_LANGOPT(Exceptions); |
Daniel Dunbar | 7348288 | 2010-02-10 18:48:44 +0000 | [diff] [blame] | 2196 | PARSE_LANGOPT(SjLjExceptions); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2197 | PARSE_LANGOPT(NeXTRuntime); |
| 2198 | PARSE_LANGOPT(Freestanding); |
| 2199 | PARSE_LANGOPT(NoBuiltin); |
| 2200 | PARSE_LANGOPT(ThreadsafeStatics); |
Douglas Gregor | 972d954 | 2009-09-03 14:36:33 +0000 | [diff] [blame] | 2201 | PARSE_LANGOPT(POSIXThreads); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2202 | PARSE_LANGOPT(Blocks); |
| 2203 | PARSE_LANGOPT(EmitAllDecls); |
| 2204 | PARSE_LANGOPT(MathErrno); |
Chris Lattner | a4d7145 | 2010-06-26 21:25:03 +0000 | [diff] [blame] | 2205 | LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy) |
| 2206 | Record[Idx++]); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2207 | PARSE_LANGOPT(HeinousExtensions); |
| 2208 | PARSE_LANGOPT(Optimize); |
| 2209 | PARSE_LANGOPT(OptimizeSize); |
| 2210 | PARSE_LANGOPT(Static); |
| 2211 | PARSE_LANGOPT(PICLevel); |
| 2212 | PARSE_LANGOPT(GNUInline); |
| 2213 | PARSE_LANGOPT(NoInline); |
| 2214 | PARSE_LANGOPT(AccessControl); |
| 2215 | PARSE_LANGOPT(CharIsSigned); |
John Thompson | a6fda12 | 2009-11-05 20:14:16 +0000 | [diff] [blame] | 2216 | PARSE_LANGOPT(ShortWChar); |
Chris Lattner | a4d7145 | 2010-06-26 21:25:03 +0000 | [diff] [blame] | 2217 | LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]); |
| 2218 | LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]); |
Daniel Dunbar | ab8e281 | 2009-09-21 04:16:19 +0000 | [diff] [blame] | 2219 | LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode) |
Chris Lattner | a4d7145 | 2010-06-26 21:25:03 +0000 | [diff] [blame] | 2220 | Record[Idx++]); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2221 | PARSE_LANGOPT(InstantiationDepth); |
Nate Begeman | b9e7e63 | 2009-06-25 23:01:11 +0000 | [diff] [blame] | 2222 | PARSE_LANGOPT(OpenCL); |
Mike Stump | 9c276ae | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 2223 | PARSE_LANGOPT(CatchUndefined); |
| 2224 | // FIXME: Missing ElideConstructors?! |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2225 | #undef PARSE_LANGOPT |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 2226 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2227 | return Listener->ReadLanguageOptions(LangOpts); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 2228 | } |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 2229 | |
| 2230 | return false; |
| 2231 | } |
| 2232 | |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 2233 | void PCHReader::ReadPreprocessedEntities() { |
| 2234 | ReadDefinedMacros(); |
| 2235 | } |
| 2236 | |
Sebastian Redl | aaec0aa | 2010-07-20 22:37:49 +0000 | [diff] [blame] | 2237 | /// \brief Get the correct cursor and offset for loading a type. |
| 2238 | PCHReader::RecordLocation PCHReader::TypeCursorForIndex(unsigned Index) { |
| 2239 | PerFileData *F = 0; |
| 2240 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
| 2241 | F = Chain[N - I - 1]; |
| 2242 | if (Index < F->LocalNumTypes) |
| 2243 | break; |
| 2244 | Index -= F->LocalNumTypes; |
| 2245 | } |
| 2246 | assert(F && F->LocalNumTypes > Index && "Broken chain"); |
Sebastian Redl | 971dd44 | 2010-07-20 22:55:31 +0000 | [diff] [blame] | 2247 | return RecordLocation(&F->DeclsCursor, F->TypeOffsets[Index]); |
Sebastian Redl | aaec0aa | 2010-07-20 22:37:49 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | /// \brief Read and return the type with the given index.. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2251 | /// |
Sebastian Redl | aaec0aa | 2010-07-20 22:37:49 +0000 | [diff] [blame] | 2252 | /// The index is the type ID, shifted and minus the number of predefs. This |
| 2253 | /// routine actually reads the record corresponding to the type at the given |
| 2254 | /// location. It is a helper routine for GetType, which deals with reading type |
| 2255 | /// IDs. |
| 2256 | QualType PCHReader::ReadTypeRecord(unsigned Index) { |
| 2257 | RecordLocation Loc = TypeCursorForIndex(Index); |
Sebastian Redl | 971dd44 | 2010-07-20 22:55:31 +0000 | [diff] [blame] | 2258 | llvm::BitstreamCursor &DeclsCursor = *Loc.first; |
Sebastian Redl | 9137a52 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 2259 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 2260 | // Keep track of where we are in the stream, then jump back there |
| 2261 | // after reading this type. |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 2262 | SavedStreamPosition SavedPosition(DeclsCursor); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 2263 | |
Argyrios Kyrtzidis | 919e693 | 2010-06-28 22:28:35 +0000 | [diff] [blame] | 2264 | ReadingKindTracker ReadingKind(Read_Type, *this); |
| 2265 | |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 2266 | // Note that we are loading a type record. |
Argyrios Kyrtzidis | 29ee3a2 | 2010-07-30 10:03:16 +0000 | [diff] [blame] | 2267 | Deserializing AType(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2268 | |
Sebastian Redl | aaec0aa | 2010-07-20 22:37:49 +0000 | [diff] [blame] | 2269 | DeclsCursor.JumpToBit(Loc.second); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2270 | RecordData Record; |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 2271 | unsigned Code = DeclsCursor.ReadCode(); |
| 2272 | switch ((pch::TypeCode)DeclsCursor.ReadRecord(Code, Record)) { |
Douglas Gregor | 6d47396 | 2009-04-15 22:00:08 +0000 | [diff] [blame] | 2273 | case pch::TYPE_EXT_QUAL: { |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2274 | if (Record.size() != 2) { |
| 2275 | Error("Incorrect encoding of extended qualifier type"); |
| 2276 | return QualType(); |
| 2277 | } |
Douglas Gregor | 6d47396 | 2009-04-15 22:00:08 +0000 | [diff] [blame] | 2278 | QualType Base = GetType(Record[0]); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2279 | Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]); |
| 2280 | return Context->getQualifiedType(Base, Quals); |
Douglas Gregor | 6d47396 | 2009-04-15 22:00:08 +0000 | [diff] [blame] | 2281 | } |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2282 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2283 | case pch::TYPE_COMPLEX: { |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2284 | if (Record.size() != 1) { |
| 2285 | Error("Incorrect encoding of complex type"); |
| 2286 | return QualType(); |
| 2287 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2288 | QualType ElemType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2289 | return Context->getComplexType(ElemType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | case pch::TYPE_POINTER: { |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2293 | if (Record.size() != 1) { |
| 2294 | Error("Incorrect encoding of pointer type"); |
| 2295 | return QualType(); |
| 2296 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2297 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2298 | return Context->getPointerType(PointeeType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
| 2301 | case pch::TYPE_BLOCK_POINTER: { |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2302 | if (Record.size() != 1) { |
| 2303 | Error("Incorrect encoding of block pointer type"); |
| 2304 | return QualType(); |
| 2305 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2306 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2307 | return Context->getBlockPointerType(PointeeType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
| 2310 | case pch::TYPE_LVALUE_REFERENCE: { |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2311 | if (Record.size() != 1) { |
| 2312 | Error("Incorrect encoding of lvalue reference type"); |
| 2313 | return QualType(); |
| 2314 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2315 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2316 | return Context->getLValueReferenceType(PointeeType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | case pch::TYPE_RVALUE_REFERENCE: { |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2320 | if (Record.size() != 1) { |
| 2321 | Error("Incorrect encoding of rvalue reference type"); |
| 2322 | return QualType(); |
| 2323 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2324 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2325 | return Context->getRValueReferenceType(PointeeType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | case pch::TYPE_MEMBER_POINTER: { |
Argyrios Kyrtzidis | 240437b | 2010-07-02 11:55:15 +0000 | [diff] [blame] | 2329 | if (Record.size() != 2) { |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2330 | Error("Incorrect encoding of member pointer type"); |
| 2331 | return QualType(); |
| 2332 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2333 | QualType PointeeType = GetType(Record[0]); |
| 2334 | QualType ClassType = GetType(Record[1]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2335 | return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr()); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2336 | } |
| 2337 | |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2338 | case pch::TYPE_CONSTANT_ARRAY: { |
| 2339 | QualType ElementType = GetType(Record[0]); |
| 2340 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 2341 | unsigned IndexTypeQuals = Record[2]; |
| 2342 | unsigned Idx = 3; |
| 2343 | llvm::APInt Size = ReadAPInt(Record, Idx); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2344 | return Context->getConstantArrayType(ElementType, Size, |
| 2345 | ASM, IndexTypeQuals); |
| 2346 | } |
| 2347 | |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2348 | case pch::TYPE_INCOMPLETE_ARRAY: { |
| 2349 | QualType ElementType = GetType(Record[0]); |
| 2350 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 2351 | unsigned IndexTypeQuals = Record[2]; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2352 | return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | case pch::TYPE_VARIABLE_ARRAY: { |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 2356 | QualType ElementType = GetType(Record[0]); |
| 2357 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 2358 | unsigned IndexTypeQuals = Record[2]; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2359 | SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]); |
| 2360 | SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]); |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2361 | return Context->getVariableArrayType(ElementType, ReadExpr(DeclsCursor), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2362 | ASM, IndexTypeQuals, |
| 2363 | SourceRange(LBLoc, RBLoc)); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | case pch::TYPE_VECTOR: { |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2367 | if (Record.size() != 3) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2368 | Error("incorrect encoding of vector type in PCH file"); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2369 | return QualType(); |
| 2370 | } |
| 2371 | |
| 2372 | QualType ElementType = GetType(Record[0]); |
| 2373 | unsigned NumElements = Record[1]; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2374 | unsigned AltiVecSpec = Record[2]; |
| 2375 | return Context->getVectorType(ElementType, NumElements, |
| 2376 | (VectorType::AltiVecSpecific)AltiVecSpec); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | case pch::TYPE_EXT_VECTOR: { |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2380 | if (Record.size() != 3) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2381 | Error("incorrect encoding of extended vector type in PCH file"); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2382 | return QualType(); |
| 2383 | } |
| 2384 | |
| 2385 | QualType ElementType = GetType(Record[0]); |
| 2386 | unsigned NumElements = Record[1]; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2387 | return Context->getExtVectorType(ElementType, NumElements); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
| 2390 | case pch::TYPE_FUNCTION_NO_PROTO: { |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 2391 | if (Record.size() != 4) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2392 | Error("incorrect encoding of no-proto function type"); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2393 | return QualType(); |
| 2394 | } |
| 2395 | QualType ResultType = GetType(Record[0]); |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 2396 | FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]); |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2397 | return Context->getFunctionNoProtoType(ResultType, Info); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
| 2400 | case pch::TYPE_FUNCTION_PROTO: { |
| 2401 | QualType ResultType = GetType(Record[0]); |
Douglas Gregor | 9123666 | 2009-12-22 18:11:50 +0000 | [diff] [blame] | 2402 | bool NoReturn = Record[1]; |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 2403 | unsigned RegParm = Record[2]; |
| 2404 | CallingConv CallConv = (CallingConv)Record[3]; |
| 2405 | unsigned Idx = 4; |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2406 | unsigned NumParams = Record[Idx++]; |
| 2407 | llvm::SmallVector<QualType, 16> ParamTypes; |
| 2408 | for (unsigned I = 0; I != NumParams; ++I) |
| 2409 | ParamTypes.push_back(GetType(Record[Idx++])); |
| 2410 | bool isVariadic = Record[Idx++]; |
| 2411 | unsigned Quals = Record[Idx++]; |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2412 | bool hasExceptionSpec = Record[Idx++]; |
| 2413 | bool hasAnyExceptionSpec = Record[Idx++]; |
| 2414 | unsigned NumExceptions = Record[Idx++]; |
| 2415 | llvm::SmallVector<QualType, 2> Exceptions; |
| 2416 | for (unsigned I = 0; I != NumExceptions; ++I) |
| 2417 | Exceptions.push_back(GetType(Record[Idx++])); |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2418 | return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2419 | isVariadic, Quals, hasExceptionSpec, |
| 2420 | hasAnyExceptionSpec, NumExceptions, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2421 | Exceptions.data(), |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 2422 | FunctionType::ExtInfo(NoReturn, RegParm, |
| 2423 | CallConv)); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2426 | case pch::TYPE_UNRESOLVED_USING: |
| 2427 | return Context->getTypeDeclType( |
| 2428 | cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0]))); |
| 2429 | |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2430 | case pch::TYPE_TYPEDEF: { |
| 2431 | if (Record.size() != 2) { |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2432 | Error("incorrect encoding of typedef type"); |
| 2433 | return QualType(); |
| 2434 | } |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2435 | TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0])); |
| 2436 | QualType Canonical = GetType(Record[1]); |
| 2437 | return Context->getTypedefType(Decl, Canonical); |
| 2438 | } |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2439 | |
| 2440 | case pch::TYPE_TYPEOF_EXPR: |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2441 | return Context->getTypeOfExprType(ReadExpr(DeclsCursor)); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2442 | |
| 2443 | case pch::TYPE_TYPEOF: { |
| 2444 | if (Record.size() != 1) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2445 | Error("incorrect encoding of typeof(type) in PCH file"); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2446 | return QualType(); |
| 2447 | } |
| 2448 | QualType UnderlyingType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2449 | return Context->getTypeOfType(UnderlyingType); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2450 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2451 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2452 | case pch::TYPE_DECLTYPE: |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2453 | return Context->getDecltypeType(ReadExpr(DeclsCursor)); |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2454 | |
Argyrios Kyrtzidis | be19110 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 2455 | case pch::TYPE_RECORD: { |
| 2456 | if (Record.size() != 2) { |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2457 | Error("incorrect encoding of record type"); |
| 2458 | return QualType(); |
| 2459 | } |
Argyrios Kyrtzidis | be19110 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 2460 | bool IsDependent = Record[0]; |
| 2461 | QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1]))); |
| 2462 | T->Dependent = IsDependent; |
| 2463 | return T; |
| 2464 | } |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2465 | |
Argyrios Kyrtzidis | be19110 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 2466 | case pch::TYPE_ENUM: { |
| 2467 | if (Record.size() != 2) { |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2468 | Error("incorrect encoding of enum type"); |
| 2469 | return QualType(); |
| 2470 | } |
Argyrios Kyrtzidis | be19110 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 2471 | bool IsDependent = Record[0]; |
| 2472 | QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1]))); |
| 2473 | T->Dependent = IsDependent; |
| 2474 | return T; |
| 2475 | } |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 2476 | |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 2477 | case pch::TYPE_ELABORATED: { |
Argyrios Kyrtzidis | 3acad62 | 2010-06-25 16:24:58 +0000 | [diff] [blame] | 2478 | unsigned Idx = 0; |
| 2479 | ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; |
| 2480 | NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); |
| 2481 | QualType NamedType = GetType(Record[Idx++]); |
| 2482 | return Context->getElaboratedType(Keyword, NNS, NamedType); |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2485 | case pch::TYPE_OBJC_INTERFACE: { |
Chris Lattner | c6fa445 | 2009-04-22 06:45:28 +0000 | [diff] [blame] | 2486 | unsigned Idx = 0; |
| 2487 | ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++])); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2488 | return Context->getObjCInterfaceType(ItfD); |
| 2489 | } |
| 2490 | |
| 2491 | case pch::TYPE_OBJC_OBJECT: { |
| 2492 | unsigned Idx = 0; |
| 2493 | QualType Base = GetType(Record[Idx++]); |
Chris Lattner | c6fa445 | 2009-04-22 06:45:28 +0000 | [diff] [blame] | 2494 | unsigned NumProtos = Record[Idx++]; |
| 2495 | llvm::SmallVector<ObjCProtocolDecl*, 4> Protos; |
| 2496 | for (unsigned I = 0; I != NumProtos; ++I) |
| 2497 | Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++]))); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2498 | return Context->getObjCObjectType(Base, Protos.data(), NumProtos); |
Chris Lattner | c6fa445 | 2009-04-22 06:45:28 +0000 | [diff] [blame] | 2499 | } |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2500 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2501 | case pch::TYPE_OBJC_OBJECT_POINTER: { |
Chris Lattner | d7a3fcd | 2009-04-22 06:40:03 +0000 | [diff] [blame] | 2502 | unsigned Idx = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2503 | QualType Pointee = GetType(Record[Idx++]); |
| 2504 | return Context->getObjCObjectPointerType(Pointee); |
Chris Lattner | d7a3fcd | 2009-04-22 06:40:03 +0000 | [diff] [blame] | 2505 | } |
Argyrios Kyrtzidis | 24fab41 | 2009-09-29 19:42:55 +0000 | [diff] [blame] | 2506 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2507 | case pch::TYPE_SUBST_TEMPLATE_TYPE_PARM: { |
| 2508 | unsigned Idx = 0; |
| 2509 | QualType Parm = GetType(Record[Idx++]); |
| 2510 | QualType Replacement = GetType(Record[Idx++]); |
| 2511 | return |
| 2512 | Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm), |
| 2513 | Replacement); |
| 2514 | } |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2515 | |
| 2516 | case pch::TYPE_INJECTED_CLASS_NAME: { |
| 2517 | CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0])); |
| 2518 | QualType TST = GetType(Record[1]); // probably derivable |
Argyrios Kyrtzidis | 43921b5 | 2010-07-02 11:55:20 +0000 | [diff] [blame] | 2519 | // FIXME: ASTContext::getInjectedClassNameType is not currently suitable |
| 2520 | // for PCH reading, too much interdependencies. |
| 2521 | return |
| 2522 | QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2523 | } |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 2524 | |
Argyrios Kyrtzidis | 8731ca7 | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 2525 | case pch::TYPE_TEMPLATE_TYPE_PARM: { |
| 2526 | unsigned Idx = 0; |
| 2527 | unsigned Depth = Record[Idx++]; |
| 2528 | unsigned Index = Record[Idx++]; |
| 2529 | bool Pack = Record[Idx++]; |
| 2530 | IdentifierInfo *Name = GetIdentifierInfo(Record, Idx); |
| 2531 | return Context->getTemplateTypeParmType(Depth, Index, Pack, Name); |
| 2532 | } |
Argyrios Kyrtzidis | 8dfbd8b | 2010-06-24 08:57:31 +0000 | [diff] [blame] | 2533 | |
| 2534 | case pch::TYPE_DEPENDENT_NAME: { |
| 2535 | unsigned Idx = 0; |
| 2536 | ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; |
| 2537 | NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); |
| 2538 | const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx); |
Argyrios Kyrtzidis | f48d45e | 2010-07-02 11:55:24 +0000 | [diff] [blame] | 2539 | QualType Canon = GetType(Record[Idx++]); |
| 2540 | return Context->getDependentNameType(Keyword, NNS, Name, Canon); |
Argyrios Kyrtzidis | 8dfbd8b | 2010-06-24 08:57:31 +0000 | [diff] [blame] | 2541 | } |
Argyrios Kyrtzidis | 3acad62 | 2010-06-25 16:24:58 +0000 | [diff] [blame] | 2542 | |
| 2543 | case pch::TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: { |
| 2544 | unsigned Idx = 0; |
| 2545 | ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; |
| 2546 | NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); |
| 2547 | const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx); |
| 2548 | unsigned NumArgs = Record[Idx++]; |
| 2549 | llvm::SmallVector<TemplateArgument, 8> Args; |
| 2550 | Args.reserve(NumArgs); |
| 2551 | while (NumArgs--) |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2552 | Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx)); |
Argyrios Kyrtzidis | 3acad62 | 2010-06-25 16:24:58 +0000 | [diff] [blame] | 2553 | return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2554 | Args.size(), Args.data()); |
| 2555 | } |
Argyrios Kyrtzidis | ae8b17f | 2010-06-30 08:49:25 +0000 | [diff] [blame] | 2556 | |
| 2557 | case pch::TYPE_DEPENDENT_SIZED_ARRAY: { |
| 2558 | unsigned Idx = 0; |
| 2559 | |
| 2560 | // ArrayType |
| 2561 | QualType ElementType = GetType(Record[Idx++]); |
| 2562 | ArrayType::ArraySizeModifier ASM |
| 2563 | = (ArrayType::ArraySizeModifier)Record[Idx++]; |
| 2564 | unsigned IndexTypeQuals = Record[Idx++]; |
| 2565 | |
| 2566 | // DependentSizedArrayType |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2567 | Expr *NumElts = ReadExpr(DeclsCursor); |
Argyrios Kyrtzidis | ae8b17f | 2010-06-30 08:49:25 +0000 | [diff] [blame] | 2568 | SourceRange Brackets = ReadSourceRange(Record, Idx); |
| 2569 | |
| 2570 | return Context->getDependentSizedArrayType(ElementType, NumElts, ASM, |
| 2571 | IndexTypeQuals, Brackets); |
| 2572 | } |
Argyrios Kyrtzidis | 90b715e | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 2573 | |
Argyrios Kyrtzidis | 8731ca7 | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 2574 | case pch::TYPE_TEMPLATE_SPECIALIZATION: { |
| 2575 | unsigned Idx = 0; |
Argyrios Kyrtzidis | be19110 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 2576 | bool IsDependent = Record[Idx++]; |
Argyrios Kyrtzidis | 8731ca7 | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 2577 | TemplateName Name = ReadTemplateName(Record, Idx); |
Argyrios Kyrtzidis | 8731ca7 | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 2578 | llvm::SmallVector<TemplateArgument, 8> Args; |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2579 | ReadTemplateArgumentList(Args, DeclsCursor, Record, Idx); |
Argyrios Kyrtzidis | dd41c14 | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 2580 | QualType Canon = GetType(Record[Idx++]); |
Argyrios Kyrtzidis | be19110 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 2581 | QualType T; |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2582 | if (Canon.isNull()) |
Argyrios Kyrtzidis | be19110 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 2583 | T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(), |
| 2584 | Args.size()); |
Argyrios Kyrtzidis | 9763e22 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2585 | else |
Argyrios Kyrtzidis | be19110 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 2586 | T = Context->getTemplateSpecializationType(Name, Args.data(), |
| 2587 | Args.size(), Canon); |
| 2588 | T->Dependent = IsDependent; |
| 2589 | return T; |
Argyrios Kyrtzidis | 8731ca7 | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 2590 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2591 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2592 | // Suppress a GCC warning |
| 2593 | return QualType(); |
| 2594 | } |
| 2595 | |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2596 | namespace { |
| 2597 | |
| 2598 | class TypeLocReader : public TypeLocVisitor<TypeLocReader> { |
| 2599 | PCHReader &Reader; |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2600 | llvm::BitstreamCursor &DeclsCursor; |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2601 | const PCHReader::RecordData &Record; |
| 2602 | unsigned &Idx; |
| 2603 | |
| 2604 | public: |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2605 | TypeLocReader(PCHReader &Reader, llvm::BitstreamCursor &Cursor, |
| 2606 | const PCHReader::RecordData &Record, unsigned &Idx) |
| 2607 | : Reader(Reader), DeclsCursor(Cursor), Record(Record), Idx(Idx) { } |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2608 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2609 | // We want compile-time assurance that we've enumerated all of |
| 2610 | // these, so unfortunately we have to declare them first, then |
| 2611 | // define them out-of-line. |
| 2612 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2613 | #define TYPELOC(CLASS, PARENT) \ |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2614 | void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2615 | #include "clang/AST/TypeLocNodes.def" |
| 2616 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2617 | void VisitFunctionTypeLoc(FunctionTypeLoc); |
| 2618 | void VisitArrayTypeLoc(ArrayTypeLoc); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2619 | }; |
| 2620 | |
| 2621 | } |
| 2622 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2623 | void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2624 | // nothing to do |
| 2625 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2626 | void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
Douglas Gregor | ddf889a | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 2627 | TL.setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2628 | if (TL.needsExtraLocalData()) { |
| 2629 | TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++])); |
| 2630 | TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++])); |
| 2631 | TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++])); |
| 2632 | TL.setModeAttr(Record[Idx++]); |
| 2633 | } |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2634 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2635 | void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { |
| 2636 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2637 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2638 | void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 2639 | TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2640 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2641 | void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 2642 | TL.setCaretLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2643 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2644 | void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 2645 | TL.setAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2646 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2647 | void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 2648 | TL.setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2649 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2650 | void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 2651 | TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2652 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2653 | void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 2654 | TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2655 | TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2656 | if (Record[Idx++]) |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2657 | TL.setSizeExpr(Reader.ReadExpr(DeclsCursor)); |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 2658 | else |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2659 | TL.setSizeExpr(0); |
| 2660 | } |
| 2661 | void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { |
| 2662 | VisitArrayTypeLoc(TL); |
| 2663 | } |
| 2664 | void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { |
| 2665 | VisitArrayTypeLoc(TL); |
| 2666 | } |
| 2667 | void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { |
| 2668 | VisitArrayTypeLoc(TL); |
| 2669 | } |
| 2670 | void TypeLocReader::VisitDependentSizedArrayTypeLoc( |
| 2671 | DependentSizedArrayTypeLoc TL) { |
| 2672 | VisitArrayTypeLoc(TL); |
| 2673 | } |
| 2674 | void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( |
| 2675 | DependentSizedExtVectorTypeLoc TL) { |
| 2676 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2677 | } |
| 2678 | void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { |
| 2679 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2680 | } |
| 2681 | void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { |
| 2682 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2683 | } |
| 2684 | void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { |
| 2685 | TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2686 | TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2687 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) { |
John McCall | 86acc2a | 2009-10-23 01:28:53 +0000 | [diff] [blame] | 2688 | TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++]))); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2689 | } |
| 2690 | } |
| 2691 | void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { |
| 2692 | VisitFunctionTypeLoc(TL); |
| 2693 | } |
| 2694 | void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { |
| 2695 | VisitFunctionTypeLoc(TL); |
| 2696 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2697 | void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 2698 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2699 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2700 | void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 2701 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2702 | } |
| 2703 | void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 2704 | TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2705 | TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2706 | TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2707 | } |
| 2708 | void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 2709 | TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2710 | TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2711 | TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2712 | TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx)); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2713 | } |
| 2714 | void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 2715 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2716 | } |
| 2717 | void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { |
| 2718 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2719 | } |
| 2720 | void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { |
| 2721 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2722 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2723 | void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 2724 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2725 | } |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2726 | void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( |
| 2727 | SubstTemplateTypeParmTypeLoc TL) { |
| 2728 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2729 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2730 | void TypeLocReader::VisitTemplateSpecializationTypeLoc( |
| 2731 | TemplateSpecializationTypeLoc TL) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2732 | TL.setTemplateNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2733 | TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2734 | TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2735 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 2736 | TL.setArgLocInfo(i, |
| 2737 | Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind(), |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2738 | DeclsCursor, Record, Idx)); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2739 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2740 | void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2741 | TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2742 | TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx)); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2743 | } |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2744 | void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { |
| 2745 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2746 | } |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2747 | void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2748 | TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2749 | TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx)); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2750 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2751 | } |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2752 | void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( |
| 2753 | DependentTemplateSpecializationTypeLoc TL) { |
| 2754 | TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2755 | TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx)); |
| 2756 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2757 | TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2758 | TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2759 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) |
| 2760 | TL.setArgLocInfo(I, |
| 2761 | Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(), |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2762 | DeclsCursor, Record, Idx)); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2763 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2764 | void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 2765 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2766 | } |
| 2767 | void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 2768 | TL.setHasBaseTypeAsWritten(Record[Idx++]); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2769 | TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2770 | TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2771 | for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) |
| 2772 | TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2773 | } |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2774 | void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 2775 | TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2776 | } |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2777 | |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2778 | TypeSourceInfo *PCHReader::GetTypeSourceInfo(llvm::BitstreamCursor &DeclsCursor, |
| 2779 | const RecordData &Record, |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2780 | unsigned &Idx) { |
| 2781 | QualType InfoTy = GetType(Record[Idx++]); |
| 2782 | if (InfoTy.isNull()) |
| 2783 | return 0; |
| 2784 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2785 | TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy); |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2786 | TypeLocReader TLR(*this, DeclsCursor, Record, Idx); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2787 | for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc()) |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2788 | TLR.Visit(TL); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2789 | return TInfo; |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2790 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2791 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 2792 | QualType PCHReader::GetType(pch::TypeID ID) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2793 | unsigned FastQuals = ID & Qualifiers::FastMask; |
| 2794 | unsigned Index = ID >> Qualifiers::FastWidth; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2795 | |
| 2796 | if (Index < pch::NUM_PREDEF_TYPE_IDS) { |
| 2797 | QualType T; |
| 2798 | switch ((pch::PredefinedTypeIDs)Index) { |
| 2799 | case pch::PREDEF_TYPE_NULL_ID: return QualType(); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2800 | case pch::PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break; |
| 2801 | case pch::PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2802 | |
| 2803 | case pch::PREDEF_TYPE_CHAR_U_ID: |
| 2804 | case pch::PREDEF_TYPE_CHAR_S_ID: |
| 2805 | // FIXME: Check that the signedness of CharTy is correct! |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2806 | T = Context->CharTy; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2807 | break; |
| 2808 | |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2809 | case pch::PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break; |
| 2810 | case pch::PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break; |
| 2811 | case pch::PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break; |
| 2812 | case pch::PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break; |
| 2813 | case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2814 | case pch::PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2815 | case pch::PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break; |
| 2816 | case pch::PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break; |
| 2817 | case pch::PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break; |
| 2818 | case pch::PREDEF_TYPE_INT_ID: T = Context->IntTy; break; |
| 2819 | case pch::PREDEF_TYPE_LONG_ID: T = Context->LongTy; break; |
| 2820 | case pch::PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2821 | case pch::PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2822 | case pch::PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break; |
| 2823 | case pch::PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break; |
| 2824 | case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break; |
| 2825 | case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break; |
| 2826 | case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2827 | case pch::PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2828 | case pch::PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break; |
| 2829 | case pch::PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break; |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2830 | case pch::PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break; |
| 2831 | case pch::PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break; |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 2832 | case pch::PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | assert(!T.isNull() && "Unknown predefined type"); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2836 | return T.withFastQualifiers(FastQuals); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
| 2839 | Index -= pch::NUM_PREDEF_TYPE_IDS; |
Sebastian Redl | aaec0aa | 2010-07-20 22:37:49 +0000 | [diff] [blame] | 2840 | assert(Index < TypesLoaded.size() && "Type index out-of-range"); |
Sebastian Redl | 07a353c | 2010-07-14 20:26:45 +0000 | [diff] [blame] | 2841 | if (TypesLoaded[Index].isNull()) { |
Sebastian Redl | aaec0aa | 2010-07-20 22:37:49 +0000 | [diff] [blame] | 2842 | TypesLoaded[Index] = ReadTypeRecord(Index); |
Sebastian Redl | 07a353c | 2010-07-14 20:26:45 +0000 | [diff] [blame] | 2843 | TypesLoaded[Index]->setFromPCH(); |
Sebastian Redl | 30c514c | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 2844 | if (DeserializationListener) |
Sebastian Redl | 1476ed4 | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2845 | DeserializationListener->TypeRead(ID >> Qualifiers::FastWidth, |
| 2846 | TypesLoaded[Index]); |
Sebastian Redl | 07a353c | 2010-07-14 20:26:45 +0000 | [diff] [blame] | 2847 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2848 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2849 | return TypesLoaded[Index].withFastQualifiers(FastQuals); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2850 | } |
| 2851 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2852 | TemplateArgumentLocInfo |
| 2853 | PCHReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2854 | llvm::BitstreamCursor &DeclsCursor, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2855 | const RecordData &Record, |
Argyrios Kyrtzidis | 919e693 | 2010-06-28 22:28:35 +0000 | [diff] [blame] | 2856 | unsigned &Index) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2857 | switch (Kind) { |
| 2858 | case TemplateArgument::Expression: |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2859 | return ReadExpr(DeclsCursor); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2860 | case TemplateArgument::Type: |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2861 | return GetTypeSourceInfo(DeclsCursor, Record, Index); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2862 | case TemplateArgument::Template: { |
Argyrios Kyrtzidis | 17cfded | 2010-06-28 09:31:42 +0000 | [diff] [blame] | 2863 | SourceRange QualifierRange = ReadSourceRange(Record, Index); |
| 2864 | SourceLocation TemplateNameLoc = ReadSourceLocation(Record, Index); |
| 2865 | return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2866 | } |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2867 | case TemplateArgument::Null: |
| 2868 | case TemplateArgument::Integral: |
| 2869 | case TemplateArgument::Declaration: |
| 2870 | case TemplateArgument::Pack: |
| 2871 | return TemplateArgumentLocInfo(); |
| 2872 | } |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2873 | llvm_unreachable("unexpected template argument loc"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2874 | return TemplateArgumentLocInfo(); |
| 2875 | } |
| 2876 | |
Argyrios Kyrtzidis | 17cfded | 2010-06-28 09:31:42 +0000 | [diff] [blame] | 2877 | TemplateArgumentLoc |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2878 | PCHReader::ReadTemplateArgumentLoc(llvm::BitstreamCursor &DeclsCursor, |
| 2879 | const RecordData &Record, unsigned &Index) { |
| 2880 | TemplateArgument Arg = ReadTemplateArgument(DeclsCursor, Record, Index); |
Argyrios Kyrtzidis | 17cfded | 2010-06-28 09:31:42 +0000 | [diff] [blame] | 2881 | |
| 2882 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 2883 | if (Record[Index++]) // bool InfoHasSameExpr. |
| 2884 | return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); |
| 2885 | } |
| 2886 | return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(Arg.getKind(), |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2887 | DeclsCursor, |
Argyrios Kyrtzidis | 919e693 | 2010-06-28 22:28:35 +0000 | [diff] [blame] | 2888 | Record, Index)); |
Argyrios Kyrtzidis | 44f8c37 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 2889 | } |
| 2890 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 2891 | Decl *PCHReader::GetExternalDecl(uint32_t ID) { |
| 2892 | return GetDecl(ID); |
| 2893 | } |
| 2894 | |
Argyrios Kyrtzidis | 8871a44 | 2010-07-08 17:13:02 +0000 | [diff] [blame] | 2895 | TranslationUnitDecl *PCHReader::GetTranslationUnitDecl() { |
Sebastian Redl | 30c514c | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 2896 | if (!DeclsLoaded[0]) { |
Argyrios Kyrtzidis | a865005 | 2010-08-03 17:30:10 +0000 | [diff] [blame] | 2897 | ReadDeclRecord(0, 0); |
Sebastian Redl | 30c514c | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 2898 | if (DeserializationListener) |
Sebastian Redl | 1476ed4 | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2899 | DeserializationListener->DeclRead(1, DeclsLoaded[0]); |
Sebastian Redl | 30c514c | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 2900 | } |
Argyrios Kyrtzidis | 8871a44 | 2010-07-08 17:13:02 +0000 | [diff] [blame] | 2901 | |
| 2902 | return cast<TranslationUnitDecl>(DeclsLoaded[0]); |
| 2903 | } |
| 2904 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 2905 | Decl *PCHReader::GetDecl(pch::DeclID ID) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2906 | if (ID == 0) |
| 2907 | return 0; |
| 2908 | |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2909 | if (ID > DeclsLoaded.size()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2910 | Error("declaration ID out-of-range for PCH file"); |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2911 | return 0; |
| 2912 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2913 | |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2914 | unsigned Index = ID - 1; |
Sebastian Redl | 30c514c | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 2915 | if (!DeclsLoaded[Index]) { |
Argyrios Kyrtzidis | a865005 | 2010-08-03 17:30:10 +0000 | [diff] [blame] | 2916 | ReadDeclRecord(Index, ID); |
Sebastian Redl | 30c514c | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 2917 | if (DeserializationListener) |
| 2918 | DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); |
| 2919 | } |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2920 | |
| 2921 | return DeclsLoaded[Index]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2922 | } |
| 2923 | |
Chris Lattner | 887e2b3 | 2009-04-27 05:46:25 +0000 | [diff] [blame] | 2924 | /// \brief Resolve the offset of a statement into a statement. |
| 2925 | /// |
| 2926 | /// This operation will read a new statement from the external |
| 2927 | /// source each time it is called, and is meant to be used via a |
| 2928 | /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 2929 | Stmt *PCHReader::GetExternalDeclStmt(uint64_t Offset) { |
Sebastian Redl | 0fa7d0b | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 2930 | // Offset here is a global offset across the entire chain. |
| 2931 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
| 2932 | PerFileData &F = *Chain[N - I - 1]; |
| 2933 | if (Offset < F.SizeInBits) { |
| 2934 | // Since we know that this statement is part of a decl, make sure to use |
| 2935 | // the decl cursor to read it. |
| 2936 | F.DeclsCursor.JumpToBit(Offset); |
| 2937 | return ReadStmtFromStream(F.DeclsCursor); |
| 2938 | } |
| 2939 | Offset -= F.SizeInBits; |
| 2940 | } |
| 2941 | llvm_unreachable("Broken chain"); |
Douglas Gregor | 250fc9c | 2009-04-18 00:07:54 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 2944 | bool PCHReader::FindExternalLexicalDecls(const DeclContext *DC, |
| 2945 | llvm::SmallVectorImpl<Decl*> &Decls) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2946 | assert(DC->hasExternalLexicalStorage() && |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2947 | "DeclContext has no lexical decls in storage"); |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2948 | |
Sebastian Redl | 0fa7d0b | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 2949 | // There might be lexical decls in multiple parts of the chain, for the TU |
| 2950 | // at least. |
| 2951 | DeclContextInfos &Infos = DeclContextOffsets[DC]; |
| 2952 | for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end(); |
| 2953 | I != E; ++I) { |
Sebastian Redl | 681d723 | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 2954 | // IDs can be 0 if this context doesn't contain declarations. |
| 2955 | if (!I->LexicalDecls) |
Sebastian Redl | 0fa7d0b | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 2956 | continue; |
Sebastian Redl | 0fa7d0b | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 2957 | |
| 2958 | // Load all of the declaration IDs |
Sebastian Redl | 681d723 | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 2959 | for (const pch::DeclID *ID = I->LexicalDecls, |
| 2960 | *IDE = ID + I->NumLexicalDecls; |
| 2961 | ID != IDE; ++ID) |
| 2962 | Decls.push_back(GetDecl(*ID)); |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2963 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2964 | |
Douglas Gregor | 2512308 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 2965 | ++NumLexicalDeclContextsRead; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2966 | return false; |
| 2967 | } |
| 2968 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 2969 | DeclContext::lookup_result |
| 2970 | PCHReader::FindExternalVisibleDeclsByName(const DeclContext *DC, |
| 2971 | DeclarationName Name) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2972 | assert(DC->hasExternalVisibleStorage() && |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2973 | "DeclContext has no visible decls in storage"); |
Ted Kremenek | d5d7b3f | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2974 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 2975 | llvm::SmallVector<VisibleDeclaration, 64> Decls; |
Sebastian Redl | 0fa7d0b | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 2976 | // There might be lexical decls in multiple parts of the chain, for the TU |
| 2977 | // and namespaces. |
| 2978 | DeclContextInfos &Infos = DeclContextOffsets[DC]; |
| 2979 | for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end(); |
| 2980 | I != E; ++I) { |
| 2981 | uint64_t Offset = I->OffsetToVisibleDecls; |
| 2982 | if (Offset == 0) |
| 2983 | continue; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2984 | |
Sebastian Redl | 0fa7d0b | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 2985 | llvm::BitstreamCursor &DeclsCursor = *I->Stream; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2986 | |
Sebastian Redl | 0fa7d0b | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 2987 | // Keep track of where we are in the stream, then jump back there |
| 2988 | // after reading this context. |
| 2989 | SavedStreamPosition SavedPosition(DeclsCursor); |
| 2990 | |
| 2991 | // Load the record containing all of the declarations visible in |
| 2992 | // this context. |
| 2993 | DeclsCursor.JumpToBit(Offset); |
| 2994 | RecordData Record; |
| 2995 | unsigned Code = DeclsCursor.ReadCode(); |
| 2996 | unsigned RecCode = DeclsCursor.ReadRecord(Code, Record); |
| 2997 | if (RecCode != pch::DECL_CONTEXT_VISIBLE) { |
| 2998 | Error("Expected visible block"); |
| 2999 | return DeclContext::lookup_result(DeclContext::lookup_iterator(), |
| 3000 | DeclContext::lookup_iterator()); |
| 3001 | } |
| 3002 | |
| 3003 | if (Record.empty()) |
| 3004 | continue; |
| 3005 | |
| 3006 | unsigned Idx = 0; |
| 3007 | while (Idx < Record.size()) { |
| 3008 | Decls.push_back(VisibleDeclaration()); |
| 3009 | Decls.back().Name = ReadDeclarationName(Record, Idx); |
| 3010 | |
| 3011 | unsigned Size = Record[Idx++]; |
| 3012 | llvm::SmallVector<unsigned, 4> &LoadedDecls = Decls.back().Declarations; |
| 3013 | LoadedDecls.reserve(Size); |
| 3014 | for (unsigned J = 0; J < Size; ++J) |
| 3015 | LoadedDecls.push_back(Record[Idx++]); |
| 3016 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
Douglas Gregor | 2512308 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 3019 | ++NumVisibleDeclContextsRead; |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 3020 | |
| 3021 | SetExternalVisibleDecls(DC, Decls); |
| 3022 | return const_cast<DeclContext*>(DC)->lookup(Name); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
Argyrios Kyrtzidis | bb80a8e | 2010-07-07 15:46:26 +0000 | [diff] [blame] | 3025 | void PCHReader::PassInterestingDeclsToConsumer() { |
| 3026 | assert(Consumer); |
| 3027 | while (!InterestingDecls.empty()) { |
| 3028 | DeclGroupRef DG(InterestingDecls.front()); |
| 3029 | InterestingDecls.pop_front(); |
| 3030 | Consumer->HandleTopLevelDecl(DG); |
| 3031 | } |
| 3032 | } |
| 3033 | |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 3034 | void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) { |
Douglas Gregor | 0af2ca4 | 2009-04-22 19:09:20 +0000 | [diff] [blame] | 3035 | this->Consumer = Consumer; |
| 3036 | |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 3037 | if (!Consumer) |
| 3038 | return; |
| 3039 | |
| 3040 | for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) { |
Argyrios Kyrtzidis | bb80a8e | 2010-07-07 15:46:26 +0000 | [diff] [blame] | 3041 | // Force deserialization of this decl, which will cause it to be queued for |
| 3042 | // passing to the consumer. |
Daniel Dunbar | 04a0b50 | 2009-09-17 03:06:44 +0000 | [diff] [blame] | 3043 | GetDecl(ExternalDefinitions[I]); |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 3044 | } |
Douglas Gregor | c62a2fe | 2009-04-25 00:41:30 +0000 | [diff] [blame] | 3045 | |
Argyrios Kyrtzidis | bb80a8e | 2010-07-07 15:46:26 +0000 | [diff] [blame] | 3046 | PassInterestingDeclsToConsumer(); |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 3047 | } |
| 3048 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3049 | void PCHReader::PrintStats() { |
| 3050 | std::fprintf(stderr, "*** PCH Statistics:\n"); |
| 3051 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3052 | unsigned NumTypesLoaded |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 3053 | = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3054 | QualType()); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 3055 | unsigned NumDeclsLoaded |
| 3056 | = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), |
| 3057 | (Decl *)0); |
| 3058 | unsigned NumIdentifiersLoaded |
| 3059 | = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), |
| 3060 | IdentifiersLoaded.end(), |
| 3061 | (IdentifierInfo *)0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3062 | unsigned NumSelectorsLoaded |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 3063 | = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), |
| 3064 | SelectorsLoaded.end(), |
| 3065 | Selector()); |
Douglas Gregor | 2d41cc1 | 2009-04-13 20:50:16 +0000 | [diff] [blame] | 3066 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 3067 | std::fprintf(stderr, " %u stat cache hits\n", NumStatHits); |
| 3068 | std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 3069 | if (TotalNumSLocEntries) |
| 3070 | std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", |
| 3071 | NumSLocEntriesRead, TotalNumSLocEntries, |
| 3072 | ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 3073 | if (!TypesLoaded.empty()) |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3074 | std::fprintf(stderr, " %u/%u types read (%f%%)\n", |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 3075 | NumTypesLoaded, (unsigned)TypesLoaded.size(), |
| 3076 | ((float)NumTypesLoaded/TypesLoaded.size() * 100)); |
| 3077 | if (!DeclsLoaded.empty()) |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3078 | std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 3079 | NumDeclsLoaded, (unsigned)DeclsLoaded.size(), |
| 3080 | ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 3081 | if (!IdentifiersLoaded.empty()) |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3082 | std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 3083 | NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), |
| 3084 | ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 3085 | if (!SelectorsLoaded.empty()) |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3086 | std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 3087 | NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), |
| 3088 | ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3089 | if (TotalNumStatements) |
| 3090 | std::fprintf(stderr, " %u/%u statements read (%f%%)\n", |
| 3091 | NumStatementsRead, TotalNumStatements, |
| 3092 | ((float)NumStatementsRead/TotalNumStatements * 100)); |
| 3093 | if (TotalNumMacros) |
| 3094 | std::fprintf(stderr, " %u/%u macros read (%f%%)\n", |
| 3095 | NumMacrosRead, TotalNumMacros, |
| 3096 | ((float)NumMacrosRead/TotalNumMacros * 100)); |
| 3097 | if (TotalLexicalDeclContexts) |
| 3098 | std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", |
| 3099 | NumLexicalDeclContextsRead, TotalLexicalDeclContexts, |
| 3100 | ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts |
| 3101 | * 100)); |
| 3102 | if (TotalVisibleDeclContexts) |
| 3103 | std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", |
| 3104 | NumVisibleDeclContextsRead, TotalVisibleDeclContexts, |
| 3105 | ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts |
| 3106 | * 100)); |
Sebastian Redl | fa78dec | 2010-08-04 21:22:45 +0000 | [diff] [blame] | 3107 | if (TotalNumMethodPoolEntries) { |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3108 | std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", |
Sebastian Redl | fa78dec | 2010-08-04 21:22:45 +0000 | [diff] [blame] | 3109 | NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, |
| 3110 | ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3111 | * 100)); |
Sebastian Redl | fa78dec | 2010-08-04 21:22:45 +0000 | [diff] [blame] | 3112 | std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3113 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3114 | std::fprintf(stderr, "\n"); |
| 3115 | } |
| 3116 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3117 | void PCHReader::InitializeSema(Sema &S) { |
| 3118 | SemaObj = &S; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3119 | S.ExternalSource = this; |
| 3120 | |
Douglas Gregor | 6cfc1a8 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 3121 | // Makes sure any declarations that were deserialized "too early" |
| 3122 | // still get added to the identifier's declaration chains. |
| 3123 | for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) { |
| 3124 | SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I])); |
| 3125 | SemaObj->IdResolver.AddDecl(PreloadedDecls[I]); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3126 | } |
Douglas Gregor | 6cfc1a8 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 3127 | PreloadedDecls.clear(); |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 3128 | |
| 3129 | // If there were any tentative definitions, deserialize them and add |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 3130 | // them to Sema's list of tentative definitions. |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 3131 | for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { |
| 3132 | VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I])); |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 3133 | SemaObj->TentativeDefinitions.push_back(Var); |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 3134 | } |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 3135 | |
Tanya Lattner | e6bbc01 | 2010-02-12 00:07:30 +0000 | [diff] [blame] | 3136 | // If there were any unused static functions, deserialize them and add to |
| 3137 | // Sema's list of unused static functions. |
| 3138 | for (unsigned I = 0, N = UnusedStaticFuncs.size(); I != N; ++I) { |
| 3139 | FunctionDecl *FD = cast<FunctionDecl>(GetDecl(UnusedStaticFuncs[I])); |
| 3140 | SemaObj->UnusedStaticFuncs.push_back(FD); |
| 3141 | } |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 3142 | |
Argyrios Kyrtzidis | 72b9057 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 3143 | // If there were any weak undeclared identifiers, deserialize them and add to |
| 3144 | // Sema's list of weak undeclared identifiers. |
| 3145 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 3146 | unsigned Idx = 0; |
| 3147 | for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) { |
| 3148 | IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx); |
| 3149 | IdentifierInfo *AliasId=GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx); |
| 3150 | SourceLocation Loc = ReadSourceLocation(WeakUndeclaredIdentifiers, Idx); |
| 3151 | bool Used = WeakUndeclaredIdentifiers[Idx++]; |
| 3152 | Sema::WeakInfo WI(AliasId, Loc); |
| 3153 | WI.setUsed(Used); |
| 3154 | SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI)); |
| 3155 | } |
| 3156 | } |
| 3157 | |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 3158 | // If there were any locally-scoped external declarations, |
| 3159 | // deserialize them and add them to Sema's table of locally-scoped |
| 3160 | // external declarations. |
| 3161 | for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) { |
| 3162 | NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I])); |
| 3163 | SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D; |
| 3164 | } |
Douglas Gregor | b81c170 | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 3165 | |
| 3166 | // If there were any ext_vector type declarations, deserialize them |
| 3167 | // and add them to Sema's vector of such declarations. |
| 3168 | for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) |
| 3169 | SemaObj->ExtVectorDecls.push_back( |
| 3170 | cast<TypedefDecl>(GetDecl(ExtVectorDecls[I]))); |
Argyrios Kyrtzidis | d455add | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 3171 | |
| 3172 | // FIXME: Do VTable uses and dynamic classes deserialize too much ? |
| 3173 | // Can we cut them down before writing them ? |
| 3174 | |
| 3175 | // If there were any VTable uses, deserialize the information and add it |
| 3176 | // to Sema's vector and map of VTable uses. |
Argyrios Kyrtzidis | be4ebcd | 2010-08-03 17:29:52 +0000 | [diff] [blame] | 3177 | if (!VTableUses.empty()) { |
| 3178 | unsigned Idx = 0; |
| 3179 | for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) { |
| 3180 | CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); |
| 3181 | SourceLocation Loc = ReadSourceLocation(VTableUses, Idx); |
| 3182 | bool DefinitionRequired = VTableUses[Idx++]; |
| 3183 | SemaObj->VTableUses.push_back(std::make_pair(Class, Loc)); |
| 3184 | SemaObj->VTablesUsed[Class] = DefinitionRequired; |
| 3185 | } |
Argyrios Kyrtzidis | d455add | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 3186 | } |
| 3187 | |
| 3188 | // If there were any dynamic classes declarations, deserialize them |
| 3189 | // and add them to Sema's vector of such declarations. |
| 3190 | for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) |
| 3191 | SemaObj->DynamicClasses.push_back( |
| 3192 | cast<CXXRecordDecl>(GetDecl(DynamicClasses[I]))); |
Fariborz Jahanian | 3201983 | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 3193 | |
Argyrios Kyrtzidis | 0e03638 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 3194 | // If there were any pending implicit instantiations, deserialize them |
| 3195 | // and add them to Sema's queue of such instantiations. |
| 3196 | assert(PendingImplicitInstantiations.size() % 2 == 0 && |
| 3197 | "Expected pairs of entries"); |
| 3198 | for (unsigned Idx = 0, N = PendingImplicitInstantiations.size(); Idx < N;) { |
| 3199 | ValueDecl *D=cast<ValueDecl>(GetDecl(PendingImplicitInstantiations[Idx++])); |
| 3200 | SourceLocation Loc = ReadSourceLocation(PendingImplicitInstantiations, Idx); |
| 3201 | SemaObj->PendingImplicitInstantiations.push_back(std::make_pair(D, Loc)); |
| 3202 | } |
| 3203 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3204 | // Load the offsets of the declarations that Sema references. |
| 3205 | // They will be lazily deserialized when needed. |
| 3206 | if (!SemaDeclRefs.empty()) { |
| 3207 | assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!"); |
| 3208 | SemaObj->StdNamespace = SemaDeclRefs[0]; |
| 3209 | SemaObj->StdBadAlloc = SemaDeclRefs[1]; |
| 3210 | } |
| 3211 | |
Fariborz Jahanian | 3201983 | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 3212 | // If there are @selector references added them to its pool. This is for |
| 3213 | // implementation of -Wselector. |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 3214 | if (!ReferencedSelectorsData.empty()) { |
| 3215 | unsigned int DataSize = ReferencedSelectorsData.size()-1; |
Fariborz Jahanian | 3201983 | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 3216 | unsigned I = 0; |
| 3217 | while (I < DataSize) { |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 3218 | Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); |
Fariborz Jahanian | 3201983 | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 3219 | SourceLocation SelLoc = |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 3220 | SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); |
Fariborz Jahanian | 3201983 | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 3221 | SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc)); |
| 3222 | } |
| 3223 | } |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) { |
Sebastian Redl | d8c5abb | 2010-08-02 18:30:12 +0000 | [diff] [blame] | 3227 | // Try to find this name within our on-disk hash tables. We start with the |
| 3228 | // most recent one, since that one contains the most up-to-date info. |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 3229 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
| 3230 | PCHIdentifierLookupTable *IdTable |
Sebastian Redl | d8c5abb | 2010-08-02 18:30:12 +0000 | [diff] [blame] | 3231 | = (PCHIdentifierLookupTable *)Chain[I]->IdentifierLookupTable; |
Sebastian Redl | 0fa7d0b | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 3232 | if (!IdTable) |
| 3233 | continue; |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 3234 | std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart); |
| 3235 | PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key); |
| 3236 | if (Pos == IdTable->end()) |
| 3237 | continue; |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3238 | |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 3239 | // Dereferencing the iterator has the effect of building the |
| 3240 | // IdentifierInfo node and populating it with the various |
| 3241 | // declarations it needs. |
Sebastian Redl | d8c5abb | 2010-08-02 18:30:12 +0000 | [diff] [blame] | 3242 | return *Pos; |
Sebastian Redl | d27d3fc | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 3243 | } |
Sebastian Redl | d8c5abb | 2010-08-02 18:30:12 +0000 | [diff] [blame] | 3244 | return 0; |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3247 | std::pair<ObjCMethodList, ObjCMethodList> |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3248 | PCHReader::ReadMethodPool(Selector Sel) { |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 3249 | // Find this selector in a hash table. We want to find the most recent entry. |
| 3250 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
| 3251 | PerFileData &F = *Chain[I]; |
| 3252 | if (!F.SelectorLookupTable) |
| 3253 | continue; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3254 | |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 3255 | PCHSelectorLookupTable *PoolTable |
| 3256 | = (PCHSelectorLookupTable*)F.SelectorLookupTable; |
| 3257 | PCHSelectorLookupTable::iterator Pos = PoolTable->find(Sel); |
| 3258 | if (Pos != PoolTable->end()) { |
| 3259 | ++NumSelectorsRead; |
Sebastian Redl | fa78dec | 2010-08-04 21:22:45 +0000 | [diff] [blame] | 3260 | // FIXME: Not quite happy with the statistics here. We probably should |
| 3261 | // disable this tracking when called via LoadSelector. |
| 3262 | // Also, should entries without methods count as misses? |
| 3263 | ++NumMethodPoolEntriesRead; |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 3264 | PCHSelectorLookupTrait::data_type Data = *Pos; |
| 3265 | if (DeserializationListener) |
| 3266 | DeserializationListener->SelectorRead(Data.ID, Sel); |
| 3267 | return std::make_pair(Data.Instance, Data.Factory); |
| 3268 | } |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3269 | } |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3270 | |
Sebastian Redl | fa78dec | 2010-08-04 21:22:45 +0000 | [diff] [blame] | 3271 | ++NumMethodPoolMisses; |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 3272 | return std::pair<ObjCMethodList, ObjCMethodList>(); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
Sebastian Redl | e58aa89 | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 3275 | void PCHReader::LoadSelector(Selector Sel) { |
| 3276 | // It would be complicated to avoid reading the methods anyway. So don't. |
| 3277 | ReadMethodPool(Sel); |
| 3278 | } |
| 3279 | |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 3280 | void PCHReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) { |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3281 | assert(ID && "Non-zero identifier ID required"); |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 3282 | assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 3283 | IdentifiersLoaded[ID - 1] = II; |
Sebastian Redl | f2f0f03 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 3284 | if (DeserializationListener) |
| 3285 | DeserializationListener->IdentifierRead(ID, II); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 3288 | /// \brief Set the globally-visible declarations associated with the given |
| 3289 | /// identifier. |
| 3290 | /// |
| 3291 | /// If the PCH reader is currently in a state where the given declaration IDs |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3292 | /// cannot safely be resolved, they are queued until it is safe to resolve |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 3293 | /// them. |
| 3294 | /// |
| 3295 | /// \param II an IdentifierInfo that refers to one or more globally-visible |
| 3296 | /// declarations. |
| 3297 | /// |
| 3298 | /// \param DeclIDs the set of declaration IDs with the name @p II that are |
| 3299 | /// visible at global scope. |
| 3300 | /// |
| 3301 | /// \param Nonrecursive should be true to indicate that the caller knows that |
| 3302 | /// this call is non-recursive, and therefore the globally-visible declarations |
| 3303 | /// will not be placed onto the pending queue. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3304 | void |
| 3305 | PCHReader::SetGloballyVisibleDecls(IdentifierInfo *II, |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 3306 | const llvm::SmallVectorImpl<uint32_t> &DeclIDs, |
| 3307 | bool Nonrecursive) { |
Argyrios Kyrtzidis | 29ee3a2 | 2010-07-30 10:03:16 +0000 | [diff] [blame] | 3308 | if (NumCurrentElementsDeserializing && !Nonrecursive) { |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 3309 | PendingIdentifierInfos.push_back(PendingIdentifierInfo()); |
| 3310 | PendingIdentifierInfo &PII = PendingIdentifierInfos.back(); |
| 3311 | PII.II = II; |
| 3312 | for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) |
| 3313 | PII.DeclIDs.push_back(DeclIDs[I]); |
| 3314 | return; |
| 3315 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3316 | |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 3317 | for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { |
| 3318 | NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); |
| 3319 | if (SemaObj) { |
| 3320 | // Introduce this declaration into the translation-unit scope |
| 3321 | // and add it to the declaration chain for this identifier, so |
| 3322 | // that (unqualified) name lookup will find it. |
| 3323 | SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D)); |
| 3324 | SemaObj->IdResolver.AddDeclToIdentifierChain(II, D); |
| 3325 | } else { |
| 3326 | // Queue this declaration so that it will be added to the |
| 3327 | // translation unit scope and identifier's declaration chain |
| 3328 | // once a Sema object is known. |
| 3329 | PreloadedDecls.push_back(D); |
| 3330 | } |
| 3331 | } |
| 3332 | } |
| 3333 | |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 3334 | IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) { |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 3335 | if (ID == 0) |
| 3336 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3337 | |
Sebastian Redl | 11f5ccf | 2010-07-21 00:46:22 +0000 | [diff] [blame] | 3338 | if (IdentifiersLoaded.empty()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 3339 | Error("no identifier table in PCH file"); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 3340 | return 0; |
| 3341 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3342 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 3343 | assert(PP && "Forgot to set Preprocessor ?"); |
Sebastian Redl | 11f5ccf | 2010-07-21 00:46:22 +0000 | [diff] [blame] | 3344 | ID -= 1; |
| 3345 | if (!IdentifiersLoaded[ID]) { |
| 3346 | unsigned Index = ID; |
| 3347 | const char *Str = 0; |
| 3348 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
| 3349 | PerFileData *F = Chain[N - I - 1]; |
| 3350 | if (Index < F->LocalNumIdentifiers) { |
| 3351 | uint32_t Offset = F->IdentifierOffsets[Index]; |
| 3352 | Str = F->IdentifierTableData + Offset; |
| 3353 | break; |
| 3354 | } |
| 3355 | Index -= F->LocalNumIdentifiers; |
| 3356 | } |
| 3357 | assert(Str && "Broken Chain"); |
Douglas Gregor | d6595a4 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 3358 | |
Douglas Gregor | 02fc751 | 2009-04-28 20:01:51 +0000 | [diff] [blame] | 3359 | // All of the strings in the PCH file are preceded by a 16-bit |
| 3360 | // length. Extract that 16-bit length to avoid having to execute |
| 3361 | // strlen(). |
Ted Kremenek | 231bc0b | 2009-10-23 04:45:31 +0000 | [diff] [blame] | 3362 | // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as |
| 3363 | // unsigned integers. This is important to avoid integer overflow when |
| 3364 | // we cast them to 'unsigned'. |
Ted Kremenek | ff1ea46 | 2009-10-23 03:57:22 +0000 | [diff] [blame] | 3365 | const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; |
Douglas Gregor | 02fc751 | 2009-04-28 20:01:51 +0000 | [diff] [blame] | 3366 | unsigned StrLen = (((unsigned) StrLenPtr[0]) |
| 3367 | | (((unsigned) StrLenPtr[1]) << 8)) - 1; |
Sebastian Redl | 11f5ccf | 2010-07-21 00:46:22 +0000 | [diff] [blame] | 3368 | IdentifiersLoaded[ID] |
Kovarththanan Rajaratnam | 811f426 | 2010-03-12 10:32:27 +0000 | [diff] [blame] | 3369 | = &PP->getIdentifierTable().get(Str, StrLen); |
Sebastian Redl | f2f0f03 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 3370 | if (DeserializationListener) |
| 3371 | DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 3372 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3373 | |
Sebastian Redl | 11f5ccf | 2010-07-21 00:46:22 +0000 | [diff] [blame] | 3374 | return IdentifiersLoaded[ID]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 3377 | void PCHReader::ReadSLocEntry(unsigned ID) { |
| 3378 | ReadSLocEntryRecord(ID); |
| 3379 | } |
| 3380 | |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 3381 | Selector PCHReader::DecodeSelector(unsigned ID) { |
| 3382 | if (ID == 0) |
| 3383 | return Selector(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3384 | |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 3385 | if (ID > SelectorsLoaded.size()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 3386 | Error("selector ID out of range in PCH file"); |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 3387 | return Selector(); |
| 3388 | } |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3389 | |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 3390 | if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) { |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3391 | // Load this selector from the selector table. |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 3392 | unsigned Idx = ID - 1; |
| 3393 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
| 3394 | PerFileData &F = *Chain[N - I - 1]; |
| 3395 | if (Idx < F.LocalNumSelectors) { |
| 3396 | PCHSelectorLookupTrait Trait(*this); |
| 3397 | SelectorsLoaded[ID - 1] = |
| 3398 | Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0); |
| 3399 | if (DeserializationListener) |
| 3400 | DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); |
| 3401 | break; |
| 3402 | } |
| 3403 | Idx -= F.LocalNumSelectors; |
| 3404 | } |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3405 | } |
| 3406 | |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 3407 | return SelectorsLoaded[ID - 1]; |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 3410 | Selector PCHReader::GetExternalSelector(uint32_t ID) { |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 3411 | return DecodeSelector(ID); |
| 3412 | } |
| 3413 | |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 3414 | uint32_t PCHReader::GetNumExternalSelectors() { |
Sebastian Redl | 725cd96 | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 3415 | // ID 0 (the null selector) is considered an external selector. |
| 3416 | return getTotalNumSelectors() + 1; |
Douglas Gregor | 719770d | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 3417 | } |
| 3418 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3419 | DeclarationName |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3420 | PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) { |
| 3421 | DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; |
| 3422 | switch (Kind) { |
| 3423 | case DeclarationName::Identifier: |
| 3424 | return DeclarationName(GetIdentifierInfo(Record, Idx)); |
| 3425 | |
| 3426 | case DeclarationName::ObjCZeroArgSelector: |
| 3427 | case DeclarationName::ObjCOneArgSelector: |
| 3428 | case DeclarationName::ObjCMultiArgSelector: |
Steve Naroff | a7503a7 | 2009-04-23 15:15:40 +0000 | [diff] [blame] | 3429 | return DeclarationName(GetSelector(Record, Idx)); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3430 | |
| 3431 | case DeclarationName::CXXConstructorName: |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 3432 | return Context->DeclarationNames.getCXXConstructorName( |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 3433 | Context->getCanonicalType(GetType(Record[Idx++]))); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3434 | |
| 3435 | case DeclarationName::CXXDestructorName: |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 3436 | return Context->DeclarationNames.getCXXDestructorName( |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 3437 | Context->getCanonicalType(GetType(Record[Idx++]))); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3438 | |
| 3439 | case DeclarationName::CXXConversionFunctionName: |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 3440 | return Context->DeclarationNames.getCXXConversionFunctionName( |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 3441 | Context->getCanonicalType(GetType(Record[Idx++]))); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3442 | |
| 3443 | case DeclarationName::CXXOperatorName: |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 3444 | return Context->DeclarationNames.getCXXOperatorName( |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3445 | (OverloadedOperatorKind)Record[Idx++]); |
| 3446 | |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 3447 | case DeclarationName::CXXLiteralOperatorName: |
| 3448 | return Context->DeclarationNames.getCXXLiteralOperatorName( |
| 3449 | GetIdentifierInfo(Record, Idx)); |
| 3450 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3451 | case DeclarationName::CXXUsingDirective: |
| 3452 | return DeclarationName::getUsingDirectiveName(); |
| 3453 | } |
| 3454 | |
| 3455 | // Required to silence GCC warning |
| 3456 | return DeclarationName(); |
| 3457 | } |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 3458 | |
Argyrios Kyrtzidis | 8731ca7 | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 3459 | TemplateName |
| 3460 | PCHReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) { |
| 3461 | TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++]; |
| 3462 | switch (Kind) { |
| 3463 | case TemplateName::Template: |
| 3464 | return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++]))); |
| 3465 | |
| 3466 | case TemplateName::OverloadedTemplate: { |
| 3467 | unsigned size = Record[Idx++]; |
| 3468 | UnresolvedSet<8> Decls; |
| 3469 | while (size--) |
| 3470 | Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++]))); |
| 3471 | |
| 3472 | return Context->getOverloadedTemplateName(Decls.begin(), Decls.end()); |
| 3473 | } |
| 3474 | |
| 3475 | case TemplateName::QualifiedTemplate: { |
| 3476 | NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); |
| 3477 | bool hasTemplKeyword = Record[Idx++]; |
| 3478 | TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++])); |
| 3479 | return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template); |
| 3480 | } |
| 3481 | |
| 3482 | case TemplateName::DependentTemplate: { |
| 3483 | NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); |
| 3484 | if (Record[Idx++]) // isIdentifier |
| 3485 | return Context->getDependentTemplateName(NNS, |
| 3486 | GetIdentifierInfo(Record, Idx)); |
| 3487 | return Context->getDependentTemplateName(NNS, |
Argyrios Kyrtzidis | 17cfded | 2010-06-28 09:31:42 +0000 | [diff] [blame] | 3488 | (OverloadedOperatorKind)Record[Idx++]); |
Argyrios Kyrtzidis | 8731ca7 | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 3489 | } |
| 3490 | } |
| 3491 | |
| 3492 | assert(0 && "Unhandled template name kind!"); |
| 3493 | return TemplateName(); |
| 3494 | } |
| 3495 | |
| 3496 | TemplateArgument |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 3497 | PCHReader::ReadTemplateArgument(llvm::BitstreamCursor &DeclsCursor, |
| 3498 | const RecordData &Record, unsigned &Idx) { |
Argyrios Kyrtzidis | 8731ca7 | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 3499 | switch ((TemplateArgument::ArgKind)Record[Idx++]) { |
| 3500 | case TemplateArgument::Null: |
| 3501 | return TemplateArgument(); |
| 3502 | case TemplateArgument::Type: |
| 3503 | return TemplateArgument(GetType(Record[Idx++])); |
| 3504 | case TemplateArgument::Declaration: |
| 3505 | return TemplateArgument(GetDecl(Record[Idx++])); |
Argyrios Kyrtzidis | dc767e3 | 2010-06-28 09:31:34 +0000 | [diff] [blame] | 3506 | case TemplateArgument::Integral: { |
| 3507 | llvm::APSInt Value = ReadAPSInt(Record, Idx); |
| 3508 | QualType T = GetType(Record[Idx++]); |
| 3509 | return TemplateArgument(Value, T); |
| 3510 | } |
Argyrios Kyrtzidis | 8731ca7 | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 3511 | case TemplateArgument::Template: |
| 3512 | return TemplateArgument(ReadTemplateName(Record, Idx)); |
| 3513 | case TemplateArgument::Expression: |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 3514 | return TemplateArgument(ReadExpr(DeclsCursor)); |
Argyrios Kyrtzidis | 8731ca7 | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 3515 | case TemplateArgument::Pack: { |
| 3516 | unsigned NumArgs = Record[Idx++]; |
| 3517 | llvm::SmallVector<TemplateArgument, 8> Args; |
| 3518 | Args.reserve(NumArgs); |
| 3519 | while (NumArgs--) |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 3520 | Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx)); |
Argyrios Kyrtzidis | 8731ca7 | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 3521 | TemplateArgument TemplArg; |
| 3522 | TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true); |
| 3523 | return TemplArg; |
| 3524 | } |
| 3525 | } |
| 3526 | |
| 3527 | assert(0 && "Unhandled template argument kind!"); |
| 3528 | return TemplateArgument(); |
| 3529 | } |
| 3530 | |
Argyrios Kyrtzidis | dd41c14 | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 3531 | TemplateParameterList * |
| 3532 | PCHReader::ReadTemplateParameterList(const RecordData &Record, unsigned &Idx) { |
| 3533 | SourceLocation TemplateLoc = ReadSourceLocation(Record, Idx); |
| 3534 | SourceLocation LAngleLoc = ReadSourceLocation(Record, Idx); |
| 3535 | SourceLocation RAngleLoc = ReadSourceLocation(Record, Idx); |
| 3536 | |
| 3537 | unsigned NumParams = Record[Idx++]; |
| 3538 | llvm::SmallVector<NamedDecl *, 16> Params; |
| 3539 | Params.reserve(NumParams); |
| 3540 | while (NumParams--) |
| 3541 | Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++]))); |
| 3542 | |
| 3543 | TemplateParameterList* TemplateParams = |
| 3544 | TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc, |
| 3545 | Params.data(), Params.size(), RAngleLoc); |
| 3546 | return TemplateParams; |
| 3547 | } |
| 3548 | |
| 3549 | void |
| 3550 | PCHReader:: |
| 3551 | ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs, |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 3552 | llvm::BitstreamCursor &DeclsCursor, |
Argyrios Kyrtzidis | dd41c14 | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 3553 | const RecordData &Record, unsigned &Idx) { |
| 3554 | unsigned NumTemplateArgs = Record[Idx++]; |
| 3555 | TemplArgs.reserve(NumTemplateArgs); |
| 3556 | while (NumTemplateArgs--) |
Sebastian Redl | 577d479 | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 3557 | TemplArgs.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx)); |
Argyrios Kyrtzidis | dd41c14 | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
Argyrios Kyrtzidis | 37ffed3 | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 3560 | /// \brief Read a UnresolvedSet structure. |
| 3561 | void PCHReader::ReadUnresolvedSet(UnresolvedSetImpl &Set, |
| 3562 | const RecordData &Record, unsigned &Idx) { |
| 3563 | unsigned NumDecls = Record[Idx++]; |
| 3564 | while (NumDecls--) { |
| 3565 | NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++])); |
| 3566 | AccessSpecifier AS = (AccessSpecifier)Record[Idx++]; |
| 3567 | Set.addDecl(D, AS); |
| 3568 | } |
| 3569 | } |
| 3570 | |
Argyrios Kyrtzidis | 0745d0a | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 3571 | CXXBaseSpecifier |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 3572 | PCHReader::ReadCXXBaseSpecifier(llvm::BitstreamCursor &DeclsCursor, |
| 3573 | const RecordData &Record, unsigned &Idx) { |
Argyrios Kyrtzidis | 0745d0a | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 3574 | bool isVirtual = static_cast<bool>(Record[Idx++]); |
| 3575 | bool isBaseOfClass = static_cast<bool>(Record[Idx++]); |
| 3576 | AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]); |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 3577 | TypeSourceInfo *TInfo = GetTypeSourceInfo(DeclsCursor, Record, Idx); |
Argyrios Kyrtzidis | 0745d0a | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 3578 | SourceRange Range = ReadSourceRange(Record, Idx); |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 3579 | return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo); |
Argyrios Kyrtzidis | 0745d0a | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 3580 | } |
| 3581 | |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 3582 | NestedNameSpecifier * |
| 3583 | PCHReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) { |
| 3584 | unsigned N = Record[Idx++]; |
| 3585 | NestedNameSpecifier *NNS = 0, *Prev = 0; |
| 3586 | for (unsigned I = 0; I != N; ++I) { |
| 3587 | NestedNameSpecifier::SpecifierKind Kind |
| 3588 | = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; |
| 3589 | switch (Kind) { |
| 3590 | case NestedNameSpecifier::Identifier: { |
| 3591 | IdentifierInfo *II = GetIdentifierInfo(Record, Idx); |
| 3592 | NNS = NestedNameSpecifier::Create(*Context, Prev, II); |
| 3593 | break; |
| 3594 | } |
| 3595 | |
| 3596 | case NestedNameSpecifier::Namespace: { |
| 3597 | NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++])); |
| 3598 | NNS = NestedNameSpecifier::Create(*Context, Prev, NS); |
| 3599 | break; |
| 3600 | } |
| 3601 | |
| 3602 | case NestedNameSpecifier::TypeSpec: |
| 3603 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 3604 | Type *T = GetType(Record[Idx++]).getTypePtr(); |
| 3605 | bool Template = Record[Idx++]; |
| 3606 | NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T); |
| 3607 | break; |
| 3608 | } |
| 3609 | |
| 3610 | case NestedNameSpecifier::Global: { |
| 3611 | NNS = NestedNameSpecifier::GlobalSpecifier(*Context); |
| 3612 | // No associated value, and there can't be a prefix. |
| 3613 | break; |
| 3614 | } |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 3615 | } |
Argyrios Kyrtzidis | d2bb2c0 | 2010-07-07 15:46:30 +0000 | [diff] [blame] | 3616 | Prev = NNS; |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 3617 | } |
| 3618 | return NNS; |
| 3619 | } |
| 3620 | |
| 3621 | SourceRange |
| 3622 | PCHReader::ReadSourceRange(const RecordData &Record, unsigned &Idx) { |
Daniel Dunbar | 8ee5939 | 2010-06-02 15:47:10 +0000 | [diff] [blame] | 3623 | SourceLocation beg = SourceLocation::getFromRawEncoding(Record[Idx++]); |
| 3624 | SourceLocation end = SourceLocation::getFromRawEncoding(Record[Idx++]); |
| 3625 | return SourceRange(beg, end); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 3626 | } |
| 3627 | |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 3628 | /// \brief Read an integral value |
| 3629 | llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { |
| 3630 | unsigned BitWidth = Record[Idx++]; |
| 3631 | unsigned NumWords = llvm::APInt::getNumWords(BitWidth); |
| 3632 | llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); |
| 3633 | Idx += NumWords; |
| 3634 | return Result; |
| 3635 | } |
| 3636 | |
| 3637 | /// \brief Read a signed integral value |
| 3638 | llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { |
| 3639 | bool isUnsigned = Record[Idx++]; |
| 3640 | return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); |
| 3641 | } |
| 3642 | |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 3643 | /// \brief Read a floating-point value |
| 3644 | llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) { |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 3645 | return llvm::APFloat(ReadAPInt(Record, Idx)); |
| 3646 | } |
| 3647 | |
Douglas Gregor | 68a2eb0 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 3648 | // \brief Read a string |
| 3649 | std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) { |
| 3650 | unsigned Len = Record[Idx++]; |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 3651 | std::string Result(Record.data() + Idx, Record.data() + Idx + Len); |
Douglas Gregor | 68a2eb0 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 3652 | Idx += Len; |
| 3653 | return Result; |
| 3654 | } |
| 3655 | |
Chris Lattner | d259836 | 2010-05-10 00:25:06 +0000 | [diff] [blame] | 3656 | CXXTemporary *PCHReader::ReadCXXTemporary(const RecordData &Record, |
| 3657 | unsigned &Idx) { |
| 3658 | CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++])); |
| 3659 | return CXXTemporary::Create(*Context, Decl); |
| 3660 | } |
| 3661 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 3662 | DiagnosticBuilder PCHReader::Diag(unsigned DiagID) { |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 3663 | return Diag(SourceLocation(), DiagID); |
| 3664 | } |
| 3665 | |
| 3666 | DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 3667 | return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 3668 | } |
Douglas Gregor | 025452f | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 3669 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3670 | /// \brief Retrieve the identifier table associated with the |
| 3671 | /// preprocessor. |
| 3672 | IdentifierTable &PCHReader::getIdentifierTable() { |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 3673 | assert(PP && "Forgot to set Preprocessor ?"); |
| 3674 | return PP->getIdentifierTable(); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3675 | } |
| 3676 | |
Douglas Gregor | 025452f | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 3677 | /// \brief Record that the given ID maps to the given switch-case |
| 3678 | /// statement. |
| 3679 | void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { |
| 3680 | assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID"); |
| 3681 | SwitchCaseStmts[ID] = SC; |
| 3682 | } |
| 3683 | |
| 3684 | /// \brief Retrieve the switch-case statement with the given ID. |
| 3685 | SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) { |
| 3686 | assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID"); |
| 3687 | return SwitchCaseStmts[ID]; |
| 3688 | } |
Douglas Gregor | 1de05fe | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 3689 | |
| 3690 | /// \brief Record that the given label statement has been |
| 3691 | /// deserialized and has the given ID. |
| 3692 | void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3693 | assert(LabelStmts.find(ID) == LabelStmts.end() && |
Douglas Gregor | 1de05fe | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 3694 | "Deserialized label twice"); |
| 3695 | LabelStmts[ID] = S; |
| 3696 | |
| 3697 | // If we've already seen any goto statements that point to this |
| 3698 | // label, resolve them now. |
| 3699 | typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter; |
| 3700 | std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID); |
| 3701 | for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto) |
| 3702 | Goto->second->setLabel(S); |
| 3703 | UnresolvedGotoStmts.erase(Gotos.first, Gotos.second); |
Douglas Gregor | 7d5c2f2 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 3704 | |
| 3705 | // If we've already seen any address-label statements that point to |
| 3706 | // this label, resolve them now. |
| 3707 | typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3708 | std::pair<AddrLabelIter, AddrLabelIter> AddrLabels |
Douglas Gregor | 7d5c2f2 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 3709 | = UnresolvedAddrLabelExprs.equal_range(ID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3710 | for (AddrLabelIter AddrLabel = AddrLabels.first; |
Douglas Gregor | 7d5c2f2 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 3711 | AddrLabel != AddrLabels.second; ++AddrLabel) |
| 3712 | AddrLabel->second->setLabel(S); |
| 3713 | UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second); |
Douglas Gregor | 1de05fe | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 3714 | } |
| 3715 | |
| 3716 | /// \brief Set the label of the given statement to the label |
| 3717 | /// identified by ID. |
| 3718 | /// |
| 3719 | /// Depending on the order in which the label and other statements |
| 3720 | /// referencing that label occur, this operation may complete |
| 3721 | /// immediately (updating the statement) or it may queue the |
| 3722 | /// statement to be back-patched later. |
| 3723 | void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) { |
| 3724 | std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID); |
| 3725 | if (Label != LabelStmts.end()) { |
| 3726 | // We've already seen this label, so set the label of the goto and |
| 3727 | // we're done. |
| 3728 | S->setLabel(Label->second); |
| 3729 | } else { |
| 3730 | // We haven't seen this label yet, so add this goto to the set of |
| 3731 | // unresolved goto statements. |
| 3732 | UnresolvedGotoStmts.insert(std::make_pair(ID, S)); |
| 3733 | } |
| 3734 | } |
Douglas Gregor | 7d5c2f2 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 3735 | |
| 3736 | /// \brief Set the label of the given expression to the label |
| 3737 | /// identified by ID. |
| 3738 | /// |
| 3739 | /// Depending on the order in which the label and other statements |
| 3740 | /// referencing that label occur, this operation may complete |
| 3741 | /// immediately (updating the statement) or it may queue the |
| 3742 | /// statement to be back-patched later. |
| 3743 | void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) { |
| 3744 | std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID); |
| 3745 | if (Label != LabelStmts.end()) { |
| 3746 | // We've already seen this label, so set the label of the |
| 3747 | // label-address expression and we're done. |
| 3748 | S->setLabel(Label->second); |
| 3749 | } else { |
| 3750 | // We haven't seen this label yet, so add this label-address |
| 3751 | // expression to the set of unresolved label-address expressions. |
| 3752 | UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S)); |
| 3753 | } |
| 3754 | } |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 3755 | |
Argyrios Kyrtzidis | 29ee3a2 | 2010-07-30 10:03:16 +0000 | [diff] [blame] | 3756 | void PCHReader::FinishedDeserializing() { |
| 3757 | assert(NumCurrentElementsDeserializing && |
| 3758 | "FinishedDeserializing not paired with StartedDeserializing"); |
| 3759 | if (NumCurrentElementsDeserializing == 1) { |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 3760 | // If any identifiers with corresponding top-level declarations have |
| 3761 | // been loaded, load those declarations now. |
Argyrios Kyrtzidis | 29ee3a2 | 2010-07-30 10:03:16 +0000 | [diff] [blame] | 3762 | while (!PendingIdentifierInfos.empty()) { |
| 3763 | SetGloballyVisibleDecls(PendingIdentifierInfos.front().II, |
| 3764 | PendingIdentifierInfos.front().DeclIDs, true); |
| 3765 | PendingIdentifierInfos.pop_front(); |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 3766 | } |
Argyrios Kyrtzidis | bb80a8e | 2010-07-07 15:46:26 +0000 | [diff] [blame] | 3767 | |
| 3768 | // We are not in recursive loading, so it's safe to pass the "interesting" |
| 3769 | // decls to the consumer. |
Argyrios Kyrtzidis | 29ee3a2 | 2010-07-30 10:03:16 +0000 | [diff] [blame] | 3770 | if (Consumer) |
| 3771 | PassInterestingDeclsToConsumer(); |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 3772 | } |
Argyrios Kyrtzidis | 29ee3a2 | 2010-07-30 10:03:16 +0000 | [diff] [blame] | 3773 | --NumCurrentElementsDeserializing; |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 3774 | } |