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