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