blob: 7d84f1108c85408c55dd451868bc0fa3203c120f [file] [log] [blame]
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001//===- ASTReader.cpp - AST File Reader ------------------------------------===//
Guy Benyei11169dd2012-12-18 14:30:41 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Guy Benyei11169dd2012-12-18 14:30:41 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ASTReader class, which reads AST files.
10//
11//===----------------------------------------------------------------------===//
12
Alexey Bataev93dc40d2019-12-20 11:04:57 -050013#include "clang/Basic/OpenMPKinds.h"
John McCallc2f18312019-12-14 03:01:28 -050014#include "clang/Serialization/ASTRecordReader.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000015#include "ASTCommon.h"
16#include "ASTReaderInternals.h"
John McCalld505e572019-12-13 21:54:44 -050017#include "clang/AST/AbstractTypeReader.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000018#include "clang/AST/ASTConsumer.h"
19#include "clang/AST/ASTContext.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000020#include "clang/AST/ASTMutationListener.h"
21#include "clang/AST/ASTUnresolvedSet.h"
22#include "clang/AST/Decl.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000023#include "clang/AST/DeclBase.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000024#include "clang/AST/DeclCXX.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000025#include "clang/AST/DeclFriend.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000026#include "clang/AST/DeclGroup.h"
27#include "clang/AST/DeclObjC.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000028#include "clang/AST/DeclTemplate.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000029#include "clang/AST/DeclarationName.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000030#include "clang/AST/Expr.h"
31#include "clang/AST/ExprCXX.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000032#include "clang/AST/ExternalASTSource.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000033#include "clang/AST/NestedNameSpecifier.h"
John McCallc2f18312019-12-14 03:01:28 -050034#include "clang/AST/OpenMPClause.h"
Richard Trieue7f7ed22017-02-22 01:11:25 +000035#include "clang/AST/ODRHash.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000036#include "clang/AST/RawCommentList.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000037#include "clang/AST/TemplateBase.h"
38#include "clang/AST/TemplateName.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000039#include "clang/AST/Type.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000040#include "clang/AST/TypeLoc.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000041#include "clang/AST/TypeLocVisitor.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000042#include "clang/AST/UnresolvedSet.h"
43#include "clang/Basic/CommentOptions.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000044#include "clang/Basic/Diagnostic.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000045#include "clang/Basic/DiagnosticOptions.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000046#include "clang/Basic/ExceptionSpecificationType.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000047#include "clang/Basic/FileManager.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000048#include "clang/Basic/FileSystemOptions.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000049#include "clang/Basic/IdentifierTable.h"
50#include "clang/Basic/LLVM.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000051#include "clang/Basic/LangOptions.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000052#include "clang/Basic/Module.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000053#include "clang/Basic/ObjCRuntime.h"
54#include "clang/Basic/OperatorKinds.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000055#include "clang/Basic/PragmaKinds.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000056#include "clang/Basic/Sanitizers.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000057#include "clang/Basic/SourceLocation.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000058#include "clang/Basic/SourceManager.h"
59#include "clang/Basic/SourceManagerInternals.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000060#include "clang/Basic/Specifiers.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000061#include "clang/Basic/TargetInfo.h"
62#include "clang/Basic/TargetOptions.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000063#include "clang/Basic/TokenKinds.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000064#include "clang/Basic/Version.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"
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +000081#include "clang/Serialization/InMemoryModuleCache.h"
Duncan P. N. Exon Smithf7170d12019-11-21 18:49:05 -080082#include "clang/Serialization/ModuleFile.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000083#include "clang/Serialization/ModuleFileExtension.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000084#include "clang/Serialization/ModuleManager.h"
Richard Trieuf3b00462018-12-12 02:53:59 +000085#include "clang/Serialization/PCHContainerOperations.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000086#include "clang/Serialization/SerializationDiagnostic.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000087#include "llvm/ADT/APFloat.h"
88#include "llvm/ADT/APInt.h"
89#include "llvm/ADT/APSInt.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000090#include "llvm/ADT/ArrayRef.h"
91#include "llvm/ADT/DenseMap.h"
92#include "llvm/ADT/FoldingSet.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000093#include "llvm/ADT/Hashing.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000094#include "llvm/ADT/IntrusiveRefCntPtr.h"
95#include "llvm/ADT/None.h"
96#include "llvm/ADT/Optional.h"
97#include "llvm/ADT/STLExtras.h"
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +000098#include "llvm/ADT/ScopeExit.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000099#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000100#include "llvm/ADT/SmallString.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000101#include "llvm/ADT/SmallVector.h"
Vedant Kumar48b4f762018-04-14 01:40:48 +0000102#include "llvm/ADT/StringExtras.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000103#include "llvm/ADT/StringMap.h"
104#include "llvm/ADT/StringRef.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000105#include "llvm/ADT/Triple.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000106#include "llvm/ADT/iterator_range.h"
Francis Visoiu Mistrihe0308272019-07-03 22:40:07 +0000107#include "llvm/Bitstream/BitstreamReader.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000108#include "llvm/Support/Casting.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000109#include "llvm/Support/Compiler.h"
Pavel Labathd8c62902018-06-11 10:28:04 +0000110#include "llvm/Support/Compression.h"
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000111#include "llvm/Support/DJB.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000112#include "llvm/Support/Endian.h"
George Rimarc39f5492017-01-17 15:45:31 +0000113#include "llvm/Support/Error.h"
Guy Benyei11169dd2012-12-18 14:30:41 +0000114#include "llvm/Support/ErrorHandling.h"
115#include "llvm/Support/FileSystem.h"
116#include "llvm/Support/MemoryBuffer.h"
117#include "llvm/Support/Path.h"
118#include "llvm/Support/SaveAndRestore.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000119#include "llvm/Support/Timer.h"
Pavel Labathd8c62902018-06-11 10:28:04 +0000120#include "llvm/Support/VersionTuple.h"
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000121#include "llvm/Support/raw_ostream.h"
Guy Benyei11169dd2012-12-18 14:30:41 +0000122#include <algorithm>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000123#include <cassert>
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000124#include <cstddef>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000125#include <cstdint>
Chris Lattner91f373e2013-01-20 00:57:52 +0000126#include <cstdio>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000127#include <ctime>
Guy Benyei11169dd2012-12-18 14:30:41 +0000128#include <iterator>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000129#include <limits>
130#include <map>
131#include <memory>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000132#include <string>
Rafael Espindola8a8e5542014-06-12 17:19:42 +0000133#include <system_error>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000134#include <tuple>
135#include <utility>
136#include <vector>
Guy Benyei11169dd2012-12-18 14:30:41 +0000137
138using namespace clang;
Vedant Kumar48b4f762018-04-14 01:40:48 +0000139using namespace clang::serialization;
140using namespace clang::serialization::reader;
Chris Lattner7fb3bef2013-01-20 00:56:42 +0000141using llvm::BitstreamCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +0000142
Ben Langmuircb69b572014-03-07 06:40:32 +0000143//===----------------------------------------------------------------------===//
144// ChainedASTReaderListener implementation
145//===----------------------------------------------------------------------===//
146
147bool
148ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
149 return First->ReadFullVersionInformation(FullVersion) ||
150 Second->ReadFullVersionInformation(FullVersion);
151}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000152
Ben Langmuir4f5212a2014-04-14 22:12:44 +0000153void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
154 First->ReadModuleName(ModuleName);
155 Second->ReadModuleName(ModuleName);
156}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000157
Ben Langmuir4f5212a2014-04-14 22:12:44 +0000158void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
159 First->ReadModuleMapFile(ModuleMapPath);
160 Second->ReadModuleMapFile(ModuleMapPath);
161}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000162
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000163bool
164ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
165 bool Complain,
166 bool AllowCompatibleDifferences) {
167 return First->ReadLanguageOptions(LangOpts, Complain,
168 AllowCompatibleDifferences) ||
169 Second->ReadLanguageOptions(LangOpts, Complain,
170 AllowCompatibleDifferences);
Ben Langmuircb69b572014-03-07 06:40:32 +0000171}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000172
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000173bool ChainedASTReaderListener::ReadTargetOptions(
174 const TargetOptions &TargetOpts, bool Complain,
175 bool AllowCompatibleDifferences) {
176 return First->ReadTargetOptions(TargetOpts, Complain,
177 AllowCompatibleDifferences) ||
178 Second->ReadTargetOptions(TargetOpts, Complain,
179 AllowCompatibleDifferences);
Ben Langmuircb69b572014-03-07 06:40:32 +0000180}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000181
Ben Langmuircb69b572014-03-07 06:40:32 +0000182bool ChainedASTReaderListener::ReadDiagnosticOptions(
Ben Langmuirb92de022014-04-29 16:25:26 +0000183 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
Ben Langmuircb69b572014-03-07 06:40:32 +0000184 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
185 Second->ReadDiagnosticOptions(DiagOpts, Complain);
186}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000187
Ben Langmuircb69b572014-03-07 06:40:32 +0000188bool
189ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
190 bool Complain) {
191 return First->ReadFileSystemOptions(FSOpts, Complain) ||
192 Second->ReadFileSystemOptions(FSOpts, Complain);
193}
194
195bool ChainedASTReaderListener::ReadHeaderSearchOptions(
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000196 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
197 bool Complain) {
198 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
199 Complain) ||
200 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
201 Complain);
Ben Langmuircb69b572014-03-07 06:40:32 +0000202}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000203
Ben Langmuircb69b572014-03-07 06:40:32 +0000204bool ChainedASTReaderListener::ReadPreprocessorOptions(
205 const PreprocessorOptions &PPOpts, bool Complain,
206 std::string &SuggestedPredefines) {
207 return First->ReadPreprocessorOptions(PPOpts, Complain,
208 SuggestedPredefines) ||
209 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
210}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000211
Ben Langmuircb69b572014-03-07 06:40:32 +0000212void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
213 unsigned Value) {
214 First->ReadCounter(M, Value);
215 Second->ReadCounter(M, Value);
216}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000217
Ben Langmuircb69b572014-03-07 06:40:32 +0000218bool ChainedASTReaderListener::needsInputFileVisitation() {
219 return First->needsInputFileVisitation() ||
220 Second->needsInputFileVisitation();
221}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000222
Ben Langmuircb69b572014-03-07 06:40:32 +0000223bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
224 return First->needsSystemInputFileVisitation() ||
225 Second->needsSystemInputFileVisitation();
226}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000227
Richard Smith216a3bd2015-08-13 17:57:10 +0000228void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
229 ModuleKind Kind) {
230 First->visitModuleFile(Filename, Kind);
231 Second->visitModuleFile(Filename, Kind);
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000232}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000233
Ben Langmuircb69b572014-03-07 06:40:32 +0000234bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000235 bool isSystem,
Richard Smith216a3bd2015-08-13 17:57:10 +0000236 bool isOverridden,
237 bool isExplicitModule) {
Justin Bognerc65a66d2014-05-22 06:04:59 +0000238 bool Continue = false;
239 if (First->needsInputFileVisitation() &&
240 (!isSystem || First->needsSystemInputFileVisitation()))
Richard Smith216a3bd2015-08-13 17:57:10 +0000241 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
242 isExplicitModule);
Justin Bognerc65a66d2014-05-22 06:04:59 +0000243 if (Second->needsInputFileVisitation() &&
244 (!isSystem || Second->needsSystemInputFileVisitation()))
Richard Smith216a3bd2015-08-13 17:57:10 +0000245 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
246 isExplicitModule);
Justin Bognerc65a66d2014-05-22 06:04:59 +0000247 return Continue;
Ben Langmuircb69b572014-03-07 06:40:32 +0000248}
249
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000250void ChainedASTReaderListener::readModuleFileExtension(
251 const ModuleFileExtensionMetadata &Metadata) {
252 First->readModuleFileExtension(Metadata);
253 Second->readModuleFileExtension(Metadata);
254}
255
Guy Benyei11169dd2012-12-18 14:30:41 +0000256//===----------------------------------------------------------------------===//
257// PCH validator implementation
258//===----------------------------------------------------------------------===//
259
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000260ASTReaderListener::~ASTReaderListener() = default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000261
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000262/// Compare the given set of language options against an existing set of
Guy Benyei11169dd2012-12-18 14:30:41 +0000263/// language options.
264///
265/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000266/// \param AllowCompatibleDifferences If true, differences between compatible
267/// language options will be permitted.
Guy Benyei11169dd2012-12-18 14:30:41 +0000268///
269/// \returns true if the languagae options mis-match, false otherwise.
270static bool checkLanguageOptions(const LangOptions &LangOpts,
271 const LangOptions &ExistingLangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000272 DiagnosticsEngine *Diags,
273 bool AllowCompatibleDifferences = true) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000274#define LANGOPT(Name, Bits, Default, Description) \
275 if (ExistingLangOpts.Name != LangOpts.Name) { \
276 if (Diags) \
277 Diags->Report(diag::err_pch_langopt_mismatch) \
278 << Description << LangOpts.Name << ExistingLangOpts.Name; \
279 return true; \
280 }
281
282#define VALUE_LANGOPT(Name, Bits, Default, Description) \
283 if (ExistingLangOpts.Name != LangOpts.Name) { \
284 if (Diags) \
285 Diags->Report(diag::err_pch_langopt_value_mismatch) \
286 << Description; \
287 return true; \
288 }
289
290#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
291 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
292 if (Diags) \
293 Diags->Report(diag::err_pch_langopt_value_mismatch) \
294 << Description; \
295 return true; \
296 }
297
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000298#define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
299 if (!AllowCompatibleDifferences) \
300 LANGOPT(Name, Bits, Default, Description)
301
302#define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
303 if (!AllowCompatibleDifferences) \
304 ENUM_LANGOPT(Name, Bits, Default, Description)
305
Richard Smitha1ddf5e2016-04-07 20:47:37 +0000306#define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
307 if (!AllowCompatibleDifferences) \
308 VALUE_LANGOPT(Name, Bits, Default, Description)
309
Guy Benyei11169dd2012-12-18 14:30:41 +0000310#define BENIGN_LANGOPT(Name, Bits, Default, Description)
311#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
Richard Smitha1ddf5e2016-04-07 20:47:37 +0000312#define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
Guy Benyei11169dd2012-12-18 14:30:41 +0000313#include "clang/Basic/LangOptions.def"
314
Ben Langmuircd98cb72015-06-23 18:20:18 +0000315 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
316 if (Diags)
317 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
318 return true;
319 }
320
Guy Benyei11169dd2012-12-18 14:30:41 +0000321 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
322 if (Diags)
323 Diags->Report(diag::err_pch_langopt_value_mismatch)
324 << "target Objective-C runtime";
325 return true;
326 }
327
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +0000328 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
329 LangOpts.CommentOpts.BlockCommandNames) {
330 if (Diags)
331 Diags->Report(diag::err_pch_langopt_value_mismatch)
332 << "block command names";
333 return true;
334 }
335
Vedant Kumar85a83c22017-06-01 20:01:01 +0000336 // Sanitizer feature mismatches are treated as compatible differences. If
337 // compatible differences aren't allowed, we still only want to check for
338 // mismatches of non-modular sanitizers (the only ones which can affect AST
339 // generation).
340 if (!AllowCompatibleDifferences) {
341 SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
342 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
343 SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
344 ExistingSanitizers.clear(ModularSanitizers);
345 ImportedSanitizers.clear(ModularSanitizers);
346 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
347 const std::string Flag = "-fsanitize=";
348 if (Diags) {
349#define SANITIZER(NAME, ID) \
350 { \
351 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
352 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
353 if (InExistingModule != InImportedModule) \
354 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \
355 << InExistingModule << (Flag + NAME); \
356 }
357#include "clang/Basic/Sanitizers.def"
358 }
359 return true;
360 }
361 }
362
Guy Benyei11169dd2012-12-18 14:30:41 +0000363 return false;
364}
365
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000366/// Compare the given set of target options against an existing set of
Guy Benyei11169dd2012-12-18 14:30:41 +0000367/// target options.
368///
369/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
370///
371/// \returns true if the target options mis-match, false otherwise.
372static bool checkTargetOptions(const TargetOptions &TargetOpts,
373 const TargetOptions &ExistingTargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000374 DiagnosticsEngine *Diags,
375 bool AllowCompatibleDifferences = true) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000376#define CHECK_TARGET_OPT(Field, Name) \
377 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
378 if (Diags) \
379 Diags->Report(diag::err_pch_targetopt_mismatch) \
380 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
381 return true; \
382 }
383
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000384 // The triple and ABI must match exactly.
Guy Benyei11169dd2012-12-18 14:30:41 +0000385 CHECK_TARGET_OPT(Triple, "target");
Guy Benyei11169dd2012-12-18 14:30:41 +0000386 CHECK_TARGET_OPT(ABI, "target ABI");
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000387
388 // We can tolerate different CPUs in many cases, notably when one CPU
389 // supports a strict superset of another. When allowing compatible
390 // differences skip this check.
391 if (!AllowCompatibleDifferences)
392 CHECK_TARGET_OPT(CPU, "target CPU");
393
Guy Benyei11169dd2012-12-18 14:30:41 +0000394#undef CHECK_TARGET_OPT
395
396 // Compare feature sets.
397 SmallVector<StringRef, 4> ExistingFeatures(
398 ExistingTargetOpts.FeaturesAsWritten.begin(),
399 ExistingTargetOpts.FeaturesAsWritten.end());
400 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
401 TargetOpts.FeaturesAsWritten.end());
Fangrui Song55fab262018-09-26 22:16:28 +0000402 llvm::sort(ExistingFeatures);
403 llvm::sort(ReadFeatures);
Guy Benyei11169dd2012-12-18 14:30:41 +0000404
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000405 // We compute the set difference in both directions explicitly so that we can
406 // diagnose the differences differently.
407 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
408 std::set_difference(
409 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
410 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
411 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
412 ExistingFeatures.begin(), ExistingFeatures.end(),
413 std::back_inserter(UnmatchedReadFeatures));
Guy Benyei11169dd2012-12-18 14:30:41 +0000414
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000415 // If we are allowing compatible differences and the read feature set is
416 // a strict subset of the existing feature set, there is nothing to diagnose.
417 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
418 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +0000419
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000420 if (Diags) {
421 for (StringRef Feature : UnmatchedReadFeatures)
Guy Benyei11169dd2012-12-18 14:30:41 +0000422 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000423 << /* is-existing-feature */ false << Feature;
424 for (StringRef Feature : UnmatchedExistingFeatures)
425 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
426 << /* is-existing-feature */ true << Feature;
Guy Benyei11169dd2012-12-18 14:30:41 +0000427 }
428
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000429 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +0000430}
431
432bool
433PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000434 bool Complain,
435 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000436 const LangOptions &ExistingLangOpts = PP.getLangOpts();
437 return checkLanguageOptions(LangOpts, ExistingLangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000438 Complain ? &Reader.Diags : nullptr,
439 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +0000440}
441
442bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000443 bool Complain,
444 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000445 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
446 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000447 Complain ? &Reader.Diags : nullptr,
448 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +0000449}
450
451namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000452
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000453using MacroDefinitionsMap =
454 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
455using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000456
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000457} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +0000458
Ben Langmuirb92de022014-04-29 16:25:26 +0000459static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
460 DiagnosticsEngine &Diags,
461 bool Complain) {
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000462 using Level = DiagnosticsEngine::Level;
Ben Langmuirb92de022014-04-29 16:25:26 +0000463
464 // Check current mappings for new -Werror mappings, and the stored mappings
465 // for cases that were explicitly mapped to *not* be errors that are now
466 // errors because of options like -Werror.
467 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
468
Vedant Kumar48b4f762018-04-14 01:40:48 +0000469 for (DiagnosticsEngine *MappingSource : MappingSources) {
Ben Langmuirb92de022014-04-29 16:25:26 +0000470 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
471 diag::kind DiagID = DiagIDMappingPair.first;
472 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
473 if (CurLevel < DiagnosticsEngine::Error)
474 continue; // not significant
475 Level StoredLevel =
476 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
477 if (StoredLevel < DiagnosticsEngine::Error) {
478 if (Complain)
479 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
480 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
481 return true;
482 }
483 }
484 }
485
486 return false;
487}
488
Alp Tokerac4e8e52014-06-22 21:58:33 +0000489static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
490 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
491 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
492 return true;
493 return Ext >= diag::Severity::Error;
Ben Langmuirb92de022014-04-29 16:25:26 +0000494}
495
496static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
497 DiagnosticsEngine &Diags,
498 bool IsSystem, bool Complain) {
499 // Top-level options
500 if (IsSystem) {
501 if (Diags.getSuppressSystemWarnings())
502 return false;
503 // If -Wsystem-headers was not enabled before, be conservative
504 if (StoredDiags.getSuppressSystemWarnings()) {
505 if (Complain)
506 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
507 return true;
508 }
509 }
510
511 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
512 if (Complain)
513 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
514 return true;
515 }
516
517 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
518 !StoredDiags.getEnableAllWarnings()) {
519 if (Complain)
520 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
521 return true;
522 }
523
524 if (isExtHandlingFromDiagsError(Diags) &&
525 !isExtHandlingFromDiagsError(StoredDiags)) {
526 if (Complain)
527 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
528 return true;
529 }
530
531 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
532}
533
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +0000534/// Return the top import module if it is implicit, nullptr otherwise.
535static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
536 Preprocessor &PP) {
537 // If the original import came from a file explicitly generated by the user,
538 // don't check the diagnostic mappings.
539 // FIXME: currently this is approximated by checking whether this is not a
540 // module import of an implicitly-loaded module file.
541 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
542 // the transitive closure of its imports, since unrelated modules cannot be
543 // imported until after this module finishes validation.
544 ModuleFile *TopImport = &*ModuleMgr.rbegin();
545 while (!TopImport->ImportedBy.empty())
546 TopImport = TopImport->ImportedBy[0];
547 if (TopImport->Kind != MK_ImplicitModule)
548 return nullptr;
549
550 StringRef ModuleName = TopImport->ModuleName;
551 assert(!ModuleName.empty() && "diagnostic options read before module name");
552
553 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
554 assert(M && "missing module");
555 return M;
556}
557
Ben Langmuirb92de022014-04-29 16:25:26 +0000558bool PCHValidator::ReadDiagnosticOptions(
559 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
560 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
561 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
562 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Alp Tokerf994cef2014-07-05 03:08:06 +0000563 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
Ben Langmuirb92de022014-04-29 16:25:26 +0000564 // This should never fail, because we would have processed these options
565 // before writing them to an ASTFile.
566 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
567
568 ModuleManager &ModuleMgr = Reader.getModuleManager();
569 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
570
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +0000571 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
572 if (!TopM)
Ben Langmuirb92de022014-04-29 16:25:26 +0000573 return false;
574
Ben Langmuirb92de022014-04-29 16:25:26 +0000575 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
576 // contains the union of their flags.
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +0000577 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
578 Complain);
Ben Langmuirb92de022014-04-29 16:25:26 +0000579}
580
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000581/// Collect the macro definitions provided by the given preprocessor
Guy Benyei11169dd2012-12-18 14:30:41 +0000582/// options.
Craig Toppera13603a2014-05-22 05:54:18 +0000583static void
584collectMacroDefinitions(const PreprocessorOptions &PPOpts,
585 MacroDefinitionsMap &Macros,
586 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
Vedant Kumar48b4f762018-04-14 01:40:48 +0000587 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
588 StringRef Macro = PPOpts.Macros[I].first;
589 bool IsUndef = PPOpts.Macros[I].second;
Guy Benyei11169dd2012-12-18 14:30:41 +0000590
591 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
592 StringRef MacroName = MacroPair.first;
593 StringRef MacroBody = MacroPair.second;
594
595 // For an #undef'd macro, we only care about the name.
596 if (IsUndef) {
597 if (MacroNames && !Macros.count(MacroName))
598 MacroNames->push_back(MacroName);
599
600 Macros[MacroName] = std::make_pair("", true);
601 continue;
602 }
603
604 // For a #define'd macro, figure out the actual definition.
605 if (MacroName.size() == Macro.size())
606 MacroBody = "1";
607 else {
608 // Note: GCC drops anything following an end-of-line character.
609 StringRef::size_type End = MacroBody.find_first_of("\n\r");
610 MacroBody = MacroBody.substr(0, End);
611 }
612
613 if (MacroNames && !Macros.count(MacroName))
614 MacroNames->push_back(MacroName);
615 Macros[MacroName] = std::make_pair(MacroBody, false);
616 }
617}
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000618
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000619/// Check the preprocessor options deserialized from the control block
Guy Benyei11169dd2012-12-18 14:30:41 +0000620/// against the preprocessor options in an existing preprocessor.
621///
622/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
Yaxun Liu43712e02016-09-07 18:40:20 +0000623/// \param Validate If true, validate preprocessor options. If false, allow
624/// macros defined by \p ExistingPPOpts to override those defined by
625/// \p PPOpts in SuggestedPredefines.
Guy Benyei11169dd2012-12-18 14:30:41 +0000626static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
627 const PreprocessorOptions &ExistingPPOpts,
628 DiagnosticsEngine *Diags,
629 FileManager &FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000630 std::string &SuggestedPredefines,
Yaxun Liu43712e02016-09-07 18:40:20 +0000631 const LangOptions &LangOpts,
632 bool Validate = true) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000633 // Check macro definitions.
634 MacroDefinitionsMap ASTFileMacros;
635 collectMacroDefinitions(PPOpts, ASTFileMacros);
636 MacroDefinitionsMap ExistingMacros;
637 SmallVector<StringRef, 4> ExistingMacroNames;
638 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
639
Vedant Kumar48b4f762018-04-14 01:40:48 +0000640 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000641 // Dig out the macro definition in the existing preprocessor options.
Vedant Kumar48b4f762018-04-14 01:40:48 +0000642 StringRef MacroName = ExistingMacroNames[I];
Guy Benyei11169dd2012-12-18 14:30:41 +0000643 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
644
645 // Check whether we know anything about this macro name or not.
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000646 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
647 ASTFileMacros.find(MacroName);
Yaxun Liu43712e02016-09-07 18:40:20 +0000648 if (!Validate || Known == ASTFileMacros.end()) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000649 // FIXME: Check whether this identifier was referenced anywhere in the
650 // AST file. If so, we should reject the AST file. Unfortunately, this
651 // information isn't in the control block. What shall we do about it?
652
653 if (Existing.second) {
654 SuggestedPredefines += "#undef ";
655 SuggestedPredefines += MacroName.str();
656 SuggestedPredefines += '\n';
657 } else {
658 SuggestedPredefines += "#define ";
659 SuggestedPredefines += MacroName.str();
660 SuggestedPredefines += ' ';
661 SuggestedPredefines += Existing.first.str();
662 SuggestedPredefines += '\n';
663 }
664 continue;
665 }
666
667 // If the macro was defined in one but undef'd in the other, we have a
668 // conflict.
669 if (Existing.second != Known->second.second) {
670 if (Diags) {
671 Diags->Report(diag::err_pch_macro_def_undef)
672 << MacroName << Known->second.second;
673 }
674 return true;
675 }
676
677 // If the macro was #undef'd in both, or if the macro bodies are identical,
678 // it's fine.
679 if (Existing.second || Existing.first == Known->second.first)
680 continue;
681
682 // The macro bodies differ; complain.
683 if (Diags) {
684 Diags->Report(diag::err_pch_macro_def_conflict)
685 << MacroName << Known->second.first << Existing.first;
686 }
687 return true;
688 }
689
690 // Check whether we're using predefines.
Yaxun Liu43712e02016-09-07 18:40:20 +0000691 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000692 if (Diags) {
693 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
694 }
695 return true;
696 }
697
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000698 // Detailed record is important since it is used for the module cache hash.
699 if (LangOpts.Modules &&
Yaxun Liu43712e02016-09-07 18:40:20 +0000700 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000701 if (Diags) {
702 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
703 }
704 return true;
705 }
706
Guy Benyei11169dd2012-12-18 14:30:41 +0000707 // Compute the #include and #include_macros lines we need.
Vedant Kumar48b4f762018-04-14 01:40:48 +0000708 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
709 StringRef File = ExistingPPOpts.Includes[I];
Erich Keane76675de2018-07-05 17:22:13 +0000710
711 if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
712 !ExistingPPOpts.PCHThroughHeader.empty()) {
713 // In case the through header is an include, we must add all the includes
714 // to the predefines so the start point can be determined.
715 SuggestedPredefines += "#include \"";
716 SuggestedPredefines += File;
717 SuggestedPredefines += "\"\n";
718 continue;
719 }
720
Guy Benyei11169dd2012-12-18 14:30:41 +0000721 if (File == ExistingPPOpts.ImplicitPCHInclude)
722 continue;
723
724 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
725 != PPOpts.Includes.end())
726 continue;
727
728 SuggestedPredefines += "#include \"";
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000729 SuggestedPredefines += File;
Guy Benyei11169dd2012-12-18 14:30:41 +0000730 SuggestedPredefines += "\"\n";
731 }
732
Vedant Kumar48b4f762018-04-14 01:40:48 +0000733 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
734 StringRef File = ExistingPPOpts.MacroIncludes[I];
Guy Benyei11169dd2012-12-18 14:30:41 +0000735 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
736 File)
737 != PPOpts.MacroIncludes.end())
738 continue;
739
740 SuggestedPredefines += "#__include_macros \"";
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000741 SuggestedPredefines += File;
Guy Benyei11169dd2012-12-18 14:30:41 +0000742 SuggestedPredefines += "\"\n##\n";
743 }
744
745 return false;
746}
747
748bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
749 bool Complain,
750 std::string &SuggestedPredefines) {
751 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
752
753 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
Craig Toppera13603a2014-05-22 05:54:18 +0000754 Complain? &Reader.Diags : nullptr,
Guy Benyei11169dd2012-12-18 14:30:41 +0000755 PP.getFileManager(),
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000756 SuggestedPredefines,
757 PP.getLangOpts());
Guy Benyei11169dd2012-12-18 14:30:41 +0000758}
759
Yaxun Liu43712e02016-09-07 18:40:20 +0000760bool SimpleASTReaderListener::ReadPreprocessorOptions(
761 const PreprocessorOptions &PPOpts,
762 bool Complain,
763 std::string &SuggestedPredefines) {
764 return checkPreprocessorOptions(PPOpts,
765 PP.getPreprocessorOpts(),
766 nullptr,
767 PP.getFileManager(),
768 SuggestedPredefines,
769 PP.getLangOpts(),
770 false);
771}
772
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000773/// Check the header search options deserialized from the control block
774/// against the header search options in an existing preprocessor.
775///
776/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
777static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
778 StringRef SpecificModuleCachePath,
779 StringRef ExistingModuleCachePath,
780 DiagnosticsEngine *Diags,
781 const LangOptions &LangOpts) {
782 if (LangOpts.Modules) {
783 if (SpecificModuleCachePath != ExistingModuleCachePath) {
784 if (Diags)
785 Diags->Report(diag::err_pch_modulecache_mismatch)
786 << SpecificModuleCachePath << ExistingModuleCachePath;
787 return true;
788 }
789 }
790
791 return false;
792}
793
794bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
795 StringRef SpecificModuleCachePath,
796 bool Complain) {
797 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
798 PP.getHeaderSearchInfo().getModuleCachePath(),
799 Complain ? &Reader.Diags : nullptr,
800 PP.getLangOpts());
801}
802
Guy Benyei11169dd2012-12-18 14:30:41 +0000803void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
804 PP.setCounterValue(Value);
805}
806
807//===----------------------------------------------------------------------===//
808// AST reader implementation
809//===----------------------------------------------------------------------===//
810
Nico Weber824285e2014-05-08 04:26:47 +0000811void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
812 bool TakeOwnership) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000813 DeserializationListener = Listener;
Nico Weber824285e2014-05-08 04:26:47 +0000814 OwnsDeserializationListener = TakeOwnership;
Guy Benyei11169dd2012-12-18 14:30:41 +0000815}
816
Guy Benyei11169dd2012-12-18 14:30:41 +0000817unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
818 return serialization::ComputeHash(Sel);
819}
820
Guy Benyei11169dd2012-12-18 14:30:41 +0000821std::pair<unsigned, unsigned>
822ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000823 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000824
Justin Bogner57ba0b22014-03-28 22:03:24 +0000825 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
826 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000827 return std::make_pair(KeyLen, DataLen);
828}
829
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000830ASTSelectorLookupTrait::internal_key_type
Guy Benyei11169dd2012-12-18 14:30:41 +0000831ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000832 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000833
Guy Benyei11169dd2012-12-18 14:30:41 +0000834 SelectorTable &SelTable = Reader.getContext().Selectors;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000835 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
836 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
837 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000838 if (N == 0)
839 return SelTable.getNullarySelector(FirstII);
840 else if (N == 1)
841 return SelTable.getUnarySelector(FirstII);
842
843 SmallVector<IdentifierInfo *, 16> Args;
844 Args.push_back(FirstII);
845 for (unsigned I = 1; I != N; ++I)
Justin Bogner57ba0b22014-03-28 22:03:24 +0000846 Args.push_back(Reader.getLocalIdentifier(
847 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000848
849 return SelTable.getSelector(N, Args.data());
850}
851
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000852ASTSelectorLookupTrait::data_type
853ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
Guy Benyei11169dd2012-12-18 14:30:41 +0000854 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000855 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000856
857 data_type Result;
858
Justin Bogner57ba0b22014-03-28 22:03:24 +0000859 Result.ID = Reader.getGlobalSelectorID(
860 F, endian::readNext<uint32_t, little, unaligned>(d));
Nico Weberff4b35e2014-12-27 22:14:15 +0000861 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
862 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
863 Result.InstanceBits = FullInstanceBits & 0x3;
864 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
865 Result.FactoryBits = FullFactoryBits & 0x3;
866 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
867 unsigned NumInstanceMethods = FullInstanceBits >> 3;
868 unsigned NumFactoryMethods = FullFactoryBits >> 3;
Guy Benyei11169dd2012-12-18 14:30:41 +0000869
870 // Load instance methods
871 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Vedant Kumar48b4f762018-04-14 01:40:48 +0000872 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
Justin Bogner57ba0b22014-03-28 22:03:24 +0000873 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei11169dd2012-12-18 14:30:41 +0000874 Result.Instance.push_back(Method);
875 }
876
877 // Load factory methods
878 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Vedant Kumar48b4f762018-04-14 01:40:48 +0000879 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
Justin Bogner57ba0b22014-03-28 22:03:24 +0000880 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei11169dd2012-12-18 14:30:41 +0000881 Result.Factory.push_back(Method);
882 }
883
884 return Result;
885}
886
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000887unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000888 return llvm::djbHash(a);
Guy Benyei11169dd2012-12-18 14:30:41 +0000889}
890
891std::pair<unsigned, unsigned>
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000892ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000893 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000894
Justin Bogner57ba0b22014-03-28 22:03:24 +0000895 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
896 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000897 return std::make_pair(KeyLen, DataLen);
898}
899
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000900ASTIdentifierLookupTraitBase::internal_key_type
901ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000902 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000903 return StringRef((const char*) d, n-1);
Guy Benyei11169dd2012-12-18 14:30:41 +0000904}
905
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000906/// Whether the given identifier is "interesting".
Richard Smitha534a312015-07-21 23:54:07 +0000907static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
908 bool IsModule) {
Richard Smithcab89802015-07-17 20:19:56 +0000909 return II.hadMacroDefinition() ||
910 II.isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +0000911 (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
Douglas Gregordcf25082013-02-11 18:16:18 +0000912 II.hasRevertedTokenIDToIdentifier() ||
Richard Smithdbafb6c2017-06-29 23:23:46 +0000913 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
Bruno Ricci366ba732018-09-21 12:53:22 +0000914 II.getFETokenInfo());
Douglas Gregordcf25082013-02-11 18:16:18 +0000915}
916
Richard Smith76c2f2c2015-07-17 20:09:43 +0000917static bool readBit(unsigned &Bits) {
918 bool Value = Bits & 0x1;
919 Bits >>= 1;
920 return Value;
921}
922
Richard Smith79bf9202015-08-24 03:33:22 +0000923IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
924 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000925
Richard Smith79bf9202015-08-24 03:33:22 +0000926 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
927 return Reader.getGlobalIdentifierID(F, RawID >> 1);
928}
929
Richard Smitheb4b58f62016-02-05 01:40:54 +0000930static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
931 if (!II.isFromAST()) {
932 II.setIsFromAST();
933 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
934 if (isInterestingIdentifier(Reader, II, IsModule))
935 II.setChangedSinceDeserialization();
936 }
937}
938
Guy Benyei11169dd2012-12-18 14:30:41 +0000939IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
940 const unsigned char* d,
941 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000942 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000943
Justin Bogner57ba0b22014-03-28 22:03:24 +0000944 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000945 bool IsInteresting = RawID & 0x01;
946
947 // Wipe out the "is interesting" bit.
948 RawID = RawID >> 1;
949
Richard Smith76c2f2c2015-07-17 20:09:43 +0000950 // Build the IdentifierInfo and link the identifier ID with it.
951 IdentifierInfo *II = KnownII;
952 if (!II) {
953 II = &Reader.getIdentifierTable().getOwn(k);
954 KnownII = II;
955 }
Richard Smitheb4b58f62016-02-05 01:40:54 +0000956 markIdentifierFromAST(Reader, *II);
Richard Smith76c2f2c2015-07-17 20:09:43 +0000957 Reader.markIdentifierUpToDate(II);
958
Guy Benyei11169dd2012-12-18 14:30:41 +0000959 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
960 if (!IsInteresting) {
Richard Smith76c2f2c2015-07-17 20:09:43 +0000961 // For uninteresting identifiers, there's nothing else to do. Just notify
962 // the reader that we've finished loading this identifier.
Guy Benyei11169dd2012-12-18 14:30:41 +0000963 Reader.SetIdentifierInfo(ID, II);
Guy Benyei11169dd2012-12-18 14:30:41 +0000964 return II;
965 }
966
Justin Bogner57ba0b22014-03-28 22:03:24 +0000967 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
968 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
Richard Smith76c2f2c2015-07-17 20:09:43 +0000969 bool CPlusPlusOperatorKeyword = readBit(Bits);
970 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
Richard Smith9c254182015-07-19 21:41:12 +0000971 bool HasRevertedBuiltin = readBit(Bits);
Richard Smith76c2f2c2015-07-17 20:09:43 +0000972 bool Poisoned = readBit(Bits);
973 bool ExtensionToken = readBit(Bits);
974 bool HadMacroDefinition = readBit(Bits);
Guy Benyei11169dd2012-12-18 14:30:41 +0000975
976 assert(Bits == 0 && "Extra bits in the identifier?");
977 DataLen -= 8;
978
Guy Benyei11169dd2012-12-18 14:30:41 +0000979 // Set or check the various bits in the IdentifierInfo structure.
980 // Token IDs are read-only.
Argyrios Kyrtzidisddee8c92013-02-27 01:13:51 +0000981 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Richard Smith9c254182015-07-19 21:41:12 +0000982 II->revertTokenIDToIdentifier();
983 if (!F.isModule())
984 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
985 else if (HasRevertedBuiltin && II->getBuiltinID()) {
986 II->revertBuiltin();
987 assert((II->hasRevertedBuiltin() ||
988 II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
989 "Incorrect ObjC keyword or builtin ID");
990 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000991 assert(II->isExtensionToken() == ExtensionToken &&
992 "Incorrect extension token flag");
993 (void)ExtensionToken;
994 if (Poisoned)
995 II->setIsPoisoned(true);
996 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
997 "Incorrect C++ operator keyword flag");
998 (void)CPlusPlusOperatorKeyword;
999
1000 // If this identifier is a macro, deserialize the macro
1001 // definition.
Richard Smith76c2f2c2015-07-17 20:09:43 +00001002 if (HadMacroDefinition) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001003 uint32_t MacroDirectivesOffset =
1004 endian::readNext<uint32_t, little, unaligned>(d);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001005 DataLen -= 4;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001006
Richard Smithd7329392015-04-21 21:46:32 +00001007 Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00001008 }
1009
1010 Reader.SetIdentifierInfo(ID, II);
1011
1012 // Read all of the declarations visible at global scope with this
1013 // name.
1014 if (DataLen > 0) {
1015 SmallVector<uint32_t, 4> DeclIDs;
1016 for (; DataLen > 0; DataLen -= 4)
Justin Bogner57ba0b22014-03-28 22:03:24 +00001017 DeclIDs.push_back(Reader.getGlobalDeclID(
1018 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +00001019 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1020 }
1021
1022 return II;
1023}
1024
Richard Smitha06c7e62015-08-26 23:55:49 +00001025DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1026 : Kind(Name.getNameKind()) {
1027 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001028 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00001029 Data = (uint64_t)Name.getAsIdentifierInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +00001030 break;
1031 case DeclarationName::ObjCZeroArgSelector:
1032 case DeclarationName::ObjCOneArgSelector:
1033 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00001034 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
Guy Benyei11169dd2012-12-18 14:30:41 +00001035 break;
1036 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00001037 Data = Name.getCXXOverloadedOperator();
1038 break;
1039 case DeclarationName::CXXLiteralOperatorName:
1040 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1041 break;
Richard Smith35845152017-02-07 01:37:30 +00001042 case DeclarationName::CXXDeductionGuideName:
1043 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1044 ->getDeclName().getAsIdentifierInfo();
1045 break;
Richard Smitha06c7e62015-08-26 23:55:49 +00001046 case DeclarationName::CXXConstructorName:
1047 case DeclarationName::CXXDestructorName:
1048 case DeclarationName::CXXConversionFunctionName:
1049 case DeclarationName::CXXUsingDirective:
1050 Data = 0;
1051 break;
1052 }
1053}
1054
1055unsigned DeclarationNameKey::getHash() const {
1056 llvm::FoldingSetNodeID ID;
1057 ID.AddInteger(Kind);
1058
1059 switch (Kind) {
1060 case DeclarationName::Identifier:
1061 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00001062 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00001063 ID.AddString(((IdentifierInfo*)Data)->getName());
1064 break;
1065 case DeclarationName::ObjCZeroArgSelector:
1066 case DeclarationName::ObjCOneArgSelector:
1067 case DeclarationName::ObjCMultiArgSelector:
1068 ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1069 break;
1070 case DeclarationName::CXXOperatorName:
1071 ID.AddInteger((OverloadedOperatorKind)Data);
Guy Benyei11169dd2012-12-18 14:30:41 +00001072 break;
1073 case DeclarationName::CXXConstructorName:
1074 case DeclarationName::CXXDestructorName:
1075 case DeclarationName::CXXConversionFunctionName:
1076 case DeclarationName::CXXUsingDirective:
1077 break;
1078 }
1079
1080 return ID.ComputeHash();
1081}
1082
Richard Smithd88a7f12015-09-01 20:35:42 +00001083ModuleFile *
1084ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1085 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001086
Richard Smithd88a7f12015-09-01 20:35:42 +00001087 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1088 return Reader.getLocalModuleFile(F, ModuleFileID);
1089}
1090
Guy Benyei11169dd2012-12-18 14:30:41 +00001091std::pair<unsigned, unsigned>
Richard Smitha06c7e62015-08-26 23:55:49 +00001092ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001093 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001094
Justin Bogner57ba0b22014-03-28 22:03:24 +00001095 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1096 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +00001097 return std::make_pair(KeyLen, DataLen);
1098}
1099
Richard Smitha06c7e62015-08-26 23:55:49 +00001100ASTDeclContextNameLookupTrait::internal_key_type
1101ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001102 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +00001103
Vedant Kumar48b4f762018-04-14 01:40:48 +00001104 auto Kind = (DeclarationName::NameKind)*d++;
Richard Smitha06c7e62015-08-26 23:55:49 +00001105 uint64_t Data;
1106 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001107 case DeclarationName::Identifier:
Richard Smith35845152017-02-07 01:37:30 +00001108 case DeclarationName::CXXLiteralOperatorName:
1109 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00001110 Data = (uint64_t)Reader.getLocalIdentifier(
Justin Bogner57ba0b22014-03-28 22:03:24 +00001111 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +00001112 break;
1113 case DeclarationName::ObjCZeroArgSelector:
1114 case DeclarationName::ObjCOneArgSelector:
1115 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00001116 Data =
Justin Bogner57ba0b22014-03-28 22:03:24 +00001117 (uint64_t)Reader.getLocalSelector(
1118 F, endian::readNext<uint32_t, little, unaligned>(
1119 d)).getAsOpaquePtr();
Guy Benyei11169dd2012-12-18 14:30:41 +00001120 break;
1121 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00001122 Data = *d++; // OverloadedOperatorKind
Guy Benyei11169dd2012-12-18 14:30:41 +00001123 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001124 case DeclarationName::CXXConstructorName:
1125 case DeclarationName::CXXDestructorName:
1126 case DeclarationName::CXXConversionFunctionName:
1127 case DeclarationName::CXXUsingDirective:
Richard Smitha06c7e62015-08-26 23:55:49 +00001128 Data = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001129 break;
1130 }
1131
Richard Smitha06c7e62015-08-26 23:55:49 +00001132 return DeclarationNameKey(Kind, Data);
Guy Benyei11169dd2012-12-18 14:30:41 +00001133}
1134
Richard Smithd88a7f12015-09-01 20:35:42 +00001135void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1136 const unsigned char *d,
1137 unsigned DataLen,
1138 data_type_builder &Val) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001139 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001140
Richard Smithd88a7f12015-09-01 20:35:42 +00001141 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1142 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1143 Val.insert(Reader.getGlobalDeclID(F, LocalID));
1144 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001145}
1146
Richard Smith0f4e2c42015-08-06 04:23:48 +00001147bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1148 BitstreamCursor &Cursor,
1149 uint64_t Offset,
1150 DeclContext *DC) {
1151 assert(Offset != 0);
1152
Guy Benyei11169dd2012-12-18 14:30:41 +00001153 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00001154 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1155 Error(std::move(Err));
1156 return true;
1157 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001158
Richard Smith0f4e2c42015-08-06 04:23:48 +00001159 RecordData Record;
1160 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00001161 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1162 if (!MaybeCode) {
1163 Error(MaybeCode.takeError());
1164 return true;
1165 }
1166 unsigned Code = MaybeCode.get();
1167
1168 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1169 if (!MaybeRecCode) {
1170 Error(MaybeRecCode.takeError());
1171 return true;
1172 }
1173 unsigned RecCode = MaybeRecCode.get();
Richard Smith0f4e2c42015-08-06 04:23:48 +00001174 if (RecCode != DECL_CONTEXT_LEXICAL) {
1175 Error("Expected lexical block");
1176 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001177 }
1178
Richard Smith82f8fcd2015-08-06 22:07:25 +00001179 assert(!isa<TranslationUnitDecl>(DC) &&
1180 "expected a TU_UPDATE_LEXICAL record for TU");
Richard Smith9c9173d2015-08-11 22:00:24 +00001181 // If we are handling a C++ class template instantiation, we can see multiple
1182 // lexical updates for the same record. It's important that we select only one
1183 // of them, so that field numbering works properly. Just pick the first one we
1184 // see.
1185 auto &Lex = LexicalDecls[DC];
1186 if (!Lex.first) {
1187 Lex = std::make_pair(
1188 &M, llvm::makeArrayRef(
1189 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1190 Blob.data()),
1191 Blob.size() / 4));
1192 }
Richard Smith0f4e2c42015-08-06 04:23:48 +00001193 DC->setHasExternalLexicalStorage(true);
1194 return false;
1195}
Guy Benyei11169dd2012-12-18 14:30:41 +00001196
Richard Smith0f4e2c42015-08-06 04:23:48 +00001197bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1198 BitstreamCursor &Cursor,
1199 uint64_t Offset,
1200 DeclID ID) {
1201 assert(Offset != 0);
1202
1203 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00001204 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1205 Error(std::move(Err));
1206 return true;
1207 }
Richard Smith0f4e2c42015-08-06 04:23:48 +00001208
1209 RecordData Record;
1210 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00001211 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1212 if (!MaybeCode) {
1213 Error(MaybeCode.takeError());
1214 return true;
1215 }
1216 unsigned Code = MaybeCode.get();
1217
1218 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1219 if (!MaybeRecCode) {
1220 Error(MaybeRecCode.takeError());
1221 return true;
1222 }
1223 unsigned RecCode = MaybeRecCode.get();
Richard Smith0f4e2c42015-08-06 04:23:48 +00001224 if (RecCode != DECL_CONTEXT_VISIBLE) {
1225 Error("Expected visible lookup table block");
1226 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001227 }
1228
Richard Smith0f4e2c42015-08-06 04:23:48 +00001229 // We can't safely determine the primary context yet, so delay attaching the
1230 // lookup table until we're done with recursive deserialization.
Richard Smithd88a7f12015-09-01 20:35:42 +00001231 auto *Data = (const unsigned char*)Blob.data();
1232 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
Guy Benyei11169dd2012-12-18 14:30:41 +00001233 return false;
1234}
1235
Richard Smith37a93df2017-02-18 00:32:02 +00001236void ASTReader::Error(StringRef Msg) const {
Guy Benyei11169dd2012-12-18 14:30:41 +00001237 Error(diag::err_fe_pch_malformed, Msg);
Richard Smithdbafb6c2017-06-29 23:23:46 +00001238 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
Richard Smithfb1e7f72015-08-14 05:02:58 +00001239 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
Douglas Gregor940e8052013-05-10 22:15:13 +00001240 Diag(diag::note_module_cache_path)
1241 << PP.getHeaderSearchInfo().getModuleCachePath();
1242 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001243}
1244
Duncan P. N. Exon Smitheef69022019-11-10 11:17:42 -08001245void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1246 StringRef Arg3) const {
Guy Benyei11169dd2012-12-18 14:30:41 +00001247 if (Diags.isDiagnosticInFlight())
Duncan P. N. Exon Smitheef69022019-11-10 11:17:42 -08001248 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
Guy Benyei11169dd2012-12-18 14:30:41 +00001249 else
Duncan P. N. Exon Smitheef69022019-11-10 11:17:42 -08001250 Diag(DiagID) << Arg1 << Arg2 << Arg3;
Guy Benyei11169dd2012-12-18 14:30:41 +00001251}
1252
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00001253void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1254 unsigned Select) const {
1255 if (!Diags.isDiagnosticInFlight())
1256 Diag(DiagID) << Arg1 << Arg2 << Select;
1257}
1258
JF Bastien0e828952019-06-26 19:50:12 +00001259void ASTReader::Error(llvm::Error &&Err) const {
1260 Error(toString(std::move(Err)));
1261}
1262
Guy Benyei11169dd2012-12-18 14:30:41 +00001263//===----------------------------------------------------------------------===//
1264// Source Manager Deserialization
1265//===----------------------------------------------------------------------===//
1266
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001267/// Read the line table in the source manager block.
Guy Benyei11169dd2012-12-18 14:30:41 +00001268/// \returns true if there was an error.
1269bool ASTReader::ParseLineTable(ModuleFile &F,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001270 const RecordData &Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001271 unsigned Idx = 0;
1272 LineTableInfo &LineTable = SourceMgr.getLineTable();
1273
1274 // Parse the file names
1275 std::map<int, int> FileIDs;
Hans Wennborg14487362017-12-04 22:28:45 +00001276 FileIDs[-1] = -1; // For unspecified filenames.
Richard Smith63078492015-09-01 07:41:55 +00001277 for (unsigned I = 0; Record[Idx]; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001278 // Extract the file name
Richard Smith7ed1bc92014-12-05 22:42:13 +00001279 auto Filename = ReadPath(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001280 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1281 }
Richard Smith63078492015-09-01 07:41:55 +00001282 ++Idx;
Guy Benyei11169dd2012-12-18 14:30:41 +00001283
1284 // Parse the line entries
1285 std::vector<LineEntry> Entries;
1286 while (Idx < Record.size()) {
1287 int FID = Record[Idx++];
1288 assert(FID >= 0 && "Serialized line entries for non-local file.");
1289 // Remap FileID from 1-based old view.
1290 FID += F.SLocEntryBaseID - 1;
1291
1292 // Extract the line entries
1293 unsigned NumEntries = Record[Idx++];
Richard Smith63078492015-09-01 07:41:55 +00001294 assert(NumEntries && "no line entries for file ID");
Guy Benyei11169dd2012-12-18 14:30:41 +00001295 Entries.clear();
1296 Entries.reserve(NumEntries);
1297 for (unsigned I = 0; I != NumEntries; ++I) {
1298 unsigned FileOffset = Record[Idx++];
1299 unsigned LineNo = Record[Idx++];
1300 int FilenameID = FileIDs[Record[Idx++]];
Vedant Kumar48b4f762018-04-14 01:40:48 +00001301 SrcMgr::CharacteristicKind FileKind
1302 = (SrcMgr::CharacteristicKind)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00001303 unsigned IncludeOffset = Record[Idx++];
1304 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1305 FileKind, IncludeOffset));
1306 }
1307 LineTable.AddEntry(FileID::get(FID), Entries);
1308 }
1309
1310 return false;
1311}
1312
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001313/// Read a source manager block
Guy Benyei11169dd2012-12-18 14:30:41 +00001314bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1315 using namespace SrcMgr;
1316
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001317 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001318
1319 // Set the source-location entry cursor to the current position in
1320 // the stream. This cursor will be used to read the contents of the
1321 // source manager block initially, and then lazily read
1322 // source-location entries as needed.
1323 SLocEntryCursor = F.Stream;
1324
1325 // The stream itself is going to skip over the source manager block.
JF Bastien0e828952019-06-26 19:50:12 +00001326 if (llvm::Error Err = F.Stream.SkipBlock()) {
1327 Error(std::move(Err));
Guy Benyei11169dd2012-12-18 14:30:41 +00001328 return true;
1329 }
1330
1331 // Enter the source manager block.
JF Bastien0e828952019-06-26 19:50:12 +00001332 if (llvm::Error Err =
1333 SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1334 Error(std::move(Err));
Guy Benyei11169dd2012-12-18 14:30:41 +00001335 return true;
1336 }
1337
1338 RecordData Record;
1339 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00001340 Expected<llvm::BitstreamEntry> MaybeE =
1341 SLocEntryCursor.advanceSkippingSubblocks();
1342 if (!MaybeE) {
1343 Error(MaybeE.takeError());
1344 return true;
1345 }
1346 llvm::BitstreamEntry E = MaybeE.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001347
Chris Lattnere7b154b2013-01-19 21:39:22 +00001348 switch (E.Kind) {
1349 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1350 case llvm::BitstreamEntry::Error:
1351 Error("malformed block record in AST file");
1352 return true;
1353 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00001354 return false;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001355 case llvm::BitstreamEntry::Record:
1356 // The interesting case.
1357 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001358 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001359
Guy Benyei11169dd2012-12-18 14:30:41 +00001360 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001361 Record.clear();
Chris Lattner15c3e7d2013-01-21 18:28:26 +00001362 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00001363 Expected<unsigned> MaybeRecord =
1364 SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1365 if (!MaybeRecord) {
1366 Error(MaybeRecord.takeError());
1367 return true;
1368 }
1369 switch (MaybeRecord.get()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001370 default: // Default behavior: ignore.
1371 break;
1372
1373 case SM_SLOC_FILE_ENTRY:
1374 case SM_SLOC_BUFFER_ENTRY:
1375 case SM_SLOC_EXPANSION_ENTRY:
1376 // Once we hit one of the source location entries, we're done.
1377 return false;
1378 }
1379 }
1380}
1381
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001382/// If a header file is not found at the path that we expect it to be
Guy Benyei11169dd2012-12-18 14:30:41 +00001383/// and the PCH file was moved from its original location, try to resolve the
1384/// file by assuming that header+PCH were moved together and the header is in
1385/// the same place relative to the PCH.
1386static std::string
1387resolveFileRelativeToOriginalDir(const std::string &Filename,
1388 const std::string &OriginalDir,
1389 const std::string &CurrDir) {
1390 assert(OriginalDir != CurrDir &&
1391 "No point trying to resolve the file if the PCH dir didn't change");
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001392
Guy Benyei11169dd2012-12-18 14:30:41 +00001393 using namespace llvm::sys;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001394
Guy Benyei11169dd2012-12-18 14:30:41 +00001395 SmallString<128> filePath(Filename);
1396 fs::make_absolute(filePath);
1397 assert(path::is_absolute(OriginalDir));
1398 SmallString<128> currPCHPath(CurrDir);
1399
1400 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1401 fileDirE = path::end(path::parent_path(filePath));
1402 path::const_iterator origDirI = path::begin(OriginalDir),
1403 origDirE = path::end(OriginalDir);
1404 // Skip the common path components from filePath and OriginalDir.
1405 while (fileDirI != fileDirE && origDirI != origDirE &&
1406 *fileDirI == *origDirI) {
1407 ++fileDirI;
1408 ++origDirI;
1409 }
1410 for (; origDirI != origDirE; ++origDirI)
1411 path::append(currPCHPath, "..");
1412 path::append(currPCHPath, fileDirI, fileDirE);
1413 path::append(currPCHPath, path::filename(Filename));
Benjamin Krameradcd0262020-01-28 20:23:46 +01001414 return std::string(currPCHPath.str());
Guy Benyei11169dd2012-12-18 14:30:41 +00001415}
1416
1417bool ASTReader::ReadSLocEntry(int ID) {
1418 if (ID == 0)
1419 return false;
1420
1421 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1422 Error("source location entry ID out-of-range for AST file");
1423 return true;
1424 }
1425
Richard Smithaada85c2016-02-06 02:06:43 +00001426 // Local helper to read the (possibly-compressed) buffer data following the
1427 // entry record.
1428 auto ReadBuffer = [this](
1429 BitstreamCursor &SLocEntryCursor,
1430 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1431 RecordData Record;
1432 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00001433 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1434 if (!MaybeCode) {
1435 Error(MaybeCode.takeError());
1436 return nullptr;
1437 }
1438 unsigned Code = MaybeCode.get();
1439
1440 Expected<unsigned> MaybeRecCode =
1441 SLocEntryCursor.readRecord(Code, Record, &Blob);
1442 if (!MaybeRecCode) {
1443 Error(MaybeRecCode.takeError());
1444 return nullptr;
1445 }
1446 unsigned RecCode = MaybeRecCode.get();
Richard Smithaada85c2016-02-06 02:06:43 +00001447
1448 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
George Rimarc39f5492017-01-17 15:45:31 +00001449 if (!llvm::zlib::isAvailable()) {
1450 Error("zlib is not available");
1451 return nullptr;
1452 }
Richard Smithaada85c2016-02-06 02:06:43 +00001453 SmallString<0> Uncompressed;
George Rimarc39f5492017-01-17 15:45:31 +00001454 if (llvm::Error E =
1455 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1456 Error("could not decompress embedded file contents: " +
1457 llvm::toString(std::move(E)));
Richard Smithaada85c2016-02-06 02:06:43 +00001458 return nullptr;
1459 }
1460 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1461 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1462 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1463 } else {
1464 Error("AST record has invalid code");
1465 return nullptr;
1466 }
1467 };
1468
Guy Benyei11169dd2012-12-18 14:30:41 +00001469 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
JF Bastien0e828952019-06-26 19:50:12 +00001470 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1471 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1472 Error(std::move(Err));
1473 return true;
1474 }
1475
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001476 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001477 unsigned BaseOffset = F->SLocEntryBaseOffset;
1478
1479 ++NumSLocEntriesRead;
JF Bastien0e828952019-06-26 19:50:12 +00001480 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1481 if (!MaybeEntry) {
1482 Error(MaybeEntry.takeError());
1483 return true;
1484 }
1485 llvm::BitstreamEntry Entry = MaybeEntry.get();
1486
Chris Lattnere7b154b2013-01-19 21:39:22 +00001487 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001488 Error("incorrectly-formatted source location entry in AST file");
1489 return true;
1490 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001491
Guy Benyei11169dd2012-12-18 14:30:41 +00001492 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +00001493 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00001494 Expected<unsigned> MaybeSLOC =
1495 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1496 if (!MaybeSLOC) {
1497 Error(MaybeSLOC.takeError());
1498 return true;
1499 }
1500 switch (MaybeSLOC.get()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001501 default:
1502 Error("incorrectly-formatted source location entry in AST file");
1503 return true;
1504
1505 case SM_SLOC_FILE_ENTRY: {
1506 // We will detect whether a file changed and return 'Failure' for it, but
1507 // we will also try to fail gracefully by setting up the SLocEntry.
1508 unsigned InputID = Record[4];
1509 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001510 const FileEntry *File = IF.getFile();
1511 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei11169dd2012-12-18 14:30:41 +00001512
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001513 // Note that we only check if a File was returned. If it was out-of-date
1514 // we have complained but we will continue creating a FileID to recover
1515 // gracefully.
1516 if (!File)
Guy Benyei11169dd2012-12-18 14:30:41 +00001517 return true;
1518
1519 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1520 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1521 // This is the module's main file.
1522 IncludeLoc = getImportLocation(F);
1523 }
Vedant Kumar48b4f762018-04-14 01:40:48 +00001524 SrcMgr::CharacteristicKind
1525 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
Alex Lorenz4dc55732019-08-22 18:15:50 +00001526 // FIXME: The FileID should be created from the FileEntryRef.
Guy Benyei11169dd2012-12-18 14:30:41 +00001527 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1528 ID, BaseOffset + Record[0]);
Vedant Kumar48b4f762018-04-14 01:40:48 +00001529 SrcMgr::FileInfo &FileInfo =
1530 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Guy Benyei11169dd2012-12-18 14:30:41 +00001531 FileInfo.NumCreatedFIDs = Record[5];
1532 if (Record[3])
1533 FileInfo.setHasLineDirectives();
1534
Guy Benyei11169dd2012-12-18 14:30:41 +00001535 unsigned NumFileDecls = Record[7];
Richard Smithdbafb6c2017-06-29 23:23:46 +00001536 if (NumFileDecls && ContextObj) {
Roman Lebedevbf4f1e02019-10-10 12:22:42 +00001537 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
Guy Benyei11169dd2012-12-18 14:30:41 +00001538 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1539 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1540 NumFileDecls));
1541 }
Richard Smithaada85c2016-02-06 02:06:43 +00001542
Guy Benyei11169dd2012-12-18 14:30:41 +00001543 const SrcMgr::ContentCache *ContentCache
Richard Smithf3f84612017-06-29 02:19:42 +00001544 = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
Guy Benyei11169dd2012-12-18 14:30:41 +00001545 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
Richard Smitha8cfffa2015-11-26 02:04:16 +00001546 ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1547 !ContentCache->getRawBuffer()) {
Richard Smithaada85c2016-02-06 02:06:43 +00001548 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1549 if (!Buffer)
Guy Benyei11169dd2012-12-18 14:30:41 +00001550 return true;
David Blaikie49cc3182014-08-27 20:54:45 +00001551 SourceMgr.overrideFileContents(File, std::move(Buffer));
Guy Benyei11169dd2012-12-18 14:30:41 +00001552 }
1553
1554 break;
1555 }
1556
1557 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00001558 const char *Name = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00001559 unsigned Offset = Record[0];
Vedant Kumar48b4f762018-04-14 01:40:48 +00001560 SrcMgr::CharacteristicKind
1561 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
Guy Benyei11169dd2012-12-18 14:30:41 +00001562 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Manman Ren11f2a472016-08-18 17:42:15 +00001563 if (IncludeLoc.isInvalid() && F->isModule()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001564 IncludeLoc = getImportLocation(F);
1565 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001566
Richard Smithaada85c2016-02-06 02:06:43 +00001567 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1568 if (!Buffer)
Guy Benyei11169dd2012-12-18 14:30:41 +00001569 return true;
David Blaikie50a5f972014-08-29 07:59:55 +00001570 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
Rafael Espindolad87f8d72014-08-27 20:03:29 +00001571 BaseOffset + Offset, IncludeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001572 break;
1573 }
1574
1575 case SM_SLOC_EXPANSION_ENTRY: {
1576 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1577 SourceMgr.createExpansionLoc(SpellingLoc,
1578 ReadSourceLocation(*F, Record[2]),
1579 ReadSourceLocation(*F, Record[3]),
Richard Smithb5f81712018-04-30 05:25:48 +00001580 Record[5],
Guy Benyei11169dd2012-12-18 14:30:41 +00001581 Record[4],
1582 ID,
1583 BaseOffset + Record[0]);
1584 break;
1585 }
1586 }
1587
1588 return false;
1589}
1590
1591std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1592 if (ID == 0)
1593 return std::make_pair(SourceLocation(), "");
1594
1595 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1596 Error("source location entry ID out-of-range for AST file");
1597 return std::make_pair(SourceLocation(), "");
1598 }
1599
1600 // Find which module file this entry lands in.
1601 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
Manman Ren11f2a472016-08-18 17:42:15 +00001602 if (!M->isModule())
Guy Benyei11169dd2012-12-18 14:30:41 +00001603 return std::make_pair(SourceLocation(), "");
1604
1605 // FIXME: Can we map this down to a particular submodule? That would be
1606 // ideal.
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001607 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
Guy Benyei11169dd2012-12-18 14:30:41 +00001608}
1609
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001610/// Find the location where the module F is imported.
Guy Benyei11169dd2012-12-18 14:30:41 +00001611SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1612 if (F->ImportLoc.isValid())
1613 return F->ImportLoc;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001614
Guy Benyei11169dd2012-12-18 14:30:41 +00001615 // Otherwise we have a PCH. It's considered to be "imported" at the first
1616 // location of its includer.
1617 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001618 // Main file is the importer.
Yaron Keren8b563662015-10-03 10:46:20 +00001619 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001620 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
Guy Benyei11169dd2012-12-18 14:30:41 +00001621 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001622 return F->ImportedBy[0]->FirstLoc;
1623}
1624
JF Bastien0e828952019-06-26 19:50:12 +00001625/// Enter a subblock of the specified BlockID with the specified cursor. Read
1626/// the abbreviations that are at the top of the block and then leave the cursor
1627/// pointing into the block.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001628bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
JF Bastien0e828952019-06-26 19:50:12 +00001629 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1630 // FIXME this drops errors on the floor.
1631 consumeError(std::move(Err));
Richard Smith0516b182015-09-08 19:40:14 +00001632 return true;
JF Bastien0e828952019-06-26 19:50:12 +00001633 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001634
1635 while (true) {
1636 uint64_t Offset = Cursor.GetCurrentBitNo();
JF Bastien0e828952019-06-26 19:50:12 +00001637 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1638 if (!MaybeCode) {
1639 // FIXME this drops errors on the floor.
1640 consumeError(MaybeCode.takeError());
1641 return true;
1642 }
1643 unsigned Code = MaybeCode.get();
Guy Benyei11169dd2012-12-18 14:30:41 +00001644
1645 // We expect all abbrevs to be at the start of the block.
1646 if (Code != llvm::bitc::DEFINE_ABBREV) {
JF Bastien0e828952019-06-26 19:50:12 +00001647 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1648 // FIXME this drops errors on the floor.
1649 consumeError(std::move(Err));
1650 return true;
1651 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001652 return false;
1653 }
JF Bastien0e828952019-06-26 19:50:12 +00001654 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1655 // FIXME this drops errors on the floor.
1656 consumeError(std::move(Err));
1657 return true;
1658 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001659 }
1660}
1661
Richard Smithe40f2ba2013-08-07 21:41:30 +00001662Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
John McCallf413f5e2013-05-03 00:10:13 +00001663 unsigned &Idx) {
1664 Token Tok;
1665 Tok.startToken();
1666 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1667 Tok.setLength(Record[Idx++]);
1668 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1669 Tok.setIdentifierInfo(II);
1670 Tok.setKind((tok::TokenKind)Record[Idx++]);
1671 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1672 return Tok;
1673}
1674
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001675MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001676 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001677
1678 // Keep track of where we are in the stream, then jump back there
1679 // after reading this macro.
1680 SavedStreamPosition SavedPosition(Stream);
1681
JF Bastien0e828952019-06-26 19:50:12 +00001682 if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1683 // FIXME this drops errors on the floor.
1684 consumeError(std::move(Err));
1685 return nullptr;
1686 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001687 RecordData Record;
Faisal Valiac506d72017-07-17 17:18:43 +00001688 SmallVector<IdentifierInfo*, 16> MacroParams;
Craig Toppera13603a2014-05-22 05:54:18 +00001689 MacroInfo *Macro = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001690
Guy Benyei11169dd2012-12-18 14:30:41 +00001691 while (true) {
Chris Lattnerefa77172013-01-20 00:00:22 +00001692 // Advance to the next record, but if we get to the end of the block, don't
1693 // pop it (removing all the abbreviations from the cursor) since we want to
1694 // be able to reseek within the block and read entries.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001695 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
JF Bastien0e828952019-06-26 19:50:12 +00001696 Expected<llvm::BitstreamEntry> MaybeEntry =
1697 Stream.advanceSkippingSubblocks(Flags);
1698 if (!MaybeEntry) {
1699 Error(MaybeEntry.takeError());
1700 return Macro;
1701 }
1702 llvm::BitstreamEntry Entry = MaybeEntry.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001703
Chris Lattnerefa77172013-01-20 00:00:22 +00001704 switch (Entry.Kind) {
1705 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1706 case llvm::BitstreamEntry::Error:
1707 Error("malformed block record in AST file");
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001708 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001709 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001710 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001711 case llvm::BitstreamEntry::Record:
1712 // The interesting case.
1713 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001714 }
1715
1716 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001717 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00001718 PreprocessorRecordTypes RecType;
1719 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1720 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1721 else {
1722 Error(MaybeRecType.takeError());
1723 return Macro;
1724 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001725 switch (RecType) {
Richard Smithd7329392015-04-21 21:46:32 +00001726 case PP_MODULE_MACRO:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001727 case PP_MACRO_DIRECTIVE_HISTORY:
1728 return Macro;
1729
Guy Benyei11169dd2012-12-18 14:30:41 +00001730 case PP_MACRO_OBJECT_LIKE:
1731 case PP_MACRO_FUNCTION_LIKE: {
1732 // If we already have a macro, that means that we've hit the end
1733 // of the definition of the macro we were looking for. We're
1734 // done.
1735 if (Macro)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001736 return Macro;
Guy Benyei11169dd2012-12-18 14:30:41 +00001737
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001738 unsigned NextIndex = 1; // Skip identifier ID.
Guy Benyei11169dd2012-12-18 14:30:41 +00001739 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Richard Smith3f6dd7a2017-05-12 23:40:52 +00001740 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Argyrios Kyrtzidis7572be22013-01-07 19:16:23 +00001741 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei11169dd2012-12-18 14:30:41 +00001742 MI->setIsUsed(Record[NextIndex++]);
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00001743 MI->setUsedForHeaderGuard(Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001744
Guy Benyei11169dd2012-12-18 14:30:41 +00001745 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1746 // Decode function-like macro info.
1747 bool isC99VarArgs = Record[NextIndex++];
1748 bool isGNUVarArgs = Record[NextIndex++];
1749 bool hasCommaPasting = Record[NextIndex++];
Faisal Valiac506d72017-07-17 17:18:43 +00001750 MacroParams.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00001751 unsigned NumArgs = Record[NextIndex++];
1752 for (unsigned i = 0; i != NumArgs; ++i)
Faisal Valiac506d72017-07-17 17:18:43 +00001753 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00001754
1755 // Install function-like macro info.
1756 MI->setIsFunctionLike();
1757 if (isC99VarArgs) MI->setIsC99Varargs();
1758 if (isGNUVarArgs) MI->setIsGNUVarargs();
1759 if (hasCommaPasting) MI->setHasCommaPasting();
Faisal Valiac506d72017-07-17 17:18:43 +00001760 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
Guy Benyei11169dd2012-12-18 14:30:41 +00001761 }
1762
Guy Benyei11169dd2012-12-18 14:30:41 +00001763 // Remember that we saw this macro last so that we add the tokens that
1764 // form its body to it.
1765 Macro = MI;
1766
1767 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1768 Record[NextIndex]) {
1769 // We have a macro definition. Register the association
1770 PreprocessedEntityID
1771 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1772 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Richard Smith66a81862015-05-04 02:25:31 +00001773 PreprocessingRecord::PPEntityID PPID =
1774 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
Vedant Kumar48b4f762018-04-14 01:40:48 +00001775 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
Richard Smith66a81862015-05-04 02:25:31 +00001776 PPRec.getPreprocessedEntity(PPID));
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +00001777 if (PPDef)
1778 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei11169dd2012-12-18 14:30:41 +00001779 }
1780
1781 ++NumMacrosRead;
1782 break;
1783 }
1784
1785 case PP_TOKEN: {
1786 // If we see a TOKEN before a PP_MACRO_*, then the file is
1787 // erroneous, just pretend we didn't see this.
Craig Toppera13603a2014-05-22 05:54:18 +00001788 if (!Macro) break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001789
John McCallf413f5e2013-05-03 00:10:13 +00001790 unsigned Idx = 0;
1791 Token Tok = ReadToken(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001792 Macro->AddTokenToBody(Tok);
1793 break;
1794 }
1795 }
1796 }
1797}
1798
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001799PreprocessedEntityID
Richard Smith37a93df2017-02-18 00:32:02 +00001800ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1801 unsigned LocalID) const {
1802 if (!M.ModuleOffsetMap.empty())
1803 ReadModuleOffsetMap(M);
1804
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001805 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Guy Benyei11169dd2012-12-18 14:30:41 +00001806 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001807 assert(I != M.PreprocessedEntityRemap.end()
Guy Benyei11169dd2012-12-18 14:30:41 +00001808 && "Invalid index into preprocessed entity index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001809
Guy Benyei11169dd2012-12-18 14:30:41 +00001810 return LocalID + I->second;
1811}
1812
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001813unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1814 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei11169dd2012-12-18 14:30:41 +00001815}
Richard Smith7ed1bc92014-12-05 22:42:13 +00001816
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001817HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001818HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
Richard Smithe75ee0f2015-08-17 07:13:32 +00001819 internal_key_type ikey = {FE->getSize(),
1820 M.HasTimestamps ? FE->getModificationTime() : 0,
1821 FE->getName(), /*Imported*/ false};
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001822 return ikey;
1823}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001824
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001825bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
Richard Smithe75ee0f2015-08-17 07:13:32 +00001826 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
Guy Benyei11169dd2012-12-18 14:30:41 +00001827 return false;
1828
Mehdi Amini004b9c72016-10-10 22:52:47 +00001829 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001830 return true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001831
Guy Benyei11169dd2012-12-18 14:30:41 +00001832 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001833 FileManager &FileMgr = Reader.getFileManager();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001834 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
Harlan Haskins8d323d12019-08-01 21:31:56 +00001835 if (!Key.Imported) {
1836 if (auto File = FileMgr.getFile(Key.Filename))
1837 return *File;
1838 return nullptr;
1839 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001840
Benjamin Krameradcd0262020-01-28 20:23:46 +01001841 std::string Resolved = std::string(Key.Filename);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001842 Reader.ResolveImportedPath(M, Resolved);
Harlan Haskins8d323d12019-08-01 21:31:56 +00001843 if (auto File = FileMgr.getFile(Resolved))
1844 return *File;
1845 return nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001846 };
1847
1848 const FileEntry *FEA = GetFile(a);
1849 const FileEntry *FEB = GetFile(b);
1850 return FEA && FEA == FEB;
Guy Benyei11169dd2012-12-18 14:30:41 +00001851}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001852
Guy Benyei11169dd2012-12-18 14:30:41 +00001853std::pair<unsigned, unsigned>
1854HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001855 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001856
Vedant Kumar48b4f762018-04-14 01:40:48 +00001857 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1858 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001859 return std::make_pair(KeyLen, DataLen);
Guy Benyei11169dd2012-12-18 14:30:41 +00001860}
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001861
1862HeaderFileInfoTrait::internal_key_type
1863HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001864 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001865
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001866 internal_key_type ikey;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001867 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1868 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001869 ikey.Filename = (const char *)d;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001870 ikey.Imported = true;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001871 return ikey;
1872}
1873
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001874HeaderFileInfoTrait::data_type
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001875HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei11169dd2012-12-18 14:30:41 +00001876 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001877 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001878
1879 const unsigned char *End = d + DataLen;
Guy Benyei11169dd2012-12-18 14:30:41 +00001880 HeaderFileInfo HFI;
1881 unsigned Flags = *d++;
Richard Smith386bb072015-08-18 23:42:23 +00001882 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
Richard Smithf3f84612017-06-29 02:19:42 +00001883 HFI.isImport |= (Flags >> 5) & 0x01;
1884 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1885 HFI.DirInfo = (Flags >> 1) & 0x07;
Guy Benyei11169dd2012-12-18 14:30:41 +00001886 HFI.IndexHeaderMapHeader = Flags & 0x01;
Richard Smith386bb072015-08-18 23:42:23 +00001887 // FIXME: Find a better way to handle this. Maybe just store a
1888 // "has been included" flag?
1889 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1890 HFI.NumIncludes);
Justin Bogner57ba0b22014-03-28 22:03:24 +00001891 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1892 M, endian::readNext<uint32_t, little, unaligned>(d));
1893 if (unsigned FrameworkOffset =
1894 endian::readNext<uint32_t, little, unaligned>(d)) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001895 // The framework offset is 1 greater than the actual offset,
Guy Benyei11169dd2012-12-18 14:30:41 +00001896 // since 0 is used as an indicator for "no framework name".
1897 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1898 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1899 }
Richard Smith386bb072015-08-18 23:42:23 +00001900
1901 assert((End - d) % 4 == 0 &&
1902 "Wrong data length in HeaderFileInfo deserialization");
1903 while (d != End) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001904 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
Richard Smith386bb072015-08-18 23:42:23 +00001905 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1906 LocalSMID >>= 2;
1907
1908 // This header is part of a module. Associate it with the module to enable
1909 // implicit module import.
1910 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1911 Module *Mod = Reader.getSubmodule(GlobalSMID);
1912 FileManager &FileMgr = Reader.getFileManager();
1913 ModuleMap &ModMap =
1914 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1915
Benjamin Krameradcd0262020-01-28 20:23:46 +01001916 std::string Filename = std::string(key.Filename);
Richard Smith386bb072015-08-18 23:42:23 +00001917 if (key.Imported)
1918 Reader.ResolveImportedPath(M, Filename);
1919 // FIXME: This is not always the right filename-as-written, but we're not
1920 // going to use this information to rebuild the module, so it doesn't make
1921 // a lot of difference.
Benjamin Krameradcd0262020-01-28 20:23:46 +01001922 Module::Header H = {std::string(key.Filename), *FileMgr.getFile(Filename)};
Richard Smithd8879c82015-08-24 21:59:32 +00001923 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1924 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001925 }
1926
Guy Benyei11169dd2012-12-18 14:30:41 +00001927 // This HeaderFileInfo was externally loaded.
1928 HFI.External = true;
Richard Smithd8879c82015-08-24 21:59:32 +00001929 HFI.IsValid = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001930 return HFI;
1931}
1932
Richard Smithd7329392015-04-21 21:46:32 +00001933void ASTReader::addPendingMacro(IdentifierInfo *II,
1934 ModuleFile *M,
1935 uint64_t MacroDirectivesOffset) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001936 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1937 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00001938}
1939
1940void ASTReader::ReadDefinedMacros() {
1941 // Note that we are loading defined macros.
1942 Deserializing Macros(this);
1943
Vedant Kumar48b4f762018-04-14 01:40:48 +00001944 for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001945 BitstreamCursor &MacroCursor = I.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001946
1947 // If there was no preprocessor block, skip this file.
Peter Collingbourne77c89b62016-11-08 04:17:11 +00001948 if (MacroCursor.getBitcodeBytes().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00001949 continue;
1950
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001951 BitstreamCursor Cursor = MacroCursor;
JF Bastien0e828952019-06-26 19:50:12 +00001952 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1953 Error(std::move(Err));
1954 return;
1955 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001956
1957 RecordData Record;
1958 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00001959 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1960 if (!MaybeE) {
1961 Error(MaybeE.takeError());
1962 return;
1963 }
1964 llvm::BitstreamEntry E = MaybeE.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001965
Chris Lattnere7b154b2013-01-19 21:39:22 +00001966 switch (E.Kind) {
1967 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1968 case llvm::BitstreamEntry::Error:
1969 Error("malformed block record in AST file");
1970 return;
1971 case llvm::BitstreamEntry::EndBlock:
1972 goto NextCursor;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001973
JF Bastien0e828952019-06-26 19:50:12 +00001974 case llvm::BitstreamEntry::Record: {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001975 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00001976 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1977 if (!MaybeRecord) {
1978 Error(MaybeRecord.takeError());
1979 return;
1980 }
1981 switch (MaybeRecord.get()) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001982 default: // Default behavior: ignore.
1983 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001984
Chris Lattnere7b154b2013-01-19 21:39:22 +00001985 case PP_MACRO_OBJECT_LIKE:
Sean Callananf3682a72016-05-14 06:24:14 +00001986 case PP_MACRO_FUNCTION_LIKE: {
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001987 IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
Sean Callananf3682a72016-05-14 06:24:14 +00001988 if (II->isOutOfDate())
1989 updateOutOfDateIdentifier(*II);
Chris Lattnere7b154b2013-01-19 21:39:22 +00001990 break;
Sean Callananf3682a72016-05-14 06:24:14 +00001991 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001992
Chris Lattnere7b154b2013-01-19 21:39:22 +00001993 case PP_TOKEN:
1994 // Ignore tokens.
1995 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001996 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001997 break;
1998 }
JF Bastien0e828952019-06-26 19:50:12 +00001999 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002000 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002001 NextCursor: ;
Guy Benyei11169dd2012-12-18 14:30:41 +00002002 }
2003}
2004
2005namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002006
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002007 /// Visitor class used to look up identifirs in an AST file.
Guy Benyei11169dd2012-12-18 14:30:41 +00002008 class IdentifierLookupVisitor {
2009 StringRef Name;
Richard Smith3b637412015-07-14 18:42:41 +00002010 unsigned NameHash;
Guy Benyei11169dd2012-12-18 14:30:41 +00002011 unsigned PriorGeneration;
Douglas Gregor00a50f72013-01-25 00:38:33 +00002012 unsigned &NumIdentifierLookups;
2013 unsigned &NumIdentifierLookupHits;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002014 IdentifierInfo *Found = nullptr;
Douglas Gregor00a50f72013-01-25 00:38:33 +00002015
Guy Benyei11169dd2012-12-18 14:30:41 +00002016 public:
Douglas Gregor00a50f72013-01-25 00:38:33 +00002017 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2018 unsigned &NumIdentifierLookups,
2019 unsigned &NumIdentifierLookupHits)
Richard Smith3b637412015-07-14 18:42:41 +00002020 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2021 PriorGeneration(PriorGeneration),
Douglas Gregor00a50f72013-01-25 00:38:33 +00002022 NumIdentifierLookups(NumIdentifierLookups),
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002023 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00002024
2025 bool operator()(ModuleFile &M) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002026 // If we've already searched this module file, skip it now.
Richard Smithbdf2d932015-07-30 03:37:16 +00002027 if (M.Generation <= PriorGeneration)
Guy Benyei11169dd2012-12-18 14:30:41 +00002028 return true;
Douglas Gregore060e572013-01-25 01:03:03 +00002029
Vedant Kumar48b4f762018-04-14 01:40:48 +00002030 ASTIdentifierLookupTable *IdTable
2031 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
Guy Benyei11169dd2012-12-18 14:30:41 +00002032 if (!IdTable)
2033 return false;
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00002034
2035 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
Richard Smithbdf2d932015-07-30 03:37:16 +00002036 Found);
2037 ++NumIdentifierLookups;
Richard Smith3b637412015-07-14 18:42:41 +00002038 ASTIdentifierLookupTable::iterator Pos =
Richard Smithbdf2d932015-07-30 03:37:16 +00002039 IdTable->find_hashed(Name, NameHash, &Trait);
Guy Benyei11169dd2012-12-18 14:30:41 +00002040 if (Pos == IdTable->end())
2041 return false;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002042
Guy Benyei11169dd2012-12-18 14:30:41 +00002043 // Dereferencing the iterator has the effect of building the
2044 // IdentifierInfo node and populating it with the various
2045 // declarations it needs.
Richard Smithbdf2d932015-07-30 03:37:16 +00002046 ++NumIdentifierLookupHits;
2047 Found = *Pos;
Guy Benyei11169dd2012-12-18 14:30:41 +00002048 return true;
2049 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002050
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002051 // Retrieve the identifier info found within the module
Guy Benyei11169dd2012-12-18 14:30:41 +00002052 // files.
2053 IdentifierInfo *getIdentifierInfo() const { return Found; }
2054 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002055
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002056} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00002057
2058void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2059 // Note that we are loading an identifier.
2060 Deserializing AnIdentifier(this);
2061
2062 unsigned PriorGeneration = 0;
2063 if (getContext().getLangOpts().Modules)
2064 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregore060e572013-01-25 01:03:03 +00002065
2066 // If there is a global index, look there first to determine which modules
2067 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00002068 GlobalModuleIndex::HitSet Hits;
Craig Toppera13603a2014-05-22 05:54:18 +00002069 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
Douglas Gregore060e572013-01-25 01:03:03 +00002070 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00002071 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2072 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00002073 }
2074 }
2075
Douglas Gregor7211ac12013-01-25 23:32:03 +00002076 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregor00a50f72013-01-25 00:38:33 +00002077 NumIdentifierLookups,
2078 NumIdentifierLookupHits);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00002079 ModuleMgr.visit(Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00002080 markIdentifierUpToDate(&II);
2081}
2082
2083void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2084 if (!II)
2085 return;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002086
Guy Benyei11169dd2012-12-18 14:30:41 +00002087 II->setOutOfDate(false);
2088
2089 // Update the generation for this identifier.
2090 if (getContext().getLangOpts().Modules)
Richard Smith053f6c62014-05-16 23:01:30 +00002091 IdentifierGeneration[II] = getGeneration();
Guy Benyei11169dd2012-12-18 14:30:41 +00002092}
2093
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002094void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2095 const PendingMacroInfo &PMInfo) {
Richard Smithd7329392015-04-21 21:46:32 +00002096 ModuleFile &M = *PMInfo.M;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002097
2098 BitstreamCursor &Cursor = M.MacroCursor;
2099 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00002100 if (llvm::Error Err = Cursor.JumpToBit(PMInfo.MacroDirectivesOffset)) {
2101 Error(std::move(Err));
2102 return;
2103 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002104
Richard Smith713369b2015-04-23 20:40:50 +00002105 struct ModuleMacroRecord {
2106 SubmoduleID SubModID;
2107 MacroInfo *MI;
2108 SmallVector<SubmoduleID, 8> Overrides;
2109 };
2110 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002111
Richard Smithd7329392015-04-21 21:46:32 +00002112 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2113 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2114 // macro histroy.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002115 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002116 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00002117 Expected<llvm::BitstreamEntry> MaybeEntry =
Richard Smithd7329392015-04-21 21:46:32 +00002118 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
JF Bastien0e828952019-06-26 19:50:12 +00002119 if (!MaybeEntry) {
2120 Error(MaybeEntry.takeError());
2121 return;
2122 }
2123 llvm::BitstreamEntry Entry = MaybeEntry.get();
2124
Richard Smithd7329392015-04-21 21:46:32 +00002125 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2126 Error("malformed block record in AST file");
2127 return;
2128 }
2129
2130 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00002131 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2132 if (!MaybePP) {
2133 Error(MaybePP.takeError());
2134 return;
2135 }
2136 switch ((PreprocessorRecordTypes)MaybePP.get()) {
Richard Smithd7329392015-04-21 21:46:32 +00002137 case PP_MACRO_DIRECTIVE_HISTORY:
2138 break;
2139
2140 case PP_MODULE_MACRO: {
Richard Smith713369b2015-04-23 20:40:50 +00002141 ModuleMacros.push_back(ModuleMacroRecord());
2142 auto &Info = ModuleMacros.back();
Richard Smithe56c8bc2015-04-22 00:26:11 +00002143 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2144 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
Richard Smith713369b2015-04-23 20:40:50 +00002145 for (int I = 2, N = Record.size(); I != N; ++I)
2146 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
Richard Smithd7329392015-04-21 21:46:32 +00002147 continue;
2148 }
2149
2150 default:
2151 Error("malformed block record in AST file");
2152 return;
2153 }
2154
2155 // We found the macro directive history; that's the last record
2156 // for this macro.
2157 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002158 }
2159
Richard Smithd7329392015-04-21 21:46:32 +00002160 // Module macros are listed in reverse dependency order.
Richard Smithe56c8bc2015-04-22 00:26:11 +00002161 {
2162 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
Richard Smithe56c8bc2015-04-22 00:26:11 +00002163 llvm::SmallVector<ModuleMacro*, 8> Overrides;
Richard Smith713369b2015-04-23 20:40:50 +00002164 for (auto &MMR : ModuleMacros) {
Richard Smithe56c8bc2015-04-22 00:26:11 +00002165 Overrides.clear();
Vedant Kumar48b4f762018-04-14 01:40:48 +00002166 for (unsigned ModID : MMR.Overrides) {
Richard Smithb8b2ed62015-04-23 18:18:26 +00002167 Module *Mod = getSubmodule(ModID);
2168 auto *Macro = PP.getModuleMacro(Mod, II);
Richard Smithe56c8bc2015-04-22 00:26:11 +00002169 assert(Macro && "missing definition for overridden macro");
Richard Smith5dbef922015-04-22 02:09:43 +00002170 Overrides.push_back(Macro);
Richard Smithe56c8bc2015-04-22 00:26:11 +00002171 }
2172
2173 bool Inserted = false;
Richard Smith713369b2015-04-23 20:40:50 +00002174 Module *Owner = getSubmodule(MMR.SubModID);
Richard Smith20e883e2015-04-29 23:20:19 +00002175 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
Richard Smithd7329392015-04-21 21:46:32 +00002176 }
2177 }
2178
2179 // Don't read the directive history for a module; we don't have anywhere
2180 // to put it.
Manman Ren11f2a472016-08-18 17:42:15 +00002181 if (M.isModule())
Richard Smithd7329392015-04-21 21:46:32 +00002182 return;
2183
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002184 // Deserialize the macro directives history in reverse source-order.
Craig Toppera13603a2014-05-22 05:54:18 +00002185 MacroDirective *Latest = nullptr, *Earliest = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002186 unsigned Idx = 0, N = Record.size();
2187 while (Idx < N) {
Craig Toppera13603a2014-05-22 05:54:18 +00002188 MacroDirective *MD = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002189 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00002190 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002191 switch (K) {
2192 case MacroDirective::MD_Define: {
Richard Smith713369b2015-04-23 20:40:50 +00002193 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
Richard Smith3981b172015-04-30 02:16:23 +00002194 MD = PP.AllocateDefMacroDirective(MI, Loc);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002195 break;
2196 }
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002197 case MacroDirective::MD_Undefine:
Richard Smith3981b172015-04-30 02:16:23 +00002198 MD = PP.AllocateUndefMacroDirective(Loc);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002199 break;
Richard Smithdaa69e02014-07-25 04:40:03 +00002200 case MacroDirective::MD_Visibility:
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002201 bool isPublic = Record[Idx++];
2202 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2203 break;
2204 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002205
2206 if (!Latest)
2207 Latest = MD;
2208 if (Earliest)
2209 Earliest->setPrevious(MD);
2210 Earliest = MD;
2211 }
2212
Richard Smithd6e8c0d2015-05-04 19:58:00 +00002213 if (Latest)
Nico Weberfd870702016-12-09 17:32:52 +00002214 PP.setLoadedMacroDirective(II, Earliest, Latest);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002215}
2216
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002217ASTReader::InputFileInfo
2218ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002219 // Go find this input file.
2220 BitstreamCursor &Cursor = F.InputFilesCursor;
2221 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00002222 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2223 // FIXME this drops errors on the floor.
2224 consumeError(std::move(Err));
2225 }
Ben Langmuir198c1682014-03-07 07:27:49 +00002226
JF Bastien0e828952019-06-26 19:50:12 +00002227 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2228 if (!MaybeCode) {
2229 // FIXME this drops errors on the floor.
2230 consumeError(MaybeCode.takeError());
2231 }
2232 unsigned Code = MaybeCode.get();
Ben Langmuir198c1682014-03-07 07:27:49 +00002233 RecordData Record;
2234 StringRef Blob;
2235
JF Bastien0e828952019-06-26 19:50:12 +00002236 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2237 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2238 "invalid record type for input file");
2239 else {
2240 // FIXME this drops errors on the floor.
2241 consumeError(Maybe.takeError());
2242 }
Ben Langmuir198c1682014-03-07 07:27:49 +00002243
2244 assert(Record[0] == ID && "Bogus stored ID or offset");
Richard Smitha8cfffa2015-11-26 02:04:16 +00002245 InputFileInfo R;
2246 R.StoredSize = static_cast<off_t>(Record[1]);
2247 R.StoredTime = static_cast<time_t>(Record[2]);
2248 R.Overridden = static_cast<bool>(Record[3]);
2249 R.Transient = static_cast<bool>(Record[4]);
Richard Smithf3f84612017-06-29 02:19:42 +00002250 R.TopLevelModuleMap = static_cast<bool>(Record[5]);
Benjamin Krameradcd0262020-01-28 20:23:46 +01002251 R.Filename = std::string(Blob);
Richard Smitha8cfffa2015-11-26 02:04:16 +00002252 ResolveImportedPath(F, R.Filename);
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002253
2254 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2255 if (!MaybeEntry) // FIXME this drops errors on the floor.
2256 consumeError(MaybeEntry.takeError());
2257 llvm::BitstreamEntry Entry = MaybeEntry.get();
2258 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2259 "expected record type for input file hash");
2260
2261 Record.clear();
2262 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2263 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2264 "invalid record type for input file hash");
2265 else {
2266 // FIXME this drops errors on the floor.
2267 consumeError(Maybe.takeError());
2268 }
2269 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2270 static_cast<uint64_t>(Record[0]);
Hans Wennborg73945142014-03-14 17:45:06 +00002271 return R;
Ben Langmuir198c1682014-03-07 07:27:49 +00002272}
2273
Manman Renc8c94152016-10-21 23:35:03 +00002274static unsigned moduleKindForDiagnostic(ModuleKind Kind);
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002275InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002276 // If this ID is bogus, just return an empty input file.
2277 if (ID == 0 || ID > F.InputFilesLoaded.size())
2278 return InputFile();
2279
2280 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002281 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei11169dd2012-12-18 14:30:41 +00002282 return F.InputFilesLoaded[ID-1];
2283
Argyrios Kyrtzidis9308f0a2014-01-08 19:13:34 +00002284 if (F.InputFilesLoaded[ID-1].isNotFound())
2285 return InputFile();
2286
Guy Benyei11169dd2012-12-18 14:30:41 +00002287 // Go find this input file.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002288 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00002289 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00002290 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2291 // FIXME this drops errors on the floor.
2292 consumeError(std::move(Err));
2293 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002294
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002295 InputFileInfo FI = readInputFileInfo(F, ID);
2296 off_t StoredSize = FI.StoredSize;
2297 time_t StoredTime = FI.StoredTime;
2298 bool Overridden = FI.Overridden;
Richard Smitha8cfffa2015-11-26 02:04:16 +00002299 bool Transient = FI.Transient;
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002300 StringRef Filename = FI.Filename;
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002301 uint64_t StoredContentHash = FI.ContentHash;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002302
Harlan Haskins8d323d12019-08-01 21:31:56 +00002303 const FileEntry *File = nullptr;
2304 if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false))
2305 File = *FE;
2306
Ben Langmuir198c1682014-03-07 07:27:49 +00002307 // If we didn't find the file, resolve it relative to the
2308 // original directory from which this AST file was created.
Manuel Klimek1b29b4f2017-07-25 10:22:06 +00002309 if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2310 F.OriginalDir != F.BaseDirectory) {
2311 std::string Resolved = resolveFileRelativeToOriginalDir(
Benjamin Krameradcd0262020-01-28 20:23:46 +01002312 std::string(Filename), F.OriginalDir, F.BaseDirectory);
Ben Langmuir198c1682014-03-07 07:27:49 +00002313 if (!Resolved.empty())
Harlan Haskins8d323d12019-08-01 21:31:56 +00002314 if (auto FE = FileMgr.getFile(Resolved))
2315 File = *FE;
Ben Langmuir198c1682014-03-07 07:27:49 +00002316 }
2317
2318 // For an overridden file, create a virtual file with the stored
2319 // size/timestamp.
Richard Smitha8cfffa2015-11-26 02:04:16 +00002320 if ((Overridden || Transient) && File == nullptr)
Ben Langmuir198c1682014-03-07 07:27:49 +00002321 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
Ben Langmuir198c1682014-03-07 07:27:49 +00002322
Craig Toppera13603a2014-05-22 05:54:18 +00002323 if (File == nullptr) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002324 if (Complain) {
2325 std::string ErrorStr = "could not find file '";
2326 ErrorStr += Filename;
Richard Smith68142212015-10-13 01:26:26 +00002327 ErrorStr += "' referenced by AST file '";
2328 ErrorStr += F.FileName;
2329 ErrorStr += "'";
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002330 Error(ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00002331 }
Ben Langmuir198c1682014-03-07 07:27:49 +00002332 // Record that we didn't find the file.
2333 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2334 return InputFile();
2335 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002336
Ben Langmuir198c1682014-03-07 07:27:49 +00002337 // Check if there was a request to override the contents of the file
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002338 // that was part of the precompiled header. Overriding such a file
Ben Langmuir198c1682014-03-07 07:27:49 +00002339 // can lead to problems when lexing using the source locations from the
2340 // PCH.
2341 SourceManager &SM = getSourceManager();
Richard Smith64daf7b2015-12-01 03:32:49 +00002342 // FIXME: Reject if the overrides are different.
2343 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002344 if (Complain)
2345 Error(diag::err_fe_pch_file_overridden, Filename);
Duncan P. N. Exon Smithe1b7f222019-08-30 22:59:25 +00002346
2347 // After emitting the diagnostic, bypass the overriding file to recover
2348 // (this creates a separate FileEntry).
2349 File = SM.bypassFileContentsOverride(*File);
2350 if (!File) {
2351 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2352 return InputFile();
2353 }
Ben Langmuir198c1682014-03-07 07:27:49 +00002354 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002355
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002356 enum ModificationType {
2357 Size,
2358 ModTime,
2359 Content,
2360 None,
2361 };
2362 auto HasInputFileChanged = [&]() {
2363 if (StoredSize != File->getSize())
2364 return ModificationType::Size;
2365 if (!DisableValidation && StoredTime &&
2366 StoredTime != File->getModificationTime()) {
2367 // In case the modification time changes but not the content,
2368 // accept the cached file as legit.
2369 if (ValidateASTInputFilesContent &&
2370 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2371 auto MemBuffOrError = FileMgr.getBufferForFile(File);
2372 if (!MemBuffOrError) {
2373 if (!Complain)
2374 return ModificationType::ModTime;
2375 std::string ErrorStr = "could not get buffer for file '";
2376 ErrorStr += File->getName();
2377 ErrorStr += "'";
2378 Error(ErrorStr);
2379 return ModificationType::ModTime;
2380 }
Eric Christopher3be91692019-10-14 23:14:24 +00002381
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002382 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2383 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2384 return ModificationType::None;
2385 return ModificationType::Content;
2386 }
2387 return ModificationType::ModTime;
2388 }
2389 return ModificationType::None;
2390 };
2391
2392 bool IsOutOfDate = false;
2393 auto FileChange = HasInputFileChanged();
Ben Langmuir198c1682014-03-07 07:27:49 +00002394 // For an overridden file, there is nothing to validate.
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002395 if (!Overridden && FileChange != ModificationType::None) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002396 if (Complain) {
2397 // Build a list of the PCH imports that got us here (in reverse).
2398 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002399 while (!ImportStack.back()->ImportedBy.empty())
Ben Langmuir198c1682014-03-07 07:27:49 +00002400 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
Ben Langmuire82630d2014-01-17 00:19:09 +00002401
Ben Langmuir198c1682014-03-07 07:27:49 +00002402 // The top-level PCH is stale.
2403 StringRef TopLevelPCHName(ImportStack.back()->FileName);
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002404 unsigned DiagnosticKind =
2405 moduleKindForDiagnostic(ImportStack.back()->Kind);
Manman Renc8c94152016-10-21 23:35:03 +00002406 if (DiagnosticKind == 0)
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002407 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName,
2408 (unsigned)FileChange);
Manman Renc8c94152016-10-21 23:35:03 +00002409 else if (DiagnosticKind == 1)
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002410 Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName,
2411 (unsigned)FileChange);
Manman Renc8c94152016-10-21 23:35:03 +00002412 else
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002413 Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName,
2414 (unsigned)FileChange);
Ben Langmuire82630d2014-01-17 00:19:09 +00002415
Ben Langmuir198c1682014-03-07 07:27:49 +00002416 // Print the import stack.
2417 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2418 Diag(diag::note_pch_required_by)
2419 << Filename << ImportStack[0]->FileName;
2420 for (unsigned I = 1; I < ImportStack.size(); ++I)
Ben Langmuire82630d2014-01-17 00:19:09 +00002421 Diag(diag::note_pch_required_by)
Ben Langmuir198c1682014-03-07 07:27:49 +00002422 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
Douglas Gregor7029ce12013-03-19 00:28:20 +00002423 }
2424
Ben Langmuir198c1682014-03-07 07:27:49 +00002425 if (!Diags.isDiagnosticInFlight())
2426 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
Guy Benyei11169dd2012-12-18 14:30:41 +00002427 }
2428
Ben Langmuir198c1682014-03-07 07:27:49 +00002429 IsOutOfDate = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00002430 }
Richard Smitha8cfffa2015-11-26 02:04:16 +00002431 // FIXME: If the file is overridden and we've already opened it,
2432 // issue an error (or split it into a separate FileEntry).
Guy Benyei11169dd2012-12-18 14:30:41 +00002433
Richard Smitha8cfffa2015-11-26 02:04:16 +00002434 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
Ben Langmuir198c1682014-03-07 07:27:49 +00002435
2436 // Note that we've loaded this input file.
2437 F.InputFilesLoaded[ID-1] = IF;
2438 return IF;
Guy Benyei11169dd2012-12-18 14:30:41 +00002439}
2440
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002441/// If we are loading a relocatable PCH or module file, and the filename
Richard Smith7ed1bc92014-12-05 22:42:13 +00002442/// is not an absolute path, add the system or module root to the beginning of
2443/// the file name.
2444void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2445 // Resolve relative to the base directory, if we have one.
2446 if (!M.BaseDirectory.empty())
2447 return ResolveImportedPath(Filename, M.BaseDirectory);
Guy Benyei11169dd2012-12-18 14:30:41 +00002448}
2449
Richard Smith7ed1bc92014-12-05 22:42:13 +00002450void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002451 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2452 return;
2453
Richard Smith7ed1bc92014-12-05 22:42:13 +00002454 SmallString<128> Buffer;
2455 llvm::sys::path::append(Buffer, Prefix, Filename);
2456 Filename.assign(Buffer.begin(), Buffer.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00002457}
2458
Richard Smith0f99d6a2015-08-09 08:48:41 +00002459static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2460 switch (ARR) {
2461 case ASTReader::Failure: return true;
2462 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2463 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2464 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2465 case ASTReader::ConfigurationMismatch:
2466 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2467 case ASTReader::HadErrors: return true;
2468 case ASTReader::Success: return false;
2469 }
2470
2471 llvm_unreachable("unknown ASTReadResult");
2472}
2473
Richard Smith0516b182015-09-08 19:40:14 +00002474ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2475 BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2476 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002477 std::string &SuggestedPredefines) {
JF Bastien0e828952019-06-26 19:50:12 +00002478 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2479 // FIXME this drops errors on the floor.
2480 consumeError(std::move(Err));
Richard Smith0516b182015-09-08 19:40:14 +00002481 return Failure;
JF Bastien0e828952019-06-26 19:50:12 +00002482 }
Richard Smith0516b182015-09-08 19:40:14 +00002483
2484 // Read all of the records in the options block.
2485 RecordData Record;
2486 ASTReadResult Result = Success;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002487 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00002488 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2489 if (!MaybeEntry) {
2490 // FIXME this drops errors on the floor.
2491 consumeError(MaybeEntry.takeError());
2492 return Failure;
2493 }
2494 llvm::BitstreamEntry Entry = MaybeEntry.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002495
Richard Smith0516b182015-09-08 19:40:14 +00002496 switch (Entry.Kind) {
2497 case llvm::BitstreamEntry::Error:
2498 case llvm::BitstreamEntry::SubBlock:
2499 return Failure;
2500
2501 case llvm::BitstreamEntry::EndBlock:
2502 return Result;
2503
2504 case llvm::BitstreamEntry::Record:
2505 // The interesting case.
2506 break;
2507 }
2508
2509 // Read and process a record.
2510 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00002511 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2512 if (!MaybeRecordType) {
2513 // FIXME this drops errors on the floor.
2514 consumeError(MaybeRecordType.takeError());
2515 return Failure;
2516 }
2517 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
Richard Smith0516b182015-09-08 19:40:14 +00002518 case LANGUAGE_OPTIONS: {
2519 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2520 if (ParseLanguageOptions(Record, Complain, Listener,
2521 AllowCompatibleConfigurationMismatch))
2522 Result = ConfigurationMismatch;
2523 break;
2524 }
2525
2526 case TARGET_OPTIONS: {
2527 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2528 if (ParseTargetOptions(Record, Complain, Listener,
2529 AllowCompatibleConfigurationMismatch))
2530 Result = ConfigurationMismatch;
2531 break;
2532 }
2533
Richard Smith0516b182015-09-08 19:40:14 +00002534 case FILE_SYSTEM_OPTIONS: {
2535 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2536 if (!AllowCompatibleConfigurationMismatch &&
2537 ParseFileSystemOptions(Record, Complain, Listener))
2538 Result = ConfigurationMismatch;
2539 break;
2540 }
2541
2542 case HEADER_SEARCH_OPTIONS: {
2543 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2544 if (!AllowCompatibleConfigurationMismatch &&
2545 ParseHeaderSearchOptions(Record, Complain, Listener))
2546 Result = ConfigurationMismatch;
2547 break;
2548 }
2549
2550 case PREPROCESSOR_OPTIONS:
2551 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2552 if (!AllowCompatibleConfigurationMismatch &&
2553 ParsePreprocessorOptions(Record, Complain, Listener,
2554 SuggestedPredefines))
2555 Result = ConfigurationMismatch;
2556 break;
2557 }
2558 }
2559}
2560
Guy Benyei11169dd2012-12-18 14:30:41 +00002561ASTReader::ASTReadResult
2562ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002563 SmallVectorImpl<ImportedModule> &Loaded,
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002564 const ModuleFile *ImportedBy,
Guy Benyei11169dd2012-12-18 14:30:41 +00002565 unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002566 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002567
JF Bastien0e828952019-06-26 19:50:12 +00002568 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2569 Error(std::move(Err));
Guy Benyei11169dd2012-12-18 14:30:41 +00002570 return Failure;
2571 }
2572
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002573 // Lambda to read the unhashed control block the first time it's called.
2574 //
2575 // For PCM files, the unhashed control block cannot be read until after the
2576 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
2577 // need to look ahead before reading the IMPORTS record. For consistency,
2578 // this block is always read somehow (see BitstreamEntry::EndBlock).
2579 bool HasReadUnhashedControlBlock = false;
2580 auto readUnhashedControlBlockOnce = [&]() {
2581 if (!HasReadUnhashedControlBlock) {
2582 HasReadUnhashedControlBlock = true;
2583 if (ASTReadResult Result =
2584 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2585 return Result;
2586 }
2587 return Success;
2588 };
2589
Guy Benyei11169dd2012-12-18 14:30:41 +00002590 // Read all of the records and blocks in the control block.
2591 RecordData Record;
Richard Smitha1825302014-10-23 22:18:29 +00002592 unsigned NumInputs = 0;
2593 unsigned NumUserInputs = 0;
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00002594 StringRef BaseDirectoryAsWritten;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002595 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00002596 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2597 if (!MaybeEntry) {
2598 Error(MaybeEntry.takeError());
2599 return Failure;
2600 }
2601 llvm::BitstreamEntry Entry = MaybeEntry.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002602
Chris Lattnere7b154b2013-01-19 21:39:22 +00002603 switch (Entry.Kind) {
2604 case llvm::BitstreamEntry::Error:
2605 Error("malformed block record in AST file");
2606 return Failure;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002607 case llvm::BitstreamEntry::EndBlock: {
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002608 // Validate the module before returning. This call catches an AST with
2609 // no module name and no imports.
2610 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2611 return Result;
2612
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002613 // Validate input files.
2614 const HeaderSearchOptions &HSOpts =
2615 PP.getHeaderSearchInfo().getHeaderSearchOpts();
Ben Langmuircb69b572014-03-07 06:40:32 +00002616
Richard Smitha1825302014-10-23 22:18:29 +00002617 // All user input files reside at the index range [0, NumUserInputs), and
Richard Smith0f99d6a2015-08-09 08:48:41 +00002618 // system input files reside at [NumUserInputs, NumInputs). For explicitly
2619 // loaded module files, ignore missing inputs.
Manman Ren11f2a472016-08-18 17:42:15 +00002620 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2621 F.Kind != MK_PrebuiltModule) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002622 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Ben Langmuircb69b572014-03-07 06:40:32 +00002623
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002624 // If we are reading a module, we will create a verification timestamp,
2625 // so we verify all input files. Otherwise, verify only user input
2626 // files.
Ben Langmuircb69b572014-03-07 06:40:32 +00002627
2628 unsigned N = NumUserInputs;
2629 if (ValidateSystemInputs ||
Richard Smithe842a472014-10-22 02:05:46 +00002630 (HSOpts.ModulesValidateOncePerBuildSession &&
Ben Langmuiracb803e2014-11-10 22:13:10 +00002631 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
Richard Smithe842a472014-10-22 02:05:46 +00002632 F.Kind == MK_ImplicitModule))
Ben Langmuircb69b572014-03-07 06:40:32 +00002633 N = NumInputs;
2634
Ben Langmuir3d4417c2014-02-07 17:31:11 +00002635 for (unsigned I = 0; I < N; ++I) {
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002636 InputFile IF = getInputFile(F, I+1, Complain);
2637 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei11169dd2012-12-18 14:30:41 +00002638 return OutOfDate;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002639 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002640 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002641
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002642 if (Listener)
Richard Smith216a3bd2015-08-13 17:57:10 +00002643 Listener->visitModuleFile(F.FileName, F.Kind);
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002644
Ben Langmuircb69b572014-03-07 06:40:32 +00002645 if (Listener && Listener->needsInputFileVisitation()) {
2646 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2647 : NumUserInputs;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002648 for (unsigned I = 0; I < N; ++I) {
2649 bool IsSystem = I >= NumUserInputs;
2650 InputFileInfo FI = readInputFileInfo(F, I+1);
Richard Smith216a3bd2015-08-13 17:57:10 +00002651 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
Manman Ren11f2a472016-08-18 17:42:15 +00002652 F.Kind == MK_ExplicitModule ||
2653 F.Kind == MK_PrebuiltModule);
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002654 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002655 }
2656
Duncan P. N. Exon Smith69242e92019-11-19 14:44:22 -08002657 return Success;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002658 }
2659
Chris Lattnere7b154b2013-01-19 21:39:22 +00002660 case llvm::BitstreamEntry::SubBlock:
2661 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002662 case INPUT_FILES_BLOCK_ID:
2663 F.InputFilesCursor = Stream;
JF Bastien0e828952019-06-26 19:50:12 +00002664 if (llvm::Error Err = Stream.SkipBlock()) {
2665 Error(std::move(Err));
2666 return Failure;
2667 }
2668 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002669 Error("malformed block record in AST file");
2670 return Failure;
2671 }
2672 continue;
Richard Smith0516b182015-09-08 19:40:14 +00002673
2674 case OPTIONS_BLOCK_ID:
2675 // If we're reading the first module for this group, check its options
2676 // are compatible with ours. For modules it imports, no further checking
2677 // is required, because we checked them when we built it.
2678 if (Listener && !ImportedBy) {
2679 // Should we allow the configuration of the module file to differ from
2680 // the configuration of the current translation unit in a compatible
2681 // way?
2682 //
2683 // FIXME: Allow this for files explicitly specified with -include-pch.
2684 bool AllowCompatibleConfigurationMismatch =
Manman Ren11f2a472016-08-18 17:42:15 +00002685 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
Richard Smith0516b182015-09-08 19:40:14 +00002686
Duncan P. N. Exon Smith69242e92019-11-19 14:44:22 -08002687 ASTReadResult Result =
2688 ReadOptionsBlock(Stream, ClientLoadCapabilities,
2689 AllowCompatibleConfigurationMismatch, *Listener,
2690 SuggestedPredefines);
Richard Smith0516b182015-09-08 19:40:14 +00002691 if (Result == Failure) {
2692 Error("malformed block record in AST file");
2693 return Result;
2694 }
2695
Richard Smith8a308ec2015-11-05 00:54:55 +00002696 if (DisableValidation ||
2697 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2698 Result = Success;
2699
Ben Langmuir9b1e442e2016-02-11 18:54:02 +00002700 // If we can't load the module, exit early since we likely
2701 // will rebuild the module anyway. The stream may be in the
2702 // middle of a block.
2703 if (Result != Success)
Richard Smith0516b182015-09-08 19:40:14 +00002704 return Result;
JF Bastien0e828952019-06-26 19:50:12 +00002705 } else if (llvm::Error Err = Stream.SkipBlock()) {
2706 Error(std::move(Err));
Richard Smith0516b182015-09-08 19:40:14 +00002707 return Failure;
2708 }
2709 continue;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002710
Guy Benyei11169dd2012-12-18 14:30:41 +00002711 default:
JF Bastien0e828952019-06-26 19:50:12 +00002712 if (llvm::Error Err = Stream.SkipBlock()) {
2713 Error(std::move(Err));
Chris Lattnere7b154b2013-01-19 21:39:22 +00002714 return Failure;
2715 }
2716 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00002717 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002718
Chris Lattnere7b154b2013-01-19 21:39:22 +00002719 case llvm::BitstreamEntry::Record:
2720 // The interesting case.
2721 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002722 }
2723
2724 // Read and process a record.
2725 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002726 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00002727 Expected<unsigned> MaybeRecordType =
2728 Stream.readRecord(Entry.ID, Record, &Blob);
2729 if (!MaybeRecordType) {
2730 Error(MaybeRecordType.takeError());
2731 return Failure;
2732 }
2733 switch ((ControlRecordTypes)MaybeRecordType.get()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002734 case METADATA: {
2735 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2736 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002737 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2738 : diag::err_pch_version_too_new);
Guy Benyei11169dd2012-12-18 14:30:41 +00002739 return VersionMismatch;
2740 }
2741
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00002742 bool hasErrors = Record[7];
Guy Benyei11169dd2012-12-18 14:30:41 +00002743 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2744 Diag(diag::err_pch_with_compiler_errors);
2745 return HadErrors;
2746 }
Argyrios Kyrtzidis70ec1c72016-07-13 20:35:26 +00002747 if (hasErrors) {
2748 Diags.ErrorOccurred = true;
2749 Diags.UncompilableErrorOccurred = true;
2750 Diags.UnrecoverableErrorOccurred = true;
2751 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002752
2753 F.RelocatablePCH = Record[4];
Richard Smith7ed1bc92014-12-05 22:42:13 +00002754 // Relative paths in a relocatable PCH are relative to our sysroot.
2755 if (F.RelocatablePCH)
2756 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
Guy Benyei11169dd2012-12-18 14:30:41 +00002757
Richard Smithe75ee0f2015-08-17 07:13:32 +00002758 F.HasTimestamps = Record[5];
2759
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00002760 F.PCHHasObjectFile = Record[6];
2761
Guy Benyei11169dd2012-12-18 14:30:41 +00002762 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002763 StringRef ASTBranch = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002764 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2765 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002766 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
Guy Benyei11169dd2012-12-18 14:30:41 +00002767 return VersionMismatch;
2768 }
2769 break;
2770 }
2771
2772 case IMPORTS: {
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002773 // Validate the AST before processing any imports (otherwise, untangling
2774 // them can be error-prone and expensive). A module will have a name and
2775 // will already have been validated, but this catches the PCH case.
2776 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2777 return Result;
2778
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002779 // Load each of the imported PCH files.
Guy Benyei11169dd2012-12-18 14:30:41 +00002780 unsigned Idx = 0, N = Record.size();
2781 while (Idx < N) {
2782 // Read information about the AST file.
Vedant Kumar48b4f762018-04-14 01:40:48 +00002783 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00002784 // The import location will be the local one for now; we will adjust
2785 // all import locations of module imports after the global source
Richard Smithb22a1d12016-03-27 20:13:24 +00002786 // location info are setup, in ReadAST.
Guy Benyei11169dd2012-12-18 14:30:41 +00002787 SourceLocation ImportLoc =
Richard Smithb22a1d12016-03-27 20:13:24 +00002788 ReadUntranslatedSourceLocation(Record[Idx++]);
Vedant Kumar48b4f762018-04-14 01:40:48 +00002789 off_t StoredSize = (off_t)Record[Idx++];
2790 time_t StoredModTime = (time_t)Record[Idx++];
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002791 ASTFileSignature StoredSignature = {
2792 {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2793 (uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2794 (uint32_t)Record[Idx++]}}};
Boris Kolpackovd30446f2017-08-31 06:26:43 +00002795
2796 std::string ImportedName = ReadString(Record, Idx);
2797 std::string ImportedFile;
2798
2799 // For prebuilt and explicit modules first consult the file map for
2800 // an override. Note that here we don't search prebuilt module
2801 // directories, only the explicit name to file mappings. Also, we will
2802 // still verify the size/signature making sure it is essentially the
2803 // same file but perhaps in a different location.
2804 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2805 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2806 ImportedName, /*FileMapOnly*/ true);
2807
2808 if (ImportedFile.empty())
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00002809 // Use BaseDirectoryAsWritten to ensure we use the same path in the
2810 // ModuleCache as when writing.
2811 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
Boris Kolpackovd30446f2017-08-31 06:26:43 +00002812 else
2813 SkipPath(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00002814
Richard Smith0f99d6a2015-08-09 08:48:41 +00002815 // If our client can't cope with us being out of date, we can't cope with
2816 // our dependency being missing.
2817 unsigned Capabilities = ClientLoadCapabilities;
2818 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2819 Capabilities &= ~ARR_Missing;
2820
Guy Benyei11169dd2012-12-18 14:30:41 +00002821 // Load the AST file.
Richard Smith0f99d6a2015-08-09 08:48:41 +00002822 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2823 Loaded, StoredSize, StoredModTime,
2824 StoredSignature, Capabilities);
2825
2826 // If we diagnosed a problem, produce a backtrace.
2827 if (isDiagnosedResult(Result, Capabilities))
2828 Diag(diag::note_module_file_imported_by)
2829 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2830
2831 switch (Result) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002832 case Failure: return Failure;
2833 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor2f1806e2013-03-19 00:38:50 +00002834 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00002835 case OutOfDate: return OutOfDate;
2836 case VersionMismatch: return VersionMismatch;
2837 case ConfigurationMismatch: return ConfigurationMismatch;
2838 case HadErrors: return HadErrors;
2839 case Success: break;
2840 }
2841 }
2842 break;
2843 }
2844
Guy Benyei11169dd2012-12-18 14:30:41 +00002845 case ORIGINAL_FILE:
2846 F.OriginalSourceFileID = FileID::get(Record[0]);
Benjamin Krameradcd0262020-01-28 20:23:46 +01002847 F.ActualOriginalSourceFileName = std::string(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00002848 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
Richard Smith7ed1bc92014-12-05 22:42:13 +00002849 ResolveImportedPath(F, F.OriginalSourceFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002850 break;
2851
2852 case ORIGINAL_FILE_ID:
2853 F.OriginalSourceFileID = FileID::get(Record[0]);
2854 break;
2855
2856 case ORIGINAL_PCH_DIR:
Benjamin Krameradcd0262020-01-28 20:23:46 +01002857 F.OriginalDir = std::string(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00002858 break;
2859
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002860 case MODULE_NAME:
Benjamin Krameradcd0262020-01-28 20:23:46 +01002861 F.ModuleName = std::string(Blob);
Duncan P. N. Exon Smith9dda8f52019-03-06 02:50:46 +00002862 Diag(diag::remark_module_import)
2863 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2864 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
Ben Langmuir4f5212a2014-04-14 22:12:44 +00002865 if (Listener)
2866 Listener->ReadModuleName(F.ModuleName);
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002867
2868 // Validate the AST as soon as we have a name so we can exit early on
2869 // failure.
2870 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2871 return Result;
Vedant Kumar48b4f762018-04-14 01:40:48 +00002872
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002873 break;
2874
Richard Smith223d3f22014-12-06 03:21:08 +00002875 case MODULE_DIRECTORY: {
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00002876 // Save the BaseDirectory as written in the PCM for computing the module
2877 // filename for the ModuleCache.
2878 BaseDirectoryAsWritten = Blob;
Richard Smith223d3f22014-12-06 03:21:08 +00002879 assert(!F.ModuleName.empty() &&
2880 "MODULE_DIRECTORY found before MODULE_NAME");
2881 // If we've already loaded a module map file covering this module, we may
2882 // have a better path for it (relative to the current build).
Bruno Cardoso Lopes52431f32018-07-18 23:21:19 +00002883 Module *M = PP.getHeaderSearchInfo().lookupModule(
2884 F.ModuleName, /*AllowSearch*/ true,
2885 /*AllowExtraModuleMapSearch*/ true);
Richard Smith223d3f22014-12-06 03:21:08 +00002886 if (M && M->Directory) {
2887 // If we're implicitly loading a module, the base directory can't
2888 // change between the build and use.
Yuka Takahashid8baec22018-08-01 09:50:02 +00002889 // Don't emit module relocation error if we have -fno-validate-pch
2890 if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2891 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
Harlan Haskins8d323d12019-08-01 21:31:56 +00002892 auto BuildDir = PP.getFileManager().getDirectory(Blob);
2893 if (!BuildDir || *BuildDir != M->Directory) {
Richard Smith223d3f22014-12-06 03:21:08 +00002894 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2895 Diag(diag::err_imported_module_relocated)
2896 << F.ModuleName << Blob << M->Directory->getName();
2897 return OutOfDate;
2898 }
2899 }
Benjamin Krameradcd0262020-01-28 20:23:46 +01002900 F.BaseDirectory = std::string(M->Directory->getName());
Richard Smith223d3f22014-12-06 03:21:08 +00002901 } else {
Benjamin Krameradcd0262020-01-28 20:23:46 +01002902 F.BaseDirectory = std::string(Blob);
Richard Smith223d3f22014-12-06 03:21:08 +00002903 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002904 break;
Richard Smith223d3f22014-12-06 03:21:08 +00002905 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002906
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002907 case MODULE_MAP_FILE:
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00002908 if (ASTReadResult Result =
2909 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2910 return Result;
Ben Langmuir264ea152014-11-08 00:06:39 +00002911 break;
2912
Justin Bognerca9c0cc2015-06-21 20:32:36 +00002913 case INPUT_FILE_OFFSETS:
Richard Smitha1825302014-10-23 22:18:29 +00002914 NumInputs = Record[0];
2915 NumUserInputs = Record[1];
Justin Bogner4c183242015-06-21 20:32:40 +00002916 F.InputFileOffsets =
2917 (const llvm::support::unaligned_uint64_t *)Blob.data();
Richard Smitha1825302014-10-23 22:18:29 +00002918 F.InputFilesLoaded.resize(NumInputs);
Argyrios Kyrtzidisa38cb202017-01-30 06:05:58 +00002919 F.NumUserInputFiles = NumUserInputs;
Guy Benyei11169dd2012-12-18 14:30:41 +00002920 break;
2921 }
2922 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002923}
2924
Ben Langmuir2c9af442014-04-10 17:57:43 +00002925ASTReader::ASTReadResult
2926ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002927 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002928
JF Bastien0e828952019-06-26 19:50:12 +00002929 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2930 Error(std::move(Err));
Ben Langmuir2c9af442014-04-10 17:57:43 +00002931 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002932 }
2933
2934 // Read all of the records and blocks for the AST file.
2935 RecordData Record;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002936 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00002937 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2938 if (!MaybeEntry) {
2939 Error(MaybeEntry.takeError());
2940 return Failure;
2941 }
2942 llvm::BitstreamEntry Entry = MaybeEntry.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002943
Chris Lattnere7b154b2013-01-19 21:39:22 +00002944 switch (Entry.Kind) {
2945 case llvm::BitstreamEntry::Error:
2946 Error("error at end of module block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002947 return Failure;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002948 case llvm::BitstreamEntry::EndBlock:
Richard Smithc0fbba72013-04-03 22:49:41 +00002949 // Outside of C++, we do not store a lookup map for the translation unit.
2950 // Instead, mark it as needing a lookup map to be built if this module
2951 // contains any declarations lexically within it (which it always does!).
2952 // This usually has no cost, since we very rarely need the lookup map for
2953 // the translation unit outside C++.
Richard Smithdbafb6c2017-06-29 23:23:46 +00002954 if (ASTContext *Ctx = ContextObj) {
2955 DeclContext *DC = Ctx->getTranslationUnitDecl();
2956 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2957 DC->setMustBuildLookupTable();
2958 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002959
Ben Langmuir2c9af442014-04-10 17:57:43 +00002960 return Success;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002961 case llvm::BitstreamEntry::SubBlock:
2962 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002963 case DECLTYPES_BLOCK_ID:
2964 // We lazily load the decls block, but we want to set up the
2965 // DeclsCursor cursor to point into it. Clone our current bitcode
2966 // cursor to it, enter the block and read the abbrevs in that block.
2967 // With the main cursor, we just skip over it.
2968 F.DeclsCursor = Stream;
JF Bastien0e828952019-06-26 19:50:12 +00002969 if (llvm::Error Err = Stream.SkipBlock()) {
2970 Error(std::move(Err));
2971 return Failure;
2972 }
2973 if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002974 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002975 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002976 }
2977 break;
Richard Smithb9eab6d2014-03-20 19:44:17 +00002978
Guy Benyei11169dd2012-12-18 14:30:41 +00002979 case PREPROCESSOR_BLOCK_ID:
2980 F.MacroCursor = Stream;
2981 if (!PP.getExternalSource())
2982 PP.setExternalSource(this);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002983
JF Bastien0e828952019-06-26 19:50:12 +00002984 if (llvm::Error Err = Stream.SkipBlock()) {
2985 Error(std::move(Err));
2986 return Failure;
2987 }
2988 if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002989 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002990 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002991 }
2992 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2993 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002994
Guy Benyei11169dd2012-12-18 14:30:41 +00002995 case PREPROCESSOR_DETAIL_BLOCK_ID:
2996 F.PreprocessorDetailCursor = Stream;
JF Bastien0e828952019-06-26 19:50:12 +00002997
2998 if (llvm::Error Err = Stream.SkipBlock()) {
2999 Error(std::move(Err));
3000 return Failure;
3001 }
3002 if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00003003 PREPROCESSOR_DETAIL_BLOCK_ID)) {
JF Bastien0e828952019-06-26 19:50:12 +00003004 Error("malformed preprocessor detail record in AST file");
3005 return Failure;
3006 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003007 F.PreprocessorDetailStartOffset
Chris Lattnere7b154b2013-01-19 21:39:22 +00003008 = F.PreprocessorDetailCursor.GetCurrentBitNo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003009
Guy Benyei11169dd2012-12-18 14:30:41 +00003010 if (!PP.getPreprocessingRecord())
3011 PP.createPreprocessingRecord();
3012 if (!PP.getPreprocessingRecord()->getExternalSource())
3013 PP.getPreprocessingRecord()->SetExternalSource(*this);
3014 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003015
Guy Benyei11169dd2012-12-18 14:30:41 +00003016 case SOURCE_MANAGER_BLOCK_ID:
3017 if (ReadSourceManagerBlock(F))
Ben Langmuir2c9af442014-04-10 17:57:43 +00003018 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003019 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003020
Guy Benyei11169dd2012-12-18 14:30:41 +00003021 case SUBMODULE_BLOCK_ID:
David Blaikie9ffe5a32017-01-30 05:00:26 +00003022 if (ASTReadResult Result =
3023 ReadSubmoduleBlock(F, ClientLoadCapabilities))
Ben Langmuir2c9af442014-04-10 17:57:43 +00003024 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00003025 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003026
Guy Benyei11169dd2012-12-18 14:30:41 +00003027 case COMMENTS_BLOCK_ID: {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003028 BitstreamCursor C = Stream;
JF Bastien0e828952019-06-26 19:50:12 +00003029
3030 if (llvm::Error Err = Stream.SkipBlock()) {
3031 Error(std::move(Err));
3032 return Failure;
3033 }
3034 if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003035 Error("malformed comments block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003036 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003037 }
3038 CommentsCursors.push_back(std::make_pair(C, &F));
3039 break;
3040 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003041
Guy Benyei11169dd2012-12-18 14:30:41 +00003042 default:
JF Bastien0e828952019-06-26 19:50:12 +00003043 if (llvm::Error Err = Stream.SkipBlock()) {
3044 Error(std::move(Err));
Ben Langmuir2c9af442014-04-10 17:57:43 +00003045 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00003046 }
3047 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003048 }
3049 continue;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003050
Chris Lattnere7b154b2013-01-19 21:39:22 +00003051 case llvm::BitstreamEntry::Record:
3052 // The interesting case.
3053 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003054 }
3055
3056 // Read and process a record.
3057 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00003058 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00003059 Expected<unsigned> MaybeRecordType =
3060 Stream.readRecord(Entry.ID, Record, &Blob);
3061 if (!MaybeRecordType) {
3062 Error(MaybeRecordType.takeError());
3063 return Failure;
3064 }
3065 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
Richard Smithdbafb6c2017-06-29 23:23:46 +00003066
3067 // If we're not loading an AST context, we don't care about most records.
3068 if (!ContextObj) {
3069 switch (RecordType) {
3070 case IDENTIFIER_TABLE:
3071 case IDENTIFIER_OFFSET:
3072 case INTERESTING_IDENTIFIERS:
3073 case STATISTICS:
3074 case PP_CONDITIONAL_STACK:
3075 case PP_COUNTER_VALUE:
3076 case SOURCE_LOCATION_OFFSETS:
3077 case MODULE_OFFSET_MAP:
3078 case SOURCE_MANAGER_LINE_TABLE:
3079 case SOURCE_LOCATION_PRELOADS:
3080 case PPD_ENTITIES_OFFSETS:
3081 case HEADER_SEARCH_TABLE:
3082 case IMPORTED_MODULES:
3083 case MACRO_OFFSET:
3084 break;
3085 default:
3086 continue;
3087 }
3088 }
3089
3090 switch (RecordType) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003091 default: // Default behavior: ignore.
3092 break;
3093
3094 case TYPE_OFFSET: {
3095 if (F.LocalNumTypes != 0) {
3096 Error("duplicate TYPE_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003097 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003098 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003099 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003100 F.LocalNumTypes = Record[0];
3101 unsigned LocalBaseTypeIndex = Record[1];
3102 F.BaseTypeIndex = getTotalNumTypes();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003103
Guy Benyei11169dd2012-12-18 14:30:41 +00003104 if (F.LocalNumTypes > 0) {
3105 // Introduce the global -> local mapping for types within this module.
3106 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003107
Guy Benyei11169dd2012-12-18 14:30:41 +00003108 // Introduce the local -> global mapping for types within this module.
3109 F.TypeRemap.insertOrReplace(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003110 std::make_pair(LocalBaseTypeIndex,
Guy Benyei11169dd2012-12-18 14:30:41 +00003111 F.BaseTypeIndex - LocalBaseTypeIndex));
Ben Langmuir52ca6782014-10-20 16:27:32 +00003112
3113 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
Guy Benyei11169dd2012-12-18 14:30:41 +00003114 }
3115 break;
3116 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003117
Guy Benyei11169dd2012-12-18 14:30:41 +00003118 case DECL_OFFSET: {
3119 if (F.LocalNumDecls != 0) {
3120 Error("duplicate DECL_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003121 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003122 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003123 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003124 F.LocalNumDecls = Record[0];
3125 unsigned LocalBaseDeclID = Record[1];
3126 F.BaseDeclID = getTotalNumDecls();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003127
Guy Benyei11169dd2012-12-18 14:30:41 +00003128 if (F.LocalNumDecls > 0) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003129 // Introduce the global -> local mapping for declarations within this
Guy Benyei11169dd2012-12-18 14:30:41 +00003130 // module.
3131 GlobalDeclMap.insert(
3132 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003133
Guy Benyei11169dd2012-12-18 14:30:41 +00003134 // Introduce the local -> global mapping for declarations within this
3135 // module.
3136 F.DeclRemap.insertOrReplace(
3137 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003138
Guy Benyei11169dd2012-12-18 14:30:41 +00003139 // Introduce the global -> local mapping for declarations within this
3140 // module.
3141 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
Ben Langmuirfe971d92014-08-16 04:54:18 +00003142
Ben Langmuir52ca6782014-10-20 16:27:32 +00003143 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3144 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003145 break;
3146 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003147
Guy Benyei11169dd2012-12-18 14:30:41 +00003148 case TU_UPDATE_LEXICAL: {
Richard Smithdbafb6c2017-06-29 23:23:46 +00003149 DeclContext *TU = ContextObj->getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00003150 LexicalContents Contents(
3151 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3152 Blob.data()),
3153 static_cast<unsigned int>(Blob.size() / 4));
3154 TULexicalDecls.push_back(std::make_pair(&F, Contents));
Guy Benyei11169dd2012-12-18 14:30:41 +00003155 TU->setHasExternalLexicalStorage(true);
3156 break;
3157 }
3158
3159 case UPDATE_VISIBLE: {
3160 unsigned Idx = 0;
3161 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003162 auto *Data = (const unsigned char*)Blob.data();
Richard Smithd88a7f12015-09-01 20:35:42 +00003163 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
Richard Smith0f4e2c42015-08-06 04:23:48 +00003164 // If we've already loaded the decl, perform the updates when we finish
3165 // loading this block.
3166 if (Decl *D = GetExistingDecl(ID))
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003167 PendingUpdateRecords.push_back(
3168 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
Guy Benyei11169dd2012-12-18 14:30:41 +00003169 break;
3170 }
3171
3172 case IDENTIFIER_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00003173 F.IdentifierTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003174 if (Record[0]) {
Justin Bognerda4e6502014-04-14 16:34:29 +00003175 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3176 (const unsigned char *)F.IdentifierTableData + Record[0],
3177 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3178 (const unsigned char *)F.IdentifierTableData,
3179 ASTIdentifierLookupTrait(*this, F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003180
Guy Benyei11169dd2012-12-18 14:30:41 +00003181 PP.getIdentifierTable().setExternalIdentifierLookup(this);
3182 }
3183 break;
3184
3185 case IDENTIFIER_OFFSET: {
3186 if (F.LocalNumIdentifiers != 0) {
3187 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003188 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003189 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003190 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003191 F.LocalNumIdentifiers = Record[0];
3192 unsigned LocalBaseIdentifierID = Record[1];
3193 F.BaseIdentifierID = getTotalNumIdentifiers();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003194
Guy Benyei11169dd2012-12-18 14:30:41 +00003195 if (F.LocalNumIdentifiers > 0) {
3196 // Introduce the global -> local mapping for identifiers within this
3197 // module.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003198 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
Guy Benyei11169dd2012-12-18 14:30:41 +00003199 &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003200
Guy Benyei11169dd2012-12-18 14:30:41 +00003201 // Introduce the local -> global mapping for identifiers within this
3202 // module.
3203 F.IdentifierRemap.insertOrReplace(
3204 std::make_pair(LocalBaseIdentifierID,
3205 F.BaseIdentifierID - LocalBaseIdentifierID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00003206
Ben Langmuir52ca6782014-10-20 16:27:32 +00003207 IdentifiersLoaded.resize(IdentifiersLoaded.size()
3208 + F.LocalNumIdentifiers);
3209 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003210 break;
3211 }
3212
Richard Smith33e0f7e2015-07-22 02:08:40 +00003213 case INTERESTING_IDENTIFIERS:
3214 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3215 break;
3216
Ben Langmuir332aafe2014-01-31 01:06:56 +00003217 case EAGERLY_DESERIALIZED_DECLS:
Richard Smith9e2341d2015-03-23 03:25:59 +00003218 // FIXME: Skip reading this record if our ASTConsumer doesn't care
3219 // about "interesting" decls (for instance, if we're building a module).
Guy Benyei11169dd2012-12-18 14:30:41 +00003220 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Ben Langmuir332aafe2014-01-31 01:06:56 +00003221 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00003222 break;
3223
David Blaikie9ffe5a32017-01-30 05:00:26 +00003224 case MODULAR_CODEGEN_DECLS:
3225 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3226 // them (ie: if we're not codegenerating this module).
Hans Wennborg7ea9a6e2020-02-27 14:33:43 +01003227 if (F.Kind == MK_MainFile)
David Blaikie9ffe5a32017-01-30 05:00:26 +00003228 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3229 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3230 break;
3231
Guy Benyei11169dd2012-12-18 14:30:41 +00003232 case SPECIAL_TYPES:
Douglas Gregor44180f82013-02-01 23:45:03 +00003233 if (SpecialTypes.empty()) {
3234 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3235 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3236 break;
3237 }
3238
3239 if (SpecialTypes.size() != Record.size()) {
3240 Error("invalid special-types record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003241 return Failure;
Douglas Gregor44180f82013-02-01 23:45:03 +00003242 }
3243
3244 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3245 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3246 if (!SpecialTypes[I])
3247 SpecialTypes[I] = ID;
3248 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3249 // merge step?
3250 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003251 break;
3252
3253 case STATISTICS:
3254 TotalNumStatements += Record[0];
3255 TotalNumMacros += Record[1];
3256 TotalLexicalDeclContexts += Record[2];
3257 TotalVisibleDeclContexts += Record[3];
3258 break;
3259
3260 case UNUSED_FILESCOPED_DECLS:
3261 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3262 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3263 break;
3264
3265 case DELEGATING_CTORS:
3266 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3267 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3268 break;
3269
3270 case WEAK_UNDECLARED_IDENTIFIERS:
3271 if (Record.size() % 4 != 0) {
3272 Error("invalid weak identifiers record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003273 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003274 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003275
3276 // FIXME: Ignore weak undeclared identifiers from non-original PCH
Guy Benyei11169dd2012-12-18 14:30:41 +00003277 // files. This isn't the way to do it :)
3278 WeakUndeclaredIdentifiers.clear();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003279
Guy Benyei11169dd2012-12-18 14:30:41 +00003280 // Translate the weak, undeclared identifiers into global IDs.
3281 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3282 WeakUndeclaredIdentifiers.push_back(
3283 getGlobalIdentifierID(F, Record[I++]));
3284 WeakUndeclaredIdentifiers.push_back(
3285 getGlobalIdentifierID(F, Record[I++]));
3286 WeakUndeclaredIdentifiers.push_back(
3287 ReadSourceLocation(F, Record, I).getRawEncoding());
3288 WeakUndeclaredIdentifiers.push_back(Record[I++]);
3289 }
3290 break;
3291
Guy Benyei11169dd2012-12-18 14:30:41 +00003292 case SELECTOR_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003293 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003294 F.LocalNumSelectors = Record[0];
3295 unsigned LocalBaseSelectorID = Record[1];
3296 F.BaseSelectorID = getTotalNumSelectors();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003297
Guy Benyei11169dd2012-12-18 14:30:41 +00003298 if (F.LocalNumSelectors > 0) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003299 // Introduce the global -> local mapping for selectors within this
Guy Benyei11169dd2012-12-18 14:30:41 +00003300 // module.
3301 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003302
3303 // Introduce the local -> global mapping for selectors within this
Guy Benyei11169dd2012-12-18 14:30:41 +00003304 // module.
3305 F.SelectorRemap.insertOrReplace(
3306 std::make_pair(LocalBaseSelectorID,
3307 F.BaseSelectorID - LocalBaseSelectorID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00003308
3309 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
Guy Benyei11169dd2012-12-18 14:30:41 +00003310 }
3311 break;
3312 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003313
Guy Benyei11169dd2012-12-18 14:30:41 +00003314 case METHOD_POOL:
Chris Lattner0e6c9402013-01-20 02:38:54 +00003315 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003316 if (Record[0])
3317 F.SelectorLookupTable
3318 = ASTSelectorLookupTable::Create(
3319 F.SelectorLookupTableData + Record[0],
3320 F.SelectorLookupTableData,
3321 ASTSelectorLookupTrait(*this, F));
3322 TotalNumMethodPoolEntries += Record[1];
3323 break;
3324
3325 case REFERENCED_SELECTOR_POOL:
3326 if (!Record.empty()) {
3327 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003328 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
Guy Benyei11169dd2012-12-18 14:30:41 +00003329 Record[Idx++]));
3330 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3331 getRawEncoding());
3332 }
3333 }
3334 break;
3335
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00003336 case PP_CONDITIONAL_STACK:
3337 if (!Record.empty()) {
Erik Verbruggen4d1eb2d2017-11-03 09:40:07 +00003338 unsigned Idx = 0, End = Record.size() - 1;
3339 bool ReachedEOFWhileSkipping = Record[Idx++];
3340 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3341 if (ReachedEOFWhileSkipping) {
3342 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3343 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3344 bool FoundNonSkipPortion = Record[Idx++];
3345 bool FoundElse = Record[Idx++];
3346 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3347 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3348 FoundElse, ElseLoc);
3349 }
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00003350 SmallVector<PPConditionalInfo, 4> ConditionalStack;
Erik Verbruggen4d1eb2d2017-11-03 09:40:07 +00003351 while (Idx < End) {
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00003352 auto Loc = ReadSourceLocation(F, Record, Idx);
3353 bool WasSkipping = Record[Idx++];
3354 bool FoundNonSkip = Record[Idx++];
3355 bool FoundElse = Record[Idx++];
3356 ConditionalStack.push_back(
3357 {Loc, WasSkipping, FoundNonSkip, FoundElse});
3358 }
Erik Verbruggen4d1eb2d2017-11-03 09:40:07 +00003359 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00003360 }
3361 break;
3362
Guy Benyei11169dd2012-12-18 14:30:41 +00003363 case PP_COUNTER_VALUE:
3364 if (!Record.empty() && Listener)
3365 Listener->ReadCounter(F, Record[0]);
3366 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003367
Guy Benyei11169dd2012-12-18 14:30:41 +00003368 case FILE_SORTED_DECLS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00003369 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003370 F.NumFileSortedDecls = Record[0];
3371 break;
3372
3373 case SOURCE_LOCATION_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003374 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003375 F.LocalNumSLocEntries = Record[0];
3376 unsigned SLocSpaceSize = Record[1];
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00003377 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Ben Langmuir52ca6782014-10-20 16:27:32 +00003378 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
Guy Benyei11169dd2012-12-18 14:30:41 +00003379 SLocSpaceSize);
Richard Smith78d81ec2015-08-12 22:25:24 +00003380 if (!F.SLocEntryBaseID) {
3381 Error("ran out of source locations");
3382 break;
3383 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003384 // Make our entry in the range map. BaseID is negative and growing, so
3385 // we invert it. Because we invert it, though, we need the other end of
3386 // the range.
3387 unsigned RangeStart =
3388 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3389 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3390 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3391
3392 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3393 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3394 GlobalSLocOffsetMap.insert(
3395 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3396 - SLocSpaceSize,&F));
3397
3398 // Initialize the remapping table.
3399 // Invalid stays invalid.
Richard Smithb9eab6d2014-03-20 19:44:17 +00003400 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
Guy Benyei11169dd2012-12-18 14:30:41 +00003401 // This module. Base was 2 when being compiled.
Richard Smithb9eab6d2014-03-20 19:44:17 +00003402 F.SLocRemap.insertOrReplace(std::make_pair(2U,
Guy Benyei11169dd2012-12-18 14:30:41 +00003403 static_cast<int>(F.SLocEntryBaseOffset - 2)));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003404
Guy Benyei11169dd2012-12-18 14:30:41 +00003405 TotalNumSLocEntries += F.LocalNumSLocEntries;
3406 break;
3407 }
3408
Richard Smith37a93df2017-02-18 00:32:02 +00003409 case MODULE_OFFSET_MAP:
3410 F.ModuleOffsetMap = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00003411 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003412
3413 case SOURCE_MANAGER_LINE_TABLE:
Duncan P. N. Exon Smith8e2c1922019-11-10 11:07:20 -08003414 if (ParseLineTable(F, Record)) {
3415 Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003416 return Failure;
Duncan P. N. Exon Smith8e2c1922019-11-10 11:07:20 -08003417 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003418 break;
3419
3420 case SOURCE_LOCATION_PRELOADS: {
3421 // Need to transform from the local view (1-based IDs) to the global view,
3422 // which is based off F.SLocEntryBaseID.
3423 if (!F.PreloadSLocEntries.empty()) {
3424 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003425 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003426 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003427
Guy Benyei11169dd2012-12-18 14:30:41 +00003428 F.PreloadSLocEntries.swap(Record);
3429 break;
3430 }
3431
3432 case EXT_VECTOR_DECLS:
3433 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3434 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3435 break;
3436
3437 case VTABLE_USES:
3438 if (Record.size() % 3 != 0) {
3439 Error("Invalid VTABLE_USES record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003440 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003441 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003442
Guy Benyei11169dd2012-12-18 14:30:41 +00003443 // Later tables overwrite earlier ones.
3444 // FIXME: Modules will have some trouble with this. This is clearly not
3445 // the right way to do this.
3446 VTableUses.clear();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003447
Guy Benyei11169dd2012-12-18 14:30:41 +00003448 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3449 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3450 VTableUses.push_back(
3451 ReadSourceLocation(F, Record, Idx).getRawEncoding());
3452 VTableUses.push_back(Record[Idx++]);
3453 }
3454 break;
3455
Guy Benyei11169dd2012-12-18 14:30:41 +00003456 case PENDING_IMPLICIT_INSTANTIATIONS:
3457 if (PendingInstantiations.size() % 2 != 0) {
3458 Error("Invalid existing PendingInstantiations");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003459 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003460 }
3461
3462 if (Record.size() % 2 != 0) {
3463 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003464 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003465 }
3466
3467 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3468 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3469 PendingInstantiations.push_back(
3470 ReadSourceLocation(F, Record, I).getRawEncoding());
3471 }
3472 break;
3473
3474 case SEMA_DECL_REFS:
Richard Smith96269c52016-09-29 22:49:46 +00003475 if (Record.size() != 3) {
Richard Smith3d8e97e2013-10-18 06:54:39 +00003476 Error("Invalid SEMA_DECL_REFS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003477 return Failure;
Richard Smith3d8e97e2013-10-18 06:54:39 +00003478 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003479 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3480 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3481 break;
3482
3483 case PPD_ENTITIES_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003484 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3485 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3486 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00003487
3488 unsigned LocalBasePreprocessedEntityID = Record[0];
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003489
Guy Benyei11169dd2012-12-18 14:30:41 +00003490 unsigned StartingID;
3491 if (!PP.getPreprocessingRecord())
3492 PP.createPreprocessingRecord();
3493 if (!PP.getPreprocessingRecord()->getExternalSource())
3494 PP.getPreprocessingRecord()->SetExternalSource(*this);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003495 StartingID
Guy Benyei11169dd2012-12-18 14:30:41 +00003496 = PP.getPreprocessingRecord()
Ben Langmuir52ca6782014-10-20 16:27:32 +00003497 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Guy Benyei11169dd2012-12-18 14:30:41 +00003498 F.BasePreprocessedEntityID = StartingID;
3499
3500 if (F.NumPreprocessedEntities > 0) {
3501 // Introduce the global -> local mapping for preprocessed entities in
3502 // this module.
3503 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003504
Guy Benyei11169dd2012-12-18 14:30:41 +00003505 // Introduce the local -> global mapping for preprocessed entities in
3506 // this module.
3507 F.PreprocessedEntityRemap.insertOrReplace(
3508 std::make_pair(LocalBasePreprocessedEntityID,
3509 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3510 }
3511
3512 break;
3513 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003514
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00003515 case PPD_SKIPPED_RANGES: {
3516 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3517 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3518 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3519
3520 if (!PP.getPreprocessingRecord())
3521 PP.createPreprocessingRecord();
3522 if (!PP.getPreprocessingRecord()->getExternalSource())
3523 PP.getPreprocessingRecord()->SetExternalSource(*this);
3524 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3525 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003526
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00003527 if (F.NumPreprocessedSkippedRanges > 0)
3528 GlobalSkippedRangeMap.insert(
3529 std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3530 break;
3531 }
3532
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003533 case DECL_UPDATE_OFFSETS:
Guy Benyei11169dd2012-12-18 14:30:41 +00003534 if (Record.size() % 2 != 0) {
3535 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003536 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003537 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003538 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3539 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3540 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3541
3542 // If we've already loaded the decl, perform the updates when we finish
3543 // loading this block.
3544 if (Decl *D = GetExistingDecl(ID))
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003545 PendingUpdateRecords.push_back(
3546 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
Richard Smithcd45dbc2014-04-19 03:48:30 +00003547 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003548 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003549
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003550 case OBJC_CATEGORIES_MAP:
Guy Benyei11169dd2012-12-18 14:30:41 +00003551 if (F.LocalNumObjCCategoriesInMap != 0) {
3552 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003553 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003554 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003555
Guy Benyei11169dd2012-12-18 14:30:41 +00003556 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003557 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003558 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003559
Guy Benyei11169dd2012-12-18 14:30:41 +00003560 case OBJC_CATEGORIES:
3561 F.ObjCCategories.swap(Record);
3562 break;
Richard Smithc2bb8182015-03-24 06:36:48 +00003563
Guy Benyei11169dd2012-12-18 14:30:41 +00003564 case CUDA_SPECIAL_DECL_REFS:
3565 // Later tables overwrite earlier ones.
3566 // FIXME: Modules will have trouble with this.
3567 CUDASpecialDeclRefs.clear();
3568 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3569 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3570 break;
3571
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003572 case HEADER_SEARCH_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00003573 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003574 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00003575 if (Record[0]) {
3576 F.HeaderFileInfoTable
3577 = HeaderFileInfoLookupTable::Create(
3578 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3579 (const unsigned char *)F.HeaderFileInfoTableData,
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003580 HeaderFileInfoTrait(*this, F,
Guy Benyei11169dd2012-12-18 14:30:41 +00003581 &PP.getHeaderSearchInfo(),
Chris Lattner0e6c9402013-01-20 02:38:54 +00003582 Blob.data() + Record[2]));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003583
Guy Benyei11169dd2012-12-18 14:30:41 +00003584 PP.getHeaderSearchInfo().SetExternalSource(this);
3585 if (!PP.getHeaderSearchInfo().getExternalLookup())
3586 PP.getHeaderSearchInfo().SetExternalLookup(this);
3587 }
3588 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003589
Guy Benyei11169dd2012-12-18 14:30:41 +00003590 case FP_PRAGMA_OPTIONS:
3591 // Later tables overwrite earlier ones.
3592 FPPragmaOptions.swap(Record);
3593 break;
3594
3595 case OPENCL_EXTENSIONS:
Yaxun Liu5b746652016-12-18 05:18:55 +00003596 for (unsigned I = 0, E = Record.size(); I != E; ) {
3597 auto Name = ReadString(Record, I);
3598 auto &Opt = OpenCLExtensions.OptMap[Name];
Yaxun Liucc2741c2016-12-18 06:35:06 +00003599 Opt.Supported = Record[I++] != 0;
3600 Opt.Enabled = Record[I++] != 0;
Yaxun Liu5b746652016-12-18 05:18:55 +00003601 Opt.Avail = Record[I++];
3602 Opt.Core = Record[I++];
3603 }
3604 break;
3605
3606 case OPENCL_EXTENSION_TYPES:
3607 for (unsigned I = 0, E = Record.size(); I != E;) {
3608 auto TypeID = static_cast<::TypeID>(Record[I++]);
3609 auto *Type = GetType(TypeID).getTypePtr();
3610 auto NumExt = static_cast<unsigned>(Record[I++]);
3611 for (unsigned II = 0; II != NumExt; ++II) {
3612 auto Ext = ReadString(Record, I);
3613 OpenCLTypeExtMap[Type].insert(Ext);
3614 }
3615 }
3616 break;
3617
3618 case OPENCL_EXTENSION_DECLS:
3619 for (unsigned I = 0, E = Record.size(); I != E;) {
3620 auto DeclID = static_cast<::DeclID>(Record[I++]);
3621 auto *Decl = GetDecl(DeclID);
3622 auto NumExt = static_cast<unsigned>(Record[I++]);
3623 for (unsigned II = 0; II != NumExt; ++II) {
3624 auto Ext = ReadString(Record, I);
3625 OpenCLDeclExtMap[Decl].insert(Ext);
3626 }
3627 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003628 break;
3629
3630 case TENTATIVE_DEFINITIONS:
3631 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3632 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3633 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003634
Guy Benyei11169dd2012-12-18 14:30:41 +00003635 case KNOWN_NAMESPACES:
3636 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3637 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3638 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003639
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003640 case UNDEFINED_BUT_USED:
3641 if (UndefinedButUsed.size() % 2 != 0) {
3642 Error("Invalid existing UndefinedButUsed");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003643 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003644 }
3645
3646 if (Record.size() % 2 != 0) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003647 Error("invalid undefined-but-used record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003648 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003649 }
3650 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003651 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3652 UndefinedButUsed.push_back(
Nick Lewycky8334af82013-01-26 00:35:08 +00003653 ReadSourceLocation(F, Record, I).getRawEncoding());
3654 }
3655 break;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003656
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00003657 case DELETE_EXPRS_TO_ANALYZE:
3658 for (unsigned I = 0, N = Record.size(); I != N;) {
3659 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3660 const uint64_t Count = Record[I++];
3661 DelayedDeleteExprs.push_back(Count);
3662 for (uint64_t C = 0; C < Count; ++C) {
3663 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3664 bool IsArrayForm = Record[I++] == 1;
3665 DelayedDeleteExprs.push_back(IsArrayForm);
3666 }
3667 }
3668 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003669
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003670 case IMPORTED_MODULES:
Manman Ren11f2a472016-08-18 17:42:15 +00003671 if (!F.isModule()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003672 // If we aren't loading a module (which has its own exports), make
3673 // all of the imported modules visible.
3674 // FIXME: Deal with macros-only imports.
Richard Smith56be7542014-03-21 00:33:59 +00003675 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3676 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3677 SourceLocation Loc = ReadSourceLocation(F, Record, I);
Graydon Hoare9c982442017-01-18 20:36:59 +00003678 if (GlobalID) {
Aaron Ballman4f45b712014-03-21 15:22:56 +00003679 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
Graydon Hoare9c982442017-01-18 20:36:59 +00003680 if (DeserializationListener)
3681 DeserializationListener->ModuleImportRead(GlobalID, Loc);
3682 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003683 }
3684 }
3685 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003686
Guy Benyei11169dd2012-12-18 14:30:41 +00003687 case MACRO_OFFSET: {
3688 if (F.LocalNumMacros != 0) {
3689 Error("duplicate MACRO_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003690 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003691 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003692 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003693 F.LocalNumMacros = Record[0];
3694 unsigned LocalBaseMacroID = Record[1];
3695 F.BaseMacroID = getTotalNumMacros();
3696
3697 if (F.LocalNumMacros > 0) {
3698 // Introduce the global -> local mapping for macros within this module.
3699 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3700
3701 // Introduce the local -> global mapping for macros within this module.
3702 F.MacroRemap.insertOrReplace(
3703 std::make_pair(LocalBaseMacroID,
3704 F.BaseMacroID - LocalBaseMacroID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00003705
3706 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
Guy Benyei11169dd2012-12-18 14:30:41 +00003707 }
3708 break;
3709 }
3710
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003711 case LATE_PARSED_TEMPLATE:
Richard Smithe40f2ba2013-08-07 21:41:30 +00003712 LateParsedTemplates.append(Record.begin(), Record.end());
3713 break;
Dario Domizioli13a0a382014-05-23 12:13:25 +00003714
3715 case OPTIMIZE_PRAGMA_OPTIONS:
3716 if (Record.size() != 1) {
3717 Error("invalid pragma optimize record");
3718 return Failure;
3719 }
3720 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3721 break;
Nico Weber72889432014-09-06 01:25:55 +00003722
Nico Weber779355f2016-03-02 23:22:00 +00003723 case MSSTRUCT_PRAGMA_OPTIONS:
3724 if (Record.size() != 1) {
3725 Error("invalid pragma ms_struct record");
3726 return Failure;
3727 }
3728 PragmaMSStructState = Record[0];
3729 break;
3730
Nico Weber42932312016-03-03 00:17:35 +00003731 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3732 if (Record.size() != 2) {
3733 Error("invalid pragma ms_struct record");
3734 return Failure;
3735 }
3736 PragmaMSPointersToMembersState = Record[0];
3737 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3738 break;
3739
Nico Weber72889432014-09-06 01:25:55 +00003740 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3741 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3742 UnusedLocalTypedefNameCandidates.push_back(
3743 getGlobalDeclID(F, Record[I]));
3744 break;
Justin Lebar67a78a62016-10-08 22:15:58 +00003745
3746 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3747 if (Record.size() != 1) {
3748 Error("invalid cuda pragma options record");
3749 return Failure;
3750 }
3751 ForceCUDAHostDeviceDepth = Record[0];
3752 break;
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00003753
3754 case PACK_PRAGMA_OPTIONS: {
3755 if (Record.size() < 3) {
3756 Error("invalid pragma pack record");
3757 return Failure;
3758 }
3759 PragmaPackCurrentValue = Record[0];
3760 PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3761 unsigned NumStackEntries = Record[2];
3762 unsigned Idx = 3;
3763 // Reset the stack when importing a new module.
3764 PragmaPackStack.clear();
3765 for (unsigned I = 0; I < NumStackEntries; ++I) {
3766 PragmaPackStackEntry Entry;
3767 Entry.Value = Record[Idx++];
3768 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
Alex Lorenz45b40142017-07-28 14:41:21 +00003769 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00003770 PragmaPackStrings.push_back(ReadString(Record, Idx));
3771 Entry.SlotLabel = PragmaPackStrings.back();
3772 PragmaPackStack.push_back(Entry);
3773 }
3774 break;
3775 }
Yaxun (Sam) Liub670ab72020-02-26 10:57:39 -05003776
3777 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3778 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3779 DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I]));
3780 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003781 }
3782 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003783}
3784
Richard Smith37a93df2017-02-18 00:32:02 +00003785void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3786 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3787
3788 // Additional remapping information.
Vedant Kumar48b4f762018-04-14 01:40:48 +00003789 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
Richard Smith37a93df2017-02-18 00:32:02 +00003790 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3791 F.ModuleOffsetMap = StringRef();
3792
3793 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3794 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3795 F.SLocRemap.insert(std::make_pair(0U, 0));
3796 F.SLocRemap.insert(std::make_pair(2U, 1));
3797 }
3798
3799 // Continuous range maps we may be updating in our module.
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003800 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
Richard Smith37a93df2017-02-18 00:32:02 +00003801 RemapBuilder SLocRemap(F.SLocRemap);
3802 RemapBuilder IdentifierRemap(F.IdentifierRemap);
3803 RemapBuilder MacroRemap(F.MacroRemap);
3804 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3805 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3806 RemapBuilder SelectorRemap(F.SelectorRemap);
3807 RemapBuilder DeclRemap(F.DeclRemap);
3808 RemapBuilder TypeRemap(F.TypeRemap);
3809
3810 while (Data < DataEnd) {
Boris Kolpackovd30446f2017-08-31 06:26:43 +00003811 // FIXME: Looking up dependency modules by filename is horrible. Let's
3812 // start fixing this with prebuilt and explicit modules and see how it
3813 // goes...
Richard Smith37a93df2017-02-18 00:32:02 +00003814 using namespace llvm::support;
Vedant Kumar48b4f762018-04-14 01:40:48 +00003815 ModuleKind Kind = static_cast<ModuleKind>(
3816 endian::readNext<uint8_t, little, unaligned>(Data));
Richard Smith37a93df2017-02-18 00:32:02 +00003817 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3818 StringRef Name = StringRef((const char*)Data, Len);
3819 Data += Len;
Boris Kolpackovd30446f2017-08-31 06:26:43 +00003820 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
3821 ? ModuleMgr.lookupByModuleName(Name)
3822 : ModuleMgr.lookupByFileName(Name));
Richard Smith37a93df2017-02-18 00:32:02 +00003823 if (!OM) {
3824 std::string Msg =
3825 "SourceLocation remap refers to unknown module, cannot find ";
Benjamin Krameradcd0262020-01-28 20:23:46 +01003826 Msg.append(std::string(Name));
Richard Smith37a93df2017-02-18 00:32:02 +00003827 Error(Msg);
3828 return;
3829 }
3830
3831 uint32_t SLocOffset =
3832 endian::readNext<uint32_t, little, unaligned>(Data);
3833 uint32_t IdentifierIDOffset =
3834 endian::readNext<uint32_t, little, unaligned>(Data);
3835 uint32_t MacroIDOffset =
3836 endian::readNext<uint32_t, little, unaligned>(Data);
3837 uint32_t PreprocessedEntityIDOffset =
3838 endian::readNext<uint32_t, little, unaligned>(Data);
3839 uint32_t SubmoduleIDOffset =
3840 endian::readNext<uint32_t, little, unaligned>(Data);
3841 uint32_t SelectorIDOffset =
3842 endian::readNext<uint32_t, little, unaligned>(Data);
3843 uint32_t DeclIDOffset =
3844 endian::readNext<uint32_t, little, unaligned>(Data);
3845 uint32_t TypeIndexOffset =
3846 endian::readNext<uint32_t, little, unaligned>(Data);
3847
3848 uint32_t None = std::numeric_limits<uint32_t>::max();
3849
3850 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3851 RemapBuilder &Remap) {
3852 if (Offset != None)
3853 Remap.insert(std::make_pair(Offset,
3854 static_cast<int>(BaseOffset - Offset)));
3855 };
3856 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3857 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3858 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3859 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3860 PreprocessedEntityRemap);
3861 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3862 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3863 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3864 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3865
3866 // Global -> local mappings.
3867 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3868 }
3869}
3870
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003871ASTReader::ASTReadResult
3872ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3873 const ModuleFile *ImportedBy,
3874 unsigned ClientLoadCapabilities) {
3875 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00003876 F.ModuleMapPath = ReadPath(F, Record, Idx);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003877
3878 // Try to resolve ModuleName in the current header search context and
3879 // verify that it is found in the same module map file as we saved. If the
3880 // top-level AST file is a main file, skip this check because there is no
3881 // usable header search context.
3882 assert(!F.ModuleName.empty() &&
Richard Smithe842a472014-10-22 02:05:46 +00003883 "MODULE_NAME should come before MODULE_MAP_FILE");
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00003884 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
Richard Smithe842a472014-10-22 02:05:46 +00003885 // An implicitly-loaded module file should have its module listed in some
3886 // module map file that we've already loaded.
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003887 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
Richard Smithe842a472014-10-22 02:05:46 +00003888 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3889 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
Yuka Takahashid8baec22018-08-01 09:50:02 +00003890 // Don't emit module relocation error if we have -fno-validate-pch
3891 if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
Richard Smith0f99d6a2015-08-09 08:48:41 +00003892 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
Bruno Cardoso Lopesa66a3252017-11-17 03:24:11 +00003893 if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
Richard Smith0f99d6a2015-08-09 08:48:41 +00003894 // This module was defined by an imported (explicit) module.
3895 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3896 << ASTFE->getName();
Bruno Cardoso Lopesa66a3252017-11-17 03:24:11 +00003897 } else {
Richard Smith0f99d6a2015-08-09 08:48:41 +00003898 // This module was built with a different module map.
3899 Diag(diag::err_imported_module_not_found)
Bruno Cardoso Lopes4625c182019-08-29 23:14:08 +00003900 << F.ModuleName << F.FileName
3901 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3902 << !ImportedBy;
Bruno Cardoso Lopesa66a3252017-11-17 03:24:11 +00003903 // In case it was imported by a PCH, there's a chance the user is
3904 // just missing to include the search path to the directory containing
3905 // the modulemap.
Bruno Cardoso Lopes4625c182019-08-29 23:14:08 +00003906 if (ImportedBy && ImportedBy->Kind == MK_PCH)
Bruno Cardoso Lopesa66a3252017-11-17 03:24:11 +00003907 Diag(diag::note_imported_by_pch_module_not_found)
3908 << llvm::sys::path::parent_path(F.ModuleMapPath);
3909 }
Richard Smith0f99d6a2015-08-09 08:48:41 +00003910 }
3911 return OutOfDate;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003912 }
3913
Richard Smithe842a472014-10-22 02:05:46 +00003914 assert(M->Name == F.ModuleName && "found module with different name");
3915
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003916 // Check the primary module map file.
Harlan Haskins8d323d12019-08-01 21:31:56 +00003917 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3918 if (!StoredModMap || *StoredModMap != ModMap) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003919 assert(ModMap && "found module is missing module map file");
Bruno Cardoso Lopes4625c182019-08-29 23:14:08 +00003920 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3921 "top-level import should be verified");
3922 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003923 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3924 Diag(diag::err_imported_module_modmap_changed)
Bruno Cardoso Lopes4625c182019-08-29 23:14:08 +00003925 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3926 << ModMap->getName() << F.ModuleMapPath << NotImported;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003927 return OutOfDate;
3928 }
3929
3930 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3931 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3932 // FIXME: we should use input files rather than storing names.
Richard Smith7ed1bc92014-12-05 22:42:13 +00003933 std::string Filename = ReadPath(F, Record, Idx);
Harlan Haskins8d323d12019-08-01 21:31:56 +00003934 auto F = FileMgr.getFile(Filename, false, false);
3935 if (!F) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003936 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3937 Error("could not find file '" + Filename +"' referenced by AST file");
3938 return OutOfDate;
3939 }
Harlan Haskins8d323d12019-08-01 21:31:56 +00003940 AdditionalStoredMaps.insert(*F);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003941 }
3942
3943 // Check any additional module map files (e.g. module.private.modulemap)
3944 // that are not in the pcm.
3945 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00003946 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003947 // Remove files that match
3948 // Note: SmallPtrSet::erase is really remove
3949 if (!AdditionalStoredMaps.erase(ModMap)) {
3950 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3951 Diag(diag::err_module_different_modmap)
3952 << F.ModuleName << /*new*/0 << ModMap->getName();
3953 return OutOfDate;
3954 }
3955 }
3956 }
3957
3958 // Check any additional module map files that are in the pcm, but not
3959 // found in header search. Cases that match are already removed.
Vedant Kumar48b4f762018-04-14 01:40:48 +00003960 for (const FileEntry *ModMap : AdditionalStoredMaps) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003961 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3962 Diag(diag::err_module_different_modmap)
3963 << F.ModuleName << /*not new*/1 << ModMap->getName();
3964 return OutOfDate;
3965 }
3966 }
3967
3968 if (Listener)
3969 Listener->ReadModuleMapFile(F.ModuleMapPath);
3970 return Success;
3971}
3972
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003973/// Move the given method to the back of the global list of methods.
Douglas Gregorc1489562013-02-12 23:36:21 +00003974static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3975 // Find the entry for this selector in the method pool.
3976 Sema::GlobalMethodPool::iterator Known
3977 = S.MethodPool.find(Method->getSelector());
3978 if (Known == S.MethodPool.end())
3979 return;
3980
3981 // Retrieve the appropriate method list.
3982 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3983 : Known->second.second;
3984 bool Found = false;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003985 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003986 if (!Found) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003987 if (List->getMethod() == Method) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003988 Found = true;
3989 } else {
3990 // Keep searching.
3991 continue;
3992 }
3993 }
3994
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003995 if (List->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003996 List->setMethod(List->getNext()->getMethod());
Douglas Gregorc1489562013-02-12 23:36:21 +00003997 else
Nico Weber2e0c8f72014-12-27 03:58:08 +00003998 List->setMethod(Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00003999 }
4000}
4001
Richard Smithde711422015-04-23 21:20:19 +00004002void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Richard Smith10434f32015-05-02 02:08:26 +00004003 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
Vedant Kumar48b4f762018-04-14 01:40:48 +00004004 for (Decl *D : Names) {
Richard Smith90dc5252017-06-23 01:04:34 +00004005 bool wasHidden = D->isHidden();
4006 D->setVisibleDespiteOwningModule();
Guy Benyei11169dd2012-12-18 14:30:41 +00004007
Vedant Kumar48b4f762018-04-14 01:40:48 +00004008 if (wasHidden && SemaObj) {
4009 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
Richard Smith49f906a2014-03-01 00:08:04 +00004010 moveMethodToBackOfGlobalList(*SemaObj, Method);
Vedant Kumar48b4f762018-04-14 01:40:48 +00004011 }
4012 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004013 }
4014}
4015
Richard Smith49f906a2014-03-01 00:08:04 +00004016void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00004017 Module::NameVisibilityKind NameVisibility,
Richard Smitha7e2cc62015-05-01 01:53:09 +00004018 SourceLocation ImportLoc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004019 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004020 SmallVector<Module *, 4> Stack;
Robert Wilhelm25284cc2013-08-23 16:11:15 +00004021 Stack.push_back(Mod);
Guy Benyei11169dd2012-12-18 14:30:41 +00004022 while (!Stack.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00004023 Mod = Stack.pop_back_val();
Guy Benyei11169dd2012-12-18 14:30:41 +00004024
4025 if (NameVisibility <= Mod->NameVisibility) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00004026 // This module already has this level of visibility (or greater), so
Guy Benyei11169dd2012-12-18 14:30:41 +00004027 // there is nothing more to do.
4028 continue;
4029 }
Richard Smith49f906a2014-03-01 00:08:04 +00004030
Guy Benyei11169dd2012-12-18 14:30:41 +00004031 if (!Mod->isAvailable()) {
4032 // Modules that aren't available cannot be made visible.
4033 continue;
4034 }
4035
4036 // Update the module's name visibility.
4037 Mod->NameVisibility = NameVisibility;
Richard Smith49f906a2014-03-01 00:08:04 +00004038
Guy Benyei11169dd2012-12-18 14:30:41 +00004039 // If we've already deserialized any names from this module,
4040 // mark them as visible.
4041 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4042 if (Hidden != HiddenNamesMap.end()) {
Richard Smith57721ac2014-07-21 04:10:40 +00004043 auto HiddenNames = std::move(*Hidden);
Guy Benyei11169dd2012-12-18 14:30:41 +00004044 HiddenNamesMap.erase(Hidden);
Richard Smithde711422015-04-23 21:20:19 +00004045 makeNamesVisible(HiddenNames.second, HiddenNames.first);
Richard Smith57721ac2014-07-21 04:10:40 +00004046 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4047 "making names visible added hidden names");
Guy Benyei11169dd2012-12-18 14:30:41 +00004048 }
Dmitri Gribenkoe9bcf5b2013-11-04 21:51:33 +00004049
Guy Benyei11169dd2012-12-18 14:30:41 +00004050 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00004051 SmallVector<Module *, 16> Exports;
4052 Mod->getExportedModules(Exports);
Vedant Kumar48b4f762018-04-14 01:40:48 +00004053 for (SmallVectorImpl<Module *>::iterator
4054 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4055 Module *Exported = *I;
David Blaikie82e95a32014-11-19 07:49:47 +00004056 if (Visited.insert(Exported).second)
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00004057 Stack.push_back(Exported);
Vedant Kumar48b4f762018-04-14 01:40:48 +00004058 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004059 }
4060}
4061
Richard Smith6561f922016-09-12 21:06:40 +00004062/// We've merged the definition \p MergedDef into the existing definition
4063/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4064/// visible.
4065void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4066 NamedDecl *MergedDef) {
Richard Smith6561f922016-09-12 21:06:40 +00004067 if (Def->isHidden()) {
4068 // If MergedDef is visible or becomes visible, make the definition visible.
Benjamin Kramera72a70a2016-10-17 13:00:44 +00004069 if (!MergedDef->isHidden())
Richard Smith90dc5252017-06-23 01:04:34 +00004070 Def->setVisibleDespiteOwningModule();
Richard Smith13897eb2018-09-12 23:37:00 +00004071 else {
Benjamin Kramera72a70a2016-10-17 13:00:44 +00004072 getContext().mergeDefinitionIntoModule(
4073 Def, MergedDef->getImportedOwningModule(),
4074 /*NotifyListeners*/ false);
4075 PendingMergedDefinitionsToDeduplicate.insert(Def);
Benjamin Kramera72a70a2016-10-17 13:00:44 +00004076 }
Richard Smith6561f922016-09-12 21:06:40 +00004077 }
4078}
4079
Douglas Gregore060e572013-01-25 01:03:03 +00004080bool ASTReader::loadGlobalIndex() {
4081 if (GlobalIndex)
4082 return false;
4083
4084 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
Richard Smithdbafb6c2017-06-29 23:23:46 +00004085 !PP.getLangOpts().Modules)
Douglas Gregore060e572013-01-25 01:03:03 +00004086 return true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004087
Douglas Gregore060e572013-01-25 01:03:03 +00004088 // Try to load the global index.
4089 TriedLoadingGlobalIndex = true;
4090 StringRef ModuleCachePath
4091 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
JF Bastien0e828952019-06-26 19:50:12 +00004092 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4093 GlobalModuleIndex::readIndex(ModuleCachePath);
4094 if (llvm::Error Err = std::move(Result.second)) {
4095 assert(!Result.first);
4096 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
Douglas Gregore060e572013-01-25 01:03:03 +00004097 return true;
JF Bastien0e828952019-06-26 19:50:12 +00004098 }
Douglas Gregore060e572013-01-25 01:03:03 +00004099
4100 GlobalIndex.reset(Result.first);
Douglas Gregor7211ac12013-01-25 23:32:03 +00004101 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregore060e572013-01-25 01:03:03 +00004102 return false;
4103}
4104
4105bool ASTReader::isGlobalIndexUnavailable() const {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004106 return PP.getLangOpts().Modules && UseGlobalIndex &&
Douglas Gregore060e572013-01-25 01:03:03 +00004107 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4108}
4109
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004110static void updateModuleTimestamp(ModuleFile &MF) {
4111 // Overwrite the timestamp file contents so that file's mtime changes.
4112 std::string TimestampFilename = MF.getTimestampFilename();
Rafael Espindoladae941a2014-08-25 18:17:04 +00004113 std::error_code EC;
Fangrui Songd9b948b2019-08-05 05:43:48 +00004114 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
Rafael Espindoladae941a2014-08-25 18:17:04 +00004115 if (EC)
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004116 return;
4117 OS << "Timestamp file\n";
Alex Lorenz0bafa022017-06-02 10:36:56 +00004118 OS.close();
4119 OS.clear_error(); // Avoid triggering a fatal error.
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004120}
4121
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004122/// Given a cursor at the start of an AST file, scan ahead and drop the
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004123/// cursor into the start of the given block ID, returning false on success and
4124/// true on failure.
4125static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00004126 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00004127 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4128 if (!MaybeEntry) {
4129 // FIXME this drops errors on the floor.
4130 consumeError(MaybeEntry.takeError());
4131 return true;
4132 }
4133 llvm::BitstreamEntry Entry = MaybeEntry.get();
4134
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004135 switch (Entry.Kind) {
4136 case llvm::BitstreamEntry::Error:
4137 case llvm::BitstreamEntry::EndBlock:
4138 return true;
4139
4140 case llvm::BitstreamEntry::Record:
4141 // Ignore top-level records.
JF Bastien0e828952019-06-26 19:50:12 +00004142 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4143 break;
4144 else {
4145 // FIXME this drops errors on the floor.
4146 consumeError(Skipped.takeError());
4147 return true;
4148 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004149
4150 case llvm::BitstreamEntry::SubBlock:
4151 if (Entry.ID == BlockID) {
JF Bastien0e828952019-06-26 19:50:12 +00004152 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4153 // FIXME this drops the error on the floor.
4154 consumeError(std::move(Err));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004155 return true;
JF Bastien0e828952019-06-26 19:50:12 +00004156 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004157 // Found it!
4158 return false;
4159 }
4160
JF Bastien0e828952019-06-26 19:50:12 +00004161 if (llvm::Error Err = Cursor.SkipBlock()) {
4162 // FIXME this drops the error on the floor.
4163 consumeError(std::move(Err));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004164 return true;
JF Bastien0e828952019-06-26 19:50:12 +00004165 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004166 }
4167 }
4168}
4169
Benjamin Kramer0772c422016-02-13 13:42:54 +00004170ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
Guy Benyei11169dd2012-12-18 14:30:41 +00004171 ModuleKind Type,
4172 SourceLocation ImportLoc,
Graydon Hoaree7196af2016-12-09 21:45:49 +00004173 unsigned ClientLoadCapabilities,
4174 SmallVectorImpl<ImportedSubmodule> *Imported) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00004175 llvm::SaveAndRestore<SourceLocation>
4176 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4177
Richard Smithd1c46742014-04-30 02:24:17 +00004178 // Defer any pending actions until we get to the end of reading the AST file.
4179 Deserializing AnASTFile(this);
4180
Guy Benyei11169dd2012-12-18 14:30:41 +00004181 // Bump the generation number.
Richard Smithdbafb6c2017-06-29 23:23:46 +00004182 unsigned PreviousGeneration = 0;
4183 if (ContextObj)
4184 PreviousGeneration = incrementGeneration(*ContextObj);
Guy Benyei11169dd2012-12-18 14:30:41 +00004185
4186 unsigned NumModules = ModuleMgr.size();
Duncan P. N. Exon Smithc46b3a22019-11-10 10:42:29 -08004187 auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4188 assert(ReadResult && "expected to return error");
Duncan P. N. Exon Smith8e9e4332019-11-10 10:31:03 -08004189 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
Richard Smithdbafb6c2017-06-29 23:23:46 +00004190 PP.getLangOpts().Modules
Duncan P. N. Exon Smith8e6bc1972017-01-28 23:02:12 +00004191 ? &PP.getHeaderSearchInfo().getModuleMap()
4192 : nullptr);
Douglas Gregore060e572013-01-25 01:03:03 +00004193
4194 // If we find that any modules are unusable, the global index is going
4195 // to be out-of-date. Just remove it.
4196 GlobalIndex.reset();
Craig Toppera13603a2014-05-22 05:54:18 +00004197 ModuleMgr.setGlobalIndex(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00004198 return ReadResult;
Duncan P. N. Exon Smithc46b3a22019-11-10 10:42:29 -08004199 };
4200
4201 SmallVector<ImportedModule, 4> Loaded;
4202 switch (ASTReadResult ReadResult =
4203 ReadASTCore(FileName, Type, ImportLoc,
4204 /*ImportedBy=*/nullptr, Loaded, 0, 0,
4205 ASTFileSignature(), ClientLoadCapabilities)) {
4206 case Failure:
4207 case Missing:
4208 case OutOfDate:
4209 case VersionMismatch:
4210 case ConfigurationMismatch:
4211 case HadErrors:
4212 return removeModulesAndReturn(ReadResult);
Guy Benyei11169dd2012-12-18 14:30:41 +00004213 case Success:
4214 break;
4215 }
4216
4217 // Here comes stuff that we only do once the entire chain is loaded.
4218
Duncan P. N. Exon Smith01782c32019-11-10 10:50:12 -08004219 // Load the AST blocks of all of the modules that we loaded. We can still
4220 // hit errors parsing the ASTs at this point.
Duncan P. N. Exon Smithbfd58fc2019-11-11 15:42:25 -08004221 for (ImportedModule &M : Loaded) {
4222 ModuleFile &F = *M.Mod;
Guy Benyei11169dd2012-12-18 14:30:41 +00004223
4224 // Read the AST block.
Ben Langmuir2c9af442014-04-10 17:57:43 +00004225 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
Duncan P. N. Exon Smithc46b3a22019-11-10 10:42:29 -08004226 return removeModulesAndReturn(Result);
Guy Benyei11169dd2012-12-18 14:30:41 +00004227
Duncan P. N. Exon Smith83dcb342019-11-10 13:14:52 -08004228 // The AST block should always have a definition for the main module.
4229 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4230 Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4231 return removeModulesAndReturn(Failure);
4232 }
4233
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004234 // Read the extension blocks.
4235 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4236 if (ASTReadResult Result = ReadExtensionBlock(F))
Duncan P. N. Exon Smithc46b3a22019-11-10 10:42:29 -08004237 return removeModulesAndReturn(Result);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004238 }
4239
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004240 // Once read, set the ModuleFile bit base offset and update the size in
Guy Benyei11169dd2012-12-18 14:30:41 +00004241 // bits of all files we've seen.
4242 F.GlobalBitOffset = TotalModulesSizeInBits;
4243 TotalModulesSizeInBits += F.SizeInBits;
4244 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Duncan P. N. Exon Smith01782c32019-11-10 10:50:12 -08004245 }
4246
4247 // Preload source locations and interesting indentifiers.
4248 for (ImportedModule &M : Loaded) {
4249 ModuleFile &F = *M.Mod;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004250
Guy Benyei11169dd2012-12-18 14:30:41 +00004251 // Preload SLocEntries.
Vedant Kumar48b4f762018-04-14 01:40:48 +00004252 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4253 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Guy Benyei11169dd2012-12-18 14:30:41 +00004254 // Load it through the SourceManager and don't call ReadSLocEntry()
4255 // directly because the entry may have already been loaded in which case
4256 // calling ReadSLocEntry() directly would trigger an assertion in
4257 // SourceManager.
4258 SourceMgr.getLoadedSLocEntryByID(Index);
4259 }
Richard Smith33e0f7e2015-07-22 02:08:40 +00004260
Richard Smithea741482017-05-01 22:10:47 +00004261 // Map the original source file ID into the ID space of the current
4262 // compilation.
4263 if (F.OriginalSourceFileID.isValid()) {
4264 F.OriginalSourceFileID = FileID::get(
4265 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4266 }
4267
Richard Smith33e0f7e2015-07-22 02:08:40 +00004268 // Preload all the pending interesting identifiers by marking them out of
4269 // date.
4270 for (auto Offset : F.PreloadIdentifierOffsets) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004271 const unsigned char *Data = reinterpret_cast<const unsigned char *>(
Richard Smith33e0f7e2015-07-22 02:08:40 +00004272 F.IdentifierTableData + Offset);
4273
4274 ASTIdentifierLookupTrait Trait(*this, F);
4275 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4276 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
Richard Smith79bf9202015-08-24 03:33:22 +00004277 auto &II = PP.getIdentifierTable().getOwn(Key);
4278 II.setOutOfDate(true);
4279
4280 // Mark this identifier as being from an AST file so that we can track
4281 // whether we need to serialize it.
Richard Smitheb4b58f62016-02-05 01:40:54 +00004282 markIdentifierFromAST(*this, II);
Richard Smith79bf9202015-08-24 03:33:22 +00004283
4284 // Associate the ID with the identifier so that the writer can reuse it.
4285 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4286 SetIdentifierInfo(ID, &II);
Richard Smith33e0f7e2015-07-22 02:08:40 +00004287 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004288 }
4289
Douglas Gregor603cd862013-03-22 18:50:14 +00004290 // Setup the import locations and notify the module manager that we've
4291 // committed to these module files.
Duncan P. N. Exon Smithbfd58fc2019-11-11 15:42:25 -08004292 for (ImportedModule &M : Loaded) {
4293 ModuleFile &F = *M.Mod;
Douglas Gregor603cd862013-03-22 18:50:14 +00004294
4295 ModuleMgr.moduleFileAccepted(&F);
4296
4297 // Set the import location.
Argyrios Kyrtzidis71c1af82013-02-01 16:36:14 +00004298 F.DirectImportLoc = ImportLoc;
Richard Smithb22a1d12016-03-27 20:13:24 +00004299 // FIXME: We assume that locations from PCH / preamble do not need
4300 // any translation.
Duncan P. N. Exon Smithbfd58fc2019-11-11 15:42:25 -08004301 if (!M.ImportedBy)
4302 F.ImportLoc = M.ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00004303 else
Duncan P. N. Exon Smithbfd58fc2019-11-11 15:42:25 -08004304 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00004305 }
4306
Richard Smithdbafb6c2017-06-29 23:23:46 +00004307 if (!PP.getLangOpts().CPlusPlus ||
Manman Ren11f2a472016-08-18 17:42:15 +00004308 (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4309 Type != MK_PrebuiltModule)) {
Richard Smith33e0f7e2015-07-22 02:08:40 +00004310 // Mark all of the identifiers in the identifier table as being out of date,
4311 // so that various accessors know to check the loaded modules when the
4312 // identifier is used.
4313 //
4314 // For C++ modules, we don't need information on many identifiers (just
4315 // those that provide macros or are poisoned), so we mark all of
4316 // the interesting ones via PreloadIdentifierOffsets.
Vedant Kumar48b4f762018-04-14 01:40:48 +00004317 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4318 IdEnd = PP.getIdentifierTable().end();
4319 Id != IdEnd; ++Id)
4320 Id->second->setOutOfDate(true);
Richard Smith33e0f7e2015-07-22 02:08:40 +00004321 }
Manman Rena0f31a02016-04-29 19:04:05 +00004322 // Mark selectors as out of date.
4323 for (auto Sel : SelectorGeneration)
4324 SelectorOutOfDate[Sel.first] = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004325
Guy Benyei11169dd2012-12-18 14:30:41 +00004326 // Resolve any unresolved module exports.
Vedant Kumar48b4f762018-04-14 01:40:48 +00004327 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4328 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei11169dd2012-12-18 14:30:41 +00004329 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4330 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregorfb912652013-03-20 21:10:35 +00004331
4332 switch (Unresolved.Kind) {
4333 case UnresolvedModuleRef::Conflict:
4334 if (ResolvedMod) {
4335 Module::Conflict Conflict;
4336 Conflict.Other = ResolvedMod;
4337 Conflict.Message = Unresolved.String.str();
4338 Unresolved.Mod->Conflicts.push_back(Conflict);
4339 }
4340 continue;
4341
4342 case UnresolvedModuleRef::Import:
Guy Benyei11169dd2012-12-18 14:30:41 +00004343 if (ResolvedMod)
Richard Smith38477db2015-05-02 00:45:56 +00004344 Unresolved.Mod->Imports.insert(ResolvedMod);
Guy Benyei11169dd2012-12-18 14:30:41 +00004345 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00004346
Douglas Gregorfb912652013-03-20 21:10:35 +00004347 case UnresolvedModuleRef::Export:
4348 if (ResolvedMod || Unresolved.IsWildcard)
4349 Unresolved.Mod->Exports.push_back(
4350 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4351 continue;
4352 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004353 }
Douglas Gregorfb912652013-03-20 21:10:35 +00004354 UnresolvedModuleRefs.clear();
Daniel Jasperba7f2f72013-09-24 09:14:14 +00004355
Graydon Hoaree7196af2016-12-09 21:45:49 +00004356 if (Imported)
4357 Imported->append(ImportedModules.begin(),
4358 ImportedModules.end());
4359
Daniel Jasperba7f2f72013-09-24 09:14:14 +00004360 // FIXME: How do we load the 'use'd modules? They may not be submodules.
4361 // Might be unnecessary as use declarations are only used to build the
4362 // module itself.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004363
Richard Smithdbafb6c2017-06-29 23:23:46 +00004364 if (ContextObj)
4365 InitializeContext();
Guy Benyei11169dd2012-12-18 14:30:41 +00004366
Richard Smith3d8e97e2013-10-18 06:54:39 +00004367 if (SemaObj)
4368 UpdateSema();
4369
Guy Benyei11169dd2012-12-18 14:30:41 +00004370 if (DeserializationListener)
4371 DeserializationListener->ReaderInitialized(this);
4372
4373 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
Yaron Keren8b563662015-10-03 10:46:20 +00004374 if (PrimaryModule.OriginalSourceFileID.isValid()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004375 // If this AST file is a precompiled preamble, then set the
4376 // preamble file ID of the source manager to the file source file
4377 // from which the preamble was built.
4378 if (Type == MK_Preamble) {
4379 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4380 } else if (Type == MK_MainFile) {
4381 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4382 }
4383 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004384
Guy Benyei11169dd2012-12-18 14:30:41 +00004385 // For any Objective-C class definitions we have already loaded, make sure
4386 // that we load any additional categories.
Vedant Kumar48b4f762018-04-14 01:40:48 +00004387 if (ContextObj) {
4388 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4389 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4390 ObjCClassesLoaded[I],
Richard Smithdbafb6c2017-06-29 23:23:46 +00004391 PreviousGeneration);
Vedant Kumar48b4f762018-04-14 01:40:48 +00004392 }
4393 }
Douglas Gregore060e572013-01-25 01:03:03 +00004394
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004395 if (PP.getHeaderSearchInfo()
4396 .getHeaderSearchOpts()
4397 .ModulesValidateOncePerBuildSession) {
4398 // Now we are certain that the module and all modules it depends on are
4399 // up to date. Create or update timestamp files for modules that are
4400 // located in the module cache (not for PCH files that could be anywhere
4401 // in the filesystem).
Vedant Kumar48b4f762018-04-14 01:40:48 +00004402 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4403 ImportedModule &M = Loaded[I];
4404 if (M.Mod->Kind == MK_ImplicitModule) {
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004405 updateModuleTimestamp(*M.Mod);
Vedant Kumar48b4f762018-04-14 01:40:48 +00004406 }
4407 }
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004408 }
4409
Guy Benyei11169dd2012-12-18 14:30:41 +00004410 return Success;
4411}
4412
Peter Collingbourne77c89b62016-11-08 04:17:11 +00004413static ASTFileSignature readASTFileSignature(StringRef PCH);
Ben Langmuir487ea142014-10-23 18:05:36 +00004414
JF Bastien0e828952019-06-26 19:50:12 +00004415/// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4416static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4417 // FIXME checking magic headers is done in other places such as
4418 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4419 // always done the same. Unify it all with a helper.
4420 if (!Stream.canSkipToPos(4))
4421 return llvm::createStringError(std::errc::illegal_byte_sequence,
4422 "file too small to contain AST file magic");
4423 for (unsigned C : {'C', 'P', 'C', 'H'})
4424 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4425 if (Res.get() != C)
4426 return llvm::createStringError(
4427 std::errc::illegal_byte_sequence,
4428 "file doesn't start with AST file magic");
4429 } else
4430 return Res.takeError();
4431 return llvm::Error::success();
Ben Langmuir70a1b812015-03-24 04:43:52 +00004432}
4433
Richard Smith0f99d6a2015-08-09 08:48:41 +00004434static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4435 switch (Kind) {
4436 case MK_PCH:
4437 return 0; // PCH
4438 case MK_ImplicitModule:
4439 case MK_ExplicitModule:
Manman Ren11f2a472016-08-18 17:42:15 +00004440 case MK_PrebuiltModule:
Richard Smith0f99d6a2015-08-09 08:48:41 +00004441 return 1; // module
4442 case MK_MainFile:
4443 case MK_Preamble:
4444 return 2; // main source file
4445 }
4446 llvm_unreachable("unknown module kind");
4447}
4448
Guy Benyei11169dd2012-12-18 14:30:41 +00004449ASTReader::ASTReadResult
4450ASTReader::ReadASTCore(StringRef FileName,
4451 ModuleKind Type,
4452 SourceLocation ImportLoc,
4453 ModuleFile *ImportedBy,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004454 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00004455 off_t ExpectedSize, time_t ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00004456 ASTFileSignature ExpectedSignature,
Guy Benyei11169dd2012-12-18 14:30:41 +00004457 unsigned ClientLoadCapabilities) {
4458 ModuleFile *M;
Guy Benyei11169dd2012-12-18 14:30:41 +00004459 std::string ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00004460 ModuleManager::AddModuleResult AddResult
4461 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
Richard Smith053f6c62014-05-16 23:01:30 +00004462 getGeneration(), ExpectedSize, ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00004463 ExpectedSignature, readASTFileSignature,
Douglas Gregor7029ce12013-03-19 00:28:20 +00004464 M, ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00004465
Douglas Gregor7029ce12013-03-19 00:28:20 +00004466 switch (AddResult) {
4467 case ModuleManager::AlreadyLoaded:
Duncan P. N. Exon Smith9dda8f52019-03-06 02:50:46 +00004468 Diag(diag::remark_module_import)
4469 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4470 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
Douglas Gregor7029ce12013-03-19 00:28:20 +00004471 return Success;
4472
4473 case ModuleManager::NewlyLoaded:
4474 // Load module file below.
4475 break;
4476
4477 case ModuleManager::Missing:
Richard Smithe842a472014-10-22 02:05:46 +00004478 // The module file was missing; if the client can handle that, return
Douglas Gregor7029ce12013-03-19 00:28:20 +00004479 // it.
4480 if (ClientLoadCapabilities & ARR_Missing)
4481 return Missing;
4482
4483 // Otherwise, return an error.
Richard Smith0f99d6a2015-08-09 08:48:41 +00004484 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
Adrian Prantlb3b5a732016-08-29 20:46:59 +00004485 << FileName << !ErrorStr.empty()
Richard Smith0f99d6a2015-08-09 08:48:41 +00004486 << ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00004487 return Failure;
4488
4489 case ModuleManager::OutOfDate:
4490 // We couldn't load the module file because it is out-of-date. If the
4491 // client can handle out-of-date, return it.
4492 if (ClientLoadCapabilities & ARR_OutOfDate)
4493 return OutOfDate;
4494
4495 // Otherwise, return an error.
Richard Smith0f99d6a2015-08-09 08:48:41 +00004496 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
Adrian Prantl9a06a882016-08-29 20:46:56 +00004497 << FileName << !ErrorStr.empty()
Richard Smith0f99d6a2015-08-09 08:48:41 +00004498 << ErrorStr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004499 return Failure;
4500 }
4501
Douglas Gregor7029ce12013-03-19 00:28:20 +00004502 assert(M && "Missing module file");
Guy Benyei11169dd2012-12-18 14:30:41 +00004503
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00004504 bool ShouldFinalizePCM = false;
4505 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4506 auto &MC = getModuleManager().getModuleCache();
4507 if (ShouldFinalizePCM)
4508 MC.finalizePCM(FileName);
4509 else
Rumeet Dhindsa57a2eaf2020-03-10 10:59:26 -07004510 MC.tryToDropPCM(FileName);
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00004511 });
Guy Benyei11169dd2012-12-18 14:30:41 +00004512 ModuleFile &F = *M;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004513 BitstreamCursor &Stream = F.Stream;
Peter Collingbourne77c89b62016-11-08 04:17:11 +00004514 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
Adrian Prantlcbc368c2015-02-25 02:44:04 +00004515 F.SizeInBits = F.Buffer->getBufferSize() * 8;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004516
Guy Benyei11169dd2012-12-18 14:30:41 +00004517 // Sniff for the signature.
JF Bastien0e828952019-06-26 19:50:12 +00004518 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4519 Diag(diag::err_module_file_invalid)
4520 << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
Guy Benyei11169dd2012-12-18 14:30:41 +00004521 return Failure;
4522 }
4523
4524 // This is used for compatibility with older PCH formats.
4525 bool HaveReadControlBlock = false;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00004526 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00004527 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4528 if (!MaybeEntry) {
4529 Error(MaybeEntry.takeError());
4530 return Failure;
4531 }
4532 llvm::BitstreamEntry Entry = MaybeEntry.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004533
Chris Lattnerefa77172013-01-20 00:00:22 +00004534 switch (Entry.Kind) {
4535 case llvm::BitstreamEntry::Error:
Chris Lattnerefa77172013-01-20 00:00:22 +00004536 case llvm::BitstreamEntry::Record:
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004537 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00004538 Error("invalid record at top-level of AST file");
4539 return Failure;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004540
Chris Lattnerefa77172013-01-20 00:00:22 +00004541 case llvm::BitstreamEntry::SubBlock:
4542 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004543 }
4544
Chris Lattnerefa77172013-01-20 00:00:22 +00004545 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004546 case CONTROL_BLOCK_ID:
4547 HaveReadControlBlock = true;
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004548 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004549 case Success:
Richard Smith0f99d6a2015-08-09 08:48:41 +00004550 // Check that we didn't try to load a non-module AST file as a module.
4551 //
4552 // FIXME: Should we also perform the converse check? Loading a module as
4553 // a PCH file sort of works, but it's a bit wonky.
Manman Ren11f2a472016-08-18 17:42:15 +00004554 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4555 Type == MK_PrebuiltModule) &&
Richard Smith0f99d6a2015-08-09 08:48:41 +00004556 F.ModuleName.empty()) {
4557 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4558 if (Result != OutOfDate ||
4559 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4560 Diag(diag::err_module_file_not_module) << FileName;
4561 return Result;
4562 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004563 break;
4564
4565 case Failure: return Failure;
Douglas Gregor7029ce12013-03-19 00:28:20 +00004566 case Missing: return Missing;
Guy Benyei11169dd2012-12-18 14:30:41 +00004567 case OutOfDate: return OutOfDate;
4568 case VersionMismatch: return VersionMismatch;
4569 case ConfigurationMismatch: return ConfigurationMismatch;
4570 case HadErrors: return HadErrors;
4571 }
4572 break;
Richard Smithf8c32552015-09-02 17:45:54 +00004573
Guy Benyei11169dd2012-12-18 14:30:41 +00004574 case AST_BLOCK_ID:
4575 if (!HaveReadControlBlock) {
4576 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00004577 Diag(diag::err_pch_version_too_old);
Guy Benyei11169dd2012-12-18 14:30:41 +00004578 return VersionMismatch;
4579 }
4580
4581 // Record that we've loaded this module.
4582 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00004583 ShouldFinalizePCM = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00004584 return Success;
4585
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004586 case UNHASHED_CONTROL_BLOCK_ID:
4587 // This block is handled using look-ahead during ReadControlBlock. We
4588 // shouldn't get here!
4589 Error("malformed block record in AST file");
4590 return Failure;
4591
Guy Benyei11169dd2012-12-18 14:30:41 +00004592 default:
JF Bastien0e828952019-06-26 19:50:12 +00004593 if (llvm::Error Err = Stream.SkipBlock()) {
4594 Error(std::move(Err));
Guy Benyei11169dd2012-12-18 14:30:41 +00004595 return Failure;
4596 }
4597 break;
4598 }
4599 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004600
Duncan P. N. Exon Smithfae03d82019-03-03 20:17:53 +00004601 llvm_unreachable("unexpected break; expected return");
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004602}
4603
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004604ASTReader::ASTReadResult
4605ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4606 unsigned ClientLoadCapabilities) {
4607 const HeaderSearchOptions &HSOpts =
4608 PP.getHeaderSearchInfo().getHeaderSearchOpts();
4609 bool AllowCompatibleConfigurationMismatch =
4610 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4611
4612 ASTReadResult Result = readUnhashedControlBlockImpl(
4613 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4614 Listener.get(),
4615 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4616
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004617 // If F was directly imported by another module, it's implicitly validated by
4618 // the importing module.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004619 if (DisableValidation || WasImportedBy ||
4620 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4621 return Success;
4622
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004623 if (Result == Failure) {
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004624 Error("malformed block record in AST file");
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004625 return Failure;
4626 }
4627
4628 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +00004629 // If this module has already been finalized in the ModuleCache, we're stuck
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004630 // with it; we can only load a single version of each module.
4631 //
4632 // This can happen when a module is imported in two contexts: in one, as a
4633 // user module; in another, as a system module (due to an import from
4634 // another module marked with the [system] flag). It usually indicates a
4635 // bug in the module map: this module should also be marked with [system].
4636 //
4637 // If -Wno-system-headers (the default), and the first import is as a
4638 // system module, then validation will fail during the as-user import,
4639 // since -Werror flags won't have been validated. However, it's reasonable
4640 // to treat this consistently as a system module.
4641 //
4642 // If -Wsystem-headers, the PCM on disk was built with
4643 // -Wno-system-headers, and the first import is as a user module, then
4644 // validation will fail during the as-system import since the PCM on disk
4645 // doesn't guarantee that -Werror was respected. However, the -Werror
4646 // flags were checked during the initial as-user import.
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00004647 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004648 Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4649 return Success;
4650 }
4651 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004652
4653 return Result;
4654}
4655
4656ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4657 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4658 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4659 bool ValidateDiagnosticOptions) {
4660 // Initialize a stream.
4661 BitstreamCursor Stream(StreamData);
4662
4663 // Sniff for the signature.
JF Bastien0e828952019-06-26 19:50:12 +00004664 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4665 // FIXME this drops the error on the floor.
4666 consumeError(std::move(Err));
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004667 return Failure;
JF Bastien0e828952019-06-26 19:50:12 +00004668 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004669
4670 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4671 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4672 return Failure;
4673
4674 // Read all of the records in the options block.
4675 RecordData Record;
4676 ASTReadResult Result = Success;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00004677 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00004678 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4679 if (!MaybeEntry) {
4680 // FIXME this drops the error on the floor.
4681 consumeError(MaybeEntry.takeError());
4682 return Failure;
4683 }
4684 llvm::BitstreamEntry Entry = MaybeEntry.get();
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004685
4686 switch (Entry.Kind) {
4687 case llvm::BitstreamEntry::Error:
4688 case llvm::BitstreamEntry::SubBlock:
4689 return Failure;
4690
4691 case llvm::BitstreamEntry::EndBlock:
4692 return Result;
4693
4694 case llvm::BitstreamEntry::Record:
4695 // The interesting case.
4696 break;
4697 }
4698
4699 // Read and process a record.
4700 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00004701 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4702 if (!MaybeRecordType) {
4703 // FIXME this drops the error.
4704 return Failure;
4705 }
4706 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00004707 case SIGNATURE:
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004708 if (F)
4709 std::copy(Record.begin(), Record.end(), F->Signature.data());
4710 break;
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004711 case DIAGNOSTIC_OPTIONS: {
4712 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4713 if (Listener && ValidateDiagnosticOptions &&
4714 !AllowCompatibleConfigurationMismatch &&
4715 ParseDiagnosticOptions(Record, Complain, *Listener))
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004716 Result = OutOfDate; // Don't return early. Read the signature.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004717 break;
4718 }
4719 case DIAG_PRAGMA_MAPPINGS:
4720 if (!F)
4721 break;
4722 if (F->PragmaDiagMappings.empty())
4723 F->PragmaDiagMappings.swap(Record);
4724 else
4725 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4726 Record.begin(), Record.end());
4727 break;
4728 }
4729 }
4730}
4731
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004732/// Parse a record and blob containing module file extension metadata.
4733static bool parseModuleFileExtensionMetadata(
4734 const SmallVectorImpl<uint64_t> &Record,
4735 StringRef Blob,
4736 ModuleFileExtensionMetadata &Metadata) {
4737 if (Record.size() < 4) return true;
4738
4739 Metadata.MajorVersion = Record[0];
4740 Metadata.MinorVersion = Record[1];
4741
4742 unsigned BlockNameLen = Record[2];
4743 unsigned UserInfoLen = Record[3];
4744
4745 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4746
4747 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4748 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4749 Blob.data() + BlockNameLen + UserInfoLen);
4750 return false;
4751}
4752
4753ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4754 BitstreamCursor &Stream = F.Stream;
4755
4756 RecordData Record;
4757 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00004758 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4759 if (!MaybeEntry) {
4760 Error(MaybeEntry.takeError());
4761 return Failure;
4762 }
4763 llvm::BitstreamEntry Entry = MaybeEntry.get();
4764
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004765 switch (Entry.Kind) {
4766 case llvm::BitstreamEntry::SubBlock:
JF Bastien0e828952019-06-26 19:50:12 +00004767 if (llvm::Error Err = Stream.SkipBlock()) {
4768 Error(std::move(Err));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004769 return Failure;
JF Bastien0e828952019-06-26 19:50:12 +00004770 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004771 continue;
4772
4773 case llvm::BitstreamEntry::EndBlock:
4774 return Success;
4775
4776 case llvm::BitstreamEntry::Error:
4777 return HadErrors;
4778
4779 case llvm::BitstreamEntry::Record:
4780 break;
4781 }
4782
4783 Record.clear();
4784 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00004785 Expected<unsigned> MaybeRecCode =
4786 Stream.readRecord(Entry.ID, Record, &Blob);
4787 if (!MaybeRecCode) {
4788 Error(MaybeRecCode.takeError());
4789 return Failure;
4790 }
4791 switch (MaybeRecCode.get()) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004792 case EXTENSION_METADATA: {
4793 ModuleFileExtensionMetadata Metadata;
Duncan P. N. Exon Smith8e2c1922019-11-10 11:07:20 -08004794 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4795 Error("malformed EXTENSION_METADATA in AST file");
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004796 return Failure;
Duncan P. N. Exon Smith8e2c1922019-11-10 11:07:20 -08004797 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004798
4799 // Find a module file extension with this block name.
4800 auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4801 if (Known == ModuleFileExtensions.end()) break;
4802
4803 // Form a reader.
4804 if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4805 F, Stream)) {
4806 F.ExtensionReaders.push_back(std::move(Reader));
4807 }
4808
4809 break;
4810 }
4811 }
4812 }
4813
4814 return Success;
Guy Benyei11169dd2012-12-18 14:30:41 +00004815}
4816
Richard Smitha7e2cc62015-05-01 01:53:09 +00004817void ASTReader::InitializeContext() {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004818 assert(ContextObj && "no context to initialize");
4819 ASTContext &Context = *ContextObj;
4820
Guy Benyei11169dd2012-12-18 14:30:41 +00004821 // If there's a listener, notify them that we "read" the translation unit.
4822 if (DeserializationListener)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004823 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
Guy Benyei11169dd2012-12-18 14:30:41 +00004824 Context.getTranslationUnitDecl());
4825
Guy Benyei11169dd2012-12-18 14:30:41 +00004826 // FIXME: Find a better way to deal with collisions between these
4827 // built-in types. Right now, we just ignore the problem.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004828
Guy Benyei11169dd2012-12-18 14:30:41 +00004829 // Load the special types.
4830 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4831 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4832 if (!Context.CFConstantStringTypeDecl)
4833 Context.setCFConstantStringType(GetType(String));
4834 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004835
Guy Benyei11169dd2012-12-18 14:30:41 +00004836 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4837 QualType FileType = GetType(File);
4838 if (FileType.isNull()) {
4839 Error("FILE type is NULL");
4840 return;
4841 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004842
Guy Benyei11169dd2012-12-18 14:30:41 +00004843 if (!Context.FILEDecl) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004844 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Guy Benyei11169dd2012-12-18 14:30:41 +00004845 Context.setFILEDecl(Typedef->getDecl());
4846 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004847 const TagType *Tag = FileType->getAs<TagType>();
Guy Benyei11169dd2012-12-18 14:30:41 +00004848 if (!Tag) {
4849 Error("Invalid FILE type in AST file");
4850 return;
4851 }
4852 Context.setFILEDecl(Tag->getDecl());
4853 }
4854 }
4855 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004856
Guy Benyei11169dd2012-12-18 14:30:41 +00004857 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4858 QualType Jmp_bufType = GetType(Jmp_buf);
4859 if (Jmp_bufType.isNull()) {
4860 Error("jmp_buf type is NULL");
4861 return;
4862 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004863
Guy Benyei11169dd2012-12-18 14:30:41 +00004864 if (!Context.jmp_bufDecl) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004865 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Guy Benyei11169dd2012-12-18 14:30:41 +00004866 Context.setjmp_bufDecl(Typedef->getDecl());
4867 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004868 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Guy Benyei11169dd2012-12-18 14:30:41 +00004869 if (!Tag) {
4870 Error("Invalid jmp_buf type in AST file");
4871 return;
4872 }
4873 Context.setjmp_bufDecl(Tag->getDecl());
4874 }
4875 }
4876 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004877
Guy Benyei11169dd2012-12-18 14:30:41 +00004878 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4879 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4880 if (Sigjmp_bufType.isNull()) {
4881 Error("sigjmp_buf type is NULL");
4882 return;
4883 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004884
Guy Benyei11169dd2012-12-18 14:30:41 +00004885 if (!Context.sigjmp_bufDecl) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004886 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Guy Benyei11169dd2012-12-18 14:30:41 +00004887 Context.setsigjmp_bufDecl(Typedef->getDecl());
4888 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004889 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Guy Benyei11169dd2012-12-18 14:30:41 +00004890 assert(Tag && "Invalid sigjmp_buf type in AST file");
4891 Context.setsigjmp_bufDecl(Tag->getDecl());
4892 }
4893 }
4894 }
4895
4896 if (unsigned ObjCIdRedef
4897 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4898 if (Context.ObjCIdRedefinitionType.isNull())
4899 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4900 }
4901
4902 if (unsigned ObjCClassRedef
4903 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4904 if (Context.ObjCClassRedefinitionType.isNull())
4905 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4906 }
4907
4908 if (unsigned ObjCSelRedef
4909 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4910 if (Context.ObjCSelRedefinitionType.isNull())
4911 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4912 }
4913
4914 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4915 QualType Ucontext_tType = GetType(Ucontext_t);
4916 if (Ucontext_tType.isNull()) {
4917 Error("ucontext_t type is NULL");
4918 return;
4919 }
4920
4921 if (!Context.ucontext_tDecl) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004922 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
Guy Benyei11169dd2012-12-18 14:30:41 +00004923 Context.setucontext_tDecl(Typedef->getDecl());
4924 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004925 const TagType *Tag = Ucontext_tType->getAs<TagType>();
Guy Benyei11169dd2012-12-18 14:30:41 +00004926 assert(Tag && "Invalid ucontext_t type in AST file");
4927 Context.setucontext_tDecl(Tag->getDecl());
4928 }
4929 }
4930 }
4931 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004932
Guy Benyei11169dd2012-12-18 14:30:41 +00004933 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4934
4935 // If there were any CUDA special declarations, deserialize them.
4936 if (!CUDASpecialDeclRefs.empty()) {
4937 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4938 Context.setcudaConfigureCallDecl(
4939 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4940 }
Richard Smith56be7542014-03-21 00:33:59 +00004941
Guy Benyei11169dd2012-12-18 14:30:41 +00004942 // Re-export any modules that were imported by a non-module AST file.
Richard Smitha7e2cc62015-05-01 01:53:09 +00004943 // FIXME: This does not make macro-only imports visible again.
Richard Smith56be7542014-03-21 00:33:59 +00004944 for (auto &Import : ImportedModules) {
Richard Smitha7e2cc62015-05-01 01:53:09 +00004945 if (Module *Imported = getSubmodule(Import.ID)) {
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00004946 makeModuleVisible(Imported, Module::AllVisible,
Richard Smitha7e2cc62015-05-01 01:53:09 +00004947 /*ImportLoc=*/Import.ImportLoc);
Ben Langmuir6d25fdc2016-02-11 17:04:42 +00004948 if (Import.ImportLoc.isValid())
4949 PP.makeModuleVisible(Imported, Import.ImportLoc);
4950 // FIXME: should we tell Sema to make the module visible too?
Richard Smitha7e2cc62015-05-01 01:53:09 +00004951 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004952 }
4953 ImportedModules.clear();
4954}
4955
4956void ASTReader::finalizeForWriting() {
Richard Smithde711422015-04-23 21:20:19 +00004957 // Nothing to do for now.
Guy Benyei11169dd2012-12-18 14:30:41 +00004958}
4959
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004960/// Reads and return the signature record from \p PCH's control block, or
Peter Collingbourne77c89b62016-11-08 04:17:11 +00004961/// else returns 0.
4962static ASTFileSignature readASTFileSignature(StringRef PCH) {
4963 BitstreamCursor Stream(PCH);
JF Bastien0e828952019-06-26 19:50:12 +00004964 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4965 // FIXME this drops the error on the floor.
4966 consumeError(std::move(Err));
Vedant Kumar48b4f762018-04-14 01:40:48 +00004967 return ASTFileSignature();
JF Bastien0e828952019-06-26 19:50:12 +00004968 }
Ben Langmuir487ea142014-10-23 18:05:36 +00004969
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004970 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4971 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
Vedant Kumar48b4f762018-04-14 01:40:48 +00004972 return ASTFileSignature();
Ben Langmuir487ea142014-10-23 18:05:36 +00004973
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004974 // Scan for SIGNATURE inside the diagnostic options block.
Ben Langmuir487ea142014-10-23 18:05:36 +00004975 ASTReader::RecordData Record;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00004976 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00004977 Expected<llvm::BitstreamEntry> MaybeEntry =
4978 Stream.advanceSkippingSubblocks();
4979 if (!MaybeEntry) {
4980 // FIXME this drops the error on the floor.
4981 consumeError(MaybeEntry.takeError());
4982 return ASTFileSignature();
4983 }
4984 llvm::BitstreamEntry Entry = MaybeEntry.get();
4985
Simon Pilgrim0b33f112016-11-16 16:11:08 +00004986 if (Entry.Kind != llvm::BitstreamEntry::Record)
Vedant Kumar48b4f762018-04-14 01:40:48 +00004987 return ASTFileSignature();
Ben Langmuir487ea142014-10-23 18:05:36 +00004988
4989 Record.clear();
4990 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00004991 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
4992 if (!MaybeRecord) {
4993 // FIXME this drops the error on the floor.
4994 consumeError(MaybeRecord.takeError());
4995 return ASTFileSignature();
4996 }
4997 if (SIGNATURE == MaybeRecord.get())
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004998 return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
4999 (uint32_t)Record[3], (uint32_t)Record[4]}}};
Ben Langmuir487ea142014-10-23 18:05:36 +00005000 }
5001}
5002
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005003/// Retrieve the name of the original source file name
Guy Benyei11169dd2012-12-18 14:30:41 +00005004/// directly from the AST file, without actually loading the AST
5005/// file.
Adrian Prantlbb165fb2015-06-20 18:53:08 +00005006std::string ASTReader::getOriginalSourceFile(
5007 const std::string &ASTFileName, FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +00005008 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005009 // Open the AST file.
Benjamin Kramera8857962014-10-26 22:44:13 +00005010 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00005011 if (!Buffer) {
Benjamin Kramera8857962014-10-26 22:44:13 +00005012 Diags.Report(diag::err_fe_unable_to_read_pch_file)
5013 << ASTFileName << Buffer.getError().message();
Vedant Kumar48b4f762018-04-14 01:40:48 +00005014 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00005015 }
5016
5017 // Initialize the stream
Peter Collingbourne77c89b62016-11-08 04:17:11 +00005018 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
Guy Benyei11169dd2012-12-18 14:30:41 +00005019
5020 // Sniff for the signature.
JF Bastien0e828952019-06-26 19:50:12 +00005021 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5022 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
Vedant Kumar48b4f762018-04-14 01:40:48 +00005023 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00005024 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005025
Chris Lattnere7b154b2013-01-19 21:39:22 +00005026 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005027 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005028 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Vedant Kumar48b4f762018-04-14 01:40:48 +00005029 return std::string();
Chris Lattnere7b154b2013-01-19 21:39:22 +00005030 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005031
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005032 // Scan for ORIGINAL_FILE inside the control block.
5033 RecordData Record;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005034 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00005035 Expected<llvm::BitstreamEntry> MaybeEntry =
5036 Stream.advanceSkippingSubblocks();
5037 if (!MaybeEntry) {
5038 // FIXME this drops errors on the floor.
5039 consumeError(MaybeEntry.takeError());
5040 return std::string();
5041 }
5042 llvm::BitstreamEntry Entry = MaybeEntry.get();
5043
Chris Lattnere7b154b2013-01-19 21:39:22 +00005044 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
Vedant Kumar48b4f762018-04-14 01:40:48 +00005045 return std::string();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005046
Chris Lattnere7b154b2013-01-19 21:39:22 +00005047 if (Entry.Kind != llvm::BitstreamEntry::Record) {
5048 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Vedant Kumar48b4f762018-04-14 01:40:48 +00005049 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00005050 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005051
Guy Benyei11169dd2012-12-18 14:30:41 +00005052 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005053 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00005054 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5055 if (!MaybeRecord) {
5056 // FIXME this drops the errors on the floor.
5057 consumeError(MaybeRecord.takeError());
5058 return std::string();
5059 }
5060 if (ORIGINAL_FILE == MaybeRecord.get())
Chris Lattner0e6c9402013-01-20 02:38:54 +00005061 return Blob.str();
Guy Benyei11169dd2012-12-18 14:30:41 +00005062 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005063}
5064
5065namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005066
Guy Benyei11169dd2012-12-18 14:30:41 +00005067 class SimplePCHValidator : public ASTReaderListener {
5068 const LangOptions &ExistingLangOpts;
5069 const TargetOptions &ExistingTargetOpts;
5070 const PreprocessorOptions &ExistingPPOpts;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005071 std::string ExistingModuleCachePath;
Guy Benyei11169dd2012-12-18 14:30:41 +00005072 FileManager &FileMgr;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005073
Guy Benyei11169dd2012-12-18 14:30:41 +00005074 public:
5075 SimplePCHValidator(const LangOptions &ExistingLangOpts,
5076 const TargetOptions &ExistingTargetOpts,
5077 const PreprocessorOptions &ExistingPPOpts,
Benjamin Krameradcd0262020-01-28 20:23:46 +01005078 StringRef ExistingModuleCachePath, FileManager &FileMgr)
5079 : ExistingLangOpts(ExistingLangOpts),
5080 ExistingTargetOpts(ExistingTargetOpts),
5081 ExistingPPOpts(ExistingPPOpts),
5082 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
Guy Benyei11169dd2012-12-18 14:30:41 +00005083
Richard Smith1e2cf0d2014-10-31 02:28:58 +00005084 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5085 bool AllowCompatibleDifferences) override {
5086 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5087 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00005088 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005089
Chandler Carruth0d745bc2015-03-14 04:47:43 +00005090 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5091 bool AllowCompatibleDifferences) override {
5092 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5093 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00005094 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005095
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005096 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5097 StringRef SpecificModuleCachePath,
5098 bool Complain) override {
5099 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5100 ExistingModuleCachePath,
5101 nullptr, ExistingLangOpts);
5102 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005103
Craig Topper3e89dfe2014-03-13 02:13:41 +00005104 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5105 bool Complain,
5106 std::string &SuggestedPredefines) override {
Craig Toppera13603a2014-05-22 05:54:18 +00005107 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00005108 SuggestedPredefines, ExistingLangOpts);
Guy Benyei11169dd2012-12-18 14:30:41 +00005109 }
5110 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005111
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005112} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00005113
Adrian Prantlbb165fb2015-06-20 18:53:08 +00005114bool ASTReader::readASTFileControlBlock(
5115 StringRef Filename, FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +00005116 const PCHContainerReader &PCHContainerRdr,
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005117 bool FindModuleFileExtensions,
Manman Ren47a44452016-07-26 17:12:17 +00005118 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005119 // Open the AST file.
Richard Smith7f330cd2015-03-18 01:42:29 +00005120 // FIXME: This allows use of the VFS; we do not allow use of the
5121 // VFS when actually loading a module.
Benjamin Kramera8857962014-10-26 22:44:13 +00005122 auto Buffer = FileMgr.getBufferForFile(Filename);
Guy Benyei11169dd2012-12-18 14:30:41 +00005123 if (!Buffer) {
5124 return true;
5125 }
5126
5127 // Initialize the stream
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005128 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5129 BitstreamCursor Stream(Bytes);
Guy Benyei11169dd2012-12-18 14:30:41 +00005130
5131 // Sniff for the signature.
JF Bastien0e828952019-06-26 19:50:12 +00005132 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5133 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
Guy Benyei11169dd2012-12-18 14:30:41 +00005134 return true;
JF Bastien0e828952019-06-26 19:50:12 +00005135 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005136
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005137 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005138 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005139 return true;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005140
5141 bool NeedsInputFiles = Listener.needsInputFileVisitation();
Ben Langmuircb69b572014-03-07 06:40:32 +00005142 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
Richard Smithd4b230b2014-10-27 23:01:16 +00005143 bool NeedsImports = Listener.needsImportVisitation();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005144 BitstreamCursor InputFilesCursor;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005145
Guy Benyei11169dd2012-12-18 14:30:41 +00005146 RecordData Record;
Richard Smith7ed1bc92014-12-05 22:42:13 +00005147 std::string ModuleDir;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005148 bool DoneWithControlBlock = false;
5149 while (!DoneWithControlBlock) {
JF Bastien0e828952019-06-26 19:50:12 +00005150 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5151 if (!MaybeEntry) {
5152 // FIXME this drops the error on the floor.
5153 consumeError(MaybeEntry.takeError());
5154 return true;
5155 }
5156 llvm::BitstreamEntry Entry = MaybeEntry.get();
Richard Smith0516b182015-09-08 19:40:14 +00005157
5158 switch (Entry.Kind) {
5159 case llvm::BitstreamEntry::SubBlock: {
5160 switch (Entry.ID) {
5161 case OPTIONS_BLOCK_ID: {
5162 std::string IgnoredSuggestedPredefines;
5163 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5164 /*AllowCompatibleConfigurationMismatch*/ false,
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005165 Listener, IgnoredSuggestedPredefines) != Success)
Richard Smith0516b182015-09-08 19:40:14 +00005166 return true;
5167 break;
5168 }
5169
5170 case INPUT_FILES_BLOCK_ID:
5171 InputFilesCursor = Stream;
JF Bastien0e828952019-06-26 19:50:12 +00005172 if (llvm::Error Err = Stream.SkipBlock()) {
5173 // FIXME this drops the error on the floor.
5174 consumeError(std::move(Err));
5175 return true;
5176 }
5177 if (NeedsInputFiles &&
5178 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
Richard Smith0516b182015-09-08 19:40:14 +00005179 return true;
5180 break;
5181
5182 default:
JF Bastien0e828952019-06-26 19:50:12 +00005183 if (llvm::Error Err = Stream.SkipBlock()) {
5184 // FIXME this drops the error on the floor.
5185 consumeError(std::move(Err));
Richard Smith0516b182015-09-08 19:40:14 +00005186 return true;
JF Bastien0e828952019-06-26 19:50:12 +00005187 }
Richard Smith0516b182015-09-08 19:40:14 +00005188 break;
5189 }
5190
5191 continue;
5192 }
5193
5194 case llvm::BitstreamEntry::EndBlock:
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005195 DoneWithControlBlock = true;
5196 break;
Richard Smith0516b182015-09-08 19:40:14 +00005197
5198 case llvm::BitstreamEntry::Error:
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005199 return true;
Richard Smith0516b182015-09-08 19:40:14 +00005200
5201 case llvm::BitstreamEntry::Record:
5202 break;
5203 }
5204
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005205 if (DoneWithControlBlock) break;
5206
Guy Benyei11169dd2012-12-18 14:30:41 +00005207 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005208 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00005209 Expected<unsigned> MaybeRecCode =
5210 Stream.readRecord(Entry.ID, Record, &Blob);
5211 if (!MaybeRecCode) {
5212 // FIXME this drops the error.
5213 return Failure;
5214 }
5215 switch ((ControlRecordTypes)MaybeRecCode.get()) {
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005216 case METADATA:
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005217 if (Record[0] != VERSION_MAJOR)
5218 return true;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00005219 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005220 return true;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005221 break;
Ben Langmuir4f5212a2014-04-14 22:12:44 +00005222 case MODULE_NAME:
5223 Listener.ReadModuleName(Blob);
5224 break;
Richard Smith7ed1bc92014-12-05 22:42:13 +00005225 case MODULE_DIRECTORY:
Benjamin Krameradcd0262020-01-28 20:23:46 +01005226 ModuleDir = std::string(Blob);
Richard Smith7ed1bc92014-12-05 22:42:13 +00005227 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00005228 case MODULE_MAP_FILE: {
5229 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00005230 auto Path = ReadString(Record, Idx);
5231 ResolveImportedPath(Path, ModuleDir);
5232 Listener.ReadModuleMapFile(Path);
Ben Langmuir4f5212a2014-04-14 22:12:44 +00005233 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00005234 }
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005235 case INPUT_FILE_OFFSETS: {
5236 if (!NeedsInputFiles)
5237 break;
5238
5239 unsigned NumInputFiles = Record[0];
5240 unsigned NumUserFiles = Record[1];
Raphael Isemanneb13d3d2018-05-23 09:02:40 +00005241 const llvm::support::unaligned_uint64_t *InputFileOffs =
5242 (const llvm::support::unaligned_uint64_t *)Blob.data();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005243 for (unsigned I = 0; I != NumInputFiles; ++I) {
5244 // Go find this input file.
5245 bool isSystemFile = I >= NumUserFiles;
Ben Langmuircb69b572014-03-07 06:40:32 +00005246
5247 if (isSystemFile && !NeedsSystemInputFiles)
5248 break; // the rest are system input files
5249
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005250 BitstreamCursor &Cursor = InputFilesCursor;
5251 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00005252 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5253 // FIXME this drops errors on the floor.
5254 consumeError(std::move(Err));
5255 }
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005256
JF Bastien0e828952019-06-26 19:50:12 +00005257 Expected<unsigned> MaybeCode = Cursor.ReadCode();
5258 if (!MaybeCode) {
5259 // FIXME this drops errors on the floor.
5260 consumeError(MaybeCode.takeError());
5261 }
5262 unsigned Code = MaybeCode.get();
5263
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005264 RecordData Record;
5265 StringRef Blob;
5266 bool shouldContinue = false;
JF Bastien0e828952019-06-26 19:50:12 +00005267 Expected<unsigned> MaybeRecordType =
5268 Cursor.readRecord(Code, Record, &Blob);
5269 if (!MaybeRecordType) {
5270 // FIXME this drops errors on the floor.
5271 consumeError(MaybeRecordType.takeError());
5272 }
5273 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00005274 case INPUT_FILE_HASH:
5275 break;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005276 case INPUT_FILE:
Vedant Kumar48b4f762018-04-14 01:40:48 +00005277 bool Overridden = static_cast<bool>(Record[3]);
Benjamin Krameradcd0262020-01-28 20:23:46 +01005278 std::string Filename = std::string(Blob);
Richard Smith7ed1bc92014-12-05 22:42:13 +00005279 ResolveImportedPath(Filename, ModuleDir);
Richard Smith216a3bd2015-08-13 17:57:10 +00005280 shouldContinue = Listener.visitInputFile(
5281 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005282 break;
5283 }
5284 if (!shouldContinue)
5285 break;
5286 }
5287 break;
5288 }
5289
Richard Smithd4b230b2014-10-27 23:01:16 +00005290 case IMPORTS: {
5291 if (!NeedsImports)
5292 break;
5293
5294 unsigned Idx = 0, N = Record.size();
5295 while (Idx < N) {
5296 // Read information about the AST file.
Bruno Cardoso Lopes6fc8a562018-09-11 05:17:13 +00005297 Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature
5298 std::string ModuleName = ReadString(Record, Idx);
Richard Smith7ed1bc92014-12-05 22:42:13 +00005299 std::string Filename = ReadString(Record, Idx);
5300 ResolveImportedPath(Filename, ModuleDir);
Bruno Cardoso Lopes6fc8a562018-09-11 05:17:13 +00005301 Listener.visitImport(ModuleName, Filename);
Richard Smithd4b230b2014-10-27 23:01:16 +00005302 }
5303 break;
5304 }
5305
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005306 default:
5307 // No other validation to perform.
5308 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005309 }
5310 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005311
5312 // Look for module file extension blocks, if requested.
5313 if (FindModuleFileExtensions) {
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005314 BitstreamCursor SavedStream = Stream;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005315 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5316 bool DoneWithExtensionBlock = false;
5317 while (!DoneWithExtensionBlock) {
JF Bastien0e828952019-06-26 19:50:12 +00005318 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5319 if (!MaybeEntry) {
5320 // FIXME this drops the error.
5321 return true;
5322 }
5323 llvm::BitstreamEntry Entry = MaybeEntry.get();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005324
JF Bastien0e828952019-06-26 19:50:12 +00005325 switch (Entry.Kind) {
5326 case llvm::BitstreamEntry::SubBlock:
5327 if (llvm::Error Err = Stream.SkipBlock()) {
5328 // FIXME this drops the error on the floor.
5329 consumeError(std::move(Err));
5330 return true;
5331 }
5332 continue;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005333
JF Bastien0e828952019-06-26 19:50:12 +00005334 case llvm::BitstreamEntry::EndBlock:
5335 DoneWithExtensionBlock = true;
5336 continue;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005337
JF Bastien0e828952019-06-26 19:50:12 +00005338 case llvm::BitstreamEntry::Error:
5339 return true;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005340
JF Bastien0e828952019-06-26 19:50:12 +00005341 case llvm::BitstreamEntry::Record:
5342 break;
5343 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005344
5345 Record.clear();
5346 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00005347 Expected<unsigned> MaybeRecCode =
5348 Stream.readRecord(Entry.ID, Record, &Blob);
5349 if (!MaybeRecCode) {
5350 // FIXME this drops the error.
5351 return true;
5352 }
5353 switch (MaybeRecCode.get()) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005354 case EXTENSION_METADATA: {
5355 ModuleFileExtensionMetadata Metadata;
5356 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5357 return true;
5358
5359 Listener.readModuleFileExtension(Metadata);
5360 break;
5361 }
5362 }
5363 }
5364 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005365 Stream = SavedStream;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005366 }
5367
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005368 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5369 if (readUnhashedControlBlockImpl(
5370 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5371 /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5372 ValidateDiagnosticOptions) != Success)
5373 return true;
5374
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005375 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00005376}
5377
Benjamin Kramerf6021ec2017-03-21 21:35:04 +00005378bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5379 const PCHContainerReader &PCHContainerRdr,
5380 const LangOptions &LangOpts,
5381 const TargetOptions &TargetOpts,
5382 const PreprocessorOptions &PPOpts,
5383 StringRef ExistingModuleCachePath) {
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005384 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5385 ExistingModuleCachePath, FileMgr);
Adrian Prantlfb2398d2015-07-17 01:19:54 +00005386 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005387 /*FindModuleFileExtensions=*/false,
Manman Ren47a44452016-07-26 17:12:17 +00005388 validator,
5389 /*ValidateDiagnosticOptions=*/true);
Guy Benyei11169dd2012-12-18 14:30:41 +00005390}
5391
Ben Langmuir2c9af442014-04-10 17:57:43 +00005392ASTReader::ASTReadResult
5393ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005394 // Enter the submodule block.
JF Bastien0e828952019-06-26 19:50:12 +00005395 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5396 Error(std::move(Err));
Ben Langmuir2c9af442014-04-10 17:57:43 +00005397 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00005398 }
5399
5400 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5401 bool First = true;
Craig Toppera13603a2014-05-22 05:54:18 +00005402 Module *CurrentModule = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005403 RecordData Record;
5404 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00005405 Expected<llvm::BitstreamEntry> MaybeEntry =
5406 F.Stream.advanceSkippingSubblocks();
5407 if (!MaybeEntry) {
5408 Error(MaybeEntry.takeError());
5409 return Failure;
5410 }
5411 llvm::BitstreamEntry Entry = MaybeEntry.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005412
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005413 switch (Entry.Kind) {
5414 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5415 case llvm::BitstreamEntry::Error:
5416 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00005417 return Failure;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005418 case llvm::BitstreamEntry::EndBlock:
Ben Langmuir2c9af442014-04-10 17:57:43 +00005419 return Success;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005420 case llvm::BitstreamEntry::Record:
5421 // The interesting case.
5422 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005423 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005424
Guy Benyei11169dd2012-12-18 14:30:41 +00005425 // Read a record.
Chris Lattner0e6c9402013-01-20 02:38:54 +00005426 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00005427 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00005428 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5429 if (!MaybeKind) {
5430 Error(MaybeKind.takeError());
5431 return Failure;
5432 }
5433 unsigned Kind = MaybeKind.get();
Richard Smith03478d92014-10-23 22:12:14 +00005434
5435 if ((Kind == SUBMODULE_METADATA) != First) {
5436 Error("submodule metadata record should be at beginning of block");
5437 return Failure;
5438 }
5439 First = false;
5440
5441 // Submodule information is only valid if we have a current module.
5442 // FIXME: Should we error on these cases?
5443 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5444 Kind != SUBMODULE_DEFINITION)
5445 continue;
5446
5447 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005448 default: // Default behavior: ignore.
5449 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005450
Richard Smith03478d92014-10-23 22:12:14 +00005451 case SUBMODULE_DEFINITION: {
Jordan Rose90b0a1f2018-04-20 17:16:04 +00005452 if (Record.size() < 12) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005453 Error("malformed module definition");
Ben Langmuir2c9af442014-04-10 17:57:43 +00005454 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00005455 }
Richard Smith03478d92014-10-23 22:12:14 +00005456
Chris Lattner0e6c9402013-01-20 02:38:54 +00005457 StringRef Name = Blob;
Richard Smith9bca2982014-03-08 00:03:56 +00005458 unsigned Idx = 0;
5459 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5460 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
Vedant Kumar48b4f762018-04-14 01:40:48 +00005461 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
Richard Smith9bca2982014-03-08 00:03:56 +00005462 bool IsFramework = Record[Idx++];
5463 bool IsExplicit = Record[Idx++];
5464 bool IsSystem = Record[Idx++];
5465 bool IsExternC = Record[Idx++];
5466 bool InferSubmodules = Record[Idx++];
5467 bool InferExplicitSubmodules = Record[Idx++];
5468 bool InferExportWildcard = Record[Idx++];
5469 bool ConfigMacrosExhaustive = Record[Idx++];
Jordan Rose90b0a1f2018-04-20 17:16:04 +00005470 bool ModuleMapIsPrivate = Record[Idx++];
Douglas Gregor8d932422013-03-20 03:59:18 +00005471
Ben Langmuirbeee15e2014-04-14 18:00:01 +00005472 Module *ParentModule = nullptr;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00005473 if (Parent)
Guy Benyei11169dd2012-12-18 14:30:41 +00005474 ParentModule = getSubmodule(Parent);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00005475
Guy Benyei11169dd2012-12-18 14:30:41 +00005476 // Retrieve this (sub)module from the module map, creating it if
5477 // necessary.
David Blaikie9ffe5a32017-01-30 05:00:26 +00005478 CurrentModule =
5479 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5480 .first;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00005481
5482 // FIXME: set the definition loc for CurrentModule, or call
5483 // ModMap.setInferredModuleAllowedBy()
5484
Guy Benyei11169dd2012-12-18 14:30:41 +00005485 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5486 if (GlobalIndex >= SubmodulesLoaded.size() ||
5487 SubmodulesLoaded[GlobalIndex]) {
5488 Error("too many submodules");
Ben Langmuir2c9af442014-04-10 17:57:43 +00005489 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00005490 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00005491
Douglas Gregor7029ce12013-03-19 00:28:20 +00005492 if (!ParentModule) {
5493 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
Yuka Takahashid8baec22018-08-01 09:50:02 +00005494 // Don't emit module relocation error if we have -fno-validate-pch
5495 if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5496 CurFile != F.File) {
Duncan P. N. Exon Smitheef69022019-11-10 11:17:42 -08005497 Error(diag::err_module_file_conflict,
5498 CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5499 F.File->getName());
Ben Langmuir2c9af442014-04-10 17:57:43 +00005500 return Failure;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00005501 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00005502 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00005503
Duncan P. N. Exon Smith83dcb342019-11-10 13:14:52 -08005504 F.DidReadTopLevelSubmodule = true;
Douglas Gregor7029ce12013-03-19 00:28:20 +00005505 CurrentModule->setASTFile(F.File);
Richard Smithab755972017-06-05 18:10:11 +00005506 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00005507 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00005508
Richard Smithdd8b5332017-09-04 05:37:53 +00005509 CurrentModule->Kind = Kind;
Adrian Prantl15bcf702015-06-30 17:39:43 +00005510 CurrentModule->Signature = F.Signature;
Guy Benyei11169dd2012-12-18 14:30:41 +00005511 CurrentModule->IsFromModuleFile = true;
5512 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Richard Smith9bca2982014-03-08 00:03:56 +00005513 CurrentModule->IsExternC = IsExternC;
Guy Benyei11169dd2012-12-18 14:30:41 +00005514 CurrentModule->InferSubmodules = InferSubmodules;
5515 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5516 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor8d932422013-03-20 03:59:18 +00005517 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Jordan Rose90b0a1f2018-04-20 17:16:04 +00005518 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
Guy Benyei11169dd2012-12-18 14:30:41 +00005519 if (DeserializationListener)
5520 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005521
Guy Benyei11169dd2012-12-18 14:30:41 +00005522 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005523
Richard Smith8a3e39a2016-03-28 21:31:09 +00005524 // Clear out data that will be replaced by what is in the module file.
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005525 CurrentModule->LinkLibraries.clear();
Douglas Gregor8d932422013-03-20 03:59:18 +00005526 CurrentModule->ConfigMacros.clear();
Douglas Gregorfb912652013-03-20 21:10:35 +00005527 CurrentModule->UnresolvedConflicts.clear();
5528 CurrentModule->Conflicts.clear();
Richard Smith8a3e39a2016-03-28 21:31:09 +00005529
5530 // The module is available unless it's missing a requirement; relevant
5531 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5532 // Missing headers that were present when the module was built do not
5533 // make it unavailable -- if we got this far, this must be an explicitly
5534 // imported module file.
5535 CurrentModule->Requirements.clear();
5536 CurrentModule->MissingHeaders.clear();
5537 CurrentModule->IsMissingRequirement =
5538 ParentModule && ParentModule->IsMissingRequirement;
5539 CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
Guy Benyei11169dd2012-12-18 14:30:41 +00005540 break;
5541 }
Richard Smith8a3e39a2016-03-28 21:31:09 +00005542
Guy Benyei11169dd2012-12-18 14:30:41 +00005543 case SUBMODULE_UMBRELLA_HEADER: {
Benjamin Krameradcd0262020-01-28 20:23:46 +01005544 std::string Filename = std::string(Blob);
Richard Smith2b63d152015-05-16 02:28:53 +00005545 ResolveImportedPath(F, Filename);
Harlan Haskins8d323d12019-08-01 21:31:56 +00005546 if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005547 if (!CurrentModule->getUmbrellaHeader())
Harlan Haskins8d323d12019-08-01 21:31:56 +00005548 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5549 else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
Bruno Cardoso Lopes573b13f2017-03-22 00:11:21 +00005550 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5551 Error("mismatched umbrella headers in submodule");
5552 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00005553 }
5554 }
5555 break;
5556 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005557
Richard Smith202210b2014-10-24 20:23:01 +00005558 case SUBMODULE_HEADER:
5559 case SUBMODULE_EXCLUDED_HEADER:
5560 case SUBMODULE_PRIVATE_HEADER:
5561 // We lazily associate headers with their modules via the HeaderInfo table.
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00005562 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5563 // of complete filenames or remove it entirely.
Richard Smith202210b2014-10-24 20:23:01 +00005564 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005565
Richard Smith202210b2014-10-24 20:23:01 +00005566 case SUBMODULE_TEXTUAL_HEADER:
5567 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5568 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5569 // them here.
5570 break;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00005571
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005572 case SUBMODULE_TOPHEADER:
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00005573 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00005574 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005575
5576 case SUBMODULE_UMBRELLA_DIR: {
Benjamin Krameradcd0262020-01-28 20:23:46 +01005577 std::string Dirname = std::string(Blob);
Richard Smith2b63d152015-05-16 02:28:53 +00005578 ResolveImportedPath(F, Dirname);
Harlan Haskins8d323d12019-08-01 21:31:56 +00005579 if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005580 if (!CurrentModule->getUmbrellaDir())
Harlan Haskins8d323d12019-08-01 21:31:56 +00005581 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5582 else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
Ben Langmuir2c9af442014-04-10 17:57:43 +00005583 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5584 Error("mismatched umbrella directories in submodule");
5585 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00005586 }
5587 }
5588 break;
5589 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005590
Guy Benyei11169dd2012-12-18 14:30:41 +00005591 case SUBMODULE_METADATA: {
Guy Benyei11169dd2012-12-18 14:30:41 +00005592 F.BaseSubmoduleID = getTotalNumSubmodules();
5593 F.LocalNumSubmodules = Record[0];
5594 unsigned LocalBaseSubmoduleID = Record[1];
5595 if (F.LocalNumSubmodules > 0) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005596 // Introduce the global -> local mapping for submodules within this
Guy Benyei11169dd2012-12-18 14:30:41 +00005597 // module.
5598 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005599
5600 // Introduce the local -> global mapping for submodules within this
Guy Benyei11169dd2012-12-18 14:30:41 +00005601 // module.
5602 F.SubmoduleRemap.insertOrReplace(
5603 std::make_pair(LocalBaseSubmoduleID,
5604 F.BaseSubmoduleID - LocalBaseSubmoduleID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00005605
Ben Langmuir52ca6782014-10-20 16:27:32 +00005606 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5607 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005608 break;
5609 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005610
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005611 case SUBMODULE_IMPORTS:
Guy Benyei11169dd2012-12-18 14:30:41 +00005612 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregorfb912652013-03-20 21:10:35 +00005613 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00005614 Unresolved.File = &F;
5615 Unresolved.Mod = CurrentModule;
5616 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00005617 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei11169dd2012-12-18 14:30:41 +00005618 Unresolved.IsWildcard = false;
Douglas Gregorfb912652013-03-20 21:10:35 +00005619 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00005620 }
5621 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005622
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005623 case SUBMODULE_EXPORTS:
Guy Benyei11169dd2012-12-18 14:30:41 +00005624 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregorfb912652013-03-20 21:10:35 +00005625 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00005626 Unresolved.File = &F;
5627 Unresolved.Mod = CurrentModule;
5628 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00005629 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei11169dd2012-12-18 14:30:41 +00005630 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregorfb912652013-03-20 21:10:35 +00005631 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00005632 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005633
5634 // Once we've loaded the set of exports, there's no reason to keep
Guy Benyei11169dd2012-12-18 14:30:41 +00005635 // the parsed, unresolved exports around.
5636 CurrentModule->UnresolvedExports.clear();
5637 break;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005638
5639 case SUBMODULE_REQUIRES:
Richard Smithdbafb6c2017-06-29 23:23:46 +00005640 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5641 PP.getTargetInfo());
Guy Benyei11169dd2012-12-18 14:30:41 +00005642 break;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005643
5644 case SUBMODULE_LINK_LIBRARY:
Bruno Cardoso Lopesa3b5f712018-04-16 19:42:32 +00005645 ModMap.resolveLinkAsDependencies(CurrentModule);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005646 CurrentModule->LinkLibraries.push_back(
Benjamin Krameradcd0262020-01-28 20:23:46 +01005647 Module::LinkLibrary(std::string(Blob), Record[0]));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005648 break;
Douglas Gregor35b13ec2013-03-20 00:22:05 +00005649
5650 case SUBMODULE_CONFIG_MACRO:
Douglas Gregor35b13ec2013-03-20 00:22:05 +00005651 CurrentModule->ConfigMacros.push_back(Blob.str());
5652 break;
Douglas Gregorfb912652013-03-20 21:10:35 +00005653
5654 case SUBMODULE_CONFLICT: {
Douglas Gregorfb912652013-03-20 21:10:35 +00005655 UnresolvedModuleRef Unresolved;
5656 Unresolved.File = &F;
5657 Unresolved.Mod = CurrentModule;
5658 Unresolved.ID = Record[0];
5659 Unresolved.Kind = UnresolvedModuleRef::Conflict;
5660 Unresolved.IsWildcard = false;
5661 Unresolved.String = Blob;
5662 UnresolvedModuleRefs.push_back(Unresolved);
5663 break;
5664 }
Richard Smithdc1f0422016-07-20 19:10:16 +00005665
Douglas Gregorf0b11de2017-09-14 23:38:44 +00005666 case SUBMODULE_INITIALIZERS: {
Richard Smithdbafb6c2017-06-29 23:23:46 +00005667 if (!ContextObj)
5668 break;
Richard Smithdc1f0422016-07-20 19:10:16 +00005669 SmallVector<uint32_t, 16> Inits;
5670 for (auto &ID : Record)
5671 Inits.push_back(getGlobalDeclID(F, ID));
Richard Smithdbafb6c2017-06-29 23:23:46 +00005672 ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
Richard Smithdc1f0422016-07-20 19:10:16 +00005673 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005674 }
Douglas Gregorf0b11de2017-09-14 23:38:44 +00005675
5676 case SUBMODULE_EXPORT_AS:
5677 CurrentModule->ExportAsModule = Blob.str();
Bruno Cardoso Lopesa3b5f712018-04-16 19:42:32 +00005678 ModMap.addLinkAsDependency(CurrentModule);
Douglas Gregorf0b11de2017-09-14 23:38:44 +00005679 break;
5680 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005681 }
5682}
5683
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005684/// Parse the record that corresponds to a LangOptions data
Guy Benyei11169dd2012-12-18 14:30:41 +00005685/// structure.
5686///
5687/// This routine parses the language options from the AST file and then gives
5688/// them to the AST listener if one is set.
5689///
5690/// \returns true if the listener deems the file unacceptable, false otherwise.
5691bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5692 bool Complain,
Richard Smith1e2cf0d2014-10-31 02:28:58 +00005693 ASTReaderListener &Listener,
5694 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005695 LangOptions LangOpts;
5696 unsigned Idx = 0;
5697#define LANGOPT(Name, Bits, Default, Description) \
5698 LangOpts.Name = Record[Idx++];
5699#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5700 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5701#include "clang/Basic/LangOptions.def"
Alexey Samsonovedf99a92014-11-07 22:29:38 +00005702#define SANITIZER(NAME, ID) \
5703 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
Will Dietzf54319c2013-01-18 11:30:38 +00005704#include "clang/Basic/Sanitizers.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00005705
Ben Langmuircd98cb72015-06-23 18:20:18 +00005706 for (unsigned N = Record[Idx++]; N; --N)
5707 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5708
Vedant Kumar48b4f762018-04-14 01:40:48 +00005709 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00005710 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5711 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00005712
Ben Langmuird4a667a2015-06-23 18:20:23 +00005713 LangOpts.CurrentModule = ReadString(Record, Idx);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00005714
5715 // Comment options.
5716 for (unsigned N = Record[Idx++]; N; --N) {
5717 LangOpts.CommentOpts.BlockCommandNames.push_back(
5718 ReadString(Record, Idx));
5719 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00005720 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00005721
Samuel Antaoee8fb302016-01-06 13:42:12 +00005722 // OpenMP offloading options.
5723 for (unsigned N = Record[Idx++]; N; --N) {
5724 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5725 }
5726
5727 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5728
Richard Smith1e2cf0d2014-10-31 02:28:58 +00005729 return Listener.ReadLanguageOptions(LangOpts, Complain,
5730 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00005731}
5732
Chandler Carruth0d745bc2015-03-14 04:47:43 +00005733bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5734 ASTReaderListener &Listener,
5735 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005736 unsigned Idx = 0;
5737 TargetOptions TargetOpts;
5738 TargetOpts.Triple = ReadString(Record, Idx);
5739 TargetOpts.CPU = ReadString(Record, Idx);
5740 TargetOpts.ABI = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005741 for (unsigned N = Record[Idx++]; N; --N) {
5742 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5743 }
5744 for (unsigned N = Record[Idx++]; N; --N) {
5745 TargetOpts.Features.push_back(ReadString(Record, Idx));
5746 }
5747
Chandler Carruth0d745bc2015-03-14 04:47:43 +00005748 return Listener.ReadTargetOptions(TargetOpts, Complain,
5749 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00005750}
5751
5752bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5753 ASTReaderListener &Listener) {
Ben Langmuirb92de022014-04-29 16:25:26 +00005754 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
Guy Benyei11169dd2012-12-18 14:30:41 +00005755 unsigned Idx = 0;
Ben Langmuirb92de022014-04-29 16:25:26 +00005756#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00005757#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
Ben Langmuirb92de022014-04-29 16:25:26 +00005758 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00005759#include "clang/Basic/DiagnosticOptions.def"
5760
Richard Smith3be1cb22014-08-07 00:24:21 +00005761 for (unsigned N = Record[Idx++]; N; --N)
Ben Langmuirb92de022014-04-29 16:25:26 +00005762 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
Richard Smith3be1cb22014-08-07 00:24:21 +00005763 for (unsigned N = Record[Idx++]; N; --N)
5764 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005765
5766 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5767}
5768
5769bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5770 ASTReaderListener &Listener) {
5771 FileSystemOptions FSOpts;
5772 unsigned Idx = 0;
5773 FSOpts.WorkingDir = ReadString(Record, Idx);
5774 return Listener.ReadFileSystemOptions(FSOpts, Complain);
5775}
5776
5777bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5778 bool Complain,
5779 ASTReaderListener &Listener) {
5780 HeaderSearchOptions HSOpts;
5781 unsigned Idx = 0;
5782 HSOpts.Sysroot = ReadString(Record, Idx);
5783
5784 // Include entries.
5785 for (unsigned N = Record[Idx++]; N; --N) {
5786 std::string Path = ReadString(Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00005787 frontend::IncludeDirGroup Group
5788 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00005789 bool IsFramework = Record[Idx++];
5790 bool IgnoreSysRoot = Record[Idx++];
Benjamin Kramer3204b152015-05-29 19:42:19 +00005791 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5792 IgnoreSysRoot);
Guy Benyei11169dd2012-12-18 14:30:41 +00005793 }
5794
5795 // System header prefixes.
5796 for (unsigned N = Record[Idx++]; N; --N) {
5797 std::string Prefix = ReadString(Record, Idx);
5798 bool IsSystemHeader = Record[Idx++];
Benjamin Kramer3204b152015-05-29 19:42:19 +00005799 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
Guy Benyei11169dd2012-12-18 14:30:41 +00005800 }
5801
5802 HSOpts.ResourceDir = ReadString(Record, Idx);
5803 HSOpts.ModuleCachePath = ReadString(Record, Idx);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00005804 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005805 HSOpts.DisableModuleHash = Record[Idx++];
Richard Smith18934752017-06-06 00:32:01 +00005806 HSOpts.ImplicitModuleMaps = Record[Idx++];
5807 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00005808 HSOpts.UseBuiltinIncludes = Record[Idx++];
5809 HSOpts.UseStandardSystemIncludes = Record[Idx++];
5810 HSOpts.UseStandardCXXIncludes = Record[Idx++];
5811 HSOpts.UseLibcxx = Record[Idx++];
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005812 std::string SpecificModuleCachePath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005813
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005814 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5815 Complain);
Guy Benyei11169dd2012-12-18 14:30:41 +00005816}
5817
5818bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5819 bool Complain,
5820 ASTReaderListener &Listener,
5821 std::string &SuggestedPredefines) {
5822 PreprocessorOptions PPOpts;
5823 unsigned Idx = 0;
5824
5825 // Macro definitions/undefs
5826 for (unsigned N = Record[Idx++]; N; --N) {
5827 std::string Macro = ReadString(Record, Idx);
5828 bool IsUndef = Record[Idx++];
5829 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5830 }
5831
5832 // Includes
5833 for (unsigned N = Record[Idx++]; N; --N) {
5834 PPOpts.Includes.push_back(ReadString(Record, Idx));
5835 }
5836
5837 // Macro Includes
5838 for (unsigned N = Record[Idx++]; N; --N) {
5839 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5840 }
5841
5842 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00005843 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00005844 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005845 PPOpts.ObjCXXARCStandardLibrary =
5846 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5847 SuggestedPredefines.clear();
5848 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5849 SuggestedPredefines);
5850}
5851
5852std::pair<ModuleFile *, unsigned>
5853ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5854 GlobalPreprocessedEntityMapType::iterator
5855 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005856 assert(I != GlobalPreprocessedEntityMap.end() &&
Guy Benyei11169dd2012-12-18 14:30:41 +00005857 "Corrupted global preprocessed entity map");
5858 ModuleFile *M = I->second;
5859 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5860 return std::make_pair(M, LocalIndex);
5861}
5862
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00005863llvm::iterator_range<PreprocessingRecord::iterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00005864ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5865 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5866 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5867 Mod.NumPreprocessedEntities);
5868
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00005869 return llvm::make_range(PreprocessingRecord::iterator(),
5870 PreprocessingRecord::iterator());
Guy Benyei11169dd2012-12-18 14:30:41 +00005871}
5872
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00005873llvm::iterator_range<ASTReader::ModuleDeclIterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00005874ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00005875 return llvm::make_range(
5876 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5877 ModuleDeclIterator(this, &Mod,
5878 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
Guy Benyei11169dd2012-12-18 14:30:41 +00005879}
5880
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00005881SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5882 auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5883 assert(I != GlobalSkippedRangeMap.end() &&
5884 "Corrupted global skipped range map");
5885 ModuleFile *M = I->second;
5886 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5887 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5888 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5889 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5890 TranslateSourceLocation(*M, RawRange.getEnd()));
5891 assert(Range.isValid());
5892 return Range;
5893}
5894
Guy Benyei11169dd2012-12-18 14:30:41 +00005895PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5896 PreprocessedEntityID PPID = Index+1;
5897 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5898 ModuleFile &M = *PPInfo.first;
5899 unsigned LocalIndex = PPInfo.second;
5900 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5901
Guy Benyei11169dd2012-12-18 14:30:41 +00005902 if (!PP.getPreprocessingRecord()) {
5903 Error("no preprocessing record");
Craig Toppera13603a2014-05-22 05:54:18 +00005904 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005905 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005906
5907 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
JF Bastien0e828952019-06-26 19:50:12 +00005908 if (llvm::Error Err =
5909 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset)) {
5910 Error(std::move(Err));
5911 return nullptr;
5912 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005913
JF Bastien0e828952019-06-26 19:50:12 +00005914 Expected<llvm::BitstreamEntry> MaybeEntry =
5915 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5916 if (!MaybeEntry) {
5917 Error(MaybeEntry.takeError());
5918 return nullptr;
5919 }
5920 llvm::BitstreamEntry Entry = MaybeEntry.get();
5921
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005922 if (Entry.Kind != llvm::BitstreamEntry::Record)
Craig Toppera13603a2014-05-22 05:54:18 +00005923 return nullptr;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005924
Guy Benyei11169dd2012-12-18 14:30:41 +00005925 // Read the record.
Richard Smithcb34bd32016-03-27 07:28:06 +00005926 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5927 TranslateSourceLocation(M, PPOffs.getEnd()));
Guy Benyei11169dd2012-12-18 14:30:41 +00005928 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005929 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00005930 RecordData Record;
JF Bastien0e828952019-06-26 19:50:12 +00005931 Expected<unsigned> MaybeRecType =
5932 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5933 if (!MaybeRecType) {
5934 Error(MaybeRecType.takeError());
5935 return nullptr;
5936 }
5937 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005938 case PPD_MACRO_EXPANSION: {
5939 bool isBuiltin = Record[0];
Craig Toppera13603a2014-05-22 05:54:18 +00005940 IdentifierInfo *Name = nullptr;
Richard Smith66a81862015-05-04 02:25:31 +00005941 MacroDefinitionRecord *Def = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005942 if (isBuiltin)
5943 Name = getLocalIdentifier(M, Record[1]);
5944 else {
Richard Smith66a81862015-05-04 02:25:31 +00005945 PreprocessedEntityID GlobalID =
5946 getGlobalPreprocessedEntityID(M, Record[1]);
5947 Def = cast<MacroDefinitionRecord>(
5948 PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
Guy Benyei11169dd2012-12-18 14:30:41 +00005949 }
5950
5951 MacroExpansion *ME;
5952 if (isBuiltin)
5953 ME = new (PPRec) MacroExpansion(Name, Range);
5954 else
5955 ME = new (PPRec) MacroExpansion(Def, Range);
5956
5957 return ME;
5958 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005959
Guy Benyei11169dd2012-12-18 14:30:41 +00005960 case PPD_MACRO_DEFINITION: {
5961 // Decode the identifier info and then check again; if the macro is
5962 // still defined and associated with the identifier,
5963 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Vedant Kumar48b4f762018-04-14 01:40:48 +00005964 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00005965
5966 if (DeserializationListener)
5967 DeserializationListener->MacroDefinitionRead(PPID, MD);
5968
5969 return MD;
5970 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005971
Guy Benyei11169dd2012-12-18 14:30:41 +00005972 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00005973 const char *FullFileNameStart = Blob.data() + Record[0];
5974 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Craig Toppera13603a2014-05-22 05:54:18 +00005975 const FileEntry *File = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005976 if (!FullFileName.empty())
Harlan Haskins8d323d12019-08-01 21:31:56 +00005977 if (auto FE = PP.getFileManager().getFile(FullFileName))
5978 File = *FE;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005979
Guy Benyei11169dd2012-12-18 14:30:41 +00005980 // FIXME: Stable encoding
Vedant Kumar48b4f762018-04-14 01:40:48 +00005981 InclusionDirective::InclusionKind Kind
5982 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
5983 InclusionDirective *ID
5984 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e6c9402013-01-20 02:38:54 +00005985 StringRef(Blob.data(), Record[0]),
Guy Benyei11169dd2012-12-18 14:30:41 +00005986 Record[1], Record[3],
5987 File,
5988 Range);
5989 return ID;
5990 }
5991 }
5992
5993 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
5994}
5995
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005996/// Find the next module that contains entities and return the ID
Guy Benyei11169dd2012-12-18 14:30:41 +00005997/// of the first entry.
NAKAMURA Takumi12ab07e2017-10-12 09:42:14 +00005998///
5999/// \param SLocMapI points at a chunk of a module that contains no
6000/// preprocessed entities or the entities it contains are not the ones we are
6001/// looking for.
Guy Benyei11169dd2012-12-18 14:30:41 +00006002PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6003 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6004 ++SLocMapI;
6005 for (GlobalSLocOffsetMapType::const_iterator
6006 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6007 ModuleFile &M = *SLocMapI->second;
6008 if (M.NumPreprocessedEntities)
6009 return M.BasePreprocessedEntityID;
6010 }
6011
6012 return getTotalNumPreprocessedEntities();
6013}
6014
6015namespace {
6016
Guy Benyei11169dd2012-12-18 14:30:41 +00006017struct PPEntityComp {
6018 const ASTReader &Reader;
6019 ModuleFile &M;
6020
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006021 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
Guy Benyei11169dd2012-12-18 14:30:41 +00006022
6023 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6024 SourceLocation LHS = getLoc(L);
6025 SourceLocation RHS = getLoc(R);
6026 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6027 }
6028
6029 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6030 SourceLocation LHS = getLoc(L);
6031 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6032 }
6033
6034 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6035 SourceLocation RHS = getLoc(R);
6036 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6037 }
6038
6039 SourceLocation getLoc(const PPEntityOffset &PPE) const {
Richard Smithb22a1d12016-03-27 20:13:24 +00006040 return Reader.TranslateSourceLocation(M, PPE.getBegin());
Guy Benyei11169dd2012-12-18 14:30:41 +00006041 }
6042};
6043
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006044} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00006045
Alp Toker2e9ce4c2014-05-16 18:59:21 +00006046PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6047 bool EndsAfter) const {
6048 if (SourceMgr.isLocalSourceLocation(Loc))
Guy Benyei11169dd2012-12-18 14:30:41 +00006049 return getTotalNumPreprocessedEntities();
6050
Alp Toker2e9ce4c2014-05-16 18:59:21 +00006051 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6052 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00006053 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6054 "Corrupted global sloc offset map");
6055
6056 if (SLocMapI->second->NumPreprocessedEntities == 0)
6057 return findNextPreprocessedEntity(SLocMapI);
6058
6059 ModuleFile &M = *SLocMapI->second;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006060
6061 using pp_iterator = const PPEntityOffset *;
6062
Guy Benyei11169dd2012-12-18 14:30:41 +00006063 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6064 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6065
6066 size_t Count = M.NumPreprocessedEntities;
6067 size_t Half;
6068 pp_iterator First = pp_begin;
6069 pp_iterator PPI;
6070
Alp Toker2e9ce4c2014-05-16 18:59:21 +00006071 if (EndsAfter) {
6072 PPI = std::upper_bound(pp_begin, pp_end, Loc,
Richard Smithb22a1d12016-03-27 20:13:24 +00006073 PPEntityComp(*this, M));
Alp Toker2e9ce4c2014-05-16 18:59:21 +00006074 } else {
6075 // Do a binary search manually instead of using std::lower_bound because
6076 // The end locations of entities may be unordered (when a macro expansion
6077 // is inside another macro argument), but for this case it is not important
6078 // whether we get the first macro expansion or its containing macro.
6079 while (Count > 0) {
6080 Half = Count / 2;
6081 PPI = First;
6082 std::advance(PPI, Half);
Richard Smithb22a1d12016-03-27 20:13:24 +00006083 if (SourceMgr.isBeforeInTranslationUnit(
6084 TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
Alp Toker2e9ce4c2014-05-16 18:59:21 +00006085 First = PPI;
6086 ++First;
6087 Count = Count - Half - 1;
6088 } else
6089 Count = Half;
6090 }
Guy Benyei11169dd2012-12-18 14:30:41 +00006091 }
6092
6093 if (PPI == pp_end)
6094 return findNextPreprocessedEntity(SLocMapI);
6095
6096 return M.BasePreprocessedEntityID + (PPI - pp_begin);
6097}
6098
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006099/// Returns a pair of [Begin, End) indices of preallocated
Guy Benyei11169dd2012-12-18 14:30:41 +00006100/// preprocessed entities that \arg Range encompasses.
6101std::pair<unsigned, unsigned>
6102 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6103 if (Range.isInvalid())
6104 return std::make_pair(0,0);
6105 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6106
Alp Toker2e9ce4c2014-05-16 18:59:21 +00006107 PreprocessedEntityID BeginID =
6108 findPreprocessedEntity(Range.getBegin(), false);
6109 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
Guy Benyei11169dd2012-12-18 14:30:41 +00006110 return std::make_pair(BeginID, EndID);
6111}
6112
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006113/// Optionally returns true or false if the preallocated preprocessed
Guy Benyei11169dd2012-12-18 14:30:41 +00006114/// entity with index \arg Index came from file \arg FID.
David Blaikie05785d12013-02-20 22:23:23 +00006115Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei11169dd2012-12-18 14:30:41 +00006116 FileID FID) {
6117 if (FID.isInvalid())
6118 return false;
6119
6120 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6121 ModuleFile &M = *PPInfo.first;
6122 unsigned LocalIndex = PPInfo.second;
6123 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006124
Richard Smithcb34bd32016-03-27 07:28:06 +00006125 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
Guy Benyei11169dd2012-12-18 14:30:41 +00006126 if (Loc.isInvalid())
6127 return false;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006128
Guy Benyei11169dd2012-12-18 14:30:41 +00006129 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6130 return true;
6131 else
6132 return false;
6133}
6134
6135namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006136
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006137 /// Visitor used to search for information about a header file.
Guy Benyei11169dd2012-12-18 14:30:41 +00006138 class HeaderFileInfoVisitor {
Guy Benyei11169dd2012-12-18 14:30:41 +00006139 const FileEntry *FE;
David Blaikie05785d12013-02-20 22:23:23 +00006140 Optional<HeaderFileInfo> HFI;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006141
Guy Benyei11169dd2012-12-18 14:30:41 +00006142 public:
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006143 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00006144
6145 bool operator()(ModuleFile &M) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00006146 HeaderFileInfoLookupTable *Table
6147 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
Guy Benyei11169dd2012-12-18 14:30:41 +00006148 if (!Table)
6149 return false;
6150
6151 // Look in the on-disk hash table for an entry for this file name.
Richard Smithbdf2d932015-07-30 03:37:16 +00006152 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00006153 if (Pos == Table->end())
6154 return false;
6155
Richard Smithbdf2d932015-07-30 03:37:16 +00006156 HFI = *Pos;
Guy Benyei11169dd2012-12-18 14:30:41 +00006157 return true;
6158 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006159
David Blaikie05785d12013-02-20 22:23:23 +00006160 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei11169dd2012-12-18 14:30:41 +00006161 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006162
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006163} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00006164
6165HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00006166 HeaderFileInfoVisitor Visitor(FE);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00006167 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis1054bbf2013-05-08 23:46:55 +00006168 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei11169dd2012-12-18 14:30:41 +00006169 return *HFI;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006170
Guy Benyei11169dd2012-12-18 14:30:41 +00006171 return HeaderFileInfo();
6172}
6173
6174void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Richard Smithd230de22017-01-26 01:01:01 +00006175 using DiagState = DiagnosticsEngine::DiagState;
6176 SmallVector<DiagState *, 32> DiagStates;
6177
Vedant Kumar48b4f762018-04-14 01:40:48 +00006178 for (ModuleFile &F : ModuleMgr) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006179 unsigned Idx = 0;
Richard Smithd230de22017-01-26 01:01:01 +00006180 auto &Record = F.PragmaDiagMappings;
6181 if (Record.empty())
6182 continue;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006183
Richard Smithd230de22017-01-26 01:01:01 +00006184 DiagStates.clear();
6185
6186 auto ReadDiagState =
6187 [&](const DiagState &BasedOn, SourceLocation Loc,
6188 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6189 unsigned BackrefID = Record[Idx++];
6190 if (BackrefID != 0)
6191 return DiagStates[BackrefID - 1];
6192
Guy Benyei11169dd2012-12-18 14:30:41 +00006193 // A new DiagState was created here.
Richard Smithd230de22017-01-26 01:01:01 +00006194 Diag.DiagStates.push_back(BasedOn);
6195 DiagState *NewState = &Diag.DiagStates.back();
Guy Benyei11169dd2012-12-18 14:30:41 +00006196 DiagStates.push_back(NewState);
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00006197 unsigned Size = Record[Idx++];
6198 assert(Idx + Size * 2 <= Record.size() &&
6199 "Invalid data, not enough diag/map pairs");
6200 while (Size--) {
Richard Smithd230de22017-01-26 01:01:01 +00006201 unsigned DiagID = Record[Idx++];
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006202 DiagnosticMapping NewMapping =
Richard Smithe37391c2017-05-03 00:28:49 +00006203 DiagnosticMapping::deserialize(Record[Idx++]);
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006204 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6205 continue;
6206
6207 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6208
6209 // If this mapping was specified as a warning but the severity was
6210 // upgraded due to diagnostic settings, simulate the current diagnostic
6211 // settings (and use a warning).
Richard Smithe37391c2017-05-03 00:28:49 +00006212 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6213 NewMapping.setSeverity(diag::Severity::Warning);
6214 NewMapping.setUpgradedFromWarning(false);
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006215 }
6216
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006217 Mapping = NewMapping;
Richard Smithd230de22017-01-26 01:01:01 +00006218 }
Richard Smithd230de22017-01-26 01:01:01 +00006219 return NewState;
6220 };
6221
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00006222 // Read the first state.
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006223 DiagState *FirstState;
6224 if (F.Kind == MK_ImplicitModule) {
6225 // Implicitly-built modules are reused with different diagnostic
6226 // settings. Use the initial diagnostic state from Diag to simulate this
6227 // compilation's diagnostic settings.
6228 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6229 DiagStates.push_back(FirstState);
6230
6231 // Skip the initial diagnostic state from the serialized module.
Richard Smithe37391c2017-05-03 00:28:49 +00006232 assert(Record[1] == 0 &&
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006233 "Invalid data, unexpected backref in initial state");
Richard Smithe37391c2017-05-03 00:28:49 +00006234 Idx = 3 + Record[2] * 2;
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006235 assert(Idx < Record.size() &&
6236 "Invalid data, not enough state change pairs in initial state");
Richard Smithe37391c2017-05-03 00:28:49 +00006237 } else if (F.isModule()) {
6238 // For an explicit module, preserve the flags from the module build
6239 // command line (-w, -Weverything, -Werror, ...) along with any explicit
6240 // -Wblah flags.
6241 unsigned Flags = Record[Idx++];
6242 DiagState Initial;
6243 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6244 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6245 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6246 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6247 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6248 Initial.ExtBehavior = (diag::Severity)Flags;
6249 FirstState = ReadDiagState(Initial, SourceLocation(), true);
Richard Smithea741482017-05-01 22:10:47 +00006250
Richard Smith6c2b5a82018-02-09 01:15:13 +00006251 assert(F.OriginalSourceFileID.isValid());
6252
Richard Smithe37391c2017-05-03 00:28:49 +00006253 // Set up the root buffer of the module to start with the initial
6254 // diagnostic state of the module itself, to cover files that contain no
6255 // explicit transitions (for which we did not serialize anything).
6256 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6257 .StateTransitions.push_back({FirstState, 0});
6258 } else {
6259 // For prefix ASTs, start with whatever the user configured on the
6260 // command line.
6261 Idx++; // Skip flags.
6262 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6263 SourceLocation(), false);
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006264 }
Richard Smithd230de22017-01-26 01:01:01 +00006265
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00006266 // Read the state transitions.
6267 unsigned NumLocations = Record[Idx++];
6268 while (NumLocations--) {
6269 assert(Idx < Record.size() &&
6270 "Invalid data, missing pragma diagnostic states");
Richard Smithd230de22017-01-26 01:01:01 +00006271 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6272 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
Richard Smith6c2b5a82018-02-09 01:15:13 +00006273 assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
Richard Smithd230de22017-01-26 01:01:01 +00006274 assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6275 unsigned Transitions = Record[Idx++];
6276
6277 // Note that we don't need to set up Parent/ParentOffset here, because
6278 // we won't be changing the diagnostic state within imported FileIDs
6279 // (other than perhaps appending to the main source file, which has no
6280 // parent).
6281 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6282 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6283 for (unsigned I = 0; I != Transitions; ++I) {
6284 unsigned Offset = Record[Idx++];
6285 auto *State =
6286 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6287 F.StateTransitions.push_back({State, Offset});
Guy Benyei11169dd2012-12-18 14:30:41 +00006288 }
6289 }
Richard Smithd230de22017-01-26 01:01:01 +00006290
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00006291 // Read the final state.
6292 assert(Idx < Record.size() &&
6293 "Invalid data, missing final pragma diagnostic state");
6294 SourceLocation CurStateLoc =
6295 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6296 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6297
6298 if (!F.isModule()) {
6299 Diag.DiagStatesByLoc.CurDiagState = CurState;
6300 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6301
6302 // Preserve the property that the imaginary root file describes the
6303 // current state.
Simon Pilgrim22518632017-10-10 13:56:17 +00006304 FileID NullFile;
6305 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00006306 if (T.empty())
6307 T.push_back({CurState, 0});
6308 else
6309 T[0].State = CurState;
6310 }
6311
Richard Smithd230de22017-01-26 01:01:01 +00006312 // Don't try to read these mappings again.
6313 Record.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006314 }
6315}
6316
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006317/// Get the correct cursor and offset for loading a type.
Guy Benyei11169dd2012-12-18 14:30:41 +00006318ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6319 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6320 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6321 ModuleFile *M = I->second;
6322 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
6323}
6324
John McCalld505e572019-12-13 21:54:44 -05006325static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6326 switch (code) {
6327#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6328 case TYPE_##CODE_ID: return Type::CLASS_ID;
6329#include "clang/Serialization/TypeBitCodes.def"
6330 default: return llvm::None;
6331 }
John McCall3ce3d232019-12-13 03:37:23 -05006332}
6333
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006334/// Read and return the type with the given index..
Guy Benyei11169dd2012-12-18 14:30:41 +00006335///
6336/// The index is the type ID, shifted and minus the number of predefs. This
6337/// routine actually reads the record corresponding to the type at the given
6338/// location. It is a helper routine for GetType, which deals with reading type
6339/// IDs.
6340QualType ASTReader::readTypeRecord(unsigned Index) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00006341 assert(ContextObj && "reading type with no AST context");
6342 ASTContext &Context = *ContextObj;
Guy Benyei11169dd2012-12-18 14:30:41 +00006343 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00006344 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00006345
6346 // Keep track of where we are in the stream, then jump back there
6347 // after reading this type.
6348 SavedStreamPosition SavedPosition(DeclsCursor);
6349
6350 ReadingKindTracker ReadingKind(Read_Type, *this);
6351
6352 // Note that we are loading a type record.
6353 Deserializing AType(this);
6354
JF Bastien0e828952019-06-26 19:50:12 +00006355 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6356 Error(std::move(Err));
6357 return QualType();
6358 }
John McCall3ce3d232019-12-13 03:37:23 -05006359 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6360 if (!RawCode) {
6361 Error(RawCode.takeError());
JF Bastien0e828952019-06-26 19:50:12 +00006362 return QualType();
6363 }
JF Bastien0e828952019-06-26 19:50:12 +00006364
John McCall3ce3d232019-12-13 03:37:23 -05006365 ASTRecordReader Record(*this, *Loc.F);
6366 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6367 if (!Code) {
6368 Error(Code.takeError());
JF Bastien0e828952019-06-26 19:50:12 +00006369 return QualType();
6370 }
John McCalld505e572019-12-13 21:54:44 -05006371 if (Code.get() == TYPE_EXT_QUAL) {
6372 QualType baseType = Record.readQualType();
6373 Qualifiers quals = Record.readQualifiers();
6374 return Context.getQualifiedType(baseType, quals);
Guy Benyei11169dd2012-12-18 14:30:41 +00006375 }
6376
John McCalld505e572019-12-13 21:54:44 -05006377 auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6378 if (!maybeClass) {
6379 Error("Unexpected code for type");
6380 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006381 }
6382
John McCalld505e572019-12-13 21:54:44 -05006383 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6384 return TypeReader.read(*maybeClass);
Richard Smith564417a2014-03-20 21:47:22 +00006385}
6386
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006387namespace clang {
6388
6389class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
John McCall3ce3d232019-12-13 03:37:23 -05006390 ASTRecordReader &Reader;
Guy Benyei11169dd2012-12-18 14:30:41 +00006391
John McCall3ce3d232019-12-13 03:37:23 -05006392 SourceLocation readSourceLocation() {
6393 return Reader.readSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00006394 }
6395
6396 TypeSourceInfo *GetTypeSourceInfo() {
John McCall3ce3d232019-12-13 03:37:23 -05006397 return Reader.readTypeSourceInfo();
David L. Jonesbe1557a2016-12-21 00:17:49 +00006398 }
6399
6400 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
John McCall3ce3d232019-12-13 03:37:23 -05006401 return Reader.readNestedNameSpecifierLoc();
Guy Benyei11169dd2012-12-18 14:30:41 +00006402 }
6403
Richard Smithe43e2b32018-08-20 21:47:29 +00006404 Attr *ReadAttr() {
John McCall3ce3d232019-12-13 03:37:23 -05006405 return Reader.readAttr();
Richard Smithe43e2b32018-08-20 21:47:29 +00006406 }
6407
Guy Benyei11169dd2012-12-18 14:30:41 +00006408public:
John McCall3ce3d232019-12-13 03:37:23 -05006409 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
Guy Benyei11169dd2012-12-18 14:30:41 +00006410
6411 // We want compile-time assurance that we've enumerated all of
6412 // these, so unfortunately we have to declare them first, then
6413 // define them out-of-line.
6414#define ABSTRACT_TYPELOC(CLASS, PARENT)
6415#define TYPELOC(CLASS, PARENT) \
6416 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6417#include "clang/AST/TypeLocNodes.def"
6418
6419 void VisitFunctionTypeLoc(FunctionTypeLoc);
6420 void VisitArrayTypeLoc(ArrayTypeLoc);
6421};
6422
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006423} // namespace clang
6424
Guy Benyei11169dd2012-12-18 14:30:41 +00006425void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6426 // nothing to do
6427}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006428
Guy Benyei11169dd2012-12-18 14:30:41 +00006429void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006430 TL.setBuiltinLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006431 if (TL.needsExtraLocalData()) {
John McCall3ce3d232019-12-13 03:37:23 -05006432 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6433 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt()));
6434 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt()));
6435 TL.setModeAttr(Reader.readInt());
Guy Benyei11169dd2012-12-18 14:30:41 +00006436 }
6437}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006438
Guy Benyei11169dd2012-12-18 14:30:41 +00006439void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006440 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006441}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006442
Guy Benyei11169dd2012-12-18 14:30:41 +00006443void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006444 TL.setStarLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006445}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006446
Reid Kleckner8a365022013-06-24 17:51:48 +00006447void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6448 // nothing to do
6449}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006450
Reid Kleckner0503a872013-12-05 01:23:43 +00006451void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6452 // nothing to do
6453}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006454
Leonard Chanc72aaf62019-05-07 03:20:17 +00006455void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006456 TL.setExpansionLoc(readSourceLocation());
Leonard Chanc72aaf62019-05-07 03:20:17 +00006457}
6458
Guy Benyei11169dd2012-12-18 14:30:41 +00006459void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006460 TL.setCaretLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006461}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006462
Guy Benyei11169dd2012-12-18 14:30:41 +00006463void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006464 TL.setAmpLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006465}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006466
Guy Benyei11169dd2012-12-18 14:30:41 +00006467void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006468 TL.setAmpAmpLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006469}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006470
Guy Benyei11169dd2012-12-18 14:30:41 +00006471void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006472 TL.setStarLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006473 TL.setClassTInfo(GetTypeSourceInfo());
Guy Benyei11169dd2012-12-18 14:30:41 +00006474}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006475
Guy Benyei11169dd2012-12-18 14:30:41 +00006476void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006477 TL.setLBracketLoc(readSourceLocation());
6478 TL.setRBracketLoc(readSourceLocation());
6479 if (Reader.readBool())
6480 TL.setSizeExpr(Reader.readExpr());
Guy Benyei11169dd2012-12-18 14:30:41 +00006481 else
Craig Toppera13603a2014-05-22 05:54:18 +00006482 TL.setSizeExpr(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006483}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006484
Guy Benyei11169dd2012-12-18 14:30:41 +00006485void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6486 VisitArrayTypeLoc(TL);
6487}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006488
Guy Benyei11169dd2012-12-18 14:30:41 +00006489void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6490 VisitArrayTypeLoc(TL);
6491}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006492
Guy Benyei11169dd2012-12-18 14:30:41 +00006493void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6494 VisitArrayTypeLoc(TL);
6495}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006496
Guy Benyei11169dd2012-12-18 14:30:41 +00006497void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6498 DependentSizedArrayTypeLoc TL) {
6499 VisitArrayTypeLoc(TL);
6500}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006501
Andrew Gozillon572bbb02017-10-02 06:25:51 +00006502void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6503 DependentAddressSpaceTypeLoc TL) {
6504
John McCall3ce3d232019-12-13 03:37:23 -05006505 TL.setAttrNameLoc(readSourceLocation());
6506 TL.setAttrOperandParensRange(Reader.readSourceRange());
6507 TL.setAttrExprOperand(Reader.readExpr());
Andrew Gozillon572bbb02017-10-02 06:25:51 +00006508}
6509
Guy Benyei11169dd2012-12-18 14:30:41 +00006510void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6511 DependentSizedExtVectorTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006512 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006513}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006514
Guy Benyei11169dd2012-12-18 14:30:41 +00006515void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006516 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006517}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006518
Erich Keanef702b022018-07-13 19:46:04 +00006519void TypeLocReader::VisitDependentVectorTypeLoc(
6520 DependentVectorTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006521 TL.setNameLoc(readSourceLocation());
Erich Keanef702b022018-07-13 19:46:04 +00006522}
6523
Guy Benyei11169dd2012-12-18 14:30:41 +00006524void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006525 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006526}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006527
Guy Benyei11169dd2012-12-18 14:30:41 +00006528void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006529 TL.setLocalRangeBegin(readSourceLocation());
6530 TL.setLParenLoc(readSourceLocation());
6531 TL.setRParenLoc(readSourceLocation());
6532 TL.setExceptionSpecRange(Reader.readSourceRange());
6533 TL.setLocalRangeEnd(readSourceLocation());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00006534 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
John McCall3ce3d232019-12-13 03:37:23 -05006535 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
Guy Benyei11169dd2012-12-18 14:30:41 +00006536 }
6537}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006538
Guy Benyei11169dd2012-12-18 14:30:41 +00006539void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6540 VisitFunctionTypeLoc(TL);
6541}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006542
Guy Benyei11169dd2012-12-18 14:30:41 +00006543void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6544 VisitFunctionTypeLoc(TL);
6545}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006546
Guy Benyei11169dd2012-12-18 14:30:41 +00006547void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006548 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006549}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006550
Guy Benyei11169dd2012-12-18 14:30:41 +00006551void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006552 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006553}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006554
Guy Benyei11169dd2012-12-18 14:30:41 +00006555void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006556 TL.setTypeofLoc(readSourceLocation());
6557 TL.setLParenLoc(readSourceLocation());
6558 TL.setRParenLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006559}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006560
Guy Benyei11169dd2012-12-18 14:30:41 +00006561void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006562 TL.setTypeofLoc(readSourceLocation());
6563 TL.setLParenLoc(readSourceLocation());
6564 TL.setRParenLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006565 TL.setUnderlyingTInfo(GetTypeSourceInfo());
Guy Benyei11169dd2012-12-18 14:30:41 +00006566}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006567
Guy Benyei11169dd2012-12-18 14:30:41 +00006568void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006569 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006570}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006571
Guy Benyei11169dd2012-12-18 14:30:41 +00006572void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006573 TL.setKWLoc(readSourceLocation());
6574 TL.setLParenLoc(readSourceLocation());
6575 TL.setRParenLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006576 TL.setUnderlyingTInfo(GetTypeSourceInfo());
Guy Benyei11169dd2012-12-18 14:30:41 +00006577}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006578
Guy Benyei11169dd2012-12-18 14:30:41 +00006579void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006580 TL.setNameLoc(readSourceLocation());
Saar Razb481f022020-01-22 02:03:05 +02006581 if (Reader.readBool()) {
6582 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6583 TL.setTemplateKWLoc(readSourceLocation());
6584 TL.setConceptNameLoc(readSourceLocation());
6585 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6586 TL.setLAngleLoc(readSourceLocation());
6587 TL.setRAngleLoc(readSourceLocation());
6588 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6589 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6590 TL.getTypePtr()->getArg(i).getKind()));
6591 }
Guy Benyei11169dd2012-12-18 14:30:41 +00006592}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006593
Richard Smith600b5262017-01-26 20:40:47 +00006594void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6595 DeducedTemplateSpecializationTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006596 TL.setTemplateNameLoc(readSourceLocation());
Richard Smith600b5262017-01-26 20:40:47 +00006597}
6598
Guy Benyei11169dd2012-12-18 14:30:41 +00006599void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006600 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006601}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006602
Guy Benyei11169dd2012-12-18 14:30:41 +00006603void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006604 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006605}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006606
Guy Benyei11169dd2012-12-18 14:30:41 +00006607void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smithe43e2b32018-08-20 21:47:29 +00006608 TL.setAttr(ReadAttr());
Guy Benyei11169dd2012-12-18 14:30:41 +00006609}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006610
Guy Benyei11169dd2012-12-18 14:30:41 +00006611void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006612 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006613}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006614
Guy Benyei11169dd2012-12-18 14:30:41 +00006615void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6616 SubstTemplateTypeParmTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006617 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006618}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006619
Guy Benyei11169dd2012-12-18 14:30:41 +00006620void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6621 SubstTemplateTypeParmPackTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006622 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006623}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006624
Guy Benyei11169dd2012-12-18 14:30:41 +00006625void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6626 TemplateSpecializationTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006627 TL.setTemplateKeywordLoc(readSourceLocation());
6628 TL.setTemplateNameLoc(readSourceLocation());
6629 TL.setLAngleLoc(readSourceLocation());
6630 TL.setRAngleLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006631 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
David L. Jonesbe1557a2016-12-21 00:17:49 +00006632 TL.setArgLocInfo(
6633 i,
John McCall3ce3d232019-12-13 03:37:23 -05006634 Reader.readTemplateArgumentLocInfo(
6635 TL.getTypePtr()->getArg(i).getKind()));
Guy Benyei11169dd2012-12-18 14:30:41 +00006636}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006637
Guy Benyei11169dd2012-12-18 14:30:41 +00006638void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006639 TL.setLParenLoc(readSourceLocation());
6640 TL.setRParenLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006641}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006642
Guy Benyei11169dd2012-12-18 14:30:41 +00006643void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006644 TL.setElaboratedKeywordLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006645 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
Guy Benyei11169dd2012-12-18 14:30:41 +00006646}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006647
Guy Benyei11169dd2012-12-18 14:30:41 +00006648void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006649 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006650}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006651
Guy Benyei11169dd2012-12-18 14:30:41 +00006652void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006653 TL.setElaboratedKeywordLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006654 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
John McCall3ce3d232019-12-13 03:37:23 -05006655 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006656}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006657
Guy Benyei11169dd2012-12-18 14:30:41 +00006658void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6659 DependentTemplateSpecializationTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006660 TL.setElaboratedKeywordLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006661 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
John McCall3ce3d232019-12-13 03:37:23 -05006662 TL.setTemplateKeywordLoc(readSourceLocation());
6663 TL.setTemplateNameLoc(readSourceLocation());
6664 TL.setLAngleLoc(readSourceLocation());
6665 TL.setRAngleLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006666 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
David L. Jonesbe1557a2016-12-21 00:17:49 +00006667 TL.setArgLocInfo(
6668 I,
John McCall3ce3d232019-12-13 03:37:23 -05006669 Reader.readTemplateArgumentLocInfo(
6670 TL.getTypePtr()->getArg(I).getKind()));
Guy Benyei11169dd2012-12-18 14:30:41 +00006671}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006672
Guy Benyei11169dd2012-12-18 14:30:41 +00006673void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006674 TL.setEllipsisLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006675}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006676
Guy Benyei11169dd2012-12-18 14:30:41 +00006677void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006678 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006679}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006680
Manman Rene6be26c2016-09-13 17:25:08 +00006681void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6682 if (TL.getNumProtocols()) {
John McCall3ce3d232019-12-13 03:37:23 -05006683 TL.setProtocolLAngleLoc(readSourceLocation());
6684 TL.setProtocolRAngleLoc(readSourceLocation());
Manman Rene6be26c2016-09-13 17:25:08 +00006685 }
6686 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
John McCall3ce3d232019-12-13 03:37:23 -05006687 TL.setProtocolLoc(i, readSourceLocation());
Manman Rene6be26c2016-09-13 17:25:08 +00006688}
6689
Guy Benyei11169dd2012-12-18 14:30:41 +00006690void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006691 TL.setHasBaseTypeAsWritten(Reader.readBool());
6692 TL.setTypeArgsLAngleLoc(readSourceLocation());
6693 TL.setTypeArgsRAngleLoc(readSourceLocation());
Douglas Gregore9d95f12015-07-07 03:57:35 +00006694 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
David L. Jonesbe1557a2016-12-21 00:17:49 +00006695 TL.setTypeArgTInfo(i, GetTypeSourceInfo());
John McCall3ce3d232019-12-13 03:37:23 -05006696 TL.setProtocolLAngleLoc(readSourceLocation());
6697 TL.setProtocolRAngleLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006698 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
John McCall3ce3d232019-12-13 03:37:23 -05006699 TL.setProtocolLoc(i, readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006700}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006701
Guy Benyei11169dd2012-12-18 14:30:41 +00006702void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006703 TL.setStarLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006704}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006705
Guy Benyei11169dd2012-12-18 14:30:41 +00006706void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006707 TL.setKWLoc(readSourceLocation());
6708 TL.setLParenLoc(readSourceLocation());
6709 TL.setRParenLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006710}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006711
Xiuli Pan9c14e282016-01-09 12:53:17 +00006712void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006713 TL.setKWLoc(readSourceLocation());
Xiuli Pan9c14e282016-01-09 12:53:17 +00006714}
Guy Benyei11169dd2012-12-18 14:30:41 +00006715
John McCall3ce3d232019-12-13 03:37:23 -05006716void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6717 TypeLocReader TLR(*this);
Richard Smithc23d7342018-06-29 20:46:25 +00006718 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6719 TLR.Visit(TL);
6720}
6721
John McCall3ce3d232019-12-13 03:37:23 -05006722TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6723 QualType InfoTy = readType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006724 if (InfoTy.isNull())
Craig Toppera13603a2014-05-22 05:54:18 +00006725 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006726
6727 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
John McCall3ce3d232019-12-13 03:37:23 -05006728 readTypeLoc(TInfo->getTypeLoc());
Guy Benyei11169dd2012-12-18 14:30:41 +00006729 return TInfo;
6730}
6731
6732QualType ASTReader::GetType(TypeID ID) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00006733 assert(ContextObj && "reading type with no AST context");
6734 ASTContext &Context = *ContextObj;
6735
Guy Benyei11169dd2012-12-18 14:30:41 +00006736 unsigned FastQuals = ID & Qualifiers::FastMask;
6737 unsigned Index = ID >> Qualifiers::FastWidth;
6738
6739 if (Index < NUM_PREDEF_TYPE_IDS) {
6740 QualType T;
6741 switch ((PredefinedTypeIDs)Index) {
Alexey Baderbdf7c842015-09-15 12:18:29 +00006742 case PREDEF_TYPE_NULL_ID:
Vedant Kumar48b4f762018-04-14 01:40:48 +00006743 return QualType();
Alexey Baderbdf7c842015-09-15 12:18:29 +00006744 case PREDEF_TYPE_VOID_ID:
6745 T = Context.VoidTy;
6746 break;
6747 case PREDEF_TYPE_BOOL_ID:
6748 T = Context.BoolTy;
6749 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00006750 case PREDEF_TYPE_CHAR_U_ID:
6751 case PREDEF_TYPE_CHAR_S_ID:
6752 // FIXME: Check that the signedness of CharTy is correct!
6753 T = Context.CharTy;
6754 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006755 case PREDEF_TYPE_UCHAR_ID:
6756 T = Context.UnsignedCharTy;
6757 break;
6758 case PREDEF_TYPE_USHORT_ID:
6759 T = Context.UnsignedShortTy;
6760 break;
6761 case PREDEF_TYPE_UINT_ID:
6762 T = Context.UnsignedIntTy;
6763 break;
6764 case PREDEF_TYPE_ULONG_ID:
6765 T = Context.UnsignedLongTy;
6766 break;
6767 case PREDEF_TYPE_ULONGLONG_ID:
6768 T = Context.UnsignedLongLongTy;
6769 break;
6770 case PREDEF_TYPE_UINT128_ID:
6771 T = Context.UnsignedInt128Ty;
6772 break;
6773 case PREDEF_TYPE_SCHAR_ID:
6774 T = Context.SignedCharTy;
6775 break;
6776 case PREDEF_TYPE_WCHAR_ID:
6777 T = Context.WCharTy;
6778 break;
6779 case PREDEF_TYPE_SHORT_ID:
6780 T = Context.ShortTy;
6781 break;
6782 case PREDEF_TYPE_INT_ID:
6783 T = Context.IntTy;
6784 break;
6785 case PREDEF_TYPE_LONG_ID:
6786 T = Context.LongTy;
6787 break;
6788 case PREDEF_TYPE_LONGLONG_ID:
6789 T = Context.LongLongTy;
6790 break;
6791 case PREDEF_TYPE_INT128_ID:
6792 T = Context.Int128Ty;
6793 break;
6794 case PREDEF_TYPE_HALF_ID:
6795 T = Context.HalfTy;
6796 break;
6797 case PREDEF_TYPE_FLOAT_ID:
6798 T = Context.FloatTy;
6799 break;
6800 case PREDEF_TYPE_DOUBLE_ID:
6801 T = Context.DoubleTy;
6802 break;
6803 case PREDEF_TYPE_LONGDOUBLE_ID:
6804 T = Context.LongDoubleTy;
6805 break;
Leonard Chanf921d852018-06-04 16:07:52 +00006806 case PREDEF_TYPE_SHORT_ACCUM_ID:
6807 T = Context.ShortAccumTy;
6808 break;
6809 case PREDEF_TYPE_ACCUM_ID:
6810 T = Context.AccumTy;
6811 break;
6812 case PREDEF_TYPE_LONG_ACCUM_ID:
6813 T = Context.LongAccumTy;
6814 break;
6815 case PREDEF_TYPE_USHORT_ACCUM_ID:
6816 T = Context.UnsignedShortAccumTy;
6817 break;
6818 case PREDEF_TYPE_UACCUM_ID:
6819 T = Context.UnsignedAccumTy;
6820 break;
6821 case PREDEF_TYPE_ULONG_ACCUM_ID:
6822 T = Context.UnsignedLongAccumTy;
6823 break;
Leonard Chanab80f3c2018-06-14 14:53:51 +00006824 case PREDEF_TYPE_SHORT_FRACT_ID:
6825 T = Context.ShortFractTy;
6826 break;
6827 case PREDEF_TYPE_FRACT_ID:
6828 T = Context.FractTy;
6829 break;
6830 case PREDEF_TYPE_LONG_FRACT_ID:
6831 T = Context.LongFractTy;
6832 break;
6833 case PREDEF_TYPE_USHORT_FRACT_ID:
6834 T = Context.UnsignedShortFractTy;
6835 break;
6836 case PREDEF_TYPE_UFRACT_ID:
6837 T = Context.UnsignedFractTy;
6838 break;
6839 case PREDEF_TYPE_ULONG_FRACT_ID:
6840 T = Context.UnsignedLongFractTy;
6841 break;
6842 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6843 T = Context.SatShortAccumTy;
6844 break;
6845 case PREDEF_TYPE_SAT_ACCUM_ID:
6846 T = Context.SatAccumTy;
6847 break;
6848 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6849 T = Context.SatLongAccumTy;
6850 break;
6851 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6852 T = Context.SatUnsignedShortAccumTy;
6853 break;
6854 case PREDEF_TYPE_SAT_UACCUM_ID:
6855 T = Context.SatUnsignedAccumTy;
6856 break;
6857 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6858 T = Context.SatUnsignedLongAccumTy;
6859 break;
6860 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6861 T = Context.SatShortFractTy;
6862 break;
6863 case PREDEF_TYPE_SAT_FRACT_ID:
6864 T = Context.SatFractTy;
6865 break;
6866 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6867 T = Context.SatLongFractTy;
6868 break;
6869 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6870 T = Context.SatUnsignedShortFractTy;
6871 break;
6872 case PREDEF_TYPE_SAT_UFRACT_ID:
6873 T = Context.SatUnsignedFractTy;
6874 break;
6875 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6876 T = Context.SatUnsignedLongFractTy;
6877 break;
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00006878 case PREDEF_TYPE_FLOAT16_ID:
6879 T = Context.Float16Ty;
6880 break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00006881 case PREDEF_TYPE_FLOAT128_ID:
6882 T = Context.Float128Ty;
6883 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006884 case PREDEF_TYPE_OVERLOAD_ID:
6885 T = Context.OverloadTy;
6886 break;
6887 case PREDEF_TYPE_BOUND_MEMBER:
6888 T = Context.BoundMemberTy;
6889 break;
6890 case PREDEF_TYPE_PSEUDO_OBJECT:
6891 T = Context.PseudoObjectTy;
6892 break;
6893 case PREDEF_TYPE_DEPENDENT_ID:
6894 T = Context.DependentTy;
6895 break;
6896 case PREDEF_TYPE_UNKNOWN_ANY:
6897 T = Context.UnknownAnyTy;
6898 break;
6899 case PREDEF_TYPE_NULLPTR_ID:
6900 T = Context.NullPtrTy;
6901 break;
Richard Smith3a8244d2018-05-01 05:02:45 +00006902 case PREDEF_TYPE_CHAR8_ID:
6903 T = Context.Char8Ty;
6904 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006905 case PREDEF_TYPE_CHAR16_ID:
6906 T = Context.Char16Ty;
6907 break;
6908 case PREDEF_TYPE_CHAR32_ID:
6909 T = Context.Char32Ty;
6910 break;
6911 case PREDEF_TYPE_OBJC_ID:
6912 T = Context.ObjCBuiltinIdTy;
6913 break;
6914 case PREDEF_TYPE_OBJC_CLASS:
6915 T = Context.ObjCBuiltinClassTy;
6916 break;
6917 case PREDEF_TYPE_OBJC_SEL:
6918 T = Context.ObjCBuiltinSelTy;
6919 break;
Alexey Bader954ba212016-04-08 13:40:33 +00006920#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6921 case PREDEF_TYPE_##Id##_ID: \
6922 T = Context.SingletonId; \
Alexey Baderbdf7c842015-09-15 12:18:29 +00006923 break;
Alexey Baderb62f1442016-04-13 08:33:41 +00006924#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +00006925#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6926 case PREDEF_TYPE_##Id##_ID: \
6927 T = Context.Id##Ty; \
6928 break;
6929#include "clang/Basic/OpenCLExtensionTypes.def"
Alexey Baderbdf7c842015-09-15 12:18:29 +00006930 case PREDEF_TYPE_SAMPLER_ID:
6931 T = Context.OCLSamplerTy;
6932 break;
6933 case PREDEF_TYPE_EVENT_ID:
6934 T = Context.OCLEventTy;
6935 break;
Alexey Bader9c8453f2015-09-15 11:18:52 +00006936 case PREDEF_TYPE_CLK_EVENT_ID:
6937 T = Context.OCLClkEventTy;
6938 break;
6939 case PREDEF_TYPE_QUEUE_ID:
6940 T = Context.OCLQueueTy;
6941 break;
Alexey Bader9c8453f2015-09-15 11:18:52 +00006942 case PREDEF_TYPE_RESERVE_ID_ID:
6943 T = Context.OCLReserveIDTy;
6944 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006945 case PREDEF_TYPE_AUTO_DEDUCT:
6946 T = Context.getAutoDeductType();
6947 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006948 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
6949 T = Context.getAutoRRefDeductType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006950 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00006951 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
6952 T = Context.ARCUnbridgedCastTy;
6953 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00006954 case PREDEF_TYPE_BUILTIN_FN:
6955 T = Context.BuiltinFnTy;
6956 break;
Alexey Bataev1a3320e2015-08-25 14:24:04 +00006957 case PREDEF_TYPE_OMP_ARRAY_SECTION:
6958 T = Context.OMPArraySectionTy;
6959 break;
Alexey Bataev7ac9efb2020-02-05 09:33:05 -05006960 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
6961 T = Context.OMPArraySectionTy;
6962 break;
Alexey Bataev13a15042020-04-01 15:06:38 -04006963 case PREDEF_TYPE_OMP_ITERATOR:
6964 T = Context.OMPIteratorTy;
6965 break;
Richard Sandifordeb485fb2019-08-09 08:52:54 +00006966#define SVE_TYPE(Name, Id, SingletonId) \
6967 case PREDEF_TYPE_##Id##_ID: \
6968 T = Context.SingletonId; \
6969 break;
6970#include "clang/Basic/AArch64SVEACLETypes.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00006971 }
6972
6973 assert(!T.isNull() && "Unknown predefined type");
6974 return T.withFastQualifiers(FastQuals);
6975 }
6976
6977 Index -= NUM_PREDEF_TYPE_IDS;
6978 assert(Index < TypesLoaded.size() && "Type index out-of-range");
6979 if (TypesLoaded[Index].isNull()) {
6980 TypesLoaded[Index] = readTypeRecord(Index);
6981 if (TypesLoaded[Index].isNull())
Vedant Kumar48b4f762018-04-14 01:40:48 +00006982 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006983
6984 TypesLoaded[Index]->setFromAST();
6985 if (DeserializationListener)
6986 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
6987 TypesLoaded[Index]);
6988 }
6989
6990 return TypesLoaded[Index].withFastQualifiers(FastQuals);
6991}
6992
6993QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
6994 return GetType(getGlobalTypeID(F, LocalID));
6995}
6996
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006997serialization::TypeID
Guy Benyei11169dd2012-12-18 14:30:41 +00006998ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
6999 unsigned FastQuals = LocalID & Qualifiers::FastMask;
7000 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007001
Guy Benyei11169dd2012-12-18 14:30:41 +00007002 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7003 return LocalID;
7004
Richard Smith37a93df2017-02-18 00:32:02 +00007005 if (!F.ModuleOffsetMap.empty())
7006 ReadModuleOffsetMap(F);
7007
Guy Benyei11169dd2012-12-18 14:30:41 +00007008 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7009 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7010 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007011
Guy Benyei11169dd2012-12-18 14:30:41 +00007012 unsigned GlobalIndex = LocalIndex + I->second;
7013 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7014}
7015
7016TemplateArgumentLocInfo
John McCall3ce3d232019-12-13 03:37:23 -05007017ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007018 switch (Kind) {
7019 case TemplateArgument::Expression:
John McCall3ce3d232019-12-13 03:37:23 -05007020 return readExpr();
Guy Benyei11169dd2012-12-18 14:30:41 +00007021 case TemplateArgument::Type:
John McCall3ce3d232019-12-13 03:37:23 -05007022 return readTypeSourceInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +00007023 case TemplateArgument::Template: {
John McCall3ce3d232019-12-13 03:37:23 -05007024 NestedNameSpecifierLoc QualifierLoc =
7025 readNestedNameSpecifierLoc();
7026 SourceLocation TemplateNameLoc = readSourceLocation();
Guy Benyei11169dd2012-12-18 14:30:41 +00007027 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7028 SourceLocation());
7029 }
7030 case TemplateArgument::TemplateExpansion: {
John McCall3ce3d232019-12-13 03:37:23 -05007031 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7032 SourceLocation TemplateNameLoc = readSourceLocation();
7033 SourceLocation EllipsisLoc = readSourceLocation();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007034 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Guy Benyei11169dd2012-12-18 14:30:41 +00007035 EllipsisLoc);
7036 }
7037 case TemplateArgument::Null:
7038 case TemplateArgument::Integral:
7039 case TemplateArgument::Declaration:
7040 case TemplateArgument::NullPtr:
7041 case TemplateArgument::Pack:
7042 // FIXME: Is this right?
Vedant Kumar48b4f762018-04-14 01:40:48 +00007043 return TemplateArgumentLocInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +00007044 }
7045 llvm_unreachable("unexpected template argument loc");
7046}
7047
John McCall3ce3d232019-12-13 03:37:23 -05007048TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7049 TemplateArgument Arg = readTemplateArgument();
Guy Benyei11169dd2012-12-18 14:30:41 +00007050
7051 if (Arg.getKind() == TemplateArgument::Expression) {
John McCall3ce3d232019-12-13 03:37:23 -05007052 if (readBool()) // bool InfoHasSameExpr.
Guy Benyei11169dd2012-12-18 14:30:41 +00007053 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7054 }
John McCall3ce3d232019-12-13 03:37:23 -05007055 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
Guy Benyei11169dd2012-12-18 14:30:41 +00007056}
7057
John McCall3ce3d232019-12-13 03:37:23 -05007058const ASTTemplateArgumentListInfo *
7059ASTRecordReader::readASTTemplateArgumentListInfo() {
7060 SourceLocation LAngleLoc = readSourceLocation();
7061 SourceLocation RAngleLoc = readSourceLocation();
7062 unsigned NumArgsAsWritten = readInt();
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00007063 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7064 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
John McCall3ce3d232019-12-13 03:37:23 -05007065 TemplArgsInfo.addArgument(readTemplateArgumentLoc());
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00007066 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7067}
7068
Guy Benyei11169dd2012-12-18 14:30:41 +00007069Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7070 return GetDecl(ID);
7071}
7072
Richard Smith053f6c62014-05-16 23:01:30 +00007073void ASTReader::CompleteRedeclChain(const Decl *D) {
Richard Smith851072e2014-05-19 20:59:20 +00007074 if (NumCurrentElementsDeserializing) {
7075 // We arrange to not care about the complete redeclaration chain while we're
7076 // deserializing. Just remember that the AST has marked this one as complete
7077 // but that it's not actually complete yet, so we know we still need to
7078 // complete it later.
7079 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7080 return;
7081 }
7082
Richard Smith053f6c62014-05-16 23:01:30 +00007083 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7084
Richard Smith053f6c62014-05-16 23:01:30 +00007085 // If this is a named declaration, complete it by looking it up
7086 // within its context.
7087 //
Richard Smith01bdb7a2014-08-28 05:44:07 +00007088 // FIXME: Merging a function definition should merge
Richard Smith053f6c62014-05-16 23:01:30 +00007089 // all mergeable entities within it.
7090 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7091 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7092 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
Richard Smitha534a312015-07-21 23:54:07 +00007093 if (!getContext().getLangOpts().CPlusPlus &&
7094 isa<TranslationUnitDecl>(DC)) {
Richard Smith053f6c62014-05-16 23:01:30 +00007095 // Outside of C++, we don't have a lookup table for the TU, so update
Richard Smitha534a312015-07-21 23:54:07 +00007096 // the identifier instead. (For C++ modules, we don't store decls
7097 // in the serialized identifier table, so we do the lookup in the TU.)
7098 auto *II = Name.getAsIdentifierInfo();
7099 assert(II && "non-identifier name in C?");
Richard Smith053f6c62014-05-16 23:01:30 +00007100 if (II->isOutOfDate())
7101 updateOutOfDateIdentifier(*II);
7102 } else
7103 DC->lookup(Name);
Richard Smith01bdb7a2014-08-28 05:44:07 +00007104 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
Richard Smith3cb15722015-08-05 22:41:45 +00007105 // Find all declarations of this kind from the relevant context.
7106 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7107 auto *DC = cast<DeclContext>(DCDecl);
7108 SmallVector<Decl*, 8> Decls;
7109 FindExternalLexicalDecls(
7110 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7111 }
Richard Smith053f6c62014-05-16 23:01:30 +00007112 }
7113 }
Richard Smith50895422015-01-31 03:04:55 +00007114
7115 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7116 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7117 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7118 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7119 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7120 if (auto *Template = FD->getPrimaryTemplate())
7121 Template->LoadLazySpecializations();
7122 }
Richard Smith053f6c62014-05-16 23:01:30 +00007123}
7124
Richard Smithc2bb8182015-03-24 06:36:48 +00007125CXXCtorInitializer **
7126ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7127 RecordLocation Loc = getLocalBitOffset(Offset);
7128 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7129 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00007130 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7131 Error(std::move(Err));
7132 return nullptr;
7133 }
Richard Smithc2bb8182015-03-24 06:36:48 +00007134 ReadingKindTracker ReadingKind(Read_Decl, *this);
7135
JF Bastien0e828952019-06-26 19:50:12 +00007136 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7137 if (!MaybeCode) {
7138 Error(MaybeCode.takeError());
7139 return nullptr;
7140 }
7141 unsigned Code = MaybeCode.get();
7142
John McCall3ce3d232019-12-13 03:37:23 -05007143 ASTRecordReader Record(*this, *Loc.F);
7144 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
JF Bastien0e828952019-06-26 19:50:12 +00007145 if (!MaybeRecCode) {
7146 Error(MaybeRecCode.takeError());
7147 return nullptr;
7148 }
7149 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
Richard Smithc2bb8182015-03-24 06:36:48 +00007150 Error("malformed AST file: missing C++ ctor initializers");
7151 return nullptr;
7152 }
7153
John McCall3ce3d232019-12-13 03:37:23 -05007154 return Record.readCXXCtorInitializers();
Richard Smithc2bb8182015-03-24 06:36:48 +00007155}
7156
Guy Benyei11169dd2012-12-18 14:30:41 +00007157CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00007158 assert(ContextObj && "reading base specifiers with no AST context");
7159 ASTContext &Context = *ContextObj;
7160
Guy Benyei11169dd2012-12-18 14:30:41 +00007161 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007162 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00007163 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00007164 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7165 Error(std::move(Err));
7166 return nullptr;
7167 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007168 ReadingKindTracker ReadingKind(Read_Decl, *this);
JF Bastien0e828952019-06-26 19:50:12 +00007169
7170 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7171 if (!MaybeCode) {
7172 Error(MaybeCode.takeError());
7173 return nullptr;
7174 }
7175 unsigned Code = MaybeCode.get();
7176
John McCall3ce3d232019-12-13 03:37:23 -05007177 ASTRecordReader Record(*this, *Loc.F);
7178 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
JF Bastien0e828952019-06-26 19:50:12 +00007179 if (!MaybeRecCode) {
7180 Error(MaybeCode.takeError());
7181 return nullptr;
7182 }
7183 unsigned RecCode = MaybeRecCode.get();
7184
Guy Benyei11169dd2012-12-18 14:30:41 +00007185 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00007186 Error("malformed AST file: missing C++ base specifiers");
Craig Toppera13603a2014-05-22 05:54:18 +00007187 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007188 }
7189
John McCall3ce3d232019-12-13 03:37:23 -05007190 unsigned NumBases = Record.readInt();
Guy Benyei11169dd2012-12-18 14:30:41 +00007191 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Vedant Kumar48b4f762018-04-14 01:40:48 +00007192 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
Guy Benyei11169dd2012-12-18 14:30:41 +00007193 for (unsigned I = 0; I != NumBases; ++I)
John McCall3ce3d232019-12-13 03:37:23 -05007194 Bases[I] = Record.readCXXBaseSpecifier();
Guy Benyei11169dd2012-12-18 14:30:41 +00007195 return Bases;
7196}
7197
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007198serialization::DeclID
Guy Benyei11169dd2012-12-18 14:30:41 +00007199ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7200 if (LocalID < NUM_PREDEF_DECL_IDS)
7201 return LocalID;
7202
Richard Smith37a93df2017-02-18 00:32:02 +00007203 if (!F.ModuleOffsetMap.empty())
7204 ReadModuleOffsetMap(F);
7205
Guy Benyei11169dd2012-12-18 14:30:41 +00007206 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7207 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7208 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007209
Guy Benyei11169dd2012-12-18 14:30:41 +00007210 return LocalID + I->second;
7211}
7212
7213bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7214 ModuleFile &M) const {
Richard Smithfe620d22015-03-05 23:24:12 +00007215 // Predefined decls aren't from any module.
7216 if (ID < NUM_PREDEF_DECL_IDS)
7217 return false;
7218
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007219 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
Richard Smithbcda1a92015-07-12 23:51:20 +00007220 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
Guy Benyei11169dd2012-12-18 14:30:41 +00007221}
7222
Douglas Gregor9f782892013-01-21 15:25:38 +00007223ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007224 if (!D->isFromASTFile())
Craig Toppera13603a2014-05-22 05:54:18 +00007225 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007226 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7227 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7228 return I->second;
7229}
7230
7231SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7232 if (ID < NUM_PREDEF_DECL_IDS)
Vedant Kumar48b4f762018-04-14 01:40:48 +00007233 return SourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00007234
Guy Benyei11169dd2012-12-18 14:30:41 +00007235 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7236
7237 if (Index > DeclsLoaded.size()) {
7238 Error("declaration ID out-of-range for AST file");
Vedant Kumar48b4f762018-04-14 01:40:48 +00007239 return SourceLocation();
Guy Benyei11169dd2012-12-18 14:30:41 +00007240 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00007241
Guy Benyei11169dd2012-12-18 14:30:41 +00007242 if (Decl *D = DeclsLoaded[Index])
7243 return D->getLocation();
7244
Richard Smithcb34bd32016-03-27 07:28:06 +00007245 SourceLocation Loc;
7246 DeclCursorForID(ID, Loc);
7247 return Loc;
Guy Benyei11169dd2012-12-18 14:30:41 +00007248}
7249
Richard Smithfe620d22015-03-05 23:24:12 +00007250static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7251 switch (ID) {
7252 case PREDEF_DECL_NULL_ID:
7253 return nullptr;
7254
7255 case PREDEF_DECL_TRANSLATION_UNIT_ID:
7256 return Context.getTranslationUnitDecl();
7257
7258 case PREDEF_DECL_OBJC_ID_ID:
7259 return Context.getObjCIdDecl();
7260
7261 case PREDEF_DECL_OBJC_SEL_ID:
7262 return Context.getObjCSelDecl();
7263
7264 case PREDEF_DECL_OBJC_CLASS_ID:
7265 return Context.getObjCClassDecl();
7266
7267 case PREDEF_DECL_OBJC_PROTOCOL_ID:
7268 return Context.getObjCProtocolDecl();
7269
7270 case PREDEF_DECL_INT_128_ID:
7271 return Context.getInt128Decl();
7272
7273 case PREDEF_DECL_UNSIGNED_INT_128_ID:
7274 return Context.getUInt128Decl();
7275
7276 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7277 return Context.getObjCInstanceTypeDecl();
7278
7279 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7280 return Context.getBuiltinVaListDecl();
Richard Smithf19e1272015-03-07 00:04:49 +00007281
Richard Smith9b88a4c2015-07-27 05:40:23 +00007282 case PREDEF_DECL_VA_LIST_TAG:
7283 return Context.getVaListTagDecl();
7284
Charles Davisc7d5c942015-09-17 20:55:33 +00007285 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7286 return Context.getBuiltinMSVaListDecl();
7287
Richard Smithf19e1272015-03-07 00:04:49 +00007288 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7289 return Context.getExternCContextDecl();
David Majnemerd9b1a4f2015-11-04 03:40:30 +00007290
7291 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7292 return Context.getMakeIntegerSeqDecl();
Quentin Colombet043406b2016-02-03 22:41:00 +00007293
7294 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7295 return Context.getCFConstantStringDecl();
Ben Langmuirf5416742016-02-04 00:55:24 +00007296
7297 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7298 return Context.getCFConstantStringTagDecl();
Eric Fiselier6ad68552016-07-01 01:24:09 +00007299
7300 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7301 return Context.getTypePackElementDecl();
Richard Smithfe620d22015-03-05 23:24:12 +00007302 }
Yaron Keren322bdad2015-03-06 07:49:14 +00007303 llvm_unreachable("PredefinedDeclIDs unknown enum value");
Richard Smithfe620d22015-03-05 23:24:12 +00007304}
7305
Richard Smithcd45dbc2014-04-19 03:48:30 +00007306Decl *ASTReader::GetExistingDecl(DeclID ID) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00007307 assert(ContextObj && "reading decl with no AST context");
Richard Smithcd45dbc2014-04-19 03:48:30 +00007308 if (ID < NUM_PREDEF_DECL_IDS) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00007309 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
Richard Smithfe620d22015-03-05 23:24:12 +00007310 if (D) {
7311 // Track that we have merged the declaration with ID \p ID into the
7312 // pre-existing predefined declaration \p D.
Richard Smith5fc18a92015-07-12 23:43:21 +00007313 auto &Merged = KeyDecls[D->getCanonicalDecl()];
Richard Smithfe620d22015-03-05 23:24:12 +00007314 if (Merged.empty())
7315 Merged.push_back(ID);
Guy Benyei11169dd2012-12-18 14:30:41 +00007316 }
Richard Smithfe620d22015-03-05 23:24:12 +00007317 return D;
Guy Benyei11169dd2012-12-18 14:30:41 +00007318 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00007319
Guy Benyei11169dd2012-12-18 14:30:41 +00007320 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7321
7322 if (Index >= DeclsLoaded.size()) {
7323 assert(0 && "declaration ID out-of-range for AST file");
7324 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007325 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007326 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00007327
7328 return DeclsLoaded[Index];
7329}
7330
7331Decl *ASTReader::GetDecl(DeclID ID) {
7332 if (ID < NUM_PREDEF_DECL_IDS)
7333 return GetExistingDecl(ID);
7334
7335 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7336
7337 if (Index >= DeclsLoaded.size()) {
7338 assert(0 && "declaration ID out-of-range for AST file");
7339 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007340 return nullptr;
Richard Smithcd45dbc2014-04-19 03:48:30 +00007341 }
7342
Guy Benyei11169dd2012-12-18 14:30:41 +00007343 if (!DeclsLoaded[Index]) {
7344 ReadDeclRecord(ID);
7345 if (DeserializationListener)
7346 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7347 }
7348
7349 return DeclsLoaded[Index];
7350}
7351
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007352DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
Guy Benyei11169dd2012-12-18 14:30:41 +00007353 DeclID GlobalID) {
7354 if (GlobalID < NUM_PREDEF_DECL_IDS)
7355 return GlobalID;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007356
Guy Benyei11169dd2012-12-18 14:30:41 +00007357 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7358 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7359 ModuleFile *Owner = I->second;
7360
7361 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7362 = M.GlobalToLocalDeclIDs.find(Owner);
7363 if (Pos == M.GlobalToLocalDeclIDs.end())
7364 return 0;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007365
Guy Benyei11169dd2012-12-18 14:30:41 +00007366 return GlobalID - Owner->BaseDeclID + Pos->second;
7367}
7368
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007369serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Guy Benyei11169dd2012-12-18 14:30:41 +00007370 const RecordData &Record,
7371 unsigned &Idx) {
7372 if (Idx >= Record.size()) {
7373 Error("Corrupted AST file");
7374 return 0;
7375 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007376
Guy Benyei11169dd2012-12-18 14:30:41 +00007377 return getGlobalDeclID(F, Record[Idx++]);
7378}
7379
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007380/// Resolve the offset of a statement into a statement.
Guy Benyei11169dd2012-12-18 14:30:41 +00007381///
7382/// This operation will read a new statement from the external
7383/// source each time it is called, and is meant to be used via a
7384/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7385Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7386 // Switch case IDs are per Decl.
7387 ClearSwitchCaseIDs();
7388
7389 // Offset here is a global offset across the entire chain.
7390 RecordLocation Loc = getLocalBitOffset(Offset);
JF Bastien0e828952019-06-26 19:50:12 +00007391 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7392 Error(std::move(Err));
7393 return nullptr;
7394 }
David Blaikie9fd16f82017-03-08 23:57:08 +00007395 assert(NumCurrentElementsDeserializing == 0 &&
7396 "should not be called while already deserializing");
7397 Deserializing D(this);
Guy Benyei11169dd2012-12-18 14:30:41 +00007398 return ReadStmtFromStream(*Loc.F);
7399}
7400
Richard Smith3cb15722015-08-05 22:41:45 +00007401void ASTReader::FindExternalLexicalDecls(
7402 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7403 SmallVectorImpl<Decl *> &Decls) {
Richard Smith82f8fcd2015-08-06 22:07:25 +00007404 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7405
Richard Smith9ccdd932015-08-06 22:14:12 +00007406 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
Richard Smith82f8fcd2015-08-06 22:07:25 +00007407 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7408 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7409 auto K = (Decl::Kind)+LexicalDecls[I];
7410 if (!IsKindWeWant(K))
7411 continue;
7412
7413 auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7414
7415 // Don't add predefined declarations to the lexical context more
7416 // than once.
7417 if (ID < NUM_PREDEF_DECL_IDS) {
7418 if (PredefsVisited[ID])
7419 continue;
7420
7421 PredefsVisited[ID] = true;
7422 }
7423
7424 if (Decl *D = GetLocalDecl(*M, ID)) {
Richard Smith2317a3e2015-08-11 21:21:20 +00007425 assert(D->getKind() == K && "wrong kind for lexical decl");
Richard Smith82f8fcd2015-08-06 22:07:25 +00007426 if (!DC->isDeclInLexicalTraversal(D))
7427 Decls.push_back(D);
7428 }
7429 }
7430 };
7431
7432 if (isa<TranslationUnitDecl>(DC)) {
7433 for (auto Lexical : TULexicalDecls)
7434 Visit(Lexical.first, Lexical.second);
7435 } else {
7436 auto I = LexicalDecls.find(DC);
7437 if (I != LexicalDecls.end())
Richard Smith9c9173d2015-08-11 22:00:24 +00007438 Visit(I->second.first, I->second.second);
Richard Smith82f8fcd2015-08-06 22:07:25 +00007439 }
7440
Guy Benyei11169dd2012-12-18 14:30:41 +00007441 ++NumLexicalDeclContextsRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00007442}
7443
7444namespace {
7445
7446class DeclIDComp {
7447 ASTReader &Reader;
7448 ModuleFile &Mod;
7449
7450public:
7451 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7452
7453 bool operator()(LocalDeclID L, LocalDeclID R) const {
7454 SourceLocation LHS = getLocation(L);
7455 SourceLocation RHS = getLocation(R);
7456 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7457 }
7458
7459 bool operator()(SourceLocation LHS, LocalDeclID R) const {
7460 SourceLocation RHS = getLocation(R);
7461 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7462 }
7463
7464 bool operator()(LocalDeclID L, SourceLocation RHS) const {
7465 SourceLocation LHS = getLocation(L);
7466 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7467 }
7468
7469 SourceLocation getLocation(LocalDeclID ID) const {
7470 return Reader.getSourceManager().getFileLoc(
7471 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7472 }
7473};
7474
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007475} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00007476
7477void ASTReader::FindFileRegionDecls(FileID File,
7478 unsigned Offset, unsigned Length,
7479 SmallVectorImpl<Decl *> &Decls) {
7480 SourceManager &SM = getSourceManager();
7481
7482 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7483 if (I == FileDeclIDs.end())
7484 return;
7485
7486 FileDeclsInfo &DInfo = I->second;
7487 if (DInfo.Decls.empty())
7488 return;
7489
7490 SourceLocation
7491 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7492 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7493
7494 DeclIDComp DIDComp(*this, *DInfo.Mod);
Fangrui Song7264a472019-07-03 08:13:17 +00007495 ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7496 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
Guy Benyei11169dd2012-12-18 14:30:41 +00007497 if (BeginIt != DInfo.Decls.begin())
7498 --BeginIt;
7499
7500 // If we are pointing at a top-level decl inside an objc container, we need
7501 // to backtrack until we find it otherwise we will fail to report that the
7502 // region overlaps with an objc container.
7503 while (BeginIt != DInfo.Decls.begin() &&
7504 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7505 ->isTopLevelDeclInObjCContainer())
7506 --BeginIt;
7507
Fangrui Song7264a472019-07-03 08:13:17 +00007508 ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7509 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
Guy Benyei11169dd2012-12-18 14:30:41 +00007510 if (EndIt != DInfo.Decls.end())
7511 ++EndIt;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007512
Guy Benyei11169dd2012-12-18 14:30:41 +00007513 for (ArrayRef<serialization::LocalDeclID>::iterator
7514 DIt = BeginIt; DIt != EndIt; ++DIt)
7515 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7516}
7517
Richard Smith9ce12e32013-02-07 03:30:24 +00007518bool
Guy Benyei11169dd2012-12-18 14:30:41 +00007519ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7520 DeclarationName Name) {
Richard Smithd88a7f12015-09-01 20:35:42 +00007521 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
Guy Benyei11169dd2012-12-18 14:30:41 +00007522 "DeclContext has no visible decls in storage");
7523 if (!Name)
Richard Smith9ce12e32013-02-07 03:30:24 +00007524 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00007525
Richard Smithd88a7f12015-09-01 20:35:42 +00007526 auto It = Lookups.find(DC);
7527 if (It == Lookups.end())
7528 return false;
7529
Richard Smith8c913ec2014-08-14 02:21:01 +00007530 Deserializing LookupResults(this);
7531
Richard Smithd88a7f12015-09-01 20:35:42 +00007532 // Load the list of declarations.
Guy Benyei11169dd2012-12-18 14:30:41 +00007533 SmallVector<NamedDecl *, 64> Decls;
Vedant Kumar48b4f762018-04-14 01:40:48 +00007534 for (DeclID ID : It->second.Table.find(Name)) {
7535 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
Richard Smithd88a7f12015-09-01 20:35:42 +00007536 if (ND->getDeclName() == Name)
7537 Decls.push_back(ND);
7538 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00007539
Guy Benyei11169dd2012-12-18 14:30:41 +00007540 ++NumVisibleDeclContextsRead;
7541 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith9ce12e32013-02-07 03:30:24 +00007542 return !Decls.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +00007543}
7544
Guy Benyei11169dd2012-12-18 14:30:41 +00007545void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7546 if (!DC->hasExternalVisibleStorage())
7547 return;
Richard Smithd88a7f12015-09-01 20:35:42 +00007548
7549 auto It = Lookups.find(DC);
7550 assert(It != Lookups.end() &&
7551 "have external visible storage but no lookup tables");
7552
Craig Topper79be4cd2013-07-05 04:33:53 +00007553 DeclsMap Decls;
Guy Benyei11169dd2012-12-18 14:30:41 +00007554
Vedant Kumar48b4f762018-04-14 01:40:48 +00007555 for (DeclID ID : It->second.Table.findAll()) {
7556 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
Richard Smithd88a7f12015-09-01 20:35:42 +00007557 Decls[ND->getDeclName()].push_back(ND);
Guy Benyei11169dd2012-12-18 14:30:41 +00007558 }
7559
Guy Benyei11169dd2012-12-18 14:30:41 +00007560 ++NumVisibleDeclContextsRead;
7561
Vedant Kumar48b4f762018-04-14 01:40:48 +00007562 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7563 SetExternalVisibleDeclsForName(DC, I->first, I->second);
7564 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007565 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7566}
7567
Richard Smithd88a7f12015-09-01 20:35:42 +00007568const serialization::reader::DeclContextLookupTable *
7569ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7570 auto I = Lookups.find(Primary);
7571 return I == Lookups.end() ? nullptr : &I->second;
7572}
7573
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007574/// Under non-PCH compilation the consumer receives the objc methods
Guy Benyei11169dd2012-12-18 14:30:41 +00007575/// before receiving the implementation, and codegen depends on this.
7576/// We simulate this by deserializing and passing to consumer the methods of the
7577/// implementation before passing the deserialized implementation decl.
7578static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7579 ASTConsumer *Consumer) {
7580 assert(ImplD && Consumer);
7581
Aaron Ballmanaff18c02014-03-13 19:03:34 +00007582 for (auto *I : ImplD->methods())
7583 Consumer->HandleInterestingDecl(DeclGroupRef(I));
Guy Benyei11169dd2012-12-18 14:30:41 +00007584
7585 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7586}
7587
Guy Benyei11169dd2012-12-18 14:30:41 +00007588void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00007589 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00007590 PassObjCImplDeclToConsumer(ImplD, Consumer);
7591 else
7592 Consumer->HandleInterestingDecl(DeclGroupRef(D));
7593}
7594
7595void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7596 this->Consumer = Consumer;
7597
Richard Smith9e2341d2015-03-23 03:25:59 +00007598 if (Consumer)
7599 PassInterestingDeclsToConsumer();
Richard Smith7f330cd2015-03-18 01:42:29 +00007600
7601 if (DeserializationListener)
7602 DeserializationListener->ReaderInitialized(this);
Guy Benyei11169dd2012-12-18 14:30:41 +00007603}
7604
7605void ASTReader::PrintStats() {
7606 std::fprintf(stderr, "*** AST File Statistics:\n");
7607
7608 unsigned NumTypesLoaded
7609 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7610 QualType());
7611 unsigned NumDeclsLoaded
7612 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00007613 (Decl *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00007614 unsigned NumIdentifiersLoaded
7615 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7616 IdentifiersLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00007617 (IdentifierInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00007618 unsigned NumMacrosLoaded
7619 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7620 MacrosLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00007621 (MacroInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00007622 unsigned NumSelectorsLoaded
7623 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7624 SelectorsLoaded.end(),
7625 Selector());
7626
7627 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7628 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
7629 NumSLocEntriesRead, TotalNumSLocEntries,
7630 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7631 if (!TypesLoaded.empty())
7632 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
7633 NumTypesLoaded, (unsigned)TypesLoaded.size(),
7634 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7635 if (!DeclsLoaded.empty())
7636 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
7637 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7638 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7639 if (!IdentifiersLoaded.empty())
7640 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
7641 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7642 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7643 if (!MacrosLoaded.empty())
7644 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
7645 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7646 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7647 if (!SelectorsLoaded.empty())
7648 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
7649 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7650 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7651 if (TotalNumStatements)
7652 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
7653 NumStatementsRead, TotalNumStatements,
7654 ((float)NumStatementsRead/TotalNumStatements * 100));
7655 if (TotalNumMacros)
7656 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
7657 NumMacrosRead, TotalNumMacros,
7658 ((float)NumMacrosRead/TotalNumMacros * 100));
7659 if (TotalLexicalDeclContexts)
7660 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
7661 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7662 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7663 * 100));
7664 if (TotalVisibleDeclContexts)
7665 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
7666 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7667 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7668 * 100));
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007669 if (TotalNumMethodPoolEntries)
Guy Benyei11169dd2012-12-18 14:30:41 +00007670 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
7671 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7672 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7673 * 100));
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007674 if (NumMethodPoolLookups)
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007675 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
7676 NumMethodPoolHits, NumMethodPoolLookups,
7677 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007678 if (NumMethodPoolTableLookups)
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007679 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
7680 NumMethodPoolTableHits, NumMethodPoolTableLookups,
7681 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7682 * 100.0));
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007683 if (NumIdentifierLookupHits)
Douglas Gregor00a50f72013-01-25 00:38:33 +00007684 std::fprintf(stderr,
7685 " %u / %u identifier table lookups succeeded (%f%%)\n",
7686 NumIdentifierLookupHits, NumIdentifierLookups,
7687 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
Douglas Gregor00a50f72013-01-25 00:38:33 +00007688
Douglas Gregore060e572013-01-25 01:03:03 +00007689 if (GlobalIndex) {
7690 std::fprintf(stderr, "\n");
7691 GlobalIndex->printStats();
7692 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007693
Guy Benyei11169dd2012-12-18 14:30:41 +00007694 std::fprintf(stderr, "\n");
7695 dump();
7696 std::fprintf(stderr, "\n");
7697}
7698
7699template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Vassil Vassilevb2710682017-03-02 18:13:19 +00007700LLVM_DUMP_METHOD static void
Guy Benyei11169dd2012-12-18 14:30:41 +00007701dumpModuleIDMap(StringRef Name,
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007702 const ContinuousRangeMap<Key, ModuleFile *,
Guy Benyei11169dd2012-12-18 14:30:41 +00007703 InitialCapacity> &Map) {
7704 if (Map.begin() == Map.end())
7705 return;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007706
Vedant Kumar48b4f762018-04-14 01:40:48 +00007707 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7708
Guy Benyei11169dd2012-12-18 14:30:41 +00007709 llvm::errs() << Name << ":\n";
Vedant Kumar48b4f762018-04-14 01:40:48 +00007710 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7711 I != IEnd; ++I) {
7712 llvm::errs() << " " << I->first << " -> " << I->second->FileName
7713 << "\n";
7714 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007715}
7716
Yaron Kerencdae9412016-01-29 19:38:18 +00007717LLVM_DUMP_METHOD void ASTReader::dump() {
Guy Benyei11169dd2012-12-18 14:30:41 +00007718 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7719 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7720 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7721 dumpModuleIDMap("Global type map", GlobalTypeMap);
7722 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7723 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7724 dumpModuleIDMap("Global macro map", GlobalMacroMap);
7725 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7726 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007727 dumpModuleIDMap("Global preprocessed entity map",
Guy Benyei11169dd2012-12-18 14:30:41 +00007728 GlobalPreprocessedEntityMap);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007729
Guy Benyei11169dd2012-12-18 14:30:41 +00007730 llvm::errs() << "\n*** PCH/Modules Loaded:";
Vedant Kumar48b4f762018-04-14 01:40:48 +00007731 for (ModuleFile &M : ModuleMgr)
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00007732 M.dump();
Guy Benyei11169dd2012-12-18 14:30:41 +00007733}
7734
7735/// Return the amount of memory used by memory buffers, breaking down
7736/// by heap-backed versus mmap'ed memory.
7737void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Vedant Kumar48b4f762018-04-14 01:40:48 +00007738 for (ModuleFile &I : ModuleMgr) {
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00007739 if (llvm::MemoryBuffer *buf = I.Buffer) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007740 size_t bytes = buf->getBufferSize();
7741 switch (buf->getBufferKind()) {
7742 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7743 sizes.malloc_bytes += bytes;
7744 break;
7745 case llvm::MemoryBuffer::MemoryBuffer_MMap:
7746 sizes.mmap_bytes += bytes;
7747 break;
7748 }
7749 }
7750 }
7751}
7752
7753void ASTReader::InitializeSema(Sema &S) {
7754 SemaObj = &S;
7755 S.addExternalSource(this);
7756
7757 // Makes sure any declarations that were deserialized "too early"
7758 // still get added to the identifier's declaration chains.
Vedant Kumar48b4f762018-04-14 01:40:48 +00007759 for (uint64_t ID : PreloadedDeclIDs) {
7760 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
Ben Langmuir5418f402014-09-10 21:29:41 +00007761 pushExternalDeclIntoScope(D, D->getDeclName());
Guy Benyei11169dd2012-12-18 14:30:41 +00007762 }
Ben Langmuir5418f402014-09-10 21:29:41 +00007763 PreloadedDeclIDs.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00007764
Richard Smith3d8e97e2013-10-18 06:54:39 +00007765 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00007766 if (!FPPragmaOptions.empty()) {
7767 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
Adam Nemet484aa452017-03-27 19:17:25 +00007768 SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007769 }
7770
Yaxun Liu5b746652016-12-18 05:18:55 +00007771 SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7772 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7773 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
Richard Smith3d8e97e2013-10-18 06:54:39 +00007774
7775 UpdateSema();
7776}
7777
7778void ASTReader::UpdateSema() {
7779 assert(SemaObj && "no Sema to update");
7780
7781 // Load the offsets of the declarations that Sema references.
7782 // They will be lazily deserialized when needed.
7783 if (!SemaDeclRefs.empty()) {
Richard Smith96269c52016-09-29 22:49:46 +00007784 assert(SemaDeclRefs.size() % 3 == 0);
7785 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
Richard Smith3d8e97e2013-10-18 06:54:39 +00007786 if (!SemaObj->StdNamespace)
7787 SemaObj->StdNamespace = SemaDeclRefs[I];
7788 if (!SemaObj->StdBadAlloc)
7789 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
Richard Smith96269c52016-09-29 22:49:46 +00007790 if (!SemaObj->StdAlignValT)
7791 SemaObj->StdAlignValT = SemaDeclRefs[I+2];
Richard Smith3d8e97e2013-10-18 06:54:39 +00007792 }
7793 SemaDeclRefs.clear();
7794 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00007795
Nico Weber779355f2016-03-02 23:22:00 +00007796 // Update the state of pragmas. Use the same API as if we had encountered the
7797 // pragma in the source.
Dario Domizioli13a0a382014-05-23 12:13:25 +00007798 if(OptimizeOffPragmaLocation.isValid())
Rui Ueyama49a3ad22019-07-16 04:46:31 +00007799 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
Nico Weber779355f2016-03-02 23:22:00 +00007800 if (PragmaMSStructState != -1)
7801 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
Nico Weber42932312016-03-03 00:17:35 +00007802 if (PointersToMembersPragmaLocation.isValid()) {
7803 SemaObj->ActOnPragmaMSPointersToMembers(
7804 (LangOptions::PragmaMSPointersToMembersKind)
7805 PragmaMSPointersToMembersState,
7806 PointersToMembersPragmaLocation);
7807 }
Justin Lebar67a78a62016-10-08 22:15:58 +00007808 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00007809
7810 if (PragmaPackCurrentValue) {
7811 // The bottom of the stack might have a default value. It must be adjusted
7812 // to the current value to ensure that the packing state is preserved after
7813 // popping entries that were included/imported from a PCH/module.
7814 bool DropFirst = false;
7815 if (!PragmaPackStack.empty() &&
7816 PragmaPackStack.front().Location.isInvalid()) {
7817 assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7818 "Expected a default alignment value");
7819 SemaObj->PackStack.Stack.emplace_back(
7820 PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
Alex Lorenz45b40142017-07-28 14:41:21 +00007821 SemaObj->PackStack.CurrentPragmaLocation,
7822 PragmaPackStack.front().PushLocation);
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00007823 DropFirst = true;
7824 }
7825 for (const auto &Entry :
7826 llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7827 SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
Alex Lorenz45b40142017-07-28 14:41:21 +00007828 Entry.Location, Entry.PushLocation);
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00007829 if (PragmaPackCurrentLocation.isInvalid()) {
7830 assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7831 "Expected a default alignment value");
7832 // Keep the current values.
7833 } else {
7834 SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7835 SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7836 }
7837 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007838}
7839
Richard Smitha8d5b6a2015-07-17 19:51:03 +00007840IdentifierInfo *ASTReader::get(StringRef Name) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007841 // Note that we are loading an identifier.
7842 Deserializing AnIdentifier(this);
Douglas Gregore060e572013-01-25 01:03:03 +00007843
Douglas Gregor7211ac12013-01-25 23:32:03 +00007844 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregor00a50f72013-01-25 00:38:33 +00007845 NumIdentifierLookups,
7846 NumIdentifierLookupHits);
Richard Smith33e0f7e2015-07-22 02:08:40 +00007847
7848 // We don't need to do identifier table lookups in C++ modules (we preload
7849 // all interesting declarations, and don't need to use the scope for name
7850 // lookups). Perform the lookup in PCH files, though, since we don't build
7851 // a complete initial identifier table if we're carrying on from a PCH.
Richard Smithdbafb6c2017-06-29 23:23:46 +00007852 if (PP.getLangOpts().CPlusPlus) {
Richard Smith33e0f7e2015-07-22 02:08:40 +00007853 for (auto F : ModuleMgr.pch_modules())
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007854 if (Visitor(*F))
Richard Smith33e0f7e2015-07-22 02:08:40 +00007855 break;
7856 } else {
7857 // If there is a global index, look there first to determine which modules
7858 // provably do not have any results for this identifier.
7859 GlobalModuleIndex::HitSet Hits;
7860 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7861 if (!loadGlobalIndex()) {
7862 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7863 HitsPtr = &Hits;
7864 }
7865 }
7866
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007867 ModuleMgr.visit(Visitor, HitsPtr);
Richard Smith33e0f7e2015-07-22 02:08:40 +00007868 }
7869
Guy Benyei11169dd2012-12-18 14:30:41 +00007870 IdentifierInfo *II = Visitor.getIdentifierInfo();
7871 markIdentifierUpToDate(II);
7872 return II;
7873}
7874
7875namespace clang {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00007876
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007877 /// An identifier-lookup iterator that enumerates all of the
Guy Benyei11169dd2012-12-18 14:30:41 +00007878 /// identifiers stored within a set of AST files.
7879 class ASTIdentifierIterator : public IdentifierIterator {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007880 /// The AST reader whose identifiers are being enumerated.
Guy Benyei11169dd2012-12-18 14:30:41 +00007881 const ASTReader &Reader;
7882
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007883 /// The current index into the chain of AST files stored in
Guy Benyei11169dd2012-12-18 14:30:41 +00007884 /// the AST reader.
7885 unsigned Index;
7886
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007887 /// The current position within the identifier lookup table
Guy Benyei11169dd2012-12-18 14:30:41 +00007888 /// of the current AST file.
7889 ASTIdentifierLookupTable::key_iterator Current;
7890
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007891 /// The end position within the identifier lookup table of
Guy Benyei11169dd2012-12-18 14:30:41 +00007892 /// the current AST file.
7893 ASTIdentifierLookupTable::key_iterator End;
7894
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007895 /// Whether to skip any modules in the ASTReader.
Ben Langmuir537c5b52016-05-04 00:53:13 +00007896 bool SkipModules;
7897
Guy Benyei11169dd2012-12-18 14:30:41 +00007898 public:
Ben Langmuir537c5b52016-05-04 00:53:13 +00007899 explicit ASTIdentifierIterator(const ASTReader &Reader,
7900 bool SkipModules = false);
Guy Benyei11169dd2012-12-18 14:30:41 +00007901
Craig Topper3e89dfe2014-03-13 02:13:41 +00007902 StringRef Next() override;
Guy Benyei11169dd2012-12-18 14:30:41 +00007903 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00007904
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007905} // namespace clang
Guy Benyei11169dd2012-12-18 14:30:41 +00007906
Ben Langmuir537c5b52016-05-04 00:53:13 +00007907ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
7908 bool SkipModules)
7909 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007910}
7911
7912StringRef ASTIdentifierIterator::Next() {
7913 while (Current == End) {
7914 // If we have exhausted all of our AST files, we're done.
7915 if (Index == 0)
Vedant Kumar48b4f762018-04-14 01:40:48 +00007916 return StringRef();
Guy Benyei11169dd2012-12-18 14:30:41 +00007917
7918 --Index;
Ben Langmuir537c5b52016-05-04 00:53:13 +00007919 ModuleFile &F = Reader.ModuleMgr[Index];
7920 if (SkipModules && F.isModule())
7921 continue;
7922
Vedant Kumar48b4f762018-04-14 01:40:48 +00007923 ASTIdentifierLookupTable *IdTable =
7924 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
Guy Benyei11169dd2012-12-18 14:30:41 +00007925 Current = IdTable->key_begin();
7926 End = IdTable->key_end();
7927 }
7928
7929 // We have any identifiers remaining in the current AST file; return
7930 // the next one.
Douglas Gregorbfd73d72013-01-23 18:53:14 +00007931 StringRef Result = *Current;
Guy Benyei11169dd2012-12-18 14:30:41 +00007932 ++Current;
Douglas Gregorbfd73d72013-01-23 18:53:14 +00007933 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00007934}
7935
Ben Langmuir537c5b52016-05-04 00:53:13 +00007936namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00007937
Ben Langmuir537c5b52016-05-04 00:53:13 +00007938/// A utility for appending two IdentifierIterators.
7939class ChainedIdentifierIterator : public IdentifierIterator {
7940 std::unique_ptr<IdentifierIterator> Current;
7941 std::unique_ptr<IdentifierIterator> Queued;
7942
7943public:
7944 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
7945 std::unique_ptr<IdentifierIterator> Second)
7946 : Current(std::move(First)), Queued(std::move(Second)) {}
7947
7948 StringRef Next() override {
7949 if (!Current)
Vedant Kumar48b4f762018-04-14 01:40:48 +00007950 return StringRef();
Ben Langmuir537c5b52016-05-04 00:53:13 +00007951
7952 StringRef result = Current->Next();
7953 if (!result.empty())
7954 return result;
7955
7956 // Try the queued iterator, which may itself be empty.
7957 Current.reset();
7958 std::swap(Current, Queued);
7959 return Next();
7960 }
7961};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00007962
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007963} // namespace
Ben Langmuir537c5b52016-05-04 00:53:13 +00007964
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00007965IdentifierIterator *ASTReader::getIdentifiers() {
Ben Langmuir537c5b52016-05-04 00:53:13 +00007966 if (!loadGlobalIndex()) {
7967 std::unique_ptr<IdentifierIterator> ReaderIter(
7968 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
7969 std::unique_ptr<IdentifierIterator> ModulesIter(
7970 GlobalIndex->createIdentifierIterator());
7971 return new ChainedIdentifierIterator(std::move(ReaderIter),
7972 std::move(ModulesIter));
7973 }
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00007974
Guy Benyei11169dd2012-12-18 14:30:41 +00007975 return new ASTIdentifierIterator(*this);
7976}
7977
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00007978namespace clang {
7979namespace serialization {
7980
Guy Benyei11169dd2012-12-18 14:30:41 +00007981 class ReadMethodPoolVisitor {
7982 ASTReader &Reader;
7983 Selector Sel;
7984 unsigned PriorGeneration;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007985 unsigned InstanceBits = 0;
7986 unsigned FactoryBits = 0;
7987 bool InstanceHasMoreThanOneDecl = false;
7988 bool FactoryHasMoreThanOneDecl = false;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00007989 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
7990 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00007991
7992 public:
Nico Weber2e0c8f72014-12-27 03:58:08 +00007993 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
Guy Benyei11169dd2012-12-18 14:30:41 +00007994 unsigned PriorGeneration)
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007995 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
Nico Weber2e0c8f72014-12-27 03:58:08 +00007996
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007997 bool operator()(ModuleFile &M) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007998 if (!M.SelectorLookupTable)
7999 return false;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008000
Guy Benyei11169dd2012-12-18 14:30:41 +00008001 // If we've already searched this module file, skip it now.
Richard Smithbdf2d932015-07-30 03:37:16 +00008002 if (M.Generation <= PriorGeneration)
Guy Benyei11169dd2012-12-18 14:30:41 +00008003 return true;
8004
Richard Smithbdf2d932015-07-30 03:37:16 +00008005 ++Reader.NumMethodPoolTableLookups;
Vedant Kumar48b4f762018-04-14 01:40:48 +00008006 ASTSelectorLookupTable *PoolTable
8007 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
Richard Smithbdf2d932015-07-30 03:37:16 +00008008 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Guy Benyei11169dd2012-12-18 14:30:41 +00008009 if (Pos == PoolTable->end())
8010 return false;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00008011
Richard Smithbdf2d932015-07-30 03:37:16 +00008012 ++Reader.NumMethodPoolTableHits;
8013 ++Reader.NumSelectorsRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00008014 // FIXME: Not quite happy with the statistics here. We probably should
8015 // disable this tracking when called via LoadSelector.
8016 // Also, should entries without methods count as misses?
Richard Smithbdf2d932015-07-30 03:37:16 +00008017 ++Reader.NumMethodPoolEntriesRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00008018 ASTSelectorLookupTrait::data_type Data = *Pos;
Richard Smithbdf2d932015-07-30 03:37:16 +00008019 if (Reader.DeserializationListener)
8020 Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00008021
Richard Smithbdf2d932015-07-30 03:37:16 +00008022 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8023 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8024 InstanceBits = Data.InstanceBits;
8025 FactoryBits = Data.FactoryBits;
8026 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8027 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00008028 return true;
8029 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008030
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008031 /// Retrieve the instance methods found by this visitor.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008032 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8033 return InstanceMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00008034 }
8035
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008036 /// Retrieve the instance methods found by this visitor.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008037 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
Guy Benyei11169dd2012-12-18 14:30:41 +00008038 return FactoryMethods;
8039 }
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00008040
8041 unsigned getInstanceBits() const { return InstanceBits; }
8042 unsigned getFactoryBits() const { return FactoryBits; }
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008043
Nico Weberff4b35e2014-12-27 22:14:15 +00008044 bool instanceHasMoreThanOneDecl() const {
8045 return InstanceHasMoreThanOneDecl;
8046 }
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008047
Nico Weberff4b35e2014-12-27 22:14:15 +00008048 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
Guy Benyei11169dd2012-12-18 14:30:41 +00008049 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00008050
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008051} // namespace serialization
8052} // namespace clang
Guy Benyei11169dd2012-12-18 14:30:41 +00008053
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008054/// Add the given set of methods to the method list.
Guy Benyei11169dd2012-12-18 14:30:41 +00008055static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8056 ObjCMethodList &List) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008057 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8058 S.addMethodToGlobalList(&List, Methods[I]);
8059 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008060}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008061
Guy Benyei11169dd2012-12-18 14:30:41 +00008062void ASTReader::ReadMethodPool(Selector Sel) {
8063 // Get the selector generation and update it to the current generation.
8064 unsigned &Generation = SelectorGeneration[Sel];
8065 unsigned PriorGeneration = Generation;
Richard Smith053f6c62014-05-16 23:01:30 +00008066 Generation = getGeneration();
Manman Rena0f31a02016-04-29 19:04:05 +00008067 SelectorOutOfDate[Sel] = false;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008068
Guy Benyei11169dd2012-12-18 14:30:41 +00008069 // Search for methods defined with this selector.
Douglas Gregorad2f7a52013-01-28 17:54:36 +00008070 ++NumMethodPoolLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00008071 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00008072 ModuleMgr.visit(Visitor);
8073
Guy Benyei11169dd2012-12-18 14:30:41 +00008074 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregorad2f7a52013-01-28 17:54:36 +00008075 Visitor.getFactoryMethods().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00008076 return;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00008077
8078 ++NumMethodPoolHits;
8079
Guy Benyei11169dd2012-12-18 14:30:41 +00008080 if (!getSema())
8081 return;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008082
Guy Benyei11169dd2012-12-18 14:30:41 +00008083 Sema &S = *getSema();
8084 Sema::GlobalMethodPool::iterator Pos
8085 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
Ben Langmuira0c32e92015-01-12 19:27:00 +00008086
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00008087 Pos->second.first.setBits(Visitor.getInstanceBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00008088 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00008089 Pos->second.second.setBits(Visitor.getFactoryBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00008090 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
Ben Langmuira0c32e92015-01-12 19:27:00 +00008091
8092 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8093 // when building a module we keep every method individually and may need to
8094 // update hasMoreThanOneDecl as we add the methods.
8095 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8096 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Guy Benyei11169dd2012-12-18 14:30:41 +00008097}
8098
Manman Rena0f31a02016-04-29 19:04:05 +00008099void ASTReader::updateOutOfDateSelector(Selector Sel) {
8100 if (SelectorOutOfDate[Sel])
8101 ReadMethodPool(Sel);
8102}
8103
Guy Benyei11169dd2012-12-18 14:30:41 +00008104void ASTReader::ReadKnownNamespaces(
8105 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8106 Namespaces.clear();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008107
Vedant Kumar48b4f762018-04-14 01:40:48 +00008108 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8109 if (NamespaceDecl *Namespace
8110 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
Guy Benyei11169dd2012-12-18 14:30:41 +00008111 Namespaces.push_back(Namespace);
Vedant Kumar48b4f762018-04-14 01:40:48 +00008112 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008113}
8114
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00008115void ASTReader::ReadUndefinedButUsed(
Richard Smithd6a04d72016-03-25 21:49:43 +00008116 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00008117 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008118 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky8334af82013-01-26 00:35:08 +00008119 SourceLocation Loc =
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00008120 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky8334af82013-01-26 00:35:08 +00008121 Undefined.insert(std::make_pair(D, Loc));
8122 }
8123}
Nick Lewycky8334af82013-01-26 00:35:08 +00008124
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00008125void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8126 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8127 Exprs) {
8128 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008129 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00008130 uint64_t Count = DelayedDeleteExprs[Idx++];
8131 for (uint64_t C = 0; C < Count; ++C) {
8132 SourceLocation DeleteLoc =
8133 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8134 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8135 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8136 }
8137 }
8138}
8139
Guy Benyei11169dd2012-12-18 14:30:41 +00008140void ASTReader::ReadTentativeDefinitions(
8141 SmallVectorImpl<VarDecl *> &TentativeDefs) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008142 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8143 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008144 if (Var)
8145 TentativeDefs.push_back(Var);
8146 }
8147 TentativeDefinitions.clear();
8148}
8149
8150void ASTReader::ReadUnusedFileScopedDecls(
8151 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008152 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8153 DeclaratorDecl *D
8154 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008155 if (D)
8156 Decls.push_back(D);
8157 }
8158 UnusedFileScopedDecls.clear();
8159}
8160
8161void ASTReader::ReadDelegatingConstructors(
8162 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008163 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8164 CXXConstructorDecl *D
8165 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008166 if (D)
8167 Decls.push_back(D);
8168 }
8169 DelegatingCtorDecls.clear();
8170}
8171
8172void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008173 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8174 TypedefNameDecl *D
8175 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008176 if (D)
8177 Decls.push_back(D);
8178 }
8179 ExtVectorDecls.clear();
8180}
8181
Nico Weber72889432014-09-06 01:25:55 +00008182void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8183 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008184 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8185 ++I) {
8186 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8187 GetDecl(UnusedLocalTypedefNameCandidates[I]));
Nico Weber72889432014-09-06 01:25:55 +00008188 if (D)
8189 Decls.insert(D);
8190 }
8191 UnusedLocalTypedefNameCandidates.clear();
8192}
8193
Yaxun (Sam) Liub670ab72020-02-26 10:57:39 -05008194void ASTReader::ReadDeclsToCheckForDeferredDiags(
8195 llvm::SmallVector<Decl *, 4> &Decls) {
8196 for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N;
8197 ++I) {
8198 auto *D = dyn_cast_or_null<Decl>(
8199 GetDecl(DeclsToCheckForDeferredDiags[I]));
8200 if (D)
8201 Decls.push_back(D);
8202 }
8203 DeclsToCheckForDeferredDiags.clear();
8204}
8205
8206
Guy Benyei11169dd2012-12-18 14:30:41 +00008207void ASTReader::ReadReferencedSelectors(
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008208 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008209 if (ReferencedSelectorsData.empty())
8210 return;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008211
Guy Benyei11169dd2012-12-18 14:30:41 +00008212 // If there are @selector references added them to its pool. This is for
8213 // implementation of -Wselector.
8214 unsigned int DataSize = ReferencedSelectorsData.size()-1;
8215 unsigned I = 0;
8216 while (I < DataSize) {
8217 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8218 SourceLocation SelLoc
8219 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8220 Sels.push_back(std::make_pair(Sel, SelLoc));
8221 }
8222 ReferencedSelectorsData.clear();
8223}
8224
8225void ASTReader::ReadWeakUndeclaredIdentifiers(
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008226 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008227 if (WeakUndeclaredIdentifiers.empty())
8228 return;
8229
8230 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008231 IdentifierInfo *WeakId
Guy Benyei11169dd2012-12-18 14:30:41 +00008232 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008233 IdentifierInfo *AliasId
Guy Benyei11169dd2012-12-18 14:30:41 +00008234 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8235 SourceLocation Loc
8236 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8237 bool Used = WeakUndeclaredIdentifiers[I++];
8238 WeakInfo WI(AliasId, Loc);
8239 WI.setUsed(Used);
8240 WeakIDs.push_back(std::make_pair(WeakId, WI));
8241 }
8242 WeakUndeclaredIdentifiers.clear();
8243}
8244
8245void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8246 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8247 ExternalVTableUse VT;
8248 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8249 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8250 VT.DefinitionRequired = VTableUses[Idx++];
8251 VTables.push_back(VT);
8252 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008253
Guy Benyei11169dd2012-12-18 14:30:41 +00008254 VTableUses.clear();
8255}
8256
8257void ASTReader::ReadPendingInstantiations(
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008258 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008259 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008260 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008261 SourceLocation Loc
8262 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8263
8264 Pending.push_back(std::make_pair(D, Loc));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008265 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008266 PendingInstantiations.clear();
8267}
8268
Richard Smithe40f2ba2013-08-07 21:41:30 +00008269void ASTReader::ReadLateParsedTemplates(
Justin Lebar28f09c52016-10-10 16:26:08 +00008270 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8271 &LPTMap) {
Richard Smithe40f2ba2013-08-07 21:41:30 +00008272 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8273 /* In loop */) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008274 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
Richard Smithe40f2ba2013-08-07 21:41:30 +00008275
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00008276 auto LT = std::make_unique<LateParsedTemplate>();
Richard Smithe40f2ba2013-08-07 21:41:30 +00008277 LT->D = GetDecl(LateParsedTemplates[Idx++]);
8278
8279 ModuleFile *F = getOwningModuleFile(LT->D);
8280 assert(F && "No module");
8281
8282 unsigned TokN = LateParsedTemplates[Idx++];
8283 LT->Toks.reserve(TokN);
8284 for (unsigned T = 0; T < TokN; ++T)
8285 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8286
Justin Lebar28f09c52016-10-10 16:26:08 +00008287 LPTMap.insert(std::make_pair(FD, std::move(LT)));
Richard Smithe40f2ba2013-08-07 21:41:30 +00008288 }
8289
8290 LateParsedTemplates.clear();
8291}
8292
Guy Benyei11169dd2012-12-18 14:30:41 +00008293void ASTReader::LoadSelector(Selector Sel) {
8294 // It would be complicated to avoid reading the methods anyway. So don't.
8295 ReadMethodPool(Sel);
8296}
8297
8298void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8299 assert(ID && "Non-zero identifier ID required");
8300 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8301 IdentifiersLoaded[ID - 1] = II;
8302 if (DeserializationListener)
8303 DeserializationListener->IdentifierRead(ID, II);
8304}
8305
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008306/// Set the globally-visible declarations associated with the given
Guy Benyei11169dd2012-12-18 14:30:41 +00008307/// identifier.
8308///
8309/// If the AST reader is currently in a state where the given declaration IDs
8310/// cannot safely be resolved, they are queued until it is safe to resolve
8311/// them.
8312///
8313/// \param II an IdentifierInfo that refers to one or more globally-visible
8314/// declarations.
8315///
8316/// \param DeclIDs the set of declaration IDs with the name @p II that are
8317/// visible at global scope.
8318///
Douglas Gregor6168bd22013-02-18 15:53:43 +00008319/// \param Decls if non-null, this vector will be populated with the set of
8320/// deserialized declarations. These declarations will not be pushed into
8321/// scope.
Guy Benyei11169dd2012-12-18 14:30:41 +00008322void
8323ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8324 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor6168bd22013-02-18 15:53:43 +00008325 SmallVectorImpl<Decl *> *Decls) {
8326 if (NumCurrentElementsDeserializing && !Decls) {
8327 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00008328 return;
8329 }
8330
Vedant Kumar48b4f762018-04-14 01:40:48 +00008331 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
Ben Langmuir5418f402014-09-10 21:29:41 +00008332 if (!SemaObj) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008333 // Queue this declaration so that it will be added to the
8334 // translation unit scope and identifier's declaration chain
8335 // once a Sema object is known.
Vedant Kumar48b4f762018-04-14 01:40:48 +00008336 PreloadedDeclIDs.push_back(DeclIDs[I]);
Ben Langmuir5418f402014-09-10 21:29:41 +00008337 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00008338 }
Ben Langmuir5418f402014-09-10 21:29:41 +00008339
Vedant Kumar48b4f762018-04-14 01:40:48 +00008340 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
Ben Langmuir5418f402014-09-10 21:29:41 +00008341
8342 // If we're simply supposed to record the declarations, do so now.
8343 if (Decls) {
8344 Decls->push_back(D);
8345 continue;
8346 }
8347
8348 // Introduce this declaration into the translation-unit scope
8349 // and add it to the declaration chain for this identifier, so
8350 // that (unqualified) name lookup will find it.
8351 pushExternalDeclIntoScope(D, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00008352 }
8353}
8354
Douglas Gregorc8a992f2013-01-21 16:52:34 +00008355IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008356 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00008357 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008358
8359 if (IdentifiersLoaded.empty()) {
8360 Error("no identifier table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00008361 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008362 }
8363
8364 ID -= 1;
8365 if (!IdentifiersLoaded[ID]) {
8366 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8367 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8368 ModuleFile *M = I->second;
8369 unsigned Index = ID - M->BaseIdentifierID;
8370 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8371
8372 // All of the strings in the AST file are preceded by a 16-bit length.
8373 // Extract that 16-bit length to avoid having to execute strlen().
8374 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8375 // unsigned integers. This is important to avoid integer overflow when
8376 // we cast them to 'unsigned'.
8377 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8378 unsigned StrLen = (((unsigned) StrLenPtr[0])
8379 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Richard Smitheb4b58f62016-02-05 01:40:54 +00008380 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8381 IdentifiersLoaded[ID] = &II;
8382 markIdentifierFromAST(*this, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00008383 if (DeserializationListener)
Richard Smitheb4b58f62016-02-05 01:40:54 +00008384 DeserializationListener->IdentifierRead(ID + 1, &II);
Guy Benyei11169dd2012-12-18 14:30:41 +00008385 }
8386
8387 return IdentifiersLoaded[ID];
8388}
8389
Douglas Gregorc8a992f2013-01-21 16:52:34 +00008390IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8391 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei11169dd2012-12-18 14:30:41 +00008392}
8393
8394IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8395 if (LocalID < NUM_PREDEF_IDENT_IDS)
8396 return LocalID;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008397
Richard Smith37a93df2017-02-18 00:32:02 +00008398 if (!M.ModuleOffsetMap.empty())
8399 ReadModuleOffsetMap(M);
8400
Guy Benyei11169dd2012-12-18 14:30:41 +00008401 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8402 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008403 assert(I != M.IdentifierRemap.end()
Guy Benyei11169dd2012-12-18 14:30:41 +00008404 && "Invalid index into identifier index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008405
Guy Benyei11169dd2012-12-18 14:30:41 +00008406 return LocalID + I->second;
8407}
8408
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008409MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008410 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00008411 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008412
8413 if (MacrosLoaded.empty()) {
8414 Error("no macro table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00008415 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008416 }
8417
8418 ID -= NUM_PREDEF_MACRO_IDS;
8419 if (!MacrosLoaded[ID]) {
8420 GlobalMacroMapType::iterator I
8421 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8422 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8423 ModuleFile *M = I->second;
8424 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008425 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008426
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008427 if (DeserializationListener)
8428 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8429 MacrosLoaded[ID]);
Guy Benyei11169dd2012-12-18 14:30:41 +00008430 }
8431
8432 return MacrosLoaded[ID];
8433}
8434
8435MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8436 if (LocalID < NUM_PREDEF_MACRO_IDS)
8437 return LocalID;
8438
Richard Smith37a93df2017-02-18 00:32:02 +00008439 if (!M.ModuleOffsetMap.empty())
8440 ReadModuleOffsetMap(M);
8441
Guy Benyei11169dd2012-12-18 14:30:41 +00008442 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8443 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8444 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8445
8446 return LocalID + I->second;
8447}
8448
8449serialization::SubmoduleID
8450ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8451 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8452 return LocalID;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008453
Richard Smith37a93df2017-02-18 00:32:02 +00008454 if (!M.ModuleOffsetMap.empty())
8455 ReadModuleOffsetMap(M);
8456
Guy Benyei11169dd2012-12-18 14:30:41 +00008457 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8458 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008459 assert(I != M.SubmoduleRemap.end()
Guy Benyei11169dd2012-12-18 14:30:41 +00008460 && "Invalid index into submodule index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008461
Guy Benyei11169dd2012-12-18 14:30:41 +00008462 return LocalID + I->second;
8463}
8464
8465Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8466 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8467 assert(GlobalID == 0 && "Unhandled global submodule ID");
Craig Toppera13603a2014-05-22 05:54:18 +00008468 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008469 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008470
Guy Benyei11169dd2012-12-18 14:30:41 +00008471 if (GlobalID > SubmodulesLoaded.size()) {
8472 Error("submodule ID out of range in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00008473 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008474 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008475
Guy Benyei11169dd2012-12-18 14:30:41 +00008476 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8477}
Douglas Gregorc147b0b2013-01-12 01:29:50 +00008478
8479Module *ASTReader::getModule(unsigned ID) {
8480 return getSubmodule(ID);
8481}
8482
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00008483bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8484 ModuleFile *MF = getOwningModuleFile(D);
8485 return MF && MF->PCHHasObjectFile;
8486}
8487
Richard Smithd88a7f12015-09-01 20:35:42 +00008488ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8489 if (ID & 1) {
8490 // It's a module, look it up by submodule ID.
8491 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8492 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8493 } else {
8494 // It's a prefix (preamble, PCH, ...). Look it up by index.
8495 unsigned IndexFromEnd = ID >> 1;
8496 assert(IndexFromEnd && "got reference to unknown module file");
8497 return getModuleManager().pch_modules().end()[-IndexFromEnd];
8498 }
8499}
8500
8501unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8502 if (!F)
8503 return 1;
8504
8505 // For a file representing a module, use the submodule ID of the top-level
8506 // module as the file ID. For any other kind of file, the number of such
8507 // files loaded beforehand will be the same on reload.
8508 // FIXME: Is this true even if we have an explicit module file and a PCH?
8509 if (F->isModule())
8510 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8511
8512 auto PCHModules = getModuleManager().pch_modules();
Fangrui Song75e74e02019-03-31 08:48:19 +00008513 auto I = llvm::find(PCHModules, F);
Richard Smithd88a7f12015-09-01 20:35:42 +00008514 assert(I != PCHModules.end() && "emitting reference to unknown file");
8515 return (I - PCHModules.end()) << 1;
8516}
8517
Reid Klecknerc915cb92020-02-27 18:13:54 -08008518llvm::Optional<ASTSourceDescriptor>
Adrian Prantl15bcf702015-06-30 17:39:43 +00008519ASTReader::getSourceDescriptor(unsigned ID) {
Adrian Prantlf4754ea2020-03-17 12:51:58 -07008520 if (Module *M = getSubmodule(ID))
Reid Klecknerc915cb92020-02-27 18:13:54 -08008521 return ASTSourceDescriptor(*M);
Adrian Prantl15bcf702015-06-30 17:39:43 +00008522
8523 // If there is only a single PCH, return it instead.
Hiroshi Inoue3170de02017-07-01 08:46:43 +00008524 // Chained PCH are not supported.
Saleem Abdulrasool97d25552017-03-02 17:37:11 +00008525 const auto &PCHChain = ModuleMgr.pch_modules();
8526 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
Adrian Prantl15bcf702015-06-30 17:39:43 +00008527 ModuleFile &MF = ModuleMgr.getPrimaryModule();
Adrian Prantl3a2d4942016-01-22 23:30:56 +00008528 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
Adrian Prantl9bc3c4f2016-04-27 17:06:22 +00008529 StringRef FileName = llvm::sys::path::filename(MF.FileName);
Reid Klecknerc915cb92020-02-27 18:13:54 -08008530 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8531 MF.Signature);
Adrian Prantl15bcf702015-06-30 17:39:43 +00008532 }
8533 return None;
8534}
8535
David Blaikie1ac9c982017-04-11 21:13:37 +00008536ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
Richard Smitha4653622017-09-06 20:01:14 +00008537 auto I = DefinitionSource.find(FD);
8538 if (I == DefinitionSource.end())
David Blaikie9ffe5a32017-01-30 05:00:26 +00008539 return EK_ReplyHazy;
David Blaikiee6b7c282017-04-11 20:46:34 +00008540 return I->second ? EK_Never : EK_Always;
David Blaikie9ffe5a32017-01-30 05:00:26 +00008541}
8542
Guy Benyei11169dd2012-12-18 14:30:41 +00008543Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8544 return DecodeSelector(getGlobalSelectorID(M, LocalID));
8545}
8546
8547Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8548 if (ID == 0)
Vedant Kumar48b4f762018-04-14 01:40:48 +00008549 return Selector();
Guy Benyei11169dd2012-12-18 14:30:41 +00008550
8551 if (ID > SelectorsLoaded.size()) {
8552 Error("selector ID out of range in AST file");
Vedant Kumar48b4f762018-04-14 01:40:48 +00008553 return Selector();
Guy Benyei11169dd2012-12-18 14:30:41 +00008554 }
8555
Craig Toppera13603a2014-05-22 05:54:18 +00008556 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008557 // Load this selector from the selector table.
8558 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8559 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8560 ModuleFile &M = *I->second;
8561 ASTSelectorLookupTrait Trait(*this, M);
8562 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8563 SelectorsLoaded[ID - 1] =
8564 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8565 if (DeserializationListener)
8566 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8567 }
8568
8569 return SelectorsLoaded[ID - 1];
8570}
8571
8572Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8573 return DecodeSelector(ID);
8574}
8575
8576uint32_t ASTReader::GetNumExternalSelectors() {
8577 // ID 0 (the null selector) is considered an external selector.
8578 return getTotalNumSelectors() + 1;
8579}
8580
8581serialization::SelectorID
8582ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8583 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8584 return LocalID;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008585
Richard Smith37a93df2017-02-18 00:32:02 +00008586 if (!M.ModuleOffsetMap.empty())
8587 ReadModuleOffsetMap(M);
8588
Guy Benyei11169dd2012-12-18 14:30:41 +00008589 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8590 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008591 assert(I != M.SelectorRemap.end()
Guy Benyei11169dd2012-12-18 14:30:41 +00008592 && "Invalid index into selector index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008593
Guy Benyei11169dd2012-12-18 14:30:41 +00008594 return LocalID + I->second;
8595}
8596
John McCall3ce3d232019-12-13 03:37:23 -05008597DeclarationNameLoc
8598ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8599 DeclarationNameLoc DNLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00008600 switch (Name.getNameKind()) {
8601 case DeclarationName::CXXConstructorName:
8602 case DeclarationName::CXXDestructorName:
8603 case DeclarationName::CXXConversionFunctionName:
John McCall3ce3d232019-12-13 03:37:23 -05008604 DNLoc.NamedType.TInfo = readTypeSourceInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +00008605 break;
8606
8607 case DeclarationName::CXXOperatorName:
8608 DNLoc.CXXOperatorName.BeginOpNameLoc
John McCall3ce3d232019-12-13 03:37:23 -05008609 = readSourceLocation().getRawEncoding();
Guy Benyei11169dd2012-12-18 14:30:41 +00008610 DNLoc.CXXOperatorName.EndOpNameLoc
John McCall3ce3d232019-12-13 03:37:23 -05008611 = readSourceLocation().getRawEncoding();
Guy Benyei11169dd2012-12-18 14:30:41 +00008612 break;
8613
8614 case DeclarationName::CXXLiteralOperatorName:
8615 DNLoc.CXXLiteralOperatorName.OpNameLoc
John McCall3ce3d232019-12-13 03:37:23 -05008616 = readSourceLocation().getRawEncoding();
Guy Benyei11169dd2012-12-18 14:30:41 +00008617 break;
8618
8619 case DeclarationName::Identifier:
8620 case DeclarationName::ObjCZeroArgSelector:
8621 case DeclarationName::ObjCOneArgSelector:
8622 case DeclarationName::ObjCMultiArgSelector:
8623 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00008624 case DeclarationName::CXXDeductionGuideName:
Guy Benyei11169dd2012-12-18 14:30:41 +00008625 break;
8626 }
John McCall3ce3d232019-12-13 03:37:23 -05008627 return DNLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00008628}
8629
John McCall3ce3d232019-12-13 03:37:23 -05008630DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8631 DeclarationNameInfo NameInfo;
8632 NameInfo.setName(readDeclarationName());
8633 NameInfo.setLoc(readSourceLocation());
8634 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8635 return NameInfo;
Guy Benyei11169dd2012-12-18 14:30:41 +00008636}
8637
John McCall3ce3d232019-12-13 03:37:23 -05008638void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8639 Info.QualifierLoc = readNestedNameSpecifierLoc();
8640 unsigned NumTPLists = readInt();
Guy Benyei11169dd2012-12-18 14:30:41 +00008641 Info.NumTemplParamLists = NumTPLists;
8642 if (NumTPLists) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00008643 Info.TemplParamLists =
8644 new (getContext()) TemplateParameterList *[NumTPLists];
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00008645 for (unsigned i = 0; i != NumTPLists; ++i)
John McCall3ce3d232019-12-13 03:37:23 -05008646 Info.TemplParamLists[i] = readTemplateParameterList();
Guy Benyei11169dd2012-12-18 14:30:41 +00008647 }
8648}
8649
Guy Benyei11169dd2012-12-18 14:30:41 +00008650TemplateParameterList *
John McCall3ce3d232019-12-13 03:37:23 -05008651ASTRecordReader::readTemplateParameterList() {
8652 SourceLocation TemplateLoc = readSourceLocation();
8653 SourceLocation LAngleLoc = readSourceLocation();
8654 SourceLocation RAngleLoc = readSourceLocation();
Guy Benyei11169dd2012-12-18 14:30:41 +00008655
John McCall3ce3d232019-12-13 03:37:23 -05008656 unsigned NumParams = readInt();
Guy Benyei11169dd2012-12-18 14:30:41 +00008657 SmallVector<NamedDecl *, 16> Params;
8658 Params.reserve(NumParams);
8659 while (NumParams--)
John McCall3ce3d232019-12-13 03:37:23 -05008660 Params.push_back(readDeclAs<NamedDecl>());
Guy Benyei11169dd2012-12-18 14:30:41 +00008661
John McCall3ce3d232019-12-13 03:37:23 -05008662 bool HasRequiresClause = readBool();
8663 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
Saar Raz0330fba2019-10-15 18:44:06 +00008664
Richard Smithdbafb6c2017-06-29 23:23:46 +00008665 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
Saar Raz0330fba2019-10-15 18:44:06 +00008666 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
Guy Benyei11169dd2012-12-18 14:30:41 +00008667 return TemplateParams;
8668}
8669
John McCall3ce3d232019-12-13 03:37:23 -05008670void ASTRecordReader::readTemplateArgumentList(
8671 SmallVectorImpl<TemplateArgument> &TemplArgs,
8672 bool Canonicalize) {
8673 unsigned NumTemplateArgs = readInt();
Guy Benyei11169dd2012-12-18 14:30:41 +00008674 TemplArgs.reserve(NumTemplateArgs);
8675 while (NumTemplateArgs--)
John McCall3ce3d232019-12-13 03:37:23 -05008676 TemplArgs.push_back(readTemplateArgument(Canonicalize));
Guy Benyei11169dd2012-12-18 14:30:41 +00008677}
8678
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008679/// Read a UnresolvedSet structure.
John McCall3ce3d232019-12-13 03:37:23 -05008680void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8681 unsigned NumDecls = readInt();
Richard Smithdbafb6c2017-06-29 23:23:46 +00008682 Set.reserve(getContext(), NumDecls);
Guy Benyei11169dd2012-12-18 14:30:41 +00008683 while (NumDecls--) {
John McCall3ce3d232019-12-13 03:37:23 -05008684 DeclID ID = readDeclID();
8685 AccessSpecifier AS = (AccessSpecifier) readInt();
Richard Smithdbafb6c2017-06-29 23:23:46 +00008686 Set.addLazyDecl(getContext(), ID, AS);
Guy Benyei11169dd2012-12-18 14:30:41 +00008687 }
8688}
8689
8690CXXBaseSpecifier
John McCall3ce3d232019-12-13 03:37:23 -05008691ASTRecordReader::readCXXBaseSpecifier() {
8692 bool isVirtual = readBool();
8693 bool isBaseOfClass = readBool();
8694 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8695 bool inheritConstructors = readBool();
8696 TypeSourceInfo *TInfo = readTypeSourceInfo();
8697 SourceRange Range = readSourceRange();
8698 SourceLocation EllipsisLoc = readSourceLocation();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008699 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Guy Benyei11169dd2012-12-18 14:30:41 +00008700 EllipsisLoc);
8701 Result.setInheritConstructors(inheritConstructors);
8702 return Result;
8703}
8704
Richard Smithc2bb8182015-03-24 06:36:48 +00008705CXXCtorInitializer **
John McCall3ce3d232019-12-13 03:37:23 -05008706ASTRecordReader::readCXXCtorInitializers() {
Richard Smithdbafb6c2017-06-29 23:23:46 +00008707 ASTContext &Context = getContext();
John McCall3ce3d232019-12-13 03:37:23 -05008708 unsigned NumInitializers = readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00008709 assert(NumInitializers && "wrote ctor initializers but have no inits");
Vedant Kumar48b4f762018-04-14 01:40:48 +00008710 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
Richard Smithc2bb8182015-03-24 06:36:48 +00008711 for (unsigned i = 0; i != NumInitializers; ++i) {
8712 TypeSourceInfo *TInfo = nullptr;
8713 bool IsBaseVirtual = false;
8714 FieldDecl *Member = nullptr;
8715 IndirectFieldDecl *IndirectMember = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008716
John McCall3ce3d232019-12-13 03:37:23 -05008717 CtorInitializerType Type = (CtorInitializerType) readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00008718 switch (Type) {
8719 case CTOR_INITIALIZER_BASE:
John McCall3ce3d232019-12-13 03:37:23 -05008720 TInfo = readTypeSourceInfo();
8721 IsBaseVirtual = readBool();
Richard Smithc2bb8182015-03-24 06:36:48 +00008722 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008723
Richard Smithc2bb8182015-03-24 06:36:48 +00008724 case CTOR_INITIALIZER_DELEGATING:
John McCall3ce3d232019-12-13 03:37:23 -05008725 TInfo = readTypeSourceInfo();
Richard Smithc2bb8182015-03-24 06:36:48 +00008726 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008727
Richard Smithc2bb8182015-03-24 06:36:48 +00008728 case CTOR_INITIALIZER_MEMBER:
John McCall3ce3d232019-12-13 03:37:23 -05008729 Member = readDeclAs<FieldDecl>();
Richard Smithc2bb8182015-03-24 06:36:48 +00008730 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008731
Richard Smithc2bb8182015-03-24 06:36:48 +00008732 case CTOR_INITIALIZER_INDIRECT_MEMBER:
John McCall3ce3d232019-12-13 03:37:23 -05008733 IndirectMember = readDeclAs<IndirectFieldDecl>();
Richard Smithc2bb8182015-03-24 06:36:48 +00008734 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008735 }
Richard Smithc2bb8182015-03-24 06:36:48 +00008736
John McCall3ce3d232019-12-13 03:37:23 -05008737 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8738 Expr *Init = readExpr();
8739 SourceLocation LParenLoc = readSourceLocation();
8740 SourceLocation RParenLoc = readSourceLocation();
Richard Smithc2bb8182015-03-24 06:36:48 +00008741
8742 CXXCtorInitializer *BOMInit;
Richard Smith30e304e2016-12-14 00:03:17 +00008743 if (Type == CTOR_INITIALIZER_BASE)
Richard Smithc2bb8182015-03-24 06:36:48 +00008744 BOMInit = new (Context)
8745 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8746 RParenLoc, MemberOrEllipsisLoc);
Richard Smith30e304e2016-12-14 00:03:17 +00008747 else if (Type == CTOR_INITIALIZER_DELEGATING)
Richard Smithc2bb8182015-03-24 06:36:48 +00008748 BOMInit = new (Context)
8749 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
Richard Smith30e304e2016-12-14 00:03:17 +00008750 else if (Member)
8751 BOMInit = new (Context)
8752 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8753 Init, RParenLoc);
8754 else
8755 BOMInit = new (Context)
8756 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8757 LParenLoc, Init, RParenLoc);
8758
John McCall3ce3d232019-12-13 03:37:23 -05008759 if (/*IsWritten*/readBool()) {
8760 unsigned SourceOrder = readInt();
Richard Smith30e304e2016-12-14 00:03:17 +00008761 BOMInit->setSourceOrder(SourceOrder);
Richard Smithc2bb8182015-03-24 06:36:48 +00008762 }
8763
Richard Smithc2bb8182015-03-24 06:36:48 +00008764 CtorInitializers[i] = BOMInit;
Guy Benyei11169dd2012-12-18 14:30:41 +00008765 }
8766
Richard Smithc2bb8182015-03-24 06:36:48 +00008767 return CtorInitializers;
Guy Benyei11169dd2012-12-18 14:30:41 +00008768}
8769
Guy Benyei11169dd2012-12-18 14:30:41 +00008770NestedNameSpecifierLoc
John McCall3ce3d232019-12-13 03:37:23 -05008771ASTRecordReader::readNestedNameSpecifierLoc() {
Richard Smithdbafb6c2017-06-29 23:23:46 +00008772 ASTContext &Context = getContext();
John McCall3ce3d232019-12-13 03:37:23 -05008773 unsigned N = readInt();
Guy Benyei11169dd2012-12-18 14:30:41 +00008774 NestedNameSpecifierLocBuilder Builder;
8775 for (unsigned I = 0; I != N; ++I) {
John McCalld505e572019-12-13 21:54:44 -05008776 auto Kind = readNestedNameSpecifierKind();
Guy Benyei11169dd2012-12-18 14:30:41 +00008777 switch (Kind) {
8778 case NestedNameSpecifier::Identifier: {
John McCall3ce3d232019-12-13 03:37:23 -05008779 IdentifierInfo *II = readIdentifier();
8780 SourceRange Range = readSourceRange();
Guy Benyei11169dd2012-12-18 14:30:41 +00008781 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8782 break;
8783 }
8784
8785 case NestedNameSpecifier::Namespace: {
John McCall3ce3d232019-12-13 03:37:23 -05008786 NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8787 SourceRange Range = readSourceRange();
Guy Benyei11169dd2012-12-18 14:30:41 +00008788 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8789 break;
8790 }
8791
8792 case NestedNameSpecifier::NamespaceAlias: {
John McCall3ce3d232019-12-13 03:37:23 -05008793 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8794 SourceRange Range = readSourceRange();
Guy Benyei11169dd2012-12-18 14:30:41 +00008795 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8796 break;
8797 }
8798
8799 case NestedNameSpecifier::TypeSpec:
8800 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall3ce3d232019-12-13 03:37:23 -05008801 bool Template = readBool();
8802 TypeSourceInfo *T = readTypeSourceInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +00008803 if (!T)
Vedant Kumar48b4f762018-04-14 01:40:48 +00008804 return NestedNameSpecifierLoc();
John McCall3ce3d232019-12-13 03:37:23 -05008805 SourceLocation ColonColonLoc = readSourceLocation();
Guy Benyei11169dd2012-12-18 14:30:41 +00008806
8807 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008808 Builder.Extend(Context,
Guy Benyei11169dd2012-12-18 14:30:41 +00008809 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8810 T->getTypeLoc(), ColonColonLoc);
8811 break;
8812 }
8813
8814 case NestedNameSpecifier::Global: {
John McCall3ce3d232019-12-13 03:37:23 -05008815 SourceLocation ColonColonLoc = readSourceLocation();
Guy Benyei11169dd2012-12-18 14:30:41 +00008816 Builder.MakeGlobal(Context, ColonColonLoc);
8817 break;
8818 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008819
8820 case NestedNameSpecifier::Super: {
John McCall3ce3d232019-12-13 03:37:23 -05008821 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8822 SourceRange Range = readSourceRange();
Nikola Smiljanic67860242014-09-26 00:28:20 +00008823 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8824 break;
8825 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008826 }
8827 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008828
Guy Benyei11169dd2012-12-18 14:30:41 +00008829 return Builder.getWithLocInContext(Context);
8830}
8831
8832SourceRange
8833ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8834 unsigned &Idx) {
8835 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8836 SourceLocation end = ReadSourceLocation(F, Record, Idx);
8837 return SourceRange(beg, end);
8838}
8839
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008840static FixedPointSemantics
8841ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
8842 unsigned &Idx) {
8843 unsigned Width = Record[Idx++];
8844 unsigned Scale = Record[Idx++];
8845 uint64_t Tmp = Record[Idx++];
8846 bool IsSigned = Tmp & 0x1;
8847 bool IsSaturated = Tmp & 0x2;
8848 bool HasUnsignedPadding = Tmp & 0x4;
8849 return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
8850 HasUnsignedPadding);
8851}
8852
John McCall3ce3d232019-12-13 03:37:23 -05008853static const llvm::fltSemantics &
8854readAPFloatSemantics(ASTRecordReader &reader) {
8855 return llvm::APFloatBase::EnumToSemantics(
8856 static_cast<llvm::APFloatBase::Semantics>(reader.readInt()));
8857}
8858
8859APValue ASTRecordReader::readAPValue() {
8860 unsigned Kind = readInt();
John McCalld505e572019-12-13 21:54:44 -05008861 switch ((APValue::ValueKind) Kind) {
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008862 case APValue::None:
8863 return APValue();
8864 case APValue::Indeterminate:
8865 return APValue::IndeterminateValue();
8866 case APValue::Int:
John McCall3ce3d232019-12-13 03:37:23 -05008867 return APValue(readAPSInt());
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008868 case APValue::Float: {
John McCall3ce3d232019-12-13 03:37:23 -05008869 const llvm::fltSemantics &FloatSema = readAPFloatSemantics(*this);
8870 return APValue(readAPFloat(FloatSema));
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008871 }
8872 case APValue::FixedPoint: {
8873 FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
John McCall3ce3d232019-12-13 03:37:23 -05008874 return APValue(APFixedPoint(readAPInt(), FPSema));
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008875 }
8876 case APValue::ComplexInt: {
John McCall3ce3d232019-12-13 03:37:23 -05008877 llvm::APSInt First = readAPSInt();
8878 return APValue(std::move(First), readAPSInt());
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008879 }
8880 case APValue::ComplexFloat: {
John McCall3ce3d232019-12-13 03:37:23 -05008881 const llvm::fltSemantics &FloatSema1 = readAPFloatSemantics(*this);
8882 llvm::APFloat First = readAPFloat(FloatSema1);
8883 const llvm::fltSemantics &FloatSema2 = readAPFloatSemantics(*this);
8884 return APValue(std::move(First), readAPFloat(FloatSema2));
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008885 }
8886 case APValue::LValue:
8887 case APValue::Vector:
8888 case APValue::Array:
8889 case APValue::Struct:
8890 case APValue::Union:
8891 case APValue::MemberPointer:
8892 case APValue::AddrLabelDiff:
8893 // TODO : Handle all these APValue::ValueKind.
8894 return APValue();
8895 }
8896 llvm_unreachable("Invalid APValue::ValueKind");
8897}
8898
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008899/// Read a floating-point value
John McCall3ce3d232019-12-13 03:37:23 -05008900llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
8901 return llvm::APFloat(Sem, readAPInt());
Guy Benyei11169dd2012-12-18 14:30:41 +00008902}
8903
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008904// Read a string
Guy Benyei11169dd2012-12-18 14:30:41 +00008905std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8906 unsigned Len = Record[Idx++];
8907 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8908 Idx += Len;
8909 return Result;
8910}
8911
Richard Smith7ed1bc92014-12-05 22:42:13 +00008912std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8913 unsigned &Idx) {
8914 std::string Filename = ReadString(Record, Idx);
8915 ResolveImportedPath(F, Filename);
8916 return Filename;
8917}
8918
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00008919std::string ASTReader::ReadPath(StringRef BaseDirectory,
8920 const RecordData &Record, unsigned &Idx) {
8921 std::string Filename = ReadString(Record, Idx);
8922 if (!BaseDirectory.empty())
8923 ResolveImportedPath(Filename, BaseDirectory);
8924 return Filename;
8925}
8926
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008927VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
Guy Benyei11169dd2012-12-18 14:30:41 +00008928 unsigned &Idx) {
8929 unsigned Major = Record[Idx++];
8930 unsigned Minor = Record[Idx++];
8931 unsigned Subminor = Record[Idx++];
8932 if (Minor == 0)
8933 return VersionTuple(Major);
8934 if (Subminor == 0)
8935 return VersionTuple(Major, Minor - 1);
8936 return VersionTuple(Major, Minor - 1, Subminor - 1);
8937}
8938
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008939CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Guy Benyei11169dd2012-12-18 14:30:41 +00008940 const RecordData &Record,
8941 unsigned &Idx) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008942 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Richard Smithdbafb6c2017-06-29 23:23:46 +00008943 return CXXTemporary::Create(getContext(), Decl);
Guy Benyei11169dd2012-12-18 14:30:41 +00008944}
8945
Richard Smith37a93df2017-02-18 00:32:02 +00008946DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00008947 return Diag(CurrentImportLoc, DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00008948}
8949
Richard Smith37a93df2017-02-18 00:32:02 +00008950DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
Guy Benyei11169dd2012-12-18 14:30:41 +00008951 return Diags.Report(Loc, DiagID);
8952}
8953
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008954/// Retrieve the identifier table associated with the
Guy Benyei11169dd2012-12-18 14:30:41 +00008955/// preprocessor.
8956IdentifierTable &ASTReader::getIdentifierTable() {
8957 return PP.getIdentifierTable();
8958}
8959
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008960/// Record that the given ID maps to the given switch-case
Guy Benyei11169dd2012-12-18 14:30:41 +00008961/// statement.
8962void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00008963 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
Guy Benyei11169dd2012-12-18 14:30:41 +00008964 "Already have a SwitchCase with this ID");
8965 (*CurrSwitchCaseStmts)[ID] = SC;
8966}
8967
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008968/// Retrieve the switch-case statement with the given ID.
Guy Benyei11169dd2012-12-18 14:30:41 +00008969SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00008970 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
Guy Benyei11169dd2012-12-18 14:30:41 +00008971 return (*CurrSwitchCaseStmts)[ID];
8972}
8973
8974void ASTReader::ClearSwitchCaseIDs() {
8975 CurrSwitchCaseStmts->clear();
8976}
8977
8978void ASTReader::ReadComments() {
Richard Smithdbafb6c2017-06-29 23:23:46 +00008979 ASTContext &Context = getContext();
Guy Benyei11169dd2012-12-18 14:30:41 +00008980 std::vector<RawComment *> Comments;
Vedant Kumar48b4f762018-04-14 01:40:48 +00008981 for (SmallVectorImpl<std::pair<BitstreamCursor,
8982 serialization::ModuleFile *>>::iterator
8983 I = CommentsCursors.begin(),
8984 E = CommentsCursors.end();
8985 I != E; ++I) {
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008986 Comments.clear();
Vedant Kumar48b4f762018-04-14 01:40:48 +00008987 BitstreamCursor &Cursor = I->first;
8988 serialization::ModuleFile &F = *I->second;
Guy Benyei11169dd2012-12-18 14:30:41 +00008989 SavedStreamPosition SavedPosition(Cursor);
8990
8991 RecordData Record;
8992 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00008993 Expected<llvm::BitstreamEntry> MaybeEntry =
8994 Cursor.advanceSkippingSubblocks(
8995 BitstreamCursor::AF_DontPopBlockAtEnd);
8996 if (!MaybeEntry) {
8997 Error(MaybeEntry.takeError());
8998 return;
8999 }
9000 llvm::BitstreamEntry Entry = MaybeEntry.get();
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00009001
Chris Lattner7fb3bef2013-01-20 00:56:42 +00009002 switch (Entry.Kind) {
9003 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9004 case llvm::BitstreamEntry::Error:
9005 Error("malformed block record in AST file");
9006 return;
9007 case llvm::BitstreamEntry::EndBlock:
9008 goto NextCursor;
9009 case llvm::BitstreamEntry::Record:
9010 // The interesting case.
Guy Benyei11169dd2012-12-18 14:30:41 +00009011 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00009012 }
9013
9014 // Read a record.
9015 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00009016 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9017 if (!MaybeComment) {
9018 Error(MaybeComment.takeError());
9019 return;
9020 }
9021 switch ((CommentRecordTypes)MaybeComment.get()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00009022 case COMMENTS_RAW_COMMENT: {
9023 unsigned Idx = 0;
9024 SourceRange SR = ReadSourceRange(F, Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00009025 RawComment::CommentKind Kind =
9026 (RawComment::CommentKind) Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00009027 bool IsTrailingComment = Record[Idx++];
9028 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00009029 Comments.push_back(new (Context) RawComment(
David L. Jones13d5a872018-03-02 00:07:45 +00009030 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
Guy Benyei11169dd2012-12-18 14:30:41 +00009031 break;
9032 }
9033 }
9034 }
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00009035 NextCursor:
Jan Korousf31d8df2019-08-13 18:11:44 +00009036 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9037 FileToOffsetToComment;
9038 for (RawComment *C : Comments) {
9039 SourceLocation CommentLoc = C->getBeginLoc();
9040 if (CommentLoc.isValid()) {
9041 std::pair<FileID, unsigned> Loc =
9042 SourceMgr.getDecomposedLoc(CommentLoc);
9043 if (Loc.first.isValid())
9044 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9045 }
9046 }
Guy Benyei11169dd2012-12-18 14:30:41 +00009047 }
Guy Benyei11169dd2012-12-18 14:30:41 +00009048}
9049
Argyrios Kyrtzidisa38cb202017-01-30 06:05:58 +00009050void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9051 bool IncludeSystem, bool Complain,
9052 llvm::function_ref<void(const serialization::InputFile &IF,
9053 bool isSystem)> Visitor) {
9054 unsigned NumUserInputs = MF.NumUserInputFiles;
9055 unsigned NumInputs = MF.InputFilesLoaded.size();
9056 assert(NumUserInputs <= NumInputs);
9057 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9058 for (unsigned I = 0; I < N; ++I) {
9059 bool IsSystem = I >= NumUserInputs;
9060 InputFile IF = getInputFile(MF, I+1, Complain);
9061 Visitor(IF, IsSystem);
9062 }
9063}
9064
Richard Smithf3f84612017-06-29 02:19:42 +00009065void ASTReader::visitTopLevelModuleMaps(
9066 serialization::ModuleFile &MF,
9067 llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9068 unsigned NumInputs = MF.InputFilesLoaded.size();
9069 for (unsigned I = 0; I < NumInputs; ++I) {
9070 InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9071 if (IFI.TopLevelModuleMap)
9072 // FIXME: This unnecessarily re-reads the InputFileInfo.
9073 if (auto *FE = getInputFile(MF, I + 1).getFile())
9074 Visitor(FE);
9075 }
9076}
9077
Richard Smithcd45dbc2014-04-19 03:48:30 +00009078std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9079 // If we know the owning module, use it.
Richard Smith42413142015-05-15 20:05:43 +00009080 if (Module *M = D->getImportedOwningModule())
Richard Smithcd45dbc2014-04-19 03:48:30 +00009081 return M->getFullModuleName();
9082
9083 // Otherwise, use the name of the top-level module the decl is within.
9084 if (ModuleFile *M = getOwningModuleFile(D))
9085 return M->ModuleName;
9086
9087 // Not from a module.
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00009088 return {};
Richard Smithcd45dbc2014-04-19 03:48:30 +00009089}
9090
Guy Benyei11169dd2012-12-18 14:30:41 +00009091void ASTReader::finishPendingActions() {
Richard Smitha62d1982018-08-03 01:00:01 +00009092 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
Richard Smith851072e2014-05-19 20:59:20 +00009093 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
Richard Smith2b9e3e32013-10-18 06:05:18 +00009094 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
Richard Smitha0ce9c42014-07-29 23:23:27 +00009095 !PendingUpdateRecords.empty()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00009096 // If any identifiers with corresponding top-level declarations have
9097 // been loaded, load those declarations now.
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00009098 using TopLevelDeclsMap =
9099 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
Craig Topper79be4cd2013-07-05 04:33:53 +00009100 TopLevelDeclsMap TopLevelDecls;
9101
Guy Benyei11169dd2012-12-18 14:30:41 +00009102 while (!PendingIdentifierInfos.empty()) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00009103 IdentifierInfo *II = PendingIdentifierInfos.back().first;
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00009104 SmallVector<uint32_t, 4> DeclIDs =
9105 std::move(PendingIdentifierInfos.back().second);
Douglas Gregorcb15f082013-02-19 18:26:28 +00009106 PendingIdentifierInfos.pop_back();
Douglas Gregor6168bd22013-02-18 15:53:43 +00009107
9108 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei11169dd2012-12-18 14:30:41 +00009109 }
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00009110
Richard Smitha62d1982018-08-03 01:00:01 +00009111 // Load each function type that we deferred loading because it was a
9112 // deduced type that might refer to a local type declared within itself.
9113 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9114 auto *FD = PendingFunctionTypes[I].first;
9115 FD->setType(GetType(PendingFunctionTypes[I].second));
9116
9117 // If we gave a function a deduced return type, remember that we need to
9118 // propagate that along the redeclaration chain.
9119 auto *DT = FD->getReturnType()->getContainedDeducedType();
9120 if (DT && DT->isDeduced())
9121 PendingDeducedTypeUpdates.insert(
9122 {FD->getCanonicalDecl(), FD->getReturnType()});
9123 }
9124 PendingFunctionTypes.clear();
9125
Richard Smith851072e2014-05-19 20:59:20 +00009126 // For each decl chain that we wanted to complete while deserializing, mark
9127 // it as "still needs to be completed".
Vedant Kumar48b4f762018-04-14 01:40:48 +00009128 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9129 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9130 }
Richard Smith851072e2014-05-19 20:59:20 +00009131 PendingIncompleteDeclChains.clear();
9132
Guy Benyei11169dd2012-12-18 14:30:41 +00009133 // Load pending declaration chains.
Vedant Kumar48b4f762018-04-14 01:40:48 +00009134 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
Richard Smitha62d1982018-08-03 01:00:01 +00009135 loadPendingDeclChain(PendingDeclChains[I].first,
9136 PendingDeclChains[I].second);
Guy Benyei11169dd2012-12-18 14:30:41 +00009137 PendingDeclChains.clear();
9138
Douglas Gregor6168bd22013-02-18 15:53:43 +00009139 // Make the most recent of the top-level declarations visible.
Vedant Kumar48b4f762018-04-14 01:40:48 +00009140 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9141 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9142 IdentifierInfo *II = TLD->first;
9143 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9144 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregor6168bd22013-02-18 15:53:43 +00009145 }
9146 }
9147
Guy Benyei11169dd2012-12-18 14:30:41 +00009148 // Load any pending macro definitions.
9149 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009150 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9151 SmallVector<PendingMacroInfo, 2> GlobalIDs;
9152 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9153 // Initialize the macro history from chained-PCHs ahead of module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00009154 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00009155 ++IDIdx) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009156 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Manman Ren11f2a472016-08-18 17:42:15 +00009157 if (!Info.M->isModule())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009158 resolvePendingMacro(II, Info);
9159 }
9160 // Handle module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00009161 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009162 ++IDIdx) {
9163 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Manman Ren11f2a472016-08-18 17:42:15 +00009164 if (Info.M->isModule())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009165 resolvePendingMacro(II, Info);
Guy Benyei11169dd2012-12-18 14:30:41 +00009166 }
9167 }
9168 PendingMacroIDs.clear();
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00009169
9170 // Wire up the DeclContexts for Decls that we delayed setting until
9171 // recursive loading is completed.
9172 while (!PendingDeclContextInfos.empty()) {
9173 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9174 PendingDeclContextInfos.pop_front();
Vedant Kumar48b4f762018-04-14 01:40:48 +00009175 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9176 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00009177 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9178 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00009179
Richard Smithd1c46742014-04-30 02:24:17 +00009180 // Perform any pending declaration updates.
Richard Smithd6db68c2014-08-07 20:58:41 +00009181 while (!PendingUpdateRecords.empty()) {
Richard Smithd1c46742014-04-30 02:24:17 +00009182 auto Update = PendingUpdateRecords.pop_back_val();
9183 ReadingKindTracker ReadingKind(Read_Decl, *this);
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00009184 loadDeclUpdateRecords(Update);
Richard Smithd1c46742014-04-30 02:24:17 +00009185 }
Guy Benyei11169dd2012-12-18 14:30:41 +00009186 }
Richard Smith8a639892015-01-24 01:07:20 +00009187
9188 // At this point, all update records for loaded decls are in place, so any
9189 // fake class definitions should have become real.
9190 assert(PendingFakeDefinitionData.empty() &&
9191 "faked up a class definition but never saw the real one");
9192
Guy Benyei11169dd2012-12-18 14:30:41 +00009193 // If we deserialized any C++ or Objective-C class definitions, any
9194 // Objective-C protocol definitions, or any redeclarable templates, make sure
David L. Jonesc4808b9e2016-12-15 20:53:26 +00009195 // that all redeclarations point to the definitions. Note that this can only
Guy Benyei11169dd2012-12-18 14:30:41 +00009196 // happen now, after the redeclaration chains have been fully wired.
Vedant Kumar48b4f762018-04-14 01:40:48 +00009197 for (Decl *D : PendingDefinitions) {
9198 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9199 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00009200 // Make sure that the TagType points at the definition.
9201 const_cast<TagType*>(TagT)->decl = TD;
9202 }
Richard Smith8ce51082015-03-11 01:44:51 +00009203
Vedant Kumar48b4f762018-04-14 01:40:48 +00009204 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00009205 for (auto *R = getMostRecentExistingDecl(RD); R;
9206 R = R->getPreviousDecl()) {
9207 assert((R == D) ==
9208 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
Richard Smith2c381642014-08-27 23:11:59 +00009209 "declaration thinks it's the definition but it isn't");
Aaron Ballman86c93902014-03-06 23:45:36 +00009210 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
Richard Smith2c381642014-08-27 23:11:59 +00009211 }
Guy Benyei11169dd2012-12-18 14:30:41 +00009212 }
9213
9214 continue;
9215 }
Richard Smith8ce51082015-03-11 01:44:51 +00009216
Vedant Kumar48b4f762018-04-14 01:40:48 +00009217 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00009218 // Make sure that the ObjCInterfaceType points at the definition.
9219 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9220 ->Decl = ID;
Richard Smith8ce51082015-03-11 01:44:51 +00009221
9222 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9223 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9224
Guy Benyei11169dd2012-12-18 14:30:41 +00009225 continue;
9226 }
Richard Smith8ce51082015-03-11 01:44:51 +00009227
Vedant Kumar48b4f762018-04-14 01:40:48 +00009228 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00009229 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9230 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9231
Guy Benyei11169dd2012-12-18 14:30:41 +00009232 continue;
9233 }
Richard Smith8ce51082015-03-11 01:44:51 +00009234
Vedant Kumar48b4f762018-04-14 01:40:48 +00009235 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
Richard Smith8ce51082015-03-11 01:44:51 +00009236 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9237 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
Guy Benyei11169dd2012-12-18 14:30:41 +00009238 }
9239 PendingDefinitions.clear();
9240
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009241 // Load the bodies of any functions or methods we've encountered. We do
9242 // this now (delayed) so that we can be sure that the declaration chains
9243 // have been fully wired up (hasBody relies on this).
9244 // FIXME: We shouldn't require complete redeclaration chains here.
Vedant Kumar48b4f762018-04-14 01:40:48 +00009245 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9246 PBEnd = PendingBodies.end();
9247 PB != PBEnd; ++PB) {
9248 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
Richard Smith0b70de12018-06-28 01:07:28 +00009249 // For a function defined inline within a class template, force the
9250 // canonical definition to be the one inside the canonical definition of
9251 // the template. This ensures that we instantiate from a correct view
9252 // of the template.
9253 //
9254 // Sadly we can't do this more generally: we can't be sure that all
9255 // copies of an arbitrary class definition will have the same members
9256 // defined (eg, some member functions may not be instantiated, and some
9257 // special members may or may not have been implicitly defined).
9258 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9259 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9260 continue;
9261
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009262 // FIXME: Check for =delete/=default?
9263 // FIXME: Complain about ODR violations here?
9264 const FunctionDecl *Defn = nullptr;
9265 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00009266 FD->setLazyBody(PB->second);
Richard Trieue6caa262017-12-23 00:41:01 +00009267 } else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00009268 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
Richard Trieue6caa262017-12-23 00:41:01 +00009269 mergeDefinitionVisibility(NonConstDefn, FD);
9270
9271 if (!FD->isLateTemplateParsed() &&
9272 !NonConstDefn->isLateTemplateParsed() &&
9273 FD->getODRHash() != NonConstDefn->getODRHash()) {
Richard Trieu27c1b1a2018-07-10 01:40:50 +00009274 if (!isa<CXXMethodDecl>(FD)) {
9275 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9276 } else if (FD->getLexicalParent()->isFileContext() &&
9277 NonConstDefn->getLexicalParent()->isFileContext()) {
9278 // Only diagnose out-of-line method definitions. If they are
9279 // in class definitions, then an error will be generated when
9280 // processing the class bodies.
9281 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9282 }
Richard Trieue6caa262017-12-23 00:41:01 +00009283 }
9284 }
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009285 continue;
9286 }
9287
Vedant Kumar48b4f762018-04-14 01:40:48 +00009288 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009289 if (!getContext().getLangOpts().Modules || !MD->hasBody())
Vedant Kumar48b4f762018-04-14 01:40:48 +00009290 MD->setLazyBody(PB->second);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009291 }
9292 PendingBodies.clear();
9293
Richard Smith42413142015-05-15 20:05:43 +00009294 // Do some cleanup.
9295 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9296 getContext().deduplicateMergedDefinitonsFor(ND);
9297 PendingMergedDefinitionsToDeduplicate.clear();
Richard Smitha0ce9c42014-07-29 23:23:27 +00009298}
9299
9300void ASTReader::diagnoseOdrViolations() {
Richard Trieue6caa262017-12-23 00:41:01 +00009301 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
Richard Trieuab4d7302018-07-25 22:52:05 +00009302 PendingFunctionOdrMergeFailures.empty() &&
9303 PendingEnumOdrMergeFailures.empty())
Richard Smithbb853c72014-08-13 01:23:33 +00009304 return;
9305
Richard Smitha0ce9c42014-07-29 23:23:27 +00009306 // Trigger the import of the full definition of each class that had any
9307 // odr-merging problems, so we can produce better diagnostics for them.
Richard Smithbb853c72014-08-13 01:23:33 +00009308 // These updates may in turn find and diagnose some ODR failures, so take
9309 // ownership of the set first.
9310 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9311 PendingOdrMergeFailures.clear();
9312 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00009313 Merge.first->buildLookup();
9314 Merge.first->decls_begin();
9315 Merge.first->bases_begin();
9316 Merge.first->vbases_begin();
Richard Trieue13eabe2017-09-30 02:19:17 +00009317 for (auto &RecordPair : Merge.second) {
9318 auto *RD = RecordPair.first;
Richard Smitha0ce9c42014-07-29 23:23:27 +00009319 RD->decls_begin();
9320 RD->bases_begin();
9321 RD->vbases_begin();
9322 }
9323 }
9324
Richard Trieue6caa262017-12-23 00:41:01 +00009325 // Trigger the import of functions.
9326 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9327 PendingFunctionOdrMergeFailures.clear();
9328 for (auto &Merge : FunctionOdrMergeFailures) {
9329 Merge.first->buildLookup();
9330 Merge.first->decls_begin();
9331 Merge.first->getBody();
9332 for (auto &FD : Merge.second) {
9333 FD->buildLookup();
9334 FD->decls_begin();
9335 FD->getBody();
9336 }
9337 }
9338
Richard Trieuab4d7302018-07-25 22:52:05 +00009339 // Trigger the import of enums.
9340 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9341 PendingEnumOdrMergeFailures.clear();
9342 for (auto &Merge : EnumOdrMergeFailures) {
9343 Merge.first->decls_begin();
9344 for (auto &Enum : Merge.second) {
9345 Enum->decls_begin();
9346 }
9347 }
9348
Richard Smitha0ce9c42014-07-29 23:23:27 +00009349 // For each declaration from a merged context, check that the canonical
9350 // definition of that context also contains a declaration of the same
9351 // entity.
9352 //
9353 // Caution: this loop does things that might invalidate iterators into
9354 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9355 while (!PendingOdrMergeChecks.empty()) {
9356 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9357
9358 // FIXME: Skip over implicit declarations for now. This matters for things
9359 // like implicitly-declared special member functions. This isn't entirely
9360 // correct; we can end up with multiple unmerged declarations of the same
9361 // implicit entity.
9362 if (D->isImplicit())
9363 continue;
9364
9365 DeclContext *CanonDef = D->getDeclContext();
Richard Smitha0ce9c42014-07-29 23:23:27 +00009366
9367 bool Found = false;
9368 const Decl *DCanon = D->getCanonicalDecl();
9369
Richard Smith01bdb7a2014-08-28 05:44:07 +00009370 for (auto RI : D->redecls()) {
9371 if (RI->getLexicalDeclContext() == CanonDef) {
9372 Found = true;
9373 break;
9374 }
9375 }
9376 if (Found)
9377 continue;
9378
Richard Smith0f4e2c42015-08-06 04:23:48 +00009379 // Quick check failed, time to do the slow thing. Note, we can't just
9380 // look up the name of D in CanonDef here, because the member that is
9381 // in CanonDef might not be found by name lookup (it might have been
9382 // replaced by a more recent declaration in the lookup table), and we
9383 // can't necessarily find it in the redeclaration chain because it might
9384 // be merely mergeable, not redeclarable.
Richard Smitha0ce9c42014-07-29 23:23:27 +00009385 llvm::SmallVector<const NamedDecl*, 4> Candidates;
Richard Smith0f4e2c42015-08-06 04:23:48 +00009386 for (auto *CanonMember : CanonDef->decls()) {
9387 if (CanonMember->getCanonicalDecl() == DCanon) {
9388 // This can happen if the declaration is merely mergeable and not
9389 // actually redeclarable (we looked for redeclarations earlier).
9390 //
9391 // FIXME: We should be able to detect this more efficiently, without
9392 // pulling in all of the members of CanonDef.
9393 Found = true;
9394 break;
Richard Smitha0ce9c42014-07-29 23:23:27 +00009395 }
Richard Smith0f4e2c42015-08-06 04:23:48 +00009396 if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9397 if (ND->getDeclName() == D->getDeclName())
9398 Candidates.push_back(ND);
Richard Smitha0ce9c42014-07-29 23:23:27 +00009399 }
9400
9401 if (!Found) {
Richard Smithd08aeb62014-08-28 01:33:39 +00009402 // The AST doesn't like TagDecls becoming invalid after they've been
9403 // completed. We only really need to mark FieldDecls as invalid here.
9404 if (!isa<TagDecl>(D))
9405 D->setInvalidDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00009406
Richard Smith4ab3dbd2015-02-13 22:43:51 +00009407 // Ensure we don't accidentally recursively enter deserialization while
9408 // we're producing our diagnostic.
9409 Deserializing RecursionGuard(this);
Richard Smitha0ce9c42014-07-29 23:23:27 +00009410
9411 std::string CanonDefModule =
9412 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9413 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9414 << D << getOwningModuleNameForDiagnostic(D)
9415 << CanonDef << CanonDefModule.empty() << CanonDefModule;
9416
9417 if (Candidates.empty())
9418 Diag(cast<Decl>(CanonDef)->getLocation(),
9419 diag::note_module_odr_violation_no_possible_decls) << D;
9420 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00009421 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9422 Diag(Candidates[I]->getLocation(),
Richard Smitha0ce9c42014-07-29 23:23:27 +00009423 diag::note_module_odr_violation_possible_decl)
Vedant Kumar48b4f762018-04-14 01:40:48 +00009424 << Candidates[I];
Richard Smitha0ce9c42014-07-29 23:23:27 +00009425 }
9426
9427 DiagnosedOdrMergeFailures.insert(CanonDef);
9428 }
9429 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00009430
Richard Trieuab4d7302018-07-25 22:52:05 +00009431 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9432 EnumOdrMergeFailures.empty())
Richard Smith4ab3dbd2015-02-13 22:43:51 +00009433 return;
9434
9435 // Ensure we don't accidentally recursively enter deserialization while
9436 // we're producing our diagnostics.
9437 Deserializing RecursionGuard(this);
9438
Richard Trieue6caa262017-12-23 00:41:01 +00009439 // Common code for hashing helpers.
9440 ODRHash Hash;
9441 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9442 Hash.clear();
9443 Hash.AddQualType(Ty);
9444 return Hash.CalculateHash();
9445 };
9446
9447 auto ComputeODRHash = [&Hash](const Stmt *S) {
9448 assert(S);
9449 Hash.clear();
9450 Hash.AddStmt(S);
9451 return Hash.CalculateHash();
9452 };
9453
9454 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9455 assert(D);
9456 Hash.clear();
9457 Hash.AddSubDecl(D);
9458 return Hash.CalculateHash();
9459 };
9460
Richard Trieu7282d322018-04-25 00:31:15 +00009461 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9462 Hash.clear();
9463 Hash.AddTemplateArgument(TA);
9464 return Hash.CalculateHash();
9465 };
9466
Richard Trieu9359e8f2018-05-30 01:12:26 +00009467 auto ComputeTemplateParameterListODRHash =
9468 [&Hash](const TemplateParameterList *TPL) {
9469 assert(TPL);
9470 Hash.clear();
9471 Hash.AddTemplateParameterList(TPL);
9472 return Hash.CalculateHash();
9473 };
9474
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -08009475 // Used with err_module_odr_violation_mismatch_decl and
9476 // note_module_odr_violation_mismatch_decl
9477 // This list should be the same Decl's as in ODRHash::isWhiteListedDecl
9478 enum ODRMismatchDecl {
9479 EndOfClass,
9480 PublicSpecifer,
9481 PrivateSpecifer,
9482 ProtectedSpecifer,
9483 StaticAssert,
9484 Field,
9485 CXXMethod,
9486 TypeAlias,
9487 TypeDef,
9488 Var,
9489 Friend,
9490 FunctionTemplate,
9491 Other
9492 };
9493
9494 // Used with err_module_odr_violation_mismatch_decl_diff and
9495 // note_module_odr_violation_mismatch_decl_diff
9496 enum ODRMismatchDeclDifference {
9497 StaticAssertCondition,
9498 StaticAssertMessage,
9499 StaticAssertOnlyMessage,
9500 FieldName,
9501 FieldTypeName,
9502 FieldSingleBitField,
9503 FieldDifferentWidthBitField,
9504 FieldSingleMutable,
9505 FieldSingleInitializer,
9506 FieldDifferentInitializers,
9507 MethodName,
9508 MethodDeleted,
9509 MethodDefaulted,
9510 MethodVirtual,
9511 MethodStatic,
9512 MethodVolatile,
9513 MethodConst,
9514 MethodInline,
9515 MethodNumberParameters,
9516 MethodParameterType,
9517 MethodParameterName,
9518 MethodParameterSingleDefaultArgument,
9519 MethodParameterDifferentDefaultArgument,
9520 MethodNoTemplateArguments,
9521 MethodDifferentNumberTemplateArguments,
9522 MethodDifferentTemplateArgument,
9523 MethodSingleBody,
9524 MethodDifferentBody,
9525 TypedefName,
9526 TypedefType,
9527 VarName,
9528 VarType,
9529 VarSingleInitializer,
9530 VarDifferentInitializer,
9531 VarConstexpr,
9532 FriendTypeFunction,
9533 FriendType,
9534 FriendFunction,
9535 FunctionTemplateDifferentNumberParameters,
9536 FunctionTemplateParameterDifferentKind,
9537 FunctionTemplateParameterName,
9538 FunctionTemplateParameterSingleDefaultArgument,
9539 FunctionTemplateParameterDifferentDefaultArgument,
9540 FunctionTemplateParameterDifferentType,
9541 FunctionTemplatePackParameter,
9542 };
9543
9544 // These lambdas have the common portions of the ODR diagnostics. This
9545 // has the same return as Diag(), so addition parameters can be passed
9546 // in with operator<<
9547 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9548 SourceLocation Loc, SourceRange Range,
9549 ODRMismatchDeclDifference DiffType) {
9550 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9551 << FirstRecord << FirstModule.empty() << FirstModule << Range
9552 << DiffType;
9553 };
9554 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9555 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9556 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9557 << SecondModule << Range << DiffType;
9558 };
9559
9560 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9561 &ComputeQualTypeODRHash, &ComputeODRHash](
9562 NamedDecl *FirstRecord, StringRef FirstModule,
9563 StringRef SecondModule, FieldDecl *FirstField,
9564 FieldDecl *SecondField) {
9565 IdentifierInfo *FirstII = FirstField->getIdentifier();
9566 IdentifierInfo *SecondII = SecondField->getIdentifier();
9567 if (FirstII->getName() != SecondII->getName()) {
9568 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9569 FirstField->getSourceRange(), FieldName)
9570 << FirstII;
9571 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9572 SecondField->getSourceRange(), FieldName)
9573 << SecondII;
9574
9575 return true;
9576 }
9577
9578 assert(getContext().hasSameType(FirstField->getType(),
9579 SecondField->getType()));
9580
9581 QualType FirstType = FirstField->getType();
9582 QualType SecondType = SecondField->getType();
9583 if (ComputeQualTypeODRHash(FirstType) !=
9584 ComputeQualTypeODRHash(SecondType)) {
9585 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9586 FirstField->getSourceRange(), FieldTypeName)
9587 << FirstII << FirstType;
9588 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9589 SecondField->getSourceRange(), FieldTypeName)
9590 << SecondII << SecondType;
9591
9592 return true;
9593 }
9594
9595 const bool IsFirstBitField = FirstField->isBitField();
9596 const bool IsSecondBitField = SecondField->isBitField();
9597 if (IsFirstBitField != IsSecondBitField) {
9598 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9599 FirstField->getSourceRange(), FieldSingleBitField)
9600 << FirstII << IsFirstBitField;
9601 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9602 SecondField->getSourceRange(), FieldSingleBitField)
9603 << SecondII << IsSecondBitField;
9604 return true;
9605 }
9606
9607 if (IsFirstBitField && IsSecondBitField) {
9608 unsigned FirstBitWidthHash =
9609 ComputeODRHash(FirstField->getBitWidth());
9610 unsigned SecondBitWidthHash =
9611 ComputeODRHash(SecondField->getBitWidth());
9612 if (FirstBitWidthHash != SecondBitWidthHash) {
9613 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9614 FirstField->getSourceRange(),
9615 FieldDifferentWidthBitField)
9616 << FirstII << FirstField->getBitWidth()->getSourceRange();
9617 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9618 SecondField->getSourceRange(),
9619 FieldDifferentWidthBitField)
9620 << SecondII << SecondField->getBitWidth()->getSourceRange();
9621 return true;
9622 }
9623 }
9624
9625 if (!PP.getLangOpts().CPlusPlus)
9626 return false;
9627
9628 const bool IsFirstMutable = FirstField->isMutable();
9629 const bool IsSecondMutable = SecondField->isMutable();
9630 if (IsFirstMutable != IsSecondMutable) {
9631 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9632 FirstField->getSourceRange(), FieldSingleMutable)
9633 << FirstII << IsFirstMutable;
9634 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9635 SecondField->getSourceRange(), FieldSingleMutable)
9636 << SecondII << IsSecondMutable;
9637 return true;
9638 }
9639
9640 const Expr *FirstInitializer = FirstField->getInClassInitializer();
9641 const Expr *SecondInitializer = SecondField->getInClassInitializer();
9642 if ((!FirstInitializer && SecondInitializer) ||
9643 (FirstInitializer && !SecondInitializer)) {
9644 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9645 FirstField->getSourceRange(), FieldSingleInitializer)
9646 << FirstII << (FirstInitializer != nullptr);
9647 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9648 SecondField->getSourceRange(), FieldSingleInitializer)
9649 << SecondII << (SecondInitializer != nullptr);
9650 return true;
9651 }
9652
9653 if (FirstInitializer && SecondInitializer) {
9654 unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9655 unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9656 if (FirstInitHash != SecondInitHash) {
9657 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9658 FirstField->getSourceRange(),
9659 FieldDifferentInitializers)
9660 << FirstII << FirstInitializer->getSourceRange();
9661 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9662 SecondField->getSourceRange(),
9663 FieldDifferentInitializers)
9664 << SecondII << SecondInitializer->getSourceRange();
9665 return true;
9666 }
9667 }
9668
9669 return false;
9670 };
9671
9672 auto ODRDiagTypeDefOrAlias =
9673 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9674 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9675 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9676 bool IsTypeAlias) {
9677 auto FirstName = FirstTD->getDeclName();
9678 auto SecondName = SecondTD->getDeclName();
9679 if (FirstName != SecondName) {
9680 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9681 FirstTD->getSourceRange(), TypedefName)
9682 << IsTypeAlias << FirstName;
9683 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9684 SecondTD->getSourceRange(), TypedefName)
9685 << IsTypeAlias << SecondName;
9686 return true;
9687 }
9688
9689 QualType FirstType = FirstTD->getUnderlyingType();
9690 QualType SecondType = SecondTD->getUnderlyingType();
9691 if (ComputeQualTypeODRHash(FirstType) !=
9692 ComputeQualTypeODRHash(SecondType)) {
9693 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9694 FirstTD->getSourceRange(), TypedefType)
9695 << IsTypeAlias << FirstName << FirstType;
9696 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9697 SecondTD->getSourceRange(), TypedefType)
9698 << IsTypeAlias << SecondName << SecondType;
9699 return true;
9700 }
9701
9702 return false;
9703 };
9704
9705 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9706 &ComputeQualTypeODRHash, &ComputeODRHash,
9707 this](NamedDecl *FirstRecord, StringRef FirstModule,
9708 StringRef SecondModule, VarDecl *FirstVD,
9709 VarDecl *SecondVD) {
9710 auto FirstName = FirstVD->getDeclName();
9711 auto SecondName = SecondVD->getDeclName();
9712 if (FirstName != SecondName) {
9713 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9714 FirstVD->getSourceRange(), VarName)
9715 << FirstName;
9716 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9717 SecondVD->getSourceRange(), VarName)
9718 << SecondName;
9719 return true;
9720 }
9721
9722 QualType FirstType = FirstVD->getType();
9723 QualType SecondType = SecondVD->getType();
9724 if (ComputeQualTypeODRHash(FirstType) !=
9725 ComputeQualTypeODRHash(SecondType)) {
9726 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9727 FirstVD->getSourceRange(), VarType)
9728 << FirstName << FirstType;
9729 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9730 SecondVD->getSourceRange(), VarType)
9731 << SecondName << SecondType;
9732 return true;
9733 }
9734
9735 if (!PP.getLangOpts().CPlusPlus)
9736 return false;
9737
9738 const Expr *FirstInit = FirstVD->getInit();
9739 const Expr *SecondInit = SecondVD->getInit();
9740 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9741 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9742 FirstVD->getSourceRange(), VarSingleInitializer)
9743 << FirstName << (FirstInit == nullptr)
9744 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9745 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9746 SecondVD->getSourceRange(), VarSingleInitializer)
9747 << SecondName << (SecondInit == nullptr)
9748 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9749 return true;
9750 }
9751
9752 if (FirstInit && SecondInit &&
9753 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9754 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9755 FirstVD->getSourceRange(), VarDifferentInitializer)
9756 << FirstName << FirstInit->getSourceRange();
9757 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9758 SecondVD->getSourceRange(), VarDifferentInitializer)
9759 << SecondName << SecondInit->getSourceRange();
9760 return true;
9761 }
9762
9763 const bool FirstIsConstexpr = FirstVD->isConstexpr();
9764 const bool SecondIsConstexpr = SecondVD->isConstexpr();
9765 if (FirstIsConstexpr != SecondIsConstexpr) {
9766 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9767 FirstVD->getSourceRange(), VarConstexpr)
9768 << FirstName << FirstIsConstexpr;
9769 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9770 SecondVD->getSourceRange(), VarConstexpr)
9771 << SecondName << SecondIsConstexpr;
9772 return true;
9773 }
9774 return false;
9775 };
9776
9777 auto DifferenceSelector = [](Decl *D) {
9778 assert(D && "valid Decl required");
9779 switch (D->getKind()) {
9780 default:
9781 return Other;
9782 case Decl::AccessSpec:
9783 switch (D->getAccess()) {
9784 case AS_public:
9785 return PublicSpecifer;
9786 case AS_private:
9787 return PrivateSpecifer;
9788 case AS_protected:
9789 return ProtectedSpecifer;
9790 case AS_none:
9791 break;
9792 }
9793 llvm_unreachable("Invalid access specifier");
9794 case Decl::StaticAssert:
9795 return StaticAssert;
9796 case Decl::Field:
9797 return Field;
9798 case Decl::CXXMethod:
9799 case Decl::CXXConstructor:
9800 case Decl::CXXDestructor:
9801 return CXXMethod;
9802 case Decl::TypeAlias:
9803 return TypeAlias;
9804 case Decl::Typedef:
9805 return TypeDef;
9806 case Decl::Var:
9807 return Var;
9808 case Decl::Friend:
9809 return Friend;
9810 case Decl::FunctionTemplate:
9811 return FunctionTemplate;
9812 }
9813 };
9814
9815 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9816 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9817 RecordDecl *Record,
9818 const DeclContext *DC) {
9819 for (auto *D : Record->decls()) {
9820 if (!ODRHash::isWhitelistedDecl(D, DC))
9821 continue;
9822 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9823 }
9824 };
9825
9826 struct DiffResult {
9827 Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9828 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9829 };
9830
9831 // If there is a diagnoseable difference, FirstDiffType and
9832 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9833 // filled in if not EndOfClass.
9834 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9835 DeclHashes &SecondHashes) {
9836 DiffResult DR;
9837 auto FirstIt = FirstHashes.begin();
9838 auto SecondIt = SecondHashes.begin();
9839 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9840 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9841 FirstIt->second == SecondIt->second) {
9842 ++FirstIt;
9843 ++SecondIt;
9844 continue;
9845 }
9846
9847 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9848 DR.SecondDecl =
9849 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9850
9851 DR.FirstDiffType =
9852 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9853 DR.SecondDiffType =
9854 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9855 return DR;
9856 }
9857 return DR;
9858 };
9859
9860 // Use this to diagnose that an unexpected Decl was encountered
9861 // or no difference was detected. This causes a generic error
9862 // message to be emitted.
9863 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9864 StringRef FirstModule,
9865 NamedDecl *SecondRecord,
9866 StringRef SecondModule) {
9867 Diag(FirstRecord->getLocation(),
9868 diag::err_module_odr_violation_different_definitions)
9869 << FirstRecord << FirstModule.empty() << FirstModule;
9870
9871 if (DR.FirstDecl) {
9872 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9873 << FirstRecord << DR.FirstDecl->getSourceRange();
9874 }
9875
9876 Diag(SecondRecord->getLocation(),
9877 diag::note_module_odr_violation_different_definitions)
9878 << SecondModule;
9879
9880 if (DR.SecondDecl) {
9881 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9882 << DR.SecondDecl->getSourceRange();
9883 }
9884 };
9885
9886 auto DiagnoseODRMismatch =
9887 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9888 NamedDecl *SecondRecord, StringRef SecondModule) {
9889 SourceLocation FirstLoc;
9890 SourceRange FirstRange;
9891 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9892 if (DR.FirstDiffType == EndOfClass && FirstTag) {
9893 FirstLoc = FirstTag->getBraceRange().getEnd();
9894 } else {
9895 FirstLoc = DR.FirstDecl->getLocation();
9896 FirstRange = DR.FirstDecl->getSourceRange();
9897 }
9898 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
9899 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
9900 << DR.FirstDiffType;
9901
9902 SourceLocation SecondLoc;
9903 SourceRange SecondRange;
9904 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
9905 if (DR.SecondDiffType == EndOfClass && SecondTag) {
9906 SecondLoc = SecondTag->getBraceRange().getEnd();
9907 } else {
9908 SecondLoc = DR.SecondDecl->getLocation();
9909 SecondRange = DR.SecondDecl->getSourceRange();
9910 }
9911 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
9912 << SecondModule << SecondRange << DR.SecondDiffType;
9913 };
9914
Richard Smithcd45dbc2014-04-19 03:48:30 +00009915 // Issue any pending ODR-failure diagnostics.
Richard Smithbb853c72014-08-13 01:23:33 +00009916 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00009917 // If we've already pointed out a specific problem with this class, don't
9918 // bother issuing a general "something's different" diagnostic.
David Blaikie82e95a32014-11-19 07:49:47 +00009919 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
Richard Smithcd45dbc2014-04-19 03:48:30 +00009920 continue;
9921
9922 bool Diagnosed = false;
Richard Trieue7f7ed22017-02-22 01:11:25 +00009923 CXXRecordDecl *FirstRecord = Merge.first;
9924 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
Richard Trieue13eabe2017-09-30 02:19:17 +00009925 for (auto &RecordPair : Merge.second) {
9926 CXXRecordDecl *SecondRecord = RecordPair.first;
Richard Smithcd45dbc2014-04-19 03:48:30 +00009927 // Multiple different declarations got merged together; tell the user
9928 // where they came from.
Richard Trieue7f7ed22017-02-22 01:11:25 +00009929 if (FirstRecord == SecondRecord)
9930 continue;
9931
9932 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
Richard Trieue13eabe2017-09-30 02:19:17 +00009933
9934 auto *FirstDD = FirstRecord->DefinitionData;
9935 auto *SecondDD = RecordPair.second;
9936
9937 assert(FirstDD && SecondDD && "Definitions without DefinitionData");
9938
9939 // Diagnostics from DefinitionData are emitted here.
9940 if (FirstDD != SecondDD) {
9941 enum ODRDefinitionDataDifference {
9942 NumBases,
9943 NumVBases,
9944 BaseType,
9945 BaseVirtual,
9946 BaseAccess,
9947 };
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -08009948 auto ODRDiagBaseError = [FirstRecord, &FirstModule,
9949 this](SourceLocation Loc, SourceRange Range,
9950 ODRDefinitionDataDifference DiffType) {
Richard Trieue13eabe2017-09-30 02:19:17 +00009951 return Diag(Loc, diag::err_module_odr_violation_definition_data)
9952 << FirstRecord << FirstModule.empty() << FirstModule << Range
9953 << DiffType;
9954 };
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -08009955 auto ODRDiagBaseNote = [&SecondModule,
9956 this](SourceLocation Loc, SourceRange Range,
9957 ODRDefinitionDataDifference DiffType) {
Richard Trieue13eabe2017-09-30 02:19:17 +00009958 return Diag(Loc, diag::note_module_odr_violation_definition_data)
9959 << SecondModule << Range << DiffType;
9960 };
9961
Richard Trieue13eabe2017-09-30 02:19:17 +00009962 unsigned FirstNumBases = FirstDD->NumBases;
9963 unsigned FirstNumVBases = FirstDD->NumVBases;
9964 unsigned SecondNumBases = SecondDD->NumBases;
9965 unsigned SecondNumVBases = SecondDD->NumVBases;
9966
9967 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
9968 unsigned NumBases = DD->NumBases;
9969 if (NumBases == 0) return SourceRange();
9970 auto bases = DD->bases();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00009971 return SourceRange(bases[0].getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00009972 bases[NumBases - 1].getEndLoc());
Richard Trieue13eabe2017-09-30 02:19:17 +00009973 };
9974
9975 if (FirstNumBases != SecondNumBases) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -08009976 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9977 NumBases)
Richard Trieue13eabe2017-09-30 02:19:17 +00009978 << FirstNumBases;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -08009979 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
9980 NumBases)
Richard Trieue13eabe2017-09-30 02:19:17 +00009981 << SecondNumBases;
9982 Diagnosed = true;
9983 break;
9984 }
9985
9986 if (FirstNumVBases != SecondNumVBases) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -08009987 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9988 NumVBases)
Richard Trieue13eabe2017-09-30 02:19:17 +00009989 << FirstNumVBases;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -08009990 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
9991 NumVBases)
Richard Trieue13eabe2017-09-30 02:19:17 +00009992 << SecondNumVBases;
9993 Diagnosed = true;
9994 break;
9995 }
9996
9997 auto FirstBases = FirstDD->bases();
9998 auto SecondBases = SecondDD->bases();
9999 unsigned i = 0;
10000 for (i = 0; i < FirstNumBases; ++i) {
10001 auto FirstBase = FirstBases[i];
10002 auto SecondBase = SecondBases[i];
10003 if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10004 ComputeQualTypeODRHash(SecondBase.getType())) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010005 ODRDiagBaseError(FirstRecord->getLocation(),
10006 FirstBase.getSourceRange(), BaseType)
Richard Trieue13eabe2017-09-30 02:19:17 +000010007 << (i + 1) << FirstBase.getType();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010008 ODRDiagBaseNote(SecondRecord->getLocation(),
10009 SecondBase.getSourceRange(), BaseType)
Richard Trieue13eabe2017-09-30 02:19:17 +000010010 << (i + 1) << SecondBase.getType();
10011 break;
10012 }
10013
10014 if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010015 ODRDiagBaseError(FirstRecord->getLocation(),
10016 FirstBase.getSourceRange(), BaseVirtual)
Richard Trieue13eabe2017-09-30 02:19:17 +000010017 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010018 ODRDiagBaseNote(SecondRecord->getLocation(),
10019 SecondBase.getSourceRange(), BaseVirtual)
Richard Trieue13eabe2017-09-30 02:19:17 +000010020 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10021 break;
10022 }
10023
10024 if (FirstBase.getAccessSpecifierAsWritten() !=
10025 SecondBase.getAccessSpecifierAsWritten()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010026 ODRDiagBaseError(FirstRecord->getLocation(),
10027 FirstBase.getSourceRange(), BaseAccess)
Richard Trieue13eabe2017-09-30 02:19:17 +000010028 << (i + 1) << FirstBase.getType()
10029 << (int)FirstBase.getAccessSpecifierAsWritten();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010030 ODRDiagBaseNote(SecondRecord->getLocation(),
10031 SecondBase.getSourceRange(), BaseAccess)
Richard Trieue13eabe2017-09-30 02:19:17 +000010032 << (i + 1) << SecondBase.getType()
10033 << (int)SecondBase.getAccessSpecifierAsWritten();
10034 break;
10035 }
10036 }
10037
10038 if (i != FirstNumBases) {
10039 Diagnosed = true;
10040 break;
10041 }
10042 }
10043
Richard Trieu498117b2017-08-23 02:43:59 +000010044 const ClassTemplateDecl *FirstTemplate =
10045 FirstRecord->getDescribedClassTemplate();
10046 const ClassTemplateDecl *SecondTemplate =
10047 SecondRecord->getDescribedClassTemplate();
10048
10049 assert(!FirstTemplate == !SecondTemplate &&
10050 "Both pointers should be null or non-null");
10051
10052 enum ODRTemplateDifference {
10053 ParamEmptyName,
10054 ParamName,
10055 ParamSingleDefaultArgument,
10056 ParamDifferentDefaultArgument,
10057 };
10058
10059 if (FirstTemplate && SecondTemplate) {
10060 DeclHashes FirstTemplateHashes;
10061 DeclHashes SecondTemplateHashes;
Richard Trieu498117b2017-08-23 02:43:59 +000010062
10063 auto PopulateTemplateParameterHashs =
Richard Trieue6caa262017-12-23 00:41:01 +000010064 [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10065 const ClassTemplateDecl *TD) {
Richard Trieu498117b2017-08-23 02:43:59 +000010066 for (auto *D : TD->getTemplateParameters()->asArray()) {
Richard Trieue6caa262017-12-23 00:41:01 +000010067 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
Richard Trieu498117b2017-08-23 02:43:59 +000010068 }
10069 };
10070
10071 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10072 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10073
10074 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10075 "Number of template parameters should be equal.");
10076
10077 auto FirstIt = FirstTemplateHashes.begin();
10078 auto FirstEnd = FirstTemplateHashes.end();
10079 auto SecondIt = SecondTemplateHashes.begin();
10080 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10081 if (FirstIt->second == SecondIt->second)
10082 continue;
10083
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010084 auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10085 SourceLocation Loc, SourceRange Range,
10086 ODRTemplateDifference DiffType) {
Richard Trieu498117b2017-08-23 02:43:59 +000010087 return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10088 << FirstRecord << FirstModule.empty() << FirstModule << Range
10089 << DiffType;
10090 };
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010091 auto ODRDiagTemplateNote = [&SecondModule, this](
10092 SourceLocation Loc, SourceRange Range,
10093 ODRTemplateDifference DiffType) {
Richard Trieu498117b2017-08-23 02:43:59 +000010094 return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10095 << SecondModule << Range << DiffType;
10096 };
10097
Vedant Kumar48b4f762018-04-14 01:40:48 +000010098 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10099 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
Richard Trieu498117b2017-08-23 02:43:59 +000010100
10101 assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10102 "Parameter Decl's should be the same kind.");
10103
10104 DeclarationName FirstName = FirstDecl->getDeclName();
10105 DeclarationName SecondName = SecondDecl->getDeclName();
10106
10107 if (FirstName != SecondName) {
10108 const bool FirstNameEmpty =
10109 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10110 const bool SecondNameEmpty =
10111 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10112 assert((!FirstNameEmpty || !SecondNameEmpty) &&
10113 "Both template parameters cannot be unnamed.");
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010114 ODRDiagTemplateError(FirstDecl->getLocation(),
10115 FirstDecl->getSourceRange(),
10116 FirstNameEmpty ? ParamEmptyName : ParamName)
Richard Trieu498117b2017-08-23 02:43:59 +000010117 << FirstName;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010118 ODRDiagTemplateNote(SecondDecl->getLocation(),
10119 SecondDecl->getSourceRange(),
10120 SecondNameEmpty ? ParamEmptyName : ParamName)
Richard Trieu498117b2017-08-23 02:43:59 +000010121 << SecondName;
10122 break;
10123 }
10124
10125 switch (FirstDecl->getKind()) {
10126 default:
10127 llvm_unreachable("Invalid template parameter type.");
10128 case Decl::TemplateTypeParm: {
10129 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10130 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10131 const bool HasFirstDefaultArgument =
10132 FirstParam->hasDefaultArgument() &&
10133 !FirstParam->defaultArgumentWasInherited();
10134 const bool HasSecondDefaultArgument =
10135 SecondParam->hasDefaultArgument() &&
10136 !SecondParam->defaultArgumentWasInherited();
10137
10138 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010139 ODRDiagTemplateError(FirstDecl->getLocation(),
10140 FirstDecl->getSourceRange(),
10141 ParamSingleDefaultArgument)
Richard Trieu498117b2017-08-23 02:43:59 +000010142 << HasFirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010143 ODRDiagTemplateNote(SecondDecl->getLocation(),
10144 SecondDecl->getSourceRange(),
10145 ParamSingleDefaultArgument)
Richard Trieu498117b2017-08-23 02:43:59 +000010146 << HasSecondDefaultArgument;
10147 break;
10148 }
10149
10150 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10151 "Expecting default arguments.");
10152
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010153 ODRDiagTemplateError(FirstDecl->getLocation(),
10154 FirstDecl->getSourceRange(),
10155 ParamDifferentDefaultArgument);
10156 ODRDiagTemplateNote(SecondDecl->getLocation(),
10157 SecondDecl->getSourceRange(),
10158 ParamDifferentDefaultArgument);
Richard Trieu498117b2017-08-23 02:43:59 +000010159
10160 break;
10161 }
10162 case Decl::NonTypeTemplateParm: {
10163 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10164 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10165 const bool HasFirstDefaultArgument =
10166 FirstParam->hasDefaultArgument() &&
10167 !FirstParam->defaultArgumentWasInherited();
10168 const bool HasSecondDefaultArgument =
10169 SecondParam->hasDefaultArgument() &&
10170 !SecondParam->defaultArgumentWasInherited();
10171
10172 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010173 ODRDiagTemplateError(FirstDecl->getLocation(),
10174 FirstDecl->getSourceRange(),
10175 ParamSingleDefaultArgument)
Richard Trieu498117b2017-08-23 02:43:59 +000010176 << HasFirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010177 ODRDiagTemplateNote(SecondDecl->getLocation(),
10178 SecondDecl->getSourceRange(),
10179 ParamSingleDefaultArgument)
Richard Trieu498117b2017-08-23 02:43:59 +000010180 << HasSecondDefaultArgument;
10181 break;
10182 }
10183
10184 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10185 "Expecting default arguments.");
10186
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010187 ODRDiagTemplateError(FirstDecl->getLocation(),
10188 FirstDecl->getSourceRange(),
10189 ParamDifferentDefaultArgument);
10190 ODRDiagTemplateNote(SecondDecl->getLocation(),
10191 SecondDecl->getSourceRange(),
10192 ParamDifferentDefaultArgument);
Richard Trieu498117b2017-08-23 02:43:59 +000010193
10194 break;
10195 }
10196 case Decl::TemplateTemplateParm: {
10197 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10198 const auto *SecondParam =
10199 cast<TemplateTemplateParmDecl>(SecondDecl);
10200 const bool HasFirstDefaultArgument =
10201 FirstParam->hasDefaultArgument() &&
10202 !FirstParam->defaultArgumentWasInherited();
10203 const bool HasSecondDefaultArgument =
10204 SecondParam->hasDefaultArgument() &&
10205 !SecondParam->defaultArgumentWasInherited();
10206
10207 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010208 ODRDiagTemplateError(FirstDecl->getLocation(),
10209 FirstDecl->getSourceRange(),
10210 ParamSingleDefaultArgument)
Richard Trieu498117b2017-08-23 02:43:59 +000010211 << HasFirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010212 ODRDiagTemplateNote(SecondDecl->getLocation(),
10213 SecondDecl->getSourceRange(),
10214 ParamSingleDefaultArgument)
Richard Trieu498117b2017-08-23 02:43:59 +000010215 << HasSecondDefaultArgument;
10216 break;
10217 }
10218
10219 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10220 "Expecting default arguments.");
10221
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010222 ODRDiagTemplateError(FirstDecl->getLocation(),
10223 FirstDecl->getSourceRange(),
10224 ParamDifferentDefaultArgument);
10225 ODRDiagTemplateNote(SecondDecl->getLocation(),
10226 SecondDecl->getSourceRange(),
10227 ParamDifferentDefaultArgument);
Richard Trieu498117b2017-08-23 02:43:59 +000010228
10229 break;
10230 }
10231 }
10232
10233 break;
10234 }
10235
10236 if (FirstIt != FirstEnd) {
10237 Diagnosed = true;
10238 break;
10239 }
10240 }
10241
Richard Trieue7f7ed22017-02-22 01:11:25 +000010242 DeclHashes FirstHashes;
10243 DeclHashes SecondHashes;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010244 const DeclContext *DC = FirstRecord;
10245 PopulateHashes(FirstHashes, FirstRecord, DC);
10246 PopulateHashes(SecondHashes, SecondRecord, DC);
Richard Trieue7f7ed22017-02-22 01:11:25 +000010247
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010248 auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10249 ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10250 ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10251 Decl *FirstDecl = DR.FirstDecl;
10252 Decl *SecondDecl = DR.SecondDecl;
Richard Trieue7f7ed22017-02-22 01:11:25 +000010253
10254 if (FirstDiffType == Other || SecondDiffType == Other) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010255 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10256 SecondModule);
Richard Trieue7f7ed22017-02-22 01:11:25 +000010257 Diagnosed = true;
10258 break;
10259 }
10260
10261 if (FirstDiffType != SecondDiffType) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010262 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10263 SecondModule);
Richard Trieue7f7ed22017-02-22 01:11:25 +000010264 Diagnosed = true;
10265 break;
10266 }
10267
Richard Trieu639d7b62017-02-22 22:22:42 +000010268 assert(FirstDiffType == SecondDiffType);
10269
Richard Trieu639d7b62017-02-22 22:22:42 +000010270 switch (FirstDiffType) {
10271 case Other:
10272 case EndOfClass:
10273 case PublicSpecifer:
10274 case PrivateSpecifer:
10275 case ProtectedSpecifer:
10276 llvm_unreachable("Invalid diff type");
10277
10278 case StaticAssert: {
Vedant Kumar48b4f762018-04-14 01:40:48 +000010279 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10280 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
Richard Trieu639d7b62017-02-22 22:22:42 +000010281
10282 Expr *FirstExpr = FirstSA->getAssertExpr();
10283 Expr *SecondExpr = SecondSA->getAssertExpr();
10284 unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10285 unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10286 if (FirstODRHash != SecondODRHash) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010287 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10288 FirstExpr->getSourceRange(), StaticAssertCondition);
10289 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10290 SecondExpr->getSourceRange(), StaticAssertCondition);
Richard Trieu639d7b62017-02-22 22:22:42 +000010291 Diagnosed = true;
10292 break;
10293 }
10294
10295 StringLiteral *FirstStr = FirstSA->getMessage();
10296 StringLiteral *SecondStr = SecondSA->getMessage();
10297 assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10298 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10299 SourceLocation FirstLoc, SecondLoc;
10300 SourceRange FirstRange, SecondRange;
10301 if (FirstStr) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010302 FirstLoc = FirstStr->getBeginLoc();
Richard Trieu639d7b62017-02-22 22:22:42 +000010303 FirstRange = FirstStr->getSourceRange();
10304 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010305 FirstLoc = FirstSA->getBeginLoc();
Richard Trieu639d7b62017-02-22 22:22:42 +000010306 FirstRange = FirstSA->getSourceRange();
10307 }
10308 if (SecondStr) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010309 SecondLoc = SecondStr->getBeginLoc();
Richard Trieu639d7b62017-02-22 22:22:42 +000010310 SecondRange = SecondStr->getSourceRange();
10311 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010312 SecondLoc = SecondSA->getBeginLoc();
Richard Trieu639d7b62017-02-22 22:22:42 +000010313 SecondRange = SecondSA->getSourceRange();
10314 }
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010315 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10316 StaticAssertOnlyMessage)
Richard Trieu639d7b62017-02-22 22:22:42 +000010317 << (FirstStr == nullptr);
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010318 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10319 StaticAssertOnlyMessage)
Richard Trieu639d7b62017-02-22 22:22:42 +000010320 << (SecondStr == nullptr);
10321 Diagnosed = true;
10322 break;
10323 }
10324
10325 if (FirstStr && SecondStr &&
10326 FirstStr->getString() != SecondStr->getString()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010327 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10328 FirstStr->getSourceRange(), StaticAssertMessage);
10329 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10330 SecondStr->getSourceRange(), StaticAssertMessage);
Richard Trieu639d7b62017-02-22 22:22:42 +000010331 Diagnosed = true;
10332 break;
10333 }
10334 break;
10335 }
Richard Trieud0786092017-02-23 00:23:01 +000010336 case Field: {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010337 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10338 cast<FieldDecl>(FirstDecl),
10339 cast<FieldDecl>(SecondDecl));
Richard Trieud0786092017-02-23 00:23:01 +000010340 break;
10341 }
Richard Trieu48143742017-02-28 21:24:38 +000010342 case CXXMethod: {
Richard Trieu1c71d512017-07-15 02:55:13 +000010343 enum {
10344 DiagMethod,
10345 DiagConstructor,
10346 DiagDestructor,
10347 } FirstMethodType,
10348 SecondMethodType;
10349 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10350 if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10351 if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10352 return DiagMethod;
10353 };
Vedant Kumar48b4f762018-04-14 01:40:48 +000010354 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10355 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
Richard Trieu1c71d512017-07-15 02:55:13 +000010356 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10357 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
Richard Trieu583e2c12017-03-04 00:08:58 +000010358 auto FirstName = FirstMethod->getDeclName();
10359 auto SecondName = SecondMethod->getDeclName();
Richard Trieu1c71d512017-07-15 02:55:13 +000010360 if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010361 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10362 FirstMethod->getSourceRange(), MethodName)
Richard Trieu1c71d512017-07-15 02:55:13 +000010363 << FirstMethodType << FirstName;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010364 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10365 SecondMethod->getSourceRange(), MethodName)
Richard Trieu1c71d512017-07-15 02:55:13 +000010366 << SecondMethodType << SecondName;
Richard Trieu48143742017-02-28 21:24:38 +000010367
10368 Diagnosed = true;
10369 break;
10370 }
10371
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010372 const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10373 const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
Richard Trieu583e2c12017-03-04 00:08:58 +000010374 if (FirstDeleted != SecondDeleted) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010375 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10376 FirstMethod->getSourceRange(), MethodDeleted)
Richard Trieu1c71d512017-07-15 02:55:13 +000010377 << FirstMethodType << FirstName << FirstDeleted;
Richard Trieu583e2c12017-03-04 00:08:58 +000010378
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010379 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10380 SecondMethod->getSourceRange(), MethodDeleted)
Richard Trieu1c71d512017-07-15 02:55:13 +000010381 << SecondMethodType << SecondName << SecondDeleted;
Richard Trieu583e2c12017-03-04 00:08:58 +000010382 Diagnosed = true;
10383 break;
10384 }
10385
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010386 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10387 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10388 if (FirstDefaulted != SecondDefaulted) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010389 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10390 FirstMethod->getSourceRange(), MethodDefaulted)
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010391 << FirstMethodType << FirstName << FirstDefaulted;
10392
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010393 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10394 SecondMethod->getSourceRange(), MethodDefaulted)
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010395 << SecondMethodType << SecondName << SecondDefaulted;
10396 Diagnosed = true;
10397 break;
10398 }
10399
Richard Trieu583e2c12017-03-04 00:08:58 +000010400 const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10401 const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10402 const bool FirstPure = FirstMethod->isPure();
10403 const bool SecondPure = SecondMethod->isPure();
10404 if ((FirstVirtual || SecondVirtual) &&
10405 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010406 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10407 FirstMethod->getSourceRange(), MethodVirtual)
Richard Trieu1c71d512017-07-15 02:55:13 +000010408 << FirstMethodType << FirstName << FirstPure << FirstVirtual;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010409 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10410 SecondMethod->getSourceRange(), MethodVirtual)
Richard Trieu1c71d512017-07-15 02:55:13 +000010411 << SecondMethodType << SecondName << SecondPure << SecondVirtual;
Richard Trieu583e2c12017-03-04 00:08:58 +000010412 Diagnosed = true;
10413 break;
10414 }
10415
10416 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging,
10417 // FirstDecl is the canonical Decl of SecondDecl, so the storage
10418 // class needs to be checked instead.
10419 const auto FirstStorage = FirstMethod->getStorageClass();
10420 const auto SecondStorage = SecondMethod->getStorageClass();
10421 const bool FirstStatic = FirstStorage == SC_Static;
10422 const bool SecondStatic = SecondStorage == SC_Static;
10423 if (FirstStatic != SecondStatic) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010424 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10425 FirstMethod->getSourceRange(), MethodStatic)
Richard Trieu1c71d512017-07-15 02:55:13 +000010426 << FirstMethodType << FirstName << FirstStatic;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010427 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10428 SecondMethod->getSourceRange(), MethodStatic)
Richard Trieu1c71d512017-07-15 02:55:13 +000010429 << SecondMethodType << SecondName << SecondStatic;
Richard Trieu583e2c12017-03-04 00:08:58 +000010430 Diagnosed = true;
10431 break;
10432 }
10433
10434 const bool FirstVolatile = FirstMethod->isVolatile();
10435 const bool SecondVolatile = SecondMethod->isVolatile();
10436 if (FirstVolatile != SecondVolatile) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010437 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10438 FirstMethod->getSourceRange(), MethodVolatile)
Richard Trieu1c71d512017-07-15 02:55:13 +000010439 << FirstMethodType << FirstName << FirstVolatile;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010440 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10441 SecondMethod->getSourceRange(), MethodVolatile)
Richard Trieu1c71d512017-07-15 02:55:13 +000010442 << SecondMethodType << SecondName << SecondVolatile;
Richard Trieu583e2c12017-03-04 00:08:58 +000010443 Diagnosed = true;
10444 break;
10445 }
10446
10447 const bool FirstConst = FirstMethod->isConst();
10448 const bool SecondConst = SecondMethod->isConst();
10449 if (FirstConst != SecondConst) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010450 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10451 FirstMethod->getSourceRange(), MethodConst)
Richard Trieu1c71d512017-07-15 02:55:13 +000010452 << FirstMethodType << FirstName << FirstConst;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010453 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10454 SecondMethod->getSourceRange(), MethodConst)
Richard Trieu1c71d512017-07-15 02:55:13 +000010455 << SecondMethodType << SecondName << SecondConst;
Richard Trieu583e2c12017-03-04 00:08:58 +000010456 Diagnosed = true;
10457 break;
10458 }
10459
10460 const bool FirstInline = FirstMethod->isInlineSpecified();
10461 const bool SecondInline = SecondMethod->isInlineSpecified();
10462 if (FirstInline != SecondInline) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010463 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10464 FirstMethod->getSourceRange(), MethodInline)
Richard Trieu1c71d512017-07-15 02:55:13 +000010465 << FirstMethodType << FirstName << FirstInline;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010466 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10467 SecondMethod->getSourceRange(), MethodInline)
Richard Trieu1c71d512017-07-15 02:55:13 +000010468 << SecondMethodType << SecondName << SecondInline;
Richard Trieu583e2c12017-03-04 00:08:58 +000010469 Diagnosed = true;
10470 break;
10471 }
10472
Richard Trieu02552272017-05-02 23:58:52 +000010473 const unsigned FirstNumParameters = FirstMethod->param_size();
10474 const unsigned SecondNumParameters = SecondMethod->param_size();
10475 if (FirstNumParameters != SecondNumParameters) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010476 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10477 FirstMethod->getSourceRange(),
10478 MethodNumberParameters)
Richard Trieu1c71d512017-07-15 02:55:13 +000010479 << FirstMethodType << FirstName << FirstNumParameters;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010480 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10481 SecondMethod->getSourceRange(),
10482 MethodNumberParameters)
Richard Trieu1c71d512017-07-15 02:55:13 +000010483 << SecondMethodType << SecondName << SecondNumParameters;
Richard Trieu02552272017-05-02 23:58:52 +000010484 Diagnosed = true;
10485 break;
10486 }
10487
10488 // Need this status boolean to know when break out of the switch.
10489 bool ParameterMismatch = false;
10490 for (unsigned I = 0; I < FirstNumParameters; ++I) {
10491 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10492 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10493
10494 QualType FirstParamType = FirstParam->getType();
10495 QualType SecondParamType = SecondParam->getType();
10496 if (FirstParamType != SecondParamType &&
10497 ComputeQualTypeODRHash(FirstParamType) !=
10498 ComputeQualTypeODRHash(SecondParamType)) {
Vedant Kumar48b4f762018-04-14 01:40:48 +000010499 if (const DecayedType *ParamDecayedType =
Richard Trieu02552272017-05-02 23:58:52 +000010500 FirstParamType->getAs<DecayedType>()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010501 ODRDiagDeclError(
10502 FirstRecord, FirstModule, FirstMethod->getLocation(),
10503 FirstMethod->getSourceRange(), MethodParameterType)
Richard Trieu1c71d512017-07-15 02:55:13 +000010504 << FirstMethodType << FirstName << (I + 1) << FirstParamType
10505 << true << ParamDecayedType->getOriginalType();
Richard Trieu02552272017-05-02 23:58:52 +000010506 } else {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010507 ODRDiagDeclError(
10508 FirstRecord, FirstModule, FirstMethod->getLocation(),
10509 FirstMethod->getSourceRange(), MethodParameterType)
Richard Trieu1c71d512017-07-15 02:55:13 +000010510 << FirstMethodType << FirstName << (I + 1) << FirstParamType
10511 << false;
Richard Trieu02552272017-05-02 23:58:52 +000010512 }
10513
Vedant Kumar48b4f762018-04-14 01:40:48 +000010514 if (const DecayedType *ParamDecayedType =
Richard Trieu02552272017-05-02 23:58:52 +000010515 SecondParamType->getAs<DecayedType>()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010516 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10517 SecondMethod->getSourceRange(),
10518 MethodParameterType)
Richard Trieu1c71d512017-07-15 02:55:13 +000010519 << SecondMethodType << SecondName << (I + 1)
10520 << SecondParamType << true
Richard Trieu02552272017-05-02 23:58:52 +000010521 << ParamDecayedType->getOriginalType();
10522 } else {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010523 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10524 SecondMethod->getSourceRange(),
10525 MethodParameterType)
Richard Trieu1c71d512017-07-15 02:55:13 +000010526 << SecondMethodType << SecondName << (I + 1)
10527 << SecondParamType << false;
Richard Trieu02552272017-05-02 23:58:52 +000010528 }
10529 ParameterMismatch = true;
10530 break;
10531 }
10532
10533 DeclarationName FirstParamName = FirstParam->getDeclName();
10534 DeclarationName SecondParamName = SecondParam->getDeclName();
10535 if (FirstParamName != SecondParamName) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010536 ODRDiagDeclError(FirstRecord, FirstModule,
10537 FirstMethod->getLocation(),
10538 FirstMethod->getSourceRange(), MethodParameterName)
Richard Trieu1c71d512017-07-15 02:55:13 +000010539 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010540 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10541 SecondMethod->getSourceRange(), MethodParameterName)
Richard Trieu1c71d512017-07-15 02:55:13 +000010542 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
Richard Trieu02552272017-05-02 23:58:52 +000010543 ParameterMismatch = true;
10544 break;
10545 }
Richard Trieu6e13ff32017-06-16 02:44:29 +000010546
10547 const Expr *FirstInit = FirstParam->getInit();
10548 const Expr *SecondInit = SecondParam->getInit();
10549 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010550 ODRDiagDeclError(FirstRecord, FirstModule,
10551 FirstMethod->getLocation(),
10552 FirstMethod->getSourceRange(),
10553 MethodParameterSingleDefaultArgument)
Richard Trieu1c71d512017-07-15 02:55:13 +000010554 << FirstMethodType << FirstName << (I + 1)
10555 << (FirstInit == nullptr)
Richard Trieu6e13ff32017-06-16 02:44:29 +000010556 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010557 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10558 SecondMethod->getSourceRange(),
10559 MethodParameterSingleDefaultArgument)
Richard Trieu1c71d512017-07-15 02:55:13 +000010560 << SecondMethodType << SecondName << (I + 1)
10561 << (SecondInit == nullptr)
Richard Trieu6e13ff32017-06-16 02:44:29 +000010562 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10563 ParameterMismatch = true;
10564 break;
10565 }
10566
10567 if (FirstInit && SecondInit &&
10568 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010569 ODRDiagDeclError(FirstRecord, FirstModule,
10570 FirstMethod->getLocation(),
10571 FirstMethod->getSourceRange(),
10572 MethodParameterDifferentDefaultArgument)
Richard Trieu1c71d512017-07-15 02:55:13 +000010573 << FirstMethodType << FirstName << (I + 1)
10574 << FirstInit->getSourceRange();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010575 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10576 SecondMethod->getSourceRange(),
10577 MethodParameterDifferentDefaultArgument)
Richard Trieu1c71d512017-07-15 02:55:13 +000010578 << SecondMethodType << SecondName << (I + 1)
10579 << SecondInit->getSourceRange();
Richard Trieu6e13ff32017-06-16 02:44:29 +000010580 ParameterMismatch = true;
10581 break;
10582
10583 }
Richard Trieu02552272017-05-02 23:58:52 +000010584 }
10585
10586 if (ParameterMismatch) {
10587 Diagnosed = true;
10588 break;
10589 }
10590
Richard Trieu7282d322018-04-25 00:31:15 +000010591 const auto *FirstTemplateArgs =
10592 FirstMethod->getTemplateSpecializationArgs();
10593 const auto *SecondTemplateArgs =
10594 SecondMethod->getTemplateSpecializationArgs();
10595
10596 if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10597 (!FirstTemplateArgs && SecondTemplateArgs)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010598 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10599 FirstMethod->getSourceRange(),
10600 MethodNoTemplateArguments)
Richard Trieu7282d322018-04-25 00:31:15 +000010601 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010602 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10603 SecondMethod->getSourceRange(),
10604 MethodNoTemplateArguments)
Richard Trieu7282d322018-04-25 00:31:15 +000010605 << SecondMethodType << SecondName
10606 << (SecondTemplateArgs != nullptr);
10607
10608 Diagnosed = true;
10609 break;
10610 }
10611
10612 if (FirstTemplateArgs && SecondTemplateArgs) {
10613 // Remove pack expansions from argument list.
10614 auto ExpandTemplateArgumentList =
10615 [](const TemplateArgumentList *TAL) {
10616 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10617 for (const TemplateArgument &TA : TAL->asArray()) {
10618 if (TA.getKind() != TemplateArgument::Pack) {
10619 ExpandedList.push_back(&TA);
10620 continue;
10621 }
10622 for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10623 ExpandedList.push_back(&PackTA);
10624 }
10625 }
10626 return ExpandedList;
10627 };
10628 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10629 ExpandTemplateArgumentList(FirstTemplateArgs);
10630 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10631 ExpandTemplateArgumentList(SecondTemplateArgs);
10632
10633 if (FirstExpandedList.size() != SecondExpandedList.size()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010634 ODRDiagDeclError(FirstRecord, FirstModule,
10635 FirstMethod->getLocation(),
10636 FirstMethod->getSourceRange(),
10637 MethodDifferentNumberTemplateArguments)
Richard Trieu7282d322018-04-25 00:31:15 +000010638 << FirstMethodType << FirstName
10639 << (unsigned)FirstExpandedList.size();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010640 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10641 SecondMethod->getSourceRange(),
10642 MethodDifferentNumberTemplateArguments)
Richard Trieu7282d322018-04-25 00:31:15 +000010643 << SecondMethodType << SecondName
10644 << (unsigned)SecondExpandedList.size();
10645
10646 Diagnosed = true;
10647 break;
10648 }
10649
10650 bool TemplateArgumentMismatch = false;
10651 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10652 const TemplateArgument &FirstTA = *FirstExpandedList[i],
10653 &SecondTA = *SecondExpandedList[i];
10654 if (ComputeTemplateArgumentODRHash(FirstTA) ==
10655 ComputeTemplateArgumentODRHash(SecondTA)) {
10656 continue;
10657 }
10658
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010659 ODRDiagDeclError(
10660 FirstRecord, FirstModule, FirstMethod->getLocation(),
10661 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
Richard Trieu7282d322018-04-25 00:31:15 +000010662 << FirstMethodType << FirstName << FirstTA << i + 1;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010663 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10664 SecondMethod->getSourceRange(),
10665 MethodDifferentTemplateArgument)
Richard Trieu7282d322018-04-25 00:31:15 +000010666 << SecondMethodType << SecondName << SecondTA << i + 1;
10667
10668 TemplateArgumentMismatch = true;
10669 break;
10670 }
10671
10672 if (TemplateArgumentMismatch) {
10673 Diagnosed = true;
10674 break;
10675 }
10676 }
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010677
10678 // Compute the hash of the method as if it has no body.
10679 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10680 Hash.clear();
10681 Hash.AddFunctionDecl(D, true /*SkipBody*/);
10682 return Hash.CalculateHash();
10683 };
10684
10685 // Compare the hash generated to the hash stored. A difference means
10686 // that a body was present in the original source. Due to merging,
10687 // the stardard way of detecting a body will not work.
10688 const bool HasFirstBody =
10689 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10690 const bool HasSecondBody =
10691 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10692
10693 if (HasFirstBody != HasSecondBody) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010694 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10695 FirstMethod->getSourceRange(), MethodSingleBody)
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010696 << FirstMethodType << FirstName << HasFirstBody;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010697 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10698 SecondMethod->getSourceRange(), MethodSingleBody)
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010699 << SecondMethodType << SecondName << HasSecondBody;
10700 Diagnosed = true;
10701 break;
10702 }
10703
10704 if (HasFirstBody && HasSecondBody) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010705 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10706 FirstMethod->getSourceRange(), MethodDifferentBody)
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010707 << FirstMethodType << FirstName;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010708 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10709 SecondMethod->getSourceRange(), MethodDifferentBody)
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010710 << SecondMethodType << SecondName;
10711 Diagnosed = true;
10712 break;
10713 }
10714
Richard Trieu48143742017-02-28 21:24:38 +000010715 break;
10716 }
Richard Trieu11d566a2017-06-12 21:58:22 +000010717 case TypeAlias:
10718 case TypeDef: {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010719 Diagnosed = ODRDiagTypeDefOrAlias(
10720 FirstRecord, FirstModule, SecondModule,
10721 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10722 FirstDiffType == TypeAlias);
Richard Trieu11d566a2017-06-12 21:58:22 +000010723 break;
10724 }
Richard Trieu6e13ff32017-06-16 02:44:29 +000010725 case Var: {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010726 Diagnosed =
10727 ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10728 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
Richard Trieu6e13ff32017-06-16 02:44:29 +000010729 break;
10730 }
Richard Trieuac6a1b62017-07-08 02:04:42 +000010731 case Friend: {
Vedant Kumar48b4f762018-04-14 01:40:48 +000010732 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10733 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
Richard Trieuac6a1b62017-07-08 02:04:42 +000010734
10735 NamedDecl *FirstND = FirstFriend->getFriendDecl();
10736 NamedDecl *SecondND = SecondFriend->getFriendDecl();
10737
10738 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10739 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10740
10741 if (FirstND && SecondND) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010742 ODRDiagDeclError(FirstRecord, FirstModule,
10743 FirstFriend->getFriendLoc(),
10744 FirstFriend->getSourceRange(), FriendFunction)
Richard Trieuac6a1b62017-07-08 02:04:42 +000010745 << FirstND;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010746 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10747 SecondFriend->getSourceRange(), FriendFunction)
Richard Trieuac6a1b62017-07-08 02:04:42 +000010748 << SecondND;
10749
10750 Diagnosed = true;
10751 break;
10752 }
10753
10754 if (FirstTSI && SecondTSI) {
10755 QualType FirstFriendType = FirstTSI->getType();
10756 QualType SecondFriendType = SecondTSI->getType();
10757 assert(ComputeQualTypeODRHash(FirstFriendType) !=
10758 ComputeQualTypeODRHash(SecondFriendType));
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010759 ODRDiagDeclError(FirstRecord, FirstModule,
10760 FirstFriend->getFriendLoc(),
10761 FirstFriend->getSourceRange(), FriendType)
Richard Trieuac6a1b62017-07-08 02:04:42 +000010762 << FirstFriendType;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010763 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10764 SecondFriend->getSourceRange(), FriendType)
Richard Trieuac6a1b62017-07-08 02:04:42 +000010765 << SecondFriendType;
10766 Diagnosed = true;
10767 break;
10768 }
10769
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010770 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10771 FirstFriend->getSourceRange(), FriendTypeFunction)
Richard Trieuac6a1b62017-07-08 02:04:42 +000010772 << (FirstTSI == nullptr);
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010773 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10774 SecondFriend->getSourceRange(), FriendTypeFunction)
Richard Trieuac6a1b62017-07-08 02:04:42 +000010775 << (SecondTSI == nullptr);
10776
10777 Diagnosed = true;
10778 break;
10779 }
Richard Trieu9359e8f2018-05-30 01:12:26 +000010780 case FunctionTemplate: {
10781 FunctionTemplateDecl *FirstTemplate =
10782 cast<FunctionTemplateDecl>(FirstDecl);
10783 FunctionTemplateDecl *SecondTemplate =
10784 cast<FunctionTemplateDecl>(SecondDecl);
10785
10786 TemplateParameterList *FirstTPL =
10787 FirstTemplate->getTemplateParameters();
10788 TemplateParameterList *SecondTPL =
10789 SecondTemplate->getTemplateParameters();
10790
10791 if (FirstTPL->size() != SecondTPL->size()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010792 ODRDiagDeclError(FirstRecord, FirstModule,
10793 FirstTemplate->getLocation(),
10794 FirstTemplate->getSourceRange(),
10795 FunctionTemplateDifferentNumberParameters)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010796 << FirstTemplate << FirstTPL->size();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010797 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10798 SecondTemplate->getSourceRange(),
10799 FunctionTemplateDifferentNumberParameters)
10800 << SecondTemplate << SecondTPL->size();
Richard Trieu9359e8f2018-05-30 01:12:26 +000010801
10802 Diagnosed = true;
10803 break;
10804 }
10805
10806 bool ParameterMismatch = false;
10807 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10808 NamedDecl *FirstParam = FirstTPL->getParam(i);
10809 NamedDecl *SecondParam = SecondTPL->getParam(i);
10810
10811 if (FirstParam->getKind() != SecondParam->getKind()) {
10812 enum {
10813 TemplateTypeParameter,
10814 NonTypeTemplateParameter,
10815 TemplateTemplateParameter,
10816 };
10817 auto GetParamType = [](NamedDecl *D) {
10818 switch (D->getKind()) {
10819 default:
10820 llvm_unreachable("Unexpected template parameter type");
10821 case Decl::TemplateTypeParm:
10822 return TemplateTypeParameter;
10823 case Decl::NonTypeTemplateParm:
10824 return NonTypeTemplateParameter;
10825 case Decl::TemplateTemplateParm:
10826 return TemplateTemplateParameter;
10827 }
10828 };
10829
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010830 ODRDiagDeclError(FirstRecord, FirstModule,
10831 FirstTemplate->getLocation(),
10832 FirstTemplate->getSourceRange(),
10833 FunctionTemplateParameterDifferentKind)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010834 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010835 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10836 SecondTemplate->getSourceRange(),
10837 FunctionTemplateParameterDifferentKind)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010838 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10839
10840 ParameterMismatch = true;
10841 break;
10842 }
10843
10844 if (FirstParam->getName() != SecondParam->getName()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010845 ODRDiagDeclError(
10846 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10847 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010848 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10849 << FirstParam;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010850 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10851 SecondTemplate->getSourceRange(),
10852 FunctionTemplateParameterName)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010853 << SecondTemplate << (i + 1)
10854 << (bool)SecondParam->getIdentifier() << SecondParam;
10855 ParameterMismatch = true;
10856 break;
10857 }
10858
10859 if (isa<TemplateTypeParmDecl>(FirstParam) &&
10860 isa<TemplateTypeParmDecl>(SecondParam)) {
10861 TemplateTypeParmDecl *FirstTTPD =
10862 cast<TemplateTypeParmDecl>(FirstParam);
10863 TemplateTypeParmDecl *SecondTTPD =
10864 cast<TemplateTypeParmDecl>(SecondParam);
10865 bool HasFirstDefaultArgument =
10866 FirstTTPD->hasDefaultArgument() &&
10867 !FirstTTPD->defaultArgumentWasInherited();
10868 bool HasSecondDefaultArgument =
10869 SecondTTPD->hasDefaultArgument() &&
10870 !SecondTTPD->defaultArgumentWasInherited();
10871 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010872 ODRDiagDeclError(FirstRecord, FirstModule,
10873 FirstTemplate->getLocation(),
10874 FirstTemplate->getSourceRange(),
10875 FunctionTemplateParameterSingleDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010876 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010877 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10878 SecondTemplate->getSourceRange(),
10879 FunctionTemplateParameterSingleDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010880 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10881 ParameterMismatch = true;
10882 break;
10883 }
10884
10885 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10886 QualType FirstType = FirstTTPD->getDefaultArgument();
10887 QualType SecondType = SecondTTPD->getDefaultArgument();
10888 if (ComputeQualTypeODRHash(FirstType) !=
10889 ComputeQualTypeODRHash(SecondType)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010890 ODRDiagDeclError(
10891 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10892 FirstTemplate->getSourceRange(),
10893 FunctionTemplateParameterDifferentDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010894 << FirstTemplate << (i + 1) << FirstType;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010895 ODRDiagDeclNote(
10896 SecondModule, SecondTemplate->getLocation(),
10897 SecondTemplate->getSourceRange(),
10898 FunctionTemplateParameterDifferentDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010899 << SecondTemplate << (i + 1) << SecondType;
10900 ParameterMismatch = true;
10901 break;
10902 }
10903 }
10904
10905 if (FirstTTPD->isParameterPack() !=
10906 SecondTTPD->isParameterPack()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010907 ODRDiagDeclError(FirstRecord, FirstModule,
10908 FirstTemplate->getLocation(),
10909 FirstTemplate->getSourceRange(),
10910 FunctionTemplatePackParameter)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010911 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010912 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10913 SecondTemplate->getSourceRange(),
10914 FunctionTemplatePackParameter)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010915 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10916 ParameterMismatch = true;
10917 break;
10918 }
10919 }
10920
10921 if (isa<TemplateTemplateParmDecl>(FirstParam) &&
10922 isa<TemplateTemplateParmDecl>(SecondParam)) {
10923 TemplateTemplateParmDecl *FirstTTPD =
10924 cast<TemplateTemplateParmDecl>(FirstParam);
10925 TemplateTemplateParmDecl *SecondTTPD =
10926 cast<TemplateTemplateParmDecl>(SecondParam);
10927
10928 TemplateParameterList *FirstTPL =
10929 FirstTTPD->getTemplateParameters();
10930 TemplateParameterList *SecondTPL =
10931 SecondTTPD->getTemplateParameters();
10932
10933 if (ComputeTemplateParameterListODRHash(FirstTPL) !=
10934 ComputeTemplateParameterListODRHash(SecondTPL)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010935 ODRDiagDeclError(FirstRecord, FirstModule,
10936 FirstTemplate->getLocation(),
10937 FirstTemplate->getSourceRange(),
10938 FunctionTemplateParameterDifferentType)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010939 << FirstTemplate << (i + 1);
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010940 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10941 SecondTemplate->getSourceRange(),
10942 FunctionTemplateParameterDifferentType)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010943 << SecondTemplate << (i + 1);
10944 ParameterMismatch = true;
10945 break;
10946 }
10947
10948 bool HasFirstDefaultArgument =
10949 FirstTTPD->hasDefaultArgument() &&
10950 !FirstTTPD->defaultArgumentWasInherited();
10951 bool HasSecondDefaultArgument =
10952 SecondTTPD->hasDefaultArgument() &&
10953 !SecondTTPD->defaultArgumentWasInherited();
10954 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010955 ODRDiagDeclError(FirstRecord, FirstModule,
10956 FirstTemplate->getLocation(),
10957 FirstTemplate->getSourceRange(),
10958 FunctionTemplateParameterSingleDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010959 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010960 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10961 SecondTemplate->getSourceRange(),
10962 FunctionTemplateParameterSingleDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010963 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10964 ParameterMismatch = true;
10965 break;
10966 }
10967
10968 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10969 TemplateArgument FirstTA =
10970 FirstTTPD->getDefaultArgument().getArgument();
10971 TemplateArgument SecondTA =
10972 SecondTTPD->getDefaultArgument().getArgument();
10973 if (ComputeTemplateArgumentODRHash(FirstTA) !=
10974 ComputeTemplateArgumentODRHash(SecondTA)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010975 ODRDiagDeclError(
10976 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10977 FirstTemplate->getSourceRange(),
10978 FunctionTemplateParameterDifferentDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010979 << FirstTemplate << (i + 1) << FirstTA;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010980 ODRDiagDeclNote(
10981 SecondModule, SecondTemplate->getLocation(),
10982 SecondTemplate->getSourceRange(),
10983 FunctionTemplateParameterDifferentDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010984 << SecondTemplate << (i + 1) << SecondTA;
10985 ParameterMismatch = true;
10986 break;
10987 }
10988 }
10989
10990 if (FirstTTPD->isParameterPack() !=
10991 SecondTTPD->isParameterPack()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010992 ODRDiagDeclError(FirstRecord, FirstModule,
10993 FirstTemplate->getLocation(),
10994 FirstTemplate->getSourceRange(),
10995 FunctionTemplatePackParameter)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010996 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010997 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10998 SecondTemplate->getSourceRange(),
10999 FunctionTemplatePackParameter)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011000 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11001 ParameterMismatch = true;
11002 break;
11003 }
11004 }
11005
11006 if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11007 isa<NonTypeTemplateParmDecl>(SecondParam)) {
11008 NonTypeTemplateParmDecl *FirstNTTPD =
11009 cast<NonTypeTemplateParmDecl>(FirstParam);
11010 NonTypeTemplateParmDecl *SecondNTTPD =
11011 cast<NonTypeTemplateParmDecl>(SecondParam);
11012
11013 QualType FirstType = FirstNTTPD->getType();
11014 QualType SecondType = SecondNTTPD->getType();
11015 if (ComputeQualTypeODRHash(FirstType) !=
11016 ComputeQualTypeODRHash(SecondType)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011017 ODRDiagDeclError(FirstRecord, FirstModule,
11018 FirstTemplate->getLocation(),
11019 FirstTemplate->getSourceRange(),
11020 FunctionTemplateParameterDifferentType)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011021 << FirstTemplate << (i + 1);
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011022 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11023 SecondTemplate->getSourceRange(),
11024 FunctionTemplateParameterDifferentType)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011025 << SecondTemplate << (i + 1);
11026 ParameterMismatch = true;
11027 break;
11028 }
11029
11030 bool HasFirstDefaultArgument =
11031 FirstNTTPD->hasDefaultArgument() &&
11032 !FirstNTTPD->defaultArgumentWasInherited();
11033 bool HasSecondDefaultArgument =
11034 SecondNTTPD->hasDefaultArgument() &&
11035 !SecondNTTPD->defaultArgumentWasInherited();
11036 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011037 ODRDiagDeclError(FirstRecord, FirstModule,
11038 FirstTemplate->getLocation(),
11039 FirstTemplate->getSourceRange(),
11040 FunctionTemplateParameterSingleDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011041 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011042 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11043 SecondTemplate->getSourceRange(),
11044 FunctionTemplateParameterSingleDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011045 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11046 ParameterMismatch = true;
11047 break;
11048 }
11049
11050 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11051 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11052 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11053 if (ComputeODRHash(FirstDefaultArgument) !=
11054 ComputeODRHash(SecondDefaultArgument)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011055 ODRDiagDeclError(
11056 FirstRecord, FirstModule, FirstTemplate->getLocation(),
11057 FirstTemplate->getSourceRange(),
11058 FunctionTemplateParameterDifferentDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011059 << FirstTemplate << (i + 1) << FirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011060 ODRDiagDeclNote(
11061 SecondModule, SecondTemplate->getLocation(),
11062 SecondTemplate->getSourceRange(),
11063 FunctionTemplateParameterDifferentDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011064 << SecondTemplate << (i + 1) << SecondDefaultArgument;
11065 ParameterMismatch = true;
11066 break;
11067 }
11068 }
11069
11070 if (FirstNTTPD->isParameterPack() !=
11071 SecondNTTPD->isParameterPack()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011072 ODRDiagDeclError(FirstRecord, FirstModule,
11073 FirstTemplate->getLocation(),
11074 FirstTemplate->getSourceRange(),
11075 FunctionTemplatePackParameter)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011076 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011077 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11078 SecondTemplate->getSourceRange(),
11079 FunctionTemplatePackParameter)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011080 << SecondTemplate << (i + 1)
11081 << SecondNTTPD->isParameterPack();
11082 ParameterMismatch = true;
11083 break;
11084 }
11085 }
11086 }
11087
11088 if (ParameterMismatch) {
11089 Diagnosed = true;
11090 break;
11091 }
11092
11093 break;
11094 }
Richard Trieu639d7b62017-02-22 22:22:42 +000011095 }
11096
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000011097 if (Diagnosed)
Richard Trieue7f7ed22017-02-22 01:11:25 +000011098 continue;
11099
Richard Trieu708859a2017-06-08 00:56:21 +000011100 Diag(FirstDecl->getLocation(),
11101 diag::err_module_odr_violation_mismatch_decl_unknown)
11102 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11103 << FirstDecl->getSourceRange();
11104 Diag(SecondDecl->getLocation(),
11105 diag::note_module_odr_violation_mismatch_decl_unknown)
11106 << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
Richard Trieue7f7ed22017-02-22 01:11:25 +000011107 Diagnosed = true;
Richard Smithcd45dbc2014-04-19 03:48:30 +000011108 }
11109
11110 if (!Diagnosed) {
11111 // All definitions are updates to the same declaration. This happens if a
11112 // module instantiates the declaration of a class template specialization
11113 // and two or more other modules instantiate its definition.
11114 //
11115 // FIXME: Indicate which modules had instantiations of this definition.
11116 // FIXME: How can this even happen?
11117 Diag(Merge.first->getLocation(),
11118 diag::err_module_odr_violation_different_instantiations)
11119 << Merge.first;
11120 }
11121 }
Richard Trieue6caa262017-12-23 00:41:01 +000011122
11123 // Issue ODR failures diagnostics for functions.
11124 for (auto &Merge : FunctionOdrMergeFailures) {
11125 enum ODRFunctionDifference {
11126 ReturnType,
11127 ParameterName,
11128 ParameterType,
11129 ParameterSingleDefaultArgument,
11130 ParameterDifferentDefaultArgument,
11131 FunctionBody,
11132 };
11133
11134 FunctionDecl *FirstFunction = Merge.first;
11135 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11136
11137 bool Diagnosed = false;
11138 for (auto &SecondFunction : Merge.second) {
Vedant Kumar48b4f762018-04-14 01:40:48 +000011139
Richard Trieue6caa262017-12-23 00:41:01 +000011140 if (FirstFunction == SecondFunction)
11141 continue;
11142
11143 std::string SecondModule =
11144 getOwningModuleNameForDiagnostic(SecondFunction);
11145
11146 auto ODRDiagError = [FirstFunction, &FirstModule,
11147 this](SourceLocation Loc, SourceRange Range,
11148 ODRFunctionDifference DiffType) {
11149 return Diag(Loc, diag::err_module_odr_violation_function)
11150 << FirstFunction << FirstModule.empty() << FirstModule << Range
11151 << DiffType;
11152 };
11153 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11154 SourceRange Range,
11155 ODRFunctionDifference DiffType) {
11156 return Diag(Loc, diag::note_module_odr_violation_function)
11157 << SecondModule << Range << DiffType;
11158 };
11159
11160 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11161 ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11162 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11163 FirstFunction->getReturnTypeSourceRange(), ReturnType)
11164 << FirstFunction->getReturnType();
11165 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11166 SecondFunction->getReturnTypeSourceRange(), ReturnType)
11167 << SecondFunction->getReturnType();
11168 Diagnosed = true;
11169 break;
11170 }
11171
11172 assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11173 "Merged functions with different number of parameters");
11174
11175 auto ParamSize = FirstFunction->param_size();
11176 bool ParameterMismatch = false;
11177 for (unsigned I = 0; I < ParamSize; ++I) {
11178 auto *FirstParam = FirstFunction->getParamDecl(I);
11179 auto *SecondParam = SecondFunction->getParamDecl(I);
11180
11181 assert(getContext().hasSameType(FirstParam->getType(),
11182 SecondParam->getType()) &&
11183 "Merged function has different parameter types.");
11184
11185 if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11186 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11187 ParameterName)
11188 << I + 1 << FirstParam->getDeclName();
11189 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11190 ParameterName)
11191 << I + 1 << SecondParam->getDeclName();
11192 ParameterMismatch = true;
11193 break;
11194 };
11195
11196 QualType FirstParamType = FirstParam->getType();
11197 QualType SecondParamType = SecondParam->getType();
11198 if (FirstParamType != SecondParamType &&
11199 ComputeQualTypeODRHash(FirstParamType) !=
11200 ComputeQualTypeODRHash(SecondParamType)) {
Vedant Kumar48b4f762018-04-14 01:40:48 +000011201 if (const DecayedType *ParamDecayedType =
Richard Trieue6caa262017-12-23 00:41:01 +000011202 FirstParamType->getAs<DecayedType>()) {
11203 ODRDiagError(FirstParam->getLocation(),
11204 FirstParam->getSourceRange(), ParameterType)
11205 << (I + 1) << FirstParamType << true
11206 << ParamDecayedType->getOriginalType();
11207 } else {
11208 ODRDiagError(FirstParam->getLocation(),
11209 FirstParam->getSourceRange(), ParameterType)
11210 << (I + 1) << FirstParamType << false;
11211 }
11212
Vedant Kumar48b4f762018-04-14 01:40:48 +000011213 if (const DecayedType *ParamDecayedType =
Richard Trieue6caa262017-12-23 00:41:01 +000011214 SecondParamType->getAs<DecayedType>()) {
11215 ODRDiagNote(SecondParam->getLocation(),
11216 SecondParam->getSourceRange(), ParameterType)
11217 << (I + 1) << SecondParamType << true
11218 << ParamDecayedType->getOriginalType();
11219 } else {
11220 ODRDiagNote(SecondParam->getLocation(),
11221 SecondParam->getSourceRange(), ParameterType)
11222 << (I + 1) << SecondParamType << false;
11223 }
11224 ParameterMismatch = true;
11225 break;
11226 }
11227
11228 const Expr *FirstInit = FirstParam->getInit();
11229 const Expr *SecondInit = SecondParam->getInit();
11230 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11231 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11232 ParameterSingleDefaultArgument)
11233 << (I + 1) << (FirstInit == nullptr)
11234 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11235 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11236 ParameterSingleDefaultArgument)
11237 << (I + 1) << (SecondInit == nullptr)
11238 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11239 ParameterMismatch = true;
11240 break;
11241 }
11242
11243 if (FirstInit && SecondInit &&
11244 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11245 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11246 ParameterDifferentDefaultArgument)
11247 << (I + 1) << FirstInit->getSourceRange();
11248 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11249 ParameterDifferentDefaultArgument)
11250 << (I + 1) << SecondInit->getSourceRange();
11251 ParameterMismatch = true;
11252 break;
11253 }
11254
11255 assert(ComputeSubDeclODRHash(FirstParam) ==
11256 ComputeSubDeclODRHash(SecondParam) &&
11257 "Undiagnosed parameter difference.");
11258 }
11259
11260 if (ParameterMismatch) {
11261 Diagnosed = true;
11262 break;
11263 }
11264
11265 // If no error has been generated before now, assume the problem is in
11266 // the body and generate a message.
11267 ODRDiagError(FirstFunction->getLocation(),
11268 FirstFunction->getSourceRange(), FunctionBody);
11269 ODRDiagNote(SecondFunction->getLocation(),
11270 SecondFunction->getSourceRange(), FunctionBody);
11271 Diagnosed = true;
11272 break;
11273 }
Evgeny Stupachenkobf25d672018-01-05 02:22:52 +000011274 (void)Diagnosed;
Richard Trieue6caa262017-12-23 00:41:01 +000011275 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11276 }
Richard Trieuab4d7302018-07-25 22:52:05 +000011277
11278 // Issue ODR failures diagnostics for enums.
11279 for (auto &Merge : EnumOdrMergeFailures) {
11280 enum ODREnumDifference {
11281 SingleScopedEnum,
11282 EnumTagKeywordMismatch,
11283 SingleSpecifiedType,
11284 DifferentSpecifiedTypes,
11285 DifferentNumberEnumConstants,
11286 EnumConstantName,
11287 EnumConstantSingleInitilizer,
11288 EnumConstantDifferentInitilizer,
11289 };
11290
11291 // If we've already pointed out a specific problem with this enum, don't
11292 // bother issuing a general "something's different" diagnostic.
11293 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11294 continue;
11295
11296 EnumDecl *FirstEnum = Merge.first;
11297 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11298
11299 using DeclHashes =
11300 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11301 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11302 DeclHashes &Hashes, EnumDecl *Enum) {
11303 for (auto *D : Enum->decls()) {
11304 // Due to decl merging, the first EnumDecl is the parent of
11305 // Decls in both records.
11306 if (!ODRHash::isWhitelistedDecl(D, FirstEnum))
11307 continue;
11308 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11309 Hashes.emplace_back(cast<EnumConstantDecl>(D),
11310 ComputeSubDeclODRHash(D));
11311 }
11312 };
11313 DeclHashes FirstHashes;
11314 PopulateHashes(FirstHashes, FirstEnum);
11315 bool Diagnosed = false;
11316 for (auto &SecondEnum : Merge.second) {
11317
11318 if (FirstEnum == SecondEnum)
11319 continue;
11320
11321 std::string SecondModule =
11322 getOwningModuleNameForDiagnostic(SecondEnum);
11323
11324 auto ODRDiagError = [FirstEnum, &FirstModule,
11325 this](SourceLocation Loc, SourceRange Range,
11326 ODREnumDifference DiffType) {
11327 return Diag(Loc, diag::err_module_odr_violation_enum)
11328 << FirstEnum << FirstModule.empty() << FirstModule << Range
11329 << DiffType;
11330 };
11331 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11332 SourceRange Range,
11333 ODREnumDifference DiffType) {
11334 return Diag(Loc, diag::note_module_odr_violation_enum)
11335 << SecondModule << Range << DiffType;
11336 };
11337
11338 if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11339 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11340 SingleScopedEnum)
11341 << FirstEnum->isScoped();
11342 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11343 SingleScopedEnum)
11344 << SecondEnum->isScoped();
11345 Diagnosed = true;
11346 continue;
11347 }
11348
11349 if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11350 if (FirstEnum->isScopedUsingClassTag() !=
11351 SecondEnum->isScopedUsingClassTag()) {
11352 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11353 EnumTagKeywordMismatch)
11354 << FirstEnum->isScopedUsingClassTag();
11355 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11356 EnumTagKeywordMismatch)
11357 << SecondEnum->isScopedUsingClassTag();
11358 Diagnosed = true;
11359 continue;
11360 }
11361 }
11362
11363 QualType FirstUnderlyingType =
11364 FirstEnum->getIntegerTypeSourceInfo()
11365 ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11366 : QualType();
11367 QualType SecondUnderlyingType =
11368 SecondEnum->getIntegerTypeSourceInfo()
11369 ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11370 : QualType();
11371 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11372 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11373 SingleSpecifiedType)
11374 << !FirstUnderlyingType.isNull();
11375 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11376 SingleSpecifiedType)
11377 << !SecondUnderlyingType.isNull();
11378 Diagnosed = true;
11379 continue;
11380 }
11381
11382 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11383 if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11384 ComputeQualTypeODRHash(SecondUnderlyingType)) {
11385 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11386 DifferentSpecifiedTypes)
11387 << FirstUnderlyingType;
11388 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11389 DifferentSpecifiedTypes)
11390 << SecondUnderlyingType;
11391 Diagnosed = true;
11392 continue;
11393 }
11394 }
11395
11396 DeclHashes SecondHashes;
11397 PopulateHashes(SecondHashes, SecondEnum);
11398
11399 if (FirstHashes.size() != SecondHashes.size()) {
11400 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11401 DifferentNumberEnumConstants)
11402 << (int)FirstHashes.size();
11403 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11404 DifferentNumberEnumConstants)
11405 << (int)SecondHashes.size();
11406 Diagnosed = true;
11407 continue;
11408 }
11409
11410 for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11411 if (FirstHashes[I].second == SecondHashes[I].second)
11412 continue;
11413 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11414 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11415
11416 if (FirstEnumConstant->getDeclName() !=
11417 SecondEnumConstant->getDeclName()) {
11418
11419 ODRDiagError(FirstEnumConstant->getLocation(),
11420 FirstEnumConstant->getSourceRange(), EnumConstantName)
11421 << I + 1 << FirstEnumConstant;
11422 ODRDiagNote(SecondEnumConstant->getLocation(),
11423 SecondEnumConstant->getSourceRange(), EnumConstantName)
11424 << I + 1 << SecondEnumConstant;
11425 Diagnosed = true;
11426 break;
11427 }
11428
11429 const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11430 const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11431 if (!FirstInit && !SecondInit)
11432 continue;
11433
11434 if (!FirstInit || !SecondInit) {
11435 ODRDiagError(FirstEnumConstant->getLocation(),
11436 FirstEnumConstant->getSourceRange(),
11437 EnumConstantSingleInitilizer)
11438 << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11439 ODRDiagNote(SecondEnumConstant->getLocation(),
11440 SecondEnumConstant->getSourceRange(),
11441 EnumConstantSingleInitilizer)
11442 << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11443 Diagnosed = true;
11444 break;
11445 }
11446
11447 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11448 ODRDiagError(FirstEnumConstant->getLocation(),
11449 FirstEnumConstant->getSourceRange(),
11450 EnumConstantDifferentInitilizer)
11451 << I + 1 << FirstEnumConstant;
11452 ODRDiagNote(SecondEnumConstant->getLocation(),
11453 SecondEnumConstant->getSourceRange(),
11454 EnumConstantDifferentInitilizer)
11455 << I + 1 << SecondEnumConstant;
11456 Diagnosed = true;
11457 break;
11458 }
11459 }
11460 }
11461
11462 (void)Diagnosed;
11463 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11464 }
Guy Benyei11169dd2012-12-18 14:30:41 +000011465}
11466
Richard Smithce18a182015-07-14 00:26:00 +000011467void ASTReader::StartedDeserializing() {
David L. Jonesc4808b9e2016-12-15 20:53:26 +000011468 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
Richard Smithce18a182015-07-14 00:26:00 +000011469 ReadTimer->startTimer();
11470}
11471
Guy Benyei11169dd2012-12-18 14:30:41 +000011472void ASTReader::FinishedDeserializing() {
11473 assert(NumCurrentElementsDeserializing &&
11474 "FinishedDeserializing not paired with StartedDeserializing");
11475 if (NumCurrentElementsDeserializing == 1) {
11476 // We decrease NumCurrentElementsDeserializing only after pending actions
11477 // are finished, to avoid recursively re-calling finishPendingActions().
11478 finishPendingActions();
11479 }
11480 --NumCurrentElementsDeserializing;
11481
Richard Smitha0ce9c42014-07-29 23:23:27 +000011482 if (NumCurrentElementsDeserializing == 0) {
Richard Smitha62d1982018-08-03 01:00:01 +000011483 // Propagate exception specification and deduced type updates along
11484 // redeclaration chains.
11485 //
11486 // We do this now rather than in finishPendingActions because we want to
11487 // be able to walk the complete redeclaration chains of the updated decls.
11488 while (!PendingExceptionSpecUpdates.empty() ||
11489 !PendingDeducedTypeUpdates.empty()) {
11490 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
Richard Smith7226f2a2015-03-23 19:54:56 +000011491 PendingExceptionSpecUpdates.clear();
Richard Smitha62d1982018-08-03 01:00:01 +000011492 for (auto Update : ESUpdates) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +000011493 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Richard Smith7226f2a2015-03-23 19:54:56 +000011494 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
Richard Smith1d0f1992015-08-19 21:09:32 +000011495 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
Richard Smithdbafb6c2017-06-29 23:23:46 +000011496 if (auto *Listener = getContext().getASTMutationListener())
Richard Smithd88a7f12015-09-01 20:35:42 +000011497 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
Richard Smith1d0f1992015-08-19 21:09:32 +000011498 for (auto *Redecl : Update.second->redecls())
Richard Smithdbafb6c2017-06-29 23:23:46 +000011499 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
Richard Smith7226f2a2015-03-23 19:54:56 +000011500 }
Richard Smitha62d1982018-08-03 01:00:01 +000011501
11502 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11503 PendingDeducedTypeUpdates.clear();
11504 for (auto Update : DTUpdates) {
11505 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11506 // FIXME: If the return type is already deduced, check that it matches.
11507 getContext().adjustDeducedFunctionResultType(Update.first,
11508 Update.second);
11509 }
Richard Smith9e2341d2015-03-23 03:25:59 +000011510 }
11511
Richard Smithce18a182015-07-14 00:26:00 +000011512 if (ReadTimer)
11513 ReadTimer->stopTimer();
11514
Richard Smith0f4e2c42015-08-06 04:23:48 +000011515 diagnoseOdrViolations();
11516
Richard Smith04d05b52014-03-23 00:27:18 +000011517 // We are not in recursive loading, so it's safe to pass the "interesting"
11518 // decls to the consumer.
Richard Smitha0ce9c42014-07-29 23:23:27 +000011519 if (Consumer)
11520 PassInterestingDeclsToConsumer();
Guy Benyei11169dd2012-12-18 14:30:41 +000011521 }
11522}
11523
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +000011524void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
Richard Smith9e2341d2015-03-23 03:25:59 +000011525 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11526 // Remove any fake results before adding any real ones.
11527 auto It = PendingFakeLookupResults.find(II);
11528 if (It != PendingFakeLookupResults.end()) {
Richard Smitha534a312015-07-21 23:54:07 +000011529 for (auto *ND : It->second)
Richard Smith9e2341d2015-03-23 03:25:59 +000011530 SemaObj->IdResolver.RemoveDecl(ND);
Ben Langmuireb8bd2d2015-04-10 22:25:42 +000011531 // FIXME: this works around module+PCH performance issue.
11532 // Rather than erase the result from the map, which is O(n), just clear
11533 // the vector of NamedDecls.
11534 It->second.clear();
Richard Smith9e2341d2015-03-23 03:25:59 +000011535 }
11536 }
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +000011537
11538 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11539 SemaObj->TUScope->AddDecl(D);
11540 } else if (SemaObj->TUScope) {
11541 // Adding the decl to IdResolver may have failed because it was already in
11542 // (even though it was not added in scope). If it is already in, make sure
11543 // it gets in the scope as well.
11544 if (std::find(SemaObj->IdResolver.begin(Name),
11545 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11546 SemaObj->TUScope->AddDecl(D);
11547 }
11548}
11549
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +000011550ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11551 ASTContext *Context,
David Blaikie61137e12017-01-05 18:23:18 +000011552 const PCHContainerReader &PCHContainerRdr,
11553 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11554 StringRef isysroot, bool DisableValidation,
11555 bool AllowASTWithCompilerErrors,
11556 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +000011557 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
David Blaikie61137e12017-01-05 18:23:18 +000011558 std::unique_ptr<llvm::Timer> ReadTimer)
11559 : Listener(DisableValidation
11560 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11561 : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
David Blaikie61137e12017-01-05 18:23:18 +000011562 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
David Blaikie9d7c1ba2017-01-05 18:45:43 +000011563 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +000011564 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11565 PCHContainerRdr, PP.getHeaderSearchInfo()),
11566 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
David Blaikie61137e12017-01-05 18:23:18 +000011567 DisableValidation(DisableValidation),
Nico Weber824285e2014-05-08 04:26:47 +000011568 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11569 AllowConfigurationMismatch(AllowConfigurationMismatch),
11570 ValidateSystemInputs(ValidateSystemInputs),
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +000011571 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
David Blaikie9d7c1ba2017-01-05 18:45:43 +000011572 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
Guy Benyei11169dd2012-12-18 14:30:41 +000011573 SourceMgr.setExternalSLocEntrySource(this);
Douglas Gregor6623e1f2015-11-03 18:33:07 +000011574
11575 for (const auto &Ext : Extensions) {
11576 auto BlockName = Ext->getExtensionMetadata().BlockName;
11577 auto Known = ModuleFileExtensions.find(BlockName);
11578 if (Known != ModuleFileExtensions.end()) {
11579 Diags.Report(diag::warn_duplicate_module_file_extension)
11580 << BlockName;
11581 continue;
11582 }
11583
11584 ModuleFileExtensions.insert({BlockName, Ext});
11585 }
Guy Benyei11169dd2012-12-18 14:30:41 +000011586}
11587
11588ASTReader::~ASTReader() {
Nico Weber824285e2014-05-08 04:26:47 +000011589 if (OwnsDeserializationListener)
11590 delete DeserializationListener;
Guy Benyei11169dd2012-12-18 14:30:41 +000011591}
Richard Smith10379092016-05-06 23:14:07 +000011592
11593IdentifierResolver &ASTReader::getIdResolver() {
11594 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11595}
David L. Jonesbe1557a2016-12-21 00:17:49 +000011596
JF Bastien0e828952019-06-26 19:50:12 +000011597Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11598 unsigned AbbrevID) {
David L. Jonesbe1557a2016-12-21 00:17:49 +000011599 Idx = 0;
11600 Record.clear();
11601 return Cursor.readRecord(AbbrevID, Record);
11602}
Kelvin Libe286f52018-09-15 13:54:15 +000011603//===----------------------------------------------------------------------===//
11604//// OMPClauseReader implementation
11605////===----------------------------------------------------------------------===//
11606
John McCallc2f18312019-12-14 03:01:28 -050011607// This has to be in namespace clang because it's friended by all
11608// of the OMP clauses.
11609namespace clang {
11610
11611class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11612 ASTRecordReader &Record;
11613 ASTContext &Context;
11614
11615public:
11616 OMPClauseReader(ASTRecordReader &Record)
11617 : Record(Record), Context(Record.getContext()) {}
11618
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011619#define OPENMP_CLAUSE(Name, Class) void Visit##Class(Class *C);
11620#include "clang/Basic/OpenMPKinds.def"
John McCallc2f18312019-12-14 03:01:28 -050011621 OMPClause *readClause();
11622 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11623 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11624};
11625
11626} // end namespace clang
11627
11628OMPClause *ASTRecordReader::readOMPClause() {
11629 return OMPClauseReader(*this).readClause();
11630}
11631
Kelvin Libe286f52018-09-15 13:54:15 +000011632OMPClause *OMPClauseReader::readClause() {
Simon Pilgrim556fbfe2019-09-15 16:05:20 +000011633 OMPClause *C = nullptr;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011634 switch (Record.readInt()) {
11635 case OMPC_if:
Kelvin Libe286f52018-09-15 13:54:15 +000011636 C = new (Context) OMPIfClause();
11637 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011638 case OMPC_final:
Kelvin Libe286f52018-09-15 13:54:15 +000011639 C = new (Context) OMPFinalClause();
11640 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011641 case OMPC_num_threads:
Kelvin Libe286f52018-09-15 13:54:15 +000011642 C = new (Context) OMPNumThreadsClause();
11643 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011644 case OMPC_safelen:
Kelvin Libe286f52018-09-15 13:54:15 +000011645 C = new (Context) OMPSafelenClause();
11646 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011647 case OMPC_simdlen:
Kelvin Libe286f52018-09-15 13:54:15 +000011648 C = new (Context) OMPSimdlenClause();
11649 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011650 case OMPC_allocator:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000011651 C = new (Context) OMPAllocatorClause();
11652 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011653 case OMPC_collapse:
Kelvin Libe286f52018-09-15 13:54:15 +000011654 C = new (Context) OMPCollapseClause();
11655 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011656 case OMPC_default:
Kelvin Libe286f52018-09-15 13:54:15 +000011657 C = new (Context) OMPDefaultClause();
11658 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011659 case OMPC_proc_bind:
Kelvin Libe286f52018-09-15 13:54:15 +000011660 C = new (Context) OMPProcBindClause();
11661 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011662 case OMPC_schedule:
Kelvin Libe286f52018-09-15 13:54:15 +000011663 C = new (Context) OMPScheduleClause();
11664 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011665 case OMPC_ordered:
Kelvin Libe286f52018-09-15 13:54:15 +000011666 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11667 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011668 case OMPC_nowait:
Kelvin Libe286f52018-09-15 13:54:15 +000011669 C = new (Context) OMPNowaitClause();
11670 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011671 case OMPC_untied:
Kelvin Libe286f52018-09-15 13:54:15 +000011672 C = new (Context) OMPUntiedClause();
11673 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011674 case OMPC_mergeable:
Kelvin Libe286f52018-09-15 13:54:15 +000011675 C = new (Context) OMPMergeableClause();
11676 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011677 case OMPC_read:
Kelvin Libe286f52018-09-15 13:54:15 +000011678 C = new (Context) OMPReadClause();
11679 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011680 case OMPC_write:
Kelvin Libe286f52018-09-15 13:54:15 +000011681 C = new (Context) OMPWriteClause();
11682 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011683 case OMPC_update:
Alexey Bataev82f7c202020-03-03 13:22:35 -050011684 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
Kelvin Libe286f52018-09-15 13:54:15 +000011685 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011686 case OMPC_capture:
Kelvin Libe286f52018-09-15 13:54:15 +000011687 C = new (Context) OMPCaptureClause();
11688 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011689 case OMPC_seq_cst:
Kelvin Libe286f52018-09-15 13:54:15 +000011690 C = new (Context) OMPSeqCstClause();
11691 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011692 case OMPC_acq_rel:
Alexey Bataevea9166b2020-02-06 16:30:23 -050011693 C = new (Context) OMPAcqRelClause();
11694 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011695 case OMPC_acquire:
Alexey Bataev04a830f2020-02-10 14:30:39 -050011696 C = new (Context) OMPAcquireClause();
11697 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011698 case OMPC_release:
Alexey Bataev95598342020-02-10 15:49:05 -050011699 C = new (Context) OMPReleaseClause();
11700 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011701 case OMPC_relaxed:
Alexey Bataev9a8defc2020-02-11 11:10:43 -050011702 C = new (Context) OMPRelaxedClause();
11703 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011704 case OMPC_threads:
Kelvin Libe286f52018-09-15 13:54:15 +000011705 C = new (Context) OMPThreadsClause();
11706 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011707 case OMPC_simd:
Kelvin Libe286f52018-09-15 13:54:15 +000011708 C = new (Context) OMPSIMDClause();
11709 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011710 case OMPC_nogroup:
Kelvin Libe286f52018-09-15 13:54:15 +000011711 C = new (Context) OMPNogroupClause();
11712 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011713 case OMPC_unified_address:
Kelvin Li1408f912018-09-26 04:28:39 +000011714 C = new (Context) OMPUnifiedAddressClause();
11715 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011716 case OMPC_unified_shared_memory:
Patrick Lyster4a370b92018-10-01 13:47:43 +000011717 C = new (Context) OMPUnifiedSharedMemoryClause();
11718 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011719 case OMPC_reverse_offload:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000011720 C = new (Context) OMPReverseOffloadClause();
11721 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011722 case OMPC_dynamic_allocators:
Patrick Lyster3fe9e392018-10-11 14:41:10 +000011723 C = new (Context) OMPDynamicAllocatorsClause();
11724 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011725 case OMPC_atomic_default_mem_order:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000011726 C = new (Context) OMPAtomicDefaultMemOrderClause();
11727 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011728 case OMPC_private:
Kelvin Libe286f52018-09-15 13:54:15 +000011729 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11730 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011731 case OMPC_firstprivate:
Kelvin Libe286f52018-09-15 13:54:15 +000011732 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11733 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011734 case OMPC_lastprivate:
Kelvin Libe286f52018-09-15 13:54:15 +000011735 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11736 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011737 case OMPC_shared:
Kelvin Libe286f52018-09-15 13:54:15 +000011738 C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11739 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011740 case OMPC_reduction:
Kelvin Libe286f52018-09-15 13:54:15 +000011741 C = OMPReductionClause::CreateEmpty(Context, Record.readInt());
11742 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011743 case OMPC_task_reduction:
Kelvin Libe286f52018-09-15 13:54:15 +000011744 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11745 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011746 case OMPC_in_reduction:
Kelvin Libe286f52018-09-15 13:54:15 +000011747 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11748 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011749 case OMPC_linear:
Kelvin Libe286f52018-09-15 13:54:15 +000011750 C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11751 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011752 case OMPC_aligned:
Kelvin Libe286f52018-09-15 13:54:15 +000011753 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11754 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011755 case OMPC_copyin:
Kelvin Libe286f52018-09-15 13:54:15 +000011756 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11757 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011758 case OMPC_copyprivate:
Kelvin Libe286f52018-09-15 13:54:15 +000011759 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11760 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011761 case OMPC_flush:
Kelvin Libe286f52018-09-15 13:54:15 +000011762 C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11763 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011764 case OMPC_depobj:
Alexey Bataevc112e942020-02-28 09:52:15 -050011765 C = OMPDepobjClause::CreateEmpty(Context);
11766 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011767 case OMPC_depend: {
Kelvin Libe286f52018-09-15 13:54:15 +000011768 unsigned NumVars = Record.readInt();
11769 unsigned NumLoops = Record.readInt();
11770 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11771 break;
11772 }
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011773 case OMPC_device:
Kelvin Libe286f52018-09-15 13:54:15 +000011774 C = new (Context) OMPDeviceClause();
11775 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011776 case OMPC_map: {
Michael Kruse4304e9d2019-02-19 16:38:20 +000011777 OMPMappableExprListSizeTy Sizes;
11778 Sizes.NumVars = Record.readInt();
11779 Sizes.NumUniqueDeclarations = Record.readInt();
11780 Sizes.NumComponentLists = Record.readInt();
11781 Sizes.NumComponents = Record.readInt();
11782 C = OMPMapClause::CreateEmpty(Context, Sizes);
Kelvin Libe286f52018-09-15 13:54:15 +000011783 break;
11784 }
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011785 case OMPC_num_teams:
Kelvin Libe286f52018-09-15 13:54:15 +000011786 C = new (Context) OMPNumTeamsClause();
11787 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011788 case OMPC_thread_limit:
Kelvin Libe286f52018-09-15 13:54:15 +000011789 C = new (Context) OMPThreadLimitClause();
11790 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011791 case OMPC_priority:
Kelvin Libe286f52018-09-15 13:54:15 +000011792 C = new (Context) OMPPriorityClause();
11793 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011794 case OMPC_grainsize:
Kelvin Libe286f52018-09-15 13:54:15 +000011795 C = new (Context) OMPGrainsizeClause();
11796 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011797 case OMPC_num_tasks:
Kelvin Libe286f52018-09-15 13:54:15 +000011798 C = new (Context) OMPNumTasksClause();
11799 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011800 case OMPC_hint:
Kelvin Libe286f52018-09-15 13:54:15 +000011801 C = new (Context) OMPHintClause();
11802 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011803 case OMPC_dist_schedule:
Kelvin Libe286f52018-09-15 13:54:15 +000011804 C = new (Context) OMPDistScheduleClause();
11805 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011806 case OMPC_defaultmap:
Kelvin Libe286f52018-09-15 13:54:15 +000011807 C = new (Context) OMPDefaultmapClause();
11808 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011809 case OMPC_to: {
Michael Kruse4304e9d2019-02-19 16:38:20 +000011810 OMPMappableExprListSizeTy Sizes;
11811 Sizes.NumVars = Record.readInt();
11812 Sizes.NumUniqueDeclarations = Record.readInt();
11813 Sizes.NumComponentLists = Record.readInt();
11814 Sizes.NumComponents = Record.readInt();
11815 C = OMPToClause::CreateEmpty(Context, Sizes);
Kelvin Libe286f52018-09-15 13:54:15 +000011816 break;
11817 }
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011818 case OMPC_from: {
Michael Kruse4304e9d2019-02-19 16:38:20 +000011819 OMPMappableExprListSizeTy Sizes;
11820 Sizes.NumVars = Record.readInt();
11821 Sizes.NumUniqueDeclarations = Record.readInt();
11822 Sizes.NumComponentLists = Record.readInt();
11823 Sizes.NumComponents = Record.readInt();
11824 C = OMPFromClause::CreateEmpty(Context, Sizes);
Kelvin Libe286f52018-09-15 13:54:15 +000011825 break;
11826 }
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011827 case OMPC_use_device_ptr: {
Michael Kruse4304e9d2019-02-19 16:38:20 +000011828 OMPMappableExprListSizeTy Sizes;
11829 Sizes.NumVars = Record.readInt();
11830 Sizes.NumUniqueDeclarations = Record.readInt();
11831 Sizes.NumComponentLists = Record.readInt();
11832 Sizes.NumComponents = Record.readInt();
11833 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
Kelvin Libe286f52018-09-15 13:54:15 +000011834 break;
11835 }
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011836 case OMPC_is_device_ptr: {
Michael Kruse4304e9d2019-02-19 16:38:20 +000011837 OMPMappableExprListSizeTy Sizes;
11838 Sizes.NumVars = Record.readInt();
11839 Sizes.NumUniqueDeclarations = Record.readInt();
11840 Sizes.NumComponentLists = Record.readInt();
11841 Sizes.NumComponents = Record.readInt();
11842 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
Kelvin Libe286f52018-09-15 13:54:15 +000011843 break;
11844 }
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011845 case OMPC_allocate:
Alexey Bataeve04483e2019-03-27 14:14:31 +000011846 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11847 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011848 case OMPC_nontemporal:
Alexey Bataevb6e70842019-12-16 15:54:17 -050011849 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11850 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011851 case OMPC_inclusive:
Alexey Bataev06dea732020-03-20 09:41:22 -040011852 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11853 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011854 case OMPC_exclusive:
Alexey Bataev63828a32020-03-23 10:41:08 -040011855 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11856 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011857 case OMPC_order:
Alexey Bataevcb8e6912020-01-31 16:09:26 -050011858 C = new (Context) OMPOrderClause();
11859 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011860 case OMPC_destroy:
Alexey Bataev375437a2020-03-02 14:21:20 -050011861 C = new (Context) OMPDestroyClause();
11862 break;
Johannes Doerfert1858f4b2020-04-02 02:23:22 -050011863 case OMPC_detach:
Alexey Bataev0f0564b2020-03-17 09:17:42 -040011864 C = new (Context) OMPDetachClause();
11865 break;
Kelvin Libe286f52018-09-15 13:54:15 +000011866 }
Simon Pilgrim556fbfe2019-09-15 16:05:20 +000011867 assert(C && "Unknown OMPClause type");
11868
Kelvin Libe286f52018-09-15 13:54:15 +000011869 Visit(C);
11870 C->setLocStart(Record.readSourceLocation());
11871 C->setLocEnd(Record.readSourceLocation());
11872
11873 return C;
11874}
11875
11876void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11877 C->setPreInitStmt(Record.readSubStmt(),
11878 static_cast<OpenMPDirectiveKind>(Record.readInt()));
11879}
11880
11881void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11882 VisitOMPClauseWithPreInit(C);
11883 C->setPostUpdateExpr(Record.readSubExpr());
11884}
11885
11886void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11887 VisitOMPClauseWithPreInit(C);
11888 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11889 C->setNameModifierLoc(Record.readSourceLocation());
11890 C->setColonLoc(Record.readSourceLocation());
11891 C->setCondition(Record.readSubExpr());
11892 C->setLParenLoc(Record.readSourceLocation());
11893}
11894
11895void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
Alexey Bataev3a842ec2019-10-15 19:37:05 +000011896 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +000011897 C->setCondition(Record.readSubExpr());
11898 C->setLParenLoc(Record.readSourceLocation());
11899}
11900
11901void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11902 VisitOMPClauseWithPreInit(C);
11903 C->setNumThreads(Record.readSubExpr());
11904 C->setLParenLoc(Record.readSourceLocation());
11905}
11906
11907void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11908 C->setSafelen(Record.readSubExpr());
11909 C->setLParenLoc(Record.readSourceLocation());
11910}
11911
11912void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11913 C->setSimdlen(Record.readSubExpr());
11914 C->setLParenLoc(Record.readSourceLocation());
11915}
11916
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000011917void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
11918 C->setAllocator(Record.readExpr());
11919 C->setLParenLoc(Record.readSourceLocation());
11920}
11921
Kelvin Libe286f52018-09-15 13:54:15 +000011922void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11923 C->setNumForLoops(Record.readSubExpr());
11924 C->setLParenLoc(Record.readSourceLocation());
11925}
11926
11927void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
Atmn Patel577c9b02020-02-14 21:45:49 -060011928 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
Kelvin Libe286f52018-09-15 13:54:15 +000011929 C->setLParenLoc(Record.readSourceLocation());
11930 C->setDefaultKindKwLoc(Record.readSourceLocation());
11931}
11932
11933void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -060011934 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
Kelvin Libe286f52018-09-15 13:54:15 +000011935 C->setLParenLoc(Record.readSourceLocation());
11936 C->setProcBindKindKwLoc(Record.readSourceLocation());
11937}
11938
11939void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
11940 VisitOMPClauseWithPreInit(C);
11941 C->setScheduleKind(
11942 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
11943 C->setFirstScheduleModifier(
11944 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11945 C->setSecondScheduleModifier(
11946 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11947 C->setChunkSize(Record.readSubExpr());
11948 C->setLParenLoc(Record.readSourceLocation());
11949 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
11950 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
11951 C->setScheduleKindLoc(Record.readSourceLocation());
11952 C->setCommaLoc(Record.readSourceLocation());
11953}
11954
11955void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
11956 C->setNumForLoops(Record.readSubExpr());
11957 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11958 C->setLoopNumIterations(I, Record.readSubExpr());
11959 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11960 C->setLoopCounter(I, Record.readSubExpr());
11961 C->setLParenLoc(Record.readSourceLocation());
11962}
11963
Alexey Bataev0f0564b2020-03-17 09:17:42 -040011964void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
11965 C->setEventHandler(Record.readSubExpr());
11966 C->setLParenLoc(Record.readSourceLocation());
11967}
11968
Kelvin Libe286f52018-09-15 13:54:15 +000011969void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
11970
11971void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
11972
11973void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
11974
11975void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
11976
11977void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
11978
Alexey Bataev82f7c202020-03-03 13:22:35 -050011979void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
11980 if (C->isExtended()) {
11981 C->setLParenLoc(Record.readSourceLocation());
11982 C->setArgumentLoc(Record.readSourceLocation());
11983 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
11984 }
11985}
Kelvin Libe286f52018-09-15 13:54:15 +000011986
11987void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
11988
11989void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
11990
Alexey Bataevea9166b2020-02-06 16:30:23 -050011991void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
11992
Alexey Bataev04a830f2020-02-10 14:30:39 -050011993void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
11994
Alexey Bataev95598342020-02-10 15:49:05 -050011995void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
11996
Alexey Bataev9a8defc2020-02-11 11:10:43 -050011997void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
11998
Kelvin Libe286f52018-09-15 13:54:15 +000011999void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12000
12001void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12002
12003void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12004
Alexey Bataev375437a2020-03-02 14:21:20 -050012005void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {}
12006
Kelvin Li1408f912018-09-26 04:28:39 +000012007void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12008
Patrick Lyster4a370b92018-10-01 13:47:43 +000012009void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12010 OMPUnifiedSharedMemoryClause *) {}
12011
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000012012void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12013
Patrick Lyster3fe9e392018-10-11 14:41:10 +000012014void
12015OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12016}
12017
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000012018void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12019 OMPAtomicDefaultMemOrderClause *C) {
12020 C->setAtomicDefaultMemOrderKind(
12021 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12022 C->setLParenLoc(Record.readSourceLocation());
12023 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12024}
12025
Kelvin Libe286f52018-09-15 13:54:15 +000012026void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12027 C->setLParenLoc(Record.readSourceLocation());
12028 unsigned NumVars = C->varlist_size();
12029 SmallVector<Expr *, 16> Vars;
12030 Vars.reserve(NumVars);
12031 for (unsigned i = 0; i != NumVars; ++i)
12032 Vars.push_back(Record.readSubExpr());
12033 C->setVarRefs(Vars);
12034 Vars.clear();
12035 for (unsigned i = 0; i != NumVars; ++i)
12036 Vars.push_back(Record.readSubExpr());
12037 C->setPrivateCopies(Vars);
12038}
12039
12040void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12041 VisitOMPClauseWithPreInit(C);
12042 C->setLParenLoc(Record.readSourceLocation());
12043 unsigned NumVars = C->varlist_size();
12044 SmallVector<Expr *, 16> Vars;
12045 Vars.reserve(NumVars);
12046 for (unsigned i = 0; i != NumVars; ++i)
12047 Vars.push_back(Record.readSubExpr());
12048 C->setVarRefs(Vars);
12049 Vars.clear();
12050 for (unsigned i = 0; i != NumVars; ++i)
12051 Vars.push_back(Record.readSubExpr());
12052 C->setPrivateCopies(Vars);
12053 Vars.clear();
12054 for (unsigned i = 0; i != NumVars; ++i)
12055 Vars.push_back(Record.readSubExpr());
12056 C->setInits(Vars);
12057}
12058
12059void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12060 VisitOMPClauseWithPostUpdate(C);
12061 C->setLParenLoc(Record.readSourceLocation());
Alexey Bataev93dc40d2019-12-20 11:04:57 -050012062 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12063 C->setKindLoc(Record.readSourceLocation());
12064 C->setColonLoc(Record.readSourceLocation());
Kelvin Libe286f52018-09-15 13:54:15 +000012065 unsigned NumVars = C->varlist_size();
12066 SmallVector<Expr *, 16> Vars;
12067 Vars.reserve(NumVars);
12068 for (unsigned i = 0; i != NumVars; ++i)
12069 Vars.push_back(Record.readSubExpr());
12070 C->setVarRefs(Vars);
12071 Vars.clear();
12072 for (unsigned i = 0; i != NumVars; ++i)
12073 Vars.push_back(Record.readSubExpr());
12074 C->setPrivateCopies(Vars);
12075 Vars.clear();
12076 for (unsigned i = 0; i != NumVars; ++i)
12077 Vars.push_back(Record.readSubExpr());
12078 C->setSourceExprs(Vars);
12079 Vars.clear();
12080 for (unsigned i = 0; i != NumVars; ++i)
12081 Vars.push_back(Record.readSubExpr());
12082 C->setDestinationExprs(Vars);
12083 Vars.clear();
12084 for (unsigned i = 0; i != NumVars; ++i)
12085 Vars.push_back(Record.readSubExpr());
12086 C->setAssignmentOps(Vars);
12087}
12088
12089void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12090 C->setLParenLoc(Record.readSourceLocation());
12091 unsigned NumVars = C->varlist_size();
12092 SmallVector<Expr *, 16> Vars;
12093 Vars.reserve(NumVars);
12094 for (unsigned i = 0; i != NumVars; ++i)
12095 Vars.push_back(Record.readSubExpr());
12096 C->setVarRefs(Vars);
12097}
12098
12099void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12100 VisitOMPClauseWithPostUpdate(C);
12101 C->setLParenLoc(Record.readSourceLocation());
Alexey Bataev1236eb62020-03-23 17:30:38 -040012102 C->setModifierLoc(Record.readSourceLocation());
Kelvin Libe286f52018-09-15 13:54:15 +000012103 C->setColonLoc(Record.readSourceLocation());
Alexey Bataev1236eb62020-03-23 17:30:38 -040012104 C->setModifier(Record.readEnum<OpenMPReductionClauseModifier>());
Kelvin Libe286f52018-09-15 13:54:15 +000012105 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
John McCall3ce3d232019-12-13 03:37:23 -050012106 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
Kelvin Libe286f52018-09-15 13:54:15 +000012107 C->setQualifierLoc(NNSL);
12108 C->setNameInfo(DNI);
12109
12110 unsigned NumVars = C->varlist_size();
12111 SmallVector<Expr *, 16> Vars;
12112 Vars.reserve(NumVars);
12113 for (unsigned i = 0; i != NumVars; ++i)
12114 Vars.push_back(Record.readSubExpr());
12115 C->setVarRefs(Vars);
12116 Vars.clear();
12117 for (unsigned i = 0; i != NumVars; ++i)
12118 Vars.push_back(Record.readSubExpr());
12119 C->setPrivates(Vars);
12120 Vars.clear();
12121 for (unsigned i = 0; i != NumVars; ++i)
12122 Vars.push_back(Record.readSubExpr());
12123 C->setLHSExprs(Vars);
12124 Vars.clear();
12125 for (unsigned i = 0; i != NumVars; ++i)
12126 Vars.push_back(Record.readSubExpr());
12127 C->setRHSExprs(Vars);
12128 Vars.clear();
12129 for (unsigned i = 0; i != NumVars; ++i)
12130 Vars.push_back(Record.readSubExpr());
12131 C->setReductionOps(Vars);
12132}
12133
12134void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12135 VisitOMPClauseWithPostUpdate(C);
12136 C->setLParenLoc(Record.readSourceLocation());
12137 C->setColonLoc(Record.readSourceLocation());
12138 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
John McCall3ce3d232019-12-13 03:37:23 -050012139 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
Kelvin Libe286f52018-09-15 13:54:15 +000012140 C->setQualifierLoc(NNSL);
12141 C->setNameInfo(DNI);
12142
12143 unsigned NumVars = C->varlist_size();
12144 SmallVector<Expr *, 16> Vars;
12145 Vars.reserve(NumVars);
12146 for (unsigned I = 0; I != NumVars; ++I)
12147 Vars.push_back(Record.readSubExpr());
12148 C->setVarRefs(Vars);
12149 Vars.clear();
12150 for (unsigned I = 0; I != NumVars; ++I)
12151 Vars.push_back(Record.readSubExpr());
12152 C->setPrivates(Vars);
12153 Vars.clear();
12154 for (unsigned I = 0; I != NumVars; ++I)
12155 Vars.push_back(Record.readSubExpr());
12156 C->setLHSExprs(Vars);
12157 Vars.clear();
12158 for (unsigned I = 0; I != NumVars; ++I)
12159 Vars.push_back(Record.readSubExpr());
12160 C->setRHSExprs(Vars);
12161 Vars.clear();
12162 for (unsigned I = 0; I != NumVars; ++I)
12163 Vars.push_back(Record.readSubExpr());
12164 C->setReductionOps(Vars);
12165}
12166
12167void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12168 VisitOMPClauseWithPostUpdate(C);
12169 C->setLParenLoc(Record.readSourceLocation());
12170 C->setColonLoc(Record.readSourceLocation());
12171 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
John McCall3ce3d232019-12-13 03:37:23 -050012172 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
Kelvin Libe286f52018-09-15 13:54:15 +000012173 C->setQualifierLoc(NNSL);
12174 C->setNameInfo(DNI);
12175
12176 unsigned NumVars = C->varlist_size();
12177 SmallVector<Expr *, 16> Vars;
12178 Vars.reserve(NumVars);
12179 for (unsigned I = 0; I != NumVars; ++I)
12180 Vars.push_back(Record.readSubExpr());
12181 C->setVarRefs(Vars);
12182 Vars.clear();
12183 for (unsigned I = 0; I != NumVars; ++I)
12184 Vars.push_back(Record.readSubExpr());
12185 C->setPrivates(Vars);
12186 Vars.clear();
12187 for (unsigned I = 0; I != NumVars; ++I)
12188 Vars.push_back(Record.readSubExpr());
12189 C->setLHSExprs(Vars);
12190 Vars.clear();
12191 for (unsigned I = 0; I != NumVars; ++I)
12192 Vars.push_back(Record.readSubExpr());
12193 C->setRHSExprs(Vars);
12194 Vars.clear();
12195 for (unsigned I = 0; I != NumVars; ++I)
12196 Vars.push_back(Record.readSubExpr());
12197 C->setReductionOps(Vars);
12198 Vars.clear();
12199 for (unsigned I = 0; I != NumVars; ++I)
12200 Vars.push_back(Record.readSubExpr());
12201 C->setTaskgroupDescriptors(Vars);
12202}
12203
12204void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12205 VisitOMPClauseWithPostUpdate(C);
12206 C->setLParenLoc(Record.readSourceLocation());
12207 C->setColonLoc(Record.readSourceLocation());
12208 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12209 C->setModifierLoc(Record.readSourceLocation());
12210 unsigned NumVars = C->varlist_size();
12211 SmallVector<Expr *, 16> Vars;
12212 Vars.reserve(NumVars);
12213 for (unsigned i = 0; i != NumVars; ++i)
12214 Vars.push_back(Record.readSubExpr());
12215 C->setVarRefs(Vars);
12216 Vars.clear();
12217 for (unsigned i = 0; i != NumVars; ++i)
12218 Vars.push_back(Record.readSubExpr());
12219 C->setPrivates(Vars);
12220 Vars.clear();
12221 for (unsigned i = 0; i != NumVars; ++i)
12222 Vars.push_back(Record.readSubExpr());
12223 C->setInits(Vars);
12224 Vars.clear();
12225 for (unsigned i = 0; i != NumVars; ++i)
12226 Vars.push_back(Record.readSubExpr());
12227 C->setUpdates(Vars);
12228 Vars.clear();
12229 for (unsigned i = 0; i != NumVars; ++i)
12230 Vars.push_back(Record.readSubExpr());
12231 C->setFinals(Vars);
12232 C->setStep(Record.readSubExpr());
12233 C->setCalcStep(Record.readSubExpr());
Alexey Bataev195ae902019-08-08 13:42:45 +000012234 Vars.clear();
12235 for (unsigned I = 0; I != NumVars + 1; ++I)
12236 Vars.push_back(Record.readSubExpr());
12237 C->setUsedExprs(Vars);
Kelvin Libe286f52018-09-15 13:54:15 +000012238}
12239
12240void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12241 C->setLParenLoc(Record.readSourceLocation());
12242 C->setColonLoc(Record.readSourceLocation());
12243 unsigned NumVars = C->varlist_size();
12244 SmallVector<Expr *, 16> Vars;
12245 Vars.reserve(NumVars);
12246 for (unsigned i = 0; i != NumVars; ++i)
12247 Vars.push_back(Record.readSubExpr());
12248 C->setVarRefs(Vars);
12249 C->setAlignment(Record.readSubExpr());
12250}
12251
12252void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12253 C->setLParenLoc(Record.readSourceLocation());
12254 unsigned NumVars = C->varlist_size();
12255 SmallVector<Expr *, 16> Exprs;
12256 Exprs.reserve(NumVars);
12257 for (unsigned i = 0; i != NumVars; ++i)
12258 Exprs.push_back(Record.readSubExpr());
12259 C->setVarRefs(Exprs);
12260 Exprs.clear();
12261 for (unsigned i = 0; i != NumVars; ++i)
12262 Exprs.push_back(Record.readSubExpr());
12263 C->setSourceExprs(Exprs);
12264 Exprs.clear();
12265 for (unsigned i = 0; i != NumVars; ++i)
12266 Exprs.push_back(Record.readSubExpr());
12267 C->setDestinationExprs(Exprs);
12268 Exprs.clear();
12269 for (unsigned i = 0; i != NumVars; ++i)
12270 Exprs.push_back(Record.readSubExpr());
12271 C->setAssignmentOps(Exprs);
12272}
12273
12274void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12275 C->setLParenLoc(Record.readSourceLocation());
12276 unsigned NumVars = C->varlist_size();
12277 SmallVector<Expr *, 16> Exprs;
12278 Exprs.reserve(NumVars);
12279 for (unsigned i = 0; i != NumVars; ++i)
12280 Exprs.push_back(Record.readSubExpr());
12281 C->setVarRefs(Exprs);
12282 Exprs.clear();
12283 for (unsigned i = 0; i != NumVars; ++i)
12284 Exprs.push_back(Record.readSubExpr());
12285 C->setSourceExprs(Exprs);
12286 Exprs.clear();
12287 for (unsigned i = 0; i != NumVars; ++i)
12288 Exprs.push_back(Record.readSubExpr());
12289 C->setDestinationExprs(Exprs);
12290 Exprs.clear();
12291 for (unsigned i = 0; i != NumVars; ++i)
12292 Exprs.push_back(Record.readSubExpr());
12293 C->setAssignmentOps(Exprs);
12294}
12295
12296void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12297 C->setLParenLoc(Record.readSourceLocation());
12298 unsigned NumVars = C->varlist_size();
12299 SmallVector<Expr *, 16> Vars;
12300 Vars.reserve(NumVars);
12301 for (unsigned i = 0; i != NumVars; ++i)
12302 Vars.push_back(Record.readSubExpr());
12303 C->setVarRefs(Vars);
12304}
12305
Alexey Bataevc112e942020-02-28 09:52:15 -050012306void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12307 C->setDepobj(Record.readSubExpr());
12308 C->setLParenLoc(Record.readSourceLocation());
12309}
12310
Kelvin Libe286f52018-09-15 13:54:15 +000012311void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12312 C->setLParenLoc(Record.readSourceLocation());
Alexey Bataev13a15042020-04-01 15:06:38 -040012313 C->setModifier(Record.readSubExpr());
Kelvin Libe286f52018-09-15 13:54:15 +000012314 C->setDependencyKind(
12315 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12316 C->setDependencyLoc(Record.readSourceLocation());
12317 C->setColonLoc(Record.readSourceLocation());
12318 unsigned NumVars = C->varlist_size();
12319 SmallVector<Expr *, 16> Vars;
12320 Vars.reserve(NumVars);
12321 for (unsigned I = 0; I != NumVars; ++I)
12322 Vars.push_back(Record.readSubExpr());
12323 C->setVarRefs(Vars);
12324 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12325 C->setLoopData(I, Record.readSubExpr());
12326}
12327
12328void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12329 VisitOMPClauseWithPreInit(C);
Alexey Bataev2f8894a2020-03-18 15:01:15 -040012330 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
Kelvin Libe286f52018-09-15 13:54:15 +000012331 C->setDevice(Record.readSubExpr());
Alexey Bataev2f8894a2020-03-18 15:01:15 -040012332 C->setModifierLoc(Record.readSourceLocation());
Kelvin Libe286f52018-09-15 13:54:15 +000012333 C->setLParenLoc(Record.readSourceLocation());
12334}
12335
12336void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12337 C->setLParenLoc(Record.readSourceLocation());
Reid Klecknerba1ffd22020-04-03 12:35:30 -070012338 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
Kelvin Lief579432018-12-18 22:18:41 +000012339 C->setMapTypeModifier(
12340 I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12341 C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12342 }
Michael Kruse4304e9d2019-02-19 16:38:20 +000012343 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
John McCall3ce3d232019-12-13 03:37:23 -050012344 C->setMapperIdInfo(Record.readDeclarationNameInfo());
Kelvin Libe286f52018-09-15 13:54:15 +000012345 C->setMapType(
12346 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12347 C->setMapLoc(Record.readSourceLocation());
12348 C->setColonLoc(Record.readSourceLocation());
12349 auto NumVars = C->varlist_size();
12350 auto UniqueDecls = C->getUniqueDeclarationsNum();
12351 auto TotalLists = C->getTotalComponentListNum();
12352 auto TotalComponents = C->getTotalComponentsNum();
12353
12354 SmallVector<Expr *, 16> Vars;
12355 Vars.reserve(NumVars);
12356 for (unsigned i = 0; i != NumVars; ++i)
Michael Kruse251e1482019-02-01 20:25:04 +000012357 Vars.push_back(Record.readExpr());
Kelvin Libe286f52018-09-15 13:54:15 +000012358 C->setVarRefs(Vars);
12359
Michael Kruse4304e9d2019-02-19 16:38:20 +000012360 SmallVector<Expr *, 16> UDMappers;
12361 UDMappers.reserve(NumVars);
12362 for (unsigned I = 0; I < NumVars; ++I)
12363 UDMappers.push_back(Record.readExpr());
12364 C->setUDMapperRefs(UDMappers);
12365
Kelvin Libe286f52018-09-15 13:54:15 +000012366 SmallVector<ValueDecl *, 16> Decls;
12367 Decls.reserve(UniqueDecls);
12368 for (unsigned i = 0; i < UniqueDecls; ++i)
12369 Decls.push_back(Record.readDeclAs<ValueDecl>());
12370 C->setUniqueDecls(Decls);
12371
12372 SmallVector<unsigned, 16> ListsPerDecl;
12373 ListsPerDecl.reserve(UniqueDecls);
12374 for (unsigned i = 0; i < UniqueDecls; ++i)
12375 ListsPerDecl.push_back(Record.readInt());
12376 C->setDeclNumLists(ListsPerDecl);
12377
12378 SmallVector<unsigned, 32> ListSizes;
12379 ListSizes.reserve(TotalLists);
12380 for (unsigned i = 0; i < TotalLists; ++i)
12381 ListSizes.push_back(Record.readInt());
12382 C->setComponentListSizes(ListSizes);
12383
12384 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12385 Components.reserve(TotalComponents);
12386 for (unsigned i = 0; i < TotalComponents; ++i) {
Michael Kruse251e1482019-02-01 20:25:04 +000012387 Expr *AssociatedExpr = Record.readExpr();
Kelvin Libe286f52018-09-15 13:54:15 +000012388 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12389 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12390 AssociatedExpr, AssociatedDecl));
12391 }
12392 C->setComponents(Components, ListSizes);
12393}
12394
Alexey Bataeve04483e2019-03-27 14:14:31 +000012395void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12396 C->setLParenLoc(Record.readSourceLocation());
12397 C->setColonLoc(Record.readSourceLocation());
12398 C->setAllocator(Record.readSubExpr());
12399 unsigned NumVars = C->varlist_size();
12400 SmallVector<Expr *, 16> Vars;
12401 Vars.reserve(NumVars);
12402 for (unsigned i = 0; i != NumVars; ++i)
12403 Vars.push_back(Record.readSubExpr());
12404 C->setVarRefs(Vars);
12405}
12406
Kelvin Libe286f52018-09-15 13:54:15 +000012407void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12408 VisitOMPClauseWithPreInit(C);
12409 C->setNumTeams(Record.readSubExpr());
12410 C->setLParenLoc(Record.readSourceLocation());
12411}
12412
12413void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12414 VisitOMPClauseWithPreInit(C);
12415 C->setThreadLimit(Record.readSubExpr());
12416 C->setLParenLoc(Record.readSourceLocation());
12417}
12418
12419void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
Alexey Bataev31ba4762019-10-16 18:09:37 +000012420 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +000012421 C->setPriority(Record.readSubExpr());
12422 C->setLParenLoc(Record.readSourceLocation());
12423}
12424
12425void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
Alexey Bataevb9c55e22019-10-14 19:29:52 +000012426 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +000012427 C->setGrainsize(Record.readSubExpr());
12428 C->setLParenLoc(Record.readSourceLocation());
12429}
12430
12431void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
Alexey Bataevd88c7de2019-10-14 20:44:34 +000012432 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +000012433 C->setNumTasks(Record.readSubExpr());
12434 C->setLParenLoc(Record.readSourceLocation());
12435}
12436
12437void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12438 C->setHint(Record.readSubExpr());
12439 C->setLParenLoc(Record.readSourceLocation());
12440}
12441
12442void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12443 VisitOMPClauseWithPreInit(C);
12444 C->setDistScheduleKind(
12445 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12446 C->setChunkSize(Record.readSubExpr());
12447 C->setLParenLoc(Record.readSourceLocation());
12448 C->setDistScheduleKindLoc(Record.readSourceLocation());
12449 C->setCommaLoc(Record.readSourceLocation());
12450}
12451
12452void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12453 C->setDefaultmapKind(
12454 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12455 C->setDefaultmapModifier(
12456 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12457 C->setLParenLoc(Record.readSourceLocation());
12458 C->setDefaultmapModifierLoc(Record.readSourceLocation());
12459 C->setDefaultmapKindLoc(Record.readSourceLocation());
12460}
12461
12462void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12463 C->setLParenLoc(Record.readSourceLocation());
Michael Kruse01f670d2019-02-22 22:29:42 +000012464 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
John McCall3ce3d232019-12-13 03:37:23 -050012465 C->setMapperIdInfo(Record.readDeclarationNameInfo());
Kelvin Libe286f52018-09-15 13:54:15 +000012466 auto NumVars = C->varlist_size();
12467 auto UniqueDecls = C->getUniqueDeclarationsNum();
12468 auto TotalLists = C->getTotalComponentListNum();
12469 auto TotalComponents = C->getTotalComponentsNum();
12470
12471 SmallVector<Expr *, 16> Vars;
12472 Vars.reserve(NumVars);
12473 for (unsigned i = 0; i != NumVars; ++i)
12474 Vars.push_back(Record.readSubExpr());
12475 C->setVarRefs(Vars);
12476
Michael Kruse01f670d2019-02-22 22:29:42 +000012477 SmallVector<Expr *, 16> UDMappers;
12478 UDMappers.reserve(NumVars);
12479 for (unsigned I = 0; I < NumVars; ++I)
12480 UDMappers.push_back(Record.readSubExpr());
12481 C->setUDMapperRefs(UDMappers);
12482
Kelvin Libe286f52018-09-15 13:54:15 +000012483 SmallVector<ValueDecl *, 16> Decls;
12484 Decls.reserve(UniqueDecls);
12485 for (unsigned i = 0; i < UniqueDecls; ++i)
12486 Decls.push_back(Record.readDeclAs<ValueDecl>());
12487 C->setUniqueDecls(Decls);
12488
12489 SmallVector<unsigned, 16> ListsPerDecl;
12490 ListsPerDecl.reserve(UniqueDecls);
12491 for (unsigned i = 0; i < UniqueDecls; ++i)
12492 ListsPerDecl.push_back(Record.readInt());
12493 C->setDeclNumLists(ListsPerDecl);
12494
12495 SmallVector<unsigned, 32> ListSizes;
12496 ListSizes.reserve(TotalLists);
12497 for (unsigned i = 0; i < TotalLists; ++i)
12498 ListSizes.push_back(Record.readInt());
12499 C->setComponentListSizes(ListSizes);
12500
12501 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12502 Components.reserve(TotalComponents);
12503 for (unsigned i = 0; i < TotalComponents; ++i) {
12504 Expr *AssociatedExpr = Record.readSubExpr();
12505 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12506 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12507 AssociatedExpr, AssociatedDecl));
12508 }
12509 C->setComponents(Components, ListSizes);
12510}
12511
12512void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12513 C->setLParenLoc(Record.readSourceLocation());
Michael Kruse0336c752019-02-25 20:34:15 +000012514 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
John McCall3ce3d232019-12-13 03:37:23 -050012515 C->setMapperIdInfo(Record.readDeclarationNameInfo());
Kelvin Libe286f52018-09-15 13:54:15 +000012516 auto NumVars = C->varlist_size();
12517 auto UniqueDecls = C->getUniqueDeclarationsNum();
12518 auto TotalLists = C->getTotalComponentListNum();
12519 auto TotalComponents = C->getTotalComponentsNum();
12520
12521 SmallVector<Expr *, 16> Vars;
12522 Vars.reserve(NumVars);
12523 for (unsigned i = 0; i != NumVars; ++i)
12524 Vars.push_back(Record.readSubExpr());
12525 C->setVarRefs(Vars);
12526
Michael Kruse0336c752019-02-25 20:34:15 +000012527 SmallVector<Expr *, 16> UDMappers;
12528 UDMappers.reserve(NumVars);
12529 for (unsigned I = 0; I < NumVars; ++I)
12530 UDMappers.push_back(Record.readSubExpr());
12531 C->setUDMapperRefs(UDMappers);
12532
Kelvin Libe286f52018-09-15 13:54:15 +000012533 SmallVector<ValueDecl *, 16> Decls;
12534 Decls.reserve(UniqueDecls);
12535 for (unsigned i = 0; i < UniqueDecls; ++i)
12536 Decls.push_back(Record.readDeclAs<ValueDecl>());
12537 C->setUniqueDecls(Decls);
12538
12539 SmallVector<unsigned, 16> ListsPerDecl;
12540 ListsPerDecl.reserve(UniqueDecls);
12541 for (unsigned i = 0; i < UniqueDecls; ++i)
12542 ListsPerDecl.push_back(Record.readInt());
12543 C->setDeclNumLists(ListsPerDecl);
12544
12545 SmallVector<unsigned, 32> ListSizes;
12546 ListSizes.reserve(TotalLists);
12547 for (unsigned i = 0; i < TotalLists; ++i)
12548 ListSizes.push_back(Record.readInt());
12549 C->setComponentListSizes(ListSizes);
12550
12551 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12552 Components.reserve(TotalComponents);
12553 for (unsigned i = 0; i < TotalComponents; ++i) {
12554 Expr *AssociatedExpr = Record.readSubExpr();
12555 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12556 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12557 AssociatedExpr, AssociatedDecl));
12558 }
12559 C->setComponents(Components, ListSizes);
12560}
12561
12562void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12563 C->setLParenLoc(Record.readSourceLocation());
12564 auto NumVars = C->varlist_size();
12565 auto UniqueDecls = C->getUniqueDeclarationsNum();
12566 auto TotalLists = C->getTotalComponentListNum();
12567 auto TotalComponents = C->getTotalComponentsNum();
12568
12569 SmallVector<Expr *, 16> Vars;
12570 Vars.reserve(NumVars);
12571 for (unsigned i = 0; i != NumVars; ++i)
12572 Vars.push_back(Record.readSubExpr());
12573 C->setVarRefs(Vars);
12574 Vars.clear();
12575 for (unsigned i = 0; i != NumVars; ++i)
12576 Vars.push_back(Record.readSubExpr());
12577 C->setPrivateCopies(Vars);
12578 Vars.clear();
12579 for (unsigned i = 0; i != NumVars; ++i)
12580 Vars.push_back(Record.readSubExpr());
12581 C->setInits(Vars);
12582
12583 SmallVector<ValueDecl *, 16> Decls;
12584 Decls.reserve(UniqueDecls);
12585 for (unsigned i = 0; i < UniqueDecls; ++i)
12586 Decls.push_back(Record.readDeclAs<ValueDecl>());
12587 C->setUniqueDecls(Decls);
12588
12589 SmallVector<unsigned, 16> ListsPerDecl;
12590 ListsPerDecl.reserve(UniqueDecls);
12591 for (unsigned i = 0; i < UniqueDecls; ++i)
12592 ListsPerDecl.push_back(Record.readInt());
12593 C->setDeclNumLists(ListsPerDecl);
12594
12595 SmallVector<unsigned, 32> ListSizes;
12596 ListSizes.reserve(TotalLists);
12597 for (unsigned i = 0; i < TotalLists; ++i)
12598 ListSizes.push_back(Record.readInt());
12599 C->setComponentListSizes(ListSizes);
12600
12601 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12602 Components.reserve(TotalComponents);
12603 for (unsigned i = 0; i < TotalComponents; ++i) {
12604 Expr *AssociatedExpr = Record.readSubExpr();
12605 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12606 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12607 AssociatedExpr, AssociatedDecl));
12608 }
12609 C->setComponents(Components, ListSizes);
12610}
12611
12612void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12613 C->setLParenLoc(Record.readSourceLocation());
12614 auto NumVars = C->varlist_size();
12615 auto UniqueDecls = C->getUniqueDeclarationsNum();
12616 auto TotalLists = C->getTotalComponentListNum();
12617 auto TotalComponents = C->getTotalComponentsNum();
12618
12619 SmallVector<Expr *, 16> Vars;
12620 Vars.reserve(NumVars);
12621 for (unsigned i = 0; i != NumVars; ++i)
12622 Vars.push_back(Record.readSubExpr());
12623 C->setVarRefs(Vars);
12624 Vars.clear();
12625
12626 SmallVector<ValueDecl *, 16> Decls;
12627 Decls.reserve(UniqueDecls);
12628 for (unsigned i = 0; i < UniqueDecls; ++i)
12629 Decls.push_back(Record.readDeclAs<ValueDecl>());
12630 C->setUniqueDecls(Decls);
12631
12632 SmallVector<unsigned, 16> ListsPerDecl;
12633 ListsPerDecl.reserve(UniqueDecls);
12634 for (unsigned i = 0; i < UniqueDecls; ++i)
12635 ListsPerDecl.push_back(Record.readInt());
12636 C->setDeclNumLists(ListsPerDecl);
12637
12638 SmallVector<unsigned, 32> ListSizes;
12639 ListSizes.reserve(TotalLists);
12640 for (unsigned i = 0; i < TotalLists; ++i)
12641 ListSizes.push_back(Record.readInt());
12642 C->setComponentListSizes(ListSizes);
12643
12644 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12645 Components.reserve(TotalComponents);
12646 for (unsigned i = 0; i < TotalComponents; ++i) {
12647 Expr *AssociatedExpr = Record.readSubExpr();
12648 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12649 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12650 AssociatedExpr, AssociatedDecl));
12651 }
12652 C->setComponents(Components, ListSizes);
12653}
Alexey Bataevb6e70842019-12-16 15:54:17 -050012654
12655void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12656 C->setLParenLoc(Record.readSourceLocation());
12657 unsigned NumVars = C->varlist_size();
12658 SmallVector<Expr *, 16> Vars;
12659 Vars.reserve(NumVars);
12660 for (unsigned i = 0; i != NumVars; ++i)
12661 Vars.push_back(Record.readSubExpr());
12662 C->setVarRefs(Vars);
Alexey Bataev0860db92019-12-19 10:01:10 -050012663 Vars.clear();
12664 Vars.reserve(NumVars);
12665 for (unsigned i = 0; i != NumVars; ++i)
12666 Vars.push_back(Record.readSubExpr());
12667 C->setPrivateRefs(Vars);
Alexey Bataevb6e70842019-12-16 15:54:17 -050012668}
Alexey Bataevcb8e6912020-01-31 16:09:26 -050012669
Alexey Bataev06dea732020-03-20 09:41:22 -040012670void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12671 C->setLParenLoc(Record.readSourceLocation());
12672 unsigned NumVars = C->varlist_size();
12673 SmallVector<Expr *, 16> Vars;
12674 Vars.reserve(NumVars);
12675 for (unsigned i = 0; i != NumVars; ++i)
12676 Vars.push_back(Record.readSubExpr());
12677 C->setVarRefs(Vars);
12678}
12679
Alexey Bataev63828a32020-03-23 10:41:08 -040012680void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12681 C->setLParenLoc(Record.readSourceLocation());
12682 unsigned NumVars = C->varlist_size();
12683 SmallVector<Expr *, 16> Vars;
12684 Vars.reserve(NumVars);
12685 for (unsigned i = 0; i != NumVars; ++i)
12686 Vars.push_back(Record.readSubExpr());
12687 C->setVarRefs(Vars);
12688}
12689
Alexey Bataevcb8e6912020-01-31 16:09:26 -050012690void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12691 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12692 C->setLParenLoc(Record.readSourceLocation());
12693 C->setKindKwLoc(Record.readSourceLocation());
12694}
Johannes Doerfert1228d422019-12-19 20:42:12 -060012695
Johannes Doerfert55eca282020-03-13 23:42:05 -050012696OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12697 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
Johannes Doerfertb86bf832020-02-15 18:07:42 -060012698 TI.Sets.resize(readUInt32());
12699 for (auto &Set : TI.Sets) {
Johannes Doerfert1228d422019-12-19 20:42:12 -060012700 Set.Kind = readEnum<llvm::omp::TraitSet>();
12701 Set.Selectors.resize(readUInt32());
12702 for (auto &Selector : Set.Selectors) {
12703 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12704 Selector.ScoreOrCondition = nullptr;
12705 if (readBool())
12706 Selector.ScoreOrCondition = readExprRef();
12707 Selector.Properties.resize(readUInt32());
12708 for (auto &Property : Selector.Properties)
12709 Property.Kind = readEnum<llvm::omp::TraitProperty>();
12710 }
12711 }
Johannes Doerfert55eca282020-03-13 23:42:05 -050012712 return &TI;
Johannes Doerfert1228d422019-12-19 20:42:12 -060012713}