blob: 982c4cbee886bd411ab689fe920866d6d713db78 [file] [log] [blame]
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001//===- ASTReader.cpp - AST File Reader ------------------------------------===//
Guy Benyei11169dd2012-12-18 14:30:41 +00002//
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 ASTReader class, which reads AST files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Serialization/ASTReader.h"
15#include "ASTCommon.h"
16#include "ASTReaderInternals.h"
17#include "clang/AST/ASTConsumer.h"
18#include "clang/AST/ASTContext.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000019#include "clang/AST/ASTMutationListener.h"
20#include "clang/AST/ASTUnresolvedSet.h"
21#include "clang/AST/Decl.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000022#include "clang/AST/DeclBase.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000023#include "clang/AST/DeclCXX.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000024#include "clang/AST/DeclFriend.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000025#include "clang/AST/DeclGroup.h"
26#include "clang/AST/DeclObjC.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000027#include "clang/AST/DeclTemplate.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000028#include "clang/AST/DeclarationName.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000029#include "clang/AST/Expr.h"
30#include "clang/AST/ExprCXX.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000031#include "clang/AST/ExternalASTSource.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000032#include "clang/AST/NestedNameSpecifier.h"
Richard Trieue7f7ed22017-02-22 01:11:25 +000033#include "clang/AST/ODRHash.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000034#include "clang/AST/RawCommentList.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000035#include "clang/AST/TemplateBase.h"
36#include "clang/AST/TemplateName.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000037#include "clang/AST/Type.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000038#include "clang/AST/TypeLoc.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000039#include "clang/AST/TypeLocVisitor.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000040#include "clang/AST/UnresolvedSet.h"
41#include "clang/Basic/CommentOptions.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000042#include "clang/Basic/Diagnostic.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000043#include "clang/Basic/DiagnosticOptions.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000044#include "clang/Basic/ExceptionSpecificationType.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000045#include "clang/Basic/FileManager.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000046#include "clang/Basic/FileSystemOptions.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000047#include "clang/Basic/IdentifierTable.h"
48#include "clang/Basic/LLVM.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000049#include "clang/Basic/LangOptions.h"
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +000050#include "clang/Basic/MemoryBufferCache.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000051#include "clang/Basic/Module.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000052#include "clang/Basic/ObjCRuntime.h"
53#include "clang/Basic/OperatorKinds.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000054#include "clang/Basic/PragmaKinds.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000055#include "clang/Basic/Sanitizers.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000056#include "clang/Basic/SourceLocation.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000057#include "clang/Basic/SourceManager.h"
58#include "clang/Basic/SourceManagerInternals.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000059#include "clang/Basic/Specifiers.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000060#include "clang/Basic/TargetInfo.h"
61#include "clang/Basic/TargetOptions.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000062#include "clang/Basic/TokenKinds.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000063#include "clang/Basic/Version.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000064#include "clang/Frontend/PCHContainerOperations.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000065#include "clang/Lex/HeaderSearch.h"
66#include "clang/Lex/HeaderSearchOptions.h"
67#include "clang/Lex/MacroInfo.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000068#include "clang/Lex/ModuleMap.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000069#include "clang/Lex/PreprocessingRecord.h"
70#include "clang/Lex/Preprocessor.h"
71#include "clang/Lex/PreprocessorOptions.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000072#include "clang/Lex/Token.h"
73#include "clang/Sema/ObjCMethodList.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000074#include "clang/Sema/Scope.h"
75#include "clang/Sema/Sema.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000076#include "clang/Sema/Weak.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000077#include "clang/Serialization/ASTBitCodes.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000078#include "clang/Serialization/ASTDeserializationListener.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000079#include "clang/Serialization/ContinuousRangeMap.h"
Douglas Gregore060e572013-01-25 01:03:03 +000080#include "clang/Serialization/GlobalModuleIndex.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000081#include "clang/Serialization/Module.h"
82#include "clang/Serialization/ModuleFileExtension.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000083#include "clang/Serialization/ModuleManager.h"
84#include "clang/Serialization/SerializationDiagnostic.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000085#include "llvm/ADT/APFloat.h"
86#include "llvm/ADT/APInt.h"
87#include "llvm/ADT/APSInt.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000088#include "llvm/ADT/ArrayRef.h"
89#include "llvm/ADT/DenseMap.h"
90#include "llvm/ADT/FoldingSet.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000091#include "llvm/ADT/Hashing.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000092#include "llvm/ADT/IntrusiveRefCntPtr.h"
93#include "llvm/ADT/None.h"
94#include "llvm/ADT/Optional.h"
95#include "llvm/ADT/STLExtras.h"
96#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000097#include "llvm/ADT/SmallString.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000098#include "llvm/ADT/SmallVector.h"
Vedant Kumar48b4f762018-04-14 01:40:48 +000099#include "llvm/ADT/StringExtras.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000100#include "llvm/ADT/StringMap.h"
101#include "llvm/ADT/StringRef.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000102#include "llvm/ADT/Triple.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000103#include "llvm/ADT/iterator_range.h"
Guy Benyei11169dd2012-12-18 14:30:41 +0000104#include "llvm/Bitcode/BitstreamReader.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000105#include "llvm/Support/Casting.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000106#include "llvm/Support/Compiler.h"
Pavel Labathd8c62902018-06-11 10:28:04 +0000107#include "llvm/Support/Compression.h"
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000108#include "llvm/Support/DJB.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000109#include "llvm/Support/Endian.h"
George Rimarc39f5492017-01-17 15:45:31 +0000110#include "llvm/Support/Error.h"
Guy Benyei11169dd2012-12-18 14:30:41 +0000111#include "llvm/Support/ErrorHandling.h"
112#include "llvm/Support/FileSystem.h"
113#include "llvm/Support/MemoryBuffer.h"
114#include "llvm/Support/Path.h"
115#include "llvm/Support/SaveAndRestore.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000116#include "llvm/Support/Timer.h"
Pavel Labathd8c62902018-06-11 10:28:04 +0000117#include "llvm/Support/VersionTuple.h"
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000118#include "llvm/Support/raw_ostream.h"
Guy Benyei11169dd2012-12-18 14:30:41 +0000119#include <algorithm>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000120#include <cassert>
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000121#include <cstddef>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000122#include <cstdint>
Chris Lattner91f373e2013-01-20 00:57:52 +0000123#include <cstdio>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000124#include <ctime>
Guy Benyei11169dd2012-12-18 14:30:41 +0000125#include <iterator>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000126#include <limits>
127#include <map>
128#include <memory>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000129#include <string>
Rafael Espindola8a8e5542014-06-12 17:19:42 +0000130#include <system_error>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000131#include <tuple>
132#include <utility>
133#include <vector>
Guy Benyei11169dd2012-12-18 14:30:41 +0000134
135using namespace clang;
Vedant Kumar48b4f762018-04-14 01:40:48 +0000136using namespace clang::serialization;
137using namespace clang::serialization::reader;
Chris Lattner7fb3bef2013-01-20 00:56:42 +0000138using llvm::BitstreamCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +0000139
Ben Langmuircb69b572014-03-07 06:40:32 +0000140//===----------------------------------------------------------------------===//
141// ChainedASTReaderListener implementation
142//===----------------------------------------------------------------------===//
143
144bool
145ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
146 return First->ReadFullVersionInformation(FullVersion) ||
147 Second->ReadFullVersionInformation(FullVersion);
148}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000149
Ben Langmuir4f5212a2014-04-14 22:12:44 +0000150void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
151 First->ReadModuleName(ModuleName);
152 Second->ReadModuleName(ModuleName);
153}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000154
Ben Langmuir4f5212a2014-04-14 22:12:44 +0000155void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
156 First->ReadModuleMapFile(ModuleMapPath);
157 Second->ReadModuleMapFile(ModuleMapPath);
158}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000159
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000160bool
161ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
162 bool Complain,
163 bool AllowCompatibleDifferences) {
164 return First->ReadLanguageOptions(LangOpts, Complain,
165 AllowCompatibleDifferences) ||
166 Second->ReadLanguageOptions(LangOpts, Complain,
167 AllowCompatibleDifferences);
Ben Langmuircb69b572014-03-07 06:40:32 +0000168}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000169
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000170bool ChainedASTReaderListener::ReadTargetOptions(
171 const TargetOptions &TargetOpts, bool Complain,
172 bool AllowCompatibleDifferences) {
173 return First->ReadTargetOptions(TargetOpts, Complain,
174 AllowCompatibleDifferences) ||
175 Second->ReadTargetOptions(TargetOpts, Complain,
176 AllowCompatibleDifferences);
Ben Langmuircb69b572014-03-07 06:40:32 +0000177}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000178
Ben Langmuircb69b572014-03-07 06:40:32 +0000179bool ChainedASTReaderListener::ReadDiagnosticOptions(
Ben Langmuirb92de022014-04-29 16:25:26 +0000180 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
Ben Langmuircb69b572014-03-07 06:40:32 +0000181 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
182 Second->ReadDiagnosticOptions(DiagOpts, Complain);
183}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000184
Ben Langmuircb69b572014-03-07 06:40:32 +0000185bool
186ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
187 bool Complain) {
188 return First->ReadFileSystemOptions(FSOpts, Complain) ||
189 Second->ReadFileSystemOptions(FSOpts, Complain);
190}
191
192bool ChainedASTReaderListener::ReadHeaderSearchOptions(
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000193 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
194 bool Complain) {
195 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
196 Complain) ||
197 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
198 Complain);
Ben Langmuircb69b572014-03-07 06:40:32 +0000199}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000200
Ben Langmuircb69b572014-03-07 06:40:32 +0000201bool ChainedASTReaderListener::ReadPreprocessorOptions(
202 const PreprocessorOptions &PPOpts, bool Complain,
203 std::string &SuggestedPredefines) {
204 return First->ReadPreprocessorOptions(PPOpts, Complain,
205 SuggestedPredefines) ||
206 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
207}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000208
Ben Langmuircb69b572014-03-07 06:40:32 +0000209void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
210 unsigned Value) {
211 First->ReadCounter(M, Value);
212 Second->ReadCounter(M, Value);
213}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000214
Ben Langmuircb69b572014-03-07 06:40:32 +0000215bool ChainedASTReaderListener::needsInputFileVisitation() {
216 return First->needsInputFileVisitation() ||
217 Second->needsInputFileVisitation();
218}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000219
Ben Langmuircb69b572014-03-07 06:40:32 +0000220bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
221 return First->needsSystemInputFileVisitation() ||
222 Second->needsSystemInputFileVisitation();
223}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000224
Richard Smith216a3bd2015-08-13 17:57:10 +0000225void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
226 ModuleKind Kind) {
227 First->visitModuleFile(Filename, Kind);
228 Second->visitModuleFile(Filename, Kind);
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000229}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000230
Ben Langmuircb69b572014-03-07 06:40:32 +0000231bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000232 bool isSystem,
Richard Smith216a3bd2015-08-13 17:57:10 +0000233 bool isOverridden,
234 bool isExplicitModule) {
Justin Bognerc65a66d2014-05-22 06:04:59 +0000235 bool Continue = false;
236 if (First->needsInputFileVisitation() &&
237 (!isSystem || First->needsSystemInputFileVisitation()))
Richard Smith216a3bd2015-08-13 17:57:10 +0000238 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
239 isExplicitModule);
Justin Bognerc65a66d2014-05-22 06:04:59 +0000240 if (Second->needsInputFileVisitation() &&
241 (!isSystem || Second->needsSystemInputFileVisitation()))
Richard Smith216a3bd2015-08-13 17:57:10 +0000242 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
243 isExplicitModule);
Justin Bognerc65a66d2014-05-22 06:04:59 +0000244 return Continue;
Ben Langmuircb69b572014-03-07 06:40:32 +0000245}
246
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000247void ChainedASTReaderListener::readModuleFileExtension(
248 const ModuleFileExtensionMetadata &Metadata) {
249 First->readModuleFileExtension(Metadata);
250 Second->readModuleFileExtension(Metadata);
251}
252
Guy Benyei11169dd2012-12-18 14:30:41 +0000253//===----------------------------------------------------------------------===//
254// PCH validator implementation
255//===----------------------------------------------------------------------===//
256
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000257ASTReaderListener::~ASTReaderListener() = default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000258
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000259/// Compare the given set of language options against an existing set of
Guy Benyei11169dd2012-12-18 14:30:41 +0000260/// language options.
261///
262/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000263/// \param AllowCompatibleDifferences If true, differences between compatible
264/// language options will be permitted.
Guy Benyei11169dd2012-12-18 14:30:41 +0000265///
266/// \returns true if the languagae options mis-match, false otherwise.
267static bool checkLanguageOptions(const LangOptions &LangOpts,
268 const LangOptions &ExistingLangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000269 DiagnosticsEngine *Diags,
270 bool AllowCompatibleDifferences = true) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000271#define LANGOPT(Name, Bits, Default, Description) \
272 if (ExistingLangOpts.Name != LangOpts.Name) { \
273 if (Diags) \
274 Diags->Report(diag::err_pch_langopt_mismatch) \
275 << Description << LangOpts.Name << ExistingLangOpts.Name; \
276 return true; \
277 }
278
279#define VALUE_LANGOPT(Name, Bits, Default, Description) \
280 if (ExistingLangOpts.Name != LangOpts.Name) { \
281 if (Diags) \
282 Diags->Report(diag::err_pch_langopt_value_mismatch) \
283 << Description; \
284 return true; \
285 }
286
287#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
288 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
289 if (Diags) \
290 Diags->Report(diag::err_pch_langopt_value_mismatch) \
291 << Description; \
292 return true; \
293 }
294
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000295#define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
296 if (!AllowCompatibleDifferences) \
297 LANGOPT(Name, Bits, Default, Description)
298
299#define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
300 if (!AllowCompatibleDifferences) \
301 ENUM_LANGOPT(Name, Bits, Default, Description)
302
Richard Smitha1ddf5e2016-04-07 20:47:37 +0000303#define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
304 if (!AllowCompatibleDifferences) \
305 VALUE_LANGOPT(Name, Bits, Default, Description)
306
Guy Benyei11169dd2012-12-18 14:30:41 +0000307#define BENIGN_LANGOPT(Name, Bits, Default, Description)
308#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
Richard Smitha1ddf5e2016-04-07 20:47:37 +0000309#define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
Guy Benyei11169dd2012-12-18 14:30:41 +0000310#include "clang/Basic/LangOptions.def"
311
Ben Langmuircd98cb72015-06-23 18:20:18 +0000312 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
313 if (Diags)
314 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
315 return true;
316 }
317
Guy Benyei11169dd2012-12-18 14:30:41 +0000318 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
319 if (Diags)
320 Diags->Report(diag::err_pch_langopt_value_mismatch)
321 << "target Objective-C runtime";
322 return true;
323 }
324
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +0000325 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
326 LangOpts.CommentOpts.BlockCommandNames) {
327 if (Diags)
328 Diags->Report(diag::err_pch_langopt_value_mismatch)
329 << "block command names";
330 return true;
331 }
332
Vedant Kumar85a83c22017-06-01 20:01:01 +0000333 // Sanitizer feature mismatches are treated as compatible differences. If
334 // compatible differences aren't allowed, we still only want to check for
335 // mismatches of non-modular sanitizers (the only ones which can affect AST
336 // generation).
337 if (!AllowCompatibleDifferences) {
338 SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
339 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
340 SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
341 ExistingSanitizers.clear(ModularSanitizers);
342 ImportedSanitizers.clear(ModularSanitizers);
343 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
344 const std::string Flag = "-fsanitize=";
345 if (Diags) {
346#define SANITIZER(NAME, ID) \
347 { \
348 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
349 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
350 if (InExistingModule != InImportedModule) \
351 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \
352 << InExistingModule << (Flag + NAME); \
353 }
354#include "clang/Basic/Sanitizers.def"
355 }
356 return true;
357 }
358 }
359
Guy Benyei11169dd2012-12-18 14:30:41 +0000360 return false;
361}
362
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000363/// Compare the given set of target options against an existing set of
Guy Benyei11169dd2012-12-18 14:30:41 +0000364/// target options.
365///
366/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
367///
368/// \returns true if the target options mis-match, false otherwise.
369static bool checkTargetOptions(const TargetOptions &TargetOpts,
370 const TargetOptions &ExistingTargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000371 DiagnosticsEngine *Diags,
372 bool AllowCompatibleDifferences = true) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000373#define CHECK_TARGET_OPT(Field, Name) \
374 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
375 if (Diags) \
376 Diags->Report(diag::err_pch_targetopt_mismatch) \
377 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
378 return true; \
379 }
380
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000381 // The triple and ABI must match exactly.
Guy Benyei11169dd2012-12-18 14:30:41 +0000382 CHECK_TARGET_OPT(Triple, "target");
Guy Benyei11169dd2012-12-18 14:30:41 +0000383 CHECK_TARGET_OPT(ABI, "target ABI");
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000384
385 // We can tolerate different CPUs in many cases, notably when one CPU
386 // supports a strict superset of another. When allowing compatible
387 // differences skip this check.
388 if (!AllowCompatibleDifferences)
389 CHECK_TARGET_OPT(CPU, "target CPU");
390
Guy Benyei11169dd2012-12-18 14:30:41 +0000391#undef CHECK_TARGET_OPT
392
393 // Compare feature sets.
394 SmallVector<StringRef, 4> ExistingFeatures(
395 ExistingTargetOpts.FeaturesAsWritten.begin(),
396 ExistingTargetOpts.FeaturesAsWritten.end());
397 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
398 TargetOpts.FeaturesAsWritten.end());
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +0000399 llvm::sort(ExistingFeatures.begin(), ExistingFeatures.end());
400 llvm::sort(ReadFeatures.begin(), ReadFeatures.end());
Guy Benyei11169dd2012-12-18 14:30:41 +0000401
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000402 // We compute the set difference in both directions explicitly so that we can
403 // diagnose the differences differently.
404 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
405 std::set_difference(
406 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
407 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
408 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
409 ExistingFeatures.begin(), ExistingFeatures.end(),
410 std::back_inserter(UnmatchedReadFeatures));
Guy Benyei11169dd2012-12-18 14:30:41 +0000411
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000412 // If we are allowing compatible differences and the read feature set is
413 // a strict subset of the existing feature set, there is nothing to diagnose.
414 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
415 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +0000416
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000417 if (Diags) {
418 for (StringRef Feature : UnmatchedReadFeatures)
Guy Benyei11169dd2012-12-18 14:30:41 +0000419 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000420 << /* is-existing-feature */ false << Feature;
421 for (StringRef Feature : UnmatchedExistingFeatures)
422 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
423 << /* is-existing-feature */ true << Feature;
Guy Benyei11169dd2012-12-18 14:30:41 +0000424 }
425
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000426 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +0000427}
428
429bool
430PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000431 bool Complain,
432 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000433 const LangOptions &ExistingLangOpts = PP.getLangOpts();
434 return checkLanguageOptions(LangOpts, ExistingLangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000435 Complain ? &Reader.Diags : nullptr,
436 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +0000437}
438
439bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000440 bool Complain,
441 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000442 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
443 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000444 Complain ? &Reader.Diags : nullptr,
445 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +0000446}
447
448namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000449
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000450using MacroDefinitionsMap =
451 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
452using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000453
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000454} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +0000455
Ben Langmuirb92de022014-04-29 16:25:26 +0000456static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
457 DiagnosticsEngine &Diags,
458 bool Complain) {
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000459 using Level = DiagnosticsEngine::Level;
Ben Langmuirb92de022014-04-29 16:25:26 +0000460
461 // Check current mappings for new -Werror mappings, and the stored mappings
462 // for cases that were explicitly mapped to *not* be errors that are now
463 // errors because of options like -Werror.
464 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
465
Vedant Kumar48b4f762018-04-14 01:40:48 +0000466 for (DiagnosticsEngine *MappingSource : MappingSources) {
Ben Langmuirb92de022014-04-29 16:25:26 +0000467 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
468 diag::kind DiagID = DiagIDMappingPair.first;
469 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
470 if (CurLevel < DiagnosticsEngine::Error)
471 continue; // not significant
472 Level StoredLevel =
473 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
474 if (StoredLevel < DiagnosticsEngine::Error) {
475 if (Complain)
476 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
477 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
478 return true;
479 }
480 }
481 }
482
483 return false;
484}
485
Alp Tokerac4e8e52014-06-22 21:58:33 +0000486static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
487 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
488 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
489 return true;
490 return Ext >= diag::Severity::Error;
Ben Langmuirb92de022014-04-29 16:25:26 +0000491}
492
493static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
494 DiagnosticsEngine &Diags,
495 bool IsSystem, bool Complain) {
496 // Top-level options
497 if (IsSystem) {
498 if (Diags.getSuppressSystemWarnings())
499 return false;
500 // If -Wsystem-headers was not enabled before, be conservative
501 if (StoredDiags.getSuppressSystemWarnings()) {
502 if (Complain)
503 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
504 return true;
505 }
506 }
507
508 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
509 if (Complain)
510 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
511 return true;
512 }
513
514 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
515 !StoredDiags.getEnableAllWarnings()) {
516 if (Complain)
517 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
518 return true;
519 }
520
521 if (isExtHandlingFromDiagsError(Diags) &&
522 !isExtHandlingFromDiagsError(StoredDiags)) {
523 if (Complain)
524 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
525 return true;
526 }
527
528 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
529}
530
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +0000531/// Return the top import module if it is implicit, nullptr otherwise.
532static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
533 Preprocessor &PP) {
534 // If the original import came from a file explicitly generated by the user,
535 // don't check the diagnostic mappings.
536 // FIXME: currently this is approximated by checking whether this is not a
537 // module import of an implicitly-loaded module file.
538 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
539 // the transitive closure of its imports, since unrelated modules cannot be
540 // imported until after this module finishes validation.
541 ModuleFile *TopImport = &*ModuleMgr.rbegin();
542 while (!TopImport->ImportedBy.empty())
543 TopImport = TopImport->ImportedBy[0];
544 if (TopImport->Kind != MK_ImplicitModule)
545 return nullptr;
546
547 StringRef ModuleName = TopImport->ModuleName;
548 assert(!ModuleName.empty() && "diagnostic options read before module name");
549
550 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
551 assert(M && "missing module");
552 return M;
553}
554
Ben Langmuirb92de022014-04-29 16:25:26 +0000555bool PCHValidator::ReadDiagnosticOptions(
556 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
557 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
558 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
559 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Alp Tokerf994cef2014-07-05 03:08:06 +0000560 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
Ben Langmuirb92de022014-04-29 16:25:26 +0000561 // This should never fail, because we would have processed these options
562 // before writing them to an ASTFile.
563 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
564
565 ModuleManager &ModuleMgr = Reader.getModuleManager();
566 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
567
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +0000568 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
569 if (!TopM)
Ben Langmuirb92de022014-04-29 16:25:26 +0000570 return false;
571
Ben Langmuirb92de022014-04-29 16:25:26 +0000572 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
573 // contains the union of their flags.
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +0000574 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
575 Complain);
Ben Langmuirb92de022014-04-29 16:25:26 +0000576}
577
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000578/// Collect the macro definitions provided by the given preprocessor
Guy Benyei11169dd2012-12-18 14:30:41 +0000579/// options.
Craig Toppera13603a2014-05-22 05:54:18 +0000580static void
581collectMacroDefinitions(const PreprocessorOptions &PPOpts,
582 MacroDefinitionsMap &Macros,
583 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
Vedant Kumar48b4f762018-04-14 01:40:48 +0000584 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
585 StringRef Macro = PPOpts.Macros[I].first;
586 bool IsUndef = PPOpts.Macros[I].second;
Guy Benyei11169dd2012-12-18 14:30:41 +0000587
588 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
589 StringRef MacroName = MacroPair.first;
590 StringRef MacroBody = MacroPair.second;
591
592 // For an #undef'd macro, we only care about the name.
593 if (IsUndef) {
594 if (MacroNames && !Macros.count(MacroName))
595 MacroNames->push_back(MacroName);
596
597 Macros[MacroName] = std::make_pair("", true);
598 continue;
599 }
600
601 // For a #define'd macro, figure out the actual definition.
602 if (MacroName.size() == Macro.size())
603 MacroBody = "1";
604 else {
605 // Note: GCC drops anything following an end-of-line character.
606 StringRef::size_type End = MacroBody.find_first_of("\n\r");
607 MacroBody = MacroBody.substr(0, End);
608 }
609
610 if (MacroNames && !Macros.count(MacroName))
611 MacroNames->push_back(MacroName);
612 Macros[MacroName] = std::make_pair(MacroBody, false);
613 }
614}
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000615
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000616/// Check the preprocessor options deserialized from the control block
Guy Benyei11169dd2012-12-18 14:30:41 +0000617/// against the preprocessor options in an existing preprocessor.
618///
619/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
Yaxun Liu43712e02016-09-07 18:40:20 +0000620/// \param Validate If true, validate preprocessor options. If false, allow
621/// macros defined by \p ExistingPPOpts to override those defined by
622/// \p PPOpts in SuggestedPredefines.
Guy Benyei11169dd2012-12-18 14:30:41 +0000623static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
624 const PreprocessorOptions &ExistingPPOpts,
625 DiagnosticsEngine *Diags,
626 FileManager &FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000627 std::string &SuggestedPredefines,
Yaxun Liu43712e02016-09-07 18:40:20 +0000628 const LangOptions &LangOpts,
629 bool Validate = true) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000630 // Check macro definitions.
631 MacroDefinitionsMap ASTFileMacros;
632 collectMacroDefinitions(PPOpts, ASTFileMacros);
633 MacroDefinitionsMap ExistingMacros;
634 SmallVector<StringRef, 4> ExistingMacroNames;
635 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
636
Vedant Kumar48b4f762018-04-14 01:40:48 +0000637 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000638 // Dig out the macro definition in the existing preprocessor options.
Vedant Kumar48b4f762018-04-14 01:40:48 +0000639 StringRef MacroName = ExistingMacroNames[I];
Guy Benyei11169dd2012-12-18 14:30:41 +0000640 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
641
642 // Check whether we know anything about this macro name or not.
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000643 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
644 ASTFileMacros.find(MacroName);
Yaxun Liu43712e02016-09-07 18:40:20 +0000645 if (!Validate || Known == ASTFileMacros.end()) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000646 // FIXME: Check whether this identifier was referenced anywhere in the
647 // AST file. If so, we should reject the AST file. Unfortunately, this
648 // information isn't in the control block. What shall we do about it?
649
650 if (Existing.second) {
651 SuggestedPredefines += "#undef ";
652 SuggestedPredefines += MacroName.str();
653 SuggestedPredefines += '\n';
654 } else {
655 SuggestedPredefines += "#define ";
656 SuggestedPredefines += MacroName.str();
657 SuggestedPredefines += ' ';
658 SuggestedPredefines += Existing.first.str();
659 SuggestedPredefines += '\n';
660 }
661 continue;
662 }
663
664 // If the macro was defined in one but undef'd in the other, we have a
665 // conflict.
666 if (Existing.second != Known->second.second) {
667 if (Diags) {
668 Diags->Report(diag::err_pch_macro_def_undef)
669 << MacroName << Known->second.second;
670 }
671 return true;
672 }
673
674 // If the macro was #undef'd in both, or if the macro bodies are identical,
675 // it's fine.
676 if (Existing.second || Existing.first == Known->second.first)
677 continue;
678
679 // The macro bodies differ; complain.
680 if (Diags) {
681 Diags->Report(diag::err_pch_macro_def_conflict)
682 << MacroName << Known->second.first << Existing.first;
683 }
684 return true;
685 }
686
687 // Check whether we're using predefines.
Yaxun Liu43712e02016-09-07 18:40:20 +0000688 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000689 if (Diags) {
690 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
691 }
692 return true;
693 }
694
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000695 // Detailed record is important since it is used for the module cache hash.
696 if (LangOpts.Modules &&
Yaxun Liu43712e02016-09-07 18:40:20 +0000697 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000698 if (Diags) {
699 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
700 }
701 return true;
702 }
703
Guy Benyei11169dd2012-12-18 14:30:41 +0000704 // Compute the #include and #include_macros lines we need.
Vedant Kumar48b4f762018-04-14 01:40:48 +0000705 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
706 StringRef File = ExistingPPOpts.Includes[I];
Guy Benyei11169dd2012-12-18 14:30:41 +0000707 if (File == ExistingPPOpts.ImplicitPCHInclude)
708 continue;
709
710 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
711 != PPOpts.Includes.end())
712 continue;
713
714 SuggestedPredefines += "#include \"";
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000715 SuggestedPredefines += File;
Guy Benyei11169dd2012-12-18 14:30:41 +0000716 SuggestedPredefines += "\"\n";
717 }
718
Vedant Kumar48b4f762018-04-14 01:40:48 +0000719 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
720 StringRef File = ExistingPPOpts.MacroIncludes[I];
Guy Benyei11169dd2012-12-18 14:30:41 +0000721 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
722 File)
723 != PPOpts.MacroIncludes.end())
724 continue;
725
726 SuggestedPredefines += "#__include_macros \"";
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000727 SuggestedPredefines += File;
Guy Benyei11169dd2012-12-18 14:30:41 +0000728 SuggestedPredefines += "\"\n##\n";
729 }
730
731 return false;
732}
733
734bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
735 bool Complain,
736 std::string &SuggestedPredefines) {
737 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
738
739 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
Craig Toppera13603a2014-05-22 05:54:18 +0000740 Complain? &Reader.Diags : nullptr,
Guy Benyei11169dd2012-12-18 14:30:41 +0000741 PP.getFileManager(),
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000742 SuggestedPredefines,
743 PP.getLangOpts());
Guy Benyei11169dd2012-12-18 14:30:41 +0000744}
745
Yaxun Liu43712e02016-09-07 18:40:20 +0000746bool SimpleASTReaderListener::ReadPreprocessorOptions(
747 const PreprocessorOptions &PPOpts,
748 bool Complain,
749 std::string &SuggestedPredefines) {
750 return checkPreprocessorOptions(PPOpts,
751 PP.getPreprocessorOpts(),
752 nullptr,
753 PP.getFileManager(),
754 SuggestedPredefines,
755 PP.getLangOpts(),
756 false);
757}
758
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000759/// Check the header search options deserialized from the control block
760/// against the header search options in an existing preprocessor.
761///
762/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
763static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
764 StringRef SpecificModuleCachePath,
765 StringRef ExistingModuleCachePath,
766 DiagnosticsEngine *Diags,
767 const LangOptions &LangOpts) {
768 if (LangOpts.Modules) {
769 if (SpecificModuleCachePath != ExistingModuleCachePath) {
770 if (Diags)
771 Diags->Report(diag::err_pch_modulecache_mismatch)
772 << SpecificModuleCachePath << ExistingModuleCachePath;
773 return true;
774 }
775 }
776
777 return false;
778}
779
780bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
781 StringRef SpecificModuleCachePath,
782 bool Complain) {
783 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
784 PP.getHeaderSearchInfo().getModuleCachePath(),
785 Complain ? &Reader.Diags : nullptr,
786 PP.getLangOpts());
787}
788
Guy Benyei11169dd2012-12-18 14:30:41 +0000789void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
790 PP.setCounterValue(Value);
791}
792
793//===----------------------------------------------------------------------===//
794// AST reader implementation
795//===----------------------------------------------------------------------===//
796
Nico Weber824285e2014-05-08 04:26:47 +0000797void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
798 bool TakeOwnership) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000799 DeserializationListener = Listener;
Nico Weber824285e2014-05-08 04:26:47 +0000800 OwnsDeserializationListener = TakeOwnership;
Guy Benyei11169dd2012-12-18 14:30:41 +0000801}
802
Guy Benyei11169dd2012-12-18 14:30:41 +0000803unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
804 return serialization::ComputeHash(Sel);
805}
806
Guy Benyei11169dd2012-12-18 14:30:41 +0000807std::pair<unsigned, unsigned>
808ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000809 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000810
Justin Bogner57ba0b22014-03-28 22:03:24 +0000811 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
812 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000813 return std::make_pair(KeyLen, DataLen);
814}
815
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000816ASTSelectorLookupTrait::internal_key_type
Guy Benyei11169dd2012-12-18 14:30:41 +0000817ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000818 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000819
Guy Benyei11169dd2012-12-18 14:30:41 +0000820 SelectorTable &SelTable = Reader.getContext().Selectors;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000821 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
822 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
823 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000824 if (N == 0)
825 return SelTable.getNullarySelector(FirstII);
826 else if (N == 1)
827 return SelTable.getUnarySelector(FirstII);
828
829 SmallVector<IdentifierInfo *, 16> Args;
830 Args.push_back(FirstII);
831 for (unsigned I = 1; I != N; ++I)
Justin Bogner57ba0b22014-03-28 22:03:24 +0000832 Args.push_back(Reader.getLocalIdentifier(
833 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000834
835 return SelTable.getSelector(N, Args.data());
836}
837
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000838ASTSelectorLookupTrait::data_type
839ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
Guy Benyei11169dd2012-12-18 14:30:41 +0000840 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000841 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000842
843 data_type Result;
844
Justin Bogner57ba0b22014-03-28 22:03:24 +0000845 Result.ID = Reader.getGlobalSelectorID(
846 F, endian::readNext<uint32_t, little, unaligned>(d));
Nico Weberff4b35e2014-12-27 22:14:15 +0000847 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
848 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
849 Result.InstanceBits = FullInstanceBits & 0x3;
850 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
851 Result.FactoryBits = FullFactoryBits & 0x3;
852 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
853 unsigned NumInstanceMethods = FullInstanceBits >> 3;
854 unsigned NumFactoryMethods = FullFactoryBits >> 3;
Guy Benyei11169dd2012-12-18 14:30:41 +0000855
856 // Load instance methods
857 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Vedant Kumar48b4f762018-04-14 01:40:48 +0000858 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
Justin Bogner57ba0b22014-03-28 22:03:24 +0000859 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei11169dd2012-12-18 14:30:41 +0000860 Result.Instance.push_back(Method);
861 }
862
863 // Load factory methods
864 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Vedant Kumar48b4f762018-04-14 01:40:48 +0000865 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
Justin Bogner57ba0b22014-03-28 22:03:24 +0000866 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei11169dd2012-12-18 14:30:41 +0000867 Result.Factory.push_back(Method);
868 }
869
870 return Result;
871}
872
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000873unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000874 return llvm::djbHash(a);
Guy Benyei11169dd2012-12-18 14:30:41 +0000875}
876
877std::pair<unsigned, unsigned>
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000878ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000879 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000880
Justin Bogner57ba0b22014-03-28 22:03:24 +0000881 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
882 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000883 return std::make_pair(KeyLen, DataLen);
884}
885
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000886ASTIdentifierLookupTraitBase::internal_key_type
887ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000888 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000889 return StringRef((const char*) d, n-1);
Guy Benyei11169dd2012-12-18 14:30:41 +0000890}
891
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000892/// Whether the given identifier is "interesting".
Richard Smitha534a312015-07-21 23:54:07 +0000893static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
894 bool IsModule) {
Richard Smithcab89802015-07-17 20:19:56 +0000895 return II.hadMacroDefinition() ||
896 II.isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +0000897 (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
Douglas Gregordcf25082013-02-11 18:16:18 +0000898 II.hasRevertedTokenIDToIdentifier() ||
Richard Smithdbafb6c2017-06-29 23:23:46 +0000899 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
Richard Smitha534a312015-07-21 23:54:07 +0000900 II.getFETokenInfo<void>());
Douglas Gregordcf25082013-02-11 18:16:18 +0000901}
902
Richard Smith76c2f2c2015-07-17 20:09:43 +0000903static bool readBit(unsigned &Bits) {
904 bool Value = Bits & 0x1;
905 Bits >>= 1;
906 return Value;
907}
908
Richard Smith79bf9202015-08-24 03:33:22 +0000909IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
910 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000911
Richard Smith79bf9202015-08-24 03:33:22 +0000912 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
913 return Reader.getGlobalIdentifierID(F, RawID >> 1);
914}
915
Richard Smitheb4b58f62016-02-05 01:40:54 +0000916static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
917 if (!II.isFromAST()) {
918 II.setIsFromAST();
919 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
920 if (isInterestingIdentifier(Reader, II, IsModule))
921 II.setChangedSinceDeserialization();
922 }
923}
924
Guy Benyei11169dd2012-12-18 14:30:41 +0000925IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
926 const unsigned char* d,
927 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000928 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000929
Justin Bogner57ba0b22014-03-28 22:03:24 +0000930 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000931 bool IsInteresting = RawID & 0x01;
932
933 // Wipe out the "is interesting" bit.
934 RawID = RawID >> 1;
935
Richard Smith76c2f2c2015-07-17 20:09:43 +0000936 // Build the IdentifierInfo and link the identifier ID with it.
937 IdentifierInfo *II = KnownII;
938 if (!II) {
939 II = &Reader.getIdentifierTable().getOwn(k);
940 KnownII = II;
941 }
Richard Smitheb4b58f62016-02-05 01:40:54 +0000942 markIdentifierFromAST(Reader, *II);
Richard Smith76c2f2c2015-07-17 20:09:43 +0000943 Reader.markIdentifierUpToDate(II);
944
Guy Benyei11169dd2012-12-18 14:30:41 +0000945 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
946 if (!IsInteresting) {
Richard Smith76c2f2c2015-07-17 20:09:43 +0000947 // For uninteresting identifiers, there's nothing else to do. Just notify
948 // the reader that we've finished loading this identifier.
Guy Benyei11169dd2012-12-18 14:30:41 +0000949 Reader.SetIdentifierInfo(ID, II);
Guy Benyei11169dd2012-12-18 14:30:41 +0000950 return II;
951 }
952
Justin Bogner57ba0b22014-03-28 22:03:24 +0000953 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
954 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
Richard Smith76c2f2c2015-07-17 20:09:43 +0000955 bool CPlusPlusOperatorKeyword = readBit(Bits);
956 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
Richard Smith9c254182015-07-19 21:41:12 +0000957 bool HasRevertedBuiltin = readBit(Bits);
Richard Smith76c2f2c2015-07-17 20:09:43 +0000958 bool Poisoned = readBit(Bits);
959 bool ExtensionToken = readBit(Bits);
960 bool HadMacroDefinition = readBit(Bits);
Guy Benyei11169dd2012-12-18 14:30:41 +0000961
962 assert(Bits == 0 && "Extra bits in the identifier?");
963 DataLen -= 8;
964
Guy Benyei11169dd2012-12-18 14:30:41 +0000965 // Set or check the various bits in the IdentifierInfo structure.
966 // Token IDs are read-only.
Argyrios Kyrtzidisddee8c92013-02-27 01:13:51 +0000967 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Richard Smith9c254182015-07-19 21:41:12 +0000968 II->revertTokenIDToIdentifier();
969 if (!F.isModule())
970 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
971 else if (HasRevertedBuiltin && II->getBuiltinID()) {
972 II->revertBuiltin();
973 assert((II->hasRevertedBuiltin() ||
974 II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
975 "Incorrect ObjC keyword or builtin ID");
976 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000977 assert(II->isExtensionToken() == ExtensionToken &&
978 "Incorrect extension token flag");
979 (void)ExtensionToken;
980 if (Poisoned)
981 II->setIsPoisoned(true);
982 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
983 "Incorrect C++ operator keyword flag");
984 (void)CPlusPlusOperatorKeyword;
985
986 // If this identifier is a macro, deserialize the macro
987 // definition.
Richard Smith76c2f2c2015-07-17 20:09:43 +0000988 if (HadMacroDefinition) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000989 uint32_t MacroDirectivesOffset =
990 endian::readNext<uint32_t, little, unaligned>(d);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000991 DataLen -= 4;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000992
Richard Smithd7329392015-04-21 21:46:32 +0000993 Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +0000994 }
995
996 Reader.SetIdentifierInfo(ID, II);
997
998 // Read all of the declarations visible at global scope with this
999 // name.
1000 if (DataLen > 0) {
1001 SmallVector<uint32_t, 4> DeclIDs;
1002 for (; DataLen > 0; DataLen -= 4)
Justin Bogner57ba0b22014-03-28 22:03:24 +00001003 DeclIDs.push_back(Reader.getGlobalDeclID(
1004 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +00001005 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1006 }
1007
1008 return II;
1009}
1010
Richard Smitha06c7e62015-08-26 23:55:49 +00001011DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1012 : Kind(Name.getNameKind()) {
1013 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001014 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00001015 Data = (uint64_t)Name.getAsIdentifierInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +00001016 break;
1017 case DeclarationName::ObjCZeroArgSelector:
1018 case DeclarationName::ObjCOneArgSelector:
1019 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00001020 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
Guy Benyei11169dd2012-12-18 14:30:41 +00001021 break;
1022 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00001023 Data = Name.getCXXOverloadedOperator();
1024 break;
1025 case DeclarationName::CXXLiteralOperatorName:
1026 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1027 break;
Richard Smith35845152017-02-07 01:37:30 +00001028 case DeclarationName::CXXDeductionGuideName:
1029 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1030 ->getDeclName().getAsIdentifierInfo();
1031 break;
Richard Smitha06c7e62015-08-26 23:55:49 +00001032 case DeclarationName::CXXConstructorName:
1033 case DeclarationName::CXXDestructorName:
1034 case DeclarationName::CXXConversionFunctionName:
1035 case DeclarationName::CXXUsingDirective:
1036 Data = 0;
1037 break;
1038 }
1039}
1040
1041unsigned DeclarationNameKey::getHash() const {
1042 llvm::FoldingSetNodeID ID;
1043 ID.AddInteger(Kind);
1044
1045 switch (Kind) {
1046 case DeclarationName::Identifier:
1047 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00001048 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00001049 ID.AddString(((IdentifierInfo*)Data)->getName());
1050 break;
1051 case DeclarationName::ObjCZeroArgSelector:
1052 case DeclarationName::ObjCOneArgSelector:
1053 case DeclarationName::ObjCMultiArgSelector:
1054 ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1055 break;
1056 case DeclarationName::CXXOperatorName:
1057 ID.AddInteger((OverloadedOperatorKind)Data);
Guy Benyei11169dd2012-12-18 14:30:41 +00001058 break;
1059 case DeclarationName::CXXConstructorName:
1060 case DeclarationName::CXXDestructorName:
1061 case DeclarationName::CXXConversionFunctionName:
1062 case DeclarationName::CXXUsingDirective:
1063 break;
1064 }
1065
1066 return ID.ComputeHash();
1067}
1068
Richard Smithd88a7f12015-09-01 20:35:42 +00001069ModuleFile *
1070ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1071 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001072
Richard Smithd88a7f12015-09-01 20:35:42 +00001073 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1074 return Reader.getLocalModuleFile(F, ModuleFileID);
1075}
1076
Guy Benyei11169dd2012-12-18 14:30:41 +00001077std::pair<unsigned, unsigned>
Richard Smitha06c7e62015-08-26 23:55:49 +00001078ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001079 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001080
Justin Bogner57ba0b22014-03-28 22:03:24 +00001081 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1082 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +00001083 return std::make_pair(KeyLen, DataLen);
1084}
1085
Richard Smitha06c7e62015-08-26 23:55:49 +00001086ASTDeclContextNameLookupTrait::internal_key_type
1087ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001088 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +00001089
Vedant Kumar48b4f762018-04-14 01:40:48 +00001090 auto Kind = (DeclarationName::NameKind)*d++;
Richard Smitha06c7e62015-08-26 23:55:49 +00001091 uint64_t Data;
1092 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001093 case DeclarationName::Identifier:
Richard Smith35845152017-02-07 01:37:30 +00001094 case DeclarationName::CXXLiteralOperatorName:
1095 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00001096 Data = (uint64_t)Reader.getLocalIdentifier(
Justin Bogner57ba0b22014-03-28 22:03:24 +00001097 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +00001098 break;
1099 case DeclarationName::ObjCZeroArgSelector:
1100 case DeclarationName::ObjCOneArgSelector:
1101 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00001102 Data =
Justin Bogner57ba0b22014-03-28 22:03:24 +00001103 (uint64_t)Reader.getLocalSelector(
1104 F, endian::readNext<uint32_t, little, unaligned>(
1105 d)).getAsOpaquePtr();
Guy Benyei11169dd2012-12-18 14:30:41 +00001106 break;
1107 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00001108 Data = *d++; // OverloadedOperatorKind
Guy Benyei11169dd2012-12-18 14:30:41 +00001109 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001110 case DeclarationName::CXXConstructorName:
1111 case DeclarationName::CXXDestructorName:
1112 case DeclarationName::CXXConversionFunctionName:
1113 case DeclarationName::CXXUsingDirective:
Richard Smitha06c7e62015-08-26 23:55:49 +00001114 Data = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001115 break;
1116 }
1117
Richard Smitha06c7e62015-08-26 23:55:49 +00001118 return DeclarationNameKey(Kind, Data);
Guy Benyei11169dd2012-12-18 14:30:41 +00001119}
1120
Richard Smithd88a7f12015-09-01 20:35:42 +00001121void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1122 const unsigned char *d,
1123 unsigned DataLen,
1124 data_type_builder &Val) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001125 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001126
Richard Smithd88a7f12015-09-01 20:35:42 +00001127 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1128 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1129 Val.insert(Reader.getGlobalDeclID(F, LocalID));
1130 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001131}
1132
Richard Smith0f4e2c42015-08-06 04:23:48 +00001133bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1134 BitstreamCursor &Cursor,
1135 uint64_t Offset,
1136 DeclContext *DC) {
1137 assert(Offset != 0);
1138
Guy Benyei11169dd2012-12-18 14:30:41 +00001139 SavedStreamPosition SavedPosition(Cursor);
Richard Smith0f4e2c42015-08-06 04:23:48 +00001140 Cursor.JumpToBit(Offset);
Guy Benyei11169dd2012-12-18 14:30:41 +00001141
Richard Smith0f4e2c42015-08-06 04:23:48 +00001142 RecordData Record;
1143 StringRef Blob;
1144 unsigned Code = Cursor.ReadCode();
1145 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
1146 if (RecCode != DECL_CONTEXT_LEXICAL) {
1147 Error("Expected lexical block");
1148 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001149 }
1150
Richard Smith82f8fcd2015-08-06 22:07:25 +00001151 assert(!isa<TranslationUnitDecl>(DC) &&
1152 "expected a TU_UPDATE_LEXICAL record for TU");
Richard Smith9c9173d2015-08-11 22:00:24 +00001153 // If we are handling a C++ class template instantiation, we can see multiple
1154 // lexical updates for the same record. It's important that we select only one
1155 // of them, so that field numbering works properly. Just pick the first one we
1156 // see.
1157 auto &Lex = LexicalDecls[DC];
1158 if (!Lex.first) {
1159 Lex = std::make_pair(
1160 &M, llvm::makeArrayRef(
1161 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1162 Blob.data()),
1163 Blob.size() / 4));
1164 }
Richard Smith0f4e2c42015-08-06 04:23:48 +00001165 DC->setHasExternalLexicalStorage(true);
1166 return false;
1167}
Guy Benyei11169dd2012-12-18 14:30:41 +00001168
Richard Smith0f4e2c42015-08-06 04:23:48 +00001169bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1170 BitstreamCursor &Cursor,
1171 uint64_t Offset,
1172 DeclID ID) {
1173 assert(Offset != 0);
1174
1175 SavedStreamPosition SavedPosition(Cursor);
1176 Cursor.JumpToBit(Offset);
1177
1178 RecordData Record;
1179 StringRef Blob;
1180 unsigned Code = Cursor.ReadCode();
1181 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
1182 if (RecCode != DECL_CONTEXT_VISIBLE) {
1183 Error("Expected visible lookup table block");
1184 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001185 }
1186
Richard Smith0f4e2c42015-08-06 04:23:48 +00001187 // We can't safely determine the primary context yet, so delay attaching the
1188 // lookup table until we're done with recursive deserialization.
Richard Smithd88a7f12015-09-01 20:35:42 +00001189 auto *Data = (const unsigned char*)Blob.data();
1190 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
Guy Benyei11169dd2012-12-18 14:30:41 +00001191 return false;
1192}
1193
Richard Smith37a93df2017-02-18 00:32:02 +00001194void ASTReader::Error(StringRef Msg) const {
Guy Benyei11169dd2012-12-18 14:30:41 +00001195 Error(diag::err_fe_pch_malformed, Msg);
Richard Smithdbafb6c2017-06-29 23:23:46 +00001196 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
Richard Smithfb1e7f72015-08-14 05:02:58 +00001197 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
Douglas Gregor940e8052013-05-10 22:15:13 +00001198 Diag(diag::note_module_cache_path)
1199 << PP.getHeaderSearchInfo().getModuleCachePath();
1200 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001201}
1202
1203void ASTReader::Error(unsigned DiagID,
Richard Smith37a93df2017-02-18 00:32:02 +00001204 StringRef Arg1, StringRef Arg2) const {
Guy Benyei11169dd2012-12-18 14:30:41 +00001205 if (Diags.isDiagnosticInFlight())
1206 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1207 else
1208 Diag(DiagID) << Arg1 << Arg2;
1209}
1210
1211//===----------------------------------------------------------------------===//
1212// Source Manager Deserialization
1213//===----------------------------------------------------------------------===//
1214
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001215/// Read the line table in the source manager block.
Guy Benyei11169dd2012-12-18 14:30:41 +00001216/// \returns true if there was an error.
1217bool ASTReader::ParseLineTable(ModuleFile &F,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001218 const RecordData &Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001219 unsigned Idx = 0;
1220 LineTableInfo &LineTable = SourceMgr.getLineTable();
1221
1222 // Parse the file names
1223 std::map<int, int> FileIDs;
Hans Wennborg14487362017-12-04 22:28:45 +00001224 FileIDs[-1] = -1; // For unspecified filenames.
Richard Smith63078492015-09-01 07:41:55 +00001225 for (unsigned I = 0; Record[Idx]; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001226 // Extract the file name
Richard Smith7ed1bc92014-12-05 22:42:13 +00001227 auto Filename = ReadPath(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001228 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1229 }
Richard Smith63078492015-09-01 07:41:55 +00001230 ++Idx;
Guy Benyei11169dd2012-12-18 14:30:41 +00001231
1232 // Parse the line entries
1233 std::vector<LineEntry> Entries;
1234 while (Idx < Record.size()) {
1235 int FID = Record[Idx++];
1236 assert(FID >= 0 && "Serialized line entries for non-local file.");
1237 // Remap FileID from 1-based old view.
1238 FID += F.SLocEntryBaseID - 1;
1239
1240 // Extract the line entries
1241 unsigned NumEntries = Record[Idx++];
Richard Smith63078492015-09-01 07:41:55 +00001242 assert(NumEntries && "no line entries for file ID");
Guy Benyei11169dd2012-12-18 14:30:41 +00001243 Entries.clear();
1244 Entries.reserve(NumEntries);
1245 for (unsigned I = 0; I != NumEntries; ++I) {
1246 unsigned FileOffset = Record[Idx++];
1247 unsigned LineNo = Record[Idx++];
1248 int FilenameID = FileIDs[Record[Idx++]];
Vedant Kumar48b4f762018-04-14 01:40:48 +00001249 SrcMgr::CharacteristicKind FileKind
1250 = (SrcMgr::CharacteristicKind)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00001251 unsigned IncludeOffset = Record[Idx++];
1252 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1253 FileKind, IncludeOffset));
1254 }
1255 LineTable.AddEntry(FileID::get(FID), Entries);
1256 }
1257
1258 return false;
1259}
1260
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001261/// Read a source manager block
Guy Benyei11169dd2012-12-18 14:30:41 +00001262bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1263 using namespace SrcMgr;
1264
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001265 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001266
1267 // Set the source-location entry cursor to the current position in
1268 // the stream. This cursor will be used to read the contents of the
1269 // source manager block initially, and then lazily read
1270 // source-location entries as needed.
1271 SLocEntryCursor = F.Stream;
1272
1273 // The stream itself is going to skip over the source manager block.
1274 if (F.Stream.SkipBlock()) {
1275 Error("malformed block record in AST file");
1276 return true;
1277 }
1278
1279 // Enter the source manager block.
1280 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1281 Error("malformed source manager block record in AST file");
1282 return true;
1283 }
1284
1285 RecordData Record;
1286 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001287 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001288
Chris Lattnere7b154b2013-01-19 21:39:22 +00001289 switch (E.Kind) {
1290 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1291 case llvm::BitstreamEntry::Error:
1292 Error("malformed block record in AST file");
1293 return true;
1294 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00001295 return false;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001296 case llvm::BitstreamEntry::Record:
1297 // The interesting case.
1298 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001299 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001300
Guy Benyei11169dd2012-12-18 14:30:41 +00001301 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001302 Record.clear();
Chris Lattner15c3e7d2013-01-21 18:28:26 +00001303 StringRef Blob;
1304 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001305 default: // Default behavior: ignore.
1306 break;
1307
1308 case SM_SLOC_FILE_ENTRY:
1309 case SM_SLOC_BUFFER_ENTRY:
1310 case SM_SLOC_EXPANSION_ENTRY:
1311 // Once we hit one of the source location entries, we're done.
1312 return false;
1313 }
1314 }
1315}
1316
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001317/// If a header file is not found at the path that we expect it to be
Guy Benyei11169dd2012-12-18 14:30:41 +00001318/// and the PCH file was moved from its original location, try to resolve the
1319/// file by assuming that header+PCH were moved together and the header is in
1320/// the same place relative to the PCH.
1321static std::string
1322resolveFileRelativeToOriginalDir(const std::string &Filename,
1323 const std::string &OriginalDir,
1324 const std::string &CurrDir) {
1325 assert(OriginalDir != CurrDir &&
1326 "No point trying to resolve the file if the PCH dir didn't change");
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001327
Guy Benyei11169dd2012-12-18 14:30:41 +00001328 using namespace llvm::sys;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001329
Guy Benyei11169dd2012-12-18 14:30:41 +00001330 SmallString<128> filePath(Filename);
1331 fs::make_absolute(filePath);
1332 assert(path::is_absolute(OriginalDir));
1333 SmallString<128> currPCHPath(CurrDir);
1334
1335 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1336 fileDirE = path::end(path::parent_path(filePath));
1337 path::const_iterator origDirI = path::begin(OriginalDir),
1338 origDirE = path::end(OriginalDir);
1339 // Skip the common path components from filePath and OriginalDir.
1340 while (fileDirI != fileDirE && origDirI != origDirE &&
1341 *fileDirI == *origDirI) {
1342 ++fileDirI;
1343 ++origDirI;
1344 }
1345 for (; origDirI != origDirE; ++origDirI)
1346 path::append(currPCHPath, "..");
1347 path::append(currPCHPath, fileDirI, fileDirE);
1348 path::append(currPCHPath, path::filename(Filename));
1349 return currPCHPath.str();
1350}
1351
1352bool ASTReader::ReadSLocEntry(int ID) {
1353 if (ID == 0)
1354 return false;
1355
1356 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1357 Error("source location entry ID out-of-range for AST file");
1358 return true;
1359 }
1360
Richard Smithaada85c2016-02-06 02:06:43 +00001361 // Local helper to read the (possibly-compressed) buffer data following the
1362 // entry record.
1363 auto ReadBuffer = [this](
1364 BitstreamCursor &SLocEntryCursor,
1365 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1366 RecordData Record;
1367 StringRef Blob;
1368 unsigned Code = SLocEntryCursor.ReadCode();
1369 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
1370
1371 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
George Rimarc39f5492017-01-17 15:45:31 +00001372 if (!llvm::zlib::isAvailable()) {
1373 Error("zlib is not available");
1374 return nullptr;
1375 }
Richard Smithaada85c2016-02-06 02:06:43 +00001376 SmallString<0> Uncompressed;
George Rimarc39f5492017-01-17 15:45:31 +00001377 if (llvm::Error E =
1378 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1379 Error("could not decompress embedded file contents: " +
1380 llvm::toString(std::move(E)));
Richard Smithaada85c2016-02-06 02:06:43 +00001381 return nullptr;
1382 }
1383 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1384 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1385 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1386 } else {
1387 Error("AST record has invalid code");
1388 return nullptr;
1389 }
1390 };
1391
Guy Benyei11169dd2012-12-18 14:30:41 +00001392 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1393 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001394 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001395 unsigned BaseOffset = F->SLocEntryBaseOffset;
1396
1397 ++NumSLocEntriesRead;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001398 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1399 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001400 Error("incorrectly-formatted source location entry in AST file");
1401 return true;
1402 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001403
Guy Benyei11169dd2012-12-18 14:30:41 +00001404 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +00001405 StringRef Blob;
1406 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001407 default:
1408 Error("incorrectly-formatted source location entry in AST file");
1409 return true;
1410
1411 case SM_SLOC_FILE_ENTRY: {
1412 // We will detect whether a file changed and return 'Failure' for it, but
1413 // we will also try to fail gracefully by setting up the SLocEntry.
1414 unsigned InputID = Record[4];
1415 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001416 const FileEntry *File = IF.getFile();
1417 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei11169dd2012-12-18 14:30:41 +00001418
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001419 // Note that we only check if a File was returned. If it was out-of-date
1420 // we have complained but we will continue creating a FileID to recover
1421 // gracefully.
1422 if (!File)
Guy Benyei11169dd2012-12-18 14:30:41 +00001423 return true;
1424
1425 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1426 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1427 // This is the module's main file.
1428 IncludeLoc = getImportLocation(F);
1429 }
Vedant Kumar48b4f762018-04-14 01:40:48 +00001430 SrcMgr::CharacteristicKind
1431 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
Guy Benyei11169dd2012-12-18 14:30:41 +00001432 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1433 ID, BaseOffset + Record[0]);
Vedant Kumar48b4f762018-04-14 01:40:48 +00001434 SrcMgr::FileInfo &FileInfo =
1435 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Guy Benyei11169dd2012-12-18 14:30:41 +00001436 FileInfo.NumCreatedFIDs = Record[5];
1437 if (Record[3])
1438 FileInfo.setHasLineDirectives();
1439
1440 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1441 unsigned NumFileDecls = Record[7];
Richard Smithdbafb6c2017-06-29 23:23:46 +00001442 if (NumFileDecls && ContextObj) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001443 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1444 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1445 NumFileDecls));
1446 }
Richard Smithaada85c2016-02-06 02:06:43 +00001447
Guy Benyei11169dd2012-12-18 14:30:41 +00001448 const SrcMgr::ContentCache *ContentCache
Richard Smithf3f84612017-06-29 02:19:42 +00001449 = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
Guy Benyei11169dd2012-12-18 14:30:41 +00001450 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
Richard Smitha8cfffa2015-11-26 02:04:16 +00001451 ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1452 !ContentCache->getRawBuffer()) {
Richard Smithaada85c2016-02-06 02:06:43 +00001453 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1454 if (!Buffer)
Guy Benyei11169dd2012-12-18 14:30:41 +00001455 return true;
David Blaikie49cc3182014-08-27 20:54:45 +00001456 SourceMgr.overrideFileContents(File, std::move(Buffer));
Guy Benyei11169dd2012-12-18 14:30:41 +00001457 }
1458
1459 break;
1460 }
1461
1462 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00001463 const char *Name = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00001464 unsigned Offset = Record[0];
Vedant Kumar48b4f762018-04-14 01:40:48 +00001465 SrcMgr::CharacteristicKind
1466 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
Guy Benyei11169dd2012-12-18 14:30:41 +00001467 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Manman Ren11f2a472016-08-18 17:42:15 +00001468 if (IncludeLoc.isInvalid() && F->isModule()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001469 IncludeLoc = getImportLocation(F);
1470 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001471
Richard Smithaada85c2016-02-06 02:06:43 +00001472 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1473 if (!Buffer)
Guy Benyei11169dd2012-12-18 14:30:41 +00001474 return true;
David Blaikie50a5f972014-08-29 07:59:55 +00001475 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
Rafael Espindolad87f8d72014-08-27 20:03:29 +00001476 BaseOffset + Offset, IncludeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001477 break;
1478 }
1479
1480 case SM_SLOC_EXPANSION_ENTRY: {
1481 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1482 SourceMgr.createExpansionLoc(SpellingLoc,
1483 ReadSourceLocation(*F, Record[2]),
1484 ReadSourceLocation(*F, Record[3]),
Richard Smithb5f81712018-04-30 05:25:48 +00001485 Record[5],
Guy Benyei11169dd2012-12-18 14:30:41 +00001486 Record[4],
1487 ID,
1488 BaseOffset + Record[0]);
1489 break;
1490 }
1491 }
1492
1493 return false;
1494}
1495
1496std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1497 if (ID == 0)
1498 return std::make_pair(SourceLocation(), "");
1499
1500 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1501 Error("source location entry ID out-of-range for AST file");
1502 return std::make_pair(SourceLocation(), "");
1503 }
1504
1505 // Find which module file this entry lands in.
1506 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
Manman Ren11f2a472016-08-18 17:42:15 +00001507 if (!M->isModule())
Guy Benyei11169dd2012-12-18 14:30:41 +00001508 return std::make_pair(SourceLocation(), "");
1509
1510 // FIXME: Can we map this down to a particular submodule? That would be
1511 // ideal.
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001512 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
Guy Benyei11169dd2012-12-18 14:30:41 +00001513}
1514
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001515/// Find the location where the module F is imported.
Guy Benyei11169dd2012-12-18 14:30:41 +00001516SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1517 if (F->ImportLoc.isValid())
1518 return F->ImportLoc;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001519
Guy Benyei11169dd2012-12-18 14:30:41 +00001520 // Otherwise we have a PCH. It's considered to be "imported" at the first
1521 // location of its includer.
1522 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001523 // Main file is the importer.
Yaron Keren8b563662015-10-03 10:46:20 +00001524 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001525 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
Guy Benyei11169dd2012-12-18 14:30:41 +00001526 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001527 return F->ImportedBy[0]->FirstLoc;
1528}
1529
1530/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1531/// specified cursor. Read the abbreviations that are at the top of the block
1532/// and then leave the cursor pointing into the block.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001533bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Richard Smith0516b182015-09-08 19:40:14 +00001534 if (Cursor.EnterSubBlock(BlockID))
1535 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001536
1537 while (true) {
1538 uint64_t Offset = Cursor.GetCurrentBitNo();
1539 unsigned Code = Cursor.ReadCode();
1540
1541 // We expect all abbrevs to be at the start of the block.
1542 if (Code != llvm::bitc::DEFINE_ABBREV) {
1543 Cursor.JumpToBit(Offset);
1544 return false;
1545 }
1546 Cursor.ReadAbbrevRecord();
1547 }
1548}
1549
Richard Smithe40f2ba2013-08-07 21:41:30 +00001550Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
John McCallf413f5e2013-05-03 00:10:13 +00001551 unsigned &Idx) {
1552 Token Tok;
1553 Tok.startToken();
1554 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1555 Tok.setLength(Record[Idx++]);
1556 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1557 Tok.setIdentifierInfo(II);
1558 Tok.setKind((tok::TokenKind)Record[Idx++]);
1559 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1560 return Tok;
1561}
1562
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001563MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001564 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001565
1566 // Keep track of where we are in the stream, then jump back there
1567 // after reading this macro.
1568 SavedStreamPosition SavedPosition(Stream);
1569
1570 Stream.JumpToBit(Offset);
1571 RecordData Record;
Faisal Valiac506d72017-07-17 17:18:43 +00001572 SmallVector<IdentifierInfo*, 16> MacroParams;
Craig Toppera13603a2014-05-22 05:54:18 +00001573 MacroInfo *Macro = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001574
Guy Benyei11169dd2012-12-18 14:30:41 +00001575 while (true) {
Chris Lattnerefa77172013-01-20 00:00:22 +00001576 // Advance to the next record, but if we get to the end of the block, don't
1577 // pop it (removing all the abbreviations from the cursor) since we want to
1578 // be able to reseek within the block and read entries.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001579 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattnerefa77172013-01-20 00:00:22 +00001580 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001581
Chris Lattnerefa77172013-01-20 00:00:22 +00001582 switch (Entry.Kind) {
1583 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1584 case llvm::BitstreamEntry::Error:
1585 Error("malformed block record in AST file");
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001586 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001587 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001588 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001589 case llvm::BitstreamEntry::Record:
1590 // The interesting case.
1591 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001592 }
1593
1594 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001595 Record.clear();
Vedant Kumar48b4f762018-04-14 01:40:48 +00001596 PreprocessorRecordTypes RecType =
1597 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001598 switch (RecType) {
Richard Smithd7329392015-04-21 21:46:32 +00001599 case PP_MODULE_MACRO:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001600 case PP_MACRO_DIRECTIVE_HISTORY:
1601 return Macro;
1602
Guy Benyei11169dd2012-12-18 14:30:41 +00001603 case PP_MACRO_OBJECT_LIKE:
1604 case PP_MACRO_FUNCTION_LIKE: {
1605 // If we already have a macro, that means that we've hit the end
1606 // of the definition of the macro we were looking for. We're
1607 // done.
1608 if (Macro)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001609 return Macro;
Guy Benyei11169dd2012-12-18 14:30:41 +00001610
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001611 unsigned NextIndex = 1; // Skip identifier ID.
Guy Benyei11169dd2012-12-18 14:30:41 +00001612 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Richard Smith3f6dd7a2017-05-12 23:40:52 +00001613 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Argyrios Kyrtzidis7572be22013-01-07 19:16:23 +00001614 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei11169dd2012-12-18 14:30:41 +00001615 MI->setIsUsed(Record[NextIndex++]);
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00001616 MI->setUsedForHeaderGuard(Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001617
Guy Benyei11169dd2012-12-18 14:30:41 +00001618 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1619 // Decode function-like macro info.
1620 bool isC99VarArgs = Record[NextIndex++];
1621 bool isGNUVarArgs = Record[NextIndex++];
1622 bool hasCommaPasting = Record[NextIndex++];
Faisal Valiac506d72017-07-17 17:18:43 +00001623 MacroParams.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00001624 unsigned NumArgs = Record[NextIndex++];
1625 for (unsigned i = 0; i != NumArgs; ++i)
Faisal Valiac506d72017-07-17 17:18:43 +00001626 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00001627
1628 // Install function-like macro info.
1629 MI->setIsFunctionLike();
1630 if (isC99VarArgs) MI->setIsC99Varargs();
1631 if (isGNUVarArgs) MI->setIsGNUVarargs();
1632 if (hasCommaPasting) MI->setHasCommaPasting();
Faisal Valiac506d72017-07-17 17:18:43 +00001633 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
Guy Benyei11169dd2012-12-18 14:30:41 +00001634 }
1635
Guy Benyei11169dd2012-12-18 14:30:41 +00001636 // Remember that we saw this macro last so that we add the tokens that
1637 // form its body to it.
1638 Macro = MI;
1639
1640 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1641 Record[NextIndex]) {
1642 // We have a macro definition. Register the association
1643 PreprocessedEntityID
1644 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1645 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Richard Smith66a81862015-05-04 02:25:31 +00001646 PreprocessingRecord::PPEntityID PPID =
1647 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
Vedant Kumar48b4f762018-04-14 01:40:48 +00001648 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
Richard Smith66a81862015-05-04 02:25:31 +00001649 PPRec.getPreprocessedEntity(PPID));
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +00001650 if (PPDef)
1651 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei11169dd2012-12-18 14:30:41 +00001652 }
1653
1654 ++NumMacrosRead;
1655 break;
1656 }
1657
1658 case PP_TOKEN: {
1659 // If we see a TOKEN before a PP_MACRO_*, then the file is
1660 // erroneous, just pretend we didn't see this.
Craig Toppera13603a2014-05-22 05:54:18 +00001661 if (!Macro) break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001662
John McCallf413f5e2013-05-03 00:10:13 +00001663 unsigned Idx = 0;
1664 Token Tok = ReadToken(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001665 Macro->AddTokenToBody(Tok);
1666 break;
1667 }
1668 }
1669 }
1670}
1671
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001672PreprocessedEntityID
Richard Smith37a93df2017-02-18 00:32:02 +00001673ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1674 unsigned LocalID) const {
1675 if (!M.ModuleOffsetMap.empty())
1676 ReadModuleOffsetMap(M);
1677
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001678 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Guy Benyei11169dd2012-12-18 14:30:41 +00001679 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001680 assert(I != M.PreprocessedEntityRemap.end()
Guy Benyei11169dd2012-12-18 14:30:41 +00001681 && "Invalid index into preprocessed entity index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001682
Guy Benyei11169dd2012-12-18 14:30:41 +00001683 return LocalID + I->second;
1684}
1685
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001686unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1687 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei11169dd2012-12-18 14:30:41 +00001688}
Richard Smith7ed1bc92014-12-05 22:42:13 +00001689
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001690HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001691HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
Richard Smithe75ee0f2015-08-17 07:13:32 +00001692 internal_key_type ikey = {FE->getSize(),
1693 M.HasTimestamps ? FE->getModificationTime() : 0,
1694 FE->getName(), /*Imported*/ false};
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001695 return ikey;
1696}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001697
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001698bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
Richard Smithe75ee0f2015-08-17 07:13:32 +00001699 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
Guy Benyei11169dd2012-12-18 14:30:41 +00001700 return false;
1701
Mehdi Amini004b9c72016-10-10 22:52:47 +00001702 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001703 return true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001704
Guy Benyei11169dd2012-12-18 14:30:41 +00001705 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001706 FileManager &FileMgr = Reader.getFileManager();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001707 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1708 if (!Key.Imported)
1709 return FileMgr.getFile(Key.Filename);
1710
1711 std::string Resolved = Key.Filename;
1712 Reader.ResolveImportedPath(M, Resolved);
1713 return FileMgr.getFile(Resolved);
1714 };
1715
1716 const FileEntry *FEA = GetFile(a);
1717 const FileEntry *FEB = GetFile(b);
1718 return FEA && FEA == FEB;
Guy Benyei11169dd2012-12-18 14:30:41 +00001719}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001720
Guy Benyei11169dd2012-12-18 14:30:41 +00001721std::pair<unsigned, unsigned>
1722HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001723 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001724
Vedant Kumar48b4f762018-04-14 01:40:48 +00001725 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1726 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001727 return std::make_pair(KeyLen, DataLen);
Guy Benyei11169dd2012-12-18 14:30:41 +00001728}
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001729
1730HeaderFileInfoTrait::internal_key_type
1731HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001732 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001733
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001734 internal_key_type ikey;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001735 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1736 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001737 ikey.Filename = (const char *)d;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001738 ikey.Imported = true;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001739 return ikey;
1740}
1741
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001742HeaderFileInfoTrait::data_type
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001743HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei11169dd2012-12-18 14:30:41 +00001744 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001745 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001746
1747 const unsigned char *End = d + DataLen;
Guy Benyei11169dd2012-12-18 14:30:41 +00001748 HeaderFileInfo HFI;
1749 unsigned Flags = *d++;
Richard Smith386bb072015-08-18 23:42:23 +00001750 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
Richard Smithf3f84612017-06-29 02:19:42 +00001751 HFI.isImport |= (Flags >> 5) & 0x01;
1752 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1753 HFI.DirInfo = (Flags >> 1) & 0x07;
Guy Benyei11169dd2012-12-18 14:30:41 +00001754 HFI.IndexHeaderMapHeader = Flags & 0x01;
Richard Smith386bb072015-08-18 23:42:23 +00001755 // FIXME: Find a better way to handle this. Maybe just store a
1756 // "has been included" flag?
1757 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1758 HFI.NumIncludes);
Justin Bogner57ba0b22014-03-28 22:03:24 +00001759 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1760 M, endian::readNext<uint32_t, little, unaligned>(d));
1761 if (unsigned FrameworkOffset =
1762 endian::readNext<uint32_t, little, unaligned>(d)) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001763 // The framework offset is 1 greater than the actual offset,
Guy Benyei11169dd2012-12-18 14:30:41 +00001764 // since 0 is used as an indicator for "no framework name".
1765 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1766 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1767 }
Richard Smith386bb072015-08-18 23:42:23 +00001768
1769 assert((End - d) % 4 == 0 &&
1770 "Wrong data length in HeaderFileInfo deserialization");
1771 while (d != End) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001772 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
Richard Smith386bb072015-08-18 23:42:23 +00001773 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1774 LocalSMID >>= 2;
1775
1776 // This header is part of a module. Associate it with the module to enable
1777 // implicit module import.
1778 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1779 Module *Mod = Reader.getSubmodule(GlobalSMID);
1780 FileManager &FileMgr = Reader.getFileManager();
1781 ModuleMap &ModMap =
1782 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1783
1784 std::string Filename = key.Filename;
1785 if (key.Imported)
1786 Reader.ResolveImportedPath(M, Filename);
1787 // FIXME: This is not always the right filename-as-written, but we're not
1788 // going to use this information to rebuild the module, so it doesn't make
1789 // a lot of difference.
1790 Module::Header H = { key.Filename, FileMgr.getFile(Filename) };
Richard Smithd8879c82015-08-24 21:59:32 +00001791 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1792 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001793 }
1794
Guy Benyei11169dd2012-12-18 14:30:41 +00001795 // This HeaderFileInfo was externally loaded.
1796 HFI.External = true;
Richard Smithd8879c82015-08-24 21:59:32 +00001797 HFI.IsValid = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001798 return HFI;
1799}
1800
Richard Smithd7329392015-04-21 21:46:32 +00001801void ASTReader::addPendingMacro(IdentifierInfo *II,
1802 ModuleFile *M,
1803 uint64_t MacroDirectivesOffset) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001804 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1805 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00001806}
1807
1808void ASTReader::ReadDefinedMacros() {
1809 // Note that we are loading defined macros.
1810 Deserializing Macros(this);
1811
Vedant Kumar48b4f762018-04-14 01:40:48 +00001812 for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001813 BitstreamCursor &MacroCursor = I.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001814
1815 // If there was no preprocessor block, skip this file.
Peter Collingbourne77c89b62016-11-08 04:17:11 +00001816 if (MacroCursor.getBitcodeBytes().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00001817 continue;
1818
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001819 BitstreamCursor Cursor = MacroCursor;
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001820 Cursor.JumpToBit(I.MacroStartOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00001821
1822 RecordData Record;
1823 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001824 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001825
Chris Lattnere7b154b2013-01-19 21:39:22 +00001826 switch (E.Kind) {
1827 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1828 case llvm::BitstreamEntry::Error:
1829 Error("malformed block record in AST file");
1830 return;
1831 case llvm::BitstreamEntry::EndBlock:
1832 goto NextCursor;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001833
Chris Lattnere7b154b2013-01-19 21:39:22 +00001834 case llvm::BitstreamEntry::Record:
Chris Lattnere7b154b2013-01-19 21:39:22 +00001835 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001836 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001837 default: // Default behavior: ignore.
1838 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001839
Chris Lattnere7b154b2013-01-19 21:39:22 +00001840 case PP_MACRO_OBJECT_LIKE:
Sean Callananf3682a72016-05-14 06:24:14 +00001841 case PP_MACRO_FUNCTION_LIKE: {
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001842 IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
Sean Callananf3682a72016-05-14 06:24:14 +00001843 if (II->isOutOfDate())
1844 updateOutOfDateIdentifier(*II);
Chris Lattnere7b154b2013-01-19 21:39:22 +00001845 break;
Sean Callananf3682a72016-05-14 06:24:14 +00001846 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001847
Chris Lattnere7b154b2013-01-19 21:39:22 +00001848 case PP_TOKEN:
1849 // Ignore tokens.
1850 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001851 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001852 break;
1853 }
1854 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001855 NextCursor: ;
Guy Benyei11169dd2012-12-18 14:30:41 +00001856 }
1857}
1858
1859namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001860
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001861 /// Visitor class used to look up identifirs in an AST file.
Guy Benyei11169dd2012-12-18 14:30:41 +00001862 class IdentifierLookupVisitor {
1863 StringRef Name;
Richard Smith3b637412015-07-14 18:42:41 +00001864 unsigned NameHash;
Guy Benyei11169dd2012-12-18 14:30:41 +00001865 unsigned PriorGeneration;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001866 unsigned &NumIdentifierLookups;
1867 unsigned &NumIdentifierLookupHits;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001868 IdentifierInfo *Found = nullptr;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001869
Guy Benyei11169dd2012-12-18 14:30:41 +00001870 public:
Douglas Gregor00a50f72013-01-25 00:38:33 +00001871 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1872 unsigned &NumIdentifierLookups,
1873 unsigned &NumIdentifierLookupHits)
Richard Smith3b637412015-07-14 18:42:41 +00001874 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
1875 PriorGeneration(PriorGeneration),
Douglas Gregor00a50f72013-01-25 00:38:33 +00001876 NumIdentifierLookups(NumIdentifierLookups),
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001877 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00001878
1879 bool operator()(ModuleFile &M) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001880 // If we've already searched this module file, skip it now.
Richard Smithbdf2d932015-07-30 03:37:16 +00001881 if (M.Generation <= PriorGeneration)
Guy Benyei11169dd2012-12-18 14:30:41 +00001882 return true;
Douglas Gregore060e572013-01-25 01:03:03 +00001883
Vedant Kumar48b4f762018-04-14 01:40:48 +00001884 ASTIdentifierLookupTable *IdTable
1885 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
Guy Benyei11169dd2012-12-18 14:30:41 +00001886 if (!IdTable)
1887 return false;
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00001888
1889 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
Richard Smithbdf2d932015-07-30 03:37:16 +00001890 Found);
1891 ++NumIdentifierLookups;
Richard Smith3b637412015-07-14 18:42:41 +00001892 ASTIdentifierLookupTable::iterator Pos =
Richard Smithbdf2d932015-07-30 03:37:16 +00001893 IdTable->find_hashed(Name, NameHash, &Trait);
Guy Benyei11169dd2012-12-18 14:30:41 +00001894 if (Pos == IdTable->end())
1895 return false;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001896
Guy Benyei11169dd2012-12-18 14:30:41 +00001897 // Dereferencing the iterator has the effect of building the
1898 // IdentifierInfo node and populating it with the various
1899 // declarations it needs.
Richard Smithbdf2d932015-07-30 03:37:16 +00001900 ++NumIdentifierLookupHits;
1901 Found = *Pos;
Guy Benyei11169dd2012-12-18 14:30:41 +00001902 return true;
1903 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001904
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001905 // Retrieve the identifier info found within the module
Guy Benyei11169dd2012-12-18 14:30:41 +00001906 // files.
1907 IdentifierInfo *getIdentifierInfo() const { return Found; }
1908 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001909
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001910} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00001911
1912void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1913 // Note that we are loading an identifier.
1914 Deserializing AnIdentifier(this);
1915
1916 unsigned PriorGeneration = 0;
1917 if (getContext().getLangOpts().Modules)
1918 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregore060e572013-01-25 01:03:03 +00001919
1920 // If there is a global index, look there first to determine which modules
1921 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00001922 GlobalModuleIndex::HitSet Hits;
Craig Toppera13603a2014-05-22 05:54:18 +00001923 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
Douglas Gregore060e572013-01-25 01:03:03 +00001924 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00001925 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1926 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00001927 }
1928 }
1929
Douglas Gregor7211ac12013-01-25 23:32:03 +00001930 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregor00a50f72013-01-25 00:38:33 +00001931 NumIdentifierLookups,
1932 NumIdentifierLookupHits);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00001933 ModuleMgr.visit(Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00001934 markIdentifierUpToDate(&II);
1935}
1936
1937void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1938 if (!II)
1939 return;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001940
Guy Benyei11169dd2012-12-18 14:30:41 +00001941 II->setOutOfDate(false);
1942
1943 // Update the generation for this identifier.
1944 if (getContext().getLangOpts().Modules)
Richard Smith053f6c62014-05-16 23:01:30 +00001945 IdentifierGeneration[II] = getGeneration();
Guy Benyei11169dd2012-12-18 14:30:41 +00001946}
1947
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001948void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1949 const PendingMacroInfo &PMInfo) {
Richard Smithd7329392015-04-21 21:46:32 +00001950 ModuleFile &M = *PMInfo.M;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001951
1952 BitstreamCursor &Cursor = M.MacroCursor;
1953 SavedStreamPosition SavedPosition(Cursor);
Richard Smithd7329392015-04-21 21:46:32 +00001954 Cursor.JumpToBit(PMInfo.MacroDirectivesOffset);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001955
Richard Smith713369b2015-04-23 20:40:50 +00001956 struct ModuleMacroRecord {
1957 SubmoduleID SubModID;
1958 MacroInfo *MI;
1959 SmallVector<SubmoduleID, 8> Overrides;
1960 };
1961 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001962
Richard Smithd7329392015-04-21 21:46:32 +00001963 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
1964 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
1965 // macro histroy.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001966 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00001967 while (true) {
1968 llvm::BitstreamEntry Entry =
1969 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1970 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1971 Error("malformed block record in AST file");
1972 return;
1973 }
1974
1975 Record.clear();
Aaron Ballmanc75a1922015-04-22 15:25:05 +00001976 switch ((PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Richard Smithd7329392015-04-21 21:46:32 +00001977 case PP_MACRO_DIRECTIVE_HISTORY:
1978 break;
1979
1980 case PP_MODULE_MACRO: {
Richard Smith713369b2015-04-23 20:40:50 +00001981 ModuleMacros.push_back(ModuleMacroRecord());
1982 auto &Info = ModuleMacros.back();
Richard Smithe56c8bc2015-04-22 00:26:11 +00001983 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
1984 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
Richard Smith713369b2015-04-23 20:40:50 +00001985 for (int I = 2, N = Record.size(); I != N; ++I)
1986 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
Richard Smithd7329392015-04-21 21:46:32 +00001987 continue;
1988 }
1989
1990 default:
1991 Error("malformed block record in AST file");
1992 return;
1993 }
1994
1995 // We found the macro directive history; that's the last record
1996 // for this macro.
1997 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001998 }
1999
Richard Smithd7329392015-04-21 21:46:32 +00002000 // Module macros are listed in reverse dependency order.
Richard Smithe56c8bc2015-04-22 00:26:11 +00002001 {
2002 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
Richard Smithe56c8bc2015-04-22 00:26:11 +00002003 llvm::SmallVector<ModuleMacro*, 8> Overrides;
Richard Smith713369b2015-04-23 20:40:50 +00002004 for (auto &MMR : ModuleMacros) {
Richard Smithe56c8bc2015-04-22 00:26:11 +00002005 Overrides.clear();
Vedant Kumar48b4f762018-04-14 01:40:48 +00002006 for (unsigned ModID : MMR.Overrides) {
Richard Smithb8b2ed62015-04-23 18:18:26 +00002007 Module *Mod = getSubmodule(ModID);
2008 auto *Macro = PP.getModuleMacro(Mod, II);
Richard Smithe56c8bc2015-04-22 00:26:11 +00002009 assert(Macro && "missing definition for overridden macro");
Richard Smith5dbef922015-04-22 02:09:43 +00002010 Overrides.push_back(Macro);
Richard Smithe56c8bc2015-04-22 00:26:11 +00002011 }
2012
2013 bool Inserted = false;
Richard Smith713369b2015-04-23 20:40:50 +00002014 Module *Owner = getSubmodule(MMR.SubModID);
Richard Smith20e883e2015-04-29 23:20:19 +00002015 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
Richard Smithd7329392015-04-21 21:46:32 +00002016 }
2017 }
2018
2019 // Don't read the directive history for a module; we don't have anywhere
2020 // to put it.
Manman Ren11f2a472016-08-18 17:42:15 +00002021 if (M.isModule())
Richard Smithd7329392015-04-21 21:46:32 +00002022 return;
2023
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002024 // Deserialize the macro directives history in reverse source-order.
Craig Toppera13603a2014-05-22 05:54:18 +00002025 MacroDirective *Latest = nullptr, *Earliest = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002026 unsigned Idx = 0, N = Record.size();
2027 while (Idx < N) {
Craig Toppera13603a2014-05-22 05:54:18 +00002028 MacroDirective *MD = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002029 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00002030 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002031 switch (K) {
2032 case MacroDirective::MD_Define: {
Richard Smith713369b2015-04-23 20:40:50 +00002033 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
Richard Smith3981b172015-04-30 02:16:23 +00002034 MD = PP.AllocateDefMacroDirective(MI, Loc);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002035 break;
2036 }
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002037 case MacroDirective::MD_Undefine:
Richard Smith3981b172015-04-30 02:16:23 +00002038 MD = PP.AllocateUndefMacroDirective(Loc);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002039 break;
Richard Smithdaa69e02014-07-25 04:40:03 +00002040 case MacroDirective::MD_Visibility:
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002041 bool isPublic = Record[Idx++];
2042 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2043 break;
2044 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002045
2046 if (!Latest)
2047 Latest = MD;
2048 if (Earliest)
2049 Earliest->setPrevious(MD);
2050 Earliest = MD;
2051 }
2052
Richard Smithd6e8c0d2015-05-04 19:58:00 +00002053 if (Latest)
Nico Weberfd870702016-12-09 17:32:52 +00002054 PP.setLoadedMacroDirective(II, Earliest, Latest);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002055}
2056
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002057ASTReader::InputFileInfo
2058ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002059 // Go find this input file.
2060 BitstreamCursor &Cursor = F.InputFilesCursor;
2061 SavedStreamPosition SavedPosition(Cursor);
2062 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2063
2064 unsigned Code = Cursor.ReadCode();
2065 RecordData Record;
2066 StringRef Blob;
2067
2068 unsigned Result = Cursor.readRecord(Code, Record, &Blob);
2069 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
2070 "invalid record type for input file");
2071 (void)Result;
2072
2073 assert(Record[0] == ID && "Bogus stored ID or offset");
Richard Smitha8cfffa2015-11-26 02:04:16 +00002074 InputFileInfo R;
2075 R.StoredSize = static_cast<off_t>(Record[1]);
2076 R.StoredTime = static_cast<time_t>(Record[2]);
2077 R.Overridden = static_cast<bool>(Record[3]);
2078 R.Transient = static_cast<bool>(Record[4]);
Richard Smithf3f84612017-06-29 02:19:42 +00002079 R.TopLevelModuleMap = static_cast<bool>(Record[5]);
Richard Smitha8cfffa2015-11-26 02:04:16 +00002080 R.Filename = Blob;
2081 ResolveImportedPath(F, R.Filename);
Hans Wennborg73945142014-03-14 17:45:06 +00002082 return R;
Ben Langmuir198c1682014-03-07 07:27:49 +00002083}
2084
Manman Renc8c94152016-10-21 23:35:03 +00002085static unsigned moduleKindForDiagnostic(ModuleKind Kind);
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002086InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002087 // If this ID is bogus, just return an empty input file.
2088 if (ID == 0 || ID > F.InputFilesLoaded.size())
2089 return InputFile();
2090
2091 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002092 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei11169dd2012-12-18 14:30:41 +00002093 return F.InputFilesLoaded[ID-1];
2094
Argyrios Kyrtzidis9308f0a2014-01-08 19:13:34 +00002095 if (F.InputFilesLoaded[ID-1].isNotFound())
2096 return InputFile();
2097
Guy Benyei11169dd2012-12-18 14:30:41 +00002098 // Go find this input file.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002099 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00002100 SavedStreamPosition SavedPosition(Cursor);
2101 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002102
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002103 InputFileInfo FI = readInputFileInfo(F, ID);
2104 off_t StoredSize = FI.StoredSize;
2105 time_t StoredTime = FI.StoredTime;
2106 bool Overridden = FI.Overridden;
Richard Smitha8cfffa2015-11-26 02:04:16 +00002107 bool Transient = FI.Transient;
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002108 StringRef Filename = FI.Filename;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002109
Richard Smitha8cfffa2015-11-26 02:04:16 +00002110 const FileEntry *File = FileMgr.getFile(Filename, /*OpenFile=*/false);
Ben Langmuir198c1682014-03-07 07:27:49 +00002111 // If we didn't find the file, resolve it relative to the
2112 // original directory from which this AST file was created.
Manuel Klimek1b29b4f2017-07-25 10:22:06 +00002113 if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2114 F.OriginalDir != F.BaseDirectory) {
2115 std::string Resolved = resolveFileRelativeToOriginalDir(
2116 Filename, F.OriginalDir, F.BaseDirectory);
Ben Langmuir198c1682014-03-07 07:27:49 +00002117 if (!Resolved.empty())
2118 File = FileMgr.getFile(Resolved);
2119 }
2120
2121 // For an overridden file, create a virtual file with the stored
2122 // size/timestamp.
Richard Smitha8cfffa2015-11-26 02:04:16 +00002123 if ((Overridden || Transient) && File == nullptr)
Ben Langmuir198c1682014-03-07 07:27:49 +00002124 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
Ben Langmuir198c1682014-03-07 07:27:49 +00002125
Craig Toppera13603a2014-05-22 05:54:18 +00002126 if (File == nullptr) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002127 if (Complain) {
2128 std::string ErrorStr = "could not find file '";
2129 ErrorStr += Filename;
Richard Smith68142212015-10-13 01:26:26 +00002130 ErrorStr += "' referenced by AST file '";
2131 ErrorStr += F.FileName;
2132 ErrorStr += "'";
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002133 Error(ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00002134 }
Ben Langmuir198c1682014-03-07 07:27:49 +00002135 // Record that we didn't find the file.
2136 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2137 return InputFile();
2138 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002139
Ben Langmuir198c1682014-03-07 07:27:49 +00002140 // Check if there was a request to override the contents of the file
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002141 // that was part of the precompiled header. Overriding such a file
Ben Langmuir198c1682014-03-07 07:27:49 +00002142 // can lead to problems when lexing using the source locations from the
2143 // PCH.
2144 SourceManager &SM = getSourceManager();
Richard Smith64daf7b2015-12-01 03:32:49 +00002145 // FIXME: Reject if the overrides are different.
2146 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002147 if (Complain)
2148 Error(diag::err_fe_pch_file_overridden, Filename);
2149 // After emitting the diagnostic, recover by disabling the override so
2150 // that the original file will be used.
Richard Smitha8cfffa2015-11-26 02:04:16 +00002151 //
2152 // FIXME: This recovery is just as broken as the original state; there may
2153 // be another precompiled module that's using the overridden contents, or
2154 // we might be half way through parsing it. Instead, we should treat the
2155 // overridden contents as belonging to a separate FileEntry.
Ben Langmuir198c1682014-03-07 07:27:49 +00002156 SM.disableFileContentsOverride(File);
2157 // The FileEntry is a virtual file entry with the size of the contents
2158 // that would override the original contents. Set it to the original's
2159 // size/time.
2160 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2161 StoredSize, StoredTime);
2162 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002163
Ben Langmuir198c1682014-03-07 07:27:49 +00002164 bool IsOutOfDate = false;
2165
2166 // For an overridden file, there is nothing to validate.
Richard Smith96fdab62014-10-28 16:24:08 +00002167 if (!Overridden && //
2168 (StoredSize != File->getSize() ||
Richard Smithe75ee0f2015-08-17 07:13:32 +00002169 (StoredTime && StoredTime != File->getModificationTime() &&
2170 !DisableValidation)
Ben Langmuir198c1682014-03-07 07:27:49 +00002171 )) {
2172 if (Complain) {
2173 // Build a list of the PCH imports that got us here (in reverse).
2174 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002175 while (!ImportStack.back()->ImportedBy.empty())
Ben Langmuir198c1682014-03-07 07:27:49 +00002176 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
Ben Langmuire82630d2014-01-17 00:19:09 +00002177
Ben Langmuir198c1682014-03-07 07:27:49 +00002178 // The top-level PCH is stale.
2179 StringRef TopLevelPCHName(ImportStack.back()->FileName);
Manman Renc8c94152016-10-21 23:35:03 +00002180 unsigned DiagnosticKind = moduleKindForDiagnostic(ImportStack.back()->Kind);
2181 if (DiagnosticKind == 0)
2182 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
2183 else if (DiagnosticKind == 1)
2184 Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName);
2185 else
2186 Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName);
Ben Langmuire82630d2014-01-17 00:19:09 +00002187
Ben Langmuir198c1682014-03-07 07:27:49 +00002188 // Print the import stack.
2189 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2190 Diag(diag::note_pch_required_by)
2191 << Filename << ImportStack[0]->FileName;
2192 for (unsigned I = 1; I < ImportStack.size(); ++I)
Ben Langmuire82630d2014-01-17 00:19:09 +00002193 Diag(diag::note_pch_required_by)
Ben Langmuir198c1682014-03-07 07:27:49 +00002194 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
Douglas Gregor7029ce12013-03-19 00:28:20 +00002195 }
2196
Ben Langmuir198c1682014-03-07 07:27:49 +00002197 if (!Diags.isDiagnosticInFlight())
2198 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
Guy Benyei11169dd2012-12-18 14:30:41 +00002199 }
2200
Ben Langmuir198c1682014-03-07 07:27:49 +00002201 IsOutOfDate = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00002202 }
Richard Smitha8cfffa2015-11-26 02:04:16 +00002203 // FIXME: If the file is overridden and we've already opened it,
2204 // issue an error (or split it into a separate FileEntry).
Guy Benyei11169dd2012-12-18 14:30:41 +00002205
Richard Smitha8cfffa2015-11-26 02:04:16 +00002206 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
Ben Langmuir198c1682014-03-07 07:27:49 +00002207
2208 // Note that we've loaded this input file.
2209 F.InputFilesLoaded[ID-1] = IF;
2210 return IF;
Guy Benyei11169dd2012-12-18 14:30:41 +00002211}
2212
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002213/// If we are loading a relocatable PCH or module file, and the filename
Richard Smith7ed1bc92014-12-05 22:42:13 +00002214/// is not an absolute path, add the system or module root to the beginning of
2215/// the file name.
2216void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2217 // Resolve relative to the base directory, if we have one.
2218 if (!M.BaseDirectory.empty())
2219 return ResolveImportedPath(Filename, M.BaseDirectory);
Guy Benyei11169dd2012-12-18 14:30:41 +00002220}
2221
Richard Smith7ed1bc92014-12-05 22:42:13 +00002222void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002223 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2224 return;
2225
Richard Smith7ed1bc92014-12-05 22:42:13 +00002226 SmallString<128> Buffer;
2227 llvm::sys::path::append(Buffer, Prefix, Filename);
2228 Filename.assign(Buffer.begin(), Buffer.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00002229}
2230
Richard Smith0f99d6a2015-08-09 08:48:41 +00002231static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2232 switch (ARR) {
2233 case ASTReader::Failure: return true;
2234 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2235 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2236 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2237 case ASTReader::ConfigurationMismatch:
2238 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2239 case ASTReader::HadErrors: return true;
2240 case ASTReader::Success: return false;
2241 }
2242
2243 llvm_unreachable("unknown ASTReadResult");
2244}
2245
Richard Smith0516b182015-09-08 19:40:14 +00002246ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2247 BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2248 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002249 std::string &SuggestedPredefines) {
Richard Smith0516b182015-09-08 19:40:14 +00002250 if (Stream.EnterSubBlock(OPTIONS_BLOCK_ID))
2251 return Failure;
2252
2253 // Read all of the records in the options block.
2254 RecordData Record;
2255 ASTReadResult Result = Success;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002256 while (true) {
Richard Smith0516b182015-09-08 19:40:14 +00002257 llvm::BitstreamEntry Entry = Stream.advance();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002258
Richard Smith0516b182015-09-08 19:40:14 +00002259 switch (Entry.Kind) {
2260 case llvm::BitstreamEntry::Error:
2261 case llvm::BitstreamEntry::SubBlock:
2262 return Failure;
2263
2264 case llvm::BitstreamEntry::EndBlock:
2265 return Result;
2266
2267 case llvm::BitstreamEntry::Record:
2268 // The interesting case.
2269 break;
2270 }
2271
2272 // Read and process a record.
2273 Record.clear();
2274 switch ((OptionsRecordTypes)Stream.readRecord(Entry.ID, Record)) {
2275 case LANGUAGE_OPTIONS: {
2276 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2277 if (ParseLanguageOptions(Record, Complain, Listener,
2278 AllowCompatibleConfigurationMismatch))
2279 Result = ConfigurationMismatch;
2280 break;
2281 }
2282
2283 case TARGET_OPTIONS: {
2284 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2285 if (ParseTargetOptions(Record, Complain, Listener,
2286 AllowCompatibleConfigurationMismatch))
2287 Result = ConfigurationMismatch;
2288 break;
2289 }
2290
Richard Smith0516b182015-09-08 19:40:14 +00002291 case FILE_SYSTEM_OPTIONS: {
2292 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2293 if (!AllowCompatibleConfigurationMismatch &&
2294 ParseFileSystemOptions(Record, Complain, Listener))
2295 Result = ConfigurationMismatch;
2296 break;
2297 }
2298
2299 case HEADER_SEARCH_OPTIONS: {
2300 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2301 if (!AllowCompatibleConfigurationMismatch &&
2302 ParseHeaderSearchOptions(Record, Complain, Listener))
2303 Result = ConfigurationMismatch;
2304 break;
2305 }
2306
2307 case PREPROCESSOR_OPTIONS:
2308 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2309 if (!AllowCompatibleConfigurationMismatch &&
2310 ParsePreprocessorOptions(Record, Complain, Listener,
2311 SuggestedPredefines))
2312 Result = ConfigurationMismatch;
2313 break;
2314 }
2315 }
2316}
2317
Guy Benyei11169dd2012-12-18 14:30:41 +00002318ASTReader::ASTReadResult
2319ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002320 SmallVectorImpl<ImportedModule> &Loaded,
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002321 const ModuleFile *ImportedBy,
Guy Benyei11169dd2012-12-18 14:30:41 +00002322 unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002323 BitstreamCursor &Stream = F.Stream;
Richard Smith8a308ec2015-11-05 00:54:55 +00002324 ASTReadResult Result = Success;
Guy Benyei11169dd2012-12-18 14:30:41 +00002325
2326 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2327 Error("malformed block record in AST file");
2328 return Failure;
2329 }
2330
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002331 // Lambda to read the unhashed control block the first time it's called.
2332 //
2333 // For PCM files, the unhashed control block cannot be read until after the
2334 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
2335 // need to look ahead before reading the IMPORTS record. For consistency,
2336 // this block is always read somehow (see BitstreamEntry::EndBlock).
2337 bool HasReadUnhashedControlBlock = false;
2338 auto readUnhashedControlBlockOnce = [&]() {
2339 if (!HasReadUnhashedControlBlock) {
2340 HasReadUnhashedControlBlock = true;
2341 if (ASTReadResult Result =
2342 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2343 return Result;
2344 }
2345 return Success;
2346 };
2347
Guy Benyei11169dd2012-12-18 14:30:41 +00002348 // Read all of the records and blocks in the control block.
2349 RecordData Record;
Richard Smitha1825302014-10-23 22:18:29 +00002350 unsigned NumInputs = 0;
2351 unsigned NumUserInputs = 0;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002352 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00002353 llvm::BitstreamEntry Entry = Stream.advance();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002354
Chris Lattnere7b154b2013-01-19 21:39:22 +00002355 switch (Entry.Kind) {
2356 case llvm::BitstreamEntry::Error:
2357 Error("malformed block record in AST file");
2358 return Failure;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002359 case llvm::BitstreamEntry::EndBlock: {
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002360 // Validate the module before returning. This call catches an AST with
2361 // no module name and no imports.
2362 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2363 return Result;
2364
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002365 // Validate input files.
2366 const HeaderSearchOptions &HSOpts =
2367 PP.getHeaderSearchInfo().getHeaderSearchOpts();
Ben Langmuircb69b572014-03-07 06:40:32 +00002368
Richard Smitha1825302014-10-23 22:18:29 +00002369 // All user input files reside at the index range [0, NumUserInputs), and
Richard Smith0f99d6a2015-08-09 08:48:41 +00002370 // system input files reside at [NumUserInputs, NumInputs). For explicitly
2371 // loaded module files, ignore missing inputs.
Manman Ren11f2a472016-08-18 17:42:15 +00002372 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2373 F.Kind != MK_PrebuiltModule) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002374 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Ben Langmuircb69b572014-03-07 06:40:32 +00002375
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002376 // If we are reading a module, we will create a verification timestamp,
2377 // so we verify all input files. Otherwise, verify only user input
2378 // files.
Ben Langmuircb69b572014-03-07 06:40:32 +00002379
2380 unsigned N = NumUserInputs;
2381 if (ValidateSystemInputs ||
Richard Smithe842a472014-10-22 02:05:46 +00002382 (HSOpts.ModulesValidateOncePerBuildSession &&
Ben Langmuiracb803e2014-11-10 22:13:10 +00002383 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
Richard Smithe842a472014-10-22 02:05:46 +00002384 F.Kind == MK_ImplicitModule))
Ben Langmuircb69b572014-03-07 06:40:32 +00002385 N = NumInputs;
2386
Ben Langmuir3d4417c2014-02-07 17:31:11 +00002387 for (unsigned I = 0; I < N; ++I) {
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002388 InputFile IF = getInputFile(F, I+1, Complain);
2389 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei11169dd2012-12-18 14:30:41 +00002390 return OutOfDate;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002391 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002392 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002393
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002394 if (Listener)
Richard Smith216a3bd2015-08-13 17:57:10 +00002395 Listener->visitModuleFile(F.FileName, F.Kind);
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002396
Ben Langmuircb69b572014-03-07 06:40:32 +00002397 if (Listener && Listener->needsInputFileVisitation()) {
2398 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2399 : NumUserInputs;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002400 for (unsigned I = 0; I < N; ++I) {
2401 bool IsSystem = I >= NumUserInputs;
2402 InputFileInfo FI = readInputFileInfo(F, I+1);
Richard Smith216a3bd2015-08-13 17:57:10 +00002403 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
Manman Ren11f2a472016-08-18 17:42:15 +00002404 F.Kind == MK_ExplicitModule ||
2405 F.Kind == MK_PrebuiltModule);
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002406 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002407 }
2408
Richard Smith8a308ec2015-11-05 00:54:55 +00002409 return Result;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002410 }
2411
Chris Lattnere7b154b2013-01-19 21:39:22 +00002412 case llvm::BitstreamEntry::SubBlock:
2413 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002414 case INPUT_FILES_BLOCK_ID:
2415 F.InputFilesCursor = Stream;
2416 if (Stream.SkipBlock() || // Skip with the main cursor
2417 // Read the abbreviations
2418 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2419 Error("malformed block record in AST file");
2420 return Failure;
2421 }
2422 continue;
Richard Smith0516b182015-09-08 19:40:14 +00002423
2424 case OPTIONS_BLOCK_ID:
2425 // If we're reading the first module for this group, check its options
2426 // are compatible with ours. For modules it imports, no further checking
2427 // is required, because we checked them when we built it.
2428 if (Listener && !ImportedBy) {
2429 // Should we allow the configuration of the module file to differ from
2430 // the configuration of the current translation unit in a compatible
2431 // way?
2432 //
2433 // FIXME: Allow this for files explicitly specified with -include-pch.
2434 bool AllowCompatibleConfigurationMismatch =
Manman Ren11f2a472016-08-18 17:42:15 +00002435 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
Richard Smith0516b182015-09-08 19:40:14 +00002436
Richard Smith8a308ec2015-11-05 00:54:55 +00002437 Result = ReadOptionsBlock(Stream, ClientLoadCapabilities,
2438 AllowCompatibleConfigurationMismatch,
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002439 *Listener, SuggestedPredefines);
Richard Smith0516b182015-09-08 19:40:14 +00002440 if (Result == Failure) {
2441 Error("malformed block record in AST file");
2442 return Result;
2443 }
2444
Richard Smith8a308ec2015-11-05 00:54:55 +00002445 if (DisableValidation ||
2446 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2447 Result = Success;
2448
Ben Langmuir9b1e442e2016-02-11 18:54:02 +00002449 // If we can't load the module, exit early since we likely
2450 // will rebuild the module anyway. The stream may be in the
2451 // middle of a block.
2452 if (Result != Success)
Richard Smith0516b182015-09-08 19:40:14 +00002453 return Result;
2454 } else if (Stream.SkipBlock()) {
2455 Error("malformed block record in AST file");
2456 return Failure;
2457 }
2458 continue;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002459
Guy Benyei11169dd2012-12-18 14:30:41 +00002460 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002461 if (Stream.SkipBlock()) {
2462 Error("malformed block record in AST file");
2463 return Failure;
2464 }
2465 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00002466 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002467
Chris Lattnere7b154b2013-01-19 21:39:22 +00002468 case llvm::BitstreamEntry::Record:
2469 // The interesting case.
2470 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002471 }
2472
2473 // Read and process a record.
2474 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002475 StringRef Blob;
2476 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002477 case METADATA: {
2478 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2479 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002480 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2481 : diag::err_pch_version_too_new);
Guy Benyei11169dd2012-12-18 14:30:41 +00002482 return VersionMismatch;
2483 }
2484
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00002485 bool hasErrors = Record[7];
Guy Benyei11169dd2012-12-18 14:30:41 +00002486 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2487 Diag(diag::err_pch_with_compiler_errors);
2488 return HadErrors;
2489 }
Argyrios Kyrtzidis70ec1c72016-07-13 20:35:26 +00002490 if (hasErrors) {
2491 Diags.ErrorOccurred = true;
2492 Diags.UncompilableErrorOccurred = true;
2493 Diags.UnrecoverableErrorOccurred = true;
2494 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002495
2496 F.RelocatablePCH = Record[4];
Richard Smith7ed1bc92014-12-05 22:42:13 +00002497 // Relative paths in a relocatable PCH are relative to our sysroot.
2498 if (F.RelocatablePCH)
2499 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
Guy Benyei11169dd2012-12-18 14:30:41 +00002500
Richard Smithe75ee0f2015-08-17 07:13:32 +00002501 F.HasTimestamps = Record[5];
2502
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00002503 F.PCHHasObjectFile = Record[6];
2504
Guy Benyei11169dd2012-12-18 14:30:41 +00002505 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002506 StringRef ASTBranch = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002507 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2508 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002509 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
Guy Benyei11169dd2012-12-18 14:30:41 +00002510 return VersionMismatch;
2511 }
2512 break;
2513 }
2514
2515 case IMPORTS: {
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002516 // Validate the AST before processing any imports (otherwise, untangling
2517 // them can be error-prone and expensive). A module will have a name and
2518 // will already have been validated, but this catches the PCH case.
2519 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2520 return Result;
2521
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002522 // Load each of the imported PCH files.
Guy Benyei11169dd2012-12-18 14:30:41 +00002523 unsigned Idx = 0, N = Record.size();
2524 while (Idx < N) {
2525 // Read information about the AST file.
Vedant Kumar48b4f762018-04-14 01:40:48 +00002526 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00002527 // The import location will be the local one for now; we will adjust
2528 // all import locations of module imports after the global source
Richard Smithb22a1d12016-03-27 20:13:24 +00002529 // location info are setup, in ReadAST.
Guy Benyei11169dd2012-12-18 14:30:41 +00002530 SourceLocation ImportLoc =
Richard Smithb22a1d12016-03-27 20:13:24 +00002531 ReadUntranslatedSourceLocation(Record[Idx++]);
Vedant Kumar48b4f762018-04-14 01:40:48 +00002532 off_t StoredSize = (off_t)Record[Idx++];
2533 time_t StoredModTime = (time_t)Record[Idx++];
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002534 ASTFileSignature StoredSignature = {
2535 {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2536 (uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2537 (uint32_t)Record[Idx++]}}};
Boris Kolpackovd30446f2017-08-31 06:26:43 +00002538
2539 std::string ImportedName = ReadString(Record, Idx);
2540 std::string ImportedFile;
2541
2542 // For prebuilt and explicit modules first consult the file map for
2543 // an override. Note that here we don't search prebuilt module
2544 // directories, only the explicit name to file mappings. Also, we will
2545 // still verify the size/signature making sure it is essentially the
2546 // same file but perhaps in a different location.
2547 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2548 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2549 ImportedName, /*FileMapOnly*/ true);
2550
2551 if (ImportedFile.empty())
2552 ImportedFile = ReadPath(F, Record, Idx);
2553 else
2554 SkipPath(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00002555
Richard Smith0f99d6a2015-08-09 08:48:41 +00002556 // If our client can't cope with us being out of date, we can't cope with
2557 // our dependency being missing.
2558 unsigned Capabilities = ClientLoadCapabilities;
2559 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2560 Capabilities &= ~ARR_Missing;
2561
Guy Benyei11169dd2012-12-18 14:30:41 +00002562 // Load the AST file.
Richard Smith0f99d6a2015-08-09 08:48:41 +00002563 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2564 Loaded, StoredSize, StoredModTime,
2565 StoredSignature, Capabilities);
2566
2567 // If we diagnosed a problem, produce a backtrace.
2568 if (isDiagnosedResult(Result, Capabilities))
2569 Diag(diag::note_module_file_imported_by)
2570 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2571
2572 switch (Result) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002573 case Failure: return Failure;
2574 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor2f1806e2013-03-19 00:38:50 +00002575 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00002576 case OutOfDate: return OutOfDate;
2577 case VersionMismatch: return VersionMismatch;
2578 case ConfigurationMismatch: return ConfigurationMismatch;
2579 case HadErrors: return HadErrors;
2580 case Success: break;
2581 }
2582 }
2583 break;
2584 }
2585
Guy Benyei11169dd2012-12-18 14:30:41 +00002586 case ORIGINAL_FILE:
2587 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattner0e6c9402013-01-20 02:38:54 +00002588 F.ActualOriginalSourceFileName = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002589 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
Richard Smith7ed1bc92014-12-05 22:42:13 +00002590 ResolveImportedPath(F, F.OriginalSourceFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002591 break;
2592
2593 case ORIGINAL_FILE_ID:
2594 F.OriginalSourceFileID = FileID::get(Record[0]);
2595 break;
2596
2597 case ORIGINAL_PCH_DIR:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002598 F.OriginalDir = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002599 break;
2600
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002601 case MODULE_NAME:
2602 F.ModuleName = Blob;
Ben Langmuir4f5212a2014-04-14 22:12:44 +00002603 if (Listener)
2604 Listener->ReadModuleName(F.ModuleName);
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002605
2606 // Validate the AST as soon as we have a name so we can exit early on
2607 // failure.
2608 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2609 return Result;
Vedant Kumar48b4f762018-04-14 01:40:48 +00002610
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002611 break;
2612
Richard Smith223d3f22014-12-06 03:21:08 +00002613 case MODULE_DIRECTORY: {
2614 assert(!F.ModuleName.empty() &&
2615 "MODULE_DIRECTORY found before MODULE_NAME");
2616 // If we've already loaded a module map file covering this module, we may
2617 // have a better path for it (relative to the current build).
2618 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
2619 if (M && M->Directory) {
2620 // If we're implicitly loading a module, the base directory can't
2621 // change between the build and use.
Manman Ren11f2a472016-08-18 17:42:15 +00002622 if (F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
Richard Smith223d3f22014-12-06 03:21:08 +00002623 const DirectoryEntry *BuildDir =
2624 PP.getFileManager().getDirectory(Blob);
2625 if (!BuildDir || BuildDir != M->Directory) {
2626 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2627 Diag(diag::err_imported_module_relocated)
2628 << F.ModuleName << Blob << M->Directory->getName();
2629 return OutOfDate;
2630 }
2631 }
2632 F.BaseDirectory = M->Directory->getName();
2633 } else {
2634 F.BaseDirectory = Blob;
2635 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002636 break;
Richard Smith223d3f22014-12-06 03:21:08 +00002637 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002638
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002639 case MODULE_MAP_FILE:
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00002640 if (ASTReadResult Result =
2641 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2642 return Result;
Ben Langmuir264ea152014-11-08 00:06:39 +00002643 break;
2644
Justin Bognerca9c0cc2015-06-21 20:32:36 +00002645 case INPUT_FILE_OFFSETS:
Richard Smitha1825302014-10-23 22:18:29 +00002646 NumInputs = Record[0];
2647 NumUserInputs = Record[1];
Justin Bogner4c183242015-06-21 20:32:40 +00002648 F.InputFileOffsets =
2649 (const llvm::support::unaligned_uint64_t *)Blob.data();
Richard Smitha1825302014-10-23 22:18:29 +00002650 F.InputFilesLoaded.resize(NumInputs);
Argyrios Kyrtzidisa38cb202017-01-30 06:05:58 +00002651 F.NumUserInputFiles = NumUserInputs;
Guy Benyei11169dd2012-12-18 14:30:41 +00002652 break;
2653 }
2654 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002655}
2656
Ben Langmuir2c9af442014-04-10 17:57:43 +00002657ASTReader::ASTReadResult
2658ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002659 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002660
2661 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2662 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002663 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002664 }
2665
2666 // Read all of the records and blocks for the AST file.
2667 RecordData Record;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002668 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00002669 llvm::BitstreamEntry Entry = Stream.advance();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002670
Chris Lattnere7b154b2013-01-19 21:39:22 +00002671 switch (Entry.Kind) {
2672 case llvm::BitstreamEntry::Error:
2673 Error("error at end of module block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002674 return Failure;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002675 case llvm::BitstreamEntry::EndBlock:
Richard Smithc0fbba72013-04-03 22:49:41 +00002676 // Outside of C++, we do not store a lookup map for the translation unit.
2677 // Instead, mark it as needing a lookup map to be built if this module
2678 // contains any declarations lexically within it (which it always does!).
2679 // This usually has no cost, since we very rarely need the lookup map for
2680 // the translation unit outside C++.
Richard Smithdbafb6c2017-06-29 23:23:46 +00002681 if (ASTContext *Ctx = ContextObj) {
2682 DeclContext *DC = Ctx->getTranslationUnitDecl();
2683 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2684 DC->setMustBuildLookupTable();
2685 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002686
Ben Langmuir2c9af442014-04-10 17:57:43 +00002687 return Success;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002688 case llvm::BitstreamEntry::SubBlock:
2689 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002690 case DECLTYPES_BLOCK_ID:
2691 // We lazily load the decls block, but we want to set up the
2692 // DeclsCursor cursor to point into it. Clone our current bitcode
2693 // cursor to it, enter the block and read the abbrevs in that block.
2694 // With the main cursor, we just skip over it.
2695 F.DeclsCursor = Stream;
2696 if (Stream.SkipBlock() || // Skip with the main cursor.
2697 // Read the abbrevs.
2698 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2699 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002700 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002701 }
2702 break;
Richard Smithb9eab6d2014-03-20 19:44:17 +00002703
Guy Benyei11169dd2012-12-18 14:30:41 +00002704 case PREPROCESSOR_BLOCK_ID:
2705 F.MacroCursor = Stream;
2706 if (!PP.getExternalSource())
2707 PP.setExternalSource(this);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002708
Guy Benyei11169dd2012-12-18 14:30:41 +00002709 if (Stream.SkipBlock() ||
2710 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2711 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002712 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002713 }
2714 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2715 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002716
Guy Benyei11169dd2012-12-18 14:30:41 +00002717 case PREPROCESSOR_DETAIL_BLOCK_ID:
2718 F.PreprocessorDetailCursor = Stream;
2719 if (Stream.SkipBlock() ||
Chris Lattnere7b154b2013-01-19 21:39:22 +00002720 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00002721 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00002722 Error("malformed preprocessor detail record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002723 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002724 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002725 F.PreprocessorDetailStartOffset
Chris Lattnere7b154b2013-01-19 21:39:22 +00002726 = F.PreprocessorDetailCursor.GetCurrentBitNo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002727
Guy Benyei11169dd2012-12-18 14:30:41 +00002728 if (!PP.getPreprocessingRecord())
2729 PP.createPreprocessingRecord();
2730 if (!PP.getPreprocessingRecord()->getExternalSource())
2731 PP.getPreprocessingRecord()->SetExternalSource(*this);
2732 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002733
Guy Benyei11169dd2012-12-18 14:30:41 +00002734 case SOURCE_MANAGER_BLOCK_ID:
2735 if (ReadSourceManagerBlock(F))
Ben Langmuir2c9af442014-04-10 17:57:43 +00002736 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002737 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002738
Guy Benyei11169dd2012-12-18 14:30:41 +00002739 case SUBMODULE_BLOCK_ID:
David Blaikie9ffe5a32017-01-30 05:00:26 +00002740 if (ASTReadResult Result =
2741 ReadSubmoduleBlock(F, ClientLoadCapabilities))
Ben Langmuir2c9af442014-04-10 17:57:43 +00002742 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00002743 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002744
Guy Benyei11169dd2012-12-18 14:30:41 +00002745 case COMMENTS_BLOCK_ID: {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002746 BitstreamCursor C = Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002747 if (Stream.SkipBlock() ||
2748 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2749 Error("malformed comments block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002750 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002751 }
2752 CommentsCursors.push_back(std::make_pair(C, &F));
2753 break;
2754 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002755
Guy Benyei11169dd2012-12-18 14:30:41 +00002756 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002757 if (Stream.SkipBlock()) {
2758 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002759 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002760 }
2761 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002762 }
2763 continue;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002764
Chris Lattnere7b154b2013-01-19 21:39:22 +00002765 case llvm::BitstreamEntry::Record:
2766 // The interesting case.
2767 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002768 }
2769
2770 // Read and process a record.
2771 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002772 StringRef Blob;
Richard Smithdbafb6c2017-06-29 23:23:46 +00002773 auto RecordType =
2774 (ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob);
2775
2776 // If we're not loading an AST context, we don't care about most records.
2777 if (!ContextObj) {
2778 switch (RecordType) {
2779 case IDENTIFIER_TABLE:
2780 case IDENTIFIER_OFFSET:
2781 case INTERESTING_IDENTIFIERS:
2782 case STATISTICS:
2783 case PP_CONDITIONAL_STACK:
2784 case PP_COUNTER_VALUE:
2785 case SOURCE_LOCATION_OFFSETS:
2786 case MODULE_OFFSET_MAP:
2787 case SOURCE_MANAGER_LINE_TABLE:
2788 case SOURCE_LOCATION_PRELOADS:
2789 case PPD_ENTITIES_OFFSETS:
2790 case HEADER_SEARCH_TABLE:
2791 case IMPORTED_MODULES:
2792 case MACRO_OFFSET:
2793 break;
2794 default:
2795 continue;
2796 }
2797 }
2798
2799 switch (RecordType) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002800 default: // Default behavior: ignore.
2801 break;
2802
2803 case TYPE_OFFSET: {
2804 if (F.LocalNumTypes != 0) {
2805 Error("duplicate TYPE_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002806 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002807 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002808 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002809 F.LocalNumTypes = Record[0];
2810 unsigned LocalBaseTypeIndex = Record[1];
2811 F.BaseTypeIndex = getTotalNumTypes();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002812
Guy Benyei11169dd2012-12-18 14:30:41 +00002813 if (F.LocalNumTypes > 0) {
2814 // Introduce the global -> local mapping for types within this module.
2815 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002816
Guy Benyei11169dd2012-12-18 14:30:41 +00002817 // Introduce the local -> global mapping for types within this module.
2818 F.TypeRemap.insertOrReplace(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002819 std::make_pair(LocalBaseTypeIndex,
Guy Benyei11169dd2012-12-18 14:30:41 +00002820 F.BaseTypeIndex - LocalBaseTypeIndex));
Ben Langmuir52ca6782014-10-20 16:27:32 +00002821
2822 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
Guy Benyei11169dd2012-12-18 14:30:41 +00002823 }
2824 break;
2825 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002826
Guy Benyei11169dd2012-12-18 14:30:41 +00002827 case DECL_OFFSET: {
2828 if (F.LocalNumDecls != 0) {
2829 Error("duplicate DECL_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002830 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002831 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002832 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002833 F.LocalNumDecls = Record[0];
2834 unsigned LocalBaseDeclID = Record[1];
2835 F.BaseDeclID = getTotalNumDecls();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002836
Guy Benyei11169dd2012-12-18 14:30:41 +00002837 if (F.LocalNumDecls > 0) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002838 // Introduce the global -> local mapping for declarations within this
Guy Benyei11169dd2012-12-18 14:30:41 +00002839 // module.
2840 GlobalDeclMap.insert(
2841 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002842
Guy Benyei11169dd2012-12-18 14:30:41 +00002843 // Introduce the local -> global mapping for declarations within this
2844 // module.
2845 F.DeclRemap.insertOrReplace(
2846 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002847
Guy Benyei11169dd2012-12-18 14:30:41 +00002848 // Introduce the global -> local mapping for declarations within this
2849 // module.
2850 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
Ben Langmuirfe971d92014-08-16 04:54:18 +00002851
Ben Langmuir52ca6782014-10-20 16:27:32 +00002852 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2853 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002854 break;
2855 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002856
Guy Benyei11169dd2012-12-18 14:30:41 +00002857 case TU_UPDATE_LEXICAL: {
Richard Smithdbafb6c2017-06-29 23:23:46 +00002858 DeclContext *TU = ContextObj->getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002859 LexicalContents Contents(
2860 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
2861 Blob.data()),
2862 static_cast<unsigned int>(Blob.size() / 4));
2863 TULexicalDecls.push_back(std::make_pair(&F, Contents));
Guy Benyei11169dd2012-12-18 14:30:41 +00002864 TU->setHasExternalLexicalStorage(true);
2865 break;
2866 }
2867
2868 case UPDATE_VISIBLE: {
2869 unsigned Idx = 0;
2870 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Richard Smith0f4e2c42015-08-06 04:23:48 +00002871 auto *Data = (const unsigned char*)Blob.data();
Richard Smithd88a7f12015-09-01 20:35:42 +00002872 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
Richard Smith0f4e2c42015-08-06 04:23:48 +00002873 // If we've already loaded the decl, perform the updates when we finish
2874 // loading this block.
2875 if (Decl *D = GetExistingDecl(ID))
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00002876 PendingUpdateRecords.push_back(
2877 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
Guy Benyei11169dd2012-12-18 14:30:41 +00002878 break;
2879 }
2880
2881 case IDENTIFIER_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002882 F.IdentifierTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002883 if (Record[0]) {
Justin Bognerda4e6502014-04-14 16:34:29 +00002884 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2885 (const unsigned char *)F.IdentifierTableData + Record[0],
2886 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2887 (const unsigned char *)F.IdentifierTableData,
2888 ASTIdentifierLookupTrait(*this, F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002889
Guy Benyei11169dd2012-12-18 14:30:41 +00002890 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2891 }
2892 break;
2893
2894 case IDENTIFIER_OFFSET: {
2895 if (F.LocalNumIdentifiers != 0) {
2896 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002897 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002898 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002899 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002900 F.LocalNumIdentifiers = Record[0];
2901 unsigned LocalBaseIdentifierID = Record[1];
2902 F.BaseIdentifierID = getTotalNumIdentifiers();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002903
Guy Benyei11169dd2012-12-18 14:30:41 +00002904 if (F.LocalNumIdentifiers > 0) {
2905 // Introduce the global -> local mapping for identifiers within this
2906 // module.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002907 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
Guy Benyei11169dd2012-12-18 14:30:41 +00002908 &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002909
Guy Benyei11169dd2012-12-18 14:30:41 +00002910 // Introduce the local -> global mapping for identifiers within this
2911 // module.
2912 F.IdentifierRemap.insertOrReplace(
2913 std::make_pair(LocalBaseIdentifierID,
2914 F.BaseIdentifierID - LocalBaseIdentifierID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00002915
Ben Langmuir52ca6782014-10-20 16:27:32 +00002916 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2917 + F.LocalNumIdentifiers);
2918 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002919 break;
2920 }
2921
Richard Smith33e0f7e2015-07-22 02:08:40 +00002922 case INTERESTING_IDENTIFIERS:
2923 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
2924 break;
2925
Ben Langmuir332aafe2014-01-31 01:06:56 +00002926 case EAGERLY_DESERIALIZED_DECLS:
Richard Smith9e2341d2015-03-23 03:25:59 +00002927 // FIXME: Skip reading this record if our ASTConsumer doesn't care
2928 // about "interesting" decls (for instance, if we're building a module).
Guy Benyei11169dd2012-12-18 14:30:41 +00002929 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Ben Langmuir332aafe2014-01-31 01:06:56 +00002930 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002931 break;
2932
David Blaikie9ffe5a32017-01-30 05:00:26 +00002933 case MODULAR_CODEGEN_DECLS:
2934 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
2935 // them (ie: if we're not codegenerating this module).
2936 if (F.Kind == MK_MainFile)
2937 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2938 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
2939 break;
2940
Guy Benyei11169dd2012-12-18 14:30:41 +00002941 case SPECIAL_TYPES:
Douglas Gregor44180f82013-02-01 23:45:03 +00002942 if (SpecialTypes.empty()) {
2943 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2944 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2945 break;
2946 }
2947
2948 if (SpecialTypes.size() != Record.size()) {
2949 Error("invalid special-types record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002950 return Failure;
Douglas Gregor44180f82013-02-01 23:45:03 +00002951 }
2952
2953 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2954 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2955 if (!SpecialTypes[I])
2956 SpecialTypes[I] = ID;
2957 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2958 // merge step?
2959 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002960 break;
2961
2962 case STATISTICS:
2963 TotalNumStatements += Record[0];
2964 TotalNumMacros += Record[1];
2965 TotalLexicalDeclContexts += Record[2];
2966 TotalVisibleDeclContexts += Record[3];
2967 break;
2968
2969 case UNUSED_FILESCOPED_DECLS:
2970 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2971 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2972 break;
2973
2974 case DELEGATING_CTORS:
2975 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2976 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2977 break;
2978
2979 case WEAK_UNDECLARED_IDENTIFIERS:
2980 if (Record.size() % 4 != 0) {
2981 Error("invalid weak identifiers record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002982 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002983 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002984
2985 // FIXME: Ignore weak undeclared identifiers from non-original PCH
Guy Benyei11169dd2012-12-18 14:30:41 +00002986 // files. This isn't the way to do it :)
2987 WeakUndeclaredIdentifiers.clear();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002988
Guy Benyei11169dd2012-12-18 14:30:41 +00002989 // Translate the weak, undeclared identifiers into global IDs.
2990 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2991 WeakUndeclaredIdentifiers.push_back(
2992 getGlobalIdentifierID(F, Record[I++]));
2993 WeakUndeclaredIdentifiers.push_back(
2994 getGlobalIdentifierID(F, Record[I++]));
2995 WeakUndeclaredIdentifiers.push_back(
2996 ReadSourceLocation(F, Record, I).getRawEncoding());
2997 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2998 }
2999 break;
3000
Guy Benyei11169dd2012-12-18 14:30:41 +00003001 case SELECTOR_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003002 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003003 F.LocalNumSelectors = Record[0];
3004 unsigned LocalBaseSelectorID = Record[1];
3005 F.BaseSelectorID = getTotalNumSelectors();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003006
Guy Benyei11169dd2012-12-18 14:30:41 +00003007 if (F.LocalNumSelectors > 0) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003008 // Introduce the global -> local mapping for selectors within this
Guy Benyei11169dd2012-12-18 14:30:41 +00003009 // module.
3010 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003011
3012 // Introduce the local -> global mapping for selectors within this
Guy Benyei11169dd2012-12-18 14:30:41 +00003013 // module.
3014 F.SelectorRemap.insertOrReplace(
3015 std::make_pair(LocalBaseSelectorID,
3016 F.BaseSelectorID - LocalBaseSelectorID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00003017
3018 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
Guy Benyei11169dd2012-12-18 14:30:41 +00003019 }
3020 break;
3021 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003022
Guy Benyei11169dd2012-12-18 14:30:41 +00003023 case METHOD_POOL:
Chris Lattner0e6c9402013-01-20 02:38:54 +00003024 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003025 if (Record[0])
3026 F.SelectorLookupTable
3027 = ASTSelectorLookupTable::Create(
3028 F.SelectorLookupTableData + Record[0],
3029 F.SelectorLookupTableData,
3030 ASTSelectorLookupTrait(*this, F));
3031 TotalNumMethodPoolEntries += Record[1];
3032 break;
3033
3034 case REFERENCED_SELECTOR_POOL:
3035 if (!Record.empty()) {
3036 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003037 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
Guy Benyei11169dd2012-12-18 14:30:41 +00003038 Record[Idx++]));
3039 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3040 getRawEncoding());
3041 }
3042 }
3043 break;
3044
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00003045 case PP_CONDITIONAL_STACK:
3046 if (!Record.empty()) {
Erik Verbruggen4d1eb2d2017-11-03 09:40:07 +00003047 unsigned Idx = 0, End = Record.size() - 1;
3048 bool ReachedEOFWhileSkipping = Record[Idx++];
3049 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3050 if (ReachedEOFWhileSkipping) {
3051 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3052 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3053 bool FoundNonSkipPortion = Record[Idx++];
3054 bool FoundElse = Record[Idx++];
3055 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3056 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3057 FoundElse, ElseLoc);
3058 }
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00003059 SmallVector<PPConditionalInfo, 4> ConditionalStack;
Erik Verbruggen4d1eb2d2017-11-03 09:40:07 +00003060 while (Idx < End) {
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00003061 auto Loc = ReadSourceLocation(F, Record, Idx);
3062 bool WasSkipping = Record[Idx++];
3063 bool FoundNonSkip = Record[Idx++];
3064 bool FoundElse = Record[Idx++];
3065 ConditionalStack.push_back(
3066 {Loc, WasSkipping, FoundNonSkip, FoundElse});
3067 }
Erik Verbruggen4d1eb2d2017-11-03 09:40:07 +00003068 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00003069 }
3070 break;
3071
Guy Benyei11169dd2012-12-18 14:30:41 +00003072 case PP_COUNTER_VALUE:
3073 if (!Record.empty() && Listener)
3074 Listener->ReadCounter(F, Record[0]);
3075 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003076
Guy Benyei11169dd2012-12-18 14:30:41 +00003077 case FILE_SORTED_DECLS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00003078 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003079 F.NumFileSortedDecls = Record[0];
3080 break;
3081
3082 case SOURCE_LOCATION_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003083 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003084 F.LocalNumSLocEntries = Record[0];
3085 unsigned SLocSpaceSize = Record[1];
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00003086 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Ben Langmuir52ca6782014-10-20 16:27:32 +00003087 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
Guy Benyei11169dd2012-12-18 14:30:41 +00003088 SLocSpaceSize);
Richard Smith78d81ec2015-08-12 22:25:24 +00003089 if (!F.SLocEntryBaseID) {
3090 Error("ran out of source locations");
3091 break;
3092 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003093 // Make our entry in the range map. BaseID is negative and growing, so
3094 // we invert it. Because we invert it, though, we need the other end of
3095 // the range.
3096 unsigned RangeStart =
3097 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3098 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3099 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3100
3101 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3102 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3103 GlobalSLocOffsetMap.insert(
3104 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3105 - SLocSpaceSize,&F));
3106
3107 // Initialize the remapping table.
3108 // Invalid stays invalid.
Richard Smithb9eab6d2014-03-20 19:44:17 +00003109 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
Guy Benyei11169dd2012-12-18 14:30:41 +00003110 // This module. Base was 2 when being compiled.
Richard Smithb9eab6d2014-03-20 19:44:17 +00003111 F.SLocRemap.insertOrReplace(std::make_pair(2U,
Guy Benyei11169dd2012-12-18 14:30:41 +00003112 static_cast<int>(F.SLocEntryBaseOffset - 2)));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003113
Guy Benyei11169dd2012-12-18 14:30:41 +00003114 TotalNumSLocEntries += F.LocalNumSLocEntries;
3115 break;
3116 }
3117
Richard Smith37a93df2017-02-18 00:32:02 +00003118 case MODULE_OFFSET_MAP:
3119 F.ModuleOffsetMap = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00003120 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003121
3122 case SOURCE_MANAGER_LINE_TABLE:
3123 if (ParseLineTable(F, Record))
Ben Langmuir2c9af442014-04-10 17:57:43 +00003124 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003125 break;
3126
3127 case SOURCE_LOCATION_PRELOADS: {
3128 // Need to transform from the local view (1-based IDs) to the global view,
3129 // which is based off F.SLocEntryBaseID.
3130 if (!F.PreloadSLocEntries.empty()) {
3131 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003132 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003133 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003134
Guy Benyei11169dd2012-12-18 14:30:41 +00003135 F.PreloadSLocEntries.swap(Record);
3136 break;
3137 }
3138
3139 case EXT_VECTOR_DECLS:
3140 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3141 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3142 break;
3143
3144 case VTABLE_USES:
3145 if (Record.size() % 3 != 0) {
3146 Error("Invalid VTABLE_USES record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003147 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003148 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003149
Guy Benyei11169dd2012-12-18 14:30:41 +00003150 // Later tables overwrite earlier ones.
3151 // FIXME: Modules will have some trouble with this. This is clearly not
3152 // the right way to do this.
3153 VTableUses.clear();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003154
Guy Benyei11169dd2012-12-18 14:30:41 +00003155 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3156 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3157 VTableUses.push_back(
3158 ReadSourceLocation(F, Record, Idx).getRawEncoding());
3159 VTableUses.push_back(Record[Idx++]);
3160 }
3161 break;
3162
Guy Benyei11169dd2012-12-18 14:30:41 +00003163 case PENDING_IMPLICIT_INSTANTIATIONS:
3164 if (PendingInstantiations.size() % 2 != 0) {
3165 Error("Invalid existing PendingInstantiations");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003166 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003167 }
3168
3169 if (Record.size() % 2 != 0) {
3170 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003171 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003172 }
3173
3174 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3175 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3176 PendingInstantiations.push_back(
3177 ReadSourceLocation(F, Record, I).getRawEncoding());
3178 }
3179 break;
3180
3181 case SEMA_DECL_REFS:
Richard Smith96269c52016-09-29 22:49:46 +00003182 if (Record.size() != 3) {
Richard Smith3d8e97e2013-10-18 06:54:39 +00003183 Error("Invalid SEMA_DECL_REFS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003184 return Failure;
Richard Smith3d8e97e2013-10-18 06:54:39 +00003185 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003186 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3187 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3188 break;
3189
3190 case PPD_ENTITIES_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003191 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3192 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3193 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00003194
3195 unsigned LocalBasePreprocessedEntityID = Record[0];
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003196
Guy Benyei11169dd2012-12-18 14:30:41 +00003197 unsigned StartingID;
3198 if (!PP.getPreprocessingRecord())
3199 PP.createPreprocessingRecord();
3200 if (!PP.getPreprocessingRecord()->getExternalSource())
3201 PP.getPreprocessingRecord()->SetExternalSource(*this);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003202 StartingID
Guy Benyei11169dd2012-12-18 14:30:41 +00003203 = PP.getPreprocessingRecord()
Ben Langmuir52ca6782014-10-20 16:27:32 +00003204 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Guy Benyei11169dd2012-12-18 14:30:41 +00003205 F.BasePreprocessedEntityID = StartingID;
3206
3207 if (F.NumPreprocessedEntities > 0) {
3208 // Introduce the global -> local mapping for preprocessed entities in
3209 // this module.
3210 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003211
Guy Benyei11169dd2012-12-18 14:30:41 +00003212 // Introduce the local -> global mapping for preprocessed entities in
3213 // this module.
3214 F.PreprocessedEntityRemap.insertOrReplace(
3215 std::make_pair(LocalBasePreprocessedEntityID,
3216 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3217 }
3218
3219 break;
3220 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003221
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00003222 case PPD_SKIPPED_RANGES: {
3223 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3224 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3225 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3226
3227 if (!PP.getPreprocessingRecord())
3228 PP.createPreprocessingRecord();
3229 if (!PP.getPreprocessingRecord()->getExternalSource())
3230 PP.getPreprocessingRecord()->SetExternalSource(*this);
3231 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3232 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003233
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00003234 if (F.NumPreprocessedSkippedRanges > 0)
3235 GlobalSkippedRangeMap.insert(
3236 std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3237 break;
3238 }
3239
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003240 case DECL_UPDATE_OFFSETS:
Guy Benyei11169dd2012-12-18 14:30:41 +00003241 if (Record.size() % 2 != 0) {
3242 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003243 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003244 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003245 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3246 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3247 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3248
3249 // If we've already loaded the decl, perform the updates when we finish
3250 // loading this block.
3251 if (Decl *D = GetExistingDecl(ID))
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003252 PendingUpdateRecords.push_back(
3253 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
Richard Smithcd45dbc2014-04-19 03:48:30 +00003254 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003255 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003256
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003257 case OBJC_CATEGORIES_MAP:
Guy Benyei11169dd2012-12-18 14:30:41 +00003258 if (F.LocalNumObjCCategoriesInMap != 0) {
3259 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003260 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003261 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003262
Guy Benyei11169dd2012-12-18 14:30:41 +00003263 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003264 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003265 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003266
Guy Benyei11169dd2012-12-18 14:30:41 +00003267 case OBJC_CATEGORIES:
3268 F.ObjCCategories.swap(Record);
3269 break;
Richard Smithc2bb8182015-03-24 06:36:48 +00003270
Guy Benyei11169dd2012-12-18 14:30:41 +00003271 case CUDA_SPECIAL_DECL_REFS:
3272 // Later tables overwrite earlier ones.
3273 // FIXME: Modules will have trouble with this.
3274 CUDASpecialDeclRefs.clear();
3275 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3276 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3277 break;
3278
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003279 case HEADER_SEARCH_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00003280 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003281 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00003282 if (Record[0]) {
3283 F.HeaderFileInfoTable
3284 = HeaderFileInfoLookupTable::Create(
3285 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3286 (const unsigned char *)F.HeaderFileInfoTableData,
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003287 HeaderFileInfoTrait(*this, F,
Guy Benyei11169dd2012-12-18 14:30:41 +00003288 &PP.getHeaderSearchInfo(),
Chris Lattner0e6c9402013-01-20 02:38:54 +00003289 Blob.data() + Record[2]));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003290
Guy Benyei11169dd2012-12-18 14:30:41 +00003291 PP.getHeaderSearchInfo().SetExternalSource(this);
3292 if (!PP.getHeaderSearchInfo().getExternalLookup())
3293 PP.getHeaderSearchInfo().SetExternalLookup(this);
3294 }
3295 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003296
Guy Benyei11169dd2012-12-18 14:30:41 +00003297 case FP_PRAGMA_OPTIONS:
3298 // Later tables overwrite earlier ones.
3299 FPPragmaOptions.swap(Record);
3300 break;
3301
3302 case OPENCL_EXTENSIONS:
Yaxun Liu5b746652016-12-18 05:18:55 +00003303 for (unsigned I = 0, E = Record.size(); I != E; ) {
3304 auto Name = ReadString(Record, I);
3305 auto &Opt = OpenCLExtensions.OptMap[Name];
Yaxun Liucc2741c2016-12-18 06:35:06 +00003306 Opt.Supported = Record[I++] != 0;
3307 Opt.Enabled = Record[I++] != 0;
Yaxun Liu5b746652016-12-18 05:18:55 +00003308 Opt.Avail = Record[I++];
3309 Opt.Core = Record[I++];
3310 }
3311 break;
3312
3313 case OPENCL_EXTENSION_TYPES:
3314 for (unsigned I = 0, E = Record.size(); I != E;) {
3315 auto TypeID = static_cast<::TypeID>(Record[I++]);
3316 auto *Type = GetType(TypeID).getTypePtr();
3317 auto NumExt = static_cast<unsigned>(Record[I++]);
3318 for (unsigned II = 0; II != NumExt; ++II) {
3319 auto Ext = ReadString(Record, I);
3320 OpenCLTypeExtMap[Type].insert(Ext);
3321 }
3322 }
3323 break;
3324
3325 case OPENCL_EXTENSION_DECLS:
3326 for (unsigned I = 0, E = Record.size(); I != E;) {
3327 auto DeclID = static_cast<::DeclID>(Record[I++]);
3328 auto *Decl = GetDecl(DeclID);
3329 auto NumExt = static_cast<unsigned>(Record[I++]);
3330 for (unsigned II = 0; II != NumExt; ++II) {
3331 auto Ext = ReadString(Record, I);
3332 OpenCLDeclExtMap[Decl].insert(Ext);
3333 }
3334 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003335 break;
3336
3337 case TENTATIVE_DEFINITIONS:
3338 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3339 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3340 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003341
Guy Benyei11169dd2012-12-18 14:30:41 +00003342 case KNOWN_NAMESPACES:
3343 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3344 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3345 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003346
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003347 case UNDEFINED_BUT_USED:
3348 if (UndefinedButUsed.size() % 2 != 0) {
3349 Error("Invalid existing UndefinedButUsed");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003350 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003351 }
3352
3353 if (Record.size() % 2 != 0) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003354 Error("invalid undefined-but-used record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003355 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003356 }
3357 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003358 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3359 UndefinedButUsed.push_back(
Nick Lewycky8334af82013-01-26 00:35:08 +00003360 ReadSourceLocation(F, Record, I).getRawEncoding());
3361 }
3362 break;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003363
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00003364 case DELETE_EXPRS_TO_ANALYZE:
3365 for (unsigned I = 0, N = Record.size(); I != N;) {
3366 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3367 const uint64_t Count = Record[I++];
3368 DelayedDeleteExprs.push_back(Count);
3369 for (uint64_t C = 0; C < Count; ++C) {
3370 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3371 bool IsArrayForm = Record[I++] == 1;
3372 DelayedDeleteExprs.push_back(IsArrayForm);
3373 }
3374 }
3375 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003376
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003377 case IMPORTED_MODULES:
Manman Ren11f2a472016-08-18 17:42:15 +00003378 if (!F.isModule()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003379 // If we aren't loading a module (which has its own exports), make
3380 // all of the imported modules visible.
3381 // FIXME: Deal with macros-only imports.
Richard Smith56be7542014-03-21 00:33:59 +00003382 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3383 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3384 SourceLocation Loc = ReadSourceLocation(F, Record, I);
Graydon Hoare9c982442017-01-18 20:36:59 +00003385 if (GlobalID) {
Aaron Ballman4f45b712014-03-21 15:22:56 +00003386 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
Graydon Hoare9c982442017-01-18 20:36:59 +00003387 if (DeserializationListener)
3388 DeserializationListener->ModuleImportRead(GlobalID, Loc);
3389 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003390 }
3391 }
3392 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003393
Guy Benyei11169dd2012-12-18 14:30:41 +00003394 case MACRO_OFFSET: {
3395 if (F.LocalNumMacros != 0) {
3396 Error("duplicate MACRO_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003397 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003398 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003399 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003400 F.LocalNumMacros = Record[0];
3401 unsigned LocalBaseMacroID = Record[1];
3402 F.BaseMacroID = getTotalNumMacros();
3403
3404 if (F.LocalNumMacros > 0) {
3405 // Introduce the global -> local mapping for macros within this module.
3406 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3407
3408 // Introduce the local -> global mapping for macros within this module.
3409 F.MacroRemap.insertOrReplace(
3410 std::make_pair(LocalBaseMacroID,
3411 F.BaseMacroID - LocalBaseMacroID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00003412
3413 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
Guy Benyei11169dd2012-12-18 14:30:41 +00003414 }
3415 break;
3416 }
3417
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003418 case LATE_PARSED_TEMPLATE:
Richard Smithe40f2ba2013-08-07 21:41:30 +00003419 LateParsedTemplates.append(Record.begin(), Record.end());
3420 break;
Dario Domizioli13a0a382014-05-23 12:13:25 +00003421
3422 case OPTIMIZE_PRAGMA_OPTIONS:
3423 if (Record.size() != 1) {
3424 Error("invalid pragma optimize record");
3425 return Failure;
3426 }
3427 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3428 break;
Nico Weber72889432014-09-06 01:25:55 +00003429
Nico Weber779355f2016-03-02 23:22:00 +00003430 case MSSTRUCT_PRAGMA_OPTIONS:
3431 if (Record.size() != 1) {
3432 Error("invalid pragma ms_struct record");
3433 return Failure;
3434 }
3435 PragmaMSStructState = Record[0];
3436 break;
3437
Nico Weber42932312016-03-03 00:17:35 +00003438 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3439 if (Record.size() != 2) {
3440 Error("invalid pragma ms_struct record");
3441 return Failure;
3442 }
3443 PragmaMSPointersToMembersState = Record[0];
3444 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3445 break;
3446
Nico Weber72889432014-09-06 01:25:55 +00003447 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3448 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3449 UnusedLocalTypedefNameCandidates.push_back(
3450 getGlobalDeclID(F, Record[I]));
3451 break;
Justin Lebar67a78a62016-10-08 22:15:58 +00003452
3453 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3454 if (Record.size() != 1) {
3455 Error("invalid cuda pragma options record");
3456 return Failure;
3457 }
3458 ForceCUDAHostDeviceDepth = Record[0];
3459 break;
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00003460
3461 case PACK_PRAGMA_OPTIONS: {
3462 if (Record.size() < 3) {
3463 Error("invalid pragma pack record");
3464 return Failure;
3465 }
3466 PragmaPackCurrentValue = Record[0];
3467 PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3468 unsigned NumStackEntries = Record[2];
3469 unsigned Idx = 3;
3470 // Reset the stack when importing a new module.
3471 PragmaPackStack.clear();
3472 for (unsigned I = 0; I < NumStackEntries; ++I) {
3473 PragmaPackStackEntry Entry;
3474 Entry.Value = Record[Idx++];
3475 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
Alex Lorenz45b40142017-07-28 14:41:21 +00003476 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00003477 PragmaPackStrings.push_back(ReadString(Record, Idx));
3478 Entry.SlotLabel = PragmaPackStrings.back();
3479 PragmaPackStack.push_back(Entry);
3480 }
3481 break;
3482 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003483 }
3484 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003485}
3486
Richard Smith37a93df2017-02-18 00:32:02 +00003487void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3488 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3489
3490 // Additional remapping information.
Vedant Kumar48b4f762018-04-14 01:40:48 +00003491 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
Richard Smith37a93df2017-02-18 00:32:02 +00003492 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3493 F.ModuleOffsetMap = StringRef();
3494
3495 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3496 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3497 F.SLocRemap.insert(std::make_pair(0U, 0));
3498 F.SLocRemap.insert(std::make_pair(2U, 1));
3499 }
3500
3501 // Continuous range maps we may be updating in our module.
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003502 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
Richard Smith37a93df2017-02-18 00:32:02 +00003503 RemapBuilder SLocRemap(F.SLocRemap);
3504 RemapBuilder IdentifierRemap(F.IdentifierRemap);
3505 RemapBuilder MacroRemap(F.MacroRemap);
3506 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3507 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3508 RemapBuilder SelectorRemap(F.SelectorRemap);
3509 RemapBuilder DeclRemap(F.DeclRemap);
3510 RemapBuilder TypeRemap(F.TypeRemap);
3511
3512 while (Data < DataEnd) {
Boris Kolpackovd30446f2017-08-31 06:26:43 +00003513 // FIXME: Looking up dependency modules by filename is horrible. Let's
3514 // start fixing this with prebuilt and explicit modules and see how it
3515 // goes...
Richard Smith37a93df2017-02-18 00:32:02 +00003516 using namespace llvm::support;
Vedant Kumar48b4f762018-04-14 01:40:48 +00003517 ModuleKind Kind = static_cast<ModuleKind>(
3518 endian::readNext<uint8_t, little, unaligned>(Data));
Richard Smith37a93df2017-02-18 00:32:02 +00003519 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3520 StringRef Name = StringRef((const char*)Data, Len);
3521 Data += Len;
Boris Kolpackovd30446f2017-08-31 06:26:43 +00003522 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
3523 ? ModuleMgr.lookupByModuleName(Name)
3524 : ModuleMgr.lookupByFileName(Name));
Richard Smith37a93df2017-02-18 00:32:02 +00003525 if (!OM) {
3526 std::string Msg =
3527 "SourceLocation remap refers to unknown module, cannot find ";
3528 Msg.append(Name);
3529 Error(Msg);
3530 return;
3531 }
3532
3533 uint32_t SLocOffset =
3534 endian::readNext<uint32_t, little, unaligned>(Data);
3535 uint32_t IdentifierIDOffset =
3536 endian::readNext<uint32_t, little, unaligned>(Data);
3537 uint32_t MacroIDOffset =
3538 endian::readNext<uint32_t, little, unaligned>(Data);
3539 uint32_t PreprocessedEntityIDOffset =
3540 endian::readNext<uint32_t, little, unaligned>(Data);
3541 uint32_t SubmoduleIDOffset =
3542 endian::readNext<uint32_t, little, unaligned>(Data);
3543 uint32_t SelectorIDOffset =
3544 endian::readNext<uint32_t, little, unaligned>(Data);
3545 uint32_t DeclIDOffset =
3546 endian::readNext<uint32_t, little, unaligned>(Data);
3547 uint32_t TypeIndexOffset =
3548 endian::readNext<uint32_t, little, unaligned>(Data);
3549
3550 uint32_t None = std::numeric_limits<uint32_t>::max();
3551
3552 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3553 RemapBuilder &Remap) {
3554 if (Offset != None)
3555 Remap.insert(std::make_pair(Offset,
3556 static_cast<int>(BaseOffset - Offset)));
3557 };
3558 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3559 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3560 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3561 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3562 PreprocessedEntityRemap);
3563 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3564 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3565 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3566 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3567
3568 // Global -> local mappings.
3569 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3570 }
3571}
3572
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003573ASTReader::ASTReadResult
3574ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3575 const ModuleFile *ImportedBy,
3576 unsigned ClientLoadCapabilities) {
3577 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00003578 F.ModuleMapPath = ReadPath(F, Record, Idx);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003579
3580 // Try to resolve ModuleName in the current header search context and
3581 // verify that it is found in the same module map file as we saved. If the
3582 // top-level AST file is a main file, skip this check because there is no
3583 // usable header search context.
3584 assert(!F.ModuleName.empty() &&
Richard Smithe842a472014-10-22 02:05:46 +00003585 "MODULE_NAME should come before MODULE_MAP_FILE");
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00003586 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
Richard Smithe842a472014-10-22 02:05:46 +00003587 // An implicitly-loaded module file should have its module listed in some
3588 // module map file that we've already loaded.
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003589 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
Richard Smithe842a472014-10-22 02:05:46 +00003590 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3591 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3592 if (!ModMap) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003593 assert(ImportedBy && "top-level import should be verified");
Richard Smith0f99d6a2015-08-09 08:48:41 +00003594 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
Bruno Cardoso Lopesa66a3252017-11-17 03:24:11 +00003595 if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
Richard Smith0f99d6a2015-08-09 08:48:41 +00003596 // This module was defined by an imported (explicit) module.
3597 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3598 << ASTFE->getName();
Bruno Cardoso Lopesa66a3252017-11-17 03:24:11 +00003599 } else {
Richard Smith0f99d6a2015-08-09 08:48:41 +00003600 // This module was built with a different module map.
3601 Diag(diag::err_imported_module_not_found)
3602 << F.ModuleName << F.FileName << ImportedBy->FileName
3603 << F.ModuleMapPath;
Bruno Cardoso Lopesa66a3252017-11-17 03:24:11 +00003604 // In case it was imported by a PCH, there's a chance the user is
3605 // just missing to include the search path to the directory containing
3606 // the modulemap.
3607 if (ImportedBy->Kind == MK_PCH)
3608 Diag(diag::note_imported_by_pch_module_not_found)
3609 << llvm::sys::path::parent_path(F.ModuleMapPath);
3610 }
Richard Smith0f99d6a2015-08-09 08:48:41 +00003611 }
3612 return OutOfDate;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003613 }
3614
Richard Smithe842a472014-10-22 02:05:46 +00003615 assert(M->Name == F.ModuleName && "found module with different name");
3616
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003617 // Check the primary module map file.
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003618 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003619 if (StoredModMap == nullptr || StoredModMap != ModMap) {
3620 assert(ModMap && "found module is missing module map file");
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003621 assert(ImportedBy && "top-level import should be verified");
3622 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3623 Diag(diag::err_imported_module_modmap_changed)
3624 << F.ModuleName << ImportedBy->FileName
3625 << ModMap->getName() << F.ModuleMapPath;
3626 return OutOfDate;
3627 }
3628
3629 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3630 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3631 // FIXME: we should use input files rather than storing names.
Richard Smith7ed1bc92014-12-05 22:42:13 +00003632 std::string Filename = ReadPath(F, Record, Idx);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003633 const FileEntry *F =
3634 FileMgr.getFile(Filename, false, false);
3635 if (F == nullptr) {
3636 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3637 Error("could not find file '" + Filename +"' referenced by AST file");
3638 return OutOfDate;
3639 }
3640 AdditionalStoredMaps.insert(F);
3641 }
3642
3643 // Check any additional module map files (e.g. module.private.modulemap)
3644 // that are not in the pcm.
3645 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00003646 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003647 // Remove files that match
3648 // Note: SmallPtrSet::erase is really remove
3649 if (!AdditionalStoredMaps.erase(ModMap)) {
3650 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3651 Diag(diag::err_module_different_modmap)
3652 << F.ModuleName << /*new*/0 << ModMap->getName();
3653 return OutOfDate;
3654 }
3655 }
3656 }
3657
3658 // Check any additional module map files that are in the pcm, but not
3659 // found in header search. Cases that match are already removed.
Vedant Kumar48b4f762018-04-14 01:40:48 +00003660 for (const FileEntry *ModMap : AdditionalStoredMaps) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003661 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3662 Diag(diag::err_module_different_modmap)
3663 << F.ModuleName << /*not new*/1 << ModMap->getName();
3664 return OutOfDate;
3665 }
3666 }
3667
3668 if (Listener)
3669 Listener->ReadModuleMapFile(F.ModuleMapPath);
3670 return Success;
3671}
3672
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003673/// Move the given method to the back of the global list of methods.
Douglas Gregorc1489562013-02-12 23:36:21 +00003674static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3675 // Find the entry for this selector in the method pool.
3676 Sema::GlobalMethodPool::iterator Known
3677 = S.MethodPool.find(Method->getSelector());
3678 if (Known == S.MethodPool.end())
3679 return;
3680
3681 // Retrieve the appropriate method list.
3682 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3683 : Known->second.second;
3684 bool Found = false;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003685 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003686 if (!Found) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003687 if (List->getMethod() == Method) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003688 Found = true;
3689 } else {
3690 // Keep searching.
3691 continue;
3692 }
3693 }
3694
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003695 if (List->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003696 List->setMethod(List->getNext()->getMethod());
Douglas Gregorc1489562013-02-12 23:36:21 +00003697 else
Nico Weber2e0c8f72014-12-27 03:58:08 +00003698 List->setMethod(Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00003699 }
3700}
3701
Richard Smithde711422015-04-23 21:20:19 +00003702void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Richard Smith10434f32015-05-02 02:08:26 +00003703 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
Vedant Kumar48b4f762018-04-14 01:40:48 +00003704 for (Decl *D : Names) {
Richard Smith90dc5252017-06-23 01:04:34 +00003705 bool wasHidden = D->isHidden();
3706 D->setVisibleDespiteOwningModule();
Guy Benyei11169dd2012-12-18 14:30:41 +00003707
Vedant Kumar48b4f762018-04-14 01:40:48 +00003708 if (wasHidden && SemaObj) {
3709 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
Richard Smith49f906a2014-03-01 00:08:04 +00003710 moveMethodToBackOfGlobalList(*SemaObj, Method);
Vedant Kumar48b4f762018-04-14 01:40:48 +00003711 }
3712 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003713 }
3714}
3715
Richard Smith49f906a2014-03-01 00:08:04 +00003716void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00003717 Module::NameVisibilityKind NameVisibility,
Richard Smitha7e2cc62015-05-01 01:53:09 +00003718 SourceLocation ImportLoc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003719 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003720 SmallVector<Module *, 4> Stack;
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003721 Stack.push_back(Mod);
Guy Benyei11169dd2012-12-18 14:30:41 +00003722 while (!Stack.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003723 Mod = Stack.pop_back_val();
Guy Benyei11169dd2012-12-18 14:30:41 +00003724
3725 if (NameVisibility <= Mod->NameVisibility) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003726 // This module already has this level of visibility (or greater), so
Guy Benyei11169dd2012-12-18 14:30:41 +00003727 // there is nothing more to do.
3728 continue;
3729 }
Richard Smith49f906a2014-03-01 00:08:04 +00003730
Guy Benyei11169dd2012-12-18 14:30:41 +00003731 if (!Mod->isAvailable()) {
3732 // Modules that aren't available cannot be made visible.
3733 continue;
3734 }
3735
3736 // Update the module's name visibility.
3737 Mod->NameVisibility = NameVisibility;
Richard Smith49f906a2014-03-01 00:08:04 +00003738
Guy Benyei11169dd2012-12-18 14:30:41 +00003739 // If we've already deserialized any names from this module,
3740 // mark them as visible.
3741 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3742 if (Hidden != HiddenNamesMap.end()) {
Richard Smith57721ac2014-07-21 04:10:40 +00003743 auto HiddenNames = std::move(*Hidden);
Guy Benyei11169dd2012-12-18 14:30:41 +00003744 HiddenNamesMap.erase(Hidden);
Richard Smithde711422015-04-23 21:20:19 +00003745 makeNamesVisible(HiddenNames.second, HiddenNames.first);
Richard Smith57721ac2014-07-21 04:10:40 +00003746 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3747 "making names visible added hidden names");
Guy Benyei11169dd2012-12-18 14:30:41 +00003748 }
Dmitri Gribenkoe9bcf5b2013-11-04 21:51:33 +00003749
Guy Benyei11169dd2012-12-18 14:30:41 +00003750 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003751 SmallVector<Module *, 16> Exports;
3752 Mod->getExportedModules(Exports);
Vedant Kumar48b4f762018-04-14 01:40:48 +00003753 for (SmallVectorImpl<Module *>::iterator
3754 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3755 Module *Exported = *I;
David Blaikie82e95a32014-11-19 07:49:47 +00003756 if (Visited.insert(Exported).second)
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003757 Stack.push_back(Exported);
Vedant Kumar48b4f762018-04-14 01:40:48 +00003758 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003759 }
3760}
3761
Richard Smith6561f922016-09-12 21:06:40 +00003762/// We've merged the definition \p MergedDef into the existing definition
3763/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
3764/// visible.
3765void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
3766 NamedDecl *MergedDef) {
Benjamin Kramera72a70a2016-10-17 13:00:44 +00003767 // FIXME: This doesn't correctly handle the case where MergedDef is visible
3768 // in modules other than its owning module. We should instead give the
3769 // ASTContext a list of merged definitions for Def.
Richard Smith6561f922016-09-12 21:06:40 +00003770 if (Def->isHidden()) {
3771 // If MergedDef is visible or becomes visible, make the definition visible.
Benjamin Kramera72a70a2016-10-17 13:00:44 +00003772 if (!MergedDef->isHidden())
Richard Smith90dc5252017-06-23 01:04:34 +00003773 Def->setVisibleDespiteOwningModule();
Benjamin Kramera72a70a2016-10-17 13:00:44 +00003774 else if (getContext().getLangOpts().ModulesLocalVisibility) {
3775 getContext().mergeDefinitionIntoModule(
3776 Def, MergedDef->getImportedOwningModule(),
3777 /*NotifyListeners*/ false);
3778 PendingMergedDefinitionsToDeduplicate.insert(Def);
3779 } else {
3780 auto SubmoduleID = MergedDef->getOwningModuleID();
3781 assert(SubmoduleID && "hidden definition in no module");
3782 HiddenNamesMap[getSubmodule(SubmoduleID)].push_back(Def);
3783 }
Richard Smith6561f922016-09-12 21:06:40 +00003784 }
3785}
3786
Douglas Gregore060e572013-01-25 01:03:03 +00003787bool ASTReader::loadGlobalIndex() {
3788 if (GlobalIndex)
3789 return false;
3790
3791 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
Richard Smithdbafb6c2017-06-29 23:23:46 +00003792 !PP.getLangOpts().Modules)
Douglas Gregore060e572013-01-25 01:03:03 +00003793 return true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003794
Douglas Gregore060e572013-01-25 01:03:03 +00003795 // Try to load the global index.
3796 TriedLoadingGlobalIndex = true;
3797 StringRef ModuleCachePath
3798 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3799 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor7029ce12013-03-19 00:28:20 +00003800 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregore060e572013-01-25 01:03:03 +00003801 if (!Result.first)
3802 return true;
3803
3804 GlobalIndex.reset(Result.first);
Douglas Gregor7211ac12013-01-25 23:32:03 +00003805 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregore060e572013-01-25 01:03:03 +00003806 return false;
3807}
3808
3809bool ASTReader::isGlobalIndexUnavailable() const {
Richard Smithdbafb6c2017-06-29 23:23:46 +00003810 return PP.getLangOpts().Modules && UseGlobalIndex &&
Douglas Gregore060e572013-01-25 01:03:03 +00003811 !hasGlobalIndex() && TriedLoadingGlobalIndex;
3812}
3813
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003814static void updateModuleTimestamp(ModuleFile &MF) {
3815 // Overwrite the timestamp file contents so that file's mtime changes.
3816 std::string TimestampFilename = MF.getTimestampFilename();
Rafael Espindoladae941a2014-08-25 18:17:04 +00003817 std::error_code EC;
3818 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text);
3819 if (EC)
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003820 return;
3821 OS << "Timestamp file\n";
Alex Lorenz0bafa022017-06-02 10:36:56 +00003822 OS.close();
3823 OS.clear_error(); // Avoid triggering a fatal error.
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003824}
3825
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003826/// Given a cursor at the start of an AST file, scan ahead and drop the
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003827/// cursor into the start of the given block ID, returning false on success and
3828/// true on failure.
3829static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003830 while (true) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003831 llvm::BitstreamEntry Entry = Cursor.advance();
3832 switch (Entry.Kind) {
3833 case llvm::BitstreamEntry::Error:
3834 case llvm::BitstreamEntry::EndBlock:
3835 return true;
3836
3837 case llvm::BitstreamEntry::Record:
3838 // Ignore top-level records.
3839 Cursor.skipRecord(Entry.ID);
3840 break;
3841
3842 case llvm::BitstreamEntry::SubBlock:
3843 if (Entry.ID == BlockID) {
3844 if (Cursor.EnterSubBlock(BlockID))
3845 return true;
3846 // Found it!
3847 return false;
3848 }
3849
3850 if (Cursor.SkipBlock())
3851 return true;
3852 }
3853 }
3854}
3855
Benjamin Kramer0772c422016-02-13 13:42:54 +00003856ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
Guy Benyei11169dd2012-12-18 14:30:41 +00003857 ModuleKind Type,
3858 SourceLocation ImportLoc,
Graydon Hoaree7196af2016-12-09 21:45:49 +00003859 unsigned ClientLoadCapabilities,
3860 SmallVectorImpl<ImportedSubmodule> *Imported) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00003861 llvm::SaveAndRestore<SourceLocation>
3862 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3863
Richard Smithd1c46742014-04-30 02:24:17 +00003864 // Defer any pending actions until we get to the end of reading the AST file.
3865 Deserializing AnASTFile(this);
3866
Guy Benyei11169dd2012-12-18 14:30:41 +00003867 // Bump the generation number.
Richard Smithdbafb6c2017-06-29 23:23:46 +00003868 unsigned PreviousGeneration = 0;
3869 if (ContextObj)
3870 PreviousGeneration = incrementGeneration(*ContextObj);
Guy Benyei11169dd2012-12-18 14:30:41 +00003871
3872 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003873 SmallVector<ImportedModule, 4> Loaded;
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00003874 switch (ASTReadResult ReadResult =
3875 ReadASTCore(FileName, Type, ImportLoc,
3876 /*ImportedBy=*/nullptr, Loaded, 0, 0,
3877 ASTFileSignature(), ClientLoadCapabilities)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003878 case Failure:
Douglas Gregor7029ce12013-03-19 00:28:20 +00003879 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00003880 case OutOfDate:
3881 case VersionMismatch:
3882 case ConfigurationMismatch:
Ben Langmuir9801b252014-06-20 00:24:56 +00003883 case HadErrors: {
3884 llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
Vedant Kumar48b4f762018-04-14 01:40:48 +00003885 for (const ImportedModule &IM : Loaded)
Ben Langmuir9801b252014-06-20 00:24:56 +00003886 LoadedSet.insert(IM.Mod);
3887
Duncan P. N. Exon Smith8e6bc1972017-01-28 23:02:12 +00003888 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, LoadedSet,
Richard Smithdbafb6c2017-06-29 23:23:46 +00003889 PP.getLangOpts().Modules
Duncan P. N. Exon Smith8e6bc1972017-01-28 23:02:12 +00003890 ? &PP.getHeaderSearchInfo().getModuleMap()
3891 : nullptr);
Douglas Gregore060e572013-01-25 01:03:03 +00003892
3893 // If we find that any modules are unusable, the global index is going
3894 // to be out-of-date. Just remove it.
3895 GlobalIndex.reset();
Craig Toppera13603a2014-05-22 05:54:18 +00003896 ModuleMgr.setGlobalIndex(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003897 return ReadResult;
Ben Langmuir9801b252014-06-20 00:24:56 +00003898 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003899 case Success:
3900 break;
3901 }
3902
3903 // Here comes stuff that we only do once the entire chain is loaded.
3904
3905 // Load the AST blocks of all of the modules that we loaded.
Vedant Kumar48b4f762018-04-14 01:40:48 +00003906 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3907 MEnd = Loaded.end();
3908 M != MEnd; ++M) {
3909 ModuleFile &F = *M->Mod;
Guy Benyei11169dd2012-12-18 14:30:41 +00003910
3911 // Read the AST block.
Ben Langmuir2c9af442014-04-10 17:57:43 +00003912 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3913 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00003914
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003915 // Read the extension blocks.
3916 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
3917 if (ASTReadResult Result = ReadExtensionBlock(F))
3918 return Result;
3919 }
3920
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003921 // Once read, set the ModuleFile bit base offset and update the size in
Guy Benyei11169dd2012-12-18 14:30:41 +00003922 // bits of all files we've seen.
3923 F.GlobalBitOffset = TotalModulesSizeInBits;
3924 TotalModulesSizeInBits += F.SizeInBits;
3925 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003926
Guy Benyei11169dd2012-12-18 14:30:41 +00003927 // Preload SLocEntries.
Vedant Kumar48b4f762018-04-14 01:40:48 +00003928 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3929 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Guy Benyei11169dd2012-12-18 14:30:41 +00003930 // Load it through the SourceManager and don't call ReadSLocEntry()
3931 // directly because the entry may have already been loaded in which case
3932 // calling ReadSLocEntry() directly would trigger an assertion in
3933 // SourceManager.
3934 SourceMgr.getLoadedSLocEntryByID(Index);
3935 }
Richard Smith33e0f7e2015-07-22 02:08:40 +00003936
Richard Smithea741482017-05-01 22:10:47 +00003937 // Map the original source file ID into the ID space of the current
3938 // compilation.
3939 if (F.OriginalSourceFileID.isValid()) {
3940 F.OriginalSourceFileID = FileID::get(
3941 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
3942 }
3943
Richard Smith33e0f7e2015-07-22 02:08:40 +00003944 // Preload all the pending interesting identifiers by marking them out of
3945 // date.
3946 for (auto Offset : F.PreloadIdentifierOffsets) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00003947 const unsigned char *Data = reinterpret_cast<const unsigned char *>(
Richard Smith33e0f7e2015-07-22 02:08:40 +00003948 F.IdentifierTableData + Offset);
3949
3950 ASTIdentifierLookupTrait Trait(*this, F);
3951 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
3952 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
Richard Smith79bf9202015-08-24 03:33:22 +00003953 auto &II = PP.getIdentifierTable().getOwn(Key);
3954 II.setOutOfDate(true);
3955
3956 // Mark this identifier as being from an AST file so that we can track
3957 // whether we need to serialize it.
Richard Smitheb4b58f62016-02-05 01:40:54 +00003958 markIdentifierFromAST(*this, II);
Richard Smith79bf9202015-08-24 03:33:22 +00003959
3960 // Associate the ID with the identifier so that the writer can reuse it.
3961 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
3962 SetIdentifierInfo(ID, &II);
Richard Smith33e0f7e2015-07-22 02:08:40 +00003963 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003964 }
3965
Douglas Gregor603cd862013-03-22 18:50:14 +00003966 // Setup the import locations and notify the module manager that we've
3967 // committed to these module files.
Vedant Kumar48b4f762018-04-14 01:40:48 +00003968 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3969 MEnd = Loaded.end();
3970 M != MEnd; ++M) {
3971 ModuleFile &F = *M->Mod;
Douglas Gregor603cd862013-03-22 18:50:14 +00003972
3973 ModuleMgr.moduleFileAccepted(&F);
3974
3975 // Set the import location.
Argyrios Kyrtzidis71c1af82013-02-01 16:36:14 +00003976 F.DirectImportLoc = ImportLoc;
Richard Smithb22a1d12016-03-27 20:13:24 +00003977 // FIXME: We assume that locations from PCH / preamble do not need
3978 // any translation.
Vedant Kumar48b4f762018-04-14 01:40:48 +00003979 if (!M->ImportedBy)
3980 F.ImportLoc = M->ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00003981 else
Vedant Kumar48b4f762018-04-14 01:40:48 +00003982 F.ImportLoc = TranslateSourceLocation(*M->ImportedBy, M->ImportLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003983 }
3984
Richard Smithdbafb6c2017-06-29 23:23:46 +00003985 if (!PP.getLangOpts().CPlusPlus ||
Manman Ren11f2a472016-08-18 17:42:15 +00003986 (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
3987 Type != MK_PrebuiltModule)) {
Richard Smith33e0f7e2015-07-22 02:08:40 +00003988 // Mark all of the identifiers in the identifier table as being out of date,
3989 // so that various accessors know to check the loaded modules when the
3990 // identifier is used.
3991 //
3992 // For C++ modules, we don't need information on many identifiers (just
3993 // those that provide macros or are poisoned), so we mark all of
3994 // the interesting ones via PreloadIdentifierOffsets.
Vedant Kumar48b4f762018-04-14 01:40:48 +00003995 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3996 IdEnd = PP.getIdentifierTable().end();
3997 Id != IdEnd; ++Id)
3998 Id->second->setOutOfDate(true);
Richard Smith33e0f7e2015-07-22 02:08:40 +00003999 }
Manman Rena0f31a02016-04-29 19:04:05 +00004000 // Mark selectors as out of date.
4001 for (auto Sel : SelectorGeneration)
4002 SelectorOutOfDate[Sel.first] = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004003
Guy Benyei11169dd2012-12-18 14:30:41 +00004004 // Resolve any unresolved module exports.
Vedant Kumar48b4f762018-04-14 01:40:48 +00004005 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4006 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei11169dd2012-12-18 14:30:41 +00004007 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4008 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregorfb912652013-03-20 21:10:35 +00004009
4010 switch (Unresolved.Kind) {
4011 case UnresolvedModuleRef::Conflict:
4012 if (ResolvedMod) {
4013 Module::Conflict Conflict;
4014 Conflict.Other = ResolvedMod;
4015 Conflict.Message = Unresolved.String.str();
4016 Unresolved.Mod->Conflicts.push_back(Conflict);
4017 }
4018 continue;
4019
4020 case UnresolvedModuleRef::Import:
Guy Benyei11169dd2012-12-18 14:30:41 +00004021 if (ResolvedMod)
Richard Smith38477db2015-05-02 00:45:56 +00004022 Unresolved.Mod->Imports.insert(ResolvedMod);
Guy Benyei11169dd2012-12-18 14:30:41 +00004023 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00004024
Douglas Gregorfb912652013-03-20 21:10:35 +00004025 case UnresolvedModuleRef::Export:
4026 if (ResolvedMod || Unresolved.IsWildcard)
4027 Unresolved.Mod->Exports.push_back(
4028 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4029 continue;
4030 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004031 }
Douglas Gregorfb912652013-03-20 21:10:35 +00004032 UnresolvedModuleRefs.clear();
Daniel Jasperba7f2f72013-09-24 09:14:14 +00004033
Graydon Hoaree7196af2016-12-09 21:45:49 +00004034 if (Imported)
4035 Imported->append(ImportedModules.begin(),
4036 ImportedModules.end());
4037
Daniel Jasperba7f2f72013-09-24 09:14:14 +00004038 // FIXME: How do we load the 'use'd modules? They may not be submodules.
4039 // Might be unnecessary as use declarations are only used to build the
4040 // module itself.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004041
Richard Smithdbafb6c2017-06-29 23:23:46 +00004042 if (ContextObj)
4043 InitializeContext();
Guy Benyei11169dd2012-12-18 14:30:41 +00004044
Richard Smith3d8e97e2013-10-18 06:54:39 +00004045 if (SemaObj)
4046 UpdateSema();
4047
Guy Benyei11169dd2012-12-18 14:30:41 +00004048 if (DeserializationListener)
4049 DeserializationListener->ReaderInitialized(this);
4050
4051 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
Yaron Keren8b563662015-10-03 10:46:20 +00004052 if (PrimaryModule.OriginalSourceFileID.isValid()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004053 // If this AST file is a precompiled preamble, then set the
4054 // preamble file ID of the source manager to the file source file
4055 // from which the preamble was built.
4056 if (Type == MK_Preamble) {
4057 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4058 } else if (Type == MK_MainFile) {
4059 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4060 }
4061 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004062
Guy Benyei11169dd2012-12-18 14:30:41 +00004063 // For any Objective-C class definitions we have already loaded, make sure
4064 // that we load any additional categories.
Vedant Kumar48b4f762018-04-14 01:40:48 +00004065 if (ContextObj) {
4066 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4067 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4068 ObjCClassesLoaded[I],
Richard Smithdbafb6c2017-06-29 23:23:46 +00004069 PreviousGeneration);
Vedant Kumar48b4f762018-04-14 01:40:48 +00004070 }
4071 }
Douglas Gregore060e572013-01-25 01:03:03 +00004072
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004073 if (PP.getHeaderSearchInfo()
4074 .getHeaderSearchOpts()
4075 .ModulesValidateOncePerBuildSession) {
4076 // Now we are certain that the module and all modules it depends on are
4077 // up to date. Create or update timestamp files for modules that are
4078 // located in the module cache (not for PCH files that could be anywhere
4079 // in the filesystem).
Vedant Kumar48b4f762018-04-14 01:40:48 +00004080 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4081 ImportedModule &M = Loaded[I];
4082 if (M.Mod->Kind == MK_ImplicitModule) {
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004083 updateModuleTimestamp(*M.Mod);
Vedant Kumar48b4f762018-04-14 01:40:48 +00004084 }
4085 }
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004086 }
4087
Guy Benyei11169dd2012-12-18 14:30:41 +00004088 return Success;
4089}
4090
Peter Collingbourne77c89b62016-11-08 04:17:11 +00004091static ASTFileSignature readASTFileSignature(StringRef PCH);
Ben Langmuir487ea142014-10-23 18:05:36 +00004092
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004093/// Whether \p Stream starts with the AST/PCH file magic number 'CPCH'.
Ben Langmuir70a1b812015-03-24 04:43:52 +00004094static bool startsWithASTFileMagic(BitstreamCursor &Stream) {
Peter Collingbourne028eb5a2016-11-02 00:08:19 +00004095 return Stream.canSkipToPos(4) &&
4096 Stream.Read(8) == 'C' &&
Ben Langmuir70a1b812015-03-24 04:43:52 +00004097 Stream.Read(8) == 'P' &&
4098 Stream.Read(8) == 'C' &&
4099 Stream.Read(8) == 'H';
4100}
4101
Richard Smith0f99d6a2015-08-09 08:48:41 +00004102static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4103 switch (Kind) {
4104 case MK_PCH:
4105 return 0; // PCH
4106 case MK_ImplicitModule:
4107 case MK_ExplicitModule:
Manman Ren11f2a472016-08-18 17:42:15 +00004108 case MK_PrebuiltModule:
Richard Smith0f99d6a2015-08-09 08:48:41 +00004109 return 1; // module
4110 case MK_MainFile:
4111 case MK_Preamble:
4112 return 2; // main source file
4113 }
4114 llvm_unreachable("unknown module kind");
4115}
4116
Guy Benyei11169dd2012-12-18 14:30:41 +00004117ASTReader::ASTReadResult
4118ASTReader::ReadASTCore(StringRef FileName,
4119 ModuleKind Type,
4120 SourceLocation ImportLoc,
4121 ModuleFile *ImportedBy,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004122 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00004123 off_t ExpectedSize, time_t ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00004124 ASTFileSignature ExpectedSignature,
Guy Benyei11169dd2012-12-18 14:30:41 +00004125 unsigned ClientLoadCapabilities) {
4126 ModuleFile *M;
Guy Benyei11169dd2012-12-18 14:30:41 +00004127 std::string ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00004128 ModuleManager::AddModuleResult AddResult
4129 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
Richard Smith053f6c62014-05-16 23:01:30 +00004130 getGeneration(), ExpectedSize, ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00004131 ExpectedSignature, readASTFileSignature,
Douglas Gregor7029ce12013-03-19 00:28:20 +00004132 M, ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00004133
Douglas Gregor7029ce12013-03-19 00:28:20 +00004134 switch (AddResult) {
4135 case ModuleManager::AlreadyLoaded:
4136 return Success;
4137
4138 case ModuleManager::NewlyLoaded:
4139 // Load module file below.
4140 break;
4141
4142 case ModuleManager::Missing:
Richard Smithe842a472014-10-22 02:05:46 +00004143 // The module file was missing; if the client can handle that, return
Douglas Gregor7029ce12013-03-19 00:28:20 +00004144 // it.
4145 if (ClientLoadCapabilities & ARR_Missing)
4146 return Missing;
4147
4148 // Otherwise, return an error.
Richard Smith0f99d6a2015-08-09 08:48:41 +00004149 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
Adrian Prantlb3b5a732016-08-29 20:46:59 +00004150 << FileName << !ErrorStr.empty()
Richard Smith0f99d6a2015-08-09 08:48:41 +00004151 << ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00004152 return Failure;
4153
4154 case ModuleManager::OutOfDate:
4155 // We couldn't load the module file because it is out-of-date. If the
4156 // client can handle out-of-date, return it.
4157 if (ClientLoadCapabilities & ARR_OutOfDate)
4158 return OutOfDate;
4159
4160 // Otherwise, return an error.
Richard Smith0f99d6a2015-08-09 08:48:41 +00004161 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
Adrian Prantl9a06a882016-08-29 20:46:56 +00004162 << FileName << !ErrorStr.empty()
Richard Smith0f99d6a2015-08-09 08:48:41 +00004163 << ErrorStr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004164 return Failure;
4165 }
4166
Douglas Gregor7029ce12013-03-19 00:28:20 +00004167 assert(M && "Missing module file");
Guy Benyei11169dd2012-12-18 14:30:41 +00004168
Guy Benyei11169dd2012-12-18 14:30:41 +00004169 ModuleFile &F = *M;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004170 BitstreamCursor &Stream = F.Stream;
Peter Collingbourne77c89b62016-11-08 04:17:11 +00004171 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
Adrian Prantlcbc368c2015-02-25 02:44:04 +00004172 F.SizeInBits = F.Buffer->getBufferSize() * 8;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004173
Guy Benyei11169dd2012-12-18 14:30:41 +00004174 // Sniff for the signature.
Ben Langmuir70a1b812015-03-24 04:43:52 +00004175 if (!startsWithASTFileMagic(Stream)) {
Richard Smith0f99d6a2015-08-09 08:48:41 +00004176 Diag(diag::err_module_file_invalid) << moduleKindForDiagnostic(Type)
4177 << FileName;
Guy Benyei11169dd2012-12-18 14:30:41 +00004178 return Failure;
4179 }
4180
4181 // This is used for compatibility with older PCH formats.
4182 bool HaveReadControlBlock = false;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00004183 while (true) {
Chris Lattnerefa77172013-01-20 00:00:22 +00004184 llvm::BitstreamEntry Entry = Stream.advance();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004185
Chris Lattnerefa77172013-01-20 00:00:22 +00004186 switch (Entry.Kind) {
4187 case llvm::BitstreamEntry::Error:
Chris Lattnerefa77172013-01-20 00:00:22 +00004188 case llvm::BitstreamEntry::Record:
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004189 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00004190 Error("invalid record at top-level of AST file");
4191 return Failure;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004192
Chris Lattnerefa77172013-01-20 00:00:22 +00004193 case llvm::BitstreamEntry::SubBlock:
4194 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004195 }
4196
Chris Lattnerefa77172013-01-20 00:00:22 +00004197 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004198 case CONTROL_BLOCK_ID:
4199 HaveReadControlBlock = true;
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004200 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004201 case Success:
Richard Smith0f99d6a2015-08-09 08:48:41 +00004202 // Check that we didn't try to load a non-module AST file as a module.
4203 //
4204 // FIXME: Should we also perform the converse check? Loading a module as
4205 // a PCH file sort of works, but it's a bit wonky.
Manman Ren11f2a472016-08-18 17:42:15 +00004206 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4207 Type == MK_PrebuiltModule) &&
Richard Smith0f99d6a2015-08-09 08:48:41 +00004208 F.ModuleName.empty()) {
4209 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4210 if (Result != OutOfDate ||
4211 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4212 Diag(diag::err_module_file_not_module) << FileName;
4213 return Result;
4214 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004215 break;
4216
4217 case Failure: return Failure;
Douglas Gregor7029ce12013-03-19 00:28:20 +00004218 case Missing: return Missing;
Guy Benyei11169dd2012-12-18 14:30:41 +00004219 case OutOfDate: return OutOfDate;
4220 case VersionMismatch: return VersionMismatch;
4221 case ConfigurationMismatch: return ConfigurationMismatch;
4222 case HadErrors: return HadErrors;
4223 }
4224 break;
Richard Smithf8c32552015-09-02 17:45:54 +00004225
Guy Benyei11169dd2012-12-18 14:30:41 +00004226 case AST_BLOCK_ID:
4227 if (!HaveReadControlBlock) {
4228 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00004229 Diag(diag::err_pch_version_too_old);
Guy Benyei11169dd2012-12-18 14:30:41 +00004230 return VersionMismatch;
4231 }
4232
4233 // Record that we've loaded this module.
4234 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4235 return Success;
4236
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004237 case UNHASHED_CONTROL_BLOCK_ID:
4238 // This block is handled using look-ahead during ReadControlBlock. We
4239 // shouldn't get here!
4240 Error("malformed block record in AST file");
4241 return Failure;
4242
Guy Benyei11169dd2012-12-18 14:30:41 +00004243 default:
4244 if (Stream.SkipBlock()) {
4245 Error("malformed block record in AST file");
4246 return Failure;
4247 }
4248 break;
4249 }
4250 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004251
4252 return Success;
4253}
4254
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004255ASTReader::ASTReadResult
4256ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4257 unsigned ClientLoadCapabilities) {
4258 const HeaderSearchOptions &HSOpts =
4259 PP.getHeaderSearchInfo().getHeaderSearchOpts();
4260 bool AllowCompatibleConfigurationMismatch =
4261 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4262
4263 ASTReadResult Result = readUnhashedControlBlockImpl(
4264 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4265 Listener.get(),
4266 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4267
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004268 // If F was directly imported by another module, it's implicitly validated by
4269 // the importing module.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004270 if (DisableValidation || WasImportedBy ||
4271 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4272 return Success;
4273
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004274 if (Result == Failure) {
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004275 Error("malformed block record in AST file");
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004276 return Failure;
4277 }
4278
4279 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4280 // If this module has already been finalized in the PCMCache, we're stuck
4281 // with it; we can only load a single version of each module.
4282 //
4283 // This can happen when a module is imported in two contexts: in one, as a
4284 // user module; in another, as a system module (due to an import from
4285 // another module marked with the [system] flag). It usually indicates a
4286 // bug in the module map: this module should also be marked with [system].
4287 //
4288 // If -Wno-system-headers (the default), and the first import is as a
4289 // system module, then validation will fail during the as-user import,
4290 // since -Werror flags won't have been validated. However, it's reasonable
4291 // to treat this consistently as a system module.
4292 //
4293 // If -Wsystem-headers, the PCM on disk was built with
4294 // -Wno-system-headers, and the first import is as a user module, then
4295 // validation will fail during the as-system import since the PCM on disk
4296 // doesn't guarantee that -Werror was respected. However, the -Werror
4297 // flags were checked during the initial as-user import.
4298 if (PCMCache.isBufferFinal(F.FileName)) {
4299 Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4300 return Success;
4301 }
4302 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004303
4304 return Result;
4305}
4306
4307ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4308 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4309 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4310 bool ValidateDiagnosticOptions) {
4311 // Initialize a stream.
4312 BitstreamCursor Stream(StreamData);
4313
4314 // Sniff for the signature.
4315 if (!startsWithASTFileMagic(Stream))
4316 return Failure;
4317
4318 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4319 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4320 return Failure;
4321
4322 // Read all of the records in the options block.
4323 RecordData Record;
4324 ASTReadResult Result = Success;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00004325 while (true) {
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004326 llvm::BitstreamEntry Entry = Stream.advance();
4327
4328 switch (Entry.Kind) {
4329 case llvm::BitstreamEntry::Error:
4330 case llvm::BitstreamEntry::SubBlock:
4331 return Failure;
4332
4333 case llvm::BitstreamEntry::EndBlock:
4334 return Result;
4335
4336 case llvm::BitstreamEntry::Record:
4337 // The interesting case.
4338 break;
4339 }
4340
4341 // Read and process a record.
4342 Record.clear();
4343 switch (
4344 (UnhashedControlBlockRecordTypes)Stream.readRecord(Entry.ID, Record)) {
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00004345 case SIGNATURE:
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004346 if (F)
4347 std::copy(Record.begin(), Record.end(), F->Signature.data());
4348 break;
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004349 case DIAGNOSTIC_OPTIONS: {
4350 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4351 if (Listener && ValidateDiagnosticOptions &&
4352 !AllowCompatibleConfigurationMismatch &&
4353 ParseDiagnosticOptions(Record, Complain, *Listener))
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004354 Result = OutOfDate; // Don't return early. Read the signature.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004355 break;
4356 }
4357 case DIAG_PRAGMA_MAPPINGS:
4358 if (!F)
4359 break;
4360 if (F->PragmaDiagMappings.empty())
4361 F->PragmaDiagMappings.swap(Record);
4362 else
4363 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4364 Record.begin(), Record.end());
4365 break;
4366 }
4367 }
4368}
4369
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004370/// Parse a record and blob containing module file extension metadata.
4371static bool parseModuleFileExtensionMetadata(
4372 const SmallVectorImpl<uint64_t> &Record,
4373 StringRef Blob,
4374 ModuleFileExtensionMetadata &Metadata) {
4375 if (Record.size() < 4) return true;
4376
4377 Metadata.MajorVersion = Record[0];
4378 Metadata.MinorVersion = Record[1];
4379
4380 unsigned BlockNameLen = Record[2];
4381 unsigned UserInfoLen = Record[3];
4382
4383 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4384
4385 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4386 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4387 Blob.data() + BlockNameLen + UserInfoLen);
4388 return false;
4389}
4390
4391ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4392 BitstreamCursor &Stream = F.Stream;
4393
4394 RecordData Record;
4395 while (true) {
4396 llvm::BitstreamEntry Entry = Stream.advance();
4397 switch (Entry.Kind) {
4398 case llvm::BitstreamEntry::SubBlock:
4399 if (Stream.SkipBlock())
4400 return Failure;
4401
4402 continue;
4403
4404 case llvm::BitstreamEntry::EndBlock:
4405 return Success;
4406
4407 case llvm::BitstreamEntry::Error:
4408 return HadErrors;
4409
4410 case llvm::BitstreamEntry::Record:
4411 break;
4412 }
4413
4414 Record.clear();
4415 StringRef Blob;
4416 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4417 switch (RecCode) {
4418 case EXTENSION_METADATA: {
4419 ModuleFileExtensionMetadata Metadata;
4420 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4421 return Failure;
4422
4423 // Find a module file extension with this block name.
4424 auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4425 if (Known == ModuleFileExtensions.end()) break;
4426
4427 // Form a reader.
4428 if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4429 F, Stream)) {
4430 F.ExtensionReaders.push_back(std::move(Reader));
4431 }
4432
4433 break;
4434 }
4435 }
4436 }
4437
4438 return Success;
Guy Benyei11169dd2012-12-18 14:30:41 +00004439}
4440
Richard Smitha7e2cc62015-05-01 01:53:09 +00004441void ASTReader::InitializeContext() {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004442 assert(ContextObj && "no context to initialize");
4443 ASTContext &Context = *ContextObj;
4444
Guy Benyei11169dd2012-12-18 14:30:41 +00004445 // If there's a listener, notify them that we "read" the translation unit.
4446 if (DeserializationListener)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004447 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
Guy Benyei11169dd2012-12-18 14:30:41 +00004448 Context.getTranslationUnitDecl());
4449
Guy Benyei11169dd2012-12-18 14:30:41 +00004450 // FIXME: Find a better way to deal with collisions between these
4451 // built-in types. Right now, we just ignore the problem.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004452
Guy Benyei11169dd2012-12-18 14:30:41 +00004453 // Load the special types.
4454 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4455 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4456 if (!Context.CFConstantStringTypeDecl)
4457 Context.setCFConstantStringType(GetType(String));
4458 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004459
Guy Benyei11169dd2012-12-18 14:30:41 +00004460 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4461 QualType FileType = GetType(File);
4462 if (FileType.isNull()) {
4463 Error("FILE type is NULL");
4464 return;
4465 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004466
Guy Benyei11169dd2012-12-18 14:30:41 +00004467 if (!Context.FILEDecl) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004468 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Guy Benyei11169dd2012-12-18 14:30:41 +00004469 Context.setFILEDecl(Typedef->getDecl());
4470 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004471 const TagType *Tag = FileType->getAs<TagType>();
Guy Benyei11169dd2012-12-18 14:30:41 +00004472 if (!Tag) {
4473 Error("Invalid FILE type in AST file");
4474 return;
4475 }
4476 Context.setFILEDecl(Tag->getDecl());
4477 }
4478 }
4479 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004480
Guy Benyei11169dd2012-12-18 14:30:41 +00004481 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4482 QualType Jmp_bufType = GetType(Jmp_buf);
4483 if (Jmp_bufType.isNull()) {
4484 Error("jmp_buf type is NULL");
4485 return;
4486 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004487
Guy Benyei11169dd2012-12-18 14:30:41 +00004488 if (!Context.jmp_bufDecl) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004489 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Guy Benyei11169dd2012-12-18 14:30:41 +00004490 Context.setjmp_bufDecl(Typedef->getDecl());
4491 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004492 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Guy Benyei11169dd2012-12-18 14:30:41 +00004493 if (!Tag) {
4494 Error("Invalid jmp_buf type in AST file");
4495 return;
4496 }
4497 Context.setjmp_bufDecl(Tag->getDecl());
4498 }
4499 }
4500 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004501
Guy Benyei11169dd2012-12-18 14:30:41 +00004502 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4503 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4504 if (Sigjmp_bufType.isNull()) {
4505 Error("sigjmp_buf type is NULL");
4506 return;
4507 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004508
Guy Benyei11169dd2012-12-18 14:30:41 +00004509 if (!Context.sigjmp_bufDecl) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004510 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Guy Benyei11169dd2012-12-18 14:30:41 +00004511 Context.setsigjmp_bufDecl(Typedef->getDecl());
4512 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004513 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Guy Benyei11169dd2012-12-18 14:30:41 +00004514 assert(Tag && "Invalid sigjmp_buf type in AST file");
4515 Context.setsigjmp_bufDecl(Tag->getDecl());
4516 }
4517 }
4518 }
4519
4520 if (unsigned ObjCIdRedef
4521 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4522 if (Context.ObjCIdRedefinitionType.isNull())
4523 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4524 }
4525
4526 if (unsigned ObjCClassRedef
4527 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4528 if (Context.ObjCClassRedefinitionType.isNull())
4529 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4530 }
4531
4532 if (unsigned ObjCSelRedef
4533 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4534 if (Context.ObjCSelRedefinitionType.isNull())
4535 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4536 }
4537
4538 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4539 QualType Ucontext_tType = GetType(Ucontext_t);
4540 if (Ucontext_tType.isNull()) {
4541 Error("ucontext_t type is NULL");
4542 return;
4543 }
4544
4545 if (!Context.ucontext_tDecl) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004546 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
Guy Benyei11169dd2012-12-18 14:30:41 +00004547 Context.setucontext_tDecl(Typedef->getDecl());
4548 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004549 const TagType *Tag = Ucontext_tType->getAs<TagType>();
Guy Benyei11169dd2012-12-18 14:30:41 +00004550 assert(Tag && "Invalid ucontext_t type in AST file");
4551 Context.setucontext_tDecl(Tag->getDecl());
4552 }
4553 }
4554 }
4555 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004556
Guy Benyei11169dd2012-12-18 14:30:41 +00004557 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4558
4559 // If there were any CUDA special declarations, deserialize them.
4560 if (!CUDASpecialDeclRefs.empty()) {
4561 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4562 Context.setcudaConfigureCallDecl(
4563 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4564 }
Richard Smith56be7542014-03-21 00:33:59 +00004565
Guy Benyei11169dd2012-12-18 14:30:41 +00004566 // Re-export any modules that were imported by a non-module AST file.
Richard Smitha7e2cc62015-05-01 01:53:09 +00004567 // FIXME: This does not make macro-only imports visible again.
Richard Smith56be7542014-03-21 00:33:59 +00004568 for (auto &Import : ImportedModules) {
Richard Smitha7e2cc62015-05-01 01:53:09 +00004569 if (Module *Imported = getSubmodule(Import.ID)) {
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00004570 makeModuleVisible(Imported, Module::AllVisible,
Richard Smitha7e2cc62015-05-01 01:53:09 +00004571 /*ImportLoc=*/Import.ImportLoc);
Ben Langmuir6d25fdc2016-02-11 17:04:42 +00004572 if (Import.ImportLoc.isValid())
4573 PP.makeModuleVisible(Imported, Import.ImportLoc);
4574 // FIXME: should we tell Sema to make the module visible too?
Richard Smitha7e2cc62015-05-01 01:53:09 +00004575 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004576 }
4577 ImportedModules.clear();
4578}
4579
4580void ASTReader::finalizeForWriting() {
Richard Smithde711422015-04-23 21:20:19 +00004581 // Nothing to do for now.
Guy Benyei11169dd2012-12-18 14:30:41 +00004582}
4583
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004584/// Reads and return the signature record from \p PCH's control block, or
Peter Collingbourne77c89b62016-11-08 04:17:11 +00004585/// else returns 0.
4586static ASTFileSignature readASTFileSignature(StringRef PCH) {
4587 BitstreamCursor Stream(PCH);
Ben Langmuir70a1b812015-03-24 04:43:52 +00004588 if (!startsWithASTFileMagic(Stream))
Vedant Kumar48b4f762018-04-14 01:40:48 +00004589 return ASTFileSignature();
Ben Langmuir487ea142014-10-23 18:05:36 +00004590
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004591 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4592 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
Vedant Kumar48b4f762018-04-14 01:40:48 +00004593 return ASTFileSignature();
Ben Langmuir487ea142014-10-23 18:05:36 +00004594
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004595 // Scan for SIGNATURE inside the diagnostic options block.
Ben Langmuir487ea142014-10-23 18:05:36 +00004596 ASTReader::RecordData Record;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00004597 while (true) {
Ben Langmuir487ea142014-10-23 18:05:36 +00004598 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Simon Pilgrim0b33f112016-11-16 16:11:08 +00004599 if (Entry.Kind != llvm::BitstreamEntry::Record)
Vedant Kumar48b4f762018-04-14 01:40:48 +00004600 return ASTFileSignature();
Ben Langmuir487ea142014-10-23 18:05:36 +00004601
4602 Record.clear();
4603 StringRef Blob;
4604 if (SIGNATURE == Stream.readRecord(Entry.ID, Record, &Blob))
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004605 return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
4606 (uint32_t)Record[3], (uint32_t)Record[4]}}};
Ben Langmuir487ea142014-10-23 18:05:36 +00004607 }
4608}
4609
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004610/// Retrieve the name of the original source file name
Guy Benyei11169dd2012-12-18 14:30:41 +00004611/// directly from the AST file, without actually loading the AST
4612/// file.
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004613std::string ASTReader::getOriginalSourceFile(
4614 const std::string &ASTFileName, FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004615 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004616 // Open the AST file.
Benjamin Kramera8857962014-10-26 22:44:13 +00004617 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00004618 if (!Buffer) {
Benjamin Kramera8857962014-10-26 22:44:13 +00004619 Diags.Report(diag::err_fe_unable_to_read_pch_file)
4620 << ASTFileName << Buffer.getError().message();
Vedant Kumar48b4f762018-04-14 01:40:48 +00004621 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00004622 }
4623
4624 // Initialize the stream
Peter Collingbourne77c89b62016-11-08 04:17:11 +00004625 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
Guy Benyei11169dd2012-12-18 14:30:41 +00004626
4627 // Sniff for the signature.
Ben Langmuir70a1b812015-03-24 04:43:52 +00004628 if (!startsWithASTFileMagic(Stream)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004629 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Vedant Kumar48b4f762018-04-14 01:40:48 +00004630 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00004631 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004632
Chris Lattnere7b154b2013-01-19 21:39:22 +00004633 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004634 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004635 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Vedant Kumar48b4f762018-04-14 01:40:48 +00004636 return std::string();
Chris Lattnere7b154b2013-01-19 21:39:22 +00004637 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004638
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004639 // Scan for ORIGINAL_FILE inside the control block.
4640 RecordData Record;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00004641 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004642 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattnere7b154b2013-01-19 21:39:22 +00004643 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
Vedant Kumar48b4f762018-04-14 01:40:48 +00004644 return std::string();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004645
Chris Lattnere7b154b2013-01-19 21:39:22 +00004646 if (Entry.Kind != llvm::BitstreamEntry::Record) {
4647 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Vedant Kumar48b4f762018-04-14 01:40:48 +00004648 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00004649 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004650
Guy Benyei11169dd2012-12-18 14:30:41 +00004651 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004652 StringRef Blob;
4653 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
4654 return Blob.str();
Guy Benyei11169dd2012-12-18 14:30:41 +00004655 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004656}
4657
4658namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00004659
Guy Benyei11169dd2012-12-18 14:30:41 +00004660 class SimplePCHValidator : public ASTReaderListener {
4661 const LangOptions &ExistingLangOpts;
4662 const TargetOptions &ExistingTargetOpts;
4663 const PreprocessorOptions &ExistingPPOpts;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004664 std::string ExistingModuleCachePath;
Guy Benyei11169dd2012-12-18 14:30:41 +00004665 FileManager &FileMgr;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004666
Guy Benyei11169dd2012-12-18 14:30:41 +00004667 public:
4668 SimplePCHValidator(const LangOptions &ExistingLangOpts,
4669 const TargetOptions &ExistingTargetOpts,
4670 const PreprocessorOptions &ExistingPPOpts,
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004671 StringRef ExistingModuleCachePath,
Guy Benyei11169dd2012-12-18 14:30:41 +00004672 FileManager &FileMgr)
4673 : ExistingLangOpts(ExistingLangOpts),
4674 ExistingTargetOpts(ExistingTargetOpts),
4675 ExistingPPOpts(ExistingPPOpts),
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004676 ExistingModuleCachePath(ExistingModuleCachePath),
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00004677 FileMgr(FileMgr) {}
Guy Benyei11169dd2012-12-18 14:30:41 +00004678
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004679 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
4680 bool AllowCompatibleDifferences) override {
4681 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
4682 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004683 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00004684
Chandler Carruth0d745bc2015-03-14 04:47:43 +00004685 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
4686 bool AllowCompatibleDifferences) override {
4687 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
4688 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004689 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00004690
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004691 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
4692 StringRef SpecificModuleCachePath,
4693 bool Complain) override {
4694 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4695 ExistingModuleCachePath,
4696 nullptr, ExistingLangOpts);
4697 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00004698
Craig Topper3e89dfe2014-03-13 02:13:41 +00004699 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
4700 bool Complain,
4701 std::string &SuggestedPredefines) override {
Craig Toppera13603a2014-05-22 05:54:18 +00004702 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00004703 SuggestedPredefines, ExistingLangOpts);
Guy Benyei11169dd2012-12-18 14:30:41 +00004704 }
4705 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00004706
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00004707} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00004708
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004709bool ASTReader::readASTFileControlBlock(
4710 StringRef Filename, FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004711 const PCHContainerReader &PCHContainerRdr,
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004712 bool FindModuleFileExtensions,
Manman Ren47a44452016-07-26 17:12:17 +00004713 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004714 // Open the AST file.
Richard Smith7f330cd2015-03-18 01:42:29 +00004715 // FIXME: This allows use of the VFS; we do not allow use of the
4716 // VFS when actually loading a module.
Benjamin Kramera8857962014-10-26 22:44:13 +00004717 auto Buffer = FileMgr.getBufferForFile(Filename);
Guy Benyei11169dd2012-12-18 14:30:41 +00004718 if (!Buffer) {
4719 return true;
4720 }
4721
4722 // Initialize the stream
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004723 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
4724 BitstreamCursor Stream(Bytes);
Guy Benyei11169dd2012-12-18 14:30:41 +00004725
4726 // Sniff for the signature.
Ben Langmuir70a1b812015-03-24 04:43:52 +00004727 if (!startsWithASTFileMagic(Stream))
Guy Benyei11169dd2012-12-18 14:30:41 +00004728 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00004729
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004730 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004731 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004732 return true;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004733
4734 bool NeedsInputFiles = Listener.needsInputFileVisitation();
Ben Langmuircb69b572014-03-07 06:40:32 +00004735 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
Richard Smithd4b230b2014-10-27 23:01:16 +00004736 bool NeedsImports = Listener.needsImportVisitation();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004737 BitstreamCursor InputFilesCursor;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004738
Guy Benyei11169dd2012-12-18 14:30:41 +00004739 RecordData Record;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004740 std::string ModuleDir;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004741 bool DoneWithControlBlock = false;
4742 while (!DoneWithControlBlock) {
Richard Smith0516b182015-09-08 19:40:14 +00004743 llvm::BitstreamEntry Entry = Stream.advance();
4744
4745 switch (Entry.Kind) {
4746 case llvm::BitstreamEntry::SubBlock: {
4747 switch (Entry.ID) {
4748 case OPTIONS_BLOCK_ID: {
4749 std::string IgnoredSuggestedPredefines;
4750 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
4751 /*AllowCompatibleConfigurationMismatch*/ false,
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004752 Listener, IgnoredSuggestedPredefines) != Success)
Richard Smith0516b182015-09-08 19:40:14 +00004753 return true;
4754 break;
4755 }
4756
4757 case INPUT_FILES_BLOCK_ID:
4758 InputFilesCursor = Stream;
4759 if (Stream.SkipBlock() ||
4760 (NeedsInputFiles &&
4761 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)))
4762 return true;
4763 break;
4764
4765 default:
4766 if (Stream.SkipBlock())
4767 return true;
4768 break;
4769 }
4770
4771 continue;
4772 }
4773
4774 case llvm::BitstreamEntry::EndBlock:
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004775 DoneWithControlBlock = true;
4776 break;
Richard Smith0516b182015-09-08 19:40:14 +00004777
4778 case llvm::BitstreamEntry::Error:
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004779 return true;
Richard Smith0516b182015-09-08 19:40:14 +00004780
4781 case llvm::BitstreamEntry::Record:
4782 break;
4783 }
4784
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004785 if (DoneWithControlBlock) break;
4786
Guy Benyei11169dd2012-12-18 14:30:41 +00004787 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004788 StringRef Blob;
4789 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004790 switch ((ControlRecordTypes)RecCode) {
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00004791 case METADATA:
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004792 if (Record[0] != VERSION_MAJOR)
4793 return true;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00004794 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004795 return true;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004796 break;
Ben Langmuir4f5212a2014-04-14 22:12:44 +00004797 case MODULE_NAME:
4798 Listener.ReadModuleName(Blob);
4799 break;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004800 case MODULE_DIRECTORY:
4801 ModuleDir = Blob;
4802 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00004803 case MODULE_MAP_FILE: {
4804 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004805 auto Path = ReadString(Record, Idx);
4806 ResolveImportedPath(Path, ModuleDir);
4807 Listener.ReadModuleMapFile(Path);
Ben Langmuir4f5212a2014-04-14 22:12:44 +00004808 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00004809 }
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004810 case INPUT_FILE_OFFSETS: {
4811 if (!NeedsInputFiles)
4812 break;
4813
4814 unsigned NumInputFiles = Record[0];
4815 unsigned NumUserFiles = Record[1];
Raphael Isemanneb13d3d2018-05-23 09:02:40 +00004816 const llvm::support::unaligned_uint64_t *InputFileOffs =
4817 (const llvm::support::unaligned_uint64_t *)Blob.data();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004818 for (unsigned I = 0; I != NumInputFiles; ++I) {
4819 // Go find this input file.
4820 bool isSystemFile = I >= NumUserFiles;
Ben Langmuircb69b572014-03-07 06:40:32 +00004821
4822 if (isSystemFile && !NeedsSystemInputFiles)
4823 break; // the rest are system input files
4824
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004825 BitstreamCursor &Cursor = InputFilesCursor;
4826 SavedStreamPosition SavedPosition(Cursor);
4827 Cursor.JumpToBit(InputFileOffs[I]);
4828
4829 unsigned Code = Cursor.ReadCode();
4830 RecordData Record;
4831 StringRef Blob;
4832 bool shouldContinue = false;
4833 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4834 case INPUT_FILE:
Vedant Kumar48b4f762018-04-14 01:40:48 +00004835 bool Overridden = static_cast<bool>(Record[3]);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004836 std::string Filename = Blob;
4837 ResolveImportedPath(Filename, ModuleDir);
Richard Smith216a3bd2015-08-13 17:57:10 +00004838 shouldContinue = Listener.visitInputFile(
4839 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004840 break;
4841 }
4842 if (!shouldContinue)
4843 break;
4844 }
4845 break;
4846 }
4847
Richard Smithd4b230b2014-10-27 23:01:16 +00004848 case IMPORTS: {
4849 if (!NeedsImports)
4850 break;
4851
4852 unsigned Idx = 0, N = Record.size();
4853 while (Idx < N) {
4854 // Read information about the AST file.
Richard Smith79c98cc2014-10-27 23:25:15 +00004855 Idx += 5; // ImportLoc, Size, ModTime, Signature
Boris Kolpackovd30446f2017-08-31 06:26:43 +00004856 SkipString(Record, Idx); // Module name; FIXME: pass to listener?
Richard Smith7ed1bc92014-12-05 22:42:13 +00004857 std::string Filename = ReadString(Record, Idx);
4858 ResolveImportedPath(Filename, ModuleDir);
4859 Listener.visitImport(Filename);
Richard Smithd4b230b2014-10-27 23:01:16 +00004860 }
4861 break;
4862 }
4863
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004864 default:
4865 // No other validation to perform.
4866 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004867 }
4868 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004869
4870 // Look for module file extension blocks, if requested.
4871 if (FindModuleFileExtensions) {
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004872 BitstreamCursor SavedStream = Stream;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004873 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
4874 bool DoneWithExtensionBlock = false;
4875 while (!DoneWithExtensionBlock) {
4876 llvm::BitstreamEntry Entry = Stream.advance();
4877
4878 switch (Entry.Kind) {
4879 case llvm::BitstreamEntry::SubBlock:
4880 if (Stream.SkipBlock())
4881 return true;
4882
4883 continue;
4884
4885 case llvm::BitstreamEntry::EndBlock:
4886 DoneWithExtensionBlock = true;
4887 continue;
4888
4889 case llvm::BitstreamEntry::Error:
4890 return true;
4891
4892 case llvm::BitstreamEntry::Record:
4893 break;
4894 }
4895
4896 Record.clear();
4897 StringRef Blob;
4898 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4899 switch (RecCode) {
4900 case EXTENSION_METADATA: {
4901 ModuleFileExtensionMetadata Metadata;
4902 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4903 return true;
4904
4905 Listener.readModuleFileExtension(Metadata);
4906 break;
4907 }
4908 }
4909 }
4910 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004911 Stream = SavedStream;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004912 }
4913
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004914 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4915 if (readUnhashedControlBlockImpl(
4916 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
4917 /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
4918 ValidateDiagnosticOptions) != Success)
4919 return true;
4920
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004921 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00004922}
4923
Benjamin Kramerf6021ec2017-03-21 21:35:04 +00004924bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
4925 const PCHContainerReader &PCHContainerRdr,
4926 const LangOptions &LangOpts,
4927 const TargetOptions &TargetOpts,
4928 const PreprocessorOptions &PPOpts,
4929 StringRef ExistingModuleCachePath) {
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004930 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
4931 ExistingModuleCachePath, FileMgr);
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004932 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004933 /*FindModuleFileExtensions=*/false,
Manman Ren47a44452016-07-26 17:12:17 +00004934 validator,
4935 /*ValidateDiagnosticOptions=*/true);
Guy Benyei11169dd2012-12-18 14:30:41 +00004936}
4937
Ben Langmuir2c9af442014-04-10 17:57:43 +00004938ASTReader::ASTReadResult
4939ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004940 // Enter the submodule block.
4941 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4942 Error("malformed submodule block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004943 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004944 }
4945
4946 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4947 bool First = true;
Craig Toppera13603a2014-05-22 05:54:18 +00004948 Module *CurrentModule = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004949 RecordData Record;
4950 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004951 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004952
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004953 switch (Entry.Kind) {
4954 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4955 case llvm::BitstreamEntry::Error:
4956 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004957 return Failure;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004958 case llvm::BitstreamEntry::EndBlock:
Ben Langmuir2c9af442014-04-10 17:57:43 +00004959 return Success;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004960 case llvm::BitstreamEntry::Record:
4961 // The interesting case.
4962 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004963 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004964
Guy Benyei11169dd2012-12-18 14:30:41 +00004965 // Read a record.
Chris Lattner0e6c9402013-01-20 02:38:54 +00004966 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004967 Record.clear();
Richard Smith03478d92014-10-23 22:12:14 +00004968 auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob);
4969
4970 if ((Kind == SUBMODULE_METADATA) != First) {
4971 Error("submodule metadata record should be at beginning of block");
4972 return Failure;
4973 }
4974 First = false;
4975
4976 // Submodule information is only valid if we have a current module.
4977 // FIXME: Should we error on these cases?
4978 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
4979 Kind != SUBMODULE_DEFINITION)
4980 continue;
4981
4982 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004983 default: // Default behavior: ignore.
4984 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004985
Richard Smith03478d92014-10-23 22:12:14 +00004986 case SUBMODULE_DEFINITION: {
Jordan Rose90b0a1f2018-04-20 17:16:04 +00004987 if (Record.size() < 12) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004988 Error("malformed module definition");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004989 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004990 }
Richard Smith03478d92014-10-23 22:12:14 +00004991
Chris Lattner0e6c9402013-01-20 02:38:54 +00004992 StringRef Name = Blob;
Richard Smith9bca2982014-03-08 00:03:56 +00004993 unsigned Idx = 0;
4994 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4995 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
Vedant Kumar48b4f762018-04-14 01:40:48 +00004996 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
Richard Smith9bca2982014-03-08 00:03:56 +00004997 bool IsFramework = Record[Idx++];
4998 bool IsExplicit = Record[Idx++];
4999 bool IsSystem = Record[Idx++];
5000 bool IsExternC = Record[Idx++];
5001 bool InferSubmodules = Record[Idx++];
5002 bool InferExplicitSubmodules = Record[Idx++];
5003 bool InferExportWildcard = Record[Idx++];
5004 bool ConfigMacrosExhaustive = Record[Idx++];
Jordan Rose90b0a1f2018-04-20 17:16:04 +00005005 bool ModuleMapIsPrivate = Record[Idx++];
Douglas Gregor8d932422013-03-20 03:59:18 +00005006
Ben Langmuirbeee15e2014-04-14 18:00:01 +00005007 Module *ParentModule = nullptr;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00005008 if (Parent)
Guy Benyei11169dd2012-12-18 14:30:41 +00005009 ParentModule = getSubmodule(Parent);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00005010
Guy Benyei11169dd2012-12-18 14:30:41 +00005011 // Retrieve this (sub)module from the module map, creating it if
5012 // necessary.
David Blaikie9ffe5a32017-01-30 05:00:26 +00005013 CurrentModule =
5014 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5015 .first;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00005016
5017 // FIXME: set the definition loc for CurrentModule, or call
5018 // ModMap.setInferredModuleAllowedBy()
5019
Guy Benyei11169dd2012-12-18 14:30:41 +00005020 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5021 if (GlobalIndex >= SubmodulesLoaded.size() ||
5022 SubmodulesLoaded[GlobalIndex]) {
5023 Error("too many submodules");
Ben Langmuir2c9af442014-04-10 17:57:43 +00005024 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00005025 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00005026
Douglas Gregor7029ce12013-03-19 00:28:20 +00005027 if (!ParentModule) {
5028 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5029 if (CurFile != F.File) {
5030 if (!Diags.isDiagnosticInFlight()) {
5031 Diag(diag::err_module_file_conflict)
5032 << CurrentModule->getTopLevelModuleName()
5033 << CurFile->getName()
5034 << F.File->getName();
5035 }
Ben Langmuir2c9af442014-04-10 17:57:43 +00005036 return Failure;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00005037 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00005038 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00005039
5040 CurrentModule->setASTFile(F.File);
Richard Smithab755972017-06-05 18:10:11 +00005041 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00005042 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00005043
Richard Smithdd8b5332017-09-04 05:37:53 +00005044 CurrentModule->Kind = Kind;
Adrian Prantl15bcf702015-06-30 17:39:43 +00005045 CurrentModule->Signature = F.Signature;
Guy Benyei11169dd2012-12-18 14:30:41 +00005046 CurrentModule->IsFromModuleFile = true;
5047 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Richard Smith9bca2982014-03-08 00:03:56 +00005048 CurrentModule->IsExternC = IsExternC;
Guy Benyei11169dd2012-12-18 14:30:41 +00005049 CurrentModule->InferSubmodules = InferSubmodules;
5050 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5051 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor8d932422013-03-20 03:59:18 +00005052 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Jordan Rose90b0a1f2018-04-20 17:16:04 +00005053 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
Guy Benyei11169dd2012-12-18 14:30:41 +00005054 if (DeserializationListener)
5055 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005056
Guy Benyei11169dd2012-12-18 14:30:41 +00005057 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005058
Richard Smith8a3e39a2016-03-28 21:31:09 +00005059 // Clear out data that will be replaced by what is in the module file.
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005060 CurrentModule->LinkLibraries.clear();
Douglas Gregor8d932422013-03-20 03:59:18 +00005061 CurrentModule->ConfigMacros.clear();
Douglas Gregorfb912652013-03-20 21:10:35 +00005062 CurrentModule->UnresolvedConflicts.clear();
5063 CurrentModule->Conflicts.clear();
Richard Smith8a3e39a2016-03-28 21:31:09 +00005064
5065 // The module is available unless it's missing a requirement; relevant
5066 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5067 // Missing headers that were present when the module was built do not
5068 // make it unavailable -- if we got this far, this must be an explicitly
5069 // imported module file.
5070 CurrentModule->Requirements.clear();
5071 CurrentModule->MissingHeaders.clear();
5072 CurrentModule->IsMissingRequirement =
5073 ParentModule && ParentModule->IsMissingRequirement;
5074 CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
Guy Benyei11169dd2012-12-18 14:30:41 +00005075 break;
5076 }
Richard Smith8a3e39a2016-03-28 21:31:09 +00005077
Guy Benyei11169dd2012-12-18 14:30:41 +00005078 case SUBMODULE_UMBRELLA_HEADER: {
Richard Smith2b63d152015-05-16 02:28:53 +00005079 std::string Filename = Blob;
5080 ResolveImportedPath(F, Filename);
5081 if (auto *Umbrella = PP.getFileManager().getFile(Filename)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005082 if (!CurrentModule->getUmbrellaHeader())
Richard Smith2b63d152015-05-16 02:28:53 +00005083 ModMap.setUmbrellaHeader(CurrentModule, Umbrella, Blob);
5084 else if (CurrentModule->getUmbrellaHeader().Entry != Umbrella) {
Bruno Cardoso Lopes573b13f2017-03-22 00:11:21 +00005085 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5086 Error("mismatched umbrella headers in submodule");
5087 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00005088 }
5089 }
5090 break;
5091 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005092
Richard Smith202210b2014-10-24 20:23:01 +00005093 case SUBMODULE_HEADER:
5094 case SUBMODULE_EXCLUDED_HEADER:
5095 case SUBMODULE_PRIVATE_HEADER:
5096 // We lazily associate headers with their modules via the HeaderInfo table.
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00005097 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5098 // of complete filenames or remove it entirely.
Richard Smith202210b2014-10-24 20:23:01 +00005099 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005100
Richard Smith202210b2014-10-24 20:23:01 +00005101 case SUBMODULE_TEXTUAL_HEADER:
5102 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5103 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5104 // them here.
5105 break;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00005106
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005107 case SUBMODULE_TOPHEADER:
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00005108 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00005109 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005110
5111 case SUBMODULE_UMBRELLA_DIR: {
Richard Smith2b63d152015-05-16 02:28:53 +00005112 std::string Dirname = Blob;
5113 ResolveImportedPath(F, Dirname);
5114 if (auto *Umbrella = PP.getFileManager().getDirectory(Dirname)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005115 if (!CurrentModule->getUmbrellaDir())
Richard Smith2b63d152015-05-16 02:28:53 +00005116 ModMap.setUmbrellaDir(CurrentModule, Umbrella, Blob);
5117 else if (CurrentModule->getUmbrellaDir().Entry != Umbrella) {
Ben Langmuir2c9af442014-04-10 17:57:43 +00005118 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5119 Error("mismatched umbrella directories in submodule");
5120 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00005121 }
5122 }
5123 break;
5124 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005125
Guy Benyei11169dd2012-12-18 14:30:41 +00005126 case SUBMODULE_METADATA: {
Guy Benyei11169dd2012-12-18 14:30:41 +00005127 F.BaseSubmoduleID = getTotalNumSubmodules();
5128 F.LocalNumSubmodules = Record[0];
5129 unsigned LocalBaseSubmoduleID = Record[1];
5130 if (F.LocalNumSubmodules > 0) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005131 // Introduce the global -> local mapping for submodules within this
Guy Benyei11169dd2012-12-18 14:30:41 +00005132 // module.
5133 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005134
5135 // Introduce the local -> global mapping for submodules within this
Guy Benyei11169dd2012-12-18 14:30:41 +00005136 // module.
5137 F.SubmoduleRemap.insertOrReplace(
5138 std::make_pair(LocalBaseSubmoduleID,
5139 F.BaseSubmoduleID - LocalBaseSubmoduleID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00005140
Ben Langmuir52ca6782014-10-20 16:27:32 +00005141 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5142 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005143 break;
5144 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005145
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005146 case SUBMODULE_IMPORTS:
Guy Benyei11169dd2012-12-18 14:30:41 +00005147 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregorfb912652013-03-20 21:10:35 +00005148 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00005149 Unresolved.File = &F;
5150 Unresolved.Mod = CurrentModule;
5151 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00005152 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei11169dd2012-12-18 14:30:41 +00005153 Unresolved.IsWildcard = false;
Douglas Gregorfb912652013-03-20 21:10:35 +00005154 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00005155 }
5156 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005157
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005158 case SUBMODULE_EXPORTS:
Guy Benyei11169dd2012-12-18 14:30:41 +00005159 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregorfb912652013-03-20 21:10:35 +00005160 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00005161 Unresolved.File = &F;
5162 Unresolved.Mod = CurrentModule;
5163 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00005164 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei11169dd2012-12-18 14:30:41 +00005165 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregorfb912652013-03-20 21:10:35 +00005166 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00005167 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005168
5169 // Once we've loaded the set of exports, there's no reason to keep
Guy Benyei11169dd2012-12-18 14:30:41 +00005170 // the parsed, unresolved exports around.
5171 CurrentModule->UnresolvedExports.clear();
5172 break;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005173
5174 case SUBMODULE_REQUIRES:
Richard Smithdbafb6c2017-06-29 23:23:46 +00005175 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5176 PP.getTargetInfo());
Guy Benyei11169dd2012-12-18 14:30:41 +00005177 break;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005178
5179 case SUBMODULE_LINK_LIBRARY:
Bruno Cardoso Lopesa3b5f712018-04-16 19:42:32 +00005180 ModMap.resolveLinkAsDependencies(CurrentModule);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005181 CurrentModule->LinkLibraries.push_back(
Chris Lattner0e6c9402013-01-20 02:38:54 +00005182 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005183 break;
Douglas Gregor35b13ec2013-03-20 00:22:05 +00005184
5185 case SUBMODULE_CONFIG_MACRO:
Douglas Gregor35b13ec2013-03-20 00:22:05 +00005186 CurrentModule->ConfigMacros.push_back(Blob.str());
5187 break;
Douglas Gregorfb912652013-03-20 21:10:35 +00005188
5189 case SUBMODULE_CONFLICT: {
Douglas Gregorfb912652013-03-20 21:10:35 +00005190 UnresolvedModuleRef Unresolved;
5191 Unresolved.File = &F;
5192 Unresolved.Mod = CurrentModule;
5193 Unresolved.ID = Record[0];
5194 Unresolved.Kind = UnresolvedModuleRef::Conflict;
5195 Unresolved.IsWildcard = false;
5196 Unresolved.String = Blob;
5197 UnresolvedModuleRefs.push_back(Unresolved);
5198 break;
5199 }
Richard Smithdc1f0422016-07-20 19:10:16 +00005200
Douglas Gregorf0b11de2017-09-14 23:38:44 +00005201 case SUBMODULE_INITIALIZERS: {
Richard Smithdbafb6c2017-06-29 23:23:46 +00005202 if (!ContextObj)
5203 break;
Richard Smithdc1f0422016-07-20 19:10:16 +00005204 SmallVector<uint32_t, 16> Inits;
5205 for (auto &ID : Record)
5206 Inits.push_back(getGlobalDeclID(F, ID));
Richard Smithdbafb6c2017-06-29 23:23:46 +00005207 ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
Richard Smithdc1f0422016-07-20 19:10:16 +00005208 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005209 }
Douglas Gregorf0b11de2017-09-14 23:38:44 +00005210
5211 case SUBMODULE_EXPORT_AS:
5212 CurrentModule->ExportAsModule = Blob.str();
Bruno Cardoso Lopesa3b5f712018-04-16 19:42:32 +00005213 ModMap.addLinkAsDependency(CurrentModule);
Douglas Gregorf0b11de2017-09-14 23:38:44 +00005214 break;
5215 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005216 }
5217}
5218
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005219/// Parse the record that corresponds to a LangOptions data
Guy Benyei11169dd2012-12-18 14:30:41 +00005220/// structure.
5221///
5222/// This routine parses the language options from the AST file and then gives
5223/// them to the AST listener if one is set.
5224///
5225/// \returns true if the listener deems the file unacceptable, false otherwise.
5226bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5227 bool Complain,
Richard Smith1e2cf0d2014-10-31 02:28:58 +00005228 ASTReaderListener &Listener,
5229 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005230 LangOptions LangOpts;
5231 unsigned Idx = 0;
5232#define LANGOPT(Name, Bits, Default, Description) \
5233 LangOpts.Name = Record[Idx++];
5234#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5235 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5236#include "clang/Basic/LangOptions.def"
Alexey Samsonovedf99a92014-11-07 22:29:38 +00005237#define SANITIZER(NAME, ID) \
5238 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
Will Dietzf54319c2013-01-18 11:30:38 +00005239#include "clang/Basic/Sanitizers.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00005240
Ben Langmuircd98cb72015-06-23 18:20:18 +00005241 for (unsigned N = Record[Idx++]; N; --N)
5242 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5243
Vedant Kumar48b4f762018-04-14 01:40:48 +00005244 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00005245 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5246 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00005247
Ben Langmuird4a667a2015-06-23 18:20:23 +00005248 LangOpts.CurrentModule = ReadString(Record, Idx);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00005249
5250 // Comment options.
5251 for (unsigned N = Record[Idx++]; N; --N) {
5252 LangOpts.CommentOpts.BlockCommandNames.push_back(
5253 ReadString(Record, Idx));
5254 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00005255 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00005256
Samuel Antaoee8fb302016-01-06 13:42:12 +00005257 // OpenMP offloading options.
5258 for (unsigned N = Record[Idx++]; N; --N) {
5259 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5260 }
5261
5262 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5263
Richard Smith1e2cf0d2014-10-31 02:28:58 +00005264 return Listener.ReadLanguageOptions(LangOpts, Complain,
5265 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00005266}
5267
Chandler Carruth0d745bc2015-03-14 04:47:43 +00005268bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5269 ASTReaderListener &Listener,
5270 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005271 unsigned Idx = 0;
5272 TargetOptions TargetOpts;
5273 TargetOpts.Triple = ReadString(Record, Idx);
5274 TargetOpts.CPU = ReadString(Record, Idx);
5275 TargetOpts.ABI = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005276 for (unsigned N = Record[Idx++]; N; --N) {
5277 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5278 }
5279 for (unsigned N = Record[Idx++]; N; --N) {
5280 TargetOpts.Features.push_back(ReadString(Record, Idx));
5281 }
5282
Chandler Carruth0d745bc2015-03-14 04:47:43 +00005283 return Listener.ReadTargetOptions(TargetOpts, Complain,
5284 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00005285}
5286
5287bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5288 ASTReaderListener &Listener) {
Ben Langmuirb92de022014-04-29 16:25:26 +00005289 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
Guy Benyei11169dd2012-12-18 14:30:41 +00005290 unsigned Idx = 0;
Ben Langmuirb92de022014-04-29 16:25:26 +00005291#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00005292#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
Ben Langmuirb92de022014-04-29 16:25:26 +00005293 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00005294#include "clang/Basic/DiagnosticOptions.def"
5295
Richard Smith3be1cb22014-08-07 00:24:21 +00005296 for (unsigned N = Record[Idx++]; N; --N)
Ben Langmuirb92de022014-04-29 16:25:26 +00005297 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
Richard Smith3be1cb22014-08-07 00:24:21 +00005298 for (unsigned N = Record[Idx++]; N; --N)
5299 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005300
5301 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5302}
5303
5304bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5305 ASTReaderListener &Listener) {
5306 FileSystemOptions FSOpts;
5307 unsigned Idx = 0;
5308 FSOpts.WorkingDir = ReadString(Record, Idx);
5309 return Listener.ReadFileSystemOptions(FSOpts, Complain);
5310}
5311
5312bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5313 bool Complain,
5314 ASTReaderListener &Listener) {
5315 HeaderSearchOptions HSOpts;
5316 unsigned Idx = 0;
5317 HSOpts.Sysroot = ReadString(Record, Idx);
5318
5319 // Include entries.
5320 for (unsigned N = Record[Idx++]; N; --N) {
5321 std::string Path = ReadString(Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00005322 frontend::IncludeDirGroup Group
5323 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00005324 bool IsFramework = Record[Idx++];
5325 bool IgnoreSysRoot = Record[Idx++];
Benjamin Kramer3204b152015-05-29 19:42:19 +00005326 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5327 IgnoreSysRoot);
Guy Benyei11169dd2012-12-18 14:30:41 +00005328 }
5329
5330 // System header prefixes.
5331 for (unsigned N = Record[Idx++]; N; --N) {
5332 std::string Prefix = ReadString(Record, Idx);
5333 bool IsSystemHeader = Record[Idx++];
Benjamin Kramer3204b152015-05-29 19:42:19 +00005334 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
Guy Benyei11169dd2012-12-18 14:30:41 +00005335 }
5336
5337 HSOpts.ResourceDir = ReadString(Record, Idx);
5338 HSOpts.ModuleCachePath = ReadString(Record, Idx);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00005339 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005340 HSOpts.DisableModuleHash = Record[Idx++];
Richard Smith18934752017-06-06 00:32:01 +00005341 HSOpts.ImplicitModuleMaps = Record[Idx++];
5342 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00005343 HSOpts.UseBuiltinIncludes = Record[Idx++];
5344 HSOpts.UseStandardSystemIncludes = Record[Idx++];
5345 HSOpts.UseStandardCXXIncludes = Record[Idx++];
5346 HSOpts.UseLibcxx = Record[Idx++];
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005347 std::string SpecificModuleCachePath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005348
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005349 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5350 Complain);
Guy Benyei11169dd2012-12-18 14:30:41 +00005351}
5352
5353bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5354 bool Complain,
5355 ASTReaderListener &Listener,
5356 std::string &SuggestedPredefines) {
5357 PreprocessorOptions PPOpts;
5358 unsigned Idx = 0;
5359
5360 // Macro definitions/undefs
5361 for (unsigned N = Record[Idx++]; N; --N) {
5362 std::string Macro = ReadString(Record, Idx);
5363 bool IsUndef = Record[Idx++];
5364 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5365 }
5366
5367 // Includes
5368 for (unsigned N = Record[Idx++]; N; --N) {
5369 PPOpts.Includes.push_back(ReadString(Record, Idx));
5370 }
5371
5372 // Macro Includes
5373 for (unsigned N = Record[Idx++]; N; --N) {
5374 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5375 }
5376
5377 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00005378 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00005379 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5380 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
5381 PPOpts.ObjCXXARCStandardLibrary =
5382 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5383 SuggestedPredefines.clear();
5384 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5385 SuggestedPredefines);
5386}
5387
5388std::pair<ModuleFile *, unsigned>
5389ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5390 GlobalPreprocessedEntityMapType::iterator
5391 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005392 assert(I != GlobalPreprocessedEntityMap.end() &&
Guy Benyei11169dd2012-12-18 14:30:41 +00005393 "Corrupted global preprocessed entity map");
5394 ModuleFile *M = I->second;
5395 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5396 return std::make_pair(M, LocalIndex);
5397}
5398
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00005399llvm::iterator_range<PreprocessingRecord::iterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00005400ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5401 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5402 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5403 Mod.NumPreprocessedEntities);
5404
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00005405 return llvm::make_range(PreprocessingRecord::iterator(),
5406 PreprocessingRecord::iterator());
Guy Benyei11169dd2012-12-18 14:30:41 +00005407}
5408
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00005409llvm::iterator_range<ASTReader::ModuleDeclIterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00005410ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00005411 return llvm::make_range(
5412 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5413 ModuleDeclIterator(this, &Mod,
5414 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
Guy Benyei11169dd2012-12-18 14:30:41 +00005415}
5416
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00005417SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5418 auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5419 assert(I != GlobalSkippedRangeMap.end() &&
5420 "Corrupted global skipped range map");
5421 ModuleFile *M = I->second;
5422 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5423 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5424 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5425 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5426 TranslateSourceLocation(*M, RawRange.getEnd()));
5427 assert(Range.isValid());
5428 return Range;
5429}
5430
Guy Benyei11169dd2012-12-18 14:30:41 +00005431PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5432 PreprocessedEntityID PPID = Index+1;
5433 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5434 ModuleFile &M = *PPInfo.first;
5435 unsigned LocalIndex = PPInfo.second;
5436 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5437
Guy Benyei11169dd2012-12-18 14:30:41 +00005438 if (!PP.getPreprocessingRecord()) {
5439 Error("no preprocessing record");
Craig Toppera13603a2014-05-22 05:54:18 +00005440 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005441 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005442
5443 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005444 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
5445
5446 llvm::BitstreamEntry Entry =
5447 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5448 if (Entry.Kind != llvm::BitstreamEntry::Record)
Craig Toppera13603a2014-05-22 05:54:18 +00005449 return nullptr;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005450
Guy Benyei11169dd2012-12-18 14:30:41 +00005451 // Read the record.
Richard Smithcb34bd32016-03-27 07:28:06 +00005452 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5453 TranslateSourceLocation(M, PPOffs.getEnd()));
Guy Benyei11169dd2012-12-18 14:30:41 +00005454 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005455 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00005456 RecordData Record;
Vedant Kumar48b4f762018-04-14 01:40:48 +00005457 PreprocessorDetailRecordTypes RecType =
5458 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
5459 Entry.ID, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00005460 switch (RecType) {
5461 case PPD_MACRO_EXPANSION: {
5462 bool isBuiltin = Record[0];
Craig Toppera13603a2014-05-22 05:54:18 +00005463 IdentifierInfo *Name = nullptr;
Richard Smith66a81862015-05-04 02:25:31 +00005464 MacroDefinitionRecord *Def = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005465 if (isBuiltin)
5466 Name = getLocalIdentifier(M, Record[1]);
5467 else {
Richard Smith66a81862015-05-04 02:25:31 +00005468 PreprocessedEntityID GlobalID =
5469 getGlobalPreprocessedEntityID(M, Record[1]);
5470 Def = cast<MacroDefinitionRecord>(
5471 PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
Guy Benyei11169dd2012-12-18 14:30:41 +00005472 }
5473
5474 MacroExpansion *ME;
5475 if (isBuiltin)
5476 ME = new (PPRec) MacroExpansion(Name, Range);
5477 else
5478 ME = new (PPRec) MacroExpansion(Def, Range);
5479
5480 return ME;
5481 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005482
Guy Benyei11169dd2012-12-18 14:30:41 +00005483 case PPD_MACRO_DEFINITION: {
5484 // Decode the identifier info and then check again; if the macro is
5485 // still defined and associated with the identifier,
5486 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Vedant Kumar48b4f762018-04-14 01:40:48 +00005487 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00005488
5489 if (DeserializationListener)
5490 DeserializationListener->MacroDefinitionRead(PPID, MD);
5491
5492 return MD;
5493 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005494
Guy Benyei11169dd2012-12-18 14:30:41 +00005495 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00005496 const char *FullFileNameStart = Blob.data() + Record[0];
5497 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Craig Toppera13603a2014-05-22 05:54:18 +00005498 const FileEntry *File = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005499 if (!FullFileName.empty())
5500 File = PP.getFileManager().getFile(FullFileName);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005501
Guy Benyei11169dd2012-12-18 14:30:41 +00005502 // FIXME: Stable encoding
Vedant Kumar48b4f762018-04-14 01:40:48 +00005503 InclusionDirective::InclusionKind Kind
5504 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
5505 InclusionDirective *ID
5506 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e6c9402013-01-20 02:38:54 +00005507 StringRef(Blob.data(), Record[0]),
Guy Benyei11169dd2012-12-18 14:30:41 +00005508 Record[1], Record[3],
5509 File,
5510 Range);
5511 return ID;
5512 }
5513 }
5514
5515 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
5516}
5517
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005518/// Find the next module that contains entities and return the ID
Guy Benyei11169dd2012-12-18 14:30:41 +00005519/// of the first entry.
NAKAMURA Takumi12ab07e2017-10-12 09:42:14 +00005520///
5521/// \param SLocMapI points at a chunk of a module that contains no
5522/// preprocessed entities or the entities it contains are not the ones we are
5523/// looking for.
Guy Benyei11169dd2012-12-18 14:30:41 +00005524PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
5525 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
5526 ++SLocMapI;
5527 for (GlobalSLocOffsetMapType::const_iterator
5528 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
5529 ModuleFile &M = *SLocMapI->second;
5530 if (M.NumPreprocessedEntities)
5531 return M.BasePreprocessedEntityID;
5532 }
5533
5534 return getTotalNumPreprocessedEntities();
5535}
5536
5537namespace {
5538
Guy Benyei11169dd2012-12-18 14:30:41 +00005539struct PPEntityComp {
5540 const ASTReader &Reader;
5541 ModuleFile &M;
5542
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005543 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
Guy Benyei11169dd2012-12-18 14:30:41 +00005544
5545 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
5546 SourceLocation LHS = getLoc(L);
5547 SourceLocation RHS = getLoc(R);
5548 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5549 }
5550
5551 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
5552 SourceLocation LHS = getLoc(L);
5553 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5554 }
5555
5556 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
5557 SourceLocation RHS = getLoc(R);
5558 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5559 }
5560
5561 SourceLocation getLoc(const PPEntityOffset &PPE) const {
Richard Smithb22a1d12016-03-27 20:13:24 +00005562 return Reader.TranslateSourceLocation(M, PPE.getBegin());
Guy Benyei11169dd2012-12-18 14:30:41 +00005563 }
5564};
5565
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005566} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00005567
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005568PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
5569 bool EndsAfter) const {
5570 if (SourceMgr.isLocalSourceLocation(Loc))
Guy Benyei11169dd2012-12-18 14:30:41 +00005571 return getTotalNumPreprocessedEntities();
5572
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005573 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
5574 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00005575 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
5576 "Corrupted global sloc offset map");
5577
5578 if (SLocMapI->second->NumPreprocessedEntities == 0)
5579 return findNextPreprocessedEntity(SLocMapI);
5580
5581 ModuleFile &M = *SLocMapI->second;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005582
5583 using pp_iterator = const PPEntityOffset *;
5584
Guy Benyei11169dd2012-12-18 14:30:41 +00005585 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
5586 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
5587
5588 size_t Count = M.NumPreprocessedEntities;
5589 size_t Half;
5590 pp_iterator First = pp_begin;
5591 pp_iterator PPI;
5592
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005593 if (EndsAfter) {
5594 PPI = std::upper_bound(pp_begin, pp_end, Loc,
Richard Smithb22a1d12016-03-27 20:13:24 +00005595 PPEntityComp(*this, M));
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005596 } else {
5597 // Do a binary search manually instead of using std::lower_bound because
5598 // The end locations of entities may be unordered (when a macro expansion
5599 // is inside another macro argument), but for this case it is not important
5600 // whether we get the first macro expansion or its containing macro.
5601 while (Count > 0) {
5602 Half = Count / 2;
5603 PPI = First;
5604 std::advance(PPI, Half);
Richard Smithb22a1d12016-03-27 20:13:24 +00005605 if (SourceMgr.isBeforeInTranslationUnit(
5606 TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005607 First = PPI;
5608 ++First;
5609 Count = Count - Half - 1;
5610 } else
5611 Count = Half;
5612 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005613 }
5614
5615 if (PPI == pp_end)
5616 return findNextPreprocessedEntity(SLocMapI);
5617
5618 return M.BasePreprocessedEntityID + (PPI - pp_begin);
5619}
5620
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005621/// Returns a pair of [Begin, End) indices of preallocated
Guy Benyei11169dd2012-12-18 14:30:41 +00005622/// preprocessed entities that \arg Range encompasses.
5623std::pair<unsigned, unsigned>
5624 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
5625 if (Range.isInvalid())
5626 return std::make_pair(0,0);
5627 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
5628
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005629 PreprocessedEntityID BeginID =
5630 findPreprocessedEntity(Range.getBegin(), false);
5631 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
Guy Benyei11169dd2012-12-18 14:30:41 +00005632 return std::make_pair(BeginID, EndID);
5633}
5634
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005635/// Optionally returns true or false if the preallocated preprocessed
Guy Benyei11169dd2012-12-18 14:30:41 +00005636/// entity with index \arg Index came from file \arg FID.
David Blaikie05785d12013-02-20 22:23:23 +00005637Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei11169dd2012-12-18 14:30:41 +00005638 FileID FID) {
5639 if (FID.isInvalid())
5640 return false;
5641
5642 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5643 ModuleFile &M = *PPInfo.first;
5644 unsigned LocalIndex = PPInfo.second;
5645 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005646
Richard Smithcb34bd32016-03-27 07:28:06 +00005647 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
Guy Benyei11169dd2012-12-18 14:30:41 +00005648 if (Loc.isInvalid())
5649 return false;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005650
Guy Benyei11169dd2012-12-18 14:30:41 +00005651 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
5652 return true;
5653 else
5654 return false;
5655}
5656
5657namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005658
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005659 /// Visitor used to search for information about a header file.
Guy Benyei11169dd2012-12-18 14:30:41 +00005660 class HeaderFileInfoVisitor {
Guy Benyei11169dd2012-12-18 14:30:41 +00005661 const FileEntry *FE;
David Blaikie05785d12013-02-20 22:23:23 +00005662 Optional<HeaderFileInfo> HFI;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005663
Guy Benyei11169dd2012-12-18 14:30:41 +00005664 public:
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005665 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00005666
5667 bool operator()(ModuleFile &M) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00005668 HeaderFileInfoLookupTable *Table
5669 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
Guy Benyei11169dd2012-12-18 14:30:41 +00005670 if (!Table)
5671 return false;
5672
5673 // Look in the on-disk hash table for an entry for this file name.
Richard Smithbdf2d932015-07-30 03:37:16 +00005674 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00005675 if (Pos == Table->end())
5676 return false;
5677
Richard Smithbdf2d932015-07-30 03:37:16 +00005678 HFI = *Pos;
Guy Benyei11169dd2012-12-18 14:30:41 +00005679 return true;
5680 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005681
David Blaikie05785d12013-02-20 22:23:23 +00005682 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei11169dd2012-12-18 14:30:41 +00005683 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005684
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005685} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00005686
5687HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00005688 HeaderFileInfoVisitor Visitor(FE);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00005689 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis1054bbf2013-05-08 23:46:55 +00005690 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei11169dd2012-12-18 14:30:41 +00005691 return *HFI;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005692
Guy Benyei11169dd2012-12-18 14:30:41 +00005693 return HeaderFileInfo();
5694}
5695
5696void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Richard Smithd230de22017-01-26 01:01:01 +00005697 using DiagState = DiagnosticsEngine::DiagState;
5698 SmallVector<DiagState *, 32> DiagStates;
5699
Vedant Kumar48b4f762018-04-14 01:40:48 +00005700 for (ModuleFile &F : ModuleMgr) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005701 unsigned Idx = 0;
Richard Smithd230de22017-01-26 01:01:01 +00005702 auto &Record = F.PragmaDiagMappings;
5703 if (Record.empty())
5704 continue;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005705
Richard Smithd230de22017-01-26 01:01:01 +00005706 DiagStates.clear();
5707
5708 auto ReadDiagState =
5709 [&](const DiagState &BasedOn, SourceLocation Loc,
5710 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
5711 unsigned BackrefID = Record[Idx++];
5712 if (BackrefID != 0)
5713 return DiagStates[BackrefID - 1];
5714
Guy Benyei11169dd2012-12-18 14:30:41 +00005715 // A new DiagState was created here.
Richard Smithd230de22017-01-26 01:01:01 +00005716 Diag.DiagStates.push_back(BasedOn);
5717 DiagState *NewState = &Diag.DiagStates.back();
Guy Benyei11169dd2012-12-18 14:30:41 +00005718 DiagStates.push_back(NewState);
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00005719 unsigned Size = Record[Idx++];
5720 assert(Idx + Size * 2 <= Record.size() &&
5721 "Invalid data, not enough diag/map pairs");
5722 while (Size--) {
Richard Smithd230de22017-01-26 01:01:01 +00005723 unsigned DiagID = Record[Idx++];
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00005724 DiagnosticMapping NewMapping =
Richard Smithe37391c2017-05-03 00:28:49 +00005725 DiagnosticMapping::deserialize(Record[Idx++]);
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00005726 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
5727 continue;
5728
5729 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
5730
5731 // If this mapping was specified as a warning but the severity was
5732 // upgraded due to diagnostic settings, simulate the current diagnostic
5733 // settings (and use a warning).
Richard Smithe37391c2017-05-03 00:28:49 +00005734 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
5735 NewMapping.setSeverity(diag::Severity::Warning);
5736 NewMapping.setUpgradedFromWarning(false);
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00005737 }
5738
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00005739 Mapping = NewMapping;
Richard Smithd230de22017-01-26 01:01:01 +00005740 }
Richard Smithd230de22017-01-26 01:01:01 +00005741 return NewState;
5742 };
5743
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00005744 // Read the first state.
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00005745 DiagState *FirstState;
5746 if (F.Kind == MK_ImplicitModule) {
5747 // Implicitly-built modules are reused with different diagnostic
5748 // settings. Use the initial diagnostic state from Diag to simulate this
5749 // compilation's diagnostic settings.
5750 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
5751 DiagStates.push_back(FirstState);
5752
5753 // Skip the initial diagnostic state from the serialized module.
Richard Smithe37391c2017-05-03 00:28:49 +00005754 assert(Record[1] == 0 &&
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00005755 "Invalid data, unexpected backref in initial state");
Richard Smithe37391c2017-05-03 00:28:49 +00005756 Idx = 3 + Record[2] * 2;
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00005757 assert(Idx < Record.size() &&
5758 "Invalid data, not enough state change pairs in initial state");
Richard Smithe37391c2017-05-03 00:28:49 +00005759 } else if (F.isModule()) {
5760 // For an explicit module, preserve the flags from the module build
5761 // command line (-w, -Weverything, -Werror, ...) along with any explicit
5762 // -Wblah flags.
5763 unsigned Flags = Record[Idx++];
5764 DiagState Initial;
5765 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
5766 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
5767 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
5768 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
5769 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
5770 Initial.ExtBehavior = (diag::Severity)Flags;
5771 FirstState = ReadDiagState(Initial, SourceLocation(), true);
Richard Smithea741482017-05-01 22:10:47 +00005772
Richard Smith6c2b5a82018-02-09 01:15:13 +00005773 assert(F.OriginalSourceFileID.isValid());
5774
Richard Smithe37391c2017-05-03 00:28:49 +00005775 // Set up the root buffer of the module to start with the initial
5776 // diagnostic state of the module itself, to cover files that contain no
5777 // explicit transitions (for which we did not serialize anything).
5778 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
5779 .StateTransitions.push_back({FirstState, 0});
5780 } else {
5781 // For prefix ASTs, start with whatever the user configured on the
5782 // command line.
5783 Idx++; // Skip flags.
5784 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
5785 SourceLocation(), false);
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00005786 }
Richard Smithd230de22017-01-26 01:01:01 +00005787
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00005788 // Read the state transitions.
5789 unsigned NumLocations = Record[Idx++];
5790 while (NumLocations--) {
5791 assert(Idx < Record.size() &&
5792 "Invalid data, missing pragma diagnostic states");
Richard Smithd230de22017-01-26 01:01:01 +00005793 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
5794 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
Richard Smith6c2b5a82018-02-09 01:15:13 +00005795 assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
Richard Smithd230de22017-01-26 01:01:01 +00005796 assert(IDAndOffset.second == 0 && "not a start location for a FileID");
5797 unsigned Transitions = Record[Idx++];
5798
5799 // Note that we don't need to set up Parent/ParentOffset here, because
5800 // we won't be changing the diagnostic state within imported FileIDs
5801 // (other than perhaps appending to the main source file, which has no
5802 // parent).
5803 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
5804 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
5805 for (unsigned I = 0; I != Transitions; ++I) {
5806 unsigned Offset = Record[Idx++];
5807 auto *State =
5808 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
5809 F.StateTransitions.push_back({State, Offset});
Guy Benyei11169dd2012-12-18 14:30:41 +00005810 }
5811 }
Richard Smithd230de22017-01-26 01:01:01 +00005812
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00005813 // Read the final state.
5814 assert(Idx < Record.size() &&
5815 "Invalid data, missing final pragma diagnostic state");
5816 SourceLocation CurStateLoc =
5817 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
5818 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
5819
5820 if (!F.isModule()) {
5821 Diag.DiagStatesByLoc.CurDiagState = CurState;
5822 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
5823
5824 // Preserve the property that the imaginary root file describes the
5825 // current state.
Simon Pilgrim22518632017-10-10 13:56:17 +00005826 FileID NullFile;
5827 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00005828 if (T.empty())
5829 T.push_back({CurState, 0});
5830 else
5831 T[0].State = CurState;
5832 }
5833
Richard Smithd230de22017-01-26 01:01:01 +00005834 // Don't try to read these mappings again.
5835 Record.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00005836 }
5837}
5838
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005839/// Get the correct cursor and offset for loading a type.
Guy Benyei11169dd2012-12-18 14:30:41 +00005840ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
5841 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
5842 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
5843 ModuleFile *M = I->second;
5844 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
5845}
5846
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005847/// Read and return the type with the given index..
Guy Benyei11169dd2012-12-18 14:30:41 +00005848///
5849/// The index is the type ID, shifted and minus the number of predefs. This
5850/// routine actually reads the record corresponding to the type at the given
5851/// location. It is a helper routine for GetType, which deals with reading type
5852/// IDs.
5853QualType ASTReader::readTypeRecord(unsigned Index) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00005854 assert(ContextObj && "reading type with no AST context");
5855 ASTContext &Context = *ContextObj;
Guy Benyei11169dd2012-12-18 14:30:41 +00005856 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005857 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00005858
5859 // Keep track of where we are in the stream, then jump back there
5860 // after reading this type.
5861 SavedStreamPosition SavedPosition(DeclsCursor);
5862
5863 ReadingKindTracker ReadingKind(Read_Type, *this);
5864
5865 // Note that we are loading a type record.
5866 Deserializing AType(this);
5867
5868 unsigned Idx = 0;
5869 DeclsCursor.JumpToBit(Loc.Offset);
5870 RecordData Record;
5871 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005872 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005873 case TYPE_EXT_QUAL: {
5874 if (Record.size() != 2) {
5875 Error("Incorrect encoding of extended qualifier type");
Vedant Kumar48b4f762018-04-14 01:40:48 +00005876 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00005877 }
5878 QualType Base = readType(*Loc.F, Record, Idx);
5879 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
5880 return Context.getQualifiedType(Base, Quals);
5881 }
5882
5883 case TYPE_COMPLEX: {
5884 if (Record.size() != 1) {
5885 Error("Incorrect encoding of complex type");
Vedant Kumar48b4f762018-04-14 01:40:48 +00005886 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00005887 }
5888 QualType ElemType = readType(*Loc.F, Record, Idx);
5889 return Context.getComplexType(ElemType);
5890 }
5891
5892 case TYPE_POINTER: {
5893 if (Record.size() != 1) {
5894 Error("Incorrect encoding of pointer type");
Vedant Kumar48b4f762018-04-14 01:40:48 +00005895 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00005896 }
5897 QualType PointeeType = readType(*Loc.F, Record, Idx);
5898 return Context.getPointerType(PointeeType);
5899 }
5900
Reid Kleckner8a365022013-06-24 17:51:48 +00005901 case TYPE_DECAYED: {
5902 if (Record.size() != 1) {
5903 Error("Incorrect encoding of decayed type");
Vedant Kumar48b4f762018-04-14 01:40:48 +00005904 return QualType();
Reid Kleckner8a365022013-06-24 17:51:48 +00005905 }
5906 QualType OriginalType = readType(*Loc.F, Record, Idx);
5907 QualType DT = Context.getAdjustedParameterType(OriginalType);
5908 if (!isa<DecayedType>(DT))
5909 Error("Decayed type does not decay");
5910 return DT;
5911 }
5912
Reid Kleckner0503a872013-12-05 01:23:43 +00005913 case TYPE_ADJUSTED: {
5914 if (Record.size() != 2) {
5915 Error("Incorrect encoding of adjusted type");
Vedant Kumar48b4f762018-04-14 01:40:48 +00005916 return QualType();
Reid Kleckner0503a872013-12-05 01:23:43 +00005917 }
5918 QualType OriginalTy = readType(*Loc.F, Record, Idx);
5919 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
5920 return Context.getAdjustedType(OriginalTy, AdjustedTy);
5921 }
5922
Guy Benyei11169dd2012-12-18 14:30:41 +00005923 case TYPE_BLOCK_POINTER: {
5924 if (Record.size() != 1) {
5925 Error("Incorrect encoding of block pointer type");
Vedant Kumar48b4f762018-04-14 01:40:48 +00005926 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00005927 }
5928 QualType PointeeType = readType(*Loc.F, Record, Idx);
5929 return Context.getBlockPointerType(PointeeType);
5930 }
5931
5932 case TYPE_LVALUE_REFERENCE: {
5933 if (Record.size() != 2) {
5934 Error("Incorrect encoding of lvalue reference type");
Vedant Kumar48b4f762018-04-14 01:40:48 +00005935 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00005936 }
5937 QualType PointeeType = readType(*Loc.F, Record, Idx);
5938 return Context.getLValueReferenceType(PointeeType, Record[1]);
5939 }
5940
5941 case TYPE_RVALUE_REFERENCE: {
5942 if (Record.size() != 1) {
5943 Error("Incorrect encoding of rvalue reference type");
Vedant Kumar48b4f762018-04-14 01:40:48 +00005944 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00005945 }
5946 QualType PointeeType = readType(*Loc.F, Record, Idx);
5947 return Context.getRValueReferenceType(PointeeType);
5948 }
5949
5950 case TYPE_MEMBER_POINTER: {
5951 if (Record.size() != 2) {
5952 Error("Incorrect encoding of member pointer type");
Vedant Kumar48b4f762018-04-14 01:40:48 +00005953 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00005954 }
5955 QualType PointeeType = readType(*Loc.F, Record, Idx);
5956 QualType ClassType = readType(*Loc.F, Record, Idx);
5957 if (PointeeType.isNull() || ClassType.isNull())
Vedant Kumar48b4f762018-04-14 01:40:48 +00005958 return QualType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005959
Guy Benyei11169dd2012-12-18 14:30:41 +00005960 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5961 }
5962
5963 case TYPE_CONSTANT_ARRAY: {
5964 QualType ElementType = readType(*Loc.F, Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00005965 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00005966 unsigned IndexTypeQuals = Record[2];
5967 unsigned Idx = 3;
5968 llvm::APInt Size = ReadAPInt(Record, Idx);
5969 return Context.getConstantArrayType(ElementType, Size,
5970 ASM, IndexTypeQuals);
5971 }
5972
5973 case TYPE_INCOMPLETE_ARRAY: {
5974 QualType ElementType = readType(*Loc.F, Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00005975 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00005976 unsigned IndexTypeQuals = Record[2];
5977 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
5978 }
5979
5980 case TYPE_VARIABLE_ARRAY: {
5981 QualType ElementType = readType(*Loc.F, Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00005982 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00005983 unsigned IndexTypeQuals = Record[2];
5984 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
5985 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
5986 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
5987 ASM, IndexTypeQuals,
5988 SourceRange(LBLoc, RBLoc));
5989 }
5990
5991 case TYPE_VECTOR: {
5992 if (Record.size() != 3) {
5993 Error("incorrect encoding of vector type in AST file");
Vedant Kumar48b4f762018-04-14 01:40:48 +00005994 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00005995 }
5996
5997 QualType ElementType = readType(*Loc.F, Record, Idx);
5998 unsigned NumElements = Record[1];
5999 unsigned VecKind = Record[2];
6000 return Context.getVectorType(ElementType, NumElements,
6001 (VectorType::VectorKind)VecKind);
6002 }
6003
6004 case TYPE_EXT_VECTOR: {
6005 if (Record.size() != 3) {
6006 Error("incorrect encoding of extended vector type in AST file");
Vedant Kumar48b4f762018-04-14 01:40:48 +00006007 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006008 }
6009
6010 QualType ElementType = readType(*Loc.F, Record, Idx);
6011 unsigned NumElements = Record[1];
6012 return Context.getExtVectorType(ElementType, NumElements);
6013 }
6014
6015 case TYPE_FUNCTION_NO_PROTO: {
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006016 if (Record.size() != 8) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006017 Error("incorrect encoding of no-proto function type");
Vedant Kumar48b4f762018-04-14 01:40:48 +00006018 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006019 }
6020 QualType ResultType = readType(*Loc.F, Record, Idx);
6021 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006022 (CallingConv)Record[4], Record[5], Record[6],
6023 Record[7]);
Guy Benyei11169dd2012-12-18 14:30:41 +00006024 return Context.getFunctionNoProtoType(ResultType, Info);
6025 }
6026
6027 case TYPE_FUNCTION_PROTO: {
6028 QualType ResultType = readType(*Loc.F, Record, Idx);
6029
6030 FunctionProtoType::ExtProtoInfo EPI;
6031 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
6032 /*hasregparm*/ Record[2],
6033 /*regparm*/ Record[3],
6034 static_cast<CallingConv>(Record[4]),
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +00006035 /*produces*/ Record[5],
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006036 /*nocallersavedregs*/ Record[6],
6037 /*nocfcheck*/ Record[7]);
Guy Benyei11169dd2012-12-18 14:30:41 +00006038
Oren Ben Simhon220671a2018-03-17 13:31:35 +00006039 unsigned Idx = 8;
Guy Benyei11169dd2012-12-18 14:30:41 +00006040
6041 EPI.Variadic = Record[Idx++];
6042 EPI.HasTrailingReturn = Record[Idx++];
6043 EPI.TypeQuals = Record[Idx++];
6044 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Richard Smith564417a2014-03-20 21:47:22 +00006045 SmallVector<QualType, 8> ExceptionStorage;
Richard Smith8acb4282014-07-31 21:57:55 +00006046 readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
Richard Smith01b2cb42014-07-26 06:37:51 +00006047
6048 unsigned NumParams = Record[Idx++];
6049 SmallVector<QualType, 16> ParamTypes;
6050 for (unsigned I = 0; I != NumParams; ++I)
6051 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
6052
John McCall18afab72016-03-01 00:49:02 +00006053 SmallVector<FunctionProtoType::ExtParameterInfo, 4> ExtParameterInfos;
6054 if (Idx != Record.size()) {
6055 for (unsigned I = 0; I != NumParams; ++I)
6056 ExtParameterInfos.push_back(
6057 FunctionProtoType::ExtParameterInfo
6058 ::getFromOpaqueValue(Record[Idx++]));
6059 EPI.ExtParameterInfos = ExtParameterInfos.data();
6060 }
6061
6062 assert(Idx == Record.size());
6063
Jordan Rose5c382722013-03-08 21:51:21 +00006064 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei11169dd2012-12-18 14:30:41 +00006065 }
6066
6067 case TYPE_UNRESOLVED_USING: {
6068 unsigned Idx = 0;
6069 return Context.getTypeDeclType(
6070 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
6071 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006072
Guy Benyei11169dd2012-12-18 14:30:41 +00006073 case TYPE_TYPEDEF: {
6074 if (Record.size() != 2) {
6075 Error("incorrect encoding of typedef type");
Vedant Kumar48b4f762018-04-14 01:40:48 +00006076 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006077 }
6078 unsigned Idx = 0;
Vedant Kumar48b4f762018-04-14 01:40:48 +00006079 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00006080 QualType Canonical = readType(*Loc.F, Record, Idx);
6081 if (!Canonical.isNull())
6082 Canonical = Context.getCanonicalType(Canonical);
6083 return Context.getTypedefType(Decl, Canonical);
6084 }
6085
6086 case TYPE_TYPEOF_EXPR:
6087 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
6088
6089 case TYPE_TYPEOF: {
6090 if (Record.size() != 1) {
6091 Error("incorrect encoding of typeof(type) in AST file");
Vedant Kumar48b4f762018-04-14 01:40:48 +00006092 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006093 }
6094 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6095 return Context.getTypeOfType(UnderlyingType);
6096 }
6097
6098 case TYPE_DECLTYPE: {
6099 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
6100 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
6101 }
6102
6103 case TYPE_UNARY_TRANSFORM: {
6104 QualType BaseType = readType(*Loc.F, Record, Idx);
6105 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00006106 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Guy Benyei11169dd2012-12-18 14:30:41 +00006107 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
6108 }
6109
Richard Smith74aeef52013-04-26 16:15:35 +00006110 case TYPE_AUTO: {
6111 QualType Deduced = readType(*Loc.F, Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00006112 AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++];
Richard Smith27d807c2013-04-30 13:56:41 +00006113 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
Richard Smithe301ba22015-11-11 02:02:15 +00006114 return Context.getAutoType(Deduced, Keyword, IsDependent);
Richard Smith74aeef52013-04-26 16:15:35 +00006115 }
Guy Benyei11169dd2012-12-18 14:30:41 +00006116
Richard Smith600b5262017-01-26 20:40:47 +00006117 case TYPE_DEDUCED_TEMPLATE_SPECIALIZATION: {
6118 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6119 QualType Deduced = readType(*Loc.F, Record, Idx);
6120 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
6121 return Context.getDeducedTemplateSpecializationType(Name, Deduced,
6122 IsDependent);
6123 }
6124
Guy Benyei11169dd2012-12-18 14:30:41 +00006125 case TYPE_RECORD: {
6126 if (Record.size() != 2) {
6127 Error("incorrect encoding of record type");
Vedant Kumar48b4f762018-04-14 01:40:48 +00006128 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006129 }
6130 unsigned Idx = 0;
6131 bool IsDependent = Record[Idx++];
Vedant Kumar48b4f762018-04-14 01:40:48 +00006132 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00006133 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
6134 QualType T = Context.getRecordType(RD);
6135 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6136 return T;
6137 }
6138
6139 case TYPE_ENUM: {
6140 if (Record.size() != 2) {
6141 Error("incorrect encoding of enum type");
Vedant Kumar48b4f762018-04-14 01:40:48 +00006142 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006143 }
6144 unsigned Idx = 0;
6145 bool IsDependent = Record[Idx++];
6146 QualType T
6147 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
6148 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6149 return T;
6150 }
6151
6152 case TYPE_ATTRIBUTED: {
6153 if (Record.size() != 3) {
6154 Error("incorrect encoding of attributed type");
Vedant Kumar48b4f762018-04-14 01:40:48 +00006155 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006156 }
6157 QualType modifiedType = readType(*Loc.F, Record, Idx);
6158 QualType equivalentType = readType(*Loc.F, Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00006159 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Guy Benyei11169dd2012-12-18 14:30:41 +00006160 return Context.getAttributedType(kind, modifiedType, equivalentType);
6161 }
6162
6163 case TYPE_PAREN: {
6164 if (Record.size() != 1) {
6165 Error("incorrect encoding of paren type");
Vedant Kumar48b4f762018-04-14 01:40:48 +00006166 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006167 }
6168 QualType InnerType = readType(*Loc.F, Record, Idx);
6169 return Context.getParenType(InnerType);
6170 }
6171
6172 case TYPE_PACK_EXPANSION: {
6173 if (Record.size() != 2) {
6174 Error("incorrect encoding of pack expansion type");
Vedant Kumar48b4f762018-04-14 01:40:48 +00006175 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006176 }
6177 QualType Pattern = readType(*Loc.F, Record, Idx);
6178 if (Pattern.isNull())
Vedant Kumar48b4f762018-04-14 01:40:48 +00006179 return QualType();
David Blaikie05785d12013-02-20 22:23:23 +00006180 Optional<unsigned> NumExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00006181 if (Record[1])
6182 NumExpansions = Record[1] - 1;
6183 return Context.getPackExpansionType(Pattern, NumExpansions);
6184 }
6185
6186 case TYPE_ELABORATED: {
6187 unsigned Idx = 0;
Vedant Kumar48b4f762018-04-14 01:40:48 +00006188 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00006189 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
6190 QualType NamedType = readType(*Loc.F, Record, Idx);
Joel E. Denny7509a2f2018-05-14 19:36:45 +00006191 TagDecl *OwnedTagDecl = ReadDeclAs<TagDecl>(*Loc.F, Record, Idx);
6192 return Context.getElaboratedType(Keyword, NNS, NamedType, OwnedTagDecl);
Guy Benyei11169dd2012-12-18 14:30:41 +00006193 }
6194
6195 case TYPE_OBJC_INTERFACE: {
6196 unsigned Idx = 0;
Vedant Kumar48b4f762018-04-14 01:40:48 +00006197 ObjCInterfaceDecl *ItfD
6198 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00006199 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
6200 }
6201
Manman Rene6be26c2016-09-13 17:25:08 +00006202 case TYPE_OBJC_TYPE_PARAM: {
6203 unsigned Idx = 0;
Vedant Kumar48b4f762018-04-14 01:40:48 +00006204 ObjCTypeParamDecl *Decl
6205 = ReadDeclAs<ObjCTypeParamDecl>(*Loc.F, Record, Idx);
Manman Rene6be26c2016-09-13 17:25:08 +00006206 unsigned NumProtos = Record[Idx++];
6207 SmallVector<ObjCProtocolDecl*, 4> Protos;
6208 for (unsigned I = 0; I != NumProtos; ++I)
6209 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
6210 return Context.getObjCTypeParamType(Decl, Protos);
6211 }
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006212
Guy Benyei11169dd2012-12-18 14:30:41 +00006213 case TYPE_OBJC_OBJECT: {
6214 unsigned Idx = 0;
6215 QualType Base = readType(*Loc.F, Record, Idx);
Douglas Gregore9d95f12015-07-07 03:57:35 +00006216 unsigned NumTypeArgs = Record[Idx++];
6217 SmallVector<QualType, 4> TypeArgs;
6218 for (unsigned I = 0; I != NumTypeArgs; ++I)
6219 TypeArgs.push_back(readType(*Loc.F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00006220 unsigned NumProtos = Record[Idx++];
6221 SmallVector<ObjCProtocolDecl*, 4> Protos;
6222 for (unsigned I = 0; I != NumProtos; ++I)
6223 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregorab209d82015-07-07 03:58:42 +00006224 bool IsKindOf = Record[Idx++];
6225 return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf);
Guy Benyei11169dd2012-12-18 14:30:41 +00006226 }
6227
6228 case TYPE_OBJC_OBJECT_POINTER: {
6229 unsigned Idx = 0;
6230 QualType Pointee = readType(*Loc.F, Record, Idx);
6231 return Context.getObjCObjectPointerType(Pointee);
6232 }
6233
6234 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
6235 unsigned Idx = 0;
6236 QualType Parm = readType(*Loc.F, Record, Idx);
6237 QualType Replacement = readType(*Loc.F, Record, Idx);
Stephan Tolksdorfe96f8b32014-03-15 10:23:27 +00006238 return Context.getSubstTemplateTypeParmType(
6239 cast<TemplateTypeParmType>(Parm),
6240 Context.getCanonicalType(Replacement));
Guy Benyei11169dd2012-12-18 14:30:41 +00006241 }
6242
6243 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
6244 unsigned Idx = 0;
6245 QualType Parm = readType(*Loc.F, Record, Idx);
6246 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
6247 return Context.getSubstTemplateTypeParmPackType(
6248 cast<TemplateTypeParmType>(Parm),
6249 ArgPack);
6250 }
6251
6252 case TYPE_INJECTED_CLASS_NAME: {
Vedant Kumar48b4f762018-04-14 01:40:48 +00006253 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00006254 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
6255 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
6256 // for AST reading, too much interdependencies.
Richard Smith6377f8f2014-10-21 21:15:18 +00006257 const Type *T = nullptr;
6258 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
6259 if (const Type *Existing = DI->getTypeForDecl()) {
6260 T = Existing;
6261 break;
6262 }
6263 }
6264 if (!T) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00006265 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
Richard Smith6377f8f2014-10-21 21:15:18 +00006266 for (auto *DI = D; DI; DI = DI->getPreviousDecl())
6267 DI->setTypeForDecl(T);
6268 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00006269 return QualType(T, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00006270 }
6271
6272 case TYPE_TEMPLATE_TYPE_PARM: {
6273 unsigned Idx = 0;
6274 unsigned Depth = Record[Idx++];
6275 unsigned Index = Record[Idx++];
6276 bool Pack = Record[Idx++];
Vedant Kumar48b4f762018-04-14 01:40:48 +00006277 TemplateTypeParmDecl *D
6278 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00006279 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
6280 }
6281
6282 case TYPE_DEPENDENT_NAME: {
6283 unsigned Idx = 0;
Vedant Kumar48b4f762018-04-14 01:40:48 +00006284 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00006285 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Richard Smithbdf2d932015-07-30 03:37:16 +00006286 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00006287 QualType Canon = readType(*Loc.F, Record, Idx);
6288 if (!Canon.isNull())
6289 Canon = Context.getCanonicalType(Canon);
6290 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
6291 }
6292
6293 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
6294 unsigned Idx = 0;
Vedant Kumar48b4f762018-04-14 01:40:48 +00006295 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00006296 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Richard Smithbdf2d932015-07-30 03:37:16 +00006297 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00006298 unsigned NumArgs = Record[Idx++];
6299 SmallVector<TemplateArgument, 8> Args;
6300 Args.reserve(NumArgs);
6301 while (NumArgs--)
6302 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
6303 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
David Majnemer6fbeee32016-07-07 04:43:07 +00006304 Args);
Guy Benyei11169dd2012-12-18 14:30:41 +00006305 }
6306
6307 case TYPE_DEPENDENT_SIZED_ARRAY: {
6308 unsigned Idx = 0;
6309
6310 // ArrayType
6311 QualType ElementType = readType(*Loc.F, Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00006312 ArrayType::ArraySizeModifier ASM
6313 = (ArrayType::ArraySizeModifier)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00006314 unsigned IndexTypeQuals = Record[Idx++];
6315
6316 // DependentSizedArrayType
6317 Expr *NumElts = ReadExpr(*Loc.F);
6318 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
6319
6320 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
6321 IndexTypeQuals, Brackets);
6322 }
6323
6324 case TYPE_TEMPLATE_SPECIALIZATION: {
6325 unsigned Idx = 0;
6326 bool IsDependent = Record[Idx++];
6327 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
6328 SmallVector<TemplateArgument, 8> Args;
6329 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
6330 QualType Underlying = readType(*Loc.F, Record, Idx);
6331 QualType T;
6332 if (Underlying.isNull())
David Majnemer6fbeee32016-07-07 04:43:07 +00006333 T = Context.getCanonicalTemplateSpecializationType(Name, Args);
Guy Benyei11169dd2012-12-18 14:30:41 +00006334 else
David Majnemer6fbeee32016-07-07 04:43:07 +00006335 T = Context.getTemplateSpecializationType(Name, Args, Underlying);
Guy Benyei11169dd2012-12-18 14:30:41 +00006336 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
6337 return T;
6338 }
6339
6340 case TYPE_ATOMIC: {
6341 if (Record.size() != 1) {
6342 Error("Incorrect encoding of atomic type");
Vedant Kumar48b4f762018-04-14 01:40:48 +00006343 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006344 }
6345 QualType ValueType = readType(*Loc.F, Record, Idx);
6346 return Context.getAtomicType(ValueType);
6347 }
Xiuli Pan9c14e282016-01-09 12:53:17 +00006348
Joey Goulye3c85de2016-12-01 11:30:49 +00006349 case TYPE_PIPE: {
6350 if (Record.size() != 2) {
Xiuli Pan9c14e282016-01-09 12:53:17 +00006351 Error("Incorrect encoding of pipe type");
Vedant Kumar48b4f762018-04-14 01:40:48 +00006352 return QualType();
Xiuli Pan9c14e282016-01-09 12:53:17 +00006353 }
6354
6355 // Reading the pipe element type.
6356 QualType ElementType = readType(*Loc.F, Record, Idx);
Joey Goulye3c85de2016-12-01 11:30:49 +00006357 unsigned ReadOnly = Record[1];
6358 return Context.getPipeType(ElementType, ReadOnly);
Joey Gouly5788b782016-11-18 14:10:54 +00006359 }
6360
Alex Lorenz00aee432017-03-22 10:04:48 +00006361 case TYPE_DEPENDENT_SIZED_EXT_VECTOR: {
6362 unsigned Idx = 0;
6363
6364 // DependentSizedExtVectorType
6365 QualType ElementType = readType(*Loc.F, Record, Idx);
6366 Expr *SizeExpr = ReadExpr(*Loc.F);
6367 SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6368
6369 return Context.getDependentSizedExtVectorType(ElementType, SizeExpr,
6370 AttrLoc);
6371 }
Andrew Gozillon572bbb02017-10-02 06:25:51 +00006372
6373 case TYPE_DEPENDENT_ADDRESS_SPACE: {
6374 unsigned Idx = 0;
6375
6376 // DependentAddressSpaceType
6377 QualType PointeeType = readType(*Loc.F, Record, Idx);
6378 Expr *AddrSpaceExpr = ReadExpr(*Loc.F);
6379 SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx);
6380
6381 return Context.getDependentAddressSpaceType(PointeeType, AddrSpaceExpr,
6382 AttrLoc);
6383 }
Guy Benyei11169dd2012-12-18 14:30:41 +00006384 }
6385 llvm_unreachable("Invalid TypeCode!");
6386}
6387
Richard Smith564417a2014-03-20 21:47:22 +00006388void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
6389 SmallVectorImpl<QualType> &Exceptions,
Richard Smith8acb4282014-07-31 21:57:55 +00006390 FunctionProtoType::ExceptionSpecInfo &ESI,
Richard Smith564417a2014-03-20 21:47:22 +00006391 const RecordData &Record, unsigned &Idx) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00006392 ExceptionSpecificationType EST =
6393 static_cast<ExceptionSpecificationType>(Record[Idx++]);
Richard Smith8acb4282014-07-31 21:57:55 +00006394 ESI.Type = EST;
Richard Smith564417a2014-03-20 21:47:22 +00006395 if (EST == EST_Dynamic) {
Richard Smith8acb4282014-07-31 21:57:55 +00006396 for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
Richard Smith564417a2014-03-20 21:47:22 +00006397 Exceptions.push_back(readType(ModuleFile, Record, Idx));
Richard Smith8acb4282014-07-31 21:57:55 +00006398 ESI.Exceptions = Exceptions;
Richard Smitheaf11ad2018-05-03 03:58:32 +00006399 } else if (isComputedNoexcept(EST)) {
Richard Smith8acb4282014-07-31 21:57:55 +00006400 ESI.NoexceptExpr = ReadExpr(ModuleFile);
Richard Smith564417a2014-03-20 21:47:22 +00006401 } else if (EST == EST_Uninstantiated) {
Richard Smith8acb4282014-07-31 21:57:55 +00006402 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
6403 ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
Richard Smith564417a2014-03-20 21:47:22 +00006404 } else if (EST == EST_Unevaluated) {
Richard Smith8acb4282014-07-31 21:57:55 +00006405 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
Richard Smith564417a2014-03-20 21:47:22 +00006406 }
6407}
6408
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006409namespace clang {
6410
6411class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
David L. Jonesbe1557a2016-12-21 00:17:49 +00006412 ModuleFile *F;
6413 ASTReader *Reader;
6414 const ASTReader::RecordData &Record;
Guy Benyei11169dd2012-12-18 14:30:41 +00006415 unsigned &Idx;
6416
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006417 SourceLocation ReadSourceLocation() {
David L. Jonesbe1557a2016-12-21 00:17:49 +00006418 return Reader->ReadSourceLocation(*F, Record, Idx);
6419 }
6420
6421 TypeSourceInfo *GetTypeSourceInfo() {
6422 return Reader->GetTypeSourceInfo(*F, Record, Idx);
6423 }
6424
6425 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6426 return Reader->ReadNestedNameSpecifierLoc(*F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00006427 }
6428
Guy Benyei11169dd2012-12-18 14:30:41 +00006429public:
David L. Jonesbe1557a2016-12-21 00:17:49 +00006430 TypeLocReader(ModuleFile &F, ASTReader &Reader,
Guy Benyei11169dd2012-12-18 14:30:41 +00006431 const ASTReader::RecordData &Record, unsigned &Idx)
David L. Jonesbe1557a2016-12-21 00:17:49 +00006432 : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {}
Guy Benyei11169dd2012-12-18 14:30:41 +00006433
6434 // We want compile-time assurance that we've enumerated all of
6435 // these, so unfortunately we have to declare them first, then
6436 // define them out-of-line.
6437#define ABSTRACT_TYPELOC(CLASS, PARENT)
6438#define TYPELOC(CLASS, PARENT) \
6439 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6440#include "clang/AST/TypeLocNodes.def"
6441
6442 void VisitFunctionTypeLoc(FunctionTypeLoc);
6443 void VisitArrayTypeLoc(ArrayTypeLoc);
6444};
6445
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006446} // namespace clang
6447
Guy Benyei11169dd2012-12-18 14:30:41 +00006448void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6449 // nothing to do
6450}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006451
Guy Benyei11169dd2012-12-18 14:30:41 +00006452void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006453 TL.setBuiltinLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006454 if (TL.needsExtraLocalData()) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00006455 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
6456 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
6457 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
6458 TL.setModeAttr(Record[Idx++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00006459 }
6460}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006461
Guy Benyei11169dd2012-12-18 14:30:41 +00006462void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006463 TL.setNameLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006464}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006465
Guy Benyei11169dd2012-12-18 14:30:41 +00006466void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006467 TL.setStarLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006468}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006469
Reid Kleckner8a365022013-06-24 17:51:48 +00006470void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6471 // nothing to do
6472}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006473
Reid Kleckner0503a872013-12-05 01:23:43 +00006474void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6475 // nothing to do
6476}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006477
Guy Benyei11169dd2012-12-18 14:30:41 +00006478void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006479 TL.setCaretLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006480}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006481
Guy Benyei11169dd2012-12-18 14:30:41 +00006482void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006483 TL.setAmpLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006484}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006485
Guy Benyei11169dd2012-12-18 14:30:41 +00006486void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006487 TL.setAmpAmpLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006488}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006489
Guy Benyei11169dd2012-12-18 14:30:41 +00006490void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006491 TL.setStarLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006492 TL.setClassTInfo(GetTypeSourceInfo());
Guy Benyei11169dd2012-12-18 14:30:41 +00006493}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006494
Guy Benyei11169dd2012-12-18 14:30:41 +00006495void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006496 TL.setLBracketLoc(ReadSourceLocation());
6497 TL.setRBracketLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006498 if (Record[Idx++])
6499 TL.setSizeExpr(Reader->ReadExpr(*F));
Guy Benyei11169dd2012-12-18 14:30:41 +00006500 else
Craig Toppera13603a2014-05-22 05:54:18 +00006501 TL.setSizeExpr(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006502}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006503
Guy Benyei11169dd2012-12-18 14:30:41 +00006504void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6505 VisitArrayTypeLoc(TL);
6506}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006507
Guy Benyei11169dd2012-12-18 14:30:41 +00006508void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6509 VisitArrayTypeLoc(TL);
6510}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006511
Guy Benyei11169dd2012-12-18 14:30:41 +00006512void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6513 VisitArrayTypeLoc(TL);
6514}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006515
Guy Benyei11169dd2012-12-18 14:30:41 +00006516void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6517 DependentSizedArrayTypeLoc TL) {
6518 VisitArrayTypeLoc(TL);
6519}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006520
Andrew Gozillon572bbb02017-10-02 06:25:51 +00006521void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6522 DependentAddressSpaceTypeLoc TL) {
6523
6524 TL.setAttrNameLoc(ReadSourceLocation());
6525 SourceRange range;
6526 range.setBegin(ReadSourceLocation());
6527 range.setEnd(ReadSourceLocation());
6528 TL.setAttrOperandParensRange(range);
6529 TL.setAttrExprOperand(Reader->ReadExpr(*F));
6530}
6531
Guy Benyei11169dd2012-12-18 14:30:41 +00006532void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6533 DependentSizedExtVectorTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006534 TL.setNameLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006535}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006536
Guy Benyei11169dd2012-12-18 14:30:41 +00006537void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006538 TL.setNameLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006539}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006540
Guy Benyei11169dd2012-12-18 14:30:41 +00006541void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006542 TL.setNameLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006543}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006544
Guy Benyei11169dd2012-12-18 14:30:41 +00006545void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006546 TL.setLocalRangeBegin(ReadSourceLocation());
6547 TL.setLParenLoc(ReadSourceLocation());
6548 TL.setRParenLoc(ReadSourceLocation());
Malcolm Parsonsa3220ce2017-01-12 16:11:28 +00006549 TL.setExceptionSpecRange(SourceRange(Reader->ReadSourceLocation(*F, Record, Idx),
6550 Reader->ReadSourceLocation(*F, Record, Idx)));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006551 TL.setLocalRangeEnd(ReadSourceLocation());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00006552 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00006553 TL.setParam(i, Reader->ReadDeclAs<ParmVarDecl>(*F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00006554 }
6555}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006556
Guy Benyei11169dd2012-12-18 14:30:41 +00006557void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6558 VisitFunctionTypeLoc(TL);
6559}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006560
Guy Benyei11169dd2012-12-18 14:30:41 +00006561void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6562 VisitFunctionTypeLoc(TL);
6563}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006564
Guy Benyei11169dd2012-12-18 14:30:41 +00006565void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006566 TL.setNameLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006567}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006568
Guy Benyei11169dd2012-12-18 14:30:41 +00006569void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006570 TL.setNameLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006571}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006572
Guy Benyei11169dd2012-12-18 14:30:41 +00006573void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006574 TL.setTypeofLoc(ReadSourceLocation());
6575 TL.setLParenLoc(ReadSourceLocation());
6576 TL.setRParenLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006577}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006578
Guy Benyei11169dd2012-12-18 14:30:41 +00006579void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006580 TL.setTypeofLoc(ReadSourceLocation());
6581 TL.setLParenLoc(ReadSourceLocation());
6582 TL.setRParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006583 TL.setUnderlyingTInfo(GetTypeSourceInfo());
Guy Benyei11169dd2012-12-18 14:30:41 +00006584}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006585
Guy Benyei11169dd2012-12-18 14:30:41 +00006586void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006587 TL.setNameLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006588}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006589
Guy Benyei11169dd2012-12-18 14:30:41 +00006590void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006591 TL.setKWLoc(ReadSourceLocation());
6592 TL.setLParenLoc(ReadSourceLocation());
6593 TL.setRParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006594 TL.setUnderlyingTInfo(GetTypeSourceInfo());
Guy Benyei11169dd2012-12-18 14:30:41 +00006595}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006596
Guy Benyei11169dd2012-12-18 14:30:41 +00006597void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006598 TL.setNameLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006599}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006600
Richard Smith600b5262017-01-26 20:40:47 +00006601void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6602 DeducedTemplateSpecializationTypeLoc TL) {
6603 TL.setTemplateNameLoc(ReadSourceLocation());
6604}
6605
Guy Benyei11169dd2012-12-18 14:30:41 +00006606void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006607 TL.setNameLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006608}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006609
Guy Benyei11169dd2012-12-18 14:30:41 +00006610void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006611 TL.setNameLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006612}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006613
Guy Benyei11169dd2012-12-18 14:30:41 +00006614void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006615 TL.setAttrNameLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006616 if (TL.hasAttrOperand()) {
6617 SourceRange range;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006618 range.setBegin(ReadSourceLocation());
6619 range.setEnd(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006620 TL.setAttrOperandParensRange(range);
6621 }
6622 if (TL.hasAttrExprOperand()) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00006623 if (Record[Idx++])
6624 TL.setAttrExprOperand(Reader->ReadExpr(*F));
Guy Benyei11169dd2012-12-18 14:30:41 +00006625 else
Craig Toppera13603a2014-05-22 05:54:18 +00006626 TL.setAttrExprOperand(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006627 } else if (TL.hasAttrEnumOperand())
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006628 TL.setAttrEnumOperandLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006629}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006630
Guy Benyei11169dd2012-12-18 14:30:41 +00006631void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006632 TL.setNameLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006633}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006634
Guy Benyei11169dd2012-12-18 14:30:41 +00006635void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6636 SubstTemplateTypeParmTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006637 TL.setNameLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006638}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006639
Guy Benyei11169dd2012-12-18 14:30:41 +00006640void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6641 SubstTemplateTypeParmPackTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006642 TL.setNameLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006643}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006644
Guy Benyei11169dd2012-12-18 14:30:41 +00006645void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6646 TemplateSpecializationTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006647 TL.setTemplateKeywordLoc(ReadSourceLocation());
6648 TL.setTemplateNameLoc(ReadSourceLocation());
6649 TL.setLAngleLoc(ReadSourceLocation());
6650 TL.setRAngleLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006651 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
David L. Jonesbe1557a2016-12-21 00:17:49 +00006652 TL.setArgLocInfo(
6653 i,
6654 Reader->GetTemplateArgumentLocInfo(
6655 *F, TL.getTypePtr()->getArg(i).getKind(), Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00006656}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006657
Guy Benyei11169dd2012-12-18 14:30:41 +00006658void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006659 TL.setLParenLoc(ReadSourceLocation());
6660 TL.setRParenLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006661}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006662
Guy Benyei11169dd2012-12-18 14:30:41 +00006663void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006664 TL.setElaboratedKeywordLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006665 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
Guy Benyei11169dd2012-12-18 14:30:41 +00006666}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006667
Guy Benyei11169dd2012-12-18 14:30:41 +00006668void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006669 TL.setNameLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006670}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006671
Guy Benyei11169dd2012-12-18 14:30:41 +00006672void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006673 TL.setElaboratedKeywordLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006674 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006675 TL.setNameLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006676}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006677
Guy Benyei11169dd2012-12-18 14:30:41 +00006678void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6679 DependentTemplateSpecializationTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006680 TL.setElaboratedKeywordLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006681 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006682 TL.setTemplateKeywordLoc(ReadSourceLocation());
6683 TL.setTemplateNameLoc(ReadSourceLocation());
6684 TL.setLAngleLoc(ReadSourceLocation());
6685 TL.setRAngleLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006686 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
David L. Jonesbe1557a2016-12-21 00:17:49 +00006687 TL.setArgLocInfo(
6688 I,
6689 Reader->GetTemplateArgumentLocInfo(
6690 *F, TL.getTypePtr()->getArg(I).getKind(), Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00006691}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006692
Guy Benyei11169dd2012-12-18 14:30:41 +00006693void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006694 TL.setEllipsisLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006695}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006696
Guy Benyei11169dd2012-12-18 14:30:41 +00006697void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006698 TL.setNameLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006699}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006700
Manman Rene6be26c2016-09-13 17:25:08 +00006701void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6702 if (TL.getNumProtocols()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006703 TL.setProtocolLAngleLoc(ReadSourceLocation());
6704 TL.setProtocolRAngleLoc(ReadSourceLocation());
Manman Rene6be26c2016-09-13 17:25:08 +00006705 }
6706 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006707 TL.setProtocolLoc(i, ReadSourceLocation());
Manman Rene6be26c2016-09-13 17:25:08 +00006708}
6709
Guy Benyei11169dd2012-12-18 14:30:41 +00006710void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00006711 TL.setHasBaseTypeAsWritten(Record[Idx++]);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006712 TL.setTypeArgsLAngleLoc(ReadSourceLocation());
6713 TL.setTypeArgsRAngleLoc(ReadSourceLocation());
Douglas Gregore9d95f12015-07-07 03:57:35 +00006714 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
David L. Jonesbe1557a2016-12-21 00:17:49 +00006715 TL.setTypeArgTInfo(i, GetTypeSourceInfo());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006716 TL.setProtocolLAngleLoc(ReadSourceLocation());
6717 TL.setProtocolRAngleLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006718 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006719 TL.setProtocolLoc(i, ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006720}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006721
Guy Benyei11169dd2012-12-18 14:30:41 +00006722void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006723 TL.setStarLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006724}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006725
Guy Benyei11169dd2012-12-18 14:30:41 +00006726void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006727 TL.setKWLoc(ReadSourceLocation());
6728 TL.setLParenLoc(ReadSourceLocation());
6729 TL.setRParenLoc(ReadSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006730}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006731
Xiuli Pan9c14e282016-01-09 12:53:17 +00006732void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006733 TL.setKWLoc(ReadSourceLocation());
Xiuli Pan9c14e282016-01-09 12:53:17 +00006734}
Guy Benyei11169dd2012-12-18 14:30:41 +00006735
David L. Jonesbe1557a2016-12-21 00:17:49 +00006736TypeSourceInfo *
6737ASTReader::GetTypeSourceInfo(ModuleFile &F, const ASTReader::RecordData &Record,
6738 unsigned &Idx) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006739 QualType InfoTy = readType(F, Record, Idx);
6740 if (InfoTy.isNull())
Craig Toppera13603a2014-05-22 05:54:18 +00006741 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006742
6743 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
David L. Jonesbe1557a2016-12-21 00:17:49 +00006744 TypeLocReader TLR(F, *this, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00006745 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
6746 TLR.Visit(TL);
6747 return TInfo;
6748}
6749
6750QualType ASTReader::GetType(TypeID ID) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00006751 assert(ContextObj && "reading type with no AST context");
6752 ASTContext &Context = *ContextObj;
6753
Guy Benyei11169dd2012-12-18 14:30:41 +00006754 unsigned FastQuals = ID & Qualifiers::FastMask;
6755 unsigned Index = ID >> Qualifiers::FastWidth;
6756
6757 if (Index < NUM_PREDEF_TYPE_IDS) {
6758 QualType T;
6759 switch ((PredefinedTypeIDs)Index) {
Alexey Baderbdf7c842015-09-15 12:18:29 +00006760 case PREDEF_TYPE_NULL_ID:
Vedant Kumar48b4f762018-04-14 01:40:48 +00006761 return QualType();
Alexey Baderbdf7c842015-09-15 12:18:29 +00006762 case PREDEF_TYPE_VOID_ID:
6763 T = Context.VoidTy;
6764 break;
6765 case PREDEF_TYPE_BOOL_ID:
6766 T = Context.BoolTy;
6767 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00006768 case PREDEF_TYPE_CHAR_U_ID:
6769 case PREDEF_TYPE_CHAR_S_ID:
6770 // FIXME: Check that the signedness of CharTy is correct!
6771 T = Context.CharTy;
6772 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006773 case PREDEF_TYPE_UCHAR_ID:
6774 T = Context.UnsignedCharTy;
6775 break;
6776 case PREDEF_TYPE_USHORT_ID:
6777 T = Context.UnsignedShortTy;
6778 break;
6779 case PREDEF_TYPE_UINT_ID:
6780 T = Context.UnsignedIntTy;
6781 break;
6782 case PREDEF_TYPE_ULONG_ID:
6783 T = Context.UnsignedLongTy;
6784 break;
6785 case PREDEF_TYPE_ULONGLONG_ID:
6786 T = Context.UnsignedLongLongTy;
6787 break;
6788 case PREDEF_TYPE_UINT128_ID:
6789 T = Context.UnsignedInt128Ty;
6790 break;
6791 case PREDEF_TYPE_SCHAR_ID:
6792 T = Context.SignedCharTy;
6793 break;
6794 case PREDEF_TYPE_WCHAR_ID:
6795 T = Context.WCharTy;
6796 break;
6797 case PREDEF_TYPE_SHORT_ID:
6798 T = Context.ShortTy;
6799 break;
6800 case PREDEF_TYPE_INT_ID:
6801 T = Context.IntTy;
6802 break;
6803 case PREDEF_TYPE_LONG_ID:
6804 T = Context.LongTy;
6805 break;
6806 case PREDEF_TYPE_LONGLONG_ID:
6807 T = Context.LongLongTy;
6808 break;
6809 case PREDEF_TYPE_INT128_ID:
6810 T = Context.Int128Ty;
6811 break;
6812 case PREDEF_TYPE_HALF_ID:
6813 T = Context.HalfTy;
6814 break;
6815 case PREDEF_TYPE_FLOAT_ID:
6816 T = Context.FloatTy;
6817 break;
6818 case PREDEF_TYPE_DOUBLE_ID:
6819 T = Context.DoubleTy;
6820 break;
6821 case PREDEF_TYPE_LONGDOUBLE_ID:
6822 T = Context.LongDoubleTy;
6823 break;
Leonard Chanf921d852018-06-04 16:07:52 +00006824 case PREDEF_TYPE_SHORT_ACCUM_ID:
6825 T = Context.ShortAccumTy;
6826 break;
6827 case PREDEF_TYPE_ACCUM_ID:
6828 T = Context.AccumTy;
6829 break;
6830 case PREDEF_TYPE_LONG_ACCUM_ID:
6831 T = Context.LongAccumTy;
6832 break;
6833 case PREDEF_TYPE_USHORT_ACCUM_ID:
6834 T = Context.UnsignedShortAccumTy;
6835 break;
6836 case PREDEF_TYPE_UACCUM_ID:
6837 T = Context.UnsignedAccumTy;
6838 break;
6839 case PREDEF_TYPE_ULONG_ACCUM_ID:
6840 T = Context.UnsignedLongAccumTy;
6841 break;
Leonard Chanab80f3c2018-06-14 14:53:51 +00006842 case PREDEF_TYPE_SHORT_FRACT_ID:
6843 T = Context.ShortFractTy;
6844 break;
6845 case PREDEF_TYPE_FRACT_ID:
6846 T = Context.FractTy;
6847 break;
6848 case PREDEF_TYPE_LONG_FRACT_ID:
6849 T = Context.LongFractTy;
6850 break;
6851 case PREDEF_TYPE_USHORT_FRACT_ID:
6852 T = Context.UnsignedShortFractTy;
6853 break;
6854 case PREDEF_TYPE_UFRACT_ID:
6855 T = Context.UnsignedFractTy;
6856 break;
6857 case PREDEF_TYPE_ULONG_FRACT_ID:
6858 T = Context.UnsignedLongFractTy;
6859 break;
6860 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6861 T = Context.SatShortAccumTy;
6862 break;
6863 case PREDEF_TYPE_SAT_ACCUM_ID:
6864 T = Context.SatAccumTy;
6865 break;
6866 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6867 T = Context.SatLongAccumTy;
6868 break;
6869 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6870 T = Context.SatUnsignedShortAccumTy;
6871 break;
6872 case PREDEF_TYPE_SAT_UACCUM_ID:
6873 T = Context.SatUnsignedAccumTy;
6874 break;
6875 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6876 T = Context.SatUnsignedLongAccumTy;
6877 break;
6878 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6879 T = Context.SatShortFractTy;
6880 break;
6881 case PREDEF_TYPE_SAT_FRACT_ID:
6882 T = Context.SatFractTy;
6883 break;
6884 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6885 T = Context.SatLongFractTy;
6886 break;
6887 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6888 T = Context.SatUnsignedShortFractTy;
6889 break;
6890 case PREDEF_TYPE_SAT_UFRACT_ID:
6891 T = Context.SatUnsignedFractTy;
6892 break;
6893 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6894 T = Context.SatUnsignedLongFractTy;
6895 break;
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00006896 case PREDEF_TYPE_FLOAT16_ID:
6897 T = Context.Float16Ty;
6898 break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00006899 case PREDEF_TYPE_FLOAT128_ID:
6900 T = Context.Float128Ty;
6901 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006902 case PREDEF_TYPE_OVERLOAD_ID:
6903 T = Context.OverloadTy;
6904 break;
6905 case PREDEF_TYPE_BOUND_MEMBER:
6906 T = Context.BoundMemberTy;
6907 break;
6908 case PREDEF_TYPE_PSEUDO_OBJECT:
6909 T = Context.PseudoObjectTy;
6910 break;
6911 case PREDEF_TYPE_DEPENDENT_ID:
6912 T = Context.DependentTy;
6913 break;
6914 case PREDEF_TYPE_UNKNOWN_ANY:
6915 T = Context.UnknownAnyTy;
6916 break;
6917 case PREDEF_TYPE_NULLPTR_ID:
6918 T = Context.NullPtrTy;
6919 break;
Richard Smith3a8244d2018-05-01 05:02:45 +00006920 case PREDEF_TYPE_CHAR8_ID:
6921 T = Context.Char8Ty;
6922 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006923 case PREDEF_TYPE_CHAR16_ID:
6924 T = Context.Char16Ty;
6925 break;
6926 case PREDEF_TYPE_CHAR32_ID:
6927 T = Context.Char32Ty;
6928 break;
6929 case PREDEF_TYPE_OBJC_ID:
6930 T = Context.ObjCBuiltinIdTy;
6931 break;
6932 case PREDEF_TYPE_OBJC_CLASS:
6933 T = Context.ObjCBuiltinClassTy;
6934 break;
6935 case PREDEF_TYPE_OBJC_SEL:
6936 T = Context.ObjCBuiltinSelTy;
6937 break;
Alexey Bader954ba212016-04-08 13:40:33 +00006938#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6939 case PREDEF_TYPE_##Id##_ID: \
6940 T = Context.SingletonId; \
Alexey Baderbdf7c842015-09-15 12:18:29 +00006941 break;
Alexey Baderb62f1442016-04-13 08:33:41 +00006942#include "clang/Basic/OpenCLImageTypes.def"
Alexey Baderbdf7c842015-09-15 12:18:29 +00006943 case PREDEF_TYPE_SAMPLER_ID:
6944 T = Context.OCLSamplerTy;
6945 break;
6946 case PREDEF_TYPE_EVENT_ID:
6947 T = Context.OCLEventTy;
6948 break;
Alexey Bader9c8453f2015-09-15 11:18:52 +00006949 case PREDEF_TYPE_CLK_EVENT_ID:
6950 T = Context.OCLClkEventTy;
6951 break;
6952 case PREDEF_TYPE_QUEUE_ID:
6953 T = Context.OCLQueueTy;
6954 break;
Alexey Bader9c8453f2015-09-15 11:18:52 +00006955 case PREDEF_TYPE_RESERVE_ID_ID:
6956 T = Context.OCLReserveIDTy;
6957 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006958 case PREDEF_TYPE_AUTO_DEDUCT:
6959 T = Context.getAutoDeductType();
6960 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006961 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
6962 T = Context.getAutoRRefDeductType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006963 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00006964 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
6965 T = Context.ARCUnbridgedCastTy;
6966 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00006967 case PREDEF_TYPE_BUILTIN_FN:
6968 T = Context.BuiltinFnTy;
6969 break;
Alexey Bataev1a3320e2015-08-25 14:24:04 +00006970 case PREDEF_TYPE_OMP_ARRAY_SECTION:
6971 T = Context.OMPArraySectionTy;
6972 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00006973 }
6974
6975 assert(!T.isNull() && "Unknown predefined type");
6976 return T.withFastQualifiers(FastQuals);
6977 }
6978
6979 Index -= NUM_PREDEF_TYPE_IDS;
6980 assert(Index < TypesLoaded.size() && "Type index out-of-range");
6981 if (TypesLoaded[Index].isNull()) {
6982 TypesLoaded[Index] = readTypeRecord(Index);
6983 if (TypesLoaded[Index].isNull())
Vedant Kumar48b4f762018-04-14 01:40:48 +00006984 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006985
6986 TypesLoaded[Index]->setFromAST();
6987 if (DeserializationListener)
6988 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
6989 TypesLoaded[Index]);
6990 }
6991
6992 return TypesLoaded[Index].withFastQualifiers(FastQuals);
6993}
6994
6995QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
6996 return GetType(getGlobalTypeID(F, LocalID));
6997}
6998
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006999serialization::TypeID
Guy Benyei11169dd2012-12-18 14:30:41 +00007000ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7001 unsigned FastQuals = LocalID & Qualifiers::FastMask;
7002 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007003
Guy Benyei11169dd2012-12-18 14:30:41 +00007004 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7005 return LocalID;
7006
Richard Smith37a93df2017-02-18 00:32:02 +00007007 if (!F.ModuleOffsetMap.empty())
7008 ReadModuleOffsetMap(F);
7009
Guy Benyei11169dd2012-12-18 14:30:41 +00007010 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7011 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7012 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007013
Guy Benyei11169dd2012-12-18 14:30:41 +00007014 unsigned GlobalIndex = LocalIndex + I->second;
7015 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7016}
7017
7018TemplateArgumentLocInfo
7019ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
7020 TemplateArgument::ArgKind Kind,
7021 const RecordData &Record,
7022 unsigned &Index) {
7023 switch (Kind) {
7024 case TemplateArgument::Expression:
7025 return ReadExpr(F);
7026 case TemplateArgument::Type:
7027 return GetTypeSourceInfo(F, Record, Index);
7028 case TemplateArgument::Template: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007029 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
Guy Benyei11169dd2012-12-18 14:30:41 +00007030 Index);
7031 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7032 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7033 SourceLocation());
7034 }
7035 case TemplateArgument::TemplateExpansion: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007036 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
Guy Benyei11169dd2012-12-18 14:30:41 +00007037 Index);
7038 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
7039 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007040 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Guy Benyei11169dd2012-12-18 14:30:41 +00007041 EllipsisLoc);
7042 }
7043 case TemplateArgument::Null:
7044 case TemplateArgument::Integral:
7045 case TemplateArgument::Declaration:
7046 case TemplateArgument::NullPtr:
7047 case TemplateArgument::Pack:
7048 // FIXME: Is this right?
Vedant Kumar48b4f762018-04-14 01:40:48 +00007049 return TemplateArgumentLocInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +00007050 }
7051 llvm_unreachable("unexpected template argument loc");
7052}
7053
7054TemplateArgumentLoc
7055ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
7056 const RecordData &Record, unsigned &Index) {
7057 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
7058
7059 if (Arg.getKind() == TemplateArgument::Expression) {
7060 if (Record[Index++]) // bool InfoHasSameExpr.
7061 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7062 }
7063 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
7064 Record, Index));
7065}
7066
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00007067const ASTTemplateArgumentListInfo*
7068ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
7069 const RecordData &Record,
7070 unsigned &Index) {
7071 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
7072 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
7073 unsigned NumArgsAsWritten = Record[Index++];
7074 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7075 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7076 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
7077 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7078}
7079
Guy Benyei11169dd2012-12-18 14:30:41 +00007080Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7081 return GetDecl(ID);
7082}
7083
Richard Smith053f6c62014-05-16 23:01:30 +00007084void ASTReader::CompleteRedeclChain(const Decl *D) {
Richard Smith851072e2014-05-19 20:59:20 +00007085 if (NumCurrentElementsDeserializing) {
7086 // We arrange to not care about the complete redeclaration chain while we're
7087 // deserializing. Just remember that the AST has marked this one as complete
7088 // but that it's not actually complete yet, so we know we still need to
7089 // complete it later.
7090 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7091 return;
7092 }
7093
Richard Smith053f6c62014-05-16 23:01:30 +00007094 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7095
Richard Smith053f6c62014-05-16 23:01:30 +00007096 // If this is a named declaration, complete it by looking it up
7097 // within its context.
7098 //
Richard Smith01bdb7a2014-08-28 05:44:07 +00007099 // FIXME: Merging a function definition should merge
Richard Smith053f6c62014-05-16 23:01:30 +00007100 // all mergeable entities within it.
7101 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7102 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7103 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
Richard Smitha534a312015-07-21 23:54:07 +00007104 if (!getContext().getLangOpts().CPlusPlus &&
7105 isa<TranslationUnitDecl>(DC)) {
Richard Smith053f6c62014-05-16 23:01:30 +00007106 // Outside of C++, we don't have a lookup table for the TU, so update
Richard Smitha534a312015-07-21 23:54:07 +00007107 // the identifier instead. (For C++ modules, we don't store decls
7108 // in the serialized identifier table, so we do the lookup in the TU.)
7109 auto *II = Name.getAsIdentifierInfo();
7110 assert(II && "non-identifier name in C?");
Richard Smith053f6c62014-05-16 23:01:30 +00007111 if (II->isOutOfDate())
7112 updateOutOfDateIdentifier(*II);
7113 } else
7114 DC->lookup(Name);
Richard Smith01bdb7a2014-08-28 05:44:07 +00007115 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
Richard Smith3cb15722015-08-05 22:41:45 +00007116 // Find all declarations of this kind from the relevant context.
7117 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7118 auto *DC = cast<DeclContext>(DCDecl);
7119 SmallVector<Decl*, 8> Decls;
7120 FindExternalLexicalDecls(
7121 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7122 }
Richard Smith053f6c62014-05-16 23:01:30 +00007123 }
7124 }
Richard Smith50895422015-01-31 03:04:55 +00007125
7126 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7127 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7128 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7129 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7130 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7131 if (auto *Template = FD->getPrimaryTemplate())
7132 Template->LoadLazySpecializations();
7133 }
Richard Smith053f6c62014-05-16 23:01:30 +00007134}
7135
Richard Smithc2bb8182015-03-24 06:36:48 +00007136CXXCtorInitializer **
7137ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7138 RecordLocation Loc = getLocalBitOffset(Offset);
7139 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7140 SavedStreamPosition SavedPosition(Cursor);
7141 Cursor.JumpToBit(Loc.Offset);
7142 ReadingKindTracker ReadingKind(Read_Decl, *this);
7143
7144 RecordData Record;
7145 unsigned Code = Cursor.ReadCode();
7146 unsigned RecCode = Cursor.readRecord(Code, Record);
7147 if (RecCode != DECL_CXX_CTOR_INITIALIZERS) {
7148 Error("malformed AST file: missing C++ ctor initializers");
7149 return nullptr;
7150 }
7151
7152 unsigned Idx = 0;
7153 return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
7154}
7155
Guy Benyei11169dd2012-12-18 14:30:41 +00007156CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00007157 assert(ContextObj && "reading base specifiers with no AST context");
7158 ASTContext &Context = *ContextObj;
7159
Guy Benyei11169dd2012-12-18 14:30:41 +00007160 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007161 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00007162 SavedStreamPosition SavedPosition(Cursor);
7163 Cursor.JumpToBit(Loc.Offset);
7164 ReadingKindTracker ReadingKind(Read_Decl, *this);
7165 RecordData Record;
7166 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00007167 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00007168 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00007169 Error("malformed AST file: missing C++ base specifiers");
Craig Toppera13603a2014-05-22 05:54:18 +00007170 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007171 }
7172
7173 unsigned Idx = 0;
7174 unsigned NumBases = Record[Idx++];
7175 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Vedant Kumar48b4f762018-04-14 01:40:48 +00007176 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
Guy Benyei11169dd2012-12-18 14:30:41 +00007177 for (unsigned I = 0; I != NumBases; ++I)
7178 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
7179 return Bases;
7180}
7181
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007182serialization::DeclID
Guy Benyei11169dd2012-12-18 14:30:41 +00007183ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7184 if (LocalID < NUM_PREDEF_DECL_IDS)
7185 return LocalID;
7186
Richard Smith37a93df2017-02-18 00:32:02 +00007187 if (!F.ModuleOffsetMap.empty())
7188 ReadModuleOffsetMap(F);
7189
Guy Benyei11169dd2012-12-18 14:30:41 +00007190 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7191 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7192 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007193
Guy Benyei11169dd2012-12-18 14:30:41 +00007194 return LocalID + I->second;
7195}
7196
7197bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7198 ModuleFile &M) const {
Richard Smithfe620d22015-03-05 23:24:12 +00007199 // Predefined decls aren't from any module.
7200 if (ID < NUM_PREDEF_DECL_IDS)
7201 return false;
7202
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007203 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
Richard Smithbcda1a92015-07-12 23:51:20 +00007204 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
Guy Benyei11169dd2012-12-18 14:30:41 +00007205}
7206
Douglas Gregor9f782892013-01-21 15:25:38 +00007207ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007208 if (!D->isFromASTFile())
Craig Toppera13603a2014-05-22 05:54:18 +00007209 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007210 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7211 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7212 return I->second;
7213}
7214
7215SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7216 if (ID < NUM_PREDEF_DECL_IDS)
Vedant Kumar48b4f762018-04-14 01:40:48 +00007217 return SourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00007218
Guy Benyei11169dd2012-12-18 14:30:41 +00007219 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7220
7221 if (Index > DeclsLoaded.size()) {
7222 Error("declaration ID out-of-range for AST file");
Vedant Kumar48b4f762018-04-14 01:40:48 +00007223 return SourceLocation();
Guy Benyei11169dd2012-12-18 14:30:41 +00007224 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00007225
Guy Benyei11169dd2012-12-18 14:30:41 +00007226 if (Decl *D = DeclsLoaded[Index])
7227 return D->getLocation();
7228
Richard Smithcb34bd32016-03-27 07:28:06 +00007229 SourceLocation Loc;
7230 DeclCursorForID(ID, Loc);
7231 return Loc;
Guy Benyei11169dd2012-12-18 14:30:41 +00007232}
7233
Richard Smithfe620d22015-03-05 23:24:12 +00007234static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7235 switch (ID) {
7236 case PREDEF_DECL_NULL_ID:
7237 return nullptr;
7238
7239 case PREDEF_DECL_TRANSLATION_UNIT_ID:
7240 return Context.getTranslationUnitDecl();
7241
7242 case PREDEF_DECL_OBJC_ID_ID:
7243 return Context.getObjCIdDecl();
7244
7245 case PREDEF_DECL_OBJC_SEL_ID:
7246 return Context.getObjCSelDecl();
7247
7248 case PREDEF_DECL_OBJC_CLASS_ID:
7249 return Context.getObjCClassDecl();
7250
7251 case PREDEF_DECL_OBJC_PROTOCOL_ID:
7252 return Context.getObjCProtocolDecl();
7253
7254 case PREDEF_DECL_INT_128_ID:
7255 return Context.getInt128Decl();
7256
7257 case PREDEF_DECL_UNSIGNED_INT_128_ID:
7258 return Context.getUInt128Decl();
7259
7260 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7261 return Context.getObjCInstanceTypeDecl();
7262
7263 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7264 return Context.getBuiltinVaListDecl();
Richard Smithf19e1272015-03-07 00:04:49 +00007265
Richard Smith9b88a4c2015-07-27 05:40:23 +00007266 case PREDEF_DECL_VA_LIST_TAG:
7267 return Context.getVaListTagDecl();
7268
Charles Davisc7d5c942015-09-17 20:55:33 +00007269 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7270 return Context.getBuiltinMSVaListDecl();
7271
Richard Smithf19e1272015-03-07 00:04:49 +00007272 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7273 return Context.getExternCContextDecl();
David Majnemerd9b1a4f2015-11-04 03:40:30 +00007274
7275 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7276 return Context.getMakeIntegerSeqDecl();
Quentin Colombet043406b2016-02-03 22:41:00 +00007277
7278 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7279 return Context.getCFConstantStringDecl();
Ben Langmuirf5416742016-02-04 00:55:24 +00007280
7281 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7282 return Context.getCFConstantStringTagDecl();
Eric Fiselier6ad68552016-07-01 01:24:09 +00007283
7284 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7285 return Context.getTypePackElementDecl();
Richard Smithfe620d22015-03-05 23:24:12 +00007286 }
Yaron Keren322bdad2015-03-06 07:49:14 +00007287 llvm_unreachable("PredefinedDeclIDs unknown enum value");
Richard Smithfe620d22015-03-05 23:24:12 +00007288}
7289
Richard Smithcd45dbc2014-04-19 03:48:30 +00007290Decl *ASTReader::GetExistingDecl(DeclID ID) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00007291 assert(ContextObj && "reading decl with no AST context");
Richard Smithcd45dbc2014-04-19 03:48:30 +00007292 if (ID < NUM_PREDEF_DECL_IDS) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00007293 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
Richard Smithfe620d22015-03-05 23:24:12 +00007294 if (D) {
7295 // Track that we have merged the declaration with ID \p ID into the
7296 // pre-existing predefined declaration \p D.
Richard Smith5fc18a92015-07-12 23:43:21 +00007297 auto &Merged = KeyDecls[D->getCanonicalDecl()];
Richard Smithfe620d22015-03-05 23:24:12 +00007298 if (Merged.empty())
7299 Merged.push_back(ID);
Guy Benyei11169dd2012-12-18 14:30:41 +00007300 }
Richard Smithfe620d22015-03-05 23:24:12 +00007301 return D;
Guy Benyei11169dd2012-12-18 14:30:41 +00007302 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00007303
Guy Benyei11169dd2012-12-18 14:30:41 +00007304 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7305
7306 if (Index >= DeclsLoaded.size()) {
7307 assert(0 && "declaration ID out-of-range for AST file");
7308 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007309 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007310 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00007311
7312 return DeclsLoaded[Index];
7313}
7314
7315Decl *ASTReader::GetDecl(DeclID ID) {
7316 if (ID < NUM_PREDEF_DECL_IDS)
7317 return GetExistingDecl(ID);
7318
7319 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7320
7321 if (Index >= DeclsLoaded.size()) {
7322 assert(0 && "declaration ID out-of-range for AST file");
7323 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007324 return nullptr;
Richard Smithcd45dbc2014-04-19 03:48:30 +00007325 }
7326
Guy Benyei11169dd2012-12-18 14:30:41 +00007327 if (!DeclsLoaded[Index]) {
7328 ReadDeclRecord(ID);
7329 if (DeserializationListener)
7330 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7331 }
7332
7333 return DeclsLoaded[Index];
7334}
7335
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007336DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
Guy Benyei11169dd2012-12-18 14:30:41 +00007337 DeclID GlobalID) {
7338 if (GlobalID < NUM_PREDEF_DECL_IDS)
7339 return GlobalID;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007340
Guy Benyei11169dd2012-12-18 14:30:41 +00007341 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7342 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7343 ModuleFile *Owner = I->second;
7344
7345 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7346 = M.GlobalToLocalDeclIDs.find(Owner);
7347 if (Pos == M.GlobalToLocalDeclIDs.end())
7348 return 0;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007349
Guy Benyei11169dd2012-12-18 14:30:41 +00007350 return GlobalID - Owner->BaseDeclID + Pos->second;
7351}
7352
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007353serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Guy Benyei11169dd2012-12-18 14:30:41 +00007354 const RecordData &Record,
7355 unsigned &Idx) {
7356 if (Idx >= Record.size()) {
7357 Error("Corrupted AST file");
7358 return 0;
7359 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007360
Guy Benyei11169dd2012-12-18 14:30:41 +00007361 return getGlobalDeclID(F, Record[Idx++]);
7362}
7363
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007364/// Resolve the offset of a statement into a statement.
Guy Benyei11169dd2012-12-18 14:30:41 +00007365///
7366/// This operation will read a new statement from the external
7367/// source each time it is called, and is meant to be used via a
7368/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7369Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7370 // Switch case IDs are per Decl.
7371 ClearSwitchCaseIDs();
7372
7373 // Offset here is a global offset across the entire chain.
7374 RecordLocation Loc = getLocalBitOffset(Offset);
7375 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
David Blaikie9fd16f82017-03-08 23:57:08 +00007376 assert(NumCurrentElementsDeserializing == 0 &&
7377 "should not be called while already deserializing");
7378 Deserializing D(this);
Guy Benyei11169dd2012-12-18 14:30:41 +00007379 return ReadStmtFromStream(*Loc.F);
7380}
7381
Richard Smith3cb15722015-08-05 22:41:45 +00007382void ASTReader::FindExternalLexicalDecls(
7383 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7384 SmallVectorImpl<Decl *> &Decls) {
Richard Smith82f8fcd2015-08-06 22:07:25 +00007385 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7386
Richard Smith9ccdd932015-08-06 22:14:12 +00007387 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
Richard Smith82f8fcd2015-08-06 22:07:25 +00007388 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7389 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7390 auto K = (Decl::Kind)+LexicalDecls[I];
7391 if (!IsKindWeWant(K))
7392 continue;
7393
7394 auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7395
7396 // Don't add predefined declarations to the lexical context more
7397 // than once.
7398 if (ID < NUM_PREDEF_DECL_IDS) {
7399 if (PredefsVisited[ID])
7400 continue;
7401
7402 PredefsVisited[ID] = true;
7403 }
7404
7405 if (Decl *D = GetLocalDecl(*M, ID)) {
Richard Smith2317a3e2015-08-11 21:21:20 +00007406 assert(D->getKind() == K && "wrong kind for lexical decl");
Richard Smith82f8fcd2015-08-06 22:07:25 +00007407 if (!DC->isDeclInLexicalTraversal(D))
7408 Decls.push_back(D);
7409 }
7410 }
7411 };
7412
7413 if (isa<TranslationUnitDecl>(DC)) {
7414 for (auto Lexical : TULexicalDecls)
7415 Visit(Lexical.first, Lexical.second);
7416 } else {
7417 auto I = LexicalDecls.find(DC);
7418 if (I != LexicalDecls.end())
Richard Smith9c9173d2015-08-11 22:00:24 +00007419 Visit(I->second.first, I->second.second);
Richard Smith82f8fcd2015-08-06 22:07:25 +00007420 }
7421
Guy Benyei11169dd2012-12-18 14:30:41 +00007422 ++NumLexicalDeclContextsRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00007423}
7424
7425namespace {
7426
7427class DeclIDComp {
7428 ASTReader &Reader;
7429 ModuleFile &Mod;
7430
7431public:
7432 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7433
7434 bool operator()(LocalDeclID L, LocalDeclID R) const {
7435 SourceLocation LHS = getLocation(L);
7436 SourceLocation RHS = getLocation(R);
7437 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7438 }
7439
7440 bool operator()(SourceLocation LHS, LocalDeclID R) const {
7441 SourceLocation RHS = getLocation(R);
7442 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7443 }
7444
7445 bool operator()(LocalDeclID L, SourceLocation RHS) const {
7446 SourceLocation LHS = getLocation(L);
7447 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7448 }
7449
7450 SourceLocation getLocation(LocalDeclID ID) const {
7451 return Reader.getSourceManager().getFileLoc(
7452 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7453 }
7454};
7455
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007456} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00007457
7458void ASTReader::FindFileRegionDecls(FileID File,
7459 unsigned Offset, unsigned Length,
7460 SmallVectorImpl<Decl *> &Decls) {
7461 SourceManager &SM = getSourceManager();
7462
7463 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7464 if (I == FileDeclIDs.end())
7465 return;
7466
7467 FileDeclsInfo &DInfo = I->second;
7468 if (DInfo.Decls.empty())
7469 return;
7470
7471 SourceLocation
7472 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7473 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7474
7475 DeclIDComp DIDComp(*this, *DInfo.Mod);
7476 ArrayRef<serialization::LocalDeclID>::iterator
7477 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
7478 BeginLoc, DIDComp);
7479 if (BeginIt != DInfo.Decls.begin())
7480 --BeginIt;
7481
7482 // If we are pointing at a top-level decl inside an objc container, we need
7483 // to backtrack until we find it otherwise we will fail to report that the
7484 // region overlaps with an objc container.
7485 while (BeginIt != DInfo.Decls.begin() &&
7486 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7487 ->isTopLevelDeclInObjCContainer())
7488 --BeginIt;
7489
7490 ArrayRef<serialization::LocalDeclID>::iterator
7491 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
7492 EndLoc, DIDComp);
7493 if (EndIt != DInfo.Decls.end())
7494 ++EndIt;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007495
Guy Benyei11169dd2012-12-18 14:30:41 +00007496 for (ArrayRef<serialization::LocalDeclID>::iterator
7497 DIt = BeginIt; DIt != EndIt; ++DIt)
7498 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7499}
7500
Richard Smith9ce12e32013-02-07 03:30:24 +00007501bool
Guy Benyei11169dd2012-12-18 14:30:41 +00007502ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7503 DeclarationName Name) {
Richard Smithd88a7f12015-09-01 20:35:42 +00007504 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
Guy Benyei11169dd2012-12-18 14:30:41 +00007505 "DeclContext has no visible decls in storage");
7506 if (!Name)
Richard Smith9ce12e32013-02-07 03:30:24 +00007507 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00007508
Richard Smithd88a7f12015-09-01 20:35:42 +00007509 auto It = Lookups.find(DC);
7510 if (It == Lookups.end())
7511 return false;
7512
Richard Smith8c913ec2014-08-14 02:21:01 +00007513 Deserializing LookupResults(this);
7514
Richard Smithd88a7f12015-09-01 20:35:42 +00007515 // Load the list of declarations.
Guy Benyei11169dd2012-12-18 14:30:41 +00007516 SmallVector<NamedDecl *, 64> Decls;
Vedant Kumar48b4f762018-04-14 01:40:48 +00007517 for (DeclID ID : It->second.Table.find(Name)) {
7518 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
Richard Smithd88a7f12015-09-01 20:35:42 +00007519 if (ND->getDeclName() == Name)
7520 Decls.push_back(ND);
7521 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00007522
Guy Benyei11169dd2012-12-18 14:30:41 +00007523 ++NumVisibleDeclContextsRead;
7524 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith9ce12e32013-02-07 03:30:24 +00007525 return !Decls.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +00007526}
7527
Guy Benyei11169dd2012-12-18 14:30:41 +00007528void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7529 if (!DC->hasExternalVisibleStorage())
7530 return;
Richard Smithd88a7f12015-09-01 20:35:42 +00007531
7532 auto It = Lookups.find(DC);
7533 assert(It != Lookups.end() &&
7534 "have external visible storage but no lookup tables");
7535
Craig Topper79be4cd2013-07-05 04:33:53 +00007536 DeclsMap Decls;
Guy Benyei11169dd2012-12-18 14:30:41 +00007537
Vedant Kumar48b4f762018-04-14 01:40:48 +00007538 for (DeclID ID : It->second.Table.findAll()) {
7539 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
Richard Smithd88a7f12015-09-01 20:35:42 +00007540 Decls[ND->getDeclName()].push_back(ND);
Guy Benyei11169dd2012-12-18 14:30:41 +00007541 }
7542
Guy Benyei11169dd2012-12-18 14:30:41 +00007543 ++NumVisibleDeclContextsRead;
7544
Vedant Kumar48b4f762018-04-14 01:40:48 +00007545 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7546 SetExternalVisibleDeclsForName(DC, I->first, I->second);
7547 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007548 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7549}
7550
Richard Smithd88a7f12015-09-01 20:35:42 +00007551const serialization::reader::DeclContextLookupTable *
7552ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7553 auto I = Lookups.find(Primary);
7554 return I == Lookups.end() ? nullptr : &I->second;
7555}
7556
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007557/// Under non-PCH compilation the consumer receives the objc methods
Guy Benyei11169dd2012-12-18 14:30:41 +00007558/// before receiving the implementation, and codegen depends on this.
7559/// We simulate this by deserializing and passing to consumer the methods of the
7560/// implementation before passing the deserialized implementation decl.
7561static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7562 ASTConsumer *Consumer) {
7563 assert(ImplD && Consumer);
7564
Aaron Ballmanaff18c02014-03-13 19:03:34 +00007565 for (auto *I : ImplD->methods())
7566 Consumer->HandleInterestingDecl(DeclGroupRef(I));
Guy Benyei11169dd2012-12-18 14:30:41 +00007567
7568 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7569}
7570
Guy Benyei11169dd2012-12-18 14:30:41 +00007571void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00007572 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00007573 PassObjCImplDeclToConsumer(ImplD, Consumer);
7574 else
7575 Consumer->HandleInterestingDecl(DeclGroupRef(D));
7576}
7577
7578void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7579 this->Consumer = Consumer;
7580
Richard Smith9e2341d2015-03-23 03:25:59 +00007581 if (Consumer)
7582 PassInterestingDeclsToConsumer();
Richard Smith7f330cd2015-03-18 01:42:29 +00007583
7584 if (DeserializationListener)
7585 DeserializationListener->ReaderInitialized(this);
Guy Benyei11169dd2012-12-18 14:30:41 +00007586}
7587
7588void ASTReader::PrintStats() {
7589 std::fprintf(stderr, "*** AST File Statistics:\n");
7590
7591 unsigned NumTypesLoaded
7592 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7593 QualType());
7594 unsigned NumDeclsLoaded
7595 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00007596 (Decl *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00007597 unsigned NumIdentifiersLoaded
7598 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7599 IdentifiersLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00007600 (IdentifierInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00007601 unsigned NumMacrosLoaded
7602 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7603 MacrosLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00007604 (MacroInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00007605 unsigned NumSelectorsLoaded
7606 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7607 SelectorsLoaded.end(),
7608 Selector());
7609
7610 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7611 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
7612 NumSLocEntriesRead, TotalNumSLocEntries,
7613 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7614 if (!TypesLoaded.empty())
7615 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
7616 NumTypesLoaded, (unsigned)TypesLoaded.size(),
7617 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7618 if (!DeclsLoaded.empty())
7619 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
7620 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7621 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7622 if (!IdentifiersLoaded.empty())
7623 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
7624 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7625 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7626 if (!MacrosLoaded.empty())
7627 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
7628 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7629 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7630 if (!SelectorsLoaded.empty())
7631 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
7632 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7633 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7634 if (TotalNumStatements)
7635 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
7636 NumStatementsRead, TotalNumStatements,
7637 ((float)NumStatementsRead/TotalNumStatements * 100));
7638 if (TotalNumMacros)
7639 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
7640 NumMacrosRead, TotalNumMacros,
7641 ((float)NumMacrosRead/TotalNumMacros * 100));
7642 if (TotalLexicalDeclContexts)
7643 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
7644 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7645 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7646 * 100));
7647 if (TotalVisibleDeclContexts)
7648 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
7649 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7650 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7651 * 100));
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007652 if (TotalNumMethodPoolEntries)
Guy Benyei11169dd2012-12-18 14:30:41 +00007653 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
7654 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7655 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7656 * 100));
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007657 if (NumMethodPoolLookups)
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007658 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
7659 NumMethodPoolHits, NumMethodPoolLookups,
7660 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007661 if (NumMethodPoolTableLookups)
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007662 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
7663 NumMethodPoolTableHits, NumMethodPoolTableLookups,
7664 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7665 * 100.0));
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007666 if (NumIdentifierLookupHits)
Douglas Gregor00a50f72013-01-25 00:38:33 +00007667 std::fprintf(stderr,
7668 " %u / %u identifier table lookups succeeded (%f%%)\n",
7669 NumIdentifierLookupHits, NumIdentifierLookups,
7670 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
Douglas Gregor00a50f72013-01-25 00:38:33 +00007671
Douglas Gregore060e572013-01-25 01:03:03 +00007672 if (GlobalIndex) {
7673 std::fprintf(stderr, "\n");
7674 GlobalIndex->printStats();
7675 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007676
Guy Benyei11169dd2012-12-18 14:30:41 +00007677 std::fprintf(stderr, "\n");
7678 dump();
7679 std::fprintf(stderr, "\n");
7680}
7681
7682template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Vassil Vassilevb2710682017-03-02 18:13:19 +00007683LLVM_DUMP_METHOD static void
Guy Benyei11169dd2012-12-18 14:30:41 +00007684dumpModuleIDMap(StringRef Name,
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007685 const ContinuousRangeMap<Key, ModuleFile *,
Guy Benyei11169dd2012-12-18 14:30:41 +00007686 InitialCapacity> &Map) {
7687 if (Map.begin() == Map.end())
7688 return;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007689
Vedant Kumar48b4f762018-04-14 01:40:48 +00007690 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7691
Guy Benyei11169dd2012-12-18 14:30:41 +00007692 llvm::errs() << Name << ":\n";
Vedant Kumar48b4f762018-04-14 01:40:48 +00007693 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7694 I != IEnd; ++I) {
7695 llvm::errs() << " " << I->first << " -> " << I->second->FileName
7696 << "\n";
7697 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007698}
7699
Yaron Kerencdae9412016-01-29 19:38:18 +00007700LLVM_DUMP_METHOD void ASTReader::dump() {
Guy Benyei11169dd2012-12-18 14:30:41 +00007701 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7702 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7703 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7704 dumpModuleIDMap("Global type map", GlobalTypeMap);
7705 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7706 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7707 dumpModuleIDMap("Global macro map", GlobalMacroMap);
7708 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7709 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007710 dumpModuleIDMap("Global preprocessed entity map",
Guy Benyei11169dd2012-12-18 14:30:41 +00007711 GlobalPreprocessedEntityMap);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007712
Guy Benyei11169dd2012-12-18 14:30:41 +00007713 llvm::errs() << "\n*** PCH/Modules Loaded:";
Vedant Kumar48b4f762018-04-14 01:40:48 +00007714 for (ModuleFile &M : ModuleMgr)
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00007715 M.dump();
Guy Benyei11169dd2012-12-18 14:30:41 +00007716}
7717
7718/// Return the amount of memory used by memory buffers, breaking down
7719/// by heap-backed versus mmap'ed memory.
7720void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Vedant Kumar48b4f762018-04-14 01:40:48 +00007721 for (ModuleFile &I : ModuleMgr) {
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00007722 if (llvm::MemoryBuffer *buf = I.Buffer) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007723 size_t bytes = buf->getBufferSize();
7724 switch (buf->getBufferKind()) {
7725 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7726 sizes.malloc_bytes += bytes;
7727 break;
7728 case llvm::MemoryBuffer::MemoryBuffer_MMap:
7729 sizes.mmap_bytes += bytes;
7730 break;
7731 }
7732 }
7733 }
7734}
7735
7736void ASTReader::InitializeSema(Sema &S) {
7737 SemaObj = &S;
7738 S.addExternalSource(this);
7739
7740 // Makes sure any declarations that were deserialized "too early"
7741 // still get added to the identifier's declaration chains.
Vedant Kumar48b4f762018-04-14 01:40:48 +00007742 for (uint64_t ID : PreloadedDeclIDs) {
7743 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
Ben Langmuir5418f402014-09-10 21:29:41 +00007744 pushExternalDeclIntoScope(D, D->getDeclName());
Guy Benyei11169dd2012-12-18 14:30:41 +00007745 }
Ben Langmuir5418f402014-09-10 21:29:41 +00007746 PreloadedDeclIDs.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00007747
Richard Smith3d8e97e2013-10-18 06:54:39 +00007748 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00007749 if (!FPPragmaOptions.empty()) {
7750 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
Adam Nemet484aa452017-03-27 19:17:25 +00007751 SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007752 }
7753
Yaxun Liu5b746652016-12-18 05:18:55 +00007754 SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7755 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7756 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
Richard Smith3d8e97e2013-10-18 06:54:39 +00007757
7758 UpdateSema();
7759}
7760
7761void ASTReader::UpdateSema() {
7762 assert(SemaObj && "no Sema to update");
7763
7764 // Load the offsets of the declarations that Sema references.
7765 // They will be lazily deserialized when needed.
7766 if (!SemaDeclRefs.empty()) {
Richard Smith96269c52016-09-29 22:49:46 +00007767 assert(SemaDeclRefs.size() % 3 == 0);
7768 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
Richard Smith3d8e97e2013-10-18 06:54:39 +00007769 if (!SemaObj->StdNamespace)
7770 SemaObj->StdNamespace = SemaDeclRefs[I];
7771 if (!SemaObj->StdBadAlloc)
7772 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
Richard Smith96269c52016-09-29 22:49:46 +00007773 if (!SemaObj->StdAlignValT)
7774 SemaObj->StdAlignValT = SemaDeclRefs[I+2];
Richard Smith3d8e97e2013-10-18 06:54:39 +00007775 }
7776 SemaDeclRefs.clear();
7777 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00007778
Nico Weber779355f2016-03-02 23:22:00 +00007779 // Update the state of pragmas. Use the same API as if we had encountered the
7780 // pragma in the source.
Dario Domizioli13a0a382014-05-23 12:13:25 +00007781 if(OptimizeOffPragmaLocation.isValid())
7782 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
Nico Weber779355f2016-03-02 23:22:00 +00007783 if (PragmaMSStructState != -1)
7784 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
Nico Weber42932312016-03-03 00:17:35 +00007785 if (PointersToMembersPragmaLocation.isValid()) {
7786 SemaObj->ActOnPragmaMSPointersToMembers(
7787 (LangOptions::PragmaMSPointersToMembersKind)
7788 PragmaMSPointersToMembersState,
7789 PointersToMembersPragmaLocation);
7790 }
Justin Lebar67a78a62016-10-08 22:15:58 +00007791 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00007792
7793 if (PragmaPackCurrentValue) {
7794 // The bottom of the stack might have a default value. It must be adjusted
7795 // to the current value to ensure that the packing state is preserved after
7796 // popping entries that were included/imported from a PCH/module.
7797 bool DropFirst = false;
7798 if (!PragmaPackStack.empty() &&
7799 PragmaPackStack.front().Location.isInvalid()) {
7800 assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7801 "Expected a default alignment value");
7802 SemaObj->PackStack.Stack.emplace_back(
7803 PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
Alex Lorenz45b40142017-07-28 14:41:21 +00007804 SemaObj->PackStack.CurrentPragmaLocation,
7805 PragmaPackStack.front().PushLocation);
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00007806 DropFirst = true;
7807 }
7808 for (const auto &Entry :
7809 llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7810 SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
Alex Lorenz45b40142017-07-28 14:41:21 +00007811 Entry.Location, Entry.PushLocation);
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00007812 if (PragmaPackCurrentLocation.isInvalid()) {
7813 assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7814 "Expected a default alignment value");
7815 // Keep the current values.
7816 } else {
7817 SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7818 SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7819 }
7820 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007821}
7822
Richard Smitha8d5b6a2015-07-17 19:51:03 +00007823IdentifierInfo *ASTReader::get(StringRef Name) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007824 // Note that we are loading an identifier.
7825 Deserializing AnIdentifier(this);
Douglas Gregore060e572013-01-25 01:03:03 +00007826
Douglas Gregor7211ac12013-01-25 23:32:03 +00007827 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregor00a50f72013-01-25 00:38:33 +00007828 NumIdentifierLookups,
7829 NumIdentifierLookupHits);
Richard Smith33e0f7e2015-07-22 02:08:40 +00007830
7831 // We don't need to do identifier table lookups in C++ modules (we preload
7832 // all interesting declarations, and don't need to use the scope for name
7833 // lookups). Perform the lookup in PCH files, though, since we don't build
7834 // a complete initial identifier table if we're carrying on from a PCH.
Richard Smithdbafb6c2017-06-29 23:23:46 +00007835 if (PP.getLangOpts().CPlusPlus) {
Richard Smith33e0f7e2015-07-22 02:08:40 +00007836 for (auto F : ModuleMgr.pch_modules())
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007837 if (Visitor(*F))
Richard Smith33e0f7e2015-07-22 02:08:40 +00007838 break;
7839 } else {
7840 // If there is a global index, look there first to determine which modules
7841 // provably do not have any results for this identifier.
7842 GlobalModuleIndex::HitSet Hits;
7843 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7844 if (!loadGlobalIndex()) {
7845 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7846 HitsPtr = &Hits;
7847 }
7848 }
7849
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007850 ModuleMgr.visit(Visitor, HitsPtr);
Richard Smith33e0f7e2015-07-22 02:08:40 +00007851 }
7852
Guy Benyei11169dd2012-12-18 14:30:41 +00007853 IdentifierInfo *II = Visitor.getIdentifierInfo();
7854 markIdentifierUpToDate(II);
7855 return II;
7856}
7857
7858namespace clang {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00007859
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007860 /// An identifier-lookup iterator that enumerates all of the
Guy Benyei11169dd2012-12-18 14:30:41 +00007861 /// identifiers stored within a set of AST files.
7862 class ASTIdentifierIterator : public IdentifierIterator {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007863 /// The AST reader whose identifiers are being enumerated.
Guy Benyei11169dd2012-12-18 14:30:41 +00007864 const ASTReader &Reader;
7865
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007866 /// The current index into the chain of AST files stored in
Guy Benyei11169dd2012-12-18 14:30:41 +00007867 /// the AST reader.
7868 unsigned Index;
7869
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007870 /// The current position within the identifier lookup table
Guy Benyei11169dd2012-12-18 14:30:41 +00007871 /// of the current AST file.
7872 ASTIdentifierLookupTable::key_iterator Current;
7873
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007874 /// The end position within the identifier lookup table of
Guy Benyei11169dd2012-12-18 14:30:41 +00007875 /// the current AST file.
7876 ASTIdentifierLookupTable::key_iterator End;
7877
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007878 /// Whether to skip any modules in the ASTReader.
Ben Langmuir537c5b52016-05-04 00:53:13 +00007879 bool SkipModules;
7880
Guy Benyei11169dd2012-12-18 14:30:41 +00007881 public:
Ben Langmuir537c5b52016-05-04 00:53:13 +00007882 explicit ASTIdentifierIterator(const ASTReader &Reader,
7883 bool SkipModules = false);
Guy Benyei11169dd2012-12-18 14:30:41 +00007884
Craig Topper3e89dfe2014-03-13 02:13:41 +00007885 StringRef Next() override;
Guy Benyei11169dd2012-12-18 14:30:41 +00007886 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00007887
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007888} // namespace clang
Guy Benyei11169dd2012-12-18 14:30:41 +00007889
Ben Langmuir537c5b52016-05-04 00:53:13 +00007890ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
7891 bool SkipModules)
7892 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007893}
7894
7895StringRef ASTIdentifierIterator::Next() {
7896 while (Current == End) {
7897 // If we have exhausted all of our AST files, we're done.
7898 if (Index == 0)
Vedant Kumar48b4f762018-04-14 01:40:48 +00007899 return StringRef();
Guy Benyei11169dd2012-12-18 14:30:41 +00007900
7901 --Index;
Ben Langmuir537c5b52016-05-04 00:53:13 +00007902 ModuleFile &F = Reader.ModuleMgr[Index];
7903 if (SkipModules && F.isModule())
7904 continue;
7905
Vedant Kumar48b4f762018-04-14 01:40:48 +00007906 ASTIdentifierLookupTable *IdTable =
7907 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
Guy Benyei11169dd2012-12-18 14:30:41 +00007908 Current = IdTable->key_begin();
7909 End = IdTable->key_end();
7910 }
7911
7912 // We have any identifiers remaining in the current AST file; return
7913 // the next one.
Douglas Gregorbfd73d72013-01-23 18:53:14 +00007914 StringRef Result = *Current;
Guy Benyei11169dd2012-12-18 14:30:41 +00007915 ++Current;
Douglas Gregorbfd73d72013-01-23 18:53:14 +00007916 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00007917}
7918
Ben Langmuir537c5b52016-05-04 00:53:13 +00007919namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00007920
Ben Langmuir537c5b52016-05-04 00:53:13 +00007921/// A utility for appending two IdentifierIterators.
7922class ChainedIdentifierIterator : public IdentifierIterator {
7923 std::unique_ptr<IdentifierIterator> Current;
7924 std::unique_ptr<IdentifierIterator> Queued;
7925
7926public:
7927 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
7928 std::unique_ptr<IdentifierIterator> Second)
7929 : Current(std::move(First)), Queued(std::move(Second)) {}
7930
7931 StringRef Next() override {
7932 if (!Current)
Vedant Kumar48b4f762018-04-14 01:40:48 +00007933 return StringRef();
Ben Langmuir537c5b52016-05-04 00:53:13 +00007934
7935 StringRef result = Current->Next();
7936 if (!result.empty())
7937 return result;
7938
7939 // Try the queued iterator, which may itself be empty.
7940 Current.reset();
7941 std::swap(Current, Queued);
7942 return Next();
7943 }
7944};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00007945
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007946} // namespace
Ben Langmuir537c5b52016-05-04 00:53:13 +00007947
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00007948IdentifierIterator *ASTReader::getIdentifiers() {
Ben Langmuir537c5b52016-05-04 00:53:13 +00007949 if (!loadGlobalIndex()) {
7950 std::unique_ptr<IdentifierIterator> ReaderIter(
7951 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
7952 std::unique_ptr<IdentifierIterator> ModulesIter(
7953 GlobalIndex->createIdentifierIterator());
7954 return new ChainedIdentifierIterator(std::move(ReaderIter),
7955 std::move(ModulesIter));
7956 }
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00007957
Guy Benyei11169dd2012-12-18 14:30:41 +00007958 return new ASTIdentifierIterator(*this);
7959}
7960
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00007961namespace clang {
7962namespace serialization {
7963
Guy Benyei11169dd2012-12-18 14:30:41 +00007964 class ReadMethodPoolVisitor {
7965 ASTReader &Reader;
7966 Selector Sel;
7967 unsigned PriorGeneration;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007968 unsigned InstanceBits = 0;
7969 unsigned FactoryBits = 0;
7970 bool InstanceHasMoreThanOneDecl = false;
7971 bool FactoryHasMoreThanOneDecl = false;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00007972 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
7973 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00007974
7975 public:
Nico Weber2e0c8f72014-12-27 03:58:08 +00007976 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
Guy Benyei11169dd2012-12-18 14:30:41 +00007977 unsigned PriorGeneration)
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007978 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
Nico Weber2e0c8f72014-12-27 03:58:08 +00007979
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007980 bool operator()(ModuleFile &M) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007981 if (!M.SelectorLookupTable)
7982 return false;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007983
Guy Benyei11169dd2012-12-18 14:30:41 +00007984 // If we've already searched this module file, skip it now.
Richard Smithbdf2d932015-07-30 03:37:16 +00007985 if (M.Generation <= PriorGeneration)
Guy Benyei11169dd2012-12-18 14:30:41 +00007986 return true;
7987
Richard Smithbdf2d932015-07-30 03:37:16 +00007988 ++Reader.NumMethodPoolTableLookups;
Vedant Kumar48b4f762018-04-14 01:40:48 +00007989 ASTSelectorLookupTable *PoolTable
7990 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
Richard Smithbdf2d932015-07-30 03:37:16 +00007991 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Guy Benyei11169dd2012-12-18 14:30:41 +00007992 if (Pos == PoolTable->end())
7993 return false;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007994
Richard Smithbdf2d932015-07-30 03:37:16 +00007995 ++Reader.NumMethodPoolTableHits;
7996 ++Reader.NumSelectorsRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00007997 // FIXME: Not quite happy with the statistics here. We probably should
7998 // disable this tracking when called via LoadSelector.
7999 // Also, should entries without methods count as misses?
Richard Smithbdf2d932015-07-30 03:37:16 +00008000 ++Reader.NumMethodPoolEntriesRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00008001 ASTSelectorLookupTrait::data_type Data = *Pos;
Richard Smithbdf2d932015-07-30 03:37:16 +00008002 if (Reader.DeserializationListener)
8003 Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00008004
Richard Smithbdf2d932015-07-30 03:37:16 +00008005 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8006 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8007 InstanceBits = Data.InstanceBits;
8008 FactoryBits = Data.FactoryBits;
8009 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8010 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00008011 return true;
8012 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008013
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008014 /// Retrieve the instance methods found by this visitor.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008015 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8016 return InstanceMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00008017 }
8018
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008019 /// Retrieve the instance methods found by this visitor.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008020 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
Guy Benyei11169dd2012-12-18 14:30:41 +00008021 return FactoryMethods;
8022 }
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00008023
8024 unsigned getInstanceBits() const { return InstanceBits; }
8025 unsigned getFactoryBits() const { return FactoryBits; }
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008026
Nico Weberff4b35e2014-12-27 22:14:15 +00008027 bool instanceHasMoreThanOneDecl() const {
8028 return InstanceHasMoreThanOneDecl;
8029 }
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008030
Nico Weberff4b35e2014-12-27 22:14:15 +00008031 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
Guy Benyei11169dd2012-12-18 14:30:41 +00008032 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00008033
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008034} // namespace serialization
8035} // namespace clang
Guy Benyei11169dd2012-12-18 14:30:41 +00008036
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008037/// Add the given set of methods to the method list.
Guy Benyei11169dd2012-12-18 14:30:41 +00008038static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8039 ObjCMethodList &List) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008040 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8041 S.addMethodToGlobalList(&List, Methods[I]);
8042 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008043}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008044
Guy Benyei11169dd2012-12-18 14:30:41 +00008045void ASTReader::ReadMethodPool(Selector Sel) {
8046 // Get the selector generation and update it to the current generation.
8047 unsigned &Generation = SelectorGeneration[Sel];
8048 unsigned PriorGeneration = Generation;
Richard Smith053f6c62014-05-16 23:01:30 +00008049 Generation = getGeneration();
Manman Rena0f31a02016-04-29 19:04:05 +00008050 SelectorOutOfDate[Sel] = false;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008051
Guy Benyei11169dd2012-12-18 14:30:41 +00008052 // Search for methods defined with this selector.
Douglas Gregorad2f7a52013-01-28 17:54:36 +00008053 ++NumMethodPoolLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00008054 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00008055 ModuleMgr.visit(Visitor);
8056
Guy Benyei11169dd2012-12-18 14:30:41 +00008057 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregorad2f7a52013-01-28 17:54:36 +00008058 Visitor.getFactoryMethods().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00008059 return;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00008060
8061 ++NumMethodPoolHits;
8062
Guy Benyei11169dd2012-12-18 14:30:41 +00008063 if (!getSema())
8064 return;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008065
Guy Benyei11169dd2012-12-18 14:30:41 +00008066 Sema &S = *getSema();
8067 Sema::GlobalMethodPool::iterator Pos
8068 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
Ben Langmuira0c32e92015-01-12 19:27:00 +00008069
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00008070 Pos->second.first.setBits(Visitor.getInstanceBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00008071 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00008072 Pos->second.second.setBits(Visitor.getFactoryBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00008073 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
Ben Langmuira0c32e92015-01-12 19:27:00 +00008074
8075 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8076 // when building a module we keep every method individually and may need to
8077 // update hasMoreThanOneDecl as we add the methods.
8078 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8079 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Guy Benyei11169dd2012-12-18 14:30:41 +00008080}
8081
Manman Rena0f31a02016-04-29 19:04:05 +00008082void ASTReader::updateOutOfDateSelector(Selector Sel) {
8083 if (SelectorOutOfDate[Sel])
8084 ReadMethodPool(Sel);
8085}
8086
Guy Benyei11169dd2012-12-18 14:30:41 +00008087void ASTReader::ReadKnownNamespaces(
8088 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8089 Namespaces.clear();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008090
Vedant Kumar48b4f762018-04-14 01:40:48 +00008091 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8092 if (NamespaceDecl *Namespace
8093 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
Guy Benyei11169dd2012-12-18 14:30:41 +00008094 Namespaces.push_back(Namespace);
Vedant Kumar48b4f762018-04-14 01:40:48 +00008095 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008096}
8097
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00008098void ASTReader::ReadUndefinedButUsed(
Richard Smithd6a04d72016-03-25 21:49:43 +00008099 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00008100 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008101 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky8334af82013-01-26 00:35:08 +00008102 SourceLocation Loc =
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00008103 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky8334af82013-01-26 00:35:08 +00008104 Undefined.insert(std::make_pair(D, Loc));
8105 }
8106}
Nick Lewycky8334af82013-01-26 00:35:08 +00008107
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00008108void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8109 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8110 Exprs) {
8111 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008112 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00008113 uint64_t Count = DelayedDeleteExprs[Idx++];
8114 for (uint64_t C = 0; C < Count; ++C) {
8115 SourceLocation DeleteLoc =
8116 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8117 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8118 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8119 }
8120 }
8121}
8122
Guy Benyei11169dd2012-12-18 14:30:41 +00008123void ASTReader::ReadTentativeDefinitions(
8124 SmallVectorImpl<VarDecl *> &TentativeDefs) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008125 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8126 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008127 if (Var)
8128 TentativeDefs.push_back(Var);
8129 }
8130 TentativeDefinitions.clear();
8131}
8132
8133void ASTReader::ReadUnusedFileScopedDecls(
8134 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008135 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8136 DeclaratorDecl *D
8137 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008138 if (D)
8139 Decls.push_back(D);
8140 }
8141 UnusedFileScopedDecls.clear();
8142}
8143
8144void ASTReader::ReadDelegatingConstructors(
8145 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008146 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8147 CXXConstructorDecl *D
8148 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008149 if (D)
8150 Decls.push_back(D);
8151 }
8152 DelegatingCtorDecls.clear();
8153}
8154
8155void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008156 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8157 TypedefNameDecl *D
8158 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008159 if (D)
8160 Decls.push_back(D);
8161 }
8162 ExtVectorDecls.clear();
8163}
8164
Nico Weber72889432014-09-06 01:25:55 +00008165void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8166 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008167 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8168 ++I) {
8169 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8170 GetDecl(UnusedLocalTypedefNameCandidates[I]));
Nico Weber72889432014-09-06 01:25:55 +00008171 if (D)
8172 Decls.insert(D);
8173 }
8174 UnusedLocalTypedefNameCandidates.clear();
8175}
8176
Guy Benyei11169dd2012-12-18 14:30:41 +00008177void ASTReader::ReadReferencedSelectors(
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008178 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008179 if (ReferencedSelectorsData.empty())
8180 return;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008181
Guy Benyei11169dd2012-12-18 14:30:41 +00008182 // If there are @selector references added them to its pool. This is for
8183 // implementation of -Wselector.
8184 unsigned int DataSize = ReferencedSelectorsData.size()-1;
8185 unsigned I = 0;
8186 while (I < DataSize) {
8187 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8188 SourceLocation SelLoc
8189 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8190 Sels.push_back(std::make_pair(Sel, SelLoc));
8191 }
8192 ReferencedSelectorsData.clear();
8193}
8194
8195void ASTReader::ReadWeakUndeclaredIdentifiers(
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008196 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008197 if (WeakUndeclaredIdentifiers.empty())
8198 return;
8199
8200 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008201 IdentifierInfo *WeakId
Guy Benyei11169dd2012-12-18 14:30:41 +00008202 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008203 IdentifierInfo *AliasId
Guy Benyei11169dd2012-12-18 14:30:41 +00008204 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8205 SourceLocation Loc
8206 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8207 bool Used = WeakUndeclaredIdentifiers[I++];
8208 WeakInfo WI(AliasId, Loc);
8209 WI.setUsed(Used);
8210 WeakIDs.push_back(std::make_pair(WeakId, WI));
8211 }
8212 WeakUndeclaredIdentifiers.clear();
8213}
8214
8215void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8216 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8217 ExternalVTableUse VT;
8218 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8219 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8220 VT.DefinitionRequired = VTableUses[Idx++];
8221 VTables.push_back(VT);
8222 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008223
Guy Benyei11169dd2012-12-18 14:30:41 +00008224 VTableUses.clear();
8225}
8226
8227void ASTReader::ReadPendingInstantiations(
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008228 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008229 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008230 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008231 SourceLocation Loc
8232 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8233
8234 Pending.push_back(std::make_pair(D, Loc));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008235 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008236 PendingInstantiations.clear();
8237}
8238
Richard Smithe40f2ba2013-08-07 21:41:30 +00008239void ASTReader::ReadLateParsedTemplates(
Justin Lebar28f09c52016-10-10 16:26:08 +00008240 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8241 &LPTMap) {
Richard Smithe40f2ba2013-08-07 21:41:30 +00008242 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8243 /* In loop */) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008244 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
Richard Smithe40f2ba2013-08-07 21:41:30 +00008245
Justin Lebar28f09c52016-10-10 16:26:08 +00008246 auto LT = llvm::make_unique<LateParsedTemplate>();
Richard Smithe40f2ba2013-08-07 21:41:30 +00008247 LT->D = GetDecl(LateParsedTemplates[Idx++]);
8248
8249 ModuleFile *F = getOwningModuleFile(LT->D);
8250 assert(F && "No module");
8251
8252 unsigned TokN = LateParsedTemplates[Idx++];
8253 LT->Toks.reserve(TokN);
8254 for (unsigned T = 0; T < TokN; ++T)
8255 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8256
Justin Lebar28f09c52016-10-10 16:26:08 +00008257 LPTMap.insert(std::make_pair(FD, std::move(LT)));
Richard Smithe40f2ba2013-08-07 21:41:30 +00008258 }
8259
8260 LateParsedTemplates.clear();
8261}
8262
Guy Benyei11169dd2012-12-18 14:30:41 +00008263void ASTReader::LoadSelector(Selector Sel) {
8264 // It would be complicated to avoid reading the methods anyway. So don't.
8265 ReadMethodPool(Sel);
8266}
8267
8268void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8269 assert(ID && "Non-zero identifier ID required");
8270 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8271 IdentifiersLoaded[ID - 1] = II;
8272 if (DeserializationListener)
8273 DeserializationListener->IdentifierRead(ID, II);
8274}
8275
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008276/// Set the globally-visible declarations associated with the given
Guy Benyei11169dd2012-12-18 14:30:41 +00008277/// identifier.
8278///
8279/// If the AST reader is currently in a state where the given declaration IDs
8280/// cannot safely be resolved, they are queued until it is safe to resolve
8281/// them.
8282///
8283/// \param II an IdentifierInfo that refers to one or more globally-visible
8284/// declarations.
8285///
8286/// \param DeclIDs the set of declaration IDs with the name @p II that are
8287/// visible at global scope.
8288///
Douglas Gregor6168bd22013-02-18 15:53:43 +00008289/// \param Decls if non-null, this vector will be populated with the set of
8290/// deserialized declarations. These declarations will not be pushed into
8291/// scope.
Guy Benyei11169dd2012-12-18 14:30:41 +00008292void
8293ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8294 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor6168bd22013-02-18 15:53:43 +00008295 SmallVectorImpl<Decl *> *Decls) {
8296 if (NumCurrentElementsDeserializing && !Decls) {
8297 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00008298 return;
8299 }
8300
Vedant Kumar48b4f762018-04-14 01:40:48 +00008301 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
Ben Langmuir5418f402014-09-10 21:29:41 +00008302 if (!SemaObj) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008303 // Queue this declaration so that it will be added to the
8304 // translation unit scope and identifier's declaration chain
8305 // once a Sema object is known.
Vedant Kumar48b4f762018-04-14 01:40:48 +00008306 PreloadedDeclIDs.push_back(DeclIDs[I]);
Ben Langmuir5418f402014-09-10 21:29:41 +00008307 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00008308 }
Ben Langmuir5418f402014-09-10 21:29:41 +00008309
Vedant Kumar48b4f762018-04-14 01:40:48 +00008310 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
Ben Langmuir5418f402014-09-10 21:29:41 +00008311
8312 // If we're simply supposed to record the declarations, do so now.
8313 if (Decls) {
8314 Decls->push_back(D);
8315 continue;
8316 }
8317
8318 // Introduce this declaration into the translation-unit scope
8319 // and add it to the declaration chain for this identifier, so
8320 // that (unqualified) name lookup will find it.
8321 pushExternalDeclIntoScope(D, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00008322 }
8323}
8324
Douglas Gregorc8a992f2013-01-21 16:52:34 +00008325IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008326 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00008327 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008328
8329 if (IdentifiersLoaded.empty()) {
8330 Error("no identifier table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00008331 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008332 }
8333
8334 ID -= 1;
8335 if (!IdentifiersLoaded[ID]) {
8336 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8337 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8338 ModuleFile *M = I->second;
8339 unsigned Index = ID - M->BaseIdentifierID;
8340 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8341
8342 // All of the strings in the AST file are preceded by a 16-bit length.
8343 // Extract that 16-bit length to avoid having to execute strlen().
8344 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8345 // unsigned integers. This is important to avoid integer overflow when
8346 // we cast them to 'unsigned'.
8347 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8348 unsigned StrLen = (((unsigned) StrLenPtr[0])
8349 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Richard Smitheb4b58f62016-02-05 01:40:54 +00008350 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8351 IdentifiersLoaded[ID] = &II;
8352 markIdentifierFromAST(*this, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00008353 if (DeserializationListener)
Richard Smitheb4b58f62016-02-05 01:40:54 +00008354 DeserializationListener->IdentifierRead(ID + 1, &II);
Guy Benyei11169dd2012-12-18 14:30:41 +00008355 }
8356
8357 return IdentifiersLoaded[ID];
8358}
8359
Douglas Gregorc8a992f2013-01-21 16:52:34 +00008360IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8361 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei11169dd2012-12-18 14:30:41 +00008362}
8363
8364IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8365 if (LocalID < NUM_PREDEF_IDENT_IDS)
8366 return LocalID;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008367
Richard Smith37a93df2017-02-18 00:32:02 +00008368 if (!M.ModuleOffsetMap.empty())
8369 ReadModuleOffsetMap(M);
8370
Guy Benyei11169dd2012-12-18 14:30:41 +00008371 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8372 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008373 assert(I != M.IdentifierRemap.end()
Guy Benyei11169dd2012-12-18 14:30:41 +00008374 && "Invalid index into identifier index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008375
Guy Benyei11169dd2012-12-18 14:30:41 +00008376 return LocalID + I->second;
8377}
8378
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008379MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008380 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00008381 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008382
8383 if (MacrosLoaded.empty()) {
8384 Error("no macro table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00008385 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008386 }
8387
8388 ID -= NUM_PREDEF_MACRO_IDS;
8389 if (!MacrosLoaded[ID]) {
8390 GlobalMacroMapType::iterator I
8391 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8392 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8393 ModuleFile *M = I->second;
8394 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008395 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008396
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008397 if (DeserializationListener)
8398 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8399 MacrosLoaded[ID]);
Guy Benyei11169dd2012-12-18 14:30:41 +00008400 }
8401
8402 return MacrosLoaded[ID];
8403}
8404
8405MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8406 if (LocalID < NUM_PREDEF_MACRO_IDS)
8407 return LocalID;
8408
Richard Smith37a93df2017-02-18 00:32:02 +00008409 if (!M.ModuleOffsetMap.empty())
8410 ReadModuleOffsetMap(M);
8411
Guy Benyei11169dd2012-12-18 14:30:41 +00008412 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8413 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8414 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8415
8416 return LocalID + I->second;
8417}
8418
8419serialization::SubmoduleID
8420ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8421 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8422 return LocalID;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008423
Richard Smith37a93df2017-02-18 00:32:02 +00008424 if (!M.ModuleOffsetMap.empty())
8425 ReadModuleOffsetMap(M);
8426
Guy Benyei11169dd2012-12-18 14:30:41 +00008427 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8428 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008429 assert(I != M.SubmoduleRemap.end()
Guy Benyei11169dd2012-12-18 14:30:41 +00008430 && "Invalid index into submodule index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008431
Guy Benyei11169dd2012-12-18 14:30:41 +00008432 return LocalID + I->second;
8433}
8434
8435Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8436 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8437 assert(GlobalID == 0 && "Unhandled global submodule ID");
Craig Toppera13603a2014-05-22 05:54:18 +00008438 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008439 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008440
Guy Benyei11169dd2012-12-18 14:30:41 +00008441 if (GlobalID > SubmodulesLoaded.size()) {
8442 Error("submodule ID out of range in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00008443 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008444 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008445
Guy Benyei11169dd2012-12-18 14:30:41 +00008446 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8447}
Douglas Gregorc147b0b2013-01-12 01:29:50 +00008448
8449Module *ASTReader::getModule(unsigned ID) {
8450 return getSubmodule(ID);
8451}
8452
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00008453bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8454 ModuleFile *MF = getOwningModuleFile(D);
8455 return MF && MF->PCHHasObjectFile;
8456}
8457
Richard Smithd88a7f12015-09-01 20:35:42 +00008458ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8459 if (ID & 1) {
8460 // It's a module, look it up by submodule ID.
8461 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8462 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8463 } else {
8464 // It's a prefix (preamble, PCH, ...). Look it up by index.
8465 unsigned IndexFromEnd = ID >> 1;
8466 assert(IndexFromEnd && "got reference to unknown module file");
8467 return getModuleManager().pch_modules().end()[-IndexFromEnd];
8468 }
8469}
8470
8471unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8472 if (!F)
8473 return 1;
8474
8475 // For a file representing a module, use the submodule ID of the top-level
8476 // module as the file ID. For any other kind of file, the number of such
8477 // files loaded beforehand will be the same on reload.
8478 // FIXME: Is this true even if we have an explicit module file and a PCH?
8479 if (F->isModule())
8480 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8481
8482 auto PCHModules = getModuleManager().pch_modules();
8483 auto I = std::find(PCHModules.begin(), PCHModules.end(), F);
8484 assert(I != PCHModules.end() && "emitting reference to unknown file");
8485 return (I - PCHModules.end()) << 1;
8486}
8487
Adrian Prantl15bcf702015-06-30 17:39:43 +00008488llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
8489ASTReader::getSourceDescriptor(unsigned ID) {
8490 if (const Module *M = getSubmodule(ID))
Adrian Prantlc6458d62015-09-19 00:10:32 +00008491 return ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl15bcf702015-06-30 17:39:43 +00008492
8493 // If there is only a single PCH, return it instead.
Hiroshi Inoue3170de02017-07-01 08:46:43 +00008494 // Chained PCH are not supported.
Saleem Abdulrasool97d25552017-03-02 17:37:11 +00008495 const auto &PCHChain = ModuleMgr.pch_modules();
8496 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
Adrian Prantl15bcf702015-06-30 17:39:43 +00008497 ModuleFile &MF = ModuleMgr.getPrimaryModule();
Adrian Prantl3a2d4942016-01-22 23:30:56 +00008498 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
Adrian Prantl9bc3c4f2016-04-27 17:06:22 +00008499 StringRef FileName = llvm::sys::path::filename(MF.FileName);
8500 return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8501 MF.Signature);
Adrian Prantl15bcf702015-06-30 17:39:43 +00008502 }
8503 return None;
8504}
8505
David Blaikie1ac9c982017-04-11 21:13:37 +00008506ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
Richard Smitha4653622017-09-06 20:01:14 +00008507 auto I = DefinitionSource.find(FD);
8508 if (I == DefinitionSource.end())
David Blaikie9ffe5a32017-01-30 05:00:26 +00008509 return EK_ReplyHazy;
David Blaikiee6b7c282017-04-11 20:46:34 +00008510 return I->second ? EK_Never : EK_Always;
David Blaikie9ffe5a32017-01-30 05:00:26 +00008511}
8512
Guy Benyei11169dd2012-12-18 14:30:41 +00008513Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8514 return DecodeSelector(getGlobalSelectorID(M, LocalID));
8515}
8516
8517Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8518 if (ID == 0)
Vedant Kumar48b4f762018-04-14 01:40:48 +00008519 return Selector();
Guy Benyei11169dd2012-12-18 14:30:41 +00008520
8521 if (ID > SelectorsLoaded.size()) {
8522 Error("selector ID out of range in AST file");
Vedant Kumar48b4f762018-04-14 01:40:48 +00008523 return Selector();
Guy Benyei11169dd2012-12-18 14:30:41 +00008524 }
8525
Craig Toppera13603a2014-05-22 05:54:18 +00008526 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008527 // Load this selector from the selector table.
8528 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8529 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8530 ModuleFile &M = *I->second;
8531 ASTSelectorLookupTrait Trait(*this, M);
8532 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8533 SelectorsLoaded[ID - 1] =
8534 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8535 if (DeserializationListener)
8536 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8537 }
8538
8539 return SelectorsLoaded[ID - 1];
8540}
8541
8542Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8543 return DecodeSelector(ID);
8544}
8545
8546uint32_t ASTReader::GetNumExternalSelectors() {
8547 // ID 0 (the null selector) is considered an external selector.
8548 return getTotalNumSelectors() + 1;
8549}
8550
8551serialization::SelectorID
8552ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8553 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8554 return LocalID;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008555
Richard Smith37a93df2017-02-18 00:32:02 +00008556 if (!M.ModuleOffsetMap.empty())
8557 ReadModuleOffsetMap(M);
8558
Guy Benyei11169dd2012-12-18 14:30:41 +00008559 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8560 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008561 assert(I != M.SelectorRemap.end()
Guy Benyei11169dd2012-12-18 14:30:41 +00008562 && "Invalid index into selector index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008563
Guy Benyei11169dd2012-12-18 14:30:41 +00008564 return LocalID + I->second;
8565}
8566
8567DeclarationName
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008568ASTReader::ReadDeclarationName(ModuleFile &F,
Guy Benyei11169dd2012-12-18 14:30:41 +00008569 const RecordData &Record, unsigned &Idx) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00008570 ASTContext &Context = getContext();
Vedant Kumar48b4f762018-04-14 01:40:48 +00008571 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00008572 switch (Kind) {
8573 case DeclarationName::Identifier:
Douglas Gregorc8a992f2013-01-21 16:52:34 +00008574 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00008575
8576 case DeclarationName::ObjCZeroArgSelector:
8577 case DeclarationName::ObjCOneArgSelector:
8578 case DeclarationName::ObjCMultiArgSelector:
8579 return DeclarationName(ReadSelector(F, Record, Idx));
8580
8581 case DeclarationName::CXXConstructorName:
8582 return Context.DeclarationNames.getCXXConstructorName(
8583 Context.getCanonicalType(readType(F, Record, Idx)));
8584
8585 case DeclarationName::CXXDestructorName:
8586 return Context.DeclarationNames.getCXXDestructorName(
8587 Context.getCanonicalType(readType(F, Record, Idx)));
8588
Richard Smith35845152017-02-07 01:37:30 +00008589 case DeclarationName::CXXDeductionGuideName:
8590 return Context.DeclarationNames.getCXXDeductionGuideName(
8591 ReadDeclAs<TemplateDecl>(F, Record, Idx));
8592
Guy Benyei11169dd2012-12-18 14:30:41 +00008593 case DeclarationName::CXXConversionFunctionName:
8594 return Context.DeclarationNames.getCXXConversionFunctionName(
8595 Context.getCanonicalType(readType(F, Record, Idx)));
8596
8597 case DeclarationName::CXXOperatorName:
8598 return Context.DeclarationNames.getCXXOperatorName(
8599 (OverloadedOperatorKind)Record[Idx++]);
8600
8601 case DeclarationName::CXXLiteralOperatorName:
8602 return Context.DeclarationNames.getCXXLiteralOperatorName(
8603 GetIdentifierInfo(F, Record, Idx));
8604
8605 case DeclarationName::CXXUsingDirective:
8606 return DeclarationName::getUsingDirectiveName();
8607 }
8608
8609 llvm_unreachable("Invalid NameKind!");
8610}
8611
8612void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
8613 DeclarationNameLoc &DNLoc,
8614 DeclarationName Name,
8615 const RecordData &Record, unsigned &Idx) {
8616 switch (Name.getNameKind()) {
8617 case DeclarationName::CXXConstructorName:
8618 case DeclarationName::CXXDestructorName:
8619 case DeclarationName::CXXConversionFunctionName:
8620 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
8621 break;
8622
8623 case DeclarationName::CXXOperatorName:
8624 DNLoc.CXXOperatorName.BeginOpNameLoc
8625 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
8626 DNLoc.CXXOperatorName.EndOpNameLoc
8627 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
8628 break;
8629
8630 case DeclarationName::CXXLiteralOperatorName:
8631 DNLoc.CXXLiteralOperatorName.OpNameLoc
8632 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
8633 break;
8634
8635 case DeclarationName::Identifier:
8636 case DeclarationName::ObjCZeroArgSelector:
8637 case DeclarationName::ObjCOneArgSelector:
8638 case DeclarationName::ObjCMultiArgSelector:
8639 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00008640 case DeclarationName::CXXDeductionGuideName:
Guy Benyei11169dd2012-12-18 14:30:41 +00008641 break;
8642 }
8643}
8644
8645void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
8646 DeclarationNameInfo &NameInfo,
8647 const RecordData &Record, unsigned &Idx) {
8648 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
8649 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
8650 DeclarationNameLoc DNLoc;
8651 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
8652 NameInfo.setInfo(DNLoc);
8653}
8654
8655void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
8656 const RecordData &Record, unsigned &Idx) {
8657 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
8658 unsigned NumTPLists = Record[Idx++];
8659 Info.NumTemplParamLists = NumTPLists;
8660 if (NumTPLists) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00008661 Info.TemplParamLists =
8662 new (getContext()) TemplateParameterList *[NumTPLists];
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00008663 for (unsigned i = 0; i != NumTPLists; ++i)
Guy Benyei11169dd2012-12-18 14:30:41 +00008664 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
8665 }
8666}
8667
8668TemplateName
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008669ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Guy Benyei11169dd2012-12-18 14:30:41 +00008670 unsigned &Idx) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00008671 ASTContext &Context = getContext();
Vedant Kumar48b4f762018-04-14 01:40:48 +00008672 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00008673 switch (Kind) {
8674 case TemplateName::Template:
8675 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
8676
8677 case TemplateName::OverloadedTemplate: {
8678 unsigned size = Record[Idx++];
8679 UnresolvedSet<8> Decls;
8680 while (size--)
8681 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
8682
8683 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
8684 }
8685
8686 case TemplateName::QualifiedTemplate: {
8687 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
8688 bool hasTemplKeyword = Record[Idx++];
Vedant Kumar48b4f762018-04-14 01:40:48 +00008689 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00008690 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
8691 }
8692
8693 case TemplateName::DependentTemplate: {
8694 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
8695 if (Record[Idx++]) // isIdentifier
8696 return Context.getDependentTemplateName(NNS,
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008697 GetIdentifierInfo(F, Record,
Guy Benyei11169dd2012-12-18 14:30:41 +00008698 Idx));
8699 return Context.getDependentTemplateName(NNS,
8700 (OverloadedOperatorKind)Record[Idx++]);
8701 }
8702
8703 case TemplateName::SubstTemplateTemplateParm: {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008704 TemplateTemplateParmDecl *param
8705 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
8706 if (!param) return TemplateName();
8707 TemplateName replacement = ReadTemplateName(F, Record, Idx);
8708 return Context.getSubstTemplateTemplateParm(param, replacement);
Guy Benyei11169dd2012-12-18 14:30:41 +00008709 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008710
Guy Benyei11169dd2012-12-18 14:30:41 +00008711 case TemplateName::SubstTemplateTemplateParmPack: {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008712 TemplateTemplateParmDecl *Param
8713 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00008714 if (!Param)
Vedant Kumar48b4f762018-04-14 01:40:48 +00008715 return TemplateName();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008716
Guy Benyei11169dd2012-12-18 14:30:41 +00008717 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
8718 if (ArgPack.getKind() != TemplateArgument::Pack)
Vedant Kumar48b4f762018-04-14 01:40:48 +00008719 return TemplateName();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008720
Guy Benyei11169dd2012-12-18 14:30:41 +00008721 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
8722 }
8723 }
8724
8725 llvm_unreachable("Unhandled template name kind!");
8726}
8727
Richard Smith2bb3c342015-08-09 01:05:31 +00008728TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F,
8729 const RecordData &Record,
8730 unsigned &Idx,
8731 bool Canonicalize) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00008732 ASTContext &Context = getContext();
Richard Smith2bb3c342015-08-09 01:05:31 +00008733 if (Canonicalize) {
8734 // The caller wants a canonical template argument. Sometimes the AST only
8735 // wants template arguments in canonical form (particularly as the template
8736 // argument lists of template specializations) so ensure we preserve that
8737 // canonical form across serialization.
8738 TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false);
8739 return Context.getCanonicalTemplateArgument(Arg);
8740 }
8741
Vedant Kumar48b4f762018-04-14 01:40:48 +00008742 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00008743 switch (Kind) {
8744 case TemplateArgument::Null:
Vedant Kumar48b4f762018-04-14 01:40:48 +00008745 return TemplateArgument();
Guy Benyei11169dd2012-12-18 14:30:41 +00008746 case TemplateArgument::Type:
8747 return TemplateArgument(readType(F, Record, Idx));
8748 case TemplateArgument::Declaration: {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008749 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
David Blaikie0f62c8d2014-10-16 04:21:25 +00008750 return TemplateArgument(D, readType(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00008751 }
8752 case TemplateArgument::NullPtr:
8753 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
8754 case TemplateArgument::Integral: {
8755 llvm::APSInt Value = ReadAPSInt(Record, Idx);
8756 QualType T = readType(F, Record, Idx);
8757 return TemplateArgument(Context, Value, T);
8758 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008759 case TemplateArgument::Template:
Guy Benyei11169dd2012-12-18 14:30:41 +00008760 return TemplateArgument(ReadTemplateName(F, Record, Idx));
8761 case TemplateArgument::TemplateExpansion: {
8762 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikie05785d12013-02-20 22:23:23 +00008763 Optional<unsigned> NumTemplateExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00008764 if (unsigned NumExpansions = Record[Idx++])
8765 NumTemplateExpansions = NumExpansions - 1;
8766 return TemplateArgument(Name, NumTemplateExpansions);
8767 }
8768 case TemplateArgument::Expression:
8769 return TemplateArgument(ReadExpr(F));
8770 case TemplateArgument::Pack: {
8771 unsigned NumArgs = Record[Idx++];
Vedant Kumar48b4f762018-04-14 01:40:48 +00008772 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Guy Benyei11169dd2012-12-18 14:30:41 +00008773 for (unsigned I = 0; I != NumArgs; ++I)
8774 Args[I] = ReadTemplateArgument(F, Record, Idx);
Benjamin Kramercce63472015-08-05 09:40:22 +00008775 return TemplateArgument(llvm::makeArrayRef(Args, NumArgs));
Guy Benyei11169dd2012-12-18 14:30:41 +00008776 }
8777 }
8778
8779 llvm_unreachable("Unhandled template argument kind!");
8780}
8781
8782TemplateParameterList *
8783ASTReader::ReadTemplateParameterList(ModuleFile &F,
8784 const RecordData &Record, unsigned &Idx) {
8785 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
8786 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
8787 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
8788
8789 unsigned NumParams = Record[Idx++];
8790 SmallVector<NamedDecl *, 16> Params;
8791 Params.reserve(NumParams);
8792 while (NumParams--)
8793 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
8794
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00008795 // TODO: Concepts
Richard Smithdbafb6c2017-06-29 23:23:46 +00008796 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8797 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00008798 return TemplateParams;
8799}
8800
8801void
8802ASTReader::
Craig Topper5603df42013-07-05 19:34:19 +00008803ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
Guy Benyei11169dd2012-12-18 14:30:41 +00008804 ModuleFile &F, const RecordData &Record,
Richard Smith2bb3c342015-08-09 01:05:31 +00008805 unsigned &Idx, bool Canonicalize) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008806 unsigned NumTemplateArgs = Record[Idx++];
8807 TemplArgs.reserve(NumTemplateArgs);
8808 while (NumTemplateArgs--)
Richard Smith2bb3c342015-08-09 01:05:31 +00008809 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize));
Guy Benyei11169dd2012-12-18 14:30:41 +00008810}
8811
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008812/// Read a UnresolvedSet structure.
Richard Smitha4ba74c2013-08-30 04:46:40 +00008813void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
Guy Benyei11169dd2012-12-18 14:30:41 +00008814 const RecordData &Record, unsigned &Idx) {
8815 unsigned NumDecls = Record[Idx++];
Richard Smithdbafb6c2017-06-29 23:23:46 +00008816 Set.reserve(getContext(), NumDecls);
Guy Benyei11169dd2012-12-18 14:30:41 +00008817 while (NumDecls--) {
Richard Smitha4ba74c2013-08-30 04:46:40 +00008818 DeclID ID = ReadDeclID(F, Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00008819 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
Richard Smithdbafb6c2017-06-29 23:23:46 +00008820 Set.addLazyDecl(getContext(), ID, AS);
Guy Benyei11169dd2012-12-18 14:30:41 +00008821 }
8822}
8823
8824CXXBaseSpecifier
8825ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
8826 const RecordData &Record, unsigned &Idx) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008827 bool isVirtual = static_cast<bool>(Record[Idx++]);
8828 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
8829 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
8830 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00008831 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
8832 SourceRange Range = ReadSourceRange(F, Record, Idx);
8833 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008834 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Guy Benyei11169dd2012-12-18 14:30:41 +00008835 EllipsisLoc);
8836 Result.setInheritConstructors(inheritConstructors);
8837 return Result;
8838}
8839
Richard Smithc2bb8182015-03-24 06:36:48 +00008840CXXCtorInitializer **
Guy Benyei11169dd2012-12-18 14:30:41 +00008841ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
8842 unsigned &Idx) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00008843 ASTContext &Context = getContext();
Guy Benyei11169dd2012-12-18 14:30:41 +00008844 unsigned NumInitializers = Record[Idx++];
Richard Smithc2bb8182015-03-24 06:36:48 +00008845 assert(NumInitializers && "wrote ctor initializers but have no inits");
Vedant Kumar48b4f762018-04-14 01:40:48 +00008846 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
Richard Smithc2bb8182015-03-24 06:36:48 +00008847 for (unsigned i = 0; i != NumInitializers; ++i) {
8848 TypeSourceInfo *TInfo = nullptr;
8849 bool IsBaseVirtual = false;
8850 FieldDecl *Member = nullptr;
8851 IndirectFieldDecl *IndirectMember = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008852
Vedant Kumar48b4f762018-04-14 01:40:48 +00008853 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
Richard Smithc2bb8182015-03-24 06:36:48 +00008854 switch (Type) {
8855 case CTOR_INITIALIZER_BASE:
8856 TInfo = GetTypeSourceInfo(F, Record, Idx);
8857 IsBaseVirtual = Record[Idx++];
8858 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008859
Richard Smithc2bb8182015-03-24 06:36:48 +00008860 case CTOR_INITIALIZER_DELEGATING:
8861 TInfo = GetTypeSourceInfo(F, Record, Idx);
8862 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008863
Richard Smithc2bb8182015-03-24 06:36:48 +00008864 case CTOR_INITIALIZER_MEMBER:
8865 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
8866 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008867
Richard Smithc2bb8182015-03-24 06:36:48 +00008868 case CTOR_INITIALIZER_INDIRECT_MEMBER:
8869 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
8870 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008871 }
Richard Smithc2bb8182015-03-24 06:36:48 +00008872
8873 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
8874 Expr *Init = ReadExpr(F);
8875 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
8876 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Richard Smithc2bb8182015-03-24 06:36:48 +00008877
8878 CXXCtorInitializer *BOMInit;
Richard Smith30e304e2016-12-14 00:03:17 +00008879 if (Type == CTOR_INITIALIZER_BASE)
Richard Smithc2bb8182015-03-24 06:36:48 +00008880 BOMInit = new (Context)
8881 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8882 RParenLoc, MemberOrEllipsisLoc);
Richard Smith30e304e2016-12-14 00:03:17 +00008883 else if (Type == CTOR_INITIALIZER_DELEGATING)
Richard Smithc2bb8182015-03-24 06:36:48 +00008884 BOMInit = new (Context)
8885 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
Richard Smith30e304e2016-12-14 00:03:17 +00008886 else if (Member)
8887 BOMInit = new (Context)
8888 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8889 Init, RParenLoc);
8890 else
8891 BOMInit = new (Context)
8892 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8893 LParenLoc, Init, RParenLoc);
8894
Richard Smith418ed822016-12-14 19:45:03 +00008895 if (/*IsWritten*/Record[Idx++]) {
Richard Smith30e304e2016-12-14 00:03:17 +00008896 unsigned SourceOrder = Record[Idx++];
8897 BOMInit->setSourceOrder(SourceOrder);
Richard Smithc2bb8182015-03-24 06:36:48 +00008898 }
8899
Richard Smithc2bb8182015-03-24 06:36:48 +00008900 CtorInitializers[i] = BOMInit;
Guy Benyei11169dd2012-12-18 14:30:41 +00008901 }
8902
Richard Smithc2bb8182015-03-24 06:36:48 +00008903 return CtorInitializers;
Guy Benyei11169dd2012-12-18 14:30:41 +00008904}
8905
8906NestedNameSpecifier *
8907ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
8908 const RecordData &Record, unsigned &Idx) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00008909 ASTContext &Context = getContext();
Guy Benyei11169dd2012-12-18 14:30:41 +00008910 unsigned N = Record[Idx++];
Craig Toppera13603a2014-05-22 05:54:18 +00008911 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008912 for (unsigned I = 0; I != N; ++I) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008913 NestedNameSpecifier::SpecifierKind Kind
8914 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00008915 switch (Kind) {
8916 case NestedNameSpecifier::Identifier: {
8917 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
8918 NNS = NestedNameSpecifier::Create(Context, Prev, II);
8919 break;
8920 }
8921
8922 case NestedNameSpecifier::Namespace: {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008923 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00008924 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
8925 break;
8926 }
8927
8928 case NestedNameSpecifier::NamespaceAlias: {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008929 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00008930 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
8931 break;
8932 }
8933
8934 case NestedNameSpecifier::TypeSpec:
8935 case NestedNameSpecifier::TypeSpecWithTemplate: {
8936 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
8937 if (!T)
Craig Toppera13603a2014-05-22 05:54:18 +00008938 return nullptr;
8939
Guy Benyei11169dd2012-12-18 14:30:41 +00008940 bool Template = Record[Idx++];
8941 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
8942 break;
8943 }
8944
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008945 case NestedNameSpecifier::Global:
Guy Benyei11169dd2012-12-18 14:30:41 +00008946 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
8947 // No associated value, and there can't be a prefix.
8948 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00008949
8950 case NestedNameSpecifier::Super: {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008951 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
Nikola Smiljanic67860242014-09-26 00:28:20 +00008952 NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
8953 break;
8954 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008955 }
8956 Prev = NNS;
8957 }
8958 return NNS;
8959}
8960
8961NestedNameSpecifierLoc
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008962ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Guy Benyei11169dd2012-12-18 14:30:41 +00008963 unsigned &Idx) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00008964 ASTContext &Context = getContext();
Guy Benyei11169dd2012-12-18 14:30:41 +00008965 unsigned N = Record[Idx++];
8966 NestedNameSpecifierLocBuilder Builder;
8967 for (unsigned I = 0; I != N; ++I) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008968 NestedNameSpecifier::SpecifierKind Kind
8969 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00008970 switch (Kind) {
8971 case NestedNameSpecifier::Identifier: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008972 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00008973 SourceRange Range = ReadSourceRange(F, Record, Idx);
8974 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8975 break;
8976 }
8977
8978 case NestedNameSpecifier::Namespace: {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008979 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00008980 SourceRange Range = ReadSourceRange(F, Record, Idx);
8981 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8982 break;
8983 }
8984
8985 case NestedNameSpecifier::NamespaceAlias: {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008986 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00008987 SourceRange Range = ReadSourceRange(F, Record, Idx);
8988 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8989 break;
8990 }
8991
8992 case NestedNameSpecifier::TypeSpec:
8993 case NestedNameSpecifier::TypeSpecWithTemplate: {
8994 bool Template = Record[Idx++];
8995 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
8996 if (!T)
Vedant Kumar48b4f762018-04-14 01:40:48 +00008997 return NestedNameSpecifierLoc();
Guy Benyei11169dd2012-12-18 14:30:41 +00008998 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
8999
9000 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00009001 Builder.Extend(Context,
Guy Benyei11169dd2012-12-18 14:30:41 +00009002 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
9003 T->getTypeLoc(), ColonColonLoc);
9004 break;
9005 }
9006
9007 case NestedNameSpecifier::Global: {
9008 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
9009 Builder.MakeGlobal(Context, ColonColonLoc);
9010 break;
9011 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00009012
9013 case NestedNameSpecifier::Super: {
Vedant Kumar48b4f762018-04-14 01:40:48 +00009014 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
Nikola Smiljanic67860242014-09-26 00:28:20 +00009015 SourceRange Range = ReadSourceRange(F, Record, Idx);
9016 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
9017 break;
9018 }
Guy Benyei11169dd2012-12-18 14:30:41 +00009019 }
9020 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00009021
Guy Benyei11169dd2012-12-18 14:30:41 +00009022 return Builder.getWithLocInContext(Context);
9023}
9024
9025SourceRange
9026ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
9027 unsigned &Idx) {
9028 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
9029 SourceLocation end = ReadSourceLocation(F, Record, Idx);
9030 return SourceRange(beg, end);
9031}
9032
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009033/// Read an integral value
Guy Benyei11169dd2012-12-18 14:30:41 +00009034llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
9035 unsigned BitWidth = Record[Idx++];
9036 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
9037 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
9038 Idx += NumWords;
9039 return Result;
9040}
9041
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009042/// Read a signed integral value
Guy Benyei11169dd2012-12-18 14:30:41 +00009043llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
9044 bool isUnsigned = Record[Idx++];
9045 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
9046}
9047
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009048/// Read a floating-point value
Tim Northover178723a2013-01-22 09:46:51 +00009049llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
9050 const llvm::fltSemantics &Sem,
9051 unsigned &Idx) {
9052 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00009053}
9054
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009055// Read a string
Guy Benyei11169dd2012-12-18 14:30:41 +00009056std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9057 unsigned Len = Record[Idx++];
9058 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9059 Idx += Len;
9060 return Result;
9061}
9062
Richard Smith7ed1bc92014-12-05 22:42:13 +00009063std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9064 unsigned &Idx) {
9065 std::string Filename = ReadString(Record, Idx);
9066 ResolveImportedPath(F, Filename);
9067 return Filename;
9068}
9069
David L. Jonesc4808b9e2016-12-15 20:53:26 +00009070VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
Guy Benyei11169dd2012-12-18 14:30:41 +00009071 unsigned &Idx) {
9072 unsigned Major = Record[Idx++];
9073 unsigned Minor = Record[Idx++];
9074 unsigned Subminor = Record[Idx++];
9075 if (Minor == 0)
9076 return VersionTuple(Major);
9077 if (Subminor == 0)
9078 return VersionTuple(Major, Minor - 1);
9079 return VersionTuple(Major, Minor - 1, Subminor - 1);
9080}
9081
David L. Jonesc4808b9e2016-12-15 20:53:26 +00009082CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Guy Benyei11169dd2012-12-18 14:30:41 +00009083 const RecordData &Record,
9084 unsigned &Idx) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00009085 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Richard Smithdbafb6c2017-06-29 23:23:46 +00009086 return CXXTemporary::Create(getContext(), Decl);
Guy Benyei11169dd2012-12-18 14:30:41 +00009087}
9088
Richard Smith37a93df2017-02-18 00:32:02 +00009089DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00009090 return Diag(CurrentImportLoc, DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00009091}
9092
Richard Smith37a93df2017-02-18 00:32:02 +00009093DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
Guy Benyei11169dd2012-12-18 14:30:41 +00009094 return Diags.Report(Loc, DiagID);
9095}
9096
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009097/// Retrieve the identifier table associated with the
Guy Benyei11169dd2012-12-18 14:30:41 +00009098/// preprocessor.
9099IdentifierTable &ASTReader::getIdentifierTable() {
9100 return PP.getIdentifierTable();
9101}
9102
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009103/// Record that the given ID maps to the given switch-case
Guy Benyei11169dd2012-12-18 14:30:41 +00009104/// statement.
9105void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00009106 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
Guy Benyei11169dd2012-12-18 14:30:41 +00009107 "Already have a SwitchCase with this ID");
9108 (*CurrSwitchCaseStmts)[ID] = SC;
9109}
9110
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009111/// Retrieve the switch-case statement with the given ID.
Guy Benyei11169dd2012-12-18 14:30:41 +00009112SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00009113 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
Guy Benyei11169dd2012-12-18 14:30:41 +00009114 return (*CurrSwitchCaseStmts)[ID];
9115}
9116
9117void ASTReader::ClearSwitchCaseIDs() {
9118 CurrSwitchCaseStmts->clear();
9119}
9120
9121void ASTReader::ReadComments() {
Richard Smithdbafb6c2017-06-29 23:23:46 +00009122 ASTContext &Context = getContext();
Guy Benyei11169dd2012-12-18 14:30:41 +00009123 std::vector<RawComment *> Comments;
Vedant Kumar48b4f762018-04-14 01:40:48 +00009124 for (SmallVectorImpl<std::pair<BitstreamCursor,
9125 serialization::ModuleFile *>>::iterator
9126 I = CommentsCursors.begin(),
9127 E = CommentsCursors.end();
9128 I != E; ++I) {
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00009129 Comments.clear();
Vedant Kumar48b4f762018-04-14 01:40:48 +00009130 BitstreamCursor &Cursor = I->first;
9131 serialization::ModuleFile &F = *I->second;
Guy Benyei11169dd2012-12-18 14:30:41 +00009132 SavedStreamPosition SavedPosition(Cursor);
9133
9134 RecordData Record;
9135 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00009136 llvm::BitstreamEntry Entry =
9137 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00009138
Chris Lattner7fb3bef2013-01-20 00:56:42 +00009139 switch (Entry.Kind) {
9140 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9141 case llvm::BitstreamEntry::Error:
9142 Error("malformed block record in AST file");
9143 return;
9144 case llvm::BitstreamEntry::EndBlock:
9145 goto NextCursor;
9146 case llvm::BitstreamEntry::Record:
9147 // The interesting case.
Guy Benyei11169dd2012-12-18 14:30:41 +00009148 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00009149 }
9150
9151 // Read a record.
9152 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00009153 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00009154 case COMMENTS_RAW_COMMENT: {
9155 unsigned Idx = 0;
9156 SourceRange SR = ReadSourceRange(F, Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00009157 RawComment::CommentKind Kind =
9158 (RawComment::CommentKind) Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00009159 bool IsTrailingComment = Record[Idx++];
9160 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00009161 Comments.push_back(new (Context) RawComment(
David L. Jones13d5a872018-03-02 00:07:45 +00009162 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
Guy Benyei11169dd2012-12-18 14:30:41 +00009163 break;
9164 }
9165 }
9166 }
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00009167 NextCursor:
Bruno Cardoso Lopesc23af572016-12-19 21:06:06 +00009168 // De-serialized SourceLocations get negative FileIDs for other modules,
9169 // potentially invalidating the original order. Sort it again.
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00009170 llvm::sort(Comments.begin(), Comments.end(),
9171 BeforeThanCompare<RawComment>(SourceMgr));
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00009172 Context.Comments.addDeserializedComments(Comments);
Guy Benyei11169dd2012-12-18 14:30:41 +00009173 }
Guy Benyei11169dd2012-12-18 14:30:41 +00009174}
9175
Argyrios Kyrtzidisa38cb202017-01-30 06:05:58 +00009176void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9177 bool IncludeSystem, bool Complain,
9178 llvm::function_ref<void(const serialization::InputFile &IF,
9179 bool isSystem)> Visitor) {
9180 unsigned NumUserInputs = MF.NumUserInputFiles;
9181 unsigned NumInputs = MF.InputFilesLoaded.size();
9182 assert(NumUserInputs <= NumInputs);
9183 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9184 for (unsigned I = 0; I < N; ++I) {
9185 bool IsSystem = I >= NumUserInputs;
9186 InputFile IF = getInputFile(MF, I+1, Complain);
9187 Visitor(IF, IsSystem);
9188 }
9189}
9190
Richard Smithf3f84612017-06-29 02:19:42 +00009191void ASTReader::visitTopLevelModuleMaps(
9192 serialization::ModuleFile &MF,
9193 llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9194 unsigned NumInputs = MF.InputFilesLoaded.size();
9195 for (unsigned I = 0; I < NumInputs; ++I) {
9196 InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9197 if (IFI.TopLevelModuleMap)
9198 // FIXME: This unnecessarily re-reads the InputFileInfo.
9199 if (auto *FE = getInputFile(MF, I + 1).getFile())
9200 Visitor(FE);
9201 }
9202}
9203
Richard Smithcd45dbc2014-04-19 03:48:30 +00009204std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9205 // If we know the owning module, use it.
Richard Smith42413142015-05-15 20:05:43 +00009206 if (Module *M = D->getImportedOwningModule())
Richard Smithcd45dbc2014-04-19 03:48:30 +00009207 return M->getFullModuleName();
9208
9209 // Otherwise, use the name of the top-level module the decl is within.
9210 if (ModuleFile *M = getOwningModuleFile(D))
9211 return M->ModuleName;
9212
9213 // Not from a module.
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00009214 return {};
Richard Smithcd45dbc2014-04-19 03:48:30 +00009215}
9216
Guy Benyei11169dd2012-12-18 14:30:41 +00009217void ASTReader::finishPendingActions() {
Richard Smith851072e2014-05-19 20:59:20 +00009218 while (!PendingIdentifierInfos.empty() ||
9219 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
Richard Smith2b9e3e32013-10-18 06:05:18 +00009220 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
Richard Smitha0ce9c42014-07-29 23:23:27 +00009221 !PendingUpdateRecords.empty()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00009222 // If any identifiers with corresponding top-level declarations have
9223 // been loaded, load those declarations now.
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00009224 using TopLevelDeclsMap =
9225 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
Craig Topper79be4cd2013-07-05 04:33:53 +00009226 TopLevelDeclsMap TopLevelDecls;
9227
Guy Benyei11169dd2012-12-18 14:30:41 +00009228 while (!PendingIdentifierInfos.empty()) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00009229 IdentifierInfo *II = PendingIdentifierInfos.back().first;
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00009230 SmallVector<uint32_t, 4> DeclIDs =
9231 std::move(PendingIdentifierInfos.back().second);
Douglas Gregorcb15f082013-02-19 18:26:28 +00009232 PendingIdentifierInfos.pop_back();
Douglas Gregor6168bd22013-02-18 15:53:43 +00009233
9234 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei11169dd2012-12-18 14:30:41 +00009235 }
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00009236
Richard Smith851072e2014-05-19 20:59:20 +00009237 // For each decl chain that we wanted to complete while deserializing, mark
9238 // it as "still needs to be completed".
Vedant Kumar48b4f762018-04-14 01:40:48 +00009239 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9240 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9241 }
Richard Smith851072e2014-05-19 20:59:20 +00009242 PendingIncompleteDeclChains.clear();
9243
Guy Benyei11169dd2012-12-18 14:30:41 +00009244 // Load pending declaration chains.
Vedant Kumar48b4f762018-04-14 01:40:48 +00009245 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9246 loadPendingDeclChain(PendingDeclChains[I].first, PendingDeclChains[I].second);
Guy Benyei11169dd2012-12-18 14:30:41 +00009247 PendingDeclChains.clear();
9248
Douglas Gregor6168bd22013-02-18 15:53:43 +00009249 // Make the most recent of the top-level declarations visible.
Vedant Kumar48b4f762018-04-14 01:40:48 +00009250 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9251 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9252 IdentifierInfo *II = TLD->first;
9253 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9254 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregor6168bd22013-02-18 15:53:43 +00009255 }
9256 }
9257
Guy Benyei11169dd2012-12-18 14:30:41 +00009258 // Load any pending macro definitions.
9259 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009260 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9261 SmallVector<PendingMacroInfo, 2> GlobalIDs;
9262 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9263 // Initialize the macro history from chained-PCHs ahead of module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00009264 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00009265 ++IDIdx) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009266 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Manman Ren11f2a472016-08-18 17:42:15 +00009267 if (!Info.M->isModule())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009268 resolvePendingMacro(II, Info);
9269 }
9270 // Handle module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00009271 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009272 ++IDIdx) {
9273 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Manman Ren11f2a472016-08-18 17:42:15 +00009274 if (Info.M->isModule())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009275 resolvePendingMacro(II, Info);
Guy Benyei11169dd2012-12-18 14:30:41 +00009276 }
9277 }
9278 PendingMacroIDs.clear();
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00009279
9280 // Wire up the DeclContexts for Decls that we delayed setting until
9281 // recursive loading is completed.
9282 while (!PendingDeclContextInfos.empty()) {
9283 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9284 PendingDeclContextInfos.pop_front();
Vedant Kumar48b4f762018-04-14 01:40:48 +00009285 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9286 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00009287 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9288 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00009289
Richard Smithd1c46742014-04-30 02:24:17 +00009290 // Perform any pending declaration updates.
Richard Smithd6db68c2014-08-07 20:58:41 +00009291 while (!PendingUpdateRecords.empty()) {
Richard Smithd1c46742014-04-30 02:24:17 +00009292 auto Update = PendingUpdateRecords.pop_back_val();
9293 ReadingKindTracker ReadingKind(Read_Decl, *this);
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00009294 loadDeclUpdateRecords(Update);
Richard Smithd1c46742014-04-30 02:24:17 +00009295 }
Guy Benyei11169dd2012-12-18 14:30:41 +00009296 }
Richard Smith8a639892015-01-24 01:07:20 +00009297
9298 // At this point, all update records for loaded decls are in place, so any
9299 // fake class definitions should have become real.
9300 assert(PendingFakeDefinitionData.empty() &&
9301 "faked up a class definition but never saw the real one");
9302
Guy Benyei11169dd2012-12-18 14:30:41 +00009303 // If we deserialized any C++ or Objective-C class definitions, any
9304 // Objective-C protocol definitions, or any redeclarable templates, make sure
David L. Jonesc4808b9e2016-12-15 20:53:26 +00009305 // that all redeclarations point to the definitions. Note that this can only
Guy Benyei11169dd2012-12-18 14:30:41 +00009306 // happen now, after the redeclaration chains have been fully wired.
Vedant Kumar48b4f762018-04-14 01:40:48 +00009307 for (Decl *D : PendingDefinitions) {
9308 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9309 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00009310 // Make sure that the TagType points at the definition.
9311 const_cast<TagType*>(TagT)->decl = TD;
9312 }
Richard Smith8ce51082015-03-11 01:44:51 +00009313
Vedant Kumar48b4f762018-04-14 01:40:48 +00009314 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00009315 for (auto *R = getMostRecentExistingDecl(RD); R;
9316 R = R->getPreviousDecl()) {
9317 assert((R == D) ==
9318 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
Richard Smith2c381642014-08-27 23:11:59 +00009319 "declaration thinks it's the definition but it isn't");
Aaron Ballman86c93902014-03-06 23:45:36 +00009320 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
Richard Smith2c381642014-08-27 23:11:59 +00009321 }
Guy Benyei11169dd2012-12-18 14:30:41 +00009322 }
9323
9324 continue;
9325 }
Richard Smith8ce51082015-03-11 01:44:51 +00009326
Vedant Kumar48b4f762018-04-14 01:40:48 +00009327 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00009328 // Make sure that the ObjCInterfaceType points at the definition.
9329 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9330 ->Decl = ID;
Richard Smith8ce51082015-03-11 01:44:51 +00009331
9332 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9333 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9334
Guy Benyei11169dd2012-12-18 14:30:41 +00009335 continue;
9336 }
Richard Smith8ce51082015-03-11 01:44:51 +00009337
Vedant Kumar48b4f762018-04-14 01:40:48 +00009338 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00009339 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9340 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9341
Guy Benyei11169dd2012-12-18 14:30:41 +00009342 continue;
9343 }
Richard Smith8ce51082015-03-11 01:44:51 +00009344
Vedant Kumar48b4f762018-04-14 01:40:48 +00009345 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
Richard Smith8ce51082015-03-11 01:44:51 +00009346 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9347 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
Guy Benyei11169dd2012-12-18 14:30:41 +00009348 }
9349 PendingDefinitions.clear();
9350
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009351 // Load the bodies of any functions or methods we've encountered. We do
9352 // this now (delayed) so that we can be sure that the declaration chains
9353 // have been fully wired up (hasBody relies on this).
9354 // FIXME: We shouldn't require complete redeclaration chains here.
Vedant Kumar48b4f762018-04-14 01:40:48 +00009355 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9356 PBEnd = PendingBodies.end();
9357 PB != PBEnd; ++PB) {
9358 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009359 // FIXME: Check for =delete/=default?
9360 // FIXME: Complain about ODR violations here?
9361 const FunctionDecl *Defn = nullptr;
9362 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00009363 FD->setLazyBody(PB->second);
Richard Trieue6caa262017-12-23 00:41:01 +00009364 } else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00009365 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
Richard Trieue6caa262017-12-23 00:41:01 +00009366 mergeDefinitionVisibility(NonConstDefn, FD);
9367
9368 if (!FD->isLateTemplateParsed() &&
9369 !NonConstDefn->isLateTemplateParsed() &&
9370 FD->getODRHash() != NonConstDefn->getODRHash()) {
9371 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9372 }
9373 }
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009374 continue;
9375 }
9376
Vedant Kumar48b4f762018-04-14 01:40:48 +00009377 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009378 if (!getContext().getLangOpts().Modules || !MD->hasBody())
Vedant Kumar48b4f762018-04-14 01:40:48 +00009379 MD->setLazyBody(PB->second);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009380 }
9381 PendingBodies.clear();
9382
Richard Smith42413142015-05-15 20:05:43 +00009383 // Do some cleanup.
9384 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9385 getContext().deduplicateMergedDefinitonsFor(ND);
9386 PendingMergedDefinitionsToDeduplicate.clear();
Richard Smitha0ce9c42014-07-29 23:23:27 +00009387}
9388
9389void ASTReader::diagnoseOdrViolations() {
Richard Trieue6caa262017-12-23 00:41:01 +00009390 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9391 PendingFunctionOdrMergeFailures.empty())
Richard Smithbb853c72014-08-13 01:23:33 +00009392 return;
9393
Richard Smitha0ce9c42014-07-29 23:23:27 +00009394 // Trigger the import of the full definition of each class that had any
9395 // odr-merging problems, so we can produce better diagnostics for them.
Richard Smithbb853c72014-08-13 01:23:33 +00009396 // These updates may in turn find and diagnose some ODR failures, so take
9397 // ownership of the set first.
9398 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9399 PendingOdrMergeFailures.clear();
9400 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00009401 Merge.first->buildLookup();
9402 Merge.first->decls_begin();
9403 Merge.first->bases_begin();
9404 Merge.first->vbases_begin();
Richard Trieue13eabe2017-09-30 02:19:17 +00009405 for (auto &RecordPair : Merge.second) {
9406 auto *RD = RecordPair.first;
Richard Smitha0ce9c42014-07-29 23:23:27 +00009407 RD->decls_begin();
9408 RD->bases_begin();
9409 RD->vbases_begin();
9410 }
9411 }
9412
Richard Trieue6caa262017-12-23 00:41:01 +00009413 // Trigger the import of functions.
9414 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9415 PendingFunctionOdrMergeFailures.clear();
9416 for (auto &Merge : FunctionOdrMergeFailures) {
9417 Merge.first->buildLookup();
9418 Merge.first->decls_begin();
9419 Merge.first->getBody();
9420 for (auto &FD : Merge.second) {
9421 FD->buildLookup();
9422 FD->decls_begin();
9423 FD->getBody();
9424 }
9425 }
9426
Richard Smitha0ce9c42014-07-29 23:23:27 +00009427 // For each declaration from a merged context, check that the canonical
9428 // definition of that context also contains a declaration of the same
9429 // entity.
9430 //
9431 // Caution: this loop does things that might invalidate iterators into
9432 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9433 while (!PendingOdrMergeChecks.empty()) {
9434 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9435
9436 // FIXME: Skip over implicit declarations for now. This matters for things
9437 // like implicitly-declared special member functions. This isn't entirely
9438 // correct; we can end up with multiple unmerged declarations of the same
9439 // implicit entity.
9440 if (D->isImplicit())
9441 continue;
9442
9443 DeclContext *CanonDef = D->getDeclContext();
Richard Smitha0ce9c42014-07-29 23:23:27 +00009444
9445 bool Found = false;
9446 const Decl *DCanon = D->getCanonicalDecl();
9447
Richard Smith01bdb7a2014-08-28 05:44:07 +00009448 for (auto RI : D->redecls()) {
9449 if (RI->getLexicalDeclContext() == CanonDef) {
9450 Found = true;
9451 break;
9452 }
9453 }
9454 if (Found)
9455 continue;
9456
Richard Smith0f4e2c42015-08-06 04:23:48 +00009457 // Quick check failed, time to do the slow thing. Note, we can't just
9458 // look up the name of D in CanonDef here, because the member that is
9459 // in CanonDef might not be found by name lookup (it might have been
9460 // replaced by a more recent declaration in the lookup table), and we
9461 // can't necessarily find it in the redeclaration chain because it might
9462 // be merely mergeable, not redeclarable.
Richard Smitha0ce9c42014-07-29 23:23:27 +00009463 llvm::SmallVector<const NamedDecl*, 4> Candidates;
Richard Smith0f4e2c42015-08-06 04:23:48 +00009464 for (auto *CanonMember : CanonDef->decls()) {
9465 if (CanonMember->getCanonicalDecl() == DCanon) {
9466 // This can happen if the declaration is merely mergeable and not
9467 // actually redeclarable (we looked for redeclarations earlier).
9468 //
9469 // FIXME: We should be able to detect this more efficiently, without
9470 // pulling in all of the members of CanonDef.
9471 Found = true;
9472 break;
Richard Smitha0ce9c42014-07-29 23:23:27 +00009473 }
Richard Smith0f4e2c42015-08-06 04:23:48 +00009474 if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9475 if (ND->getDeclName() == D->getDeclName())
9476 Candidates.push_back(ND);
Richard Smitha0ce9c42014-07-29 23:23:27 +00009477 }
9478
9479 if (!Found) {
Richard Smithd08aeb62014-08-28 01:33:39 +00009480 // The AST doesn't like TagDecls becoming invalid after they've been
9481 // completed. We only really need to mark FieldDecls as invalid here.
9482 if (!isa<TagDecl>(D))
9483 D->setInvalidDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00009484
Richard Smith4ab3dbd2015-02-13 22:43:51 +00009485 // Ensure we don't accidentally recursively enter deserialization while
9486 // we're producing our diagnostic.
9487 Deserializing RecursionGuard(this);
Richard Smitha0ce9c42014-07-29 23:23:27 +00009488
9489 std::string CanonDefModule =
9490 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9491 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9492 << D << getOwningModuleNameForDiagnostic(D)
9493 << CanonDef << CanonDefModule.empty() << CanonDefModule;
9494
9495 if (Candidates.empty())
9496 Diag(cast<Decl>(CanonDef)->getLocation(),
9497 diag::note_module_odr_violation_no_possible_decls) << D;
9498 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00009499 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9500 Diag(Candidates[I]->getLocation(),
Richard Smitha0ce9c42014-07-29 23:23:27 +00009501 diag::note_module_odr_violation_possible_decl)
Vedant Kumar48b4f762018-04-14 01:40:48 +00009502 << Candidates[I];
Richard Smitha0ce9c42014-07-29 23:23:27 +00009503 }
9504
9505 DiagnosedOdrMergeFailures.insert(CanonDef);
9506 }
9507 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00009508
Richard Trieue6caa262017-12-23 00:41:01 +00009509 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty())
Richard Smith4ab3dbd2015-02-13 22:43:51 +00009510 return;
9511
9512 // Ensure we don't accidentally recursively enter deserialization while
9513 // we're producing our diagnostics.
9514 Deserializing RecursionGuard(this);
9515
Richard Trieue6caa262017-12-23 00:41:01 +00009516 // Common code for hashing helpers.
9517 ODRHash Hash;
9518 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9519 Hash.clear();
9520 Hash.AddQualType(Ty);
9521 return Hash.CalculateHash();
9522 };
9523
9524 auto ComputeODRHash = [&Hash](const Stmt *S) {
9525 assert(S);
9526 Hash.clear();
9527 Hash.AddStmt(S);
9528 return Hash.CalculateHash();
9529 };
9530
9531 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9532 assert(D);
9533 Hash.clear();
9534 Hash.AddSubDecl(D);
9535 return Hash.CalculateHash();
9536 };
9537
Richard Trieu7282d322018-04-25 00:31:15 +00009538 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9539 Hash.clear();
9540 Hash.AddTemplateArgument(TA);
9541 return Hash.CalculateHash();
9542 };
9543
Richard Trieu9359e8f2018-05-30 01:12:26 +00009544 auto ComputeTemplateParameterListODRHash =
9545 [&Hash](const TemplateParameterList *TPL) {
9546 assert(TPL);
9547 Hash.clear();
9548 Hash.AddTemplateParameterList(TPL);
9549 return Hash.CalculateHash();
9550 };
9551
Richard Smithcd45dbc2014-04-19 03:48:30 +00009552 // Issue any pending ODR-failure diagnostics.
Richard Smithbb853c72014-08-13 01:23:33 +00009553 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00009554 // If we've already pointed out a specific problem with this class, don't
9555 // bother issuing a general "something's different" diagnostic.
David Blaikie82e95a32014-11-19 07:49:47 +00009556 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
Richard Smithcd45dbc2014-04-19 03:48:30 +00009557 continue;
9558
9559 bool Diagnosed = false;
Richard Trieue7f7ed22017-02-22 01:11:25 +00009560 CXXRecordDecl *FirstRecord = Merge.first;
9561 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
Richard Trieue13eabe2017-09-30 02:19:17 +00009562 for (auto &RecordPair : Merge.second) {
9563 CXXRecordDecl *SecondRecord = RecordPair.first;
Richard Smithcd45dbc2014-04-19 03:48:30 +00009564 // Multiple different declarations got merged together; tell the user
9565 // where they came from.
Richard Trieue7f7ed22017-02-22 01:11:25 +00009566 if (FirstRecord == SecondRecord)
9567 continue;
9568
9569 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
Richard Trieue13eabe2017-09-30 02:19:17 +00009570
9571 auto *FirstDD = FirstRecord->DefinitionData;
9572 auto *SecondDD = RecordPair.second;
9573
9574 assert(FirstDD && SecondDD && "Definitions without DefinitionData");
9575
9576 // Diagnostics from DefinitionData are emitted here.
9577 if (FirstDD != SecondDD) {
9578 enum ODRDefinitionDataDifference {
9579 NumBases,
9580 NumVBases,
9581 BaseType,
9582 BaseVirtual,
9583 BaseAccess,
9584 };
9585 auto ODRDiagError = [FirstRecord, &FirstModule,
9586 this](SourceLocation Loc, SourceRange Range,
9587 ODRDefinitionDataDifference DiffType) {
9588 return Diag(Loc, diag::err_module_odr_violation_definition_data)
9589 << FirstRecord << FirstModule.empty() << FirstModule << Range
9590 << DiffType;
9591 };
9592 auto ODRDiagNote = [&SecondModule,
9593 this](SourceLocation Loc, SourceRange Range,
9594 ODRDefinitionDataDifference DiffType) {
9595 return Diag(Loc, diag::note_module_odr_violation_definition_data)
9596 << SecondModule << Range << DiffType;
9597 };
9598
Richard Trieue13eabe2017-09-30 02:19:17 +00009599 unsigned FirstNumBases = FirstDD->NumBases;
9600 unsigned FirstNumVBases = FirstDD->NumVBases;
9601 unsigned SecondNumBases = SecondDD->NumBases;
9602 unsigned SecondNumVBases = SecondDD->NumVBases;
9603
9604 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
9605 unsigned NumBases = DD->NumBases;
9606 if (NumBases == 0) return SourceRange();
9607 auto bases = DD->bases();
9608 return SourceRange(bases[0].getLocStart(),
9609 bases[NumBases - 1].getLocEnd());
9610 };
9611
9612 if (FirstNumBases != SecondNumBases) {
9613 ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9614 NumBases)
9615 << FirstNumBases;
9616 ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
9617 NumBases)
9618 << SecondNumBases;
9619 Diagnosed = true;
9620 break;
9621 }
9622
9623 if (FirstNumVBases != SecondNumVBases) {
9624 ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9625 NumVBases)
9626 << FirstNumVBases;
9627 ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
9628 NumVBases)
9629 << SecondNumVBases;
9630 Diagnosed = true;
9631 break;
9632 }
9633
9634 auto FirstBases = FirstDD->bases();
9635 auto SecondBases = SecondDD->bases();
9636 unsigned i = 0;
9637 for (i = 0; i < FirstNumBases; ++i) {
9638 auto FirstBase = FirstBases[i];
9639 auto SecondBase = SecondBases[i];
9640 if (ComputeQualTypeODRHash(FirstBase.getType()) !=
9641 ComputeQualTypeODRHash(SecondBase.getType())) {
9642 ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
9643 BaseType)
9644 << (i + 1) << FirstBase.getType();
9645 ODRDiagNote(SecondRecord->getLocation(),
9646 SecondBase.getSourceRange(), BaseType)
9647 << (i + 1) << SecondBase.getType();
9648 break;
9649 }
9650
9651 if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
9652 ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
9653 BaseVirtual)
9654 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
9655 ODRDiagNote(SecondRecord->getLocation(),
9656 SecondBase.getSourceRange(), BaseVirtual)
9657 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
9658 break;
9659 }
9660
9661 if (FirstBase.getAccessSpecifierAsWritten() !=
9662 SecondBase.getAccessSpecifierAsWritten()) {
9663 ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
9664 BaseAccess)
9665 << (i + 1) << FirstBase.getType()
9666 << (int)FirstBase.getAccessSpecifierAsWritten();
9667 ODRDiagNote(SecondRecord->getLocation(),
9668 SecondBase.getSourceRange(), BaseAccess)
9669 << (i + 1) << SecondBase.getType()
9670 << (int)SecondBase.getAccessSpecifierAsWritten();
9671 break;
9672 }
9673 }
9674
9675 if (i != FirstNumBases) {
9676 Diagnosed = true;
9677 break;
9678 }
9679 }
9680
Richard Trieue7f7ed22017-02-22 01:11:25 +00009681 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
Richard Trieu498117b2017-08-23 02:43:59 +00009682
9683 const ClassTemplateDecl *FirstTemplate =
9684 FirstRecord->getDescribedClassTemplate();
9685 const ClassTemplateDecl *SecondTemplate =
9686 SecondRecord->getDescribedClassTemplate();
9687
9688 assert(!FirstTemplate == !SecondTemplate &&
9689 "Both pointers should be null or non-null");
9690
9691 enum ODRTemplateDifference {
9692 ParamEmptyName,
9693 ParamName,
9694 ParamSingleDefaultArgument,
9695 ParamDifferentDefaultArgument,
9696 };
9697
9698 if (FirstTemplate && SecondTemplate) {
9699 DeclHashes FirstTemplateHashes;
9700 DeclHashes SecondTemplateHashes;
Richard Trieu498117b2017-08-23 02:43:59 +00009701
9702 auto PopulateTemplateParameterHashs =
Richard Trieue6caa262017-12-23 00:41:01 +00009703 [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9704 const ClassTemplateDecl *TD) {
Richard Trieu498117b2017-08-23 02:43:59 +00009705 for (auto *D : TD->getTemplateParameters()->asArray()) {
Richard Trieue6caa262017-12-23 00:41:01 +00009706 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
Richard Trieu498117b2017-08-23 02:43:59 +00009707 }
9708 };
9709
9710 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
9711 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
9712
9713 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
9714 "Number of template parameters should be equal.");
9715
9716 auto FirstIt = FirstTemplateHashes.begin();
9717 auto FirstEnd = FirstTemplateHashes.end();
9718 auto SecondIt = SecondTemplateHashes.begin();
9719 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
9720 if (FirstIt->second == SecondIt->second)
9721 continue;
9722
9723 auto ODRDiagError = [FirstRecord, &FirstModule,
9724 this](SourceLocation Loc, SourceRange Range,
9725 ODRTemplateDifference DiffType) {
9726 return Diag(Loc, diag::err_module_odr_violation_template_parameter)
9727 << FirstRecord << FirstModule.empty() << FirstModule << Range
9728 << DiffType;
9729 };
9730 auto ODRDiagNote = [&SecondModule,
9731 this](SourceLocation Loc, SourceRange Range,
9732 ODRTemplateDifference DiffType) {
9733 return Diag(Loc, diag::note_module_odr_violation_template_parameter)
9734 << SecondModule << Range << DiffType;
9735 };
9736
Vedant Kumar48b4f762018-04-14 01:40:48 +00009737 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
9738 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
Richard Trieu498117b2017-08-23 02:43:59 +00009739
9740 assert(FirstDecl->getKind() == SecondDecl->getKind() &&
9741 "Parameter Decl's should be the same kind.");
9742
9743 DeclarationName FirstName = FirstDecl->getDeclName();
9744 DeclarationName SecondName = SecondDecl->getDeclName();
9745
9746 if (FirstName != SecondName) {
9747 const bool FirstNameEmpty =
9748 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
9749 const bool SecondNameEmpty =
9750 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
9751 assert((!FirstNameEmpty || !SecondNameEmpty) &&
9752 "Both template parameters cannot be unnamed.");
9753 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9754 FirstNameEmpty ? ParamEmptyName : ParamName)
9755 << FirstName;
9756 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9757 SecondNameEmpty ? ParamEmptyName : ParamName)
9758 << SecondName;
9759 break;
9760 }
9761
9762 switch (FirstDecl->getKind()) {
9763 default:
9764 llvm_unreachable("Invalid template parameter type.");
9765 case Decl::TemplateTypeParm: {
9766 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
9767 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
9768 const bool HasFirstDefaultArgument =
9769 FirstParam->hasDefaultArgument() &&
9770 !FirstParam->defaultArgumentWasInherited();
9771 const bool HasSecondDefaultArgument =
9772 SecondParam->hasDefaultArgument() &&
9773 !SecondParam->defaultArgumentWasInherited();
9774
9775 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
9776 ODRDiagError(FirstDecl->getLocation(),
9777 FirstDecl->getSourceRange(),
9778 ParamSingleDefaultArgument)
9779 << HasFirstDefaultArgument;
9780 ODRDiagNote(SecondDecl->getLocation(),
9781 SecondDecl->getSourceRange(),
9782 ParamSingleDefaultArgument)
9783 << HasSecondDefaultArgument;
9784 break;
9785 }
9786
9787 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
9788 "Expecting default arguments.");
9789
9790 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9791 ParamDifferentDefaultArgument);
9792 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9793 ParamDifferentDefaultArgument);
9794
9795 break;
9796 }
9797 case Decl::NonTypeTemplateParm: {
9798 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
9799 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
9800 const bool HasFirstDefaultArgument =
9801 FirstParam->hasDefaultArgument() &&
9802 !FirstParam->defaultArgumentWasInherited();
9803 const bool HasSecondDefaultArgument =
9804 SecondParam->hasDefaultArgument() &&
9805 !SecondParam->defaultArgumentWasInherited();
9806
9807 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
9808 ODRDiagError(FirstDecl->getLocation(),
9809 FirstDecl->getSourceRange(),
9810 ParamSingleDefaultArgument)
9811 << HasFirstDefaultArgument;
9812 ODRDiagNote(SecondDecl->getLocation(),
9813 SecondDecl->getSourceRange(),
9814 ParamSingleDefaultArgument)
9815 << HasSecondDefaultArgument;
9816 break;
9817 }
9818
9819 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
9820 "Expecting default arguments.");
9821
9822 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9823 ParamDifferentDefaultArgument);
9824 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9825 ParamDifferentDefaultArgument);
9826
9827 break;
9828 }
9829 case Decl::TemplateTemplateParm: {
9830 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
9831 const auto *SecondParam =
9832 cast<TemplateTemplateParmDecl>(SecondDecl);
9833 const bool HasFirstDefaultArgument =
9834 FirstParam->hasDefaultArgument() &&
9835 !FirstParam->defaultArgumentWasInherited();
9836 const bool HasSecondDefaultArgument =
9837 SecondParam->hasDefaultArgument() &&
9838 !SecondParam->defaultArgumentWasInherited();
9839
9840 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
9841 ODRDiagError(FirstDecl->getLocation(),
9842 FirstDecl->getSourceRange(),
9843 ParamSingleDefaultArgument)
9844 << HasFirstDefaultArgument;
9845 ODRDiagNote(SecondDecl->getLocation(),
9846 SecondDecl->getSourceRange(),
9847 ParamSingleDefaultArgument)
9848 << HasSecondDefaultArgument;
9849 break;
9850 }
9851
9852 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
9853 "Expecting default arguments.");
9854
9855 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(),
9856 ParamDifferentDefaultArgument);
9857 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(),
9858 ParamDifferentDefaultArgument);
9859
9860 break;
9861 }
9862 }
9863
9864 break;
9865 }
9866
9867 if (FirstIt != FirstEnd) {
9868 Diagnosed = true;
9869 break;
9870 }
9871 }
9872
Richard Trieue7f7ed22017-02-22 01:11:25 +00009873 DeclHashes FirstHashes;
9874 DeclHashes SecondHashes;
Richard Trieue7f7ed22017-02-22 01:11:25 +00009875
Richard Trieue6caa262017-12-23 00:41:01 +00009876 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstRecord](
9877 DeclHashes &Hashes, CXXRecordDecl *Record) {
Richard Trieue7f7ed22017-02-22 01:11:25 +00009878 for (auto *D : Record->decls()) {
9879 // Due to decl merging, the first CXXRecordDecl is the parent of
9880 // Decls in both records.
9881 if (!ODRHash::isWhitelistedDecl(D, FirstRecord))
9882 continue;
Richard Trieue6caa262017-12-23 00:41:01 +00009883 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
Richard Trieue7f7ed22017-02-22 01:11:25 +00009884 }
9885 };
9886 PopulateHashes(FirstHashes, FirstRecord);
9887 PopulateHashes(SecondHashes, SecondRecord);
9888
9889 // Used with err_module_odr_violation_mismatch_decl and
9890 // note_module_odr_violation_mismatch_decl
Richard Trieu11d566a2017-06-12 21:58:22 +00009891 // This list should be the same Decl's as in ODRHash::isWhiteListedDecl
Richard Trieue7f7ed22017-02-22 01:11:25 +00009892 enum {
9893 EndOfClass,
9894 PublicSpecifer,
9895 PrivateSpecifer,
9896 ProtectedSpecifer,
Richard Trieu639d7b62017-02-22 22:22:42 +00009897 StaticAssert,
Richard Trieud0786092017-02-23 00:23:01 +00009898 Field,
Richard Trieu48143742017-02-28 21:24:38 +00009899 CXXMethod,
Richard Trieu11d566a2017-06-12 21:58:22 +00009900 TypeAlias,
9901 TypeDef,
Richard Trieu6e13ff32017-06-16 02:44:29 +00009902 Var,
Richard Trieuac6a1b62017-07-08 02:04:42 +00009903 Friend,
Richard Trieu9359e8f2018-05-30 01:12:26 +00009904 FunctionTemplate,
Richard Trieue7f7ed22017-02-22 01:11:25 +00009905 Other
9906 } FirstDiffType = Other,
9907 SecondDiffType = Other;
9908
9909 auto DifferenceSelector = [](Decl *D) {
9910 assert(D && "valid Decl required");
9911 switch (D->getKind()) {
9912 default:
9913 return Other;
9914 case Decl::AccessSpec:
9915 switch (D->getAccess()) {
9916 case AS_public:
9917 return PublicSpecifer;
9918 case AS_private:
9919 return PrivateSpecifer;
9920 case AS_protected:
9921 return ProtectedSpecifer;
9922 case AS_none:
Simon Pilgrimeeb1b302017-02-22 13:21:24 +00009923 break;
Richard Trieue7f7ed22017-02-22 01:11:25 +00009924 }
Simon Pilgrimeeb1b302017-02-22 13:21:24 +00009925 llvm_unreachable("Invalid access specifier");
Richard Trieu639d7b62017-02-22 22:22:42 +00009926 case Decl::StaticAssert:
9927 return StaticAssert;
Richard Trieud0786092017-02-23 00:23:01 +00009928 case Decl::Field:
9929 return Field;
Richard Trieu48143742017-02-28 21:24:38 +00009930 case Decl::CXXMethod:
Richard Trieu1c71d512017-07-15 02:55:13 +00009931 case Decl::CXXConstructor:
9932 case Decl::CXXDestructor:
Richard Trieu48143742017-02-28 21:24:38 +00009933 return CXXMethod;
Richard Trieu11d566a2017-06-12 21:58:22 +00009934 case Decl::TypeAlias:
9935 return TypeAlias;
9936 case Decl::Typedef:
9937 return TypeDef;
Richard Trieu6e13ff32017-06-16 02:44:29 +00009938 case Decl::Var:
9939 return Var;
Richard Trieuac6a1b62017-07-08 02:04:42 +00009940 case Decl::Friend:
9941 return Friend;
Richard Trieu9359e8f2018-05-30 01:12:26 +00009942 case Decl::FunctionTemplate:
9943 return FunctionTemplate;
Richard Trieue7f7ed22017-02-22 01:11:25 +00009944 }
9945 };
9946
9947 Decl *FirstDecl = nullptr;
9948 Decl *SecondDecl = nullptr;
9949 auto FirstIt = FirstHashes.begin();
9950 auto SecondIt = SecondHashes.begin();
9951
9952 // If there is a diagnoseable difference, FirstDiffType and
9953 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9954 // filled in if not EndOfClass.
9955 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
Benjamin Kramer6f224d22017-02-22 10:19:45 +00009956 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9957 FirstIt->second == SecondIt->second) {
Richard Trieue7f7ed22017-02-22 01:11:25 +00009958 ++FirstIt;
9959 ++SecondIt;
9960 continue;
Richard Trieudc4cb022017-02-17 07:19:24 +00009961 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00009962
Richard Trieue7f7ed22017-02-22 01:11:25 +00009963 FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9964 SecondDecl = SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9965
9966 FirstDiffType = FirstDecl ? DifferenceSelector(FirstDecl) : EndOfClass;
9967 SecondDiffType =
9968 SecondDecl ? DifferenceSelector(SecondDecl) : EndOfClass;
9969
9970 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00009971 }
Richard Trieue7f7ed22017-02-22 01:11:25 +00009972
9973 if (FirstDiffType == Other || SecondDiffType == Other) {
9974 // Reaching this point means an unexpected Decl was encountered
9975 // or no difference was detected. This causes a generic error
9976 // message to be emitted.
9977 Diag(FirstRecord->getLocation(),
9978 diag::err_module_odr_violation_different_definitions)
9979 << FirstRecord << FirstModule.empty() << FirstModule;
9980
Richard Trieuca48d362017-06-21 01:43:13 +00009981 if (FirstDecl) {
9982 Diag(FirstDecl->getLocation(), diag::note_first_module_difference)
9983 << FirstRecord << FirstDecl->getSourceRange();
9984 }
9985
Richard Trieue7f7ed22017-02-22 01:11:25 +00009986 Diag(SecondRecord->getLocation(),
9987 diag::note_module_odr_violation_different_definitions)
9988 << SecondModule;
Richard Trieuca48d362017-06-21 01:43:13 +00009989
9990 if (SecondDecl) {
9991 Diag(SecondDecl->getLocation(), diag::note_second_module_difference)
9992 << SecondDecl->getSourceRange();
9993 }
9994
Richard Trieue7f7ed22017-02-22 01:11:25 +00009995 Diagnosed = true;
9996 break;
9997 }
9998
9999 if (FirstDiffType != SecondDiffType) {
10000 SourceLocation FirstLoc;
10001 SourceRange FirstRange;
10002 if (FirstDiffType == EndOfClass) {
10003 FirstLoc = FirstRecord->getBraceRange().getEnd();
10004 } else {
10005 FirstLoc = FirstIt->first->getLocation();
10006 FirstRange = FirstIt->first->getSourceRange();
10007 }
10008 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10009 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10010 << FirstDiffType;
10011
10012 SourceLocation SecondLoc;
10013 SourceRange SecondRange;
10014 if (SecondDiffType == EndOfClass) {
10015 SecondLoc = SecondRecord->getBraceRange().getEnd();
10016 } else {
10017 SecondLoc = SecondDecl->getLocation();
10018 SecondRange = SecondDecl->getSourceRange();
10019 }
10020 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10021 << SecondModule << SecondRange << SecondDiffType;
10022 Diagnosed = true;
10023 break;
10024 }
10025
Richard Trieu639d7b62017-02-22 22:22:42 +000010026 assert(FirstDiffType == SecondDiffType);
10027
10028 // Used with err_module_odr_violation_mismatch_decl_diff and
10029 // note_module_odr_violation_mismatch_decl_diff
Richard Trieu9359e8f2018-05-30 01:12:26 +000010030 enum ODRDeclDifference {
Richard Trieu639d7b62017-02-22 22:22:42 +000010031 StaticAssertCondition,
10032 StaticAssertMessage,
10033 StaticAssertOnlyMessage,
Richard Trieud0786092017-02-23 00:23:01 +000010034 FieldName,
Richard Trieu8459ddf2017-02-24 02:59:12 +000010035 FieldTypeName,
Richard Trieu93772fc2017-02-24 20:59:28 +000010036 FieldSingleBitField,
Richard Trieu8d543e22017-02-24 23:35:37 +000010037 FieldDifferentWidthBitField,
10038 FieldSingleMutable,
10039 FieldSingleInitializer,
10040 FieldDifferentInitializers,
Richard Trieu48143742017-02-28 21:24:38 +000010041 MethodName,
Richard Trieu583e2c12017-03-04 00:08:58 +000010042 MethodDeleted,
10043 MethodVirtual,
10044 MethodStatic,
10045 MethodVolatile,
10046 MethodConst,
10047 MethodInline,
Richard Trieu02552272017-05-02 23:58:52 +000010048 MethodNumberParameters,
10049 MethodParameterType,
10050 MethodParameterName,
Richard Trieu6e13ff32017-06-16 02:44:29 +000010051 MethodParameterSingleDefaultArgument,
10052 MethodParameterDifferentDefaultArgument,
Richard Trieu7282d322018-04-25 00:31:15 +000010053 MethodNoTemplateArguments,
10054 MethodDifferentNumberTemplateArguments,
10055 MethodDifferentTemplateArgument,
Richard Trieu11d566a2017-06-12 21:58:22 +000010056 TypedefName,
10057 TypedefType,
Richard Trieu6e13ff32017-06-16 02:44:29 +000010058 VarName,
10059 VarType,
10060 VarSingleInitializer,
10061 VarDifferentInitializer,
10062 VarConstexpr,
Richard Trieuac6a1b62017-07-08 02:04:42 +000010063 FriendTypeFunction,
10064 FriendType,
10065 FriendFunction,
Richard Trieu9359e8f2018-05-30 01:12:26 +000010066 FunctionTemplateDifferentNumberParameters,
10067 FunctionTemplateParameterDifferentKind,
10068 FunctionTemplateParameterName,
10069 FunctionTemplateParameterSingleDefaultArgument,
10070 FunctionTemplateParameterDifferentDefaultArgument,
10071 FunctionTemplateParameterDifferentType,
10072 FunctionTemplatePackParameter,
Richard Trieu639d7b62017-02-22 22:22:42 +000010073 };
10074
10075 // These lambdas have the common portions of the ODR diagnostics. This
10076 // has the same return as Diag(), so addition parameters can be passed
10077 // in with operator<<
10078 auto ODRDiagError = [FirstRecord, &FirstModule, this](
10079 SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10080 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
10081 << FirstRecord << FirstModule.empty() << FirstModule << Range
10082 << DiffType;
10083 };
10084 auto ODRDiagNote = [&SecondModule, this](
10085 SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) {
10086 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
10087 << SecondModule << Range << DiffType;
10088 };
10089
Richard Trieu639d7b62017-02-22 22:22:42 +000010090 switch (FirstDiffType) {
10091 case Other:
10092 case EndOfClass:
10093 case PublicSpecifer:
10094 case PrivateSpecifer:
10095 case ProtectedSpecifer:
10096 llvm_unreachable("Invalid diff type");
10097
10098 case StaticAssert: {
Vedant Kumar48b4f762018-04-14 01:40:48 +000010099 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10100 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
Richard Trieu639d7b62017-02-22 22:22:42 +000010101
10102 Expr *FirstExpr = FirstSA->getAssertExpr();
10103 Expr *SecondExpr = SecondSA->getAssertExpr();
10104 unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10105 unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10106 if (FirstODRHash != SecondODRHash) {
10107 ODRDiagError(FirstExpr->getLocStart(), FirstExpr->getSourceRange(),
10108 StaticAssertCondition);
10109 ODRDiagNote(SecondExpr->getLocStart(),
10110 SecondExpr->getSourceRange(), StaticAssertCondition);
10111 Diagnosed = true;
10112 break;
10113 }
10114
10115 StringLiteral *FirstStr = FirstSA->getMessage();
10116 StringLiteral *SecondStr = SecondSA->getMessage();
10117 assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10118 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10119 SourceLocation FirstLoc, SecondLoc;
10120 SourceRange FirstRange, SecondRange;
10121 if (FirstStr) {
10122 FirstLoc = FirstStr->getLocStart();
10123 FirstRange = FirstStr->getSourceRange();
10124 } else {
10125 FirstLoc = FirstSA->getLocStart();
10126 FirstRange = FirstSA->getSourceRange();
10127 }
10128 if (SecondStr) {
10129 SecondLoc = SecondStr->getLocStart();
10130 SecondRange = SecondStr->getSourceRange();
10131 } else {
10132 SecondLoc = SecondSA->getLocStart();
10133 SecondRange = SecondSA->getSourceRange();
10134 }
10135 ODRDiagError(FirstLoc, FirstRange, StaticAssertOnlyMessage)
10136 << (FirstStr == nullptr);
10137 ODRDiagNote(SecondLoc, SecondRange, StaticAssertOnlyMessage)
10138 << (SecondStr == nullptr);
10139 Diagnosed = true;
10140 break;
10141 }
10142
10143 if (FirstStr && SecondStr &&
10144 FirstStr->getString() != SecondStr->getString()) {
10145 ODRDiagError(FirstStr->getLocStart(), FirstStr->getSourceRange(),
10146 StaticAssertMessage);
10147 ODRDiagNote(SecondStr->getLocStart(), SecondStr->getSourceRange(),
10148 StaticAssertMessage);
10149 Diagnosed = true;
10150 break;
10151 }
10152 break;
10153 }
Richard Trieud0786092017-02-23 00:23:01 +000010154 case Field: {
Vedant Kumar48b4f762018-04-14 01:40:48 +000010155 FieldDecl *FirstField = cast<FieldDecl>(FirstDecl);
10156 FieldDecl *SecondField = cast<FieldDecl>(SecondDecl);
Richard Trieud0786092017-02-23 00:23:01 +000010157 IdentifierInfo *FirstII = FirstField->getIdentifier();
10158 IdentifierInfo *SecondII = SecondField->getIdentifier();
10159 if (FirstII->getName() != SecondII->getName()) {
10160 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10161 FieldName)
10162 << FirstII;
10163 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10164 FieldName)
10165 << SecondII;
10166
10167 Diagnosed = true;
10168 break;
10169 }
Richard Trieu8459ddf2017-02-24 02:59:12 +000010170
Richard Smithdbafb6c2017-06-29 23:23:46 +000010171 assert(getContext().hasSameType(FirstField->getType(),
10172 SecondField->getType()));
Richard Trieu8459ddf2017-02-24 02:59:12 +000010173
10174 QualType FirstType = FirstField->getType();
10175 QualType SecondType = SecondField->getType();
Richard Trieuce81b192017-05-17 03:23:35 +000010176 if (ComputeQualTypeODRHash(FirstType) !=
10177 ComputeQualTypeODRHash(SecondType)) {
Richard Trieu8459ddf2017-02-24 02:59:12 +000010178 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10179 FieldTypeName)
10180 << FirstII << FirstType;
10181 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10182 FieldTypeName)
10183 << SecondII << SecondType;
10184
10185 Diagnosed = true;
10186 break;
10187 }
10188
Richard Trieu93772fc2017-02-24 20:59:28 +000010189 const bool IsFirstBitField = FirstField->isBitField();
10190 const bool IsSecondBitField = SecondField->isBitField();
10191 if (IsFirstBitField != IsSecondBitField) {
10192 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10193 FieldSingleBitField)
10194 << FirstII << IsFirstBitField;
10195 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10196 FieldSingleBitField)
10197 << SecondII << IsSecondBitField;
10198 Diagnosed = true;
10199 break;
10200 }
10201
10202 if (IsFirstBitField && IsSecondBitField) {
10203 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10204 FieldDifferentWidthBitField)
10205 << FirstII << FirstField->getBitWidth()->getSourceRange();
10206 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10207 FieldDifferentWidthBitField)
10208 << SecondII << SecondField->getBitWidth()->getSourceRange();
10209 Diagnosed = true;
10210 break;
10211 }
10212
Richard Trieu8d543e22017-02-24 23:35:37 +000010213 const bool IsFirstMutable = FirstField->isMutable();
10214 const bool IsSecondMutable = SecondField->isMutable();
10215 if (IsFirstMutable != IsSecondMutable) {
10216 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10217 FieldSingleMutable)
10218 << FirstII << IsFirstMutable;
10219 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10220 FieldSingleMutable)
10221 << SecondII << IsSecondMutable;
10222 Diagnosed = true;
10223 break;
10224 }
10225
10226 const Expr *FirstInitializer = FirstField->getInClassInitializer();
10227 const Expr *SecondInitializer = SecondField->getInClassInitializer();
10228 if ((!FirstInitializer && SecondInitializer) ||
10229 (FirstInitializer && !SecondInitializer)) {
10230 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(),
10231 FieldSingleInitializer)
10232 << FirstII << (FirstInitializer != nullptr);
10233 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(),
10234 FieldSingleInitializer)
10235 << SecondII << (SecondInitializer != nullptr);
10236 Diagnosed = true;
10237 break;
10238 }
10239
10240 if (FirstInitializer && SecondInitializer) {
10241 unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
10242 unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
10243 if (FirstInitHash != SecondInitHash) {
10244 ODRDiagError(FirstField->getLocation(),
10245 FirstField->getSourceRange(),
10246 FieldDifferentInitializers)
10247 << FirstII << FirstInitializer->getSourceRange();
10248 ODRDiagNote(SecondField->getLocation(),
10249 SecondField->getSourceRange(),
10250 FieldDifferentInitializers)
10251 << SecondII << SecondInitializer->getSourceRange();
10252 Diagnosed = true;
10253 break;
10254 }
10255 }
10256
Richard Trieud0786092017-02-23 00:23:01 +000010257 break;
10258 }
Richard Trieu48143742017-02-28 21:24:38 +000010259 case CXXMethod: {
Richard Trieu1c71d512017-07-15 02:55:13 +000010260 enum {
10261 DiagMethod,
10262 DiagConstructor,
10263 DiagDestructor,
10264 } FirstMethodType,
10265 SecondMethodType;
10266 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10267 if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10268 if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10269 return DiagMethod;
10270 };
Vedant Kumar48b4f762018-04-14 01:40:48 +000010271 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10272 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
Richard Trieu1c71d512017-07-15 02:55:13 +000010273 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10274 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
Richard Trieu583e2c12017-03-04 00:08:58 +000010275 auto FirstName = FirstMethod->getDeclName();
10276 auto SecondName = SecondMethod->getDeclName();
Richard Trieu1c71d512017-07-15 02:55:13 +000010277 if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
Richard Trieu48143742017-02-28 21:24:38 +000010278 ODRDiagError(FirstMethod->getLocation(),
10279 FirstMethod->getSourceRange(), MethodName)
Richard Trieu1c71d512017-07-15 02:55:13 +000010280 << FirstMethodType << FirstName;
Richard Trieu48143742017-02-28 21:24:38 +000010281 ODRDiagNote(SecondMethod->getLocation(),
10282 SecondMethod->getSourceRange(), MethodName)
Richard Trieu1c71d512017-07-15 02:55:13 +000010283 << SecondMethodType << SecondName;
Richard Trieu48143742017-02-28 21:24:38 +000010284
10285 Diagnosed = true;
10286 break;
10287 }
10288
Richard Trieu583e2c12017-03-04 00:08:58 +000010289 const bool FirstDeleted = FirstMethod->isDeleted();
10290 const bool SecondDeleted = SecondMethod->isDeleted();
10291 if (FirstDeleted != SecondDeleted) {
10292 ODRDiagError(FirstMethod->getLocation(),
10293 FirstMethod->getSourceRange(), MethodDeleted)
Richard Trieu1c71d512017-07-15 02:55:13 +000010294 << FirstMethodType << FirstName << FirstDeleted;
Richard Trieu583e2c12017-03-04 00:08:58 +000010295
10296 ODRDiagNote(SecondMethod->getLocation(),
10297 SecondMethod->getSourceRange(), MethodDeleted)
Richard Trieu1c71d512017-07-15 02:55:13 +000010298 << SecondMethodType << SecondName << SecondDeleted;
Richard Trieu583e2c12017-03-04 00:08:58 +000010299 Diagnosed = true;
10300 break;
10301 }
10302
10303 const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10304 const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10305 const bool FirstPure = FirstMethod->isPure();
10306 const bool SecondPure = SecondMethod->isPure();
10307 if ((FirstVirtual || SecondVirtual) &&
10308 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10309 ODRDiagError(FirstMethod->getLocation(),
10310 FirstMethod->getSourceRange(), MethodVirtual)
Richard Trieu1c71d512017-07-15 02:55:13 +000010311 << FirstMethodType << FirstName << FirstPure << FirstVirtual;
Richard Trieu583e2c12017-03-04 00:08:58 +000010312 ODRDiagNote(SecondMethod->getLocation(),
10313 SecondMethod->getSourceRange(), MethodVirtual)
Richard Trieu1c71d512017-07-15 02:55:13 +000010314 << SecondMethodType << SecondName << SecondPure << SecondVirtual;
Richard Trieu583e2c12017-03-04 00:08:58 +000010315 Diagnosed = true;
10316 break;
10317 }
10318
10319 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging,
10320 // FirstDecl is the canonical Decl of SecondDecl, so the storage
10321 // class needs to be checked instead.
10322 const auto FirstStorage = FirstMethod->getStorageClass();
10323 const auto SecondStorage = SecondMethod->getStorageClass();
10324 const bool FirstStatic = FirstStorage == SC_Static;
10325 const bool SecondStatic = SecondStorage == SC_Static;
10326 if (FirstStatic != SecondStatic) {
10327 ODRDiagError(FirstMethod->getLocation(),
10328 FirstMethod->getSourceRange(), MethodStatic)
Richard Trieu1c71d512017-07-15 02:55:13 +000010329 << FirstMethodType << FirstName << FirstStatic;
Richard Trieu583e2c12017-03-04 00:08:58 +000010330 ODRDiagNote(SecondMethod->getLocation(),
10331 SecondMethod->getSourceRange(), MethodStatic)
Richard Trieu1c71d512017-07-15 02:55:13 +000010332 << SecondMethodType << SecondName << SecondStatic;
Richard Trieu583e2c12017-03-04 00:08:58 +000010333 Diagnosed = true;
10334 break;
10335 }
10336
10337 const bool FirstVolatile = FirstMethod->isVolatile();
10338 const bool SecondVolatile = SecondMethod->isVolatile();
10339 if (FirstVolatile != SecondVolatile) {
10340 ODRDiagError(FirstMethod->getLocation(),
10341 FirstMethod->getSourceRange(), MethodVolatile)
Richard Trieu1c71d512017-07-15 02:55:13 +000010342 << FirstMethodType << FirstName << FirstVolatile;
Richard Trieu583e2c12017-03-04 00:08:58 +000010343 ODRDiagNote(SecondMethod->getLocation(),
10344 SecondMethod->getSourceRange(), MethodVolatile)
Richard Trieu1c71d512017-07-15 02:55:13 +000010345 << SecondMethodType << SecondName << SecondVolatile;
Richard Trieu583e2c12017-03-04 00:08:58 +000010346 Diagnosed = true;
10347 break;
10348 }
10349
10350 const bool FirstConst = FirstMethod->isConst();
10351 const bool SecondConst = SecondMethod->isConst();
10352 if (FirstConst != SecondConst) {
10353 ODRDiagError(FirstMethod->getLocation(),
10354 FirstMethod->getSourceRange(), MethodConst)
Richard Trieu1c71d512017-07-15 02:55:13 +000010355 << FirstMethodType << FirstName << FirstConst;
Richard Trieu583e2c12017-03-04 00:08:58 +000010356 ODRDiagNote(SecondMethod->getLocation(),
10357 SecondMethod->getSourceRange(), MethodConst)
Richard Trieu1c71d512017-07-15 02:55:13 +000010358 << SecondMethodType << SecondName << SecondConst;
Richard Trieu583e2c12017-03-04 00:08:58 +000010359 Diagnosed = true;
10360 break;
10361 }
10362
10363 const bool FirstInline = FirstMethod->isInlineSpecified();
10364 const bool SecondInline = SecondMethod->isInlineSpecified();
10365 if (FirstInline != SecondInline) {
10366 ODRDiagError(FirstMethod->getLocation(),
10367 FirstMethod->getSourceRange(), MethodInline)
Richard Trieu1c71d512017-07-15 02:55:13 +000010368 << FirstMethodType << FirstName << FirstInline;
Richard Trieu583e2c12017-03-04 00:08:58 +000010369 ODRDiagNote(SecondMethod->getLocation(),
10370 SecondMethod->getSourceRange(), MethodInline)
Richard Trieu1c71d512017-07-15 02:55:13 +000010371 << SecondMethodType << SecondName << SecondInline;
Richard Trieu583e2c12017-03-04 00:08:58 +000010372 Diagnosed = true;
10373 break;
10374 }
10375
Richard Trieu02552272017-05-02 23:58:52 +000010376 const unsigned FirstNumParameters = FirstMethod->param_size();
10377 const unsigned SecondNumParameters = SecondMethod->param_size();
10378 if (FirstNumParameters != SecondNumParameters) {
10379 ODRDiagError(FirstMethod->getLocation(),
10380 FirstMethod->getSourceRange(), MethodNumberParameters)
Richard Trieu1c71d512017-07-15 02:55:13 +000010381 << FirstMethodType << FirstName << FirstNumParameters;
Richard Trieu02552272017-05-02 23:58:52 +000010382 ODRDiagNote(SecondMethod->getLocation(),
10383 SecondMethod->getSourceRange(), MethodNumberParameters)
Richard Trieu1c71d512017-07-15 02:55:13 +000010384 << SecondMethodType << SecondName << SecondNumParameters;
Richard Trieu02552272017-05-02 23:58:52 +000010385 Diagnosed = true;
10386 break;
10387 }
10388
10389 // Need this status boolean to know when break out of the switch.
10390 bool ParameterMismatch = false;
10391 for (unsigned I = 0; I < FirstNumParameters; ++I) {
10392 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10393 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10394
10395 QualType FirstParamType = FirstParam->getType();
10396 QualType SecondParamType = SecondParam->getType();
10397 if (FirstParamType != SecondParamType &&
10398 ComputeQualTypeODRHash(FirstParamType) !=
10399 ComputeQualTypeODRHash(SecondParamType)) {
Vedant Kumar48b4f762018-04-14 01:40:48 +000010400 if (const DecayedType *ParamDecayedType =
Richard Trieu02552272017-05-02 23:58:52 +000010401 FirstParamType->getAs<DecayedType>()) {
10402 ODRDiagError(FirstMethod->getLocation(),
10403 FirstMethod->getSourceRange(), MethodParameterType)
Richard Trieu1c71d512017-07-15 02:55:13 +000010404 << FirstMethodType << FirstName << (I + 1) << FirstParamType
10405 << true << ParamDecayedType->getOriginalType();
Richard Trieu02552272017-05-02 23:58:52 +000010406 } else {
10407 ODRDiagError(FirstMethod->getLocation(),
10408 FirstMethod->getSourceRange(), MethodParameterType)
Richard Trieu1c71d512017-07-15 02:55:13 +000010409 << FirstMethodType << FirstName << (I + 1) << FirstParamType
10410 << false;
Richard Trieu02552272017-05-02 23:58:52 +000010411 }
10412
Vedant Kumar48b4f762018-04-14 01:40:48 +000010413 if (const DecayedType *ParamDecayedType =
Richard Trieu02552272017-05-02 23:58:52 +000010414 SecondParamType->getAs<DecayedType>()) {
10415 ODRDiagNote(SecondMethod->getLocation(),
10416 SecondMethod->getSourceRange(), MethodParameterType)
Richard Trieu1c71d512017-07-15 02:55:13 +000010417 << SecondMethodType << SecondName << (I + 1)
10418 << SecondParamType << true
Richard Trieu02552272017-05-02 23:58:52 +000010419 << ParamDecayedType->getOriginalType();
10420 } else {
10421 ODRDiagNote(SecondMethod->getLocation(),
10422 SecondMethod->getSourceRange(), MethodParameterType)
Richard Trieu1c71d512017-07-15 02:55:13 +000010423 << SecondMethodType << SecondName << (I + 1)
10424 << SecondParamType << false;
Richard Trieu02552272017-05-02 23:58:52 +000010425 }
10426 ParameterMismatch = true;
10427 break;
10428 }
10429
10430 DeclarationName FirstParamName = FirstParam->getDeclName();
10431 DeclarationName SecondParamName = SecondParam->getDeclName();
10432 if (FirstParamName != SecondParamName) {
10433 ODRDiagError(FirstMethod->getLocation(),
10434 FirstMethod->getSourceRange(), MethodParameterName)
Richard Trieu1c71d512017-07-15 02:55:13 +000010435 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
Richard Trieu02552272017-05-02 23:58:52 +000010436 ODRDiagNote(SecondMethod->getLocation(),
10437 SecondMethod->getSourceRange(), MethodParameterName)
Richard Trieu1c71d512017-07-15 02:55:13 +000010438 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
Richard Trieu02552272017-05-02 23:58:52 +000010439 ParameterMismatch = true;
10440 break;
10441 }
Richard Trieu6e13ff32017-06-16 02:44:29 +000010442
10443 const Expr *FirstInit = FirstParam->getInit();
10444 const Expr *SecondInit = SecondParam->getInit();
10445 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10446 ODRDiagError(FirstMethod->getLocation(),
10447 FirstMethod->getSourceRange(),
10448 MethodParameterSingleDefaultArgument)
Richard Trieu1c71d512017-07-15 02:55:13 +000010449 << FirstMethodType << FirstName << (I + 1)
10450 << (FirstInit == nullptr)
Richard Trieu6e13ff32017-06-16 02:44:29 +000010451 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10452 ODRDiagNote(SecondMethod->getLocation(),
10453 SecondMethod->getSourceRange(),
10454 MethodParameterSingleDefaultArgument)
Richard Trieu1c71d512017-07-15 02:55:13 +000010455 << SecondMethodType << SecondName << (I + 1)
10456 << (SecondInit == nullptr)
Richard Trieu6e13ff32017-06-16 02:44:29 +000010457 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10458 ParameterMismatch = true;
10459 break;
10460 }
10461
10462 if (FirstInit && SecondInit &&
10463 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10464 ODRDiagError(FirstMethod->getLocation(),
10465 FirstMethod->getSourceRange(),
10466 MethodParameterDifferentDefaultArgument)
Richard Trieu1c71d512017-07-15 02:55:13 +000010467 << FirstMethodType << FirstName << (I + 1)
10468 << FirstInit->getSourceRange();
Richard Trieu6e13ff32017-06-16 02:44:29 +000010469 ODRDiagNote(SecondMethod->getLocation(),
10470 SecondMethod->getSourceRange(),
10471 MethodParameterDifferentDefaultArgument)
Richard Trieu1c71d512017-07-15 02:55:13 +000010472 << SecondMethodType << SecondName << (I + 1)
10473 << SecondInit->getSourceRange();
Richard Trieu6e13ff32017-06-16 02:44:29 +000010474 ParameterMismatch = true;
10475 break;
10476
10477 }
Richard Trieu02552272017-05-02 23:58:52 +000010478 }
10479
10480 if (ParameterMismatch) {
10481 Diagnosed = true;
10482 break;
10483 }
10484
Richard Trieu7282d322018-04-25 00:31:15 +000010485 const auto *FirstTemplateArgs =
10486 FirstMethod->getTemplateSpecializationArgs();
10487 const auto *SecondTemplateArgs =
10488 SecondMethod->getTemplateSpecializationArgs();
10489
10490 if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10491 (!FirstTemplateArgs && SecondTemplateArgs)) {
10492 ODRDiagError(FirstMethod->getLocation(),
10493 FirstMethod->getSourceRange(), MethodNoTemplateArguments)
10494 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10495 ODRDiagNote(SecondMethod->getLocation(),
10496 SecondMethod->getSourceRange(), MethodNoTemplateArguments)
10497 << SecondMethodType << SecondName
10498 << (SecondTemplateArgs != nullptr);
10499
10500 Diagnosed = true;
10501 break;
10502 }
10503
10504 if (FirstTemplateArgs && SecondTemplateArgs) {
10505 // Remove pack expansions from argument list.
10506 auto ExpandTemplateArgumentList =
10507 [](const TemplateArgumentList *TAL) {
10508 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10509 for (const TemplateArgument &TA : TAL->asArray()) {
10510 if (TA.getKind() != TemplateArgument::Pack) {
10511 ExpandedList.push_back(&TA);
10512 continue;
10513 }
10514 for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10515 ExpandedList.push_back(&PackTA);
10516 }
10517 }
10518 return ExpandedList;
10519 };
10520 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10521 ExpandTemplateArgumentList(FirstTemplateArgs);
10522 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10523 ExpandTemplateArgumentList(SecondTemplateArgs);
10524
10525 if (FirstExpandedList.size() != SecondExpandedList.size()) {
10526 ODRDiagError(FirstMethod->getLocation(),
10527 FirstMethod->getSourceRange(),
10528 MethodDifferentNumberTemplateArguments)
10529 << FirstMethodType << FirstName
10530 << (unsigned)FirstExpandedList.size();
10531 ODRDiagNote(SecondMethod->getLocation(),
10532 SecondMethod->getSourceRange(),
10533 MethodDifferentNumberTemplateArguments)
10534 << SecondMethodType << SecondName
10535 << (unsigned)SecondExpandedList.size();
10536
10537 Diagnosed = true;
10538 break;
10539 }
10540
10541 bool TemplateArgumentMismatch = false;
10542 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10543 const TemplateArgument &FirstTA = *FirstExpandedList[i],
10544 &SecondTA = *SecondExpandedList[i];
10545 if (ComputeTemplateArgumentODRHash(FirstTA) ==
10546 ComputeTemplateArgumentODRHash(SecondTA)) {
10547 continue;
10548 }
10549
10550 ODRDiagError(FirstMethod->getLocation(),
10551 FirstMethod->getSourceRange(),
10552 MethodDifferentTemplateArgument)
10553 << FirstMethodType << FirstName << FirstTA << i + 1;
10554 ODRDiagNote(SecondMethod->getLocation(),
10555 SecondMethod->getSourceRange(),
10556 MethodDifferentTemplateArgument)
10557 << SecondMethodType << SecondName << SecondTA << i + 1;
10558
10559 TemplateArgumentMismatch = true;
10560 break;
10561 }
10562
10563 if (TemplateArgumentMismatch) {
10564 Diagnosed = true;
10565 break;
10566 }
10567 }
Richard Trieu48143742017-02-28 21:24:38 +000010568 break;
10569 }
Richard Trieu11d566a2017-06-12 21:58:22 +000010570 case TypeAlias:
10571 case TypeDef: {
Vedant Kumar48b4f762018-04-14 01:40:48 +000010572 TypedefNameDecl *FirstTD = cast<TypedefNameDecl>(FirstDecl);
10573 TypedefNameDecl *SecondTD = cast<TypedefNameDecl>(SecondDecl);
Richard Trieu11d566a2017-06-12 21:58:22 +000010574 auto FirstName = FirstTD->getDeclName();
10575 auto SecondName = SecondTD->getDeclName();
10576 if (FirstName != SecondName) {
10577 ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
10578 TypedefName)
10579 << (FirstDiffType == TypeAlias) << FirstName;
10580 ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
10581 TypedefName)
10582 << (FirstDiffType == TypeAlias) << SecondName;
10583 Diagnosed = true;
10584 break;
10585 }
10586
10587 QualType FirstType = FirstTD->getUnderlyingType();
10588 QualType SecondType = SecondTD->getUnderlyingType();
10589 if (ComputeQualTypeODRHash(FirstType) !=
10590 ComputeQualTypeODRHash(SecondType)) {
10591 ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(),
10592 TypedefType)
10593 << (FirstDiffType == TypeAlias) << FirstName << FirstType;
10594 ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(),
10595 TypedefType)
10596 << (FirstDiffType == TypeAlias) << SecondName << SecondType;
10597 Diagnosed = true;
10598 break;
10599 }
10600 break;
10601 }
Richard Trieu6e13ff32017-06-16 02:44:29 +000010602 case Var: {
Vedant Kumar48b4f762018-04-14 01:40:48 +000010603 VarDecl *FirstVD = cast<VarDecl>(FirstDecl);
10604 VarDecl *SecondVD = cast<VarDecl>(SecondDecl);
Richard Trieu6e13ff32017-06-16 02:44:29 +000010605 auto FirstName = FirstVD->getDeclName();
10606 auto SecondName = SecondVD->getDeclName();
10607 if (FirstName != SecondName) {
10608 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10609 VarName)
10610 << FirstName;
10611 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10612 VarName)
10613 << SecondName;
10614 Diagnosed = true;
10615 break;
10616 }
10617
10618 QualType FirstType = FirstVD->getType();
10619 QualType SecondType = SecondVD->getType();
10620 if (ComputeQualTypeODRHash(FirstType) !=
10621 ComputeQualTypeODRHash(SecondType)) {
10622 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10623 VarType)
10624 << FirstName << FirstType;
10625 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10626 VarType)
10627 << SecondName << SecondType;
10628 Diagnosed = true;
10629 break;
10630 }
10631
10632 const Expr *FirstInit = FirstVD->getInit();
10633 const Expr *SecondInit = SecondVD->getInit();
10634 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10635 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10636 VarSingleInitializer)
10637 << FirstName << (FirstInit == nullptr)
10638 << (FirstInit ? FirstInit->getSourceRange(): SourceRange());
10639 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10640 VarSingleInitializer)
10641 << SecondName << (SecondInit == nullptr)
10642 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10643 Diagnosed = true;
10644 break;
10645 }
10646
10647 if (FirstInit && SecondInit &&
10648 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10649 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10650 VarDifferentInitializer)
10651 << FirstName << FirstInit->getSourceRange();
10652 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10653 VarDifferentInitializer)
10654 << SecondName << SecondInit->getSourceRange();
10655 Diagnosed = true;
10656 break;
10657 }
10658
10659 const bool FirstIsConstexpr = FirstVD->isConstexpr();
10660 const bool SecondIsConstexpr = SecondVD->isConstexpr();
10661 if (FirstIsConstexpr != SecondIsConstexpr) {
10662 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(),
10663 VarConstexpr)
10664 << FirstName << FirstIsConstexpr;
10665 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(),
10666 VarConstexpr)
10667 << SecondName << SecondIsConstexpr;
10668 Diagnosed = true;
10669 break;
10670 }
10671 break;
10672 }
Richard Trieuac6a1b62017-07-08 02:04:42 +000010673 case Friend: {
Vedant Kumar48b4f762018-04-14 01:40:48 +000010674 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10675 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
Richard Trieuac6a1b62017-07-08 02:04:42 +000010676
10677 NamedDecl *FirstND = FirstFriend->getFriendDecl();
10678 NamedDecl *SecondND = SecondFriend->getFriendDecl();
10679
10680 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10681 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10682
10683 if (FirstND && SecondND) {
10684 ODRDiagError(FirstFriend->getFriendLoc(),
10685 FirstFriend->getSourceRange(), FriendFunction)
10686 << FirstND;
10687 ODRDiagNote(SecondFriend->getFriendLoc(),
10688 SecondFriend->getSourceRange(), FriendFunction)
10689 << SecondND;
10690
10691 Diagnosed = true;
10692 break;
10693 }
10694
10695 if (FirstTSI && SecondTSI) {
10696 QualType FirstFriendType = FirstTSI->getType();
10697 QualType SecondFriendType = SecondTSI->getType();
10698 assert(ComputeQualTypeODRHash(FirstFriendType) !=
10699 ComputeQualTypeODRHash(SecondFriendType));
10700 ODRDiagError(FirstFriend->getFriendLoc(),
10701 FirstFriend->getSourceRange(), FriendType)
10702 << FirstFriendType;
10703 ODRDiagNote(SecondFriend->getFriendLoc(),
10704 SecondFriend->getSourceRange(), FriendType)
10705 << SecondFriendType;
10706 Diagnosed = true;
10707 break;
10708 }
10709
10710 ODRDiagError(FirstFriend->getFriendLoc(), FirstFriend->getSourceRange(),
10711 FriendTypeFunction)
10712 << (FirstTSI == nullptr);
10713 ODRDiagNote(SecondFriend->getFriendLoc(),
10714 SecondFriend->getSourceRange(), FriendTypeFunction)
10715 << (SecondTSI == nullptr);
10716
10717 Diagnosed = true;
10718 break;
10719 }
Richard Trieu9359e8f2018-05-30 01:12:26 +000010720 case FunctionTemplate: {
10721 FunctionTemplateDecl *FirstTemplate =
10722 cast<FunctionTemplateDecl>(FirstDecl);
10723 FunctionTemplateDecl *SecondTemplate =
10724 cast<FunctionTemplateDecl>(SecondDecl);
10725
10726 TemplateParameterList *FirstTPL =
10727 FirstTemplate->getTemplateParameters();
10728 TemplateParameterList *SecondTPL =
10729 SecondTemplate->getTemplateParameters();
10730
10731 if (FirstTPL->size() != SecondTPL->size()) {
10732 ODRDiagError(FirstTemplate->getLocation(),
10733 FirstTemplate->getSourceRange(),
10734 FunctionTemplateDifferentNumberParameters)
10735 << FirstTemplate << FirstTPL->size();
10736 ODRDiagNote(SecondTemplate->getLocation(),
10737 SecondTemplate->getSourceRange(),
10738 FunctionTemplateDifferentNumberParameters)
10739 << SecondTemplate << SecondTPL->size();
10740
10741 Diagnosed = true;
10742 break;
10743 }
10744
10745 bool ParameterMismatch = false;
10746 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10747 NamedDecl *FirstParam = FirstTPL->getParam(i);
10748 NamedDecl *SecondParam = SecondTPL->getParam(i);
10749
10750 if (FirstParam->getKind() != SecondParam->getKind()) {
10751 enum {
10752 TemplateTypeParameter,
10753 NonTypeTemplateParameter,
10754 TemplateTemplateParameter,
10755 };
10756 auto GetParamType = [](NamedDecl *D) {
10757 switch (D->getKind()) {
10758 default:
10759 llvm_unreachable("Unexpected template parameter type");
10760 case Decl::TemplateTypeParm:
10761 return TemplateTypeParameter;
10762 case Decl::NonTypeTemplateParm:
10763 return NonTypeTemplateParameter;
10764 case Decl::TemplateTemplateParm:
10765 return TemplateTemplateParameter;
10766 }
10767 };
10768
10769 ODRDiagError(FirstTemplate->getLocation(),
10770 FirstTemplate->getSourceRange(),
10771 FunctionTemplateParameterDifferentKind)
10772 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10773 ODRDiagNote(SecondTemplate->getLocation(),
10774 SecondTemplate->getSourceRange(),
10775 FunctionTemplateParameterDifferentKind)
10776 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10777
10778 ParameterMismatch = true;
10779 break;
10780 }
10781
10782 if (FirstParam->getName() != SecondParam->getName()) {
10783 ODRDiagError(FirstTemplate->getLocation(),
10784 FirstTemplate->getSourceRange(),
10785 FunctionTemplateParameterName)
10786 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10787 << FirstParam;
10788 ODRDiagNote(SecondTemplate->getLocation(),
10789 SecondTemplate->getSourceRange(),
10790 FunctionTemplateParameterName)
10791 << SecondTemplate << (i + 1)
10792 << (bool)SecondParam->getIdentifier() << SecondParam;
10793 ParameterMismatch = true;
10794 break;
10795 }
10796
10797 if (isa<TemplateTypeParmDecl>(FirstParam) &&
10798 isa<TemplateTypeParmDecl>(SecondParam)) {
10799 TemplateTypeParmDecl *FirstTTPD =
10800 cast<TemplateTypeParmDecl>(FirstParam);
10801 TemplateTypeParmDecl *SecondTTPD =
10802 cast<TemplateTypeParmDecl>(SecondParam);
10803 bool HasFirstDefaultArgument =
10804 FirstTTPD->hasDefaultArgument() &&
10805 !FirstTTPD->defaultArgumentWasInherited();
10806 bool HasSecondDefaultArgument =
10807 SecondTTPD->hasDefaultArgument() &&
10808 !SecondTTPD->defaultArgumentWasInherited();
10809 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10810 ODRDiagError(FirstTemplate->getLocation(),
10811 FirstTemplate->getSourceRange(),
10812 FunctionTemplateParameterSingleDefaultArgument)
10813 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10814 ODRDiagNote(SecondTemplate->getLocation(),
10815 SecondTemplate->getSourceRange(),
10816 FunctionTemplateParameterSingleDefaultArgument)
10817 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10818 ParameterMismatch = true;
10819 break;
10820 }
10821
10822 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10823 QualType FirstType = FirstTTPD->getDefaultArgument();
10824 QualType SecondType = SecondTTPD->getDefaultArgument();
10825 if (ComputeQualTypeODRHash(FirstType) !=
10826 ComputeQualTypeODRHash(SecondType)) {
10827 ODRDiagError(FirstTemplate->getLocation(),
10828 FirstTemplate->getSourceRange(),
10829 FunctionTemplateParameterDifferentDefaultArgument)
10830 << FirstTemplate << (i + 1) << FirstType;
10831 ODRDiagNote(SecondTemplate->getLocation(),
10832 SecondTemplate->getSourceRange(),
10833 FunctionTemplateParameterDifferentDefaultArgument)
10834 << SecondTemplate << (i + 1) << SecondType;
10835 ParameterMismatch = true;
10836 break;
10837 }
10838 }
10839
10840 if (FirstTTPD->isParameterPack() !=
10841 SecondTTPD->isParameterPack()) {
10842 ODRDiagError(FirstTemplate->getLocation(),
10843 FirstTemplate->getSourceRange(),
10844 FunctionTemplatePackParameter)
10845 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10846 ODRDiagNote(SecondTemplate->getLocation(),
10847 SecondTemplate->getSourceRange(),
10848 FunctionTemplatePackParameter)
10849 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10850 ParameterMismatch = true;
10851 break;
10852 }
10853 }
10854
10855 if (isa<TemplateTemplateParmDecl>(FirstParam) &&
10856 isa<TemplateTemplateParmDecl>(SecondParam)) {
10857 TemplateTemplateParmDecl *FirstTTPD =
10858 cast<TemplateTemplateParmDecl>(FirstParam);
10859 TemplateTemplateParmDecl *SecondTTPD =
10860 cast<TemplateTemplateParmDecl>(SecondParam);
10861
10862 TemplateParameterList *FirstTPL =
10863 FirstTTPD->getTemplateParameters();
10864 TemplateParameterList *SecondTPL =
10865 SecondTTPD->getTemplateParameters();
10866
10867 if (ComputeTemplateParameterListODRHash(FirstTPL) !=
10868 ComputeTemplateParameterListODRHash(SecondTPL)) {
10869 ODRDiagError(FirstTemplate->getLocation(),
10870 FirstTemplate->getSourceRange(),
10871 FunctionTemplateParameterDifferentType)
10872 << FirstTemplate << (i + 1);
10873 ODRDiagNote(SecondTemplate->getLocation(),
10874 SecondTemplate->getSourceRange(),
10875 FunctionTemplateParameterDifferentType)
10876 << SecondTemplate << (i + 1);
10877 ParameterMismatch = true;
10878 break;
10879 }
10880
10881 bool HasFirstDefaultArgument =
10882 FirstTTPD->hasDefaultArgument() &&
10883 !FirstTTPD->defaultArgumentWasInherited();
10884 bool HasSecondDefaultArgument =
10885 SecondTTPD->hasDefaultArgument() &&
10886 !SecondTTPD->defaultArgumentWasInherited();
10887 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10888 ODRDiagError(FirstTemplate->getLocation(),
10889 FirstTemplate->getSourceRange(),
10890 FunctionTemplateParameterSingleDefaultArgument)
10891 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10892 ODRDiagNote(SecondTemplate->getLocation(),
10893 SecondTemplate->getSourceRange(),
10894 FunctionTemplateParameterSingleDefaultArgument)
10895 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10896 ParameterMismatch = true;
10897 break;
10898 }
10899
10900 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10901 TemplateArgument FirstTA =
10902 FirstTTPD->getDefaultArgument().getArgument();
10903 TemplateArgument SecondTA =
10904 SecondTTPD->getDefaultArgument().getArgument();
10905 if (ComputeTemplateArgumentODRHash(FirstTA) !=
10906 ComputeTemplateArgumentODRHash(SecondTA)) {
10907 ODRDiagError(FirstTemplate->getLocation(),
10908 FirstTemplate->getSourceRange(),
10909 FunctionTemplateParameterDifferentDefaultArgument)
10910 << FirstTemplate << (i + 1) << FirstTA;
10911 ODRDiagNote(SecondTemplate->getLocation(),
10912 SecondTemplate->getSourceRange(),
10913 FunctionTemplateParameterDifferentDefaultArgument)
10914 << SecondTemplate << (i + 1) << SecondTA;
10915 ParameterMismatch = true;
10916 break;
10917 }
10918 }
10919
10920 if (FirstTTPD->isParameterPack() !=
10921 SecondTTPD->isParameterPack()) {
10922 ODRDiagError(FirstTemplate->getLocation(),
10923 FirstTemplate->getSourceRange(),
10924 FunctionTemplatePackParameter)
10925 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10926 ODRDiagNote(SecondTemplate->getLocation(),
10927 SecondTemplate->getSourceRange(),
10928 FunctionTemplatePackParameter)
10929 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10930 ParameterMismatch = true;
10931 break;
10932 }
10933 }
10934
10935 if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
10936 isa<NonTypeTemplateParmDecl>(SecondParam)) {
10937 NonTypeTemplateParmDecl *FirstNTTPD =
10938 cast<NonTypeTemplateParmDecl>(FirstParam);
10939 NonTypeTemplateParmDecl *SecondNTTPD =
10940 cast<NonTypeTemplateParmDecl>(SecondParam);
10941
10942 QualType FirstType = FirstNTTPD->getType();
10943 QualType SecondType = SecondNTTPD->getType();
10944 if (ComputeQualTypeODRHash(FirstType) !=
10945 ComputeQualTypeODRHash(SecondType)) {
10946 ODRDiagError(FirstTemplate->getLocation(),
10947 FirstTemplate->getSourceRange(),
10948 FunctionTemplateParameterDifferentType)
10949 << FirstTemplate << (i + 1);
10950 ODRDiagNote(SecondTemplate->getLocation(),
10951 SecondTemplate->getSourceRange(),
10952 FunctionTemplateParameterDifferentType)
10953 << SecondTemplate << (i + 1);
10954 ParameterMismatch = true;
10955 break;
10956 }
10957
10958 bool HasFirstDefaultArgument =
10959 FirstNTTPD->hasDefaultArgument() &&
10960 !FirstNTTPD->defaultArgumentWasInherited();
10961 bool HasSecondDefaultArgument =
10962 SecondNTTPD->hasDefaultArgument() &&
10963 !SecondNTTPD->defaultArgumentWasInherited();
10964 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10965 ODRDiagError(FirstTemplate->getLocation(),
10966 FirstTemplate->getSourceRange(),
10967 FunctionTemplateParameterSingleDefaultArgument)
10968 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10969 ODRDiagNote(SecondTemplate->getLocation(),
10970 SecondTemplate->getSourceRange(),
10971 FunctionTemplateParameterSingleDefaultArgument)
10972 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10973 ParameterMismatch = true;
10974 break;
10975 }
10976
10977 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10978 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
10979 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
10980 if (ComputeODRHash(FirstDefaultArgument) !=
10981 ComputeODRHash(SecondDefaultArgument)) {
10982 ODRDiagError(FirstTemplate->getLocation(),
10983 FirstTemplate->getSourceRange(),
10984 FunctionTemplateParameterDifferentDefaultArgument)
10985 << FirstTemplate << (i + 1) << FirstDefaultArgument;
10986 ODRDiagNote(SecondTemplate->getLocation(),
10987 SecondTemplate->getSourceRange(),
10988 FunctionTemplateParameterDifferentDefaultArgument)
10989 << SecondTemplate << (i + 1) << SecondDefaultArgument;
10990 ParameterMismatch = true;
10991 break;
10992 }
10993 }
10994
10995 if (FirstNTTPD->isParameterPack() !=
10996 SecondNTTPD->isParameterPack()) {
10997 ODRDiagError(FirstTemplate->getLocation(),
10998 FirstTemplate->getSourceRange(),
10999 FunctionTemplatePackParameter)
11000 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11001 ODRDiagNote(SecondTemplate->getLocation(),
11002 SecondTemplate->getSourceRange(),
11003 FunctionTemplatePackParameter)
11004 << SecondTemplate << (i + 1)
11005 << SecondNTTPD->isParameterPack();
11006 ParameterMismatch = true;
11007 break;
11008 }
11009 }
11010 }
11011
11012 if (ParameterMismatch) {
11013 Diagnosed = true;
11014 break;
11015 }
11016
11017 break;
11018 }
Richard Trieu639d7b62017-02-22 22:22:42 +000011019 }
11020
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000011021 if (Diagnosed)
Richard Trieue7f7ed22017-02-22 01:11:25 +000011022 continue;
11023
Richard Trieu708859a2017-06-08 00:56:21 +000011024 Diag(FirstDecl->getLocation(),
11025 diag::err_module_odr_violation_mismatch_decl_unknown)
11026 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11027 << FirstDecl->getSourceRange();
11028 Diag(SecondDecl->getLocation(),
11029 diag::note_module_odr_violation_mismatch_decl_unknown)
11030 << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
Richard Trieue7f7ed22017-02-22 01:11:25 +000011031 Diagnosed = true;
Richard Smithcd45dbc2014-04-19 03:48:30 +000011032 }
11033
11034 if (!Diagnosed) {
11035 // All definitions are updates to the same declaration. This happens if a
11036 // module instantiates the declaration of a class template specialization
11037 // and two or more other modules instantiate its definition.
11038 //
11039 // FIXME: Indicate which modules had instantiations of this definition.
11040 // FIXME: How can this even happen?
11041 Diag(Merge.first->getLocation(),
11042 diag::err_module_odr_violation_different_instantiations)
11043 << Merge.first;
11044 }
11045 }
Richard Trieue6caa262017-12-23 00:41:01 +000011046
11047 // Issue ODR failures diagnostics for functions.
11048 for (auto &Merge : FunctionOdrMergeFailures) {
11049 enum ODRFunctionDifference {
11050 ReturnType,
11051 ParameterName,
11052 ParameterType,
11053 ParameterSingleDefaultArgument,
11054 ParameterDifferentDefaultArgument,
11055 FunctionBody,
11056 };
11057
11058 FunctionDecl *FirstFunction = Merge.first;
11059 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11060
11061 bool Diagnosed = false;
11062 for (auto &SecondFunction : Merge.second) {
Vedant Kumar48b4f762018-04-14 01:40:48 +000011063
Richard Trieue6caa262017-12-23 00:41:01 +000011064 if (FirstFunction == SecondFunction)
11065 continue;
11066
11067 std::string SecondModule =
11068 getOwningModuleNameForDiagnostic(SecondFunction);
11069
11070 auto ODRDiagError = [FirstFunction, &FirstModule,
11071 this](SourceLocation Loc, SourceRange Range,
11072 ODRFunctionDifference DiffType) {
11073 return Diag(Loc, diag::err_module_odr_violation_function)
11074 << FirstFunction << FirstModule.empty() << FirstModule << Range
11075 << DiffType;
11076 };
11077 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11078 SourceRange Range,
11079 ODRFunctionDifference DiffType) {
11080 return Diag(Loc, diag::note_module_odr_violation_function)
11081 << SecondModule << Range << DiffType;
11082 };
11083
11084 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11085 ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11086 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11087 FirstFunction->getReturnTypeSourceRange(), ReturnType)
11088 << FirstFunction->getReturnType();
11089 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11090 SecondFunction->getReturnTypeSourceRange(), ReturnType)
11091 << SecondFunction->getReturnType();
11092 Diagnosed = true;
11093 break;
11094 }
11095
11096 assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11097 "Merged functions with different number of parameters");
11098
11099 auto ParamSize = FirstFunction->param_size();
11100 bool ParameterMismatch = false;
11101 for (unsigned I = 0; I < ParamSize; ++I) {
11102 auto *FirstParam = FirstFunction->getParamDecl(I);
11103 auto *SecondParam = SecondFunction->getParamDecl(I);
11104
11105 assert(getContext().hasSameType(FirstParam->getType(),
11106 SecondParam->getType()) &&
11107 "Merged function has different parameter types.");
11108
11109 if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11110 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11111 ParameterName)
11112 << I + 1 << FirstParam->getDeclName();
11113 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11114 ParameterName)
11115 << I + 1 << SecondParam->getDeclName();
11116 ParameterMismatch = true;
11117 break;
11118 };
11119
11120 QualType FirstParamType = FirstParam->getType();
11121 QualType SecondParamType = SecondParam->getType();
11122 if (FirstParamType != SecondParamType &&
11123 ComputeQualTypeODRHash(FirstParamType) !=
11124 ComputeQualTypeODRHash(SecondParamType)) {
Vedant Kumar48b4f762018-04-14 01:40:48 +000011125 if (const DecayedType *ParamDecayedType =
Richard Trieue6caa262017-12-23 00:41:01 +000011126 FirstParamType->getAs<DecayedType>()) {
11127 ODRDiagError(FirstParam->getLocation(),
11128 FirstParam->getSourceRange(), ParameterType)
11129 << (I + 1) << FirstParamType << true
11130 << ParamDecayedType->getOriginalType();
11131 } else {
11132 ODRDiagError(FirstParam->getLocation(),
11133 FirstParam->getSourceRange(), ParameterType)
11134 << (I + 1) << FirstParamType << false;
11135 }
11136
Vedant Kumar48b4f762018-04-14 01:40:48 +000011137 if (const DecayedType *ParamDecayedType =
Richard Trieue6caa262017-12-23 00:41:01 +000011138 SecondParamType->getAs<DecayedType>()) {
11139 ODRDiagNote(SecondParam->getLocation(),
11140 SecondParam->getSourceRange(), ParameterType)
11141 << (I + 1) << SecondParamType << true
11142 << ParamDecayedType->getOriginalType();
11143 } else {
11144 ODRDiagNote(SecondParam->getLocation(),
11145 SecondParam->getSourceRange(), ParameterType)
11146 << (I + 1) << SecondParamType << false;
11147 }
11148 ParameterMismatch = true;
11149 break;
11150 }
11151
11152 const Expr *FirstInit = FirstParam->getInit();
11153 const Expr *SecondInit = SecondParam->getInit();
11154 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11155 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11156 ParameterSingleDefaultArgument)
11157 << (I + 1) << (FirstInit == nullptr)
11158 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11159 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11160 ParameterSingleDefaultArgument)
11161 << (I + 1) << (SecondInit == nullptr)
11162 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11163 ParameterMismatch = true;
11164 break;
11165 }
11166
11167 if (FirstInit && SecondInit &&
11168 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11169 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11170 ParameterDifferentDefaultArgument)
11171 << (I + 1) << FirstInit->getSourceRange();
11172 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11173 ParameterDifferentDefaultArgument)
11174 << (I + 1) << SecondInit->getSourceRange();
11175 ParameterMismatch = true;
11176 break;
11177 }
11178
11179 assert(ComputeSubDeclODRHash(FirstParam) ==
11180 ComputeSubDeclODRHash(SecondParam) &&
11181 "Undiagnosed parameter difference.");
11182 }
11183
11184 if (ParameterMismatch) {
11185 Diagnosed = true;
11186 break;
11187 }
11188
11189 // If no error has been generated before now, assume the problem is in
11190 // the body and generate a message.
11191 ODRDiagError(FirstFunction->getLocation(),
11192 FirstFunction->getSourceRange(), FunctionBody);
11193 ODRDiagNote(SecondFunction->getLocation(),
11194 SecondFunction->getSourceRange(), FunctionBody);
11195 Diagnosed = true;
11196 break;
11197 }
Evgeny Stupachenkobf25d672018-01-05 02:22:52 +000011198 (void)Diagnosed;
Richard Trieue6caa262017-12-23 00:41:01 +000011199 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11200 }
Guy Benyei11169dd2012-12-18 14:30:41 +000011201}
11202
Richard Smithce18a182015-07-14 00:26:00 +000011203void ASTReader::StartedDeserializing() {
David L. Jonesc4808b9e2016-12-15 20:53:26 +000011204 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
Richard Smithce18a182015-07-14 00:26:00 +000011205 ReadTimer->startTimer();
11206}
11207
Guy Benyei11169dd2012-12-18 14:30:41 +000011208void ASTReader::FinishedDeserializing() {
11209 assert(NumCurrentElementsDeserializing &&
11210 "FinishedDeserializing not paired with StartedDeserializing");
11211 if (NumCurrentElementsDeserializing == 1) {
11212 // We decrease NumCurrentElementsDeserializing only after pending actions
11213 // are finished, to avoid recursively re-calling finishPendingActions().
11214 finishPendingActions();
11215 }
11216 --NumCurrentElementsDeserializing;
11217
Richard Smitha0ce9c42014-07-29 23:23:27 +000011218 if (NumCurrentElementsDeserializing == 0) {
Richard Smith9e2341d2015-03-23 03:25:59 +000011219 // Propagate exception specification updates along redeclaration chains.
Richard Smith7226f2a2015-03-23 19:54:56 +000011220 while (!PendingExceptionSpecUpdates.empty()) {
11221 auto Updates = std::move(PendingExceptionSpecUpdates);
11222 PendingExceptionSpecUpdates.clear();
11223 for (auto Update : Updates) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +000011224 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Richard Smith7226f2a2015-03-23 19:54:56 +000011225 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
Richard Smith1d0f1992015-08-19 21:09:32 +000011226 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
Richard Smithdbafb6c2017-06-29 23:23:46 +000011227 if (auto *Listener = getContext().getASTMutationListener())
Richard Smithd88a7f12015-09-01 20:35:42 +000011228 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
Richard Smith1d0f1992015-08-19 21:09:32 +000011229 for (auto *Redecl : Update.second->redecls())
Richard Smithdbafb6c2017-06-29 23:23:46 +000011230 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
Richard Smith7226f2a2015-03-23 19:54:56 +000011231 }
Richard Smith9e2341d2015-03-23 03:25:59 +000011232 }
11233
Richard Smithce18a182015-07-14 00:26:00 +000011234 if (ReadTimer)
11235 ReadTimer->stopTimer();
11236
Richard Smith0f4e2c42015-08-06 04:23:48 +000011237 diagnoseOdrViolations();
11238
Richard Smith04d05b52014-03-23 00:27:18 +000011239 // We are not in recursive loading, so it's safe to pass the "interesting"
11240 // decls to the consumer.
Richard Smitha0ce9c42014-07-29 23:23:27 +000011241 if (Consumer)
11242 PassInterestingDeclsToConsumer();
Guy Benyei11169dd2012-12-18 14:30:41 +000011243 }
11244}
11245
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +000011246void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
Richard Smith9e2341d2015-03-23 03:25:59 +000011247 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11248 // Remove any fake results before adding any real ones.
11249 auto It = PendingFakeLookupResults.find(II);
11250 if (It != PendingFakeLookupResults.end()) {
Richard Smitha534a312015-07-21 23:54:07 +000011251 for (auto *ND : It->second)
Richard Smith9e2341d2015-03-23 03:25:59 +000011252 SemaObj->IdResolver.RemoveDecl(ND);
Ben Langmuireb8bd2d2015-04-10 22:25:42 +000011253 // FIXME: this works around module+PCH performance issue.
11254 // Rather than erase the result from the map, which is O(n), just clear
11255 // the vector of NamedDecls.
11256 It->second.clear();
Richard Smith9e2341d2015-03-23 03:25:59 +000011257 }
11258 }
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +000011259
11260 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11261 SemaObj->TUScope->AddDecl(D);
11262 } else if (SemaObj->TUScope) {
11263 // Adding the decl to IdResolver may have failed because it was already in
11264 // (even though it was not added in scope). If it is already in, make sure
11265 // it gets in the scope as well.
11266 if (std::find(SemaObj->IdResolver.begin(Name),
11267 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11268 SemaObj->TUScope->AddDecl(D);
11269 }
11270}
11271
Richard Smithdbafb6c2017-06-29 23:23:46 +000011272ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
David Blaikie61137e12017-01-05 18:23:18 +000011273 const PCHContainerReader &PCHContainerRdr,
11274 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11275 StringRef isysroot, bool DisableValidation,
11276 bool AllowASTWithCompilerErrors,
11277 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11278 bool UseGlobalIndex,
11279 std::unique_ptr<llvm::Timer> ReadTimer)
11280 : Listener(DisableValidation
11281 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11282 : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
David Blaikie61137e12017-01-05 18:23:18 +000011283 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
David Blaikie9d7c1ba2017-01-05 18:45:43 +000011284 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
Richard Smithdbafb6c2017-06-29 23:23:46 +000011285 ContextObj(Context),
Boris Kolpackovd30446f2017-08-31 06:26:43 +000011286 ModuleMgr(PP.getFileManager(), PP.getPCMCache(), PCHContainerRdr,
11287 PP.getHeaderSearchInfo()),
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +000011288 PCMCache(PP.getPCMCache()), DummyIdResolver(PP),
11289 ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
David Blaikie61137e12017-01-05 18:23:18 +000011290 DisableValidation(DisableValidation),
Nico Weber824285e2014-05-08 04:26:47 +000011291 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11292 AllowConfigurationMismatch(AllowConfigurationMismatch),
11293 ValidateSystemInputs(ValidateSystemInputs),
David Blaikie9d7c1ba2017-01-05 18:45:43 +000011294 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
Guy Benyei11169dd2012-12-18 14:30:41 +000011295 SourceMgr.setExternalSLocEntrySource(this);
Douglas Gregor6623e1f2015-11-03 18:33:07 +000011296
11297 for (const auto &Ext : Extensions) {
11298 auto BlockName = Ext->getExtensionMetadata().BlockName;
11299 auto Known = ModuleFileExtensions.find(BlockName);
11300 if (Known != ModuleFileExtensions.end()) {
11301 Diags.Report(diag::warn_duplicate_module_file_extension)
11302 << BlockName;
11303 continue;
11304 }
11305
11306 ModuleFileExtensions.insert({BlockName, Ext});
11307 }
Guy Benyei11169dd2012-12-18 14:30:41 +000011308}
11309
11310ASTReader::~ASTReader() {
Nico Weber824285e2014-05-08 04:26:47 +000011311 if (OwnsDeserializationListener)
11312 delete DeserializationListener;
Guy Benyei11169dd2012-12-18 14:30:41 +000011313}
Richard Smith10379092016-05-06 23:14:07 +000011314
11315IdentifierResolver &ASTReader::getIdResolver() {
11316 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11317}
David L. Jonesbe1557a2016-12-21 00:17:49 +000011318
11319unsigned ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11320 unsigned AbbrevID) {
11321 Idx = 0;
11322 Record.clear();
11323 return Cursor.readRecord(AbbrevID, Record);
11324}