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" |
Daniel Dunbar | c716293 | 2009-11-11 23:58:53 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/Utils.h" |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 17 | #include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 21 | #include "clang/AST/Type.h" |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 22 | #include "clang/AST/TypeLocVisitor.h" |
Chris Lattner | 42d42b5 | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 23 | #include "clang/Lex/MacroInfo.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 24 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 25 | #include "clang/Lex/HeaderSearch.h" |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 26 | #include "clang/Basic/OnDiskHashTable.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 27 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 28 | #include "clang/Basic/SourceManagerInternals.h" |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 29 | #include "clang/Basic/FileManager.h" |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 30 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | 445e23e | 2009-10-05 21:07:28 +0000 | [diff] [blame] | 31 | #include "clang/Basic/Version.h" |
Daniel Dunbar | 2596e42 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 33 | #include "llvm/Bitcode/BitstreamReader.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MemoryBuffer.h" |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | d5b2197 | 2009-11-18 19:50:41 +0000 | [diff] [blame] | 36 | #include "llvm/System/Path.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 37 | #include <algorithm> |
Douglas Gregor | e721f95 | 2009-04-28 18:58:38 +0000 | [diff] [blame] | 38 | #include <iterator> |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 39 | #include <cstdio> |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 40 | #include <sys/stat.h> |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 41 | using namespace clang; |
| 42 | |
| 43 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 44 | // PCH reader validator implementation |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
| 47 | PCHReaderListener::~PCHReaderListener() {} |
| 48 | |
| 49 | bool |
| 50 | PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) { |
| 51 | const LangOptions &PPLangOpts = PP.getLangOptions(); |
| 52 | #define PARSE_LANGOPT_BENIGN(Option) |
| 53 | #define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \ |
| 54 | if (PPLangOpts.Option != LangOpts.Option) { \ |
| 55 | Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \ |
| 56 | return true; \ |
| 57 | } |
| 58 | |
| 59 | PARSE_LANGOPT_BENIGN(Trigraphs); |
| 60 | PARSE_LANGOPT_BENIGN(BCPLComment); |
| 61 | PARSE_LANGOPT_BENIGN(DollarIdents); |
| 62 | PARSE_LANGOPT_BENIGN(AsmPreprocessor); |
| 63 | PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions); |
| 64 | PARSE_LANGOPT_BENIGN(ImplicitInt); |
| 65 | PARSE_LANGOPT_BENIGN(Digraphs); |
| 66 | PARSE_LANGOPT_BENIGN(HexFloats); |
| 67 | PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99); |
| 68 | PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions); |
| 69 | PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus); |
| 70 | PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x); |
| 71 | PARSE_LANGOPT_BENIGN(CXXOperatorName); |
| 72 | PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c); |
| 73 | PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2); |
| 74 | PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi); |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 75 | PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 76 | PARSE_LANGOPT_BENIGN(PascalStrings); |
| 77 | PARSE_LANGOPT_BENIGN(WritableStrings); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | PARSE_LANGOPT_IMPORTANT(LaxVectorConversions, |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 79 | diag::warn_pch_lax_vector_conversions); |
Nate Begeman | 69cfb9b | 2009-06-25 22:57:40 +0000 | [diff] [blame] | 80 | PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 81 | PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions); |
Daniel Dunbar | 7348288 | 2010-02-10 18:48:44 +0000 | [diff] [blame] | 82 | PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 83 | PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime); |
| 84 | PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding); |
| 85 | PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics, |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 87 | diag::warn_pch_thread_safe_statics); |
Daniel Dunbar | 5345c39 | 2009-09-03 04:54:28 +0000 | [diff] [blame] | 88 | PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 89 | PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks); |
| 90 | PARSE_LANGOPT_BENIGN(EmitAllDecls); |
| 91 | PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno); |
| 92 | PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | PARSE_LANGOPT_IMPORTANT(HeinousExtensions, |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 94 | diag::warn_pch_heinous_extensions); |
| 95 | // FIXME: Most of the options below are benign if the macro wasn't |
| 96 | // used. Unfortunately, this means that a PCH compiled without |
| 97 | // optimization can't be used with optimization turned on, even |
| 98 | // though the only thing that changes is whether __OPTIMIZE__ was |
| 99 | // defined... but if __OPTIMIZE__ never showed up in the header, it |
| 100 | // doesn't matter. We could consider making this some special kind |
| 101 | // of check. |
| 102 | PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize); |
| 103 | PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size); |
| 104 | PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static); |
| 105 | PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level); |
| 106 | PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline); |
| 107 | PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline); |
| 108 | PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control); |
| 109 | PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed); |
John Thompson | a6fda12 | 2009-11-05 20:14:16 +0000 | [diff] [blame] | 110 | PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 111 | if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | Reader.Diag(diag::warn_pch_gc_mode) |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 113 | << LangOpts.getGCMode() << PPLangOpts.getGCMode(); |
| 114 | return true; |
| 115 | } |
| 116 | PARSE_LANGOPT_BENIGN(getVisibilityMode()); |
Daniel Dunbar | ab8e281 | 2009-09-21 04:16:19 +0000 | [diff] [blame] | 117 | PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(), |
| 118 | diag::warn_pch_stack_protector); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 119 | PARSE_LANGOPT_BENIGN(InstantiationDepth); |
Nate Begeman | 69cfb9b | 2009-06-25 22:57:40 +0000 | [diff] [blame] | 120 | PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl); |
Mike Stump | 9c276ae | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 121 | PARSE_LANGOPT_BENIGN(CatchUndefined); |
Daniel Dunbar | ab8e281 | 2009-09-21 04:16:19 +0000 | [diff] [blame] | 122 | PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors); |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame^] | 123 | #undef PARSE_LANGOPT_IMPORTANT |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 124 | #undef PARSE_LANGOPT_BENIGN |
| 125 | |
| 126 | return false; |
| 127 | } |
| 128 | |
Daniel Dunbar | dc3c0d2 | 2009-11-11 00:52:11 +0000 | [diff] [blame] | 129 | bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) { |
| 130 | if (Triple == PP.getTargetInfo().getTriple().str()) |
| 131 | return false; |
| 132 | |
| 133 | Reader.Diag(diag::warn_pch_target_triple) |
| 134 | << Triple << PP.getTargetInfo().getTriple().str(); |
| 135 | return true; |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Daniel Dunbar | dc3c0d2 | 2009-11-11 00:52:11 +0000 | [diff] [blame] | 138 | bool PCHValidator::ReadPredefinesBuffer(llvm::StringRef PCHPredef, |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 139 | FileID PCHBufferID, |
Daniel Dunbar | 7b5a121 | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 140 | llvm::StringRef OriginalFileName, |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 141 | std::string &SuggestedPredefines) { |
Daniel Dunbar | c716293 | 2009-11-11 23:58:53 +0000 | [diff] [blame] | 142 | // We are in the context of an implicit include, so the predefines buffer will |
| 143 | // have a #include entry for the PCH file itself (as normalized by the |
| 144 | // preprocessor initialization). Find it and skip over it in the checking |
| 145 | // below. |
Daniel Dunbar | 7b5a121 | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 146 | llvm::SmallString<256> PCHInclude; |
| 147 | PCHInclude += "#include \""; |
Daniel Dunbar | c716293 | 2009-11-11 23:58:53 +0000 | [diff] [blame] | 148 | PCHInclude += NormalizeDashIncludePath(OriginalFileName); |
Daniel Dunbar | 7b5a121 | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 149 | PCHInclude += "\"\n"; |
| 150 | std::pair<llvm::StringRef,llvm::StringRef> Split = |
| 151 | llvm::StringRef(PP.getPredefines()).split(PCHInclude.str()); |
| 152 | llvm::StringRef Left = Split.first, Right = Split.second; |
| 153 | assert(Left != PP.getPredefines() && "Missing PCH include entry!"); |
| 154 | |
| 155 | // If the predefines is equal to the joined left and right halves, we're done! |
| 156 | if (Left.size() + Right.size() == PCHPredef.size() && |
| 157 | PCHPredef.startswith(Left) && PCHPredef.endswith(Right)) |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 158 | return false; |
| 159 | |
| 160 | SourceManager &SourceMgr = PP.getSourceManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | |
Daniel Dunbar | 10014aa | 2009-11-11 03:45:59 +0000 | [diff] [blame] | 162 | // The predefines buffers are different. Determine what the differences are, |
| 163 | // and whether they require us to reject the PCH file. |
Daniel Dunbar | e675049 | 2009-11-13 16:46:11 +0000 | [diff] [blame] | 164 | llvm::SmallVector<llvm::StringRef, 8> PCHLines; |
| 165 | PCHPredef.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
| 166 | |
| 167 | llvm::SmallVector<llvm::StringRef, 8> CmdLineLines; |
| 168 | Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
| 169 | Right.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 170 | |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 171 | // Sort both sets of predefined buffer lines, since we allow some extra |
| 172 | // definitions and they may appear at any point in the output. |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 173 | std::sort(CmdLineLines.begin(), CmdLineLines.end()); |
| 174 | std::sort(PCHLines.begin(), PCHLines.end()); |
| 175 | |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 176 | // Determine which predefines that were used to build the PCH file are missing |
| 177 | // from the command line. |
| 178 | std::vector<llvm::StringRef> MissingPredefines; |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 179 | std::set_difference(PCHLines.begin(), PCHLines.end(), |
| 180 | CmdLineLines.begin(), CmdLineLines.end(), |
| 181 | std::back_inserter(MissingPredefines)); |
| 182 | |
| 183 | bool MissingDefines = false; |
| 184 | bool ConflictingDefines = false; |
| 185 | for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) { |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 186 | llvm::StringRef Missing = MissingPredefines[I]; |
| 187 | if (!Missing.startswith("#define ")) { |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 188 | Reader.Diag(diag::warn_pch_compiler_options_mismatch); |
| 189 | return true; |
| 190 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
Daniel Dunbar | 10014aa | 2009-11-11 03:45:59 +0000 | [diff] [blame] | 192 | // This is a macro definition. Determine the name of the macro we're |
| 193 | // defining. |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 194 | std::string::size_type StartOfMacroName = strlen("#define "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | std::string::size_type EndOfMacroName |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 196 | = Missing.find_first_of("( \n\r", StartOfMacroName); |
| 197 | assert(EndOfMacroName != std::string::npos && |
| 198 | "Couldn't find the end of the macro name"); |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 199 | llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 200 | |
Daniel Dunbar | 10014aa | 2009-11-11 03:45:59 +0000 | [diff] [blame] | 201 | // Determine whether this macro was given a different definition on the |
| 202 | // command line. |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 203 | std::string MacroDefStart = "#define " + MacroName.str(); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 204 | std::string::size_type MacroDefLen = MacroDefStart.size(); |
Daniel Dunbar | e675049 | 2009-11-13 16:46:11 +0000 | [diff] [blame] | 205 | llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 206 | = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(), |
| 207 | MacroDefStart); |
| 208 | for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) { |
Daniel Dunbar | 10014aa | 2009-11-11 03:45:59 +0000 | [diff] [blame] | 209 | if (!ConflictPos->startswith(MacroDefStart)) { |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 210 | // Different macro; we're done. |
| 211 | ConflictPos = CmdLineLines.end(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | break; |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 213 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | |
| 215 | assert(ConflictPos->size() > MacroDefLen && |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 216 | "Invalid #define in predefines buffer?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 217 | if ((*ConflictPos)[MacroDefLen] != ' ' && |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 218 | (*ConflictPos)[MacroDefLen] != '(') |
| 219 | continue; // Longer macro name; keep trying. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 221 | // We found a conflicting macro definition. |
| 222 | break; |
| 223 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 225 | if (ConflictPos != CmdLineLines.end()) { |
| 226 | Reader.Diag(diag::warn_cmdline_conflicting_macro_def) |
| 227 | << MacroName; |
| 228 | |
| 229 | // Show the definition of this macro within the PCH file. |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 230 | llvm::StringRef::size_type Offset = PCHPredef.find(Missing); |
| 231 | assert(Offset != llvm::StringRef::npos && "Unable to find macro!"); |
| 232 | SourceLocation PCHMissingLoc = SourceMgr.getLocForStartOfFile(PCHBufferID) |
| 233 | .getFileLocWithOffset(Offset); |
| 234 | Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName; |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 235 | |
| 236 | ConflictingDefines = true; |
| 237 | continue; |
| 238 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | |
Daniel Dunbar | 10014aa | 2009-11-11 03:45:59 +0000 | [diff] [blame] | 240 | // If the macro doesn't conflict, then we'll just pick up the macro |
| 241 | // 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] | 242 | if (ConflictingDefines) |
| 243 | continue; // Don't complain if there are already conflicting defs |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 245 | if (!MissingDefines) { |
| 246 | Reader.Diag(diag::warn_cmdline_missing_macro_defs); |
| 247 | MissingDefines = true; |
| 248 | } |
| 249 | |
| 250 | // Show the definition of this macro within the PCH file. |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 251 | llvm::StringRef::size_type Offset = PCHPredef.find(Missing); |
| 252 | assert(Offset != llvm::StringRef::npos && "Unable to find macro!"); |
| 253 | SourceLocation PCHMissingLoc = SourceMgr.getLocForStartOfFile(PCHBufferID) |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 254 | .getFileLocWithOffset(Offset); |
| 255 | Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch); |
| 256 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 258 | if (ConflictingDefines) |
| 259 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 261 | // Determine what predefines were introduced based on command-line |
| 262 | // parameters that were not present when building the PCH |
| 263 | // file. Extra #defines are okay, so long as the identifiers being |
| 264 | // defined were not used within the precompiled header. |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 265 | std::vector<llvm::StringRef> ExtraPredefines; |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 266 | std::set_difference(CmdLineLines.begin(), CmdLineLines.end(), |
| 267 | PCHLines.begin(), PCHLines.end(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | std::back_inserter(ExtraPredefines)); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 269 | for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) { |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 270 | llvm::StringRef &Extra = ExtraPredefines[I]; |
| 271 | if (!Extra.startswith("#define ")) { |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 272 | Reader.Diag(diag::warn_pch_compiler_options_mismatch); |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | // This is an extra macro definition. Determine the name of the |
| 277 | // macro we're defining. |
| 278 | std::string::size_type StartOfMacroName = strlen("#define "); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | std::string::size_type EndOfMacroName |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 280 | = Extra.find_first_of("( \n\r", StartOfMacroName); |
| 281 | assert(EndOfMacroName != std::string::npos && |
| 282 | "Couldn't find the end of the macro name"); |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 283 | llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 284 | |
| 285 | // Check whether this name was used somewhere in the PCH file. If |
| 286 | // so, defining it as a macro could change behavior, so we reject |
| 287 | // the PCH file. |
Daniel Dunbar | 4d5936a | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 288 | if (IdentifierInfo *II = Reader.get(MacroName)) { |
Daniel Dunbar | 4fda42e | 2009-11-11 00:52:00 +0000 | [diff] [blame] | 289 | Reader.Diag(diag::warn_macro_name_used_in_pch) << II; |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 290 | return true; |
| 291 | } |
| 292 | |
| 293 | // Add this definition to the suggested predefines buffer. |
| 294 | SuggestedPredefines += Extra; |
| 295 | SuggestedPredefines += '\n'; |
| 296 | } |
| 297 | |
| 298 | // If we get here, it's because the predefines buffer had compatible |
| 299 | // contents. Accept the PCH file. |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI) { |
| 304 | PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, NumHeaderInfos++); |
| 305 | } |
| 306 | |
| 307 | void PCHValidator::ReadCounter(unsigned Value) { |
| 308 | PP.setCounterValue(Value); |
| 309 | } |
| 310 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 311 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 312 | // PCH reader implementation |
| 313 | //===----------------------------------------------------------------------===// |
| 314 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | PCHReader::PCHReader(Preprocessor &PP, ASTContext *Context, |
| 316 | const char *isysroot) |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 317 | : Listener(new PCHValidator(PP, *this)), SourceMgr(PP.getSourceManager()), |
| 318 | FileMgr(PP.getFileManager()), Diags(PP.getDiagnostics()), |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 319 | SemaObj(0), PP(&PP), Context(Context), StatCache(0), Consumer(0), |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 320 | IdentifierTableData(0), IdentifierLookupTable(0), |
| 321 | IdentifierOffsets(0), |
| 322 | MethodPoolLookupTable(0), MethodPoolLookupTableData(0), |
| 323 | TotalSelectorsInMethodPool(0), SelectorOffsets(0), |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 324 | TotalNumSelectors(0), Comments(0), NumComments(0), isysroot(isysroot), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | NumStatHits(0), NumStatMisses(0), |
| 326 | NumSLocEntriesRead(0), NumStatementsRead(0), |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 327 | NumMacrosRead(0), NumMethodPoolSelectorsRead(0), NumMethodPoolMisses(0), |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 328 | NumLexicalDeclContextsRead(0), NumVisibleDeclContextsRead(0), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | CurrentlyLoadingTypeOrDecl(0) { |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 330 | RelocatablePCH = false; |
| 331 | } |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 332 | |
| 333 | PCHReader::PCHReader(SourceManager &SourceMgr, FileManager &FileMgr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | Diagnostic &Diags, const char *isysroot) |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 335 | : SourceMgr(SourceMgr), FileMgr(FileMgr), Diags(Diags), |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 336 | SemaObj(0), PP(0), Context(0), StatCache(0), Consumer(0), |
Chris Lattner | 4c6f952 | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 337 | IdentifierTableData(0), IdentifierLookupTable(0), |
| 338 | IdentifierOffsets(0), |
| 339 | MethodPoolLookupTable(0), MethodPoolLookupTableData(0), |
| 340 | TotalSelectorsInMethodPool(0), SelectorOffsets(0), |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 341 | TotalNumSelectors(0), Comments(0), NumComments(0), isysroot(isysroot), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | NumStatHits(0), NumStatMisses(0), |
| 343 | NumSLocEntriesRead(0), NumStatementsRead(0), |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 344 | NumMacrosRead(0), NumMethodPoolSelectorsRead(0), NumMethodPoolMisses(0), |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 345 | NumLexicalDeclContextsRead(0), NumVisibleDeclContextsRead(0), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | CurrentlyLoadingTypeOrDecl(0) { |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 347 | RelocatablePCH = false; |
| 348 | } |
Chris Lattner | 4c6f952 | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 349 | |
| 350 | PCHReader::~PCHReader() {} |
| 351 | |
Chris Lattner | da93061 | 2009-04-27 05:58:23 +0000 | [diff] [blame] | 352 | Expr *PCHReader::ReadDeclExpr() { |
| 353 | return dyn_cast_or_null<Expr>(ReadStmt(DeclsCursor)); |
| 354 | } |
| 355 | |
| 356 | Expr *PCHReader::ReadTypeExpr() { |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 357 | return dyn_cast_or_null<Expr>(ReadStmt(DeclsCursor)); |
Chris Lattner | 4c6f952 | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 361 | namespace { |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 362 | class PCHMethodPoolLookupTrait { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 363 | PCHReader &Reader; |
| 364 | |
| 365 | public: |
| 366 | typedef std::pair<ObjCMethodList, ObjCMethodList> data_type; |
| 367 | |
| 368 | typedef Selector external_key_type; |
| 369 | typedef external_key_type internal_key_type; |
| 370 | |
| 371 | explicit PCHMethodPoolLookupTrait(PCHReader &Reader) : Reader(Reader) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 373 | static bool EqualKey(const internal_key_type& a, |
| 374 | const internal_key_type& b) { |
| 375 | return a == b; |
| 376 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 378 | static unsigned ComputeHash(Selector Sel) { |
| 379 | unsigned N = Sel.getNumArgs(); |
| 380 | if (N == 0) |
| 381 | ++N; |
| 382 | unsigned R = 5381; |
| 383 | for (unsigned I = 0; I != N; ++I) |
| 384 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I)) |
Daniel Dunbar | 2596e42 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 385 | R = llvm::HashString(II->getName(), R); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 386 | return R; |
| 387 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 389 | // This hopefully will just get inlined and removed by the optimizer. |
| 390 | static const internal_key_type& |
| 391 | GetInternalKey(const external_key_type& x) { return x; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 393 | static std::pair<unsigned, unsigned> |
| 394 | ReadKeyDataLength(const unsigned char*& d) { |
| 395 | using namespace clang::io; |
| 396 | unsigned KeyLen = ReadUnalignedLE16(d); |
| 397 | unsigned DataLen = ReadUnalignedLE16(d); |
| 398 | return std::make_pair(KeyLen, DataLen); |
| 399 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 400 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 401 | internal_key_type ReadKey(const unsigned char* d, unsigned) { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 402 | using namespace clang::io; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 403 | SelectorTable &SelTable = Reader.getContext()->Selectors; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 404 | unsigned N = ReadUnalignedLE16(d); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | IdentifierInfo *FirstII |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 406 | = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)); |
| 407 | if (N == 0) |
| 408 | return SelTable.getNullarySelector(FirstII); |
| 409 | else if (N == 1) |
| 410 | return SelTable.getUnarySelector(FirstII); |
| 411 | |
| 412 | llvm::SmallVector<IdentifierInfo *, 16> Args; |
| 413 | Args.push_back(FirstII); |
| 414 | for (unsigned I = 1; I != N; ++I) |
| 415 | Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d))); |
| 416 | |
Douglas Gregor | 75fdb23 | 2009-05-22 22:45:36 +0000 | [diff] [blame] | 417 | return SelTable.getSelector(N, Args.data()); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 418 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 420 | data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) { |
| 421 | using namespace clang::io; |
| 422 | unsigned NumInstanceMethods = ReadUnalignedLE16(d); |
| 423 | unsigned NumFactoryMethods = ReadUnalignedLE16(d); |
| 424 | |
| 425 | data_type Result; |
| 426 | |
| 427 | // Load instance methods |
| 428 | ObjCMethodList *Prev = 0; |
| 429 | for (unsigned I = 0; I != NumInstanceMethods; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 430 | ObjCMethodDecl *Method |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 431 | = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d))); |
| 432 | if (!Result.first.Method) { |
| 433 | // This is the first method, which is the easy case. |
| 434 | Result.first.Method = Method; |
| 435 | Prev = &Result.first; |
| 436 | continue; |
| 437 | } |
| 438 | |
Ted Kremenek | 298ed87 | 2010-02-11 00:53:01 +0000 | [diff] [blame] | 439 | ObjCMethodList *Mem = |
| 440 | Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>(); |
| 441 | Prev->Next = new (Mem) ObjCMethodList(Method, 0); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 442 | Prev = Prev->Next; |
| 443 | } |
| 444 | |
| 445 | // Load factory methods |
| 446 | Prev = 0; |
| 447 | for (unsigned I = 0; I != NumFactoryMethods; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 448 | ObjCMethodDecl *Method |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 449 | = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d))); |
| 450 | if (!Result.second.Method) { |
| 451 | // This is the first method, which is the easy case. |
| 452 | Result.second.Method = Method; |
| 453 | Prev = &Result.second; |
| 454 | continue; |
| 455 | } |
| 456 | |
Ted Kremenek | 298ed87 | 2010-02-11 00:53:01 +0000 | [diff] [blame] | 457 | ObjCMethodList *Mem = |
| 458 | Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>(); |
| 459 | Prev->Next = new (Mem) ObjCMethodList(Method, 0); |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 460 | Prev = Prev->Next; |
| 461 | } |
| 462 | |
| 463 | return Result; |
| 464 | } |
| 465 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 466 | |
| 467 | } // end anonymous namespace |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 468 | |
| 469 | /// \brief The on-disk hash table used for the global method pool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 470 | typedef OnDiskChainedHashTable<PCHMethodPoolLookupTrait> |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 471 | PCHMethodPoolLookupTable; |
| 472 | |
| 473 | namespace { |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 474 | class PCHIdentifierLookupTrait { |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 475 | PCHReader &Reader; |
| 476 | |
| 477 | // If we know the IdentifierInfo in advance, it is here and we will |
| 478 | // not build a new one. Used when deserializing information about an |
| 479 | // identifier that was constructed before the PCH file was read. |
| 480 | IdentifierInfo *KnownII; |
| 481 | |
| 482 | public: |
| 483 | typedef IdentifierInfo * data_type; |
| 484 | |
| 485 | typedef const std::pair<const char*, unsigned> external_key_type; |
| 486 | |
| 487 | typedef external_key_type internal_key_type; |
| 488 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0) |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 490 | : Reader(Reader), KnownII(II) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 492 | static bool EqualKey(const internal_key_type& a, |
| 493 | const internal_key_type& b) { |
| 494 | return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0 |
| 495 | : false; |
| 496 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 498 | static unsigned ComputeHash(const internal_key_type& a) { |
Daniel Dunbar | 2596e42 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 499 | return llvm::HashString(llvm::StringRef(a.first, a.second)); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 500 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 501 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 502 | // This hopefully will just get inlined and removed by the optimizer. |
| 503 | static const internal_key_type& |
| 504 | GetInternalKey(const external_key_type& x) { return x; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 505 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 506 | static std::pair<unsigned, unsigned> |
| 507 | ReadKeyDataLength(const unsigned char*& d) { |
| 508 | using namespace clang::io; |
Douglas Gregor | 5f8e330 | 2009-04-25 20:26:24 +0000 | [diff] [blame] | 509 | unsigned DataLen = ReadUnalignedLE16(d); |
Douglas Gregor | d6595a4 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 510 | unsigned KeyLen = ReadUnalignedLE16(d); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 511 | return std::make_pair(KeyLen, DataLen); |
| 512 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 514 | static std::pair<const char*, unsigned> |
| 515 | ReadKey(const unsigned char* d, unsigned n) { |
| 516 | assert(n >= 2 && d[n-1] == '\0'); |
| 517 | return std::make_pair((const char*) d, n-1); |
| 518 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 519 | |
| 520 | IdentifierInfo *ReadData(const internal_key_type& k, |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 521 | const unsigned char* d, |
| 522 | unsigned DataLen) { |
| 523 | using namespace clang::io; |
Douglas Gregor | a92193e | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 524 | pch::IdentID ID = ReadUnalignedLE32(d); |
| 525 | bool IsInteresting = ID & 0x01; |
| 526 | |
| 527 | // Wipe out the "is interesting" bit. |
| 528 | ID = ID >> 1; |
| 529 | |
| 530 | if (!IsInteresting) { |
| 531 | // For unintersting identifiers, just build the IdentifierInfo |
| 532 | // and associate it with the persistent ID. |
| 533 | IdentifierInfo *II = KnownII; |
| 534 | if (!II) |
| 535 | II = &Reader.getIdentifierTable().CreateIdentifierInfo( |
| 536 | k.first, k.first + k.second); |
| 537 | Reader.SetIdentifierInfo(ID, II); |
| 538 | return II; |
| 539 | } |
| 540 | |
Douglas Gregor | 5998da5 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 541 | unsigned Bits = ReadUnalignedLE16(d); |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 542 | bool CPlusPlusOperatorKeyword = Bits & 0x01; |
| 543 | Bits >>= 1; |
| 544 | bool Poisoned = Bits & 0x01; |
| 545 | Bits >>= 1; |
| 546 | bool ExtensionToken = Bits & 0x01; |
| 547 | Bits >>= 1; |
| 548 | bool hasMacroDefinition = Bits & 0x01; |
| 549 | Bits >>= 1; |
| 550 | unsigned ObjCOrBuiltinID = Bits & 0x3FF; |
| 551 | Bits >>= 10; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 552 | |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 553 | assert(Bits == 0 && "Extra bits in the identifier?"); |
Douglas Gregor | 5998da5 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 554 | DataLen -= 6; |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 555 | |
| 556 | // Build the IdentifierInfo itself and link the identifier ID with |
| 557 | // the new IdentifierInfo. |
| 558 | IdentifierInfo *II = KnownII; |
| 559 | if (!II) |
Douglas Gregor | 5f8e330 | 2009-04-25 20:26:24 +0000 | [diff] [blame] | 560 | II = &Reader.getIdentifierTable().CreateIdentifierInfo( |
| 561 | k.first, k.first + k.second); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 562 | Reader.SetIdentifierInfo(ID, II); |
| 563 | |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 564 | // Set or check the various bits in the IdentifierInfo structure. |
| 565 | // FIXME: Load token IDs lazily, too? |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 566 | II->setObjCOrBuiltinID(ObjCOrBuiltinID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | assert(II->isExtensionToken() == ExtensionToken && |
Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 568 | "Incorrect extension token flag"); |
| 569 | (void)ExtensionToken; |
| 570 | II->setIsPoisoned(Poisoned); |
| 571 | assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && |
| 572 | "Incorrect C++ operator keyword flag"); |
| 573 | (void)CPlusPlusOperatorKeyword; |
| 574 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 575 | // If this identifier is a macro, deserialize the macro |
| 576 | // definition. |
| 577 | if (hasMacroDefinition) { |
Douglas Gregor | 5998da5 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 578 | uint32_t Offset = ReadUnalignedLE32(d); |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 579 | Reader.ReadMacroRecord(Offset); |
Douglas Gregor | 5998da5 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 580 | DataLen -= 4; |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 581 | } |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 582 | |
| 583 | // Read all of the declarations visible at global scope with this |
| 584 | // name. |
Chris Lattner | 6bf690f | 2009-04-27 22:17:41 +0000 | [diff] [blame] | 585 | if (Reader.getContext() == 0) return II; |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 586 | if (DataLen > 0) { |
| 587 | llvm::SmallVector<uint32_t, 4> DeclIDs; |
| 588 | for (; DataLen > 0; DataLen -= 4) |
| 589 | DeclIDs.push_back(ReadUnalignedLE32(d)); |
| 590 | Reader.SetGloballyVisibleDecls(II, DeclIDs); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 591 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 592 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 593 | return II; |
| 594 | } |
| 595 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 596 | |
| 597 | } // end anonymous namespace |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 598 | |
| 599 | /// \brief The on-disk hash table used to contain information about |
| 600 | /// all of the identifiers in the program. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 601 | typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait> |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 602 | PCHIdentifierLookupTable; |
| 603 | |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 604 | bool PCHReader::Error(const char *Msg) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 605 | unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Fatal, Msg); |
| 606 | Diag(DiagID); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 607 | return true; |
| 608 | } |
| 609 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 610 | /// \brief Check the contents of the predefines buffer against the |
| 611 | /// contents of the predefines buffer used to build the PCH file. |
| 612 | /// |
| 613 | /// The contents of the two predefines buffers should be the same. If |
| 614 | /// not, then some command-line option changed the preprocessor state |
| 615 | /// and we must reject the PCH file. |
| 616 | /// |
| 617 | /// \param PCHPredef The start of the predefines buffer in the PCH |
| 618 | /// file. |
| 619 | /// |
| 620 | /// \param PCHPredefLen The length of the predefines buffer in the PCH |
| 621 | /// file. |
| 622 | /// |
| 623 | /// \param PCHBufferID The FileID for the PCH predefines buffer. |
| 624 | /// |
| 625 | /// \returns true if there was a mismatch (in which case the PCH file |
| 626 | /// should be ignored), or false otherwise. |
Daniel Dunbar | dc3c0d2 | 2009-11-11 00:52:11 +0000 | [diff] [blame] | 627 | bool PCHReader::CheckPredefinesBuffer(llvm::StringRef PCHPredef, |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 628 | FileID PCHBufferID) { |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 629 | if (Listener) |
Daniel Dunbar | dc3c0d2 | 2009-11-11 00:52:11 +0000 | [diff] [blame] | 630 | return Listener->ReadPredefinesBuffer(PCHPredef, PCHBufferID, |
Daniel Dunbar | 7b5a121 | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 631 | ActualOriginalFileName, |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 632 | SuggestedPredefines); |
Douglas Gregor | e721f95 | 2009-04-28 18:58:38 +0000 | [diff] [blame] | 633 | return false; |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 636 | //===----------------------------------------------------------------------===// |
| 637 | // Source Manager Deserialization |
| 638 | //===----------------------------------------------------------------------===// |
| 639 | |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 640 | /// \brief Read the line table in the source manager block. |
| 641 | /// \returns true if ther was an error. |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 642 | bool PCHReader::ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record) { |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 643 | unsigned Idx = 0; |
| 644 | LineTableInfo &LineTable = SourceMgr.getLineTable(); |
| 645 | |
| 646 | // Parse the file names |
Douglas Gregor | ff0a987 | 2009-04-13 17:12:42 +0000 | [diff] [blame] | 647 | std::map<int, int> FileIDs; |
| 648 | for (int I = 0, N = Record[Idx++]; I != N; ++I) { |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 649 | // Extract the file name |
| 650 | unsigned FilenameLen = Record[Idx++]; |
| 651 | std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen); |
| 652 | Idx += FilenameLen; |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 653 | MaybeAddSystemRootToFilename(Filename); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(), |
Douglas Gregor | ff0a987 | 2009-04-13 17:12:42 +0000 | [diff] [blame] | 655 | Filename.size()); |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | // Parse the line entries |
| 659 | std::vector<LineEntry> Entries; |
| 660 | while (Idx < Record.size()) { |
Douglas Gregor | ff0a987 | 2009-04-13 17:12:42 +0000 | [diff] [blame] | 661 | int FID = FileIDs[Record[Idx++]]; |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 662 | |
| 663 | // Extract the line entries |
| 664 | unsigned NumEntries = Record[Idx++]; |
| 665 | Entries.clear(); |
| 666 | Entries.reserve(NumEntries); |
| 667 | for (unsigned I = 0; I != NumEntries; ++I) { |
| 668 | unsigned FileOffset = Record[Idx++]; |
| 669 | unsigned LineNo = Record[Idx++]; |
| 670 | int FilenameID = Record[Idx++]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | SrcMgr::CharacteristicKind FileKind |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 672 | = (SrcMgr::CharacteristicKind)Record[Idx++]; |
| 673 | unsigned IncludeOffset = Record[Idx++]; |
| 674 | Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, |
| 675 | FileKind, IncludeOffset)); |
| 676 | } |
| 677 | LineTable.AddEntry(FID, Entries); |
| 678 | } |
| 679 | |
| 680 | return false; |
| 681 | } |
| 682 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 683 | namespace { |
| 684 | |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 685 | class PCHStatData { |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 686 | public: |
| 687 | const bool hasStat; |
| 688 | const ino_t ino; |
| 689 | const dev_t dev; |
| 690 | const mode_t mode; |
| 691 | const time_t mtime; |
| 692 | const off_t size; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 694 | 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] | 695 | : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {} |
| 696 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 697 | PCHStatData() |
| 698 | : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {} |
| 699 | }; |
| 700 | |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 701 | class PCHStatLookupTrait { |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 702 | public: |
| 703 | typedef const char *external_key_type; |
| 704 | typedef const char *internal_key_type; |
| 705 | |
| 706 | typedef PCHStatData data_type; |
| 707 | |
| 708 | static unsigned ComputeHash(const char *path) { |
Daniel Dunbar | 2596e42 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 709 | return llvm::HashString(path); |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | static internal_key_type GetInternalKey(const char *path) { return path; } |
| 713 | |
| 714 | static bool EqualKey(internal_key_type a, internal_key_type b) { |
| 715 | return strcmp(a, b) == 0; |
| 716 | } |
| 717 | |
| 718 | static std::pair<unsigned, unsigned> |
| 719 | ReadKeyDataLength(const unsigned char*& d) { |
| 720 | unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d); |
| 721 | unsigned DataLen = (unsigned) *d++; |
| 722 | return std::make_pair(KeyLen + 1, DataLen); |
| 723 | } |
| 724 | |
| 725 | static internal_key_type ReadKey(const unsigned char *d, unsigned) { |
| 726 | return (const char *)d; |
| 727 | } |
| 728 | |
| 729 | static data_type ReadData(const internal_key_type, const unsigned char *d, |
| 730 | unsigned /*DataLen*/) { |
| 731 | using namespace clang::io; |
| 732 | |
| 733 | if (*d++ == 1) |
| 734 | return data_type(); |
| 735 | |
| 736 | ino_t ino = (ino_t) ReadUnalignedLE32(d); |
| 737 | dev_t dev = (dev_t) ReadUnalignedLE32(d); |
| 738 | mode_t mode = (mode_t) ReadUnalignedLE16(d); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | time_t mtime = (time_t) ReadUnalignedLE64(d); |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 740 | off_t size = (off_t) ReadUnalignedLE64(d); |
| 741 | return data_type(ino, dev, mode, mtime, size); |
| 742 | } |
| 743 | }; |
| 744 | |
| 745 | /// \brief stat() cache for precompiled headers. |
| 746 | /// |
| 747 | /// This cache is very similar to the stat cache used by pretokenized |
| 748 | /// headers. |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 749 | class PCHStatCache : public StatSysCallCache { |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 750 | typedef OnDiskChainedHashTable<PCHStatLookupTrait> CacheTy; |
| 751 | CacheTy *Cache; |
| 752 | |
| 753 | unsigned &NumStatHits, &NumStatMisses; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 754 | public: |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 755 | PCHStatCache(const unsigned char *Buckets, |
| 756 | const unsigned char *Base, |
| 757 | unsigned &NumStatHits, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 758 | unsigned &NumStatMisses) |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 759 | : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) { |
| 760 | Cache = CacheTy::Create(Buckets, Base); |
| 761 | } |
| 762 | |
| 763 | ~PCHStatCache() { delete Cache; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 764 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 765 | int stat(const char *path, struct stat *buf) { |
| 766 | // Do the lookup for the file's data in the PCH file. |
| 767 | CacheTy::iterator I = Cache->find(path); |
| 768 | |
| 769 | // If we don't get a hit in the PCH file just forward to 'stat'. |
| 770 | if (I == Cache->end()) { |
| 771 | ++NumStatMisses; |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 772 | return StatSysCallCache::stat(path, buf); |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 773 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 775 | ++NumStatHits; |
| 776 | PCHStatData Data = *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 777 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 778 | if (!Data.hasStat) |
| 779 | return 1; |
| 780 | |
| 781 | buf->st_ino = Data.ino; |
| 782 | buf->st_dev = Data.dev; |
| 783 | buf->st_mtime = Data.mtime; |
| 784 | buf->st_mode = Data.mode; |
| 785 | buf->st_size = Data.size; |
| 786 | return 0; |
| 787 | } |
| 788 | }; |
| 789 | } // end anonymous namespace |
| 790 | |
| 791 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 792 | /// \brief Read the source manager block |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 793 | PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() { |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 794 | using namespace SrcMgr; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 795 | |
| 796 | // Set the source-location entry cursor to the current position in |
| 797 | // the stream. This cursor will be used to read the contents of the |
| 798 | // source manager block initially, and then lazily read |
| 799 | // source-location entries as needed. |
| 800 | SLocEntryCursor = Stream; |
| 801 | |
| 802 | // The stream itself is going to skip over the source manager block. |
| 803 | if (Stream.SkipBlock()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 804 | Error("malformed block record in PCH file"); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 805 | return Failure; |
| 806 | } |
| 807 | |
| 808 | // Enter the source manager block. |
| 809 | if (SLocEntryCursor.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 810 | Error("malformed source manager block record in PCH file"); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 811 | return Failure; |
| 812 | } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 813 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 814 | RecordData Record; |
| 815 | while (true) { |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 816 | unsigned Code = SLocEntryCursor.ReadCode(); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 817 | if (Code == llvm::bitc::END_BLOCK) { |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 818 | if (SLocEntryCursor.ReadBlockEnd()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 819 | Error("error at end of Source Manager block in PCH file"); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 820 | return Failure; |
| 821 | } |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 822 | return Success; |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 823 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 824 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 825 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 826 | // No known subblocks, always skip them. |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 827 | SLocEntryCursor.ReadSubBlockID(); |
| 828 | if (SLocEntryCursor.SkipBlock()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 829 | Error("malformed block record in PCH file"); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 830 | return Failure; |
| 831 | } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 832 | continue; |
| 833 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 835 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 836 | SLocEntryCursor.ReadAbbrevRecord(); |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 837 | continue; |
| 838 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 839 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 840 | // Read a record. |
| 841 | const char *BlobStart; |
| 842 | unsigned BlobLen; |
| 843 | Record.clear(); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 844 | switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 845 | default: // Default behavior: ignore. |
| 846 | break; |
| 847 | |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 848 | case pch::SM_LINE_TABLE: |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 849 | if (ParseLineTable(Record)) |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 850 | return Failure; |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 851 | break; |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 852 | |
| 853 | case pch::SM_HEADER_FILE_INFO: { |
| 854 | HeaderFileInfo HFI; |
| 855 | HFI.isImport = Record[0]; |
| 856 | HFI.DirInfo = Record[1]; |
| 857 | HFI.NumIncludes = Record[2]; |
| 858 | HFI.ControllingMacroID = Record[3]; |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 859 | if (Listener) |
| 860 | Listener->ReadHeaderFileInfo(HFI); |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 861 | break; |
| 862 | } |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 863 | |
| 864 | case pch::SM_SLOC_FILE_ENTRY: |
| 865 | case pch::SM_SLOC_BUFFER_ENTRY: |
| 866 | case pch::SM_SLOC_INSTANTIATION_ENTRY: |
| 867 | // Once we hit one of the source location entries, we're done. |
| 868 | return Success; |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | } |
| 872 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 873 | /// \brief Read in the source location entry with the given ID. |
| 874 | PCHReader::PCHReadResult PCHReader::ReadSLocEntryRecord(unsigned ID) { |
| 875 | if (ID == 0) |
| 876 | return Success; |
| 877 | |
| 878 | if (ID > TotalNumSLocEntries) { |
| 879 | Error("source location entry ID out-of-range for PCH file"); |
| 880 | return Failure; |
| 881 | } |
| 882 | |
| 883 | ++NumSLocEntriesRead; |
| 884 | SLocEntryCursor.JumpToBit(SLocOffsets[ID - 1]); |
| 885 | unsigned Code = SLocEntryCursor.ReadCode(); |
| 886 | if (Code == llvm::bitc::END_BLOCK || |
| 887 | Code == llvm::bitc::ENTER_SUBBLOCK || |
| 888 | Code == llvm::bitc::DEFINE_ABBREV) { |
| 889 | Error("incorrectly-formatted source location entry in PCH file"); |
| 890 | return Failure; |
| 891 | } |
| 892 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 893 | RecordData Record; |
| 894 | const char *BlobStart; |
| 895 | unsigned BlobLen; |
| 896 | switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { |
| 897 | default: |
| 898 | Error("incorrectly-formatted source location entry in PCH file"); |
| 899 | return Failure; |
| 900 | |
| 901 | case pch::SM_SLOC_FILE_ENTRY: { |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 902 | std::string Filename(BlobStart, BlobStart + BlobLen); |
| 903 | MaybeAddSystemRootToFilename(Filename); |
| 904 | const FileEntry *File = FileMgr.getFile(Filename); |
Chris Lattner | d3555ae | 2009-06-15 04:35:16 +0000 | [diff] [blame] | 905 | if (File == 0) { |
| 906 | std::string ErrorStr = "could not find file '"; |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 907 | ErrorStr += Filename; |
Chris Lattner | d3555ae | 2009-06-15 04:35:16 +0000 | [diff] [blame] | 908 | ErrorStr += "' referenced by PCH file"; |
| 909 | Error(ErrorStr.c_str()); |
| 910 | return Failure; |
| 911 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 912 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 913 | FileID FID = SourceMgr.createFileID(File, |
| 914 | SourceLocation::getFromRawEncoding(Record[1]), |
| 915 | (SrcMgr::CharacteristicKind)Record[2], |
| 916 | ID, Record[0]); |
| 917 | if (Record[3]) |
| 918 | const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()) |
| 919 | .setHasLineDirectives(); |
| 920 | |
| 921 | break; |
| 922 | } |
| 923 | |
| 924 | case pch::SM_SLOC_BUFFER_ENTRY: { |
| 925 | const char *Name = BlobStart; |
| 926 | unsigned Offset = Record[0]; |
| 927 | unsigned Code = SLocEntryCursor.ReadCode(); |
| 928 | Record.clear(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 929 | unsigned RecCode |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 930 | = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen); |
| 931 | assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file"); |
| 932 | (void)RecCode; |
| 933 | llvm::MemoryBuffer *Buffer |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 934 | = llvm::MemoryBuffer::getMemBuffer(BlobStart, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 935 | BlobStart + BlobLen - 1, |
| 936 | Name); |
| 937 | FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 938 | |
Douglas Gregor | 92b059e | 2009-04-28 20:33:11 +0000 | [diff] [blame] | 939 | if (strcmp(Name, "<built-in>") == 0) { |
| 940 | PCHPredefinesBufferID = BufferID; |
| 941 | PCHPredefines = BlobStart; |
| 942 | PCHPredefinesLen = BlobLen - 1; |
| 943 | } |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 944 | |
| 945 | break; |
| 946 | } |
| 947 | |
| 948 | case pch::SM_SLOC_INSTANTIATION_ENTRY: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 949 | SourceLocation SpellingLoc |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 950 | = SourceLocation::getFromRawEncoding(Record[1]); |
| 951 | SourceMgr.createInstantiationLoc(SpellingLoc, |
| 952 | SourceLocation::getFromRawEncoding(Record[2]), |
| 953 | SourceLocation::getFromRawEncoding(Record[3]), |
| 954 | Record[4], |
| 955 | ID, |
| 956 | Record[0]); |
| 957 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 958 | } |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | return Success; |
| 962 | } |
| 963 | |
Chris Lattner | 6367f6d | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 964 | /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the |
| 965 | /// specified cursor. Read the abbreviations that are at the top of the block |
| 966 | /// and then leave the cursor pointing into the block. |
| 967 | bool PCHReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, |
| 968 | unsigned BlockID) { |
| 969 | if (Cursor.EnterSubBlock(BlockID)) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 970 | Error("malformed block record in PCH file"); |
Chris Lattner | 6367f6d | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 971 | return Failure; |
| 972 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 973 | |
Chris Lattner | 6367f6d | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 974 | while (true) { |
| 975 | unsigned Code = Cursor.ReadCode(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | |
Chris Lattner | 6367f6d | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 977 | // We expect all abbrevs to be at the start of the block. |
| 978 | if (Code != llvm::bitc::DEFINE_ABBREV) |
| 979 | return false; |
| 980 | Cursor.ReadAbbrevRecord(); |
| 981 | } |
| 982 | } |
| 983 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 984 | void PCHReader::ReadMacroRecord(uint64_t Offset) { |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 985 | assert(PP && "Forgot to set Preprocessor ?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 986 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 987 | // Keep track of where we are in the stream, then jump back there |
| 988 | // after reading this macro. |
| 989 | SavedStreamPosition SavedPosition(Stream); |
| 990 | |
| 991 | Stream.JumpToBit(Offset); |
| 992 | RecordData Record; |
| 993 | llvm::SmallVector<IdentifierInfo*, 16> MacroArgs; |
| 994 | MacroInfo *Macro = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 995 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 996 | while (true) { |
| 997 | unsigned Code = Stream.ReadCode(); |
| 998 | switch (Code) { |
| 999 | case llvm::bitc::END_BLOCK: |
| 1000 | return; |
| 1001 | |
| 1002 | case llvm::bitc::ENTER_SUBBLOCK: |
| 1003 | // No known subblocks, always skip them. |
| 1004 | Stream.ReadSubBlockID(); |
| 1005 | if (Stream.SkipBlock()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1006 | Error("malformed block record in PCH file"); |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1007 | return; |
| 1008 | } |
| 1009 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1010 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1011 | case llvm::bitc::DEFINE_ABBREV: |
| 1012 | Stream.ReadAbbrevRecord(); |
| 1013 | continue; |
| 1014 | default: break; |
| 1015 | } |
| 1016 | |
| 1017 | // Read a record. |
| 1018 | Record.clear(); |
| 1019 | pch::PreprocessorRecordTypes RecType = |
| 1020 | (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record); |
| 1021 | switch (RecType) { |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1022 | case pch::PP_MACRO_OBJECT_LIKE: |
| 1023 | case pch::PP_MACRO_FUNCTION_LIKE: { |
| 1024 | // If we already have a macro, that means that we've hit the end |
| 1025 | // of the definition of the macro we were looking for. We're |
| 1026 | // done. |
| 1027 | if (Macro) |
| 1028 | return; |
| 1029 | |
| 1030 | IdentifierInfo *II = DecodeIdentifierInfo(Record[0]); |
| 1031 | if (II == 0) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1032 | Error("macro must have a name in PCH file"); |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1033 | return; |
| 1034 | } |
| 1035 | SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]); |
| 1036 | bool isUsed = Record[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1037 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1038 | MacroInfo *MI = PP->AllocateMacroInfo(Loc); |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1039 | MI->setIsUsed(isUsed); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1040 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1041 | if (RecType == pch::PP_MACRO_FUNCTION_LIKE) { |
| 1042 | // Decode function-like macro info. |
| 1043 | bool isC99VarArgs = Record[3]; |
| 1044 | bool isGNUVarArgs = Record[4]; |
| 1045 | MacroArgs.clear(); |
| 1046 | unsigned NumArgs = Record[5]; |
| 1047 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1048 | MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i])); |
| 1049 | |
| 1050 | // Install function-like macro info. |
| 1051 | MI->setIsFunctionLike(); |
| 1052 | if (isC99VarArgs) MI->setIsC99Varargs(); |
| 1053 | if (isGNUVarArgs) MI->setIsGNUVarargs(); |
Douglas Gregor | 75fdb23 | 2009-05-22 22:45:36 +0000 | [diff] [blame] | 1054 | MI->setArgumentList(MacroArgs.data(), MacroArgs.size(), |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1055 | PP->getPreprocessorAllocator()); |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | // Finally, install the macro. |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1059 | PP->setMacroInfo(II, MI); |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1060 | |
| 1061 | // Remember that we saw this macro last so that we add the tokens that |
| 1062 | // form its body to it. |
| 1063 | Macro = MI; |
| 1064 | ++NumMacrosRead; |
| 1065 | break; |
| 1066 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1068 | case pch::PP_TOKEN: { |
| 1069 | // If we see a TOKEN before a PP_MACRO_*, then the file is |
| 1070 | // erroneous, just pretend we didn't see this. |
| 1071 | if (Macro == 0) break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1072 | |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1073 | Token Tok; |
| 1074 | Tok.startToken(); |
| 1075 | Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0])); |
| 1076 | Tok.setLength(Record[1]); |
| 1077 | if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2])) |
| 1078 | Tok.setIdentifierInfo(II); |
| 1079 | Tok.setKind((tok::TokenKind)Record[3]); |
| 1080 | Tok.setFlag((Token::TokenFlags)Record[4]); |
| 1081 | Macro->AddTokenToBody(Tok); |
| 1082 | break; |
| 1083 | } |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 1084 | } |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1085 | } |
| 1086 | } |
| 1087 | |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1088 | void PCHReader::ReadDefinedMacros() { |
| 1089 | // If there was no preprocessor block, do nothing. |
| 1090 | if (!MacroCursor.getBitStreamReader()) |
| 1091 | return; |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame^] | 1092 | |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1093 | llvm::BitstreamCursor Cursor = MacroCursor; |
| 1094 | if (Cursor.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID)) { |
| 1095 | Error("malformed preprocessor block record in PCH file"); |
| 1096 | return; |
| 1097 | } |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame^] | 1098 | |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1099 | RecordData Record; |
| 1100 | while (true) { |
| 1101 | unsigned Code = Cursor.ReadCode(); |
| 1102 | if (Code == llvm::bitc::END_BLOCK) { |
| 1103 | if (Cursor.ReadBlockEnd()) |
| 1104 | Error("error at end of preprocessor block in PCH file"); |
| 1105 | return; |
| 1106 | } |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame^] | 1107 | |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1108 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 1109 | // No known subblocks, always skip them. |
| 1110 | Cursor.ReadSubBlockID(); |
| 1111 | if (Cursor.SkipBlock()) { |
| 1112 | Error("malformed block record in PCH file"); |
| 1113 | return; |
| 1114 | } |
| 1115 | continue; |
| 1116 | } |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame^] | 1117 | |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1118 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
| 1119 | Cursor.ReadAbbrevRecord(); |
| 1120 | continue; |
| 1121 | } |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame^] | 1122 | |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1123 | // Read a record. |
| 1124 | const char *BlobStart; |
| 1125 | unsigned BlobLen; |
| 1126 | Record.clear(); |
| 1127 | switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { |
| 1128 | default: // Default behavior: ignore. |
| 1129 | break; |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame^] | 1130 | |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1131 | case pch::PP_MACRO_OBJECT_LIKE: |
| 1132 | case pch::PP_MACRO_FUNCTION_LIKE: |
| 1133 | DecodeIdentifierInfo(Record[0]); |
| 1134 | break; |
| 1135 | |
| 1136 | case pch::PP_TOKEN: |
| 1137 | // Ignore tokens. |
| 1138 | break; |
| 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1143 | /// \brief If we are loading a relocatable PCH file, and the filename is |
| 1144 | /// not an absolute path, add the system root to the beginning of the file |
| 1145 | /// name. |
| 1146 | void PCHReader::MaybeAddSystemRootToFilename(std::string &Filename) { |
| 1147 | // If this is not a relocatable PCH file, there's nothing to do. |
| 1148 | if (!RelocatablePCH) |
| 1149 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1150 | |
Daniel Dunbar | d5b2197 | 2009-11-18 19:50:41 +0000 | [diff] [blame] | 1151 | if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute()) |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1152 | return; |
| 1153 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1154 | if (isysroot == 0) { |
| 1155 | // If no system root was given, default to '/' |
| 1156 | Filename.insert(Filename.begin(), '/'); |
| 1157 | return; |
| 1158 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1160 | unsigned Length = strlen(isysroot); |
| 1161 | if (isysroot[Length - 1] != '/') |
| 1162 | Filename.insert(Filename.begin(), '/'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1163 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1164 | Filename.insert(Filename.begin(), isysroot, isysroot + Length); |
| 1165 | } |
| 1166 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1167 | PCHReader::PCHReadResult |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 1168 | PCHReader::ReadPCHBlock() { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1169 | if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1170 | Error("malformed block record in PCH file"); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1171 | return Failure; |
| 1172 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1173 | |
| 1174 | // Read all of the records and blocks for the PCH file. |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1175 | RecordData Record; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1176 | while (!Stream.AtEndOfStream()) { |
| 1177 | unsigned Code = Stream.ReadCode(); |
| 1178 | if (Code == llvm::bitc::END_BLOCK) { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1179 | if (Stream.ReadBlockEnd()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1180 | Error("error at end of module block in PCH file"); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1181 | return Failure; |
| 1182 | } |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1183 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1184 | return Success; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 1188 | switch (Stream.ReadSubBlockID()) { |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 1189 | case pch::DECLTYPES_BLOCK_ID: |
Chris Lattner | 6367f6d | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 1190 | // We lazily load the decls block, but we want to set up the |
| 1191 | // DeclsCursor cursor to point into it. Clone our current bitcode |
| 1192 | // cursor to it, enter the block and read the abbrevs in that block. |
| 1193 | // With the main cursor, we just skip over it. |
| 1194 | DeclsCursor = Stream; |
| 1195 | if (Stream.SkipBlock() || // Skip with the main cursor. |
| 1196 | // Read the abbrevs. |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 1197 | ReadBlockAbbrevs(DeclsCursor, pch::DECLTYPES_BLOCK_ID)) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1198 | Error("malformed block record in PCH file"); |
Chris Lattner | 6367f6d | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 1199 | return Failure; |
| 1200 | } |
| 1201 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1202 | |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1203 | case pch::PREPROCESSOR_BLOCK_ID: |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1204 | MacroCursor = Stream; |
| 1205 | if (PP) |
| 1206 | PP->setExternalSource(this); |
| 1207 | |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1208 | if (Stream.SkipBlock()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1209 | Error("malformed block record in PCH file"); |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1210 | return Failure; |
| 1211 | } |
| 1212 | break; |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 1213 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1214 | case pch::SOURCE_MANAGER_BLOCK_ID: |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1215 | switch (ReadSourceManagerBlock()) { |
| 1216 | case Success: |
| 1217 | break; |
| 1218 | |
| 1219 | case Failure: |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1220 | Error("malformed source manager block in PCH file"); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1221 | return Failure; |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1222 | |
| 1223 | case IgnorePCH: |
| 1224 | return IgnorePCH; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1225 | } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1226 | break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1227 | } |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1228 | continue; |
| 1229 | } |
| 1230 | |
| 1231 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
| 1232 | Stream.ReadAbbrevRecord(); |
| 1233 | continue; |
| 1234 | } |
| 1235 | |
| 1236 | // Read and process a record. |
| 1237 | Record.clear(); |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1238 | const char *BlobStart = 0; |
| 1239 | unsigned BlobLen = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1240 | switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record, |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1241 | &BlobStart, &BlobLen)) { |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1242 | default: // Default behavior: ignore. |
| 1243 | break; |
| 1244 | |
| 1245 | case pch::TYPE_OFFSET: |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1246 | if (!TypesLoaded.empty()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1247 | Error("duplicate TYPE_OFFSET record in PCH file"); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1248 | return Failure; |
| 1249 | } |
Chris Lattner | c732f5a | 2009-04-27 18:24:17 +0000 | [diff] [blame] | 1250 | TypeOffsets = (const uint32_t *)BlobStart; |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1251 | TypesLoaded.resize(Record[0]); |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1252 | break; |
| 1253 | |
| 1254 | case pch::DECL_OFFSET: |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1255 | if (!DeclsLoaded.empty()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1256 | Error("duplicate DECL_OFFSET record in PCH file"); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1257 | return Failure; |
| 1258 | } |
Chris Lattner | c732f5a | 2009-04-27 18:24:17 +0000 | [diff] [blame] | 1259 | DeclOffsets = (const uint32_t *)BlobStart; |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 1260 | DeclsLoaded.resize(Record[0]); |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1261 | break; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1262 | |
| 1263 | case pch::LANGUAGE_OPTIONS: |
| 1264 | if (ParseLanguageOptions(Record)) |
| 1265 | return IgnorePCH; |
| 1266 | break; |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1267 | |
Douglas Gregor | ab41e63 | 2009-04-27 22:23:34 +0000 | [diff] [blame] | 1268 | case pch::METADATA: { |
| 1269 | if (Record[0] != pch::VERSION_MAJOR) { |
| 1270 | Diag(Record[0] < pch::VERSION_MAJOR? diag::warn_pch_version_too_old |
| 1271 | : diag::warn_pch_version_too_new); |
| 1272 | return IgnorePCH; |
| 1273 | } |
| 1274 | |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1275 | RelocatablePCH = Record[4]; |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1276 | if (Listener) { |
| 1277 | std::string TargetTriple(BlobStart, BlobLen); |
| 1278 | if (Listener->ReadTargetTriple(TargetTriple)) |
| 1279 | return IgnorePCH; |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1280 | } |
| 1281 | break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1282 | } |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1283 | |
| 1284 | case pch::IDENTIFIER_TABLE: |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1285 | IdentifierTableData = BlobStart; |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1286 | if (Record[0]) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1287 | IdentifierLookupTable |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1288 | = PCHIdentifierLookupTable::Create( |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1289 | (const unsigned char *)IdentifierTableData + Record[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1290 | (const unsigned char *)IdentifierTableData, |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1291 | PCHIdentifierLookupTrait(*this)); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1292 | if (PP) |
| 1293 | PP->getIdentifierTable().setExternalIdentifierLookup(this); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1294 | } |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1295 | break; |
| 1296 | |
| 1297 | case pch::IDENTIFIER_OFFSET: |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1298 | if (!IdentifiersLoaded.empty()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1299 | Error("duplicate IDENTIFIER_OFFSET record in PCH file"); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1300 | return Failure; |
| 1301 | } |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1302 | IdentifierOffsets = (const uint32_t *)BlobStart; |
| 1303 | IdentifiersLoaded.resize(Record[0]); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1304 | if (PP) |
| 1305 | PP->getHeaderSearchInfo().SetExternalLookup(this); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1306 | break; |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1307 | |
| 1308 | case pch::EXTERNAL_DEFINITIONS: |
| 1309 | if (!ExternalDefinitions.empty()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1310 | Error("duplicate EXTERNAL_DEFINITIONS record in PCH file"); |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1311 | return Failure; |
| 1312 | } |
| 1313 | ExternalDefinitions.swap(Record); |
| 1314 | break; |
Douglas Gregor | 3e1af84 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 1315 | |
Douglas Gregor | ad1de00 | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 1316 | case pch::SPECIAL_TYPES: |
| 1317 | SpecialTypes.swap(Record); |
| 1318 | break; |
| 1319 | |
Douglas Gregor | 3e1af84 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 1320 | case pch::STATISTICS: |
| 1321 | TotalNumStatements = Record[0]; |
Douglas Gregor | 37e2684 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1322 | TotalNumMacros = Record[1]; |
Douglas Gregor | 2512308 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 1323 | TotalLexicalDeclContexts = Record[2]; |
| 1324 | TotalVisibleDeclContexts = Record[3]; |
Douglas Gregor | 3e1af84 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 1325 | break; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1326 | |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 1327 | case pch::TENTATIVE_DEFINITIONS: |
| 1328 | if (!TentativeDefinitions.empty()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1329 | Error("duplicate TENTATIVE_DEFINITIONS record in PCH file"); |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 1330 | return Failure; |
| 1331 | } |
| 1332 | TentativeDefinitions.swap(Record); |
| 1333 | break; |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 1334 | |
Tanya Lattner | e6bbc01 | 2010-02-12 00:07:30 +0000 | [diff] [blame] | 1335 | case pch::UNUSED_STATIC_FUNCS: |
| 1336 | if (!UnusedStaticFuncs.empty()) { |
| 1337 | Error("duplicate UNUSED_STATIC_FUNCS record in PCH file"); |
| 1338 | return Failure; |
| 1339 | } |
| 1340 | UnusedStaticFuncs.swap(Record); |
| 1341 | break; |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame^] | 1342 | |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 1343 | case pch::LOCALLY_SCOPED_EXTERNAL_DECLS: |
| 1344 | if (!LocallyScopedExternalDecls.empty()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1345 | Error("duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file"); |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 1346 | return Failure; |
| 1347 | } |
| 1348 | LocallyScopedExternalDecls.swap(Record); |
| 1349 | break; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1350 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1351 | case pch::SELECTOR_OFFSETS: |
| 1352 | SelectorOffsets = (const uint32_t *)BlobStart; |
| 1353 | TotalNumSelectors = Record[0]; |
| 1354 | SelectorsLoaded.resize(TotalNumSelectors); |
| 1355 | break; |
| 1356 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1357 | case pch::METHOD_POOL: |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1358 | MethodPoolLookupTableData = (const unsigned char *)BlobStart; |
| 1359 | if (Record[0]) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1360 | MethodPoolLookupTable |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1361 | = PCHMethodPoolLookupTable::Create( |
| 1362 | MethodPoolLookupTableData + Record[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | MethodPoolLookupTableData, |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1364 | PCHMethodPoolLookupTrait(*this)); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1365 | TotalSelectorsInMethodPool = Record[1]; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1366 | break; |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 1367 | |
| 1368 | case pch::PP_COUNTER_VALUE: |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1369 | if (!Record.empty() && Listener) |
| 1370 | Listener->ReadCounter(Record[0]); |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 1371 | break; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1372 | |
| 1373 | case pch::SOURCE_LOCATION_OFFSETS: |
Chris Lattner | 090d9b5 | 2009-04-27 19:01:47 +0000 | [diff] [blame] | 1374 | SLocOffsets = (const uint32_t *)BlobStart; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1375 | TotalNumSLocEntries = Record[0]; |
Douglas Gregor | 445e23e | 2009-10-05 21:07:28 +0000 | [diff] [blame] | 1376 | SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, Record[1]); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1377 | break; |
| 1378 | |
| 1379 | case pch::SOURCE_LOCATION_PRELOADS: |
| 1380 | for (unsigned I = 0, N = Record.size(); I != N; ++I) { |
| 1381 | PCHReadResult Result = ReadSLocEntryRecord(Record[I]); |
| 1382 | if (Result != Success) |
| 1383 | return Result; |
| 1384 | } |
| 1385 | break; |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1386 | |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 1387 | case pch::STAT_CACHE: { |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame^] | 1388 | PCHStatCache *MyStatCache = |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 1389 | new PCHStatCache((const unsigned char *)BlobStart + Record[0], |
| 1390 | (const unsigned char *)BlobStart, |
| 1391 | NumStatHits, NumStatMisses); |
| 1392 | FileMgr.addStatCache(MyStatCache); |
| 1393 | StatCache = MyStatCache; |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1394 | break; |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 1395 | } |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame^] | 1396 | |
Douglas Gregor | b81c170 | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 1397 | case pch::EXT_VECTOR_DECLS: |
| 1398 | if (!ExtVectorDecls.empty()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1399 | Error("duplicate EXT_VECTOR_DECLS record in PCH file"); |
Douglas Gregor | b81c170 | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 1400 | return Failure; |
| 1401 | } |
| 1402 | ExtVectorDecls.swap(Record); |
| 1403 | break; |
| 1404 | |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1405 | case pch::ORIGINAL_FILE_NAME: |
Daniel Dunbar | 7b5a121 | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 1406 | ActualOriginalFileName.assign(BlobStart, BlobLen); |
| 1407 | OriginalFileName = ActualOriginalFileName; |
Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1408 | MaybeAddSystemRootToFilename(OriginalFileName); |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1409 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 1411 | case pch::COMMENT_RANGES: |
| 1412 | Comments = (SourceRange *)BlobStart; |
| 1413 | NumComments = BlobLen / sizeof(SourceRange); |
| 1414 | break; |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame^] | 1415 | |
Ted Kremenek | 5b4ec63 | 2010-01-22 20:59:36 +0000 | [diff] [blame] | 1416 | case pch::VERSION_CONTROL_BRANCH_REVISION: { |
Ted Kremenek | 974be4d | 2010-02-12 23:31:14 +0000 | [diff] [blame] | 1417 | const std::string &CurBranch = getClangFullRepositoryVersion(); |
Ted Kremenek | 517e676 | 2010-01-22 20:55:35 +0000 | [diff] [blame] | 1418 | llvm::StringRef PCHBranch(BlobStart, BlobLen); |
Ted Kremenek | 974be4d | 2010-02-12 23:31:14 +0000 | [diff] [blame] | 1419 | if (llvm::StringRef(CurBranch) != PCHBranch) { |
Douglas Gregor | 445e23e | 2009-10-05 21:07:28 +0000 | [diff] [blame] | 1420 | Diag(diag::warn_pch_different_branch) << PCHBranch << CurBranch; |
| 1421 | return IgnorePCH; |
| 1422 | } |
| 1423 | break; |
| 1424 | } |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1425 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1426 | } |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1427 | Error("premature end of bitstream in PCH file"); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1428 | return Failure; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1431 | PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1432 | // Set the PCH file name. |
| 1433 | this->FileName = FileName; |
| 1434 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1435 | // Open the PCH file. |
Daniel Dunbar | f3c740e | 2009-09-22 05:38:01 +0000 | [diff] [blame] | 1436 | // |
| 1437 | // FIXME: This shouldn't be here, we should just take a raw_ostream. |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1438 | std::string ErrStr; |
Daniel Dunbar | 731ad8f | 2009-11-10 00:46:19 +0000 | [diff] [blame] | 1439 | Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr)); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1440 | if (!Buffer) { |
| 1441 | Error(ErrStr.c_str()); |
| 1442 | return IgnorePCH; |
| 1443 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1444 | |
| 1445 | // Initialize the stream |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1446 | StreamFile.init((const unsigned char *)Buffer->getBufferStart(), |
Chris Lattner | b9fa917 | 2009-04-26 20:59:20 +0000 | [diff] [blame] | 1447 | (const unsigned char *)Buffer->getBufferEnd()); |
| 1448 | Stream.init(StreamFile); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1449 | |
| 1450 | // Sniff for the signature. |
| 1451 | if (Stream.Read(8) != 'C' || |
| 1452 | Stream.Read(8) != 'P' || |
| 1453 | Stream.Read(8) != 'C' || |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1454 | Stream.Read(8) != 'H') { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1455 | Diag(diag::err_not_a_pch_file) << FileName; |
| 1456 | return Failure; |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1457 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1458 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1459 | while (!Stream.AtEndOfStream()) { |
| 1460 | unsigned Code = Stream.ReadCode(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1462 | if (Code != llvm::bitc::ENTER_SUBBLOCK) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1463 | Error("invalid record at top-level of PCH file"); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1464 | return Failure; |
| 1465 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1466 | |
| 1467 | unsigned BlockID = Stream.ReadSubBlockID(); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1468 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1469 | // We only know the PCH subblock ID. |
| 1470 | switch (BlockID) { |
| 1471 | case llvm::bitc::BLOCKINFO_BLOCK_ID: |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1472 | if (Stream.ReadBlockInfoBlock()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1473 | Error("malformed BlockInfoBlock in PCH file"); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1474 | return Failure; |
| 1475 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1476 | break; |
| 1477 | case pch::PCH_BLOCK_ID: |
Douglas Gregor | 2eafc1b | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 1478 | switch (ReadPCHBlock()) { |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1479 | case Success: |
| 1480 | break; |
| 1481 | |
| 1482 | case Failure: |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1483 | return Failure; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1484 | |
| 1485 | case IgnorePCH: |
Douglas Gregor | 2bec041 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1486 | // FIXME: We could consider reading through to the end of this |
| 1487 | // PCH block, skipping subblocks, to see if there are other |
| 1488 | // PCH blocks elsewhere. |
Douglas Gregor | 2bf1eb0 | 2009-04-27 21:28:04 +0000 | [diff] [blame] | 1489 | |
| 1490 | // Clear out any preallocated source location entries, so that |
| 1491 | // the source manager does not try to resolve them later. |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1492 | SourceMgr.ClearPreallocatedSLocEntries(); |
Douglas Gregor | 2bf1eb0 | 2009-04-27 21:28:04 +0000 | [diff] [blame] | 1493 | |
| 1494 | // Remove the stat cache. |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 1495 | if (StatCache) |
| 1496 | FileMgr.removeStatCache((PCHStatCache*)StatCache); |
Douglas Gregor | 2bf1eb0 | 2009-04-27 21:28:04 +0000 | [diff] [blame] | 1497 | |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1498 | return IgnorePCH; |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1499 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1500 | break; |
| 1501 | default: |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1502 | if (Stream.SkipBlock()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1503 | Error("malformed block record in PCH file"); |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1504 | return Failure; |
| 1505 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1506 | break; |
| 1507 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
Douglas Gregor | 92b059e | 2009-04-28 20:33:11 +0000 | [diff] [blame] | 1510 | // Check the predefines buffer. |
Daniel Dunbar | dc3c0d2 | 2009-11-11 00:52:11 +0000 | [diff] [blame] | 1511 | if (CheckPredefinesBuffer(llvm::StringRef(PCHPredefines, PCHPredefinesLen), |
Douglas Gregor | 92b059e | 2009-04-28 20:33:11 +0000 | [diff] [blame] | 1512 | PCHPredefinesBufferID)) |
| 1513 | return IgnorePCH; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1514 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1515 | if (PP) { |
Zhongxing Xu | 0899621 | 2009-07-18 09:26:51 +0000 | [diff] [blame] | 1516 | // Initialization of keywords and pragmas occurs before the |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1517 | // PCH file is read, so there may be some identifiers that were |
| 1518 | // loaded into the IdentifierTable before we intercepted the |
| 1519 | // creation of identifiers. Iterate through the list of known |
| 1520 | // identifiers and determine whether we have to establish |
| 1521 | // preprocessor definitions or top-level identifier declaration |
| 1522 | // chains for those identifiers. |
| 1523 | // |
| 1524 | // We copy the IdentifierInfo pointers to a small vector first, |
| 1525 | // since de-serializing declarations or macro definitions can add |
| 1526 | // new entries into the identifier table, invalidating the |
| 1527 | // iterators. |
| 1528 | llvm::SmallVector<IdentifierInfo *, 128> Identifiers; |
| 1529 | for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(), |
| 1530 | IdEnd = PP->getIdentifierTable().end(); |
| 1531 | Id != IdEnd; ++Id) |
| 1532 | Identifiers.push_back(Id->second); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1533 | PCHIdentifierLookupTable *IdTable |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1534 | = (PCHIdentifierLookupTable *)IdentifierLookupTable; |
| 1535 | for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) { |
| 1536 | IdentifierInfo *II = Identifiers[I]; |
| 1537 | // Look in the on-disk hash table for an entry for |
| 1538 | PCHIdentifierLookupTrait Info(*this, II); |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 1539 | std::pair<const char*, unsigned> Key(II->getNameStart(), II->getLength()); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1540 | PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info); |
| 1541 | if (Pos == IdTable->end()) |
| 1542 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1543 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1544 | // Dereferencing the iterator has the effect of populating the |
| 1545 | // IdentifierInfo node with the various declarations it needs. |
| 1546 | (void)*Pos; |
| 1547 | } |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1550 | if (Context) |
| 1551 | InitializeContext(*Context); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1552 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1553 | return Success; |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1556 | void PCHReader::InitializeContext(ASTContext &Ctx) { |
| 1557 | Context = &Ctx; |
| 1558 | assert(Context && "Passed null context!"); |
| 1559 | |
| 1560 | assert(PP && "Forgot to set Preprocessor ?"); |
| 1561 | PP->getIdentifierTable().setExternalIdentifierLookup(this); |
| 1562 | PP->getHeaderSearchInfo().SetExternalLookup(this); |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1563 | PP->setExternalSource(this); |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame^] | 1564 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1565 | // Load the translation unit declaration |
| 1566 | ReadDeclRecord(DeclOffsets[0], 0); |
| 1567 | |
| 1568 | // Load the special types. |
| 1569 | Context->setBuiltinVaListType( |
| 1570 | GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST])); |
| 1571 | if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID]) |
| 1572 | Context->setObjCIdType(GetType(Id)); |
| 1573 | if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR]) |
| 1574 | Context->setObjCSelType(GetType(Sel)); |
| 1575 | if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL]) |
| 1576 | Context->setObjCProtoType(GetType(Proto)); |
| 1577 | if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS]) |
| 1578 | Context->setObjCClassType(GetType(Class)); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1579 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1580 | if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING]) |
| 1581 | Context->setCFConstantStringType(GetType(String)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1582 | if (unsigned FastEnum |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1583 | = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE]) |
| 1584 | Context->setObjCFastEnumerationStateType(GetType(FastEnum)); |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 1585 | if (unsigned File = SpecialTypes[pch::SPECIAL_TYPE_FILE]) { |
| 1586 | QualType FileType = GetType(File); |
| 1587 | assert(!FileType.isNull() && "FILE type is NULL"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1588 | if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 1589 | Context->setFILEDecl(Typedef->getDecl()); |
| 1590 | else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1591 | const TagType *Tag = FileType->getAs<TagType>(); |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 1592 | assert(Tag && "Invalid FILE type in PCH file"); |
| 1593 | Context->setFILEDecl(Tag->getDecl()); |
| 1594 | } |
| 1595 | } |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1596 | if (unsigned Jmp_buf = SpecialTypes[pch::SPECIAL_TYPE_jmp_buf]) { |
| 1597 | QualType Jmp_bufType = GetType(Jmp_buf); |
| 1598 | assert(!Jmp_bufType.isNull() && "jmp_bug type is NULL"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1599 | if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1600 | Context->setjmp_bufDecl(Typedef->getDecl()); |
| 1601 | else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1602 | const TagType *Tag = Jmp_bufType->getAs<TagType>(); |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1603 | assert(Tag && "Invalid jmp_bug type in PCH file"); |
| 1604 | Context->setjmp_bufDecl(Tag->getDecl()); |
| 1605 | } |
| 1606 | } |
| 1607 | if (unsigned Sigjmp_buf = SpecialTypes[pch::SPECIAL_TYPE_sigjmp_buf]) { |
| 1608 | QualType Sigjmp_bufType = GetType(Sigjmp_buf); |
| 1609 | assert(!Sigjmp_bufType.isNull() && "sigjmp_buf type is NULL"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1610 | if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1611 | Context->setsigjmp_bufDecl(Typedef->getDecl()); |
| 1612 | else { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1613 | const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1614 | assert(Tag && "Invalid sigjmp_buf type in PCH file"); |
| 1615 | Context->setsigjmp_bufDecl(Tag->getDecl()); |
| 1616 | } |
| 1617 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1618 | if (unsigned ObjCIdRedef |
Douglas Gregor | d1571ac | 2009-08-21 00:27:50 +0000 | [diff] [blame] | 1619 | = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID_REDEFINITION]) |
| 1620 | Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1621 | if (unsigned ObjCClassRedef |
Douglas Gregor | d1571ac | 2009-08-21 00:27:50 +0000 | [diff] [blame] | 1622 | = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) |
| 1623 | Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1624 | #if 0 |
| 1625 | // FIXME. Accommodate for this in several PCH/Index tests |
| 1626 | if (unsigned ObjCSelRedef |
| 1627 | = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) |
Fariborz Jahanian | 369a3bd | 2009-11-25 23:07:42 +0000 | [diff] [blame] | 1628 | Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1629 | #endif |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 1630 | if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_BLOCK_DESCRIPTOR]) |
| 1631 | Context->setBlockDescriptorType(GetType(String)); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 1632 | if (unsigned String |
| 1633 | = SpecialTypes[pch::SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR]) |
| 1634 | Context->setBlockDescriptorExtendedType(GetType(String)); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1637 | /// \brief Retrieve the name of the original source file name |
| 1638 | /// directly from the PCH file, without actually loading the PCH |
| 1639 | /// file. |
Daniel Dunbar | 93ebb1b | 2009-12-03 09:13:06 +0000 | [diff] [blame] | 1640 | std::string PCHReader::getOriginalSourceFile(const std::string &PCHFileName, |
| 1641 | Diagnostic &Diags) { |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1642 | // Open the PCH file. |
| 1643 | std::string ErrStr; |
| 1644 | llvm::OwningPtr<llvm::MemoryBuffer> Buffer; |
| 1645 | Buffer.reset(llvm::MemoryBuffer::getFile(PCHFileName.c_str(), &ErrStr)); |
| 1646 | if (!Buffer) { |
Daniel Dunbar | 93ebb1b | 2009-12-03 09:13:06 +0000 | [diff] [blame] | 1647 | Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr; |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1648 | return std::string(); |
| 1649 | } |
| 1650 | |
| 1651 | // Initialize the stream |
| 1652 | llvm::BitstreamReader StreamFile; |
| 1653 | llvm::BitstreamCursor Stream; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1654 | StreamFile.init((const unsigned char *)Buffer->getBufferStart(), |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1655 | (const unsigned char *)Buffer->getBufferEnd()); |
| 1656 | Stream.init(StreamFile); |
| 1657 | |
| 1658 | // Sniff for the signature. |
| 1659 | if (Stream.Read(8) != 'C' || |
| 1660 | Stream.Read(8) != 'P' || |
| 1661 | Stream.Read(8) != 'C' || |
| 1662 | Stream.Read(8) != 'H') { |
Daniel Dunbar | 93ebb1b | 2009-12-03 09:13:06 +0000 | [diff] [blame] | 1663 | Diags.Report(diag::err_fe_not_a_pch_file) << PCHFileName; |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1664 | return std::string(); |
| 1665 | } |
| 1666 | |
| 1667 | RecordData Record; |
| 1668 | while (!Stream.AtEndOfStream()) { |
| 1669 | unsigned Code = Stream.ReadCode(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1670 | |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1671 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 1672 | unsigned BlockID = Stream.ReadSubBlockID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1673 | |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1674 | // We only know the PCH subblock ID. |
| 1675 | switch (BlockID) { |
| 1676 | case pch::PCH_BLOCK_ID: |
| 1677 | if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) { |
Daniel Dunbar | 93ebb1b | 2009-12-03 09:13:06 +0000 | [diff] [blame] | 1678 | Diags.Report(diag::err_fe_pch_malformed_block) << PCHFileName; |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1679 | return std::string(); |
| 1680 | } |
| 1681 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1682 | |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1683 | default: |
| 1684 | if (Stream.SkipBlock()) { |
Daniel Dunbar | 93ebb1b | 2009-12-03 09:13:06 +0000 | [diff] [blame] | 1685 | Diags.Report(diag::err_fe_pch_malformed_block) << PCHFileName; |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1686 | return std::string(); |
| 1687 | } |
| 1688 | break; |
| 1689 | } |
| 1690 | continue; |
| 1691 | } |
| 1692 | |
| 1693 | if (Code == llvm::bitc::END_BLOCK) { |
| 1694 | if (Stream.ReadBlockEnd()) { |
Daniel Dunbar | 93ebb1b | 2009-12-03 09:13:06 +0000 | [diff] [blame] | 1695 | Diags.Report(diag::err_fe_pch_error_at_end_block) << PCHFileName; |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1696 | return std::string(); |
| 1697 | } |
| 1698 | continue; |
| 1699 | } |
| 1700 | |
| 1701 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
| 1702 | Stream.ReadAbbrevRecord(); |
| 1703 | continue; |
| 1704 | } |
| 1705 | |
| 1706 | Record.clear(); |
| 1707 | const char *BlobStart = 0; |
| 1708 | unsigned BlobLen = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1709 | if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1710 | == pch::ORIGINAL_FILE_NAME) |
| 1711 | return std::string(BlobStart, BlobLen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1712 | } |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1713 | |
| 1714 | return std::string(); |
| 1715 | } |
| 1716 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1717 | /// \brief Parse the record that corresponds to a LangOptions data |
| 1718 | /// structure. |
| 1719 | /// |
| 1720 | /// This routine compares the language options used to generate the |
| 1721 | /// PCH file against the language options set for the current |
| 1722 | /// compilation. For each option, we classify differences between the |
| 1723 | /// two compiler states as either "benign" or "important". Benign |
| 1724 | /// differences don't matter, and we accept them without complaint |
| 1725 | /// (and without modifying the language options). Differences between |
| 1726 | /// the states for important options cause the PCH file to be |
| 1727 | /// unusable, so we emit a warning and return true to indicate that |
| 1728 | /// there was an error. |
| 1729 | /// |
| 1730 | /// \returns true if the PCH file is unacceptable, false otherwise. |
| 1731 | bool PCHReader::ParseLanguageOptions( |
| 1732 | const llvm::SmallVectorImpl<uint64_t> &Record) { |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1733 | if (Listener) { |
| 1734 | LangOptions LangOpts; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1736 | #define PARSE_LANGOPT(Option) \ |
| 1737 | LangOpts.Option = Record[Idx]; \ |
| 1738 | ++Idx |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1739 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1740 | unsigned Idx = 0; |
| 1741 | PARSE_LANGOPT(Trigraphs); |
| 1742 | PARSE_LANGOPT(BCPLComment); |
| 1743 | PARSE_LANGOPT(DollarIdents); |
| 1744 | PARSE_LANGOPT(AsmPreprocessor); |
| 1745 | PARSE_LANGOPT(GNUMode); |
| 1746 | PARSE_LANGOPT(ImplicitInt); |
| 1747 | PARSE_LANGOPT(Digraphs); |
| 1748 | PARSE_LANGOPT(HexFloats); |
| 1749 | PARSE_LANGOPT(C99); |
| 1750 | PARSE_LANGOPT(Microsoft); |
| 1751 | PARSE_LANGOPT(CPlusPlus); |
| 1752 | PARSE_LANGOPT(CPlusPlus0x); |
| 1753 | PARSE_LANGOPT(CXXOperatorNames); |
| 1754 | PARSE_LANGOPT(ObjC1); |
| 1755 | PARSE_LANGOPT(ObjC2); |
| 1756 | PARSE_LANGOPT(ObjCNonFragileABI); |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1757 | PARSE_LANGOPT(ObjCNonFragileABI2); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1758 | PARSE_LANGOPT(PascalStrings); |
| 1759 | PARSE_LANGOPT(WritableStrings); |
| 1760 | PARSE_LANGOPT(LaxVectorConversions); |
Nate Begeman | b9e7e63 | 2009-06-25 23:01:11 +0000 | [diff] [blame] | 1761 | PARSE_LANGOPT(AltiVec); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1762 | PARSE_LANGOPT(Exceptions); |
Daniel Dunbar | 7348288 | 2010-02-10 18:48:44 +0000 | [diff] [blame] | 1763 | PARSE_LANGOPT(SjLjExceptions); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1764 | PARSE_LANGOPT(NeXTRuntime); |
| 1765 | PARSE_LANGOPT(Freestanding); |
| 1766 | PARSE_LANGOPT(NoBuiltin); |
| 1767 | PARSE_LANGOPT(ThreadsafeStatics); |
Douglas Gregor | 972d954 | 2009-09-03 14:36:33 +0000 | [diff] [blame] | 1768 | PARSE_LANGOPT(POSIXThreads); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1769 | PARSE_LANGOPT(Blocks); |
| 1770 | PARSE_LANGOPT(EmitAllDecls); |
| 1771 | PARSE_LANGOPT(MathErrno); |
| 1772 | PARSE_LANGOPT(OverflowChecking); |
| 1773 | PARSE_LANGOPT(HeinousExtensions); |
| 1774 | PARSE_LANGOPT(Optimize); |
| 1775 | PARSE_LANGOPT(OptimizeSize); |
| 1776 | PARSE_LANGOPT(Static); |
| 1777 | PARSE_LANGOPT(PICLevel); |
| 1778 | PARSE_LANGOPT(GNUInline); |
| 1779 | PARSE_LANGOPT(NoInline); |
| 1780 | PARSE_LANGOPT(AccessControl); |
| 1781 | PARSE_LANGOPT(CharIsSigned); |
John Thompson | a6fda12 | 2009-11-05 20:14:16 +0000 | [diff] [blame] | 1782 | PARSE_LANGOPT(ShortWChar); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1783 | LangOpts.setGCMode((LangOptions::GCMode)Record[Idx]); |
| 1784 | ++Idx; |
| 1785 | LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx]); |
| 1786 | ++Idx; |
Daniel Dunbar | ab8e281 | 2009-09-21 04:16:19 +0000 | [diff] [blame] | 1787 | LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode) |
| 1788 | Record[Idx]); |
| 1789 | ++Idx; |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1790 | PARSE_LANGOPT(InstantiationDepth); |
Nate Begeman | b9e7e63 | 2009-06-25 23:01:11 +0000 | [diff] [blame] | 1791 | PARSE_LANGOPT(OpenCL); |
Mike Stump | 9c276ae | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 1792 | PARSE_LANGOPT(CatchUndefined); |
| 1793 | // FIXME: Missing ElideConstructors?! |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1794 | #undef PARSE_LANGOPT |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1795 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1796 | return Listener->ReadLanguageOptions(LangOpts); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1797 | } |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1798 | |
| 1799 | return false; |
| 1800 | } |
| 1801 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 1802 | void PCHReader::ReadComments(std::vector<SourceRange> &Comments) { |
| 1803 | Comments.resize(NumComments); |
| 1804 | std::copy(this->Comments, this->Comments + NumComments, |
| 1805 | Comments.begin()); |
| 1806 | } |
| 1807 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1808 | /// \brief Read and return the type at the given offset. |
| 1809 | /// |
| 1810 | /// This routine actually reads the record corresponding to the type |
| 1811 | /// at the given offset in the bitstream. It is a helper routine for |
| 1812 | /// GetType, which deals with reading type IDs. |
| 1813 | QualType PCHReader::ReadTypeRecord(uint64_t Offset) { |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1814 | // Keep track of where we are in the stream, then jump back there |
| 1815 | // after reading this type. |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 1816 | SavedStreamPosition SavedPosition(DeclsCursor); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1817 | |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 1818 | // Note that we are loading a type record. |
| 1819 | LoadingTypeOrDecl Loading(*this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1820 | |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 1821 | DeclsCursor.JumpToBit(Offset); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1822 | RecordData Record; |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 1823 | unsigned Code = DeclsCursor.ReadCode(); |
| 1824 | switch ((pch::TypeCode)DeclsCursor.ReadRecord(Code, Record)) { |
Douglas Gregor | 6d47396 | 2009-04-15 22:00:08 +0000 | [diff] [blame] | 1825 | case pch::TYPE_EXT_QUAL: { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1826 | assert(Record.size() == 2 && |
Douglas Gregor | 6d47396 | 2009-04-15 22:00:08 +0000 | [diff] [blame] | 1827 | "Incorrect encoding of extended qualifier type"); |
| 1828 | QualType Base = GetType(Record[0]); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1829 | Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]); |
| 1830 | return Context->getQualifiedType(Base, Quals); |
Douglas Gregor | 6d47396 | 2009-04-15 22:00:08 +0000 | [diff] [blame] | 1831 | } |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1832 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1833 | case pch::TYPE_COMPLEX: { |
| 1834 | assert(Record.size() == 1 && "Incorrect encoding of complex type"); |
| 1835 | QualType ElemType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1836 | return Context->getComplexType(ElemType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | case pch::TYPE_POINTER: { |
| 1840 | assert(Record.size() == 1 && "Incorrect encoding of pointer type"); |
| 1841 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1842 | return Context->getPointerType(PointeeType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | case pch::TYPE_BLOCK_POINTER: { |
| 1846 | assert(Record.size() == 1 && "Incorrect encoding of block pointer type"); |
| 1847 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1848 | return Context->getBlockPointerType(PointeeType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | case pch::TYPE_LVALUE_REFERENCE: { |
| 1852 | assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type"); |
| 1853 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1854 | return Context->getLValueReferenceType(PointeeType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | case pch::TYPE_RVALUE_REFERENCE: { |
| 1858 | assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type"); |
| 1859 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1860 | return Context->getRValueReferenceType(PointeeType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | case pch::TYPE_MEMBER_POINTER: { |
| 1864 | assert(Record.size() == 1 && "Incorrect encoding of member pointer type"); |
| 1865 | QualType PointeeType = GetType(Record[0]); |
| 1866 | QualType ClassType = GetType(Record[1]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1867 | return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr()); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1870 | case pch::TYPE_CONSTANT_ARRAY: { |
| 1871 | QualType ElementType = GetType(Record[0]); |
| 1872 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 1873 | unsigned IndexTypeQuals = Record[2]; |
| 1874 | unsigned Idx = 3; |
| 1875 | llvm::APInt Size = ReadAPInt(Record, Idx); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1876 | return Context->getConstantArrayType(ElementType, Size, |
| 1877 | ASM, IndexTypeQuals); |
| 1878 | } |
| 1879 | |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1880 | case pch::TYPE_INCOMPLETE_ARRAY: { |
| 1881 | QualType ElementType = GetType(Record[0]); |
| 1882 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 1883 | unsigned IndexTypeQuals = Record[2]; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1884 | return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | case pch::TYPE_VARIABLE_ARRAY: { |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1888 | QualType ElementType = GetType(Record[0]); |
| 1889 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 1890 | unsigned IndexTypeQuals = Record[2]; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1891 | SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]); |
| 1892 | SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1893 | return Context->getVariableArrayType(ElementType, ReadTypeExpr(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1894 | ASM, IndexTypeQuals, |
| 1895 | SourceRange(LBLoc, RBLoc)); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | case pch::TYPE_VECTOR: { |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1899 | if (Record.size() != 4) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1900 | Error("incorrect encoding of vector type in PCH file"); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1901 | return QualType(); |
| 1902 | } |
| 1903 | |
| 1904 | QualType ElementType = GetType(Record[0]); |
| 1905 | unsigned NumElements = Record[1]; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1906 | bool AltiVec = Record[2]; |
| 1907 | bool Pixel = Record[3]; |
| 1908 | return Context->getVectorType(ElementType, NumElements, AltiVec, Pixel); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | case pch::TYPE_EXT_VECTOR: { |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1912 | if (Record.size() != 4) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1913 | Error("incorrect encoding of extended vector type in PCH file"); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1914 | return QualType(); |
| 1915 | } |
| 1916 | |
| 1917 | QualType ElementType = GetType(Record[0]); |
| 1918 | unsigned NumElements = Record[1]; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1919 | return Context->getExtVectorType(ElementType, NumElements); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | case pch::TYPE_FUNCTION_NO_PROTO: { |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1923 | if (Record.size() != 3) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1924 | Error("incorrect encoding of no-proto function type"); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1925 | return QualType(); |
| 1926 | } |
| 1927 | QualType ResultType = GetType(Record[0]); |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1928 | return Context->getFunctionNoProtoType(ResultType, Record[1], |
| 1929 | (CallingConv)Record[2]); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
| 1932 | case pch::TYPE_FUNCTION_PROTO: { |
| 1933 | QualType ResultType = GetType(Record[0]); |
Douglas Gregor | 9123666 | 2009-12-22 18:11:50 +0000 | [diff] [blame] | 1934 | bool NoReturn = Record[1]; |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1935 | CallingConv CallConv = (CallingConv)Record[2]; |
| 1936 | unsigned Idx = 3; |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1937 | unsigned NumParams = Record[Idx++]; |
| 1938 | llvm::SmallVector<QualType, 16> ParamTypes; |
| 1939 | for (unsigned I = 0; I != NumParams; ++I) |
| 1940 | ParamTypes.push_back(GetType(Record[Idx++])); |
| 1941 | bool isVariadic = Record[Idx++]; |
| 1942 | unsigned Quals = Record[Idx++]; |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1943 | bool hasExceptionSpec = Record[Idx++]; |
| 1944 | bool hasAnyExceptionSpec = Record[Idx++]; |
| 1945 | unsigned NumExceptions = Record[Idx++]; |
| 1946 | llvm::SmallVector<QualType, 2> Exceptions; |
| 1947 | for (unsigned I = 0; I != NumExceptions; ++I) |
| 1948 | Exceptions.push_back(GetType(Record[Idx++])); |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1949 | return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1950 | isVariadic, Quals, hasExceptionSpec, |
| 1951 | hasAnyExceptionSpec, NumExceptions, |
Douglas Gregor | ab8bbf4 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 1952 | Exceptions.data(), NoReturn, CallConv); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1955 | case pch::TYPE_UNRESOLVED_USING: |
| 1956 | return Context->getTypeDeclType( |
| 1957 | cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0]))); |
| 1958 | |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1959 | case pch::TYPE_TYPEDEF: |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1960 | assert(Record.size() == 1 && "incorrect encoding of typedef type"); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1961 | return Context->getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0]))); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1962 | |
| 1963 | case pch::TYPE_TYPEOF_EXPR: |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1964 | return Context->getTypeOfExprType(ReadTypeExpr()); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1965 | |
| 1966 | case pch::TYPE_TYPEOF: { |
| 1967 | if (Record.size() != 1) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1968 | Error("incorrect encoding of typeof(type) in PCH file"); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1969 | return QualType(); |
| 1970 | } |
| 1971 | QualType UnderlyingType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1972 | return Context->getTypeOfType(UnderlyingType); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1973 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1974 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1975 | case pch::TYPE_DECLTYPE: |
| 1976 | return Context->getDecltypeType(ReadTypeExpr()); |
| 1977 | |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1978 | case pch::TYPE_RECORD: |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1979 | assert(Record.size() == 1 && "incorrect encoding of record type"); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1980 | return Context->getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0]))); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1981 | |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1982 | case pch::TYPE_ENUM: |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1983 | assert(Record.size() == 1 && "incorrect encoding of enum type"); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1984 | return Context->getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0]))); |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1985 | |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 1986 | case pch::TYPE_ELABORATED: { |
| 1987 | assert(Record.size() == 2 && "incorrect encoding of elaborated type"); |
| 1988 | unsigned Tag = Record[1]; |
| 1989 | return Context->getElaboratedType(GetType(Record[0]), |
| 1990 | (ElaboratedType::TagKind) Tag); |
| 1991 | } |
| 1992 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1993 | case pch::TYPE_OBJC_INTERFACE: { |
Chris Lattner | c6fa445 | 2009-04-22 06:45:28 +0000 | [diff] [blame] | 1994 | unsigned Idx = 0; |
| 1995 | ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++])); |
| 1996 | unsigned NumProtos = Record[Idx++]; |
| 1997 | llvm::SmallVector<ObjCProtocolDecl*, 4> Protos; |
| 1998 | for (unsigned I = 0; I != NumProtos; ++I) |
| 1999 | Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++]))); |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2000 | return Context->getObjCInterfaceType(ItfD, Protos.data(), NumProtos); |
Chris Lattner | c6fa445 | 2009-04-22 06:45:28 +0000 | [diff] [blame] | 2001 | } |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2002 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2003 | case pch::TYPE_OBJC_OBJECT_POINTER: { |
Chris Lattner | d7a3fcd | 2009-04-22 06:40:03 +0000 | [diff] [blame] | 2004 | unsigned Idx = 0; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2005 | QualType OIT = GetType(Record[Idx++]); |
Chris Lattner | d7a3fcd | 2009-04-22 06:40:03 +0000 | [diff] [blame] | 2006 | unsigned NumProtos = Record[Idx++]; |
| 2007 | llvm::SmallVector<ObjCProtocolDecl*, 4> Protos; |
| 2008 | for (unsigned I = 0; I != NumProtos; ++I) |
| 2009 | Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++]))); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2010 | return Context->getObjCObjectPointerType(OIT, Protos.data(), NumProtos); |
Chris Lattner | d7a3fcd | 2009-04-22 06:40:03 +0000 | [diff] [blame] | 2011 | } |
Argyrios Kyrtzidis | 24fab41 | 2009-09-29 19:42:55 +0000 | [diff] [blame] | 2012 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2013 | case pch::TYPE_SUBST_TEMPLATE_TYPE_PARM: { |
| 2014 | unsigned Idx = 0; |
| 2015 | QualType Parm = GetType(Record[Idx++]); |
| 2016 | QualType Replacement = GetType(Record[Idx++]); |
| 2017 | return |
| 2018 | Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm), |
| 2019 | Replacement); |
| 2020 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2021 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2022 | // Suppress a GCC warning |
| 2023 | return QualType(); |
| 2024 | } |
| 2025 | |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2026 | namespace { |
| 2027 | |
| 2028 | class TypeLocReader : public TypeLocVisitor<TypeLocReader> { |
| 2029 | PCHReader &Reader; |
| 2030 | const PCHReader::RecordData &Record; |
| 2031 | unsigned &Idx; |
| 2032 | |
| 2033 | public: |
| 2034 | TypeLocReader(PCHReader &Reader, const PCHReader::RecordData &Record, |
| 2035 | unsigned &Idx) |
| 2036 | : Reader(Reader), Record(Record), Idx(Idx) { } |
| 2037 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2038 | // We want compile-time assurance that we've enumerated all of |
| 2039 | // these, so unfortunately we have to declare them first, then |
| 2040 | // define them out-of-line. |
| 2041 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2042 | #define TYPELOC(CLASS, PARENT) \ |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2043 | void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2044 | #include "clang/AST/TypeLocNodes.def" |
| 2045 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2046 | void VisitFunctionTypeLoc(FunctionTypeLoc); |
| 2047 | void VisitArrayTypeLoc(ArrayTypeLoc); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2048 | }; |
| 2049 | |
| 2050 | } |
| 2051 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2052 | void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2053 | // nothing to do |
| 2054 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2055 | void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
Douglas Gregor | ddf889a | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 2056 | TL.setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2057 | if (TL.needsExtraLocalData()) { |
| 2058 | TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++])); |
| 2059 | TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++])); |
| 2060 | TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++])); |
| 2061 | TL.setModeAttr(Record[Idx++]); |
| 2062 | } |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2063 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2064 | void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { |
| 2065 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2066 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2067 | void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 2068 | TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2069 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2070 | void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 2071 | TL.setCaretLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2072 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2073 | void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 2074 | TL.setAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2075 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2076 | void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 2077 | TL.setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2078 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2079 | void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 2080 | TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2081 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2082 | void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 2083 | TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2084 | TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2085 | if (Record[Idx++]) |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2086 | TL.setSizeExpr(Reader.ReadDeclExpr()); |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 2087 | else |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2088 | TL.setSizeExpr(0); |
| 2089 | } |
| 2090 | void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { |
| 2091 | VisitArrayTypeLoc(TL); |
| 2092 | } |
| 2093 | void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { |
| 2094 | VisitArrayTypeLoc(TL); |
| 2095 | } |
| 2096 | void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { |
| 2097 | VisitArrayTypeLoc(TL); |
| 2098 | } |
| 2099 | void TypeLocReader::VisitDependentSizedArrayTypeLoc( |
| 2100 | DependentSizedArrayTypeLoc TL) { |
| 2101 | VisitArrayTypeLoc(TL); |
| 2102 | } |
| 2103 | void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( |
| 2104 | DependentSizedExtVectorTypeLoc TL) { |
| 2105 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2106 | } |
| 2107 | void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { |
| 2108 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2109 | } |
| 2110 | void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { |
| 2111 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2112 | } |
| 2113 | void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { |
| 2114 | TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2115 | TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2116 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) { |
John McCall | 86acc2a | 2009-10-23 01:28:53 +0000 | [diff] [blame] | 2117 | TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++]))); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2118 | } |
| 2119 | } |
| 2120 | void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { |
| 2121 | VisitFunctionTypeLoc(TL); |
| 2122 | } |
| 2123 | void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { |
| 2124 | VisitFunctionTypeLoc(TL); |
| 2125 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2126 | void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 2127 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2128 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2129 | void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 2130 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2131 | } |
| 2132 | void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 2133 | TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2134 | TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2135 | TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2136 | } |
| 2137 | void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 2138 | TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2139 | TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2140 | TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2141 | TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(Record, Idx)); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2142 | } |
| 2143 | void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 2144 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2145 | } |
| 2146 | void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { |
| 2147 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2148 | } |
| 2149 | void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { |
| 2150 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2151 | } |
| 2152 | void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 2153 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2154 | } |
| 2155 | void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 2156 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2157 | } |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2158 | void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( |
| 2159 | SubstTemplateTypeParmTypeLoc TL) { |
| 2160 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2161 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2162 | void TypeLocReader::VisitTemplateSpecializationTypeLoc( |
| 2163 | TemplateSpecializationTypeLoc TL) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2164 | TL.setTemplateNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2165 | TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2166 | TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2167 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 2168 | TL.setArgLocInfo(i, |
| 2169 | Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind(), |
| 2170 | Record, Idx)); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2171 | } |
| 2172 | void TypeLocReader::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) { |
| 2173 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2174 | } |
| 2175 | void TypeLocReader::VisitTypenameTypeLoc(TypenameTypeLoc TL) { |
| 2176 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2177 | } |
| 2178 | void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 2179 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2180 | TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2181 | TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2182 | for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) |
| 2183 | TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2184 | } |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2185 | void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 2186 | TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2187 | TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2188 | TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2189 | TL.setHasBaseTypeAsWritten(Record[Idx++]); |
| 2190 | TL.setHasProtocolsAsWritten(Record[Idx++]); |
| 2191 | if (TL.hasProtocolsAsWritten()) |
| 2192 | for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) |
| 2193 | TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2194 | } |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2195 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2196 | TypeSourceInfo *PCHReader::GetTypeSourceInfo(const RecordData &Record, |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2197 | unsigned &Idx) { |
| 2198 | QualType InfoTy = GetType(Record[Idx++]); |
| 2199 | if (InfoTy.isNull()) |
| 2200 | return 0; |
| 2201 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2202 | TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2203 | TypeLocReader TLR(*this, Record, Idx); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2204 | for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc()) |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2205 | TLR.Visit(TL); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2206 | return TInfo; |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2207 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2208 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 2209 | QualType PCHReader::GetType(pch::TypeID ID) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2210 | unsigned FastQuals = ID & Qualifiers::FastMask; |
| 2211 | unsigned Index = ID >> Qualifiers::FastWidth; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2212 | |
| 2213 | if (Index < pch::NUM_PREDEF_TYPE_IDS) { |
| 2214 | QualType T; |
| 2215 | switch ((pch::PredefinedTypeIDs)Index) { |
| 2216 | case pch::PREDEF_TYPE_NULL_ID: return QualType(); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2217 | case pch::PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break; |
| 2218 | case pch::PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2219 | |
| 2220 | case pch::PREDEF_TYPE_CHAR_U_ID: |
| 2221 | case pch::PREDEF_TYPE_CHAR_S_ID: |
| 2222 | // FIXME: Check that the signedness of CharTy is correct! |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2223 | T = Context->CharTy; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2224 | break; |
| 2225 | |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2226 | case pch::PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break; |
| 2227 | case pch::PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break; |
| 2228 | case pch::PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break; |
| 2229 | case pch::PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break; |
| 2230 | case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2231 | case pch::PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2232 | case pch::PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break; |
| 2233 | case pch::PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break; |
| 2234 | case pch::PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break; |
| 2235 | case pch::PREDEF_TYPE_INT_ID: T = Context->IntTy; break; |
| 2236 | case pch::PREDEF_TYPE_LONG_ID: T = Context->LongTy; break; |
| 2237 | case pch::PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2238 | case pch::PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2239 | case pch::PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break; |
| 2240 | case pch::PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break; |
| 2241 | case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break; |
| 2242 | case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break; |
| 2243 | case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2244 | case pch::PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2245 | case pch::PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break; |
| 2246 | case pch::PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break; |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2247 | case pch::PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break; |
| 2248 | case pch::PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break; |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 2249 | case pch::PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | assert(!T.isNull() && "Unknown predefined type"); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2253 | return T.withFastQualifiers(FastQuals); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | Index -= pch::NUM_PREDEF_TYPE_IDS; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2257 | //assert(Index < TypesLoaded.size() && "Type index out-of-range"); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2258 | if (TypesLoaded[Index].isNull()) |
| 2259 | TypesLoaded[Index] = ReadTypeRecord(TypeOffsets[Index]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2260 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2261 | return TypesLoaded[Index].withFastQualifiers(FastQuals); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2264 | TemplateArgumentLocInfo |
| 2265 | PCHReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, |
| 2266 | const RecordData &Record, |
| 2267 | unsigned &Index) { |
| 2268 | switch (Kind) { |
| 2269 | case TemplateArgument::Expression: |
| 2270 | return ReadDeclExpr(); |
| 2271 | case TemplateArgument::Type: |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2272 | return GetTypeSourceInfo(Record, Index); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2273 | case TemplateArgument::Template: { |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame^] | 2274 | SourceLocation |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2275 | QualStart = SourceLocation::getFromRawEncoding(Record[Index++]), |
| 2276 | QualEnd = SourceLocation::getFromRawEncoding(Record[Index++]), |
| 2277 | TemplateNameLoc = SourceLocation::getFromRawEncoding(Record[Index++]); |
| 2278 | return TemplateArgumentLocInfo(SourceRange(QualStart, QualEnd), |
| 2279 | TemplateNameLoc); |
| 2280 | } |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2281 | case TemplateArgument::Null: |
| 2282 | case TemplateArgument::Integral: |
| 2283 | case TemplateArgument::Declaration: |
| 2284 | case TemplateArgument::Pack: |
| 2285 | return TemplateArgumentLocInfo(); |
| 2286 | } |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2287 | llvm_unreachable("unexpected template argument loc"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2288 | return TemplateArgumentLocInfo(); |
| 2289 | } |
| 2290 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 2291 | Decl *PCHReader::GetDecl(pch::DeclID ID) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2292 | if (ID == 0) |
| 2293 | return 0; |
| 2294 | |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2295 | if (ID > DeclsLoaded.size()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2296 | Error("declaration ID out-of-range for PCH file"); |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2297 | return 0; |
| 2298 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2299 | |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2300 | unsigned Index = ID - 1; |
| 2301 | if (!DeclsLoaded[Index]) |
| 2302 | ReadDeclRecord(DeclOffsets[Index], Index); |
| 2303 | |
| 2304 | return DeclsLoaded[Index]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
Chris Lattner | 887e2b3 | 2009-04-27 05:46:25 +0000 | [diff] [blame] | 2307 | /// \brief Resolve the offset of a statement into a statement. |
| 2308 | /// |
| 2309 | /// This operation will read a new statement from the external |
| 2310 | /// source each time it is called, and is meant to be used via a |
| 2311 | /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). |
| 2312 | Stmt *PCHReader::GetDeclStmt(uint64_t Offset) { |
Chris Lattner | da93061 | 2009-04-27 05:58:23 +0000 | [diff] [blame] | 2313 | // Since we know tha this statement is part of a decl, make sure to use the |
| 2314 | // decl cursor to read it. |
| 2315 | DeclsCursor.JumpToBit(Offset); |
| 2316 | return ReadStmt(DeclsCursor); |
Douglas Gregor | 250fc9c | 2009-04-18 00:07:54 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2319 | bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC, |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 2320 | llvm::SmallVectorImpl<pch::DeclID> &Decls) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2321 | assert(DC->hasExternalLexicalStorage() && |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2322 | "DeclContext has no lexical decls in storage"); |
| 2323 | uint64_t Offset = DeclContextOffsets[DC].first; |
| 2324 | assert(Offset && "DeclContext has no lexical decls in storage"); |
| 2325 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 2326 | // Keep track of where we are in the stream, then jump back there |
| 2327 | // after reading this context. |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 2328 | SavedStreamPosition SavedPosition(DeclsCursor); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 2329 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2330 | // Load the record containing all of the declarations lexically in |
| 2331 | // this context. |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 2332 | DeclsCursor.JumpToBit(Offset); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2333 | RecordData Record; |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 2334 | unsigned Code = DeclsCursor.ReadCode(); |
| 2335 | unsigned RecCode = DeclsCursor.ReadRecord(Code, Record); |
Douglas Gregor | 6a2bfb2 | 2009-04-15 18:43:11 +0000 | [diff] [blame] | 2336 | (void)RecCode; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2337 | assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block"); |
| 2338 | |
| 2339 | // Load all of the declaration IDs |
| 2340 | Decls.clear(); |
| 2341 | Decls.insert(Decls.end(), Record.begin(), Record.end()); |
Douglas Gregor | 2512308 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 2342 | ++NumLexicalDeclContextsRead; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2343 | return false; |
| 2344 | } |
| 2345 | |
| 2346 | bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC, |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 2347 | llvm::SmallVectorImpl<VisibleDeclaration> &Decls) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2348 | assert(DC->hasExternalVisibleStorage() && |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2349 | "DeclContext has no visible decls in storage"); |
| 2350 | uint64_t Offset = DeclContextOffsets[DC].second; |
| 2351 | assert(Offset && "DeclContext has no visible decls in storage"); |
| 2352 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 2353 | // Keep track of where we are in the stream, then jump back there |
| 2354 | // after reading this context. |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 2355 | SavedStreamPosition SavedPosition(DeclsCursor); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 2356 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2357 | // Load the record containing all of the declarations visible in |
| 2358 | // this context. |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 2359 | DeclsCursor.JumpToBit(Offset); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2360 | RecordData Record; |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 2361 | unsigned Code = DeclsCursor.ReadCode(); |
| 2362 | unsigned RecCode = DeclsCursor.ReadRecord(Code, Record); |
Douglas Gregor | 6a2bfb2 | 2009-04-15 18:43:11 +0000 | [diff] [blame] | 2363 | (void)RecCode; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2364 | assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block"); |
| 2365 | if (Record.size() == 0) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2366 | return false; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2367 | |
| 2368 | Decls.clear(); |
| 2369 | |
| 2370 | unsigned Idx = 0; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2371 | while (Idx < Record.size()) { |
| 2372 | Decls.push_back(VisibleDeclaration()); |
| 2373 | Decls.back().Name = ReadDeclarationName(Record, Idx); |
| 2374 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2375 | unsigned Size = Record[Idx++]; |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 2376 | llvm::SmallVector<unsigned, 4> &LoadedDecls = Decls.back().Declarations; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2377 | LoadedDecls.reserve(Size); |
| 2378 | for (unsigned I = 0; I < Size; ++I) |
| 2379 | LoadedDecls.push_back(Record[Idx++]); |
| 2380 | } |
| 2381 | |
Douglas Gregor | 2512308 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 2382 | ++NumVisibleDeclContextsRead; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2383 | return false; |
| 2384 | } |
| 2385 | |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 2386 | void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) { |
Douglas Gregor | 0af2ca4 | 2009-04-22 19:09:20 +0000 | [diff] [blame] | 2387 | this->Consumer = Consumer; |
| 2388 | |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 2389 | if (!Consumer) |
| 2390 | return; |
| 2391 | |
| 2392 | for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) { |
Daniel Dunbar | 04a0b50 | 2009-09-17 03:06:44 +0000 | [diff] [blame] | 2393 | // Force deserialization of this decl, which will cause it to be passed to |
| 2394 | // the consumer (or queued). |
| 2395 | GetDecl(ExternalDefinitions[I]); |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 2396 | } |
Douglas Gregor | c62a2fe | 2009-04-25 00:41:30 +0000 | [diff] [blame] | 2397 | |
| 2398 | for (unsigned I = 0, N = InterestingDecls.size(); I != N; ++I) { |
| 2399 | DeclGroupRef DG(InterestingDecls[I]); |
| 2400 | Consumer->HandleTopLevelDecl(DG); |
| 2401 | } |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2404 | void PCHReader::PrintStats() { |
| 2405 | std::fprintf(stderr, "*** PCH Statistics:\n"); |
| 2406 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2407 | unsigned NumTypesLoaded |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2408 | = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2409 | QualType()); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2410 | unsigned NumDeclsLoaded |
| 2411 | = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), |
| 2412 | (Decl *)0); |
| 2413 | unsigned NumIdentifiersLoaded |
| 2414 | = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), |
| 2415 | IdentifiersLoaded.end(), |
| 2416 | (IdentifierInfo *)0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2417 | unsigned NumSelectorsLoaded |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2418 | = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), |
| 2419 | SelectorsLoaded.end(), |
| 2420 | Selector()); |
Douglas Gregor | 2d41cc1 | 2009-04-13 20:50:16 +0000 | [diff] [blame] | 2421 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 2422 | std::fprintf(stderr, " %u stat cache hits\n", NumStatHits); |
| 2423 | std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 2424 | if (TotalNumSLocEntries) |
| 2425 | std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", |
| 2426 | NumSLocEntriesRead, TotalNumSLocEntries, |
| 2427 | ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2428 | if (!TypesLoaded.empty()) |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2429 | std::fprintf(stderr, " %u/%u types read (%f%%)\n", |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2430 | NumTypesLoaded, (unsigned)TypesLoaded.size(), |
| 2431 | ((float)NumTypesLoaded/TypesLoaded.size() * 100)); |
| 2432 | if (!DeclsLoaded.empty()) |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2433 | std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2434 | NumDeclsLoaded, (unsigned)DeclsLoaded.size(), |
| 2435 | ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2436 | if (!IdentifiersLoaded.empty()) |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2437 | std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2438 | NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), |
| 2439 | ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2440 | if (TotalNumSelectors) |
| 2441 | std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", |
| 2442 | NumSelectorsLoaded, TotalNumSelectors, |
| 2443 | ((float)NumSelectorsLoaded/TotalNumSelectors * 100)); |
| 2444 | if (TotalNumStatements) |
| 2445 | std::fprintf(stderr, " %u/%u statements read (%f%%)\n", |
| 2446 | NumStatementsRead, TotalNumStatements, |
| 2447 | ((float)NumStatementsRead/TotalNumStatements * 100)); |
| 2448 | if (TotalNumMacros) |
| 2449 | std::fprintf(stderr, " %u/%u macros read (%f%%)\n", |
| 2450 | NumMacrosRead, TotalNumMacros, |
| 2451 | ((float)NumMacrosRead/TotalNumMacros * 100)); |
| 2452 | if (TotalLexicalDeclContexts) |
| 2453 | std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", |
| 2454 | NumLexicalDeclContextsRead, TotalLexicalDeclContexts, |
| 2455 | ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts |
| 2456 | * 100)); |
| 2457 | if (TotalVisibleDeclContexts) |
| 2458 | std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", |
| 2459 | NumVisibleDeclContextsRead, TotalVisibleDeclContexts, |
| 2460 | ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts |
| 2461 | * 100)); |
| 2462 | if (TotalSelectorsInMethodPool) { |
| 2463 | std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", |
| 2464 | NumMethodPoolSelectorsRead, TotalSelectorsInMethodPool, |
| 2465 | ((float)NumMethodPoolSelectorsRead/TotalSelectorsInMethodPool |
| 2466 | * 100)); |
| 2467 | std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses); |
| 2468 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2469 | std::fprintf(stderr, "\n"); |
| 2470 | } |
| 2471 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2472 | void PCHReader::InitializeSema(Sema &S) { |
| 2473 | SemaObj = &S; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2474 | S.ExternalSource = this; |
| 2475 | |
Douglas Gregor | 6cfc1a8 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 2476 | // Makes sure any declarations that were deserialized "too early" |
| 2477 | // still get added to the identifier's declaration chains. |
| 2478 | for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) { |
| 2479 | SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I])); |
| 2480 | SemaObj->IdResolver.AddDecl(PreloadedDecls[I]); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2481 | } |
Douglas Gregor | 6cfc1a8 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 2482 | PreloadedDecls.clear(); |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 2483 | |
| 2484 | // If there were any tentative definitions, deserialize them and add |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2485 | // them to Sema's list of tentative definitions. |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 2486 | for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { |
| 2487 | VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I])); |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2488 | SemaObj->TentativeDefinitions.push_back(Var); |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 2489 | } |
Kovarththanan Rajaratnam | 6b82f64 | 2010-03-07 19:10:13 +0000 | [diff] [blame^] | 2490 | |
Tanya Lattner | e6bbc01 | 2010-02-12 00:07:30 +0000 | [diff] [blame] | 2491 | // If there were any unused static functions, deserialize them and add to |
| 2492 | // Sema's list of unused static functions. |
| 2493 | for (unsigned I = 0, N = UnusedStaticFuncs.size(); I != N; ++I) { |
| 2494 | FunctionDecl *FD = cast<FunctionDecl>(GetDecl(UnusedStaticFuncs[I])); |
| 2495 | SemaObj->UnusedStaticFuncs.push_back(FD); |
| 2496 | } |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 2497 | |
| 2498 | // If there were any locally-scoped external declarations, |
| 2499 | // deserialize them and add them to Sema's table of locally-scoped |
| 2500 | // external declarations. |
| 2501 | for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) { |
| 2502 | NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I])); |
| 2503 | SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D; |
| 2504 | } |
Douglas Gregor | b81c170 | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 2505 | |
| 2506 | // If there were any ext_vector type declarations, deserialize them |
| 2507 | // and add them to Sema's vector of such declarations. |
| 2508 | for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) |
| 2509 | SemaObj->ExtVectorDecls.push_back( |
| 2510 | cast<TypedefDecl>(GetDecl(ExtVectorDecls[I]))); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2511 | } |
| 2512 | |
| 2513 | IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) { |
| 2514 | // Try to find this name within our on-disk hash table |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2515 | PCHIdentifierLookupTable *IdTable |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2516 | = (PCHIdentifierLookupTable *)IdentifierLookupTable; |
| 2517 | std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart); |
| 2518 | PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key); |
| 2519 | if (Pos == IdTable->end()) |
| 2520 | return 0; |
| 2521 | |
| 2522 | // Dereferencing the iterator has the effect of building the |
| 2523 | // IdentifierInfo node and populating it with the various |
| 2524 | // declarations it needs. |
| 2525 | return *Pos; |
| 2526 | } |
| 2527 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2528 | std::pair<ObjCMethodList, ObjCMethodList> |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2529 | PCHReader::ReadMethodPool(Selector Sel) { |
| 2530 | if (!MethodPoolLookupTable) |
| 2531 | return std::pair<ObjCMethodList, ObjCMethodList>(); |
| 2532 | |
| 2533 | // Try to find this selector within our on-disk hash table. |
| 2534 | PCHMethodPoolLookupTable *PoolTable |
| 2535 | = (PCHMethodPoolLookupTable*)MethodPoolLookupTable; |
| 2536 | PCHMethodPoolLookupTable::iterator Pos = PoolTable->find(Sel); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2537 | if (Pos == PoolTable->end()) { |
| 2538 | ++NumMethodPoolMisses; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2539 | return std::pair<ObjCMethodList, ObjCMethodList>();; |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2540 | } |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2541 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2542 | ++NumMethodPoolSelectorsRead; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2543 | return *Pos; |
| 2544 | } |
| 2545 | |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2546 | void PCHReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) { |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2547 | assert(ID && "Non-zero identifier ID required"); |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2548 | assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2549 | IdentifiersLoaded[ID - 1] = II; |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 2552 | /// \brief Set the globally-visible declarations associated with the given |
| 2553 | /// identifier. |
| 2554 | /// |
| 2555 | /// 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] | 2556 | /// 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] | 2557 | /// them. |
| 2558 | /// |
| 2559 | /// \param II an IdentifierInfo that refers to one or more globally-visible |
| 2560 | /// declarations. |
| 2561 | /// |
| 2562 | /// \param DeclIDs the set of declaration IDs with the name @p II that are |
| 2563 | /// visible at global scope. |
| 2564 | /// |
| 2565 | /// \param Nonrecursive should be true to indicate that the caller knows that |
| 2566 | /// this call is non-recursive, and therefore the globally-visible declarations |
| 2567 | /// will not be placed onto the pending queue. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2568 | void |
| 2569 | PCHReader::SetGloballyVisibleDecls(IdentifierInfo *II, |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 2570 | const llvm::SmallVectorImpl<uint32_t> &DeclIDs, |
| 2571 | bool Nonrecursive) { |
| 2572 | if (CurrentlyLoadingTypeOrDecl && !Nonrecursive) { |
| 2573 | PendingIdentifierInfos.push_back(PendingIdentifierInfo()); |
| 2574 | PendingIdentifierInfo &PII = PendingIdentifierInfos.back(); |
| 2575 | PII.II = II; |
| 2576 | for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) |
| 2577 | PII.DeclIDs.push_back(DeclIDs[I]); |
| 2578 | return; |
| 2579 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2580 | |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 2581 | for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { |
| 2582 | NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); |
| 2583 | if (SemaObj) { |
| 2584 | // Introduce this declaration into the translation-unit scope |
| 2585 | // and add it to the declaration chain for this identifier, so |
| 2586 | // that (unqualified) name lookup will find it. |
| 2587 | SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D)); |
| 2588 | SemaObj->IdResolver.AddDeclToIdentifierChain(II, D); |
| 2589 | } else { |
| 2590 | // Queue this declaration so that it will be added to the |
| 2591 | // translation unit scope and identifier's declaration chain |
| 2592 | // once a Sema object is known. |
| 2593 | PreloadedDecls.push_back(D); |
| 2594 | } |
| 2595 | } |
| 2596 | } |
| 2597 | |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 2598 | IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) { |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2599 | if (ID == 0) |
| 2600 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2601 | |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2602 | if (!IdentifierTableData || IdentifiersLoaded.empty()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2603 | Error("no identifier table in PCH file"); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2604 | return 0; |
| 2605 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2606 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2607 | assert(PP && "Forgot to set Preprocessor ?"); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2608 | if (!IdentifiersLoaded[ID - 1]) { |
| 2609 | uint32_t Offset = IdentifierOffsets[ID - 1]; |
Douglas Gregor | 17e1c5e | 2009-04-25 21:21:38 +0000 | [diff] [blame] | 2610 | const char *Str = IdentifierTableData + Offset; |
Douglas Gregor | d6595a4 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 2611 | |
Douglas Gregor | 02fc751 | 2009-04-28 20:01:51 +0000 | [diff] [blame] | 2612 | // All of the strings in the PCH file are preceded by a 16-bit |
| 2613 | // length. Extract that 16-bit length to avoid having to execute |
| 2614 | // strlen(). |
Ted Kremenek | 231bc0b | 2009-10-23 04:45:31 +0000 | [diff] [blame] | 2615 | // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as |
| 2616 | // unsigned integers. This is important to avoid integer overflow when |
| 2617 | // we cast them to 'unsigned'. |
Ted Kremenek | ff1ea46 | 2009-10-23 03:57:22 +0000 | [diff] [blame] | 2618 | const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; |
Douglas Gregor | 02fc751 | 2009-04-28 20:01:51 +0000 | [diff] [blame] | 2619 | unsigned StrLen = (((unsigned) StrLenPtr[0]) |
| 2620 | | (((unsigned) StrLenPtr[1]) << 8)) - 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2621 | IdentifiersLoaded[ID - 1] |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2622 | = &PP->getIdentifierTable().get(Str, Str + StrLen); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2623 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2624 | |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2625 | return IdentifiersLoaded[ID - 1]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 2628 | void PCHReader::ReadSLocEntry(unsigned ID) { |
| 2629 | ReadSLocEntryRecord(ID); |
| 2630 | } |
| 2631 | |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 2632 | Selector PCHReader::DecodeSelector(unsigned ID) { |
| 2633 | if (ID == 0) |
| 2634 | return Selector(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2635 | |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2636 | if (!MethodPoolLookupTableData) |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 2637 | return Selector(); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2638 | |
| 2639 | if (ID > TotalNumSelectors) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2640 | Error("selector ID out of range in PCH file"); |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 2641 | return Selector(); |
| 2642 | } |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2643 | |
| 2644 | unsigned Index = ID - 1; |
| 2645 | if (SelectorsLoaded[Index].getAsOpaquePtr() == 0) { |
| 2646 | // Load this selector from the selector table. |
| 2647 | // FIXME: endianness portability issues with SelectorOffsets table |
| 2648 | PCHMethodPoolLookupTrait Trait(*this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2649 | SelectorsLoaded[Index] |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2650 | = Trait.ReadKey(MethodPoolLookupTableData + SelectorOffsets[Index], 0); |
| 2651 | } |
| 2652 | |
| 2653 | return SelectorsLoaded[Index]; |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2656 | DeclarationName |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2657 | PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) { |
| 2658 | DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; |
| 2659 | switch (Kind) { |
| 2660 | case DeclarationName::Identifier: |
| 2661 | return DeclarationName(GetIdentifierInfo(Record, Idx)); |
| 2662 | |
| 2663 | case DeclarationName::ObjCZeroArgSelector: |
| 2664 | case DeclarationName::ObjCOneArgSelector: |
| 2665 | case DeclarationName::ObjCMultiArgSelector: |
Steve Naroff | a7503a7 | 2009-04-23 15:15:40 +0000 | [diff] [blame] | 2666 | return DeclarationName(GetSelector(Record, Idx)); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2667 | |
| 2668 | case DeclarationName::CXXConstructorName: |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2669 | return Context->DeclarationNames.getCXXConstructorName( |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2670 | Context->getCanonicalType(GetType(Record[Idx++]))); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2671 | |
| 2672 | case DeclarationName::CXXDestructorName: |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2673 | return Context->DeclarationNames.getCXXDestructorName( |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2674 | Context->getCanonicalType(GetType(Record[Idx++]))); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2675 | |
| 2676 | case DeclarationName::CXXConversionFunctionName: |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2677 | return Context->DeclarationNames.getCXXConversionFunctionName( |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2678 | Context->getCanonicalType(GetType(Record[Idx++]))); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2679 | |
| 2680 | case DeclarationName::CXXOperatorName: |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2681 | return Context->DeclarationNames.getCXXOperatorName( |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2682 | (OverloadedOperatorKind)Record[Idx++]); |
| 2683 | |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 2684 | case DeclarationName::CXXLiteralOperatorName: |
| 2685 | return Context->DeclarationNames.getCXXLiteralOperatorName( |
| 2686 | GetIdentifierInfo(Record, Idx)); |
| 2687 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2688 | case DeclarationName::CXXUsingDirective: |
| 2689 | return DeclarationName::getUsingDirectiveName(); |
| 2690 | } |
| 2691 | |
| 2692 | // Required to silence GCC warning |
| 2693 | return DeclarationName(); |
| 2694 | } |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 2695 | |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 2696 | /// \brief Read an integral value |
| 2697 | llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { |
| 2698 | unsigned BitWidth = Record[Idx++]; |
| 2699 | unsigned NumWords = llvm::APInt::getNumWords(BitWidth); |
| 2700 | llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); |
| 2701 | Idx += NumWords; |
| 2702 | return Result; |
| 2703 | } |
| 2704 | |
| 2705 | /// \brief Read a signed integral value |
| 2706 | llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { |
| 2707 | bool isUnsigned = Record[Idx++]; |
| 2708 | return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); |
| 2709 | } |
| 2710 | |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 2711 | /// \brief Read a floating-point value |
| 2712 | llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) { |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 2713 | return llvm::APFloat(ReadAPInt(Record, Idx)); |
| 2714 | } |
| 2715 | |
Douglas Gregor | 68a2eb0 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 2716 | // \brief Read a string |
| 2717 | std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) { |
| 2718 | unsigned Len = Record[Idx++]; |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2719 | std::string Result(Record.data() + Idx, Record.data() + Idx + Len); |
Douglas Gregor | 68a2eb0 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 2720 | Idx += Len; |
| 2721 | return Result; |
| 2722 | } |
| 2723 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 2724 | DiagnosticBuilder PCHReader::Diag(unsigned DiagID) { |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 2725 | return Diag(SourceLocation(), DiagID); |
| 2726 | } |
| 2727 | |
| 2728 | DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2729 | return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 2730 | } |
Douglas Gregor | 025452f | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 2731 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2732 | /// \brief Retrieve the identifier table associated with the |
| 2733 | /// preprocessor. |
| 2734 | IdentifierTable &PCHReader::getIdentifierTable() { |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2735 | assert(PP && "Forgot to set Preprocessor ?"); |
| 2736 | return PP->getIdentifierTable(); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
Douglas Gregor | 025452f | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 2739 | /// \brief Record that the given ID maps to the given switch-case |
| 2740 | /// statement. |
| 2741 | void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { |
| 2742 | assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID"); |
| 2743 | SwitchCaseStmts[ID] = SC; |
| 2744 | } |
| 2745 | |
| 2746 | /// \brief Retrieve the switch-case statement with the given ID. |
| 2747 | SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) { |
| 2748 | assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID"); |
| 2749 | return SwitchCaseStmts[ID]; |
| 2750 | } |
Douglas Gregor | 1de05fe | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 2751 | |
| 2752 | /// \brief Record that the given label statement has been |
| 2753 | /// deserialized and has the given ID. |
| 2754 | void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | assert(LabelStmts.find(ID) == LabelStmts.end() && |
Douglas Gregor | 1de05fe | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 2756 | "Deserialized label twice"); |
| 2757 | LabelStmts[ID] = S; |
| 2758 | |
| 2759 | // If we've already seen any goto statements that point to this |
| 2760 | // label, resolve them now. |
| 2761 | typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter; |
| 2762 | std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID); |
| 2763 | for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto) |
| 2764 | Goto->second->setLabel(S); |
| 2765 | UnresolvedGotoStmts.erase(Gotos.first, Gotos.second); |
Douglas Gregor | 7d5c2f2 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 2766 | |
| 2767 | // If we've already seen any address-label statements that point to |
| 2768 | // this label, resolve them now. |
| 2769 | typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2770 | std::pair<AddrLabelIter, AddrLabelIter> AddrLabels |
Douglas Gregor | 7d5c2f2 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 2771 | = UnresolvedAddrLabelExprs.equal_range(ID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2772 | for (AddrLabelIter AddrLabel = AddrLabels.first; |
Douglas Gregor | 7d5c2f2 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 2773 | AddrLabel != AddrLabels.second; ++AddrLabel) |
| 2774 | AddrLabel->second->setLabel(S); |
| 2775 | UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second); |
Douglas Gregor | 1de05fe | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
| 2778 | /// \brief Set the label of the given statement to the label |
| 2779 | /// identified by ID. |
| 2780 | /// |
| 2781 | /// Depending on the order in which the label and other statements |
| 2782 | /// referencing that label occur, this operation may complete |
| 2783 | /// immediately (updating the statement) or it may queue the |
| 2784 | /// statement to be back-patched later. |
| 2785 | void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) { |
| 2786 | std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID); |
| 2787 | if (Label != LabelStmts.end()) { |
| 2788 | // We've already seen this label, so set the label of the goto and |
| 2789 | // we're done. |
| 2790 | S->setLabel(Label->second); |
| 2791 | } else { |
| 2792 | // We haven't seen this label yet, so add this goto to the set of |
| 2793 | // unresolved goto statements. |
| 2794 | UnresolvedGotoStmts.insert(std::make_pair(ID, S)); |
| 2795 | } |
| 2796 | } |
Douglas Gregor | 7d5c2f2 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 2797 | |
| 2798 | /// \brief Set the label of the given expression to the label |
| 2799 | /// identified by ID. |
| 2800 | /// |
| 2801 | /// Depending on the order in which the label and other statements |
| 2802 | /// referencing that label occur, this operation may complete |
| 2803 | /// immediately (updating the statement) or it may queue the |
| 2804 | /// statement to be back-patched later. |
| 2805 | void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) { |
| 2806 | std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID); |
| 2807 | if (Label != LabelStmts.end()) { |
| 2808 | // We've already seen this label, so set the label of the |
| 2809 | // label-address expression and we're done. |
| 2810 | S->setLabel(Label->second); |
| 2811 | } else { |
| 2812 | // We haven't seen this label yet, so add this label-address |
| 2813 | // expression to the set of unresolved label-address expressions. |
| 2814 | UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S)); |
| 2815 | } |
| 2816 | } |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 2817 | |
| 2818 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2819 | PCHReader::LoadingTypeOrDecl::LoadingTypeOrDecl(PCHReader &Reader) |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 2820 | : Reader(Reader), Parent(Reader.CurrentlyLoadingTypeOrDecl) { |
| 2821 | Reader.CurrentlyLoadingTypeOrDecl = this; |
| 2822 | } |
| 2823 | |
| 2824 | PCHReader::LoadingTypeOrDecl::~LoadingTypeOrDecl() { |
| 2825 | if (!Parent) { |
| 2826 | // If any identifiers with corresponding top-level declarations have |
| 2827 | // been loaded, load those declarations now. |
| 2828 | while (!Reader.PendingIdentifierInfos.empty()) { |
| 2829 | Reader.SetGloballyVisibleDecls(Reader.PendingIdentifierInfos.front().II, |
| 2830 | Reader.PendingIdentifierInfos.front().DeclIDs, |
| 2831 | true); |
| 2832 | Reader.PendingIdentifierInfos.pop_front(); |
| 2833 | } |
| 2834 | } |
| 2835 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2836 | Reader.CurrentlyLoadingTypeOrDecl = Parent; |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 2837 | } |