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