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