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