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