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); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1594 | /// \brief Retrieve the name of the original source file name |
| 1595 | /// directly from the PCH file, without actually loading the PCH |
| 1596 | /// file. |
| 1597 | std::string PCHReader::getOriginalSourceFile(const std::string &PCHFileName) { |
| 1598 | // Open the PCH file. |
| 1599 | std::string ErrStr; |
| 1600 | llvm::OwningPtr<llvm::MemoryBuffer> Buffer; |
| 1601 | Buffer.reset(llvm::MemoryBuffer::getFile(PCHFileName.c_str(), &ErrStr)); |
| 1602 | if (!Buffer) { |
| 1603 | fprintf(stderr, "error: %s\n", ErrStr.c_str()); |
| 1604 | return std::string(); |
| 1605 | } |
| 1606 | |
| 1607 | // Initialize the stream |
| 1608 | llvm::BitstreamReader StreamFile; |
| 1609 | llvm::BitstreamCursor Stream; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1610 | StreamFile.init((const unsigned char *)Buffer->getBufferStart(), |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1611 | (const unsigned char *)Buffer->getBufferEnd()); |
| 1612 | Stream.init(StreamFile); |
| 1613 | |
| 1614 | // Sniff for the signature. |
| 1615 | if (Stream.Read(8) != 'C' || |
| 1616 | Stream.Read(8) != 'P' || |
| 1617 | Stream.Read(8) != 'C' || |
| 1618 | Stream.Read(8) != 'H') { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1619 | fprintf(stderr, |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1620 | "error: '%s' does not appear to be a precompiled header file\n", |
| 1621 | PCHFileName.c_str()); |
| 1622 | return std::string(); |
| 1623 | } |
| 1624 | |
| 1625 | RecordData Record; |
| 1626 | while (!Stream.AtEndOfStream()) { |
| 1627 | unsigned Code = Stream.ReadCode(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1629 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 1630 | unsigned BlockID = Stream.ReadSubBlockID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1631 | |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1632 | // We only know the PCH subblock ID. |
| 1633 | switch (BlockID) { |
| 1634 | case pch::PCH_BLOCK_ID: |
| 1635 | if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) { |
| 1636 | fprintf(stderr, "error: malformed block record in PCH file\n"); |
| 1637 | return std::string(); |
| 1638 | } |
| 1639 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1640 | |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1641 | default: |
| 1642 | if (Stream.SkipBlock()) { |
| 1643 | fprintf(stderr, "error: malformed block record in PCH file\n"); |
| 1644 | return std::string(); |
| 1645 | } |
| 1646 | break; |
| 1647 | } |
| 1648 | continue; |
| 1649 | } |
| 1650 | |
| 1651 | if (Code == llvm::bitc::END_BLOCK) { |
| 1652 | if (Stream.ReadBlockEnd()) { |
| 1653 | fprintf(stderr, "error: error at end of module block in PCH file\n"); |
| 1654 | return std::string(); |
| 1655 | } |
| 1656 | continue; |
| 1657 | } |
| 1658 | |
| 1659 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
| 1660 | Stream.ReadAbbrevRecord(); |
| 1661 | continue; |
| 1662 | } |
| 1663 | |
| 1664 | Record.clear(); |
| 1665 | const char *BlobStart = 0; |
| 1666 | unsigned BlobLen = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1667 | if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1668 | == pch::ORIGINAL_FILE_NAME) |
| 1669 | return std::string(BlobStart, BlobLen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1670 | } |
Douglas Gregor | b64c193 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1671 | |
| 1672 | return std::string(); |
| 1673 | } |
| 1674 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1675 | /// \brief Parse the record that corresponds to a LangOptions data |
| 1676 | /// structure. |
| 1677 | /// |
| 1678 | /// This routine compares the language options used to generate the |
| 1679 | /// PCH file against the language options set for the current |
| 1680 | /// compilation. For each option, we classify differences between the |
| 1681 | /// two compiler states as either "benign" or "important". Benign |
| 1682 | /// differences don't matter, and we accept them without complaint |
| 1683 | /// (and without modifying the language options). Differences between |
| 1684 | /// the states for important options cause the PCH file to be |
| 1685 | /// unusable, so we emit a warning and return true to indicate that |
| 1686 | /// there was an error. |
| 1687 | /// |
| 1688 | /// \returns true if the PCH file is unacceptable, false otherwise. |
| 1689 | bool PCHReader::ParseLanguageOptions( |
| 1690 | const llvm::SmallVectorImpl<uint64_t> &Record) { |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1691 | if (Listener) { |
| 1692 | LangOptions LangOpts; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1693 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1694 | #define PARSE_LANGOPT(Option) \ |
| 1695 | LangOpts.Option = Record[Idx]; \ |
| 1696 | ++Idx |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1697 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1698 | unsigned Idx = 0; |
| 1699 | PARSE_LANGOPT(Trigraphs); |
| 1700 | PARSE_LANGOPT(BCPLComment); |
| 1701 | PARSE_LANGOPT(DollarIdents); |
| 1702 | PARSE_LANGOPT(AsmPreprocessor); |
| 1703 | PARSE_LANGOPT(GNUMode); |
| 1704 | PARSE_LANGOPT(ImplicitInt); |
| 1705 | PARSE_LANGOPT(Digraphs); |
| 1706 | PARSE_LANGOPT(HexFloats); |
| 1707 | PARSE_LANGOPT(C99); |
| 1708 | PARSE_LANGOPT(Microsoft); |
| 1709 | PARSE_LANGOPT(CPlusPlus); |
| 1710 | PARSE_LANGOPT(CPlusPlus0x); |
| 1711 | PARSE_LANGOPT(CXXOperatorNames); |
| 1712 | PARSE_LANGOPT(ObjC1); |
| 1713 | PARSE_LANGOPT(ObjC2); |
| 1714 | PARSE_LANGOPT(ObjCNonFragileABI); |
| 1715 | PARSE_LANGOPT(PascalStrings); |
| 1716 | PARSE_LANGOPT(WritableStrings); |
| 1717 | PARSE_LANGOPT(LaxVectorConversions); |
Nate Begeman | b9e7e63 | 2009-06-25 23:01:11 +0000 | [diff] [blame] | 1718 | PARSE_LANGOPT(AltiVec); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1719 | PARSE_LANGOPT(Exceptions); |
| 1720 | PARSE_LANGOPT(NeXTRuntime); |
| 1721 | PARSE_LANGOPT(Freestanding); |
| 1722 | PARSE_LANGOPT(NoBuiltin); |
| 1723 | PARSE_LANGOPT(ThreadsafeStatics); |
Douglas Gregor | 972d954 | 2009-09-03 14:36:33 +0000 | [diff] [blame] | 1724 | PARSE_LANGOPT(POSIXThreads); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1725 | PARSE_LANGOPT(Blocks); |
| 1726 | PARSE_LANGOPT(EmitAllDecls); |
| 1727 | PARSE_LANGOPT(MathErrno); |
| 1728 | PARSE_LANGOPT(OverflowChecking); |
| 1729 | PARSE_LANGOPT(HeinousExtensions); |
| 1730 | PARSE_LANGOPT(Optimize); |
| 1731 | PARSE_LANGOPT(OptimizeSize); |
| 1732 | PARSE_LANGOPT(Static); |
| 1733 | PARSE_LANGOPT(PICLevel); |
| 1734 | PARSE_LANGOPT(GNUInline); |
| 1735 | PARSE_LANGOPT(NoInline); |
| 1736 | PARSE_LANGOPT(AccessControl); |
| 1737 | PARSE_LANGOPT(CharIsSigned); |
| 1738 | LangOpts.setGCMode((LangOptions::GCMode)Record[Idx]); |
| 1739 | ++Idx; |
| 1740 | LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx]); |
| 1741 | ++Idx; |
Daniel Dunbar | ab8e281 | 2009-09-21 04:16:19 +0000 | [diff] [blame] | 1742 | LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode) |
| 1743 | Record[Idx]); |
| 1744 | ++Idx; |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1745 | PARSE_LANGOPT(InstantiationDepth); |
Nate Begeman | b9e7e63 | 2009-06-25 23:01:11 +0000 | [diff] [blame] | 1746 | PARSE_LANGOPT(OpenCL); |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1747 | #undef PARSE_LANGOPT |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1748 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1749 | return Listener->ReadLanguageOptions(LangOpts); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1750 | } |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1751 | |
| 1752 | return false; |
| 1753 | } |
| 1754 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 1755 | void PCHReader::ReadComments(std::vector<SourceRange> &Comments) { |
| 1756 | Comments.resize(NumComments); |
| 1757 | std::copy(this->Comments, this->Comments + NumComments, |
| 1758 | Comments.begin()); |
| 1759 | } |
| 1760 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1761 | /// \brief Read and return the type at the given offset. |
| 1762 | /// |
| 1763 | /// This routine actually reads the record corresponding to the type |
| 1764 | /// at the given offset in the bitstream. It is a helper routine for |
| 1765 | /// GetType, which deals with reading type IDs. |
| 1766 | QualType PCHReader::ReadTypeRecord(uint64_t Offset) { |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1767 | // Keep track of where we are in the stream, then jump back there |
| 1768 | // after reading this type. |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 1769 | SavedStreamPosition SavedPosition(DeclsCursor); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1770 | |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 1771 | // Note that we are loading a type record. |
| 1772 | LoadingTypeOrDecl Loading(*this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1773 | |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 1774 | DeclsCursor.JumpToBit(Offset); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1775 | RecordData Record; |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 1776 | unsigned Code = DeclsCursor.ReadCode(); |
| 1777 | switch ((pch::TypeCode)DeclsCursor.ReadRecord(Code, Record)) { |
Douglas Gregor | 6d47396 | 2009-04-15 22:00:08 +0000 | [diff] [blame] | 1778 | case pch::TYPE_EXT_QUAL: { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1779 | assert(Record.size() == 2 && |
Douglas Gregor | 6d47396 | 2009-04-15 22:00:08 +0000 | [diff] [blame] | 1780 | "Incorrect encoding of extended qualifier type"); |
| 1781 | QualType Base = GetType(Record[0]); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1782 | Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]); |
| 1783 | return Context->getQualifiedType(Base, Quals); |
Douglas Gregor | 6d47396 | 2009-04-15 22:00:08 +0000 | [diff] [blame] | 1784 | } |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1785 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1786 | case pch::TYPE_FIXED_WIDTH_INT: { |
| 1787 | assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type"); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1788 | return Context->getFixedWidthIntType(Record[0], Record[1]); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | case pch::TYPE_COMPLEX: { |
| 1792 | assert(Record.size() == 1 && "Incorrect encoding of complex type"); |
| 1793 | QualType ElemType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1794 | return Context->getComplexType(ElemType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | case pch::TYPE_POINTER: { |
| 1798 | assert(Record.size() == 1 && "Incorrect encoding of pointer type"); |
| 1799 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1800 | return Context->getPointerType(PointeeType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | case pch::TYPE_BLOCK_POINTER: { |
| 1804 | assert(Record.size() == 1 && "Incorrect encoding of block pointer type"); |
| 1805 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1806 | return Context->getBlockPointerType(PointeeType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | case pch::TYPE_LVALUE_REFERENCE: { |
| 1810 | assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type"); |
| 1811 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1812 | return Context->getLValueReferenceType(PointeeType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | case pch::TYPE_RVALUE_REFERENCE: { |
| 1816 | assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type"); |
| 1817 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1818 | return Context->getRValueReferenceType(PointeeType); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | case pch::TYPE_MEMBER_POINTER: { |
| 1822 | assert(Record.size() == 1 && "Incorrect encoding of member pointer type"); |
| 1823 | QualType PointeeType = GetType(Record[0]); |
| 1824 | QualType ClassType = GetType(Record[1]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1825 | return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr()); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1828 | case pch::TYPE_CONSTANT_ARRAY: { |
| 1829 | QualType ElementType = GetType(Record[0]); |
| 1830 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 1831 | unsigned IndexTypeQuals = Record[2]; |
| 1832 | unsigned Idx = 3; |
| 1833 | llvm::APInt Size = ReadAPInt(Record, Idx); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1834 | return Context->getConstantArrayType(ElementType, Size, |
| 1835 | ASM, IndexTypeQuals); |
| 1836 | } |
| 1837 | |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1838 | case pch::TYPE_INCOMPLETE_ARRAY: { |
| 1839 | QualType ElementType = GetType(Record[0]); |
| 1840 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 1841 | unsigned IndexTypeQuals = Record[2]; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1842 | return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | case pch::TYPE_VARIABLE_ARRAY: { |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1846 | QualType ElementType = GetType(Record[0]); |
| 1847 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 1848 | unsigned IndexTypeQuals = Record[2]; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1849 | SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]); |
| 1850 | SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1851 | return Context->getVariableArrayType(ElementType, ReadTypeExpr(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1852 | ASM, IndexTypeQuals, |
| 1853 | SourceRange(LBLoc, RBLoc)); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | case pch::TYPE_VECTOR: { |
| 1857 | if (Record.size() != 2) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1858 | Error("incorrect encoding of vector type in PCH file"); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1859 | return QualType(); |
| 1860 | } |
| 1861 | |
| 1862 | QualType ElementType = GetType(Record[0]); |
| 1863 | unsigned NumElements = Record[1]; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1864 | return Context->getVectorType(ElementType, NumElements); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | case pch::TYPE_EXT_VECTOR: { |
| 1868 | if (Record.size() != 2) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1869 | Error("incorrect encoding of extended vector type in PCH file"); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1870 | return QualType(); |
| 1871 | } |
| 1872 | |
| 1873 | QualType ElementType = GetType(Record[0]); |
| 1874 | unsigned NumElements = Record[1]; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1875 | return Context->getExtVectorType(ElementType, NumElements); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | case pch::TYPE_FUNCTION_NO_PROTO: { |
| 1879 | if (Record.size() != 1) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1880 | Error("incorrect encoding of no-proto function type"); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1881 | return QualType(); |
| 1882 | } |
| 1883 | QualType ResultType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1884 | return Context->getFunctionNoProtoType(ResultType); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | case pch::TYPE_FUNCTION_PROTO: { |
| 1888 | QualType ResultType = GetType(Record[0]); |
| 1889 | unsigned Idx = 1; |
| 1890 | unsigned NumParams = Record[Idx++]; |
| 1891 | llvm::SmallVector<QualType, 16> ParamTypes; |
| 1892 | for (unsigned I = 0; I != NumParams; ++I) |
| 1893 | ParamTypes.push_back(GetType(Record[Idx++])); |
| 1894 | bool isVariadic = Record[Idx++]; |
| 1895 | unsigned Quals = Record[Idx++]; |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1896 | bool hasExceptionSpec = Record[Idx++]; |
| 1897 | bool hasAnyExceptionSpec = Record[Idx++]; |
| 1898 | unsigned NumExceptions = Record[Idx++]; |
| 1899 | llvm::SmallVector<QualType, 2> Exceptions; |
| 1900 | for (unsigned I = 0; I != NumExceptions; ++I) |
| 1901 | Exceptions.push_back(GetType(Record[Idx++])); |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1902 | return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1903 | isVariadic, Quals, hasExceptionSpec, |
| 1904 | hasAnyExceptionSpec, NumExceptions, |
| 1905 | Exceptions.data()); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | case pch::TYPE_TYPEDEF: |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1909 | assert(Record.size() == 1 && "incorrect encoding of typedef type"); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1910 | return Context->getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0]))); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1911 | |
| 1912 | case pch::TYPE_TYPEOF_EXPR: |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1913 | return Context->getTypeOfExprType(ReadTypeExpr()); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1914 | |
| 1915 | case pch::TYPE_TYPEOF: { |
| 1916 | if (Record.size() != 1) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1917 | Error("incorrect encoding of typeof(type) in PCH file"); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1918 | return QualType(); |
| 1919 | } |
| 1920 | QualType UnderlyingType = GetType(Record[0]); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1921 | return Context->getTypeOfType(UnderlyingType); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1922 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1923 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1924 | case pch::TYPE_DECLTYPE: |
| 1925 | return Context->getDecltypeType(ReadTypeExpr()); |
| 1926 | |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1927 | case pch::TYPE_RECORD: |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1928 | assert(Record.size() == 1 && "incorrect encoding of record type"); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1929 | return Context->getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0]))); |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1930 | |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1931 | case pch::TYPE_ENUM: |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1932 | assert(Record.size() == 1 && "incorrect encoding of enum type"); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 1933 | return Context->getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0]))); |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 1934 | |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 1935 | case pch::TYPE_ELABORATED: { |
| 1936 | assert(Record.size() == 2 && "incorrect encoding of elaborated type"); |
| 1937 | unsigned Tag = Record[1]; |
| 1938 | return Context->getElaboratedType(GetType(Record[0]), |
| 1939 | (ElaboratedType::TagKind) Tag); |
| 1940 | } |
| 1941 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1942 | case pch::TYPE_OBJC_INTERFACE: { |
Chris Lattner | c6fa445 | 2009-04-22 06:45:28 +0000 | [diff] [blame] | 1943 | unsigned Idx = 0; |
| 1944 | ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++])); |
| 1945 | unsigned NumProtos = Record[Idx++]; |
| 1946 | llvm::SmallVector<ObjCProtocolDecl*, 4> Protos; |
| 1947 | for (unsigned I = 0; I != NumProtos; ++I) |
| 1948 | Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++]))); |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1949 | return Context->getObjCInterfaceType(ItfD, Protos.data(), NumProtos); |
Chris Lattner | c6fa445 | 2009-04-22 06:45:28 +0000 | [diff] [blame] | 1950 | } |
Douglas Gregor | b4e715b | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 1951 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1952 | case pch::TYPE_OBJC_OBJECT_POINTER: { |
Chris Lattner | d7a3fcd | 2009-04-22 06:40:03 +0000 | [diff] [blame] | 1953 | unsigned Idx = 0; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1954 | QualType OIT = GetType(Record[Idx++]); |
Chris Lattner | d7a3fcd | 2009-04-22 06:40:03 +0000 | [diff] [blame] | 1955 | unsigned NumProtos = Record[Idx++]; |
| 1956 | llvm::SmallVector<ObjCProtocolDecl*, 4> Protos; |
| 1957 | for (unsigned I = 0; I != NumProtos; ++I) |
| 1958 | Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++]))); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1959 | return Context->getObjCObjectPointerType(OIT, Protos.data(), NumProtos); |
Chris Lattner | d7a3fcd | 2009-04-22 06:40:03 +0000 | [diff] [blame] | 1960 | } |
Argyrios Kyrtzidis | 24fab41 | 2009-09-29 19:42:55 +0000 | [diff] [blame] | 1961 | |
| 1962 | case pch::TYPE_OBJC_PROTOCOL_LIST: { |
| 1963 | unsigned Idx = 0; |
| 1964 | QualType OIT = GetType(Record[Idx++]); |
| 1965 | unsigned NumProtos = Record[Idx++]; |
| 1966 | llvm::SmallVector<ObjCProtocolDecl*, 4> Protos; |
| 1967 | for (unsigned I = 0; I != NumProtos; ++I) |
| 1968 | Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++]))); |
| 1969 | return Context->getObjCProtocolListType(OIT, Protos.data(), NumProtos); |
| 1970 | } |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1971 | |
| 1972 | case pch::TYPE_SUBST_TEMPLATE_TYPE_PARM: { |
| 1973 | unsigned Idx = 0; |
| 1974 | QualType Parm = GetType(Record[Idx++]); |
| 1975 | QualType Replacement = GetType(Record[Idx++]); |
| 1976 | return |
| 1977 | Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm), |
| 1978 | Replacement); |
| 1979 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1980 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1981 | // Suppress a GCC warning |
| 1982 | return QualType(); |
| 1983 | } |
| 1984 | |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 1985 | namespace { |
| 1986 | |
| 1987 | class TypeLocReader : public TypeLocVisitor<TypeLocReader> { |
| 1988 | PCHReader &Reader; |
| 1989 | const PCHReader::RecordData &Record; |
| 1990 | unsigned &Idx; |
| 1991 | |
| 1992 | public: |
| 1993 | TypeLocReader(PCHReader &Reader, const PCHReader::RecordData &Record, |
| 1994 | unsigned &Idx) |
| 1995 | : Reader(Reader), Record(Record), Idx(Idx) { } |
| 1996 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1997 | // We want compile-time assurance that we've enumerated all of |
| 1998 | // these, so unfortunately we have to declare them first, then |
| 1999 | // define them out-of-line. |
| 2000 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2001 | #define TYPELOC(CLASS, PARENT) \ |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2002 | void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2003 | #include "clang/AST/TypeLocNodes.def" |
| 2004 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2005 | void VisitFunctionTypeLoc(FunctionTypeLoc); |
| 2006 | void VisitArrayTypeLoc(ArrayTypeLoc); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2007 | }; |
| 2008 | |
| 2009 | } |
| 2010 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2011 | void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2012 | // nothing to do |
| 2013 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2014 | void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
| 2015 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2016 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2017 | void TypeLocReader::VisitFixedWidthIntTypeLoc(FixedWidthIntTypeLoc TL) { |
| 2018 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2019 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2020 | void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { |
| 2021 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2022 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2023 | void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 2024 | TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2025 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2026 | void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 2027 | TL.setCaretLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2028 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2029 | void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 2030 | TL.setAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2031 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2032 | void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 2033 | TL.setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2034 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2035 | void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 2036 | TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2037 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2038 | void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 2039 | TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2040 | TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2041 | if (Record[Idx++]) |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2042 | TL.setSizeExpr(Reader.ReadDeclExpr()); |
Douglas Gregor | 61d60ee | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 2043 | else |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2044 | TL.setSizeExpr(0); |
| 2045 | } |
| 2046 | void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { |
| 2047 | VisitArrayTypeLoc(TL); |
| 2048 | } |
| 2049 | void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { |
| 2050 | VisitArrayTypeLoc(TL); |
| 2051 | } |
| 2052 | void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { |
| 2053 | VisitArrayTypeLoc(TL); |
| 2054 | } |
| 2055 | void TypeLocReader::VisitDependentSizedArrayTypeLoc( |
| 2056 | DependentSizedArrayTypeLoc TL) { |
| 2057 | VisitArrayTypeLoc(TL); |
| 2058 | } |
| 2059 | void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( |
| 2060 | DependentSizedExtVectorTypeLoc TL) { |
| 2061 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2062 | } |
| 2063 | void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { |
| 2064 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2065 | } |
| 2066 | void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { |
| 2067 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2068 | } |
| 2069 | void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { |
| 2070 | TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2071 | TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2072 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) { |
| 2073 | TL.setArg(i, cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++]))); |
| 2074 | } |
| 2075 | } |
| 2076 | void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { |
| 2077 | VisitFunctionTypeLoc(TL); |
| 2078 | } |
| 2079 | void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { |
| 2080 | VisitFunctionTypeLoc(TL); |
| 2081 | } |
| 2082 | void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 2083 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2084 | } |
| 2085 | void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 2086 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2087 | } |
| 2088 | void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 2089 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2090 | } |
| 2091 | void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 2092 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2093 | } |
| 2094 | void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { |
| 2095 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2096 | } |
| 2097 | void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { |
| 2098 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2099 | } |
| 2100 | void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 2101 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2102 | } |
| 2103 | void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 2104 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2105 | } |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2106 | void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( |
| 2107 | SubstTemplateTypeParmTypeLoc TL) { |
| 2108 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2109 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2110 | void TypeLocReader::VisitTemplateSpecializationTypeLoc( |
| 2111 | TemplateSpecializationTypeLoc TL) { |
| 2112 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2113 | } |
| 2114 | void TypeLocReader::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) { |
| 2115 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2116 | } |
| 2117 | void TypeLocReader::VisitTypenameTypeLoc(TypenameTypeLoc TL) { |
| 2118 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2119 | } |
| 2120 | void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 2121 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2122 | } |
| 2123 | void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 2124 | TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2125 | } |
| 2126 | void TypeLocReader::VisitObjCProtocolListTypeLoc(ObjCProtocolListTypeLoc TL) { |
| 2127 | TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2128 | TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2129 | for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) |
| 2130 | TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | a1ee0c5 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
| 2133 | DeclaratorInfo *PCHReader::GetDeclaratorInfo(const RecordData &Record, |
| 2134 | unsigned &Idx) { |
| 2135 | QualType InfoTy = GetType(Record[Idx++]); |
| 2136 | if (InfoTy.isNull()) |
| 2137 | return 0; |
| 2138 | |
| 2139 | DeclaratorInfo *DInfo = getContext()->CreateDeclaratorInfo(InfoTy); |
| 2140 | TypeLocReader TLR(*this, Record, Idx); |
| 2141 | for (TypeLoc TL = DInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc()) |
| 2142 | TLR.Visit(TL); |
| 2143 | return DInfo; |
| 2144 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2145 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 2146 | QualType PCHReader::GetType(pch::TypeID ID) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2147 | unsigned FastQuals = ID & Qualifiers::FastMask; |
| 2148 | unsigned Index = ID >> Qualifiers::FastWidth; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2149 | |
| 2150 | if (Index < pch::NUM_PREDEF_TYPE_IDS) { |
| 2151 | QualType T; |
| 2152 | switch ((pch::PredefinedTypeIDs)Index) { |
| 2153 | case pch::PREDEF_TYPE_NULL_ID: return QualType(); |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2154 | case pch::PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break; |
| 2155 | case pch::PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2156 | |
| 2157 | case pch::PREDEF_TYPE_CHAR_U_ID: |
| 2158 | case pch::PREDEF_TYPE_CHAR_S_ID: |
| 2159 | // FIXME: Check that the signedness of CharTy is correct! |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2160 | T = Context->CharTy; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2161 | break; |
| 2162 | |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2163 | case pch::PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break; |
| 2164 | case pch::PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break; |
| 2165 | case pch::PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break; |
| 2166 | case pch::PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break; |
| 2167 | case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2168 | case pch::PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2169 | case pch::PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break; |
| 2170 | case pch::PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break; |
| 2171 | case pch::PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break; |
| 2172 | case pch::PREDEF_TYPE_INT_ID: T = Context->IntTy; break; |
| 2173 | case pch::PREDEF_TYPE_LONG_ID: T = Context->LongTy; break; |
| 2174 | case pch::PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2175 | case pch::PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break; |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2176 | case pch::PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break; |
| 2177 | case pch::PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break; |
| 2178 | case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break; |
| 2179 | case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break; |
| 2180 | case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2181 | case pch::PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2182 | case pch::PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break; |
| 2183 | case pch::PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break; |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2184 | case pch::PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break; |
| 2185 | case pch::PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
| 2188 | assert(!T.isNull() && "Unknown predefined type"); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2189 | return T.withFastQualifiers(FastQuals); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | Index -= pch::NUM_PREDEF_TYPE_IDS; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2193 | //assert(Index < TypesLoaded.size() && "Type index out-of-range"); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2194 | if (TypesLoaded[Index].isNull()) |
| 2195 | TypesLoaded[Index] = ReadTypeRecord(TypeOffsets[Index]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2196 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2197 | return TypesLoaded[Index].withFastQualifiers(FastQuals); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 2200 | Decl *PCHReader::GetDecl(pch::DeclID ID) { |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2201 | if (ID == 0) |
| 2202 | return 0; |
| 2203 | |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2204 | if (ID > DeclsLoaded.size()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2205 | Error("declaration ID out-of-range for PCH file"); |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2206 | return 0; |
| 2207 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2208 | |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2209 | unsigned Index = ID - 1; |
| 2210 | if (!DeclsLoaded[Index]) |
| 2211 | ReadDeclRecord(DeclOffsets[Index], Index); |
| 2212 | |
| 2213 | return DeclsLoaded[Index]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
Chris Lattner | 887e2b3 | 2009-04-27 05:46:25 +0000 | [diff] [blame] | 2216 | /// \brief Resolve the offset of a statement into a statement. |
| 2217 | /// |
| 2218 | /// This operation will read a new statement from the external |
| 2219 | /// source each time it is called, and is meant to be used via a |
| 2220 | /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). |
| 2221 | Stmt *PCHReader::GetDeclStmt(uint64_t Offset) { |
Chris Lattner | da93061 | 2009-04-27 05:58:23 +0000 | [diff] [blame] | 2222 | // Since we know tha this statement is part of a decl, make sure to use the |
| 2223 | // decl cursor to read it. |
| 2224 | DeclsCursor.JumpToBit(Offset); |
| 2225 | return ReadStmt(DeclsCursor); |
Douglas Gregor | 250fc9c | 2009-04-18 00:07:54 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2228 | bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC, |
Douglas Gregor | 8038d51 | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 2229 | llvm::SmallVectorImpl<pch::DeclID> &Decls) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2230 | assert(DC->hasExternalLexicalStorage() && |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2231 | "DeclContext has no lexical decls in storage"); |
| 2232 | uint64_t Offset = DeclContextOffsets[DC].first; |
| 2233 | assert(Offset && "DeclContext has no lexical decls in storage"); |
| 2234 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 2235 | // Keep track of where we are in the stream, then jump back there |
| 2236 | // after reading this context. |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 2237 | SavedStreamPosition SavedPosition(DeclsCursor); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 2238 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2239 | // Load the record containing all of the declarations lexically in |
| 2240 | // this context. |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 2241 | DeclsCursor.JumpToBit(Offset); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2242 | RecordData Record; |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 2243 | unsigned Code = DeclsCursor.ReadCode(); |
| 2244 | unsigned RecCode = DeclsCursor.ReadRecord(Code, Record); |
Douglas Gregor | 6a2bfb2 | 2009-04-15 18:43:11 +0000 | [diff] [blame] | 2245 | (void)RecCode; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2246 | assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block"); |
| 2247 | |
| 2248 | // Load all of the declaration IDs |
| 2249 | Decls.clear(); |
| 2250 | Decls.insert(Decls.end(), Record.begin(), Record.end()); |
Douglas Gregor | 2512308 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 2251 | ++NumLexicalDeclContextsRead; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2252 | return false; |
| 2253 | } |
| 2254 | |
| 2255 | bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC, |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 2256 | llvm::SmallVectorImpl<VisibleDeclaration> &Decls) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2257 | assert(DC->hasExternalVisibleStorage() && |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2258 | "DeclContext has no visible decls in storage"); |
| 2259 | uint64_t Offset = DeclContextOffsets[DC].second; |
| 2260 | assert(Offset && "DeclContext has no visible decls in storage"); |
| 2261 | |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 2262 | // Keep track of where we are in the stream, then jump back there |
| 2263 | // after reading this context. |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 2264 | SavedStreamPosition SavedPosition(DeclsCursor); |
Douglas Gregor | 0b74891 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 2265 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2266 | // Load the record containing all of the declarations visible in |
| 2267 | // this context. |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 2268 | DeclsCursor.JumpToBit(Offset); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2269 | RecordData Record; |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 2270 | unsigned Code = DeclsCursor.ReadCode(); |
| 2271 | unsigned RecCode = DeclsCursor.ReadRecord(Code, Record); |
Douglas Gregor | 6a2bfb2 | 2009-04-15 18:43:11 +0000 | [diff] [blame] | 2272 | (void)RecCode; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2273 | assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block"); |
| 2274 | if (Record.size() == 0) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2275 | return false; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2276 | |
| 2277 | Decls.clear(); |
| 2278 | |
| 2279 | unsigned Idx = 0; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2280 | while (Idx < Record.size()) { |
| 2281 | Decls.push_back(VisibleDeclaration()); |
| 2282 | Decls.back().Name = ReadDeclarationName(Record, Idx); |
| 2283 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2284 | unsigned Size = Record[Idx++]; |
Chris Lattner | c47be9e | 2009-04-27 07:35:40 +0000 | [diff] [blame] | 2285 | llvm::SmallVector<unsigned, 4> &LoadedDecls = Decls.back().Declarations; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2286 | LoadedDecls.reserve(Size); |
| 2287 | for (unsigned I = 0; I < Size; ++I) |
| 2288 | LoadedDecls.push_back(Record[Idx++]); |
| 2289 | } |
| 2290 | |
Douglas Gregor | 2512308 | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 2291 | ++NumVisibleDeclContextsRead; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2292 | return false; |
| 2293 | } |
| 2294 | |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 2295 | void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) { |
Douglas Gregor | 0af2ca4 | 2009-04-22 19:09:20 +0000 | [diff] [blame] | 2296 | this->Consumer = Consumer; |
| 2297 | |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 2298 | if (!Consumer) |
| 2299 | return; |
| 2300 | |
| 2301 | for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) { |
Daniel Dunbar | 04a0b50 | 2009-09-17 03:06:44 +0000 | [diff] [blame] | 2302 | // Force deserialization of this decl, which will cause it to be passed to |
| 2303 | // the consumer (or queued). |
| 2304 | GetDecl(ExternalDefinitions[I]); |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 2305 | } |
Douglas Gregor | c62a2fe | 2009-04-25 00:41:30 +0000 | [diff] [blame] | 2306 | |
| 2307 | for (unsigned I = 0, N = InterestingDecls.size(); I != N; ++I) { |
| 2308 | DeclGroupRef DG(InterestingDecls[I]); |
| 2309 | Consumer->HandleTopLevelDecl(DG); |
| 2310 | } |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2313 | void PCHReader::PrintStats() { |
| 2314 | std::fprintf(stderr, "*** PCH Statistics:\n"); |
| 2315 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2316 | unsigned NumTypesLoaded |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2317 | = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2318 | QualType()); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2319 | unsigned NumDeclsLoaded |
| 2320 | = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), |
| 2321 | (Decl *)0); |
| 2322 | unsigned NumIdentifiersLoaded |
| 2323 | = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), |
| 2324 | IdentifiersLoaded.end(), |
| 2325 | (IdentifierInfo *)0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2326 | unsigned NumSelectorsLoaded |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2327 | = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), |
| 2328 | SelectorsLoaded.end(), |
| 2329 | Selector()); |
Douglas Gregor | 2d41cc1 | 2009-04-13 20:50:16 +0000 | [diff] [blame] | 2330 | |
Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 2331 | std::fprintf(stderr, " %u stat cache hits\n", NumStatHits); |
| 2332 | std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 2333 | if (TotalNumSLocEntries) |
| 2334 | std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", |
| 2335 | NumSLocEntriesRead, TotalNumSLocEntries, |
| 2336 | ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2337 | if (!TypesLoaded.empty()) |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2338 | std::fprintf(stderr, " %u/%u types read (%f%%)\n", |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2339 | NumTypesLoaded, (unsigned)TypesLoaded.size(), |
| 2340 | ((float)NumTypesLoaded/TypesLoaded.size() * 100)); |
| 2341 | if (!DeclsLoaded.empty()) |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2342 | std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", |
Douglas Gregor | 8f5dc7f | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2343 | NumDeclsLoaded, (unsigned)DeclsLoaded.size(), |
| 2344 | ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2345 | if (!IdentifiersLoaded.empty()) |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2346 | std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2347 | NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), |
| 2348 | ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2349 | if (TotalNumSelectors) |
| 2350 | std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", |
| 2351 | NumSelectorsLoaded, TotalNumSelectors, |
| 2352 | ((float)NumSelectorsLoaded/TotalNumSelectors * 100)); |
| 2353 | if (TotalNumStatements) |
| 2354 | std::fprintf(stderr, " %u/%u statements read (%f%%)\n", |
| 2355 | NumStatementsRead, TotalNumStatements, |
| 2356 | ((float)NumStatementsRead/TotalNumStatements * 100)); |
| 2357 | if (TotalNumMacros) |
| 2358 | std::fprintf(stderr, " %u/%u macros read (%f%%)\n", |
| 2359 | NumMacrosRead, TotalNumMacros, |
| 2360 | ((float)NumMacrosRead/TotalNumMacros * 100)); |
| 2361 | if (TotalLexicalDeclContexts) |
| 2362 | std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", |
| 2363 | NumLexicalDeclContextsRead, TotalLexicalDeclContexts, |
| 2364 | ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts |
| 2365 | * 100)); |
| 2366 | if (TotalVisibleDeclContexts) |
| 2367 | std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", |
| 2368 | NumVisibleDeclContextsRead, TotalVisibleDeclContexts, |
| 2369 | ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts |
| 2370 | * 100)); |
| 2371 | if (TotalSelectorsInMethodPool) { |
| 2372 | std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", |
| 2373 | NumMethodPoolSelectorsRead, TotalSelectorsInMethodPool, |
| 2374 | ((float)NumMethodPoolSelectorsRead/TotalSelectorsInMethodPool |
| 2375 | * 100)); |
| 2376 | std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses); |
| 2377 | } |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2378 | std::fprintf(stderr, "\n"); |
| 2379 | } |
| 2380 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2381 | void PCHReader::InitializeSema(Sema &S) { |
| 2382 | SemaObj = &S; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2383 | S.ExternalSource = this; |
| 2384 | |
Douglas Gregor | 6cfc1a8 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 2385 | // Makes sure any declarations that were deserialized "too early" |
| 2386 | // still get added to the identifier's declaration chains. |
| 2387 | for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) { |
| 2388 | SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I])); |
| 2389 | SemaObj->IdResolver.AddDecl(PreloadedDecls[I]); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2390 | } |
Douglas Gregor | 6cfc1a8 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 2391 | PreloadedDecls.clear(); |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 2392 | |
| 2393 | // If there were any tentative definitions, deserialize them and add |
| 2394 | // them to Sema's table of tentative definitions. |
| 2395 | for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { |
| 2396 | VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I])); |
| 2397 | SemaObj->TentativeDefinitions[Var->getDeclName()] = Var; |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 2398 | SemaObj->TentativeDefinitionList.push_back(Var->getDeclName()); |
Douglas Gregor | 4c0e86b | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 2399 | } |
Douglas Gregor | 14c22f2 | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 2400 | |
| 2401 | // If there were any locally-scoped external declarations, |
| 2402 | // deserialize them and add them to Sema's table of locally-scoped |
| 2403 | // external declarations. |
| 2404 | for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) { |
| 2405 | NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I])); |
| 2406 | SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D; |
| 2407 | } |
Douglas Gregor | b81c170 | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 2408 | |
| 2409 | // If there were any ext_vector type declarations, deserialize them |
| 2410 | // and add them to Sema's vector of such declarations. |
| 2411 | for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) |
| 2412 | SemaObj->ExtVectorDecls.push_back( |
| 2413 | cast<TypedefDecl>(GetDecl(ExtVectorDecls[I]))); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
| 2416 | IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) { |
| 2417 | // Try to find this name within our on-disk hash table |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2418 | PCHIdentifierLookupTable *IdTable |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2419 | = (PCHIdentifierLookupTable *)IdentifierLookupTable; |
| 2420 | std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart); |
| 2421 | PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key); |
| 2422 | if (Pos == IdTable->end()) |
| 2423 | return 0; |
| 2424 | |
| 2425 | // Dereferencing the iterator has the effect of building the |
| 2426 | // IdentifierInfo node and populating it with the various |
| 2427 | // declarations it needs. |
| 2428 | return *Pos; |
| 2429 | } |
| 2430 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2431 | std::pair<ObjCMethodList, ObjCMethodList> |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2432 | PCHReader::ReadMethodPool(Selector Sel) { |
| 2433 | if (!MethodPoolLookupTable) |
| 2434 | return std::pair<ObjCMethodList, ObjCMethodList>(); |
| 2435 | |
| 2436 | // Try to find this selector within our on-disk hash table. |
| 2437 | PCHMethodPoolLookupTable *PoolTable |
| 2438 | = (PCHMethodPoolLookupTable*)MethodPoolLookupTable; |
| 2439 | PCHMethodPoolLookupTable::iterator Pos = PoolTable->find(Sel); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2440 | if (Pos == PoolTable->end()) { |
| 2441 | ++NumMethodPoolMisses; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2442 | return std::pair<ObjCMethodList, ObjCMethodList>();; |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2443 | } |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2444 | |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2445 | ++NumMethodPoolSelectorsRead; |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2446 | return *Pos; |
| 2447 | } |
| 2448 | |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2449 | void PCHReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) { |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2450 | assert(ID && "Non-zero identifier ID required"); |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2451 | assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2452 | IdentifiersLoaded[ID - 1] = II; |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 2455 | /// \brief Set the globally-visible declarations associated with the given |
| 2456 | /// identifier. |
| 2457 | /// |
| 2458 | /// 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] | 2459 | /// 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] | 2460 | /// them. |
| 2461 | /// |
| 2462 | /// \param II an IdentifierInfo that refers to one or more globally-visible |
| 2463 | /// declarations. |
| 2464 | /// |
| 2465 | /// \param DeclIDs the set of declaration IDs with the name @p II that are |
| 2466 | /// visible at global scope. |
| 2467 | /// |
| 2468 | /// \param Nonrecursive should be true to indicate that the caller knows that |
| 2469 | /// this call is non-recursive, and therefore the globally-visible declarations |
| 2470 | /// will not be placed onto the pending queue. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2471 | void |
| 2472 | PCHReader::SetGloballyVisibleDecls(IdentifierInfo *II, |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 2473 | const llvm::SmallVectorImpl<uint32_t> &DeclIDs, |
| 2474 | bool Nonrecursive) { |
| 2475 | if (CurrentlyLoadingTypeOrDecl && !Nonrecursive) { |
| 2476 | PendingIdentifierInfos.push_back(PendingIdentifierInfo()); |
| 2477 | PendingIdentifierInfo &PII = PendingIdentifierInfos.back(); |
| 2478 | PII.II = II; |
| 2479 | for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) |
| 2480 | PII.DeclIDs.push_back(DeclIDs[I]); |
| 2481 | return; |
| 2482 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2483 | |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 2484 | for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { |
| 2485 | NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); |
| 2486 | if (SemaObj) { |
| 2487 | // Introduce this declaration into the translation-unit scope |
| 2488 | // and add it to the declaration chain for this identifier, so |
| 2489 | // that (unqualified) name lookup will find it. |
| 2490 | SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D)); |
| 2491 | SemaObj->IdResolver.AddDeclToIdentifierChain(II, D); |
| 2492 | } else { |
| 2493 | // Queue this declaration so that it will be added to the |
| 2494 | // translation unit scope and identifier's declaration chain |
| 2495 | // once a Sema object is known. |
| 2496 | PreloadedDecls.push_back(D); |
| 2497 | } |
| 2498 | } |
| 2499 | } |
| 2500 | |
Chris Lattner | 7356a31 | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 2501 | IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) { |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2502 | if (ID == 0) |
| 2503 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2504 | |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2505 | if (!IdentifierTableData || IdentifiersLoaded.empty()) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2506 | Error("no identifier table in PCH file"); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2507 | return 0; |
| 2508 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2509 | |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2510 | assert(PP && "Forgot to set Preprocessor ?"); |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2511 | if (!IdentifiersLoaded[ID - 1]) { |
| 2512 | uint32_t Offset = IdentifierOffsets[ID - 1]; |
Douglas Gregor | 17e1c5e | 2009-04-25 21:21:38 +0000 | [diff] [blame] | 2513 | const char *Str = IdentifierTableData + Offset; |
Douglas Gregor | d6595a4 | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 2514 | |
Douglas Gregor | 02fc751 | 2009-04-28 20:01:51 +0000 | [diff] [blame] | 2515 | // All of the strings in the PCH file are preceded by a 16-bit |
| 2516 | // length. Extract that 16-bit length to avoid having to execute |
| 2517 | // strlen(). |
| 2518 | const char *StrLenPtr = Str - 2; |
| 2519 | unsigned StrLen = (((unsigned) StrLenPtr[0]) |
| 2520 | | (((unsigned) StrLenPtr[1]) << 8)) - 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2521 | IdentifiersLoaded[ID - 1] |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2522 | = &PP->getIdentifierTable().get(Str, Str + StrLen); |
Douglas Gregor | afaf308 | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2523 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2524 | |
Douglas Gregor | 2b3a5a8 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2525 | return IdentifiersLoaded[ID - 1]; |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 2528 | void PCHReader::ReadSLocEntry(unsigned ID) { |
| 2529 | ReadSLocEntryRecord(ID); |
| 2530 | } |
| 2531 | |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 2532 | Selector PCHReader::DecodeSelector(unsigned ID) { |
| 2533 | if (ID == 0) |
| 2534 | return Selector(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2535 | |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2536 | if (!MethodPoolLookupTableData) |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 2537 | return Selector(); |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2538 | |
| 2539 | if (ID > TotalNumSelectors) { |
Douglas Gregor | a02b147 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2540 | Error("selector ID out of range in PCH file"); |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 2541 | return Selector(); |
| 2542 | } |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2543 | |
| 2544 | unsigned Index = ID - 1; |
| 2545 | if (SelectorsLoaded[Index].getAsOpaquePtr() == 0) { |
| 2546 | // Load this selector from the selector table. |
| 2547 | // FIXME: endianness portability issues with SelectorOffsets table |
| 2548 | PCHMethodPoolLookupTrait Trait(*this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2549 | SelectorsLoaded[Index] |
Douglas Gregor | 83941df | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2550 | = Trait.ReadKey(MethodPoolLookupTableData + SelectorOffsets[Index], 0); |
| 2551 | } |
| 2552 | |
| 2553 | return SelectorsLoaded[Index]; |
Steve Naroff | 90cd1bb | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2556 | DeclarationName |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2557 | PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) { |
| 2558 | DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; |
| 2559 | switch (Kind) { |
| 2560 | case DeclarationName::Identifier: |
| 2561 | return DeclarationName(GetIdentifierInfo(Record, Idx)); |
| 2562 | |
| 2563 | case DeclarationName::ObjCZeroArgSelector: |
| 2564 | case DeclarationName::ObjCOneArgSelector: |
| 2565 | case DeclarationName::ObjCMultiArgSelector: |
Steve Naroff | a7503a7 | 2009-04-23 15:15:40 +0000 | [diff] [blame] | 2566 | return DeclarationName(GetSelector(Record, Idx)); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2567 | |
| 2568 | case DeclarationName::CXXConstructorName: |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2569 | return Context->DeclarationNames.getCXXConstructorName( |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2570 | Context->getCanonicalType(GetType(Record[Idx++]))); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2571 | |
| 2572 | case DeclarationName::CXXDestructorName: |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2573 | return Context->DeclarationNames.getCXXDestructorName( |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2574 | Context->getCanonicalType(GetType(Record[Idx++]))); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2575 | |
| 2576 | case DeclarationName::CXXConversionFunctionName: |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2577 | return Context->DeclarationNames.getCXXConversionFunctionName( |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2578 | Context->getCanonicalType(GetType(Record[Idx++]))); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2579 | |
| 2580 | case DeclarationName::CXXOperatorName: |
Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2581 | return Context->DeclarationNames.getCXXOperatorName( |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2582 | (OverloadedOperatorKind)Record[Idx++]); |
| 2583 | |
| 2584 | case DeclarationName::CXXUsingDirective: |
| 2585 | return DeclarationName::getUsingDirectiveName(); |
| 2586 | } |
| 2587 | |
| 2588 | // Required to silence GCC warning |
| 2589 | return DeclarationName(); |
| 2590 | } |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 2591 | |
Douglas Gregor | 0a2b45e | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 2592 | /// \brief Read an integral value |
| 2593 | llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { |
| 2594 | unsigned BitWidth = Record[Idx++]; |
| 2595 | unsigned NumWords = llvm::APInt::getNumWords(BitWidth); |
| 2596 | llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); |
| 2597 | Idx += NumWords; |
| 2598 | return Result; |
| 2599 | } |
| 2600 | |
| 2601 | /// \brief Read a signed integral value |
| 2602 | llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { |
| 2603 | bool isUnsigned = Record[Idx++]; |
| 2604 | return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); |
| 2605 | } |
| 2606 | |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 2607 | /// \brief Read a floating-point value |
| 2608 | llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) { |
Douglas Gregor | 17fc223 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 2609 | return llvm::APFloat(ReadAPInt(Record, Idx)); |
| 2610 | } |
| 2611 | |
Douglas Gregor | 68a2eb0 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 2612 | // \brief Read a string |
| 2613 | std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) { |
| 2614 | unsigned Len = Record[Idx++]; |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2615 | std::string Result(Record.data() + Idx, Record.data() + Idx + Len); |
Douglas Gregor | 68a2eb0 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 2616 | Idx += Len; |
| 2617 | return Result; |
| 2618 | } |
| 2619 | |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 2620 | DiagnosticBuilder PCHReader::Diag(unsigned DiagID) { |
Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 2621 | return Diag(SourceLocation(), DiagID); |
| 2622 | } |
| 2623 | |
| 2624 | DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2625 | return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID); |
Douglas Gregor | 0a0428e | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 2626 | } |
Douglas Gregor | 025452f | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 2627 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2628 | /// \brief Retrieve the identifier table associated with the |
| 2629 | /// preprocessor. |
| 2630 | IdentifierTable &PCHReader::getIdentifierTable() { |
Argyrios Kyrtzidis | 11e5110 | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2631 | assert(PP && "Forgot to set Preprocessor ?"); |
| 2632 | return PP->getIdentifierTable(); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
Douglas Gregor | 025452f | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 2635 | /// \brief Record that the given ID maps to the given switch-case |
| 2636 | /// statement. |
| 2637 | void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { |
| 2638 | assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID"); |
| 2639 | SwitchCaseStmts[ID] = SC; |
| 2640 | } |
| 2641 | |
| 2642 | /// \brief Retrieve the switch-case statement with the given ID. |
| 2643 | SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) { |
| 2644 | assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID"); |
| 2645 | return SwitchCaseStmts[ID]; |
| 2646 | } |
Douglas Gregor | 1de05fe | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 2647 | |
| 2648 | /// \brief Record that the given label statement has been |
| 2649 | /// deserialized and has the given ID. |
| 2650 | void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2651 | assert(LabelStmts.find(ID) == LabelStmts.end() && |
Douglas Gregor | 1de05fe | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 2652 | "Deserialized label twice"); |
| 2653 | LabelStmts[ID] = S; |
| 2654 | |
| 2655 | // If we've already seen any goto statements that point to this |
| 2656 | // label, resolve them now. |
| 2657 | typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter; |
| 2658 | std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID); |
| 2659 | for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto) |
| 2660 | Goto->second->setLabel(S); |
| 2661 | UnresolvedGotoStmts.erase(Gotos.first, Gotos.second); |
Douglas Gregor | 7d5c2f2 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 2662 | |
| 2663 | // If we've already seen any address-label statements that point to |
| 2664 | // this label, resolve them now. |
| 2665 | typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2666 | std::pair<AddrLabelIter, AddrLabelIter> AddrLabels |
Douglas Gregor | 7d5c2f2 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 2667 | = UnresolvedAddrLabelExprs.equal_range(ID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2668 | for (AddrLabelIter AddrLabel = AddrLabels.first; |
Douglas Gregor | 7d5c2f2 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 2669 | AddrLabel != AddrLabels.second; ++AddrLabel) |
| 2670 | AddrLabel->second->setLabel(S); |
| 2671 | UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second); |
Douglas Gregor | 1de05fe | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | /// \brief Set the label of the given statement to the label |
| 2675 | /// identified by ID. |
| 2676 | /// |
| 2677 | /// Depending on the order in which the label and other statements |
| 2678 | /// referencing that label occur, this operation may complete |
| 2679 | /// immediately (updating the statement) or it may queue the |
| 2680 | /// statement to be back-patched later. |
| 2681 | void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) { |
| 2682 | std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID); |
| 2683 | if (Label != LabelStmts.end()) { |
| 2684 | // We've already seen this label, so set the label of the goto and |
| 2685 | // we're done. |
| 2686 | S->setLabel(Label->second); |
| 2687 | } else { |
| 2688 | // We haven't seen this label yet, so add this goto to the set of |
| 2689 | // unresolved goto statements. |
| 2690 | UnresolvedGotoStmts.insert(std::make_pair(ID, S)); |
| 2691 | } |
| 2692 | } |
Douglas Gregor | 7d5c2f2 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 2693 | |
| 2694 | /// \brief Set the label of the given expression to the label |
| 2695 | /// identified by ID. |
| 2696 | /// |
| 2697 | /// Depending on the order in which the label and other statements |
| 2698 | /// referencing that label occur, this operation may complete |
| 2699 | /// immediately (updating the statement) or it may queue the |
| 2700 | /// statement to be back-patched later. |
| 2701 | void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) { |
| 2702 | std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID); |
| 2703 | if (Label != LabelStmts.end()) { |
| 2704 | // We've already seen this label, so set the label of the |
| 2705 | // label-address expression and we're done. |
| 2706 | S->setLabel(Label->second); |
| 2707 | } else { |
| 2708 | // We haven't seen this label yet, so add this label-address |
| 2709 | // expression to the set of unresolved label-address expressions. |
| 2710 | UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S)); |
| 2711 | } |
| 2712 | } |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 2713 | |
| 2714 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2715 | PCHReader::LoadingTypeOrDecl::LoadingTypeOrDecl(PCHReader &Reader) |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 2716 | : Reader(Reader), Parent(Reader.CurrentlyLoadingTypeOrDecl) { |
| 2717 | Reader.CurrentlyLoadingTypeOrDecl = this; |
| 2718 | } |
| 2719 | |
| 2720 | PCHReader::LoadingTypeOrDecl::~LoadingTypeOrDecl() { |
| 2721 | if (!Parent) { |
| 2722 | // If any identifiers with corresponding top-level declarations have |
| 2723 | // been loaded, load those declarations now. |
| 2724 | while (!Reader.PendingIdentifierInfos.empty()) { |
| 2725 | Reader.SetGloballyVisibleDecls(Reader.PendingIdentifierInfos.front().II, |
| 2726 | Reader.PendingIdentifierInfos.front().DeclIDs, |
| 2727 | true); |
| 2728 | Reader.PendingIdentifierInfos.pop_front(); |
| 2729 | } |
| 2730 | } |
| 2731 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2732 | Reader.CurrentlyLoadingTypeOrDecl = Parent; |
Douglas Gregor | d89275b | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 2733 | } |