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