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