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