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