blob: 9f662c6e0d4cd2873956f5a456dafd60a852cfef [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"
Serge Pavlovc7ff5b32020-03-26 14:51:09 +070092#include "llvm/ADT/FloatingPointMode.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000093#include "llvm/ADT/FoldingSet.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000094#include "llvm/ADT/Hashing.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000095#include "llvm/ADT/IntrusiveRefCntPtr.h"
96#include "llvm/ADT/None.h"
97#include "llvm/ADT/Optional.h"
98#include "llvm/ADT/STLExtras.h"
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +000099#include "llvm/ADT/ScopeExit.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000100#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000101#include "llvm/ADT/SmallString.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000102#include "llvm/ADT/SmallVector.h"
Vedant Kumar48b4f762018-04-14 01:40:48 +0000103#include "llvm/ADT/StringExtras.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000104#include "llvm/ADT/StringMap.h"
105#include "llvm/ADT/StringRef.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000106#include "llvm/ADT/Triple.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000107#include "llvm/ADT/iterator_range.h"
Francis Visoiu Mistrihe0308272019-07-03 22:40:07 +0000108#include "llvm/Bitstream/BitstreamReader.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000109#include "llvm/Support/Casting.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000110#include "llvm/Support/Compiler.h"
Pavel Labathd8c62902018-06-11 10:28:04 +0000111#include "llvm/Support/Compression.h"
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000112#include "llvm/Support/DJB.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000113#include "llvm/Support/Endian.h"
George Rimarc39f5492017-01-17 15:45:31 +0000114#include "llvm/Support/Error.h"
Guy Benyei11169dd2012-12-18 14:30:41 +0000115#include "llvm/Support/ErrorHandling.h"
116#include "llvm/Support/FileSystem.h"
117#include "llvm/Support/MemoryBuffer.h"
118#include "llvm/Support/Path.h"
119#include "llvm/Support/SaveAndRestore.h"
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000120#include "llvm/Support/Timer.h"
Pavel Labathd8c62902018-06-11 10:28:04 +0000121#include "llvm/Support/VersionTuple.h"
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000122#include "llvm/Support/raw_ostream.h"
Guy Benyei11169dd2012-12-18 14:30:41 +0000123#include <algorithm>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000124#include <cassert>
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000125#include <cstddef>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000126#include <cstdint>
Chris Lattner91f373e2013-01-20 00:57:52 +0000127#include <cstdio>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000128#include <ctime>
Guy Benyei11169dd2012-12-18 14:30:41 +0000129#include <iterator>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000130#include <limits>
131#include <map>
132#include <memory>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000133#include <string>
Rafael Espindola8a8e5542014-06-12 17:19:42 +0000134#include <system_error>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000135#include <tuple>
136#include <utility>
137#include <vector>
Guy Benyei11169dd2012-12-18 14:30:41 +0000138
139using namespace clang;
Vedant Kumar48b4f762018-04-14 01:40:48 +0000140using namespace clang::serialization;
141using namespace clang::serialization::reader;
Chris Lattner7fb3bef2013-01-20 00:56:42 +0000142using llvm::BitstreamCursor;
Serge Pavlovc7ff5b32020-03-26 14:51:09 +0700143using llvm::RoundingMode;
Guy Benyei11169dd2012-12-18 14:30:41 +0000144
Ben Langmuircb69b572014-03-07 06:40:32 +0000145//===----------------------------------------------------------------------===//
146// ChainedASTReaderListener implementation
147//===----------------------------------------------------------------------===//
148
149bool
150ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
151 return First->ReadFullVersionInformation(FullVersion) ||
152 Second->ReadFullVersionInformation(FullVersion);
153}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000154
Ben Langmuir4f5212a2014-04-14 22:12:44 +0000155void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
156 First->ReadModuleName(ModuleName);
157 Second->ReadModuleName(ModuleName);
158}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000159
Ben Langmuir4f5212a2014-04-14 22:12:44 +0000160void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
161 First->ReadModuleMapFile(ModuleMapPath);
162 Second->ReadModuleMapFile(ModuleMapPath);
163}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000164
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000165bool
166ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
167 bool Complain,
168 bool AllowCompatibleDifferences) {
169 return First->ReadLanguageOptions(LangOpts, Complain,
170 AllowCompatibleDifferences) ||
171 Second->ReadLanguageOptions(LangOpts, Complain,
172 AllowCompatibleDifferences);
Ben Langmuircb69b572014-03-07 06:40:32 +0000173}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000174
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000175bool ChainedASTReaderListener::ReadTargetOptions(
176 const TargetOptions &TargetOpts, bool Complain,
177 bool AllowCompatibleDifferences) {
178 return First->ReadTargetOptions(TargetOpts, Complain,
179 AllowCompatibleDifferences) ||
180 Second->ReadTargetOptions(TargetOpts, Complain,
181 AllowCompatibleDifferences);
Ben Langmuircb69b572014-03-07 06:40:32 +0000182}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000183
Ben Langmuircb69b572014-03-07 06:40:32 +0000184bool ChainedASTReaderListener::ReadDiagnosticOptions(
Ben Langmuirb92de022014-04-29 16:25:26 +0000185 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
Ben Langmuircb69b572014-03-07 06:40:32 +0000186 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
187 Second->ReadDiagnosticOptions(DiagOpts, Complain);
188}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000189
Ben Langmuircb69b572014-03-07 06:40:32 +0000190bool
191ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
192 bool Complain) {
193 return First->ReadFileSystemOptions(FSOpts, Complain) ||
194 Second->ReadFileSystemOptions(FSOpts, Complain);
195}
196
197bool ChainedASTReaderListener::ReadHeaderSearchOptions(
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000198 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
199 bool Complain) {
200 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
201 Complain) ||
202 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
203 Complain);
Ben Langmuircb69b572014-03-07 06:40:32 +0000204}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000205
Ben Langmuircb69b572014-03-07 06:40:32 +0000206bool ChainedASTReaderListener::ReadPreprocessorOptions(
207 const PreprocessorOptions &PPOpts, bool Complain,
208 std::string &SuggestedPredefines) {
209 return First->ReadPreprocessorOptions(PPOpts, Complain,
210 SuggestedPredefines) ||
211 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
212}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000213
Ben Langmuircb69b572014-03-07 06:40:32 +0000214void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
215 unsigned Value) {
216 First->ReadCounter(M, Value);
217 Second->ReadCounter(M, Value);
218}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000219
Ben Langmuircb69b572014-03-07 06:40:32 +0000220bool ChainedASTReaderListener::needsInputFileVisitation() {
221 return First->needsInputFileVisitation() ||
222 Second->needsInputFileVisitation();
223}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000224
Ben Langmuircb69b572014-03-07 06:40:32 +0000225bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
226 return First->needsSystemInputFileVisitation() ||
227 Second->needsSystemInputFileVisitation();
228}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000229
Richard Smith216a3bd2015-08-13 17:57:10 +0000230void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
231 ModuleKind Kind) {
232 First->visitModuleFile(Filename, Kind);
233 Second->visitModuleFile(Filename, Kind);
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000234}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000235
Ben Langmuircb69b572014-03-07 06:40:32 +0000236bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000237 bool isSystem,
Richard Smith216a3bd2015-08-13 17:57:10 +0000238 bool isOverridden,
239 bool isExplicitModule) {
Justin Bognerc65a66d2014-05-22 06:04:59 +0000240 bool Continue = false;
241 if (First->needsInputFileVisitation() &&
242 (!isSystem || First->needsSystemInputFileVisitation()))
Richard Smith216a3bd2015-08-13 17:57:10 +0000243 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
244 isExplicitModule);
Justin Bognerc65a66d2014-05-22 06:04:59 +0000245 if (Second->needsInputFileVisitation() &&
246 (!isSystem || Second->needsSystemInputFileVisitation()))
Richard Smith216a3bd2015-08-13 17:57:10 +0000247 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
248 isExplicitModule);
Justin Bognerc65a66d2014-05-22 06:04:59 +0000249 return Continue;
Ben Langmuircb69b572014-03-07 06:40:32 +0000250}
251
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000252void ChainedASTReaderListener::readModuleFileExtension(
253 const ModuleFileExtensionMetadata &Metadata) {
254 First->readModuleFileExtension(Metadata);
255 Second->readModuleFileExtension(Metadata);
256}
257
Guy Benyei11169dd2012-12-18 14:30:41 +0000258//===----------------------------------------------------------------------===//
259// PCH validator implementation
260//===----------------------------------------------------------------------===//
261
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000262ASTReaderListener::~ASTReaderListener() = default;
Guy Benyei11169dd2012-12-18 14:30:41 +0000263
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000264/// Compare the given set of language options against an existing set of
Guy Benyei11169dd2012-12-18 14:30:41 +0000265/// language options.
266///
267/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000268/// \param AllowCompatibleDifferences If true, differences between compatible
269/// language options will be permitted.
Guy Benyei11169dd2012-12-18 14:30:41 +0000270///
271/// \returns true if the languagae options mis-match, false otherwise.
272static bool checkLanguageOptions(const LangOptions &LangOpts,
273 const LangOptions &ExistingLangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000274 DiagnosticsEngine *Diags,
275 bool AllowCompatibleDifferences = true) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000276#define LANGOPT(Name, Bits, Default, Description) \
277 if (ExistingLangOpts.Name != LangOpts.Name) { \
278 if (Diags) \
279 Diags->Report(diag::err_pch_langopt_mismatch) \
280 << Description << LangOpts.Name << ExistingLangOpts.Name; \
281 return true; \
282 }
283
284#define VALUE_LANGOPT(Name, Bits, Default, Description) \
285 if (ExistingLangOpts.Name != LangOpts.Name) { \
286 if (Diags) \
287 Diags->Report(diag::err_pch_langopt_value_mismatch) \
288 << Description; \
289 return true; \
290 }
291
292#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
293 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
294 if (Diags) \
295 Diags->Report(diag::err_pch_langopt_value_mismatch) \
296 << Description; \
297 return true; \
298 }
299
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000300#define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
301 if (!AllowCompatibleDifferences) \
302 LANGOPT(Name, Bits, Default, Description)
303
304#define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
305 if (!AllowCompatibleDifferences) \
306 ENUM_LANGOPT(Name, Bits, Default, Description)
307
Richard Smitha1ddf5e2016-04-07 20:47:37 +0000308#define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
309 if (!AllowCompatibleDifferences) \
310 VALUE_LANGOPT(Name, Bits, Default, Description)
311
Guy Benyei11169dd2012-12-18 14:30:41 +0000312#define BENIGN_LANGOPT(Name, Bits, Default, Description)
313#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
Richard Smitha1ddf5e2016-04-07 20:47:37 +0000314#define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
Guy Benyei11169dd2012-12-18 14:30:41 +0000315#include "clang/Basic/LangOptions.def"
316
Ben Langmuircd98cb72015-06-23 18:20:18 +0000317 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
318 if (Diags)
319 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
320 return true;
321 }
322
Guy Benyei11169dd2012-12-18 14:30:41 +0000323 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
324 if (Diags)
325 Diags->Report(diag::err_pch_langopt_value_mismatch)
326 << "target Objective-C runtime";
327 return true;
328 }
329
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +0000330 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
331 LangOpts.CommentOpts.BlockCommandNames) {
332 if (Diags)
333 Diags->Report(diag::err_pch_langopt_value_mismatch)
334 << "block command names";
335 return true;
336 }
337
Vedant Kumar85a83c22017-06-01 20:01:01 +0000338 // Sanitizer feature mismatches are treated as compatible differences. If
339 // compatible differences aren't allowed, we still only want to check for
340 // mismatches of non-modular sanitizers (the only ones which can affect AST
341 // generation).
342 if (!AllowCompatibleDifferences) {
343 SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
344 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
345 SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
346 ExistingSanitizers.clear(ModularSanitizers);
347 ImportedSanitizers.clear(ModularSanitizers);
348 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
349 const std::string Flag = "-fsanitize=";
350 if (Diags) {
351#define SANITIZER(NAME, ID) \
352 { \
353 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
354 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
355 if (InExistingModule != InImportedModule) \
356 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \
357 << InExistingModule << (Flag + NAME); \
358 }
359#include "clang/Basic/Sanitizers.def"
360 }
361 return true;
362 }
363 }
364
Guy Benyei11169dd2012-12-18 14:30:41 +0000365 return false;
366}
367
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000368/// Compare the given set of target options against an existing set of
Guy Benyei11169dd2012-12-18 14:30:41 +0000369/// target options.
370///
371/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
372///
373/// \returns true if the target options mis-match, false otherwise.
374static bool checkTargetOptions(const TargetOptions &TargetOpts,
375 const TargetOptions &ExistingTargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000376 DiagnosticsEngine *Diags,
377 bool AllowCompatibleDifferences = true) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000378#define CHECK_TARGET_OPT(Field, Name) \
379 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
380 if (Diags) \
381 Diags->Report(diag::err_pch_targetopt_mismatch) \
382 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
383 return true; \
384 }
385
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000386 // The triple and ABI must match exactly.
Guy Benyei11169dd2012-12-18 14:30:41 +0000387 CHECK_TARGET_OPT(Triple, "target");
Guy Benyei11169dd2012-12-18 14:30:41 +0000388 CHECK_TARGET_OPT(ABI, "target ABI");
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000389
390 // We can tolerate different CPUs in many cases, notably when one CPU
391 // supports a strict superset of another. When allowing compatible
392 // differences skip this check.
393 if (!AllowCompatibleDifferences)
394 CHECK_TARGET_OPT(CPU, "target CPU");
395
Guy Benyei11169dd2012-12-18 14:30:41 +0000396#undef CHECK_TARGET_OPT
397
398 // Compare feature sets.
399 SmallVector<StringRef, 4> ExistingFeatures(
400 ExistingTargetOpts.FeaturesAsWritten.begin(),
401 ExistingTargetOpts.FeaturesAsWritten.end());
402 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
403 TargetOpts.FeaturesAsWritten.end());
Fangrui Song55fab262018-09-26 22:16:28 +0000404 llvm::sort(ExistingFeatures);
405 llvm::sort(ReadFeatures);
Guy Benyei11169dd2012-12-18 14:30:41 +0000406
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000407 // We compute the set difference in both directions explicitly so that we can
408 // diagnose the differences differently.
409 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
410 std::set_difference(
411 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
412 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
413 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
414 ExistingFeatures.begin(), ExistingFeatures.end(),
415 std::back_inserter(UnmatchedReadFeatures));
Guy Benyei11169dd2012-12-18 14:30:41 +0000416
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000417 // If we are allowing compatible differences and the read feature set is
418 // a strict subset of the existing feature set, there is nothing to diagnose.
419 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
420 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +0000421
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000422 if (Diags) {
423 for (StringRef Feature : UnmatchedReadFeatures)
Guy Benyei11169dd2012-12-18 14:30:41 +0000424 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000425 << /* is-existing-feature */ false << Feature;
426 for (StringRef Feature : UnmatchedExistingFeatures)
427 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
428 << /* is-existing-feature */ true << Feature;
Guy Benyei11169dd2012-12-18 14:30:41 +0000429 }
430
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000431 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +0000432}
433
434bool
435PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000436 bool Complain,
437 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000438 const LangOptions &ExistingLangOpts = PP.getLangOpts();
439 return checkLanguageOptions(LangOpts, ExistingLangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000440 Complain ? &Reader.Diags : nullptr,
441 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +0000442}
443
444bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000445 bool Complain,
446 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000447 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
448 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000449 Complain ? &Reader.Diags : nullptr,
450 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +0000451}
452
453namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000454
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000455using MacroDefinitionsMap =
456 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
457using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000458
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000459} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +0000460
Ben Langmuirb92de022014-04-29 16:25:26 +0000461static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
462 DiagnosticsEngine &Diags,
463 bool Complain) {
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000464 using Level = DiagnosticsEngine::Level;
Ben Langmuirb92de022014-04-29 16:25:26 +0000465
466 // Check current mappings for new -Werror mappings, and the stored mappings
467 // for cases that were explicitly mapped to *not* be errors that are now
468 // errors because of options like -Werror.
469 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
470
Vedant Kumar48b4f762018-04-14 01:40:48 +0000471 for (DiagnosticsEngine *MappingSource : MappingSources) {
Ben Langmuirb92de022014-04-29 16:25:26 +0000472 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
473 diag::kind DiagID = DiagIDMappingPair.first;
474 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
475 if (CurLevel < DiagnosticsEngine::Error)
476 continue; // not significant
477 Level StoredLevel =
478 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
479 if (StoredLevel < DiagnosticsEngine::Error) {
480 if (Complain)
481 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
482 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
483 return true;
484 }
485 }
486 }
487
488 return false;
489}
490
Alp Tokerac4e8e52014-06-22 21:58:33 +0000491static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
492 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
493 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
494 return true;
495 return Ext >= diag::Severity::Error;
Ben Langmuirb92de022014-04-29 16:25:26 +0000496}
497
498static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
499 DiagnosticsEngine &Diags,
500 bool IsSystem, bool Complain) {
501 // Top-level options
502 if (IsSystem) {
503 if (Diags.getSuppressSystemWarnings())
504 return false;
505 // If -Wsystem-headers was not enabled before, be conservative
506 if (StoredDiags.getSuppressSystemWarnings()) {
507 if (Complain)
508 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
509 return true;
510 }
511 }
512
513 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
514 if (Complain)
515 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
516 return true;
517 }
518
519 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
520 !StoredDiags.getEnableAllWarnings()) {
521 if (Complain)
522 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
523 return true;
524 }
525
526 if (isExtHandlingFromDiagsError(Diags) &&
527 !isExtHandlingFromDiagsError(StoredDiags)) {
528 if (Complain)
529 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
530 return true;
531 }
532
533 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
534}
535
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +0000536/// Return the top import module if it is implicit, nullptr otherwise.
537static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
538 Preprocessor &PP) {
539 // If the original import came from a file explicitly generated by the user,
540 // don't check the diagnostic mappings.
541 // FIXME: currently this is approximated by checking whether this is not a
542 // module import of an implicitly-loaded module file.
543 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
544 // the transitive closure of its imports, since unrelated modules cannot be
545 // imported until after this module finishes validation.
546 ModuleFile *TopImport = &*ModuleMgr.rbegin();
547 while (!TopImport->ImportedBy.empty())
548 TopImport = TopImport->ImportedBy[0];
549 if (TopImport->Kind != MK_ImplicitModule)
550 return nullptr;
551
552 StringRef ModuleName = TopImport->ModuleName;
553 assert(!ModuleName.empty() && "diagnostic options read before module name");
554
555 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
556 assert(M && "missing module");
557 return M;
558}
559
Ben Langmuirb92de022014-04-29 16:25:26 +0000560bool PCHValidator::ReadDiagnosticOptions(
561 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
562 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
563 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
564 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Alp Tokerf994cef2014-07-05 03:08:06 +0000565 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
Ben Langmuirb92de022014-04-29 16:25:26 +0000566 // This should never fail, because we would have processed these options
567 // before writing them to an ASTFile.
568 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
569
570 ModuleManager &ModuleMgr = Reader.getModuleManager();
571 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
572
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +0000573 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
574 if (!TopM)
Ben Langmuirb92de022014-04-29 16:25:26 +0000575 return false;
576
Ben Langmuirb92de022014-04-29 16:25:26 +0000577 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
578 // contains the union of their flags.
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +0000579 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
580 Complain);
Ben Langmuirb92de022014-04-29 16:25:26 +0000581}
582
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000583/// Collect the macro definitions provided by the given preprocessor
Guy Benyei11169dd2012-12-18 14:30:41 +0000584/// options.
Craig Toppera13603a2014-05-22 05:54:18 +0000585static void
586collectMacroDefinitions(const PreprocessorOptions &PPOpts,
587 MacroDefinitionsMap &Macros,
588 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
Vedant Kumar48b4f762018-04-14 01:40:48 +0000589 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
590 StringRef Macro = PPOpts.Macros[I].first;
591 bool IsUndef = PPOpts.Macros[I].second;
Guy Benyei11169dd2012-12-18 14:30:41 +0000592
593 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
594 StringRef MacroName = MacroPair.first;
595 StringRef MacroBody = MacroPair.second;
596
597 // For an #undef'd macro, we only care about the name.
598 if (IsUndef) {
599 if (MacroNames && !Macros.count(MacroName))
600 MacroNames->push_back(MacroName);
601
602 Macros[MacroName] = std::make_pair("", true);
603 continue;
604 }
605
606 // For a #define'd macro, figure out the actual definition.
607 if (MacroName.size() == Macro.size())
608 MacroBody = "1";
609 else {
610 // Note: GCC drops anything following an end-of-line character.
611 StringRef::size_type End = MacroBody.find_first_of("\n\r");
612 MacroBody = MacroBody.substr(0, End);
613 }
614
615 if (MacroNames && !Macros.count(MacroName))
616 MacroNames->push_back(MacroName);
617 Macros[MacroName] = std::make_pair(MacroBody, false);
618 }
619}
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000620
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000621/// Check the preprocessor options deserialized from the control block
Guy Benyei11169dd2012-12-18 14:30:41 +0000622/// against the preprocessor options in an existing preprocessor.
623///
624/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
Yaxun Liu43712e02016-09-07 18:40:20 +0000625/// \param Validate If true, validate preprocessor options. If false, allow
626/// macros defined by \p ExistingPPOpts to override those defined by
627/// \p PPOpts in SuggestedPredefines.
Guy Benyei11169dd2012-12-18 14:30:41 +0000628static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
629 const PreprocessorOptions &ExistingPPOpts,
630 DiagnosticsEngine *Diags,
631 FileManager &FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000632 std::string &SuggestedPredefines,
Yaxun Liu43712e02016-09-07 18:40:20 +0000633 const LangOptions &LangOpts,
634 bool Validate = true) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000635 // Check macro definitions.
636 MacroDefinitionsMap ASTFileMacros;
637 collectMacroDefinitions(PPOpts, ASTFileMacros);
638 MacroDefinitionsMap ExistingMacros;
639 SmallVector<StringRef, 4> ExistingMacroNames;
640 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
641
Vedant Kumar48b4f762018-04-14 01:40:48 +0000642 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000643 // Dig out the macro definition in the existing preprocessor options.
Vedant Kumar48b4f762018-04-14 01:40:48 +0000644 StringRef MacroName = ExistingMacroNames[I];
Guy Benyei11169dd2012-12-18 14:30:41 +0000645 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
646
647 // Check whether we know anything about this macro name or not.
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000648 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
649 ASTFileMacros.find(MacroName);
Yaxun Liu43712e02016-09-07 18:40:20 +0000650 if (!Validate || Known == ASTFileMacros.end()) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000651 // FIXME: Check whether this identifier was referenced anywhere in the
652 // AST file. If so, we should reject the AST file. Unfortunately, this
653 // information isn't in the control block. What shall we do about it?
654
655 if (Existing.second) {
656 SuggestedPredefines += "#undef ";
657 SuggestedPredefines += MacroName.str();
658 SuggestedPredefines += '\n';
659 } else {
660 SuggestedPredefines += "#define ";
661 SuggestedPredefines += MacroName.str();
662 SuggestedPredefines += ' ';
663 SuggestedPredefines += Existing.first.str();
664 SuggestedPredefines += '\n';
665 }
666 continue;
667 }
668
669 // If the macro was defined in one but undef'd in the other, we have a
670 // conflict.
671 if (Existing.second != Known->second.second) {
672 if (Diags) {
673 Diags->Report(diag::err_pch_macro_def_undef)
674 << MacroName << Known->second.second;
675 }
676 return true;
677 }
678
679 // If the macro was #undef'd in both, or if the macro bodies are identical,
680 // it's fine.
681 if (Existing.second || Existing.first == Known->second.first)
682 continue;
683
684 // The macro bodies differ; complain.
685 if (Diags) {
686 Diags->Report(diag::err_pch_macro_def_conflict)
687 << MacroName << Known->second.first << Existing.first;
688 }
689 return true;
690 }
691
692 // Check whether we're using predefines.
Yaxun Liu43712e02016-09-07 18:40:20 +0000693 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000694 if (Diags) {
695 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
696 }
697 return true;
698 }
699
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000700 // Detailed record is important since it is used for the module cache hash.
701 if (LangOpts.Modules &&
Yaxun Liu43712e02016-09-07 18:40:20 +0000702 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000703 if (Diags) {
704 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
705 }
706 return true;
707 }
708
Guy Benyei11169dd2012-12-18 14:30:41 +0000709 // Compute the #include and #include_macros lines we need.
Vedant Kumar48b4f762018-04-14 01:40:48 +0000710 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
711 StringRef File = ExistingPPOpts.Includes[I];
Erich Keane76675de2018-07-05 17:22:13 +0000712
713 if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
714 !ExistingPPOpts.PCHThroughHeader.empty()) {
715 // In case the through header is an include, we must add all the includes
716 // to the predefines so the start point can be determined.
717 SuggestedPredefines += "#include \"";
718 SuggestedPredefines += File;
719 SuggestedPredefines += "\"\n";
720 continue;
721 }
722
Guy Benyei11169dd2012-12-18 14:30:41 +0000723 if (File == ExistingPPOpts.ImplicitPCHInclude)
724 continue;
725
726 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
727 != PPOpts.Includes.end())
728 continue;
729
730 SuggestedPredefines += "#include \"";
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000731 SuggestedPredefines += File;
Guy Benyei11169dd2012-12-18 14:30:41 +0000732 SuggestedPredefines += "\"\n";
733 }
734
Vedant Kumar48b4f762018-04-14 01:40:48 +0000735 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
736 StringRef File = ExistingPPOpts.MacroIncludes[I];
Guy Benyei11169dd2012-12-18 14:30:41 +0000737 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
738 File)
739 != PPOpts.MacroIncludes.end())
740 continue;
741
742 SuggestedPredefines += "#__include_macros \"";
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000743 SuggestedPredefines += File;
Guy Benyei11169dd2012-12-18 14:30:41 +0000744 SuggestedPredefines += "\"\n##\n";
745 }
746
747 return false;
748}
749
750bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
751 bool Complain,
752 std::string &SuggestedPredefines) {
753 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
754
755 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
Craig Toppera13603a2014-05-22 05:54:18 +0000756 Complain? &Reader.Diags : nullptr,
Guy Benyei11169dd2012-12-18 14:30:41 +0000757 PP.getFileManager(),
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000758 SuggestedPredefines,
759 PP.getLangOpts());
Guy Benyei11169dd2012-12-18 14:30:41 +0000760}
761
Yaxun Liu43712e02016-09-07 18:40:20 +0000762bool SimpleASTReaderListener::ReadPreprocessorOptions(
763 const PreprocessorOptions &PPOpts,
764 bool Complain,
765 std::string &SuggestedPredefines) {
766 return checkPreprocessorOptions(PPOpts,
767 PP.getPreprocessorOpts(),
768 nullptr,
769 PP.getFileManager(),
770 SuggestedPredefines,
771 PP.getLangOpts(),
772 false);
773}
774
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000775/// Check the header search options deserialized from the control block
776/// against the header search options in an existing preprocessor.
777///
778/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
779static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
780 StringRef SpecificModuleCachePath,
781 StringRef ExistingModuleCachePath,
782 DiagnosticsEngine *Diags,
783 const LangOptions &LangOpts) {
784 if (LangOpts.Modules) {
785 if (SpecificModuleCachePath != ExistingModuleCachePath) {
786 if (Diags)
787 Diags->Report(diag::err_pch_modulecache_mismatch)
788 << SpecificModuleCachePath << ExistingModuleCachePath;
789 return true;
790 }
791 }
792
793 return false;
794}
795
796bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
797 StringRef SpecificModuleCachePath,
798 bool Complain) {
799 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
800 PP.getHeaderSearchInfo().getModuleCachePath(),
801 Complain ? &Reader.Diags : nullptr,
802 PP.getLangOpts());
803}
804
Guy Benyei11169dd2012-12-18 14:30:41 +0000805void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
806 PP.setCounterValue(Value);
807}
808
809//===----------------------------------------------------------------------===//
810// AST reader implementation
811//===----------------------------------------------------------------------===//
812
Nico Weber824285e2014-05-08 04:26:47 +0000813void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
814 bool TakeOwnership) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000815 DeserializationListener = Listener;
Nico Weber824285e2014-05-08 04:26:47 +0000816 OwnsDeserializationListener = TakeOwnership;
Guy Benyei11169dd2012-12-18 14:30:41 +0000817}
818
Guy Benyei11169dd2012-12-18 14:30:41 +0000819unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
820 return serialization::ComputeHash(Sel);
821}
822
Guy Benyei11169dd2012-12-18 14:30:41 +0000823std::pair<unsigned, unsigned>
824ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000825 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000826
Justin Bogner57ba0b22014-03-28 22:03:24 +0000827 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
828 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000829 return std::make_pair(KeyLen, DataLen);
830}
831
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000832ASTSelectorLookupTrait::internal_key_type
Guy Benyei11169dd2012-12-18 14:30:41 +0000833ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000834 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000835
Guy Benyei11169dd2012-12-18 14:30:41 +0000836 SelectorTable &SelTable = Reader.getContext().Selectors;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000837 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
838 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
839 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000840 if (N == 0)
841 return SelTable.getNullarySelector(FirstII);
842 else if (N == 1)
843 return SelTable.getUnarySelector(FirstII);
844
845 SmallVector<IdentifierInfo *, 16> Args;
846 Args.push_back(FirstII);
847 for (unsigned I = 1; I != N; ++I)
Justin Bogner57ba0b22014-03-28 22:03:24 +0000848 Args.push_back(Reader.getLocalIdentifier(
849 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000850
851 return SelTable.getSelector(N, Args.data());
852}
853
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000854ASTSelectorLookupTrait::data_type
855ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
Guy Benyei11169dd2012-12-18 14:30:41 +0000856 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000857 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000858
859 data_type Result;
860
Justin Bogner57ba0b22014-03-28 22:03:24 +0000861 Result.ID = Reader.getGlobalSelectorID(
862 F, endian::readNext<uint32_t, little, unaligned>(d));
Nico Weberff4b35e2014-12-27 22:14:15 +0000863 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
864 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
865 Result.InstanceBits = FullInstanceBits & 0x3;
866 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
867 Result.FactoryBits = FullFactoryBits & 0x3;
868 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
869 unsigned NumInstanceMethods = FullInstanceBits >> 3;
870 unsigned NumFactoryMethods = FullFactoryBits >> 3;
Guy Benyei11169dd2012-12-18 14:30:41 +0000871
872 // Load instance methods
873 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Vedant Kumar48b4f762018-04-14 01:40:48 +0000874 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
Justin Bogner57ba0b22014-03-28 22:03:24 +0000875 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei11169dd2012-12-18 14:30:41 +0000876 Result.Instance.push_back(Method);
877 }
878
879 // Load factory methods
880 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Vedant Kumar48b4f762018-04-14 01:40:48 +0000881 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
Justin Bogner57ba0b22014-03-28 22:03:24 +0000882 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei11169dd2012-12-18 14:30:41 +0000883 Result.Factory.push_back(Method);
884 }
885
886 return Result;
887}
888
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000889unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +0000890 return llvm::djbHash(a);
Guy Benyei11169dd2012-12-18 14:30:41 +0000891}
892
893std::pair<unsigned, unsigned>
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000894ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000895 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000896
Justin Bogner57ba0b22014-03-28 22:03:24 +0000897 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
898 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000899 return std::make_pair(KeyLen, DataLen);
900}
901
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000902ASTIdentifierLookupTraitBase::internal_key_type
903ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000904 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000905 return StringRef((const char*) d, n-1);
Guy Benyei11169dd2012-12-18 14:30:41 +0000906}
907
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000908/// Whether the given identifier is "interesting".
Richard Smitha534a312015-07-21 23:54:07 +0000909static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
910 bool IsModule) {
Richard Smithcab89802015-07-17 20:19:56 +0000911 return II.hadMacroDefinition() ||
912 II.isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +0000913 (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
Douglas Gregordcf25082013-02-11 18:16:18 +0000914 II.hasRevertedTokenIDToIdentifier() ||
Richard Smithdbafb6c2017-06-29 23:23:46 +0000915 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
Bruno Ricci366ba732018-09-21 12:53:22 +0000916 II.getFETokenInfo());
Douglas Gregordcf25082013-02-11 18:16:18 +0000917}
918
Richard Smith76c2f2c2015-07-17 20:09:43 +0000919static bool readBit(unsigned &Bits) {
920 bool Value = Bits & 0x1;
921 Bits >>= 1;
922 return Value;
923}
924
Richard Smith79bf9202015-08-24 03:33:22 +0000925IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
926 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000927
Richard Smith79bf9202015-08-24 03:33:22 +0000928 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
929 return Reader.getGlobalIdentifierID(F, RawID >> 1);
930}
931
Richard Smitheb4b58f62016-02-05 01:40:54 +0000932static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
933 if (!II.isFromAST()) {
934 II.setIsFromAST();
935 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
936 if (isInterestingIdentifier(Reader, II, IsModule))
937 II.setChangedSinceDeserialization();
938 }
939}
940
Guy Benyei11169dd2012-12-18 14:30:41 +0000941IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
942 const unsigned char* d,
943 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000944 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +0000945
Justin Bogner57ba0b22014-03-28 22:03:24 +0000946 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000947 bool IsInteresting = RawID & 0x01;
948
949 // Wipe out the "is interesting" bit.
950 RawID = RawID >> 1;
951
Richard Smith76c2f2c2015-07-17 20:09:43 +0000952 // Build the IdentifierInfo and link the identifier ID with it.
953 IdentifierInfo *II = KnownII;
954 if (!II) {
955 II = &Reader.getIdentifierTable().getOwn(k);
956 KnownII = II;
957 }
Richard Smitheb4b58f62016-02-05 01:40:54 +0000958 markIdentifierFromAST(Reader, *II);
Richard Smith76c2f2c2015-07-17 20:09:43 +0000959 Reader.markIdentifierUpToDate(II);
960
Guy Benyei11169dd2012-12-18 14:30:41 +0000961 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
962 if (!IsInteresting) {
Richard Smith76c2f2c2015-07-17 20:09:43 +0000963 // For uninteresting identifiers, there's nothing else to do. Just notify
964 // the reader that we've finished loading this identifier.
Guy Benyei11169dd2012-12-18 14:30:41 +0000965 Reader.SetIdentifierInfo(ID, II);
Guy Benyei11169dd2012-12-18 14:30:41 +0000966 return II;
967 }
968
Justin Bogner57ba0b22014-03-28 22:03:24 +0000969 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
970 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
Richard Smith76c2f2c2015-07-17 20:09:43 +0000971 bool CPlusPlusOperatorKeyword = readBit(Bits);
972 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
Richard Smith9c254182015-07-19 21:41:12 +0000973 bool HasRevertedBuiltin = readBit(Bits);
Richard Smith76c2f2c2015-07-17 20:09:43 +0000974 bool Poisoned = readBit(Bits);
975 bool ExtensionToken = readBit(Bits);
976 bool HadMacroDefinition = readBit(Bits);
Guy Benyei11169dd2012-12-18 14:30:41 +0000977
978 assert(Bits == 0 && "Extra bits in the identifier?");
979 DataLen -= 8;
980
Guy Benyei11169dd2012-12-18 14:30:41 +0000981 // Set or check the various bits in the IdentifierInfo structure.
982 // Token IDs are read-only.
Argyrios Kyrtzidisddee8c92013-02-27 01:13:51 +0000983 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Richard Smith9c254182015-07-19 21:41:12 +0000984 II->revertTokenIDToIdentifier();
985 if (!F.isModule())
986 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
987 else if (HasRevertedBuiltin && II->getBuiltinID()) {
988 II->revertBuiltin();
989 assert((II->hasRevertedBuiltin() ||
990 II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
991 "Incorrect ObjC keyword or builtin ID");
992 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000993 assert(II->isExtensionToken() == ExtensionToken &&
994 "Incorrect extension token flag");
995 (void)ExtensionToken;
996 if (Poisoned)
997 II->setIsPoisoned(true);
998 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
999 "Incorrect C++ operator keyword flag");
1000 (void)CPlusPlusOperatorKeyword;
1001
1002 // If this identifier is a macro, deserialize the macro
1003 // definition.
Richard Smith76c2f2c2015-07-17 20:09:43 +00001004 if (HadMacroDefinition) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001005 uint32_t MacroDirectivesOffset =
1006 endian::readNext<uint32_t, little, unaligned>(d);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001007 DataLen -= 4;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001008
Richard Smithd7329392015-04-21 21:46:32 +00001009 Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00001010 }
1011
1012 Reader.SetIdentifierInfo(ID, II);
1013
1014 // Read all of the declarations visible at global scope with this
1015 // name.
1016 if (DataLen > 0) {
1017 SmallVector<uint32_t, 4> DeclIDs;
1018 for (; DataLen > 0; DataLen -= 4)
Justin Bogner57ba0b22014-03-28 22:03:24 +00001019 DeclIDs.push_back(Reader.getGlobalDeclID(
1020 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +00001021 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1022 }
1023
1024 return II;
1025}
1026
Richard Smitha06c7e62015-08-26 23:55:49 +00001027DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1028 : Kind(Name.getNameKind()) {
1029 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001030 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00001031 Data = (uint64_t)Name.getAsIdentifierInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +00001032 break;
1033 case DeclarationName::ObjCZeroArgSelector:
1034 case DeclarationName::ObjCOneArgSelector:
1035 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00001036 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
Guy Benyei11169dd2012-12-18 14:30:41 +00001037 break;
1038 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00001039 Data = Name.getCXXOverloadedOperator();
1040 break;
1041 case DeclarationName::CXXLiteralOperatorName:
1042 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1043 break;
Richard Smith35845152017-02-07 01:37:30 +00001044 case DeclarationName::CXXDeductionGuideName:
1045 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1046 ->getDeclName().getAsIdentifierInfo();
1047 break;
Richard Smitha06c7e62015-08-26 23:55:49 +00001048 case DeclarationName::CXXConstructorName:
1049 case DeclarationName::CXXDestructorName:
1050 case DeclarationName::CXXConversionFunctionName:
1051 case DeclarationName::CXXUsingDirective:
1052 Data = 0;
1053 break;
1054 }
1055}
1056
1057unsigned DeclarationNameKey::getHash() const {
1058 llvm::FoldingSetNodeID ID;
1059 ID.AddInteger(Kind);
1060
1061 switch (Kind) {
1062 case DeclarationName::Identifier:
1063 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00001064 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00001065 ID.AddString(((IdentifierInfo*)Data)->getName());
1066 break;
1067 case DeclarationName::ObjCZeroArgSelector:
1068 case DeclarationName::ObjCOneArgSelector:
1069 case DeclarationName::ObjCMultiArgSelector:
1070 ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1071 break;
1072 case DeclarationName::CXXOperatorName:
1073 ID.AddInteger((OverloadedOperatorKind)Data);
Guy Benyei11169dd2012-12-18 14:30:41 +00001074 break;
1075 case DeclarationName::CXXConstructorName:
1076 case DeclarationName::CXXDestructorName:
1077 case DeclarationName::CXXConversionFunctionName:
1078 case DeclarationName::CXXUsingDirective:
1079 break;
1080 }
1081
1082 return ID.ComputeHash();
1083}
1084
Richard Smithd88a7f12015-09-01 20:35:42 +00001085ModuleFile *
1086ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1087 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001088
Richard Smithd88a7f12015-09-01 20:35:42 +00001089 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1090 return Reader.getLocalModuleFile(F, ModuleFileID);
1091}
1092
Guy Benyei11169dd2012-12-18 14:30:41 +00001093std::pair<unsigned, unsigned>
Richard Smitha06c7e62015-08-26 23:55:49 +00001094ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001095 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001096
Justin Bogner57ba0b22014-03-28 22:03:24 +00001097 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1098 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +00001099 return std::make_pair(KeyLen, DataLen);
1100}
1101
Richard Smitha06c7e62015-08-26 23:55:49 +00001102ASTDeclContextNameLookupTrait::internal_key_type
1103ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001104 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +00001105
Vedant Kumar48b4f762018-04-14 01:40:48 +00001106 auto Kind = (DeclarationName::NameKind)*d++;
Richard Smitha06c7e62015-08-26 23:55:49 +00001107 uint64_t Data;
1108 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001109 case DeclarationName::Identifier:
Richard Smith35845152017-02-07 01:37:30 +00001110 case DeclarationName::CXXLiteralOperatorName:
1111 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00001112 Data = (uint64_t)Reader.getLocalIdentifier(
Justin Bogner57ba0b22014-03-28 22:03:24 +00001113 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +00001114 break;
1115 case DeclarationName::ObjCZeroArgSelector:
1116 case DeclarationName::ObjCOneArgSelector:
1117 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00001118 Data =
Justin Bogner57ba0b22014-03-28 22:03:24 +00001119 (uint64_t)Reader.getLocalSelector(
1120 F, endian::readNext<uint32_t, little, unaligned>(
1121 d)).getAsOpaquePtr();
Guy Benyei11169dd2012-12-18 14:30:41 +00001122 break;
1123 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00001124 Data = *d++; // OverloadedOperatorKind
Guy Benyei11169dd2012-12-18 14:30:41 +00001125 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001126 case DeclarationName::CXXConstructorName:
1127 case DeclarationName::CXXDestructorName:
1128 case DeclarationName::CXXConversionFunctionName:
1129 case DeclarationName::CXXUsingDirective:
Richard Smitha06c7e62015-08-26 23:55:49 +00001130 Data = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +00001131 break;
1132 }
1133
Richard Smitha06c7e62015-08-26 23:55:49 +00001134 return DeclarationNameKey(Kind, Data);
Guy Benyei11169dd2012-12-18 14:30:41 +00001135}
1136
Richard Smithd88a7f12015-09-01 20:35:42 +00001137void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1138 const unsigned char *d,
1139 unsigned DataLen,
1140 data_type_builder &Val) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001141 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001142
Richard Smithd88a7f12015-09-01 20:35:42 +00001143 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1144 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1145 Val.insert(Reader.getGlobalDeclID(F, LocalID));
1146 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001147}
1148
Richard Smith0f4e2c42015-08-06 04:23:48 +00001149bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1150 BitstreamCursor &Cursor,
1151 uint64_t Offset,
1152 DeclContext *DC) {
1153 assert(Offset != 0);
1154
Guy Benyei11169dd2012-12-18 14:30:41 +00001155 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00001156 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1157 Error(std::move(Err));
1158 return true;
1159 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001160
Richard Smith0f4e2c42015-08-06 04:23:48 +00001161 RecordData Record;
1162 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00001163 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1164 if (!MaybeCode) {
1165 Error(MaybeCode.takeError());
1166 return true;
1167 }
1168 unsigned Code = MaybeCode.get();
1169
1170 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1171 if (!MaybeRecCode) {
1172 Error(MaybeRecCode.takeError());
1173 return true;
1174 }
1175 unsigned RecCode = MaybeRecCode.get();
Richard Smith0f4e2c42015-08-06 04:23:48 +00001176 if (RecCode != DECL_CONTEXT_LEXICAL) {
1177 Error("Expected lexical block");
1178 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001179 }
1180
Richard Smith82f8fcd2015-08-06 22:07:25 +00001181 assert(!isa<TranslationUnitDecl>(DC) &&
1182 "expected a TU_UPDATE_LEXICAL record for TU");
Richard Smith9c9173d2015-08-11 22:00:24 +00001183 // If we are handling a C++ class template instantiation, we can see multiple
1184 // lexical updates for the same record. It's important that we select only one
1185 // of them, so that field numbering works properly. Just pick the first one we
1186 // see.
1187 auto &Lex = LexicalDecls[DC];
1188 if (!Lex.first) {
1189 Lex = std::make_pair(
1190 &M, llvm::makeArrayRef(
1191 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1192 Blob.data()),
1193 Blob.size() / 4));
1194 }
Richard Smith0f4e2c42015-08-06 04:23:48 +00001195 DC->setHasExternalLexicalStorage(true);
1196 return false;
1197}
Guy Benyei11169dd2012-12-18 14:30:41 +00001198
Richard Smith0f4e2c42015-08-06 04:23:48 +00001199bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1200 BitstreamCursor &Cursor,
1201 uint64_t Offset,
1202 DeclID ID) {
1203 assert(Offset != 0);
1204
1205 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00001206 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1207 Error(std::move(Err));
1208 return true;
1209 }
Richard Smith0f4e2c42015-08-06 04:23:48 +00001210
1211 RecordData Record;
1212 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00001213 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1214 if (!MaybeCode) {
1215 Error(MaybeCode.takeError());
1216 return true;
1217 }
1218 unsigned Code = MaybeCode.get();
1219
1220 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1221 if (!MaybeRecCode) {
1222 Error(MaybeRecCode.takeError());
1223 return true;
1224 }
1225 unsigned RecCode = MaybeRecCode.get();
Richard Smith0f4e2c42015-08-06 04:23:48 +00001226 if (RecCode != DECL_CONTEXT_VISIBLE) {
1227 Error("Expected visible lookup table block");
1228 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001229 }
1230
Richard Smith0f4e2c42015-08-06 04:23:48 +00001231 // We can't safely determine the primary context yet, so delay attaching the
1232 // lookup table until we're done with recursive deserialization.
Richard Smithd88a7f12015-09-01 20:35:42 +00001233 auto *Data = (const unsigned char*)Blob.data();
1234 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
Guy Benyei11169dd2012-12-18 14:30:41 +00001235 return false;
1236}
1237
Richard Smith37a93df2017-02-18 00:32:02 +00001238void ASTReader::Error(StringRef Msg) const {
Guy Benyei11169dd2012-12-18 14:30:41 +00001239 Error(diag::err_fe_pch_malformed, Msg);
Richard Smithdbafb6c2017-06-29 23:23:46 +00001240 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
Richard Smithfb1e7f72015-08-14 05:02:58 +00001241 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
Douglas Gregor940e8052013-05-10 22:15:13 +00001242 Diag(diag::note_module_cache_path)
1243 << PP.getHeaderSearchInfo().getModuleCachePath();
1244 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001245}
1246
Duncan P. N. Exon Smitheef69022019-11-10 11:17:42 -08001247void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1248 StringRef Arg3) const {
Guy Benyei11169dd2012-12-18 14:30:41 +00001249 if (Diags.isDiagnosticInFlight())
Duncan P. N. Exon Smitheef69022019-11-10 11:17:42 -08001250 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
Guy Benyei11169dd2012-12-18 14:30:41 +00001251 else
Duncan P. N. Exon Smitheef69022019-11-10 11:17:42 -08001252 Diag(DiagID) << Arg1 << Arg2 << Arg3;
Guy Benyei11169dd2012-12-18 14:30:41 +00001253}
1254
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00001255void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1256 unsigned Select) const {
1257 if (!Diags.isDiagnosticInFlight())
1258 Diag(DiagID) << Arg1 << Arg2 << Select;
1259}
1260
JF Bastien0e828952019-06-26 19:50:12 +00001261void ASTReader::Error(llvm::Error &&Err) const {
1262 Error(toString(std::move(Err)));
1263}
1264
Guy Benyei11169dd2012-12-18 14:30:41 +00001265//===----------------------------------------------------------------------===//
1266// Source Manager Deserialization
1267//===----------------------------------------------------------------------===//
1268
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001269/// Read the line table in the source manager block.
Guy Benyei11169dd2012-12-18 14:30:41 +00001270/// \returns true if there was an error.
1271bool ASTReader::ParseLineTable(ModuleFile &F,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001272 const RecordData &Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001273 unsigned Idx = 0;
1274 LineTableInfo &LineTable = SourceMgr.getLineTable();
1275
1276 // Parse the file names
1277 std::map<int, int> FileIDs;
Hans Wennborg14487362017-12-04 22:28:45 +00001278 FileIDs[-1] = -1; // For unspecified filenames.
Richard Smith63078492015-09-01 07:41:55 +00001279 for (unsigned I = 0; Record[Idx]; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001280 // Extract the file name
Richard Smith7ed1bc92014-12-05 22:42:13 +00001281 auto Filename = ReadPath(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001282 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1283 }
Richard Smith63078492015-09-01 07:41:55 +00001284 ++Idx;
Guy Benyei11169dd2012-12-18 14:30:41 +00001285
1286 // Parse the line entries
1287 std::vector<LineEntry> Entries;
1288 while (Idx < Record.size()) {
1289 int FID = Record[Idx++];
1290 assert(FID >= 0 && "Serialized line entries for non-local file.");
1291 // Remap FileID from 1-based old view.
1292 FID += F.SLocEntryBaseID - 1;
1293
1294 // Extract the line entries
1295 unsigned NumEntries = Record[Idx++];
Richard Smith63078492015-09-01 07:41:55 +00001296 assert(NumEntries && "no line entries for file ID");
Guy Benyei11169dd2012-12-18 14:30:41 +00001297 Entries.clear();
1298 Entries.reserve(NumEntries);
1299 for (unsigned I = 0; I != NumEntries; ++I) {
1300 unsigned FileOffset = Record[Idx++];
1301 unsigned LineNo = Record[Idx++];
1302 int FilenameID = FileIDs[Record[Idx++]];
Vedant Kumar48b4f762018-04-14 01:40:48 +00001303 SrcMgr::CharacteristicKind FileKind
1304 = (SrcMgr::CharacteristicKind)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00001305 unsigned IncludeOffset = Record[Idx++];
1306 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1307 FileKind, IncludeOffset));
1308 }
1309 LineTable.AddEntry(FileID::get(FID), Entries);
1310 }
1311
1312 return false;
1313}
1314
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001315/// Read a source manager block
Guy Benyei11169dd2012-12-18 14:30:41 +00001316bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1317 using namespace SrcMgr;
1318
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001319 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001320
1321 // Set the source-location entry cursor to the current position in
1322 // the stream. This cursor will be used to read the contents of the
1323 // source manager block initially, and then lazily read
1324 // source-location entries as needed.
1325 SLocEntryCursor = F.Stream;
1326
1327 // The stream itself is going to skip over the source manager block.
JF Bastien0e828952019-06-26 19:50:12 +00001328 if (llvm::Error Err = F.Stream.SkipBlock()) {
1329 Error(std::move(Err));
Guy Benyei11169dd2012-12-18 14:30:41 +00001330 return true;
1331 }
1332
1333 // Enter the source manager block.
JF Bastien0e828952019-06-26 19:50:12 +00001334 if (llvm::Error Err =
1335 SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1336 Error(std::move(Err));
Guy Benyei11169dd2012-12-18 14:30:41 +00001337 return true;
1338 }
1339
1340 RecordData Record;
1341 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00001342 Expected<llvm::BitstreamEntry> MaybeE =
1343 SLocEntryCursor.advanceSkippingSubblocks();
1344 if (!MaybeE) {
1345 Error(MaybeE.takeError());
1346 return true;
1347 }
1348 llvm::BitstreamEntry E = MaybeE.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001349
Chris Lattnere7b154b2013-01-19 21:39:22 +00001350 switch (E.Kind) {
1351 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1352 case llvm::BitstreamEntry::Error:
1353 Error("malformed block record in AST file");
1354 return true;
1355 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00001356 return false;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001357 case llvm::BitstreamEntry::Record:
1358 // The interesting case.
1359 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001360 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001361
Guy Benyei11169dd2012-12-18 14:30:41 +00001362 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001363 Record.clear();
Chris Lattner15c3e7d2013-01-21 18:28:26 +00001364 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00001365 Expected<unsigned> MaybeRecord =
1366 SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1367 if (!MaybeRecord) {
1368 Error(MaybeRecord.takeError());
1369 return true;
1370 }
1371 switch (MaybeRecord.get()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001372 default: // Default behavior: ignore.
1373 break;
1374
1375 case SM_SLOC_FILE_ENTRY:
1376 case SM_SLOC_BUFFER_ENTRY:
1377 case SM_SLOC_EXPANSION_ENTRY:
1378 // Once we hit one of the source location entries, we're done.
1379 return false;
1380 }
1381 }
1382}
1383
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001384/// If a header file is not found at the path that we expect it to be
Guy Benyei11169dd2012-12-18 14:30:41 +00001385/// and the PCH file was moved from its original location, try to resolve the
1386/// file by assuming that header+PCH were moved together and the header is in
1387/// the same place relative to the PCH.
1388static std::string
1389resolveFileRelativeToOriginalDir(const std::string &Filename,
1390 const std::string &OriginalDir,
1391 const std::string &CurrDir) {
1392 assert(OriginalDir != CurrDir &&
1393 "No point trying to resolve the file if the PCH dir didn't change");
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001394
Guy Benyei11169dd2012-12-18 14:30:41 +00001395 using namespace llvm::sys;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001396
Guy Benyei11169dd2012-12-18 14:30:41 +00001397 SmallString<128> filePath(Filename);
1398 fs::make_absolute(filePath);
1399 assert(path::is_absolute(OriginalDir));
1400 SmallString<128> currPCHPath(CurrDir);
1401
1402 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1403 fileDirE = path::end(path::parent_path(filePath));
1404 path::const_iterator origDirI = path::begin(OriginalDir),
1405 origDirE = path::end(OriginalDir);
1406 // Skip the common path components from filePath and OriginalDir.
1407 while (fileDirI != fileDirE && origDirI != origDirE &&
1408 *fileDirI == *origDirI) {
1409 ++fileDirI;
1410 ++origDirI;
1411 }
1412 for (; origDirI != origDirE; ++origDirI)
1413 path::append(currPCHPath, "..");
1414 path::append(currPCHPath, fileDirI, fileDirE);
1415 path::append(currPCHPath, path::filename(Filename));
Benjamin Krameradcd0262020-01-28 20:23:46 +01001416 return std::string(currPCHPath.str());
Guy Benyei11169dd2012-12-18 14:30:41 +00001417}
1418
1419bool ASTReader::ReadSLocEntry(int ID) {
1420 if (ID == 0)
1421 return false;
1422
1423 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1424 Error("source location entry ID out-of-range for AST file");
1425 return true;
1426 }
1427
Richard Smithaada85c2016-02-06 02:06:43 +00001428 // Local helper to read the (possibly-compressed) buffer data following the
1429 // entry record.
1430 auto ReadBuffer = [this](
1431 BitstreamCursor &SLocEntryCursor,
1432 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1433 RecordData Record;
1434 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00001435 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1436 if (!MaybeCode) {
1437 Error(MaybeCode.takeError());
1438 return nullptr;
1439 }
1440 unsigned Code = MaybeCode.get();
1441
1442 Expected<unsigned> MaybeRecCode =
1443 SLocEntryCursor.readRecord(Code, Record, &Blob);
1444 if (!MaybeRecCode) {
1445 Error(MaybeRecCode.takeError());
1446 return nullptr;
1447 }
1448 unsigned RecCode = MaybeRecCode.get();
Richard Smithaada85c2016-02-06 02:06:43 +00001449
1450 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
George Rimarc39f5492017-01-17 15:45:31 +00001451 if (!llvm::zlib::isAvailable()) {
1452 Error("zlib is not available");
1453 return nullptr;
1454 }
Richard Smithaada85c2016-02-06 02:06:43 +00001455 SmallString<0> Uncompressed;
George Rimarc39f5492017-01-17 15:45:31 +00001456 if (llvm::Error E =
1457 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1458 Error("could not decompress embedded file contents: " +
1459 llvm::toString(std::move(E)));
Richard Smithaada85c2016-02-06 02:06:43 +00001460 return nullptr;
1461 }
1462 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1463 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1464 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1465 } else {
1466 Error("AST record has invalid code");
1467 return nullptr;
1468 }
1469 };
1470
Guy Benyei11169dd2012-12-18 14:30:41 +00001471 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
JF Bastien0e828952019-06-26 19:50:12 +00001472 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
Dmitry Polukhin30d59462020-03-21 13:52:51 -07001473 F->SLocEntryOffsetsBase +
JF Bastien0e828952019-06-26 19:50:12 +00001474 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1475 Error(std::move(Err));
1476 return true;
1477 }
1478
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001479 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001480 unsigned BaseOffset = F->SLocEntryBaseOffset;
1481
1482 ++NumSLocEntriesRead;
JF Bastien0e828952019-06-26 19:50:12 +00001483 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1484 if (!MaybeEntry) {
1485 Error(MaybeEntry.takeError());
1486 return true;
1487 }
1488 llvm::BitstreamEntry Entry = MaybeEntry.get();
1489
Chris Lattnere7b154b2013-01-19 21:39:22 +00001490 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001491 Error("incorrectly-formatted source location entry in AST file");
1492 return true;
1493 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001494
Guy Benyei11169dd2012-12-18 14:30:41 +00001495 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +00001496 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00001497 Expected<unsigned> MaybeSLOC =
1498 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1499 if (!MaybeSLOC) {
1500 Error(MaybeSLOC.takeError());
1501 return true;
1502 }
1503 switch (MaybeSLOC.get()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001504 default:
1505 Error("incorrectly-formatted source location entry in AST file");
1506 return true;
1507
1508 case SM_SLOC_FILE_ENTRY: {
1509 // We will detect whether a file changed and return 'Failure' for it, but
1510 // we will also try to fail gracefully by setting up the SLocEntry.
1511 unsigned InputID = Record[4];
1512 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001513 const FileEntry *File = IF.getFile();
1514 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei11169dd2012-12-18 14:30:41 +00001515
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001516 // Note that we only check if a File was returned. If it was out-of-date
1517 // we have complained but we will continue creating a FileID to recover
1518 // gracefully.
1519 if (!File)
Guy Benyei11169dd2012-12-18 14:30:41 +00001520 return true;
1521
1522 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1523 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1524 // This is the module's main file.
1525 IncludeLoc = getImportLocation(F);
1526 }
Vedant Kumar48b4f762018-04-14 01:40:48 +00001527 SrcMgr::CharacteristicKind
1528 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
Alex Lorenz4dc55732019-08-22 18:15:50 +00001529 // FIXME: The FileID should be created from the FileEntryRef.
Guy Benyei11169dd2012-12-18 14:30:41 +00001530 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1531 ID, BaseOffset + Record[0]);
Vedant Kumar48b4f762018-04-14 01:40:48 +00001532 SrcMgr::FileInfo &FileInfo =
1533 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Guy Benyei11169dd2012-12-18 14:30:41 +00001534 FileInfo.NumCreatedFIDs = Record[5];
1535 if (Record[3])
1536 FileInfo.setHasLineDirectives();
1537
Guy Benyei11169dd2012-12-18 14:30:41 +00001538 unsigned NumFileDecls = Record[7];
Richard Smithdbafb6c2017-06-29 23:23:46 +00001539 if (NumFileDecls && ContextObj) {
Roman Lebedevbf4f1e02019-10-10 12:22:42 +00001540 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
Guy Benyei11169dd2012-12-18 14:30:41 +00001541 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1542 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1543 NumFileDecls));
1544 }
Richard Smithaada85c2016-02-06 02:06:43 +00001545
Guy Benyei11169dd2012-12-18 14:30:41 +00001546 const SrcMgr::ContentCache *ContentCache
Richard Smithf3f84612017-06-29 02:19:42 +00001547 = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
Guy Benyei11169dd2012-12-18 14:30:41 +00001548 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
Richard Smitha8cfffa2015-11-26 02:04:16 +00001549 ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1550 !ContentCache->getRawBuffer()) {
Richard Smithaada85c2016-02-06 02:06:43 +00001551 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1552 if (!Buffer)
Guy Benyei11169dd2012-12-18 14:30:41 +00001553 return true;
David Blaikie49cc3182014-08-27 20:54:45 +00001554 SourceMgr.overrideFileContents(File, std::move(Buffer));
Guy Benyei11169dd2012-12-18 14:30:41 +00001555 }
1556
1557 break;
1558 }
1559
1560 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00001561 const char *Name = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00001562 unsigned Offset = Record[0];
Vedant Kumar48b4f762018-04-14 01:40:48 +00001563 SrcMgr::CharacteristicKind
1564 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
Guy Benyei11169dd2012-12-18 14:30:41 +00001565 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Manman Ren11f2a472016-08-18 17:42:15 +00001566 if (IncludeLoc.isInvalid() && F->isModule()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001567 IncludeLoc = getImportLocation(F);
1568 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001569
Richard Smithaada85c2016-02-06 02:06:43 +00001570 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1571 if (!Buffer)
Guy Benyei11169dd2012-12-18 14:30:41 +00001572 return true;
David Blaikie50a5f972014-08-29 07:59:55 +00001573 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
Rafael Espindolad87f8d72014-08-27 20:03:29 +00001574 BaseOffset + Offset, IncludeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001575 break;
1576 }
1577
1578 case SM_SLOC_EXPANSION_ENTRY: {
1579 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1580 SourceMgr.createExpansionLoc(SpellingLoc,
1581 ReadSourceLocation(*F, Record[2]),
1582 ReadSourceLocation(*F, Record[3]),
Richard Smithb5f81712018-04-30 05:25:48 +00001583 Record[5],
Guy Benyei11169dd2012-12-18 14:30:41 +00001584 Record[4],
1585 ID,
1586 BaseOffset + Record[0]);
1587 break;
1588 }
1589 }
1590
1591 return false;
1592}
1593
1594std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1595 if (ID == 0)
1596 return std::make_pair(SourceLocation(), "");
1597
1598 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1599 Error("source location entry ID out-of-range for AST file");
1600 return std::make_pair(SourceLocation(), "");
1601 }
1602
1603 // Find which module file this entry lands in.
1604 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
Manman Ren11f2a472016-08-18 17:42:15 +00001605 if (!M->isModule())
Guy Benyei11169dd2012-12-18 14:30:41 +00001606 return std::make_pair(SourceLocation(), "");
1607
1608 // FIXME: Can we map this down to a particular submodule? That would be
1609 // ideal.
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001610 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
Guy Benyei11169dd2012-12-18 14:30:41 +00001611}
1612
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001613/// Find the location where the module F is imported.
Guy Benyei11169dd2012-12-18 14:30:41 +00001614SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1615 if (F->ImportLoc.isValid())
1616 return F->ImportLoc;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001617
Guy Benyei11169dd2012-12-18 14:30:41 +00001618 // Otherwise we have a PCH. It's considered to be "imported" at the first
1619 // location of its includer.
1620 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001621 // Main file is the importer.
Yaron Keren8b563662015-10-03 10:46:20 +00001622 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001623 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
Guy Benyei11169dd2012-12-18 14:30:41 +00001624 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001625 return F->ImportedBy[0]->FirstLoc;
1626}
1627
JF Bastien0e828952019-06-26 19:50:12 +00001628/// Enter a subblock of the specified BlockID with the specified cursor. Read
1629/// the abbreviations that are at the top of the block and then leave the cursor
1630/// pointing into the block.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001631bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
JF Bastien0e828952019-06-26 19:50:12 +00001632 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1633 // FIXME this drops errors on the floor.
1634 consumeError(std::move(Err));
Richard Smith0516b182015-09-08 19:40:14 +00001635 return true;
JF Bastien0e828952019-06-26 19:50:12 +00001636 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001637
1638 while (true) {
1639 uint64_t Offset = Cursor.GetCurrentBitNo();
JF Bastien0e828952019-06-26 19:50:12 +00001640 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1641 if (!MaybeCode) {
1642 // FIXME this drops errors on the floor.
1643 consumeError(MaybeCode.takeError());
1644 return true;
1645 }
1646 unsigned Code = MaybeCode.get();
Guy Benyei11169dd2012-12-18 14:30:41 +00001647
1648 // We expect all abbrevs to be at the start of the block.
1649 if (Code != llvm::bitc::DEFINE_ABBREV) {
JF Bastien0e828952019-06-26 19:50:12 +00001650 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1651 // FIXME this drops errors on the floor.
1652 consumeError(std::move(Err));
1653 return true;
1654 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001655 return false;
1656 }
JF Bastien0e828952019-06-26 19:50:12 +00001657 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1658 // FIXME this drops errors on the floor.
1659 consumeError(std::move(Err));
1660 return true;
1661 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001662 }
1663}
1664
Richard Smithe40f2ba2013-08-07 21:41:30 +00001665Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
John McCallf413f5e2013-05-03 00:10:13 +00001666 unsigned &Idx) {
1667 Token Tok;
1668 Tok.startToken();
1669 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1670 Tok.setLength(Record[Idx++]);
1671 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1672 Tok.setIdentifierInfo(II);
1673 Tok.setKind((tok::TokenKind)Record[Idx++]);
1674 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1675 return Tok;
1676}
1677
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001678MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001679 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001680
1681 // Keep track of where we are in the stream, then jump back there
1682 // after reading this macro.
1683 SavedStreamPosition SavedPosition(Stream);
1684
JF Bastien0e828952019-06-26 19:50:12 +00001685 if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1686 // FIXME this drops errors on the floor.
1687 consumeError(std::move(Err));
1688 return nullptr;
1689 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001690 RecordData Record;
Faisal Valiac506d72017-07-17 17:18:43 +00001691 SmallVector<IdentifierInfo*, 16> MacroParams;
Craig Toppera13603a2014-05-22 05:54:18 +00001692 MacroInfo *Macro = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001693
Guy Benyei11169dd2012-12-18 14:30:41 +00001694 while (true) {
Chris Lattnerefa77172013-01-20 00:00:22 +00001695 // Advance to the next record, but if we get to the end of the block, don't
1696 // pop it (removing all the abbreviations from the cursor) since we want to
1697 // be able to reseek within the block and read entries.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001698 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
JF Bastien0e828952019-06-26 19:50:12 +00001699 Expected<llvm::BitstreamEntry> MaybeEntry =
1700 Stream.advanceSkippingSubblocks(Flags);
1701 if (!MaybeEntry) {
1702 Error(MaybeEntry.takeError());
1703 return Macro;
1704 }
1705 llvm::BitstreamEntry Entry = MaybeEntry.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001706
Chris Lattnerefa77172013-01-20 00:00:22 +00001707 switch (Entry.Kind) {
1708 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1709 case llvm::BitstreamEntry::Error:
1710 Error("malformed block record in AST file");
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001711 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001712 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001713 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001714 case llvm::BitstreamEntry::Record:
1715 // The interesting case.
1716 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001717 }
1718
1719 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001720 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00001721 PreprocessorRecordTypes RecType;
1722 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1723 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1724 else {
1725 Error(MaybeRecType.takeError());
1726 return Macro;
1727 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001728 switch (RecType) {
Richard Smithd7329392015-04-21 21:46:32 +00001729 case PP_MODULE_MACRO:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001730 case PP_MACRO_DIRECTIVE_HISTORY:
1731 return Macro;
1732
Guy Benyei11169dd2012-12-18 14:30:41 +00001733 case PP_MACRO_OBJECT_LIKE:
1734 case PP_MACRO_FUNCTION_LIKE: {
1735 // If we already have a macro, that means that we've hit the end
1736 // of the definition of the macro we were looking for. We're
1737 // done.
1738 if (Macro)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001739 return Macro;
Guy Benyei11169dd2012-12-18 14:30:41 +00001740
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001741 unsigned NextIndex = 1; // Skip identifier ID.
Guy Benyei11169dd2012-12-18 14:30:41 +00001742 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Richard Smith3f6dd7a2017-05-12 23:40:52 +00001743 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Argyrios Kyrtzidis7572be22013-01-07 19:16:23 +00001744 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei11169dd2012-12-18 14:30:41 +00001745 MI->setIsUsed(Record[NextIndex++]);
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00001746 MI->setUsedForHeaderGuard(Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001747
Guy Benyei11169dd2012-12-18 14:30:41 +00001748 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1749 // Decode function-like macro info.
1750 bool isC99VarArgs = Record[NextIndex++];
1751 bool isGNUVarArgs = Record[NextIndex++];
1752 bool hasCommaPasting = Record[NextIndex++];
Faisal Valiac506d72017-07-17 17:18:43 +00001753 MacroParams.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00001754 unsigned NumArgs = Record[NextIndex++];
1755 for (unsigned i = 0; i != NumArgs; ++i)
Faisal Valiac506d72017-07-17 17:18:43 +00001756 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00001757
1758 // Install function-like macro info.
1759 MI->setIsFunctionLike();
1760 if (isC99VarArgs) MI->setIsC99Varargs();
1761 if (isGNUVarArgs) MI->setIsGNUVarargs();
1762 if (hasCommaPasting) MI->setHasCommaPasting();
Faisal Valiac506d72017-07-17 17:18:43 +00001763 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
Guy Benyei11169dd2012-12-18 14:30:41 +00001764 }
1765
Guy Benyei11169dd2012-12-18 14:30:41 +00001766 // Remember that we saw this macro last so that we add the tokens that
1767 // form its body to it.
1768 Macro = MI;
1769
1770 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1771 Record[NextIndex]) {
1772 // We have a macro definition. Register the association
1773 PreprocessedEntityID
1774 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1775 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Richard Smith66a81862015-05-04 02:25:31 +00001776 PreprocessingRecord::PPEntityID PPID =
1777 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
Vedant Kumar48b4f762018-04-14 01:40:48 +00001778 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
Richard Smith66a81862015-05-04 02:25:31 +00001779 PPRec.getPreprocessedEntity(PPID));
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +00001780 if (PPDef)
1781 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei11169dd2012-12-18 14:30:41 +00001782 }
1783
1784 ++NumMacrosRead;
1785 break;
1786 }
1787
1788 case PP_TOKEN: {
1789 // If we see a TOKEN before a PP_MACRO_*, then the file is
1790 // erroneous, just pretend we didn't see this.
Craig Toppera13603a2014-05-22 05:54:18 +00001791 if (!Macro) break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001792
John McCallf413f5e2013-05-03 00:10:13 +00001793 unsigned Idx = 0;
1794 Token Tok = ReadToken(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001795 Macro->AddTokenToBody(Tok);
1796 break;
1797 }
1798 }
1799 }
1800}
1801
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001802PreprocessedEntityID
Richard Smith37a93df2017-02-18 00:32:02 +00001803ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1804 unsigned LocalID) const {
1805 if (!M.ModuleOffsetMap.empty())
1806 ReadModuleOffsetMap(M);
1807
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001808 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Guy Benyei11169dd2012-12-18 14:30:41 +00001809 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001810 assert(I != M.PreprocessedEntityRemap.end()
Guy Benyei11169dd2012-12-18 14:30:41 +00001811 && "Invalid index into preprocessed entity index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001812
Guy Benyei11169dd2012-12-18 14:30:41 +00001813 return LocalID + I->second;
1814}
1815
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001816unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1817 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei11169dd2012-12-18 14:30:41 +00001818}
Richard Smith7ed1bc92014-12-05 22:42:13 +00001819
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001820HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001821HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
Richard Smithe75ee0f2015-08-17 07:13:32 +00001822 internal_key_type ikey = {FE->getSize(),
1823 M.HasTimestamps ? FE->getModificationTime() : 0,
1824 FE->getName(), /*Imported*/ false};
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001825 return ikey;
1826}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001827
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001828bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
Richard Smithe75ee0f2015-08-17 07:13:32 +00001829 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
Guy Benyei11169dd2012-12-18 14:30:41 +00001830 return false;
1831
Mehdi Amini004b9c72016-10-10 22:52:47 +00001832 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001833 return true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001834
Guy Benyei11169dd2012-12-18 14:30:41 +00001835 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001836 FileManager &FileMgr = Reader.getFileManager();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001837 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
Harlan Haskins8d323d12019-08-01 21:31:56 +00001838 if (!Key.Imported) {
1839 if (auto File = FileMgr.getFile(Key.Filename))
1840 return *File;
1841 return nullptr;
1842 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001843
Benjamin Krameradcd0262020-01-28 20:23:46 +01001844 std::string Resolved = std::string(Key.Filename);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001845 Reader.ResolveImportedPath(M, Resolved);
Harlan Haskins8d323d12019-08-01 21:31:56 +00001846 if (auto File = FileMgr.getFile(Resolved))
1847 return *File;
1848 return nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001849 };
1850
1851 const FileEntry *FEA = GetFile(a);
1852 const FileEntry *FEB = GetFile(b);
1853 return FEA && FEA == FEB;
Guy Benyei11169dd2012-12-18 14:30:41 +00001854}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001855
Guy Benyei11169dd2012-12-18 14:30:41 +00001856std::pair<unsigned, unsigned>
1857HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001858 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001859
Vedant Kumar48b4f762018-04-14 01:40:48 +00001860 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1861 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001862 return std::make_pair(KeyLen, DataLen);
Guy Benyei11169dd2012-12-18 14:30:41 +00001863}
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001864
1865HeaderFileInfoTrait::internal_key_type
1866HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001867 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001868
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001869 internal_key_type ikey;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001870 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1871 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001872 ikey.Filename = (const char *)d;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001873 ikey.Imported = true;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001874 return ikey;
1875}
1876
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001877HeaderFileInfoTrait::data_type
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001878HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei11169dd2012-12-18 14:30:41 +00001879 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001880 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001881
1882 const unsigned char *End = d + DataLen;
Guy Benyei11169dd2012-12-18 14:30:41 +00001883 HeaderFileInfo HFI;
1884 unsigned Flags = *d++;
Richard Smith386bb072015-08-18 23:42:23 +00001885 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
Richard Smithf3f84612017-06-29 02:19:42 +00001886 HFI.isImport |= (Flags >> 5) & 0x01;
1887 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1888 HFI.DirInfo = (Flags >> 1) & 0x07;
Guy Benyei11169dd2012-12-18 14:30:41 +00001889 HFI.IndexHeaderMapHeader = Flags & 0x01;
Richard Smith386bb072015-08-18 23:42:23 +00001890 // FIXME: Find a better way to handle this. Maybe just store a
1891 // "has been included" flag?
1892 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1893 HFI.NumIncludes);
Justin Bogner57ba0b22014-03-28 22:03:24 +00001894 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1895 M, endian::readNext<uint32_t, little, unaligned>(d));
1896 if (unsigned FrameworkOffset =
1897 endian::readNext<uint32_t, little, unaligned>(d)) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001898 // The framework offset is 1 greater than the actual offset,
Guy Benyei11169dd2012-12-18 14:30:41 +00001899 // since 0 is used as an indicator for "no framework name".
1900 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1901 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1902 }
Richard Smith386bb072015-08-18 23:42:23 +00001903
1904 assert((End - d) % 4 == 0 &&
1905 "Wrong data length in HeaderFileInfo deserialization");
1906 while (d != End) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001907 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
Richard Smith386bb072015-08-18 23:42:23 +00001908 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1909 LocalSMID >>= 2;
1910
1911 // This header is part of a module. Associate it with the module to enable
1912 // implicit module import.
1913 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1914 Module *Mod = Reader.getSubmodule(GlobalSMID);
1915 FileManager &FileMgr = Reader.getFileManager();
1916 ModuleMap &ModMap =
1917 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1918
Benjamin Krameradcd0262020-01-28 20:23:46 +01001919 std::string Filename = std::string(key.Filename);
Richard Smith386bb072015-08-18 23:42:23 +00001920 if (key.Imported)
1921 Reader.ResolveImportedPath(M, Filename);
1922 // FIXME: This is not always the right filename-as-written, but we're not
1923 // going to use this information to rebuild the module, so it doesn't make
1924 // a lot of difference.
Benjamin Krameradcd0262020-01-28 20:23:46 +01001925 Module::Header H = {std::string(key.Filename), *FileMgr.getFile(Filename)};
Richard Smithd8879c82015-08-24 21:59:32 +00001926 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1927 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001928 }
1929
Guy Benyei11169dd2012-12-18 14:30:41 +00001930 // This HeaderFileInfo was externally loaded.
1931 HFI.External = true;
Richard Smithd8879c82015-08-24 21:59:32 +00001932 HFI.IsValid = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001933 return HFI;
1934}
1935
Dmitry Polukhin30d59462020-03-21 13:52:51 -07001936void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1937 uint32_t MacroDirectivesOffset) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001938 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1939 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00001940}
1941
1942void ASTReader::ReadDefinedMacros() {
1943 // Note that we are loading defined macros.
1944 Deserializing Macros(this);
1945
Vedant Kumar48b4f762018-04-14 01:40:48 +00001946 for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001947 BitstreamCursor &MacroCursor = I.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001948
1949 // If there was no preprocessor block, skip this file.
Peter Collingbourne77c89b62016-11-08 04:17:11 +00001950 if (MacroCursor.getBitcodeBytes().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00001951 continue;
1952
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001953 BitstreamCursor Cursor = MacroCursor;
JF Bastien0e828952019-06-26 19:50:12 +00001954 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1955 Error(std::move(Err));
1956 return;
1957 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001958
1959 RecordData Record;
1960 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00001961 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1962 if (!MaybeE) {
1963 Error(MaybeE.takeError());
1964 return;
1965 }
1966 llvm::BitstreamEntry E = MaybeE.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001967
Chris Lattnere7b154b2013-01-19 21:39:22 +00001968 switch (E.Kind) {
1969 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1970 case llvm::BitstreamEntry::Error:
1971 Error("malformed block record in AST file");
1972 return;
1973 case llvm::BitstreamEntry::EndBlock:
1974 goto NextCursor;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001975
JF Bastien0e828952019-06-26 19:50:12 +00001976 case llvm::BitstreamEntry::Record: {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001977 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00001978 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1979 if (!MaybeRecord) {
1980 Error(MaybeRecord.takeError());
1981 return;
1982 }
1983 switch (MaybeRecord.get()) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001984 default: // Default behavior: ignore.
1985 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001986
Chris Lattnere7b154b2013-01-19 21:39:22 +00001987 case PP_MACRO_OBJECT_LIKE:
Sean Callananf3682a72016-05-14 06:24:14 +00001988 case PP_MACRO_FUNCTION_LIKE: {
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001989 IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
Sean Callananf3682a72016-05-14 06:24:14 +00001990 if (II->isOutOfDate())
1991 updateOutOfDateIdentifier(*II);
Chris Lattnere7b154b2013-01-19 21:39:22 +00001992 break;
Sean Callananf3682a72016-05-14 06:24:14 +00001993 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001994
Chris Lattnere7b154b2013-01-19 21:39:22 +00001995 case PP_TOKEN:
1996 // Ignore tokens.
1997 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001998 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001999 break;
2000 }
JF Bastien0e828952019-06-26 19:50:12 +00002001 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002002 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002003 NextCursor: ;
Guy Benyei11169dd2012-12-18 14:30:41 +00002004 }
2005}
2006
2007namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002008
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002009 /// Visitor class used to look up identifirs in an AST file.
Guy Benyei11169dd2012-12-18 14:30:41 +00002010 class IdentifierLookupVisitor {
2011 StringRef Name;
Richard Smith3b637412015-07-14 18:42:41 +00002012 unsigned NameHash;
Guy Benyei11169dd2012-12-18 14:30:41 +00002013 unsigned PriorGeneration;
Douglas Gregor00a50f72013-01-25 00:38:33 +00002014 unsigned &NumIdentifierLookups;
2015 unsigned &NumIdentifierLookupHits;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002016 IdentifierInfo *Found = nullptr;
Douglas Gregor00a50f72013-01-25 00:38:33 +00002017
Guy Benyei11169dd2012-12-18 14:30:41 +00002018 public:
Douglas Gregor00a50f72013-01-25 00:38:33 +00002019 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2020 unsigned &NumIdentifierLookups,
2021 unsigned &NumIdentifierLookupHits)
Richard Smith3b637412015-07-14 18:42:41 +00002022 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2023 PriorGeneration(PriorGeneration),
Douglas Gregor00a50f72013-01-25 00:38:33 +00002024 NumIdentifierLookups(NumIdentifierLookups),
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002025 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00002026
2027 bool operator()(ModuleFile &M) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002028 // If we've already searched this module file, skip it now.
Richard Smithbdf2d932015-07-30 03:37:16 +00002029 if (M.Generation <= PriorGeneration)
Guy Benyei11169dd2012-12-18 14:30:41 +00002030 return true;
Douglas Gregore060e572013-01-25 01:03:03 +00002031
Vedant Kumar48b4f762018-04-14 01:40:48 +00002032 ASTIdentifierLookupTable *IdTable
2033 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
Guy Benyei11169dd2012-12-18 14:30:41 +00002034 if (!IdTable)
2035 return false;
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00002036
2037 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
Richard Smithbdf2d932015-07-30 03:37:16 +00002038 Found);
2039 ++NumIdentifierLookups;
Richard Smith3b637412015-07-14 18:42:41 +00002040 ASTIdentifierLookupTable::iterator Pos =
Richard Smithbdf2d932015-07-30 03:37:16 +00002041 IdTable->find_hashed(Name, NameHash, &Trait);
Guy Benyei11169dd2012-12-18 14:30:41 +00002042 if (Pos == IdTable->end())
2043 return false;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002044
Guy Benyei11169dd2012-12-18 14:30:41 +00002045 // Dereferencing the iterator has the effect of building the
2046 // IdentifierInfo node and populating it with the various
2047 // declarations it needs.
Richard Smithbdf2d932015-07-30 03:37:16 +00002048 ++NumIdentifierLookupHits;
2049 Found = *Pos;
Guy Benyei11169dd2012-12-18 14:30:41 +00002050 return true;
2051 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002052
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002053 // Retrieve the identifier info found within the module
Guy Benyei11169dd2012-12-18 14:30:41 +00002054 // files.
2055 IdentifierInfo *getIdentifierInfo() const { return Found; }
2056 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002057
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002058} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00002059
2060void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2061 // Note that we are loading an identifier.
2062 Deserializing AnIdentifier(this);
2063
2064 unsigned PriorGeneration = 0;
2065 if (getContext().getLangOpts().Modules)
2066 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregore060e572013-01-25 01:03:03 +00002067
2068 // If there is a global index, look there first to determine which modules
2069 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00002070 GlobalModuleIndex::HitSet Hits;
Craig Toppera13603a2014-05-22 05:54:18 +00002071 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
Douglas Gregore060e572013-01-25 01:03:03 +00002072 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00002073 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2074 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00002075 }
2076 }
2077
Douglas Gregor7211ac12013-01-25 23:32:03 +00002078 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregor00a50f72013-01-25 00:38:33 +00002079 NumIdentifierLookups,
2080 NumIdentifierLookupHits);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00002081 ModuleMgr.visit(Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00002082 markIdentifierUpToDate(&II);
2083}
2084
2085void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2086 if (!II)
2087 return;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002088
Guy Benyei11169dd2012-12-18 14:30:41 +00002089 II->setOutOfDate(false);
2090
2091 // Update the generation for this identifier.
2092 if (getContext().getLangOpts().Modules)
Richard Smith053f6c62014-05-16 23:01:30 +00002093 IdentifierGeneration[II] = getGeneration();
Guy Benyei11169dd2012-12-18 14:30:41 +00002094}
2095
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002096void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2097 const PendingMacroInfo &PMInfo) {
Richard Smithd7329392015-04-21 21:46:32 +00002098 ModuleFile &M = *PMInfo.M;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002099
2100 BitstreamCursor &Cursor = M.MacroCursor;
2101 SavedStreamPosition SavedPosition(Cursor);
Dmitry Polukhin30d59462020-03-21 13:52:51 -07002102 if (llvm::Error Err =
2103 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
JF Bastien0e828952019-06-26 19:50:12 +00002104 Error(std::move(Err));
2105 return;
2106 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002107
Richard Smith713369b2015-04-23 20:40:50 +00002108 struct ModuleMacroRecord {
2109 SubmoduleID SubModID;
2110 MacroInfo *MI;
2111 SmallVector<SubmoduleID, 8> Overrides;
2112 };
2113 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002114
Richard Smithd7329392015-04-21 21:46:32 +00002115 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2116 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2117 // macro histroy.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002118 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002119 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00002120 Expected<llvm::BitstreamEntry> MaybeEntry =
Richard Smithd7329392015-04-21 21:46:32 +00002121 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
JF Bastien0e828952019-06-26 19:50:12 +00002122 if (!MaybeEntry) {
2123 Error(MaybeEntry.takeError());
2124 return;
2125 }
2126 llvm::BitstreamEntry Entry = MaybeEntry.get();
2127
Richard Smithd7329392015-04-21 21:46:32 +00002128 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2129 Error("malformed block record in AST file");
2130 return;
2131 }
2132
2133 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00002134 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2135 if (!MaybePP) {
2136 Error(MaybePP.takeError());
2137 return;
2138 }
2139 switch ((PreprocessorRecordTypes)MaybePP.get()) {
Richard Smithd7329392015-04-21 21:46:32 +00002140 case PP_MACRO_DIRECTIVE_HISTORY:
2141 break;
2142
2143 case PP_MODULE_MACRO: {
Richard Smith713369b2015-04-23 20:40:50 +00002144 ModuleMacros.push_back(ModuleMacroRecord());
2145 auto &Info = ModuleMacros.back();
Richard Smithe56c8bc2015-04-22 00:26:11 +00002146 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2147 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
Richard Smith713369b2015-04-23 20:40:50 +00002148 for (int I = 2, N = Record.size(); I != N; ++I)
2149 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
Richard Smithd7329392015-04-21 21:46:32 +00002150 continue;
2151 }
2152
2153 default:
2154 Error("malformed block record in AST file");
2155 return;
2156 }
2157
2158 // We found the macro directive history; that's the last record
2159 // for this macro.
2160 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002161 }
2162
Richard Smithd7329392015-04-21 21:46:32 +00002163 // Module macros are listed in reverse dependency order.
Richard Smithe56c8bc2015-04-22 00:26:11 +00002164 {
2165 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
Richard Smithe56c8bc2015-04-22 00:26:11 +00002166 llvm::SmallVector<ModuleMacro*, 8> Overrides;
Richard Smith713369b2015-04-23 20:40:50 +00002167 for (auto &MMR : ModuleMacros) {
Richard Smithe56c8bc2015-04-22 00:26:11 +00002168 Overrides.clear();
Vedant Kumar48b4f762018-04-14 01:40:48 +00002169 for (unsigned ModID : MMR.Overrides) {
Richard Smithb8b2ed62015-04-23 18:18:26 +00002170 Module *Mod = getSubmodule(ModID);
2171 auto *Macro = PP.getModuleMacro(Mod, II);
Richard Smithe56c8bc2015-04-22 00:26:11 +00002172 assert(Macro && "missing definition for overridden macro");
Richard Smith5dbef922015-04-22 02:09:43 +00002173 Overrides.push_back(Macro);
Richard Smithe56c8bc2015-04-22 00:26:11 +00002174 }
2175
2176 bool Inserted = false;
Richard Smith713369b2015-04-23 20:40:50 +00002177 Module *Owner = getSubmodule(MMR.SubModID);
Richard Smith20e883e2015-04-29 23:20:19 +00002178 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
Richard Smithd7329392015-04-21 21:46:32 +00002179 }
2180 }
2181
2182 // Don't read the directive history for a module; we don't have anywhere
2183 // to put it.
Manman Ren11f2a472016-08-18 17:42:15 +00002184 if (M.isModule())
Richard Smithd7329392015-04-21 21:46:32 +00002185 return;
2186
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002187 // Deserialize the macro directives history in reverse source-order.
Craig Toppera13603a2014-05-22 05:54:18 +00002188 MacroDirective *Latest = nullptr, *Earliest = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002189 unsigned Idx = 0, N = Record.size();
2190 while (Idx < N) {
Craig Toppera13603a2014-05-22 05:54:18 +00002191 MacroDirective *MD = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002192 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00002193 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002194 switch (K) {
2195 case MacroDirective::MD_Define: {
Richard Smith713369b2015-04-23 20:40:50 +00002196 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
Richard Smith3981b172015-04-30 02:16:23 +00002197 MD = PP.AllocateDefMacroDirective(MI, Loc);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002198 break;
2199 }
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002200 case MacroDirective::MD_Undefine:
Richard Smith3981b172015-04-30 02:16:23 +00002201 MD = PP.AllocateUndefMacroDirective(Loc);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002202 break;
Richard Smithdaa69e02014-07-25 04:40:03 +00002203 case MacroDirective::MD_Visibility:
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002204 bool isPublic = Record[Idx++];
2205 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2206 break;
2207 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002208
2209 if (!Latest)
2210 Latest = MD;
2211 if (Earliest)
2212 Earliest->setPrevious(MD);
2213 Earliest = MD;
2214 }
2215
Richard Smithd6e8c0d2015-05-04 19:58:00 +00002216 if (Latest)
Nico Weberfd870702016-12-09 17:32:52 +00002217 PP.setLoadedMacroDirective(II, Earliest, Latest);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002218}
2219
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002220ASTReader::InputFileInfo
2221ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002222 // Go find this input file.
2223 BitstreamCursor &Cursor = F.InputFilesCursor;
2224 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00002225 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2226 // FIXME this drops errors on the floor.
2227 consumeError(std::move(Err));
2228 }
Ben Langmuir198c1682014-03-07 07:27:49 +00002229
JF Bastien0e828952019-06-26 19:50:12 +00002230 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2231 if (!MaybeCode) {
2232 // FIXME this drops errors on the floor.
2233 consumeError(MaybeCode.takeError());
2234 }
2235 unsigned Code = MaybeCode.get();
Ben Langmuir198c1682014-03-07 07:27:49 +00002236 RecordData Record;
2237 StringRef Blob;
2238
JF Bastien0e828952019-06-26 19:50:12 +00002239 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2240 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2241 "invalid record type for input file");
2242 else {
2243 // FIXME this drops errors on the floor.
2244 consumeError(Maybe.takeError());
2245 }
Ben Langmuir198c1682014-03-07 07:27:49 +00002246
2247 assert(Record[0] == ID && "Bogus stored ID or offset");
Richard Smitha8cfffa2015-11-26 02:04:16 +00002248 InputFileInfo R;
2249 R.StoredSize = static_cast<off_t>(Record[1]);
2250 R.StoredTime = static_cast<time_t>(Record[2]);
2251 R.Overridden = static_cast<bool>(Record[3]);
2252 R.Transient = static_cast<bool>(Record[4]);
Richard Smithf3f84612017-06-29 02:19:42 +00002253 R.TopLevelModuleMap = static_cast<bool>(Record[5]);
Benjamin Krameradcd0262020-01-28 20:23:46 +01002254 R.Filename = std::string(Blob);
Richard Smitha8cfffa2015-11-26 02:04:16 +00002255 ResolveImportedPath(F, R.Filename);
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002256
2257 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2258 if (!MaybeEntry) // FIXME this drops errors on the floor.
2259 consumeError(MaybeEntry.takeError());
2260 llvm::BitstreamEntry Entry = MaybeEntry.get();
2261 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2262 "expected record type for input file hash");
2263
2264 Record.clear();
2265 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2266 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2267 "invalid record type for input file hash");
2268 else {
2269 // FIXME this drops errors on the floor.
2270 consumeError(Maybe.takeError());
2271 }
2272 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2273 static_cast<uint64_t>(Record[0]);
Hans Wennborg73945142014-03-14 17:45:06 +00002274 return R;
Ben Langmuir198c1682014-03-07 07:27:49 +00002275}
2276
Manman Renc8c94152016-10-21 23:35:03 +00002277static unsigned moduleKindForDiagnostic(ModuleKind Kind);
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002278InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002279 // If this ID is bogus, just return an empty input file.
2280 if (ID == 0 || ID > F.InputFilesLoaded.size())
2281 return InputFile();
2282
2283 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002284 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei11169dd2012-12-18 14:30:41 +00002285 return F.InputFilesLoaded[ID-1];
2286
Argyrios Kyrtzidis9308f0a2014-01-08 19:13:34 +00002287 if (F.InputFilesLoaded[ID-1].isNotFound())
2288 return InputFile();
2289
Guy Benyei11169dd2012-12-18 14:30:41 +00002290 // Go find this input file.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002291 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00002292 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00002293 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2294 // FIXME this drops errors on the floor.
2295 consumeError(std::move(Err));
2296 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002297
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002298 InputFileInfo FI = readInputFileInfo(F, ID);
2299 off_t StoredSize = FI.StoredSize;
2300 time_t StoredTime = FI.StoredTime;
2301 bool Overridden = FI.Overridden;
Richard Smitha8cfffa2015-11-26 02:04:16 +00002302 bool Transient = FI.Transient;
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002303 StringRef Filename = FI.Filename;
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002304 uint64_t StoredContentHash = FI.ContentHash;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002305
Harlan Haskins8d323d12019-08-01 21:31:56 +00002306 const FileEntry *File = nullptr;
2307 if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false))
2308 File = *FE;
2309
Ben Langmuir198c1682014-03-07 07:27:49 +00002310 // If we didn't find the file, resolve it relative to the
2311 // original directory from which this AST file was created.
Manuel Klimek1b29b4f2017-07-25 10:22:06 +00002312 if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2313 F.OriginalDir != F.BaseDirectory) {
2314 std::string Resolved = resolveFileRelativeToOriginalDir(
Benjamin Krameradcd0262020-01-28 20:23:46 +01002315 std::string(Filename), F.OriginalDir, F.BaseDirectory);
Ben Langmuir198c1682014-03-07 07:27:49 +00002316 if (!Resolved.empty())
Harlan Haskins8d323d12019-08-01 21:31:56 +00002317 if (auto FE = FileMgr.getFile(Resolved))
2318 File = *FE;
Ben Langmuir198c1682014-03-07 07:27:49 +00002319 }
2320
2321 // For an overridden file, create a virtual file with the stored
2322 // size/timestamp.
Richard Smitha8cfffa2015-11-26 02:04:16 +00002323 if ((Overridden || Transient) && File == nullptr)
Ben Langmuir198c1682014-03-07 07:27:49 +00002324 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
Ben Langmuir198c1682014-03-07 07:27:49 +00002325
Craig Toppera13603a2014-05-22 05:54:18 +00002326 if (File == nullptr) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002327 if (Complain) {
2328 std::string ErrorStr = "could not find file '";
2329 ErrorStr += Filename;
Richard Smith68142212015-10-13 01:26:26 +00002330 ErrorStr += "' referenced by AST file '";
2331 ErrorStr += F.FileName;
2332 ErrorStr += "'";
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002333 Error(ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00002334 }
Ben Langmuir198c1682014-03-07 07:27:49 +00002335 // Record that we didn't find the file.
2336 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2337 return InputFile();
2338 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002339
Ben Langmuir198c1682014-03-07 07:27:49 +00002340 // Check if there was a request to override the contents of the file
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002341 // that was part of the precompiled header. Overriding such a file
Ben Langmuir198c1682014-03-07 07:27:49 +00002342 // can lead to problems when lexing using the source locations from the
2343 // PCH.
2344 SourceManager &SM = getSourceManager();
Richard Smith64daf7b2015-12-01 03:32:49 +00002345 // FIXME: Reject if the overrides are different.
2346 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002347 if (Complain)
2348 Error(diag::err_fe_pch_file_overridden, Filename);
Duncan P. N. Exon Smithe1b7f222019-08-30 22:59:25 +00002349
2350 // After emitting the diagnostic, bypass the overriding file to recover
2351 // (this creates a separate FileEntry).
2352 File = SM.bypassFileContentsOverride(*File);
2353 if (!File) {
2354 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2355 return InputFile();
2356 }
Ben Langmuir198c1682014-03-07 07:27:49 +00002357 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002358
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002359 enum ModificationType {
2360 Size,
2361 ModTime,
2362 Content,
2363 None,
2364 };
2365 auto HasInputFileChanged = [&]() {
2366 if (StoredSize != File->getSize())
2367 return ModificationType::Size;
2368 if (!DisableValidation && StoredTime &&
2369 StoredTime != File->getModificationTime()) {
2370 // In case the modification time changes but not the content,
2371 // accept the cached file as legit.
2372 if (ValidateASTInputFilesContent &&
2373 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2374 auto MemBuffOrError = FileMgr.getBufferForFile(File);
2375 if (!MemBuffOrError) {
2376 if (!Complain)
2377 return ModificationType::ModTime;
2378 std::string ErrorStr = "could not get buffer for file '";
2379 ErrorStr += File->getName();
2380 ErrorStr += "'";
2381 Error(ErrorStr);
2382 return ModificationType::ModTime;
2383 }
Eric Christopher3be91692019-10-14 23:14:24 +00002384
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002385 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2386 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2387 return ModificationType::None;
2388 return ModificationType::Content;
2389 }
2390 return ModificationType::ModTime;
2391 }
2392 return ModificationType::None;
2393 };
2394
2395 bool IsOutOfDate = false;
2396 auto FileChange = HasInputFileChanged();
Ben Langmuir198c1682014-03-07 07:27:49 +00002397 // For an overridden file, there is nothing to validate.
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002398 if (!Overridden && FileChange != ModificationType::None) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002399 if (Complain) {
2400 // Build a list of the PCH imports that got us here (in reverse).
2401 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002402 while (!ImportStack.back()->ImportedBy.empty())
Ben Langmuir198c1682014-03-07 07:27:49 +00002403 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
Ben Langmuire82630d2014-01-17 00:19:09 +00002404
Ben Langmuir198c1682014-03-07 07:27:49 +00002405 // The top-level PCH is stale.
2406 StringRef TopLevelPCHName(ImportStack.back()->FileName);
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002407 unsigned DiagnosticKind =
2408 moduleKindForDiagnostic(ImportStack.back()->Kind);
Manman Renc8c94152016-10-21 23:35:03 +00002409 if (DiagnosticKind == 0)
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002410 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName,
2411 (unsigned)FileChange);
Manman Renc8c94152016-10-21 23:35:03 +00002412 else if (DiagnosticKind == 1)
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002413 Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName,
2414 (unsigned)FileChange);
Manman Renc8c94152016-10-21 23:35:03 +00002415 else
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002416 Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName,
2417 (unsigned)FileChange);
Ben Langmuire82630d2014-01-17 00:19:09 +00002418
Ben Langmuir198c1682014-03-07 07:27:49 +00002419 // Print the import stack.
2420 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2421 Diag(diag::note_pch_required_by)
2422 << Filename << ImportStack[0]->FileName;
2423 for (unsigned I = 1; I < ImportStack.size(); ++I)
Ben Langmuire82630d2014-01-17 00:19:09 +00002424 Diag(diag::note_pch_required_by)
Ben Langmuir198c1682014-03-07 07:27:49 +00002425 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
Douglas Gregor7029ce12013-03-19 00:28:20 +00002426 }
2427
Ben Langmuir198c1682014-03-07 07:27:49 +00002428 if (!Diags.isDiagnosticInFlight())
2429 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
Guy Benyei11169dd2012-12-18 14:30:41 +00002430 }
2431
Ben Langmuir198c1682014-03-07 07:27:49 +00002432 IsOutOfDate = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00002433 }
Richard Smitha8cfffa2015-11-26 02:04:16 +00002434 // FIXME: If the file is overridden and we've already opened it,
2435 // issue an error (or split it into a separate FileEntry).
Guy Benyei11169dd2012-12-18 14:30:41 +00002436
Richard Smitha8cfffa2015-11-26 02:04:16 +00002437 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
Ben Langmuir198c1682014-03-07 07:27:49 +00002438
2439 // Note that we've loaded this input file.
2440 F.InputFilesLoaded[ID-1] = IF;
2441 return IF;
Guy Benyei11169dd2012-12-18 14:30:41 +00002442}
2443
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002444/// If we are loading a relocatable PCH or module file, and the filename
Richard Smith7ed1bc92014-12-05 22:42:13 +00002445/// is not an absolute path, add the system or module root to the beginning of
2446/// the file name.
2447void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2448 // Resolve relative to the base directory, if we have one.
2449 if (!M.BaseDirectory.empty())
2450 return ResolveImportedPath(Filename, M.BaseDirectory);
Guy Benyei11169dd2012-12-18 14:30:41 +00002451}
2452
Richard Smith7ed1bc92014-12-05 22:42:13 +00002453void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002454 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2455 return;
2456
Richard Smith7ed1bc92014-12-05 22:42:13 +00002457 SmallString<128> Buffer;
2458 llvm::sys::path::append(Buffer, Prefix, Filename);
2459 Filename.assign(Buffer.begin(), Buffer.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00002460}
2461
Richard Smith0f99d6a2015-08-09 08:48:41 +00002462static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2463 switch (ARR) {
2464 case ASTReader::Failure: return true;
2465 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2466 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2467 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2468 case ASTReader::ConfigurationMismatch:
2469 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2470 case ASTReader::HadErrors: return true;
2471 case ASTReader::Success: return false;
2472 }
2473
2474 llvm_unreachable("unknown ASTReadResult");
2475}
2476
Richard Smith0516b182015-09-08 19:40:14 +00002477ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2478 BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2479 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002480 std::string &SuggestedPredefines) {
JF Bastien0e828952019-06-26 19:50:12 +00002481 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2482 // FIXME this drops errors on the floor.
2483 consumeError(std::move(Err));
Richard Smith0516b182015-09-08 19:40:14 +00002484 return Failure;
JF Bastien0e828952019-06-26 19:50:12 +00002485 }
Richard Smith0516b182015-09-08 19:40:14 +00002486
2487 // Read all of the records in the options block.
2488 RecordData Record;
2489 ASTReadResult Result = Success;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002490 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00002491 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2492 if (!MaybeEntry) {
2493 // FIXME this drops errors on the floor.
2494 consumeError(MaybeEntry.takeError());
2495 return Failure;
2496 }
2497 llvm::BitstreamEntry Entry = MaybeEntry.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002498
Richard Smith0516b182015-09-08 19:40:14 +00002499 switch (Entry.Kind) {
2500 case llvm::BitstreamEntry::Error:
2501 case llvm::BitstreamEntry::SubBlock:
2502 return Failure;
2503
2504 case llvm::BitstreamEntry::EndBlock:
2505 return Result;
2506
2507 case llvm::BitstreamEntry::Record:
2508 // The interesting case.
2509 break;
2510 }
2511
2512 // Read and process a record.
2513 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00002514 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2515 if (!MaybeRecordType) {
2516 // FIXME this drops errors on the floor.
2517 consumeError(MaybeRecordType.takeError());
2518 return Failure;
2519 }
2520 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
Richard Smith0516b182015-09-08 19:40:14 +00002521 case LANGUAGE_OPTIONS: {
2522 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2523 if (ParseLanguageOptions(Record, Complain, Listener,
2524 AllowCompatibleConfigurationMismatch))
2525 Result = ConfigurationMismatch;
2526 break;
2527 }
2528
2529 case TARGET_OPTIONS: {
2530 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2531 if (ParseTargetOptions(Record, Complain, Listener,
2532 AllowCompatibleConfigurationMismatch))
2533 Result = ConfigurationMismatch;
2534 break;
2535 }
2536
Richard Smith0516b182015-09-08 19:40:14 +00002537 case FILE_SYSTEM_OPTIONS: {
2538 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2539 if (!AllowCompatibleConfigurationMismatch &&
2540 ParseFileSystemOptions(Record, Complain, Listener))
2541 Result = ConfigurationMismatch;
2542 break;
2543 }
2544
2545 case HEADER_SEARCH_OPTIONS: {
2546 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2547 if (!AllowCompatibleConfigurationMismatch &&
2548 ParseHeaderSearchOptions(Record, Complain, Listener))
2549 Result = ConfigurationMismatch;
2550 break;
2551 }
2552
2553 case PREPROCESSOR_OPTIONS:
2554 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2555 if (!AllowCompatibleConfigurationMismatch &&
2556 ParsePreprocessorOptions(Record, Complain, Listener,
2557 SuggestedPredefines))
2558 Result = ConfigurationMismatch;
2559 break;
2560 }
2561 }
2562}
2563
Guy Benyei11169dd2012-12-18 14:30:41 +00002564ASTReader::ASTReadResult
2565ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002566 SmallVectorImpl<ImportedModule> &Loaded,
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002567 const ModuleFile *ImportedBy,
Guy Benyei11169dd2012-12-18 14:30:41 +00002568 unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002569 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002570
JF Bastien0e828952019-06-26 19:50:12 +00002571 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2572 Error(std::move(Err));
Guy Benyei11169dd2012-12-18 14:30:41 +00002573 return Failure;
2574 }
2575
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002576 // Lambda to read the unhashed control block the first time it's called.
2577 //
2578 // For PCM files, the unhashed control block cannot be read until after the
2579 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
2580 // need to look ahead before reading the IMPORTS record. For consistency,
2581 // this block is always read somehow (see BitstreamEntry::EndBlock).
2582 bool HasReadUnhashedControlBlock = false;
2583 auto readUnhashedControlBlockOnce = [&]() {
2584 if (!HasReadUnhashedControlBlock) {
2585 HasReadUnhashedControlBlock = true;
2586 if (ASTReadResult Result =
2587 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2588 return Result;
2589 }
2590 return Success;
2591 };
2592
Guy Benyei11169dd2012-12-18 14:30:41 +00002593 // Read all of the records and blocks in the control block.
2594 RecordData Record;
Richard Smitha1825302014-10-23 22:18:29 +00002595 unsigned NumInputs = 0;
2596 unsigned NumUserInputs = 0;
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00002597 StringRef BaseDirectoryAsWritten;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002598 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00002599 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2600 if (!MaybeEntry) {
2601 Error(MaybeEntry.takeError());
2602 return Failure;
2603 }
2604 llvm::BitstreamEntry Entry = MaybeEntry.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002605
Chris Lattnere7b154b2013-01-19 21:39:22 +00002606 switch (Entry.Kind) {
2607 case llvm::BitstreamEntry::Error:
2608 Error("malformed block record in AST file");
2609 return Failure;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002610 case llvm::BitstreamEntry::EndBlock: {
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002611 // Validate the module before returning. This call catches an AST with
2612 // no module name and no imports.
2613 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2614 return Result;
2615
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002616 // Validate input files.
2617 const HeaderSearchOptions &HSOpts =
2618 PP.getHeaderSearchInfo().getHeaderSearchOpts();
Ben Langmuircb69b572014-03-07 06:40:32 +00002619
Richard Smitha1825302014-10-23 22:18:29 +00002620 // All user input files reside at the index range [0, NumUserInputs), and
Richard Smith0f99d6a2015-08-09 08:48:41 +00002621 // system input files reside at [NumUserInputs, NumInputs). For explicitly
2622 // loaded module files, ignore missing inputs.
Manman Ren11f2a472016-08-18 17:42:15 +00002623 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2624 F.Kind != MK_PrebuiltModule) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002625 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Ben Langmuircb69b572014-03-07 06:40:32 +00002626
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002627 // If we are reading a module, we will create a verification timestamp,
2628 // so we verify all input files. Otherwise, verify only user input
2629 // files.
Ben Langmuircb69b572014-03-07 06:40:32 +00002630
2631 unsigned N = NumUserInputs;
2632 if (ValidateSystemInputs ||
Richard Smithe842a472014-10-22 02:05:46 +00002633 (HSOpts.ModulesValidateOncePerBuildSession &&
Ben Langmuiracb803e2014-11-10 22:13:10 +00002634 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
Richard Smithe842a472014-10-22 02:05:46 +00002635 F.Kind == MK_ImplicitModule))
Ben Langmuircb69b572014-03-07 06:40:32 +00002636 N = NumInputs;
2637
Ben Langmuir3d4417c2014-02-07 17:31:11 +00002638 for (unsigned I = 0; I < N; ++I) {
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002639 InputFile IF = getInputFile(F, I+1, Complain);
2640 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei11169dd2012-12-18 14:30:41 +00002641 return OutOfDate;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002642 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002643 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002644
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002645 if (Listener)
Richard Smith216a3bd2015-08-13 17:57:10 +00002646 Listener->visitModuleFile(F.FileName, F.Kind);
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002647
Ben Langmuircb69b572014-03-07 06:40:32 +00002648 if (Listener && Listener->needsInputFileVisitation()) {
2649 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2650 : NumUserInputs;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002651 for (unsigned I = 0; I < N; ++I) {
2652 bool IsSystem = I >= NumUserInputs;
2653 InputFileInfo FI = readInputFileInfo(F, I+1);
Richard Smith216a3bd2015-08-13 17:57:10 +00002654 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
Manman Ren11f2a472016-08-18 17:42:15 +00002655 F.Kind == MK_ExplicitModule ||
2656 F.Kind == MK_PrebuiltModule);
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002657 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002658 }
2659
Duncan P. N. Exon Smith69242e92019-11-19 14:44:22 -08002660 return Success;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002661 }
2662
Chris Lattnere7b154b2013-01-19 21:39:22 +00002663 case llvm::BitstreamEntry::SubBlock:
2664 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002665 case INPUT_FILES_BLOCK_ID:
2666 F.InputFilesCursor = Stream;
JF Bastien0e828952019-06-26 19:50:12 +00002667 if (llvm::Error Err = Stream.SkipBlock()) {
2668 Error(std::move(Err));
2669 return Failure;
2670 }
2671 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002672 Error("malformed block record in AST file");
2673 return Failure;
2674 }
2675 continue;
Richard Smith0516b182015-09-08 19:40:14 +00002676
2677 case OPTIONS_BLOCK_ID:
2678 // If we're reading the first module for this group, check its options
2679 // are compatible with ours. For modules it imports, no further checking
2680 // is required, because we checked them when we built it.
2681 if (Listener && !ImportedBy) {
2682 // Should we allow the configuration of the module file to differ from
2683 // the configuration of the current translation unit in a compatible
2684 // way?
2685 //
2686 // FIXME: Allow this for files explicitly specified with -include-pch.
2687 bool AllowCompatibleConfigurationMismatch =
Manman Ren11f2a472016-08-18 17:42:15 +00002688 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
Richard Smith0516b182015-09-08 19:40:14 +00002689
Duncan P. N. Exon Smith69242e92019-11-19 14:44:22 -08002690 ASTReadResult Result =
2691 ReadOptionsBlock(Stream, ClientLoadCapabilities,
2692 AllowCompatibleConfigurationMismatch, *Listener,
2693 SuggestedPredefines);
Richard Smith0516b182015-09-08 19:40:14 +00002694 if (Result == Failure) {
2695 Error("malformed block record in AST file");
2696 return Result;
2697 }
2698
Richard Smith8a308ec2015-11-05 00:54:55 +00002699 if (DisableValidation ||
2700 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2701 Result = Success;
2702
Ben Langmuir9b1e442e2016-02-11 18:54:02 +00002703 // If we can't load the module, exit early since we likely
2704 // will rebuild the module anyway. The stream may be in the
2705 // middle of a block.
2706 if (Result != Success)
Richard Smith0516b182015-09-08 19:40:14 +00002707 return Result;
JF Bastien0e828952019-06-26 19:50:12 +00002708 } else if (llvm::Error Err = Stream.SkipBlock()) {
2709 Error(std::move(Err));
Richard Smith0516b182015-09-08 19:40:14 +00002710 return Failure;
2711 }
2712 continue;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002713
Guy Benyei11169dd2012-12-18 14:30:41 +00002714 default:
JF Bastien0e828952019-06-26 19:50:12 +00002715 if (llvm::Error Err = Stream.SkipBlock()) {
2716 Error(std::move(Err));
Chris Lattnere7b154b2013-01-19 21:39:22 +00002717 return Failure;
2718 }
2719 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00002720 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002721
Chris Lattnere7b154b2013-01-19 21:39:22 +00002722 case llvm::BitstreamEntry::Record:
2723 // The interesting case.
2724 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002725 }
2726
2727 // Read and process a record.
2728 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002729 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00002730 Expected<unsigned> MaybeRecordType =
2731 Stream.readRecord(Entry.ID, Record, &Blob);
2732 if (!MaybeRecordType) {
2733 Error(MaybeRecordType.takeError());
2734 return Failure;
2735 }
2736 switch ((ControlRecordTypes)MaybeRecordType.get()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002737 case METADATA: {
2738 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2739 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002740 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2741 : diag::err_pch_version_too_new);
Guy Benyei11169dd2012-12-18 14:30:41 +00002742 return VersionMismatch;
2743 }
2744
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00002745 bool hasErrors = Record[7];
Guy Benyei11169dd2012-12-18 14:30:41 +00002746 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2747 Diag(diag::err_pch_with_compiler_errors);
2748 return HadErrors;
2749 }
Argyrios Kyrtzidis70ec1c72016-07-13 20:35:26 +00002750 if (hasErrors) {
2751 Diags.ErrorOccurred = true;
2752 Diags.UncompilableErrorOccurred = true;
2753 Diags.UnrecoverableErrorOccurred = true;
2754 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002755
2756 F.RelocatablePCH = Record[4];
Richard Smith7ed1bc92014-12-05 22:42:13 +00002757 // Relative paths in a relocatable PCH are relative to our sysroot.
2758 if (F.RelocatablePCH)
2759 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
Guy Benyei11169dd2012-12-18 14:30:41 +00002760
Richard Smithe75ee0f2015-08-17 07:13:32 +00002761 F.HasTimestamps = Record[5];
2762
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00002763 F.PCHHasObjectFile = Record[6];
2764
Guy Benyei11169dd2012-12-18 14:30:41 +00002765 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002766 StringRef ASTBranch = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002767 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2768 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002769 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
Guy Benyei11169dd2012-12-18 14:30:41 +00002770 return VersionMismatch;
2771 }
2772 break;
2773 }
2774
2775 case IMPORTS: {
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002776 // Validate the AST before processing any imports (otherwise, untangling
2777 // them can be error-prone and expensive). A module will have a name and
2778 // will already have been validated, but this catches the PCH case.
2779 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2780 return Result;
2781
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002782 // Load each of the imported PCH files.
Guy Benyei11169dd2012-12-18 14:30:41 +00002783 unsigned Idx = 0, N = Record.size();
2784 while (Idx < N) {
2785 // Read information about the AST file.
Vedant Kumar48b4f762018-04-14 01:40:48 +00002786 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00002787 // The import location will be the local one for now; we will adjust
2788 // all import locations of module imports after the global source
Richard Smithb22a1d12016-03-27 20:13:24 +00002789 // location info are setup, in ReadAST.
Guy Benyei11169dd2012-12-18 14:30:41 +00002790 SourceLocation ImportLoc =
Richard Smithb22a1d12016-03-27 20:13:24 +00002791 ReadUntranslatedSourceLocation(Record[Idx++]);
Vedant Kumar48b4f762018-04-14 01:40:48 +00002792 off_t StoredSize = (off_t)Record[Idx++];
2793 time_t StoredModTime = (time_t)Record[Idx++];
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002794 ASTFileSignature StoredSignature = {
2795 {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2796 (uint32_t)Record[Idx++], (uint32_t)Record[Idx++],
2797 (uint32_t)Record[Idx++]}}};
Boris Kolpackovd30446f2017-08-31 06:26:43 +00002798
2799 std::string ImportedName = ReadString(Record, Idx);
2800 std::string ImportedFile;
2801
2802 // For prebuilt and explicit modules first consult the file map for
2803 // an override. Note that here we don't search prebuilt module
2804 // directories, only the explicit name to file mappings. Also, we will
2805 // still verify the size/signature making sure it is essentially the
2806 // same file but perhaps in a different location.
2807 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2808 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2809 ImportedName, /*FileMapOnly*/ true);
2810
2811 if (ImportedFile.empty())
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00002812 // Use BaseDirectoryAsWritten to ensure we use the same path in the
2813 // ModuleCache as when writing.
2814 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
Boris Kolpackovd30446f2017-08-31 06:26:43 +00002815 else
2816 SkipPath(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00002817
Richard Smith0f99d6a2015-08-09 08:48:41 +00002818 // If our client can't cope with us being out of date, we can't cope with
2819 // our dependency being missing.
2820 unsigned Capabilities = ClientLoadCapabilities;
2821 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2822 Capabilities &= ~ARR_Missing;
2823
Guy Benyei11169dd2012-12-18 14:30:41 +00002824 // Load the AST file.
Richard Smith0f99d6a2015-08-09 08:48:41 +00002825 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2826 Loaded, StoredSize, StoredModTime,
2827 StoredSignature, Capabilities);
2828
2829 // If we diagnosed a problem, produce a backtrace.
2830 if (isDiagnosedResult(Result, Capabilities))
2831 Diag(diag::note_module_file_imported_by)
2832 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2833
2834 switch (Result) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002835 case Failure: return Failure;
2836 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor2f1806e2013-03-19 00:38:50 +00002837 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00002838 case OutOfDate: return OutOfDate;
2839 case VersionMismatch: return VersionMismatch;
2840 case ConfigurationMismatch: return ConfigurationMismatch;
2841 case HadErrors: return HadErrors;
2842 case Success: break;
2843 }
2844 }
2845 break;
2846 }
2847
Guy Benyei11169dd2012-12-18 14:30:41 +00002848 case ORIGINAL_FILE:
2849 F.OriginalSourceFileID = FileID::get(Record[0]);
Benjamin Krameradcd0262020-01-28 20:23:46 +01002850 F.ActualOriginalSourceFileName = std::string(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00002851 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
Richard Smith7ed1bc92014-12-05 22:42:13 +00002852 ResolveImportedPath(F, F.OriginalSourceFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002853 break;
2854
2855 case ORIGINAL_FILE_ID:
2856 F.OriginalSourceFileID = FileID::get(Record[0]);
2857 break;
2858
2859 case ORIGINAL_PCH_DIR:
Benjamin Krameradcd0262020-01-28 20:23:46 +01002860 F.OriginalDir = std::string(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00002861 break;
2862
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002863 case MODULE_NAME:
Benjamin Krameradcd0262020-01-28 20:23:46 +01002864 F.ModuleName = std::string(Blob);
Duncan P. N. Exon Smith9dda8f52019-03-06 02:50:46 +00002865 Diag(diag::remark_module_import)
2866 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2867 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
Ben Langmuir4f5212a2014-04-14 22:12:44 +00002868 if (Listener)
2869 Listener->ReadModuleName(F.ModuleName);
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002870
2871 // Validate the AST as soon as we have a name so we can exit early on
2872 // failure.
2873 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2874 return Result;
Vedant Kumar48b4f762018-04-14 01:40:48 +00002875
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002876 break;
2877
Richard Smith223d3f22014-12-06 03:21:08 +00002878 case MODULE_DIRECTORY: {
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00002879 // Save the BaseDirectory as written in the PCM for computing the module
2880 // filename for the ModuleCache.
2881 BaseDirectoryAsWritten = Blob;
Richard Smith223d3f22014-12-06 03:21:08 +00002882 assert(!F.ModuleName.empty() &&
2883 "MODULE_DIRECTORY found before MODULE_NAME");
2884 // If we've already loaded a module map file covering this module, we may
2885 // have a better path for it (relative to the current build).
Bruno Cardoso Lopes52431f32018-07-18 23:21:19 +00002886 Module *M = PP.getHeaderSearchInfo().lookupModule(
2887 F.ModuleName, /*AllowSearch*/ true,
2888 /*AllowExtraModuleMapSearch*/ true);
Richard Smith223d3f22014-12-06 03:21:08 +00002889 if (M && M->Directory) {
2890 // If we're implicitly loading a module, the base directory can't
2891 // change between the build and use.
Yuka Takahashid8baec22018-08-01 09:50:02 +00002892 // Don't emit module relocation error if we have -fno-validate-pch
2893 if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2894 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
Harlan Haskins8d323d12019-08-01 21:31:56 +00002895 auto BuildDir = PP.getFileManager().getDirectory(Blob);
2896 if (!BuildDir || *BuildDir != M->Directory) {
Richard Smith223d3f22014-12-06 03:21:08 +00002897 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2898 Diag(diag::err_imported_module_relocated)
2899 << F.ModuleName << Blob << M->Directory->getName();
2900 return OutOfDate;
2901 }
2902 }
Benjamin Krameradcd0262020-01-28 20:23:46 +01002903 F.BaseDirectory = std::string(M->Directory->getName());
Richard Smith223d3f22014-12-06 03:21:08 +00002904 } else {
Benjamin Krameradcd0262020-01-28 20:23:46 +01002905 F.BaseDirectory = std::string(Blob);
Richard Smith223d3f22014-12-06 03:21:08 +00002906 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002907 break;
Richard Smith223d3f22014-12-06 03:21:08 +00002908 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002909
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002910 case MODULE_MAP_FILE:
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00002911 if (ASTReadResult Result =
2912 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2913 return Result;
Ben Langmuir264ea152014-11-08 00:06:39 +00002914 break;
2915
Justin Bognerca9c0cc2015-06-21 20:32:36 +00002916 case INPUT_FILE_OFFSETS:
Richard Smitha1825302014-10-23 22:18:29 +00002917 NumInputs = Record[0];
2918 NumUserInputs = Record[1];
Justin Bogner4c183242015-06-21 20:32:40 +00002919 F.InputFileOffsets =
2920 (const llvm::support::unaligned_uint64_t *)Blob.data();
Richard Smitha1825302014-10-23 22:18:29 +00002921 F.InputFilesLoaded.resize(NumInputs);
Argyrios Kyrtzidisa38cb202017-01-30 06:05:58 +00002922 F.NumUserInputFiles = NumUserInputs;
Guy Benyei11169dd2012-12-18 14:30:41 +00002923 break;
2924 }
2925 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002926}
2927
Ben Langmuir2c9af442014-04-10 17:57:43 +00002928ASTReader::ASTReadResult
2929ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002930 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002931
JF Bastien0e828952019-06-26 19:50:12 +00002932 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2933 Error(std::move(Err));
Ben Langmuir2c9af442014-04-10 17:57:43 +00002934 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002935 }
2936
2937 // Read all of the records and blocks for the AST file.
2938 RecordData Record;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002939 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00002940 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2941 if (!MaybeEntry) {
2942 Error(MaybeEntry.takeError());
2943 return Failure;
2944 }
2945 llvm::BitstreamEntry Entry = MaybeEntry.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002946
Chris Lattnere7b154b2013-01-19 21:39:22 +00002947 switch (Entry.Kind) {
2948 case llvm::BitstreamEntry::Error:
2949 Error("error at end of module block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002950 return Failure;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002951 case llvm::BitstreamEntry::EndBlock:
Richard Smithc0fbba72013-04-03 22:49:41 +00002952 // Outside of C++, we do not store a lookup map for the translation unit.
2953 // Instead, mark it as needing a lookup map to be built if this module
2954 // contains any declarations lexically within it (which it always does!).
2955 // This usually has no cost, since we very rarely need the lookup map for
2956 // the translation unit outside C++.
Richard Smithdbafb6c2017-06-29 23:23:46 +00002957 if (ASTContext *Ctx = ContextObj) {
2958 DeclContext *DC = Ctx->getTranslationUnitDecl();
2959 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2960 DC->setMustBuildLookupTable();
2961 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002962
Ben Langmuir2c9af442014-04-10 17:57:43 +00002963 return Success;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002964 case llvm::BitstreamEntry::SubBlock:
2965 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002966 case DECLTYPES_BLOCK_ID:
2967 // We lazily load the decls block, but we want to set up the
2968 // DeclsCursor cursor to point into it. Clone our current bitcode
2969 // cursor to it, enter the block and read the abbrevs in that block.
2970 // With the main cursor, we just skip over it.
2971 F.DeclsCursor = Stream;
JF Bastien0e828952019-06-26 19:50:12 +00002972 if (llvm::Error Err = Stream.SkipBlock()) {
2973 Error(std::move(Err));
2974 return Failure;
2975 }
2976 if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002977 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002978 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002979 }
2980 break;
Richard Smithb9eab6d2014-03-20 19:44:17 +00002981
Guy Benyei11169dd2012-12-18 14:30:41 +00002982 case PREPROCESSOR_BLOCK_ID:
2983 F.MacroCursor = Stream;
2984 if (!PP.getExternalSource())
2985 PP.setExternalSource(this);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002986
JF Bastien0e828952019-06-26 19:50:12 +00002987 if (llvm::Error Err = Stream.SkipBlock()) {
2988 Error(std::move(Err));
2989 return Failure;
2990 }
2991 if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002992 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002993 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002994 }
2995 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2996 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002997
Guy Benyei11169dd2012-12-18 14:30:41 +00002998 case PREPROCESSOR_DETAIL_BLOCK_ID:
2999 F.PreprocessorDetailCursor = Stream;
JF Bastien0e828952019-06-26 19:50:12 +00003000
3001 if (llvm::Error Err = Stream.SkipBlock()) {
3002 Error(std::move(Err));
3003 return Failure;
3004 }
3005 if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00003006 PREPROCESSOR_DETAIL_BLOCK_ID)) {
JF Bastien0e828952019-06-26 19:50:12 +00003007 Error("malformed preprocessor detail record in AST file");
3008 return Failure;
3009 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003010 F.PreprocessorDetailStartOffset
Chris Lattnere7b154b2013-01-19 21:39:22 +00003011 = F.PreprocessorDetailCursor.GetCurrentBitNo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003012
Guy Benyei11169dd2012-12-18 14:30:41 +00003013 if (!PP.getPreprocessingRecord())
3014 PP.createPreprocessingRecord();
3015 if (!PP.getPreprocessingRecord()->getExternalSource())
3016 PP.getPreprocessingRecord()->SetExternalSource(*this);
3017 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003018
Guy Benyei11169dd2012-12-18 14:30:41 +00003019 case SOURCE_MANAGER_BLOCK_ID:
3020 if (ReadSourceManagerBlock(F))
Ben Langmuir2c9af442014-04-10 17:57:43 +00003021 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003022 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003023
Guy Benyei11169dd2012-12-18 14:30:41 +00003024 case SUBMODULE_BLOCK_ID:
David Blaikie9ffe5a32017-01-30 05:00:26 +00003025 if (ASTReadResult Result =
3026 ReadSubmoduleBlock(F, ClientLoadCapabilities))
Ben Langmuir2c9af442014-04-10 17:57:43 +00003027 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00003028 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003029
Guy Benyei11169dd2012-12-18 14:30:41 +00003030 case COMMENTS_BLOCK_ID: {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003031 BitstreamCursor C = Stream;
JF Bastien0e828952019-06-26 19:50:12 +00003032
3033 if (llvm::Error Err = Stream.SkipBlock()) {
3034 Error(std::move(Err));
3035 return Failure;
3036 }
3037 if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003038 Error("malformed comments block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003039 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003040 }
3041 CommentsCursors.push_back(std::make_pair(C, &F));
3042 break;
3043 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003044
Guy Benyei11169dd2012-12-18 14:30:41 +00003045 default:
JF Bastien0e828952019-06-26 19:50:12 +00003046 if (llvm::Error Err = Stream.SkipBlock()) {
3047 Error(std::move(Err));
Ben Langmuir2c9af442014-04-10 17:57:43 +00003048 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00003049 }
3050 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003051 }
3052 continue;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003053
Chris Lattnere7b154b2013-01-19 21:39:22 +00003054 case llvm::BitstreamEntry::Record:
3055 // The interesting case.
3056 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003057 }
3058
3059 // Read and process a record.
3060 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00003061 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00003062 Expected<unsigned> MaybeRecordType =
3063 Stream.readRecord(Entry.ID, Record, &Blob);
3064 if (!MaybeRecordType) {
3065 Error(MaybeRecordType.takeError());
3066 return Failure;
3067 }
3068 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
Richard Smithdbafb6c2017-06-29 23:23:46 +00003069
3070 // If we're not loading an AST context, we don't care about most records.
3071 if (!ContextObj) {
3072 switch (RecordType) {
3073 case IDENTIFIER_TABLE:
3074 case IDENTIFIER_OFFSET:
3075 case INTERESTING_IDENTIFIERS:
3076 case STATISTICS:
3077 case PP_CONDITIONAL_STACK:
3078 case PP_COUNTER_VALUE:
3079 case SOURCE_LOCATION_OFFSETS:
3080 case MODULE_OFFSET_MAP:
3081 case SOURCE_MANAGER_LINE_TABLE:
3082 case SOURCE_LOCATION_PRELOADS:
3083 case PPD_ENTITIES_OFFSETS:
3084 case HEADER_SEARCH_TABLE:
3085 case IMPORTED_MODULES:
3086 case MACRO_OFFSET:
3087 break;
3088 default:
3089 continue;
3090 }
3091 }
3092
3093 switch (RecordType) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003094 default: // Default behavior: ignore.
3095 break;
3096
3097 case TYPE_OFFSET: {
3098 if (F.LocalNumTypes != 0) {
3099 Error("duplicate TYPE_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003100 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003101 }
Dmitry Polukhin30d59462020-03-21 13:52:51 -07003102 F.TypeOffsets = reinterpret_cast<const uint64_t *>(Blob.data());
Guy Benyei11169dd2012-12-18 14:30:41 +00003103 F.LocalNumTypes = Record[0];
3104 unsigned LocalBaseTypeIndex = Record[1];
3105 F.BaseTypeIndex = getTotalNumTypes();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003106
Guy Benyei11169dd2012-12-18 14:30:41 +00003107 if (F.LocalNumTypes > 0) {
3108 // Introduce the global -> local mapping for types within this module.
3109 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003110
Guy Benyei11169dd2012-12-18 14:30:41 +00003111 // Introduce the local -> global mapping for types within this module.
3112 F.TypeRemap.insertOrReplace(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003113 std::make_pair(LocalBaseTypeIndex,
Guy Benyei11169dd2012-12-18 14:30:41 +00003114 F.BaseTypeIndex - LocalBaseTypeIndex));
Ben Langmuir52ca6782014-10-20 16:27:32 +00003115
3116 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
Guy Benyei11169dd2012-12-18 14:30:41 +00003117 }
3118 break;
3119 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003120
Guy Benyei11169dd2012-12-18 14:30:41 +00003121 case DECL_OFFSET: {
3122 if (F.LocalNumDecls != 0) {
3123 Error("duplicate DECL_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003124 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003125 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003126 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003127 F.LocalNumDecls = Record[0];
3128 unsigned LocalBaseDeclID = Record[1];
3129 F.BaseDeclID = getTotalNumDecls();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003130
Guy Benyei11169dd2012-12-18 14:30:41 +00003131 if (F.LocalNumDecls > 0) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003132 // Introduce the global -> local mapping for declarations within this
Guy Benyei11169dd2012-12-18 14:30:41 +00003133 // module.
3134 GlobalDeclMap.insert(
3135 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003136
Guy Benyei11169dd2012-12-18 14:30:41 +00003137 // Introduce the local -> global mapping for declarations within this
3138 // module.
3139 F.DeclRemap.insertOrReplace(
3140 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003141
Guy Benyei11169dd2012-12-18 14:30:41 +00003142 // Introduce the global -> local mapping for declarations within this
3143 // module.
3144 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
Ben Langmuirfe971d92014-08-16 04:54:18 +00003145
Ben Langmuir52ca6782014-10-20 16:27:32 +00003146 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3147 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003148 break;
3149 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003150
Guy Benyei11169dd2012-12-18 14:30:41 +00003151 case TU_UPDATE_LEXICAL: {
Richard Smithdbafb6c2017-06-29 23:23:46 +00003152 DeclContext *TU = ContextObj->getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00003153 LexicalContents Contents(
3154 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3155 Blob.data()),
3156 static_cast<unsigned int>(Blob.size() / 4));
3157 TULexicalDecls.push_back(std::make_pair(&F, Contents));
Guy Benyei11169dd2012-12-18 14:30:41 +00003158 TU->setHasExternalLexicalStorage(true);
3159 break;
3160 }
3161
3162 case UPDATE_VISIBLE: {
3163 unsigned Idx = 0;
3164 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003165 auto *Data = (const unsigned char*)Blob.data();
Richard Smithd88a7f12015-09-01 20:35:42 +00003166 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
Richard Smith0f4e2c42015-08-06 04:23:48 +00003167 // If we've already loaded the decl, perform the updates when we finish
3168 // loading this block.
3169 if (Decl *D = GetExistingDecl(ID))
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003170 PendingUpdateRecords.push_back(
3171 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
Guy Benyei11169dd2012-12-18 14:30:41 +00003172 break;
3173 }
3174
3175 case IDENTIFIER_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00003176 F.IdentifierTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003177 if (Record[0]) {
Justin Bognerda4e6502014-04-14 16:34:29 +00003178 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3179 (const unsigned char *)F.IdentifierTableData + Record[0],
3180 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3181 (const unsigned char *)F.IdentifierTableData,
3182 ASTIdentifierLookupTrait(*this, F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003183
Guy Benyei11169dd2012-12-18 14:30:41 +00003184 PP.getIdentifierTable().setExternalIdentifierLookup(this);
3185 }
3186 break;
3187
3188 case IDENTIFIER_OFFSET: {
3189 if (F.LocalNumIdentifiers != 0) {
3190 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003191 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003192 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003193 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003194 F.LocalNumIdentifiers = Record[0];
3195 unsigned LocalBaseIdentifierID = Record[1];
3196 F.BaseIdentifierID = getTotalNumIdentifiers();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003197
Guy Benyei11169dd2012-12-18 14:30:41 +00003198 if (F.LocalNumIdentifiers > 0) {
3199 // Introduce the global -> local mapping for identifiers within this
3200 // module.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003201 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
Guy Benyei11169dd2012-12-18 14:30:41 +00003202 &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003203
Guy Benyei11169dd2012-12-18 14:30:41 +00003204 // Introduce the local -> global mapping for identifiers within this
3205 // module.
3206 F.IdentifierRemap.insertOrReplace(
3207 std::make_pair(LocalBaseIdentifierID,
3208 F.BaseIdentifierID - LocalBaseIdentifierID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00003209
Ben Langmuir52ca6782014-10-20 16:27:32 +00003210 IdentifiersLoaded.resize(IdentifiersLoaded.size()
3211 + F.LocalNumIdentifiers);
3212 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003213 break;
3214 }
3215
Richard Smith33e0f7e2015-07-22 02:08:40 +00003216 case INTERESTING_IDENTIFIERS:
3217 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3218 break;
3219
Ben Langmuir332aafe2014-01-31 01:06:56 +00003220 case EAGERLY_DESERIALIZED_DECLS:
Richard Smith9e2341d2015-03-23 03:25:59 +00003221 // FIXME: Skip reading this record if our ASTConsumer doesn't care
3222 // about "interesting" decls (for instance, if we're building a module).
Guy Benyei11169dd2012-12-18 14:30:41 +00003223 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Ben Langmuir332aafe2014-01-31 01:06:56 +00003224 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00003225 break;
3226
David Blaikie9ffe5a32017-01-30 05:00:26 +00003227 case MODULAR_CODEGEN_DECLS:
3228 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3229 // them (ie: if we're not codegenerating this module).
Hans Wennborg7ea9a6e2020-02-27 14:33:43 +01003230 if (F.Kind == MK_MainFile)
David Blaikie9ffe5a32017-01-30 05:00:26 +00003231 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3232 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3233 break;
3234
Guy Benyei11169dd2012-12-18 14:30:41 +00003235 case SPECIAL_TYPES:
Douglas Gregor44180f82013-02-01 23:45:03 +00003236 if (SpecialTypes.empty()) {
3237 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3238 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3239 break;
3240 }
3241
3242 if (SpecialTypes.size() != Record.size()) {
3243 Error("invalid special-types record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003244 return Failure;
Douglas Gregor44180f82013-02-01 23:45:03 +00003245 }
3246
3247 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3248 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3249 if (!SpecialTypes[I])
3250 SpecialTypes[I] = ID;
3251 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3252 // merge step?
3253 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003254 break;
3255
3256 case STATISTICS:
3257 TotalNumStatements += Record[0];
3258 TotalNumMacros += Record[1];
3259 TotalLexicalDeclContexts += Record[2];
3260 TotalVisibleDeclContexts += Record[3];
3261 break;
3262
3263 case UNUSED_FILESCOPED_DECLS:
3264 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3265 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3266 break;
3267
3268 case DELEGATING_CTORS:
3269 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3270 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3271 break;
3272
3273 case WEAK_UNDECLARED_IDENTIFIERS:
3274 if (Record.size() % 4 != 0) {
3275 Error("invalid weak identifiers record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003276 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003277 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003278
3279 // FIXME: Ignore weak undeclared identifiers from non-original PCH
Guy Benyei11169dd2012-12-18 14:30:41 +00003280 // files. This isn't the way to do it :)
3281 WeakUndeclaredIdentifiers.clear();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003282
Guy Benyei11169dd2012-12-18 14:30:41 +00003283 // Translate the weak, undeclared identifiers into global IDs.
3284 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3285 WeakUndeclaredIdentifiers.push_back(
3286 getGlobalIdentifierID(F, Record[I++]));
3287 WeakUndeclaredIdentifiers.push_back(
3288 getGlobalIdentifierID(F, Record[I++]));
3289 WeakUndeclaredIdentifiers.push_back(
3290 ReadSourceLocation(F, Record, I).getRawEncoding());
3291 WeakUndeclaredIdentifiers.push_back(Record[I++]);
3292 }
3293 break;
3294
Guy Benyei11169dd2012-12-18 14:30:41 +00003295 case SELECTOR_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003296 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003297 F.LocalNumSelectors = Record[0];
3298 unsigned LocalBaseSelectorID = Record[1];
3299 F.BaseSelectorID = getTotalNumSelectors();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003300
Guy Benyei11169dd2012-12-18 14:30:41 +00003301 if (F.LocalNumSelectors > 0) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003302 // Introduce the global -> local mapping for selectors within this
Guy Benyei11169dd2012-12-18 14:30:41 +00003303 // module.
3304 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003305
3306 // Introduce the local -> global mapping for selectors within this
Guy Benyei11169dd2012-12-18 14:30:41 +00003307 // module.
3308 F.SelectorRemap.insertOrReplace(
3309 std::make_pair(LocalBaseSelectorID,
3310 F.BaseSelectorID - LocalBaseSelectorID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00003311
3312 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
Guy Benyei11169dd2012-12-18 14:30:41 +00003313 }
3314 break;
3315 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003316
Guy Benyei11169dd2012-12-18 14:30:41 +00003317 case METHOD_POOL:
Chris Lattner0e6c9402013-01-20 02:38:54 +00003318 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003319 if (Record[0])
3320 F.SelectorLookupTable
3321 = ASTSelectorLookupTable::Create(
3322 F.SelectorLookupTableData + Record[0],
3323 F.SelectorLookupTableData,
3324 ASTSelectorLookupTrait(*this, F));
3325 TotalNumMethodPoolEntries += Record[1];
3326 break;
3327
3328 case REFERENCED_SELECTOR_POOL:
3329 if (!Record.empty()) {
3330 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003331 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
Guy Benyei11169dd2012-12-18 14:30:41 +00003332 Record[Idx++]));
3333 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3334 getRawEncoding());
3335 }
3336 }
3337 break;
3338
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00003339 case PP_CONDITIONAL_STACK:
3340 if (!Record.empty()) {
Erik Verbruggen4d1eb2d2017-11-03 09:40:07 +00003341 unsigned Idx = 0, End = Record.size() - 1;
3342 bool ReachedEOFWhileSkipping = Record[Idx++];
3343 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3344 if (ReachedEOFWhileSkipping) {
3345 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3346 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3347 bool FoundNonSkipPortion = Record[Idx++];
3348 bool FoundElse = Record[Idx++];
3349 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3350 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3351 FoundElse, ElseLoc);
3352 }
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00003353 SmallVector<PPConditionalInfo, 4> ConditionalStack;
Erik Verbruggen4d1eb2d2017-11-03 09:40:07 +00003354 while (Idx < End) {
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00003355 auto Loc = ReadSourceLocation(F, Record, Idx);
3356 bool WasSkipping = Record[Idx++];
3357 bool FoundNonSkip = Record[Idx++];
3358 bool FoundElse = Record[Idx++];
3359 ConditionalStack.push_back(
3360 {Loc, WasSkipping, FoundNonSkip, FoundElse});
3361 }
Erik Verbruggen4d1eb2d2017-11-03 09:40:07 +00003362 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00003363 }
3364 break;
3365
Guy Benyei11169dd2012-12-18 14:30:41 +00003366 case PP_COUNTER_VALUE:
3367 if (!Record.empty() && Listener)
3368 Listener->ReadCounter(F, Record[0]);
3369 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003370
Guy Benyei11169dd2012-12-18 14:30:41 +00003371 case FILE_SORTED_DECLS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00003372 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003373 F.NumFileSortedDecls = Record[0];
3374 break;
3375
3376 case SOURCE_LOCATION_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003377 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003378 F.LocalNumSLocEntries = Record[0];
3379 unsigned SLocSpaceSize = Record[1];
Dmitry Polukhin30d59462020-03-21 13:52:51 -07003380 F.SLocEntryOffsetsBase = Record[2];
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00003381 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Ben Langmuir52ca6782014-10-20 16:27:32 +00003382 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
Guy Benyei11169dd2012-12-18 14:30:41 +00003383 SLocSpaceSize);
Richard Smith78d81ec2015-08-12 22:25:24 +00003384 if (!F.SLocEntryBaseID) {
3385 Error("ran out of source locations");
3386 break;
3387 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003388 // Make our entry in the range map. BaseID is negative and growing, so
3389 // we invert it. Because we invert it, though, we need the other end of
3390 // the range.
3391 unsigned RangeStart =
3392 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3393 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3394 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3395
3396 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3397 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3398 GlobalSLocOffsetMap.insert(
3399 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3400 - SLocSpaceSize,&F));
3401
3402 // Initialize the remapping table.
3403 // Invalid stays invalid.
Richard Smithb9eab6d2014-03-20 19:44:17 +00003404 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
Guy Benyei11169dd2012-12-18 14:30:41 +00003405 // This module. Base was 2 when being compiled.
Richard Smithb9eab6d2014-03-20 19:44:17 +00003406 F.SLocRemap.insertOrReplace(std::make_pair(2U,
Guy Benyei11169dd2012-12-18 14:30:41 +00003407 static_cast<int>(F.SLocEntryBaseOffset - 2)));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003408
Guy Benyei11169dd2012-12-18 14:30:41 +00003409 TotalNumSLocEntries += F.LocalNumSLocEntries;
3410 break;
3411 }
3412
Richard Smith37a93df2017-02-18 00:32:02 +00003413 case MODULE_OFFSET_MAP:
3414 F.ModuleOffsetMap = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00003415 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003416
3417 case SOURCE_MANAGER_LINE_TABLE:
Duncan P. N. Exon Smith8e2c1922019-11-10 11:07:20 -08003418 if (ParseLineTable(F, Record)) {
3419 Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003420 return Failure;
Duncan P. N. Exon Smith8e2c1922019-11-10 11:07:20 -08003421 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003422 break;
3423
3424 case SOURCE_LOCATION_PRELOADS: {
3425 // Need to transform from the local view (1-based IDs) to the global view,
3426 // which is based off F.SLocEntryBaseID.
3427 if (!F.PreloadSLocEntries.empty()) {
3428 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003429 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003430 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003431
Guy Benyei11169dd2012-12-18 14:30:41 +00003432 F.PreloadSLocEntries.swap(Record);
3433 break;
3434 }
3435
3436 case EXT_VECTOR_DECLS:
3437 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3438 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3439 break;
3440
3441 case VTABLE_USES:
3442 if (Record.size() % 3 != 0) {
3443 Error("Invalid VTABLE_USES record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003444 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003445 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003446
Guy Benyei11169dd2012-12-18 14:30:41 +00003447 // Later tables overwrite earlier ones.
3448 // FIXME: Modules will have some trouble with this. This is clearly not
3449 // the right way to do this.
3450 VTableUses.clear();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003451
Guy Benyei11169dd2012-12-18 14:30:41 +00003452 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3453 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3454 VTableUses.push_back(
3455 ReadSourceLocation(F, Record, Idx).getRawEncoding());
3456 VTableUses.push_back(Record[Idx++]);
3457 }
3458 break;
3459
Guy Benyei11169dd2012-12-18 14:30:41 +00003460 case PENDING_IMPLICIT_INSTANTIATIONS:
3461 if (PendingInstantiations.size() % 2 != 0) {
3462 Error("Invalid existing PendingInstantiations");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003463 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003464 }
3465
3466 if (Record.size() % 2 != 0) {
3467 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003468 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003469 }
3470
3471 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3472 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3473 PendingInstantiations.push_back(
3474 ReadSourceLocation(F, Record, I).getRawEncoding());
3475 }
3476 break;
3477
3478 case SEMA_DECL_REFS:
Richard Smith96269c52016-09-29 22:49:46 +00003479 if (Record.size() != 3) {
Richard Smith3d8e97e2013-10-18 06:54:39 +00003480 Error("Invalid SEMA_DECL_REFS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003481 return Failure;
Richard Smith3d8e97e2013-10-18 06:54:39 +00003482 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003483 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3484 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3485 break;
3486
3487 case PPD_ENTITIES_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003488 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3489 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3490 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00003491
3492 unsigned LocalBasePreprocessedEntityID = Record[0];
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003493
Guy Benyei11169dd2012-12-18 14:30:41 +00003494 unsigned StartingID;
3495 if (!PP.getPreprocessingRecord())
3496 PP.createPreprocessingRecord();
3497 if (!PP.getPreprocessingRecord()->getExternalSource())
3498 PP.getPreprocessingRecord()->SetExternalSource(*this);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003499 StartingID
Guy Benyei11169dd2012-12-18 14:30:41 +00003500 = PP.getPreprocessingRecord()
Ben Langmuir52ca6782014-10-20 16:27:32 +00003501 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Guy Benyei11169dd2012-12-18 14:30:41 +00003502 F.BasePreprocessedEntityID = StartingID;
3503
3504 if (F.NumPreprocessedEntities > 0) {
3505 // Introduce the global -> local mapping for preprocessed entities in
3506 // this module.
3507 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003508
Guy Benyei11169dd2012-12-18 14:30:41 +00003509 // Introduce the local -> global mapping for preprocessed entities in
3510 // this module.
3511 F.PreprocessedEntityRemap.insertOrReplace(
3512 std::make_pair(LocalBasePreprocessedEntityID,
3513 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3514 }
3515
3516 break;
3517 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003518
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00003519 case PPD_SKIPPED_RANGES: {
3520 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3521 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3522 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3523
3524 if (!PP.getPreprocessingRecord())
3525 PP.createPreprocessingRecord();
3526 if (!PP.getPreprocessingRecord()->getExternalSource())
3527 PP.getPreprocessingRecord()->SetExternalSource(*this);
3528 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3529 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003530
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00003531 if (F.NumPreprocessedSkippedRanges > 0)
3532 GlobalSkippedRangeMap.insert(
3533 std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3534 break;
3535 }
3536
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003537 case DECL_UPDATE_OFFSETS:
Guy Benyei11169dd2012-12-18 14:30:41 +00003538 if (Record.size() % 2 != 0) {
3539 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003540 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003541 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003542 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3543 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3544 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3545
3546 // If we've already loaded the decl, perform the updates when we finish
3547 // loading this block.
3548 if (Decl *D = GetExistingDecl(ID))
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003549 PendingUpdateRecords.push_back(
3550 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
Richard Smithcd45dbc2014-04-19 03:48:30 +00003551 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003552 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003553
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003554 case OBJC_CATEGORIES_MAP:
Guy Benyei11169dd2012-12-18 14:30:41 +00003555 if (F.LocalNumObjCCategoriesInMap != 0) {
3556 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003557 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003558 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003559
Guy Benyei11169dd2012-12-18 14:30:41 +00003560 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003561 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003562 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003563
Guy Benyei11169dd2012-12-18 14:30:41 +00003564 case OBJC_CATEGORIES:
3565 F.ObjCCategories.swap(Record);
3566 break;
Richard Smithc2bb8182015-03-24 06:36:48 +00003567
Guy Benyei11169dd2012-12-18 14:30:41 +00003568 case CUDA_SPECIAL_DECL_REFS:
3569 // Later tables overwrite earlier ones.
3570 // FIXME: Modules will have trouble with this.
3571 CUDASpecialDeclRefs.clear();
3572 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3573 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3574 break;
3575
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003576 case HEADER_SEARCH_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00003577 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003578 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00003579 if (Record[0]) {
3580 F.HeaderFileInfoTable
3581 = HeaderFileInfoLookupTable::Create(
3582 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3583 (const unsigned char *)F.HeaderFileInfoTableData,
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003584 HeaderFileInfoTrait(*this, F,
Guy Benyei11169dd2012-12-18 14:30:41 +00003585 &PP.getHeaderSearchInfo(),
Chris Lattner0e6c9402013-01-20 02:38:54 +00003586 Blob.data() + Record[2]));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003587
Guy Benyei11169dd2012-12-18 14:30:41 +00003588 PP.getHeaderSearchInfo().SetExternalSource(this);
3589 if (!PP.getHeaderSearchInfo().getExternalLookup())
3590 PP.getHeaderSearchInfo().SetExternalLookup(this);
3591 }
3592 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003593
Guy Benyei11169dd2012-12-18 14:30:41 +00003594 case FP_PRAGMA_OPTIONS:
3595 // Later tables overwrite earlier ones.
3596 FPPragmaOptions.swap(Record);
3597 break;
3598
3599 case OPENCL_EXTENSIONS:
Yaxun Liu5b746652016-12-18 05:18:55 +00003600 for (unsigned I = 0, E = Record.size(); I != E; ) {
3601 auto Name = ReadString(Record, I);
3602 auto &Opt = OpenCLExtensions.OptMap[Name];
Yaxun Liucc2741c2016-12-18 06:35:06 +00003603 Opt.Supported = Record[I++] != 0;
3604 Opt.Enabled = Record[I++] != 0;
Yaxun Liu5b746652016-12-18 05:18:55 +00003605 Opt.Avail = Record[I++];
3606 Opt.Core = Record[I++];
3607 }
3608 break;
3609
3610 case OPENCL_EXTENSION_TYPES:
3611 for (unsigned I = 0, E = Record.size(); I != E;) {
3612 auto TypeID = static_cast<::TypeID>(Record[I++]);
3613 auto *Type = GetType(TypeID).getTypePtr();
3614 auto NumExt = static_cast<unsigned>(Record[I++]);
3615 for (unsigned II = 0; II != NumExt; ++II) {
3616 auto Ext = ReadString(Record, I);
3617 OpenCLTypeExtMap[Type].insert(Ext);
3618 }
3619 }
3620 break;
3621
3622 case OPENCL_EXTENSION_DECLS:
3623 for (unsigned I = 0, E = Record.size(); I != E;) {
3624 auto DeclID = static_cast<::DeclID>(Record[I++]);
3625 auto *Decl = GetDecl(DeclID);
3626 auto NumExt = static_cast<unsigned>(Record[I++]);
3627 for (unsigned II = 0; II != NumExt; ++II) {
3628 auto Ext = ReadString(Record, I);
3629 OpenCLDeclExtMap[Decl].insert(Ext);
3630 }
3631 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003632 break;
3633
3634 case TENTATIVE_DEFINITIONS:
3635 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3636 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3637 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003638
Guy Benyei11169dd2012-12-18 14:30:41 +00003639 case KNOWN_NAMESPACES:
3640 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3641 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3642 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003643
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003644 case UNDEFINED_BUT_USED:
3645 if (UndefinedButUsed.size() % 2 != 0) {
3646 Error("Invalid existing UndefinedButUsed");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003647 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003648 }
3649
3650 if (Record.size() % 2 != 0) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003651 Error("invalid undefined-but-used record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003652 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003653 }
3654 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003655 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3656 UndefinedButUsed.push_back(
Nick Lewycky8334af82013-01-26 00:35:08 +00003657 ReadSourceLocation(F, Record, I).getRawEncoding());
3658 }
3659 break;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003660
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00003661 case DELETE_EXPRS_TO_ANALYZE:
3662 for (unsigned I = 0, N = Record.size(); I != N;) {
3663 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3664 const uint64_t Count = Record[I++];
3665 DelayedDeleteExprs.push_back(Count);
3666 for (uint64_t C = 0; C < Count; ++C) {
3667 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3668 bool IsArrayForm = Record[I++] == 1;
3669 DelayedDeleteExprs.push_back(IsArrayForm);
3670 }
3671 }
3672 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003673
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003674 case IMPORTED_MODULES:
Manman Ren11f2a472016-08-18 17:42:15 +00003675 if (!F.isModule()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003676 // If we aren't loading a module (which has its own exports), make
3677 // all of the imported modules visible.
3678 // FIXME: Deal with macros-only imports.
Richard Smith56be7542014-03-21 00:33:59 +00003679 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3680 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3681 SourceLocation Loc = ReadSourceLocation(F, Record, I);
Graydon Hoare9c982442017-01-18 20:36:59 +00003682 if (GlobalID) {
Aaron Ballman4f45b712014-03-21 15:22:56 +00003683 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
Graydon Hoare9c982442017-01-18 20:36:59 +00003684 if (DeserializationListener)
3685 DeserializationListener->ModuleImportRead(GlobalID, Loc);
3686 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003687 }
3688 }
3689 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003690
Guy Benyei11169dd2012-12-18 14:30:41 +00003691 case MACRO_OFFSET: {
3692 if (F.LocalNumMacros != 0) {
3693 Error("duplicate MACRO_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003694 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003695 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003696 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003697 F.LocalNumMacros = Record[0];
3698 unsigned LocalBaseMacroID = Record[1];
Dmitry Polukhin30d59462020-03-21 13:52:51 -07003699 F.MacroOffsetsBase = Record[2];
Guy Benyei11169dd2012-12-18 14:30:41 +00003700 F.BaseMacroID = getTotalNumMacros();
3701
3702 if (F.LocalNumMacros > 0) {
3703 // Introduce the global -> local mapping for macros within this module.
3704 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3705
3706 // Introduce the local -> global mapping for macros within this module.
3707 F.MacroRemap.insertOrReplace(
3708 std::make_pair(LocalBaseMacroID,
3709 F.BaseMacroID - LocalBaseMacroID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00003710
3711 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
Guy Benyei11169dd2012-12-18 14:30:41 +00003712 }
3713 break;
3714 }
3715
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003716 case LATE_PARSED_TEMPLATE:
Richard Smithe40f2ba2013-08-07 21:41:30 +00003717 LateParsedTemplates.append(Record.begin(), Record.end());
3718 break;
Dario Domizioli13a0a382014-05-23 12:13:25 +00003719
3720 case OPTIMIZE_PRAGMA_OPTIONS:
3721 if (Record.size() != 1) {
3722 Error("invalid pragma optimize record");
3723 return Failure;
3724 }
3725 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3726 break;
Nico Weber72889432014-09-06 01:25:55 +00003727
Nico Weber779355f2016-03-02 23:22:00 +00003728 case MSSTRUCT_PRAGMA_OPTIONS:
3729 if (Record.size() != 1) {
3730 Error("invalid pragma ms_struct record");
3731 return Failure;
3732 }
3733 PragmaMSStructState = Record[0];
3734 break;
3735
Nico Weber42932312016-03-03 00:17:35 +00003736 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3737 if (Record.size() != 2) {
3738 Error("invalid pragma ms_struct record");
3739 return Failure;
3740 }
3741 PragmaMSPointersToMembersState = Record[0];
3742 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3743 break;
3744
Nico Weber72889432014-09-06 01:25:55 +00003745 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3746 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3747 UnusedLocalTypedefNameCandidates.push_back(
3748 getGlobalDeclID(F, Record[I]));
3749 break;
Justin Lebar67a78a62016-10-08 22:15:58 +00003750
3751 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3752 if (Record.size() != 1) {
3753 Error("invalid cuda pragma options record");
3754 return Failure;
3755 }
3756 ForceCUDAHostDeviceDepth = Record[0];
3757 break;
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00003758
3759 case PACK_PRAGMA_OPTIONS: {
3760 if (Record.size() < 3) {
3761 Error("invalid pragma pack record");
3762 return Failure;
3763 }
3764 PragmaPackCurrentValue = Record[0];
3765 PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3766 unsigned NumStackEntries = Record[2];
3767 unsigned Idx = 3;
3768 // Reset the stack when importing a new module.
3769 PragmaPackStack.clear();
3770 for (unsigned I = 0; I < NumStackEntries; ++I) {
3771 PragmaPackStackEntry Entry;
3772 Entry.Value = Record[Idx++];
3773 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
Alex Lorenz45b40142017-07-28 14:41:21 +00003774 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00003775 PragmaPackStrings.push_back(ReadString(Record, Idx));
3776 Entry.SlotLabel = PragmaPackStrings.back();
3777 PragmaPackStack.push_back(Entry);
3778 }
3779 break;
3780 }
Yaxun (Sam) Liub670ab72020-02-26 10:57:39 -05003781
3782 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3783 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3784 DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I]));
3785 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003786 }
3787 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003788}
3789
Richard Smith37a93df2017-02-18 00:32:02 +00003790void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3791 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3792
3793 // Additional remapping information.
Vedant Kumar48b4f762018-04-14 01:40:48 +00003794 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
Richard Smith37a93df2017-02-18 00:32:02 +00003795 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3796 F.ModuleOffsetMap = StringRef();
3797
3798 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3799 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3800 F.SLocRemap.insert(std::make_pair(0U, 0));
3801 F.SLocRemap.insert(std::make_pair(2U, 1));
3802 }
3803
3804 // Continuous range maps we may be updating in our module.
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003805 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
Richard Smith37a93df2017-02-18 00:32:02 +00003806 RemapBuilder SLocRemap(F.SLocRemap);
3807 RemapBuilder IdentifierRemap(F.IdentifierRemap);
3808 RemapBuilder MacroRemap(F.MacroRemap);
3809 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3810 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3811 RemapBuilder SelectorRemap(F.SelectorRemap);
3812 RemapBuilder DeclRemap(F.DeclRemap);
3813 RemapBuilder TypeRemap(F.TypeRemap);
3814
3815 while (Data < DataEnd) {
Boris Kolpackovd30446f2017-08-31 06:26:43 +00003816 // FIXME: Looking up dependency modules by filename is horrible. Let's
3817 // start fixing this with prebuilt and explicit modules and see how it
3818 // goes...
Richard Smith37a93df2017-02-18 00:32:02 +00003819 using namespace llvm::support;
Vedant Kumar48b4f762018-04-14 01:40:48 +00003820 ModuleKind Kind = static_cast<ModuleKind>(
3821 endian::readNext<uint8_t, little, unaligned>(Data));
Richard Smith37a93df2017-02-18 00:32:02 +00003822 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3823 StringRef Name = StringRef((const char*)Data, Len);
3824 Data += Len;
Boris Kolpackovd30446f2017-08-31 06:26:43 +00003825 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
3826 ? ModuleMgr.lookupByModuleName(Name)
3827 : ModuleMgr.lookupByFileName(Name));
Richard Smith37a93df2017-02-18 00:32:02 +00003828 if (!OM) {
3829 std::string Msg =
3830 "SourceLocation remap refers to unknown module, cannot find ";
Benjamin Krameradcd0262020-01-28 20:23:46 +01003831 Msg.append(std::string(Name));
Richard Smith37a93df2017-02-18 00:32:02 +00003832 Error(Msg);
3833 return;
3834 }
3835
3836 uint32_t SLocOffset =
3837 endian::readNext<uint32_t, little, unaligned>(Data);
3838 uint32_t IdentifierIDOffset =
3839 endian::readNext<uint32_t, little, unaligned>(Data);
3840 uint32_t MacroIDOffset =
3841 endian::readNext<uint32_t, little, unaligned>(Data);
3842 uint32_t PreprocessedEntityIDOffset =
3843 endian::readNext<uint32_t, little, unaligned>(Data);
3844 uint32_t SubmoduleIDOffset =
3845 endian::readNext<uint32_t, little, unaligned>(Data);
3846 uint32_t SelectorIDOffset =
3847 endian::readNext<uint32_t, little, unaligned>(Data);
3848 uint32_t DeclIDOffset =
3849 endian::readNext<uint32_t, little, unaligned>(Data);
3850 uint32_t TypeIndexOffset =
3851 endian::readNext<uint32_t, little, unaligned>(Data);
3852
3853 uint32_t None = std::numeric_limits<uint32_t>::max();
3854
3855 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3856 RemapBuilder &Remap) {
3857 if (Offset != None)
3858 Remap.insert(std::make_pair(Offset,
3859 static_cast<int>(BaseOffset - Offset)));
3860 };
3861 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3862 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3863 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3864 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3865 PreprocessedEntityRemap);
3866 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3867 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3868 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3869 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3870
3871 // Global -> local mappings.
3872 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3873 }
3874}
3875
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003876ASTReader::ASTReadResult
3877ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3878 const ModuleFile *ImportedBy,
3879 unsigned ClientLoadCapabilities) {
3880 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00003881 F.ModuleMapPath = ReadPath(F, Record, Idx);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003882
3883 // Try to resolve ModuleName in the current header search context and
3884 // verify that it is found in the same module map file as we saved. If the
3885 // top-level AST file is a main file, skip this check because there is no
3886 // usable header search context.
3887 assert(!F.ModuleName.empty() &&
Richard Smithe842a472014-10-22 02:05:46 +00003888 "MODULE_NAME should come before MODULE_MAP_FILE");
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00003889 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
Richard Smithe842a472014-10-22 02:05:46 +00003890 // An implicitly-loaded module file should have its module listed in some
3891 // module map file that we've already loaded.
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003892 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
Richard Smithe842a472014-10-22 02:05:46 +00003893 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3894 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
Yuka Takahashid8baec22018-08-01 09:50:02 +00003895 // Don't emit module relocation error if we have -fno-validate-pch
3896 if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
Richard Smith0f99d6a2015-08-09 08:48:41 +00003897 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
Bruno Cardoso Lopesa66a3252017-11-17 03:24:11 +00003898 if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
Richard Smith0f99d6a2015-08-09 08:48:41 +00003899 // This module was defined by an imported (explicit) module.
3900 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3901 << ASTFE->getName();
Bruno Cardoso Lopesa66a3252017-11-17 03:24:11 +00003902 } else {
Richard Smith0f99d6a2015-08-09 08:48:41 +00003903 // This module was built with a different module map.
3904 Diag(diag::err_imported_module_not_found)
Bruno Cardoso Lopes4625c182019-08-29 23:14:08 +00003905 << F.ModuleName << F.FileName
3906 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3907 << !ImportedBy;
Bruno Cardoso Lopesa66a3252017-11-17 03:24:11 +00003908 // In case it was imported by a PCH, there's a chance the user is
3909 // just missing to include the search path to the directory containing
3910 // the modulemap.
Bruno Cardoso Lopes4625c182019-08-29 23:14:08 +00003911 if (ImportedBy && ImportedBy->Kind == MK_PCH)
Bruno Cardoso Lopesa66a3252017-11-17 03:24:11 +00003912 Diag(diag::note_imported_by_pch_module_not_found)
3913 << llvm::sys::path::parent_path(F.ModuleMapPath);
3914 }
Richard Smith0f99d6a2015-08-09 08:48:41 +00003915 }
3916 return OutOfDate;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003917 }
3918
Richard Smithe842a472014-10-22 02:05:46 +00003919 assert(M->Name == F.ModuleName && "found module with different name");
3920
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003921 // Check the primary module map file.
Harlan Haskins8d323d12019-08-01 21:31:56 +00003922 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3923 if (!StoredModMap || *StoredModMap != ModMap) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003924 assert(ModMap && "found module is missing module map file");
Bruno Cardoso Lopes4625c182019-08-29 23:14:08 +00003925 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3926 "top-level import should be verified");
3927 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003928 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3929 Diag(diag::err_imported_module_modmap_changed)
Bruno Cardoso Lopes4625c182019-08-29 23:14:08 +00003930 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3931 << ModMap->getName() << F.ModuleMapPath << NotImported;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003932 return OutOfDate;
3933 }
3934
3935 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3936 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3937 // FIXME: we should use input files rather than storing names.
Richard Smith7ed1bc92014-12-05 22:42:13 +00003938 std::string Filename = ReadPath(F, Record, Idx);
Harlan Haskins8d323d12019-08-01 21:31:56 +00003939 auto F = FileMgr.getFile(Filename, false, false);
3940 if (!F) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003941 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3942 Error("could not find file '" + Filename +"' referenced by AST file");
3943 return OutOfDate;
3944 }
Harlan Haskins8d323d12019-08-01 21:31:56 +00003945 AdditionalStoredMaps.insert(*F);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003946 }
3947
3948 // Check any additional module map files (e.g. module.private.modulemap)
3949 // that are not in the pcm.
3950 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00003951 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003952 // Remove files that match
3953 // Note: SmallPtrSet::erase is really remove
3954 if (!AdditionalStoredMaps.erase(ModMap)) {
3955 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3956 Diag(diag::err_module_different_modmap)
3957 << F.ModuleName << /*new*/0 << ModMap->getName();
3958 return OutOfDate;
3959 }
3960 }
3961 }
3962
3963 // Check any additional module map files that are in the pcm, but not
3964 // found in header search. Cases that match are already removed.
Vedant Kumar48b4f762018-04-14 01:40:48 +00003965 for (const FileEntry *ModMap : AdditionalStoredMaps) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003966 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3967 Diag(diag::err_module_different_modmap)
3968 << F.ModuleName << /*not new*/1 << ModMap->getName();
3969 return OutOfDate;
3970 }
3971 }
3972
3973 if (Listener)
3974 Listener->ReadModuleMapFile(F.ModuleMapPath);
3975 return Success;
3976}
3977
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003978/// Move the given method to the back of the global list of methods.
Douglas Gregorc1489562013-02-12 23:36:21 +00003979static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3980 // Find the entry for this selector in the method pool.
3981 Sema::GlobalMethodPool::iterator Known
3982 = S.MethodPool.find(Method->getSelector());
3983 if (Known == S.MethodPool.end())
3984 return;
3985
3986 // Retrieve the appropriate method list.
3987 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3988 : Known->second.second;
3989 bool Found = false;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003990 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003991 if (!Found) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003992 if (List->getMethod() == Method) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003993 Found = true;
3994 } else {
3995 // Keep searching.
3996 continue;
3997 }
3998 }
3999
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00004000 if (List->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00004001 List->setMethod(List->getNext()->getMethod());
Douglas Gregorc1489562013-02-12 23:36:21 +00004002 else
Nico Weber2e0c8f72014-12-27 03:58:08 +00004003 List->setMethod(Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00004004 }
4005}
4006
Richard Smithde711422015-04-23 21:20:19 +00004007void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Richard Smith10434f32015-05-02 02:08:26 +00004008 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
Vedant Kumar48b4f762018-04-14 01:40:48 +00004009 for (Decl *D : Names) {
Richard Smith90dc5252017-06-23 01:04:34 +00004010 bool wasHidden = D->isHidden();
4011 D->setVisibleDespiteOwningModule();
Guy Benyei11169dd2012-12-18 14:30:41 +00004012
Vedant Kumar48b4f762018-04-14 01:40:48 +00004013 if (wasHidden && SemaObj) {
4014 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
Richard Smith49f906a2014-03-01 00:08:04 +00004015 moveMethodToBackOfGlobalList(*SemaObj, Method);
Vedant Kumar48b4f762018-04-14 01:40:48 +00004016 }
4017 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004018 }
4019}
4020
Richard Smith49f906a2014-03-01 00:08:04 +00004021void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00004022 Module::NameVisibilityKind NameVisibility,
Richard Smitha7e2cc62015-05-01 01:53:09 +00004023 SourceLocation ImportLoc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004024 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004025 SmallVector<Module *, 4> Stack;
Robert Wilhelm25284cc2013-08-23 16:11:15 +00004026 Stack.push_back(Mod);
Guy Benyei11169dd2012-12-18 14:30:41 +00004027 while (!Stack.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00004028 Mod = Stack.pop_back_val();
Guy Benyei11169dd2012-12-18 14:30:41 +00004029
4030 if (NameVisibility <= Mod->NameVisibility) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00004031 // This module already has this level of visibility (or greater), so
Guy Benyei11169dd2012-12-18 14:30:41 +00004032 // there is nothing more to do.
4033 continue;
4034 }
Richard Smith49f906a2014-03-01 00:08:04 +00004035
Guy Benyei11169dd2012-12-18 14:30:41 +00004036 if (!Mod->isAvailable()) {
4037 // Modules that aren't available cannot be made visible.
4038 continue;
4039 }
4040
4041 // Update the module's name visibility.
4042 Mod->NameVisibility = NameVisibility;
Richard Smith49f906a2014-03-01 00:08:04 +00004043
Guy Benyei11169dd2012-12-18 14:30:41 +00004044 // If we've already deserialized any names from this module,
4045 // mark them as visible.
4046 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4047 if (Hidden != HiddenNamesMap.end()) {
Richard Smith57721ac2014-07-21 04:10:40 +00004048 auto HiddenNames = std::move(*Hidden);
Guy Benyei11169dd2012-12-18 14:30:41 +00004049 HiddenNamesMap.erase(Hidden);
Richard Smithde711422015-04-23 21:20:19 +00004050 makeNamesVisible(HiddenNames.second, HiddenNames.first);
Richard Smith57721ac2014-07-21 04:10:40 +00004051 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4052 "making names visible added hidden names");
Guy Benyei11169dd2012-12-18 14:30:41 +00004053 }
Dmitri Gribenkoe9bcf5b2013-11-04 21:51:33 +00004054
Guy Benyei11169dd2012-12-18 14:30:41 +00004055 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00004056 SmallVector<Module *, 16> Exports;
4057 Mod->getExportedModules(Exports);
Vedant Kumar48b4f762018-04-14 01:40:48 +00004058 for (SmallVectorImpl<Module *>::iterator
4059 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4060 Module *Exported = *I;
David Blaikie82e95a32014-11-19 07:49:47 +00004061 if (Visited.insert(Exported).second)
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00004062 Stack.push_back(Exported);
Vedant Kumar48b4f762018-04-14 01:40:48 +00004063 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004064 }
4065}
4066
Richard Smith6561f922016-09-12 21:06:40 +00004067/// We've merged the definition \p MergedDef into the existing definition
4068/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4069/// visible.
4070void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4071 NamedDecl *MergedDef) {
Richard Smith6561f922016-09-12 21:06:40 +00004072 if (Def->isHidden()) {
4073 // If MergedDef is visible or becomes visible, make the definition visible.
Benjamin Kramera72a70a2016-10-17 13:00:44 +00004074 if (!MergedDef->isHidden())
Richard Smith90dc5252017-06-23 01:04:34 +00004075 Def->setVisibleDespiteOwningModule();
Richard Smith13897eb2018-09-12 23:37:00 +00004076 else {
Benjamin Kramera72a70a2016-10-17 13:00:44 +00004077 getContext().mergeDefinitionIntoModule(
4078 Def, MergedDef->getImportedOwningModule(),
4079 /*NotifyListeners*/ false);
4080 PendingMergedDefinitionsToDeduplicate.insert(Def);
Benjamin Kramera72a70a2016-10-17 13:00:44 +00004081 }
Richard Smith6561f922016-09-12 21:06:40 +00004082 }
4083}
4084
Douglas Gregore060e572013-01-25 01:03:03 +00004085bool ASTReader::loadGlobalIndex() {
4086 if (GlobalIndex)
4087 return false;
4088
4089 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
Richard Smithdbafb6c2017-06-29 23:23:46 +00004090 !PP.getLangOpts().Modules)
Douglas Gregore060e572013-01-25 01:03:03 +00004091 return true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004092
Douglas Gregore060e572013-01-25 01:03:03 +00004093 // Try to load the global index.
4094 TriedLoadingGlobalIndex = true;
4095 StringRef ModuleCachePath
4096 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
JF Bastien0e828952019-06-26 19:50:12 +00004097 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4098 GlobalModuleIndex::readIndex(ModuleCachePath);
4099 if (llvm::Error Err = std::move(Result.second)) {
4100 assert(!Result.first);
4101 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
Douglas Gregore060e572013-01-25 01:03:03 +00004102 return true;
JF Bastien0e828952019-06-26 19:50:12 +00004103 }
Douglas Gregore060e572013-01-25 01:03:03 +00004104
4105 GlobalIndex.reset(Result.first);
Douglas Gregor7211ac12013-01-25 23:32:03 +00004106 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregore060e572013-01-25 01:03:03 +00004107 return false;
4108}
4109
4110bool ASTReader::isGlobalIndexUnavailable() const {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004111 return PP.getLangOpts().Modules && UseGlobalIndex &&
Douglas Gregore060e572013-01-25 01:03:03 +00004112 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4113}
4114
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004115static void updateModuleTimestamp(ModuleFile &MF) {
4116 // Overwrite the timestamp file contents so that file's mtime changes.
4117 std::string TimestampFilename = MF.getTimestampFilename();
Rafael Espindoladae941a2014-08-25 18:17:04 +00004118 std::error_code EC;
Fangrui Songd9b948b2019-08-05 05:43:48 +00004119 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
Rafael Espindoladae941a2014-08-25 18:17:04 +00004120 if (EC)
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004121 return;
4122 OS << "Timestamp file\n";
Alex Lorenz0bafa022017-06-02 10:36:56 +00004123 OS.close();
4124 OS.clear_error(); // Avoid triggering a fatal error.
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004125}
4126
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004127/// Given a cursor at the start of an AST file, scan ahead and drop the
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004128/// cursor into the start of the given block ID, returning false on success and
4129/// true on failure.
4130static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00004131 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00004132 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4133 if (!MaybeEntry) {
4134 // FIXME this drops errors on the floor.
4135 consumeError(MaybeEntry.takeError());
4136 return true;
4137 }
4138 llvm::BitstreamEntry Entry = MaybeEntry.get();
4139
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004140 switch (Entry.Kind) {
4141 case llvm::BitstreamEntry::Error:
4142 case llvm::BitstreamEntry::EndBlock:
4143 return true;
4144
4145 case llvm::BitstreamEntry::Record:
4146 // Ignore top-level records.
JF Bastien0e828952019-06-26 19:50:12 +00004147 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4148 break;
4149 else {
4150 // FIXME this drops errors on the floor.
4151 consumeError(Skipped.takeError());
4152 return true;
4153 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004154
4155 case llvm::BitstreamEntry::SubBlock:
4156 if (Entry.ID == BlockID) {
JF Bastien0e828952019-06-26 19:50:12 +00004157 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4158 // FIXME this drops the error on the floor.
4159 consumeError(std::move(Err));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004160 return true;
JF Bastien0e828952019-06-26 19:50:12 +00004161 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004162 // Found it!
4163 return false;
4164 }
4165
JF Bastien0e828952019-06-26 19:50:12 +00004166 if (llvm::Error Err = Cursor.SkipBlock()) {
4167 // FIXME this drops the error on the floor.
4168 consumeError(std::move(Err));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004169 return true;
JF Bastien0e828952019-06-26 19:50:12 +00004170 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004171 }
4172 }
4173}
4174
Benjamin Kramer0772c422016-02-13 13:42:54 +00004175ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
Guy Benyei11169dd2012-12-18 14:30:41 +00004176 ModuleKind Type,
4177 SourceLocation ImportLoc,
Graydon Hoaree7196af2016-12-09 21:45:49 +00004178 unsigned ClientLoadCapabilities,
4179 SmallVectorImpl<ImportedSubmodule> *Imported) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00004180 llvm::SaveAndRestore<SourceLocation>
4181 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4182
Richard Smithd1c46742014-04-30 02:24:17 +00004183 // Defer any pending actions until we get to the end of reading the AST file.
4184 Deserializing AnASTFile(this);
4185
Guy Benyei11169dd2012-12-18 14:30:41 +00004186 // Bump the generation number.
Richard Smithdbafb6c2017-06-29 23:23:46 +00004187 unsigned PreviousGeneration = 0;
4188 if (ContextObj)
4189 PreviousGeneration = incrementGeneration(*ContextObj);
Guy Benyei11169dd2012-12-18 14:30:41 +00004190
4191 unsigned NumModules = ModuleMgr.size();
Duncan P. N. Exon Smithc46b3a22019-11-10 10:42:29 -08004192 auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4193 assert(ReadResult && "expected to return error");
Duncan P. N. Exon Smith8e9e4332019-11-10 10:31:03 -08004194 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
Richard Smithdbafb6c2017-06-29 23:23:46 +00004195 PP.getLangOpts().Modules
Duncan P. N. Exon Smith8e6bc1972017-01-28 23:02:12 +00004196 ? &PP.getHeaderSearchInfo().getModuleMap()
4197 : nullptr);
Douglas Gregore060e572013-01-25 01:03:03 +00004198
4199 // If we find that any modules are unusable, the global index is going
4200 // to be out-of-date. Just remove it.
4201 GlobalIndex.reset();
Craig Toppera13603a2014-05-22 05:54:18 +00004202 ModuleMgr.setGlobalIndex(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00004203 return ReadResult;
Duncan P. N. Exon Smithc46b3a22019-11-10 10:42:29 -08004204 };
4205
4206 SmallVector<ImportedModule, 4> Loaded;
4207 switch (ASTReadResult ReadResult =
4208 ReadASTCore(FileName, Type, ImportLoc,
4209 /*ImportedBy=*/nullptr, Loaded, 0, 0,
4210 ASTFileSignature(), ClientLoadCapabilities)) {
4211 case Failure:
4212 case Missing:
4213 case OutOfDate:
4214 case VersionMismatch:
4215 case ConfigurationMismatch:
4216 case HadErrors:
4217 return removeModulesAndReturn(ReadResult);
Guy Benyei11169dd2012-12-18 14:30:41 +00004218 case Success:
4219 break;
4220 }
4221
4222 // Here comes stuff that we only do once the entire chain is loaded.
4223
Duncan P. N. Exon Smith01782c32019-11-10 10:50:12 -08004224 // Load the AST blocks of all of the modules that we loaded. We can still
4225 // hit errors parsing the ASTs at this point.
Duncan P. N. Exon Smithbfd58fc2019-11-11 15:42:25 -08004226 for (ImportedModule &M : Loaded) {
4227 ModuleFile &F = *M.Mod;
Guy Benyei11169dd2012-12-18 14:30:41 +00004228
4229 // Read the AST block.
Ben Langmuir2c9af442014-04-10 17:57:43 +00004230 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
Duncan P. N. Exon Smithc46b3a22019-11-10 10:42:29 -08004231 return removeModulesAndReturn(Result);
Guy Benyei11169dd2012-12-18 14:30:41 +00004232
Duncan P. N. Exon Smith83dcb342019-11-10 13:14:52 -08004233 // The AST block should always have a definition for the main module.
4234 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4235 Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4236 return removeModulesAndReturn(Failure);
4237 }
4238
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004239 // Read the extension blocks.
4240 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4241 if (ASTReadResult Result = ReadExtensionBlock(F))
Duncan P. N. Exon Smithc46b3a22019-11-10 10:42:29 -08004242 return removeModulesAndReturn(Result);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004243 }
4244
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004245 // Once read, set the ModuleFile bit base offset and update the size in
Guy Benyei11169dd2012-12-18 14:30:41 +00004246 // bits of all files we've seen.
4247 F.GlobalBitOffset = TotalModulesSizeInBits;
4248 TotalModulesSizeInBits += F.SizeInBits;
4249 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Duncan P. N. Exon Smith01782c32019-11-10 10:50:12 -08004250 }
4251
4252 // Preload source locations and interesting indentifiers.
4253 for (ImportedModule &M : Loaded) {
4254 ModuleFile &F = *M.Mod;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004255
Guy Benyei11169dd2012-12-18 14:30:41 +00004256 // Preload SLocEntries.
Vedant Kumar48b4f762018-04-14 01:40:48 +00004257 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4258 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Guy Benyei11169dd2012-12-18 14:30:41 +00004259 // Load it through the SourceManager and don't call ReadSLocEntry()
4260 // directly because the entry may have already been loaded in which case
4261 // calling ReadSLocEntry() directly would trigger an assertion in
4262 // SourceManager.
4263 SourceMgr.getLoadedSLocEntryByID(Index);
4264 }
Richard Smith33e0f7e2015-07-22 02:08:40 +00004265
Richard Smithea741482017-05-01 22:10:47 +00004266 // Map the original source file ID into the ID space of the current
4267 // compilation.
4268 if (F.OriginalSourceFileID.isValid()) {
4269 F.OriginalSourceFileID = FileID::get(
4270 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4271 }
4272
Richard Smith33e0f7e2015-07-22 02:08:40 +00004273 // Preload all the pending interesting identifiers by marking them out of
4274 // date.
4275 for (auto Offset : F.PreloadIdentifierOffsets) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004276 const unsigned char *Data = reinterpret_cast<const unsigned char *>(
Richard Smith33e0f7e2015-07-22 02:08:40 +00004277 F.IdentifierTableData + Offset);
4278
4279 ASTIdentifierLookupTrait Trait(*this, F);
4280 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4281 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
Richard Smith79bf9202015-08-24 03:33:22 +00004282 auto &II = PP.getIdentifierTable().getOwn(Key);
4283 II.setOutOfDate(true);
4284
4285 // Mark this identifier as being from an AST file so that we can track
4286 // whether we need to serialize it.
Richard Smitheb4b58f62016-02-05 01:40:54 +00004287 markIdentifierFromAST(*this, II);
Richard Smith79bf9202015-08-24 03:33:22 +00004288
4289 // Associate the ID with the identifier so that the writer can reuse it.
4290 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4291 SetIdentifierInfo(ID, &II);
Richard Smith33e0f7e2015-07-22 02:08:40 +00004292 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004293 }
4294
Douglas Gregor603cd862013-03-22 18:50:14 +00004295 // Setup the import locations and notify the module manager that we've
4296 // committed to these module files.
Duncan P. N. Exon Smithbfd58fc2019-11-11 15:42:25 -08004297 for (ImportedModule &M : Loaded) {
4298 ModuleFile &F = *M.Mod;
Douglas Gregor603cd862013-03-22 18:50:14 +00004299
4300 ModuleMgr.moduleFileAccepted(&F);
4301
4302 // Set the import location.
Argyrios Kyrtzidis71c1af82013-02-01 16:36:14 +00004303 F.DirectImportLoc = ImportLoc;
Richard Smithb22a1d12016-03-27 20:13:24 +00004304 // FIXME: We assume that locations from PCH / preamble do not need
4305 // any translation.
Duncan P. N. Exon Smithbfd58fc2019-11-11 15:42:25 -08004306 if (!M.ImportedBy)
4307 F.ImportLoc = M.ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00004308 else
Duncan P. N. Exon Smithbfd58fc2019-11-11 15:42:25 -08004309 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00004310 }
4311
Richard Smithdbafb6c2017-06-29 23:23:46 +00004312 if (!PP.getLangOpts().CPlusPlus ||
Manman Ren11f2a472016-08-18 17:42:15 +00004313 (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4314 Type != MK_PrebuiltModule)) {
Richard Smith33e0f7e2015-07-22 02:08:40 +00004315 // Mark all of the identifiers in the identifier table as being out of date,
4316 // so that various accessors know to check the loaded modules when the
4317 // identifier is used.
4318 //
4319 // For C++ modules, we don't need information on many identifiers (just
4320 // those that provide macros or are poisoned), so we mark all of
4321 // the interesting ones via PreloadIdentifierOffsets.
Vedant Kumar48b4f762018-04-14 01:40:48 +00004322 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4323 IdEnd = PP.getIdentifierTable().end();
4324 Id != IdEnd; ++Id)
4325 Id->second->setOutOfDate(true);
Richard Smith33e0f7e2015-07-22 02:08:40 +00004326 }
Manman Rena0f31a02016-04-29 19:04:05 +00004327 // Mark selectors as out of date.
4328 for (auto Sel : SelectorGeneration)
4329 SelectorOutOfDate[Sel.first] = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004330
Guy Benyei11169dd2012-12-18 14:30:41 +00004331 // Resolve any unresolved module exports.
Vedant Kumar48b4f762018-04-14 01:40:48 +00004332 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4333 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei11169dd2012-12-18 14:30:41 +00004334 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4335 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregorfb912652013-03-20 21:10:35 +00004336
4337 switch (Unresolved.Kind) {
4338 case UnresolvedModuleRef::Conflict:
4339 if (ResolvedMod) {
4340 Module::Conflict Conflict;
4341 Conflict.Other = ResolvedMod;
4342 Conflict.Message = Unresolved.String.str();
4343 Unresolved.Mod->Conflicts.push_back(Conflict);
4344 }
4345 continue;
4346
4347 case UnresolvedModuleRef::Import:
Guy Benyei11169dd2012-12-18 14:30:41 +00004348 if (ResolvedMod)
Richard Smith38477db2015-05-02 00:45:56 +00004349 Unresolved.Mod->Imports.insert(ResolvedMod);
Guy Benyei11169dd2012-12-18 14:30:41 +00004350 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00004351
Douglas Gregorfb912652013-03-20 21:10:35 +00004352 case UnresolvedModuleRef::Export:
4353 if (ResolvedMod || Unresolved.IsWildcard)
4354 Unresolved.Mod->Exports.push_back(
4355 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4356 continue;
4357 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004358 }
Douglas Gregorfb912652013-03-20 21:10:35 +00004359 UnresolvedModuleRefs.clear();
Daniel Jasperba7f2f72013-09-24 09:14:14 +00004360
Graydon Hoaree7196af2016-12-09 21:45:49 +00004361 if (Imported)
4362 Imported->append(ImportedModules.begin(),
4363 ImportedModules.end());
4364
Daniel Jasperba7f2f72013-09-24 09:14:14 +00004365 // FIXME: How do we load the 'use'd modules? They may not be submodules.
4366 // Might be unnecessary as use declarations are only used to build the
4367 // module itself.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004368
Richard Smithdbafb6c2017-06-29 23:23:46 +00004369 if (ContextObj)
4370 InitializeContext();
Guy Benyei11169dd2012-12-18 14:30:41 +00004371
Richard Smith3d8e97e2013-10-18 06:54:39 +00004372 if (SemaObj)
4373 UpdateSema();
4374
Guy Benyei11169dd2012-12-18 14:30:41 +00004375 if (DeserializationListener)
4376 DeserializationListener->ReaderInitialized(this);
4377
4378 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
Yaron Keren8b563662015-10-03 10:46:20 +00004379 if (PrimaryModule.OriginalSourceFileID.isValid()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004380 // If this AST file is a precompiled preamble, then set the
4381 // preamble file ID of the source manager to the file source file
4382 // from which the preamble was built.
4383 if (Type == MK_Preamble) {
4384 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4385 } else if (Type == MK_MainFile) {
4386 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4387 }
4388 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004389
Guy Benyei11169dd2012-12-18 14:30:41 +00004390 // For any Objective-C class definitions we have already loaded, make sure
4391 // that we load any additional categories.
Vedant Kumar48b4f762018-04-14 01:40:48 +00004392 if (ContextObj) {
4393 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4394 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4395 ObjCClassesLoaded[I],
Richard Smithdbafb6c2017-06-29 23:23:46 +00004396 PreviousGeneration);
Vedant Kumar48b4f762018-04-14 01:40:48 +00004397 }
4398 }
Douglas Gregore060e572013-01-25 01:03:03 +00004399
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004400 if (PP.getHeaderSearchInfo()
4401 .getHeaderSearchOpts()
4402 .ModulesValidateOncePerBuildSession) {
4403 // Now we are certain that the module and all modules it depends on are
4404 // up to date. Create or update timestamp files for modules that are
4405 // located in the module cache (not for PCH files that could be anywhere
4406 // in the filesystem).
Vedant Kumar48b4f762018-04-14 01:40:48 +00004407 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4408 ImportedModule &M = Loaded[I];
4409 if (M.Mod->Kind == MK_ImplicitModule) {
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004410 updateModuleTimestamp(*M.Mod);
Vedant Kumar48b4f762018-04-14 01:40:48 +00004411 }
4412 }
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004413 }
4414
Guy Benyei11169dd2012-12-18 14:30:41 +00004415 return Success;
4416}
4417
Peter Collingbourne77c89b62016-11-08 04:17:11 +00004418static ASTFileSignature readASTFileSignature(StringRef PCH);
Ben Langmuir487ea142014-10-23 18:05:36 +00004419
JF Bastien0e828952019-06-26 19:50:12 +00004420/// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4421static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4422 // FIXME checking magic headers is done in other places such as
4423 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4424 // always done the same. Unify it all with a helper.
4425 if (!Stream.canSkipToPos(4))
4426 return llvm::createStringError(std::errc::illegal_byte_sequence,
4427 "file too small to contain AST file magic");
4428 for (unsigned C : {'C', 'P', 'C', 'H'})
4429 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4430 if (Res.get() != C)
4431 return llvm::createStringError(
4432 std::errc::illegal_byte_sequence,
4433 "file doesn't start with AST file magic");
4434 } else
4435 return Res.takeError();
4436 return llvm::Error::success();
Ben Langmuir70a1b812015-03-24 04:43:52 +00004437}
4438
Richard Smith0f99d6a2015-08-09 08:48:41 +00004439static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4440 switch (Kind) {
4441 case MK_PCH:
4442 return 0; // PCH
4443 case MK_ImplicitModule:
4444 case MK_ExplicitModule:
Manman Ren11f2a472016-08-18 17:42:15 +00004445 case MK_PrebuiltModule:
Richard Smith0f99d6a2015-08-09 08:48:41 +00004446 return 1; // module
4447 case MK_MainFile:
4448 case MK_Preamble:
4449 return 2; // main source file
4450 }
4451 llvm_unreachable("unknown module kind");
4452}
4453
Guy Benyei11169dd2012-12-18 14:30:41 +00004454ASTReader::ASTReadResult
4455ASTReader::ReadASTCore(StringRef FileName,
4456 ModuleKind Type,
4457 SourceLocation ImportLoc,
4458 ModuleFile *ImportedBy,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004459 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00004460 off_t ExpectedSize, time_t ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00004461 ASTFileSignature ExpectedSignature,
Guy Benyei11169dd2012-12-18 14:30:41 +00004462 unsigned ClientLoadCapabilities) {
4463 ModuleFile *M;
Guy Benyei11169dd2012-12-18 14:30:41 +00004464 std::string ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00004465 ModuleManager::AddModuleResult AddResult
4466 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
Richard Smith053f6c62014-05-16 23:01:30 +00004467 getGeneration(), ExpectedSize, ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00004468 ExpectedSignature, readASTFileSignature,
Douglas Gregor7029ce12013-03-19 00:28:20 +00004469 M, ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00004470
Douglas Gregor7029ce12013-03-19 00:28:20 +00004471 switch (AddResult) {
4472 case ModuleManager::AlreadyLoaded:
Duncan P. N. Exon Smith9dda8f52019-03-06 02:50:46 +00004473 Diag(diag::remark_module_import)
4474 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4475 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
Douglas Gregor7029ce12013-03-19 00:28:20 +00004476 return Success;
4477
4478 case ModuleManager::NewlyLoaded:
4479 // Load module file below.
4480 break;
4481
4482 case ModuleManager::Missing:
Richard Smithe842a472014-10-22 02:05:46 +00004483 // The module file was missing; if the client can handle that, return
Douglas Gregor7029ce12013-03-19 00:28:20 +00004484 // it.
4485 if (ClientLoadCapabilities & ARR_Missing)
4486 return Missing;
4487
4488 // Otherwise, return an error.
Richard Smith0f99d6a2015-08-09 08:48:41 +00004489 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
Adrian Prantlb3b5a732016-08-29 20:46:59 +00004490 << FileName << !ErrorStr.empty()
Richard Smith0f99d6a2015-08-09 08:48:41 +00004491 << ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00004492 return Failure;
4493
4494 case ModuleManager::OutOfDate:
4495 // We couldn't load the module file because it is out-of-date. If the
4496 // client can handle out-of-date, return it.
4497 if (ClientLoadCapabilities & ARR_OutOfDate)
4498 return OutOfDate;
4499
4500 // Otherwise, return an error.
Richard Smith0f99d6a2015-08-09 08:48:41 +00004501 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
Adrian Prantl9a06a882016-08-29 20:46:56 +00004502 << FileName << !ErrorStr.empty()
Richard Smith0f99d6a2015-08-09 08:48:41 +00004503 << ErrorStr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004504 return Failure;
4505 }
4506
Douglas Gregor7029ce12013-03-19 00:28:20 +00004507 assert(M && "Missing module file");
Guy Benyei11169dd2012-12-18 14:30:41 +00004508
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00004509 bool ShouldFinalizePCM = false;
4510 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4511 auto &MC = getModuleManager().getModuleCache();
4512 if (ShouldFinalizePCM)
4513 MC.finalizePCM(FileName);
4514 else
Rumeet Dhindsa57a2eaf2020-03-10 10:59:26 -07004515 MC.tryToDropPCM(FileName);
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00004516 });
Guy Benyei11169dd2012-12-18 14:30:41 +00004517 ModuleFile &F = *M;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004518 BitstreamCursor &Stream = F.Stream;
Peter Collingbourne77c89b62016-11-08 04:17:11 +00004519 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
Adrian Prantlcbc368c2015-02-25 02:44:04 +00004520 F.SizeInBits = F.Buffer->getBufferSize() * 8;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004521
Guy Benyei11169dd2012-12-18 14:30:41 +00004522 // Sniff for the signature.
JF Bastien0e828952019-06-26 19:50:12 +00004523 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4524 Diag(diag::err_module_file_invalid)
4525 << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
Guy Benyei11169dd2012-12-18 14:30:41 +00004526 return Failure;
4527 }
4528
4529 // This is used for compatibility with older PCH formats.
4530 bool HaveReadControlBlock = false;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00004531 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00004532 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4533 if (!MaybeEntry) {
4534 Error(MaybeEntry.takeError());
4535 return Failure;
4536 }
4537 llvm::BitstreamEntry Entry = MaybeEntry.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004538
Chris Lattnerefa77172013-01-20 00:00:22 +00004539 switch (Entry.Kind) {
4540 case llvm::BitstreamEntry::Error:
Chris Lattnerefa77172013-01-20 00:00:22 +00004541 case llvm::BitstreamEntry::Record:
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004542 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00004543 Error("invalid record at top-level of AST file");
4544 return Failure;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004545
Chris Lattnerefa77172013-01-20 00:00:22 +00004546 case llvm::BitstreamEntry::SubBlock:
4547 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004548 }
4549
Chris Lattnerefa77172013-01-20 00:00:22 +00004550 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004551 case CONTROL_BLOCK_ID:
4552 HaveReadControlBlock = true;
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004553 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004554 case Success:
Richard Smith0f99d6a2015-08-09 08:48:41 +00004555 // Check that we didn't try to load a non-module AST file as a module.
4556 //
4557 // FIXME: Should we also perform the converse check? Loading a module as
4558 // a PCH file sort of works, but it's a bit wonky.
Manman Ren11f2a472016-08-18 17:42:15 +00004559 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4560 Type == MK_PrebuiltModule) &&
Richard Smith0f99d6a2015-08-09 08:48:41 +00004561 F.ModuleName.empty()) {
4562 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4563 if (Result != OutOfDate ||
4564 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4565 Diag(diag::err_module_file_not_module) << FileName;
4566 return Result;
4567 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004568 break;
4569
4570 case Failure: return Failure;
Douglas Gregor7029ce12013-03-19 00:28:20 +00004571 case Missing: return Missing;
Guy Benyei11169dd2012-12-18 14:30:41 +00004572 case OutOfDate: return OutOfDate;
4573 case VersionMismatch: return VersionMismatch;
4574 case ConfigurationMismatch: return ConfigurationMismatch;
4575 case HadErrors: return HadErrors;
4576 }
4577 break;
Richard Smithf8c32552015-09-02 17:45:54 +00004578
Guy Benyei11169dd2012-12-18 14:30:41 +00004579 case AST_BLOCK_ID:
4580 if (!HaveReadControlBlock) {
4581 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00004582 Diag(diag::err_pch_version_too_old);
Guy Benyei11169dd2012-12-18 14:30:41 +00004583 return VersionMismatch;
4584 }
4585
4586 // Record that we've loaded this module.
4587 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00004588 ShouldFinalizePCM = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00004589 return Success;
4590
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004591 case UNHASHED_CONTROL_BLOCK_ID:
4592 // This block is handled using look-ahead during ReadControlBlock. We
4593 // shouldn't get here!
4594 Error("malformed block record in AST file");
4595 return Failure;
4596
Guy Benyei11169dd2012-12-18 14:30:41 +00004597 default:
JF Bastien0e828952019-06-26 19:50:12 +00004598 if (llvm::Error Err = Stream.SkipBlock()) {
4599 Error(std::move(Err));
Guy Benyei11169dd2012-12-18 14:30:41 +00004600 return Failure;
4601 }
4602 break;
4603 }
4604 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004605
Duncan P. N. Exon Smithfae03d82019-03-03 20:17:53 +00004606 llvm_unreachable("unexpected break; expected return");
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004607}
4608
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004609ASTReader::ASTReadResult
4610ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4611 unsigned ClientLoadCapabilities) {
4612 const HeaderSearchOptions &HSOpts =
4613 PP.getHeaderSearchInfo().getHeaderSearchOpts();
4614 bool AllowCompatibleConfigurationMismatch =
4615 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4616
4617 ASTReadResult Result = readUnhashedControlBlockImpl(
4618 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4619 Listener.get(),
4620 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4621
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004622 // If F was directly imported by another module, it's implicitly validated by
4623 // the importing module.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004624 if (DisableValidation || WasImportedBy ||
4625 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4626 return Success;
4627
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004628 if (Result == Failure) {
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004629 Error("malformed block record in AST file");
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004630 return Failure;
4631 }
4632
4633 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +00004634 // If this module has already been finalized in the ModuleCache, we're stuck
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004635 // with it; we can only load a single version of each module.
4636 //
4637 // This can happen when a module is imported in two contexts: in one, as a
4638 // user module; in another, as a system module (due to an import from
4639 // another module marked with the [system] flag). It usually indicates a
4640 // bug in the module map: this module should also be marked with [system].
4641 //
4642 // If -Wno-system-headers (the default), and the first import is as a
4643 // system module, then validation will fail during the as-user import,
4644 // since -Werror flags won't have been validated. However, it's reasonable
4645 // to treat this consistently as a system module.
4646 //
4647 // If -Wsystem-headers, the PCM on disk was built with
4648 // -Wno-system-headers, and the first import is as a user module, then
4649 // validation will fail during the as-system import since the PCM on disk
4650 // doesn't guarantee that -Werror was respected. However, the -Werror
4651 // flags were checked during the initial as-user import.
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00004652 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004653 Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4654 return Success;
4655 }
4656 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004657
4658 return Result;
4659}
4660
4661ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4662 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4663 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4664 bool ValidateDiagnosticOptions) {
4665 // Initialize a stream.
4666 BitstreamCursor Stream(StreamData);
4667
4668 // Sniff for the signature.
JF Bastien0e828952019-06-26 19:50:12 +00004669 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4670 // FIXME this drops the error on the floor.
4671 consumeError(std::move(Err));
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004672 return Failure;
JF Bastien0e828952019-06-26 19:50:12 +00004673 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004674
4675 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4676 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4677 return Failure;
4678
4679 // Read all of the records in the options block.
4680 RecordData Record;
4681 ASTReadResult Result = Success;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00004682 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00004683 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4684 if (!MaybeEntry) {
4685 // FIXME this drops the error on the floor.
4686 consumeError(MaybeEntry.takeError());
4687 return Failure;
4688 }
4689 llvm::BitstreamEntry Entry = MaybeEntry.get();
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004690
4691 switch (Entry.Kind) {
4692 case llvm::BitstreamEntry::Error:
4693 case llvm::BitstreamEntry::SubBlock:
4694 return Failure;
4695
4696 case llvm::BitstreamEntry::EndBlock:
4697 return Result;
4698
4699 case llvm::BitstreamEntry::Record:
4700 // The interesting case.
4701 break;
4702 }
4703
4704 // Read and process a record.
4705 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00004706 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4707 if (!MaybeRecordType) {
4708 // FIXME this drops the error.
4709 return Failure;
4710 }
4711 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00004712 case SIGNATURE:
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004713 if (F)
4714 std::copy(Record.begin(), Record.end(), F->Signature.data());
4715 break;
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004716 case DIAGNOSTIC_OPTIONS: {
4717 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4718 if (Listener && ValidateDiagnosticOptions &&
4719 !AllowCompatibleConfigurationMismatch &&
4720 ParseDiagnosticOptions(Record, Complain, *Listener))
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004721 Result = OutOfDate; // Don't return early. Read the signature.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004722 break;
4723 }
4724 case DIAG_PRAGMA_MAPPINGS:
4725 if (!F)
4726 break;
4727 if (F->PragmaDiagMappings.empty())
4728 F->PragmaDiagMappings.swap(Record);
4729 else
4730 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4731 Record.begin(), Record.end());
4732 break;
4733 }
4734 }
4735}
4736
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004737/// Parse a record and blob containing module file extension metadata.
4738static bool parseModuleFileExtensionMetadata(
4739 const SmallVectorImpl<uint64_t> &Record,
4740 StringRef Blob,
4741 ModuleFileExtensionMetadata &Metadata) {
4742 if (Record.size() < 4) return true;
4743
4744 Metadata.MajorVersion = Record[0];
4745 Metadata.MinorVersion = Record[1];
4746
4747 unsigned BlockNameLen = Record[2];
4748 unsigned UserInfoLen = Record[3];
4749
4750 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4751
4752 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4753 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4754 Blob.data() + BlockNameLen + UserInfoLen);
4755 return false;
4756}
4757
4758ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4759 BitstreamCursor &Stream = F.Stream;
4760
4761 RecordData Record;
4762 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00004763 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4764 if (!MaybeEntry) {
4765 Error(MaybeEntry.takeError());
4766 return Failure;
4767 }
4768 llvm::BitstreamEntry Entry = MaybeEntry.get();
4769
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004770 switch (Entry.Kind) {
4771 case llvm::BitstreamEntry::SubBlock:
JF Bastien0e828952019-06-26 19:50:12 +00004772 if (llvm::Error Err = Stream.SkipBlock()) {
4773 Error(std::move(Err));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004774 return Failure;
JF Bastien0e828952019-06-26 19:50:12 +00004775 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004776 continue;
4777
4778 case llvm::BitstreamEntry::EndBlock:
4779 return Success;
4780
4781 case llvm::BitstreamEntry::Error:
4782 return HadErrors;
4783
4784 case llvm::BitstreamEntry::Record:
4785 break;
4786 }
4787
4788 Record.clear();
4789 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00004790 Expected<unsigned> MaybeRecCode =
4791 Stream.readRecord(Entry.ID, Record, &Blob);
4792 if (!MaybeRecCode) {
4793 Error(MaybeRecCode.takeError());
4794 return Failure;
4795 }
4796 switch (MaybeRecCode.get()) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004797 case EXTENSION_METADATA: {
4798 ModuleFileExtensionMetadata Metadata;
Duncan P. N. Exon Smith8e2c1922019-11-10 11:07:20 -08004799 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4800 Error("malformed EXTENSION_METADATA in AST file");
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004801 return Failure;
Duncan P. N. Exon Smith8e2c1922019-11-10 11:07:20 -08004802 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004803
4804 // Find a module file extension with this block name.
4805 auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4806 if (Known == ModuleFileExtensions.end()) break;
4807
4808 // Form a reader.
4809 if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4810 F, Stream)) {
4811 F.ExtensionReaders.push_back(std::move(Reader));
4812 }
4813
4814 break;
4815 }
4816 }
4817 }
4818
4819 return Success;
Guy Benyei11169dd2012-12-18 14:30:41 +00004820}
4821
Richard Smitha7e2cc62015-05-01 01:53:09 +00004822void ASTReader::InitializeContext() {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004823 assert(ContextObj && "no context to initialize");
4824 ASTContext &Context = *ContextObj;
4825
Guy Benyei11169dd2012-12-18 14:30:41 +00004826 // If there's a listener, notify them that we "read" the translation unit.
4827 if (DeserializationListener)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004828 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
Guy Benyei11169dd2012-12-18 14:30:41 +00004829 Context.getTranslationUnitDecl());
4830
Guy Benyei11169dd2012-12-18 14:30:41 +00004831 // FIXME: Find a better way to deal with collisions between these
4832 // built-in types. Right now, we just ignore the problem.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004833
Guy Benyei11169dd2012-12-18 14:30:41 +00004834 // Load the special types.
4835 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4836 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4837 if (!Context.CFConstantStringTypeDecl)
4838 Context.setCFConstantStringType(GetType(String));
4839 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004840
Guy Benyei11169dd2012-12-18 14:30:41 +00004841 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4842 QualType FileType = GetType(File);
4843 if (FileType.isNull()) {
4844 Error("FILE type is NULL");
4845 return;
4846 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004847
Guy Benyei11169dd2012-12-18 14:30:41 +00004848 if (!Context.FILEDecl) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004849 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Guy Benyei11169dd2012-12-18 14:30:41 +00004850 Context.setFILEDecl(Typedef->getDecl());
4851 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004852 const TagType *Tag = FileType->getAs<TagType>();
Guy Benyei11169dd2012-12-18 14:30:41 +00004853 if (!Tag) {
4854 Error("Invalid FILE type in AST file");
4855 return;
4856 }
4857 Context.setFILEDecl(Tag->getDecl());
4858 }
4859 }
4860 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004861
Guy Benyei11169dd2012-12-18 14:30:41 +00004862 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4863 QualType Jmp_bufType = GetType(Jmp_buf);
4864 if (Jmp_bufType.isNull()) {
4865 Error("jmp_buf type is NULL");
4866 return;
4867 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004868
Guy Benyei11169dd2012-12-18 14:30:41 +00004869 if (!Context.jmp_bufDecl) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004870 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Guy Benyei11169dd2012-12-18 14:30:41 +00004871 Context.setjmp_bufDecl(Typedef->getDecl());
4872 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004873 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Guy Benyei11169dd2012-12-18 14:30:41 +00004874 if (!Tag) {
4875 Error("Invalid jmp_buf type in AST file");
4876 return;
4877 }
4878 Context.setjmp_bufDecl(Tag->getDecl());
4879 }
4880 }
4881 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004882
Guy Benyei11169dd2012-12-18 14:30:41 +00004883 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4884 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4885 if (Sigjmp_bufType.isNull()) {
4886 Error("sigjmp_buf type is NULL");
4887 return;
4888 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004889
Guy Benyei11169dd2012-12-18 14:30:41 +00004890 if (!Context.sigjmp_bufDecl) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004891 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Guy Benyei11169dd2012-12-18 14:30:41 +00004892 Context.setsigjmp_bufDecl(Typedef->getDecl());
4893 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004894 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Guy Benyei11169dd2012-12-18 14:30:41 +00004895 assert(Tag && "Invalid sigjmp_buf type in AST file");
4896 Context.setsigjmp_bufDecl(Tag->getDecl());
4897 }
4898 }
4899 }
4900
4901 if (unsigned ObjCIdRedef
4902 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4903 if (Context.ObjCIdRedefinitionType.isNull())
4904 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4905 }
4906
4907 if (unsigned ObjCClassRedef
4908 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4909 if (Context.ObjCClassRedefinitionType.isNull())
4910 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4911 }
4912
4913 if (unsigned ObjCSelRedef
4914 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4915 if (Context.ObjCSelRedefinitionType.isNull())
4916 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4917 }
4918
4919 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4920 QualType Ucontext_tType = GetType(Ucontext_t);
4921 if (Ucontext_tType.isNull()) {
4922 Error("ucontext_t type is NULL");
4923 return;
4924 }
4925
4926 if (!Context.ucontext_tDecl) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004927 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
Guy Benyei11169dd2012-12-18 14:30:41 +00004928 Context.setucontext_tDecl(Typedef->getDecl());
4929 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004930 const TagType *Tag = Ucontext_tType->getAs<TagType>();
Guy Benyei11169dd2012-12-18 14:30:41 +00004931 assert(Tag && "Invalid ucontext_t type in AST file");
4932 Context.setucontext_tDecl(Tag->getDecl());
4933 }
4934 }
4935 }
4936 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004937
Guy Benyei11169dd2012-12-18 14:30:41 +00004938 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4939
4940 // If there were any CUDA special declarations, deserialize them.
4941 if (!CUDASpecialDeclRefs.empty()) {
4942 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4943 Context.setcudaConfigureCallDecl(
4944 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4945 }
Richard Smith56be7542014-03-21 00:33:59 +00004946
Guy Benyei11169dd2012-12-18 14:30:41 +00004947 // Re-export any modules that were imported by a non-module AST file.
Richard Smitha7e2cc62015-05-01 01:53:09 +00004948 // FIXME: This does not make macro-only imports visible again.
Richard Smith56be7542014-03-21 00:33:59 +00004949 for (auto &Import : ImportedModules) {
Richard Smitha7e2cc62015-05-01 01:53:09 +00004950 if (Module *Imported = getSubmodule(Import.ID)) {
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00004951 makeModuleVisible(Imported, Module::AllVisible,
Richard Smitha7e2cc62015-05-01 01:53:09 +00004952 /*ImportLoc=*/Import.ImportLoc);
Ben Langmuir6d25fdc2016-02-11 17:04:42 +00004953 if (Import.ImportLoc.isValid())
4954 PP.makeModuleVisible(Imported, Import.ImportLoc);
4955 // FIXME: should we tell Sema to make the module visible too?
Richard Smitha7e2cc62015-05-01 01:53:09 +00004956 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004957 }
4958 ImportedModules.clear();
4959}
4960
4961void ASTReader::finalizeForWriting() {
Richard Smithde711422015-04-23 21:20:19 +00004962 // Nothing to do for now.
Guy Benyei11169dd2012-12-18 14:30:41 +00004963}
4964
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004965/// Reads and return the signature record from \p PCH's control block, or
Peter Collingbourne77c89b62016-11-08 04:17:11 +00004966/// else returns 0.
4967static ASTFileSignature readASTFileSignature(StringRef PCH) {
4968 BitstreamCursor Stream(PCH);
JF Bastien0e828952019-06-26 19:50:12 +00004969 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4970 // FIXME this drops the error on the floor.
4971 consumeError(std::move(Err));
Vedant Kumar48b4f762018-04-14 01:40:48 +00004972 return ASTFileSignature();
JF Bastien0e828952019-06-26 19:50:12 +00004973 }
Ben Langmuir487ea142014-10-23 18:05:36 +00004974
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004975 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4976 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
Vedant Kumar48b4f762018-04-14 01:40:48 +00004977 return ASTFileSignature();
Ben Langmuir487ea142014-10-23 18:05:36 +00004978
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004979 // Scan for SIGNATURE inside the diagnostic options block.
Ben Langmuir487ea142014-10-23 18:05:36 +00004980 ASTReader::RecordData Record;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00004981 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00004982 Expected<llvm::BitstreamEntry> MaybeEntry =
4983 Stream.advanceSkippingSubblocks();
4984 if (!MaybeEntry) {
4985 // FIXME this drops the error on the floor.
4986 consumeError(MaybeEntry.takeError());
4987 return ASTFileSignature();
4988 }
4989 llvm::BitstreamEntry Entry = MaybeEntry.get();
4990
Simon Pilgrim0b33f112016-11-16 16:11:08 +00004991 if (Entry.Kind != llvm::BitstreamEntry::Record)
Vedant Kumar48b4f762018-04-14 01:40:48 +00004992 return ASTFileSignature();
Ben Langmuir487ea142014-10-23 18:05:36 +00004993
4994 Record.clear();
4995 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00004996 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
4997 if (!MaybeRecord) {
4998 // FIXME this drops the error on the floor.
4999 consumeError(MaybeRecord.takeError());
5000 return ASTFileSignature();
5001 }
5002 if (SIGNATURE == MaybeRecord.get())
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005003 return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
5004 (uint32_t)Record[3], (uint32_t)Record[4]}}};
Ben Langmuir487ea142014-10-23 18:05:36 +00005005 }
5006}
5007
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005008/// Retrieve the name of the original source file name
Guy Benyei11169dd2012-12-18 14:30:41 +00005009/// directly from the AST file, without actually loading the AST
5010/// file.
Adrian Prantlbb165fb2015-06-20 18:53:08 +00005011std::string ASTReader::getOriginalSourceFile(
5012 const std::string &ASTFileName, FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +00005013 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005014 // Open the AST file.
Benjamin Kramera8857962014-10-26 22:44:13 +00005015 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00005016 if (!Buffer) {
Benjamin Kramera8857962014-10-26 22:44:13 +00005017 Diags.Report(diag::err_fe_unable_to_read_pch_file)
5018 << ASTFileName << Buffer.getError().message();
Vedant Kumar48b4f762018-04-14 01:40:48 +00005019 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00005020 }
5021
5022 // Initialize the stream
Peter Collingbourne77c89b62016-11-08 04:17:11 +00005023 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
Guy Benyei11169dd2012-12-18 14:30:41 +00005024
5025 // Sniff for the signature.
JF Bastien0e828952019-06-26 19:50:12 +00005026 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5027 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
Vedant Kumar48b4f762018-04-14 01:40:48 +00005028 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00005029 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005030
Chris Lattnere7b154b2013-01-19 21:39:22 +00005031 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005032 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005033 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Vedant Kumar48b4f762018-04-14 01:40:48 +00005034 return std::string();
Chris Lattnere7b154b2013-01-19 21:39:22 +00005035 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005036
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005037 // Scan for ORIGINAL_FILE inside the control block.
5038 RecordData Record;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005039 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00005040 Expected<llvm::BitstreamEntry> MaybeEntry =
5041 Stream.advanceSkippingSubblocks();
5042 if (!MaybeEntry) {
5043 // FIXME this drops errors on the floor.
5044 consumeError(MaybeEntry.takeError());
5045 return std::string();
5046 }
5047 llvm::BitstreamEntry Entry = MaybeEntry.get();
5048
Chris Lattnere7b154b2013-01-19 21:39:22 +00005049 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
Vedant Kumar48b4f762018-04-14 01:40:48 +00005050 return std::string();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005051
Chris Lattnere7b154b2013-01-19 21:39:22 +00005052 if (Entry.Kind != llvm::BitstreamEntry::Record) {
5053 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Vedant Kumar48b4f762018-04-14 01:40:48 +00005054 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00005055 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005056
Guy Benyei11169dd2012-12-18 14:30:41 +00005057 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005058 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00005059 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5060 if (!MaybeRecord) {
5061 // FIXME this drops the errors on the floor.
5062 consumeError(MaybeRecord.takeError());
5063 return std::string();
5064 }
5065 if (ORIGINAL_FILE == MaybeRecord.get())
Chris Lattner0e6c9402013-01-20 02:38:54 +00005066 return Blob.str();
Guy Benyei11169dd2012-12-18 14:30:41 +00005067 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005068}
5069
5070namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005071
Guy Benyei11169dd2012-12-18 14:30:41 +00005072 class SimplePCHValidator : public ASTReaderListener {
5073 const LangOptions &ExistingLangOpts;
5074 const TargetOptions &ExistingTargetOpts;
5075 const PreprocessorOptions &ExistingPPOpts;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005076 std::string ExistingModuleCachePath;
Guy Benyei11169dd2012-12-18 14:30:41 +00005077 FileManager &FileMgr;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005078
Guy Benyei11169dd2012-12-18 14:30:41 +00005079 public:
5080 SimplePCHValidator(const LangOptions &ExistingLangOpts,
5081 const TargetOptions &ExistingTargetOpts,
5082 const PreprocessorOptions &ExistingPPOpts,
Benjamin Krameradcd0262020-01-28 20:23:46 +01005083 StringRef ExistingModuleCachePath, FileManager &FileMgr)
5084 : ExistingLangOpts(ExistingLangOpts),
5085 ExistingTargetOpts(ExistingTargetOpts),
5086 ExistingPPOpts(ExistingPPOpts),
5087 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
Guy Benyei11169dd2012-12-18 14:30:41 +00005088
Richard Smith1e2cf0d2014-10-31 02:28:58 +00005089 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5090 bool AllowCompatibleDifferences) override {
5091 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5092 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00005093 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005094
Chandler Carruth0d745bc2015-03-14 04:47:43 +00005095 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5096 bool AllowCompatibleDifferences) override {
5097 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5098 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00005099 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005100
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005101 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5102 StringRef SpecificModuleCachePath,
5103 bool Complain) override {
5104 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5105 ExistingModuleCachePath,
5106 nullptr, ExistingLangOpts);
5107 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005108
Craig Topper3e89dfe2014-03-13 02:13:41 +00005109 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5110 bool Complain,
5111 std::string &SuggestedPredefines) override {
Craig Toppera13603a2014-05-22 05:54:18 +00005112 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00005113 SuggestedPredefines, ExistingLangOpts);
Guy Benyei11169dd2012-12-18 14:30:41 +00005114 }
5115 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005116
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005117} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00005118
Adrian Prantlbb165fb2015-06-20 18:53:08 +00005119bool ASTReader::readASTFileControlBlock(
5120 StringRef Filename, FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +00005121 const PCHContainerReader &PCHContainerRdr,
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005122 bool FindModuleFileExtensions,
Manman Ren47a44452016-07-26 17:12:17 +00005123 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005124 // Open the AST file.
Richard Smith7f330cd2015-03-18 01:42:29 +00005125 // FIXME: This allows use of the VFS; we do not allow use of the
5126 // VFS when actually loading a module.
Benjamin Kramera8857962014-10-26 22:44:13 +00005127 auto Buffer = FileMgr.getBufferForFile(Filename);
Guy Benyei11169dd2012-12-18 14:30:41 +00005128 if (!Buffer) {
5129 return true;
5130 }
5131
5132 // Initialize the stream
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005133 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5134 BitstreamCursor Stream(Bytes);
Guy Benyei11169dd2012-12-18 14:30:41 +00005135
5136 // Sniff for the signature.
JF Bastien0e828952019-06-26 19:50:12 +00005137 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5138 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
Guy Benyei11169dd2012-12-18 14:30:41 +00005139 return true;
JF Bastien0e828952019-06-26 19:50:12 +00005140 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005141
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005142 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005143 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005144 return true;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005145
5146 bool NeedsInputFiles = Listener.needsInputFileVisitation();
Ben Langmuircb69b572014-03-07 06:40:32 +00005147 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
Richard Smithd4b230b2014-10-27 23:01:16 +00005148 bool NeedsImports = Listener.needsImportVisitation();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005149 BitstreamCursor InputFilesCursor;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005150
Guy Benyei11169dd2012-12-18 14:30:41 +00005151 RecordData Record;
Richard Smith7ed1bc92014-12-05 22:42:13 +00005152 std::string ModuleDir;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005153 bool DoneWithControlBlock = false;
5154 while (!DoneWithControlBlock) {
JF Bastien0e828952019-06-26 19:50:12 +00005155 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5156 if (!MaybeEntry) {
5157 // FIXME this drops the error on the floor.
5158 consumeError(MaybeEntry.takeError());
5159 return true;
5160 }
5161 llvm::BitstreamEntry Entry = MaybeEntry.get();
Richard Smith0516b182015-09-08 19:40:14 +00005162
5163 switch (Entry.Kind) {
5164 case llvm::BitstreamEntry::SubBlock: {
5165 switch (Entry.ID) {
5166 case OPTIONS_BLOCK_ID: {
5167 std::string IgnoredSuggestedPredefines;
5168 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5169 /*AllowCompatibleConfigurationMismatch*/ false,
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005170 Listener, IgnoredSuggestedPredefines) != Success)
Richard Smith0516b182015-09-08 19:40:14 +00005171 return true;
5172 break;
5173 }
5174
5175 case INPUT_FILES_BLOCK_ID:
5176 InputFilesCursor = Stream;
JF Bastien0e828952019-06-26 19:50:12 +00005177 if (llvm::Error Err = Stream.SkipBlock()) {
5178 // FIXME this drops the error on the floor.
5179 consumeError(std::move(Err));
5180 return true;
5181 }
5182 if (NeedsInputFiles &&
5183 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
Richard Smith0516b182015-09-08 19:40:14 +00005184 return true;
5185 break;
5186
5187 default:
JF Bastien0e828952019-06-26 19:50:12 +00005188 if (llvm::Error Err = Stream.SkipBlock()) {
5189 // FIXME this drops the error on the floor.
5190 consumeError(std::move(Err));
Richard Smith0516b182015-09-08 19:40:14 +00005191 return true;
JF Bastien0e828952019-06-26 19:50:12 +00005192 }
Richard Smith0516b182015-09-08 19:40:14 +00005193 break;
5194 }
5195
5196 continue;
5197 }
5198
5199 case llvm::BitstreamEntry::EndBlock:
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005200 DoneWithControlBlock = true;
5201 break;
Richard Smith0516b182015-09-08 19:40:14 +00005202
5203 case llvm::BitstreamEntry::Error:
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005204 return true;
Richard Smith0516b182015-09-08 19:40:14 +00005205
5206 case llvm::BitstreamEntry::Record:
5207 break;
5208 }
5209
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005210 if (DoneWithControlBlock) break;
5211
Guy Benyei11169dd2012-12-18 14:30:41 +00005212 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005213 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00005214 Expected<unsigned> MaybeRecCode =
5215 Stream.readRecord(Entry.ID, Record, &Blob);
5216 if (!MaybeRecCode) {
5217 // FIXME this drops the error.
5218 return Failure;
5219 }
5220 switch ((ControlRecordTypes)MaybeRecCode.get()) {
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005221 case METADATA:
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005222 if (Record[0] != VERSION_MAJOR)
5223 return true;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00005224 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005225 return true;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005226 break;
Ben Langmuir4f5212a2014-04-14 22:12:44 +00005227 case MODULE_NAME:
5228 Listener.ReadModuleName(Blob);
5229 break;
Richard Smith7ed1bc92014-12-05 22:42:13 +00005230 case MODULE_DIRECTORY:
Benjamin Krameradcd0262020-01-28 20:23:46 +01005231 ModuleDir = std::string(Blob);
Richard Smith7ed1bc92014-12-05 22:42:13 +00005232 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00005233 case MODULE_MAP_FILE: {
5234 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00005235 auto Path = ReadString(Record, Idx);
5236 ResolveImportedPath(Path, ModuleDir);
5237 Listener.ReadModuleMapFile(Path);
Ben Langmuir4f5212a2014-04-14 22:12:44 +00005238 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00005239 }
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005240 case INPUT_FILE_OFFSETS: {
5241 if (!NeedsInputFiles)
5242 break;
5243
5244 unsigned NumInputFiles = Record[0];
5245 unsigned NumUserFiles = Record[1];
Raphael Isemanneb13d3d2018-05-23 09:02:40 +00005246 const llvm::support::unaligned_uint64_t *InputFileOffs =
5247 (const llvm::support::unaligned_uint64_t *)Blob.data();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005248 for (unsigned I = 0; I != NumInputFiles; ++I) {
5249 // Go find this input file.
5250 bool isSystemFile = I >= NumUserFiles;
Ben Langmuircb69b572014-03-07 06:40:32 +00005251
5252 if (isSystemFile && !NeedsSystemInputFiles)
5253 break; // the rest are system input files
5254
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005255 BitstreamCursor &Cursor = InputFilesCursor;
5256 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00005257 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5258 // FIXME this drops errors on the floor.
5259 consumeError(std::move(Err));
5260 }
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005261
JF Bastien0e828952019-06-26 19:50:12 +00005262 Expected<unsigned> MaybeCode = Cursor.ReadCode();
5263 if (!MaybeCode) {
5264 // FIXME this drops errors on the floor.
5265 consumeError(MaybeCode.takeError());
5266 }
5267 unsigned Code = MaybeCode.get();
5268
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005269 RecordData Record;
5270 StringRef Blob;
5271 bool shouldContinue = false;
JF Bastien0e828952019-06-26 19:50:12 +00005272 Expected<unsigned> MaybeRecordType =
5273 Cursor.readRecord(Code, Record, &Blob);
5274 if (!MaybeRecordType) {
5275 // FIXME this drops errors on the floor.
5276 consumeError(MaybeRecordType.takeError());
5277 }
5278 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00005279 case INPUT_FILE_HASH:
5280 break;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005281 case INPUT_FILE:
Vedant Kumar48b4f762018-04-14 01:40:48 +00005282 bool Overridden = static_cast<bool>(Record[3]);
Benjamin Krameradcd0262020-01-28 20:23:46 +01005283 std::string Filename = std::string(Blob);
Richard Smith7ed1bc92014-12-05 22:42:13 +00005284 ResolveImportedPath(Filename, ModuleDir);
Richard Smith216a3bd2015-08-13 17:57:10 +00005285 shouldContinue = Listener.visitInputFile(
5286 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005287 break;
5288 }
5289 if (!shouldContinue)
5290 break;
5291 }
5292 break;
5293 }
5294
Richard Smithd4b230b2014-10-27 23:01:16 +00005295 case IMPORTS: {
5296 if (!NeedsImports)
5297 break;
5298
5299 unsigned Idx = 0, N = Record.size();
5300 while (Idx < N) {
5301 // Read information about the AST file.
Bruno Cardoso Lopes6fc8a562018-09-11 05:17:13 +00005302 Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature
5303 std::string ModuleName = ReadString(Record, Idx);
Richard Smith7ed1bc92014-12-05 22:42:13 +00005304 std::string Filename = ReadString(Record, Idx);
5305 ResolveImportedPath(Filename, ModuleDir);
Bruno Cardoso Lopes6fc8a562018-09-11 05:17:13 +00005306 Listener.visitImport(ModuleName, Filename);
Richard Smithd4b230b2014-10-27 23:01:16 +00005307 }
5308 break;
5309 }
5310
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005311 default:
5312 // No other validation to perform.
5313 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005314 }
5315 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005316
5317 // Look for module file extension blocks, if requested.
5318 if (FindModuleFileExtensions) {
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005319 BitstreamCursor SavedStream = Stream;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005320 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5321 bool DoneWithExtensionBlock = false;
5322 while (!DoneWithExtensionBlock) {
JF Bastien0e828952019-06-26 19:50:12 +00005323 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5324 if (!MaybeEntry) {
5325 // FIXME this drops the error.
5326 return true;
5327 }
5328 llvm::BitstreamEntry Entry = MaybeEntry.get();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005329
JF Bastien0e828952019-06-26 19:50:12 +00005330 switch (Entry.Kind) {
5331 case llvm::BitstreamEntry::SubBlock:
5332 if (llvm::Error Err = Stream.SkipBlock()) {
5333 // FIXME this drops the error on the floor.
5334 consumeError(std::move(Err));
5335 return true;
5336 }
5337 continue;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005338
JF Bastien0e828952019-06-26 19:50:12 +00005339 case llvm::BitstreamEntry::EndBlock:
5340 DoneWithExtensionBlock = true;
5341 continue;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005342
JF Bastien0e828952019-06-26 19:50:12 +00005343 case llvm::BitstreamEntry::Error:
5344 return true;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005345
JF Bastien0e828952019-06-26 19:50:12 +00005346 case llvm::BitstreamEntry::Record:
5347 break;
5348 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005349
5350 Record.clear();
5351 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00005352 Expected<unsigned> MaybeRecCode =
5353 Stream.readRecord(Entry.ID, Record, &Blob);
5354 if (!MaybeRecCode) {
5355 // FIXME this drops the error.
5356 return true;
5357 }
5358 switch (MaybeRecCode.get()) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005359 case EXTENSION_METADATA: {
5360 ModuleFileExtensionMetadata Metadata;
5361 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5362 return true;
5363
5364 Listener.readModuleFileExtension(Metadata);
5365 break;
5366 }
5367 }
5368 }
5369 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005370 Stream = SavedStream;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005371 }
5372
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005373 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5374 if (readUnhashedControlBlockImpl(
5375 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5376 /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5377 ValidateDiagnosticOptions) != Success)
5378 return true;
5379
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005380 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00005381}
5382
Benjamin Kramerf6021ec2017-03-21 21:35:04 +00005383bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5384 const PCHContainerReader &PCHContainerRdr,
5385 const LangOptions &LangOpts,
5386 const TargetOptions &TargetOpts,
5387 const PreprocessorOptions &PPOpts,
5388 StringRef ExistingModuleCachePath) {
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005389 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5390 ExistingModuleCachePath, FileMgr);
Adrian Prantlfb2398d2015-07-17 01:19:54 +00005391 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005392 /*FindModuleFileExtensions=*/false,
Manman Ren47a44452016-07-26 17:12:17 +00005393 validator,
5394 /*ValidateDiagnosticOptions=*/true);
Guy Benyei11169dd2012-12-18 14:30:41 +00005395}
5396
Ben Langmuir2c9af442014-04-10 17:57:43 +00005397ASTReader::ASTReadResult
5398ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005399 // Enter the submodule block.
JF Bastien0e828952019-06-26 19:50:12 +00005400 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5401 Error(std::move(Err));
Ben Langmuir2c9af442014-04-10 17:57:43 +00005402 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00005403 }
5404
5405 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5406 bool First = true;
Craig Toppera13603a2014-05-22 05:54:18 +00005407 Module *CurrentModule = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005408 RecordData Record;
5409 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00005410 Expected<llvm::BitstreamEntry> MaybeEntry =
5411 F.Stream.advanceSkippingSubblocks();
5412 if (!MaybeEntry) {
5413 Error(MaybeEntry.takeError());
5414 return Failure;
5415 }
5416 llvm::BitstreamEntry Entry = MaybeEntry.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005417
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005418 switch (Entry.Kind) {
5419 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5420 case llvm::BitstreamEntry::Error:
5421 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00005422 return Failure;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005423 case llvm::BitstreamEntry::EndBlock:
Ben Langmuir2c9af442014-04-10 17:57:43 +00005424 return Success;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005425 case llvm::BitstreamEntry::Record:
5426 // The interesting case.
5427 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005428 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005429
Guy Benyei11169dd2012-12-18 14:30:41 +00005430 // Read a record.
Chris Lattner0e6c9402013-01-20 02:38:54 +00005431 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00005432 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00005433 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5434 if (!MaybeKind) {
5435 Error(MaybeKind.takeError());
5436 return Failure;
5437 }
5438 unsigned Kind = MaybeKind.get();
Richard Smith03478d92014-10-23 22:12:14 +00005439
5440 if ((Kind == SUBMODULE_METADATA) != First) {
5441 Error("submodule metadata record should be at beginning of block");
5442 return Failure;
5443 }
5444 First = false;
5445
5446 // Submodule information is only valid if we have a current module.
5447 // FIXME: Should we error on these cases?
5448 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5449 Kind != SUBMODULE_DEFINITION)
5450 continue;
5451
5452 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005453 default: // Default behavior: ignore.
5454 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005455
Richard Smith03478d92014-10-23 22:12:14 +00005456 case SUBMODULE_DEFINITION: {
Jordan Rose90b0a1f2018-04-20 17:16:04 +00005457 if (Record.size() < 12) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005458 Error("malformed module definition");
Ben Langmuir2c9af442014-04-10 17:57:43 +00005459 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00005460 }
Richard Smith03478d92014-10-23 22:12:14 +00005461
Chris Lattner0e6c9402013-01-20 02:38:54 +00005462 StringRef Name = Blob;
Richard Smith9bca2982014-03-08 00:03:56 +00005463 unsigned Idx = 0;
5464 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5465 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
Vedant Kumar48b4f762018-04-14 01:40:48 +00005466 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
Richard Smith9bca2982014-03-08 00:03:56 +00005467 bool IsFramework = Record[Idx++];
5468 bool IsExplicit = Record[Idx++];
5469 bool IsSystem = Record[Idx++];
5470 bool IsExternC = Record[Idx++];
5471 bool InferSubmodules = Record[Idx++];
5472 bool InferExplicitSubmodules = Record[Idx++];
5473 bool InferExportWildcard = Record[Idx++];
5474 bool ConfigMacrosExhaustive = Record[Idx++];
Jordan Rose90b0a1f2018-04-20 17:16:04 +00005475 bool ModuleMapIsPrivate = Record[Idx++];
Douglas Gregor8d932422013-03-20 03:59:18 +00005476
Ben Langmuirbeee15e2014-04-14 18:00:01 +00005477 Module *ParentModule = nullptr;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00005478 if (Parent)
Guy Benyei11169dd2012-12-18 14:30:41 +00005479 ParentModule = getSubmodule(Parent);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00005480
Guy Benyei11169dd2012-12-18 14:30:41 +00005481 // Retrieve this (sub)module from the module map, creating it if
5482 // necessary.
David Blaikie9ffe5a32017-01-30 05:00:26 +00005483 CurrentModule =
5484 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5485 .first;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00005486
5487 // FIXME: set the definition loc for CurrentModule, or call
5488 // ModMap.setInferredModuleAllowedBy()
5489
Guy Benyei11169dd2012-12-18 14:30:41 +00005490 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5491 if (GlobalIndex >= SubmodulesLoaded.size() ||
5492 SubmodulesLoaded[GlobalIndex]) {
5493 Error("too many submodules");
Ben Langmuir2c9af442014-04-10 17:57:43 +00005494 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00005495 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00005496
Douglas Gregor7029ce12013-03-19 00:28:20 +00005497 if (!ParentModule) {
5498 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
Yuka Takahashid8baec22018-08-01 09:50:02 +00005499 // Don't emit module relocation error if we have -fno-validate-pch
5500 if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5501 CurFile != F.File) {
Duncan P. N. Exon Smitheef69022019-11-10 11:17:42 -08005502 Error(diag::err_module_file_conflict,
5503 CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5504 F.File->getName());
Ben Langmuir2c9af442014-04-10 17:57:43 +00005505 return Failure;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00005506 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00005507 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00005508
Duncan P. N. Exon Smith83dcb342019-11-10 13:14:52 -08005509 F.DidReadTopLevelSubmodule = true;
Douglas Gregor7029ce12013-03-19 00:28:20 +00005510 CurrentModule->setASTFile(F.File);
Richard Smithab755972017-06-05 18:10:11 +00005511 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00005512 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00005513
Richard Smithdd8b5332017-09-04 05:37:53 +00005514 CurrentModule->Kind = Kind;
Adrian Prantl15bcf702015-06-30 17:39:43 +00005515 CurrentModule->Signature = F.Signature;
Guy Benyei11169dd2012-12-18 14:30:41 +00005516 CurrentModule->IsFromModuleFile = true;
5517 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Richard Smith9bca2982014-03-08 00:03:56 +00005518 CurrentModule->IsExternC = IsExternC;
Guy Benyei11169dd2012-12-18 14:30:41 +00005519 CurrentModule->InferSubmodules = InferSubmodules;
5520 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5521 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor8d932422013-03-20 03:59:18 +00005522 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Jordan Rose90b0a1f2018-04-20 17:16:04 +00005523 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
Guy Benyei11169dd2012-12-18 14:30:41 +00005524 if (DeserializationListener)
5525 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005526
Guy Benyei11169dd2012-12-18 14:30:41 +00005527 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005528
Richard Smith8a3e39a2016-03-28 21:31:09 +00005529 // Clear out data that will be replaced by what is in the module file.
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005530 CurrentModule->LinkLibraries.clear();
Douglas Gregor8d932422013-03-20 03:59:18 +00005531 CurrentModule->ConfigMacros.clear();
Douglas Gregorfb912652013-03-20 21:10:35 +00005532 CurrentModule->UnresolvedConflicts.clear();
5533 CurrentModule->Conflicts.clear();
Richard Smith8a3e39a2016-03-28 21:31:09 +00005534
5535 // The module is available unless it's missing a requirement; relevant
5536 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5537 // Missing headers that were present when the module was built do not
5538 // make it unavailable -- if we got this far, this must be an explicitly
5539 // imported module file.
5540 CurrentModule->Requirements.clear();
5541 CurrentModule->MissingHeaders.clear();
5542 CurrentModule->IsMissingRequirement =
5543 ParentModule && ParentModule->IsMissingRequirement;
5544 CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
Guy Benyei11169dd2012-12-18 14:30:41 +00005545 break;
5546 }
Richard Smith8a3e39a2016-03-28 21:31:09 +00005547
Guy Benyei11169dd2012-12-18 14:30:41 +00005548 case SUBMODULE_UMBRELLA_HEADER: {
Benjamin Krameradcd0262020-01-28 20:23:46 +01005549 std::string Filename = std::string(Blob);
Richard Smith2b63d152015-05-16 02:28:53 +00005550 ResolveImportedPath(F, Filename);
Harlan Haskins8d323d12019-08-01 21:31:56 +00005551 if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005552 if (!CurrentModule->getUmbrellaHeader())
Harlan Haskins8d323d12019-08-01 21:31:56 +00005553 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5554 else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
Bruno Cardoso Lopes573b13f2017-03-22 00:11:21 +00005555 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5556 Error("mismatched umbrella headers in submodule");
5557 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00005558 }
5559 }
5560 break;
5561 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005562
Richard Smith202210b2014-10-24 20:23:01 +00005563 case SUBMODULE_HEADER:
5564 case SUBMODULE_EXCLUDED_HEADER:
5565 case SUBMODULE_PRIVATE_HEADER:
5566 // We lazily associate headers with their modules via the HeaderInfo table.
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00005567 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5568 // of complete filenames or remove it entirely.
Richard Smith202210b2014-10-24 20:23:01 +00005569 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005570
Richard Smith202210b2014-10-24 20:23:01 +00005571 case SUBMODULE_TEXTUAL_HEADER:
5572 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5573 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5574 // them here.
5575 break;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00005576
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005577 case SUBMODULE_TOPHEADER:
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00005578 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00005579 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005580
5581 case SUBMODULE_UMBRELLA_DIR: {
Benjamin Krameradcd0262020-01-28 20:23:46 +01005582 std::string Dirname = std::string(Blob);
Richard Smith2b63d152015-05-16 02:28:53 +00005583 ResolveImportedPath(F, Dirname);
Harlan Haskins8d323d12019-08-01 21:31:56 +00005584 if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005585 if (!CurrentModule->getUmbrellaDir())
Harlan Haskins8d323d12019-08-01 21:31:56 +00005586 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5587 else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
Ben Langmuir2c9af442014-04-10 17:57:43 +00005588 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5589 Error("mismatched umbrella directories in submodule");
5590 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00005591 }
5592 }
5593 break;
5594 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005595
Guy Benyei11169dd2012-12-18 14:30:41 +00005596 case SUBMODULE_METADATA: {
Guy Benyei11169dd2012-12-18 14:30:41 +00005597 F.BaseSubmoduleID = getTotalNumSubmodules();
5598 F.LocalNumSubmodules = Record[0];
5599 unsigned LocalBaseSubmoduleID = Record[1];
5600 if (F.LocalNumSubmodules > 0) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005601 // Introduce the global -> local mapping for submodules within this
Guy Benyei11169dd2012-12-18 14:30:41 +00005602 // module.
5603 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005604
5605 // Introduce the local -> global mapping for submodules within this
Guy Benyei11169dd2012-12-18 14:30:41 +00005606 // module.
5607 F.SubmoduleRemap.insertOrReplace(
5608 std::make_pair(LocalBaseSubmoduleID,
5609 F.BaseSubmoduleID - LocalBaseSubmoduleID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00005610
Ben Langmuir52ca6782014-10-20 16:27:32 +00005611 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5612 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005613 break;
5614 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005615
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005616 case SUBMODULE_IMPORTS:
Guy Benyei11169dd2012-12-18 14:30:41 +00005617 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregorfb912652013-03-20 21:10:35 +00005618 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00005619 Unresolved.File = &F;
5620 Unresolved.Mod = CurrentModule;
5621 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00005622 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei11169dd2012-12-18 14:30:41 +00005623 Unresolved.IsWildcard = false;
Douglas Gregorfb912652013-03-20 21:10:35 +00005624 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00005625 }
5626 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005627
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005628 case SUBMODULE_EXPORTS:
Guy Benyei11169dd2012-12-18 14:30:41 +00005629 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregorfb912652013-03-20 21:10:35 +00005630 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00005631 Unresolved.File = &F;
5632 Unresolved.Mod = CurrentModule;
5633 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00005634 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei11169dd2012-12-18 14:30:41 +00005635 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregorfb912652013-03-20 21:10:35 +00005636 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00005637 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005638
5639 // Once we've loaded the set of exports, there's no reason to keep
Guy Benyei11169dd2012-12-18 14:30:41 +00005640 // the parsed, unresolved exports around.
5641 CurrentModule->UnresolvedExports.clear();
5642 break;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005643
5644 case SUBMODULE_REQUIRES:
Richard Smithdbafb6c2017-06-29 23:23:46 +00005645 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5646 PP.getTargetInfo());
Guy Benyei11169dd2012-12-18 14:30:41 +00005647 break;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005648
5649 case SUBMODULE_LINK_LIBRARY:
Bruno Cardoso Lopesa3b5f712018-04-16 19:42:32 +00005650 ModMap.resolveLinkAsDependencies(CurrentModule);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005651 CurrentModule->LinkLibraries.push_back(
Benjamin Krameradcd0262020-01-28 20:23:46 +01005652 Module::LinkLibrary(std::string(Blob), Record[0]));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005653 break;
Douglas Gregor35b13ec2013-03-20 00:22:05 +00005654
5655 case SUBMODULE_CONFIG_MACRO:
Douglas Gregor35b13ec2013-03-20 00:22:05 +00005656 CurrentModule->ConfigMacros.push_back(Blob.str());
5657 break;
Douglas Gregorfb912652013-03-20 21:10:35 +00005658
5659 case SUBMODULE_CONFLICT: {
Douglas Gregorfb912652013-03-20 21:10:35 +00005660 UnresolvedModuleRef Unresolved;
5661 Unresolved.File = &F;
5662 Unresolved.Mod = CurrentModule;
5663 Unresolved.ID = Record[0];
5664 Unresolved.Kind = UnresolvedModuleRef::Conflict;
5665 Unresolved.IsWildcard = false;
5666 Unresolved.String = Blob;
5667 UnresolvedModuleRefs.push_back(Unresolved);
5668 break;
5669 }
Richard Smithdc1f0422016-07-20 19:10:16 +00005670
Douglas Gregorf0b11de2017-09-14 23:38:44 +00005671 case SUBMODULE_INITIALIZERS: {
Richard Smithdbafb6c2017-06-29 23:23:46 +00005672 if (!ContextObj)
5673 break;
Richard Smithdc1f0422016-07-20 19:10:16 +00005674 SmallVector<uint32_t, 16> Inits;
5675 for (auto &ID : Record)
5676 Inits.push_back(getGlobalDeclID(F, ID));
Richard Smithdbafb6c2017-06-29 23:23:46 +00005677 ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
Richard Smithdc1f0422016-07-20 19:10:16 +00005678 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005679 }
Douglas Gregorf0b11de2017-09-14 23:38:44 +00005680
5681 case SUBMODULE_EXPORT_AS:
5682 CurrentModule->ExportAsModule = Blob.str();
Bruno Cardoso Lopesa3b5f712018-04-16 19:42:32 +00005683 ModMap.addLinkAsDependency(CurrentModule);
Douglas Gregorf0b11de2017-09-14 23:38:44 +00005684 break;
5685 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005686 }
5687}
5688
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005689/// Parse the record that corresponds to a LangOptions data
Guy Benyei11169dd2012-12-18 14:30:41 +00005690/// structure.
5691///
5692/// This routine parses the language options from the AST file and then gives
5693/// them to the AST listener if one is set.
5694///
5695/// \returns true if the listener deems the file unacceptable, false otherwise.
5696bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5697 bool Complain,
Richard Smith1e2cf0d2014-10-31 02:28:58 +00005698 ASTReaderListener &Listener,
5699 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005700 LangOptions LangOpts;
5701 unsigned Idx = 0;
5702#define LANGOPT(Name, Bits, Default, Description) \
5703 LangOpts.Name = Record[Idx++];
5704#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5705 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5706#include "clang/Basic/LangOptions.def"
Alexey Samsonovedf99a92014-11-07 22:29:38 +00005707#define SANITIZER(NAME, ID) \
5708 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
Will Dietzf54319c2013-01-18 11:30:38 +00005709#include "clang/Basic/Sanitizers.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00005710
Ben Langmuircd98cb72015-06-23 18:20:18 +00005711 for (unsigned N = Record[Idx++]; N; --N)
5712 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5713
Vedant Kumar48b4f762018-04-14 01:40:48 +00005714 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00005715 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5716 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00005717
Ben Langmuird4a667a2015-06-23 18:20:23 +00005718 LangOpts.CurrentModule = ReadString(Record, Idx);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00005719
5720 // Comment options.
5721 for (unsigned N = Record[Idx++]; N; --N) {
5722 LangOpts.CommentOpts.BlockCommandNames.push_back(
5723 ReadString(Record, Idx));
5724 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00005725 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00005726
Samuel Antaoee8fb302016-01-06 13:42:12 +00005727 // OpenMP offloading options.
5728 for (unsigned N = Record[Idx++]; N; --N) {
5729 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5730 }
5731
5732 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5733
Richard Smith1e2cf0d2014-10-31 02:28:58 +00005734 return Listener.ReadLanguageOptions(LangOpts, Complain,
5735 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00005736}
5737
Chandler Carruth0d745bc2015-03-14 04:47:43 +00005738bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5739 ASTReaderListener &Listener,
5740 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005741 unsigned Idx = 0;
5742 TargetOptions TargetOpts;
5743 TargetOpts.Triple = ReadString(Record, Idx);
5744 TargetOpts.CPU = ReadString(Record, Idx);
5745 TargetOpts.ABI = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005746 for (unsigned N = Record[Idx++]; N; --N) {
5747 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5748 }
5749 for (unsigned N = Record[Idx++]; N; --N) {
5750 TargetOpts.Features.push_back(ReadString(Record, Idx));
5751 }
5752
Chandler Carruth0d745bc2015-03-14 04:47:43 +00005753 return Listener.ReadTargetOptions(TargetOpts, Complain,
5754 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00005755}
5756
5757bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5758 ASTReaderListener &Listener) {
Ben Langmuirb92de022014-04-29 16:25:26 +00005759 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
Guy Benyei11169dd2012-12-18 14:30:41 +00005760 unsigned Idx = 0;
Ben Langmuirb92de022014-04-29 16:25:26 +00005761#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00005762#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
Ben Langmuirb92de022014-04-29 16:25:26 +00005763 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00005764#include "clang/Basic/DiagnosticOptions.def"
5765
Richard Smith3be1cb22014-08-07 00:24:21 +00005766 for (unsigned N = Record[Idx++]; N; --N)
Ben Langmuirb92de022014-04-29 16:25:26 +00005767 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
Richard Smith3be1cb22014-08-07 00:24:21 +00005768 for (unsigned N = Record[Idx++]; N; --N)
5769 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005770
5771 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5772}
5773
5774bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5775 ASTReaderListener &Listener) {
5776 FileSystemOptions FSOpts;
5777 unsigned Idx = 0;
5778 FSOpts.WorkingDir = ReadString(Record, Idx);
5779 return Listener.ReadFileSystemOptions(FSOpts, Complain);
5780}
5781
5782bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5783 bool Complain,
5784 ASTReaderListener &Listener) {
5785 HeaderSearchOptions HSOpts;
5786 unsigned Idx = 0;
5787 HSOpts.Sysroot = ReadString(Record, Idx);
5788
5789 // Include entries.
5790 for (unsigned N = Record[Idx++]; N; --N) {
5791 std::string Path = ReadString(Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00005792 frontend::IncludeDirGroup Group
5793 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00005794 bool IsFramework = Record[Idx++];
5795 bool IgnoreSysRoot = Record[Idx++];
Benjamin Kramer3204b152015-05-29 19:42:19 +00005796 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5797 IgnoreSysRoot);
Guy Benyei11169dd2012-12-18 14:30:41 +00005798 }
5799
5800 // System header prefixes.
5801 for (unsigned N = Record[Idx++]; N; --N) {
5802 std::string Prefix = ReadString(Record, Idx);
5803 bool IsSystemHeader = Record[Idx++];
Benjamin Kramer3204b152015-05-29 19:42:19 +00005804 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
Guy Benyei11169dd2012-12-18 14:30:41 +00005805 }
5806
5807 HSOpts.ResourceDir = ReadString(Record, Idx);
5808 HSOpts.ModuleCachePath = ReadString(Record, Idx);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00005809 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005810 HSOpts.DisableModuleHash = Record[Idx++];
Richard Smith18934752017-06-06 00:32:01 +00005811 HSOpts.ImplicitModuleMaps = Record[Idx++];
5812 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00005813 HSOpts.UseBuiltinIncludes = Record[Idx++];
5814 HSOpts.UseStandardSystemIncludes = Record[Idx++];
5815 HSOpts.UseStandardCXXIncludes = Record[Idx++];
5816 HSOpts.UseLibcxx = Record[Idx++];
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005817 std::string SpecificModuleCachePath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005818
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005819 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5820 Complain);
Guy Benyei11169dd2012-12-18 14:30:41 +00005821}
5822
5823bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5824 bool Complain,
5825 ASTReaderListener &Listener,
5826 std::string &SuggestedPredefines) {
5827 PreprocessorOptions PPOpts;
5828 unsigned Idx = 0;
5829
5830 // Macro definitions/undefs
5831 for (unsigned N = Record[Idx++]; N; --N) {
5832 std::string Macro = ReadString(Record, Idx);
5833 bool IsUndef = Record[Idx++];
5834 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5835 }
5836
5837 // Includes
5838 for (unsigned N = Record[Idx++]; N; --N) {
5839 PPOpts.Includes.push_back(ReadString(Record, Idx));
5840 }
5841
5842 // Macro Includes
5843 for (unsigned N = Record[Idx++]; N; --N) {
5844 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5845 }
5846
5847 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00005848 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00005849 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005850 PPOpts.ObjCXXARCStandardLibrary =
5851 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5852 SuggestedPredefines.clear();
5853 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5854 SuggestedPredefines);
5855}
5856
5857std::pair<ModuleFile *, unsigned>
5858ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5859 GlobalPreprocessedEntityMapType::iterator
5860 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005861 assert(I != GlobalPreprocessedEntityMap.end() &&
Guy Benyei11169dd2012-12-18 14:30:41 +00005862 "Corrupted global preprocessed entity map");
5863 ModuleFile *M = I->second;
5864 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5865 return std::make_pair(M, LocalIndex);
5866}
5867
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00005868llvm::iterator_range<PreprocessingRecord::iterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00005869ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5870 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5871 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5872 Mod.NumPreprocessedEntities);
5873
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00005874 return llvm::make_range(PreprocessingRecord::iterator(),
5875 PreprocessingRecord::iterator());
Guy Benyei11169dd2012-12-18 14:30:41 +00005876}
5877
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00005878llvm::iterator_range<ASTReader::ModuleDeclIterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00005879ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00005880 return llvm::make_range(
5881 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5882 ModuleDeclIterator(this, &Mod,
5883 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
Guy Benyei11169dd2012-12-18 14:30:41 +00005884}
5885
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00005886SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5887 auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5888 assert(I != GlobalSkippedRangeMap.end() &&
5889 "Corrupted global skipped range map");
5890 ModuleFile *M = I->second;
5891 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5892 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5893 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5894 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5895 TranslateSourceLocation(*M, RawRange.getEnd()));
5896 assert(Range.isValid());
5897 return Range;
5898}
5899
Guy Benyei11169dd2012-12-18 14:30:41 +00005900PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5901 PreprocessedEntityID PPID = Index+1;
5902 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5903 ModuleFile &M = *PPInfo.first;
5904 unsigned LocalIndex = PPInfo.second;
5905 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5906
Guy Benyei11169dd2012-12-18 14:30:41 +00005907 if (!PP.getPreprocessingRecord()) {
5908 Error("no preprocessing record");
Craig Toppera13603a2014-05-22 05:54:18 +00005909 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005910 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005911
5912 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Dmitry Polukhin30d59462020-03-21 13:52:51 -07005913 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5914 M.MacroOffsetsBase + PPOffs.BitOffset)) {
JF Bastien0e828952019-06-26 19:50:12 +00005915 Error(std::move(Err));
5916 return nullptr;
5917 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005918
JF Bastien0e828952019-06-26 19:50:12 +00005919 Expected<llvm::BitstreamEntry> MaybeEntry =
5920 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5921 if (!MaybeEntry) {
5922 Error(MaybeEntry.takeError());
5923 return nullptr;
5924 }
5925 llvm::BitstreamEntry Entry = MaybeEntry.get();
5926
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005927 if (Entry.Kind != llvm::BitstreamEntry::Record)
Craig Toppera13603a2014-05-22 05:54:18 +00005928 return nullptr;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005929
Guy Benyei11169dd2012-12-18 14:30:41 +00005930 // Read the record.
Richard Smithcb34bd32016-03-27 07:28:06 +00005931 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5932 TranslateSourceLocation(M, PPOffs.getEnd()));
Guy Benyei11169dd2012-12-18 14:30:41 +00005933 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005934 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00005935 RecordData Record;
JF Bastien0e828952019-06-26 19:50:12 +00005936 Expected<unsigned> MaybeRecType =
5937 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5938 if (!MaybeRecType) {
5939 Error(MaybeRecType.takeError());
5940 return nullptr;
5941 }
5942 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005943 case PPD_MACRO_EXPANSION: {
5944 bool isBuiltin = Record[0];
Craig Toppera13603a2014-05-22 05:54:18 +00005945 IdentifierInfo *Name = nullptr;
Richard Smith66a81862015-05-04 02:25:31 +00005946 MacroDefinitionRecord *Def = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005947 if (isBuiltin)
5948 Name = getLocalIdentifier(M, Record[1]);
5949 else {
Richard Smith66a81862015-05-04 02:25:31 +00005950 PreprocessedEntityID GlobalID =
5951 getGlobalPreprocessedEntityID(M, Record[1]);
5952 Def = cast<MacroDefinitionRecord>(
5953 PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
Guy Benyei11169dd2012-12-18 14:30:41 +00005954 }
5955
5956 MacroExpansion *ME;
5957 if (isBuiltin)
5958 ME = new (PPRec) MacroExpansion(Name, Range);
5959 else
5960 ME = new (PPRec) MacroExpansion(Def, Range);
5961
5962 return ME;
5963 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005964
Guy Benyei11169dd2012-12-18 14:30:41 +00005965 case PPD_MACRO_DEFINITION: {
5966 // Decode the identifier info and then check again; if the macro is
5967 // still defined and associated with the identifier,
5968 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Vedant Kumar48b4f762018-04-14 01:40:48 +00005969 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00005970
5971 if (DeserializationListener)
5972 DeserializationListener->MacroDefinitionRead(PPID, MD);
5973
5974 return MD;
5975 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005976
Guy Benyei11169dd2012-12-18 14:30:41 +00005977 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00005978 const char *FullFileNameStart = Blob.data() + Record[0];
5979 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Craig Toppera13603a2014-05-22 05:54:18 +00005980 const FileEntry *File = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005981 if (!FullFileName.empty())
Harlan Haskins8d323d12019-08-01 21:31:56 +00005982 if (auto FE = PP.getFileManager().getFile(FullFileName))
5983 File = *FE;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005984
Guy Benyei11169dd2012-12-18 14:30:41 +00005985 // FIXME: Stable encoding
Vedant Kumar48b4f762018-04-14 01:40:48 +00005986 InclusionDirective::InclusionKind Kind
5987 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
5988 InclusionDirective *ID
5989 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e6c9402013-01-20 02:38:54 +00005990 StringRef(Blob.data(), Record[0]),
Guy Benyei11169dd2012-12-18 14:30:41 +00005991 Record[1], Record[3],
5992 File,
5993 Range);
5994 return ID;
5995 }
5996 }
5997
5998 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
5999}
6000
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006001/// Find the next module that contains entities and return the ID
Guy Benyei11169dd2012-12-18 14:30:41 +00006002/// of the first entry.
NAKAMURA Takumi12ab07e2017-10-12 09:42:14 +00006003///
6004/// \param SLocMapI points at a chunk of a module that contains no
6005/// preprocessed entities or the entities it contains are not the ones we are
6006/// looking for.
Guy Benyei11169dd2012-12-18 14:30:41 +00006007PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6008 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6009 ++SLocMapI;
6010 for (GlobalSLocOffsetMapType::const_iterator
6011 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6012 ModuleFile &M = *SLocMapI->second;
6013 if (M.NumPreprocessedEntities)
6014 return M.BasePreprocessedEntityID;
6015 }
6016
6017 return getTotalNumPreprocessedEntities();
6018}
6019
6020namespace {
6021
Guy Benyei11169dd2012-12-18 14:30:41 +00006022struct PPEntityComp {
6023 const ASTReader &Reader;
6024 ModuleFile &M;
6025
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006026 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
Guy Benyei11169dd2012-12-18 14:30:41 +00006027
6028 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6029 SourceLocation LHS = getLoc(L);
6030 SourceLocation RHS = getLoc(R);
6031 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6032 }
6033
6034 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6035 SourceLocation LHS = getLoc(L);
6036 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6037 }
6038
6039 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6040 SourceLocation RHS = getLoc(R);
6041 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6042 }
6043
6044 SourceLocation getLoc(const PPEntityOffset &PPE) const {
Richard Smithb22a1d12016-03-27 20:13:24 +00006045 return Reader.TranslateSourceLocation(M, PPE.getBegin());
Guy Benyei11169dd2012-12-18 14:30:41 +00006046 }
6047};
6048
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006049} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00006050
Alp Toker2e9ce4c2014-05-16 18:59:21 +00006051PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6052 bool EndsAfter) const {
6053 if (SourceMgr.isLocalSourceLocation(Loc))
Guy Benyei11169dd2012-12-18 14:30:41 +00006054 return getTotalNumPreprocessedEntities();
6055
Alp Toker2e9ce4c2014-05-16 18:59:21 +00006056 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6057 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00006058 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6059 "Corrupted global sloc offset map");
6060
6061 if (SLocMapI->second->NumPreprocessedEntities == 0)
6062 return findNextPreprocessedEntity(SLocMapI);
6063
6064 ModuleFile &M = *SLocMapI->second;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006065
6066 using pp_iterator = const PPEntityOffset *;
6067
Guy Benyei11169dd2012-12-18 14:30:41 +00006068 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6069 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6070
6071 size_t Count = M.NumPreprocessedEntities;
6072 size_t Half;
6073 pp_iterator First = pp_begin;
6074 pp_iterator PPI;
6075
Alp Toker2e9ce4c2014-05-16 18:59:21 +00006076 if (EndsAfter) {
6077 PPI = std::upper_bound(pp_begin, pp_end, Loc,
Richard Smithb22a1d12016-03-27 20:13:24 +00006078 PPEntityComp(*this, M));
Alp Toker2e9ce4c2014-05-16 18:59:21 +00006079 } else {
6080 // Do a binary search manually instead of using std::lower_bound because
6081 // The end locations of entities may be unordered (when a macro expansion
6082 // is inside another macro argument), but for this case it is not important
6083 // whether we get the first macro expansion or its containing macro.
6084 while (Count > 0) {
6085 Half = Count / 2;
6086 PPI = First;
6087 std::advance(PPI, Half);
Richard Smithb22a1d12016-03-27 20:13:24 +00006088 if (SourceMgr.isBeforeInTranslationUnit(
6089 TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
Alp Toker2e9ce4c2014-05-16 18:59:21 +00006090 First = PPI;
6091 ++First;
6092 Count = Count - Half - 1;
6093 } else
6094 Count = Half;
6095 }
Guy Benyei11169dd2012-12-18 14:30:41 +00006096 }
6097
6098 if (PPI == pp_end)
6099 return findNextPreprocessedEntity(SLocMapI);
6100
6101 return M.BasePreprocessedEntityID + (PPI - pp_begin);
6102}
6103
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006104/// Returns a pair of [Begin, End) indices of preallocated
Guy Benyei11169dd2012-12-18 14:30:41 +00006105/// preprocessed entities that \arg Range encompasses.
6106std::pair<unsigned, unsigned>
6107 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6108 if (Range.isInvalid())
6109 return std::make_pair(0,0);
6110 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6111
Alp Toker2e9ce4c2014-05-16 18:59:21 +00006112 PreprocessedEntityID BeginID =
6113 findPreprocessedEntity(Range.getBegin(), false);
6114 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
Guy Benyei11169dd2012-12-18 14:30:41 +00006115 return std::make_pair(BeginID, EndID);
6116}
6117
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006118/// Optionally returns true or false if the preallocated preprocessed
Guy Benyei11169dd2012-12-18 14:30:41 +00006119/// entity with index \arg Index came from file \arg FID.
David Blaikie05785d12013-02-20 22:23:23 +00006120Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei11169dd2012-12-18 14:30:41 +00006121 FileID FID) {
6122 if (FID.isInvalid())
6123 return false;
6124
6125 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6126 ModuleFile &M = *PPInfo.first;
6127 unsigned LocalIndex = PPInfo.second;
6128 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006129
Richard Smithcb34bd32016-03-27 07:28:06 +00006130 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
Guy Benyei11169dd2012-12-18 14:30:41 +00006131 if (Loc.isInvalid())
6132 return false;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006133
Guy Benyei11169dd2012-12-18 14:30:41 +00006134 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6135 return true;
6136 else
6137 return false;
6138}
6139
6140namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006141
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006142 /// Visitor used to search for information about a header file.
Guy Benyei11169dd2012-12-18 14:30:41 +00006143 class HeaderFileInfoVisitor {
Guy Benyei11169dd2012-12-18 14:30:41 +00006144 const FileEntry *FE;
David Blaikie05785d12013-02-20 22:23:23 +00006145 Optional<HeaderFileInfo> HFI;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006146
Guy Benyei11169dd2012-12-18 14:30:41 +00006147 public:
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006148 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00006149
6150 bool operator()(ModuleFile &M) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00006151 HeaderFileInfoLookupTable *Table
6152 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
Guy Benyei11169dd2012-12-18 14:30:41 +00006153 if (!Table)
6154 return false;
6155
6156 // Look in the on-disk hash table for an entry for this file name.
Richard Smithbdf2d932015-07-30 03:37:16 +00006157 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00006158 if (Pos == Table->end())
6159 return false;
6160
Richard Smithbdf2d932015-07-30 03:37:16 +00006161 HFI = *Pos;
Guy Benyei11169dd2012-12-18 14:30:41 +00006162 return true;
6163 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006164
David Blaikie05785d12013-02-20 22:23:23 +00006165 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei11169dd2012-12-18 14:30:41 +00006166 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006167
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006168} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00006169
6170HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00006171 HeaderFileInfoVisitor Visitor(FE);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00006172 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis1054bbf2013-05-08 23:46:55 +00006173 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei11169dd2012-12-18 14:30:41 +00006174 return *HFI;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006175
Guy Benyei11169dd2012-12-18 14:30:41 +00006176 return HeaderFileInfo();
6177}
6178
6179void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Richard Smithd230de22017-01-26 01:01:01 +00006180 using DiagState = DiagnosticsEngine::DiagState;
6181 SmallVector<DiagState *, 32> DiagStates;
6182
Vedant Kumar48b4f762018-04-14 01:40:48 +00006183 for (ModuleFile &F : ModuleMgr) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006184 unsigned Idx = 0;
Richard Smithd230de22017-01-26 01:01:01 +00006185 auto &Record = F.PragmaDiagMappings;
6186 if (Record.empty())
6187 continue;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006188
Richard Smithd230de22017-01-26 01:01:01 +00006189 DiagStates.clear();
6190
6191 auto ReadDiagState =
6192 [&](const DiagState &BasedOn, SourceLocation Loc,
6193 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6194 unsigned BackrefID = Record[Idx++];
6195 if (BackrefID != 0)
6196 return DiagStates[BackrefID - 1];
6197
Guy Benyei11169dd2012-12-18 14:30:41 +00006198 // A new DiagState was created here.
Richard Smithd230de22017-01-26 01:01:01 +00006199 Diag.DiagStates.push_back(BasedOn);
6200 DiagState *NewState = &Diag.DiagStates.back();
Guy Benyei11169dd2012-12-18 14:30:41 +00006201 DiagStates.push_back(NewState);
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00006202 unsigned Size = Record[Idx++];
6203 assert(Idx + Size * 2 <= Record.size() &&
6204 "Invalid data, not enough diag/map pairs");
6205 while (Size--) {
Richard Smithd230de22017-01-26 01:01:01 +00006206 unsigned DiagID = Record[Idx++];
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006207 DiagnosticMapping NewMapping =
Richard Smithe37391c2017-05-03 00:28:49 +00006208 DiagnosticMapping::deserialize(Record[Idx++]);
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006209 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6210 continue;
6211
6212 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6213
6214 // If this mapping was specified as a warning but the severity was
6215 // upgraded due to diagnostic settings, simulate the current diagnostic
6216 // settings (and use a warning).
Richard Smithe37391c2017-05-03 00:28:49 +00006217 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6218 NewMapping.setSeverity(diag::Severity::Warning);
6219 NewMapping.setUpgradedFromWarning(false);
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006220 }
6221
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006222 Mapping = NewMapping;
Richard Smithd230de22017-01-26 01:01:01 +00006223 }
Richard Smithd230de22017-01-26 01:01:01 +00006224 return NewState;
6225 };
6226
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00006227 // Read the first state.
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006228 DiagState *FirstState;
6229 if (F.Kind == MK_ImplicitModule) {
6230 // Implicitly-built modules are reused with different diagnostic
6231 // settings. Use the initial diagnostic state from Diag to simulate this
6232 // compilation's diagnostic settings.
6233 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6234 DiagStates.push_back(FirstState);
6235
6236 // Skip the initial diagnostic state from the serialized module.
Richard Smithe37391c2017-05-03 00:28:49 +00006237 assert(Record[1] == 0 &&
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006238 "Invalid data, unexpected backref in initial state");
Richard Smithe37391c2017-05-03 00:28:49 +00006239 Idx = 3 + Record[2] * 2;
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006240 assert(Idx < Record.size() &&
6241 "Invalid data, not enough state change pairs in initial state");
Richard Smithe37391c2017-05-03 00:28:49 +00006242 } else if (F.isModule()) {
6243 // For an explicit module, preserve the flags from the module build
6244 // command line (-w, -Weverything, -Werror, ...) along with any explicit
6245 // -Wblah flags.
6246 unsigned Flags = Record[Idx++];
6247 DiagState Initial;
6248 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6249 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6250 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6251 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6252 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6253 Initial.ExtBehavior = (diag::Severity)Flags;
6254 FirstState = ReadDiagState(Initial, SourceLocation(), true);
Richard Smithea741482017-05-01 22:10:47 +00006255
Richard Smith6c2b5a82018-02-09 01:15:13 +00006256 assert(F.OriginalSourceFileID.isValid());
6257
Richard Smithe37391c2017-05-03 00:28:49 +00006258 // Set up the root buffer of the module to start with the initial
6259 // diagnostic state of the module itself, to cover files that contain no
6260 // explicit transitions (for which we did not serialize anything).
6261 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6262 .StateTransitions.push_back({FirstState, 0});
6263 } else {
6264 // For prefix ASTs, start with whatever the user configured on the
6265 // command line.
6266 Idx++; // Skip flags.
6267 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6268 SourceLocation(), false);
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006269 }
Richard Smithd230de22017-01-26 01:01:01 +00006270
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00006271 // Read the state transitions.
6272 unsigned NumLocations = Record[Idx++];
6273 while (NumLocations--) {
6274 assert(Idx < Record.size() &&
6275 "Invalid data, missing pragma diagnostic states");
Richard Smithd230de22017-01-26 01:01:01 +00006276 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6277 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
Richard Smith6c2b5a82018-02-09 01:15:13 +00006278 assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
Richard Smithd230de22017-01-26 01:01:01 +00006279 assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6280 unsigned Transitions = Record[Idx++];
6281
6282 // Note that we don't need to set up Parent/ParentOffset here, because
6283 // we won't be changing the diagnostic state within imported FileIDs
6284 // (other than perhaps appending to the main source file, which has no
6285 // parent).
6286 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6287 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6288 for (unsigned I = 0; I != Transitions; ++I) {
6289 unsigned Offset = Record[Idx++];
6290 auto *State =
6291 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6292 F.StateTransitions.push_back({State, Offset});
Guy Benyei11169dd2012-12-18 14:30:41 +00006293 }
6294 }
Richard Smithd230de22017-01-26 01:01:01 +00006295
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00006296 // Read the final state.
6297 assert(Idx < Record.size() &&
6298 "Invalid data, missing final pragma diagnostic state");
6299 SourceLocation CurStateLoc =
6300 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6301 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6302
6303 if (!F.isModule()) {
6304 Diag.DiagStatesByLoc.CurDiagState = CurState;
6305 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6306
6307 // Preserve the property that the imaginary root file describes the
6308 // current state.
Simon Pilgrim22518632017-10-10 13:56:17 +00006309 FileID NullFile;
6310 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00006311 if (T.empty())
6312 T.push_back({CurState, 0});
6313 else
6314 T[0].State = CurState;
6315 }
6316
Richard Smithd230de22017-01-26 01:01:01 +00006317 // Don't try to read these mappings again.
6318 Record.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006319 }
6320}
6321
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006322/// Get the correct cursor and offset for loading a type.
Guy Benyei11169dd2012-12-18 14:30:41 +00006323ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6324 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6325 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6326 ModuleFile *M = I->second;
6327 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
6328}
6329
John McCalld505e572019-12-13 21:54:44 -05006330static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6331 switch (code) {
6332#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6333 case TYPE_##CODE_ID: return Type::CLASS_ID;
6334#include "clang/Serialization/TypeBitCodes.def"
6335 default: return llvm::None;
6336 }
John McCall3ce3d232019-12-13 03:37:23 -05006337}
6338
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006339/// Read and return the type with the given index..
Guy Benyei11169dd2012-12-18 14:30:41 +00006340///
6341/// The index is the type ID, shifted and minus the number of predefs. This
6342/// routine actually reads the record corresponding to the type at the given
6343/// location. It is a helper routine for GetType, which deals with reading type
6344/// IDs.
6345QualType ASTReader::readTypeRecord(unsigned Index) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00006346 assert(ContextObj && "reading type with no AST context");
6347 ASTContext &Context = *ContextObj;
Guy Benyei11169dd2012-12-18 14:30:41 +00006348 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00006349 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00006350
6351 // Keep track of where we are in the stream, then jump back there
6352 // after reading this type.
6353 SavedStreamPosition SavedPosition(DeclsCursor);
6354
6355 ReadingKindTracker ReadingKind(Read_Type, *this);
6356
6357 // Note that we are loading a type record.
6358 Deserializing AType(this);
6359
JF Bastien0e828952019-06-26 19:50:12 +00006360 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6361 Error(std::move(Err));
6362 return QualType();
6363 }
John McCall3ce3d232019-12-13 03:37:23 -05006364 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6365 if (!RawCode) {
6366 Error(RawCode.takeError());
JF Bastien0e828952019-06-26 19:50:12 +00006367 return QualType();
6368 }
JF Bastien0e828952019-06-26 19:50:12 +00006369
John McCall3ce3d232019-12-13 03:37:23 -05006370 ASTRecordReader Record(*this, *Loc.F);
6371 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6372 if (!Code) {
6373 Error(Code.takeError());
JF Bastien0e828952019-06-26 19:50:12 +00006374 return QualType();
6375 }
John McCalld505e572019-12-13 21:54:44 -05006376 if (Code.get() == TYPE_EXT_QUAL) {
6377 QualType baseType = Record.readQualType();
6378 Qualifiers quals = Record.readQualifiers();
6379 return Context.getQualifiedType(baseType, quals);
Guy Benyei11169dd2012-12-18 14:30:41 +00006380 }
6381
John McCalld505e572019-12-13 21:54:44 -05006382 auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6383 if (!maybeClass) {
6384 Error("Unexpected code for type");
6385 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006386 }
6387
John McCalld505e572019-12-13 21:54:44 -05006388 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6389 return TypeReader.read(*maybeClass);
Richard Smith564417a2014-03-20 21:47:22 +00006390}
6391
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006392namespace clang {
6393
6394class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
John McCall3ce3d232019-12-13 03:37:23 -05006395 ASTRecordReader &Reader;
Guy Benyei11169dd2012-12-18 14:30:41 +00006396
John McCall3ce3d232019-12-13 03:37:23 -05006397 SourceLocation readSourceLocation() {
6398 return Reader.readSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00006399 }
6400
6401 TypeSourceInfo *GetTypeSourceInfo() {
John McCall3ce3d232019-12-13 03:37:23 -05006402 return Reader.readTypeSourceInfo();
David L. Jonesbe1557a2016-12-21 00:17:49 +00006403 }
6404
6405 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
John McCall3ce3d232019-12-13 03:37:23 -05006406 return Reader.readNestedNameSpecifierLoc();
Guy Benyei11169dd2012-12-18 14:30:41 +00006407 }
6408
Richard Smithe43e2b32018-08-20 21:47:29 +00006409 Attr *ReadAttr() {
John McCall3ce3d232019-12-13 03:37:23 -05006410 return Reader.readAttr();
Richard Smithe43e2b32018-08-20 21:47:29 +00006411 }
6412
Guy Benyei11169dd2012-12-18 14:30:41 +00006413public:
John McCall3ce3d232019-12-13 03:37:23 -05006414 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
Guy Benyei11169dd2012-12-18 14:30:41 +00006415
6416 // We want compile-time assurance that we've enumerated all of
6417 // these, so unfortunately we have to declare them first, then
6418 // define them out-of-line.
6419#define ABSTRACT_TYPELOC(CLASS, PARENT)
6420#define TYPELOC(CLASS, PARENT) \
6421 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6422#include "clang/AST/TypeLocNodes.def"
6423
6424 void VisitFunctionTypeLoc(FunctionTypeLoc);
6425 void VisitArrayTypeLoc(ArrayTypeLoc);
6426};
6427
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006428} // namespace clang
6429
Guy Benyei11169dd2012-12-18 14:30:41 +00006430void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6431 // nothing to do
6432}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006433
Guy Benyei11169dd2012-12-18 14:30:41 +00006434void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006435 TL.setBuiltinLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006436 if (TL.needsExtraLocalData()) {
John McCall3ce3d232019-12-13 03:37:23 -05006437 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6438 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt()));
6439 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt()));
6440 TL.setModeAttr(Reader.readInt());
Guy Benyei11169dd2012-12-18 14:30:41 +00006441 }
6442}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006443
Guy Benyei11169dd2012-12-18 14:30:41 +00006444void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006445 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006446}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006447
Guy Benyei11169dd2012-12-18 14:30:41 +00006448void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006449 TL.setStarLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006450}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006451
Reid Kleckner8a365022013-06-24 17:51:48 +00006452void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6453 // nothing to do
6454}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006455
Reid Kleckner0503a872013-12-05 01:23:43 +00006456void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6457 // nothing to do
6458}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006459
Leonard Chanc72aaf62019-05-07 03:20:17 +00006460void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006461 TL.setExpansionLoc(readSourceLocation());
Leonard Chanc72aaf62019-05-07 03:20:17 +00006462}
6463
Guy Benyei11169dd2012-12-18 14:30:41 +00006464void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006465 TL.setCaretLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006466}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006467
Guy Benyei11169dd2012-12-18 14:30:41 +00006468void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006469 TL.setAmpLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006470}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006471
Guy Benyei11169dd2012-12-18 14:30:41 +00006472void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006473 TL.setAmpAmpLoc(readSourceLocation());
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::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006477 TL.setStarLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006478 TL.setClassTInfo(GetTypeSourceInfo());
Guy Benyei11169dd2012-12-18 14:30:41 +00006479}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006480
Guy Benyei11169dd2012-12-18 14:30:41 +00006481void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006482 TL.setLBracketLoc(readSourceLocation());
6483 TL.setRBracketLoc(readSourceLocation());
6484 if (Reader.readBool())
6485 TL.setSizeExpr(Reader.readExpr());
Guy Benyei11169dd2012-12-18 14:30:41 +00006486 else
Craig Toppera13603a2014-05-22 05:54:18 +00006487 TL.setSizeExpr(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006488}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006489
Guy Benyei11169dd2012-12-18 14:30:41 +00006490void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6491 VisitArrayTypeLoc(TL);
6492}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006493
Guy Benyei11169dd2012-12-18 14:30:41 +00006494void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6495 VisitArrayTypeLoc(TL);
6496}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006497
Guy Benyei11169dd2012-12-18 14:30:41 +00006498void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6499 VisitArrayTypeLoc(TL);
6500}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006501
Guy Benyei11169dd2012-12-18 14:30:41 +00006502void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6503 DependentSizedArrayTypeLoc TL) {
6504 VisitArrayTypeLoc(TL);
6505}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006506
Andrew Gozillon572bbb02017-10-02 06:25:51 +00006507void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6508 DependentAddressSpaceTypeLoc TL) {
6509
John McCall3ce3d232019-12-13 03:37:23 -05006510 TL.setAttrNameLoc(readSourceLocation());
6511 TL.setAttrOperandParensRange(Reader.readSourceRange());
6512 TL.setAttrExprOperand(Reader.readExpr());
Andrew Gozillon572bbb02017-10-02 06:25:51 +00006513}
6514
Guy Benyei11169dd2012-12-18 14:30:41 +00006515void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6516 DependentSizedExtVectorTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006517 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006518}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006519
Guy Benyei11169dd2012-12-18 14:30:41 +00006520void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006521 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006522}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006523
Erich Keanef702b022018-07-13 19:46:04 +00006524void TypeLocReader::VisitDependentVectorTypeLoc(
6525 DependentVectorTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006526 TL.setNameLoc(readSourceLocation());
Erich Keanef702b022018-07-13 19:46:04 +00006527}
6528
Guy Benyei11169dd2012-12-18 14:30:41 +00006529void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006530 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006531}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006532
Guy Benyei11169dd2012-12-18 14:30:41 +00006533void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006534 TL.setLocalRangeBegin(readSourceLocation());
6535 TL.setLParenLoc(readSourceLocation());
6536 TL.setRParenLoc(readSourceLocation());
6537 TL.setExceptionSpecRange(Reader.readSourceRange());
6538 TL.setLocalRangeEnd(readSourceLocation());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00006539 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
John McCall3ce3d232019-12-13 03:37:23 -05006540 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
Guy Benyei11169dd2012-12-18 14:30:41 +00006541 }
6542}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006543
Guy Benyei11169dd2012-12-18 14:30:41 +00006544void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6545 VisitFunctionTypeLoc(TL);
6546}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006547
Guy Benyei11169dd2012-12-18 14:30:41 +00006548void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6549 VisitFunctionTypeLoc(TL);
6550}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006551
Guy Benyei11169dd2012-12-18 14:30:41 +00006552void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006553 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006554}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006555
Guy Benyei11169dd2012-12-18 14:30:41 +00006556void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006557 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006558}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006559
Guy Benyei11169dd2012-12-18 14:30:41 +00006560void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006561 TL.setTypeofLoc(readSourceLocation());
6562 TL.setLParenLoc(readSourceLocation());
6563 TL.setRParenLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006564}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006565
Guy Benyei11169dd2012-12-18 14:30:41 +00006566void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006567 TL.setTypeofLoc(readSourceLocation());
6568 TL.setLParenLoc(readSourceLocation());
6569 TL.setRParenLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006570 TL.setUnderlyingTInfo(GetTypeSourceInfo());
Guy Benyei11169dd2012-12-18 14:30:41 +00006571}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006572
Guy Benyei11169dd2012-12-18 14:30:41 +00006573void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006574 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006575}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006576
Guy Benyei11169dd2012-12-18 14:30:41 +00006577void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006578 TL.setKWLoc(readSourceLocation());
6579 TL.setLParenLoc(readSourceLocation());
6580 TL.setRParenLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006581 TL.setUnderlyingTInfo(GetTypeSourceInfo());
Guy Benyei11169dd2012-12-18 14:30:41 +00006582}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006583
Guy Benyei11169dd2012-12-18 14:30:41 +00006584void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006585 TL.setNameLoc(readSourceLocation());
Saar Razb481f022020-01-22 02:03:05 +02006586 if (Reader.readBool()) {
6587 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6588 TL.setTemplateKWLoc(readSourceLocation());
6589 TL.setConceptNameLoc(readSourceLocation());
6590 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6591 TL.setLAngleLoc(readSourceLocation());
6592 TL.setRAngleLoc(readSourceLocation());
6593 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6594 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6595 TL.getTypePtr()->getArg(i).getKind()));
6596 }
Guy Benyei11169dd2012-12-18 14:30:41 +00006597}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006598
Richard Smith600b5262017-01-26 20:40:47 +00006599void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6600 DeducedTemplateSpecializationTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006601 TL.setTemplateNameLoc(readSourceLocation());
Richard Smith600b5262017-01-26 20:40:47 +00006602}
6603
Guy Benyei11169dd2012-12-18 14:30:41 +00006604void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006605 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006606}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006607
Guy Benyei11169dd2012-12-18 14:30:41 +00006608void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006609 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006610}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006611
Guy Benyei11169dd2012-12-18 14:30:41 +00006612void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smithe43e2b32018-08-20 21:47:29 +00006613 TL.setAttr(ReadAttr());
Guy Benyei11169dd2012-12-18 14:30:41 +00006614}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006615
Guy Benyei11169dd2012-12-18 14:30:41 +00006616void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006617 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006618}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006619
Guy Benyei11169dd2012-12-18 14:30:41 +00006620void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6621 SubstTemplateTypeParmTypeLoc 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::VisitSubstTemplateTypeParmPackTypeLoc(
6626 SubstTemplateTypeParmPackTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006627 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006628}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006629
Guy Benyei11169dd2012-12-18 14:30:41 +00006630void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6631 TemplateSpecializationTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006632 TL.setTemplateKeywordLoc(readSourceLocation());
6633 TL.setTemplateNameLoc(readSourceLocation());
6634 TL.setLAngleLoc(readSourceLocation());
6635 TL.setRAngleLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006636 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
David L. Jonesbe1557a2016-12-21 00:17:49 +00006637 TL.setArgLocInfo(
6638 i,
John McCall3ce3d232019-12-13 03:37:23 -05006639 Reader.readTemplateArgumentLocInfo(
6640 TL.getTypePtr()->getArg(i).getKind()));
Guy Benyei11169dd2012-12-18 14:30:41 +00006641}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006642
Guy Benyei11169dd2012-12-18 14:30:41 +00006643void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006644 TL.setLParenLoc(readSourceLocation());
6645 TL.setRParenLoc(readSourceLocation());
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::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006649 TL.setElaboratedKeywordLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006650 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
Guy Benyei11169dd2012-12-18 14:30:41 +00006651}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006652
Guy Benyei11169dd2012-12-18 14:30:41 +00006653void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006654 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006655}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006656
Guy Benyei11169dd2012-12-18 14:30:41 +00006657void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006658 TL.setElaboratedKeywordLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006659 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
John McCall3ce3d232019-12-13 03:37:23 -05006660 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006661}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006662
Guy Benyei11169dd2012-12-18 14:30:41 +00006663void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6664 DependentTemplateSpecializationTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006665 TL.setElaboratedKeywordLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006666 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
John McCall3ce3d232019-12-13 03:37:23 -05006667 TL.setTemplateKeywordLoc(readSourceLocation());
6668 TL.setTemplateNameLoc(readSourceLocation());
6669 TL.setLAngleLoc(readSourceLocation());
6670 TL.setRAngleLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006671 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
David L. Jonesbe1557a2016-12-21 00:17:49 +00006672 TL.setArgLocInfo(
6673 I,
John McCall3ce3d232019-12-13 03:37:23 -05006674 Reader.readTemplateArgumentLocInfo(
6675 TL.getTypePtr()->getArg(I).getKind()));
Guy Benyei11169dd2012-12-18 14:30:41 +00006676}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006677
Guy Benyei11169dd2012-12-18 14:30:41 +00006678void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006679 TL.setEllipsisLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006680}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006681
Guy Benyei11169dd2012-12-18 14:30:41 +00006682void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006683 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006684}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006685
Manman Rene6be26c2016-09-13 17:25:08 +00006686void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6687 if (TL.getNumProtocols()) {
John McCall3ce3d232019-12-13 03:37:23 -05006688 TL.setProtocolLAngleLoc(readSourceLocation());
6689 TL.setProtocolRAngleLoc(readSourceLocation());
Manman Rene6be26c2016-09-13 17:25:08 +00006690 }
6691 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
John McCall3ce3d232019-12-13 03:37:23 -05006692 TL.setProtocolLoc(i, readSourceLocation());
Manman Rene6be26c2016-09-13 17:25:08 +00006693}
6694
Guy Benyei11169dd2012-12-18 14:30:41 +00006695void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006696 TL.setHasBaseTypeAsWritten(Reader.readBool());
6697 TL.setTypeArgsLAngleLoc(readSourceLocation());
6698 TL.setTypeArgsRAngleLoc(readSourceLocation());
Douglas Gregore9d95f12015-07-07 03:57:35 +00006699 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
David L. Jonesbe1557a2016-12-21 00:17:49 +00006700 TL.setTypeArgTInfo(i, GetTypeSourceInfo());
John McCall3ce3d232019-12-13 03:37:23 -05006701 TL.setProtocolLAngleLoc(readSourceLocation());
6702 TL.setProtocolRAngleLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006703 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
John McCall3ce3d232019-12-13 03:37:23 -05006704 TL.setProtocolLoc(i, readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006705}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006706
Guy Benyei11169dd2012-12-18 14:30:41 +00006707void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006708 TL.setStarLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006709}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006710
Guy Benyei11169dd2012-12-18 14:30:41 +00006711void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006712 TL.setKWLoc(readSourceLocation());
6713 TL.setLParenLoc(readSourceLocation());
6714 TL.setRParenLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006715}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006716
Xiuli Pan9c14e282016-01-09 12:53:17 +00006717void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006718 TL.setKWLoc(readSourceLocation());
Xiuli Pan9c14e282016-01-09 12:53:17 +00006719}
Guy Benyei11169dd2012-12-18 14:30:41 +00006720
John McCall3ce3d232019-12-13 03:37:23 -05006721void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6722 TypeLocReader TLR(*this);
Richard Smithc23d7342018-06-29 20:46:25 +00006723 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6724 TLR.Visit(TL);
6725}
6726
John McCall3ce3d232019-12-13 03:37:23 -05006727TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6728 QualType InfoTy = readType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006729 if (InfoTy.isNull())
Craig Toppera13603a2014-05-22 05:54:18 +00006730 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006731
6732 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
John McCall3ce3d232019-12-13 03:37:23 -05006733 readTypeLoc(TInfo->getTypeLoc());
Guy Benyei11169dd2012-12-18 14:30:41 +00006734 return TInfo;
6735}
6736
6737QualType ASTReader::GetType(TypeID ID) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00006738 assert(ContextObj && "reading type with no AST context");
6739 ASTContext &Context = *ContextObj;
6740
Guy Benyei11169dd2012-12-18 14:30:41 +00006741 unsigned FastQuals = ID & Qualifiers::FastMask;
6742 unsigned Index = ID >> Qualifiers::FastWidth;
6743
6744 if (Index < NUM_PREDEF_TYPE_IDS) {
6745 QualType T;
6746 switch ((PredefinedTypeIDs)Index) {
Alexey Baderbdf7c842015-09-15 12:18:29 +00006747 case PREDEF_TYPE_NULL_ID:
Vedant Kumar48b4f762018-04-14 01:40:48 +00006748 return QualType();
Alexey Baderbdf7c842015-09-15 12:18:29 +00006749 case PREDEF_TYPE_VOID_ID:
6750 T = Context.VoidTy;
6751 break;
6752 case PREDEF_TYPE_BOOL_ID:
6753 T = Context.BoolTy;
6754 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00006755 case PREDEF_TYPE_CHAR_U_ID:
6756 case PREDEF_TYPE_CHAR_S_ID:
6757 // FIXME: Check that the signedness of CharTy is correct!
6758 T = Context.CharTy;
6759 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006760 case PREDEF_TYPE_UCHAR_ID:
6761 T = Context.UnsignedCharTy;
6762 break;
6763 case PREDEF_TYPE_USHORT_ID:
6764 T = Context.UnsignedShortTy;
6765 break;
6766 case PREDEF_TYPE_UINT_ID:
6767 T = Context.UnsignedIntTy;
6768 break;
6769 case PREDEF_TYPE_ULONG_ID:
6770 T = Context.UnsignedLongTy;
6771 break;
6772 case PREDEF_TYPE_ULONGLONG_ID:
6773 T = Context.UnsignedLongLongTy;
6774 break;
6775 case PREDEF_TYPE_UINT128_ID:
6776 T = Context.UnsignedInt128Ty;
6777 break;
6778 case PREDEF_TYPE_SCHAR_ID:
6779 T = Context.SignedCharTy;
6780 break;
6781 case PREDEF_TYPE_WCHAR_ID:
6782 T = Context.WCharTy;
6783 break;
6784 case PREDEF_TYPE_SHORT_ID:
6785 T = Context.ShortTy;
6786 break;
6787 case PREDEF_TYPE_INT_ID:
6788 T = Context.IntTy;
6789 break;
6790 case PREDEF_TYPE_LONG_ID:
6791 T = Context.LongTy;
6792 break;
6793 case PREDEF_TYPE_LONGLONG_ID:
6794 T = Context.LongLongTy;
6795 break;
6796 case PREDEF_TYPE_INT128_ID:
6797 T = Context.Int128Ty;
6798 break;
6799 case PREDEF_TYPE_HALF_ID:
6800 T = Context.HalfTy;
6801 break;
6802 case PREDEF_TYPE_FLOAT_ID:
6803 T = Context.FloatTy;
6804 break;
6805 case PREDEF_TYPE_DOUBLE_ID:
6806 T = Context.DoubleTy;
6807 break;
6808 case PREDEF_TYPE_LONGDOUBLE_ID:
6809 T = Context.LongDoubleTy;
6810 break;
Leonard Chanf921d852018-06-04 16:07:52 +00006811 case PREDEF_TYPE_SHORT_ACCUM_ID:
6812 T = Context.ShortAccumTy;
6813 break;
6814 case PREDEF_TYPE_ACCUM_ID:
6815 T = Context.AccumTy;
6816 break;
6817 case PREDEF_TYPE_LONG_ACCUM_ID:
6818 T = Context.LongAccumTy;
6819 break;
6820 case PREDEF_TYPE_USHORT_ACCUM_ID:
6821 T = Context.UnsignedShortAccumTy;
6822 break;
6823 case PREDEF_TYPE_UACCUM_ID:
6824 T = Context.UnsignedAccumTy;
6825 break;
6826 case PREDEF_TYPE_ULONG_ACCUM_ID:
6827 T = Context.UnsignedLongAccumTy;
6828 break;
Leonard Chanab80f3c2018-06-14 14:53:51 +00006829 case PREDEF_TYPE_SHORT_FRACT_ID:
6830 T = Context.ShortFractTy;
6831 break;
6832 case PREDEF_TYPE_FRACT_ID:
6833 T = Context.FractTy;
6834 break;
6835 case PREDEF_TYPE_LONG_FRACT_ID:
6836 T = Context.LongFractTy;
6837 break;
6838 case PREDEF_TYPE_USHORT_FRACT_ID:
6839 T = Context.UnsignedShortFractTy;
6840 break;
6841 case PREDEF_TYPE_UFRACT_ID:
6842 T = Context.UnsignedFractTy;
6843 break;
6844 case PREDEF_TYPE_ULONG_FRACT_ID:
6845 T = Context.UnsignedLongFractTy;
6846 break;
6847 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6848 T = Context.SatShortAccumTy;
6849 break;
6850 case PREDEF_TYPE_SAT_ACCUM_ID:
6851 T = Context.SatAccumTy;
6852 break;
6853 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6854 T = Context.SatLongAccumTy;
6855 break;
6856 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6857 T = Context.SatUnsignedShortAccumTy;
6858 break;
6859 case PREDEF_TYPE_SAT_UACCUM_ID:
6860 T = Context.SatUnsignedAccumTy;
6861 break;
6862 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6863 T = Context.SatUnsignedLongAccumTy;
6864 break;
6865 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6866 T = Context.SatShortFractTy;
6867 break;
6868 case PREDEF_TYPE_SAT_FRACT_ID:
6869 T = Context.SatFractTy;
6870 break;
6871 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6872 T = Context.SatLongFractTy;
6873 break;
6874 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6875 T = Context.SatUnsignedShortFractTy;
6876 break;
6877 case PREDEF_TYPE_SAT_UFRACT_ID:
6878 T = Context.SatUnsignedFractTy;
6879 break;
6880 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6881 T = Context.SatUnsignedLongFractTy;
6882 break;
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00006883 case PREDEF_TYPE_FLOAT16_ID:
6884 T = Context.Float16Ty;
6885 break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00006886 case PREDEF_TYPE_FLOAT128_ID:
6887 T = Context.Float128Ty;
6888 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006889 case PREDEF_TYPE_OVERLOAD_ID:
6890 T = Context.OverloadTy;
6891 break;
6892 case PREDEF_TYPE_BOUND_MEMBER:
6893 T = Context.BoundMemberTy;
6894 break;
6895 case PREDEF_TYPE_PSEUDO_OBJECT:
6896 T = Context.PseudoObjectTy;
6897 break;
6898 case PREDEF_TYPE_DEPENDENT_ID:
6899 T = Context.DependentTy;
6900 break;
6901 case PREDEF_TYPE_UNKNOWN_ANY:
6902 T = Context.UnknownAnyTy;
6903 break;
6904 case PREDEF_TYPE_NULLPTR_ID:
6905 T = Context.NullPtrTy;
6906 break;
Richard Smith3a8244d2018-05-01 05:02:45 +00006907 case PREDEF_TYPE_CHAR8_ID:
6908 T = Context.Char8Ty;
6909 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006910 case PREDEF_TYPE_CHAR16_ID:
6911 T = Context.Char16Ty;
6912 break;
6913 case PREDEF_TYPE_CHAR32_ID:
6914 T = Context.Char32Ty;
6915 break;
6916 case PREDEF_TYPE_OBJC_ID:
6917 T = Context.ObjCBuiltinIdTy;
6918 break;
6919 case PREDEF_TYPE_OBJC_CLASS:
6920 T = Context.ObjCBuiltinClassTy;
6921 break;
6922 case PREDEF_TYPE_OBJC_SEL:
6923 T = Context.ObjCBuiltinSelTy;
6924 break;
Alexey Bader954ba212016-04-08 13:40:33 +00006925#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6926 case PREDEF_TYPE_##Id##_ID: \
6927 T = Context.SingletonId; \
Alexey Baderbdf7c842015-09-15 12:18:29 +00006928 break;
Alexey Baderb62f1442016-04-13 08:33:41 +00006929#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +00006930#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6931 case PREDEF_TYPE_##Id##_ID: \
6932 T = Context.Id##Ty; \
6933 break;
6934#include "clang/Basic/OpenCLExtensionTypes.def"
Alexey Baderbdf7c842015-09-15 12:18:29 +00006935 case PREDEF_TYPE_SAMPLER_ID:
6936 T = Context.OCLSamplerTy;
6937 break;
6938 case PREDEF_TYPE_EVENT_ID:
6939 T = Context.OCLEventTy;
6940 break;
Alexey Bader9c8453f2015-09-15 11:18:52 +00006941 case PREDEF_TYPE_CLK_EVENT_ID:
6942 T = Context.OCLClkEventTy;
6943 break;
6944 case PREDEF_TYPE_QUEUE_ID:
6945 T = Context.OCLQueueTy;
6946 break;
Alexey Bader9c8453f2015-09-15 11:18:52 +00006947 case PREDEF_TYPE_RESERVE_ID_ID:
6948 T = Context.OCLReserveIDTy;
6949 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006950 case PREDEF_TYPE_AUTO_DEDUCT:
6951 T = Context.getAutoDeductType();
6952 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006953 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
6954 T = Context.getAutoRRefDeductType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006955 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00006956 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
6957 T = Context.ARCUnbridgedCastTy;
6958 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00006959 case PREDEF_TYPE_BUILTIN_FN:
6960 T = Context.BuiltinFnTy;
6961 break;
Alexey Bataev1a3320e2015-08-25 14:24:04 +00006962 case PREDEF_TYPE_OMP_ARRAY_SECTION:
6963 T = Context.OMPArraySectionTy;
6964 break;
Alexey Bataev7ac9efb2020-02-05 09:33:05 -05006965 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
6966 T = Context.OMPArraySectionTy;
6967 break;
Alexey Bataev13a15042020-04-01 15:06:38 -04006968 case PREDEF_TYPE_OMP_ITERATOR:
6969 T = Context.OMPIteratorTy;
6970 break;
Richard Sandifordeb485fb2019-08-09 08:52:54 +00006971#define SVE_TYPE(Name, Id, SingletonId) \
6972 case PREDEF_TYPE_##Id##_ID: \
6973 T = Context.SingletonId; \
6974 break;
6975#include "clang/Basic/AArch64SVEACLETypes.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00006976 }
6977
6978 assert(!T.isNull() && "Unknown predefined type");
6979 return T.withFastQualifiers(FastQuals);
6980 }
6981
6982 Index -= NUM_PREDEF_TYPE_IDS;
6983 assert(Index < TypesLoaded.size() && "Type index out-of-range");
6984 if (TypesLoaded[Index].isNull()) {
6985 TypesLoaded[Index] = readTypeRecord(Index);
6986 if (TypesLoaded[Index].isNull())
Vedant Kumar48b4f762018-04-14 01:40:48 +00006987 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006988
6989 TypesLoaded[Index]->setFromAST();
6990 if (DeserializationListener)
6991 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
6992 TypesLoaded[Index]);
6993 }
6994
6995 return TypesLoaded[Index].withFastQualifiers(FastQuals);
6996}
6997
6998QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
6999 return GetType(getGlobalTypeID(F, LocalID));
7000}
7001
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007002serialization::TypeID
Guy Benyei11169dd2012-12-18 14:30:41 +00007003ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7004 unsigned FastQuals = LocalID & Qualifiers::FastMask;
7005 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007006
Guy Benyei11169dd2012-12-18 14:30:41 +00007007 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7008 return LocalID;
7009
Richard Smith37a93df2017-02-18 00:32:02 +00007010 if (!F.ModuleOffsetMap.empty())
7011 ReadModuleOffsetMap(F);
7012
Guy Benyei11169dd2012-12-18 14:30:41 +00007013 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7014 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7015 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007016
Guy Benyei11169dd2012-12-18 14:30:41 +00007017 unsigned GlobalIndex = LocalIndex + I->second;
7018 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7019}
7020
7021TemplateArgumentLocInfo
John McCall3ce3d232019-12-13 03:37:23 -05007022ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007023 switch (Kind) {
7024 case TemplateArgument::Expression:
John McCall3ce3d232019-12-13 03:37:23 -05007025 return readExpr();
Guy Benyei11169dd2012-12-18 14:30:41 +00007026 case TemplateArgument::Type:
John McCall3ce3d232019-12-13 03:37:23 -05007027 return readTypeSourceInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +00007028 case TemplateArgument::Template: {
John McCall3ce3d232019-12-13 03:37:23 -05007029 NestedNameSpecifierLoc QualifierLoc =
7030 readNestedNameSpecifierLoc();
7031 SourceLocation TemplateNameLoc = readSourceLocation();
Guy Benyei11169dd2012-12-18 14:30:41 +00007032 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7033 SourceLocation());
7034 }
7035 case TemplateArgument::TemplateExpansion: {
John McCall3ce3d232019-12-13 03:37:23 -05007036 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7037 SourceLocation TemplateNameLoc = readSourceLocation();
7038 SourceLocation EllipsisLoc = readSourceLocation();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007039 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Guy Benyei11169dd2012-12-18 14:30:41 +00007040 EllipsisLoc);
7041 }
7042 case TemplateArgument::Null:
7043 case TemplateArgument::Integral:
7044 case TemplateArgument::Declaration:
7045 case TemplateArgument::NullPtr:
7046 case TemplateArgument::Pack:
7047 // FIXME: Is this right?
Vedant Kumar48b4f762018-04-14 01:40:48 +00007048 return TemplateArgumentLocInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +00007049 }
7050 llvm_unreachable("unexpected template argument loc");
7051}
7052
John McCall3ce3d232019-12-13 03:37:23 -05007053TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7054 TemplateArgument Arg = readTemplateArgument();
Guy Benyei11169dd2012-12-18 14:30:41 +00007055
7056 if (Arg.getKind() == TemplateArgument::Expression) {
John McCall3ce3d232019-12-13 03:37:23 -05007057 if (readBool()) // bool InfoHasSameExpr.
Guy Benyei11169dd2012-12-18 14:30:41 +00007058 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7059 }
John McCall3ce3d232019-12-13 03:37:23 -05007060 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
Guy Benyei11169dd2012-12-18 14:30:41 +00007061}
7062
John McCall3ce3d232019-12-13 03:37:23 -05007063const ASTTemplateArgumentListInfo *
7064ASTRecordReader::readASTTemplateArgumentListInfo() {
7065 SourceLocation LAngleLoc = readSourceLocation();
7066 SourceLocation RAngleLoc = readSourceLocation();
7067 unsigned NumArgsAsWritten = readInt();
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00007068 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7069 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
John McCall3ce3d232019-12-13 03:37:23 -05007070 TemplArgsInfo.addArgument(readTemplateArgumentLoc());
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00007071 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7072}
7073
Guy Benyei11169dd2012-12-18 14:30:41 +00007074Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7075 return GetDecl(ID);
7076}
7077
Richard Smith053f6c62014-05-16 23:01:30 +00007078void ASTReader::CompleteRedeclChain(const Decl *D) {
Richard Smith851072e2014-05-19 20:59:20 +00007079 if (NumCurrentElementsDeserializing) {
7080 // We arrange to not care about the complete redeclaration chain while we're
7081 // deserializing. Just remember that the AST has marked this one as complete
7082 // but that it's not actually complete yet, so we know we still need to
7083 // complete it later.
7084 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7085 return;
7086 }
7087
Richard Smith053f6c62014-05-16 23:01:30 +00007088 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7089
Richard Smith053f6c62014-05-16 23:01:30 +00007090 // If this is a named declaration, complete it by looking it up
7091 // within its context.
7092 //
Richard Smith01bdb7a2014-08-28 05:44:07 +00007093 // FIXME: Merging a function definition should merge
Richard Smith053f6c62014-05-16 23:01:30 +00007094 // all mergeable entities within it.
7095 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7096 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7097 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
Richard Smitha534a312015-07-21 23:54:07 +00007098 if (!getContext().getLangOpts().CPlusPlus &&
7099 isa<TranslationUnitDecl>(DC)) {
Richard Smith053f6c62014-05-16 23:01:30 +00007100 // Outside of C++, we don't have a lookup table for the TU, so update
Richard Smitha534a312015-07-21 23:54:07 +00007101 // the identifier instead. (For C++ modules, we don't store decls
7102 // in the serialized identifier table, so we do the lookup in the TU.)
7103 auto *II = Name.getAsIdentifierInfo();
7104 assert(II && "non-identifier name in C?");
Richard Smith053f6c62014-05-16 23:01:30 +00007105 if (II->isOutOfDate())
7106 updateOutOfDateIdentifier(*II);
7107 } else
7108 DC->lookup(Name);
Richard Smith01bdb7a2014-08-28 05:44:07 +00007109 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
Richard Smith3cb15722015-08-05 22:41:45 +00007110 // Find all declarations of this kind from the relevant context.
7111 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7112 auto *DC = cast<DeclContext>(DCDecl);
7113 SmallVector<Decl*, 8> Decls;
7114 FindExternalLexicalDecls(
7115 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7116 }
Richard Smith053f6c62014-05-16 23:01:30 +00007117 }
7118 }
Richard Smith50895422015-01-31 03:04:55 +00007119
7120 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7121 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7122 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7123 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7124 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7125 if (auto *Template = FD->getPrimaryTemplate())
7126 Template->LoadLazySpecializations();
7127 }
Richard Smith053f6c62014-05-16 23:01:30 +00007128}
7129
Richard Smithc2bb8182015-03-24 06:36:48 +00007130CXXCtorInitializer **
7131ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7132 RecordLocation Loc = getLocalBitOffset(Offset);
7133 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7134 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00007135 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7136 Error(std::move(Err));
7137 return nullptr;
7138 }
Richard Smithc2bb8182015-03-24 06:36:48 +00007139 ReadingKindTracker ReadingKind(Read_Decl, *this);
7140
JF Bastien0e828952019-06-26 19:50:12 +00007141 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7142 if (!MaybeCode) {
7143 Error(MaybeCode.takeError());
7144 return nullptr;
7145 }
7146 unsigned Code = MaybeCode.get();
7147
John McCall3ce3d232019-12-13 03:37:23 -05007148 ASTRecordReader Record(*this, *Loc.F);
7149 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
JF Bastien0e828952019-06-26 19:50:12 +00007150 if (!MaybeRecCode) {
7151 Error(MaybeRecCode.takeError());
7152 return nullptr;
7153 }
7154 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
Richard Smithc2bb8182015-03-24 06:36:48 +00007155 Error("malformed AST file: missing C++ ctor initializers");
7156 return nullptr;
7157 }
7158
John McCall3ce3d232019-12-13 03:37:23 -05007159 return Record.readCXXCtorInitializers();
Richard Smithc2bb8182015-03-24 06:36:48 +00007160}
7161
Guy Benyei11169dd2012-12-18 14:30:41 +00007162CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00007163 assert(ContextObj && "reading base specifiers with no AST context");
7164 ASTContext &Context = *ContextObj;
7165
Guy Benyei11169dd2012-12-18 14:30:41 +00007166 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007167 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00007168 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00007169 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7170 Error(std::move(Err));
7171 return nullptr;
7172 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007173 ReadingKindTracker ReadingKind(Read_Decl, *this);
JF Bastien0e828952019-06-26 19:50:12 +00007174
7175 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7176 if (!MaybeCode) {
7177 Error(MaybeCode.takeError());
7178 return nullptr;
7179 }
7180 unsigned Code = MaybeCode.get();
7181
John McCall3ce3d232019-12-13 03:37:23 -05007182 ASTRecordReader Record(*this, *Loc.F);
7183 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
JF Bastien0e828952019-06-26 19:50:12 +00007184 if (!MaybeRecCode) {
7185 Error(MaybeCode.takeError());
7186 return nullptr;
7187 }
7188 unsigned RecCode = MaybeRecCode.get();
7189
Guy Benyei11169dd2012-12-18 14:30:41 +00007190 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00007191 Error("malformed AST file: missing C++ base specifiers");
Craig Toppera13603a2014-05-22 05:54:18 +00007192 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007193 }
7194
John McCall3ce3d232019-12-13 03:37:23 -05007195 unsigned NumBases = Record.readInt();
Guy Benyei11169dd2012-12-18 14:30:41 +00007196 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Vedant Kumar48b4f762018-04-14 01:40:48 +00007197 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
Guy Benyei11169dd2012-12-18 14:30:41 +00007198 for (unsigned I = 0; I != NumBases; ++I)
John McCall3ce3d232019-12-13 03:37:23 -05007199 Bases[I] = Record.readCXXBaseSpecifier();
Guy Benyei11169dd2012-12-18 14:30:41 +00007200 return Bases;
7201}
7202
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007203serialization::DeclID
Guy Benyei11169dd2012-12-18 14:30:41 +00007204ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7205 if (LocalID < NUM_PREDEF_DECL_IDS)
7206 return LocalID;
7207
Richard Smith37a93df2017-02-18 00:32:02 +00007208 if (!F.ModuleOffsetMap.empty())
7209 ReadModuleOffsetMap(F);
7210
Guy Benyei11169dd2012-12-18 14:30:41 +00007211 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7212 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7213 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007214
Guy Benyei11169dd2012-12-18 14:30:41 +00007215 return LocalID + I->second;
7216}
7217
7218bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7219 ModuleFile &M) const {
Richard Smithfe620d22015-03-05 23:24:12 +00007220 // Predefined decls aren't from any module.
7221 if (ID < NUM_PREDEF_DECL_IDS)
7222 return false;
7223
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007224 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
Richard Smithbcda1a92015-07-12 23:51:20 +00007225 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
Guy Benyei11169dd2012-12-18 14:30:41 +00007226}
7227
Douglas Gregor9f782892013-01-21 15:25:38 +00007228ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007229 if (!D->isFromASTFile())
Craig Toppera13603a2014-05-22 05:54:18 +00007230 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007231 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7232 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7233 return I->second;
7234}
7235
7236SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7237 if (ID < NUM_PREDEF_DECL_IDS)
Vedant Kumar48b4f762018-04-14 01:40:48 +00007238 return SourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00007239
Guy Benyei11169dd2012-12-18 14:30:41 +00007240 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7241
7242 if (Index > DeclsLoaded.size()) {
7243 Error("declaration ID out-of-range for AST file");
Vedant Kumar48b4f762018-04-14 01:40:48 +00007244 return SourceLocation();
Guy Benyei11169dd2012-12-18 14:30:41 +00007245 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00007246
Guy Benyei11169dd2012-12-18 14:30:41 +00007247 if (Decl *D = DeclsLoaded[Index])
7248 return D->getLocation();
7249
Richard Smithcb34bd32016-03-27 07:28:06 +00007250 SourceLocation Loc;
7251 DeclCursorForID(ID, Loc);
7252 return Loc;
Guy Benyei11169dd2012-12-18 14:30:41 +00007253}
7254
Richard Smithfe620d22015-03-05 23:24:12 +00007255static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7256 switch (ID) {
7257 case PREDEF_DECL_NULL_ID:
7258 return nullptr;
7259
7260 case PREDEF_DECL_TRANSLATION_UNIT_ID:
7261 return Context.getTranslationUnitDecl();
7262
7263 case PREDEF_DECL_OBJC_ID_ID:
7264 return Context.getObjCIdDecl();
7265
7266 case PREDEF_DECL_OBJC_SEL_ID:
7267 return Context.getObjCSelDecl();
7268
7269 case PREDEF_DECL_OBJC_CLASS_ID:
7270 return Context.getObjCClassDecl();
7271
7272 case PREDEF_DECL_OBJC_PROTOCOL_ID:
7273 return Context.getObjCProtocolDecl();
7274
7275 case PREDEF_DECL_INT_128_ID:
7276 return Context.getInt128Decl();
7277
7278 case PREDEF_DECL_UNSIGNED_INT_128_ID:
7279 return Context.getUInt128Decl();
7280
7281 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7282 return Context.getObjCInstanceTypeDecl();
7283
7284 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7285 return Context.getBuiltinVaListDecl();
Richard Smithf19e1272015-03-07 00:04:49 +00007286
Richard Smith9b88a4c2015-07-27 05:40:23 +00007287 case PREDEF_DECL_VA_LIST_TAG:
7288 return Context.getVaListTagDecl();
7289
Charles Davisc7d5c942015-09-17 20:55:33 +00007290 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7291 return Context.getBuiltinMSVaListDecl();
7292
Richard Smithbab6df82020-04-11 22:15:29 -07007293 case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7294 return Context.getMSGuidTagDecl();
7295
Richard Smithf19e1272015-03-07 00:04:49 +00007296 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7297 return Context.getExternCContextDecl();
David Majnemerd9b1a4f2015-11-04 03:40:30 +00007298
7299 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7300 return Context.getMakeIntegerSeqDecl();
Quentin Colombet043406b2016-02-03 22:41:00 +00007301
7302 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7303 return Context.getCFConstantStringDecl();
Ben Langmuirf5416742016-02-04 00:55:24 +00007304
7305 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7306 return Context.getCFConstantStringTagDecl();
Eric Fiselier6ad68552016-07-01 01:24:09 +00007307
7308 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7309 return Context.getTypePackElementDecl();
Richard Smithfe620d22015-03-05 23:24:12 +00007310 }
Yaron Keren322bdad2015-03-06 07:49:14 +00007311 llvm_unreachable("PredefinedDeclIDs unknown enum value");
Richard Smithfe620d22015-03-05 23:24:12 +00007312}
7313
Richard Smithcd45dbc2014-04-19 03:48:30 +00007314Decl *ASTReader::GetExistingDecl(DeclID ID) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00007315 assert(ContextObj && "reading decl with no AST context");
Richard Smithcd45dbc2014-04-19 03:48:30 +00007316 if (ID < NUM_PREDEF_DECL_IDS) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00007317 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
Richard Smithfe620d22015-03-05 23:24:12 +00007318 if (D) {
7319 // Track that we have merged the declaration with ID \p ID into the
7320 // pre-existing predefined declaration \p D.
Richard Smith5fc18a92015-07-12 23:43:21 +00007321 auto &Merged = KeyDecls[D->getCanonicalDecl()];
Richard Smithfe620d22015-03-05 23:24:12 +00007322 if (Merged.empty())
7323 Merged.push_back(ID);
Guy Benyei11169dd2012-12-18 14:30:41 +00007324 }
Richard Smithfe620d22015-03-05 23:24:12 +00007325 return D;
Guy Benyei11169dd2012-12-18 14:30:41 +00007326 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00007327
Guy Benyei11169dd2012-12-18 14:30:41 +00007328 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7329
7330 if (Index >= DeclsLoaded.size()) {
7331 assert(0 && "declaration ID out-of-range for AST file");
7332 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007333 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007334 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00007335
7336 return DeclsLoaded[Index];
7337}
7338
7339Decl *ASTReader::GetDecl(DeclID ID) {
7340 if (ID < NUM_PREDEF_DECL_IDS)
7341 return GetExistingDecl(ID);
7342
7343 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7344
7345 if (Index >= DeclsLoaded.size()) {
7346 assert(0 && "declaration ID out-of-range for AST file");
7347 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007348 return nullptr;
Richard Smithcd45dbc2014-04-19 03:48:30 +00007349 }
7350
Guy Benyei11169dd2012-12-18 14:30:41 +00007351 if (!DeclsLoaded[Index]) {
7352 ReadDeclRecord(ID);
7353 if (DeserializationListener)
7354 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7355 }
7356
7357 return DeclsLoaded[Index];
7358}
7359
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007360DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
Guy Benyei11169dd2012-12-18 14:30:41 +00007361 DeclID GlobalID) {
7362 if (GlobalID < NUM_PREDEF_DECL_IDS)
7363 return GlobalID;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007364
Guy Benyei11169dd2012-12-18 14:30:41 +00007365 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7366 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7367 ModuleFile *Owner = I->second;
7368
7369 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7370 = M.GlobalToLocalDeclIDs.find(Owner);
7371 if (Pos == M.GlobalToLocalDeclIDs.end())
7372 return 0;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007373
Guy Benyei11169dd2012-12-18 14:30:41 +00007374 return GlobalID - Owner->BaseDeclID + Pos->second;
7375}
7376
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007377serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Guy Benyei11169dd2012-12-18 14:30:41 +00007378 const RecordData &Record,
7379 unsigned &Idx) {
7380 if (Idx >= Record.size()) {
7381 Error("Corrupted AST file");
7382 return 0;
7383 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007384
Guy Benyei11169dd2012-12-18 14:30:41 +00007385 return getGlobalDeclID(F, Record[Idx++]);
7386}
7387
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007388/// Resolve the offset of a statement into a statement.
Guy Benyei11169dd2012-12-18 14:30:41 +00007389///
7390/// This operation will read a new statement from the external
7391/// source each time it is called, and is meant to be used via a
7392/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7393Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7394 // Switch case IDs are per Decl.
7395 ClearSwitchCaseIDs();
7396
7397 // Offset here is a global offset across the entire chain.
7398 RecordLocation Loc = getLocalBitOffset(Offset);
JF Bastien0e828952019-06-26 19:50:12 +00007399 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7400 Error(std::move(Err));
7401 return nullptr;
7402 }
David Blaikie9fd16f82017-03-08 23:57:08 +00007403 assert(NumCurrentElementsDeserializing == 0 &&
7404 "should not be called while already deserializing");
7405 Deserializing D(this);
Guy Benyei11169dd2012-12-18 14:30:41 +00007406 return ReadStmtFromStream(*Loc.F);
7407}
7408
Richard Smith3cb15722015-08-05 22:41:45 +00007409void ASTReader::FindExternalLexicalDecls(
7410 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7411 SmallVectorImpl<Decl *> &Decls) {
Richard Smith82f8fcd2015-08-06 22:07:25 +00007412 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7413
Richard Smith9ccdd932015-08-06 22:14:12 +00007414 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
Richard Smith82f8fcd2015-08-06 22:07:25 +00007415 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7416 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7417 auto K = (Decl::Kind)+LexicalDecls[I];
7418 if (!IsKindWeWant(K))
7419 continue;
7420
7421 auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7422
7423 // Don't add predefined declarations to the lexical context more
7424 // than once.
7425 if (ID < NUM_PREDEF_DECL_IDS) {
7426 if (PredefsVisited[ID])
7427 continue;
7428
7429 PredefsVisited[ID] = true;
7430 }
7431
7432 if (Decl *D = GetLocalDecl(*M, ID)) {
Richard Smith2317a3e2015-08-11 21:21:20 +00007433 assert(D->getKind() == K && "wrong kind for lexical decl");
Richard Smith82f8fcd2015-08-06 22:07:25 +00007434 if (!DC->isDeclInLexicalTraversal(D))
7435 Decls.push_back(D);
7436 }
7437 }
7438 };
7439
7440 if (isa<TranslationUnitDecl>(DC)) {
7441 for (auto Lexical : TULexicalDecls)
7442 Visit(Lexical.first, Lexical.second);
7443 } else {
7444 auto I = LexicalDecls.find(DC);
7445 if (I != LexicalDecls.end())
Richard Smith9c9173d2015-08-11 22:00:24 +00007446 Visit(I->second.first, I->second.second);
Richard Smith82f8fcd2015-08-06 22:07:25 +00007447 }
7448
Guy Benyei11169dd2012-12-18 14:30:41 +00007449 ++NumLexicalDeclContextsRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00007450}
7451
7452namespace {
7453
7454class DeclIDComp {
7455 ASTReader &Reader;
7456 ModuleFile &Mod;
7457
7458public:
7459 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7460
7461 bool operator()(LocalDeclID L, LocalDeclID R) const {
7462 SourceLocation LHS = getLocation(L);
7463 SourceLocation RHS = getLocation(R);
7464 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7465 }
7466
7467 bool operator()(SourceLocation LHS, LocalDeclID R) const {
7468 SourceLocation RHS = getLocation(R);
7469 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7470 }
7471
7472 bool operator()(LocalDeclID L, SourceLocation RHS) const {
7473 SourceLocation LHS = getLocation(L);
7474 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7475 }
7476
7477 SourceLocation getLocation(LocalDeclID ID) const {
7478 return Reader.getSourceManager().getFileLoc(
7479 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7480 }
7481};
7482
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007483} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00007484
7485void ASTReader::FindFileRegionDecls(FileID File,
7486 unsigned Offset, unsigned Length,
7487 SmallVectorImpl<Decl *> &Decls) {
7488 SourceManager &SM = getSourceManager();
7489
7490 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7491 if (I == FileDeclIDs.end())
7492 return;
7493
7494 FileDeclsInfo &DInfo = I->second;
7495 if (DInfo.Decls.empty())
7496 return;
7497
7498 SourceLocation
7499 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7500 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7501
7502 DeclIDComp DIDComp(*this, *DInfo.Mod);
Fangrui Song7264a472019-07-03 08:13:17 +00007503 ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7504 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
Guy Benyei11169dd2012-12-18 14:30:41 +00007505 if (BeginIt != DInfo.Decls.begin())
7506 --BeginIt;
7507
7508 // If we are pointing at a top-level decl inside an objc container, we need
7509 // to backtrack until we find it otherwise we will fail to report that the
7510 // region overlaps with an objc container.
7511 while (BeginIt != DInfo.Decls.begin() &&
7512 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7513 ->isTopLevelDeclInObjCContainer())
7514 --BeginIt;
7515
Fangrui Song7264a472019-07-03 08:13:17 +00007516 ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7517 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
Guy Benyei11169dd2012-12-18 14:30:41 +00007518 if (EndIt != DInfo.Decls.end())
7519 ++EndIt;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007520
Guy Benyei11169dd2012-12-18 14:30:41 +00007521 for (ArrayRef<serialization::LocalDeclID>::iterator
7522 DIt = BeginIt; DIt != EndIt; ++DIt)
7523 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7524}
7525
Richard Smith9ce12e32013-02-07 03:30:24 +00007526bool
Guy Benyei11169dd2012-12-18 14:30:41 +00007527ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7528 DeclarationName Name) {
Richard Smithd88a7f12015-09-01 20:35:42 +00007529 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
Guy Benyei11169dd2012-12-18 14:30:41 +00007530 "DeclContext has no visible decls in storage");
7531 if (!Name)
Richard Smith9ce12e32013-02-07 03:30:24 +00007532 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00007533
Richard Smithd88a7f12015-09-01 20:35:42 +00007534 auto It = Lookups.find(DC);
7535 if (It == Lookups.end())
7536 return false;
7537
Richard Smith8c913ec2014-08-14 02:21:01 +00007538 Deserializing LookupResults(this);
7539
Richard Smithd88a7f12015-09-01 20:35:42 +00007540 // Load the list of declarations.
Guy Benyei11169dd2012-12-18 14:30:41 +00007541 SmallVector<NamedDecl *, 64> Decls;
Vedant Kumar48b4f762018-04-14 01:40:48 +00007542 for (DeclID ID : It->second.Table.find(Name)) {
7543 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
Richard Smithd88a7f12015-09-01 20:35:42 +00007544 if (ND->getDeclName() == Name)
7545 Decls.push_back(ND);
7546 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00007547
Guy Benyei11169dd2012-12-18 14:30:41 +00007548 ++NumVisibleDeclContextsRead;
7549 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith9ce12e32013-02-07 03:30:24 +00007550 return !Decls.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +00007551}
7552
Guy Benyei11169dd2012-12-18 14:30:41 +00007553void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7554 if (!DC->hasExternalVisibleStorage())
7555 return;
Richard Smithd88a7f12015-09-01 20:35:42 +00007556
7557 auto It = Lookups.find(DC);
7558 assert(It != Lookups.end() &&
7559 "have external visible storage but no lookup tables");
7560
Craig Topper79be4cd2013-07-05 04:33:53 +00007561 DeclsMap Decls;
Guy Benyei11169dd2012-12-18 14:30:41 +00007562
Vedant Kumar48b4f762018-04-14 01:40:48 +00007563 for (DeclID ID : It->second.Table.findAll()) {
7564 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
Richard Smithd88a7f12015-09-01 20:35:42 +00007565 Decls[ND->getDeclName()].push_back(ND);
Guy Benyei11169dd2012-12-18 14:30:41 +00007566 }
7567
Guy Benyei11169dd2012-12-18 14:30:41 +00007568 ++NumVisibleDeclContextsRead;
7569
Vedant Kumar48b4f762018-04-14 01:40:48 +00007570 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7571 SetExternalVisibleDeclsForName(DC, I->first, I->second);
7572 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007573 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7574}
7575
Richard Smithd88a7f12015-09-01 20:35:42 +00007576const serialization::reader::DeclContextLookupTable *
7577ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7578 auto I = Lookups.find(Primary);
7579 return I == Lookups.end() ? nullptr : &I->second;
7580}
7581
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007582/// Under non-PCH compilation the consumer receives the objc methods
Guy Benyei11169dd2012-12-18 14:30:41 +00007583/// before receiving the implementation, and codegen depends on this.
7584/// We simulate this by deserializing and passing to consumer the methods of the
7585/// implementation before passing the deserialized implementation decl.
7586static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7587 ASTConsumer *Consumer) {
7588 assert(ImplD && Consumer);
7589
Aaron Ballmanaff18c02014-03-13 19:03:34 +00007590 for (auto *I : ImplD->methods())
7591 Consumer->HandleInterestingDecl(DeclGroupRef(I));
Guy Benyei11169dd2012-12-18 14:30:41 +00007592
7593 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7594}
7595
Guy Benyei11169dd2012-12-18 14:30:41 +00007596void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00007597 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00007598 PassObjCImplDeclToConsumer(ImplD, Consumer);
7599 else
7600 Consumer->HandleInterestingDecl(DeclGroupRef(D));
7601}
7602
7603void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7604 this->Consumer = Consumer;
7605
Richard Smith9e2341d2015-03-23 03:25:59 +00007606 if (Consumer)
7607 PassInterestingDeclsToConsumer();
Richard Smith7f330cd2015-03-18 01:42:29 +00007608
7609 if (DeserializationListener)
7610 DeserializationListener->ReaderInitialized(this);
Guy Benyei11169dd2012-12-18 14:30:41 +00007611}
7612
7613void ASTReader::PrintStats() {
7614 std::fprintf(stderr, "*** AST File Statistics:\n");
7615
7616 unsigned NumTypesLoaded
7617 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7618 QualType());
7619 unsigned NumDeclsLoaded
7620 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00007621 (Decl *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00007622 unsigned NumIdentifiersLoaded
7623 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7624 IdentifiersLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00007625 (IdentifierInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00007626 unsigned NumMacrosLoaded
7627 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7628 MacrosLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00007629 (MacroInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00007630 unsigned NumSelectorsLoaded
7631 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7632 SelectorsLoaded.end(),
7633 Selector());
7634
7635 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7636 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
7637 NumSLocEntriesRead, TotalNumSLocEntries,
7638 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7639 if (!TypesLoaded.empty())
7640 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
7641 NumTypesLoaded, (unsigned)TypesLoaded.size(),
7642 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7643 if (!DeclsLoaded.empty())
7644 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
7645 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7646 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7647 if (!IdentifiersLoaded.empty())
7648 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
7649 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7650 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7651 if (!MacrosLoaded.empty())
7652 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
7653 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7654 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7655 if (!SelectorsLoaded.empty())
7656 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
7657 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7658 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7659 if (TotalNumStatements)
7660 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
7661 NumStatementsRead, TotalNumStatements,
7662 ((float)NumStatementsRead/TotalNumStatements * 100));
7663 if (TotalNumMacros)
7664 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
7665 NumMacrosRead, TotalNumMacros,
7666 ((float)NumMacrosRead/TotalNumMacros * 100));
7667 if (TotalLexicalDeclContexts)
7668 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
7669 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7670 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7671 * 100));
7672 if (TotalVisibleDeclContexts)
7673 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
7674 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7675 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7676 * 100));
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007677 if (TotalNumMethodPoolEntries)
Guy Benyei11169dd2012-12-18 14:30:41 +00007678 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
7679 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7680 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7681 * 100));
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007682 if (NumMethodPoolLookups)
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007683 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
7684 NumMethodPoolHits, NumMethodPoolLookups,
7685 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007686 if (NumMethodPoolTableLookups)
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007687 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
7688 NumMethodPoolTableHits, NumMethodPoolTableLookups,
7689 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7690 * 100.0));
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007691 if (NumIdentifierLookupHits)
Douglas Gregor00a50f72013-01-25 00:38:33 +00007692 std::fprintf(stderr,
7693 " %u / %u identifier table lookups succeeded (%f%%)\n",
7694 NumIdentifierLookupHits, NumIdentifierLookups,
7695 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
Douglas Gregor00a50f72013-01-25 00:38:33 +00007696
Douglas Gregore060e572013-01-25 01:03:03 +00007697 if (GlobalIndex) {
7698 std::fprintf(stderr, "\n");
7699 GlobalIndex->printStats();
7700 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007701
Guy Benyei11169dd2012-12-18 14:30:41 +00007702 std::fprintf(stderr, "\n");
7703 dump();
7704 std::fprintf(stderr, "\n");
7705}
7706
7707template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Vassil Vassilevb2710682017-03-02 18:13:19 +00007708LLVM_DUMP_METHOD static void
Guy Benyei11169dd2012-12-18 14:30:41 +00007709dumpModuleIDMap(StringRef Name,
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007710 const ContinuousRangeMap<Key, ModuleFile *,
Guy Benyei11169dd2012-12-18 14:30:41 +00007711 InitialCapacity> &Map) {
7712 if (Map.begin() == Map.end())
7713 return;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007714
Vedant Kumar48b4f762018-04-14 01:40:48 +00007715 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7716
Guy Benyei11169dd2012-12-18 14:30:41 +00007717 llvm::errs() << Name << ":\n";
Vedant Kumar48b4f762018-04-14 01:40:48 +00007718 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7719 I != IEnd; ++I) {
7720 llvm::errs() << " " << I->first << " -> " << I->second->FileName
7721 << "\n";
7722 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007723}
7724
Yaron Kerencdae9412016-01-29 19:38:18 +00007725LLVM_DUMP_METHOD void ASTReader::dump() {
Guy Benyei11169dd2012-12-18 14:30:41 +00007726 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7727 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7728 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7729 dumpModuleIDMap("Global type map", GlobalTypeMap);
7730 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7731 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7732 dumpModuleIDMap("Global macro map", GlobalMacroMap);
7733 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7734 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007735 dumpModuleIDMap("Global preprocessed entity map",
Guy Benyei11169dd2012-12-18 14:30:41 +00007736 GlobalPreprocessedEntityMap);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007737
Guy Benyei11169dd2012-12-18 14:30:41 +00007738 llvm::errs() << "\n*** PCH/Modules Loaded:";
Vedant Kumar48b4f762018-04-14 01:40:48 +00007739 for (ModuleFile &M : ModuleMgr)
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00007740 M.dump();
Guy Benyei11169dd2012-12-18 14:30:41 +00007741}
7742
7743/// Return the amount of memory used by memory buffers, breaking down
7744/// by heap-backed versus mmap'ed memory.
7745void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Vedant Kumar48b4f762018-04-14 01:40:48 +00007746 for (ModuleFile &I : ModuleMgr) {
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00007747 if (llvm::MemoryBuffer *buf = I.Buffer) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007748 size_t bytes = buf->getBufferSize();
7749 switch (buf->getBufferKind()) {
7750 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7751 sizes.malloc_bytes += bytes;
7752 break;
7753 case llvm::MemoryBuffer::MemoryBuffer_MMap:
7754 sizes.mmap_bytes += bytes;
7755 break;
7756 }
7757 }
7758 }
7759}
7760
7761void ASTReader::InitializeSema(Sema &S) {
7762 SemaObj = &S;
7763 S.addExternalSource(this);
7764
7765 // Makes sure any declarations that were deserialized "too early"
7766 // still get added to the identifier's declaration chains.
Vedant Kumar48b4f762018-04-14 01:40:48 +00007767 for (uint64_t ID : PreloadedDeclIDs) {
7768 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
Ben Langmuir5418f402014-09-10 21:29:41 +00007769 pushExternalDeclIntoScope(D, D->getDeclName());
Guy Benyei11169dd2012-12-18 14:30:41 +00007770 }
Ben Langmuir5418f402014-09-10 21:29:41 +00007771 PreloadedDeclIDs.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00007772
Richard Smith3d8e97e2013-10-18 06:54:39 +00007773 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00007774 if (!FPPragmaOptions.empty()) {
7775 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
Melanie Blower8812b0c2020-04-16 08:45:26 -07007776 SemaObj->CurFPFeatures = FPOptions(FPPragmaOptions[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007777 }
7778
Yaxun Liu5b746652016-12-18 05:18:55 +00007779 SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7780 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7781 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
Richard Smith3d8e97e2013-10-18 06:54:39 +00007782
7783 UpdateSema();
7784}
7785
7786void ASTReader::UpdateSema() {
7787 assert(SemaObj && "no Sema to update");
7788
7789 // Load the offsets of the declarations that Sema references.
7790 // They will be lazily deserialized when needed.
7791 if (!SemaDeclRefs.empty()) {
Richard Smith96269c52016-09-29 22:49:46 +00007792 assert(SemaDeclRefs.size() % 3 == 0);
7793 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
Richard Smith3d8e97e2013-10-18 06:54:39 +00007794 if (!SemaObj->StdNamespace)
7795 SemaObj->StdNamespace = SemaDeclRefs[I];
7796 if (!SemaObj->StdBadAlloc)
7797 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
Richard Smith96269c52016-09-29 22:49:46 +00007798 if (!SemaObj->StdAlignValT)
7799 SemaObj->StdAlignValT = SemaDeclRefs[I+2];
Richard Smith3d8e97e2013-10-18 06:54:39 +00007800 }
7801 SemaDeclRefs.clear();
7802 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00007803
Nico Weber779355f2016-03-02 23:22:00 +00007804 // Update the state of pragmas. Use the same API as if we had encountered the
7805 // pragma in the source.
Dario Domizioli13a0a382014-05-23 12:13:25 +00007806 if(OptimizeOffPragmaLocation.isValid())
Rui Ueyama49a3ad22019-07-16 04:46:31 +00007807 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
Nico Weber779355f2016-03-02 23:22:00 +00007808 if (PragmaMSStructState != -1)
7809 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
Nico Weber42932312016-03-03 00:17:35 +00007810 if (PointersToMembersPragmaLocation.isValid()) {
7811 SemaObj->ActOnPragmaMSPointersToMembers(
7812 (LangOptions::PragmaMSPointersToMembersKind)
7813 PragmaMSPointersToMembersState,
7814 PointersToMembersPragmaLocation);
7815 }
Justin Lebar67a78a62016-10-08 22:15:58 +00007816 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00007817
7818 if (PragmaPackCurrentValue) {
7819 // The bottom of the stack might have a default value. It must be adjusted
7820 // to the current value to ensure that the packing state is preserved after
7821 // popping entries that were included/imported from a PCH/module.
7822 bool DropFirst = false;
7823 if (!PragmaPackStack.empty() &&
7824 PragmaPackStack.front().Location.isInvalid()) {
7825 assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7826 "Expected a default alignment value");
7827 SemaObj->PackStack.Stack.emplace_back(
7828 PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
Alex Lorenz45b40142017-07-28 14:41:21 +00007829 SemaObj->PackStack.CurrentPragmaLocation,
7830 PragmaPackStack.front().PushLocation);
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00007831 DropFirst = true;
7832 }
7833 for (const auto &Entry :
7834 llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7835 SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
Alex Lorenz45b40142017-07-28 14:41:21 +00007836 Entry.Location, Entry.PushLocation);
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00007837 if (PragmaPackCurrentLocation.isInvalid()) {
7838 assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7839 "Expected a default alignment value");
7840 // Keep the current values.
7841 } else {
7842 SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7843 SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7844 }
7845 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007846}
7847
Richard Smitha8d5b6a2015-07-17 19:51:03 +00007848IdentifierInfo *ASTReader::get(StringRef Name) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007849 // Note that we are loading an identifier.
7850 Deserializing AnIdentifier(this);
Douglas Gregore060e572013-01-25 01:03:03 +00007851
Douglas Gregor7211ac12013-01-25 23:32:03 +00007852 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregor00a50f72013-01-25 00:38:33 +00007853 NumIdentifierLookups,
7854 NumIdentifierLookupHits);
Richard Smith33e0f7e2015-07-22 02:08:40 +00007855
7856 // We don't need to do identifier table lookups in C++ modules (we preload
7857 // all interesting declarations, and don't need to use the scope for name
7858 // lookups). Perform the lookup in PCH files, though, since we don't build
7859 // a complete initial identifier table if we're carrying on from a PCH.
Richard Smithdbafb6c2017-06-29 23:23:46 +00007860 if (PP.getLangOpts().CPlusPlus) {
Richard Smith33e0f7e2015-07-22 02:08:40 +00007861 for (auto F : ModuleMgr.pch_modules())
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007862 if (Visitor(*F))
Richard Smith33e0f7e2015-07-22 02:08:40 +00007863 break;
7864 } else {
7865 // If there is a global index, look there first to determine which modules
7866 // provably do not have any results for this identifier.
7867 GlobalModuleIndex::HitSet Hits;
7868 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7869 if (!loadGlobalIndex()) {
7870 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7871 HitsPtr = &Hits;
7872 }
7873 }
7874
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007875 ModuleMgr.visit(Visitor, HitsPtr);
Richard Smith33e0f7e2015-07-22 02:08:40 +00007876 }
7877
Guy Benyei11169dd2012-12-18 14:30:41 +00007878 IdentifierInfo *II = Visitor.getIdentifierInfo();
7879 markIdentifierUpToDate(II);
7880 return II;
7881}
7882
7883namespace clang {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00007884
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007885 /// An identifier-lookup iterator that enumerates all of the
Guy Benyei11169dd2012-12-18 14:30:41 +00007886 /// identifiers stored within a set of AST files.
7887 class ASTIdentifierIterator : public IdentifierIterator {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007888 /// The AST reader whose identifiers are being enumerated.
Guy Benyei11169dd2012-12-18 14:30:41 +00007889 const ASTReader &Reader;
7890
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007891 /// The current index into the chain of AST files stored in
Guy Benyei11169dd2012-12-18 14:30:41 +00007892 /// the AST reader.
7893 unsigned Index;
7894
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007895 /// The current position within the identifier lookup table
Guy Benyei11169dd2012-12-18 14:30:41 +00007896 /// of the current AST file.
7897 ASTIdentifierLookupTable::key_iterator Current;
7898
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007899 /// The end position within the identifier lookup table of
Guy Benyei11169dd2012-12-18 14:30:41 +00007900 /// the current AST file.
7901 ASTIdentifierLookupTable::key_iterator End;
7902
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007903 /// Whether to skip any modules in the ASTReader.
Ben Langmuir537c5b52016-05-04 00:53:13 +00007904 bool SkipModules;
7905
Guy Benyei11169dd2012-12-18 14:30:41 +00007906 public:
Ben Langmuir537c5b52016-05-04 00:53:13 +00007907 explicit ASTIdentifierIterator(const ASTReader &Reader,
7908 bool SkipModules = false);
Guy Benyei11169dd2012-12-18 14:30:41 +00007909
Craig Topper3e89dfe2014-03-13 02:13:41 +00007910 StringRef Next() override;
Guy Benyei11169dd2012-12-18 14:30:41 +00007911 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00007912
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007913} // namespace clang
Guy Benyei11169dd2012-12-18 14:30:41 +00007914
Ben Langmuir537c5b52016-05-04 00:53:13 +00007915ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
7916 bool SkipModules)
7917 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007918}
7919
7920StringRef ASTIdentifierIterator::Next() {
7921 while (Current == End) {
7922 // If we have exhausted all of our AST files, we're done.
7923 if (Index == 0)
Vedant Kumar48b4f762018-04-14 01:40:48 +00007924 return StringRef();
Guy Benyei11169dd2012-12-18 14:30:41 +00007925
7926 --Index;
Ben Langmuir537c5b52016-05-04 00:53:13 +00007927 ModuleFile &F = Reader.ModuleMgr[Index];
7928 if (SkipModules && F.isModule())
7929 continue;
7930
Vedant Kumar48b4f762018-04-14 01:40:48 +00007931 ASTIdentifierLookupTable *IdTable =
7932 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
Guy Benyei11169dd2012-12-18 14:30:41 +00007933 Current = IdTable->key_begin();
7934 End = IdTable->key_end();
7935 }
7936
7937 // We have any identifiers remaining in the current AST file; return
7938 // the next one.
Douglas Gregorbfd73d72013-01-23 18:53:14 +00007939 StringRef Result = *Current;
Guy Benyei11169dd2012-12-18 14:30:41 +00007940 ++Current;
Douglas Gregorbfd73d72013-01-23 18:53:14 +00007941 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00007942}
7943
Ben Langmuir537c5b52016-05-04 00:53:13 +00007944namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00007945
Ben Langmuir537c5b52016-05-04 00:53:13 +00007946/// A utility for appending two IdentifierIterators.
7947class ChainedIdentifierIterator : public IdentifierIterator {
7948 std::unique_ptr<IdentifierIterator> Current;
7949 std::unique_ptr<IdentifierIterator> Queued;
7950
7951public:
7952 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
7953 std::unique_ptr<IdentifierIterator> Second)
7954 : Current(std::move(First)), Queued(std::move(Second)) {}
7955
7956 StringRef Next() override {
7957 if (!Current)
Vedant Kumar48b4f762018-04-14 01:40:48 +00007958 return StringRef();
Ben Langmuir537c5b52016-05-04 00:53:13 +00007959
7960 StringRef result = Current->Next();
7961 if (!result.empty())
7962 return result;
7963
7964 // Try the queued iterator, which may itself be empty.
7965 Current.reset();
7966 std::swap(Current, Queued);
7967 return Next();
7968 }
7969};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00007970
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007971} // namespace
Ben Langmuir537c5b52016-05-04 00:53:13 +00007972
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00007973IdentifierIterator *ASTReader::getIdentifiers() {
Ben Langmuir537c5b52016-05-04 00:53:13 +00007974 if (!loadGlobalIndex()) {
7975 std::unique_ptr<IdentifierIterator> ReaderIter(
7976 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
7977 std::unique_ptr<IdentifierIterator> ModulesIter(
7978 GlobalIndex->createIdentifierIterator());
7979 return new ChainedIdentifierIterator(std::move(ReaderIter),
7980 std::move(ModulesIter));
7981 }
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00007982
Guy Benyei11169dd2012-12-18 14:30:41 +00007983 return new ASTIdentifierIterator(*this);
7984}
7985
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00007986namespace clang {
7987namespace serialization {
7988
Guy Benyei11169dd2012-12-18 14:30:41 +00007989 class ReadMethodPoolVisitor {
7990 ASTReader &Reader;
7991 Selector Sel;
7992 unsigned PriorGeneration;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007993 unsigned InstanceBits = 0;
7994 unsigned FactoryBits = 0;
7995 bool InstanceHasMoreThanOneDecl = false;
7996 bool FactoryHasMoreThanOneDecl = false;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00007997 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
7998 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00007999
8000 public:
Nico Weber2e0c8f72014-12-27 03:58:08 +00008001 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
Guy Benyei11169dd2012-12-18 14:30:41 +00008002 unsigned PriorGeneration)
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008003 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
Nico Weber2e0c8f72014-12-27 03:58:08 +00008004
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00008005 bool operator()(ModuleFile &M) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008006 if (!M.SelectorLookupTable)
8007 return false;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008008
Guy Benyei11169dd2012-12-18 14:30:41 +00008009 // If we've already searched this module file, skip it now.
Richard Smithbdf2d932015-07-30 03:37:16 +00008010 if (M.Generation <= PriorGeneration)
Guy Benyei11169dd2012-12-18 14:30:41 +00008011 return true;
8012
Richard Smithbdf2d932015-07-30 03:37:16 +00008013 ++Reader.NumMethodPoolTableLookups;
Vedant Kumar48b4f762018-04-14 01:40:48 +00008014 ASTSelectorLookupTable *PoolTable
8015 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
Richard Smithbdf2d932015-07-30 03:37:16 +00008016 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Guy Benyei11169dd2012-12-18 14:30:41 +00008017 if (Pos == PoolTable->end())
8018 return false;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00008019
Richard Smithbdf2d932015-07-30 03:37:16 +00008020 ++Reader.NumMethodPoolTableHits;
8021 ++Reader.NumSelectorsRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00008022 // FIXME: Not quite happy with the statistics here. We probably should
8023 // disable this tracking when called via LoadSelector.
8024 // Also, should entries without methods count as misses?
Richard Smithbdf2d932015-07-30 03:37:16 +00008025 ++Reader.NumMethodPoolEntriesRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00008026 ASTSelectorLookupTrait::data_type Data = *Pos;
Richard Smithbdf2d932015-07-30 03:37:16 +00008027 if (Reader.DeserializationListener)
8028 Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00008029
Richard Smithbdf2d932015-07-30 03:37:16 +00008030 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8031 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8032 InstanceBits = Data.InstanceBits;
8033 FactoryBits = Data.FactoryBits;
8034 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8035 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00008036 return true;
8037 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008038
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008039 /// Retrieve the instance methods found by this visitor.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008040 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8041 return InstanceMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00008042 }
8043
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008044 /// Retrieve the instance methods found by this visitor.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008045 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
Guy Benyei11169dd2012-12-18 14:30:41 +00008046 return FactoryMethods;
8047 }
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00008048
8049 unsigned getInstanceBits() const { return InstanceBits; }
8050 unsigned getFactoryBits() const { return FactoryBits; }
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008051
Nico Weberff4b35e2014-12-27 22:14:15 +00008052 bool instanceHasMoreThanOneDecl() const {
8053 return InstanceHasMoreThanOneDecl;
8054 }
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008055
Nico Weberff4b35e2014-12-27 22:14:15 +00008056 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
Guy Benyei11169dd2012-12-18 14:30:41 +00008057 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00008058
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008059} // namespace serialization
8060} // namespace clang
Guy Benyei11169dd2012-12-18 14:30:41 +00008061
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008062/// Add the given set of methods to the method list.
Guy Benyei11169dd2012-12-18 14:30:41 +00008063static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8064 ObjCMethodList &List) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008065 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8066 S.addMethodToGlobalList(&List, Methods[I]);
8067 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008068}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008069
Guy Benyei11169dd2012-12-18 14:30:41 +00008070void ASTReader::ReadMethodPool(Selector Sel) {
8071 // Get the selector generation and update it to the current generation.
8072 unsigned &Generation = SelectorGeneration[Sel];
8073 unsigned PriorGeneration = Generation;
Richard Smith053f6c62014-05-16 23:01:30 +00008074 Generation = getGeneration();
Manman Rena0f31a02016-04-29 19:04:05 +00008075 SelectorOutOfDate[Sel] = false;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008076
Guy Benyei11169dd2012-12-18 14:30:41 +00008077 // Search for methods defined with this selector.
Douglas Gregorad2f7a52013-01-28 17:54:36 +00008078 ++NumMethodPoolLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00008079 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00008080 ModuleMgr.visit(Visitor);
8081
Guy Benyei11169dd2012-12-18 14:30:41 +00008082 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregorad2f7a52013-01-28 17:54:36 +00008083 Visitor.getFactoryMethods().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00008084 return;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00008085
8086 ++NumMethodPoolHits;
8087
Guy Benyei11169dd2012-12-18 14:30:41 +00008088 if (!getSema())
8089 return;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008090
Guy Benyei11169dd2012-12-18 14:30:41 +00008091 Sema &S = *getSema();
8092 Sema::GlobalMethodPool::iterator Pos
8093 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
Ben Langmuira0c32e92015-01-12 19:27:00 +00008094
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00008095 Pos->second.first.setBits(Visitor.getInstanceBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00008096 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00008097 Pos->second.second.setBits(Visitor.getFactoryBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00008098 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
Ben Langmuira0c32e92015-01-12 19:27:00 +00008099
8100 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8101 // when building a module we keep every method individually and may need to
8102 // update hasMoreThanOneDecl as we add the methods.
8103 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8104 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Guy Benyei11169dd2012-12-18 14:30:41 +00008105}
8106
Manman Rena0f31a02016-04-29 19:04:05 +00008107void ASTReader::updateOutOfDateSelector(Selector Sel) {
8108 if (SelectorOutOfDate[Sel])
8109 ReadMethodPool(Sel);
8110}
8111
Guy Benyei11169dd2012-12-18 14:30:41 +00008112void ASTReader::ReadKnownNamespaces(
8113 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8114 Namespaces.clear();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008115
Vedant Kumar48b4f762018-04-14 01:40:48 +00008116 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8117 if (NamespaceDecl *Namespace
8118 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
Guy Benyei11169dd2012-12-18 14:30:41 +00008119 Namespaces.push_back(Namespace);
Vedant Kumar48b4f762018-04-14 01:40:48 +00008120 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008121}
8122
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00008123void ASTReader::ReadUndefinedButUsed(
Richard Smithd6a04d72016-03-25 21:49:43 +00008124 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00008125 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008126 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky8334af82013-01-26 00:35:08 +00008127 SourceLocation Loc =
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00008128 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky8334af82013-01-26 00:35:08 +00008129 Undefined.insert(std::make_pair(D, Loc));
8130 }
8131}
Nick Lewycky8334af82013-01-26 00:35:08 +00008132
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00008133void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8134 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8135 Exprs) {
8136 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008137 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00008138 uint64_t Count = DelayedDeleteExprs[Idx++];
8139 for (uint64_t C = 0; C < Count; ++C) {
8140 SourceLocation DeleteLoc =
8141 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8142 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8143 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8144 }
8145 }
8146}
8147
Guy Benyei11169dd2012-12-18 14:30:41 +00008148void ASTReader::ReadTentativeDefinitions(
8149 SmallVectorImpl<VarDecl *> &TentativeDefs) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008150 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8151 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008152 if (Var)
8153 TentativeDefs.push_back(Var);
8154 }
8155 TentativeDefinitions.clear();
8156}
8157
8158void ASTReader::ReadUnusedFileScopedDecls(
8159 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008160 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8161 DeclaratorDecl *D
8162 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008163 if (D)
8164 Decls.push_back(D);
8165 }
8166 UnusedFileScopedDecls.clear();
8167}
8168
8169void ASTReader::ReadDelegatingConstructors(
8170 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008171 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8172 CXXConstructorDecl *D
8173 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008174 if (D)
8175 Decls.push_back(D);
8176 }
8177 DelegatingCtorDecls.clear();
8178}
8179
8180void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008181 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8182 TypedefNameDecl *D
8183 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008184 if (D)
8185 Decls.push_back(D);
8186 }
8187 ExtVectorDecls.clear();
8188}
8189
Nico Weber72889432014-09-06 01:25:55 +00008190void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8191 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008192 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8193 ++I) {
8194 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8195 GetDecl(UnusedLocalTypedefNameCandidates[I]));
Nico Weber72889432014-09-06 01:25:55 +00008196 if (D)
8197 Decls.insert(D);
8198 }
8199 UnusedLocalTypedefNameCandidates.clear();
8200}
8201
Yaxun (Sam) Liub670ab72020-02-26 10:57:39 -05008202void ASTReader::ReadDeclsToCheckForDeferredDiags(
8203 llvm::SmallVector<Decl *, 4> &Decls) {
8204 for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N;
8205 ++I) {
8206 auto *D = dyn_cast_or_null<Decl>(
8207 GetDecl(DeclsToCheckForDeferredDiags[I]));
8208 if (D)
8209 Decls.push_back(D);
8210 }
8211 DeclsToCheckForDeferredDiags.clear();
8212}
8213
8214
Guy Benyei11169dd2012-12-18 14:30:41 +00008215void ASTReader::ReadReferencedSelectors(
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008216 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008217 if (ReferencedSelectorsData.empty())
8218 return;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008219
Guy Benyei11169dd2012-12-18 14:30:41 +00008220 // If there are @selector references added them to its pool. This is for
8221 // implementation of -Wselector.
8222 unsigned int DataSize = ReferencedSelectorsData.size()-1;
8223 unsigned I = 0;
8224 while (I < DataSize) {
8225 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8226 SourceLocation SelLoc
8227 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8228 Sels.push_back(std::make_pair(Sel, SelLoc));
8229 }
8230 ReferencedSelectorsData.clear();
8231}
8232
8233void ASTReader::ReadWeakUndeclaredIdentifiers(
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008234 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008235 if (WeakUndeclaredIdentifiers.empty())
8236 return;
8237
8238 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008239 IdentifierInfo *WeakId
Guy Benyei11169dd2012-12-18 14:30:41 +00008240 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008241 IdentifierInfo *AliasId
Guy Benyei11169dd2012-12-18 14:30:41 +00008242 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8243 SourceLocation Loc
8244 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8245 bool Used = WeakUndeclaredIdentifiers[I++];
8246 WeakInfo WI(AliasId, Loc);
8247 WI.setUsed(Used);
8248 WeakIDs.push_back(std::make_pair(WeakId, WI));
8249 }
8250 WeakUndeclaredIdentifiers.clear();
8251}
8252
8253void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8254 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8255 ExternalVTableUse VT;
8256 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8257 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8258 VT.DefinitionRequired = VTableUses[Idx++];
8259 VTables.push_back(VT);
8260 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008261
Guy Benyei11169dd2012-12-18 14:30:41 +00008262 VTableUses.clear();
8263}
8264
8265void ASTReader::ReadPendingInstantiations(
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008266 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008267 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008268 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008269 SourceLocation Loc
8270 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8271
8272 Pending.push_back(std::make_pair(D, Loc));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008273 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008274 PendingInstantiations.clear();
8275}
8276
Richard Smithe40f2ba2013-08-07 21:41:30 +00008277void ASTReader::ReadLateParsedTemplates(
Justin Lebar28f09c52016-10-10 16:26:08 +00008278 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8279 &LPTMap) {
Richard Smithe40f2ba2013-08-07 21:41:30 +00008280 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8281 /* In loop */) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008282 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
Richard Smithe40f2ba2013-08-07 21:41:30 +00008283
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00008284 auto LT = std::make_unique<LateParsedTemplate>();
Richard Smithe40f2ba2013-08-07 21:41:30 +00008285 LT->D = GetDecl(LateParsedTemplates[Idx++]);
8286
8287 ModuleFile *F = getOwningModuleFile(LT->D);
8288 assert(F && "No module");
8289
8290 unsigned TokN = LateParsedTemplates[Idx++];
8291 LT->Toks.reserve(TokN);
8292 for (unsigned T = 0; T < TokN; ++T)
8293 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8294
Justin Lebar28f09c52016-10-10 16:26:08 +00008295 LPTMap.insert(std::make_pair(FD, std::move(LT)));
Richard Smithe40f2ba2013-08-07 21:41:30 +00008296 }
8297
8298 LateParsedTemplates.clear();
8299}
8300
Guy Benyei11169dd2012-12-18 14:30:41 +00008301void ASTReader::LoadSelector(Selector Sel) {
8302 // It would be complicated to avoid reading the methods anyway. So don't.
8303 ReadMethodPool(Sel);
8304}
8305
8306void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8307 assert(ID && "Non-zero identifier ID required");
8308 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8309 IdentifiersLoaded[ID - 1] = II;
8310 if (DeserializationListener)
8311 DeserializationListener->IdentifierRead(ID, II);
8312}
8313
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008314/// Set the globally-visible declarations associated with the given
Guy Benyei11169dd2012-12-18 14:30:41 +00008315/// identifier.
8316///
8317/// If the AST reader is currently in a state where the given declaration IDs
8318/// cannot safely be resolved, they are queued until it is safe to resolve
8319/// them.
8320///
8321/// \param II an IdentifierInfo that refers to one or more globally-visible
8322/// declarations.
8323///
8324/// \param DeclIDs the set of declaration IDs with the name @p II that are
8325/// visible at global scope.
8326///
Douglas Gregor6168bd22013-02-18 15:53:43 +00008327/// \param Decls if non-null, this vector will be populated with the set of
8328/// deserialized declarations. These declarations will not be pushed into
8329/// scope.
Guy Benyei11169dd2012-12-18 14:30:41 +00008330void
8331ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8332 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor6168bd22013-02-18 15:53:43 +00008333 SmallVectorImpl<Decl *> *Decls) {
8334 if (NumCurrentElementsDeserializing && !Decls) {
8335 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00008336 return;
8337 }
8338
Vedant Kumar48b4f762018-04-14 01:40:48 +00008339 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
Ben Langmuir5418f402014-09-10 21:29:41 +00008340 if (!SemaObj) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008341 // Queue this declaration so that it will be added to the
8342 // translation unit scope and identifier's declaration chain
8343 // once a Sema object is known.
Vedant Kumar48b4f762018-04-14 01:40:48 +00008344 PreloadedDeclIDs.push_back(DeclIDs[I]);
Ben Langmuir5418f402014-09-10 21:29:41 +00008345 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00008346 }
Ben Langmuir5418f402014-09-10 21:29:41 +00008347
Vedant Kumar48b4f762018-04-14 01:40:48 +00008348 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
Ben Langmuir5418f402014-09-10 21:29:41 +00008349
8350 // If we're simply supposed to record the declarations, do so now.
8351 if (Decls) {
8352 Decls->push_back(D);
8353 continue;
8354 }
8355
8356 // Introduce this declaration into the translation-unit scope
8357 // and add it to the declaration chain for this identifier, so
8358 // that (unqualified) name lookup will find it.
8359 pushExternalDeclIntoScope(D, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00008360 }
8361}
8362
Douglas Gregorc8a992f2013-01-21 16:52:34 +00008363IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008364 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00008365 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008366
8367 if (IdentifiersLoaded.empty()) {
8368 Error("no identifier table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00008369 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008370 }
8371
8372 ID -= 1;
8373 if (!IdentifiersLoaded[ID]) {
8374 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8375 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8376 ModuleFile *M = I->second;
8377 unsigned Index = ID - M->BaseIdentifierID;
8378 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8379
8380 // All of the strings in the AST file are preceded by a 16-bit length.
8381 // Extract that 16-bit length to avoid having to execute strlen().
8382 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8383 // unsigned integers. This is important to avoid integer overflow when
8384 // we cast them to 'unsigned'.
8385 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8386 unsigned StrLen = (((unsigned) StrLenPtr[0])
8387 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Richard Smitheb4b58f62016-02-05 01:40:54 +00008388 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8389 IdentifiersLoaded[ID] = &II;
8390 markIdentifierFromAST(*this, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00008391 if (DeserializationListener)
Richard Smitheb4b58f62016-02-05 01:40:54 +00008392 DeserializationListener->IdentifierRead(ID + 1, &II);
Guy Benyei11169dd2012-12-18 14:30:41 +00008393 }
8394
8395 return IdentifiersLoaded[ID];
8396}
8397
Douglas Gregorc8a992f2013-01-21 16:52:34 +00008398IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8399 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei11169dd2012-12-18 14:30:41 +00008400}
8401
8402IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8403 if (LocalID < NUM_PREDEF_IDENT_IDS)
8404 return LocalID;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008405
Richard Smith37a93df2017-02-18 00:32:02 +00008406 if (!M.ModuleOffsetMap.empty())
8407 ReadModuleOffsetMap(M);
8408
Guy Benyei11169dd2012-12-18 14:30:41 +00008409 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8410 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008411 assert(I != M.IdentifierRemap.end()
Guy Benyei11169dd2012-12-18 14:30:41 +00008412 && "Invalid index into identifier index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008413
Guy Benyei11169dd2012-12-18 14:30:41 +00008414 return LocalID + I->second;
8415}
8416
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008417MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008418 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00008419 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008420
8421 if (MacrosLoaded.empty()) {
8422 Error("no macro table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00008423 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008424 }
8425
8426 ID -= NUM_PREDEF_MACRO_IDS;
8427 if (!MacrosLoaded[ID]) {
8428 GlobalMacroMapType::iterator I
8429 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8430 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8431 ModuleFile *M = I->second;
8432 unsigned Index = ID - M->BaseMacroID;
Dmitry Polukhin30d59462020-03-21 13:52:51 -07008433 MacrosLoaded[ID] =
8434 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008435
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008436 if (DeserializationListener)
8437 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8438 MacrosLoaded[ID]);
Guy Benyei11169dd2012-12-18 14:30:41 +00008439 }
8440
8441 return MacrosLoaded[ID];
8442}
8443
8444MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8445 if (LocalID < NUM_PREDEF_MACRO_IDS)
8446 return LocalID;
8447
Richard Smith37a93df2017-02-18 00:32:02 +00008448 if (!M.ModuleOffsetMap.empty())
8449 ReadModuleOffsetMap(M);
8450
Guy Benyei11169dd2012-12-18 14:30:41 +00008451 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8452 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8453 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8454
8455 return LocalID + I->second;
8456}
8457
8458serialization::SubmoduleID
8459ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8460 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8461 return LocalID;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008462
Richard Smith37a93df2017-02-18 00:32:02 +00008463 if (!M.ModuleOffsetMap.empty())
8464 ReadModuleOffsetMap(M);
8465
Guy Benyei11169dd2012-12-18 14:30:41 +00008466 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8467 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008468 assert(I != M.SubmoduleRemap.end()
Guy Benyei11169dd2012-12-18 14:30:41 +00008469 && "Invalid index into submodule index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008470
Guy Benyei11169dd2012-12-18 14:30:41 +00008471 return LocalID + I->second;
8472}
8473
8474Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8475 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8476 assert(GlobalID == 0 && "Unhandled global submodule ID");
Craig Toppera13603a2014-05-22 05:54:18 +00008477 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008478 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008479
Guy Benyei11169dd2012-12-18 14:30:41 +00008480 if (GlobalID > SubmodulesLoaded.size()) {
8481 Error("submodule ID out of range in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00008482 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008483 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008484
Guy Benyei11169dd2012-12-18 14:30:41 +00008485 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8486}
Douglas Gregorc147b0b2013-01-12 01:29:50 +00008487
8488Module *ASTReader::getModule(unsigned ID) {
8489 return getSubmodule(ID);
8490}
8491
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00008492bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8493 ModuleFile *MF = getOwningModuleFile(D);
8494 return MF && MF->PCHHasObjectFile;
8495}
8496
Richard Smithd88a7f12015-09-01 20:35:42 +00008497ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8498 if (ID & 1) {
8499 // It's a module, look it up by submodule ID.
8500 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8501 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8502 } else {
8503 // It's a prefix (preamble, PCH, ...). Look it up by index.
8504 unsigned IndexFromEnd = ID >> 1;
8505 assert(IndexFromEnd && "got reference to unknown module file");
8506 return getModuleManager().pch_modules().end()[-IndexFromEnd];
8507 }
8508}
8509
8510unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8511 if (!F)
8512 return 1;
8513
8514 // For a file representing a module, use the submodule ID of the top-level
8515 // module as the file ID. For any other kind of file, the number of such
8516 // files loaded beforehand will be the same on reload.
8517 // FIXME: Is this true even if we have an explicit module file and a PCH?
8518 if (F->isModule())
8519 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8520
8521 auto PCHModules = getModuleManager().pch_modules();
Fangrui Song75e74e02019-03-31 08:48:19 +00008522 auto I = llvm::find(PCHModules, F);
Richard Smithd88a7f12015-09-01 20:35:42 +00008523 assert(I != PCHModules.end() && "emitting reference to unknown file");
8524 return (I - PCHModules.end()) << 1;
8525}
8526
Reid Klecknerc915cb92020-02-27 18:13:54 -08008527llvm::Optional<ASTSourceDescriptor>
Adrian Prantl15bcf702015-06-30 17:39:43 +00008528ASTReader::getSourceDescriptor(unsigned ID) {
Adrian Prantlf4754ea2020-03-17 12:51:58 -07008529 if (Module *M = getSubmodule(ID))
Reid Klecknerc915cb92020-02-27 18:13:54 -08008530 return ASTSourceDescriptor(*M);
Adrian Prantl15bcf702015-06-30 17:39:43 +00008531
8532 // If there is only a single PCH, return it instead.
Hiroshi Inoue3170de02017-07-01 08:46:43 +00008533 // Chained PCH are not supported.
Saleem Abdulrasool97d25552017-03-02 17:37:11 +00008534 const auto &PCHChain = ModuleMgr.pch_modules();
8535 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
Adrian Prantl15bcf702015-06-30 17:39:43 +00008536 ModuleFile &MF = ModuleMgr.getPrimaryModule();
Adrian Prantl3a2d4942016-01-22 23:30:56 +00008537 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
Adrian Prantl9bc3c4f2016-04-27 17:06:22 +00008538 StringRef FileName = llvm::sys::path::filename(MF.FileName);
Reid Klecknerc915cb92020-02-27 18:13:54 -08008539 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8540 MF.Signature);
Adrian Prantl15bcf702015-06-30 17:39:43 +00008541 }
8542 return None;
8543}
8544
David Blaikie1ac9c982017-04-11 21:13:37 +00008545ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
Richard Smitha4653622017-09-06 20:01:14 +00008546 auto I = DefinitionSource.find(FD);
8547 if (I == DefinitionSource.end())
David Blaikie9ffe5a32017-01-30 05:00:26 +00008548 return EK_ReplyHazy;
David Blaikiee6b7c282017-04-11 20:46:34 +00008549 return I->second ? EK_Never : EK_Always;
David Blaikie9ffe5a32017-01-30 05:00:26 +00008550}
8551
Guy Benyei11169dd2012-12-18 14:30:41 +00008552Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8553 return DecodeSelector(getGlobalSelectorID(M, LocalID));
8554}
8555
8556Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8557 if (ID == 0)
Vedant Kumar48b4f762018-04-14 01:40:48 +00008558 return Selector();
Guy Benyei11169dd2012-12-18 14:30:41 +00008559
8560 if (ID > SelectorsLoaded.size()) {
8561 Error("selector ID out of range in AST file");
Vedant Kumar48b4f762018-04-14 01:40:48 +00008562 return Selector();
Guy Benyei11169dd2012-12-18 14:30:41 +00008563 }
8564
Craig Toppera13603a2014-05-22 05:54:18 +00008565 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008566 // Load this selector from the selector table.
8567 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8568 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8569 ModuleFile &M = *I->second;
8570 ASTSelectorLookupTrait Trait(*this, M);
8571 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8572 SelectorsLoaded[ID - 1] =
8573 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8574 if (DeserializationListener)
8575 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8576 }
8577
8578 return SelectorsLoaded[ID - 1];
8579}
8580
8581Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8582 return DecodeSelector(ID);
8583}
8584
8585uint32_t ASTReader::GetNumExternalSelectors() {
8586 // ID 0 (the null selector) is considered an external selector.
8587 return getTotalNumSelectors() + 1;
8588}
8589
8590serialization::SelectorID
8591ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8592 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8593 return LocalID;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008594
Richard Smith37a93df2017-02-18 00:32:02 +00008595 if (!M.ModuleOffsetMap.empty())
8596 ReadModuleOffsetMap(M);
8597
Guy Benyei11169dd2012-12-18 14:30:41 +00008598 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8599 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008600 assert(I != M.SelectorRemap.end()
Guy Benyei11169dd2012-12-18 14:30:41 +00008601 && "Invalid index into selector index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008602
Guy Benyei11169dd2012-12-18 14:30:41 +00008603 return LocalID + I->second;
8604}
8605
John McCall3ce3d232019-12-13 03:37:23 -05008606DeclarationNameLoc
8607ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8608 DeclarationNameLoc DNLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00008609 switch (Name.getNameKind()) {
8610 case DeclarationName::CXXConstructorName:
8611 case DeclarationName::CXXDestructorName:
8612 case DeclarationName::CXXConversionFunctionName:
John McCall3ce3d232019-12-13 03:37:23 -05008613 DNLoc.NamedType.TInfo = readTypeSourceInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +00008614 break;
8615
8616 case DeclarationName::CXXOperatorName:
8617 DNLoc.CXXOperatorName.BeginOpNameLoc
John McCall3ce3d232019-12-13 03:37:23 -05008618 = readSourceLocation().getRawEncoding();
Guy Benyei11169dd2012-12-18 14:30:41 +00008619 DNLoc.CXXOperatorName.EndOpNameLoc
John McCall3ce3d232019-12-13 03:37:23 -05008620 = readSourceLocation().getRawEncoding();
Guy Benyei11169dd2012-12-18 14:30:41 +00008621 break;
8622
8623 case DeclarationName::CXXLiteralOperatorName:
8624 DNLoc.CXXLiteralOperatorName.OpNameLoc
John McCall3ce3d232019-12-13 03:37:23 -05008625 = readSourceLocation().getRawEncoding();
Guy Benyei11169dd2012-12-18 14:30:41 +00008626 break;
8627
8628 case DeclarationName::Identifier:
8629 case DeclarationName::ObjCZeroArgSelector:
8630 case DeclarationName::ObjCOneArgSelector:
8631 case DeclarationName::ObjCMultiArgSelector:
8632 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00008633 case DeclarationName::CXXDeductionGuideName:
Guy Benyei11169dd2012-12-18 14:30:41 +00008634 break;
8635 }
John McCall3ce3d232019-12-13 03:37:23 -05008636 return DNLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00008637}
8638
John McCall3ce3d232019-12-13 03:37:23 -05008639DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8640 DeclarationNameInfo NameInfo;
8641 NameInfo.setName(readDeclarationName());
8642 NameInfo.setLoc(readSourceLocation());
8643 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8644 return NameInfo;
Guy Benyei11169dd2012-12-18 14:30:41 +00008645}
8646
John McCall3ce3d232019-12-13 03:37:23 -05008647void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8648 Info.QualifierLoc = readNestedNameSpecifierLoc();
8649 unsigned NumTPLists = readInt();
Guy Benyei11169dd2012-12-18 14:30:41 +00008650 Info.NumTemplParamLists = NumTPLists;
8651 if (NumTPLists) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00008652 Info.TemplParamLists =
8653 new (getContext()) TemplateParameterList *[NumTPLists];
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00008654 for (unsigned i = 0; i != NumTPLists; ++i)
John McCall3ce3d232019-12-13 03:37:23 -05008655 Info.TemplParamLists[i] = readTemplateParameterList();
Guy Benyei11169dd2012-12-18 14:30:41 +00008656 }
8657}
8658
Guy Benyei11169dd2012-12-18 14:30:41 +00008659TemplateParameterList *
John McCall3ce3d232019-12-13 03:37:23 -05008660ASTRecordReader::readTemplateParameterList() {
8661 SourceLocation TemplateLoc = readSourceLocation();
8662 SourceLocation LAngleLoc = readSourceLocation();
8663 SourceLocation RAngleLoc = readSourceLocation();
Guy Benyei11169dd2012-12-18 14:30:41 +00008664
John McCall3ce3d232019-12-13 03:37:23 -05008665 unsigned NumParams = readInt();
Guy Benyei11169dd2012-12-18 14:30:41 +00008666 SmallVector<NamedDecl *, 16> Params;
8667 Params.reserve(NumParams);
8668 while (NumParams--)
John McCall3ce3d232019-12-13 03:37:23 -05008669 Params.push_back(readDeclAs<NamedDecl>());
Guy Benyei11169dd2012-12-18 14:30:41 +00008670
John McCall3ce3d232019-12-13 03:37:23 -05008671 bool HasRequiresClause = readBool();
8672 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
Saar Raz0330fba2019-10-15 18:44:06 +00008673
Richard Smithdbafb6c2017-06-29 23:23:46 +00008674 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
Saar Raz0330fba2019-10-15 18:44:06 +00008675 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
Guy Benyei11169dd2012-12-18 14:30:41 +00008676 return TemplateParams;
8677}
8678
John McCall3ce3d232019-12-13 03:37:23 -05008679void ASTRecordReader::readTemplateArgumentList(
8680 SmallVectorImpl<TemplateArgument> &TemplArgs,
8681 bool Canonicalize) {
8682 unsigned NumTemplateArgs = readInt();
Guy Benyei11169dd2012-12-18 14:30:41 +00008683 TemplArgs.reserve(NumTemplateArgs);
8684 while (NumTemplateArgs--)
John McCall3ce3d232019-12-13 03:37:23 -05008685 TemplArgs.push_back(readTemplateArgument(Canonicalize));
Guy Benyei11169dd2012-12-18 14:30:41 +00008686}
8687
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008688/// Read a UnresolvedSet structure.
John McCall3ce3d232019-12-13 03:37:23 -05008689void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8690 unsigned NumDecls = readInt();
Richard Smithdbafb6c2017-06-29 23:23:46 +00008691 Set.reserve(getContext(), NumDecls);
Guy Benyei11169dd2012-12-18 14:30:41 +00008692 while (NumDecls--) {
John McCall3ce3d232019-12-13 03:37:23 -05008693 DeclID ID = readDeclID();
8694 AccessSpecifier AS = (AccessSpecifier) readInt();
Richard Smithdbafb6c2017-06-29 23:23:46 +00008695 Set.addLazyDecl(getContext(), ID, AS);
Guy Benyei11169dd2012-12-18 14:30:41 +00008696 }
8697}
8698
8699CXXBaseSpecifier
John McCall3ce3d232019-12-13 03:37:23 -05008700ASTRecordReader::readCXXBaseSpecifier() {
8701 bool isVirtual = readBool();
8702 bool isBaseOfClass = readBool();
8703 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8704 bool inheritConstructors = readBool();
8705 TypeSourceInfo *TInfo = readTypeSourceInfo();
8706 SourceRange Range = readSourceRange();
8707 SourceLocation EllipsisLoc = readSourceLocation();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008708 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Guy Benyei11169dd2012-12-18 14:30:41 +00008709 EllipsisLoc);
8710 Result.setInheritConstructors(inheritConstructors);
8711 return Result;
8712}
8713
Richard Smithc2bb8182015-03-24 06:36:48 +00008714CXXCtorInitializer **
John McCall3ce3d232019-12-13 03:37:23 -05008715ASTRecordReader::readCXXCtorInitializers() {
Richard Smithdbafb6c2017-06-29 23:23:46 +00008716 ASTContext &Context = getContext();
John McCall3ce3d232019-12-13 03:37:23 -05008717 unsigned NumInitializers = readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00008718 assert(NumInitializers && "wrote ctor initializers but have no inits");
Vedant Kumar48b4f762018-04-14 01:40:48 +00008719 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
Richard Smithc2bb8182015-03-24 06:36:48 +00008720 for (unsigned i = 0; i != NumInitializers; ++i) {
8721 TypeSourceInfo *TInfo = nullptr;
8722 bool IsBaseVirtual = false;
8723 FieldDecl *Member = nullptr;
8724 IndirectFieldDecl *IndirectMember = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008725
John McCall3ce3d232019-12-13 03:37:23 -05008726 CtorInitializerType Type = (CtorInitializerType) readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00008727 switch (Type) {
8728 case CTOR_INITIALIZER_BASE:
John McCall3ce3d232019-12-13 03:37:23 -05008729 TInfo = readTypeSourceInfo();
8730 IsBaseVirtual = readBool();
Richard Smithc2bb8182015-03-24 06:36:48 +00008731 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008732
Richard Smithc2bb8182015-03-24 06:36:48 +00008733 case CTOR_INITIALIZER_DELEGATING:
John McCall3ce3d232019-12-13 03:37:23 -05008734 TInfo = readTypeSourceInfo();
Richard Smithc2bb8182015-03-24 06:36:48 +00008735 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008736
Richard Smithc2bb8182015-03-24 06:36:48 +00008737 case CTOR_INITIALIZER_MEMBER:
John McCall3ce3d232019-12-13 03:37:23 -05008738 Member = readDeclAs<FieldDecl>();
Richard Smithc2bb8182015-03-24 06:36:48 +00008739 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008740
Richard Smithc2bb8182015-03-24 06:36:48 +00008741 case CTOR_INITIALIZER_INDIRECT_MEMBER:
John McCall3ce3d232019-12-13 03:37:23 -05008742 IndirectMember = readDeclAs<IndirectFieldDecl>();
Richard Smithc2bb8182015-03-24 06:36:48 +00008743 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008744 }
Richard Smithc2bb8182015-03-24 06:36:48 +00008745
John McCall3ce3d232019-12-13 03:37:23 -05008746 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8747 Expr *Init = readExpr();
8748 SourceLocation LParenLoc = readSourceLocation();
8749 SourceLocation RParenLoc = readSourceLocation();
Richard Smithc2bb8182015-03-24 06:36:48 +00008750
8751 CXXCtorInitializer *BOMInit;
Richard Smith30e304e2016-12-14 00:03:17 +00008752 if (Type == CTOR_INITIALIZER_BASE)
Richard Smithc2bb8182015-03-24 06:36:48 +00008753 BOMInit = new (Context)
8754 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8755 RParenLoc, MemberOrEllipsisLoc);
Richard Smith30e304e2016-12-14 00:03:17 +00008756 else if (Type == CTOR_INITIALIZER_DELEGATING)
Richard Smithc2bb8182015-03-24 06:36:48 +00008757 BOMInit = new (Context)
8758 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
Richard Smith30e304e2016-12-14 00:03:17 +00008759 else if (Member)
8760 BOMInit = new (Context)
8761 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8762 Init, RParenLoc);
8763 else
8764 BOMInit = new (Context)
8765 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8766 LParenLoc, Init, RParenLoc);
8767
John McCall3ce3d232019-12-13 03:37:23 -05008768 if (/*IsWritten*/readBool()) {
8769 unsigned SourceOrder = readInt();
Richard Smith30e304e2016-12-14 00:03:17 +00008770 BOMInit->setSourceOrder(SourceOrder);
Richard Smithc2bb8182015-03-24 06:36:48 +00008771 }
8772
Richard Smithc2bb8182015-03-24 06:36:48 +00008773 CtorInitializers[i] = BOMInit;
Guy Benyei11169dd2012-12-18 14:30:41 +00008774 }
8775
Richard Smithc2bb8182015-03-24 06:36:48 +00008776 return CtorInitializers;
Guy Benyei11169dd2012-12-18 14:30:41 +00008777}
8778
Guy Benyei11169dd2012-12-18 14:30:41 +00008779NestedNameSpecifierLoc
John McCall3ce3d232019-12-13 03:37:23 -05008780ASTRecordReader::readNestedNameSpecifierLoc() {
Richard Smithdbafb6c2017-06-29 23:23:46 +00008781 ASTContext &Context = getContext();
John McCall3ce3d232019-12-13 03:37:23 -05008782 unsigned N = readInt();
Guy Benyei11169dd2012-12-18 14:30:41 +00008783 NestedNameSpecifierLocBuilder Builder;
8784 for (unsigned I = 0; I != N; ++I) {
John McCalld505e572019-12-13 21:54:44 -05008785 auto Kind = readNestedNameSpecifierKind();
Guy Benyei11169dd2012-12-18 14:30:41 +00008786 switch (Kind) {
8787 case NestedNameSpecifier::Identifier: {
John McCall3ce3d232019-12-13 03:37:23 -05008788 IdentifierInfo *II = readIdentifier();
8789 SourceRange Range = readSourceRange();
Guy Benyei11169dd2012-12-18 14:30:41 +00008790 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8791 break;
8792 }
8793
8794 case NestedNameSpecifier::Namespace: {
John McCall3ce3d232019-12-13 03:37:23 -05008795 NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8796 SourceRange Range = readSourceRange();
Guy Benyei11169dd2012-12-18 14:30:41 +00008797 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8798 break;
8799 }
8800
8801 case NestedNameSpecifier::NamespaceAlias: {
John McCall3ce3d232019-12-13 03:37:23 -05008802 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8803 SourceRange Range = readSourceRange();
Guy Benyei11169dd2012-12-18 14:30:41 +00008804 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8805 break;
8806 }
8807
8808 case NestedNameSpecifier::TypeSpec:
8809 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall3ce3d232019-12-13 03:37:23 -05008810 bool Template = readBool();
8811 TypeSourceInfo *T = readTypeSourceInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +00008812 if (!T)
Vedant Kumar48b4f762018-04-14 01:40:48 +00008813 return NestedNameSpecifierLoc();
John McCall3ce3d232019-12-13 03:37:23 -05008814 SourceLocation ColonColonLoc = readSourceLocation();
Guy Benyei11169dd2012-12-18 14:30:41 +00008815
8816 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008817 Builder.Extend(Context,
Guy Benyei11169dd2012-12-18 14:30:41 +00008818 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8819 T->getTypeLoc(), ColonColonLoc);
8820 break;
8821 }
8822
8823 case NestedNameSpecifier::Global: {
John McCall3ce3d232019-12-13 03:37:23 -05008824 SourceLocation ColonColonLoc = readSourceLocation();
Guy Benyei11169dd2012-12-18 14:30:41 +00008825 Builder.MakeGlobal(Context, ColonColonLoc);
8826 break;
8827 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008828
8829 case NestedNameSpecifier::Super: {
John McCall3ce3d232019-12-13 03:37:23 -05008830 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8831 SourceRange Range = readSourceRange();
Nikola Smiljanic67860242014-09-26 00:28:20 +00008832 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8833 break;
8834 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008835 }
8836 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008837
Guy Benyei11169dd2012-12-18 14:30:41 +00008838 return Builder.getWithLocInContext(Context);
8839}
8840
8841SourceRange
8842ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8843 unsigned &Idx) {
8844 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8845 SourceLocation end = ReadSourceLocation(F, Record, Idx);
8846 return SourceRange(beg, end);
8847}
8848
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008849static FixedPointSemantics
8850ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
8851 unsigned &Idx) {
8852 unsigned Width = Record[Idx++];
8853 unsigned Scale = Record[Idx++];
8854 uint64_t Tmp = Record[Idx++];
8855 bool IsSigned = Tmp & 0x1;
8856 bool IsSaturated = Tmp & 0x2;
8857 bool HasUnsignedPadding = Tmp & 0x4;
8858 return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
8859 HasUnsignedPadding);
8860}
8861
John McCall3ce3d232019-12-13 03:37:23 -05008862static const llvm::fltSemantics &
8863readAPFloatSemantics(ASTRecordReader &reader) {
8864 return llvm::APFloatBase::EnumToSemantics(
8865 static_cast<llvm::APFloatBase::Semantics>(reader.readInt()));
8866}
8867
8868APValue ASTRecordReader::readAPValue() {
8869 unsigned Kind = readInt();
John McCalld505e572019-12-13 21:54:44 -05008870 switch ((APValue::ValueKind) Kind) {
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008871 case APValue::None:
8872 return APValue();
8873 case APValue::Indeterminate:
8874 return APValue::IndeterminateValue();
8875 case APValue::Int:
John McCall3ce3d232019-12-13 03:37:23 -05008876 return APValue(readAPSInt());
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008877 case APValue::Float: {
John McCall3ce3d232019-12-13 03:37:23 -05008878 const llvm::fltSemantics &FloatSema = readAPFloatSemantics(*this);
8879 return APValue(readAPFloat(FloatSema));
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008880 }
8881 case APValue::FixedPoint: {
8882 FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
John McCall3ce3d232019-12-13 03:37:23 -05008883 return APValue(APFixedPoint(readAPInt(), FPSema));
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008884 }
8885 case APValue::ComplexInt: {
John McCall3ce3d232019-12-13 03:37:23 -05008886 llvm::APSInt First = readAPSInt();
8887 return APValue(std::move(First), readAPSInt());
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008888 }
8889 case APValue::ComplexFloat: {
John McCall3ce3d232019-12-13 03:37:23 -05008890 const llvm::fltSemantics &FloatSema1 = readAPFloatSemantics(*this);
8891 llvm::APFloat First = readAPFloat(FloatSema1);
8892 const llvm::fltSemantics &FloatSema2 = readAPFloatSemantics(*this);
8893 return APValue(std::move(First), readAPFloat(FloatSema2));
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008894 }
8895 case APValue::LValue:
8896 case APValue::Vector:
8897 case APValue::Array:
8898 case APValue::Struct:
8899 case APValue::Union:
8900 case APValue::MemberPointer:
8901 case APValue::AddrLabelDiff:
8902 // TODO : Handle all these APValue::ValueKind.
8903 return APValue();
8904 }
8905 llvm_unreachable("Invalid APValue::ValueKind");
8906}
8907
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008908/// Read a floating-point value
John McCall3ce3d232019-12-13 03:37:23 -05008909llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
8910 return llvm::APFloat(Sem, readAPInt());
Guy Benyei11169dd2012-12-18 14:30:41 +00008911}
8912
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008913// Read a string
Guy Benyei11169dd2012-12-18 14:30:41 +00008914std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8915 unsigned Len = Record[Idx++];
8916 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8917 Idx += Len;
8918 return Result;
8919}
8920
Richard Smith7ed1bc92014-12-05 22:42:13 +00008921std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8922 unsigned &Idx) {
8923 std::string Filename = ReadString(Record, Idx);
8924 ResolveImportedPath(F, Filename);
8925 return Filename;
8926}
8927
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00008928std::string ASTReader::ReadPath(StringRef BaseDirectory,
8929 const RecordData &Record, unsigned &Idx) {
8930 std::string Filename = ReadString(Record, Idx);
8931 if (!BaseDirectory.empty())
8932 ResolveImportedPath(Filename, BaseDirectory);
8933 return Filename;
8934}
8935
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008936VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
Guy Benyei11169dd2012-12-18 14:30:41 +00008937 unsigned &Idx) {
8938 unsigned Major = Record[Idx++];
8939 unsigned Minor = Record[Idx++];
8940 unsigned Subminor = Record[Idx++];
8941 if (Minor == 0)
8942 return VersionTuple(Major);
8943 if (Subminor == 0)
8944 return VersionTuple(Major, Minor - 1);
8945 return VersionTuple(Major, Minor - 1, Subminor - 1);
8946}
8947
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008948CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Guy Benyei11169dd2012-12-18 14:30:41 +00008949 const RecordData &Record,
8950 unsigned &Idx) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008951 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Richard Smithdbafb6c2017-06-29 23:23:46 +00008952 return CXXTemporary::Create(getContext(), Decl);
Guy Benyei11169dd2012-12-18 14:30:41 +00008953}
8954
Richard Smith37a93df2017-02-18 00:32:02 +00008955DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00008956 return Diag(CurrentImportLoc, DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00008957}
8958
Richard Smith37a93df2017-02-18 00:32:02 +00008959DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
Guy Benyei11169dd2012-12-18 14:30:41 +00008960 return Diags.Report(Loc, DiagID);
8961}
8962
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008963/// Retrieve the identifier table associated with the
Guy Benyei11169dd2012-12-18 14:30:41 +00008964/// preprocessor.
8965IdentifierTable &ASTReader::getIdentifierTable() {
8966 return PP.getIdentifierTable();
8967}
8968
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008969/// Record that the given ID maps to the given switch-case
Guy Benyei11169dd2012-12-18 14:30:41 +00008970/// statement.
8971void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00008972 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
Guy Benyei11169dd2012-12-18 14:30:41 +00008973 "Already have a SwitchCase with this ID");
8974 (*CurrSwitchCaseStmts)[ID] = SC;
8975}
8976
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008977/// Retrieve the switch-case statement with the given ID.
Guy Benyei11169dd2012-12-18 14:30:41 +00008978SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00008979 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
Guy Benyei11169dd2012-12-18 14:30:41 +00008980 return (*CurrSwitchCaseStmts)[ID];
8981}
8982
8983void ASTReader::ClearSwitchCaseIDs() {
8984 CurrSwitchCaseStmts->clear();
8985}
8986
8987void ASTReader::ReadComments() {
Richard Smithdbafb6c2017-06-29 23:23:46 +00008988 ASTContext &Context = getContext();
Guy Benyei11169dd2012-12-18 14:30:41 +00008989 std::vector<RawComment *> Comments;
Vedant Kumar48b4f762018-04-14 01:40:48 +00008990 for (SmallVectorImpl<std::pair<BitstreamCursor,
8991 serialization::ModuleFile *>>::iterator
8992 I = CommentsCursors.begin(),
8993 E = CommentsCursors.end();
8994 I != E; ++I) {
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008995 Comments.clear();
Vedant Kumar48b4f762018-04-14 01:40:48 +00008996 BitstreamCursor &Cursor = I->first;
8997 serialization::ModuleFile &F = *I->second;
Guy Benyei11169dd2012-12-18 14:30:41 +00008998 SavedStreamPosition SavedPosition(Cursor);
8999
9000 RecordData Record;
9001 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00009002 Expected<llvm::BitstreamEntry> MaybeEntry =
9003 Cursor.advanceSkippingSubblocks(
9004 BitstreamCursor::AF_DontPopBlockAtEnd);
9005 if (!MaybeEntry) {
9006 Error(MaybeEntry.takeError());
9007 return;
9008 }
9009 llvm::BitstreamEntry Entry = MaybeEntry.get();
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00009010
Chris Lattner7fb3bef2013-01-20 00:56:42 +00009011 switch (Entry.Kind) {
9012 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9013 case llvm::BitstreamEntry::Error:
9014 Error("malformed block record in AST file");
9015 return;
9016 case llvm::BitstreamEntry::EndBlock:
9017 goto NextCursor;
9018 case llvm::BitstreamEntry::Record:
9019 // The interesting case.
Guy Benyei11169dd2012-12-18 14:30:41 +00009020 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00009021 }
9022
9023 // Read a record.
9024 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00009025 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9026 if (!MaybeComment) {
9027 Error(MaybeComment.takeError());
9028 return;
9029 }
9030 switch ((CommentRecordTypes)MaybeComment.get()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00009031 case COMMENTS_RAW_COMMENT: {
9032 unsigned Idx = 0;
9033 SourceRange SR = ReadSourceRange(F, Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00009034 RawComment::CommentKind Kind =
9035 (RawComment::CommentKind) Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00009036 bool IsTrailingComment = Record[Idx++];
9037 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00009038 Comments.push_back(new (Context) RawComment(
David L. Jones13d5a872018-03-02 00:07:45 +00009039 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
Guy Benyei11169dd2012-12-18 14:30:41 +00009040 break;
9041 }
9042 }
9043 }
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00009044 NextCursor:
Jan Korousf31d8df2019-08-13 18:11:44 +00009045 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9046 FileToOffsetToComment;
9047 for (RawComment *C : Comments) {
9048 SourceLocation CommentLoc = C->getBeginLoc();
9049 if (CommentLoc.isValid()) {
9050 std::pair<FileID, unsigned> Loc =
9051 SourceMgr.getDecomposedLoc(CommentLoc);
9052 if (Loc.first.isValid())
9053 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9054 }
9055 }
Guy Benyei11169dd2012-12-18 14:30:41 +00009056 }
Guy Benyei11169dd2012-12-18 14:30:41 +00009057}
9058
Argyrios Kyrtzidisa38cb202017-01-30 06:05:58 +00009059void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9060 bool IncludeSystem, bool Complain,
9061 llvm::function_ref<void(const serialization::InputFile &IF,
9062 bool isSystem)> Visitor) {
9063 unsigned NumUserInputs = MF.NumUserInputFiles;
9064 unsigned NumInputs = MF.InputFilesLoaded.size();
9065 assert(NumUserInputs <= NumInputs);
9066 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9067 for (unsigned I = 0; I < N; ++I) {
9068 bool IsSystem = I >= NumUserInputs;
9069 InputFile IF = getInputFile(MF, I+1, Complain);
9070 Visitor(IF, IsSystem);
9071 }
9072}
9073
Richard Smithf3f84612017-06-29 02:19:42 +00009074void ASTReader::visitTopLevelModuleMaps(
9075 serialization::ModuleFile &MF,
9076 llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9077 unsigned NumInputs = MF.InputFilesLoaded.size();
9078 for (unsigned I = 0; I < NumInputs; ++I) {
9079 InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9080 if (IFI.TopLevelModuleMap)
9081 // FIXME: This unnecessarily re-reads the InputFileInfo.
9082 if (auto *FE = getInputFile(MF, I + 1).getFile())
9083 Visitor(FE);
9084 }
9085}
9086
Richard Smithcd45dbc2014-04-19 03:48:30 +00009087std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9088 // If we know the owning module, use it.
Richard Smith42413142015-05-15 20:05:43 +00009089 if (Module *M = D->getImportedOwningModule())
Richard Smithcd45dbc2014-04-19 03:48:30 +00009090 return M->getFullModuleName();
9091
9092 // Otherwise, use the name of the top-level module the decl is within.
9093 if (ModuleFile *M = getOwningModuleFile(D))
9094 return M->ModuleName;
9095
9096 // Not from a module.
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00009097 return {};
Richard Smithcd45dbc2014-04-19 03:48:30 +00009098}
9099
Guy Benyei11169dd2012-12-18 14:30:41 +00009100void ASTReader::finishPendingActions() {
Richard Smitha62d1982018-08-03 01:00:01 +00009101 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
Richard Smith851072e2014-05-19 20:59:20 +00009102 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
Richard Smith2b9e3e32013-10-18 06:05:18 +00009103 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
Richard Smitha0ce9c42014-07-29 23:23:27 +00009104 !PendingUpdateRecords.empty()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00009105 // If any identifiers with corresponding top-level declarations have
9106 // been loaded, load those declarations now.
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00009107 using TopLevelDeclsMap =
9108 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
Craig Topper79be4cd2013-07-05 04:33:53 +00009109 TopLevelDeclsMap TopLevelDecls;
9110
Guy Benyei11169dd2012-12-18 14:30:41 +00009111 while (!PendingIdentifierInfos.empty()) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00009112 IdentifierInfo *II = PendingIdentifierInfos.back().first;
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00009113 SmallVector<uint32_t, 4> DeclIDs =
9114 std::move(PendingIdentifierInfos.back().second);
Douglas Gregorcb15f082013-02-19 18:26:28 +00009115 PendingIdentifierInfos.pop_back();
Douglas Gregor6168bd22013-02-18 15:53:43 +00009116
9117 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei11169dd2012-12-18 14:30:41 +00009118 }
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00009119
Richard Smitha62d1982018-08-03 01:00:01 +00009120 // Load each function type that we deferred loading because it was a
9121 // deduced type that might refer to a local type declared within itself.
9122 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9123 auto *FD = PendingFunctionTypes[I].first;
9124 FD->setType(GetType(PendingFunctionTypes[I].second));
9125
9126 // If we gave a function a deduced return type, remember that we need to
9127 // propagate that along the redeclaration chain.
9128 auto *DT = FD->getReturnType()->getContainedDeducedType();
9129 if (DT && DT->isDeduced())
9130 PendingDeducedTypeUpdates.insert(
9131 {FD->getCanonicalDecl(), FD->getReturnType()});
9132 }
9133 PendingFunctionTypes.clear();
9134
Richard Smith851072e2014-05-19 20:59:20 +00009135 // For each decl chain that we wanted to complete while deserializing, mark
9136 // it as "still needs to be completed".
Vedant Kumar48b4f762018-04-14 01:40:48 +00009137 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9138 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9139 }
Richard Smith851072e2014-05-19 20:59:20 +00009140 PendingIncompleteDeclChains.clear();
9141
Guy Benyei11169dd2012-12-18 14:30:41 +00009142 // Load pending declaration chains.
Vedant Kumar48b4f762018-04-14 01:40:48 +00009143 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
Richard Smitha62d1982018-08-03 01:00:01 +00009144 loadPendingDeclChain(PendingDeclChains[I].first,
9145 PendingDeclChains[I].second);
Guy Benyei11169dd2012-12-18 14:30:41 +00009146 PendingDeclChains.clear();
9147
Douglas Gregor6168bd22013-02-18 15:53:43 +00009148 // Make the most recent of the top-level declarations visible.
Vedant Kumar48b4f762018-04-14 01:40:48 +00009149 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9150 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9151 IdentifierInfo *II = TLD->first;
9152 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9153 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregor6168bd22013-02-18 15:53:43 +00009154 }
9155 }
9156
Guy Benyei11169dd2012-12-18 14:30:41 +00009157 // Load any pending macro definitions.
9158 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009159 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9160 SmallVector<PendingMacroInfo, 2> GlobalIDs;
9161 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9162 // Initialize the macro history from chained-PCHs ahead of module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00009163 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00009164 ++IDIdx) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009165 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Manman Ren11f2a472016-08-18 17:42:15 +00009166 if (!Info.M->isModule())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009167 resolvePendingMacro(II, Info);
9168 }
9169 // Handle module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00009170 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009171 ++IDIdx) {
9172 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Manman Ren11f2a472016-08-18 17:42:15 +00009173 if (Info.M->isModule())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009174 resolvePendingMacro(II, Info);
Guy Benyei11169dd2012-12-18 14:30:41 +00009175 }
9176 }
9177 PendingMacroIDs.clear();
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00009178
9179 // Wire up the DeclContexts for Decls that we delayed setting until
9180 // recursive loading is completed.
9181 while (!PendingDeclContextInfos.empty()) {
9182 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9183 PendingDeclContextInfos.pop_front();
Vedant Kumar48b4f762018-04-14 01:40:48 +00009184 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9185 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00009186 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9187 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00009188
Richard Smithd1c46742014-04-30 02:24:17 +00009189 // Perform any pending declaration updates.
Richard Smithd6db68c2014-08-07 20:58:41 +00009190 while (!PendingUpdateRecords.empty()) {
Richard Smithd1c46742014-04-30 02:24:17 +00009191 auto Update = PendingUpdateRecords.pop_back_val();
9192 ReadingKindTracker ReadingKind(Read_Decl, *this);
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00009193 loadDeclUpdateRecords(Update);
Richard Smithd1c46742014-04-30 02:24:17 +00009194 }
Guy Benyei11169dd2012-12-18 14:30:41 +00009195 }
Richard Smith8a639892015-01-24 01:07:20 +00009196
9197 // At this point, all update records for loaded decls are in place, so any
9198 // fake class definitions should have become real.
9199 assert(PendingFakeDefinitionData.empty() &&
9200 "faked up a class definition but never saw the real one");
9201
Guy Benyei11169dd2012-12-18 14:30:41 +00009202 // If we deserialized any C++ or Objective-C class definitions, any
9203 // Objective-C protocol definitions, or any redeclarable templates, make sure
David L. Jonesc4808b9e2016-12-15 20:53:26 +00009204 // that all redeclarations point to the definitions. Note that this can only
Guy Benyei11169dd2012-12-18 14:30:41 +00009205 // happen now, after the redeclaration chains have been fully wired.
Vedant Kumar48b4f762018-04-14 01:40:48 +00009206 for (Decl *D : PendingDefinitions) {
9207 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9208 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00009209 // Make sure that the TagType points at the definition.
9210 const_cast<TagType*>(TagT)->decl = TD;
9211 }
Richard Smith8ce51082015-03-11 01:44:51 +00009212
Vedant Kumar48b4f762018-04-14 01:40:48 +00009213 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00009214 for (auto *R = getMostRecentExistingDecl(RD); R;
9215 R = R->getPreviousDecl()) {
9216 assert((R == D) ==
9217 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
Richard Smith2c381642014-08-27 23:11:59 +00009218 "declaration thinks it's the definition but it isn't");
Aaron Ballman86c93902014-03-06 23:45:36 +00009219 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
Richard Smith2c381642014-08-27 23:11:59 +00009220 }
Guy Benyei11169dd2012-12-18 14:30:41 +00009221 }
9222
9223 continue;
9224 }
Richard Smith8ce51082015-03-11 01:44:51 +00009225
Vedant Kumar48b4f762018-04-14 01:40:48 +00009226 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00009227 // Make sure that the ObjCInterfaceType points at the definition.
9228 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9229 ->Decl = ID;
Richard Smith8ce51082015-03-11 01:44:51 +00009230
9231 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9232 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9233
Guy Benyei11169dd2012-12-18 14:30:41 +00009234 continue;
9235 }
Richard Smith8ce51082015-03-11 01:44:51 +00009236
Vedant Kumar48b4f762018-04-14 01:40:48 +00009237 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00009238 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9239 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9240
Guy Benyei11169dd2012-12-18 14:30:41 +00009241 continue;
9242 }
Richard Smith8ce51082015-03-11 01:44:51 +00009243
Vedant Kumar48b4f762018-04-14 01:40:48 +00009244 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
Richard Smith8ce51082015-03-11 01:44:51 +00009245 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9246 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
Guy Benyei11169dd2012-12-18 14:30:41 +00009247 }
9248 PendingDefinitions.clear();
9249
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009250 // Load the bodies of any functions or methods we've encountered. We do
9251 // this now (delayed) so that we can be sure that the declaration chains
9252 // have been fully wired up (hasBody relies on this).
9253 // FIXME: We shouldn't require complete redeclaration chains here.
Vedant Kumar48b4f762018-04-14 01:40:48 +00009254 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9255 PBEnd = PendingBodies.end();
9256 PB != PBEnd; ++PB) {
9257 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
Richard Smith0b70de12018-06-28 01:07:28 +00009258 // For a function defined inline within a class template, force the
9259 // canonical definition to be the one inside the canonical definition of
9260 // the template. This ensures that we instantiate from a correct view
9261 // of the template.
9262 //
9263 // Sadly we can't do this more generally: we can't be sure that all
9264 // copies of an arbitrary class definition will have the same members
9265 // defined (eg, some member functions may not be instantiated, and some
9266 // special members may or may not have been implicitly defined).
9267 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9268 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9269 continue;
9270
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009271 // FIXME: Check for =delete/=default?
9272 // FIXME: Complain about ODR violations here?
9273 const FunctionDecl *Defn = nullptr;
9274 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00009275 FD->setLazyBody(PB->second);
Richard Trieue6caa262017-12-23 00:41:01 +00009276 } else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00009277 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
Richard Trieue6caa262017-12-23 00:41:01 +00009278 mergeDefinitionVisibility(NonConstDefn, FD);
9279
9280 if (!FD->isLateTemplateParsed() &&
9281 !NonConstDefn->isLateTemplateParsed() &&
9282 FD->getODRHash() != NonConstDefn->getODRHash()) {
Richard Trieu27c1b1a2018-07-10 01:40:50 +00009283 if (!isa<CXXMethodDecl>(FD)) {
9284 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9285 } else if (FD->getLexicalParent()->isFileContext() &&
9286 NonConstDefn->getLexicalParent()->isFileContext()) {
9287 // Only diagnose out-of-line method definitions. If they are
9288 // in class definitions, then an error will be generated when
9289 // processing the class bodies.
9290 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9291 }
Richard Trieue6caa262017-12-23 00:41:01 +00009292 }
9293 }
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009294 continue;
9295 }
9296
Vedant Kumar48b4f762018-04-14 01:40:48 +00009297 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009298 if (!getContext().getLangOpts().Modules || !MD->hasBody())
Vedant Kumar48b4f762018-04-14 01:40:48 +00009299 MD->setLazyBody(PB->second);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009300 }
9301 PendingBodies.clear();
9302
Richard Smith42413142015-05-15 20:05:43 +00009303 // Do some cleanup.
9304 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9305 getContext().deduplicateMergedDefinitonsFor(ND);
9306 PendingMergedDefinitionsToDeduplicate.clear();
Richard Smitha0ce9c42014-07-29 23:23:27 +00009307}
9308
9309void ASTReader::diagnoseOdrViolations() {
Richard Trieue6caa262017-12-23 00:41:01 +00009310 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
Richard Trieuab4d7302018-07-25 22:52:05 +00009311 PendingFunctionOdrMergeFailures.empty() &&
9312 PendingEnumOdrMergeFailures.empty())
Richard Smithbb853c72014-08-13 01:23:33 +00009313 return;
9314
Richard Smitha0ce9c42014-07-29 23:23:27 +00009315 // Trigger the import of the full definition of each class that had any
9316 // odr-merging problems, so we can produce better diagnostics for them.
Richard Smithbb853c72014-08-13 01:23:33 +00009317 // These updates may in turn find and diagnose some ODR failures, so take
9318 // ownership of the set first.
9319 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9320 PendingOdrMergeFailures.clear();
9321 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00009322 Merge.first->buildLookup();
9323 Merge.first->decls_begin();
9324 Merge.first->bases_begin();
9325 Merge.first->vbases_begin();
Richard Trieue13eabe2017-09-30 02:19:17 +00009326 for (auto &RecordPair : Merge.second) {
9327 auto *RD = RecordPair.first;
Richard Smitha0ce9c42014-07-29 23:23:27 +00009328 RD->decls_begin();
9329 RD->bases_begin();
9330 RD->vbases_begin();
9331 }
9332 }
9333
Richard Trieue6caa262017-12-23 00:41:01 +00009334 // Trigger the import of functions.
9335 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9336 PendingFunctionOdrMergeFailures.clear();
9337 for (auto &Merge : FunctionOdrMergeFailures) {
9338 Merge.first->buildLookup();
9339 Merge.first->decls_begin();
9340 Merge.first->getBody();
9341 for (auto &FD : Merge.second) {
9342 FD->buildLookup();
9343 FD->decls_begin();
9344 FD->getBody();
9345 }
9346 }
9347
Richard Trieuab4d7302018-07-25 22:52:05 +00009348 // Trigger the import of enums.
9349 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9350 PendingEnumOdrMergeFailures.clear();
9351 for (auto &Merge : EnumOdrMergeFailures) {
9352 Merge.first->decls_begin();
9353 for (auto &Enum : Merge.second) {
9354 Enum->decls_begin();
9355 }
9356 }
9357
Richard Smitha0ce9c42014-07-29 23:23:27 +00009358 // For each declaration from a merged context, check that the canonical
9359 // definition of that context also contains a declaration of the same
9360 // entity.
9361 //
9362 // Caution: this loop does things that might invalidate iterators into
9363 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9364 while (!PendingOdrMergeChecks.empty()) {
9365 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9366
9367 // FIXME: Skip over implicit declarations for now. This matters for things
9368 // like implicitly-declared special member functions. This isn't entirely
9369 // correct; we can end up with multiple unmerged declarations of the same
9370 // implicit entity.
9371 if (D->isImplicit())
9372 continue;
9373
9374 DeclContext *CanonDef = D->getDeclContext();
Richard Smitha0ce9c42014-07-29 23:23:27 +00009375
9376 bool Found = false;
9377 const Decl *DCanon = D->getCanonicalDecl();
9378
Richard Smith01bdb7a2014-08-28 05:44:07 +00009379 for (auto RI : D->redecls()) {
9380 if (RI->getLexicalDeclContext() == CanonDef) {
9381 Found = true;
9382 break;
9383 }
9384 }
9385 if (Found)
9386 continue;
9387
Richard Smith0f4e2c42015-08-06 04:23:48 +00009388 // Quick check failed, time to do the slow thing. Note, we can't just
9389 // look up the name of D in CanonDef here, because the member that is
9390 // in CanonDef might not be found by name lookup (it might have been
9391 // replaced by a more recent declaration in the lookup table), and we
9392 // can't necessarily find it in the redeclaration chain because it might
9393 // be merely mergeable, not redeclarable.
Richard Smitha0ce9c42014-07-29 23:23:27 +00009394 llvm::SmallVector<const NamedDecl*, 4> Candidates;
Richard Smith0f4e2c42015-08-06 04:23:48 +00009395 for (auto *CanonMember : CanonDef->decls()) {
9396 if (CanonMember->getCanonicalDecl() == DCanon) {
9397 // This can happen if the declaration is merely mergeable and not
9398 // actually redeclarable (we looked for redeclarations earlier).
9399 //
9400 // FIXME: We should be able to detect this more efficiently, without
9401 // pulling in all of the members of CanonDef.
9402 Found = true;
9403 break;
Richard Smitha0ce9c42014-07-29 23:23:27 +00009404 }
Richard Smith0f4e2c42015-08-06 04:23:48 +00009405 if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9406 if (ND->getDeclName() == D->getDeclName())
9407 Candidates.push_back(ND);
Richard Smitha0ce9c42014-07-29 23:23:27 +00009408 }
9409
9410 if (!Found) {
Richard Smithd08aeb62014-08-28 01:33:39 +00009411 // The AST doesn't like TagDecls becoming invalid after they've been
9412 // completed. We only really need to mark FieldDecls as invalid here.
9413 if (!isa<TagDecl>(D))
9414 D->setInvalidDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00009415
Richard Smith4ab3dbd2015-02-13 22:43:51 +00009416 // Ensure we don't accidentally recursively enter deserialization while
9417 // we're producing our diagnostic.
9418 Deserializing RecursionGuard(this);
Richard Smitha0ce9c42014-07-29 23:23:27 +00009419
9420 std::string CanonDefModule =
9421 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9422 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9423 << D << getOwningModuleNameForDiagnostic(D)
9424 << CanonDef << CanonDefModule.empty() << CanonDefModule;
9425
9426 if (Candidates.empty())
9427 Diag(cast<Decl>(CanonDef)->getLocation(),
9428 diag::note_module_odr_violation_no_possible_decls) << D;
9429 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00009430 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9431 Diag(Candidates[I]->getLocation(),
Richard Smitha0ce9c42014-07-29 23:23:27 +00009432 diag::note_module_odr_violation_possible_decl)
Vedant Kumar48b4f762018-04-14 01:40:48 +00009433 << Candidates[I];
Richard Smitha0ce9c42014-07-29 23:23:27 +00009434 }
9435
9436 DiagnosedOdrMergeFailures.insert(CanonDef);
9437 }
9438 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00009439
Richard Trieuab4d7302018-07-25 22:52:05 +00009440 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9441 EnumOdrMergeFailures.empty())
Richard Smith4ab3dbd2015-02-13 22:43:51 +00009442 return;
9443
9444 // Ensure we don't accidentally recursively enter deserialization while
9445 // we're producing our diagnostics.
9446 Deserializing RecursionGuard(this);
9447
Richard Trieue6caa262017-12-23 00:41:01 +00009448 // Common code for hashing helpers.
9449 ODRHash Hash;
9450 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9451 Hash.clear();
9452 Hash.AddQualType(Ty);
9453 return Hash.CalculateHash();
9454 };
9455
9456 auto ComputeODRHash = [&Hash](const Stmt *S) {
9457 assert(S);
9458 Hash.clear();
9459 Hash.AddStmt(S);
9460 return Hash.CalculateHash();
9461 };
9462
9463 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9464 assert(D);
9465 Hash.clear();
9466 Hash.AddSubDecl(D);
9467 return Hash.CalculateHash();
9468 };
9469
Richard Trieu7282d322018-04-25 00:31:15 +00009470 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9471 Hash.clear();
9472 Hash.AddTemplateArgument(TA);
9473 return Hash.CalculateHash();
9474 };
9475
Richard Trieu9359e8f2018-05-30 01:12:26 +00009476 auto ComputeTemplateParameterListODRHash =
9477 [&Hash](const TemplateParameterList *TPL) {
9478 assert(TPL);
9479 Hash.clear();
9480 Hash.AddTemplateParameterList(TPL);
9481 return Hash.CalculateHash();
9482 };
9483
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -08009484 // Used with err_module_odr_violation_mismatch_decl and
9485 // note_module_odr_violation_mismatch_decl
9486 // This list should be the same Decl's as in ODRHash::isWhiteListedDecl
9487 enum ODRMismatchDecl {
9488 EndOfClass,
9489 PublicSpecifer,
9490 PrivateSpecifer,
9491 ProtectedSpecifer,
9492 StaticAssert,
9493 Field,
9494 CXXMethod,
9495 TypeAlias,
9496 TypeDef,
9497 Var,
9498 Friend,
9499 FunctionTemplate,
9500 Other
9501 };
9502
9503 // Used with err_module_odr_violation_mismatch_decl_diff and
9504 // note_module_odr_violation_mismatch_decl_diff
9505 enum ODRMismatchDeclDifference {
9506 StaticAssertCondition,
9507 StaticAssertMessage,
9508 StaticAssertOnlyMessage,
9509 FieldName,
9510 FieldTypeName,
9511 FieldSingleBitField,
9512 FieldDifferentWidthBitField,
9513 FieldSingleMutable,
9514 FieldSingleInitializer,
9515 FieldDifferentInitializers,
9516 MethodName,
9517 MethodDeleted,
9518 MethodDefaulted,
9519 MethodVirtual,
9520 MethodStatic,
9521 MethodVolatile,
9522 MethodConst,
9523 MethodInline,
9524 MethodNumberParameters,
9525 MethodParameterType,
9526 MethodParameterName,
9527 MethodParameterSingleDefaultArgument,
9528 MethodParameterDifferentDefaultArgument,
9529 MethodNoTemplateArguments,
9530 MethodDifferentNumberTemplateArguments,
9531 MethodDifferentTemplateArgument,
9532 MethodSingleBody,
9533 MethodDifferentBody,
9534 TypedefName,
9535 TypedefType,
9536 VarName,
9537 VarType,
9538 VarSingleInitializer,
9539 VarDifferentInitializer,
9540 VarConstexpr,
9541 FriendTypeFunction,
9542 FriendType,
9543 FriendFunction,
9544 FunctionTemplateDifferentNumberParameters,
9545 FunctionTemplateParameterDifferentKind,
9546 FunctionTemplateParameterName,
9547 FunctionTemplateParameterSingleDefaultArgument,
9548 FunctionTemplateParameterDifferentDefaultArgument,
9549 FunctionTemplateParameterDifferentType,
9550 FunctionTemplatePackParameter,
9551 };
9552
9553 // These lambdas have the common portions of the ODR diagnostics. This
9554 // has the same return as Diag(), so addition parameters can be passed
9555 // in with operator<<
9556 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9557 SourceLocation Loc, SourceRange Range,
9558 ODRMismatchDeclDifference DiffType) {
9559 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9560 << FirstRecord << FirstModule.empty() << FirstModule << Range
9561 << DiffType;
9562 };
9563 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9564 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9565 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9566 << SecondModule << Range << DiffType;
9567 };
9568
9569 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9570 &ComputeQualTypeODRHash, &ComputeODRHash](
9571 NamedDecl *FirstRecord, StringRef FirstModule,
9572 StringRef SecondModule, FieldDecl *FirstField,
9573 FieldDecl *SecondField) {
9574 IdentifierInfo *FirstII = FirstField->getIdentifier();
9575 IdentifierInfo *SecondII = SecondField->getIdentifier();
9576 if (FirstII->getName() != SecondII->getName()) {
9577 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9578 FirstField->getSourceRange(), FieldName)
9579 << FirstII;
9580 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9581 SecondField->getSourceRange(), FieldName)
9582 << SecondII;
9583
9584 return true;
9585 }
9586
9587 assert(getContext().hasSameType(FirstField->getType(),
9588 SecondField->getType()));
9589
9590 QualType FirstType = FirstField->getType();
9591 QualType SecondType = SecondField->getType();
9592 if (ComputeQualTypeODRHash(FirstType) !=
9593 ComputeQualTypeODRHash(SecondType)) {
9594 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9595 FirstField->getSourceRange(), FieldTypeName)
9596 << FirstII << FirstType;
9597 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9598 SecondField->getSourceRange(), FieldTypeName)
9599 << SecondII << SecondType;
9600
9601 return true;
9602 }
9603
9604 const bool IsFirstBitField = FirstField->isBitField();
9605 const bool IsSecondBitField = SecondField->isBitField();
9606 if (IsFirstBitField != IsSecondBitField) {
9607 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9608 FirstField->getSourceRange(), FieldSingleBitField)
9609 << FirstII << IsFirstBitField;
9610 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9611 SecondField->getSourceRange(), FieldSingleBitField)
9612 << SecondII << IsSecondBitField;
9613 return true;
9614 }
9615
9616 if (IsFirstBitField && IsSecondBitField) {
9617 unsigned FirstBitWidthHash =
9618 ComputeODRHash(FirstField->getBitWidth());
9619 unsigned SecondBitWidthHash =
9620 ComputeODRHash(SecondField->getBitWidth());
9621 if (FirstBitWidthHash != SecondBitWidthHash) {
9622 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9623 FirstField->getSourceRange(),
9624 FieldDifferentWidthBitField)
9625 << FirstII << FirstField->getBitWidth()->getSourceRange();
9626 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9627 SecondField->getSourceRange(),
9628 FieldDifferentWidthBitField)
9629 << SecondII << SecondField->getBitWidth()->getSourceRange();
9630 return true;
9631 }
9632 }
9633
9634 if (!PP.getLangOpts().CPlusPlus)
9635 return false;
9636
9637 const bool IsFirstMutable = FirstField->isMutable();
9638 const bool IsSecondMutable = SecondField->isMutable();
9639 if (IsFirstMutable != IsSecondMutable) {
9640 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9641 FirstField->getSourceRange(), FieldSingleMutable)
9642 << FirstII << IsFirstMutable;
9643 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9644 SecondField->getSourceRange(), FieldSingleMutable)
9645 << SecondII << IsSecondMutable;
9646 return true;
9647 }
9648
9649 const Expr *FirstInitializer = FirstField->getInClassInitializer();
9650 const Expr *SecondInitializer = SecondField->getInClassInitializer();
9651 if ((!FirstInitializer && SecondInitializer) ||
9652 (FirstInitializer && !SecondInitializer)) {
9653 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9654 FirstField->getSourceRange(), FieldSingleInitializer)
9655 << FirstII << (FirstInitializer != nullptr);
9656 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9657 SecondField->getSourceRange(), FieldSingleInitializer)
9658 << SecondII << (SecondInitializer != nullptr);
9659 return true;
9660 }
9661
9662 if (FirstInitializer && SecondInitializer) {
9663 unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9664 unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9665 if (FirstInitHash != SecondInitHash) {
9666 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9667 FirstField->getSourceRange(),
9668 FieldDifferentInitializers)
9669 << FirstII << FirstInitializer->getSourceRange();
9670 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9671 SecondField->getSourceRange(),
9672 FieldDifferentInitializers)
9673 << SecondII << SecondInitializer->getSourceRange();
9674 return true;
9675 }
9676 }
9677
9678 return false;
9679 };
9680
9681 auto ODRDiagTypeDefOrAlias =
9682 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9683 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9684 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9685 bool IsTypeAlias) {
9686 auto FirstName = FirstTD->getDeclName();
9687 auto SecondName = SecondTD->getDeclName();
9688 if (FirstName != SecondName) {
9689 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9690 FirstTD->getSourceRange(), TypedefName)
9691 << IsTypeAlias << FirstName;
9692 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9693 SecondTD->getSourceRange(), TypedefName)
9694 << IsTypeAlias << SecondName;
9695 return true;
9696 }
9697
9698 QualType FirstType = FirstTD->getUnderlyingType();
9699 QualType SecondType = SecondTD->getUnderlyingType();
9700 if (ComputeQualTypeODRHash(FirstType) !=
9701 ComputeQualTypeODRHash(SecondType)) {
9702 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9703 FirstTD->getSourceRange(), TypedefType)
9704 << IsTypeAlias << FirstName << FirstType;
9705 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9706 SecondTD->getSourceRange(), TypedefType)
9707 << IsTypeAlias << SecondName << SecondType;
9708 return true;
9709 }
9710
9711 return false;
9712 };
9713
9714 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9715 &ComputeQualTypeODRHash, &ComputeODRHash,
9716 this](NamedDecl *FirstRecord, StringRef FirstModule,
9717 StringRef SecondModule, VarDecl *FirstVD,
9718 VarDecl *SecondVD) {
9719 auto FirstName = FirstVD->getDeclName();
9720 auto SecondName = SecondVD->getDeclName();
9721 if (FirstName != SecondName) {
9722 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9723 FirstVD->getSourceRange(), VarName)
9724 << FirstName;
9725 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9726 SecondVD->getSourceRange(), VarName)
9727 << SecondName;
9728 return true;
9729 }
9730
9731 QualType FirstType = FirstVD->getType();
9732 QualType SecondType = SecondVD->getType();
9733 if (ComputeQualTypeODRHash(FirstType) !=
9734 ComputeQualTypeODRHash(SecondType)) {
9735 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9736 FirstVD->getSourceRange(), VarType)
9737 << FirstName << FirstType;
9738 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9739 SecondVD->getSourceRange(), VarType)
9740 << SecondName << SecondType;
9741 return true;
9742 }
9743
9744 if (!PP.getLangOpts().CPlusPlus)
9745 return false;
9746
9747 const Expr *FirstInit = FirstVD->getInit();
9748 const Expr *SecondInit = SecondVD->getInit();
9749 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9750 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9751 FirstVD->getSourceRange(), VarSingleInitializer)
9752 << FirstName << (FirstInit == nullptr)
9753 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9754 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9755 SecondVD->getSourceRange(), VarSingleInitializer)
9756 << SecondName << (SecondInit == nullptr)
9757 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9758 return true;
9759 }
9760
9761 if (FirstInit && SecondInit &&
9762 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9763 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9764 FirstVD->getSourceRange(), VarDifferentInitializer)
9765 << FirstName << FirstInit->getSourceRange();
9766 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9767 SecondVD->getSourceRange(), VarDifferentInitializer)
9768 << SecondName << SecondInit->getSourceRange();
9769 return true;
9770 }
9771
9772 const bool FirstIsConstexpr = FirstVD->isConstexpr();
9773 const bool SecondIsConstexpr = SecondVD->isConstexpr();
9774 if (FirstIsConstexpr != SecondIsConstexpr) {
9775 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9776 FirstVD->getSourceRange(), VarConstexpr)
9777 << FirstName << FirstIsConstexpr;
9778 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9779 SecondVD->getSourceRange(), VarConstexpr)
9780 << SecondName << SecondIsConstexpr;
9781 return true;
9782 }
9783 return false;
9784 };
9785
9786 auto DifferenceSelector = [](Decl *D) {
9787 assert(D && "valid Decl required");
9788 switch (D->getKind()) {
9789 default:
9790 return Other;
9791 case Decl::AccessSpec:
9792 switch (D->getAccess()) {
9793 case AS_public:
9794 return PublicSpecifer;
9795 case AS_private:
9796 return PrivateSpecifer;
9797 case AS_protected:
9798 return ProtectedSpecifer;
9799 case AS_none:
9800 break;
9801 }
9802 llvm_unreachable("Invalid access specifier");
9803 case Decl::StaticAssert:
9804 return StaticAssert;
9805 case Decl::Field:
9806 return Field;
9807 case Decl::CXXMethod:
9808 case Decl::CXXConstructor:
9809 case Decl::CXXDestructor:
9810 return CXXMethod;
9811 case Decl::TypeAlias:
9812 return TypeAlias;
9813 case Decl::Typedef:
9814 return TypeDef;
9815 case Decl::Var:
9816 return Var;
9817 case Decl::Friend:
9818 return Friend;
9819 case Decl::FunctionTemplate:
9820 return FunctionTemplate;
9821 }
9822 };
9823
9824 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9825 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9826 RecordDecl *Record,
9827 const DeclContext *DC) {
9828 for (auto *D : Record->decls()) {
9829 if (!ODRHash::isWhitelistedDecl(D, DC))
9830 continue;
9831 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9832 }
9833 };
9834
9835 struct DiffResult {
9836 Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9837 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9838 };
9839
9840 // If there is a diagnoseable difference, FirstDiffType and
9841 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9842 // filled in if not EndOfClass.
9843 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9844 DeclHashes &SecondHashes) {
9845 DiffResult DR;
9846 auto FirstIt = FirstHashes.begin();
9847 auto SecondIt = SecondHashes.begin();
9848 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9849 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9850 FirstIt->second == SecondIt->second) {
9851 ++FirstIt;
9852 ++SecondIt;
9853 continue;
9854 }
9855
9856 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9857 DR.SecondDecl =
9858 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9859
9860 DR.FirstDiffType =
9861 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9862 DR.SecondDiffType =
9863 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9864 return DR;
9865 }
9866 return DR;
9867 };
9868
9869 // Use this to diagnose that an unexpected Decl was encountered
9870 // or no difference was detected. This causes a generic error
9871 // message to be emitted.
9872 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9873 StringRef FirstModule,
9874 NamedDecl *SecondRecord,
9875 StringRef SecondModule) {
9876 Diag(FirstRecord->getLocation(),
9877 diag::err_module_odr_violation_different_definitions)
9878 << FirstRecord << FirstModule.empty() << FirstModule;
9879
9880 if (DR.FirstDecl) {
9881 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9882 << FirstRecord << DR.FirstDecl->getSourceRange();
9883 }
9884
9885 Diag(SecondRecord->getLocation(),
9886 diag::note_module_odr_violation_different_definitions)
9887 << SecondModule;
9888
9889 if (DR.SecondDecl) {
9890 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9891 << DR.SecondDecl->getSourceRange();
9892 }
9893 };
9894
9895 auto DiagnoseODRMismatch =
9896 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9897 NamedDecl *SecondRecord, StringRef SecondModule) {
9898 SourceLocation FirstLoc;
9899 SourceRange FirstRange;
9900 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9901 if (DR.FirstDiffType == EndOfClass && FirstTag) {
9902 FirstLoc = FirstTag->getBraceRange().getEnd();
9903 } else {
9904 FirstLoc = DR.FirstDecl->getLocation();
9905 FirstRange = DR.FirstDecl->getSourceRange();
9906 }
9907 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
9908 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
9909 << DR.FirstDiffType;
9910
9911 SourceLocation SecondLoc;
9912 SourceRange SecondRange;
9913 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
9914 if (DR.SecondDiffType == EndOfClass && SecondTag) {
9915 SecondLoc = SecondTag->getBraceRange().getEnd();
9916 } else {
9917 SecondLoc = DR.SecondDecl->getLocation();
9918 SecondRange = DR.SecondDecl->getSourceRange();
9919 }
9920 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
9921 << SecondModule << SecondRange << DR.SecondDiffType;
9922 };
9923
Richard Smithcd45dbc2014-04-19 03:48:30 +00009924 // Issue any pending ODR-failure diagnostics.
Richard Smithbb853c72014-08-13 01:23:33 +00009925 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00009926 // If we've already pointed out a specific problem with this class, don't
9927 // bother issuing a general "something's different" diagnostic.
David Blaikie82e95a32014-11-19 07:49:47 +00009928 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
Richard Smithcd45dbc2014-04-19 03:48:30 +00009929 continue;
9930
9931 bool Diagnosed = false;
Richard Trieue7f7ed22017-02-22 01:11:25 +00009932 CXXRecordDecl *FirstRecord = Merge.first;
9933 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
Richard Trieue13eabe2017-09-30 02:19:17 +00009934 for (auto &RecordPair : Merge.second) {
9935 CXXRecordDecl *SecondRecord = RecordPair.first;
Richard Smithcd45dbc2014-04-19 03:48:30 +00009936 // Multiple different declarations got merged together; tell the user
9937 // where they came from.
Richard Trieue7f7ed22017-02-22 01:11:25 +00009938 if (FirstRecord == SecondRecord)
9939 continue;
9940
9941 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
Richard Trieue13eabe2017-09-30 02:19:17 +00009942
9943 auto *FirstDD = FirstRecord->DefinitionData;
9944 auto *SecondDD = RecordPair.second;
9945
9946 assert(FirstDD && SecondDD && "Definitions without DefinitionData");
9947
9948 // Diagnostics from DefinitionData are emitted here.
9949 if (FirstDD != SecondDD) {
9950 enum ODRDefinitionDataDifference {
9951 NumBases,
9952 NumVBases,
9953 BaseType,
9954 BaseVirtual,
9955 BaseAccess,
9956 };
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -08009957 auto ODRDiagBaseError = [FirstRecord, &FirstModule,
9958 this](SourceLocation Loc, SourceRange Range,
9959 ODRDefinitionDataDifference DiffType) {
Richard Trieue13eabe2017-09-30 02:19:17 +00009960 return Diag(Loc, diag::err_module_odr_violation_definition_data)
9961 << FirstRecord << FirstModule.empty() << FirstModule << Range
9962 << DiffType;
9963 };
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -08009964 auto ODRDiagBaseNote = [&SecondModule,
9965 this](SourceLocation Loc, SourceRange Range,
9966 ODRDefinitionDataDifference DiffType) {
Richard Trieue13eabe2017-09-30 02:19:17 +00009967 return Diag(Loc, diag::note_module_odr_violation_definition_data)
9968 << SecondModule << Range << DiffType;
9969 };
9970
Richard Trieue13eabe2017-09-30 02:19:17 +00009971 unsigned FirstNumBases = FirstDD->NumBases;
9972 unsigned FirstNumVBases = FirstDD->NumVBases;
9973 unsigned SecondNumBases = SecondDD->NumBases;
9974 unsigned SecondNumVBases = SecondDD->NumVBases;
9975
9976 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
9977 unsigned NumBases = DD->NumBases;
9978 if (NumBases == 0) return SourceRange();
9979 auto bases = DD->bases();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00009980 return SourceRange(bases[0].getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00009981 bases[NumBases - 1].getEndLoc());
Richard Trieue13eabe2017-09-30 02:19:17 +00009982 };
9983
9984 if (FirstNumBases != SecondNumBases) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -08009985 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9986 NumBases)
Richard Trieue13eabe2017-09-30 02:19:17 +00009987 << FirstNumBases;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -08009988 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
9989 NumBases)
Richard Trieue13eabe2017-09-30 02:19:17 +00009990 << SecondNumBases;
9991 Diagnosed = true;
9992 break;
9993 }
9994
9995 if (FirstNumVBases != SecondNumVBases) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -08009996 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
9997 NumVBases)
Richard Trieue13eabe2017-09-30 02:19:17 +00009998 << FirstNumVBases;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -08009999 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10000 NumVBases)
Richard Trieue13eabe2017-09-30 02:19:17 +000010001 << SecondNumVBases;
10002 Diagnosed = true;
10003 break;
10004 }
10005
10006 auto FirstBases = FirstDD->bases();
10007 auto SecondBases = SecondDD->bases();
10008 unsigned i = 0;
10009 for (i = 0; i < FirstNumBases; ++i) {
10010 auto FirstBase = FirstBases[i];
10011 auto SecondBase = SecondBases[i];
10012 if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10013 ComputeQualTypeODRHash(SecondBase.getType())) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010014 ODRDiagBaseError(FirstRecord->getLocation(),
10015 FirstBase.getSourceRange(), BaseType)
Richard Trieue13eabe2017-09-30 02:19:17 +000010016 << (i + 1) << FirstBase.getType();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010017 ODRDiagBaseNote(SecondRecord->getLocation(),
10018 SecondBase.getSourceRange(), BaseType)
Richard Trieue13eabe2017-09-30 02:19:17 +000010019 << (i + 1) << SecondBase.getType();
10020 break;
10021 }
10022
10023 if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010024 ODRDiagBaseError(FirstRecord->getLocation(),
10025 FirstBase.getSourceRange(), BaseVirtual)
Richard Trieue13eabe2017-09-30 02:19:17 +000010026 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010027 ODRDiagBaseNote(SecondRecord->getLocation(),
10028 SecondBase.getSourceRange(), BaseVirtual)
Richard Trieue13eabe2017-09-30 02:19:17 +000010029 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10030 break;
10031 }
10032
10033 if (FirstBase.getAccessSpecifierAsWritten() !=
10034 SecondBase.getAccessSpecifierAsWritten()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010035 ODRDiagBaseError(FirstRecord->getLocation(),
10036 FirstBase.getSourceRange(), BaseAccess)
Richard Trieue13eabe2017-09-30 02:19:17 +000010037 << (i + 1) << FirstBase.getType()
10038 << (int)FirstBase.getAccessSpecifierAsWritten();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010039 ODRDiagBaseNote(SecondRecord->getLocation(),
10040 SecondBase.getSourceRange(), BaseAccess)
Richard Trieue13eabe2017-09-30 02:19:17 +000010041 << (i + 1) << SecondBase.getType()
10042 << (int)SecondBase.getAccessSpecifierAsWritten();
10043 break;
10044 }
10045 }
10046
10047 if (i != FirstNumBases) {
10048 Diagnosed = true;
10049 break;
10050 }
10051 }
10052
Richard Trieu498117b2017-08-23 02:43:59 +000010053 const ClassTemplateDecl *FirstTemplate =
10054 FirstRecord->getDescribedClassTemplate();
10055 const ClassTemplateDecl *SecondTemplate =
10056 SecondRecord->getDescribedClassTemplate();
10057
10058 assert(!FirstTemplate == !SecondTemplate &&
10059 "Both pointers should be null or non-null");
10060
10061 enum ODRTemplateDifference {
10062 ParamEmptyName,
10063 ParamName,
10064 ParamSingleDefaultArgument,
10065 ParamDifferentDefaultArgument,
10066 };
10067
10068 if (FirstTemplate && SecondTemplate) {
10069 DeclHashes FirstTemplateHashes;
10070 DeclHashes SecondTemplateHashes;
Richard Trieu498117b2017-08-23 02:43:59 +000010071
10072 auto PopulateTemplateParameterHashs =
Richard Trieue6caa262017-12-23 00:41:01 +000010073 [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10074 const ClassTemplateDecl *TD) {
Richard Trieu498117b2017-08-23 02:43:59 +000010075 for (auto *D : TD->getTemplateParameters()->asArray()) {
Richard Trieue6caa262017-12-23 00:41:01 +000010076 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
Richard Trieu498117b2017-08-23 02:43:59 +000010077 }
10078 };
10079
10080 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10081 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10082
10083 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10084 "Number of template parameters should be equal.");
10085
10086 auto FirstIt = FirstTemplateHashes.begin();
10087 auto FirstEnd = FirstTemplateHashes.end();
10088 auto SecondIt = SecondTemplateHashes.begin();
10089 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10090 if (FirstIt->second == SecondIt->second)
10091 continue;
10092
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010093 auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10094 SourceLocation Loc, SourceRange Range,
10095 ODRTemplateDifference DiffType) {
Richard Trieu498117b2017-08-23 02:43:59 +000010096 return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10097 << FirstRecord << FirstModule.empty() << FirstModule << Range
10098 << DiffType;
10099 };
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010100 auto ODRDiagTemplateNote = [&SecondModule, this](
10101 SourceLocation Loc, SourceRange Range,
10102 ODRTemplateDifference DiffType) {
Richard Trieu498117b2017-08-23 02:43:59 +000010103 return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10104 << SecondModule << Range << DiffType;
10105 };
10106
Vedant Kumar48b4f762018-04-14 01:40:48 +000010107 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10108 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
Richard Trieu498117b2017-08-23 02:43:59 +000010109
10110 assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10111 "Parameter Decl's should be the same kind.");
10112
10113 DeclarationName FirstName = FirstDecl->getDeclName();
10114 DeclarationName SecondName = SecondDecl->getDeclName();
10115
10116 if (FirstName != SecondName) {
10117 const bool FirstNameEmpty =
10118 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10119 const bool SecondNameEmpty =
10120 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10121 assert((!FirstNameEmpty || !SecondNameEmpty) &&
10122 "Both template parameters cannot be unnamed.");
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010123 ODRDiagTemplateError(FirstDecl->getLocation(),
10124 FirstDecl->getSourceRange(),
10125 FirstNameEmpty ? ParamEmptyName : ParamName)
Richard Trieu498117b2017-08-23 02:43:59 +000010126 << FirstName;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010127 ODRDiagTemplateNote(SecondDecl->getLocation(),
10128 SecondDecl->getSourceRange(),
10129 SecondNameEmpty ? ParamEmptyName : ParamName)
Richard Trieu498117b2017-08-23 02:43:59 +000010130 << SecondName;
10131 break;
10132 }
10133
10134 switch (FirstDecl->getKind()) {
10135 default:
10136 llvm_unreachable("Invalid template parameter type.");
10137 case Decl::TemplateTypeParm: {
10138 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10139 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10140 const bool HasFirstDefaultArgument =
10141 FirstParam->hasDefaultArgument() &&
10142 !FirstParam->defaultArgumentWasInherited();
10143 const bool HasSecondDefaultArgument =
10144 SecondParam->hasDefaultArgument() &&
10145 !SecondParam->defaultArgumentWasInherited();
10146
10147 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010148 ODRDiagTemplateError(FirstDecl->getLocation(),
10149 FirstDecl->getSourceRange(),
10150 ParamSingleDefaultArgument)
Richard Trieu498117b2017-08-23 02:43:59 +000010151 << HasFirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010152 ODRDiagTemplateNote(SecondDecl->getLocation(),
10153 SecondDecl->getSourceRange(),
10154 ParamSingleDefaultArgument)
Richard Trieu498117b2017-08-23 02:43:59 +000010155 << HasSecondDefaultArgument;
10156 break;
10157 }
10158
10159 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10160 "Expecting default arguments.");
10161
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010162 ODRDiagTemplateError(FirstDecl->getLocation(),
10163 FirstDecl->getSourceRange(),
10164 ParamDifferentDefaultArgument);
10165 ODRDiagTemplateNote(SecondDecl->getLocation(),
10166 SecondDecl->getSourceRange(),
10167 ParamDifferentDefaultArgument);
Richard Trieu498117b2017-08-23 02:43:59 +000010168
10169 break;
10170 }
10171 case Decl::NonTypeTemplateParm: {
10172 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10173 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10174 const bool HasFirstDefaultArgument =
10175 FirstParam->hasDefaultArgument() &&
10176 !FirstParam->defaultArgumentWasInherited();
10177 const bool HasSecondDefaultArgument =
10178 SecondParam->hasDefaultArgument() &&
10179 !SecondParam->defaultArgumentWasInherited();
10180
10181 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010182 ODRDiagTemplateError(FirstDecl->getLocation(),
10183 FirstDecl->getSourceRange(),
10184 ParamSingleDefaultArgument)
Richard Trieu498117b2017-08-23 02:43:59 +000010185 << HasFirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010186 ODRDiagTemplateNote(SecondDecl->getLocation(),
10187 SecondDecl->getSourceRange(),
10188 ParamSingleDefaultArgument)
Richard Trieu498117b2017-08-23 02:43:59 +000010189 << HasSecondDefaultArgument;
10190 break;
10191 }
10192
10193 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10194 "Expecting default arguments.");
10195
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010196 ODRDiagTemplateError(FirstDecl->getLocation(),
10197 FirstDecl->getSourceRange(),
10198 ParamDifferentDefaultArgument);
10199 ODRDiagTemplateNote(SecondDecl->getLocation(),
10200 SecondDecl->getSourceRange(),
10201 ParamDifferentDefaultArgument);
Richard Trieu498117b2017-08-23 02:43:59 +000010202
10203 break;
10204 }
10205 case Decl::TemplateTemplateParm: {
10206 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10207 const auto *SecondParam =
10208 cast<TemplateTemplateParmDecl>(SecondDecl);
10209 const bool HasFirstDefaultArgument =
10210 FirstParam->hasDefaultArgument() &&
10211 !FirstParam->defaultArgumentWasInherited();
10212 const bool HasSecondDefaultArgument =
10213 SecondParam->hasDefaultArgument() &&
10214 !SecondParam->defaultArgumentWasInherited();
10215
10216 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010217 ODRDiagTemplateError(FirstDecl->getLocation(),
10218 FirstDecl->getSourceRange(),
10219 ParamSingleDefaultArgument)
Richard Trieu498117b2017-08-23 02:43:59 +000010220 << HasFirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010221 ODRDiagTemplateNote(SecondDecl->getLocation(),
10222 SecondDecl->getSourceRange(),
10223 ParamSingleDefaultArgument)
Richard Trieu498117b2017-08-23 02:43:59 +000010224 << HasSecondDefaultArgument;
10225 break;
10226 }
10227
10228 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10229 "Expecting default arguments.");
10230
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010231 ODRDiagTemplateError(FirstDecl->getLocation(),
10232 FirstDecl->getSourceRange(),
10233 ParamDifferentDefaultArgument);
10234 ODRDiagTemplateNote(SecondDecl->getLocation(),
10235 SecondDecl->getSourceRange(),
10236 ParamDifferentDefaultArgument);
Richard Trieu498117b2017-08-23 02:43:59 +000010237
10238 break;
10239 }
10240 }
10241
10242 break;
10243 }
10244
10245 if (FirstIt != FirstEnd) {
10246 Diagnosed = true;
10247 break;
10248 }
10249 }
10250
Richard Trieue7f7ed22017-02-22 01:11:25 +000010251 DeclHashes FirstHashes;
10252 DeclHashes SecondHashes;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010253 const DeclContext *DC = FirstRecord;
10254 PopulateHashes(FirstHashes, FirstRecord, DC);
10255 PopulateHashes(SecondHashes, SecondRecord, DC);
Richard Trieue7f7ed22017-02-22 01:11:25 +000010256
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010257 auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10258 ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10259 ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10260 Decl *FirstDecl = DR.FirstDecl;
10261 Decl *SecondDecl = DR.SecondDecl;
Richard Trieue7f7ed22017-02-22 01:11:25 +000010262
10263 if (FirstDiffType == Other || SecondDiffType == Other) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010264 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10265 SecondModule);
Richard Trieue7f7ed22017-02-22 01:11:25 +000010266 Diagnosed = true;
10267 break;
10268 }
10269
10270 if (FirstDiffType != SecondDiffType) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010271 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10272 SecondModule);
Richard Trieue7f7ed22017-02-22 01:11:25 +000010273 Diagnosed = true;
10274 break;
10275 }
10276
Richard Trieu639d7b62017-02-22 22:22:42 +000010277 assert(FirstDiffType == SecondDiffType);
10278
Richard Trieu639d7b62017-02-22 22:22:42 +000010279 switch (FirstDiffType) {
10280 case Other:
10281 case EndOfClass:
10282 case PublicSpecifer:
10283 case PrivateSpecifer:
10284 case ProtectedSpecifer:
10285 llvm_unreachable("Invalid diff type");
10286
10287 case StaticAssert: {
Vedant Kumar48b4f762018-04-14 01:40:48 +000010288 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10289 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
Richard Trieu639d7b62017-02-22 22:22:42 +000010290
10291 Expr *FirstExpr = FirstSA->getAssertExpr();
10292 Expr *SecondExpr = SecondSA->getAssertExpr();
10293 unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10294 unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10295 if (FirstODRHash != SecondODRHash) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010296 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10297 FirstExpr->getSourceRange(), StaticAssertCondition);
10298 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10299 SecondExpr->getSourceRange(), StaticAssertCondition);
Richard Trieu639d7b62017-02-22 22:22:42 +000010300 Diagnosed = true;
10301 break;
10302 }
10303
10304 StringLiteral *FirstStr = FirstSA->getMessage();
10305 StringLiteral *SecondStr = SecondSA->getMessage();
10306 assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10307 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10308 SourceLocation FirstLoc, SecondLoc;
10309 SourceRange FirstRange, SecondRange;
10310 if (FirstStr) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010311 FirstLoc = FirstStr->getBeginLoc();
Richard Trieu639d7b62017-02-22 22:22:42 +000010312 FirstRange = FirstStr->getSourceRange();
10313 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010314 FirstLoc = FirstSA->getBeginLoc();
Richard Trieu639d7b62017-02-22 22:22:42 +000010315 FirstRange = FirstSA->getSourceRange();
10316 }
10317 if (SecondStr) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010318 SecondLoc = SecondStr->getBeginLoc();
Richard Trieu639d7b62017-02-22 22:22:42 +000010319 SecondRange = SecondStr->getSourceRange();
10320 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010321 SecondLoc = SecondSA->getBeginLoc();
Richard Trieu639d7b62017-02-22 22:22:42 +000010322 SecondRange = SecondSA->getSourceRange();
10323 }
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010324 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10325 StaticAssertOnlyMessage)
Richard Trieu639d7b62017-02-22 22:22:42 +000010326 << (FirstStr == nullptr);
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010327 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10328 StaticAssertOnlyMessage)
Richard Trieu639d7b62017-02-22 22:22:42 +000010329 << (SecondStr == nullptr);
10330 Diagnosed = true;
10331 break;
10332 }
10333
10334 if (FirstStr && SecondStr &&
10335 FirstStr->getString() != SecondStr->getString()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010336 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10337 FirstStr->getSourceRange(), StaticAssertMessage);
10338 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10339 SecondStr->getSourceRange(), StaticAssertMessage);
Richard Trieu639d7b62017-02-22 22:22:42 +000010340 Diagnosed = true;
10341 break;
10342 }
10343 break;
10344 }
Richard Trieud0786092017-02-23 00:23:01 +000010345 case Field: {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010346 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10347 cast<FieldDecl>(FirstDecl),
10348 cast<FieldDecl>(SecondDecl));
Richard Trieud0786092017-02-23 00:23:01 +000010349 break;
10350 }
Richard Trieu48143742017-02-28 21:24:38 +000010351 case CXXMethod: {
Richard Trieu1c71d512017-07-15 02:55:13 +000010352 enum {
10353 DiagMethod,
10354 DiagConstructor,
10355 DiagDestructor,
10356 } FirstMethodType,
10357 SecondMethodType;
10358 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10359 if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10360 if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10361 return DiagMethod;
10362 };
Vedant Kumar48b4f762018-04-14 01:40:48 +000010363 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10364 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
Richard Trieu1c71d512017-07-15 02:55:13 +000010365 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10366 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
Richard Trieu583e2c12017-03-04 00:08:58 +000010367 auto FirstName = FirstMethod->getDeclName();
10368 auto SecondName = SecondMethod->getDeclName();
Richard Trieu1c71d512017-07-15 02:55:13 +000010369 if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010370 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10371 FirstMethod->getSourceRange(), MethodName)
Richard Trieu1c71d512017-07-15 02:55:13 +000010372 << FirstMethodType << FirstName;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010373 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10374 SecondMethod->getSourceRange(), MethodName)
Richard Trieu1c71d512017-07-15 02:55:13 +000010375 << SecondMethodType << SecondName;
Richard Trieu48143742017-02-28 21:24:38 +000010376
10377 Diagnosed = true;
10378 break;
10379 }
10380
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010381 const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10382 const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
Richard Trieu583e2c12017-03-04 00:08:58 +000010383 if (FirstDeleted != SecondDeleted) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010384 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10385 FirstMethod->getSourceRange(), MethodDeleted)
Richard Trieu1c71d512017-07-15 02:55:13 +000010386 << FirstMethodType << FirstName << FirstDeleted;
Richard Trieu583e2c12017-03-04 00:08:58 +000010387
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010388 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10389 SecondMethod->getSourceRange(), MethodDeleted)
Richard Trieu1c71d512017-07-15 02:55:13 +000010390 << SecondMethodType << SecondName << SecondDeleted;
Richard Trieu583e2c12017-03-04 00:08:58 +000010391 Diagnosed = true;
10392 break;
10393 }
10394
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010395 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10396 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10397 if (FirstDefaulted != SecondDefaulted) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010398 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10399 FirstMethod->getSourceRange(), MethodDefaulted)
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010400 << FirstMethodType << FirstName << FirstDefaulted;
10401
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010402 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10403 SecondMethod->getSourceRange(), MethodDefaulted)
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010404 << SecondMethodType << SecondName << SecondDefaulted;
10405 Diagnosed = true;
10406 break;
10407 }
10408
Richard Trieu583e2c12017-03-04 00:08:58 +000010409 const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10410 const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10411 const bool FirstPure = FirstMethod->isPure();
10412 const bool SecondPure = SecondMethod->isPure();
10413 if ((FirstVirtual || SecondVirtual) &&
10414 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010415 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10416 FirstMethod->getSourceRange(), MethodVirtual)
Richard Trieu1c71d512017-07-15 02:55:13 +000010417 << FirstMethodType << FirstName << FirstPure << FirstVirtual;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010418 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10419 SecondMethod->getSourceRange(), MethodVirtual)
Richard Trieu1c71d512017-07-15 02:55:13 +000010420 << SecondMethodType << SecondName << SecondPure << SecondVirtual;
Richard Trieu583e2c12017-03-04 00:08:58 +000010421 Diagnosed = true;
10422 break;
10423 }
10424
10425 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging,
10426 // FirstDecl is the canonical Decl of SecondDecl, so the storage
10427 // class needs to be checked instead.
10428 const auto FirstStorage = FirstMethod->getStorageClass();
10429 const auto SecondStorage = SecondMethod->getStorageClass();
10430 const bool FirstStatic = FirstStorage == SC_Static;
10431 const bool SecondStatic = SecondStorage == SC_Static;
10432 if (FirstStatic != SecondStatic) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010433 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10434 FirstMethod->getSourceRange(), MethodStatic)
Richard Trieu1c71d512017-07-15 02:55:13 +000010435 << FirstMethodType << FirstName << FirstStatic;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010436 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10437 SecondMethod->getSourceRange(), MethodStatic)
Richard Trieu1c71d512017-07-15 02:55:13 +000010438 << SecondMethodType << SecondName << SecondStatic;
Richard Trieu583e2c12017-03-04 00:08:58 +000010439 Diagnosed = true;
10440 break;
10441 }
10442
10443 const bool FirstVolatile = FirstMethod->isVolatile();
10444 const bool SecondVolatile = SecondMethod->isVolatile();
10445 if (FirstVolatile != SecondVolatile) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010446 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10447 FirstMethod->getSourceRange(), MethodVolatile)
Richard Trieu1c71d512017-07-15 02:55:13 +000010448 << FirstMethodType << FirstName << FirstVolatile;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010449 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10450 SecondMethod->getSourceRange(), MethodVolatile)
Richard Trieu1c71d512017-07-15 02:55:13 +000010451 << SecondMethodType << SecondName << SecondVolatile;
Richard Trieu583e2c12017-03-04 00:08:58 +000010452 Diagnosed = true;
10453 break;
10454 }
10455
10456 const bool FirstConst = FirstMethod->isConst();
10457 const bool SecondConst = SecondMethod->isConst();
10458 if (FirstConst != SecondConst) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010459 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10460 FirstMethod->getSourceRange(), MethodConst)
Richard Trieu1c71d512017-07-15 02:55:13 +000010461 << FirstMethodType << FirstName << FirstConst;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010462 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10463 SecondMethod->getSourceRange(), MethodConst)
Richard Trieu1c71d512017-07-15 02:55:13 +000010464 << SecondMethodType << SecondName << SecondConst;
Richard Trieu583e2c12017-03-04 00:08:58 +000010465 Diagnosed = true;
10466 break;
10467 }
10468
10469 const bool FirstInline = FirstMethod->isInlineSpecified();
10470 const bool SecondInline = SecondMethod->isInlineSpecified();
10471 if (FirstInline != SecondInline) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010472 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10473 FirstMethod->getSourceRange(), MethodInline)
Richard Trieu1c71d512017-07-15 02:55:13 +000010474 << FirstMethodType << FirstName << FirstInline;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010475 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10476 SecondMethod->getSourceRange(), MethodInline)
Richard Trieu1c71d512017-07-15 02:55:13 +000010477 << SecondMethodType << SecondName << SecondInline;
Richard Trieu583e2c12017-03-04 00:08:58 +000010478 Diagnosed = true;
10479 break;
10480 }
10481
Richard Trieu02552272017-05-02 23:58:52 +000010482 const unsigned FirstNumParameters = FirstMethod->param_size();
10483 const unsigned SecondNumParameters = SecondMethod->param_size();
10484 if (FirstNumParameters != SecondNumParameters) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010485 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10486 FirstMethod->getSourceRange(),
10487 MethodNumberParameters)
Richard Trieu1c71d512017-07-15 02:55:13 +000010488 << FirstMethodType << FirstName << FirstNumParameters;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010489 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10490 SecondMethod->getSourceRange(),
10491 MethodNumberParameters)
Richard Trieu1c71d512017-07-15 02:55:13 +000010492 << SecondMethodType << SecondName << SecondNumParameters;
Richard Trieu02552272017-05-02 23:58:52 +000010493 Diagnosed = true;
10494 break;
10495 }
10496
10497 // Need this status boolean to know when break out of the switch.
10498 bool ParameterMismatch = false;
10499 for (unsigned I = 0; I < FirstNumParameters; ++I) {
10500 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10501 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10502
10503 QualType FirstParamType = FirstParam->getType();
10504 QualType SecondParamType = SecondParam->getType();
10505 if (FirstParamType != SecondParamType &&
10506 ComputeQualTypeODRHash(FirstParamType) !=
10507 ComputeQualTypeODRHash(SecondParamType)) {
Vedant Kumar48b4f762018-04-14 01:40:48 +000010508 if (const DecayedType *ParamDecayedType =
Richard Trieu02552272017-05-02 23:58:52 +000010509 FirstParamType->getAs<DecayedType>()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010510 ODRDiagDeclError(
10511 FirstRecord, FirstModule, FirstMethod->getLocation(),
10512 FirstMethod->getSourceRange(), MethodParameterType)
Richard Trieu1c71d512017-07-15 02:55:13 +000010513 << FirstMethodType << FirstName << (I + 1) << FirstParamType
10514 << true << ParamDecayedType->getOriginalType();
Richard Trieu02552272017-05-02 23:58:52 +000010515 } else {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010516 ODRDiagDeclError(
10517 FirstRecord, FirstModule, FirstMethod->getLocation(),
10518 FirstMethod->getSourceRange(), MethodParameterType)
Richard Trieu1c71d512017-07-15 02:55:13 +000010519 << FirstMethodType << FirstName << (I + 1) << FirstParamType
10520 << false;
Richard Trieu02552272017-05-02 23:58:52 +000010521 }
10522
Vedant Kumar48b4f762018-04-14 01:40:48 +000010523 if (const DecayedType *ParamDecayedType =
Richard Trieu02552272017-05-02 23:58:52 +000010524 SecondParamType->getAs<DecayedType>()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010525 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10526 SecondMethod->getSourceRange(),
10527 MethodParameterType)
Richard Trieu1c71d512017-07-15 02:55:13 +000010528 << SecondMethodType << SecondName << (I + 1)
10529 << SecondParamType << true
Richard Trieu02552272017-05-02 23:58:52 +000010530 << ParamDecayedType->getOriginalType();
10531 } else {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010532 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10533 SecondMethod->getSourceRange(),
10534 MethodParameterType)
Richard Trieu1c71d512017-07-15 02:55:13 +000010535 << SecondMethodType << SecondName << (I + 1)
10536 << SecondParamType << false;
Richard Trieu02552272017-05-02 23:58:52 +000010537 }
10538 ParameterMismatch = true;
10539 break;
10540 }
10541
10542 DeclarationName FirstParamName = FirstParam->getDeclName();
10543 DeclarationName SecondParamName = SecondParam->getDeclName();
10544 if (FirstParamName != SecondParamName) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010545 ODRDiagDeclError(FirstRecord, FirstModule,
10546 FirstMethod->getLocation(),
10547 FirstMethod->getSourceRange(), MethodParameterName)
Richard Trieu1c71d512017-07-15 02:55:13 +000010548 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010549 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10550 SecondMethod->getSourceRange(), MethodParameterName)
Richard Trieu1c71d512017-07-15 02:55:13 +000010551 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
Richard Trieu02552272017-05-02 23:58:52 +000010552 ParameterMismatch = true;
10553 break;
10554 }
Richard Trieu6e13ff32017-06-16 02:44:29 +000010555
10556 const Expr *FirstInit = FirstParam->getInit();
10557 const Expr *SecondInit = SecondParam->getInit();
10558 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010559 ODRDiagDeclError(FirstRecord, FirstModule,
10560 FirstMethod->getLocation(),
10561 FirstMethod->getSourceRange(),
10562 MethodParameterSingleDefaultArgument)
Richard Trieu1c71d512017-07-15 02:55:13 +000010563 << FirstMethodType << FirstName << (I + 1)
10564 << (FirstInit == nullptr)
Richard Trieu6e13ff32017-06-16 02:44:29 +000010565 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010566 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10567 SecondMethod->getSourceRange(),
10568 MethodParameterSingleDefaultArgument)
Richard Trieu1c71d512017-07-15 02:55:13 +000010569 << SecondMethodType << SecondName << (I + 1)
10570 << (SecondInit == nullptr)
Richard Trieu6e13ff32017-06-16 02:44:29 +000010571 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10572 ParameterMismatch = true;
10573 break;
10574 }
10575
10576 if (FirstInit && SecondInit &&
10577 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010578 ODRDiagDeclError(FirstRecord, FirstModule,
10579 FirstMethod->getLocation(),
10580 FirstMethod->getSourceRange(),
10581 MethodParameterDifferentDefaultArgument)
Richard Trieu1c71d512017-07-15 02:55:13 +000010582 << FirstMethodType << FirstName << (I + 1)
10583 << FirstInit->getSourceRange();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010584 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10585 SecondMethod->getSourceRange(),
10586 MethodParameterDifferentDefaultArgument)
Richard Trieu1c71d512017-07-15 02:55:13 +000010587 << SecondMethodType << SecondName << (I + 1)
10588 << SecondInit->getSourceRange();
Richard Trieu6e13ff32017-06-16 02:44:29 +000010589 ParameterMismatch = true;
10590 break;
10591
10592 }
Richard Trieu02552272017-05-02 23:58:52 +000010593 }
10594
10595 if (ParameterMismatch) {
10596 Diagnosed = true;
10597 break;
10598 }
10599
Richard Trieu7282d322018-04-25 00:31:15 +000010600 const auto *FirstTemplateArgs =
10601 FirstMethod->getTemplateSpecializationArgs();
10602 const auto *SecondTemplateArgs =
10603 SecondMethod->getTemplateSpecializationArgs();
10604
10605 if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10606 (!FirstTemplateArgs && SecondTemplateArgs)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010607 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10608 FirstMethod->getSourceRange(),
10609 MethodNoTemplateArguments)
Richard Trieu7282d322018-04-25 00:31:15 +000010610 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010611 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10612 SecondMethod->getSourceRange(),
10613 MethodNoTemplateArguments)
Richard Trieu7282d322018-04-25 00:31:15 +000010614 << SecondMethodType << SecondName
10615 << (SecondTemplateArgs != nullptr);
10616
10617 Diagnosed = true;
10618 break;
10619 }
10620
10621 if (FirstTemplateArgs && SecondTemplateArgs) {
10622 // Remove pack expansions from argument list.
10623 auto ExpandTemplateArgumentList =
10624 [](const TemplateArgumentList *TAL) {
10625 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10626 for (const TemplateArgument &TA : TAL->asArray()) {
10627 if (TA.getKind() != TemplateArgument::Pack) {
10628 ExpandedList.push_back(&TA);
10629 continue;
10630 }
10631 for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10632 ExpandedList.push_back(&PackTA);
10633 }
10634 }
10635 return ExpandedList;
10636 };
10637 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10638 ExpandTemplateArgumentList(FirstTemplateArgs);
10639 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10640 ExpandTemplateArgumentList(SecondTemplateArgs);
10641
10642 if (FirstExpandedList.size() != SecondExpandedList.size()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010643 ODRDiagDeclError(FirstRecord, FirstModule,
10644 FirstMethod->getLocation(),
10645 FirstMethod->getSourceRange(),
10646 MethodDifferentNumberTemplateArguments)
Richard Trieu7282d322018-04-25 00:31:15 +000010647 << FirstMethodType << FirstName
10648 << (unsigned)FirstExpandedList.size();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010649 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10650 SecondMethod->getSourceRange(),
10651 MethodDifferentNumberTemplateArguments)
Richard Trieu7282d322018-04-25 00:31:15 +000010652 << SecondMethodType << SecondName
10653 << (unsigned)SecondExpandedList.size();
10654
10655 Diagnosed = true;
10656 break;
10657 }
10658
10659 bool TemplateArgumentMismatch = false;
10660 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10661 const TemplateArgument &FirstTA = *FirstExpandedList[i],
10662 &SecondTA = *SecondExpandedList[i];
10663 if (ComputeTemplateArgumentODRHash(FirstTA) ==
10664 ComputeTemplateArgumentODRHash(SecondTA)) {
10665 continue;
10666 }
10667
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010668 ODRDiagDeclError(
10669 FirstRecord, FirstModule, FirstMethod->getLocation(),
10670 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
Richard Trieu7282d322018-04-25 00:31:15 +000010671 << FirstMethodType << FirstName << FirstTA << i + 1;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010672 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10673 SecondMethod->getSourceRange(),
10674 MethodDifferentTemplateArgument)
Richard Trieu7282d322018-04-25 00:31:15 +000010675 << SecondMethodType << SecondName << SecondTA << i + 1;
10676
10677 TemplateArgumentMismatch = true;
10678 break;
10679 }
10680
10681 if (TemplateArgumentMismatch) {
10682 Diagnosed = true;
10683 break;
10684 }
10685 }
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010686
10687 // Compute the hash of the method as if it has no body.
10688 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10689 Hash.clear();
10690 Hash.AddFunctionDecl(D, true /*SkipBody*/);
10691 return Hash.CalculateHash();
10692 };
10693
10694 // Compare the hash generated to the hash stored. A difference means
10695 // that a body was present in the original source. Due to merging,
10696 // the stardard way of detecting a body will not work.
10697 const bool HasFirstBody =
10698 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10699 const bool HasSecondBody =
10700 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10701
10702 if (HasFirstBody != HasSecondBody) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010703 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10704 FirstMethod->getSourceRange(), MethodSingleBody)
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010705 << FirstMethodType << FirstName << HasFirstBody;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010706 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10707 SecondMethod->getSourceRange(), MethodSingleBody)
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010708 << SecondMethodType << SecondName << HasSecondBody;
10709 Diagnosed = true;
10710 break;
10711 }
10712
10713 if (HasFirstBody && HasSecondBody) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010714 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10715 FirstMethod->getSourceRange(), MethodDifferentBody)
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010716 << FirstMethodType << FirstName;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010717 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10718 SecondMethod->getSourceRange(), MethodDifferentBody)
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010719 << SecondMethodType << SecondName;
10720 Diagnosed = true;
10721 break;
10722 }
10723
Richard Trieu48143742017-02-28 21:24:38 +000010724 break;
10725 }
Richard Trieu11d566a2017-06-12 21:58:22 +000010726 case TypeAlias:
10727 case TypeDef: {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010728 Diagnosed = ODRDiagTypeDefOrAlias(
10729 FirstRecord, FirstModule, SecondModule,
10730 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10731 FirstDiffType == TypeAlias);
Richard Trieu11d566a2017-06-12 21:58:22 +000010732 break;
10733 }
Richard Trieu6e13ff32017-06-16 02:44:29 +000010734 case Var: {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010735 Diagnosed =
10736 ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10737 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
Richard Trieu6e13ff32017-06-16 02:44:29 +000010738 break;
10739 }
Richard Trieuac6a1b62017-07-08 02:04:42 +000010740 case Friend: {
Vedant Kumar48b4f762018-04-14 01:40:48 +000010741 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10742 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
Richard Trieuac6a1b62017-07-08 02:04:42 +000010743
10744 NamedDecl *FirstND = FirstFriend->getFriendDecl();
10745 NamedDecl *SecondND = SecondFriend->getFriendDecl();
10746
10747 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10748 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10749
10750 if (FirstND && SecondND) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010751 ODRDiagDeclError(FirstRecord, FirstModule,
10752 FirstFriend->getFriendLoc(),
10753 FirstFriend->getSourceRange(), FriendFunction)
Richard Trieuac6a1b62017-07-08 02:04:42 +000010754 << FirstND;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010755 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10756 SecondFriend->getSourceRange(), FriendFunction)
Richard Trieuac6a1b62017-07-08 02:04:42 +000010757 << SecondND;
10758
10759 Diagnosed = true;
10760 break;
10761 }
10762
10763 if (FirstTSI && SecondTSI) {
10764 QualType FirstFriendType = FirstTSI->getType();
10765 QualType SecondFriendType = SecondTSI->getType();
10766 assert(ComputeQualTypeODRHash(FirstFriendType) !=
10767 ComputeQualTypeODRHash(SecondFriendType));
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010768 ODRDiagDeclError(FirstRecord, FirstModule,
10769 FirstFriend->getFriendLoc(),
10770 FirstFriend->getSourceRange(), FriendType)
Richard Trieuac6a1b62017-07-08 02:04:42 +000010771 << FirstFriendType;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010772 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10773 SecondFriend->getSourceRange(), FriendType)
Richard Trieuac6a1b62017-07-08 02:04:42 +000010774 << SecondFriendType;
10775 Diagnosed = true;
10776 break;
10777 }
10778
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010779 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10780 FirstFriend->getSourceRange(), FriendTypeFunction)
Richard Trieuac6a1b62017-07-08 02:04:42 +000010781 << (FirstTSI == nullptr);
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010782 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10783 SecondFriend->getSourceRange(), FriendTypeFunction)
Richard Trieuac6a1b62017-07-08 02:04:42 +000010784 << (SecondTSI == nullptr);
10785
10786 Diagnosed = true;
10787 break;
10788 }
Richard Trieu9359e8f2018-05-30 01:12:26 +000010789 case FunctionTemplate: {
10790 FunctionTemplateDecl *FirstTemplate =
10791 cast<FunctionTemplateDecl>(FirstDecl);
10792 FunctionTemplateDecl *SecondTemplate =
10793 cast<FunctionTemplateDecl>(SecondDecl);
10794
10795 TemplateParameterList *FirstTPL =
10796 FirstTemplate->getTemplateParameters();
10797 TemplateParameterList *SecondTPL =
10798 SecondTemplate->getTemplateParameters();
10799
10800 if (FirstTPL->size() != SecondTPL->size()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010801 ODRDiagDeclError(FirstRecord, FirstModule,
10802 FirstTemplate->getLocation(),
10803 FirstTemplate->getSourceRange(),
10804 FunctionTemplateDifferentNumberParameters)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010805 << FirstTemplate << FirstTPL->size();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010806 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10807 SecondTemplate->getSourceRange(),
10808 FunctionTemplateDifferentNumberParameters)
10809 << SecondTemplate << SecondTPL->size();
Richard Trieu9359e8f2018-05-30 01:12:26 +000010810
10811 Diagnosed = true;
10812 break;
10813 }
10814
10815 bool ParameterMismatch = false;
10816 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10817 NamedDecl *FirstParam = FirstTPL->getParam(i);
10818 NamedDecl *SecondParam = SecondTPL->getParam(i);
10819
10820 if (FirstParam->getKind() != SecondParam->getKind()) {
10821 enum {
10822 TemplateTypeParameter,
10823 NonTypeTemplateParameter,
10824 TemplateTemplateParameter,
10825 };
10826 auto GetParamType = [](NamedDecl *D) {
10827 switch (D->getKind()) {
10828 default:
10829 llvm_unreachable("Unexpected template parameter type");
10830 case Decl::TemplateTypeParm:
10831 return TemplateTypeParameter;
10832 case Decl::NonTypeTemplateParm:
10833 return NonTypeTemplateParameter;
10834 case Decl::TemplateTemplateParm:
10835 return TemplateTemplateParameter;
10836 }
10837 };
10838
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010839 ODRDiagDeclError(FirstRecord, FirstModule,
10840 FirstTemplate->getLocation(),
10841 FirstTemplate->getSourceRange(),
10842 FunctionTemplateParameterDifferentKind)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010843 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010844 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10845 SecondTemplate->getSourceRange(),
10846 FunctionTemplateParameterDifferentKind)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010847 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10848
10849 ParameterMismatch = true;
10850 break;
10851 }
10852
10853 if (FirstParam->getName() != SecondParam->getName()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010854 ODRDiagDeclError(
10855 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10856 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010857 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10858 << FirstParam;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010859 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10860 SecondTemplate->getSourceRange(),
10861 FunctionTemplateParameterName)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010862 << SecondTemplate << (i + 1)
10863 << (bool)SecondParam->getIdentifier() << SecondParam;
10864 ParameterMismatch = true;
10865 break;
10866 }
10867
10868 if (isa<TemplateTypeParmDecl>(FirstParam) &&
10869 isa<TemplateTypeParmDecl>(SecondParam)) {
10870 TemplateTypeParmDecl *FirstTTPD =
10871 cast<TemplateTypeParmDecl>(FirstParam);
10872 TemplateTypeParmDecl *SecondTTPD =
10873 cast<TemplateTypeParmDecl>(SecondParam);
10874 bool HasFirstDefaultArgument =
10875 FirstTTPD->hasDefaultArgument() &&
10876 !FirstTTPD->defaultArgumentWasInherited();
10877 bool HasSecondDefaultArgument =
10878 SecondTTPD->hasDefaultArgument() &&
10879 !SecondTTPD->defaultArgumentWasInherited();
10880 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010881 ODRDiagDeclError(FirstRecord, FirstModule,
10882 FirstTemplate->getLocation(),
10883 FirstTemplate->getSourceRange(),
10884 FunctionTemplateParameterSingleDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010885 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010886 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10887 SecondTemplate->getSourceRange(),
10888 FunctionTemplateParameterSingleDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010889 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10890 ParameterMismatch = true;
10891 break;
10892 }
10893
10894 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10895 QualType FirstType = FirstTTPD->getDefaultArgument();
10896 QualType SecondType = SecondTTPD->getDefaultArgument();
10897 if (ComputeQualTypeODRHash(FirstType) !=
10898 ComputeQualTypeODRHash(SecondType)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010899 ODRDiagDeclError(
10900 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10901 FirstTemplate->getSourceRange(),
10902 FunctionTemplateParameterDifferentDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010903 << FirstTemplate << (i + 1) << FirstType;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010904 ODRDiagDeclNote(
10905 SecondModule, SecondTemplate->getLocation(),
10906 SecondTemplate->getSourceRange(),
10907 FunctionTemplateParameterDifferentDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010908 << SecondTemplate << (i + 1) << SecondType;
10909 ParameterMismatch = true;
10910 break;
10911 }
10912 }
10913
10914 if (FirstTTPD->isParameterPack() !=
10915 SecondTTPD->isParameterPack()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010916 ODRDiagDeclError(FirstRecord, FirstModule,
10917 FirstTemplate->getLocation(),
10918 FirstTemplate->getSourceRange(),
10919 FunctionTemplatePackParameter)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010920 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010921 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10922 SecondTemplate->getSourceRange(),
10923 FunctionTemplatePackParameter)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010924 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10925 ParameterMismatch = true;
10926 break;
10927 }
10928 }
10929
10930 if (isa<TemplateTemplateParmDecl>(FirstParam) &&
10931 isa<TemplateTemplateParmDecl>(SecondParam)) {
10932 TemplateTemplateParmDecl *FirstTTPD =
10933 cast<TemplateTemplateParmDecl>(FirstParam);
10934 TemplateTemplateParmDecl *SecondTTPD =
10935 cast<TemplateTemplateParmDecl>(SecondParam);
10936
10937 TemplateParameterList *FirstTPL =
10938 FirstTTPD->getTemplateParameters();
10939 TemplateParameterList *SecondTPL =
10940 SecondTTPD->getTemplateParameters();
10941
10942 if (ComputeTemplateParameterListODRHash(FirstTPL) !=
10943 ComputeTemplateParameterListODRHash(SecondTPL)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010944 ODRDiagDeclError(FirstRecord, FirstModule,
10945 FirstTemplate->getLocation(),
10946 FirstTemplate->getSourceRange(),
10947 FunctionTemplateParameterDifferentType)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010948 << FirstTemplate << (i + 1);
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010949 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10950 SecondTemplate->getSourceRange(),
10951 FunctionTemplateParameterDifferentType)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010952 << SecondTemplate << (i + 1);
10953 ParameterMismatch = true;
10954 break;
10955 }
10956
10957 bool HasFirstDefaultArgument =
10958 FirstTTPD->hasDefaultArgument() &&
10959 !FirstTTPD->defaultArgumentWasInherited();
10960 bool HasSecondDefaultArgument =
10961 SecondTTPD->hasDefaultArgument() &&
10962 !SecondTTPD->defaultArgumentWasInherited();
10963 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010964 ODRDiagDeclError(FirstRecord, FirstModule,
10965 FirstTemplate->getLocation(),
10966 FirstTemplate->getSourceRange(),
10967 FunctionTemplateParameterSingleDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010968 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010969 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10970 SecondTemplate->getSourceRange(),
10971 FunctionTemplateParameterSingleDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010972 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10973 ParameterMismatch = true;
10974 break;
10975 }
10976
10977 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10978 TemplateArgument FirstTA =
10979 FirstTTPD->getDefaultArgument().getArgument();
10980 TemplateArgument SecondTA =
10981 SecondTTPD->getDefaultArgument().getArgument();
10982 if (ComputeTemplateArgumentODRHash(FirstTA) !=
10983 ComputeTemplateArgumentODRHash(SecondTA)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010984 ODRDiagDeclError(
10985 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10986 FirstTemplate->getSourceRange(),
10987 FunctionTemplateParameterDifferentDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010988 << FirstTemplate << (i + 1) << FirstTA;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010989 ODRDiagDeclNote(
10990 SecondModule, SecondTemplate->getLocation(),
10991 SecondTemplate->getSourceRange(),
10992 FunctionTemplateParameterDifferentDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010993 << SecondTemplate << (i + 1) << SecondTA;
10994 ParameterMismatch = true;
10995 break;
10996 }
10997 }
10998
10999 if (FirstTTPD->isParameterPack() !=
11000 SecondTTPD->isParameterPack()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011001 ODRDiagDeclError(FirstRecord, FirstModule,
11002 FirstTemplate->getLocation(),
11003 FirstTemplate->getSourceRange(),
11004 FunctionTemplatePackParameter)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011005 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011006 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11007 SecondTemplate->getSourceRange(),
11008 FunctionTemplatePackParameter)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011009 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11010 ParameterMismatch = true;
11011 break;
11012 }
11013 }
11014
11015 if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11016 isa<NonTypeTemplateParmDecl>(SecondParam)) {
11017 NonTypeTemplateParmDecl *FirstNTTPD =
11018 cast<NonTypeTemplateParmDecl>(FirstParam);
11019 NonTypeTemplateParmDecl *SecondNTTPD =
11020 cast<NonTypeTemplateParmDecl>(SecondParam);
11021
11022 QualType FirstType = FirstNTTPD->getType();
11023 QualType SecondType = SecondNTTPD->getType();
11024 if (ComputeQualTypeODRHash(FirstType) !=
11025 ComputeQualTypeODRHash(SecondType)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011026 ODRDiagDeclError(FirstRecord, FirstModule,
11027 FirstTemplate->getLocation(),
11028 FirstTemplate->getSourceRange(),
11029 FunctionTemplateParameterDifferentType)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011030 << FirstTemplate << (i + 1);
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011031 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11032 SecondTemplate->getSourceRange(),
11033 FunctionTemplateParameterDifferentType)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011034 << SecondTemplate << (i + 1);
11035 ParameterMismatch = true;
11036 break;
11037 }
11038
11039 bool HasFirstDefaultArgument =
11040 FirstNTTPD->hasDefaultArgument() &&
11041 !FirstNTTPD->defaultArgumentWasInherited();
11042 bool HasSecondDefaultArgument =
11043 SecondNTTPD->hasDefaultArgument() &&
11044 !SecondNTTPD->defaultArgumentWasInherited();
11045 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011046 ODRDiagDeclError(FirstRecord, FirstModule,
11047 FirstTemplate->getLocation(),
11048 FirstTemplate->getSourceRange(),
11049 FunctionTemplateParameterSingleDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011050 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011051 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11052 SecondTemplate->getSourceRange(),
11053 FunctionTemplateParameterSingleDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011054 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11055 ParameterMismatch = true;
11056 break;
11057 }
11058
11059 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11060 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11061 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11062 if (ComputeODRHash(FirstDefaultArgument) !=
11063 ComputeODRHash(SecondDefaultArgument)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011064 ODRDiagDeclError(
11065 FirstRecord, FirstModule, FirstTemplate->getLocation(),
11066 FirstTemplate->getSourceRange(),
11067 FunctionTemplateParameterDifferentDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011068 << FirstTemplate << (i + 1) << FirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011069 ODRDiagDeclNote(
11070 SecondModule, SecondTemplate->getLocation(),
11071 SecondTemplate->getSourceRange(),
11072 FunctionTemplateParameterDifferentDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011073 << SecondTemplate << (i + 1) << SecondDefaultArgument;
11074 ParameterMismatch = true;
11075 break;
11076 }
11077 }
11078
11079 if (FirstNTTPD->isParameterPack() !=
11080 SecondNTTPD->isParameterPack()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011081 ODRDiagDeclError(FirstRecord, FirstModule,
11082 FirstTemplate->getLocation(),
11083 FirstTemplate->getSourceRange(),
11084 FunctionTemplatePackParameter)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011085 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011086 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11087 SecondTemplate->getSourceRange(),
11088 FunctionTemplatePackParameter)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011089 << SecondTemplate << (i + 1)
11090 << SecondNTTPD->isParameterPack();
11091 ParameterMismatch = true;
11092 break;
11093 }
11094 }
11095 }
11096
11097 if (ParameterMismatch) {
11098 Diagnosed = true;
11099 break;
11100 }
11101
11102 break;
11103 }
Richard Trieu639d7b62017-02-22 22:22:42 +000011104 }
11105
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000011106 if (Diagnosed)
Richard Trieue7f7ed22017-02-22 01:11:25 +000011107 continue;
11108
Richard Trieu708859a2017-06-08 00:56:21 +000011109 Diag(FirstDecl->getLocation(),
11110 diag::err_module_odr_violation_mismatch_decl_unknown)
11111 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11112 << FirstDecl->getSourceRange();
11113 Diag(SecondDecl->getLocation(),
11114 diag::note_module_odr_violation_mismatch_decl_unknown)
11115 << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
Richard Trieue7f7ed22017-02-22 01:11:25 +000011116 Diagnosed = true;
Richard Smithcd45dbc2014-04-19 03:48:30 +000011117 }
11118
11119 if (!Diagnosed) {
11120 // All definitions are updates to the same declaration. This happens if a
11121 // module instantiates the declaration of a class template specialization
11122 // and two or more other modules instantiate its definition.
11123 //
11124 // FIXME: Indicate which modules had instantiations of this definition.
11125 // FIXME: How can this even happen?
11126 Diag(Merge.first->getLocation(),
11127 diag::err_module_odr_violation_different_instantiations)
11128 << Merge.first;
11129 }
11130 }
Richard Trieue6caa262017-12-23 00:41:01 +000011131
11132 // Issue ODR failures diagnostics for functions.
11133 for (auto &Merge : FunctionOdrMergeFailures) {
11134 enum ODRFunctionDifference {
11135 ReturnType,
11136 ParameterName,
11137 ParameterType,
11138 ParameterSingleDefaultArgument,
11139 ParameterDifferentDefaultArgument,
11140 FunctionBody,
11141 };
11142
11143 FunctionDecl *FirstFunction = Merge.first;
11144 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11145
11146 bool Diagnosed = false;
11147 for (auto &SecondFunction : Merge.second) {
Vedant Kumar48b4f762018-04-14 01:40:48 +000011148
Richard Trieue6caa262017-12-23 00:41:01 +000011149 if (FirstFunction == SecondFunction)
11150 continue;
11151
11152 std::string SecondModule =
11153 getOwningModuleNameForDiagnostic(SecondFunction);
11154
11155 auto ODRDiagError = [FirstFunction, &FirstModule,
11156 this](SourceLocation Loc, SourceRange Range,
11157 ODRFunctionDifference DiffType) {
11158 return Diag(Loc, diag::err_module_odr_violation_function)
11159 << FirstFunction << FirstModule.empty() << FirstModule << Range
11160 << DiffType;
11161 };
11162 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11163 SourceRange Range,
11164 ODRFunctionDifference DiffType) {
11165 return Diag(Loc, diag::note_module_odr_violation_function)
11166 << SecondModule << Range << DiffType;
11167 };
11168
11169 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11170 ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11171 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11172 FirstFunction->getReturnTypeSourceRange(), ReturnType)
11173 << FirstFunction->getReturnType();
11174 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11175 SecondFunction->getReturnTypeSourceRange(), ReturnType)
11176 << SecondFunction->getReturnType();
11177 Diagnosed = true;
11178 break;
11179 }
11180
11181 assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11182 "Merged functions with different number of parameters");
11183
11184 auto ParamSize = FirstFunction->param_size();
11185 bool ParameterMismatch = false;
11186 for (unsigned I = 0; I < ParamSize; ++I) {
11187 auto *FirstParam = FirstFunction->getParamDecl(I);
11188 auto *SecondParam = SecondFunction->getParamDecl(I);
11189
11190 assert(getContext().hasSameType(FirstParam->getType(),
11191 SecondParam->getType()) &&
11192 "Merged function has different parameter types.");
11193
11194 if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11195 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11196 ParameterName)
11197 << I + 1 << FirstParam->getDeclName();
11198 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11199 ParameterName)
11200 << I + 1 << SecondParam->getDeclName();
11201 ParameterMismatch = true;
11202 break;
11203 };
11204
11205 QualType FirstParamType = FirstParam->getType();
11206 QualType SecondParamType = SecondParam->getType();
11207 if (FirstParamType != SecondParamType &&
11208 ComputeQualTypeODRHash(FirstParamType) !=
11209 ComputeQualTypeODRHash(SecondParamType)) {
Vedant Kumar48b4f762018-04-14 01:40:48 +000011210 if (const DecayedType *ParamDecayedType =
Richard Trieue6caa262017-12-23 00:41:01 +000011211 FirstParamType->getAs<DecayedType>()) {
11212 ODRDiagError(FirstParam->getLocation(),
11213 FirstParam->getSourceRange(), ParameterType)
11214 << (I + 1) << FirstParamType << true
11215 << ParamDecayedType->getOriginalType();
11216 } else {
11217 ODRDiagError(FirstParam->getLocation(),
11218 FirstParam->getSourceRange(), ParameterType)
11219 << (I + 1) << FirstParamType << false;
11220 }
11221
Vedant Kumar48b4f762018-04-14 01:40:48 +000011222 if (const DecayedType *ParamDecayedType =
Richard Trieue6caa262017-12-23 00:41:01 +000011223 SecondParamType->getAs<DecayedType>()) {
11224 ODRDiagNote(SecondParam->getLocation(),
11225 SecondParam->getSourceRange(), ParameterType)
11226 << (I + 1) << SecondParamType << true
11227 << ParamDecayedType->getOriginalType();
11228 } else {
11229 ODRDiagNote(SecondParam->getLocation(),
11230 SecondParam->getSourceRange(), ParameterType)
11231 << (I + 1) << SecondParamType << false;
11232 }
11233 ParameterMismatch = true;
11234 break;
11235 }
11236
11237 const Expr *FirstInit = FirstParam->getInit();
11238 const Expr *SecondInit = SecondParam->getInit();
11239 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11240 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11241 ParameterSingleDefaultArgument)
11242 << (I + 1) << (FirstInit == nullptr)
11243 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11244 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11245 ParameterSingleDefaultArgument)
11246 << (I + 1) << (SecondInit == nullptr)
11247 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11248 ParameterMismatch = true;
11249 break;
11250 }
11251
11252 if (FirstInit && SecondInit &&
11253 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11254 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11255 ParameterDifferentDefaultArgument)
11256 << (I + 1) << FirstInit->getSourceRange();
11257 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11258 ParameterDifferentDefaultArgument)
11259 << (I + 1) << SecondInit->getSourceRange();
11260 ParameterMismatch = true;
11261 break;
11262 }
11263
11264 assert(ComputeSubDeclODRHash(FirstParam) ==
11265 ComputeSubDeclODRHash(SecondParam) &&
11266 "Undiagnosed parameter difference.");
11267 }
11268
11269 if (ParameterMismatch) {
11270 Diagnosed = true;
11271 break;
11272 }
11273
11274 // If no error has been generated before now, assume the problem is in
11275 // the body and generate a message.
11276 ODRDiagError(FirstFunction->getLocation(),
11277 FirstFunction->getSourceRange(), FunctionBody);
11278 ODRDiagNote(SecondFunction->getLocation(),
11279 SecondFunction->getSourceRange(), FunctionBody);
11280 Diagnosed = true;
11281 break;
11282 }
Evgeny Stupachenkobf25d672018-01-05 02:22:52 +000011283 (void)Diagnosed;
Richard Trieue6caa262017-12-23 00:41:01 +000011284 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11285 }
Richard Trieuab4d7302018-07-25 22:52:05 +000011286
11287 // Issue ODR failures diagnostics for enums.
11288 for (auto &Merge : EnumOdrMergeFailures) {
11289 enum ODREnumDifference {
11290 SingleScopedEnum,
11291 EnumTagKeywordMismatch,
11292 SingleSpecifiedType,
11293 DifferentSpecifiedTypes,
11294 DifferentNumberEnumConstants,
11295 EnumConstantName,
11296 EnumConstantSingleInitilizer,
11297 EnumConstantDifferentInitilizer,
11298 };
11299
11300 // If we've already pointed out a specific problem with this enum, don't
11301 // bother issuing a general "something's different" diagnostic.
11302 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11303 continue;
11304
11305 EnumDecl *FirstEnum = Merge.first;
11306 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11307
11308 using DeclHashes =
11309 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11310 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11311 DeclHashes &Hashes, EnumDecl *Enum) {
11312 for (auto *D : Enum->decls()) {
11313 // Due to decl merging, the first EnumDecl is the parent of
11314 // Decls in both records.
11315 if (!ODRHash::isWhitelistedDecl(D, FirstEnum))
11316 continue;
11317 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11318 Hashes.emplace_back(cast<EnumConstantDecl>(D),
11319 ComputeSubDeclODRHash(D));
11320 }
11321 };
11322 DeclHashes FirstHashes;
11323 PopulateHashes(FirstHashes, FirstEnum);
11324 bool Diagnosed = false;
11325 for (auto &SecondEnum : Merge.second) {
11326
11327 if (FirstEnum == SecondEnum)
11328 continue;
11329
11330 std::string SecondModule =
11331 getOwningModuleNameForDiagnostic(SecondEnum);
11332
11333 auto ODRDiagError = [FirstEnum, &FirstModule,
11334 this](SourceLocation Loc, SourceRange Range,
11335 ODREnumDifference DiffType) {
11336 return Diag(Loc, diag::err_module_odr_violation_enum)
11337 << FirstEnum << FirstModule.empty() << FirstModule << Range
11338 << DiffType;
11339 };
11340 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11341 SourceRange Range,
11342 ODREnumDifference DiffType) {
11343 return Diag(Loc, diag::note_module_odr_violation_enum)
11344 << SecondModule << Range << DiffType;
11345 };
11346
11347 if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11348 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11349 SingleScopedEnum)
11350 << FirstEnum->isScoped();
11351 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11352 SingleScopedEnum)
11353 << SecondEnum->isScoped();
11354 Diagnosed = true;
11355 continue;
11356 }
11357
11358 if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11359 if (FirstEnum->isScopedUsingClassTag() !=
11360 SecondEnum->isScopedUsingClassTag()) {
11361 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11362 EnumTagKeywordMismatch)
11363 << FirstEnum->isScopedUsingClassTag();
11364 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11365 EnumTagKeywordMismatch)
11366 << SecondEnum->isScopedUsingClassTag();
11367 Diagnosed = true;
11368 continue;
11369 }
11370 }
11371
11372 QualType FirstUnderlyingType =
11373 FirstEnum->getIntegerTypeSourceInfo()
11374 ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11375 : QualType();
11376 QualType SecondUnderlyingType =
11377 SecondEnum->getIntegerTypeSourceInfo()
11378 ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11379 : QualType();
11380 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11381 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11382 SingleSpecifiedType)
11383 << !FirstUnderlyingType.isNull();
11384 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11385 SingleSpecifiedType)
11386 << !SecondUnderlyingType.isNull();
11387 Diagnosed = true;
11388 continue;
11389 }
11390
11391 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11392 if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11393 ComputeQualTypeODRHash(SecondUnderlyingType)) {
11394 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11395 DifferentSpecifiedTypes)
11396 << FirstUnderlyingType;
11397 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11398 DifferentSpecifiedTypes)
11399 << SecondUnderlyingType;
11400 Diagnosed = true;
11401 continue;
11402 }
11403 }
11404
11405 DeclHashes SecondHashes;
11406 PopulateHashes(SecondHashes, SecondEnum);
11407
11408 if (FirstHashes.size() != SecondHashes.size()) {
11409 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11410 DifferentNumberEnumConstants)
11411 << (int)FirstHashes.size();
11412 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11413 DifferentNumberEnumConstants)
11414 << (int)SecondHashes.size();
11415 Diagnosed = true;
11416 continue;
11417 }
11418
11419 for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11420 if (FirstHashes[I].second == SecondHashes[I].second)
11421 continue;
11422 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11423 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11424
11425 if (FirstEnumConstant->getDeclName() !=
11426 SecondEnumConstant->getDeclName()) {
11427
11428 ODRDiagError(FirstEnumConstant->getLocation(),
11429 FirstEnumConstant->getSourceRange(), EnumConstantName)
11430 << I + 1 << FirstEnumConstant;
11431 ODRDiagNote(SecondEnumConstant->getLocation(),
11432 SecondEnumConstant->getSourceRange(), EnumConstantName)
11433 << I + 1 << SecondEnumConstant;
11434 Diagnosed = true;
11435 break;
11436 }
11437
11438 const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11439 const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11440 if (!FirstInit && !SecondInit)
11441 continue;
11442
11443 if (!FirstInit || !SecondInit) {
11444 ODRDiagError(FirstEnumConstant->getLocation(),
11445 FirstEnumConstant->getSourceRange(),
11446 EnumConstantSingleInitilizer)
11447 << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11448 ODRDiagNote(SecondEnumConstant->getLocation(),
11449 SecondEnumConstant->getSourceRange(),
11450 EnumConstantSingleInitilizer)
11451 << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11452 Diagnosed = true;
11453 break;
11454 }
11455
11456 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11457 ODRDiagError(FirstEnumConstant->getLocation(),
11458 FirstEnumConstant->getSourceRange(),
11459 EnumConstantDifferentInitilizer)
11460 << I + 1 << FirstEnumConstant;
11461 ODRDiagNote(SecondEnumConstant->getLocation(),
11462 SecondEnumConstant->getSourceRange(),
11463 EnumConstantDifferentInitilizer)
11464 << I + 1 << SecondEnumConstant;
11465 Diagnosed = true;
11466 break;
11467 }
11468 }
11469 }
11470
11471 (void)Diagnosed;
11472 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11473 }
Guy Benyei11169dd2012-12-18 14:30:41 +000011474}
11475
Richard Smithce18a182015-07-14 00:26:00 +000011476void ASTReader::StartedDeserializing() {
David L. Jonesc4808b9e2016-12-15 20:53:26 +000011477 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
Richard Smithce18a182015-07-14 00:26:00 +000011478 ReadTimer->startTimer();
11479}
11480
Guy Benyei11169dd2012-12-18 14:30:41 +000011481void ASTReader::FinishedDeserializing() {
11482 assert(NumCurrentElementsDeserializing &&
11483 "FinishedDeserializing not paired with StartedDeserializing");
11484 if (NumCurrentElementsDeserializing == 1) {
11485 // We decrease NumCurrentElementsDeserializing only after pending actions
11486 // are finished, to avoid recursively re-calling finishPendingActions().
11487 finishPendingActions();
11488 }
11489 --NumCurrentElementsDeserializing;
11490
Richard Smitha0ce9c42014-07-29 23:23:27 +000011491 if (NumCurrentElementsDeserializing == 0) {
Richard Smitha62d1982018-08-03 01:00:01 +000011492 // Propagate exception specification and deduced type updates along
11493 // redeclaration chains.
11494 //
11495 // We do this now rather than in finishPendingActions because we want to
11496 // be able to walk the complete redeclaration chains of the updated decls.
11497 while (!PendingExceptionSpecUpdates.empty() ||
11498 !PendingDeducedTypeUpdates.empty()) {
11499 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
Richard Smith7226f2a2015-03-23 19:54:56 +000011500 PendingExceptionSpecUpdates.clear();
Richard Smitha62d1982018-08-03 01:00:01 +000011501 for (auto Update : ESUpdates) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +000011502 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Richard Smith7226f2a2015-03-23 19:54:56 +000011503 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
Richard Smith1d0f1992015-08-19 21:09:32 +000011504 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
Richard Smithdbafb6c2017-06-29 23:23:46 +000011505 if (auto *Listener = getContext().getASTMutationListener())
Richard Smithd88a7f12015-09-01 20:35:42 +000011506 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
Richard Smith1d0f1992015-08-19 21:09:32 +000011507 for (auto *Redecl : Update.second->redecls())
Richard Smithdbafb6c2017-06-29 23:23:46 +000011508 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
Richard Smith7226f2a2015-03-23 19:54:56 +000011509 }
Richard Smitha62d1982018-08-03 01:00:01 +000011510
11511 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11512 PendingDeducedTypeUpdates.clear();
11513 for (auto Update : DTUpdates) {
11514 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11515 // FIXME: If the return type is already deduced, check that it matches.
11516 getContext().adjustDeducedFunctionResultType(Update.first,
11517 Update.second);
11518 }
Richard Smith9e2341d2015-03-23 03:25:59 +000011519 }
11520
Richard Smithce18a182015-07-14 00:26:00 +000011521 if (ReadTimer)
11522 ReadTimer->stopTimer();
11523
Richard Smith0f4e2c42015-08-06 04:23:48 +000011524 diagnoseOdrViolations();
11525
Richard Smith04d05b52014-03-23 00:27:18 +000011526 // We are not in recursive loading, so it's safe to pass the "interesting"
11527 // decls to the consumer.
Richard Smitha0ce9c42014-07-29 23:23:27 +000011528 if (Consumer)
11529 PassInterestingDeclsToConsumer();
Guy Benyei11169dd2012-12-18 14:30:41 +000011530 }
11531}
11532
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +000011533void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
Richard Smith9e2341d2015-03-23 03:25:59 +000011534 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11535 // Remove any fake results before adding any real ones.
11536 auto It = PendingFakeLookupResults.find(II);
11537 if (It != PendingFakeLookupResults.end()) {
Richard Smitha534a312015-07-21 23:54:07 +000011538 for (auto *ND : It->second)
Richard Smith9e2341d2015-03-23 03:25:59 +000011539 SemaObj->IdResolver.RemoveDecl(ND);
Ben Langmuireb8bd2d2015-04-10 22:25:42 +000011540 // FIXME: this works around module+PCH performance issue.
11541 // Rather than erase the result from the map, which is O(n), just clear
11542 // the vector of NamedDecls.
11543 It->second.clear();
Richard Smith9e2341d2015-03-23 03:25:59 +000011544 }
11545 }
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +000011546
11547 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11548 SemaObj->TUScope->AddDecl(D);
11549 } else if (SemaObj->TUScope) {
11550 // Adding the decl to IdResolver may have failed because it was already in
11551 // (even though it was not added in scope). If it is already in, make sure
11552 // it gets in the scope as well.
11553 if (std::find(SemaObj->IdResolver.begin(Name),
11554 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11555 SemaObj->TUScope->AddDecl(D);
11556 }
11557}
11558
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +000011559ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11560 ASTContext *Context,
David Blaikie61137e12017-01-05 18:23:18 +000011561 const PCHContainerReader &PCHContainerRdr,
11562 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11563 StringRef isysroot, bool DisableValidation,
11564 bool AllowASTWithCompilerErrors,
11565 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +000011566 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
David Blaikie61137e12017-01-05 18:23:18 +000011567 std::unique_ptr<llvm::Timer> ReadTimer)
11568 : Listener(DisableValidation
11569 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11570 : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
David Blaikie61137e12017-01-05 18:23:18 +000011571 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
David Blaikie9d7c1ba2017-01-05 18:45:43 +000011572 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +000011573 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11574 PCHContainerRdr, PP.getHeaderSearchInfo()),
11575 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
David Blaikie61137e12017-01-05 18:23:18 +000011576 DisableValidation(DisableValidation),
Nico Weber824285e2014-05-08 04:26:47 +000011577 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11578 AllowConfigurationMismatch(AllowConfigurationMismatch),
11579 ValidateSystemInputs(ValidateSystemInputs),
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +000011580 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
David Blaikie9d7c1ba2017-01-05 18:45:43 +000011581 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
Guy Benyei11169dd2012-12-18 14:30:41 +000011582 SourceMgr.setExternalSLocEntrySource(this);
Douglas Gregor6623e1f2015-11-03 18:33:07 +000011583
11584 for (const auto &Ext : Extensions) {
11585 auto BlockName = Ext->getExtensionMetadata().BlockName;
11586 auto Known = ModuleFileExtensions.find(BlockName);
11587 if (Known != ModuleFileExtensions.end()) {
11588 Diags.Report(diag::warn_duplicate_module_file_extension)
11589 << BlockName;
11590 continue;
11591 }
11592
11593 ModuleFileExtensions.insert({BlockName, Ext});
11594 }
Guy Benyei11169dd2012-12-18 14:30:41 +000011595}
11596
11597ASTReader::~ASTReader() {
Nico Weber824285e2014-05-08 04:26:47 +000011598 if (OwnsDeserializationListener)
11599 delete DeserializationListener;
Guy Benyei11169dd2012-12-18 14:30:41 +000011600}
Richard Smith10379092016-05-06 23:14:07 +000011601
11602IdentifierResolver &ASTReader::getIdResolver() {
11603 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11604}
David L. Jonesbe1557a2016-12-21 00:17:49 +000011605
JF Bastien0e828952019-06-26 19:50:12 +000011606Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11607 unsigned AbbrevID) {
David L. Jonesbe1557a2016-12-21 00:17:49 +000011608 Idx = 0;
11609 Record.clear();
11610 return Cursor.readRecord(AbbrevID, Record);
11611}
Kelvin Libe286f52018-09-15 13:54:15 +000011612//===----------------------------------------------------------------------===//
11613//// OMPClauseReader implementation
11614////===----------------------------------------------------------------------===//
11615
John McCallc2f18312019-12-14 03:01:28 -050011616// This has to be in namespace clang because it's friended by all
11617// of the OMP clauses.
11618namespace clang {
11619
11620class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11621 ASTRecordReader &Record;
11622 ASTContext &Context;
11623
11624public:
11625 OMPClauseReader(ASTRecordReader &Record)
11626 : Record(Record), Context(Record.getContext()) {}
11627
Johannes Doerfert419a5592020-03-30 19:58:40 -050011628#define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11629#include "llvm/Frontend/OpenMP/OMPKinds.def"
John McCallc2f18312019-12-14 03:01:28 -050011630 OMPClause *readClause();
11631 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11632 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11633};
11634
11635} // end namespace clang
11636
11637OMPClause *ASTRecordReader::readOMPClause() {
11638 return OMPClauseReader(*this).readClause();
11639}
11640
Kelvin Libe286f52018-09-15 13:54:15 +000011641OMPClause *OMPClauseReader::readClause() {
Simon Pilgrim556fbfe2019-09-15 16:05:20 +000011642 OMPClause *C = nullptr;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011643 switch (llvm::omp::Clause(Record.readInt())) {
11644 case llvm::omp::OMPC_if:
Kelvin Libe286f52018-09-15 13:54:15 +000011645 C = new (Context) OMPIfClause();
11646 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011647 case llvm::omp::OMPC_final:
Kelvin Libe286f52018-09-15 13:54:15 +000011648 C = new (Context) OMPFinalClause();
11649 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011650 case llvm::omp::OMPC_num_threads:
Kelvin Libe286f52018-09-15 13:54:15 +000011651 C = new (Context) OMPNumThreadsClause();
11652 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011653 case llvm::omp::OMPC_safelen:
Kelvin Libe286f52018-09-15 13:54:15 +000011654 C = new (Context) OMPSafelenClause();
11655 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011656 case llvm::omp::OMPC_simdlen:
Kelvin Libe286f52018-09-15 13:54:15 +000011657 C = new (Context) OMPSimdlenClause();
11658 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011659 case llvm::omp::OMPC_allocator:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000011660 C = new (Context) OMPAllocatorClause();
11661 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011662 case llvm::omp::OMPC_collapse:
Kelvin Libe286f52018-09-15 13:54:15 +000011663 C = new (Context) OMPCollapseClause();
11664 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011665 case llvm::omp::OMPC_default:
Kelvin Libe286f52018-09-15 13:54:15 +000011666 C = new (Context) OMPDefaultClause();
11667 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011668 case llvm::omp::OMPC_proc_bind:
Kelvin Libe286f52018-09-15 13:54:15 +000011669 C = new (Context) OMPProcBindClause();
11670 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011671 case llvm::omp::OMPC_schedule:
Kelvin Libe286f52018-09-15 13:54:15 +000011672 C = new (Context) OMPScheduleClause();
11673 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011674 case llvm::omp::OMPC_ordered:
Kelvin Libe286f52018-09-15 13:54:15 +000011675 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11676 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011677 case llvm::omp::OMPC_nowait:
Kelvin Libe286f52018-09-15 13:54:15 +000011678 C = new (Context) OMPNowaitClause();
11679 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011680 case llvm::omp::OMPC_untied:
Kelvin Libe286f52018-09-15 13:54:15 +000011681 C = new (Context) OMPUntiedClause();
11682 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011683 case llvm::omp::OMPC_mergeable:
Kelvin Libe286f52018-09-15 13:54:15 +000011684 C = new (Context) OMPMergeableClause();
11685 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011686 case llvm::omp::OMPC_read:
Kelvin Libe286f52018-09-15 13:54:15 +000011687 C = new (Context) OMPReadClause();
11688 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011689 case llvm::omp::OMPC_write:
Kelvin Libe286f52018-09-15 13:54:15 +000011690 C = new (Context) OMPWriteClause();
11691 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011692 case llvm::omp::OMPC_update:
Alexey Bataev82f7c202020-03-03 13:22:35 -050011693 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
Kelvin Libe286f52018-09-15 13:54:15 +000011694 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011695 case llvm::omp::OMPC_capture:
Kelvin Libe286f52018-09-15 13:54:15 +000011696 C = new (Context) OMPCaptureClause();
11697 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011698 case llvm::omp::OMPC_seq_cst:
Kelvin Libe286f52018-09-15 13:54:15 +000011699 C = new (Context) OMPSeqCstClause();
11700 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011701 case llvm::omp::OMPC_acq_rel:
Alexey Bataevea9166b2020-02-06 16:30:23 -050011702 C = new (Context) OMPAcqRelClause();
11703 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011704 case llvm::omp::OMPC_acquire:
Alexey Bataev04a830f2020-02-10 14:30:39 -050011705 C = new (Context) OMPAcquireClause();
11706 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011707 case llvm::omp::OMPC_release:
Alexey Bataev95598342020-02-10 15:49:05 -050011708 C = new (Context) OMPReleaseClause();
11709 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011710 case llvm::omp::OMPC_relaxed:
Alexey Bataev9a8defc2020-02-11 11:10:43 -050011711 C = new (Context) OMPRelaxedClause();
11712 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011713 case llvm::omp::OMPC_threads:
Kelvin Libe286f52018-09-15 13:54:15 +000011714 C = new (Context) OMPThreadsClause();
11715 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011716 case llvm::omp::OMPC_simd:
Kelvin Libe286f52018-09-15 13:54:15 +000011717 C = new (Context) OMPSIMDClause();
11718 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011719 case llvm::omp::OMPC_nogroup:
Kelvin Libe286f52018-09-15 13:54:15 +000011720 C = new (Context) OMPNogroupClause();
11721 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011722 case llvm::omp::OMPC_unified_address:
Kelvin Li1408f912018-09-26 04:28:39 +000011723 C = new (Context) OMPUnifiedAddressClause();
11724 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011725 case llvm::omp::OMPC_unified_shared_memory:
Patrick Lyster4a370b92018-10-01 13:47:43 +000011726 C = new (Context) OMPUnifiedSharedMemoryClause();
11727 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011728 case llvm::omp::OMPC_reverse_offload:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000011729 C = new (Context) OMPReverseOffloadClause();
11730 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011731 case llvm::omp::OMPC_dynamic_allocators:
Patrick Lyster3fe9e392018-10-11 14:41:10 +000011732 C = new (Context) OMPDynamicAllocatorsClause();
11733 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011734 case llvm::omp::OMPC_atomic_default_mem_order:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000011735 C = new (Context) OMPAtomicDefaultMemOrderClause();
11736 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011737 case llvm::omp::OMPC_private:
Kelvin Libe286f52018-09-15 13:54:15 +000011738 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11739 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011740 case llvm::omp::OMPC_firstprivate:
Kelvin Libe286f52018-09-15 13:54:15 +000011741 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11742 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011743 case llvm::omp::OMPC_lastprivate:
Kelvin Libe286f52018-09-15 13:54:15 +000011744 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11745 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011746 case llvm::omp::OMPC_shared:
Kelvin Libe286f52018-09-15 13:54:15 +000011747 C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11748 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011749 case llvm::omp::OMPC_reduction:
Kelvin Libe286f52018-09-15 13:54:15 +000011750 C = OMPReductionClause::CreateEmpty(Context, Record.readInt());
11751 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011752 case llvm::omp::OMPC_task_reduction:
Kelvin Libe286f52018-09-15 13:54:15 +000011753 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11754 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011755 case llvm::omp::OMPC_in_reduction:
Kelvin Libe286f52018-09-15 13:54:15 +000011756 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11757 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011758 case llvm::omp::OMPC_linear:
Kelvin Libe286f52018-09-15 13:54:15 +000011759 C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11760 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011761 case llvm::omp::OMPC_aligned:
Kelvin Libe286f52018-09-15 13:54:15 +000011762 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11763 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011764 case llvm::omp::OMPC_copyin:
Kelvin Libe286f52018-09-15 13:54:15 +000011765 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11766 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011767 case llvm::omp::OMPC_copyprivate:
Kelvin Libe286f52018-09-15 13:54:15 +000011768 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11769 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011770 case llvm::omp::OMPC_flush:
Kelvin Libe286f52018-09-15 13:54:15 +000011771 C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11772 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011773 case llvm::omp::OMPC_depobj:
Alexey Bataevc112e942020-02-28 09:52:15 -050011774 C = OMPDepobjClause::CreateEmpty(Context);
11775 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011776 case llvm::omp::OMPC_depend: {
Kelvin Libe286f52018-09-15 13:54:15 +000011777 unsigned NumVars = Record.readInt();
11778 unsigned NumLoops = Record.readInt();
11779 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11780 break;
11781 }
Johannes Doerfert419a5592020-03-30 19:58:40 -050011782 case llvm::omp::OMPC_device:
Kelvin Libe286f52018-09-15 13:54:15 +000011783 C = new (Context) OMPDeviceClause();
11784 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011785 case llvm::omp::OMPC_map: {
Michael Kruse4304e9d2019-02-19 16:38:20 +000011786 OMPMappableExprListSizeTy Sizes;
11787 Sizes.NumVars = Record.readInt();
11788 Sizes.NumUniqueDeclarations = Record.readInt();
11789 Sizes.NumComponentLists = Record.readInt();
11790 Sizes.NumComponents = Record.readInt();
11791 C = OMPMapClause::CreateEmpty(Context, Sizes);
Kelvin Libe286f52018-09-15 13:54:15 +000011792 break;
11793 }
Johannes Doerfert419a5592020-03-30 19:58:40 -050011794 case llvm::omp::OMPC_num_teams:
Kelvin Libe286f52018-09-15 13:54:15 +000011795 C = new (Context) OMPNumTeamsClause();
11796 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011797 case llvm::omp::OMPC_thread_limit:
Kelvin Libe286f52018-09-15 13:54:15 +000011798 C = new (Context) OMPThreadLimitClause();
11799 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011800 case llvm::omp::OMPC_priority:
Kelvin Libe286f52018-09-15 13:54:15 +000011801 C = new (Context) OMPPriorityClause();
11802 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011803 case llvm::omp::OMPC_grainsize:
Kelvin Libe286f52018-09-15 13:54:15 +000011804 C = new (Context) OMPGrainsizeClause();
11805 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011806 case llvm::omp::OMPC_num_tasks:
Kelvin Libe286f52018-09-15 13:54:15 +000011807 C = new (Context) OMPNumTasksClause();
11808 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011809 case llvm::omp::OMPC_hint:
Kelvin Libe286f52018-09-15 13:54:15 +000011810 C = new (Context) OMPHintClause();
11811 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011812 case llvm::omp::OMPC_dist_schedule:
Kelvin Libe286f52018-09-15 13:54:15 +000011813 C = new (Context) OMPDistScheduleClause();
11814 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011815 case llvm::omp::OMPC_defaultmap:
Kelvin Libe286f52018-09-15 13:54:15 +000011816 C = new (Context) OMPDefaultmapClause();
11817 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011818 case llvm::omp::OMPC_to: {
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 = OMPToClause::CreateEmpty(Context, Sizes);
Kelvin Libe286f52018-09-15 13:54:15 +000011825 break;
11826 }
Johannes Doerfert419a5592020-03-30 19:58:40 -050011827 case llvm::omp::OMPC_from: {
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 = OMPFromClause::CreateEmpty(Context, Sizes);
Kelvin Libe286f52018-09-15 13:54:15 +000011834 break;
11835 }
Johannes Doerfert419a5592020-03-30 19:58:40 -050011836 case llvm::omp::OMPC_use_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 = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
Kelvin Libe286f52018-09-15 13:54:15 +000011843 break;
11844 }
Johannes Doerfert419a5592020-03-30 19:58:40 -050011845 case llvm::omp::OMPC_is_device_ptr: {
Michael Kruse4304e9d2019-02-19 16:38:20 +000011846 OMPMappableExprListSizeTy Sizes;
11847 Sizes.NumVars = Record.readInt();
11848 Sizes.NumUniqueDeclarations = Record.readInt();
11849 Sizes.NumComponentLists = Record.readInt();
11850 Sizes.NumComponents = Record.readInt();
11851 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
Kelvin Libe286f52018-09-15 13:54:15 +000011852 break;
11853 }
Johannes Doerfert419a5592020-03-30 19:58:40 -050011854 case llvm::omp::OMPC_allocate:
Alexey Bataeve04483e2019-03-27 14:14:31 +000011855 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11856 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011857 case llvm::omp::OMPC_nontemporal:
Alexey Bataevb6e70842019-12-16 15:54:17 -050011858 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11859 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011860 case llvm::omp::OMPC_inclusive:
Alexey Bataev06dea732020-03-20 09:41:22 -040011861 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11862 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011863 case llvm::omp::OMPC_exclusive:
Alexey Bataev63828a32020-03-23 10:41:08 -040011864 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11865 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011866 case llvm::omp::OMPC_order:
Alexey Bataevcb8e6912020-01-31 16:09:26 -050011867 C = new (Context) OMPOrderClause();
11868 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011869 case llvm::omp::OMPC_destroy:
Alexey Bataev375437a2020-03-02 14:21:20 -050011870 C = new (Context) OMPDestroyClause();
11871 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011872 case llvm::omp::OMPC_detach:
Alexey Bataev0f0564b2020-03-17 09:17:42 -040011873 C = new (Context) OMPDetachClause();
11874 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011875#define OMP_CLAUSE_NO_CLASS(Enum, Str) \
11876 case llvm::omp::Enum: \
11877 break;
11878#include "llvm/Frontend/OpenMP/OMPKinds.def"
Kelvin Libe286f52018-09-15 13:54:15 +000011879 }
Simon Pilgrim556fbfe2019-09-15 16:05:20 +000011880 assert(C && "Unknown OMPClause type");
11881
Kelvin Libe286f52018-09-15 13:54:15 +000011882 Visit(C);
11883 C->setLocStart(Record.readSourceLocation());
11884 C->setLocEnd(Record.readSourceLocation());
11885
11886 return C;
11887}
11888
11889void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11890 C->setPreInitStmt(Record.readSubStmt(),
11891 static_cast<OpenMPDirectiveKind>(Record.readInt()));
11892}
11893
11894void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11895 VisitOMPClauseWithPreInit(C);
11896 C->setPostUpdateExpr(Record.readSubExpr());
11897}
11898
11899void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11900 VisitOMPClauseWithPreInit(C);
11901 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11902 C->setNameModifierLoc(Record.readSourceLocation());
11903 C->setColonLoc(Record.readSourceLocation());
11904 C->setCondition(Record.readSubExpr());
11905 C->setLParenLoc(Record.readSourceLocation());
11906}
11907
11908void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
Alexey Bataev3a842ec2019-10-15 19:37:05 +000011909 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +000011910 C->setCondition(Record.readSubExpr());
11911 C->setLParenLoc(Record.readSourceLocation());
11912}
11913
11914void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11915 VisitOMPClauseWithPreInit(C);
11916 C->setNumThreads(Record.readSubExpr());
11917 C->setLParenLoc(Record.readSourceLocation());
11918}
11919
11920void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11921 C->setSafelen(Record.readSubExpr());
11922 C->setLParenLoc(Record.readSourceLocation());
11923}
11924
11925void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11926 C->setSimdlen(Record.readSubExpr());
11927 C->setLParenLoc(Record.readSourceLocation());
11928}
11929
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000011930void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
11931 C->setAllocator(Record.readExpr());
11932 C->setLParenLoc(Record.readSourceLocation());
11933}
11934
Kelvin Libe286f52018-09-15 13:54:15 +000011935void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11936 C->setNumForLoops(Record.readSubExpr());
11937 C->setLParenLoc(Record.readSourceLocation());
11938}
11939
11940void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
Atmn Patel577c9b02020-02-14 21:45:49 -060011941 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
Kelvin Libe286f52018-09-15 13:54:15 +000011942 C->setLParenLoc(Record.readSourceLocation());
11943 C->setDefaultKindKwLoc(Record.readSourceLocation());
11944}
11945
11946void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -060011947 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
Kelvin Libe286f52018-09-15 13:54:15 +000011948 C->setLParenLoc(Record.readSourceLocation());
11949 C->setProcBindKindKwLoc(Record.readSourceLocation());
11950}
11951
11952void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
11953 VisitOMPClauseWithPreInit(C);
11954 C->setScheduleKind(
11955 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
11956 C->setFirstScheduleModifier(
11957 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11958 C->setSecondScheduleModifier(
11959 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11960 C->setChunkSize(Record.readSubExpr());
11961 C->setLParenLoc(Record.readSourceLocation());
11962 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
11963 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
11964 C->setScheduleKindLoc(Record.readSourceLocation());
11965 C->setCommaLoc(Record.readSourceLocation());
11966}
11967
11968void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
11969 C->setNumForLoops(Record.readSubExpr());
11970 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11971 C->setLoopNumIterations(I, Record.readSubExpr());
11972 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11973 C->setLoopCounter(I, Record.readSubExpr());
11974 C->setLParenLoc(Record.readSourceLocation());
11975}
11976
Alexey Bataev0f0564b2020-03-17 09:17:42 -040011977void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
11978 C->setEventHandler(Record.readSubExpr());
11979 C->setLParenLoc(Record.readSourceLocation());
11980}
11981
Kelvin Libe286f52018-09-15 13:54:15 +000011982void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
11983
11984void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
11985
11986void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
11987
11988void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
11989
11990void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
11991
Alexey Bataev82f7c202020-03-03 13:22:35 -050011992void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
11993 if (C->isExtended()) {
11994 C->setLParenLoc(Record.readSourceLocation());
11995 C->setArgumentLoc(Record.readSourceLocation());
11996 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
11997 }
11998}
Kelvin Libe286f52018-09-15 13:54:15 +000011999
12000void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12001
12002void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12003
Alexey Bataevea9166b2020-02-06 16:30:23 -050012004void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12005
Alexey Bataev04a830f2020-02-10 14:30:39 -050012006void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12007
Alexey Bataev95598342020-02-10 15:49:05 -050012008void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12009
Alexey Bataev9a8defc2020-02-11 11:10:43 -050012010void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12011
Kelvin Libe286f52018-09-15 13:54:15 +000012012void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12013
12014void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12015
12016void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12017
Alexey Bataev375437a2020-03-02 14:21:20 -050012018void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {}
12019
Kelvin Li1408f912018-09-26 04:28:39 +000012020void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12021
Patrick Lyster4a370b92018-10-01 13:47:43 +000012022void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12023 OMPUnifiedSharedMemoryClause *) {}
12024
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000012025void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12026
Patrick Lyster3fe9e392018-10-11 14:41:10 +000012027void
12028OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12029}
12030
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000012031void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12032 OMPAtomicDefaultMemOrderClause *C) {
12033 C->setAtomicDefaultMemOrderKind(
12034 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12035 C->setLParenLoc(Record.readSourceLocation());
12036 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12037}
12038
Kelvin Libe286f52018-09-15 13:54:15 +000012039void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12040 C->setLParenLoc(Record.readSourceLocation());
12041 unsigned NumVars = C->varlist_size();
12042 SmallVector<Expr *, 16> Vars;
12043 Vars.reserve(NumVars);
12044 for (unsigned i = 0; i != NumVars; ++i)
12045 Vars.push_back(Record.readSubExpr());
12046 C->setVarRefs(Vars);
12047 Vars.clear();
12048 for (unsigned i = 0; i != NumVars; ++i)
12049 Vars.push_back(Record.readSubExpr());
12050 C->setPrivateCopies(Vars);
12051}
12052
12053void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12054 VisitOMPClauseWithPreInit(C);
12055 C->setLParenLoc(Record.readSourceLocation());
12056 unsigned NumVars = C->varlist_size();
12057 SmallVector<Expr *, 16> Vars;
12058 Vars.reserve(NumVars);
12059 for (unsigned i = 0; i != NumVars; ++i)
12060 Vars.push_back(Record.readSubExpr());
12061 C->setVarRefs(Vars);
12062 Vars.clear();
12063 for (unsigned i = 0; i != NumVars; ++i)
12064 Vars.push_back(Record.readSubExpr());
12065 C->setPrivateCopies(Vars);
12066 Vars.clear();
12067 for (unsigned i = 0; i != NumVars; ++i)
12068 Vars.push_back(Record.readSubExpr());
12069 C->setInits(Vars);
12070}
12071
12072void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12073 VisitOMPClauseWithPostUpdate(C);
12074 C->setLParenLoc(Record.readSourceLocation());
Alexey Bataev93dc40d2019-12-20 11:04:57 -050012075 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12076 C->setKindLoc(Record.readSourceLocation());
12077 C->setColonLoc(Record.readSourceLocation());
Kelvin Libe286f52018-09-15 13:54:15 +000012078 unsigned NumVars = C->varlist_size();
12079 SmallVector<Expr *, 16> Vars;
12080 Vars.reserve(NumVars);
12081 for (unsigned i = 0; i != NumVars; ++i)
12082 Vars.push_back(Record.readSubExpr());
12083 C->setVarRefs(Vars);
12084 Vars.clear();
12085 for (unsigned i = 0; i != NumVars; ++i)
12086 Vars.push_back(Record.readSubExpr());
12087 C->setPrivateCopies(Vars);
12088 Vars.clear();
12089 for (unsigned i = 0; i != NumVars; ++i)
12090 Vars.push_back(Record.readSubExpr());
12091 C->setSourceExprs(Vars);
12092 Vars.clear();
12093 for (unsigned i = 0; i != NumVars; ++i)
12094 Vars.push_back(Record.readSubExpr());
12095 C->setDestinationExprs(Vars);
12096 Vars.clear();
12097 for (unsigned i = 0; i != NumVars; ++i)
12098 Vars.push_back(Record.readSubExpr());
12099 C->setAssignmentOps(Vars);
12100}
12101
12102void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12103 C->setLParenLoc(Record.readSourceLocation());
12104 unsigned NumVars = C->varlist_size();
12105 SmallVector<Expr *, 16> Vars;
12106 Vars.reserve(NumVars);
12107 for (unsigned i = 0; i != NumVars; ++i)
12108 Vars.push_back(Record.readSubExpr());
12109 C->setVarRefs(Vars);
12110}
12111
12112void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12113 VisitOMPClauseWithPostUpdate(C);
12114 C->setLParenLoc(Record.readSourceLocation());
Alexey Bataev1236eb62020-03-23 17:30:38 -040012115 C->setModifierLoc(Record.readSourceLocation());
Kelvin Libe286f52018-09-15 13:54:15 +000012116 C->setColonLoc(Record.readSourceLocation());
Alexey Bataev1236eb62020-03-23 17:30:38 -040012117 C->setModifier(Record.readEnum<OpenMPReductionClauseModifier>());
Kelvin Libe286f52018-09-15 13:54:15 +000012118 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
John McCall3ce3d232019-12-13 03:37:23 -050012119 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
Kelvin Libe286f52018-09-15 13:54:15 +000012120 C->setQualifierLoc(NNSL);
12121 C->setNameInfo(DNI);
12122
12123 unsigned NumVars = C->varlist_size();
12124 SmallVector<Expr *, 16> Vars;
12125 Vars.reserve(NumVars);
12126 for (unsigned i = 0; i != NumVars; ++i)
12127 Vars.push_back(Record.readSubExpr());
12128 C->setVarRefs(Vars);
12129 Vars.clear();
12130 for (unsigned i = 0; i != NumVars; ++i)
12131 Vars.push_back(Record.readSubExpr());
12132 C->setPrivates(Vars);
12133 Vars.clear();
12134 for (unsigned i = 0; i != NumVars; ++i)
12135 Vars.push_back(Record.readSubExpr());
12136 C->setLHSExprs(Vars);
12137 Vars.clear();
12138 for (unsigned i = 0; i != NumVars; ++i)
12139 Vars.push_back(Record.readSubExpr());
12140 C->setRHSExprs(Vars);
12141 Vars.clear();
12142 for (unsigned i = 0; i != NumVars; ++i)
12143 Vars.push_back(Record.readSubExpr());
12144 C->setReductionOps(Vars);
12145}
12146
12147void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12148 VisitOMPClauseWithPostUpdate(C);
12149 C->setLParenLoc(Record.readSourceLocation());
12150 C->setColonLoc(Record.readSourceLocation());
12151 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
John McCall3ce3d232019-12-13 03:37:23 -050012152 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
Kelvin Libe286f52018-09-15 13:54:15 +000012153 C->setQualifierLoc(NNSL);
12154 C->setNameInfo(DNI);
12155
12156 unsigned NumVars = C->varlist_size();
12157 SmallVector<Expr *, 16> Vars;
12158 Vars.reserve(NumVars);
12159 for (unsigned I = 0; I != NumVars; ++I)
12160 Vars.push_back(Record.readSubExpr());
12161 C->setVarRefs(Vars);
12162 Vars.clear();
12163 for (unsigned I = 0; I != NumVars; ++I)
12164 Vars.push_back(Record.readSubExpr());
12165 C->setPrivates(Vars);
12166 Vars.clear();
12167 for (unsigned I = 0; I != NumVars; ++I)
12168 Vars.push_back(Record.readSubExpr());
12169 C->setLHSExprs(Vars);
12170 Vars.clear();
12171 for (unsigned I = 0; I != NumVars; ++I)
12172 Vars.push_back(Record.readSubExpr());
12173 C->setRHSExprs(Vars);
12174 Vars.clear();
12175 for (unsigned I = 0; I != NumVars; ++I)
12176 Vars.push_back(Record.readSubExpr());
12177 C->setReductionOps(Vars);
12178}
12179
12180void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12181 VisitOMPClauseWithPostUpdate(C);
12182 C->setLParenLoc(Record.readSourceLocation());
12183 C->setColonLoc(Record.readSourceLocation());
12184 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
John McCall3ce3d232019-12-13 03:37:23 -050012185 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
Kelvin Libe286f52018-09-15 13:54:15 +000012186 C->setQualifierLoc(NNSL);
12187 C->setNameInfo(DNI);
12188
12189 unsigned NumVars = C->varlist_size();
12190 SmallVector<Expr *, 16> Vars;
12191 Vars.reserve(NumVars);
12192 for (unsigned I = 0; I != NumVars; ++I)
12193 Vars.push_back(Record.readSubExpr());
12194 C->setVarRefs(Vars);
12195 Vars.clear();
12196 for (unsigned I = 0; I != NumVars; ++I)
12197 Vars.push_back(Record.readSubExpr());
12198 C->setPrivates(Vars);
12199 Vars.clear();
12200 for (unsigned I = 0; I != NumVars; ++I)
12201 Vars.push_back(Record.readSubExpr());
12202 C->setLHSExprs(Vars);
12203 Vars.clear();
12204 for (unsigned I = 0; I != NumVars; ++I)
12205 Vars.push_back(Record.readSubExpr());
12206 C->setRHSExprs(Vars);
12207 Vars.clear();
12208 for (unsigned I = 0; I != NumVars; ++I)
12209 Vars.push_back(Record.readSubExpr());
12210 C->setReductionOps(Vars);
12211 Vars.clear();
12212 for (unsigned I = 0; I != NumVars; ++I)
12213 Vars.push_back(Record.readSubExpr());
12214 C->setTaskgroupDescriptors(Vars);
12215}
12216
12217void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12218 VisitOMPClauseWithPostUpdate(C);
12219 C->setLParenLoc(Record.readSourceLocation());
12220 C->setColonLoc(Record.readSourceLocation());
12221 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12222 C->setModifierLoc(Record.readSourceLocation());
12223 unsigned NumVars = C->varlist_size();
12224 SmallVector<Expr *, 16> Vars;
12225 Vars.reserve(NumVars);
12226 for (unsigned i = 0; i != NumVars; ++i)
12227 Vars.push_back(Record.readSubExpr());
12228 C->setVarRefs(Vars);
12229 Vars.clear();
12230 for (unsigned i = 0; i != NumVars; ++i)
12231 Vars.push_back(Record.readSubExpr());
12232 C->setPrivates(Vars);
12233 Vars.clear();
12234 for (unsigned i = 0; i != NumVars; ++i)
12235 Vars.push_back(Record.readSubExpr());
12236 C->setInits(Vars);
12237 Vars.clear();
12238 for (unsigned i = 0; i != NumVars; ++i)
12239 Vars.push_back(Record.readSubExpr());
12240 C->setUpdates(Vars);
12241 Vars.clear();
12242 for (unsigned i = 0; i != NumVars; ++i)
12243 Vars.push_back(Record.readSubExpr());
12244 C->setFinals(Vars);
12245 C->setStep(Record.readSubExpr());
12246 C->setCalcStep(Record.readSubExpr());
Alexey Bataev195ae902019-08-08 13:42:45 +000012247 Vars.clear();
12248 for (unsigned I = 0; I != NumVars + 1; ++I)
12249 Vars.push_back(Record.readSubExpr());
12250 C->setUsedExprs(Vars);
Kelvin Libe286f52018-09-15 13:54:15 +000012251}
12252
12253void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12254 C->setLParenLoc(Record.readSourceLocation());
12255 C->setColonLoc(Record.readSourceLocation());
12256 unsigned NumVars = C->varlist_size();
12257 SmallVector<Expr *, 16> Vars;
12258 Vars.reserve(NumVars);
12259 for (unsigned i = 0; i != NumVars; ++i)
12260 Vars.push_back(Record.readSubExpr());
12261 C->setVarRefs(Vars);
12262 C->setAlignment(Record.readSubExpr());
12263}
12264
12265void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12266 C->setLParenLoc(Record.readSourceLocation());
12267 unsigned NumVars = C->varlist_size();
12268 SmallVector<Expr *, 16> Exprs;
12269 Exprs.reserve(NumVars);
12270 for (unsigned i = 0; i != NumVars; ++i)
12271 Exprs.push_back(Record.readSubExpr());
12272 C->setVarRefs(Exprs);
12273 Exprs.clear();
12274 for (unsigned i = 0; i != NumVars; ++i)
12275 Exprs.push_back(Record.readSubExpr());
12276 C->setSourceExprs(Exprs);
12277 Exprs.clear();
12278 for (unsigned i = 0; i != NumVars; ++i)
12279 Exprs.push_back(Record.readSubExpr());
12280 C->setDestinationExprs(Exprs);
12281 Exprs.clear();
12282 for (unsigned i = 0; i != NumVars; ++i)
12283 Exprs.push_back(Record.readSubExpr());
12284 C->setAssignmentOps(Exprs);
12285}
12286
12287void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12288 C->setLParenLoc(Record.readSourceLocation());
12289 unsigned NumVars = C->varlist_size();
12290 SmallVector<Expr *, 16> Exprs;
12291 Exprs.reserve(NumVars);
12292 for (unsigned i = 0; i != NumVars; ++i)
12293 Exprs.push_back(Record.readSubExpr());
12294 C->setVarRefs(Exprs);
12295 Exprs.clear();
12296 for (unsigned i = 0; i != NumVars; ++i)
12297 Exprs.push_back(Record.readSubExpr());
12298 C->setSourceExprs(Exprs);
12299 Exprs.clear();
12300 for (unsigned i = 0; i != NumVars; ++i)
12301 Exprs.push_back(Record.readSubExpr());
12302 C->setDestinationExprs(Exprs);
12303 Exprs.clear();
12304 for (unsigned i = 0; i != NumVars; ++i)
12305 Exprs.push_back(Record.readSubExpr());
12306 C->setAssignmentOps(Exprs);
12307}
12308
12309void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12310 C->setLParenLoc(Record.readSourceLocation());
12311 unsigned NumVars = C->varlist_size();
12312 SmallVector<Expr *, 16> Vars;
12313 Vars.reserve(NumVars);
12314 for (unsigned i = 0; i != NumVars; ++i)
12315 Vars.push_back(Record.readSubExpr());
12316 C->setVarRefs(Vars);
12317}
12318
Alexey Bataevc112e942020-02-28 09:52:15 -050012319void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12320 C->setDepobj(Record.readSubExpr());
12321 C->setLParenLoc(Record.readSourceLocation());
12322}
12323
Kelvin Libe286f52018-09-15 13:54:15 +000012324void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12325 C->setLParenLoc(Record.readSourceLocation());
Alexey Bataev13a15042020-04-01 15:06:38 -040012326 C->setModifier(Record.readSubExpr());
Kelvin Libe286f52018-09-15 13:54:15 +000012327 C->setDependencyKind(
12328 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12329 C->setDependencyLoc(Record.readSourceLocation());
12330 C->setColonLoc(Record.readSourceLocation());
12331 unsigned NumVars = C->varlist_size();
12332 SmallVector<Expr *, 16> Vars;
12333 Vars.reserve(NumVars);
12334 for (unsigned I = 0; I != NumVars; ++I)
12335 Vars.push_back(Record.readSubExpr());
12336 C->setVarRefs(Vars);
12337 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12338 C->setLoopData(I, Record.readSubExpr());
12339}
12340
12341void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12342 VisitOMPClauseWithPreInit(C);
Alexey Bataev2f8894a2020-03-18 15:01:15 -040012343 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
Kelvin Libe286f52018-09-15 13:54:15 +000012344 C->setDevice(Record.readSubExpr());
Alexey Bataev2f8894a2020-03-18 15:01:15 -040012345 C->setModifierLoc(Record.readSourceLocation());
Kelvin Libe286f52018-09-15 13:54:15 +000012346 C->setLParenLoc(Record.readSourceLocation());
12347}
12348
12349void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12350 C->setLParenLoc(Record.readSourceLocation());
Reid Klecknerba1ffd22020-04-03 12:35:30 -070012351 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
Kelvin Lief579432018-12-18 22:18:41 +000012352 C->setMapTypeModifier(
12353 I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12354 C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12355 }
Michael Kruse4304e9d2019-02-19 16:38:20 +000012356 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
John McCall3ce3d232019-12-13 03:37:23 -050012357 C->setMapperIdInfo(Record.readDeclarationNameInfo());
Kelvin Libe286f52018-09-15 13:54:15 +000012358 C->setMapType(
12359 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12360 C->setMapLoc(Record.readSourceLocation());
12361 C->setColonLoc(Record.readSourceLocation());
12362 auto NumVars = C->varlist_size();
12363 auto UniqueDecls = C->getUniqueDeclarationsNum();
12364 auto TotalLists = C->getTotalComponentListNum();
12365 auto TotalComponents = C->getTotalComponentsNum();
12366
12367 SmallVector<Expr *, 16> Vars;
12368 Vars.reserve(NumVars);
12369 for (unsigned i = 0; i != NumVars; ++i)
Michael Kruse251e1482019-02-01 20:25:04 +000012370 Vars.push_back(Record.readExpr());
Kelvin Libe286f52018-09-15 13:54:15 +000012371 C->setVarRefs(Vars);
12372
Michael Kruse4304e9d2019-02-19 16:38:20 +000012373 SmallVector<Expr *, 16> UDMappers;
12374 UDMappers.reserve(NumVars);
12375 for (unsigned I = 0; I < NumVars; ++I)
12376 UDMappers.push_back(Record.readExpr());
12377 C->setUDMapperRefs(UDMappers);
12378
Kelvin Libe286f52018-09-15 13:54:15 +000012379 SmallVector<ValueDecl *, 16> Decls;
12380 Decls.reserve(UniqueDecls);
12381 for (unsigned i = 0; i < UniqueDecls; ++i)
12382 Decls.push_back(Record.readDeclAs<ValueDecl>());
12383 C->setUniqueDecls(Decls);
12384
12385 SmallVector<unsigned, 16> ListsPerDecl;
12386 ListsPerDecl.reserve(UniqueDecls);
12387 for (unsigned i = 0; i < UniqueDecls; ++i)
12388 ListsPerDecl.push_back(Record.readInt());
12389 C->setDeclNumLists(ListsPerDecl);
12390
12391 SmallVector<unsigned, 32> ListSizes;
12392 ListSizes.reserve(TotalLists);
12393 for (unsigned i = 0; i < TotalLists; ++i)
12394 ListSizes.push_back(Record.readInt());
12395 C->setComponentListSizes(ListSizes);
12396
12397 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12398 Components.reserve(TotalComponents);
12399 for (unsigned i = 0; i < TotalComponents; ++i) {
Michael Kruse251e1482019-02-01 20:25:04 +000012400 Expr *AssociatedExpr = Record.readExpr();
Kelvin Libe286f52018-09-15 13:54:15 +000012401 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12402 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12403 AssociatedExpr, AssociatedDecl));
12404 }
12405 C->setComponents(Components, ListSizes);
12406}
12407
Alexey Bataeve04483e2019-03-27 14:14:31 +000012408void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12409 C->setLParenLoc(Record.readSourceLocation());
12410 C->setColonLoc(Record.readSourceLocation());
12411 C->setAllocator(Record.readSubExpr());
12412 unsigned NumVars = C->varlist_size();
12413 SmallVector<Expr *, 16> Vars;
12414 Vars.reserve(NumVars);
12415 for (unsigned i = 0; i != NumVars; ++i)
12416 Vars.push_back(Record.readSubExpr());
12417 C->setVarRefs(Vars);
12418}
12419
Kelvin Libe286f52018-09-15 13:54:15 +000012420void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12421 VisitOMPClauseWithPreInit(C);
12422 C->setNumTeams(Record.readSubExpr());
12423 C->setLParenLoc(Record.readSourceLocation());
12424}
12425
12426void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12427 VisitOMPClauseWithPreInit(C);
12428 C->setThreadLimit(Record.readSubExpr());
12429 C->setLParenLoc(Record.readSourceLocation());
12430}
12431
12432void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
Alexey Bataev31ba4762019-10-16 18:09:37 +000012433 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +000012434 C->setPriority(Record.readSubExpr());
12435 C->setLParenLoc(Record.readSourceLocation());
12436}
12437
12438void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
Alexey Bataevb9c55e22019-10-14 19:29:52 +000012439 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +000012440 C->setGrainsize(Record.readSubExpr());
12441 C->setLParenLoc(Record.readSourceLocation());
12442}
12443
12444void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
Alexey Bataevd88c7de2019-10-14 20:44:34 +000012445 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +000012446 C->setNumTasks(Record.readSubExpr());
12447 C->setLParenLoc(Record.readSourceLocation());
12448}
12449
12450void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12451 C->setHint(Record.readSubExpr());
12452 C->setLParenLoc(Record.readSourceLocation());
12453}
12454
12455void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12456 VisitOMPClauseWithPreInit(C);
12457 C->setDistScheduleKind(
12458 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12459 C->setChunkSize(Record.readSubExpr());
12460 C->setLParenLoc(Record.readSourceLocation());
12461 C->setDistScheduleKindLoc(Record.readSourceLocation());
12462 C->setCommaLoc(Record.readSourceLocation());
12463}
12464
12465void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12466 C->setDefaultmapKind(
12467 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12468 C->setDefaultmapModifier(
12469 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12470 C->setLParenLoc(Record.readSourceLocation());
12471 C->setDefaultmapModifierLoc(Record.readSourceLocation());
12472 C->setDefaultmapKindLoc(Record.readSourceLocation());
12473}
12474
12475void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12476 C->setLParenLoc(Record.readSourceLocation());
Michael Kruse01f670d2019-02-22 22:29:42 +000012477 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
John McCall3ce3d232019-12-13 03:37:23 -050012478 C->setMapperIdInfo(Record.readDeclarationNameInfo());
Kelvin Libe286f52018-09-15 13:54:15 +000012479 auto NumVars = C->varlist_size();
12480 auto UniqueDecls = C->getUniqueDeclarationsNum();
12481 auto TotalLists = C->getTotalComponentListNum();
12482 auto TotalComponents = C->getTotalComponentsNum();
12483
12484 SmallVector<Expr *, 16> Vars;
12485 Vars.reserve(NumVars);
12486 for (unsigned i = 0; i != NumVars; ++i)
12487 Vars.push_back(Record.readSubExpr());
12488 C->setVarRefs(Vars);
12489
Michael Kruse01f670d2019-02-22 22:29:42 +000012490 SmallVector<Expr *, 16> UDMappers;
12491 UDMappers.reserve(NumVars);
12492 for (unsigned I = 0; I < NumVars; ++I)
12493 UDMappers.push_back(Record.readSubExpr());
12494 C->setUDMapperRefs(UDMappers);
12495
Kelvin Libe286f52018-09-15 13:54:15 +000012496 SmallVector<ValueDecl *, 16> Decls;
12497 Decls.reserve(UniqueDecls);
12498 for (unsigned i = 0; i < UniqueDecls; ++i)
12499 Decls.push_back(Record.readDeclAs<ValueDecl>());
12500 C->setUniqueDecls(Decls);
12501
12502 SmallVector<unsigned, 16> ListsPerDecl;
12503 ListsPerDecl.reserve(UniqueDecls);
12504 for (unsigned i = 0; i < UniqueDecls; ++i)
12505 ListsPerDecl.push_back(Record.readInt());
12506 C->setDeclNumLists(ListsPerDecl);
12507
12508 SmallVector<unsigned, 32> ListSizes;
12509 ListSizes.reserve(TotalLists);
12510 for (unsigned i = 0; i < TotalLists; ++i)
12511 ListSizes.push_back(Record.readInt());
12512 C->setComponentListSizes(ListSizes);
12513
12514 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12515 Components.reserve(TotalComponents);
12516 for (unsigned i = 0; i < TotalComponents; ++i) {
12517 Expr *AssociatedExpr = Record.readSubExpr();
12518 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12519 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12520 AssociatedExpr, AssociatedDecl));
12521 }
12522 C->setComponents(Components, ListSizes);
12523}
12524
12525void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12526 C->setLParenLoc(Record.readSourceLocation());
Michael Kruse0336c752019-02-25 20:34:15 +000012527 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
John McCall3ce3d232019-12-13 03:37:23 -050012528 C->setMapperIdInfo(Record.readDeclarationNameInfo());
Kelvin Libe286f52018-09-15 13:54:15 +000012529 auto NumVars = C->varlist_size();
12530 auto UniqueDecls = C->getUniqueDeclarationsNum();
12531 auto TotalLists = C->getTotalComponentListNum();
12532 auto TotalComponents = C->getTotalComponentsNum();
12533
12534 SmallVector<Expr *, 16> Vars;
12535 Vars.reserve(NumVars);
12536 for (unsigned i = 0; i != NumVars; ++i)
12537 Vars.push_back(Record.readSubExpr());
12538 C->setVarRefs(Vars);
12539
Michael Kruse0336c752019-02-25 20:34:15 +000012540 SmallVector<Expr *, 16> UDMappers;
12541 UDMappers.reserve(NumVars);
12542 for (unsigned I = 0; I < NumVars; ++I)
12543 UDMappers.push_back(Record.readSubExpr());
12544 C->setUDMapperRefs(UDMappers);
12545
Kelvin Libe286f52018-09-15 13:54:15 +000012546 SmallVector<ValueDecl *, 16> Decls;
12547 Decls.reserve(UniqueDecls);
12548 for (unsigned i = 0; i < UniqueDecls; ++i)
12549 Decls.push_back(Record.readDeclAs<ValueDecl>());
12550 C->setUniqueDecls(Decls);
12551
12552 SmallVector<unsigned, 16> ListsPerDecl;
12553 ListsPerDecl.reserve(UniqueDecls);
12554 for (unsigned i = 0; i < UniqueDecls; ++i)
12555 ListsPerDecl.push_back(Record.readInt());
12556 C->setDeclNumLists(ListsPerDecl);
12557
12558 SmallVector<unsigned, 32> ListSizes;
12559 ListSizes.reserve(TotalLists);
12560 for (unsigned i = 0; i < TotalLists; ++i)
12561 ListSizes.push_back(Record.readInt());
12562 C->setComponentListSizes(ListSizes);
12563
12564 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12565 Components.reserve(TotalComponents);
12566 for (unsigned i = 0; i < TotalComponents; ++i) {
12567 Expr *AssociatedExpr = Record.readSubExpr();
12568 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12569 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12570 AssociatedExpr, AssociatedDecl));
12571 }
12572 C->setComponents(Components, ListSizes);
12573}
12574
12575void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12576 C->setLParenLoc(Record.readSourceLocation());
12577 auto NumVars = C->varlist_size();
12578 auto UniqueDecls = C->getUniqueDeclarationsNum();
12579 auto TotalLists = C->getTotalComponentListNum();
12580 auto TotalComponents = C->getTotalComponentsNum();
12581
12582 SmallVector<Expr *, 16> Vars;
12583 Vars.reserve(NumVars);
12584 for (unsigned i = 0; i != NumVars; ++i)
12585 Vars.push_back(Record.readSubExpr());
12586 C->setVarRefs(Vars);
12587 Vars.clear();
12588 for (unsigned i = 0; i != NumVars; ++i)
12589 Vars.push_back(Record.readSubExpr());
12590 C->setPrivateCopies(Vars);
12591 Vars.clear();
12592 for (unsigned i = 0; i != NumVars; ++i)
12593 Vars.push_back(Record.readSubExpr());
12594 C->setInits(Vars);
12595
12596 SmallVector<ValueDecl *, 16> Decls;
12597 Decls.reserve(UniqueDecls);
12598 for (unsigned i = 0; i < UniqueDecls; ++i)
12599 Decls.push_back(Record.readDeclAs<ValueDecl>());
12600 C->setUniqueDecls(Decls);
12601
12602 SmallVector<unsigned, 16> ListsPerDecl;
12603 ListsPerDecl.reserve(UniqueDecls);
12604 for (unsigned i = 0; i < UniqueDecls; ++i)
12605 ListsPerDecl.push_back(Record.readInt());
12606 C->setDeclNumLists(ListsPerDecl);
12607
12608 SmallVector<unsigned, 32> ListSizes;
12609 ListSizes.reserve(TotalLists);
12610 for (unsigned i = 0; i < TotalLists; ++i)
12611 ListSizes.push_back(Record.readInt());
12612 C->setComponentListSizes(ListSizes);
12613
12614 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12615 Components.reserve(TotalComponents);
12616 for (unsigned i = 0; i < TotalComponents; ++i) {
12617 Expr *AssociatedExpr = Record.readSubExpr();
12618 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12619 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12620 AssociatedExpr, AssociatedDecl));
12621 }
12622 C->setComponents(Components, ListSizes);
12623}
12624
12625void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12626 C->setLParenLoc(Record.readSourceLocation());
12627 auto NumVars = C->varlist_size();
12628 auto UniqueDecls = C->getUniqueDeclarationsNum();
12629 auto TotalLists = C->getTotalComponentListNum();
12630 auto TotalComponents = C->getTotalComponentsNum();
12631
12632 SmallVector<Expr *, 16> Vars;
12633 Vars.reserve(NumVars);
12634 for (unsigned i = 0; i != NumVars; ++i)
12635 Vars.push_back(Record.readSubExpr());
12636 C->setVarRefs(Vars);
12637 Vars.clear();
12638
12639 SmallVector<ValueDecl *, 16> Decls;
12640 Decls.reserve(UniqueDecls);
12641 for (unsigned i = 0; i < UniqueDecls; ++i)
12642 Decls.push_back(Record.readDeclAs<ValueDecl>());
12643 C->setUniqueDecls(Decls);
12644
12645 SmallVector<unsigned, 16> ListsPerDecl;
12646 ListsPerDecl.reserve(UniqueDecls);
12647 for (unsigned i = 0; i < UniqueDecls; ++i)
12648 ListsPerDecl.push_back(Record.readInt());
12649 C->setDeclNumLists(ListsPerDecl);
12650
12651 SmallVector<unsigned, 32> ListSizes;
12652 ListSizes.reserve(TotalLists);
12653 for (unsigned i = 0; i < TotalLists; ++i)
12654 ListSizes.push_back(Record.readInt());
12655 C->setComponentListSizes(ListSizes);
12656
12657 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12658 Components.reserve(TotalComponents);
12659 for (unsigned i = 0; i < TotalComponents; ++i) {
12660 Expr *AssociatedExpr = Record.readSubExpr();
12661 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12662 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12663 AssociatedExpr, AssociatedDecl));
12664 }
12665 C->setComponents(Components, ListSizes);
12666}
Alexey Bataevb6e70842019-12-16 15:54:17 -050012667
12668void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12669 C->setLParenLoc(Record.readSourceLocation());
12670 unsigned NumVars = C->varlist_size();
12671 SmallVector<Expr *, 16> Vars;
12672 Vars.reserve(NumVars);
12673 for (unsigned i = 0; i != NumVars; ++i)
12674 Vars.push_back(Record.readSubExpr());
12675 C->setVarRefs(Vars);
Alexey Bataev0860db92019-12-19 10:01:10 -050012676 Vars.clear();
12677 Vars.reserve(NumVars);
12678 for (unsigned i = 0; i != NumVars; ++i)
12679 Vars.push_back(Record.readSubExpr());
12680 C->setPrivateRefs(Vars);
Alexey Bataevb6e70842019-12-16 15:54:17 -050012681}
Alexey Bataevcb8e6912020-01-31 16:09:26 -050012682
Alexey Bataev06dea732020-03-20 09:41:22 -040012683void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12684 C->setLParenLoc(Record.readSourceLocation());
12685 unsigned NumVars = C->varlist_size();
12686 SmallVector<Expr *, 16> Vars;
12687 Vars.reserve(NumVars);
12688 for (unsigned i = 0; i != NumVars; ++i)
12689 Vars.push_back(Record.readSubExpr());
12690 C->setVarRefs(Vars);
12691}
12692
Alexey Bataev63828a32020-03-23 10:41:08 -040012693void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12694 C->setLParenLoc(Record.readSourceLocation());
12695 unsigned NumVars = C->varlist_size();
12696 SmallVector<Expr *, 16> Vars;
12697 Vars.reserve(NumVars);
12698 for (unsigned i = 0; i != NumVars; ++i)
12699 Vars.push_back(Record.readSubExpr());
12700 C->setVarRefs(Vars);
12701}
12702
Alexey Bataevcb8e6912020-01-31 16:09:26 -050012703void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12704 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12705 C->setLParenLoc(Record.readSourceLocation());
12706 C->setKindKwLoc(Record.readSourceLocation());
12707}
Johannes Doerfert1228d422019-12-19 20:42:12 -060012708
Johannes Doerfert55eca282020-03-13 23:42:05 -050012709OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12710 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
Johannes Doerfertb86bf832020-02-15 18:07:42 -060012711 TI.Sets.resize(readUInt32());
12712 for (auto &Set : TI.Sets) {
Johannes Doerfert1228d422019-12-19 20:42:12 -060012713 Set.Kind = readEnum<llvm::omp::TraitSet>();
12714 Set.Selectors.resize(readUInt32());
12715 for (auto &Selector : Set.Selectors) {
12716 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12717 Selector.ScoreOrCondition = nullptr;
12718 if (readBool())
12719 Selector.ScoreOrCondition = readExprRef();
12720 Selector.Properties.resize(readUInt32());
12721 for (auto &Property : Selector.Properties)
12722 Property.Kind = readEnum<llvm::omp::TraitProperty>();
12723 }
12724 }
Johannes Doerfert55eca282020-03-13 23:42:05 -050012725 return &TI;
Johannes Doerfert1228d422019-12-19 20:42:12 -060012726}