blob: 4dd2054d4b0798143b2ada7045652ef01180c3ba [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 }
Daniel Grumbergbb8c7e72020-05-13 17:07:47 +01001339 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
Guy Benyei11169dd2012-12-18 14:30:41 +00001340
1341 RecordData Record;
1342 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00001343 Expected<llvm::BitstreamEntry> MaybeE =
1344 SLocEntryCursor.advanceSkippingSubblocks();
1345 if (!MaybeE) {
1346 Error(MaybeE.takeError());
1347 return true;
1348 }
1349 llvm::BitstreamEntry E = MaybeE.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001350
Chris Lattnere7b154b2013-01-19 21:39:22 +00001351 switch (E.Kind) {
1352 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1353 case llvm::BitstreamEntry::Error:
1354 Error("malformed block record in AST file");
1355 return true;
1356 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00001357 return false;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001358 case llvm::BitstreamEntry::Record:
1359 // The interesting case.
1360 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001361 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001362
Guy Benyei11169dd2012-12-18 14:30:41 +00001363 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001364 Record.clear();
Chris Lattner15c3e7d2013-01-21 18:28:26 +00001365 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00001366 Expected<unsigned> MaybeRecord =
1367 SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1368 if (!MaybeRecord) {
1369 Error(MaybeRecord.takeError());
1370 return true;
1371 }
1372 switch (MaybeRecord.get()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001373 default: // Default behavior: ignore.
1374 break;
1375
1376 case SM_SLOC_FILE_ENTRY:
1377 case SM_SLOC_BUFFER_ENTRY:
1378 case SM_SLOC_EXPANSION_ENTRY:
1379 // Once we hit one of the source location entries, we're done.
1380 return false;
1381 }
1382 }
1383}
1384
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001385/// If a header file is not found at the path that we expect it to be
Guy Benyei11169dd2012-12-18 14:30:41 +00001386/// and the PCH file was moved from its original location, try to resolve the
1387/// file by assuming that header+PCH were moved together and the header is in
1388/// the same place relative to the PCH.
1389static std::string
1390resolveFileRelativeToOriginalDir(const std::string &Filename,
1391 const std::string &OriginalDir,
1392 const std::string &CurrDir) {
1393 assert(OriginalDir != CurrDir &&
1394 "No point trying to resolve the file if the PCH dir didn't change");
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001395
Guy Benyei11169dd2012-12-18 14:30:41 +00001396 using namespace llvm::sys;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001397
Guy Benyei11169dd2012-12-18 14:30:41 +00001398 SmallString<128> filePath(Filename);
1399 fs::make_absolute(filePath);
1400 assert(path::is_absolute(OriginalDir));
1401 SmallString<128> currPCHPath(CurrDir);
1402
1403 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1404 fileDirE = path::end(path::parent_path(filePath));
1405 path::const_iterator origDirI = path::begin(OriginalDir),
1406 origDirE = path::end(OriginalDir);
1407 // Skip the common path components from filePath and OriginalDir.
1408 while (fileDirI != fileDirE && origDirI != origDirE &&
1409 *fileDirI == *origDirI) {
1410 ++fileDirI;
1411 ++origDirI;
1412 }
1413 for (; origDirI != origDirE; ++origDirI)
1414 path::append(currPCHPath, "..");
1415 path::append(currPCHPath, fileDirI, fileDirE);
1416 path::append(currPCHPath, path::filename(Filename));
Benjamin Krameradcd0262020-01-28 20:23:46 +01001417 return std::string(currPCHPath.str());
Guy Benyei11169dd2012-12-18 14:30:41 +00001418}
1419
1420bool ASTReader::ReadSLocEntry(int ID) {
1421 if (ID == 0)
1422 return false;
1423
1424 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1425 Error("source location entry ID out-of-range for AST file");
1426 return true;
1427 }
1428
Richard Smithaada85c2016-02-06 02:06:43 +00001429 // Local helper to read the (possibly-compressed) buffer data following the
1430 // entry record.
1431 auto ReadBuffer = [this](
1432 BitstreamCursor &SLocEntryCursor,
1433 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1434 RecordData Record;
1435 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00001436 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1437 if (!MaybeCode) {
1438 Error(MaybeCode.takeError());
1439 return nullptr;
1440 }
1441 unsigned Code = MaybeCode.get();
1442
1443 Expected<unsigned> MaybeRecCode =
1444 SLocEntryCursor.readRecord(Code, Record, &Blob);
1445 if (!MaybeRecCode) {
1446 Error(MaybeRecCode.takeError());
1447 return nullptr;
1448 }
1449 unsigned RecCode = MaybeRecCode.get();
Richard Smithaada85c2016-02-06 02:06:43 +00001450
1451 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
George Rimarc39f5492017-01-17 15:45:31 +00001452 if (!llvm::zlib::isAvailable()) {
1453 Error("zlib is not available");
1454 return nullptr;
1455 }
Richard Smithaada85c2016-02-06 02:06:43 +00001456 SmallString<0> Uncompressed;
George Rimarc39f5492017-01-17 15:45:31 +00001457 if (llvm::Error E =
1458 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1459 Error("could not decompress embedded file contents: " +
1460 llvm::toString(std::move(E)));
Richard Smithaada85c2016-02-06 02:06:43 +00001461 return nullptr;
1462 }
1463 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1464 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1465 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1466 } else {
1467 Error("AST record has invalid code");
1468 return nullptr;
1469 }
1470 };
1471
Guy Benyei11169dd2012-12-18 14:30:41 +00001472 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
JF Bastien0e828952019-06-26 19:50:12 +00001473 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
Dmitry Polukhina7afb212020-04-16 09:24:46 -07001474 F->SLocEntryOffsetsBase +
JF Bastien0e828952019-06-26 19:50:12 +00001475 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1476 Error(std::move(Err));
1477 return true;
1478 }
1479
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001480 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001481 unsigned BaseOffset = F->SLocEntryBaseOffset;
1482
1483 ++NumSLocEntriesRead;
JF Bastien0e828952019-06-26 19:50:12 +00001484 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1485 if (!MaybeEntry) {
1486 Error(MaybeEntry.takeError());
1487 return true;
1488 }
1489 llvm::BitstreamEntry Entry = MaybeEntry.get();
1490
Chris Lattnere7b154b2013-01-19 21:39:22 +00001491 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001492 Error("incorrectly-formatted source location entry in AST file");
1493 return true;
1494 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001495
Guy Benyei11169dd2012-12-18 14:30:41 +00001496 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +00001497 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00001498 Expected<unsigned> MaybeSLOC =
1499 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1500 if (!MaybeSLOC) {
1501 Error(MaybeSLOC.takeError());
1502 return true;
1503 }
1504 switch (MaybeSLOC.get()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001505 default:
1506 Error("incorrectly-formatted source location entry in AST file");
1507 return true;
1508
1509 case SM_SLOC_FILE_ENTRY: {
1510 // We will detect whether a file changed and return 'Failure' for it, but
1511 // we will also try to fail gracefully by setting up the SLocEntry.
1512 unsigned InputID = Record[4];
1513 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001514 const FileEntry *File = IF.getFile();
1515 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei11169dd2012-12-18 14:30:41 +00001516
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001517 // Note that we only check if a File was returned. If it was out-of-date
1518 // we have complained but we will continue creating a FileID to recover
1519 // gracefully.
1520 if (!File)
Guy Benyei11169dd2012-12-18 14:30:41 +00001521 return true;
1522
1523 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1524 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1525 // This is the module's main file.
1526 IncludeLoc = getImportLocation(F);
1527 }
Vedant Kumar48b4f762018-04-14 01:40:48 +00001528 SrcMgr::CharacteristicKind
1529 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
Alex Lorenz4dc55732019-08-22 18:15:50 +00001530 // FIXME: The FileID should be created from the FileEntryRef.
Guy Benyei11169dd2012-12-18 14:30:41 +00001531 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1532 ID, BaseOffset + Record[0]);
Vedant Kumar48b4f762018-04-14 01:40:48 +00001533 SrcMgr::FileInfo &FileInfo =
1534 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Guy Benyei11169dd2012-12-18 14:30:41 +00001535 FileInfo.NumCreatedFIDs = Record[5];
1536 if (Record[3])
1537 FileInfo.setHasLineDirectives();
1538
Guy Benyei11169dd2012-12-18 14:30:41 +00001539 unsigned NumFileDecls = Record[7];
Richard Smithdbafb6c2017-06-29 23:23:46 +00001540 if (NumFileDecls && ContextObj) {
Roman Lebedevbf4f1e02019-10-10 12:22:42 +00001541 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
Guy Benyei11169dd2012-12-18 14:30:41 +00001542 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1543 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1544 NumFileDecls));
1545 }
Richard Smithaada85c2016-02-06 02:06:43 +00001546
Guy Benyei11169dd2012-12-18 14:30:41 +00001547 const SrcMgr::ContentCache *ContentCache
Richard Smithf3f84612017-06-29 02:19:42 +00001548 = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
Guy Benyei11169dd2012-12-18 14:30:41 +00001549 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
Richard Smitha8cfffa2015-11-26 02:04:16 +00001550 ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1551 !ContentCache->getRawBuffer()) {
Richard Smithaada85c2016-02-06 02:06:43 +00001552 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1553 if (!Buffer)
Guy Benyei11169dd2012-12-18 14:30:41 +00001554 return true;
David Blaikie49cc3182014-08-27 20:54:45 +00001555 SourceMgr.overrideFileContents(File, std::move(Buffer));
Guy Benyei11169dd2012-12-18 14:30:41 +00001556 }
1557
1558 break;
1559 }
1560
1561 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00001562 const char *Name = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00001563 unsigned Offset = Record[0];
Vedant Kumar48b4f762018-04-14 01:40:48 +00001564 SrcMgr::CharacteristicKind
1565 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
Guy Benyei11169dd2012-12-18 14:30:41 +00001566 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Manman Ren11f2a472016-08-18 17:42:15 +00001567 if (IncludeLoc.isInvalid() && F->isModule()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001568 IncludeLoc = getImportLocation(F);
1569 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001570
Richard Smithaada85c2016-02-06 02:06:43 +00001571 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1572 if (!Buffer)
Guy Benyei11169dd2012-12-18 14:30:41 +00001573 return true;
David Blaikie50a5f972014-08-29 07:59:55 +00001574 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
Rafael Espindolad87f8d72014-08-27 20:03:29 +00001575 BaseOffset + Offset, IncludeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001576 break;
1577 }
1578
1579 case SM_SLOC_EXPANSION_ENTRY: {
1580 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1581 SourceMgr.createExpansionLoc(SpellingLoc,
1582 ReadSourceLocation(*F, Record[2]),
1583 ReadSourceLocation(*F, Record[3]),
Richard Smithb5f81712018-04-30 05:25:48 +00001584 Record[5],
Guy Benyei11169dd2012-12-18 14:30:41 +00001585 Record[4],
1586 ID,
1587 BaseOffset + Record[0]);
1588 break;
1589 }
1590 }
1591
1592 return false;
1593}
1594
1595std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1596 if (ID == 0)
1597 return std::make_pair(SourceLocation(), "");
1598
1599 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1600 Error("source location entry ID out-of-range for AST file");
1601 return std::make_pair(SourceLocation(), "");
1602 }
1603
1604 // Find which module file this entry lands in.
1605 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
Manman Ren11f2a472016-08-18 17:42:15 +00001606 if (!M->isModule())
Guy Benyei11169dd2012-12-18 14:30:41 +00001607 return std::make_pair(SourceLocation(), "");
1608
1609 // FIXME: Can we map this down to a particular submodule? That would be
1610 // ideal.
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001611 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
Guy Benyei11169dd2012-12-18 14:30:41 +00001612}
1613
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001614/// Find the location where the module F is imported.
Guy Benyei11169dd2012-12-18 14:30:41 +00001615SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1616 if (F->ImportLoc.isValid())
1617 return F->ImportLoc;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001618
Guy Benyei11169dd2012-12-18 14:30:41 +00001619 // Otherwise we have a PCH. It's considered to be "imported" at the first
1620 // location of its includer.
1621 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001622 // Main file is the importer.
Yaron Keren8b563662015-10-03 10:46:20 +00001623 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001624 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
Guy Benyei11169dd2012-12-18 14:30:41 +00001625 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001626 return F->ImportedBy[0]->FirstLoc;
1627}
1628
JF Bastien0e828952019-06-26 19:50:12 +00001629/// Enter a subblock of the specified BlockID with the specified cursor. Read
1630/// the abbreviations that are at the top of the block and then leave the cursor
1631/// pointing into the block.
Daniel Grumbergbb8c7e72020-05-13 17:07:47 +01001632bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID,
1633 uint64_t *StartOfBlockOffset) {
JF Bastien0e828952019-06-26 19:50:12 +00001634 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1635 // FIXME this drops errors on the floor.
1636 consumeError(std::move(Err));
Richard Smith0516b182015-09-08 19:40:14 +00001637 return true;
JF Bastien0e828952019-06-26 19:50:12 +00001638 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001639
Daniel Grumbergbb8c7e72020-05-13 17:07:47 +01001640 if (StartOfBlockOffset)
1641 *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1642
Guy Benyei11169dd2012-12-18 14:30:41 +00001643 while (true) {
1644 uint64_t Offset = Cursor.GetCurrentBitNo();
JF Bastien0e828952019-06-26 19:50:12 +00001645 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1646 if (!MaybeCode) {
1647 // FIXME this drops errors on the floor.
1648 consumeError(MaybeCode.takeError());
1649 return true;
1650 }
1651 unsigned Code = MaybeCode.get();
Guy Benyei11169dd2012-12-18 14:30:41 +00001652
1653 // We expect all abbrevs to be at the start of the block.
1654 if (Code != llvm::bitc::DEFINE_ABBREV) {
JF Bastien0e828952019-06-26 19:50:12 +00001655 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1656 // FIXME this drops errors on the floor.
1657 consumeError(std::move(Err));
1658 return true;
1659 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001660 return false;
1661 }
JF Bastien0e828952019-06-26 19:50:12 +00001662 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1663 // FIXME this drops errors on the floor.
1664 consumeError(std::move(Err));
1665 return true;
1666 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001667 }
1668}
1669
Richard Smithe40f2ba2013-08-07 21:41:30 +00001670Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
John McCallf413f5e2013-05-03 00:10:13 +00001671 unsigned &Idx) {
1672 Token Tok;
1673 Tok.startToken();
1674 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1675 Tok.setLength(Record[Idx++]);
1676 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1677 Tok.setIdentifierInfo(II);
1678 Tok.setKind((tok::TokenKind)Record[Idx++]);
1679 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1680 return Tok;
1681}
1682
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001683MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001684 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001685
1686 // Keep track of where we are in the stream, then jump back there
1687 // after reading this macro.
1688 SavedStreamPosition SavedPosition(Stream);
1689
JF Bastien0e828952019-06-26 19:50:12 +00001690 if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1691 // FIXME this drops errors on the floor.
1692 consumeError(std::move(Err));
1693 return nullptr;
1694 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001695 RecordData Record;
Faisal Valiac506d72017-07-17 17:18:43 +00001696 SmallVector<IdentifierInfo*, 16> MacroParams;
Craig Toppera13603a2014-05-22 05:54:18 +00001697 MacroInfo *Macro = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001698
Guy Benyei11169dd2012-12-18 14:30:41 +00001699 while (true) {
Chris Lattnerefa77172013-01-20 00:00:22 +00001700 // Advance to the next record, but if we get to the end of the block, don't
1701 // pop it (removing all the abbreviations from the cursor) since we want to
1702 // be able to reseek within the block and read entries.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001703 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
JF Bastien0e828952019-06-26 19:50:12 +00001704 Expected<llvm::BitstreamEntry> MaybeEntry =
1705 Stream.advanceSkippingSubblocks(Flags);
1706 if (!MaybeEntry) {
1707 Error(MaybeEntry.takeError());
1708 return Macro;
1709 }
1710 llvm::BitstreamEntry Entry = MaybeEntry.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001711
Chris Lattnerefa77172013-01-20 00:00:22 +00001712 switch (Entry.Kind) {
1713 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1714 case llvm::BitstreamEntry::Error:
1715 Error("malformed block record in AST file");
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001716 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001717 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001718 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001719 case llvm::BitstreamEntry::Record:
1720 // The interesting case.
1721 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001722 }
1723
1724 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001725 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00001726 PreprocessorRecordTypes RecType;
1727 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1728 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1729 else {
1730 Error(MaybeRecType.takeError());
1731 return Macro;
1732 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001733 switch (RecType) {
Richard Smithd7329392015-04-21 21:46:32 +00001734 case PP_MODULE_MACRO:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001735 case PP_MACRO_DIRECTIVE_HISTORY:
1736 return Macro;
1737
Guy Benyei11169dd2012-12-18 14:30:41 +00001738 case PP_MACRO_OBJECT_LIKE:
1739 case PP_MACRO_FUNCTION_LIKE: {
1740 // If we already have a macro, that means that we've hit the end
1741 // of the definition of the macro we were looking for. We're
1742 // done.
1743 if (Macro)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001744 return Macro;
Guy Benyei11169dd2012-12-18 14:30:41 +00001745
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001746 unsigned NextIndex = 1; // Skip identifier ID.
Guy Benyei11169dd2012-12-18 14:30:41 +00001747 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Richard Smith3f6dd7a2017-05-12 23:40:52 +00001748 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Argyrios Kyrtzidis7572be22013-01-07 19:16:23 +00001749 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei11169dd2012-12-18 14:30:41 +00001750 MI->setIsUsed(Record[NextIndex++]);
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00001751 MI->setUsedForHeaderGuard(Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001752
Guy Benyei11169dd2012-12-18 14:30:41 +00001753 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1754 // Decode function-like macro info.
1755 bool isC99VarArgs = Record[NextIndex++];
1756 bool isGNUVarArgs = Record[NextIndex++];
1757 bool hasCommaPasting = Record[NextIndex++];
Faisal Valiac506d72017-07-17 17:18:43 +00001758 MacroParams.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00001759 unsigned NumArgs = Record[NextIndex++];
1760 for (unsigned i = 0; i != NumArgs; ++i)
Faisal Valiac506d72017-07-17 17:18:43 +00001761 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00001762
1763 // Install function-like macro info.
1764 MI->setIsFunctionLike();
1765 if (isC99VarArgs) MI->setIsC99Varargs();
1766 if (isGNUVarArgs) MI->setIsGNUVarargs();
1767 if (hasCommaPasting) MI->setHasCommaPasting();
Faisal Valiac506d72017-07-17 17:18:43 +00001768 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
Guy Benyei11169dd2012-12-18 14:30:41 +00001769 }
1770
Guy Benyei11169dd2012-12-18 14:30:41 +00001771 // Remember that we saw this macro last so that we add the tokens that
1772 // form its body to it.
1773 Macro = MI;
1774
1775 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1776 Record[NextIndex]) {
1777 // We have a macro definition. Register the association
1778 PreprocessedEntityID
1779 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1780 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Richard Smith66a81862015-05-04 02:25:31 +00001781 PreprocessingRecord::PPEntityID PPID =
1782 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
Vedant Kumar48b4f762018-04-14 01:40:48 +00001783 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
Richard Smith66a81862015-05-04 02:25:31 +00001784 PPRec.getPreprocessedEntity(PPID));
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +00001785 if (PPDef)
1786 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei11169dd2012-12-18 14:30:41 +00001787 }
1788
1789 ++NumMacrosRead;
1790 break;
1791 }
1792
1793 case PP_TOKEN: {
1794 // If we see a TOKEN before a PP_MACRO_*, then the file is
1795 // erroneous, just pretend we didn't see this.
Craig Toppera13603a2014-05-22 05:54:18 +00001796 if (!Macro) break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001797
John McCallf413f5e2013-05-03 00:10:13 +00001798 unsigned Idx = 0;
1799 Token Tok = ReadToken(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001800 Macro->AddTokenToBody(Tok);
1801 break;
1802 }
1803 }
1804 }
1805}
1806
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001807PreprocessedEntityID
Richard Smith37a93df2017-02-18 00:32:02 +00001808ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1809 unsigned LocalID) const {
1810 if (!M.ModuleOffsetMap.empty())
1811 ReadModuleOffsetMap(M);
1812
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001813 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Guy Benyei11169dd2012-12-18 14:30:41 +00001814 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001815 assert(I != M.PreprocessedEntityRemap.end()
Guy Benyei11169dd2012-12-18 14:30:41 +00001816 && "Invalid index into preprocessed entity index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001817
Guy Benyei11169dd2012-12-18 14:30:41 +00001818 return LocalID + I->second;
1819}
1820
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001821unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1822 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei11169dd2012-12-18 14:30:41 +00001823}
Richard Smith7ed1bc92014-12-05 22:42:13 +00001824
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001825HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001826HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
Richard Smithe75ee0f2015-08-17 07:13:32 +00001827 internal_key_type ikey = {FE->getSize(),
1828 M.HasTimestamps ? FE->getModificationTime() : 0,
1829 FE->getName(), /*Imported*/ false};
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001830 return ikey;
1831}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001832
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001833bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
Richard Smithe75ee0f2015-08-17 07:13:32 +00001834 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
Guy Benyei11169dd2012-12-18 14:30:41 +00001835 return false;
1836
Mehdi Amini004b9c72016-10-10 22:52:47 +00001837 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001838 return true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001839
Guy Benyei11169dd2012-12-18 14:30:41 +00001840 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001841 FileManager &FileMgr = Reader.getFileManager();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001842 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
Harlan Haskins8d323d12019-08-01 21:31:56 +00001843 if (!Key.Imported) {
1844 if (auto File = FileMgr.getFile(Key.Filename))
1845 return *File;
1846 return nullptr;
1847 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001848
Benjamin Krameradcd0262020-01-28 20:23:46 +01001849 std::string Resolved = std::string(Key.Filename);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001850 Reader.ResolveImportedPath(M, Resolved);
Harlan Haskins8d323d12019-08-01 21:31:56 +00001851 if (auto File = FileMgr.getFile(Resolved))
1852 return *File;
1853 return nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001854 };
1855
1856 const FileEntry *FEA = GetFile(a);
1857 const FileEntry *FEB = GetFile(b);
1858 return FEA && FEA == FEB;
Guy Benyei11169dd2012-12-18 14:30:41 +00001859}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001860
Guy Benyei11169dd2012-12-18 14:30:41 +00001861std::pair<unsigned, unsigned>
1862HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001863 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001864
Vedant Kumar48b4f762018-04-14 01:40:48 +00001865 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1866 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001867 return std::make_pair(KeyLen, DataLen);
Guy Benyei11169dd2012-12-18 14:30:41 +00001868}
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001869
1870HeaderFileInfoTrait::internal_key_type
1871HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001872 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001873
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001874 internal_key_type ikey;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001875 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1876 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001877 ikey.Filename = (const char *)d;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001878 ikey.Imported = true;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001879 return ikey;
1880}
1881
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001882HeaderFileInfoTrait::data_type
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001883HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei11169dd2012-12-18 14:30:41 +00001884 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001885 using namespace llvm::support;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00001886
1887 const unsigned char *End = d + DataLen;
Guy Benyei11169dd2012-12-18 14:30:41 +00001888 HeaderFileInfo HFI;
1889 unsigned Flags = *d++;
Richard Smith386bb072015-08-18 23:42:23 +00001890 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
Richard Smithf3f84612017-06-29 02:19:42 +00001891 HFI.isImport |= (Flags >> 5) & 0x01;
1892 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1893 HFI.DirInfo = (Flags >> 1) & 0x07;
Guy Benyei11169dd2012-12-18 14:30:41 +00001894 HFI.IndexHeaderMapHeader = Flags & 0x01;
Richard Smith386bb072015-08-18 23:42:23 +00001895 // FIXME: Find a better way to handle this. Maybe just store a
1896 // "has been included" flag?
1897 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1898 HFI.NumIncludes);
Justin Bogner57ba0b22014-03-28 22:03:24 +00001899 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1900 M, endian::readNext<uint32_t, little, unaligned>(d));
1901 if (unsigned FrameworkOffset =
1902 endian::readNext<uint32_t, little, unaligned>(d)) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001903 // The framework offset is 1 greater than the actual offset,
Guy Benyei11169dd2012-12-18 14:30:41 +00001904 // since 0 is used as an indicator for "no framework name".
1905 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1906 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1907 }
Richard Smith386bb072015-08-18 23:42:23 +00001908
1909 assert((End - d) % 4 == 0 &&
1910 "Wrong data length in HeaderFileInfo deserialization");
1911 while (d != End) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001912 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
Richard Smith386bb072015-08-18 23:42:23 +00001913 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1914 LocalSMID >>= 2;
1915
1916 // This header is part of a module. Associate it with the module to enable
1917 // implicit module import.
1918 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1919 Module *Mod = Reader.getSubmodule(GlobalSMID);
1920 FileManager &FileMgr = Reader.getFileManager();
1921 ModuleMap &ModMap =
1922 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1923
Benjamin Krameradcd0262020-01-28 20:23:46 +01001924 std::string Filename = std::string(key.Filename);
Richard Smith386bb072015-08-18 23:42:23 +00001925 if (key.Imported)
1926 Reader.ResolveImportedPath(M, Filename);
1927 // FIXME: This is not always the right filename-as-written, but we're not
1928 // going to use this information to rebuild the module, so it doesn't make
1929 // a lot of difference.
Benjamin Krameradcd0262020-01-28 20:23:46 +01001930 Module::Header H = {std::string(key.Filename), *FileMgr.getFile(Filename)};
Richard Smithd8879c82015-08-24 21:59:32 +00001931 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1932 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001933 }
1934
Guy Benyei11169dd2012-12-18 14:30:41 +00001935 // This HeaderFileInfo was externally loaded.
1936 HFI.External = true;
Richard Smithd8879c82015-08-24 21:59:32 +00001937 HFI.IsValid = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001938 return HFI;
1939}
1940
Dmitry Polukhina7afb212020-04-16 09:24:46 -07001941void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1942 uint32_t MacroDirectivesOffset) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001943 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1944 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00001945}
1946
1947void ASTReader::ReadDefinedMacros() {
1948 // Note that we are loading defined macros.
1949 Deserializing Macros(this);
1950
Vedant Kumar48b4f762018-04-14 01:40:48 +00001951 for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001952 BitstreamCursor &MacroCursor = I.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001953
1954 // If there was no preprocessor block, skip this file.
Peter Collingbourne77c89b62016-11-08 04:17:11 +00001955 if (MacroCursor.getBitcodeBytes().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00001956 continue;
1957
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001958 BitstreamCursor Cursor = MacroCursor;
JF Bastien0e828952019-06-26 19:50:12 +00001959 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1960 Error(std::move(Err));
1961 return;
1962 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001963
1964 RecordData Record;
1965 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00001966 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1967 if (!MaybeE) {
1968 Error(MaybeE.takeError());
1969 return;
1970 }
1971 llvm::BitstreamEntry E = MaybeE.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001972
Chris Lattnere7b154b2013-01-19 21:39:22 +00001973 switch (E.Kind) {
1974 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1975 case llvm::BitstreamEntry::Error:
1976 Error("malformed block record in AST file");
1977 return;
1978 case llvm::BitstreamEntry::EndBlock:
1979 goto NextCursor;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001980
JF Bastien0e828952019-06-26 19:50:12 +00001981 case llvm::BitstreamEntry::Record: {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001982 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00001983 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1984 if (!MaybeRecord) {
1985 Error(MaybeRecord.takeError());
1986 return;
1987 }
1988 switch (MaybeRecord.get()) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001989 default: // Default behavior: ignore.
1990 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001991
Chris Lattnere7b154b2013-01-19 21:39:22 +00001992 case PP_MACRO_OBJECT_LIKE:
Sean Callananf3682a72016-05-14 06:24:14 +00001993 case PP_MACRO_FUNCTION_LIKE: {
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001994 IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
Sean Callananf3682a72016-05-14 06:24:14 +00001995 if (II->isOutOfDate())
1996 updateOutOfDateIdentifier(*II);
Chris Lattnere7b154b2013-01-19 21:39:22 +00001997 break;
Sean Callananf3682a72016-05-14 06:24:14 +00001998 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001999
Chris Lattnere7b154b2013-01-19 21:39:22 +00002000 case PP_TOKEN:
2001 // Ignore tokens.
2002 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002003 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002004 break;
2005 }
JF Bastien0e828952019-06-26 19:50:12 +00002006 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002007 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002008 NextCursor: ;
Guy Benyei11169dd2012-12-18 14:30:41 +00002009 }
2010}
2011
2012namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002013
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002014 /// Visitor class used to look up identifirs in an AST file.
Guy Benyei11169dd2012-12-18 14:30:41 +00002015 class IdentifierLookupVisitor {
2016 StringRef Name;
Richard Smith3b637412015-07-14 18:42:41 +00002017 unsigned NameHash;
Guy Benyei11169dd2012-12-18 14:30:41 +00002018 unsigned PriorGeneration;
Douglas Gregor00a50f72013-01-25 00:38:33 +00002019 unsigned &NumIdentifierLookups;
2020 unsigned &NumIdentifierLookupHits;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002021 IdentifierInfo *Found = nullptr;
Douglas Gregor00a50f72013-01-25 00:38:33 +00002022
Guy Benyei11169dd2012-12-18 14:30:41 +00002023 public:
Douglas Gregor00a50f72013-01-25 00:38:33 +00002024 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2025 unsigned &NumIdentifierLookups,
2026 unsigned &NumIdentifierLookupHits)
Richard Smith3b637412015-07-14 18:42:41 +00002027 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2028 PriorGeneration(PriorGeneration),
Douglas Gregor00a50f72013-01-25 00:38:33 +00002029 NumIdentifierLookups(NumIdentifierLookups),
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002030 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00002031
2032 bool operator()(ModuleFile &M) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002033 // If we've already searched this module file, skip it now.
Richard Smithbdf2d932015-07-30 03:37:16 +00002034 if (M.Generation <= PriorGeneration)
Guy Benyei11169dd2012-12-18 14:30:41 +00002035 return true;
Douglas Gregore060e572013-01-25 01:03:03 +00002036
Vedant Kumar48b4f762018-04-14 01:40:48 +00002037 ASTIdentifierLookupTable *IdTable
2038 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
Guy Benyei11169dd2012-12-18 14:30:41 +00002039 if (!IdTable)
2040 return false;
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00002041
2042 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
Richard Smithbdf2d932015-07-30 03:37:16 +00002043 Found);
2044 ++NumIdentifierLookups;
Richard Smith3b637412015-07-14 18:42:41 +00002045 ASTIdentifierLookupTable::iterator Pos =
Richard Smithbdf2d932015-07-30 03:37:16 +00002046 IdTable->find_hashed(Name, NameHash, &Trait);
Guy Benyei11169dd2012-12-18 14:30:41 +00002047 if (Pos == IdTable->end())
2048 return false;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002049
Guy Benyei11169dd2012-12-18 14:30:41 +00002050 // Dereferencing the iterator has the effect of building the
2051 // IdentifierInfo node and populating it with the various
2052 // declarations it needs.
Richard Smithbdf2d932015-07-30 03:37:16 +00002053 ++NumIdentifierLookupHits;
2054 Found = *Pos;
Guy Benyei11169dd2012-12-18 14:30:41 +00002055 return true;
2056 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002057
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002058 // Retrieve the identifier info found within the module
Guy Benyei11169dd2012-12-18 14:30:41 +00002059 // files.
2060 IdentifierInfo *getIdentifierInfo() const { return Found; }
2061 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002062
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002063} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00002064
2065void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2066 // Note that we are loading an identifier.
2067 Deserializing AnIdentifier(this);
2068
2069 unsigned PriorGeneration = 0;
2070 if (getContext().getLangOpts().Modules)
2071 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregore060e572013-01-25 01:03:03 +00002072
2073 // If there is a global index, look there first to determine which modules
2074 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00002075 GlobalModuleIndex::HitSet Hits;
Craig Toppera13603a2014-05-22 05:54:18 +00002076 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
Douglas Gregore060e572013-01-25 01:03:03 +00002077 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00002078 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2079 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00002080 }
2081 }
2082
Douglas Gregor7211ac12013-01-25 23:32:03 +00002083 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregor00a50f72013-01-25 00:38:33 +00002084 NumIdentifierLookups,
2085 NumIdentifierLookupHits);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00002086 ModuleMgr.visit(Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00002087 markIdentifierUpToDate(&II);
2088}
2089
2090void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2091 if (!II)
2092 return;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002093
Guy Benyei11169dd2012-12-18 14:30:41 +00002094 II->setOutOfDate(false);
2095
2096 // Update the generation for this identifier.
2097 if (getContext().getLangOpts().Modules)
Richard Smith053f6c62014-05-16 23:01:30 +00002098 IdentifierGeneration[II] = getGeneration();
Guy Benyei11169dd2012-12-18 14:30:41 +00002099}
2100
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002101void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2102 const PendingMacroInfo &PMInfo) {
Richard Smithd7329392015-04-21 21:46:32 +00002103 ModuleFile &M = *PMInfo.M;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002104
2105 BitstreamCursor &Cursor = M.MacroCursor;
2106 SavedStreamPosition SavedPosition(Cursor);
Dmitry Polukhina7afb212020-04-16 09:24:46 -07002107 if (llvm::Error Err =
2108 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
JF Bastien0e828952019-06-26 19:50:12 +00002109 Error(std::move(Err));
2110 return;
2111 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002112
Richard Smith713369b2015-04-23 20:40:50 +00002113 struct ModuleMacroRecord {
2114 SubmoduleID SubModID;
2115 MacroInfo *MI;
2116 SmallVector<SubmoduleID, 8> Overrides;
2117 };
2118 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002119
Richard Smithd7329392015-04-21 21:46:32 +00002120 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2121 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2122 // macro histroy.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002123 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002124 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00002125 Expected<llvm::BitstreamEntry> MaybeEntry =
Richard Smithd7329392015-04-21 21:46:32 +00002126 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
JF Bastien0e828952019-06-26 19:50:12 +00002127 if (!MaybeEntry) {
2128 Error(MaybeEntry.takeError());
2129 return;
2130 }
2131 llvm::BitstreamEntry Entry = MaybeEntry.get();
2132
Richard Smithd7329392015-04-21 21:46:32 +00002133 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2134 Error("malformed block record in AST file");
2135 return;
2136 }
2137
2138 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00002139 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2140 if (!MaybePP) {
2141 Error(MaybePP.takeError());
2142 return;
2143 }
2144 switch ((PreprocessorRecordTypes)MaybePP.get()) {
Richard Smithd7329392015-04-21 21:46:32 +00002145 case PP_MACRO_DIRECTIVE_HISTORY:
2146 break;
2147
2148 case PP_MODULE_MACRO: {
Richard Smith713369b2015-04-23 20:40:50 +00002149 ModuleMacros.push_back(ModuleMacroRecord());
2150 auto &Info = ModuleMacros.back();
Richard Smithe56c8bc2015-04-22 00:26:11 +00002151 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2152 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
Richard Smith713369b2015-04-23 20:40:50 +00002153 for (int I = 2, N = Record.size(); I != N; ++I)
2154 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
Richard Smithd7329392015-04-21 21:46:32 +00002155 continue;
2156 }
2157
2158 default:
2159 Error("malformed block record in AST file");
2160 return;
2161 }
2162
2163 // We found the macro directive history; that's the last record
2164 // for this macro.
2165 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002166 }
2167
Richard Smithd7329392015-04-21 21:46:32 +00002168 // Module macros are listed in reverse dependency order.
Richard Smithe56c8bc2015-04-22 00:26:11 +00002169 {
2170 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
Richard Smithe56c8bc2015-04-22 00:26:11 +00002171 llvm::SmallVector<ModuleMacro*, 8> Overrides;
Richard Smith713369b2015-04-23 20:40:50 +00002172 for (auto &MMR : ModuleMacros) {
Richard Smithe56c8bc2015-04-22 00:26:11 +00002173 Overrides.clear();
Vedant Kumar48b4f762018-04-14 01:40:48 +00002174 for (unsigned ModID : MMR.Overrides) {
Richard Smithb8b2ed62015-04-23 18:18:26 +00002175 Module *Mod = getSubmodule(ModID);
2176 auto *Macro = PP.getModuleMacro(Mod, II);
Richard Smithe56c8bc2015-04-22 00:26:11 +00002177 assert(Macro && "missing definition for overridden macro");
Richard Smith5dbef922015-04-22 02:09:43 +00002178 Overrides.push_back(Macro);
Richard Smithe56c8bc2015-04-22 00:26:11 +00002179 }
2180
2181 bool Inserted = false;
Richard Smith713369b2015-04-23 20:40:50 +00002182 Module *Owner = getSubmodule(MMR.SubModID);
Richard Smith20e883e2015-04-29 23:20:19 +00002183 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
Richard Smithd7329392015-04-21 21:46:32 +00002184 }
2185 }
2186
2187 // Don't read the directive history for a module; we don't have anywhere
2188 // to put it.
Manman Ren11f2a472016-08-18 17:42:15 +00002189 if (M.isModule())
Richard Smithd7329392015-04-21 21:46:32 +00002190 return;
2191
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002192 // Deserialize the macro directives history in reverse source-order.
Craig Toppera13603a2014-05-22 05:54:18 +00002193 MacroDirective *Latest = nullptr, *Earliest = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002194 unsigned Idx = 0, N = Record.size();
2195 while (Idx < N) {
Craig Toppera13603a2014-05-22 05:54:18 +00002196 MacroDirective *MD = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002197 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00002198 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002199 switch (K) {
2200 case MacroDirective::MD_Define: {
Richard Smith713369b2015-04-23 20:40:50 +00002201 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
Richard Smith3981b172015-04-30 02:16:23 +00002202 MD = PP.AllocateDefMacroDirective(MI, Loc);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002203 break;
2204 }
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002205 case MacroDirective::MD_Undefine:
Richard Smith3981b172015-04-30 02:16:23 +00002206 MD = PP.AllocateUndefMacroDirective(Loc);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002207 break;
Richard Smithdaa69e02014-07-25 04:40:03 +00002208 case MacroDirective::MD_Visibility:
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002209 bool isPublic = Record[Idx++];
2210 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2211 break;
2212 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002213
2214 if (!Latest)
2215 Latest = MD;
2216 if (Earliest)
2217 Earliest->setPrevious(MD);
2218 Earliest = MD;
2219 }
2220
Richard Smithd6e8c0d2015-05-04 19:58:00 +00002221 if (Latest)
Nico Weberfd870702016-12-09 17:32:52 +00002222 PP.setLoadedMacroDirective(II, Earliest, Latest);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002223}
2224
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002225ASTReader::InputFileInfo
2226ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002227 // Go find this input file.
2228 BitstreamCursor &Cursor = F.InputFilesCursor;
2229 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00002230 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2231 // FIXME this drops errors on the floor.
2232 consumeError(std::move(Err));
2233 }
Ben Langmuir198c1682014-03-07 07:27:49 +00002234
JF Bastien0e828952019-06-26 19:50:12 +00002235 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2236 if (!MaybeCode) {
2237 // FIXME this drops errors on the floor.
2238 consumeError(MaybeCode.takeError());
2239 }
2240 unsigned Code = MaybeCode.get();
Ben Langmuir198c1682014-03-07 07:27:49 +00002241 RecordData Record;
2242 StringRef Blob;
2243
JF Bastien0e828952019-06-26 19:50:12 +00002244 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2245 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2246 "invalid record type for input file");
2247 else {
2248 // FIXME this drops errors on the floor.
2249 consumeError(Maybe.takeError());
2250 }
Ben Langmuir198c1682014-03-07 07:27:49 +00002251
2252 assert(Record[0] == ID && "Bogus stored ID or offset");
Richard Smitha8cfffa2015-11-26 02:04:16 +00002253 InputFileInfo R;
2254 R.StoredSize = static_cast<off_t>(Record[1]);
2255 R.StoredTime = static_cast<time_t>(Record[2]);
2256 R.Overridden = static_cast<bool>(Record[3]);
2257 R.Transient = static_cast<bool>(Record[4]);
Richard Smithf3f84612017-06-29 02:19:42 +00002258 R.TopLevelModuleMap = static_cast<bool>(Record[5]);
Benjamin Krameradcd0262020-01-28 20:23:46 +01002259 R.Filename = std::string(Blob);
Richard Smitha8cfffa2015-11-26 02:04:16 +00002260 ResolveImportedPath(F, R.Filename);
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002261
2262 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2263 if (!MaybeEntry) // FIXME this drops errors on the floor.
2264 consumeError(MaybeEntry.takeError());
2265 llvm::BitstreamEntry Entry = MaybeEntry.get();
2266 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2267 "expected record type for input file hash");
2268
2269 Record.clear();
2270 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2271 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2272 "invalid record type for input file hash");
2273 else {
2274 // FIXME this drops errors on the floor.
2275 consumeError(Maybe.takeError());
2276 }
2277 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2278 static_cast<uint64_t>(Record[0]);
Hans Wennborg73945142014-03-14 17:45:06 +00002279 return R;
Ben Langmuir198c1682014-03-07 07:27:49 +00002280}
2281
Manman Renc8c94152016-10-21 23:35:03 +00002282static unsigned moduleKindForDiagnostic(ModuleKind Kind);
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002283InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002284 // If this ID is bogus, just return an empty input file.
2285 if (ID == 0 || ID > F.InputFilesLoaded.size())
2286 return InputFile();
2287
2288 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002289 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei11169dd2012-12-18 14:30:41 +00002290 return F.InputFilesLoaded[ID-1];
2291
Argyrios Kyrtzidis9308f0a2014-01-08 19:13:34 +00002292 if (F.InputFilesLoaded[ID-1].isNotFound())
2293 return InputFile();
2294
Guy Benyei11169dd2012-12-18 14:30:41 +00002295 // Go find this input file.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002296 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00002297 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00002298 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2299 // FIXME this drops errors on the floor.
2300 consumeError(std::move(Err));
2301 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002302
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002303 InputFileInfo FI = readInputFileInfo(F, ID);
2304 off_t StoredSize = FI.StoredSize;
2305 time_t StoredTime = FI.StoredTime;
2306 bool Overridden = FI.Overridden;
Richard Smitha8cfffa2015-11-26 02:04:16 +00002307 bool Transient = FI.Transient;
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002308 StringRef Filename = FI.Filename;
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002309 uint64_t StoredContentHash = FI.ContentHash;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002310
Harlan Haskins8d323d12019-08-01 21:31:56 +00002311 const FileEntry *File = nullptr;
2312 if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false))
2313 File = *FE;
2314
Ben Langmuir198c1682014-03-07 07:27:49 +00002315 // If we didn't find the file, resolve it relative to the
2316 // original directory from which this AST file was created.
Manuel Klimek1b29b4f2017-07-25 10:22:06 +00002317 if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2318 F.OriginalDir != F.BaseDirectory) {
2319 std::string Resolved = resolveFileRelativeToOriginalDir(
Benjamin Krameradcd0262020-01-28 20:23:46 +01002320 std::string(Filename), F.OriginalDir, F.BaseDirectory);
Ben Langmuir198c1682014-03-07 07:27:49 +00002321 if (!Resolved.empty())
Harlan Haskins8d323d12019-08-01 21:31:56 +00002322 if (auto FE = FileMgr.getFile(Resolved))
2323 File = *FE;
Ben Langmuir198c1682014-03-07 07:27:49 +00002324 }
2325
2326 // For an overridden file, create a virtual file with the stored
2327 // size/timestamp.
Richard Smitha8cfffa2015-11-26 02:04:16 +00002328 if ((Overridden || Transient) && File == nullptr)
Ben Langmuir198c1682014-03-07 07:27:49 +00002329 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
Ben Langmuir198c1682014-03-07 07:27:49 +00002330
Craig Toppera13603a2014-05-22 05:54:18 +00002331 if (File == nullptr) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002332 if (Complain) {
2333 std::string ErrorStr = "could not find file '";
2334 ErrorStr += Filename;
Richard Smith68142212015-10-13 01:26:26 +00002335 ErrorStr += "' referenced by AST file '";
2336 ErrorStr += F.FileName;
2337 ErrorStr += "'";
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002338 Error(ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00002339 }
Ben Langmuir198c1682014-03-07 07:27:49 +00002340 // Record that we didn't find the file.
2341 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2342 return InputFile();
2343 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002344
Ben Langmuir198c1682014-03-07 07:27:49 +00002345 // Check if there was a request to override the contents of the file
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002346 // that was part of the precompiled header. Overriding such a file
Ben Langmuir198c1682014-03-07 07:27:49 +00002347 // can lead to problems when lexing using the source locations from the
2348 // PCH.
2349 SourceManager &SM = getSourceManager();
Richard Smith64daf7b2015-12-01 03:32:49 +00002350 // FIXME: Reject if the overrides are different.
2351 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002352 if (Complain)
2353 Error(diag::err_fe_pch_file_overridden, Filename);
Duncan P. N. Exon Smithe1b7f222019-08-30 22:59:25 +00002354
2355 // After emitting the diagnostic, bypass the overriding file to recover
2356 // (this creates a separate FileEntry).
2357 File = SM.bypassFileContentsOverride(*File);
2358 if (!File) {
2359 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2360 return InputFile();
2361 }
Ben Langmuir198c1682014-03-07 07:27:49 +00002362 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002363
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002364 enum ModificationType {
2365 Size,
2366 ModTime,
2367 Content,
2368 None,
2369 };
2370 auto HasInputFileChanged = [&]() {
2371 if (StoredSize != File->getSize())
2372 return ModificationType::Size;
2373 if (!DisableValidation && StoredTime &&
2374 StoredTime != File->getModificationTime()) {
2375 // In case the modification time changes but not the content,
2376 // accept the cached file as legit.
2377 if (ValidateASTInputFilesContent &&
2378 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2379 auto MemBuffOrError = FileMgr.getBufferForFile(File);
2380 if (!MemBuffOrError) {
2381 if (!Complain)
2382 return ModificationType::ModTime;
2383 std::string ErrorStr = "could not get buffer for file '";
2384 ErrorStr += File->getName();
2385 ErrorStr += "'";
2386 Error(ErrorStr);
2387 return ModificationType::ModTime;
2388 }
Eric Christopher3be91692019-10-14 23:14:24 +00002389
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002390 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2391 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2392 return ModificationType::None;
2393 return ModificationType::Content;
2394 }
2395 return ModificationType::ModTime;
2396 }
2397 return ModificationType::None;
2398 };
2399
2400 bool IsOutOfDate = false;
2401 auto FileChange = HasInputFileChanged();
Ben Langmuir198c1682014-03-07 07:27:49 +00002402 // For an overridden file, there is nothing to validate.
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002403 if (!Overridden && FileChange != ModificationType::None) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002404 if (Complain) {
2405 // Build a list of the PCH imports that got us here (in reverse).
2406 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002407 while (!ImportStack.back()->ImportedBy.empty())
Ben Langmuir198c1682014-03-07 07:27:49 +00002408 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
Ben Langmuire82630d2014-01-17 00:19:09 +00002409
Ben Langmuir198c1682014-03-07 07:27:49 +00002410 // The top-level PCH is stale.
2411 StringRef TopLevelPCHName(ImportStack.back()->FileName);
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002412 unsigned DiagnosticKind =
2413 moduleKindForDiagnostic(ImportStack.back()->Kind);
Manman Renc8c94152016-10-21 23:35:03 +00002414 if (DiagnosticKind == 0)
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002415 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName,
2416 (unsigned)FileChange);
Manman Renc8c94152016-10-21 23:35:03 +00002417 else if (DiagnosticKind == 1)
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002418 Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName,
2419 (unsigned)FileChange);
Manman Renc8c94152016-10-21 23:35:03 +00002420 else
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00002421 Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName,
2422 (unsigned)FileChange);
Ben Langmuire82630d2014-01-17 00:19:09 +00002423
Ben Langmuir198c1682014-03-07 07:27:49 +00002424 // Print the import stack.
2425 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2426 Diag(diag::note_pch_required_by)
2427 << Filename << ImportStack[0]->FileName;
2428 for (unsigned I = 1; I < ImportStack.size(); ++I)
Ben Langmuire82630d2014-01-17 00:19:09 +00002429 Diag(diag::note_pch_required_by)
Ben Langmuir198c1682014-03-07 07:27:49 +00002430 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
Douglas Gregor7029ce12013-03-19 00:28:20 +00002431 }
2432
Ben Langmuir198c1682014-03-07 07:27:49 +00002433 if (!Diags.isDiagnosticInFlight())
2434 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
Guy Benyei11169dd2012-12-18 14:30:41 +00002435 }
2436
Ben Langmuir198c1682014-03-07 07:27:49 +00002437 IsOutOfDate = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00002438 }
Richard Smitha8cfffa2015-11-26 02:04:16 +00002439 // FIXME: If the file is overridden and we've already opened it,
2440 // issue an error (or split it into a separate FileEntry).
Guy Benyei11169dd2012-12-18 14:30:41 +00002441
Richard Smitha8cfffa2015-11-26 02:04:16 +00002442 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
Ben Langmuir198c1682014-03-07 07:27:49 +00002443
2444 // Note that we've loaded this input file.
2445 F.InputFilesLoaded[ID-1] = IF;
2446 return IF;
Guy Benyei11169dd2012-12-18 14:30:41 +00002447}
2448
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002449/// If we are loading a relocatable PCH or module file, and the filename
Richard Smith7ed1bc92014-12-05 22:42:13 +00002450/// is not an absolute path, add the system or module root to the beginning of
2451/// the file name.
2452void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2453 // Resolve relative to the base directory, if we have one.
2454 if (!M.BaseDirectory.empty())
2455 return ResolveImportedPath(Filename, M.BaseDirectory);
Guy Benyei11169dd2012-12-18 14:30:41 +00002456}
2457
Richard Smith7ed1bc92014-12-05 22:42:13 +00002458void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002459 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2460 return;
2461
Richard Smith7ed1bc92014-12-05 22:42:13 +00002462 SmallString<128> Buffer;
2463 llvm::sys::path::append(Buffer, Prefix, Filename);
2464 Filename.assign(Buffer.begin(), Buffer.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00002465}
2466
Richard Smith0f99d6a2015-08-09 08:48:41 +00002467static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2468 switch (ARR) {
2469 case ASTReader::Failure: return true;
2470 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2471 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2472 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2473 case ASTReader::ConfigurationMismatch:
2474 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2475 case ASTReader::HadErrors: return true;
2476 case ASTReader::Success: return false;
2477 }
2478
2479 llvm_unreachable("unknown ASTReadResult");
2480}
2481
Richard Smith0516b182015-09-08 19:40:14 +00002482ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2483 BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2484 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002485 std::string &SuggestedPredefines) {
JF Bastien0e828952019-06-26 19:50:12 +00002486 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2487 // FIXME this drops errors on the floor.
2488 consumeError(std::move(Err));
Richard Smith0516b182015-09-08 19:40:14 +00002489 return Failure;
JF Bastien0e828952019-06-26 19:50:12 +00002490 }
Richard Smith0516b182015-09-08 19:40:14 +00002491
2492 // Read all of the records in the options block.
2493 RecordData Record;
2494 ASTReadResult Result = Success;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002495 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00002496 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2497 if (!MaybeEntry) {
2498 // FIXME this drops errors on the floor.
2499 consumeError(MaybeEntry.takeError());
2500 return Failure;
2501 }
2502 llvm::BitstreamEntry Entry = MaybeEntry.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002503
Richard Smith0516b182015-09-08 19:40:14 +00002504 switch (Entry.Kind) {
2505 case llvm::BitstreamEntry::Error:
2506 case llvm::BitstreamEntry::SubBlock:
2507 return Failure;
2508
2509 case llvm::BitstreamEntry::EndBlock:
2510 return Result;
2511
2512 case llvm::BitstreamEntry::Record:
2513 // The interesting case.
2514 break;
2515 }
2516
2517 // Read and process a record.
2518 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00002519 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2520 if (!MaybeRecordType) {
2521 // FIXME this drops errors on the floor.
2522 consumeError(MaybeRecordType.takeError());
2523 return Failure;
2524 }
2525 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
Richard Smith0516b182015-09-08 19:40:14 +00002526 case LANGUAGE_OPTIONS: {
2527 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2528 if (ParseLanguageOptions(Record, Complain, Listener,
2529 AllowCompatibleConfigurationMismatch))
2530 Result = ConfigurationMismatch;
2531 break;
2532 }
2533
2534 case TARGET_OPTIONS: {
2535 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2536 if (ParseTargetOptions(Record, Complain, Listener,
2537 AllowCompatibleConfigurationMismatch))
2538 Result = ConfigurationMismatch;
2539 break;
2540 }
2541
Richard Smith0516b182015-09-08 19:40:14 +00002542 case FILE_SYSTEM_OPTIONS: {
2543 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2544 if (!AllowCompatibleConfigurationMismatch &&
2545 ParseFileSystemOptions(Record, Complain, Listener))
2546 Result = ConfigurationMismatch;
2547 break;
2548 }
2549
2550 case HEADER_SEARCH_OPTIONS: {
2551 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2552 if (!AllowCompatibleConfigurationMismatch &&
2553 ParseHeaderSearchOptions(Record, Complain, Listener))
2554 Result = ConfigurationMismatch;
2555 break;
2556 }
2557
2558 case PREPROCESSOR_OPTIONS:
2559 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2560 if (!AllowCompatibleConfigurationMismatch &&
2561 ParsePreprocessorOptions(Record, Complain, Listener,
2562 SuggestedPredefines))
2563 Result = ConfigurationMismatch;
2564 break;
2565 }
2566 }
2567}
2568
Guy Benyei11169dd2012-12-18 14:30:41 +00002569ASTReader::ASTReadResult
2570ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002571 SmallVectorImpl<ImportedModule> &Loaded,
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002572 const ModuleFile *ImportedBy,
Guy Benyei11169dd2012-12-18 14:30:41 +00002573 unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002574 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002575
JF Bastien0e828952019-06-26 19:50:12 +00002576 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2577 Error(std::move(Err));
Guy Benyei11169dd2012-12-18 14:30:41 +00002578 return Failure;
2579 }
2580
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002581 // Lambda to read the unhashed control block the first time it's called.
2582 //
2583 // For PCM files, the unhashed control block cannot be read until after the
2584 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
2585 // need to look ahead before reading the IMPORTS record. For consistency,
2586 // this block is always read somehow (see BitstreamEntry::EndBlock).
2587 bool HasReadUnhashedControlBlock = false;
2588 auto readUnhashedControlBlockOnce = [&]() {
2589 if (!HasReadUnhashedControlBlock) {
2590 HasReadUnhashedControlBlock = true;
2591 if (ASTReadResult Result =
2592 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2593 return Result;
2594 }
2595 return Success;
2596 };
2597
Guy Benyei11169dd2012-12-18 14:30:41 +00002598 // Read all of the records and blocks in the control block.
2599 RecordData Record;
Richard Smitha1825302014-10-23 22:18:29 +00002600 unsigned NumInputs = 0;
2601 unsigned NumUserInputs = 0;
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00002602 StringRef BaseDirectoryAsWritten;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002603 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00002604 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2605 if (!MaybeEntry) {
2606 Error(MaybeEntry.takeError());
2607 return Failure;
2608 }
2609 llvm::BitstreamEntry Entry = MaybeEntry.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002610
Chris Lattnere7b154b2013-01-19 21:39:22 +00002611 switch (Entry.Kind) {
2612 case llvm::BitstreamEntry::Error:
2613 Error("malformed block record in AST file");
2614 return Failure;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002615 case llvm::BitstreamEntry::EndBlock: {
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002616 // Validate the module before returning. This call catches an AST with
2617 // no module name and no imports.
2618 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2619 return Result;
2620
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002621 // Validate input files.
2622 const HeaderSearchOptions &HSOpts =
2623 PP.getHeaderSearchInfo().getHeaderSearchOpts();
Ben Langmuircb69b572014-03-07 06:40:32 +00002624
Richard Smitha1825302014-10-23 22:18:29 +00002625 // All user input files reside at the index range [0, NumUserInputs), and
Richard Smith0f99d6a2015-08-09 08:48:41 +00002626 // system input files reside at [NumUserInputs, NumInputs). For explicitly
2627 // loaded module files, ignore missing inputs.
Manman Ren11f2a472016-08-18 17:42:15 +00002628 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2629 F.Kind != MK_PrebuiltModule) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002630 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Ben Langmuircb69b572014-03-07 06:40:32 +00002631
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002632 // If we are reading a module, we will create a verification timestamp,
2633 // so we verify all input files. Otherwise, verify only user input
2634 // files.
Ben Langmuircb69b572014-03-07 06:40:32 +00002635
2636 unsigned N = NumUserInputs;
2637 if (ValidateSystemInputs ||
Richard Smithe842a472014-10-22 02:05:46 +00002638 (HSOpts.ModulesValidateOncePerBuildSession &&
Ben Langmuiracb803e2014-11-10 22:13:10 +00002639 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
Richard Smithe842a472014-10-22 02:05:46 +00002640 F.Kind == MK_ImplicitModule))
Ben Langmuircb69b572014-03-07 06:40:32 +00002641 N = NumInputs;
2642
Ben Langmuir3d4417c2014-02-07 17:31:11 +00002643 for (unsigned I = 0; I < N; ++I) {
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002644 InputFile IF = getInputFile(F, I+1, Complain);
2645 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei11169dd2012-12-18 14:30:41 +00002646 return OutOfDate;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002647 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002648 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002649
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002650 if (Listener)
Richard Smith216a3bd2015-08-13 17:57:10 +00002651 Listener->visitModuleFile(F.FileName, F.Kind);
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002652
Ben Langmuircb69b572014-03-07 06:40:32 +00002653 if (Listener && Listener->needsInputFileVisitation()) {
2654 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2655 : NumUserInputs;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002656 for (unsigned I = 0; I < N; ++I) {
2657 bool IsSystem = I >= NumUserInputs;
2658 InputFileInfo FI = readInputFileInfo(F, I+1);
Richard Smith216a3bd2015-08-13 17:57:10 +00002659 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
Manman Ren11f2a472016-08-18 17:42:15 +00002660 F.Kind == MK_ExplicitModule ||
2661 F.Kind == MK_PrebuiltModule);
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002662 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002663 }
2664
Duncan P. N. Exon Smith69242e92019-11-19 14:44:22 -08002665 return Success;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002666 }
2667
Chris Lattnere7b154b2013-01-19 21:39:22 +00002668 case llvm::BitstreamEntry::SubBlock:
2669 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002670 case INPUT_FILES_BLOCK_ID:
2671 F.InputFilesCursor = Stream;
JF Bastien0e828952019-06-26 19:50:12 +00002672 if (llvm::Error Err = Stream.SkipBlock()) {
2673 Error(std::move(Err));
2674 return Failure;
2675 }
2676 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002677 Error("malformed block record in AST file");
2678 return Failure;
2679 }
2680 continue;
Richard Smith0516b182015-09-08 19:40:14 +00002681
2682 case OPTIONS_BLOCK_ID:
2683 // If we're reading the first module for this group, check its options
2684 // are compatible with ours. For modules it imports, no further checking
2685 // is required, because we checked them when we built it.
2686 if (Listener && !ImportedBy) {
2687 // Should we allow the configuration of the module file to differ from
2688 // the configuration of the current translation unit in a compatible
2689 // way?
2690 //
2691 // FIXME: Allow this for files explicitly specified with -include-pch.
2692 bool AllowCompatibleConfigurationMismatch =
Manman Ren11f2a472016-08-18 17:42:15 +00002693 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
Richard Smith0516b182015-09-08 19:40:14 +00002694
Duncan P. N. Exon Smith69242e92019-11-19 14:44:22 -08002695 ASTReadResult Result =
2696 ReadOptionsBlock(Stream, ClientLoadCapabilities,
2697 AllowCompatibleConfigurationMismatch, *Listener,
2698 SuggestedPredefines);
Richard Smith0516b182015-09-08 19:40:14 +00002699 if (Result == Failure) {
2700 Error("malformed block record in AST file");
2701 return Result;
2702 }
2703
Richard Smith8a308ec2015-11-05 00:54:55 +00002704 if (DisableValidation ||
2705 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2706 Result = Success;
2707
Ben Langmuir9b1e442e2016-02-11 18:54:02 +00002708 // If we can't load the module, exit early since we likely
2709 // will rebuild the module anyway. The stream may be in the
2710 // middle of a block.
2711 if (Result != Success)
Richard Smith0516b182015-09-08 19:40:14 +00002712 return Result;
JF Bastien0e828952019-06-26 19:50:12 +00002713 } else if (llvm::Error Err = Stream.SkipBlock()) {
2714 Error(std::move(Err));
Richard Smith0516b182015-09-08 19:40:14 +00002715 return Failure;
2716 }
2717 continue;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002718
Guy Benyei11169dd2012-12-18 14:30:41 +00002719 default:
JF Bastien0e828952019-06-26 19:50:12 +00002720 if (llvm::Error Err = Stream.SkipBlock()) {
2721 Error(std::move(Err));
Chris Lattnere7b154b2013-01-19 21:39:22 +00002722 return Failure;
2723 }
2724 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00002725 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002726
Chris Lattnere7b154b2013-01-19 21:39:22 +00002727 case llvm::BitstreamEntry::Record:
2728 // The interesting case.
2729 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002730 }
2731
2732 // Read and process a record.
2733 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002734 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00002735 Expected<unsigned> MaybeRecordType =
2736 Stream.readRecord(Entry.ID, Record, &Blob);
2737 if (!MaybeRecordType) {
2738 Error(MaybeRecordType.takeError());
2739 return Failure;
2740 }
2741 switch ((ControlRecordTypes)MaybeRecordType.get()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002742 case METADATA: {
2743 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2744 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002745 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2746 : diag::err_pch_version_too_new);
Guy Benyei11169dd2012-12-18 14:30:41 +00002747 return VersionMismatch;
2748 }
2749
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00002750 bool hasErrors = Record[7];
Guy Benyei11169dd2012-12-18 14:30:41 +00002751 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2752 Diag(diag::err_pch_with_compiler_errors);
2753 return HadErrors;
2754 }
Argyrios Kyrtzidis70ec1c72016-07-13 20:35:26 +00002755 if (hasErrors) {
2756 Diags.ErrorOccurred = true;
2757 Diags.UncompilableErrorOccurred = true;
2758 Diags.UnrecoverableErrorOccurred = true;
2759 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002760
2761 F.RelocatablePCH = Record[4];
Richard Smith7ed1bc92014-12-05 22:42:13 +00002762 // Relative paths in a relocatable PCH are relative to our sysroot.
2763 if (F.RelocatablePCH)
2764 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
Guy Benyei11169dd2012-12-18 14:30:41 +00002765
Richard Smithe75ee0f2015-08-17 07:13:32 +00002766 F.HasTimestamps = Record[5];
2767
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00002768 F.PCHHasObjectFile = Record[6];
2769
Guy Benyei11169dd2012-12-18 14:30:41 +00002770 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002771 StringRef ASTBranch = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002772 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2773 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002774 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
Guy Benyei11169dd2012-12-18 14:30:41 +00002775 return VersionMismatch;
2776 }
2777 break;
2778 }
2779
2780 case IMPORTS: {
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002781 // Validate the AST before processing any imports (otherwise, untangling
2782 // them can be error-prone and expensive). A module will have a name and
2783 // will already have been validated, but this catches the PCH case.
2784 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2785 return Result;
2786
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002787 // Load each of the imported PCH files.
Guy Benyei11169dd2012-12-18 14:30:41 +00002788 unsigned Idx = 0, N = Record.size();
2789 while (Idx < N) {
2790 // Read information about the AST file.
Vedant Kumar48b4f762018-04-14 01:40:48 +00002791 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00002792 // The import location will be the local one for now; we will adjust
2793 // all import locations of module imports after the global source
Richard Smithb22a1d12016-03-27 20:13:24 +00002794 // location info are setup, in ReadAST.
Guy Benyei11169dd2012-12-18 14:30:41 +00002795 SourceLocation ImportLoc =
Richard Smithb22a1d12016-03-27 20:13:24 +00002796 ReadUntranslatedSourceLocation(Record[Idx++]);
Vedant Kumar48b4f762018-04-14 01:40:48 +00002797 off_t StoredSize = (off_t)Record[Idx++];
2798 time_t StoredModTime = (time_t)Record[Idx++];
Daniel Grumberge87e55ed2020-06-07 20:05:25 +01002799 auto FirstSignatureByte = Record.begin() + Idx;
2800 ASTFileSignature StoredSignature = ASTFileSignature::create(
2801 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2802 Idx += ASTFileSignature::size;
Boris Kolpackovd30446f2017-08-31 06:26:43 +00002803
2804 std::string ImportedName = ReadString(Record, Idx);
2805 std::string ImportedFile;
2806
2807 // For prebuilt and explicit modules first consult the file map for
2808 // an override. Note that here we don't search prebuilt module
2809 // directories, only the explicit name to file mappings. Also, we will
2810 // still verify the size/signature making sure it is essentially the
2811 // same file but perhaps in a different location.
2812 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2813 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2814 ImportedName, /*FileMapOnly*/ true);
2815
2816 if (ImportedFile.empty())
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00002817 // Use BaseDirectoryAsWritten to ensure we use the same path in the
2818 // ModuleCache as when writing.
2819 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
Boris Kolpackovd30446f2017-08-31 06:26:43 +00002820 else
2821 SkipPath(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00002822
Richard Smith0f99d6a2015-08-09 08:48:41 +00002823 // If our client can't cope with us being out of date, we can't cope with
2824 // our dependency being missing.
2825 unsigned Capabilities = ClientLoadCapabilities;
2826 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2827 Capabilities &= ~ARR_Missing;
2828
Guy Benyei11169dd2012-12-18 14:30:41 +00002829 // Load the AST file.
Richard Smith0f99d6a2015-08-09 08:48:41 +00002830 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2831 Loaded, StoredSize, StoredModTime,
2832 StoredSignature, Capabilities);
2833
2834 // If we diagnosed a problem, produce a backtrace.
2835 if (isDiagnosedResult(Result, Capabilities))
2836 Diag(diag::note_module_file_imported_by)
2837 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2838
2839 switch (Result) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002840 case Failure: return Failure;
2841 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor2f1806e2013-03-19 00:38:50 +00002842 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00002843 case OutOfDate: return OutOfDate;
2844 case VersionMismatch: return VersionMismatch;
2845 case ConfigurationMismatch: return ConfigurationMismatch;
2846 case HadErrors: return HadErrors;
2847 case Success: break;
2848 }
2849 }
2850 break;
2851 }
2852
Guy Benyei11169dd2012-12-18 14:30:41 +00002853 case ORIGINAL_FILE:
2854 F.OriginalSourceFileID = FileID::get(Record[0]);
Benjamin Krameradcd0262020-01-28 20:23:46 +01002855 F.ActualOriginalSourceFileName = std::string(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00002856 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
Richard Smith7ed1bc92014-12-05 22:42:13 +00002857 ResolveImportedPath(F, F.OriginalSourceFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002858 break;
2859
2860 case ORIGINAL_FILE_ID:
2861 F.OriginalSourceFileID = FileID::get(Record[0]);
2862 break;
2863
2864 case ORIGINAL_PCH_DIR:
Benjamin Krameradcd0262020-01-28 20:23:46 +01002865 F.OriginalDir = std::string(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00002866 break;
2867
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002868 case MODULE_NAME:
Benjamin Krameradcd0262020-01-28 20:23:46 +01002869 F.ModuleName = std::string(Blob);
Duncan P. N. Exon Smith9dda8f52019-03-06 02:50:46 +00002870 Diag(diag::remark_module_import)
2871 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2872 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
Ben Langmuir4f5212a2014-04-14 22:12:44 +00002873 if (Listener)
2874 Listener->ReadModuleName(F.ModuleName);
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00002875
2876 // Validate the AST as soon as we have a name so we can exit early on
2877 // failure.
2878 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2879 return Result;
Vedant Kumar48b4f762018-04-14 01:40:48 +00002880
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002881 break;
2882
Richard Smith223d3f22014-12-06 03:21:08 +00002883 case MODULE_DIRECTORY: {
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00002884 // Save the BaseDirectory as written in the PCM for computing the module
2885 // filename for the ModuleCache.
2886 BaseDirectoryAsWritten = Blob;
Richard Smith223d3f22014-12-06 03:21:08 +00002887 assert(!F.ModuleName.empty() &&
2888 "MODULE_DIRECTORY found before MODULE_NAME");
2889 // If we've already loaded a module map file covering this module, we may
2890 // have a better path for it (relative to the current build).
Bruno Cardoso Lopes52431f32018-07-18 23:21:19 +00002891 Module *M = PP.getHeaderSearchInfo().lookupModule(
2892 F.ModuleName, /*AllowSearch*/ true,
2893 /*AllowExtraModuleMapSearch*/ true);
Richard Smith223d3f22014-12-06 03:21:08 +00002894 if (M && M->Directory) {
2895 // If we're implicitly loading a module, the base directory can't
2896 // change between the build and use.
Yuka Takahashid8baec22018-08-01 09:50:02 +00002897 // Don't emit module relocation error if we have -fno-validate-pch
2898 if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2899 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
Harlan Haskins8d323d12019-08-01 21:31:56 +00002900 auto BuildDir = PP.getFileManager().getDirectory(Blob);
2901 if (!BuildDir || *BuildDir != M->Directory) {
Richard Smith223d3f22014-12-06 03:21:08 +00002902 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2903 Diag(diag::err_imported_module_relocated)
2904 << F.ModuleName << Blob << M->Directory->getName();
2905 return OutOfDate;
2906 }
2907 }
Benjamin Krameradcd0262020-01-28 20:23:46 +01002908 F.BaseDirectory = std::string(M->Directory->getName());
Richard Smith223d3f22014-12-06 03:21:08 +00002909 } else {
Benjamin Krameradcd0262020-01-28 20:23:46 +01002910 F.BaseDirectory = std::string(Blob);
Richard Smith223d3f22014-12-06 03:21:08 +00002911 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002912 break;
Richard Smith223d3f22014-12-06 03:21:08 +00002913 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002914
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002915 case MODULE_MAP_FILE:
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00002916 if (ASTReadResult Result =
2917 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2918 return Result;
Ben Langmuir264ea152014-11-08 00:06:39 +00002919 break;
2920
Justin Bognerca9c0cc2015-06-21 20:32:36 +00002921 case INPUT_FILE_OFFSETS:
Richard Smitha1825302014-10-23 22:18:29 +00002922 NumInputs = Record[0];
2923 NumUserInputs = Record[1];
Justin Bogner4c183242015-06-21 20:32:40 +00002924 F.InputFileOffsets =
2925 (const llvm::support::unaligned_uint64_t *)Blob.data();
Richard Smitha1825302014-10-23 22:18:29 +00002926 F.InputFilesLoaded.resize(NumInputs);
Argyrios Kyrtzidisa38cb202017-01-30 06:05:58 +00002927 F.NumUserInputFiles = NumUserInputs;
Guy Benyei11169dd2012-12-18 14:30:41 +00002928 break;
2929 }
2930 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002931}
2932
Ben Langmuir2c9af442014-04-10 17:57:43 +00002933ASTReader::ASTReadResult
2934ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002935 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002936
JF Bastien0e828952019-06-26 19:50:12 +00002937 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2938 Error(std::move(Err));
Ben Langmuir2c9af442014-04-10 17:57:43 +00002939 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002940 }
Daniel Grumbergbb8c7e72020-05-13 17:07:47 +01002941 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
Guy Benyei11169dd2012-12-18 14:30:41 +00002942
2943 // Read all of the records and blocks for the AST file.
2944 RecordData Record;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002945 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00002946 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2947 if (!MaybeEntry) {
2948 Error(MaybeEntry.takeError());
2949 return Failure;
2950 }
2951 llvm::BitstreamEntry Entry = MaybeEntry.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002952
Chris Lattnere7b154b2013-01-19 21:39:22 +00002953 switch (Entry.Kind) {
2954 case llvm::BitstreamEntry::Error:
2955 Error("error at end of module block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002956 return Failure;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00002957 case llvm::BitstreamEntry::EndBlock:
Richard Smithc0fbba72013-04-03 22:49:41 +00002958 // Outside of C++, we do not store a lookup map for the translation unit.
2959 // Instead, mark it as needing a lookup map to be built if this module
2960 // contains any declarations lexically within it (which it always does!).
2961 // This usually has no cost, since we very rarely need the lookup map for
2962 // the translation unit outside C++.
Richard Smithdbafb6c2017-06-29 23:23:46 +00002963 if (ASTContext *Ctx = ContextObj) {
2964 DeclContext *DC = Ctx->getTranslationUnitDecl();
2965 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2966 DC->setMustBuildLookupTable();
2967 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002968
Ben Langmuir2c9af442014-04-10 17:57:43 +00002969 return Success;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002970 case llvm::BitstreamEntry::SubBlock:
2971 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002972 case DECLTYPES_BLOCK_ID:
2973 // We lazily load the decls block, but we want to set up the
2974 // DeclsCursor cursor to point into it. Clone our current bitcode
2975 // cursor to it, enter the block and read the abbrevs in that block.
2976 // With the main cursor, we just skip over it.
2977 F.DeclsCursor = Stream;
JF Bastien0e828952019-06-26 19:50:12 +00002978 if (llvm::Error Err = Stream.SkipBlock()) {
2979 Error(std::move(Err));
2980 return Failure;
2981 }
Daniel Grumbergbb8c7e72020-05-13 17:07:47 +01002982 if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID,
2983 &F.DeclsBlockStartOffset)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002984 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002985 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002986 }
2987 break;
Richard Smithb9eab6d2014-03-20 19:44:17 +00002988
Guy Benyei11169dd2012-12-18 14:30:41 +00002989 case PREPROCESSOR_BLOCK_ID:
2990 F.MacroCursor = Stream;
2991 if (!PP.getExternalSource())
2992 PP.setExternalSource(this);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002993
JF Bastien0e828952019-06-26 19:50:12 +00002994 if (llvm::Error Err = Stream.SkipBlock()) {
2995 Error(std::move(Err));
2996 return Failure;
2997 }
2998 if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002999 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003000 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003001 }
3002 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3003 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003004
Guy Benyei11169dd2012-12-18 14:30:41 +00003005 case PREPROCESSOR_DETAIL_BLOCK_ID:
3006 F.PreprocessorDetailCursor = Stream;
JF Bastien0e828952019-06-26 19:50:12 +00003007
3008 if (llvm::Error Err = Stream.SkipBlock()) {
3009 Error(std::move(Err));
3010 return Failure;
3011 }
3012 if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00003013 PREPROCESSOR_DETAIL_BLOCK_ID)) {
JF Bastien0e828952019-06-26 19:50:12 +00003014 Error("malformed preprocessor detail record in AST file");
3015 return Failure;
3016 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003017 F.PreprocessorDetailStartOffset
Chris Lattnere7b154b2013-01-19 21:39:22 +00003018 = F.PreprocessorDetailCursor.GetCurrentBitNo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003019
Guy Benyei11169dd2012-12-18 14:30:41 +00003020 if (!PP.getPreprocessingRecord())
3021 PP.createPreprocessingRecord();
3022 if (!PP.getPreprocessingRecord()->getExternalSource())
3023 PP.getPreprocessingRecord()->SetExternalSource(*this);
3024 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003025
Guy Benyei11169dd2012-12-18 14:30:41 +00003026 case SOURCE_MANAGER_BLOCK_ID:
3027 if (ReadSourceManagerBlock(F))
Ben Langmuir2c9af442014-04-10 17:57:43 +00003028 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003029 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003030
Guy Benyei11169dd2012-12-18 14:30:41 +00003031 case SUBMODULE_BLOCK_ID:
David Blaikie9ffe5a32017-01-30 05:00:26 +00003032 if (ASTReadResult Result =
3033 ReadSubmoduleBlock(F, ClientLoadCapabilities))
Ben Langmuir2c9af442014-04-10 17:57:43 +00003034 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00003035 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003036
Guy Benyei11169dd2012-12-18 14:30:41 +00003037 case COMMENTS_BLOCK_ID: {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003038 BitstreamCursor C = Stream;
JF Bastien0e828952019-06-26 19:50:12 +00003039
3040 if (llvm::Error Err = Stream.SkipBlock()) {
3041 Error(std::move(Err));
3042 return Failure;
3043 }
3044 if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003045 Error("malformed comments block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003046 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003047 }
3048 CommentsCursors.push_back(std::make_pair(C, &F));
3049 break;
3050 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003051
Guy Benyei11169dd2012-12-18 14:30:41 +00003052 default:
JF Bastien0e828952019-06-26 19:50:12 +00003053 if (llvm::Error Err = Stream.SkipBlock()) {
3054 Error(std::move(Err));
Ben Langmuir2c9af442014-04-10 17:57:43 +00003055 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00003056 }
3057 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003058 }
3059 continue;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003060
Chris Lattnere7b154b2013-01-19 21:39:22 +00003061 case llvm::BitstreamEntry::Record:
3062 // The interesting case.
3063 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003064 }
3065
3066 // Read and process a record.
3067 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00003068 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00003069 Expected<unsigned> MaybeRecordType =
3070 Stream.readRecord(Entry.ID, Record, &Blob);
3071 if (!MaybeRecordType) {
3072 Error(MaybeRecordType.takeError());
3073 return Failure;
3074 }
3075 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
Richard Smithdbafb6c2017-06-29 23:23:46 +00003076
3077 // If we're not loading an AST context, we don't care about most records.
3078 if (!ContextObj) {
3079 switch (RecordType) {
3080 case IDENTIFIER_TABLE:
3081 case IDENTIFIER_OFFSET:
3082 case INTERESTING_IDENTIFIERS:
3083 case STATISTICS:
3084 case PP_CONDITIONAL_STACK:
3085 case PP_COUNTER_VALUE:
3086 case SOURCE_LOCATION_OFFSETS:
3087 case MODULE_OFFSET_MAP:
3088 case SOURCE_MANAGER_LINE_TABLE:
3089 case SOURCE_LOCATION_PRELOADS:
3090 case PPD_ENTITIES_OFFSETS:
3091 case HEADER_SEARCH_TABLE:
3092 case IMPORTED_MODULES:
3093 case MACRO_OFFSET:
3094 break;
3095 default:
3096 continue;
3097 }
3098 }
3099
3100 switch (RecordType) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003101 default: // Default behavior: ignore.
3102 break;
3103
3104 case TYPE_OFFSET: {
3105 if (F.LocalNumTypes != 0) {
3106 Error("duplicate TYPE_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003107 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003108 }
Dmitry Polukhina7afb212020-04-16 09:24:46 -07003109 F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
Guy Benyei11169dd2012-12-18 14:30:41 +00003110 F.LocalNumTypes = Record[0];
3111 unsigned LocalBaseTypeIndex = Record[1];
3112 F.BaseTypeIndex = getTotalNumTypes();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003113
Guy Benyei11169dd2012-12-18 14:30:41 +00003114 if (F.LocalNumTypes > 0) {
3115 // Introduce the global -> local mapping for types within this module.
3116 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003117
Guy Benyei11169dd2012-12-18 14:30:41 +00003118 // Introduce the local -> global mapping for types within this module.
3119 F.TypeRemap.insertOrReplace(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003120 std::make_pair(LocalBaseTypeIndex,
Guy Benyei11169dd2012-12-18 14:30:41 +00003121 F.BaseTypeIndex - LocalBaseTypeIndex));
Ben Langmuir52ca6782014-10-20 16:27:32 +00003122
3123 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
Guy Benyei11169dd2012-12-18 14:30:41 +00003124 }
3125 break;
3126 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003127
Guy Benyei11169dd2012-12-18 14:30:41 +00003128 case DECL_OFFSET: {
3129 if (F.LocalNumDecls != 0) {
3130 Error("duplicate DECL_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003131 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003132 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003133 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003134 F.LocalNumDecls = Record[0];
3135 unsigned LocalBaseDeclID = Record[1];
3136 F.BaseDeclID = getTotalNumDecls();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003137
Guy Benyei11169dd2012-12-18 14:30:41 +00003138 if (F.LocalNumDecls > 0) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003139 // Introduce the global -> local mapping for declarations within this
Guy Benyei11169dd2012-12-18 14:30:41 +00003140 // module.
3141 GlobalDeclMap.insert(
3142 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003143
Guy Benyei11169dd2012-12-18 14:30:41 +00003144 // Introduce the local -> global mapping for declarations within this
3145 // module.
3146 F.DeclRemap.insertOrReplace(
3147 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003148
Guy Benyei11169dd2012-12-18 14:30:41 +00003149 // Introduce the global -> local mapping for declarations within this
3150 // module.
3151 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
Ben Langmuirfe971d92014-08-16 04:54:18 +00003152
Ben Langmuir52ca6782014-10-20 16:27:32 +00003153 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3154 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003155 break;
3156 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003157
Guy Benyei11169dd2012-12-18 14:30:41 +00003158 case TU_UPDATE_LEXICAL: {
Richard Smithdbafb6c2017-06-29 23:23:46 +00003159 DeclContext *TU = ContextObj->getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00003160 LexicalContents Contents(
3161 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3162 Blob.data()),
3163 static_cast<unsigned int>(Blob.size() / 4));
3164 TULexicalDecls.push_back(std::make_pair(&F, Contents));
Guy Benyei11169dd2012-12-18 14:30:41 +00003165 TU->setHasExternalLexicalStorage(true);
3166 break;
3167 }
3168
3169 case UPDATE_VISIBLE: {
3170 unsigned Idx = 0;
3171 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003172 auto *Data = (const unsigned char*)Blob.data();
Richard Smithd88a7f12015-09-01 20:35:42 +00003173 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
Richard Smith0f4e2c42015-08-06 04:23:48 +00003174 // If we've already loaded the decl, perform the updates when we finish
3175 // loading this block.
3176 if (Decl *D = GetExistingDecl(ID))
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003177 PendingUpdateRecords.push_back(
3178 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
Guy Benyei11169dd2012-12-18 14:30:41 +00003179 break;
3180 }
3181
3182 case IDENTIFIER_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00003183 F.IdentifierTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003184 if (Record[0]) {
Justin Bognerda4e6502014-04-14 16:34:29 +00003185 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3186 (const unsigned char *)F.IdentifierTableData + Record[0],
3187 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3188 (const unsigned char *)F.IdentifierTableData,
3189 ASTIdentifierLookupTrait(*this, F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003190
Guy Benyei11169dd2012-12-18 14:30:41 +00003191 PP.getIdentifierTable().setExternalIdentifierLookup(this);
3192 }
3193 break;
3194
3195 case IDENTIFIER_OFFSET: {
3196 if (F.LocalNumIdentifiers != 0) {
3197 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003198 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003199 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003200 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003201 F.LocalNumIdentifiers = Record[0];
3202 unsigned LocalBaseIdentifierID = Record[1];
3203 F.BaseIdentifierID = getTotalNumIdentifiers();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003204
Guy Benyei11169dd2012-12-18 14:30:41 +00003205 if (F.LocalNumIdentifiers > 0) {
3206 // Introduce the global -> local mapping for identifiers within this
3207 // module.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003208 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
Guy Benyei11169dd2012-12-18 14:30:41 +00003209 &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003210
Guy Benyei11169dd2012-12-18 14:30:41 +00003211 // Introduce the local -> global mapping for identifiers within this
3212 // module.
3213 F.IdentifierRemap.insertOrReplace(
3214 std::make_pair(LocalBaseIdentifierID,
3215 F.BaseIdentifierID - LocalBaseIdentifierID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00003216
Ben Langmuir52ca6782014-10-20 16:27:32 +00003217 IdentifiersLoaded.resize(IdentifiersLoaded.size()
3218 + F.LocalNumIdentifiers);
3219 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003220 break;
3221 }
3222
Richard Smith33e0f7e2015-07-22 02:08:40 +00003223 case INTERESTING_IDENTIFIERS:
3224 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3225 break;
3226
Ben Langmuir332aafe2014-01-31 01:06:56 +00003227 case EAGERLY_DESERIALIZED_DECLS:
Richard Smith9e2341d2015-03-23 03:25:59 +00003228 // FIXME: Skip reading this record if our ASTConsumer doesn't care
3229 // about "interesting" decls (for instance, if we're building a module).
Guy Benyei11169dd2012-12-18 14:30:41 +00003230 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Ben Langmuir332aafe2014-01-31 01:06:56 +00003231 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00003232 break;
3233
David Blaikie9ffe5a32017-01-30 05:00:26 +00003234 case MODULAR_CODEGEN_DECLS:
3235 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3236 // them (ie: if we're not codegenerating this module).
Hans Wennborg7ea9a6e2020-02-27 14:33:43 +01003237 if (F.Kind == MK_MainFile)
David Blaikie9ffe5a32017-01-30 05:00:26 +00003238 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3239 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3240 break;
3241
Guy Benyei11169dd2012-12-18 14:30:41 +00003242 case SPECIAL_TYPES:
Douglas Gregor44180f82013-02-01 23:45:03 +00003243 if (SpecialTypes.empty()) {
3244 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3245 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3246 break;
3247 }
3248
3249 if (SpecialTypes.size() != Record.size()) {
3250 Error("invalid special-types record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003251 return Failure;
Douglas Gregor44180f82013-02-01 23:45:03 +00003252 }
3253
3254 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3255 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3256 if (!SpecialTypes[I])
3257 SpecialTypes[I] = ID;
3258 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3259 // merge step?
3260 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003261 break;
3262
3263 case STATISTICS:
3264 TotalNumStatements += Record[0];
3265 TotalNumMacros += Record[1];
3266 TotalLexicalDeclContexts += Record[2];
3267 TotalVisibleDeclContexts += Record[3];
3268 break;
3269
3270 case UNUSED_FILESCOPED_DECLS:
3271 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3272 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3273 break;
3274
3275 case DELEGATING_CTORS:
3276 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3277 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3278 break;
3279
3280 case WEAK_UNDECLARED_IDENTIFIERS:
3281 if (Record.size() % 4 != 0) {
3282 Error("invalid weak identifiers record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003283 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003284 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003285
3286 // FIXME: Ignore weak undeclared identifiers from non-original PCH
Guy Benyei11169dd2012-12-18 14:30:41 +00003287 // files. This isn't the way to do it :)
3288 WeakUndeclaredIdentifiers.clear();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003289
Guy Benyei11169dd2012-12-18 14:30:41 +00003290 // Translate the weak, undeclared identifiers into global IDs.
3291 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3292 WeakUndeclaredIdentifiers.push_back(
3293 getGlobalIdentifierID(F, Record[I++]));
3294 WeakUndeclaredIdentifiers.push_back(
3295 getGlobalIdentifierID(F, Record[I++]));
3296 WeakUndeclaredIdentifiers.push_back(
3297 ReadSourceLocation(F, Record, I).getRawEncoding());
3298 WeakUndeclaredIdentifiers.push_back(Record[I++]);
3299 }
3300 break;
3301
Guy Benyei11169dd2012-12-18 14:30:41 +00003302 case SELECTOR_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003303 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003304 F.LocalNumSelectors = Record[0];
3305 unsigned LocalBaseSelectorID = Record[1];
3306 F.BaseSelectorID = getTotalNumSelectors();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003307
Guy Benyei11169dd2012-12-18 14:30:41 +00003308 if (F.LocalNumSelectors > 0) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003309 // Introduce the global -> local mapping for selectors within this
Guy Benyei11169dd2012-12-18 14:30:41 +00003310 // module.
3311 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003312
3313 // Introduce the local -> global mapping for selectors within this
Guy Benyei11169dd2012-12-18 14:30:41 +00003314 // module.
3315 F.SelectorRemap.insertOrReplace(
3316 std::make_pair(LocalBaseSelectorID,
3317 F.BaseSelectorID - LocalBaseSelectorID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00003318
3319 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
Guy Benyei11169dd2012-12-18 14:30:41 +00003320 }
3321 break;
3322 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003323
Guy Benyei11169dd2012-12-18 14:30:41 +00003324 case METHOD_POOL:
Chris Lattner0e6c9402013-01-20 02:38:54 +00003325 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003326 if (Record[0])
3327 F.SelectorLookupTable
3328 = ASTSelectorLookupTable::Create(
3329 F.SelectorLookupTableData + Record[0],
3330 F.SelectorLookupTableData,
3331 ASTSelectorLookupTrait(*this, F));
3332 TotalNumMethodPoolEntries += Record[1];
3333 break;
3334
3335 case REFERENCED_SELECTOR_POOL:
3336 if (!Record.empty()) {
3337 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003338 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
Guy Benyei11169dd2012-12-18 14:30:41 +00003339 Record[Idx++]));
3340 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3341 getRawEncoding());
3342 }
3343 }
3344 break;
3345
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00003346 case PP_CONDITIONAL_STACK:
3347 if (!Record.empty()) {
Erik Verbruggen4d1eb2d2017-11-03 09:40:07 +00003348 unsigned Idx = 0, End = Record.size() - 1;
3349 bool ReachedEOFWhileSkipping = Record[Idx++];
3350 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3351 if (ReachedEOFWhileSkipping) {
3352 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3353 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3354 bool FoundNonSkipPortion = Record[Idx++];
3355 bool FoundElse = Record[Idx++];
3356 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3357 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3358 FoundElse, ElseLoc);
3359 }
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00003360 SmallVector<PPConditionalInfo, 4> ConditionalStack;
Erik Verbruggen4d1eb2d2017-11-03 09:40:07 +00003361 while (Idx < End) {
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00003362 auto Loc = ReadSourceLocation(F, Record, Idx);
3363 bool WasSkipping = Record[Idx++];
3364 bool FoundNonSkip = Record[Idx++];
3365 bool FoundElse = Record[Idx++];
3366 ConditionalStack.push_back(
3367 {Loc, WasSkipping, FoundNonSkip, FoundElse});
3368 }
Erik Verbruggen4d1eb2d2017-11-03 09:40:07 +00003369 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00003370 }
3371 break;
3372
Guy Benyei11169dd2012-12-18 14:30:41 +00003373 case PP_COUNTER_VALUE:
3374 if (!Record.empty() && Listener)
3375 Listener->ReadCounter(F, Record[0]);
3376 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003377
Guy Benyei11169dd2012-12-18 14:30:41 +00003378 case FILE_SORTED_DECLS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00003379 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003380 F.NumFileSortedDecls = Record[0];
3381 break;
3382
3383 case SOURCE_LOCATION_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003384 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003385 F.LocalNumSLocEntries = Record[0];
3386 unsigned SLocSpaceSize = Record[1];
Daniel Grumbergbb8c7e72020-05-13 17:07:47 +01003387 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00003388 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Ben Langmuir52ca6782014-10-20 16:27:32 +00003389 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
Guy Benyei11169dd2012-12-18 14:30:41 +00003390 SLocSpaceSize);
Richard Smith78d81ec2015-08-12 22:25:24 +00003391 if (!F.SLocEntryBaseID) {
3392 Error("ran out of source locations");
3393 break;
3394 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003395 // Make our entry in the range map. BaseID is negative and growing, so
3396 // we invert it. Because we invert it, though, we need the other end of
3397 // the range.
3398 unsigned RangeStart =
3399 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3400 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3401 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3402
3403 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3404 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3405 GlobalSLocOffsetMap.insert(
3406 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3407 - SLocSpaceSize,&F));
3408
3409 // Initialize the remapping table.
3410 // Invalid stays invalid.
Richard Smithb9eab6d2014-03-20 19:44:17 +00003411 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
Guy Benyei11169dd2012-12-18 14:30:41 +00003412 // This module. Base was 2 when being compiled.
Richard Smithb9eab6d2014-03-20 19:44:17 +00003413 F.SLocRemap.insertOrReplace(std::make_pair(2U,
Guy Benyei11169dd2012-12-18 14:30:41 +00003414 static_cast<int>(F.SLocEntryBaseOffset - 2)));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003415
Guy Benyei11169dd2012-12-18 14:30:41 +00003416 TotalNumSLocEntries += F.LocalNumSLocEntries;
3417 break;
3418 }
3419
Richard Smith37a93df2017-02-18 00:32:02 +00003420 case MODULE_OFFSET_MAP:
3421 F.ModuleOffsetMap = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00003422 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003423
3424 case SOURCE_MANAGER_LINE_TABLE:
Duncan P. N. Exon Smith8e2c1922019-11-10 11:07:20 -08003425 if (ParseLineTable(F, Record)) {
3426 Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003427 return Failure;
Duncan P. N. Exon Smith8e2c1922019-11-10 11:07:20 -08003428 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003429 break;
3430
3431 case SOURCE_LOCATION_PRELOADS: {
3432 // Need to transform from the local view (1-based IDs) to the global view,
3433 // which is based off F.SLocEntryBaseID.
3434 if (!F.PreloadSLocEntries.empty()) {
3435 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003436 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003437 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003438
Guy Benyei11169dd2012-12-18 14:30:41 +00003439 F.PreloadSLocEntries.swap(Record);
3440 break;
3441 }
3442
3443 case EXT_VECTOR_DECLS:
3444 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3445 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3446 break;
3447
3448 case VTABLE_USES:
3449 if (Record.size() % 3 != 0) {
3450 Error("Invalid VTABLE_USES record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003451 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003452 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003453
Guy Benyei11169dd2012-12-18 14:30:41 +00003454 // Later tables overwrite earlier ones.
3455 // FIXME: Modules will have some trouble with this. This is clearly not
3456 // the right way to do this.
3457 VTableUses.clear();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003458
Guy Benyei11169dd2012-12-18 14:30:41 +00003459 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3460 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3461 VTableUses.push_back(
3462 ReadSourceLocation(F, Record, Idx).getRawEncoding());
3463 VTableUses.push_back(Record[Idx++]);
3464 }
3465 break;
3466
Guy Benyei11169dd2012-12-18 14:30:41 +00003467 case PENDING_IMPLICIT_INSTANTIATIONS:
3468 if (PendingInstantiations.size() % 2 != 0) {
3469 Error("Invalid existing PendingInstantiations");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003470 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003471 }
3472
3473 if (Record.size() % 2 != 0) {
3474 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003475 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003476 }
3477
3478 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3479 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3480 PendingInstantiations.push_back(
3481 ReadSourceLocation(F, Record, I).getRawEncoding());
3482 }
3483 break;
3484
3485 case SEMA_DECL_REFS:
Richard Smith96269c52016-09-29 22:49:46 +00003486 if (Record.size() != 3) {
Richard Smith3d8e97e2013-10-18 06:54:39 +00003487 Error("Invalid SEMA_DECL_REFS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003488 return Failure;
Richard Smith3d8e97e2013-10-18 06:54:39 +00003489 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003490 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3491 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3492 break;
3493
3494 case PPD_ENTITIES_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003495 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3496 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3497 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00003498
3499 unsigned LocalBasePreprocessedEntityID = Record[0];
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003500
Guy Benyei11169dd2012-12-18 14:30:41 +00003501 unsigned StartingID;
3502 if (!PP.getPreprocessingRecord())
3503 PP.createPreprocessingRecord();
3504 if (!PP.getPreprocessingRecord()->getExternalSource())
3505 PP.getPreprocessingRecord()->SetExternalSource(*this);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003506 StartingID
Guy Benyei11169dd2012-12-18 14:30:41 +00003507 = PP.getPreprocessingRecord()
Ben Langmuir52ca6782014-10-20 16:27:32 +00003508 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Guy Benyei11169dd2012-12-18 14:30:41 +00003509 F.BasePreprocessedEntityID = StartingID;
3510
3511 if (F.NumPreprocessedEntities > 0) {
3512 // Introduce the global -> local mapping for preprocessed entities in
3513 // this module.
3514 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003515
Guy Benyei11169dd2012-12-18 14:30:41 +00003516 // Introduce the local -> global mapping for preprocessed entities in
3517 // this module.
3518 F.PreprocessedEntityRemap.insertOrReplace(
3519 std::make_pair(LocalBasePreprocessedEntityID,
3520 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3521 }
3522
3523 break;
3524 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003525
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00003526 case PPD_SKIPPED_RANGES: {
3527 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3528 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3529 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3530
3531 if (!PP.getPreprocessingRecord())
3532 PP.createPreprocessingRecord();
3533 if (!PP.getPreprocessingRecord()->getExternalSource())
3534 PP.getPreprocessingRecord()->SetExternalSource(*this);
3535 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3536 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
Jonas Devlieghere560ce2c2018-02-26 15:16:42 +00003537
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00003538 if (F.NumPreprocessedSkippedRanges > 0)
3539 GlobalSkippedRangeMap.insert(
3540 std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3541 break;
3542 }
3543
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003544 case DECL_UPDATE_OFFSETS:
Guy Benyei11169dd2012-12-18 14:30:41 +00003545 if (Record.size() % 2 != 0) {
3546 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003547 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003548 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003549 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3550 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3551 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3552
3553 // If we've already loaded the decl, perform the updates when we finish
3554 // loading this block.
3555 if (Decl *D = GetExistingDecl(ID))
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003556 PendingUpdateRecords.push_back(
3557 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
Richard Smithcd45dbc2014-04-19 03:48:30 +00003558 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003559 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003560
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003561 case OBJC_CATEGORIES_MAP:
Guy Benyei11169dd2012-12-18 14:30:41 +00003562 if (F.LocalNumObjCCategoriesInMap != 0) {
3563 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003564 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003565 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003566
Guy Benyei11169dd2012-12-18 14:30:41 +00003567 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003568 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003569 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003570
Guy Benyei11169dd2012-12-18 14:30:41 +00003571 case OBJC_CATEGORIES:
3572 F.ObjCCategories.swap(Record);
3573 break;
Richard Smithc2bb8182015-03-24 06:36:48 +00003574
Guy Benyei11169dd2012-12-18 14:30:41 +00003575 case CUDA_SPECIAL_DECL_REFS:
3576 // Later tables overwrite earlier ones.
3577 // FIXME: Modules will have trouble with this.
3578 CUDASpecialDeclRefs.clear();
3579 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3580 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3581 break;
3582
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003583 case HEADER_SEARCH_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00003584 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003585 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00003586 if (Record[0]) {
3587 F.HeaderFileInfoTable
3588 = HeaderFileInfoLookupTable::Create(
3589 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3590 (const unsigned char *)F.HeaderFileInfoTableData,
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003591 HeaderFileInfoTrait(*this, F,
Guy Benyei11169dd2012-12-18 14:30:41 +00003592 &PP.getHeaderSearchInfo(),
Chris Lattner0e6c9402013-01-20 02:38:54 +00003593 Blob.data() + Record[2]));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003594
Guy Benyei11169dd2012-12-18 14:30:41 +00003595 PP.getHeaderSearchInfo().SetExternalSource(this);
3596 if (!PP.getHeaderSearchInfo().getExternalLookup())
3597 PP.getHeaderSearchInfo().SetExternalLookup(this);
3598 }
3599 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003600
Guy Benyei11169dd2012-12-18 14:30:41 +00003601 case FP_PRAGMA_OPTIONS:
3602 // Later tables overwrite earlier ones.
3603 FPPragmaOptions.swap(Record);
3604 break;
3605
3606 case OPENCL_EXTENSIONS:
Yaxun Liu5b746652016-12-18 05:18:55 +00003607 for (unsigned I = 0, E = Record.size(); I != E; ) {
3608 auto Name = ReadString(Record, I);
3609 auto &Opt = OpenCLExtensions.OptMap[Name];
Yaxun Liucc2741c2016-12-18 06:35:06 +00003610 Opt.Supported = Record[I++] != 0;
3611 Opt.Enabled = Record[I++] != 0;
Yaxun Liu5b746652016-12-18 05:18:55 +00003612 Opt.Avail = Record[I++];
3613 Opt.Core = Record[I++];
3614 }
3615 break;
3616
3617 case OPENCL_EXTENSION_TYPES:
3618 for (unsigned I = 0, E = Record.size(); I != E;) {
3619 auto TypeID = static_cast<::TypeID>(Record[I++]);
3620 auto *Type = GetType(TypeID).getTypePtr();
3621 auto NumExt = static_cast<unsigned>(Record[I++]);
3622 for (unsigned II = 0; II != NumExt; ++II) {
3623 auto Ext = ReadString(Record, I);
3624 OpenCLTypeExtMap[Type].insert(Ext);
3625 }
3626 }
3627 break;
3628
3629 case OPENCL_EXTENSION_DECLS:
3630 for (unsigned I = 0, E = Record.size(); I != E;) {
3631 auto DeclID = static_cast<::DeclID>(Record[I++]);
3632 auto *Decl = GetDecl(DeclID);
3633 auto NumExt = static_cast<unsigned>(Record[I++]);
3634 for (unsigned II = 0; II != NumExt; ++II) {
3635 auto Ext = ReadString(Record, I);
3636 OpenCLDeclExtMap[Decl].insert(Ext);
3637 }
3638 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003639 break;
3640
3641 case TENTATIVE_DEFINITIONS:
3642 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3643 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3644 break;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003645
Guy Benyei11169dd2012-12-18 14:30:41 +00003646 case KNOWN_NAMESPACES:
3647 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3648 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3649 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003650
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003651 case UNDEFINED_BUT_USED:
3652 if (UndefinedButUsed.size() % 2 != 0) {
3653 Error("Invalid existing UndefinedButUsed");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003654 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003655 }
3656
3657 if (Record.size() % 2 != 0) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003658 Error("invalid undefined-but-used record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003659 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003660 }
3661 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003662 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3663 UndefinedButUsed.push_back(
Nick Lewycky8334af82013-01-26 00:35:08 +00003664 ReadSourceLocation(F, Record, I).getRawEncoding());
3665 }
3666 break;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003667
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00003668 case DELETE_EXPRS_TO_ANALYZE:
3669 for (unsigned I = 0, N = Record.size(); I != N;) {
3670 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3671 const uint64_t Count = Record[I++];
3672 DelayedDeleteExprs.push_back(Count);
3673 for (uint64_t C = 0; C < Count; ++C) {
3674 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3675 bool IsArrayForm = Record[I++] == 1;
3676 DelayedDeleteExprs.push_back(IsArrayForm);
3677 }
3678 }
3679 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003680
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003681 case IMPORTED_MODULES:
Manman Ren11f2a472016-08-18 17:42:15 +00003682 if (!F.isModule()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003683 // If we aren't loading a module (which has its own exports), make
3684 // all of the imported modules visible.
3685 // FIXME: Deal with macros-only imports.
Richard Smith56be7542014-03-21 00:33:59 +00003686 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3687 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3688 SourceLocation Loc = ReadSourceLocation(F, Record, I);
Graydon Hoare9c982442017-01-18 20:36:59 +00003689 if (GlobalID) {
Aaron Ballman4f45b712014-03-21 15:22:56 +00003690 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
Graydon Hoare9c982442017-01-18 20:36:59 +00003691 if (DeserializationListener)
3692 DeserializationListener->ModuleImportRead(GlobalID, Loc);
3693 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003694 }
3695 }
3696 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003697
Guy Benyei11169dd2012-12-18 14:30:41 +00003698 case MACRO_OFFSET: {
3699 if (F.LocalNumMacros != 0) {
3700 Error("duplicate MACRO_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003701 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003702 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003703 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003704 F.LocalNumMacros = Record[0];
3705 unsigned LocalBaseMacroID = Record[1];
Daniel Grumbergbb8c7e72020-05-13 17:07:47 +01003706 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
Guy Benyei11169dd2012-12-18 14:30:41 +00003707 F.BaseMacroID = getTotalNumMacros();
3708
3709 if (F.LocalNumMacros > 0) {
3710 // Introduce the global -> local mapping for macros within this module.
3711 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3712
3713 // Introduce the local -> global mapping for macros within this module.
3714 F.MacroRemap.insertOrReplace(
3715 std::make_pair(LocalBaseMacroID,
3716 F.BaseMacroID - LocalBaseMacroID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00003717
3718 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
Guy Benyei11169dd2012-12-18 14:30:41 +00003719 }
3720 break;
3721 }
3722
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003723 case LATE_PARSED_TEMPLATE:
Richard Smithe40f2ba2013-08-07 21:41:30 +00003724 LateParsedTemplates.append(Record.begin(), Record.end());
3725 break;
Dario Domizioli13a0a382014-05-23 12:13:25 +00003726
3727 case OPTIMIZE_PRAGMA_OPTIONS:
3728 if (Record.size() != 1) {
3729 Error("invalid pragma optimize record");
3730 return Failure;
3731 }
3732 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3733 break;
Nico Weber72889432014-09-06 01:25:55 +00003734
Nico Weber779355f2016-03-02 23:22:00 +00003735 case MSSTRUCT_PRAGMA_OPTIONS:
3736 if (Record.size() != 1) {
3737 Error("invalid pragma ms_struct record");
3738 return Failure;
3739 }
3740 PragmaMSStructState = Record[0];
3741 break;
3742
Nico Weber42932312016-03-03 00:17:35 +00003743 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3744 if (Record.size() != 2) {
3745 Error("invalid pragma ms_struct record");
3746 return Failure;
3747 }
3748 PragmaMSPointersToMembersState = Record[0];
3749 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3750 break;
3751
Nico Weber72889432014-09-06 01:25:55 +00003752 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3753 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3754 UnusedLocalTypedefNameCandidates.push_back(
3755 getGlobalDeclID(F, Record[I]));
3756 break;
Justin Lebar67a78a62016-10-08 22:15:58 +00003757
3758 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3759 if (Record.size() != 1) {
3760 Error("invalid cuda pragma options record");
3761 return Failure;
3762 }
3763 ForceCUDAHostDeviceDepth = Record[0];
3764 break;
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00003765
3766 case PACK_PRAGMA_OPTIONS: {
3767 if (Record.size() < 3) {
3768 Error("invalid pragma pack record");
3769 return Failure;
3770 }
3771 PragmaPackCurrentValue = Record[0];
3772 PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3773 unsigned NumStackEntries = Record[2];
3774 unsigned Idx = 3;
3775 // Reset the stack when importing a new module.
3776 PragmaPackStack.clear();
3777 for (unsigned I = 0; I < NumStackEntries; ++I) {
3778 PragmaPackStackEntry Entry;
3779 Entry.Value = Record[Idx++];
3780 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
Alex Lorenz45b40142017-07-28 14:41:21 +00003781 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00003782 PragmaPackStrings.push_back(ReadString(Record, Idx));
3783 Entry.SlotLabel = PragmaPackStrings.back();
3784 PragmaPackStack.push_back(Entry);
3785 }
3786 break;
3787 }
Yaxun (Sam) Liub670ab72020-02-26 10:57:39 -05003788
Melanie Blowerf5360d42020-05-01 10:32:06 -07003789 case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3790 if (Record.size() < 3) {
3791 Error("invalid pragma pack record");
3792 return Failure;
3793 }
3794 FpPragmaCurrentValue = Record[0];
3795 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3796 unsigned NumStackEntries = Record[2];
3797 unsigned Idx = 3;
3798 // Reset the stack when importing a new module.
3799 FpPragmaStack.clear();
3800 for (unsigned I = 0; I < NumStackEntries; ++I) {
3801 FpPragmaStackEntry Entry;
3802 Entry.Value = Record[Idx++];
3803 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3804 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3805 FpPragmaStrings.push_back(ReadString(Record, Idx));
3806 Entry.SlotLabel = FpPragmaStrings.back();
3807 FpPragmaStack.push_back(Entry);
3808 }
3809 break;
3810 }
3811
Yaxun (Sam) Liub670ab72020-02-26 10:57:39 -05003812 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3813 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3814 DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I]));
3815 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003816 }
3817 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003818}
3819
Richard Smith37a93df2017-02-18 00:32:02 +00003820void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3821 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3822
3823 // Additional remapping information.
Vedant Kumar48b4f762018-04-14 01:40:48 +00003824 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
Richard Smith37a93df2017-02-18 00:32:02 +00003825 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3826 F.ModuleOffsetMap = StringRef();
3827
3828 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3829 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3830 F.SLocRemap.insert(std::make_pair(0U, 0));
3831 F.SLocRemap.insert(std::make_pair(2U, 1));
3832 }
3833
3834 // Continuous range maps we may be updating in our module.
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00003835 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
Richard Smith37a93df2017-02-18 00:32:02 +00003836 RemapBuilder SLocRemap(F.SLocRemap);
3837 RemapBuilder IdentifierRemap(F.IdentifierRemap);
3838 RemapBuilder MacroRemap(F.MacroRemap);
3839 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3840 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3841 RemapBuilder SelectorRemap(F.SelectorRemap);
3842 RemapBuilder DeclRemap(F.DeclRemap);
3843 RemapBuilder TypeRemap(F.TypeRemap);
3844
3845 while (Data < DataEnd) {
Boris Kolpackovd30446f2017-08-31 06:26:43 +00003846 // FIXME: Looking up dependency modules by filename is horrible. Let's
Daniel Grumbergbb8c7e72020-05-13 17:07:47 +01003847 // start fixing this with prebuilt, explicit and implicit modules and see
3848 // how it goes...
Richard Smith37a93df2017-02-18 00:32:02 +00003849 using namespace llvm::support;
Vedant Kumar48b4f762018-04-14 01:40:48 +00003850 ModuleKind Kind = static_cast<ModuleKind>(
3851 endian::readNext<uint8_t, little, unaligned>(Data));
Richard Smith37a93df2017-02-18 00:32:02 +00003852 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3853 StringRef Name = StringRef((const char*)Data, Len);
3854 Data += Len;
Daniel Grumbergbb8c7e72020-05-13 17:07:47 +01003855 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3856 Kind == MK_ImplicitModule
3857 ? ModuleMgr.lookupByModuleName(Name)
3858 : ModuleMgr.lookupByFileName(Name));
Richard Smith37a93df2017-02-18 00:32:02 +00003859 if (!OM) {
3860 std::string Msg =
3861 "SourceLocation remap refers to unknown module, cannot find ";
Benjamin Krameradcd0262020-01-28 20:23:46 +01003862 Msg.append(std::string(Name));
Richard Smith37a93df2017-02-18 00:32:02 +00003863 Error(Msg);
3864 return;
3865 }
3866
3867 uint32_t SLocOffset =
3868 endian::readNext<uint32_t, little, unaligned>(Data);
3869 uint32_t IdentifierIDOffset =
3870 endian::readNext<uint32_t, little, unaligned>(Data);
3871 uint32_t MacroIDOffset =
3872 endian::readNext<uint32_t, little, unaligned>(Data);
3873 uint32_t PreprocessedEntityIDOffset =
3874 endian::readNext<uint32_t, little, unaligned>(Data);
3875 uint32_t SubmoduleIDOffset =
3876 endian::readNext<uint32_t, little, unaligned>(Data);
3877 uint32_t SelectorIDOffset =
3878 endian::readNext<uint32_t, little, unaligned>(Data);
3879 uint32_t DeclIDOffset =
3880 endian::readNext<uint32_t, little, unaligned>(Data);
3881 uint32_t TypeIndexOffset =
3882 endian::readNext<uint32_t, little, unaligned>(Data);
3883
3884 uint32_t None = std::numeric_limits<uint32_t>::max();
3885
3886 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3887 RemapBuilder &Remap) {
3888 if (Offset != None)
3889 Remap.insert(std::make_pair(Offset,
3890 static_cast<int>(BaseOffset - Offset)));
3891 };
3892 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3893 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3894 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3895 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3896 PreprocessedEntityRemap);
3897 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3898 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3899 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3900 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3901
3902 // Global -> local mappings.
3903 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3904 }
3905}
3906
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003907ASTReader::ASTReadResult
3908ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3909 const ModuleFile *ImportedBy,
3910 unsigned ClientLoadCapabilities) {
3911 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00003912 F.ModuleMapPath = ReadPath(F, Record, Idx);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003913
3914 // Try to resolve ModuleName in the current header search context and
3915 // verify that it is found in the same module map file as we saved. If the
3916 // top-level AST file is a main file, skip this check because there is no
3917 // usable header search context.
3918 assert(!F.ModuleName.empty() &&
Richard Smithe842a472014-10-22 02:05:46 +00003919 "MODULE_NAME should come before MODULE_MAP_FILE");
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00003920 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
Richard Smithe842a472014-10-22 02:05:46 +00003921 // An implicitly-loaded module file should have its module listed in some
3922 // module map file that we've already loaded.
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003923 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
Richard Smithe842a472014-10-22 02:05:46 +00003924 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3925 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
Yuka Takahashid8baec22018-08-01 09:50:02 +00003926 // Don't emit module relocation error if we have -fno-validate-pch
3927 if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
Richard Smith0f99d6a2015-08-09 08:48:41 +00003928 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
Bruno Cardoso Lopesa66a3252017-11-17 03:24:11 +00003929 if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
Richard Smith0f99d6a2015-08-09 08:48:41 +00003930 // This module was defined by an imported (explicit) module.
3931 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3932 << ASTFE->getName();
Bruno Cardoso Lopesa66a3252017-11-17 03:24:11 +00003933 } else {
Richard Smith0f99d6a2015-08-09 08:48:41 +00003934 // This module was built with a different module map.
3935 Diag(diag::err_imported_module_not_found)
Bruno Cardoso Lopes4625c182019-08-29 23:14:08 +00003936 << F.ModuleName << F.FileName
3937 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3938 << !ImportedBy;
Bruno Cardoso Lopesa66a3252017-11-17 03:24:11 +00003939 // In case it was imported by a PCH, there's a chance the user is
3940 // just missing to include the search path to the directory containing
3941 // the modulemap.
Bruno Cardoso Lopes4625c182019-08-29 23:14:08 +00003942 if (ImportedBy && ImportedBy->Kind == MK_PCH)
Bruno Cardoso Lopesa66a3252017-11-17 03:24:11 +00003943 Diag(diag::note_imported_by_pch_module_not_found)
3944 << llvm::sys::path::parent_path(F.ModuleMapPath);
3945 }
Richard Smith0f99d6a2015-08-09 08:48:41 +00003946 }
3947 return OutOfDate;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003948 }
3949
Richard Smithe842a472014-10-22 02:05:46 +00003950 assert(M->Name == F.ModuleName && "found module with different name");
3951
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003952 // Check the primary module map file.
Harlan Haskins8d323d12019-08-01 21:31:56 +00003953 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3954 if (!StoredModMap || *StoredModMap != ModMap) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003955 assert(ModMap && "found module is missing module map file");
Bruno Cardoso Lopes4625c182019-08-29 23:14:08 +00003956 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3957 "top-level import should be verified");
3958 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003959 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3960 Diag(diag::err_imported_module_modmap_changed)
Bruno Cardoso Lopes4625c182019-08-29 23:14:08 +00003961 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3962 << ModMap->getName() << F.ModuleMapPath << NotImported;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003963 return OutOfDate;
3964 }
3965
3966 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3967 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3968 // FIXME: we should use input files rather than storing names.
Richard Smith7ed1bc92014-12-05 22:42:13 +00003969 std::string Filename = ReadPath(F, Record, Idx);
Harlan Haskins8d323d12019-08-01 21:31:56 +00003970 auto F = FileMgr.getFile(Filename, false, false);
3971 if (!F) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003972 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3973 Error("could not find file '" + Filename +"' referenced by AST file");
3974 return OutOfDate;
3975 }
Harlan Haskins8d323d12019-08-01 21:31:56 +00003976 AdditionalStoredMaps.insert(*F);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003977 }
3978
3979 // Check any additional module map files (e.g. module.private.modulemap)
3980 // that are not in the pcm.
3981 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00003982 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003983 // Remove files that match
3984 // Note: SmallPtrSet::erase is really remove
3985 if (!AdditionalStoredMaps.erase(ModMap)) {
3986 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3987 Diag(diag::err_module_different_modmap)
3988 << F.ModuleName << /*new*/0 << ModMap->getName();
3989 return OutOfDate;
3990 }
3991 }
3992 }
3993
3994 // Check any additional module map files that are in the pcm, but not
3995 // found in header search. Cases that match are already removed.
Vedant Kumar48b4f762018-04-14 01:40:48 +00003996 for (const FileEntry *ModMap : AdditionalStoredMaps) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003997 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3998 Diag(diag::err_module_different_modmap)
3999 << F.ModuleName << /*not new*/1 << ModMap->getName();
4000 return OutOfDate;
4001 }
4002 }
4003
4004 if (Listener)
4005 Listener->ReadModuleMapFile(F.ModuleMapPath);
4006 return Success;
4007}
4008
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004009/// Move the given method to the back of the global list of methods.
Douglas Gregorc1489562013-02-12 23:36:21 +00004010static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4011 // Find the entry for this selector in the method pool.
4012 Sema::GlobalMethodPool::iterator Known
4013 = S.MethodPool.find(Method->getSelector());
4014 if (Known == S.MethodPool.end())
4015 return;
4016
4017 // Retrieve the appropriate method list.
4018 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4019 : Known->second.second;
4020 bool Found = false;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00004021 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregorc1489562013-02-12 23:36:21 +00004022 if (!Found) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00004023 if (List->getMethod() == Method) {
Douglas Gregorc1489562013-02-12 23:36:21 +00004024 Found = true;
4025 } else {
4026 // Keep searching.
4027 continue;
4028 }
4029 }
4030
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00004031 if (List->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00004032 List->setMethod(List->getNext()->getMethod());
Douglas Gregorc1489562013-02-12 23:36:21 +00004033 else
Nico Weber2e0c8f72014-12-27 03:58:08 +00004034 List->setMethod(Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00004035 }
4036}
4037
Richard Smithde711422015-04-23 21:20:19 +00004038void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Richard Smith10434f32015-05-02 02:08:26 +00004039 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
Vedant Kumar48b4f762018-04-14 01:40:48 +00004040 for (Decl *D : Names) {
Martin Boehme2e92b392020-06-08 15:37:44 +02004041 bool wasHidden = !D->isUnconditionallyVisible();
Richard Smith90dc5252017-06-23 01:04:34 +00004042 D->setVisibleDespiteOwningModule();
Guy Benyei11169dd2012-12-18 14:30:41 +00004043
Vedant Kumar48b4f762018-04-14 01:40:48 +00004044 if (wasHidden && SemaObj) {
4045 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
Richard Smith49f906a2014-03-01 00:08:04 +00004046 moveMethodToBackOfGlobalList(*SemaObj, Method);
Vedant Kumar48b4f762018-04-14 01:40:48 +00004047 }
4048 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004049 }
4050}
4051
Richard Smith49f906a2014-03-01 00:08:04 +00004052void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00004053 Module::NameVisibilityKind NameVisibility,
Richard Smitha7e2cc62015-05-01 01:53:09 +00004054 SourceLocation ImportLoc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004055 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004056 SmallVector<Module *, 4> Stack;
Robert Wilhelm25284cc2013-08-23 16:11:15 +00004057 Stack.push_back(Mod);
Guy Benyei11169dd2012-12-18 14:30:41 +00004058 while (!Stack.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00004059 Mod = Stack.pop_back_val();
Guy Benyei11169dd2012-12-18 14:30:41 +00004060
4061 if (NameVisibility <= Mod->NameVisibility) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00004062 // This module already has this level of visibility (or greater), so
Guy Benyei11169dd2012-12-18 14:30:41 +00004063 // there is nothing more to do.
4064 continue;
4065 }
Richard Smith49f906a2014-03-01 00:08:04 +00004066
Richard Smith6bc75022020-04-17 20:25:15 -07004067 if (Mod->isUnimportable()) {
4068 // Modules that aren't importable cannot be made visible.
Guy Benyei11169dd2012-12-18 14:30:41 +00004069 continue;
4070 }
4071
4072 // Update the module's name visibility.
4073 Mod->NameVisibility = NameVisibility;
Richard Smith49f906a2014-03-01 00:08:04 +00004074
Guy Benyei11169dd2012-12-18 14:30:41 +00004075 // If we've already deserialized any names from this module,
4076 // mark them as visible.
4077 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4078 if (Hidden != HiddenNamesMap.end()) {
Richard Smith57721ac2014-07-21 04:10:40 +00004079 auto HiddenNames = std::move(*Hidden);
Guy Benyei11169dd2012-12-18 14:30:41 +00004080 HiddenNamesMap.erase(Hidden);
Richard Smithde711422015-04-23 21:20:19 +00004081 makeNamesVisible(HiddenNames.second, HiddenNames.first);
Richard Smith57721ac2014-07-21 04:10:40 +00004082 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4083 "making names visible added hidden names");
Guy Benyei11169dd2012-12-18 14:30:41 +00004084 }
Dmitri Gribenkoe9bcf5b2013-11-04 21:51:33 +00004085
Guy Benyei11169dd2012-12-18 14:30:41 +00004086 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00004087 SmallVector<Module *, 16> Exports;
4088 Mod->getExportedModules(Exports);
Vedant Kumar48b4f762018-04-14 01:40:48 +00004089 for (SmallVectorImpl<Module *>::iterator
4090 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4091 Module *Exported = *I;
David Blaikie82e95a32014-11-19 07:49:47 +00004092 if (Visited.insert(Exported).second)
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00004093 Stack.push_back(Exported);
Vedant Kumar48b4f762018-04-14 01:40:48 +00004094 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004095 }
4096}
4097
Richard Smith6561f922016-09-12 21:06:40 +00004098/// We've merged the definition \p MergedDef into the existing definition
4099/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4100/// visible.
4101void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4102 NamedDecl *MergedDef) {
Martin Boehme2e92b392020-06-08 15:37:44 +02004103 if (!Def->isUnconditionallyVisible()) {
Richard Smith6561f922016-09-12 21:06:40 +00004104 // If MergedDef is visible or becomes visible, make the definition visible.
Martin Boehme2e92b392020-06-08 15:37:44 +02004105 if (MergedDef->isUnconditionallyVisible())
Richard Smith90dc5252017-06-23 01:04:34 +00004106 Def->setVisibleDespiteOwningModule();
Richard Smith13897eb2018-09-12 23:37:00 +00004107 else {
Benjamin Kramera72a70a2016-10-17 13:00:44 +00004108 getContext().mergeDefinitionIntoModule(
4109 Def, MergedDef->getImportedOwningModule(),
4110 /*NotifyListeners*/ false);
4111 PendingMergedDefinitionsToDeduplicate.insert(Def);
Benjamin Kramera72a70a2016-10-17 13:00:44 +00004112 }
Richard Smith6561f922016-09-12 21:06:40 +00004113 }
4114}
4115
Douglas Gregore060e572013-01-25 01:03:03 +00004116bool ASTReader::loadGlobalIndex() {
4117 if (GlobalIndex)
4118 return false;
4119
4120 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
Richard Smithdbafb6c2017-06-29 23:23:46 +00004121 !PP.getLangOpts().Modules)
Douglas Gregore060e572013-01-25 01:03:03 +00004122 return true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004123
Douglas Gregore060e572013-01-25 01:03:03 +00004124 // Try to load the global index.
4125 TriedLoadingGlobalIndex = true;
4126 StringRef ModuleCachePath
4127 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
JF Bastien0e828952019-06-26 19:50:12 +00004128 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4129 GlobalModuleIndex::readIndex(ModuleCachePath);
4130 if (llvm::Error Err = std::move(Result.second)) {
4131 assert(!Result.first);
4132 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
Douglas Gregore060e572013-01-25 01:03:03 +00004133 return true;
JF Bastien0e828952019-06-26 19:50:12 +00004134 }
Douglas Gregore060e572013-01-25 01:03:03 +00004135
4136 GlobalIndex.reset(Result.first);
Douglas Gregor7211ac12013-01-25 23:32:03 +00004137 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregore060e572013-01-25 01:03:03 +00004138 return false;
4139}
4140
4141bool ASTReader::isGlobalIndexUnavailable() const {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004142 return PP.getLangOpts().Modules && UseGlobalIndex &&
Douglas Gregore060e572013-01-25 01:03:03 +00004143 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4144}
4145
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004146static void updateModuleTimestamp(ModuleFile &MF) {
4147 // Overwrite the timestamp file contents so that file's mtime changes.
4148 std::string TimestampFilename = MF.getTimestampFilename();
Rafael Espindoladae941a2014-08-25 18:17:04 +00004149 std::error_code EC;
Fangrui Songd9b948b2019-08-05 05:43:48 +00004150 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
Rafael Espindoladae941a2014-08-25 18:17:04 +00004151 if (EC)
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004152 return;
4153 OS << "Timestamp file\n";
Alex Lorenz0bafa022017-06-02 10:36:56 +00004154 OS.close();
4155 OS.clear_error(); // Avoid triggering a fatal error.
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004156}
4157
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004158/// Given a cursor at the start of an AST file, scan ahead and drop the
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004159/// cursor into the start of the given block ID, returning false on success and
4160/// true on failure.
4161static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00004162 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00004163 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4164 if (!MaybeEntry) {
4165 // FIXME this drops errors on the floor.
4166 consumeError(MaybeEntry.takeError());
4167 return true;
4168 }
4169 llvm::BitstreamEntry Entry = MaybeEntry.get();
4170
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004171 switch (Entry.Kind) {
4172 case llvm::BitstreamEntry::Error:
4173 case llvm::BitstreamEntry::EndBlock:
4174 return true;
4175
4176 case llvm::BitstreamEntry::Record:
4177 // Ignore top-level records.
JF Bastien0e828952019-06-26 19:50:12 +00004178 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4179 break;
4180 else {
4181 // FIXME this drops errors on the floor.
4182 consumeError(Skipped.takeError());
4183 return true;
4184 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004185
4186 case llvm::BitstreamEntry::SubBlock:
4187 if (Entry.ID == BlockID) {
JF Bastien0e828952019-06-26 19:50:12 +00004188 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4189 // FIXME this drops the error on the floor.
4190 consumeError(std::move(Err));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004191 return true;
JF Bastien0e828952019-06-26 19:50:12 +00004192 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004193 // Found it!
4194 return false;
4195 }
4196
JF Bastien0e828952019-06-26 19:50:12 +00004197 if (llvm::Error Err = Cursor.SkipBlock()) {
4198 // FIXME this drops the error on the floor.
4199 consumeError(std::move(Err));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004200 return true;
JF Bastien0e828952019-06-26 19:50:12 +00004201 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004202 }
4203 }
4204}
4205
Benjamin Kramer0772c422016-02-13 13:42:54 +00004206ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
Guy Benyei11169dd2012-12-18 14:30:41 +00004207 ModuleKind Type,
4208 SourceLocation ImportLoc,
Graydon Hoaree7196af2016-12-09 21:45:49 +00004209 unsigned ClientLoadCapabilities,
4210 SmallVectorImpl<ImportedSubmodule> *Imported) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00004211 llvm::SaveAndRestore<SourceLocation>
4212 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4213
Richard Smithd1c46742014-04-30 02:24:17 +00004214 // Defer any pending actions until we get to the end of reading the AST file.
4215 Deserializing AnASTFile(this);
4216
Guy Benyei11169dd2012-12-18 14:30:41 +00004217 // Bump the generation number.
Richard Smithdbafb6c2017-06-29 23:23:46 +00004218 unsigned PreviousGeneration = 0;
4219 if (ContextObj)
4220 PreviousGeneration = incrementGeneration(*ContextObj);
Guy Benyei11169dd2012-12-18 14:30:41 +00004221
4222 unsigned NumModules = ModuleMgr.size();
Duncan P. N. Exon Smithc46b3a22019-11-10 10:42:29 -08004223 auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4224 assert(ReadResult && "expected to return error");
Duncan P. N. Exon Smith8e9e4332019-11-10 10:31:03 -08004225 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
Richard Smithdbafb6c2017-06-29 23:23:46 +00004226 PP.getLangOpts().Modules
Duncan P. N. Exon Smith8e6bc1972017-01-28 23:02:12 +00004227 ? &PP.getHeaderSearchInfo().getModuleMap()
4228 : nullptr);
Douglas Gregore060e572013-01-25 01:03:03 +00004229
4230 // If we find that any modules are unusable, the global index is going
4231 // to be out-of-date. Just remove it.
4232 GlobalIndex.reset();
Craig Toppera13603a2014-05-22 05:54:18 +00004233 ModuleMgr.setGlobalIndex(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00004234 return ReadResult;
Duncan P. N. Exon Smithc46b3a22019-11-10 10:42:29 -08004235 };
4236
4237 SmallVector<ImportedModule, 4> Loaded;
4238 switch (ASTReadResult ReadResult =
4239 ReadASTCore(FileName, Type, ImportLoc,
4240 /*ImportedBy=*/nullptr, Loaded, 0, 0,
4241 ASTFileSignature(), ClientLoadCapabilities)) {
4242 case Failure:
4243 case Missing:
4244 case OutOfDate:
4245 case VersionMismatch:
4246 case ConfigurationMismatch:
4247 case HadErrors:
4248 return removeModulesAndReturn(ReadResult);
Guy Benyei11169dd2012-12-18 14:30:41 +00004249 case Success:
4250 break;
4251 }
4252
4253 // Here comes stuff that we only do once the entire chain is loaded.
4254
Duncan P. N. Exon Smith01782c32019-11-10 10:50:12 -08004255 // Load the AST blocks of all of the modules that we loaded. We can still
4256 // hit errors parsing the ASTs at this point.
Duncan P. N. Exon Smithbfd58fc2019-11-11 15:42:25 -08004257 for (ImportedModule &M : Loaded) {
4258 ModuleFile &F = *M.Mod;
Guy Benyei11169dd2012-12-18 14:30:41 +00004259
4260 // Read the AST block.
Ben Langmuir2c9af442014-04-10 17:57:43 +00004261 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
Duncan P. N. Exon Smithc46b3a22019-11-10 10:42:29 -08004262 return removeModulesAndReturn(Result);
Guy Benyei11169dd2012-12-18 14:30:41 +00004263
Duncan P. N. Exon Smith83dcb342019-11-10 13:14:52 -08004264 // The AST block should always have a definition for the main module.
4265 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4266 Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4267 return removeModulesAndReturn(Failure);
4268 }
4269
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004270 // Read the extension blocks.
4271 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4272 if (ASTReadResult Result = ReadExtensionBlock(F))
Duncan P. N. Exon Smithc46b3a22019-11-10 10:42:29 -08004273 return removeModulesAndReturn(Result);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004274 }
4275
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004276 // Once read, set the ModuleFile bit base offset and update the size in
Guy Benyei11169dd2012-12-18 14:30:41 +00004277 // bits of all files we've seen.
4278 F.GlobalBitOffset = TotalModulesSizeInBits;
4279 TotalModulesSizeInBits += F.SizeInBits;
4280 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Duncan P. N. Exon Smith01782c32019-11-10 10:50:12 -08004281 }
4282
4283 // Preload source locations and interesting indentifiers.
4284 for (ImportedModule &M : Loaded) {
4285 ModuleFile &F = *M.Mod;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004286
Guy Benyei11169dd2012-12-18 14:30:41 +00004287 // Preload SLocEntries.
Vedant Kumar48b4f762018-04-14 01:40:48 +00004288 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4289 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Guy Benyei11169dd2012-12-18 14:30:41 +00004290 // Load it through the SourceManager and don't call ReadSLocEntry()
4291 // directly because the entry may have already been loaded in which case
4292 // calling ReadSLocEntry() directly would trigger an assertion in
4293 // SourceManager.
4294 SourceMgr.getLoadedSLocEntryByID(Index);
4295 }
Richard Smith33e0f7e2015-07-22 02:08:40 +00004296
Richard Smithea741482017-05-01 22:10:47 +00004297 // Map the original source file ID into the ID space of the current
4298 // compilation.
4299 if (F.OriginalSourceFileID.isValid()) {
4300 F.OriginalSourceFileID = FileID::get(
4301 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4302 }
4303
Richard Smith33e0f7e2015-07-22 02:08:40 +00004304 // Preload all the pending interesting identifiers by marking them out of
4305 // date.
4306 for (auto Offset : F.PreloadIdentifierOffsets) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004307 const unsigned char *Data = reinterpret_cast<const unsigned char *>(
Richard Smith33e0f7e2015-07-22 02:08:40 +00004308 F.IdentifierTableData + Offset);
4309
4310 ASTIdentifierLookupTrait Trait(*this, F);
4311 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4312 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
Richard Smith79bf9202015-08-24 03:33:22 +00004313 auto &II = PP.getIdentifierTable().getOwn(Key);
4314 II.setOutOfDate(true);
4315
4316 // Mark this identifier as being from an AST file so that we can track
4317 // whether we need to serialize it.
Richard Smitheb4b58f62016-02-05 01:40:54 +00004318 markIdentifierFromAST(*this, II);
Richard Smith79bf9202015-08-24 03:33:22 +00004319
4320 // Associate the ID with the identifier so that the writer can reuse it.
4321 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4322 SetIdentifierInfo(ID, &II);
Richard Smith33e0f7e2015-07-22 02:08:40 +00004323 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004324 }
4325
Douglas Gregor603cd862013-03-22 18:50:14 +00004326 // Setup the import locations and notify the module manager that we've
4327 // committed to these module files.
Duncan P. N. Exon Smithbfd58fc2019-11-11 15:42:25 -08004328 for (ImportedModule &M : Loaded) {
4329 ModuleFile &F = *M.Mod;
Douglas Gregor603cd862013-03-22 18:50:14 +00004330
4331 ModuleMgr.moduleFileAccepted(&F);
4332
4333 // Set the import location.
Argyrios Kyrtzidis71c1af82013-02-01 16:36:14 +00004334 F.DirectImportLoc = ImportLoc;
Richard Smithb22a1d12016-03-27 20:13:24 +00004335 // FIXME: We assume that locations from PCH / preamble do not need
4336 // any translation.
Duncan P. N. Exon Smithbfd58fc2019-11-11 15:42:25 -08004337 if (!M.ImportedBy)
4338 F.ImportLoc = M.ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00004339 else
Duncan P. N. Exon Smithbfd58fc2019-11-11 15:42:25 -08004340 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00004341 }
4342
Richard Smithdbafb6c2017-06-29 23:23:46 +00004343 if (!PP.getLangOpts().CPlusPlus ||
Manman Ren11f2a472016-08-18 17:42:15 +00004344 (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4345 Type != MK_PrebuiltModule)) {
Richard Smith33e0f7e2015-07-22 02:08:40 +00004346 // Mark all of the identifiers in the identifier table as being out of date,
4347 // so that various accessors know to check the loaded modules when the
4348 // identifier is used.
4349 //
4350 // For C++ modules, we don't need information on many identifiers (just
4351 // those that provide macros or are poisoned), so we mark all of
4352 // the interesting ones via PreloadIdentifierOffsets.
Vedant Kumar48b4f762018-04-14 01:40:48 +00004353 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4354 IdEnd = PP.getIdentifierTable().end();
4355 Id != IdEnd; ++Id)
4356 Id->second->setOutOfDate(true);
Richard Smith33e0f7e2015-07-22 02:08:40 +00004357 }
Manman Rena0f31a02016-04-29 19:04:05 +00004358 // Mark selectors as out of date.
4359 for (auto Sel : SelectorGeneration)
4360 SelectorOutOfDate[Sel.first] = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004361
Guy Benyei11169dd2012-12-18 14:30:41 +00004362 // Resolve any unresolved module exports.
Vedant Kumar48b4f762018-04-14 01:40:48 +00004363 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4364 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei11169dd2012-12-18 14:30:41 +00004365 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4366 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregorfb912652013-03-20 21:10:35 +00004367
4368 switch (Unresolved.Kind) {
4369 case UnresolvedModuleRef::Conflict:
4370 if (ResolvedMod) {
4371 Module::Conflict Conflict;
4372 Conflict.Other = ResolvedMod;
4373 Conflict.Message = Unresolved.String.str();
4374 Unresolved.Mod->Conflicts.push_back(Conflict);
4375 }
4376 continue;
4377
4378 case UnresolvedModuleRef::Import:
Guy Benyei11169dd2012-12-18 14:30:41 +00004379 if (ResolvedMod)
Richard Smith38477db2015-05-02 00:45:56 +00004380 Unresolved.Mod->Imports.insert(ResolvedMod);
Guy Benyei11169dd2012-12-18 14:30:41 +00004381 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00004382
Douglas Gregorfb912652013-03-20 21:10:35 +00004383 case UnresolvedModuleRef::Export:
4384 if (ResolvedMod || Unresolved.IsWildcard)
4385 Unresolved.Mod->Exports.push_back(
4386 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4387 continue;
4388 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004389 }
Douglas Gregorfb912652013-03-20 21:10:35 +00004390 UnresolvedModuleRefs.clear();
Daniel Jasperba7f2f72013-09-24 09:14:14 +00004391
Graydon Hoaree7196af2016-12-09 21:45:49 +00004392 if (Imported)
4393 Imported->append(ImportedModules.begin(),
4394 ImportedModules.end());
4395
Daniel Jasperba7f2f72013-09-24 09:14:14 +00004396 // FIXME: How do we load the 'use'd modules? They may not be submodules.
4397 // Might be unnecessary as use declarations are only used to build the
4398 // module itself.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004399
Richard Smithdbafb6c2017-06-29 23:23:46 +00004400 if (ContextObj)
4401 InitializeContext();
Guy Benyei11169dd2012-12-18 14:30:41 +00004402
Richard Smith3d8e97e2013-10-18 06:54:39 +00004403 if (SemaObj)
4404 UpdateSema();
4405
Guy Benyei11169dd2012-12-18 14:30:41 +00004406 if (DeserializationListener)
4407 DeserializationListener->ReaderInitialized(this);
4408
4409 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
Yaron Keren8b563662015-10-03 10:46:20 +00004410 if (PrimaryModule.OriginalSourceFileID.isValid()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004411 // If this AST file is a precompiled preamble, then set the
4412 // preamble file ID of the source manager to the file source file
4413 // from which the preamble was built.
4414 if (Type == MK_Preamble) {
4415 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4416 } else if (Type == MK_MainFile) {
4417 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4418 }
4419 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004420
Guy Benyei11169dd2012-12-18 14:30:41 +00004421 // For any Objective-C class definitions we have already loaded, make sure
4422 // that we load any additional categories.
Vedant Kumar48b4f762018-04-14 01:40:48 +00004423 if (ContextObj) {
4424 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4425 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4426 ObjCClassesLoaded[I],
Richard Smithdbafb6c2017-06-29 23:23:46 +00004427 PreviousGeneration);
Vedant Kumar48b4f762018-04-14 01:40:48 +00004428 }
4429 }
Douglas Gregore060e572013-01-25 01:03:03 +00004430
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004431 if (PP.getHeaderSearchInfo()
4432 .getHeaderSearchOpts()
4433 .ModulesValidateOncePerBuildSession) {
4434 // Now we are certain that the module and all modules it depends on are
4435 // up to date. Create or update timestamp files for modules that are
4436 // located in the module cache (not for PCH files that could be anywhere
4437 // in the filesystem).
Vedant Kumar48b4f762018-04-14 01:40:48 +00004438 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4439 ImportedModule &M = Loaded[I];
4440 if (M.Mod->Kind == MK_ImplicitModule) {
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004441 updateModuleTimestamp(*M.Mod);
Vedant Kumar48b4f762018-04-14 01:40:48 +00004442 }
4443 }
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004444 }
4445
Guy Benyei11169dd2012-12-18 14:30:41 +00004446 return Success;
4447}
4448
Peter Collingbourne77c89b62016-11-08 04:17:11 +00004449static ASTFileSignature readASTFileSignature(StringRef PCH);
Ben Langmuir487ea142014-10-23 18:05:36 +00004450
JF Bastien0e828952019-06-26 19:50:12 +00004451/// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4452static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4453 // FIXME checking magic headers is done in other places such as
4454 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4455 // always done the same. Unify it all with a helper.
4456 if (!Stream.canSkipToPos(4))
4457 return llvm::createStringError(std::errc::illegal_byte_sequence,
4458 "file too small to contain AST file magic");
4459 for (unsigned C : {'C', 'P', 'C', 'H'})
4460 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4461 if (Res.get() != C)
4462 return llvm::createStringError(
4463 std::errc::illegal_byte_sequence,
4464 "file doesn't start with AST file magic");
4465 } else
4466 return Res.takeError();
4467 return llvm::Error::success();
Ben Langmuir70a1b812015-03-24 04:43:52 +00004468}
4469
Richard Smith0f99d6a2015-08-09 08:48:41 +00004470static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4471 switch (Kind) {
4472 case MK_PCH:
4473 return 0; // PCH
4474 case MK_ImplicitModule:
4475 case MK_ExplicitModule:
Manman Ren11f2a472016-08-18 17:42:15 +00004476 case MK_PrebuiltModule:
Richard Smith0f99d6a2015-08-09 08:48:41 +00004477 return 1; // module
4478 case MK_MainFile:
4479 case MK_Preamble:
4480 return 2; // main source file
4481 }
4482 llvm_unreachable("unknown module kind");
4483}
4484
Guy Benyei11169dd2012-12-18 14:30:41 +00004485ASTReader::ASTReadResult
4486ASTReader::ReadASTCore(StringRef FileName,
4487 ModuleKind Type,
4488 SourceLocation ImportLoc,
4489 ModuleFile *ImportedBy,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004490 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00004491 off_t ExpectedSize, time_t ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00004492 ASTFileSignature ExpectedSignature,
Guy Benyei11169dd2012-12-18 14:30:41 +00004493 unsigned ClientLoadCapabilities) {
4494 ModuleFile *M;
Guy Benyei11169dd2012-12-18 14:30:41 +00004495 std::string ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00004496 ModuleManager::AddModuleResult AddResult
4497 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
Richard Smith053f6c62014-05-16 23:01:30 +00004498 getGeneration(), ExpectedSize, ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00004499 ExpectedSignature, readASTFileSignature,
Douglas Gregor7029ce12013-03-19 00:28:20 +00004500 M, ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00004501
Douglas Gregor7029ce12013-03-19 00:28:20 +00004502 switch (AddResult) {
4503 case ModuleManager::AlreadyLoaded:
Duncan P. N. Exon Smith9dda8f52019-03-06 02:50:46 +00004504 Diag(diag::remark_module_import)
4505 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4506 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
Douglas Gregor7029ce12013-03-19 00:28:20 +00004507 return Success;
4508
4509 case ModuleManager::NewlyLoaded:
4510 // Load module file below.
4511 break;
4512
4513 case ModuleManager::Missing:
Richard Smithe842a472014-10-22 02:05:46 +00004514 // The module file was missing; if the client can handle that, return
Douglas Gregor7029ce12013-03-19 00:28:20 +00004515 // it.
4516 if (ClientLoadCapabilities & ARR_Missing)
4517 return Missing;
4518
4519 // Otherwise, return an error.
Richard Smith0f99d6a2015-08-09 08:48:41 +00004520 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
Adrian Prantlb3b5a732016-08-29 20:46:59 +00004521 << FileName << !ErrorStr.empty()
Richard Smith0f99d6a2015-08-09 08:48:41 +00004522 << ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00004523 return Failure;
4524
4525 case ModuleManager::OutOfDate:
4526 // We couldn't load the module file because it is out-of-date. If the
4527 // client can handle out-of-date, return it.
4528 if (ClientLoadCapabilities & ARR_OutOfDate)
4529 return OutOfDate;
4530
4531 // Otherwise, return an error.
Richard Smith0f99d6a2015-08-09 08:48:41 +00004532 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
Adrian Prantl9a06a882016-08-29 20:46:56 +00004533 << FileName << !ErrorStr.empty()
Richard Smith0f99d6a2015-08-09 08:48:41 +00004534 << ErrorStr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004535 return Failure;
4536 }
4537
Douglas Gregor7029ce12013-03-19 00:28:20 +00004538 assert(M && "Missing module file");
Guy Benyei11169dd2012-12-18 14:30:41 +00004539
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00004540 bool ShouldFinalizePCM = false;
4541 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4542 auto &MC = getModuleManager().getModuleCache();
4543 if (ShouldFinalizePCM)
4544 MC.finalizePCM(FileName);
4545 else
Rumeet Dhindsa57a2eaf2020-03-10 10:59:26 -07004546 MC.tryToDropPCM(FileName);
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00004547 });
Guy Benyei11169dd2012-12-18 14:30:41 +00004548 ModuleFile &F = *M;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004549 BitstreamCursor &Stream = F.Stream;
Peter Collingbourne77c89b62016-11-08 04:17:11 +00004550 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
Adrian Prantlcbc368c2015-02-25 02:44:04 +00004551 F.SizeInBits = F.Buffer->getBufferSize() * 8;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004552
Guy Benyei11169dd2012-12-18 14:30:41 +00004553 // Sniff for the signature.
JF Bastien0e828952019-06-26 19:50:12 +00004554 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4555 Diag(diag::err_module_file_invalid)
4556 << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
Guy Benyei11169dd2012-12-18 14:30:41 +00004557 return Failure;
4558 }
4559
4560 // This is used for compatibility with older PCH formats.
4561 bool HaveReadControlBlock = false;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00004562 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00004563 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4564 if (!MaybeEntry) {
4565 Error(MaybeEntry.takeError());
4566 return Failure;
4567 }
4568 llvm::BitstreamEntry Entry = MaybeEntry.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004569
Chris Lattnerefa77172013-01-20 00:00:22 +00004570 switch (Entry.Kind) {
4571 case llvm::BitstreamEntry::Error:
Chris Lattnerefa77172013-01-20 00:00:22 +00004572 case llvm::BitstreamEntry::Record:
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004573 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00004574 Error("invalid record at top-level of AST file");
4575 return Failure;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004576
Chris Lattnerefa77172013-01-20 00:00:22 +00004577 case llvm::BitstreamEntry::SubBlock:
4578 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004579 }
4580
Chris Lattnerefa77172013-01-20 00:00:22 +00004581 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004582 case CONTROL_BLOCK_ID:
4583 HaveReadControlBlock = true;
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004584 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004585 case Success:
Richard Smith0f99d6a2015-08-09 08:48:41 +00004586 // Check that we didn't try to load a non-module AST file as a module.
4587 //
4588 // FIXME: Should we also perform the converse check? Loading a module as
4589 // a PCH file sort of works, but it's a bit wonky.
Manman Ren11f2a472016-08-18 17:42:15 +00004590 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4591 Type == MK_PrebuiltModule) &&
Richard Smith0f99d6a2015-08-09 08:48:41 +00004592 F.ModuleName.empty()) {
4593 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4594 if (Result != OutOfDate ||
4595 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4596 Diag(diag::err_module_file_not_module) << FileName;
4597 return Result;
4598 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004599 break;
4600
4601 case Failure: return Failure;
Douglas Gregor7029ce12013-03-19 00:28:20 +00004602 case Missing: return Missing;
Guy Benyei11169dd2012-12-18 14:30:41 +00004603 case OutOfDate: return OutOfDate;
4604 case VersionMismatch: return VersionMismatch;
4605 case ConfigurationMismatch: return ConfigurationMismatch;
4606 case HadErrors: return HadErrors;
4607 }
4608 break;
Richard Smithf8c32552015-09-02 17:45:54 +00004609
Guy Benyei11169dd2012-12-18 14:30:41 +00004610 case AST_BLOCK_ID:
4611 if (!HaveReadControlBlock) {
4612 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00004613 Diag(diag::err_pch_version_too_old);
Guy Benyei11169dd2012-12-18 14:30:41 +00004614 return VersionMismatch;
4615 }
4616
4617 // Record that we've loaded this module.
4618 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00004619 ShouldFinalizePCM = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00004620 return Success;
4621
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004622 case UNHASHED_CONTROL_BLOCK_ID:
4623 // This block is handled using look-ahead during ReadControlBlock. We
4624 // shouldn't get here!
4625 Error("malformed block record in AST file");
4626 return Failure;
4627
Guy Benyei11169dd2012-12-18 14:30:41 +00004628 default:
JF Bastien0e828952019-06-26 19:50:12 +00004629 if (llvm::Error Err = Stream.SkipBlock()) {
4630 Error(std::move(Err));
Guy Benyei11169dd2012-12-18 14:30:41 +00004631 return Failure;
4632 }
4633 break;
4634 }
4635 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004636
Duncan P. N. Exon Smithfae03d82019-03-03 20:17:53 +00004637 llvm_unreachable("unexpected break; expected return");
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004638}
4639
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004640ASTReader::ASTReadResult
4641ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4642 unsigned ClientLoadCapabilities) {
4643 const HeaderSearchOptions &HSOpts =
4644 PP.getHeaderSearchInfo().getHeaderSearchOpts();
4645 bool AllowCompatibleConfigurationMismatch =
4646 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4647
4648 ASTReadResult Result = readUnhashedControlBlockImpl(
4649 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4650 Listener.get(),
4651 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4652
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004653 // If F was directly imported by another module, it's implicitly validated by
4654 // the importing module.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004655 if (DisableValidation || WasImportedBy ||
4656 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4657 return Success;
4658
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004659 if (Result == Failure) {
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004660 Error("malformed block record in AST file");
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004661 return Failure;
4662 }
4663
4664 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +00004665 // If this module has already been finalized in the ModuleCache, we're stuck
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004666 // with it; we can only load a single version of each module.
4667 //
4668 // This can happen when a module is imported in two contexts: in one, as a
4669 // user module; in another, as a system module (due to an import from
4670 // another module marked with the [system] flag). It usually indicates a
4671 // bug in the module map: this module should also be marked with [system].
4672 //
4673 // If -Wno-system-headers (the default), and the first import is as a
4674 // system module, then validation will fail during the as-user import,
4675 // since -Werror flags won't have been validated. However, it's reasonable
4676 // to treat this consistently as a system module.
4677 //
4678 // If -Wsystem-headers, the PCM on disk was built with
4679 // -Wno-system-headers, and the first import is as a user module, then
4680 // validation will fail during the as-system import since the PCM on disk
4681 // doesn't guarantee that -Werror was respected. However, the -Werror
4682 // flags were checked during the initial as-user import.
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00004683 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004684 Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4685 return Success;
4686 }
4687 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004688
4689 return Result;
4690}
4691
4692ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4693 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4694 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4695 bool ValidateDiagnosticOptions) {
4696 // Initialize a stream.
4697 BitstreamCursor Stream(StreamData);
4698
4699 // Sniff for the signature.
JF Bastien0e828952019-06-26 19:50:12 +00004700 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4701 // FIXME this drops the error on the floor.
4702 consumeError(std::move(Err));
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004703 return Failure;
JF Bastien0e828952019-06-26 19:50:12 +00004704 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004705
4706 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4707 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4708 return Failure;
4709
4710 // Read all of the records in the options block.
4711 RecordData Record;
4712 ASTReadResult Result = Success;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00004713 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00004714 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4715 if (!MaybeEntry) {
4716 // FIXME this drops the error on the floor.
4717 consumeError(MaybeEntry.takeError());
4718 return Failure;
4719 }
4720 llvm::BitstreamEntry Entry = MaybeEntry.get();
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004721
4722 switch (Entry.Kind) {
4723 case llvm::BitstreamEntry::Error:
4724 case llvm::BitstreamEntry::SubBlock:
4725 return Failure;
4726
4727 case llvm::BitstreamEntry::EndBlock:
4728 return Result;
4729
4730 case llvm::BitstreamEntry::Record:
4731 // The interesting case.
4732 break;
4733 }
4734
4735 // Read and process a record.
4736 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00004737 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4738 if (!MaybeRecordType) {
4739 // FIXME this drops the error.
4740 return Failure;
4741 }
4742 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00004743 case SIGNATURE:
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004744 if (F)
Daniel Grumberge87e55ed2020-06-07 20:05:25 +01004745 F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004746 break;
Daniel Grumbergbb8c7e72020-05-13 17:07:47 +01004747 case AST_BLOCK_HASH:
4748 if (F)
4749 F->ASTBlockHash =
4750 ASTFileSignature::create(Record.begin(), Record.end());
4751 break;
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004752 case DIAGNOSTIC_OPTIONS: {
4753 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4754 if (Listener && ValidateDiagnosticOptions &&
4755 !AllowCompatibleConfigurationMismatch &&
4756 ParseDiagnosticOptions(Record, Complain, *Listener))
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004757 Result = OutOfDate; // Don't return early. Read the signature.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004758 break;
4759 }
4760 case DIAG_PRAGMA_MAPPINGS:
4761 if (!F)
4762 break;
4763 if (F->PragmaDiagMappings.empty())
4764 F->PragmaDiagMappings.swap(Record);
4765 else
4766 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4767 Record.begin(), Record.end());
4768 break;
4769 }
4770 }
4771}
4772
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004773/// Parse a record and blob containing module file extension metadata.
4774static bool parseModuleFileExtensionMetadata(
4775 const SmallVectorImpl<uint64_t> &Record,
4776 StringRef Blob,
4777 ModuleFileExtensionMetadata &Metadata) {
4778 if (Record.size() < 4) return true;
4779
4780 Metadata.MajorVersion = Record[0];
4781 Metadata.MinorVersion = Record[1];
4782
4783 unsigned BlockNameLen = Record[2];
4784 unsigned UserInfoLen = Record[3];
4785
4786 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4787
4788 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4789 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4790 Blob.data() + BlockNameLen + UserInfoLen);
4791 return false;
4792}
4793
4794ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4795 BitstreamCursor &Stream = F.Stream;
4796
4797 RecordData Record;
4798 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00004799 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4800 if (!MaybeEntry) {
4801 Error(MaybeEntry.takeError());
4802 return Failure;
4803 }
4804 llvm::BitstreamEntry Entry = MaybeEntry.get();
4805
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004806 switch (Entry.Kind) {
4807 case llvm::BitstreamEntry::SubBlock:
JF Bastien0e828952019-06-26 19:50:12 +00004808 if (llvm::Error Err = Stream.SkipBlock()) {
4809 Error(std::move(Err));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004810 return Failure;
JF Bastien0e828952019-06-26 19:50:12 +00004811 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004812 continue;
4813
4814 case llvm::BitstreamEntry::EndBlock:
4815 return Success;
4816
4817 case llvm::BitstreamEntry::Error:
4818 return HadErrors;
4819
4820 case llvm::BitstreamEntry::Record:
4821 break;
4822 }
4823
4824 Record.clear();
4825 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00004826 Expected<unsigned> MaybeRecCode =
4827 Stream.readRecord(Entry.ID, Record, &Blob);
4828 if (!MaybeRecCode) {
4829 Error(MaybeRecCode.takeError());
4830 return Failure;
4831 }
4832 switch (MaybeRecCode.get()) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004833 case EXTENSION_METADATA: {
4834 ModuleFileExtensionMetadata Metadata;
Duncan P. N. Exon Smith8e2c1922019-11-10 11:07:20 -08004835 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4836 Error("malformed EXTENSION_METADATA in AST file");
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004837 return Failure;
Duncan P. N. Exon Smith8e2c1922019-11-10 11:07:20 -08004838 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004839
4840 // Find a module file extension with this block name.
4841 auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4842 if (Known == ModuleFileExtensions.end()) break;
4843
4844 // Form a reader.
4845 if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4846 F, Stream)) {
4847 F.ExtensionReaders.push_back(std::move(Reader));
4848 }
4849
4850 break;
4851 }
4852 }
4853 }
4854
4855 return Success;
Guy Benyei11169dd2012-12-18 14:30:41 +00004856}
4857
Richard Smitha7e2cc62015-05-01 01:53:09 +00004858void ASTReader::InitializeContext() {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004859 assert(ContextObj && "no context to initialize");
4860 ASTContext &Context = *ContextObj;
4861
Guy Benyei11169dd2012-12-18 14:30:41 +00004862 // If there's a listener, notify them that we "read" the translation unit.
4863 if (DeserializationListener)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004864 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
Guy Benyei11169dd2012-12-18 14:30:41 +00004865 Context.getTranslationUnitDecl());
4866
Guy Benyei11169dd2012-12-18 14:30:41 +00004867 // FIXME: Find a better way to deal with collisions between these
4868 // built-in types. Right now, we just ignore the problem.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004869
Guy Benyei11169dd2012-12-18 14:30:41 +00004870 // Load the special types.
4871 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4872 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4873 if (!Context.CFConstantStringTypeDecl)
4874 Context.setCFConstantStringType(GetType(String));
4875 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004876
Guy Benyei11169dd2012-12-18 14:30:41 +00004877 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4878 QualType FileType = GetType(File);
4879 if (FileType.isNull()) {
4880 Error("FILE type is NULL");
4881 return;
4882 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004883
Guy Benyei11169dd2012-12-18 14:30:41 +00004884 if (!Context.FILEDecl) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004885 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Guy Benyei11169dd2012-12-18 14:30:41 +00004886 Context.setFILEDecl(Typedef->getDecl());
4887 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004888 const TagType *Tag = FileType->getAs<TagType>();
Guy Benyei11169dd2012-12-18 14:30:41 +00004889 if (!Tag) {
4890 Error("Invalid FILE type in AST file");
4891 return;
4892 }
4893 Context.setFILEDecl(Tag->getDecl());
4894 }
4895 }
4896 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004897
Guy Benyei11169dd2012-12-18 14:30:41 +00004898 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4899 QualType Jmp_bufType = GetType(Jmp_buf);
4900 if (Jmp_bufType.isNull()) {
4901 Error("jmp_buf type is NULL");
4902 return;
4903 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004904
Guy Benyei11169dd2012-12-18 14:30:41 +00004905 if (!Context.jmp_bufDecl) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004906 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Guy Benyei11169dd2012-12-18 14:30:41 +00004907 Context.setjmp_bufDecl(Typedef->getDecl());
4908 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004909 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Guy Benyei11169dd2012-12-18 14:30:41 +00004910 if (!Tag) {
4911 Error("Invalid jmp_buf type in AST file");
4912 return;
4913 }
4914 Context.setjmp_bufDecl(Tag->getDecl());
4915 }
4916 }
4917 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004918
Guy Benyei11169dd2012-12-18 14:30:41 +00004919 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4920 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4921 if (Sigjmp_bufType.isNull()) {
4922 Error("sigjmp_buf type is NULL");
4923 return;
4924 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004925
Guy Benyei11169dd2012-12-18 14:30:41 +00004926 if (!Context.sigjmp_bufDecl) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004927 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Guy Benyei11169dd2012-12-18 14:30:41 +00004928 Context.setsigjmp_bufDecl(Typedef->getDecl());
4929 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004930 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Guy Benyei11169dd2012-12-18 14:30:41 +00004931 assert(Tag && "Invalid sigjmp_buf type in AST file");
4932 Context.setsigjmp_bufDecl(Tag->getDecl());
4933 }
4934 }
4935 }
4936
4937 if (unsigned ObjCIdRedef
4938 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4939 if (Context.ObjCIdRedefinitionType.isNull())
4940 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4941 }
4942
4943 if (unsigned ObjCClassRedef
4944 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4945 if (Context.ObjCClassRedefinitionType.isNull())
4946 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4947 }
4948
4949 if (unsigned ObjCSelRedef
4950 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4951 if (Context.ObjCSelRedefinitionType.isNull())
4952 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4953 }
4954
4955 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4956 QualType Ucontext_tType = GetType(Ucontext_t);
4957 if (Ucontext_tType.isNull()) {
4958 Error("ucontext_t type is NULL");
4959 return;
4960 }
4961
4962 if (!Context.ucontext_tDecl) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004963 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
Guy Benyei11169dd2012-12-18 14:30:41 +00004964 Context.setucontext_tDecl(Typedef->getDecl());
4965 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00004966 const TagType *Tag = Ucontext_tType->getAs<TagType>();
Guy Benyei11169dd2012-12-18 14:30:41 +00004967 assert(Tag && "Invalid ucontext_t type in AST file");
4968 Context.setucontext_tDecl(Tag->getDecl());
4969 }
4970 }
4971 }
4972 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004973
Guy Benyei11169dd2012-12-18 14:30:41 +00004974 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4975
4976 // If there were any CUDA special declarations, deserialize them.
4977 if (!CUDASpecialDeclRefs.empty()) {
4978 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4979 Context.setcudaConfigureCallDecl(
4980 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4981 }
Richard Smith56be7542014-03-21 00:33:59 +00004982
Guy Benyei11169dd2012-12-18 14:30:41 +00004983 // Re-export any modules that were imported by a non-module AST file.
Richard Smitha7e2cc62015-05-01 01:53:09 +00004984 // FIXME: This does not make macro-only imports visible again.
Richard Smith56be7542014-03-21 00:33:59 +00004985 for (auto &Import : ImportedModules) {
Richard Smitha7e2cc62015-05-01 01:53:09 +00004986 if (Module *Imported = getSubmodule(Import.ID)) {
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00004987 makeModuleVisible(Imported, Module::AllVisible,
Richard Smitha7e2cc62015-05-01 01:53:09 +00004988 /*ImportLoc=*/Import.ImportLoc);
Ben Langmuir6d25fdc2016-02-11 17:04:42 +00004989 if (Import.ImportLoc.isValid())
4990 PP.makeModuleVisible(Imported, Import.ImportLoc);
4991 // FIXME: should we tell Sema to make the module visible too?
Richard Smitha7e2cc62015-05-01 01:53:09 +00004992 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004993 }
4994 ImportedModules.clear();
4995}
4996
4997void ASTReader::finalizeForWriting() {
Richard Smithde711422015-04-23 21:20:19 +00004998 // Nothing to do for now.
Guy Benyei11169dd2012-12-18 14:30:41 +00004999}
5000
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005001/// Reads and return the signature record from \p PCH's control block, or
Peter Collingbourne77c89b62016-11-08 04:17:11 +00005002/// else returns 0.
5003static ASTFileSignature readASTFileSignature(StringRef PCH) {
5004 BitstreamCursor Stream(PCH);
JF Bastien0e828952019-06-26 19:50:12 +00005005 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5006 // FIXME this drops the error on the floor.
5007 consumeError(std::move(Err));
Vedant Kumar48b4f762018-04-14 01:40:48 +00005008 return ASTFileSignature();
JF Bastien0e828952019-06-26 19:50:12 +00005009 }
Ben Langmuir487ea142014-10-23 18:05:36 +00005010
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005011 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5012 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
Vedant Kumar48b4f762018-04-14 01:40:48 +00005013 return ASTFileSignature();
Ben Langmuir487ea142014-10-23 18:05:36 +00005014
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005015 // Scan for SIGNATURE inside the diagnostic options block.
Ben Langmuir487ea142014-10-23 18:05:36 +00005016 ASTReader::RecordData Record;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005017 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00005018 Expected<llvm::BitstreamEntry> MaybeEntry =
5019 Stream.advanceSkippingSubblocks();
5020 if (!MaybeEntry) {
5021 // FIXME this drops the error on the floor.
5022 consumeError(MaybeEntry.takeError());
5023 return ASTFileSignature();
5024 }
5025 llvm::BitstreamEntry Entry = MaybeEntry.get();
5026
Simon Pilgrim0b33f112016-11-16 16:11:08 +00005027 if (Entry.Kind != llvm::BitstreamEntry::Record)
Vedant Kumar48b4f762018-04-14 01:40:48 +00005028 return ASTFileSignature();
Ben Langmuir487ea142014-10-23 18:05:36 +00005029
5030 Record.clear();
5031 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00005032 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5033 if (!MaybeRecord) {
5034 // FIXME this drops the error on the floor.
5035 consumeError(MaybeRecord.takeError());
5036 return ASTFileSignature();
5037 }
5038 if (SIGNATURE == MaybeRecord.get())
Daniel Grumberge87e55ed2020-06-07 20:05:25 +01005039 return ASTFileSignature::create(Record.begin(),
5040 Record.begin() + ASTFileSignature::size);
Ben Langmuir487ea142014-10-23 18:05:36 +00005041 }
5042}
5043
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005044/// Retrieve the name of the original source file name
Guy Benyei11169dd2012-12-18 14:30:41 +00005045/// directly from the AST file, without actually loading the AST
5046/// file.
Adrian Prantlbb165fb2015-06-20 18:53:08 +00005047std::string ASTReader::getOriginalSourceFile(
5048 const std::string &ASTFileName, FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +00005049 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005050 // Open the AST file.
Benjamin Kramera8857962014-10-26 22:44:13 +00005051 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00005052 if (!Buffer) {
Benjamin Kramera8857962014-10-26 22:44:13 +00005053 Diags.Report(diag::err_fe_unable_to_read_pch_file)
5054 << ASTFileName << Buffer.getError().message();
Vedant Kumar48b4f762018-04-14 01:40:48 +00005055 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00005056 }
5057
5058 // Initialize the stream
Peter Collingbourne77c89b62016-11-08 04:17:11 +00005059 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
Guy Benyei11169dd2012-12-18 14:30:41 +00005060
5061 // Sniff for the signature.
JF Bastien0e828952019-06-26 19:50:12 +00005062 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5063 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
Vedant Kumar48b4f762018-04-14 01:40:48 +00005064 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00005065 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005066
Chris Lattnere7b154b2013-01-19 21:39:22 +00005067 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005068 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005069 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Vedant Kumar48b4f762018-04-14 01:40:48 +00005070 return std::string();
Chris Lattnere7b154b2013-01-19 21:39:22 +00005071 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005072
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005073 // Scan for ORIGINAL_FILE inside the control block.
5074 RecordData Record;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005075 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00005076 Expected<llvm::BitstreamEntry> MaybeEntry =
5077 Stream.advanceSkippingSubblocks();
5078 if (!MaybeEntry) {
5079 // FIXME this drops errors on the floor.
5080 consumeError(MaybeEntry.takeError());
5081 return std::string();
5082 }
5083 llvm::BitstreamEntry Entry = MaybeEntry.get();
5084
Chris Lattnere7b154b2013-01-19 21:39:22 +00005085 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
Vedant Kumar48b4f762018-04-14 01:40:48 +00005086 return std::string();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005087
Chris Lattnere7b154b2013-01-19 21:39:22 +00005088 if (Entry.Kind != llvm::BitstreamEntry::Record) {
5089 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Vedant Kumar48b4f762018-04-14 01:40:48 +00005090 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00005091 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005092
Guy Benyei11169dd2012-12-18 14:30:41 +00005093 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005094 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00005095 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5096 if (!MaybeRecord) {
5097 // FIXME this drops the errors on the floor.
5098 consumeError(MaybeRecord.takeError());
5099 return std::string();
5100 }
5101 if (ORIGINAL_FILE == MaybeRecord.get())
Chris Lattner0e6c9402013-01-20 02:38:54 +00005102 return Blob.str();
Guy Benyei11169dd2012-12-18 14:30:41 +00005103 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005104}
5105
5106namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005107
Guy Benyei11169dd2012-12-18 14:30:41 +00005108 class SimplePCHValidator : public ASTReaderListener {
5109 const LangOptions &ExistingLangOpts;
5110 const TargetOptions &ExistingTargetOpts;
5111 const PreprocessorOptions &ExistingPPOpts;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005112 std::string ExistingModuleCachePath;
Guy Benyei11169dd2012-12-18 14:30:41 +00005113 FileManager &FileMgr;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005114
Guy Benyei11169dd2012-12-18 14:30:41 +00005115 public:
5116 SimplePCHValidator(const LangOptions &ExistingLangOpts,
5117 const TargetOptions &ExistingTargetOpts,
5118 const PreprocessorOptions &ExistingPPOpts,
Benjamin Krameradcd0262020-01-28 20:23:46 +01005119 StringRef ExistingModuleCachePath, FileManager &FileMgr)
5120 : ExistingLangOpts(ExistingLangOpts),
5121 ExistingTargetOpts(ExistingTargetOpts),
5122 ExistingPPOpts(ExistingPPOpts),
5123 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
Guy Benyei11169dd2012-12-18 14:30:41 +00005124
Richard Smith1e2cf0d2014-10-31 02:28:58 +00005125 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5126 bool AllowCompatibleDifferences) override {
5127 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5128 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00005129 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005130
Chandler Carruth0d745bc2015-03-14 04:47:43 +00005131 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5132 bool AllowCompatibleDifferences) override {
5133 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5134 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00005135 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005136
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005137 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5138 StringRef SpecificModuleCachePath,
5139 bool Complain) override {
5140 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5141 ExistingModuleCachePath,
5142 nullptr, ExistingLangOpts);
5143 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005144
Craig Topper3e89dfe2014-03-13 02:13:41 +00005145 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5146 bool Complain,
5147 std::string &SuggestedPredefines) override {
Craig Toppera13603a2014-05-22 05:54:18 +00005148 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00005149 SuggestedPredefines, ExistingLangOpts);
Guy Benyei11169dd2012-12-18 14:30:41 +00005150 }
5151 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005152
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005153} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00005154
Adrian Prantlbb165fb2015-06-20 18:53:08 +00005155bool ASTReader::readASTFileControlBlock(
5156 StringRef Filename, FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +00005157 const PCHContainerReader &PCHContainerRdr,
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005158 bool FindModuleFileExtensions,
Manman Ren47a44452016-07-26 17:12:17 +00005159 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005160 // Open the AST file.
Richard Smith7f330cd2015-03-18 01:42:29 +00005161 // FIXME: This allows use of the VFS; we do not allow use of the
5162 // VFS when actually loading a module.
Benjamin Kramera8857962014-10-26 22:44:13 +00005163 auto Buffer = FileMgr.getBufferForFile(Filename);
Guy Benyei11169dd2012-12-18 14:30:41 +00005164 if (!Buffer) {
5165 return true;
5166 }
5167
5168 // Initialize the stream
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005169 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5170 BitstreamCursor Stream(Bytes);
Guy Benyei11169dd2012-12-18 14:30:41 +00005171
5172 // Sniff for the signature.
JF Bastien0e828952019-06-26 19:50:12 +00005173 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5174 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
Guy Benyei11169dd2012-12-18 14:30:41 +00005175 return true;
JF Bastien0e828952019-06-26 19:50:12 +00005176 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005177
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005178 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005179 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005180 return true;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005181
5182 bool NeedsInputFiles = Listener.needsInputFileVisitation();
Ben Langmuircb69b572014-03-07 06:40:32 +00005183 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
Richard Smithd4b230b2014-10-27 23:01:16 +00005184 bool NeedsImports = Listener.needsImportVisitation();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005185 BitstreamCursor InputFilesCursor;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005186
Guy Benyei11169dd2012-12-18 14:30:41 +00005187 RecordData Record;
Richard Smith7ed1bc92014-12-05 22:42:13 +00005188 std::string ModuleDir;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005189 bool DoneWithControlBlock = false;
5190 while (!DoneWithControlBlock) {
JF Bastien0e828952019-06-26 19:50:12 +00005191 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5192 if (!MaybeEntry) {
5193 // FIXME this drops the error on the floor.
5194 consumeError(MaybeEntry.takeError());
5195 return true;
5196 }
5197 llvm::BitstreamEntry Entry = MaybeEntry.get();
Richard Smith0516b182015-09-08 19:40:14 +00005198
5199 switch (Entry.Kind) {
5200 case llvm::BitstreamEntry::SubBlock: {
5201 switch (Entry.ID) {
5202 case OPTIONS_BLOCK_ID: {
5203 std::string IgnoredSuggestedPredefines;
5204 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5205 /*AllowCompatibleConfigurationMismatch*/ false,
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005206 Listener, IgnoredSuggestedPredefines) != Success)
Richard Smith0516b182015-09-08 19:40:14 +00005207 return true;
5208 break;
5209 }
5210
5211 case INPUT_FILES_BLOCK_ID:
5212 InputFilesCursor = Stream;
JF Bastien0e828952019-06-26 19:50:12 +00005213 if (llvm::Error Err = Stream.SkipBlock()) {
5214 // FIXME this drops the error on the floor.
5215 consumeError(std::move(Err));
5216 return true;
5217 }
5218 if (NeedsInputFiles &&
5219 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
Richard Smith0516b182015-09-08 19:40:14 +00005220 return true;
5221 break;
5222
5223 default:
JF Bastien0e828952019-06-26 19:50:12 +00005224 if (llvm::Error Err = Stream.SkipBlock()) {
5225 // FIXME this drops the error on the floor.
5226 consumeError(std::move(Err));
Richard Smith0516b182015-09-08 19:40:14 +00005227 return true;
JF Bastien0e828952019-06-26 19:50:12 +00005228 }
Richard Smith0516b182015-09-08 19:40:14 +00005229 break;
5230 }
5231
5232 continue;
5233 }
5234
5235 case llvm::BitstreamEntry::EndBlock:
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005236 DoneWithControlBlock = true;
5237 break;
Richard Smith0516b182015-09-08 19:40:14 +00005238
5239 case llvm::BitstreamEntry::Error:
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005240 return true;
Richard Smith0516b182015-09-08 19:40:14 +00005241
5242 case llvm::BitstreamEntry::Record:
5243 break;
5244 }
5245
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005246 if (DoneWithControlBlock) break;
5247
Guy Benyei11169dd2012-12-18 14:30:41 +00005248 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005249 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00005250 Expected<unsigned> MaybeRecCode =
5251 Stream.readRecord(Entry.ID, Record, &Blob);
5252 if (!MaybeRecCode) {
5253 // FIXME this drops the error.
5254 return Failure;
5255 }
5256 switch ((ControlRecordTypes)MaybeRecCode.get()) {
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005257 case METADATA:
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005258 if (Record[0] != VERSION_MAJOR)
5259 return true;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00005260 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005261 return true;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005262 break;
Ben Langmuir4f5212a2014-04-14 22:12:44 +00005263 case MODULE_NAME:
5264 Listener.ReadModuleName(Blob);
5265 break;
Richard Smith7ed1bc92014-12-05 22:42:13 +00005266 case MODULE_DIRECTORY:
Benjamin Krameradcd0262020-01-28 20:23:46 +01005267 ModuleDir = std::string(Blob);
Richard Smith7ed1bc92014-12-05 22:42:13 +00005268 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00005269 case MODULE_MAP_FILE: {
5270 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00005271 auto Path = ReadString(Record, Idx);
5272 ResolveImportedPath(Path, ModuleDir);
5273 Listener.ReadModuleMapFile(Path);
Ben Langmuir4f5212a2014-04-14 22:12:44 +00005274 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00005275 }
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005276 case INPUT_FILE_OFFSETS: {
5277 if (!NeedsInputFiles)
5278 break;
5279
5280 unsigned NumInputFiles = Record[0];
5281 unsigned NumUserFiles = Record[1];
Raphael Isemanneb13d3d2018-05-23 09:02:40 +00005282 const llvm::support::unaligned_uint64_t *InputFileOffs =
5283 (const llvm::support::unaligned_uint64_t *)Blob.data();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005284 for (unsigned I = 0; I != NumInputFiles; ++I) {
5285 // Go find this input file.
5286 bool isSystemFile = I >= NumUserFiles;
Ben Langmuircb69b572014-03-07 06:40:32 +00005287
5288 if (isSystemFile && !NeedsSystemInputFiles)
5289 break; // the rest are system input files
5290
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005291 BitstreamCursor &Cursor = InputFilesCursor;
5292 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00005293 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5294 // FIXME this drops errors on the floor.
5295 consumeError(std::move(Err));
5296 }
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005297
JF Bastien0e828952019-06-26 19:50:12 +00005298 Expected<unsigned> MaybeCode = Cursor.ReadCode();
5299 if (!MaybeCode) {
5300 // FIXME this drops errors on the floor.
5301 consumeError(MaybeCode.takeError());
5302 }
5303 unsigned Code = MaybeCode.get();
5304
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005305 RecordData Record;
5306 StringRef Blob;
5307 bool shouldContinue = false;
JF Bastien0e828952019-06-26 19:50:12 +00005308 Expected<unsigned> MaybeRecordType =
5309 Cursor.readRecord(Code, Record, &Blob);
5310 if (!MaybeRecordType) {
5311 // FIXME this drops errors on the floor.
5312 consumeError(MaybeRecordType.takeError());
5313 }
5314 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +00005315 case INPUT_FILE_HASH:
5316 break;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005317 case INPUT_FILE:
Vedant Kumar48b4f762018-04-14 01:40:48 +00005318 bool Overridden = static_cast<bool>(Record[3]);
Benjamin Krameradcd0262020-01-28 20:23:46 +01005319 std::string Filename = std::string(Blob);
Richard Smith7ed1bc92014-12-05 22:42:13 +00005320 ResolveImportedPath(Filename, ModuleDir);
Richard Smith216a3bd2015-08-13 17:57:10 +00005321 shouldContinue = Listener.visitInputFile(
5322 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00005323 break;
5324 }
5325 if (!shouldContinue)
5326 break;
5327 }
5328 break;
5329 }
5330
Richard Smithd4b230b2014-10-27 23:01:16 +00005331 case IMPORTS: {
5332 if (!NeedsImports)
5333 break;
5334
5335 unsigned Idx = 0, N = Record.size();
5336 while (Idx < N) {
5337 // Read information about the AST file.
Daniel Grumberge87e55ed2020-06-07 20:05:25 +01005338 Idx +=
5339 1 + 1 + 1 + 1 +
5340 ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
Bruno Cardoso Lopes6fc8a562018-09-11 05:17:13 +00005341 std::string ModuleName = ReadString(Record, Idx);
Richard Smith7ed1bc92014-12-05 22:42:13 +00005342 std::string Filename = ReadString(Record, Idx);
5343 ResolveImportedPath(Filename, ModuleDir);
Bruno Cardoso Lopes6fc8a562018-09-11 05:17:13 +00005344 Listener.visitImport(ModuleName, Filename);
Richard Smithd4b230b2014-10-27 23:01:16 +00005345 }
5346 break;
5347 }
5348
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005349 default:
5350 // No other validation to perform.
5351 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005352 }
5353 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005354
5355 // Look for module file extension blocks, if requested.
5356 if (FindModuleFileExtensions) {
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005357 BitstreamCursor SavedStream = Stream;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005358 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5359 bool DoneWithExtensionBlock = false;
5360 while (!DoneWithExtensionBlock) {
JF Bastien0e828952019-06-26 19:50:12 +00005361 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5362 if (!MaybeEntry) {
5363 // FIXME this drops the error.
5364 return true;
5365 }
5366 llvm::BitstreamEntry Entry = MaybeEntry.get();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005367
JF Bastien0e828952019-06-26 19:50:12 +00005368 switch (Entry.Kind) {
5369 case llvm::BitstreamEntry::SubBlock:
5370 if (llvm::Error Err = Stream.SkipBlock()) {
5371 // FIXME this drops the error on the floor.
5372 consumeError(std::move(Err));
5373 return true;
5374 }
5375 continue;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005376
JF Bastien0e828952019-06-26 19:50:12 +00005377 case llvm::BitstreamEntry::EndBlock:
5378 DoneWithExtensionBlock = true;
5379 continue;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005380
JF Bastien0e828952019-06-26 19:50:12 +00005381 case llvm::BitstreamEntry::Error:
5382 return true;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005383
JF Bastien0e828952019-06-26 19:50:12 +00005384 case llvm::BitstreamEntry::Record:
5385 break;
5386 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005387
5388 Record.clear();
5389 StringRef Blob;
JF Bastien0e828952019-06-26 19:50:12 +00005390 Expected<unsigned> MaybeRecCode =
5391 Stream.readRecord(Entry.ID, Record, &Blob);
5392 if (!MaybeRecCode) {
5393 // FIXME this drops the error.
5394 return true;
5395 }
5396 switch (MaybeRecCode.get()) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005397 case EXTENSION_METADATA: {
5398 ModuleFileExtensionMetadata Metadata;
5399 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5400 return true;
5401
5402 Listener.readModuleFileExtension(Metadata);
5403 break;
5404 }
5405 }
5406 }
5407 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005408 Stream = SavedStream;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005409 }
5410
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005411 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5412 if (readUnhashedControlBlockImpl(
5413 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5414 /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5415 ValidateDiagnosticOptions) != Success)
5416 return true;
5417
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005418 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00005419}
5420
Benjamin Kramerf6021ec2017-03-21 21:35:04 +00005421bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5422 const PCHContainerReader &PCHContainerRdr,
5423 const LangOptions &LangOpts,
5424 const TargetOptions &TargetOpts,
5425 const PreprocessorOptions &PPOpts,
5426 StringRef ExistingModuleCachePath) {
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005427 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5428 ExistingModuleCachePath, FileMgr);
Adrian Prantlfb2398d2015-07-17 01:19:54 +00005429 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005430 /*FindModuleFileExtensions=*/false,
Manman Ren47a44452016-07-26 17:12:17 +00005431 validator,
5432 /*ValidateDiagnosticOptions=*/true);
Guy Benyei11169dd2012-12-18 14:30:41 +00005433}
5434
Ben Langmuir2c9af442014-04-10 17:57:43 +00005435ASTReader::ASTReadResult
5436ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005437 // Enter the submodule block.
JF Bastien0e828952019-06-26 19:50:12 +00005438 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5439 Error(std::move(Err));
Ben Langmuir2c9af442014-04-10 17:57:43 +00005440 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00005441 }
5442
5443 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5444 bool First = true;
Craig Toppera13603a2014-05-22 05:54:18 +00005445 Module *CurrentModule = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005446 RecordData Record;
5447 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00005448 Expected<llvm::BitstreamEntry> MaybeEntry =
5449 F.Stream.advanceSkippingSubblocks();
5450 if (!MaybeEntry) {
5451 Error(MaybeEntry.takeError());
5452 return Failure;
5453 }
5454 llvm::BitstreamEntry Entry = MaybeEntry.get();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005455
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005456 switch (Entry.Kind) {
5457 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5458 case llvm::BitstreamEntry::Error:
5459 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00005460 return Failure;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005461 case llvm::BitstreamEntry::EndBlock:
Ben Langmuir2c9af442014-04-10 17:57:43 +00005462 return Success;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005463 case llvm::BitstreamEntry::Record:
5464 // The interesting case.
5465 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005466 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005467
Guy Benyei11169dd2012-12-18 14:30:41 +00005468 // Read a record.
Chris Lattner0e6c9402013-01-20 02:38:54 +00005469 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00005470 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00005471 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5472 if (!MaybeKind) {
5473 Error(MaybeKind.takeError());
5474 return Failure;
5475 }
5476 unsigned Kind = MaybeKind.get();
Richard Smith03478d92014-10-23 22:12:14 +00005477
5478 if ((Kind == SUBMODULE_METADATA) != First) {
5479 Error("submodule metadata record should be at beginning of block");
5480 return Failure;
5481 }
5482 First = false;
5483
5484 // Submodule information is only valid if we have a current module.
5485 // FIXME: Should we error on these cases?
5486 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5487 Kind != SUBMODULE_DEFINITION)
5488 continue;
5489
5490 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005491 default: // Default behavior: ignore.
5492 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005493
Richard Smith03478d92014-10-23 22:12:14 +00005494 case SUBMODULE_DEFINITION: {
Jordan Rose90b0a1f2018-04-20 17:16:04 +00005495 if (Record.size() < 12) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005496 Error("malformed module definition");
Ben Langmuir2c9af442014-04-10 17:57:43 +00005497 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00005498 }
Richard Smith03478d92014-10-23 22:12:14 +00005499
Chris Lattner0e6c9402013-01-20 02:38:54 +00005500 StringRef Name = Blob;
Richard Smith9bca2982014-03-08 00:03:56 +00005501 unsigned Idx = 0;
5502 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5503 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
Vedant Kumar48b4f762018-04-14 01:40:48 +00005504 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
Richard Smith9bca2982014-03-08 00:03:56 +00005505 bool IsFramework = Record[Idx++];
5506 bool IsExplicit = Record[Idx++];
5507 bool IsSystem = Record[Idx++];
5508 bool IsExternC = Record[Idx++];
5509 bool InferSubmodules = Record[Idx++];
5510 bool InferExplicitSubmodules = Record[Idx++];
5511 bool InferExportWildcard = Record[Idx++];
5512 bool ConfigMacrosExhaustive = Record[Idx++];
Jordan Rose90b0a1f2018-04-20 17:16:04 +00005513 bool ModuleMapIsPrivate = Record[Idx++];
Douglas Gregor8d932422013-03-20 03:59:18 +00005514
Ben Langmuirbeee15e2014-04-14 18:00:01 +00005515 Module *ParentModule = nullptr;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00005516 if (Parent)
Guy Benyei11169dd2012-12-18 14:30:41 +00005517 ParentModule = getSubmodule(Parent);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00005518
Guy Benyei11169dd2012-12-18 14:30:41 +00005519 // Retrieve this (sub)module from the module map, creating it if
5520 // necessary.
David Blaikie9ffe5a32017-01-30 05:00:26 +00005521 CurrentModule =
5522 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5523 .first;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00005524
5525 // FIXME: set the definition loc for CurrentModule, or call
5526 // ModMap.setInferredModuleAllowedBy()
5527
Guy Benyei11169dd2012-12-18 14:30:41 +00005528 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5529 if (GlobalIndex >= SubmodulesLoaded.size() ||
5530 SubmodulesLoaded[GlobalIndex]) {
5531 Error("too many submodules");
Ben Langmuir2c9af442014-04-10 17:57:43 +00005532 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00005533 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00005534
Douglas Gregor7029ce12013-03-19 00:28:20 +00005535 if (!ParentModule) {
5536 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
Yuka Takahashid8baec22018-08-01 09:50:02 +00005537 // Don't emit module relocation error if we have -fno-validate-pch
5538 if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5539 CurFile != F.File) {
Duncan P. N. Exon Smitheef69022019-11-10 11:17:42 -08005540 Error(diag::err_module_file_conflict,
5541 CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5542 F.File->getName());
Ben Langmuir2c9af442014-04-10 17:57:43 +00005543 return Failure;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00005544 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00005545 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00005546
Duncan P. N. Exon Smith83dcb342019-11-10 13:14:52 -08005547 F.DidReadTopLevelSubmodule = true;
Douglas Gregor7029ce12013-03-19 00:28:20 +00005548 CurrentModule->setASTFile(F.File);
Richard Smithab755972017-06-05 18:10:11 +00005549 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00005550 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00005551
Richard Smithdd8b5332017-09-04 05:37:53 +00005552 CurrentModule->Kind = Kind;
Adrian Prantl15bcf702015-06-30 17:39:43 +00005553 CurrentModule->Signature = F.Signature;
Guy Benyei11169dd2012-12-18 14:30:41 +00005554 CurrentModule->IsFromModuleFile = true;
5555 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Richard Smith9bca2982014-03-08 00:03:56 +00005556 CurrentModule->IsExternC = IsExternC;
Guy Benyei11169dd2012-12-18 14:30:41 +00005557 CurrentModule->InferSubmodules = InferSubmodules;
5558 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5559 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor8d932422013-03-20 03:59:18 +00005560 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Jordan Rose90b0a1f2018-04-20 17:16:04 +00005561 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
Guy Benyei11169dd2012-12-18 14:30:41 +00005562 if (DeserializationListener)
5563 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005564
Guy Benyei11169dd2012-12-18 14:30:41 +00005565 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005566
Richard Smith8a3e39a2016-03-28 21:31:09 +00005567 // Clear out data that will be replaced by what is in the module file.
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005568 CurrentModule->LinkLibraries.clear();
Douglas Gregor8d932422013-03-20 03:59:18 +00005569 CurrentModule->ConfigMacros.clear();
Douglas Gregorfb912652013-03-20 21:10:35 +00005570 CurrentModule->UnresolvedConflicts.clear();
5571 CurrentModule->Conflicts.clear();
Richard Smith8a3e39a2016-03-28 21:31:09 +00005572
5573 // The module is available unless it's missing a requirement; relevant
5574 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5575 // Missing headers that were present when the module was built do not
5576 // make it unavailable -- if we got this far, this must be an explicitly
5577 // imported module file.
5578 CurrentModule->Requirements.clear();
5579 CurrentModule->MissingHeaders.clear();
Richard Smithfc76b4a2020-04-17 16:23:41 -07005580 CurrentModule->IsUnimportable =
5581 ParentModule && ParentModule->IsUnimportable;
5582 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
Guy Benyei11169dd2012-12-18 14:30:41 +00005583 break;
5584 }
Richard Smith8a3e39a2016-03-28 21:31:09 +00005585
Guy Benyei11169dd2012-12-18 14:30:41 +00005586 case SUBMODULE_UMBRELLA_HEADER: {
Benjamin Krameradcd0262020-01-28 20:23:46 +01005587 std::string Filename = std::string(Blob);
Richard Smith2b63d152015-05-16 02:28:53 +00005588 ResolveImportedPath(F, Filename);
Harlan Haskins8d323d12019-08-01 21:31:56 +00005589 if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005590 if (!CurrentModule->getUmbrellaHeader())
Harlan Haskins8d323d12019-08-01 21:31:56 +00005591 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5592 else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
Bruno Cardoso Lopes573b13f2017-03-22 00:11:21 +00005593 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5594 Error("mismatched umbrella headers in submodule");
5595 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00005596 }
5597 }
5598 break;
5599 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005600
Richard Smith202210b2014-10-24 20:23:01 +00005601 case SUBMODULE_HEADER:
5602 case SUBMODULE_EXCLUDED_HEADER:
5603 case SUBMODULE_PRIVATE_HEADER:
5604 // We lazily associate headers with their modules via the HeaderInfo table.
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00005605 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5606 // of complete filenames or remove it entirely.
Richard Smith202210b2014-10-24 20:23:01 +00005607 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005608
Richard Smith202210b2014-10-24 20:23:01 +00005609 case SUBMODULE_TEXTUAL_HEADER:
5610 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5611 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5612 // them here.
5613 break;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00005614
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005615 case SUBMODULE_TOPHEADER:
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00005616 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00005617 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005618
5619 case SUBMODULE_UMBRELLA_DIR: {
Benjamin Krameradcd0262020-01-28 20:23:46 +01005620 std::string Dirname = std::string(Blob);
Richard Smith2b63d152015-05-16 02:28:53 +00005621 ResolveImportedPath(F, Dirname);
Harlan Haskins8d323d12019-08-01 21:31:56 +00005622 if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005623 if (!CurrentModule->getUmbrellaDir())
Harlan Haskins8d323d12019-08-01 21:31:56 +00005624 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5625 else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
Ben Langmuir2c9af442014-04-10 17:57:43 +00005626 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5627 Error("mismatched umbrella directories in submodule");
5628 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00005629 }
5630 }
5631 break;
5632 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005633
Guy Benyei11169dd2012-12-18 14:30:41 +00005634 case SUBMODULE_METADATA: {
Guy Benyei11169dd2012-12-18 14:30:41 +00005635 F.BaseSubmoduleID = getTotalNumSubmodules();
5636 F.LocalNumSubmodules = Record[0];
5637 unsigned LocalBaseSubmoduleID = Record[1];
5638 if (F.LocalNumSubmodules > 0) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005639 // Introduce the global -> local mapping for submodules within this
Guy Benyei11169dd2012-12-18 14:30:41 +00005640 // module.
5641 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005642
5643 // Introduce the local -> global mapping for submodules within this
Guy Benyei11169dd2012-12-18 14:30:41 +00005644 // module.
5645 F.SubmoduleRemap.insertOrReplace(
5646 std::make_pair(LocalBaseSubmoduleID,
5647 F.BaseSubmoduleID - LocalBaseSubmoduleID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00005648
Ben Langmuir52ca6782014-10-20 16:27:32 +00005649 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5650 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005651 break;
5652 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005653
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005654 case SUBMODULE_IMPORTS:
Guy Benyei11169dd2012-12-18 14:30:41 +00005655 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregorfb912652013-03-20 21:10:35 +00005656 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00005657 Unresolved.File = &F;
5658 Unresolved.Mod = CurrentModule;
5659 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00005660 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei11169dd2012-12-18 14:30:41 +00005661 Unresolved.IsWildcard = false;
Douglas Gregorfb912652013-03-20 21:10:35 +00005662 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00005663 }
5664 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005665
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005666 case SUBMODULE_EXPORTS:
Guy Benyei11169dd2012-12-18 14:30:41 +00005667 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregorfb912652013-03-20 21:10:35 +00005668 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00005669 Unresolved.File = &F;
5670 Unresolved.Mod = CurrentModule;
5671 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00005672 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei11169dd2012-12-18 14:30:41 +00005673 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregorfb912652013-03-20 21:10:35 +00005674 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00005675 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005676
5677 // Once we've loaded the set of exports, there's no reason to keep
Guy Benyei11169dd2012-12-18 14:30:41 +00005678 // the parsed, unresolved exports around.
5679 CurrentModule->UnresolvedExports.clear();
5680 break;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00005681
5682 case SUBMODULE_REQUIRES:
Richard Smithdbafb6c2017-06-29 23:23:46 +00005683 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5684 PP.getTargetInfo());
Guy Benyei11169dd2012-12-18 14:30:41 +00005685 break;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005686
5687 case SUBMODULE_LINK_LIBRARY:
Bruno Cardoso Lopesa3b5f712018-04-16 19:42:32 +00005688 ModMap.resolveLinkAsDependencies(CurrentModule);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005689 CurrentModule->LinkLibraries.push_back(
Benjamin Krameradcd0262020-01-28 20:23:46 +01005690 Module::LinkLibrary(std::string(Blob), Record[0]));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00005691 break;
Douglas Gregor35b13ec2013-03-20 00:22:05 +00005692
5693 case SUBMODULE_CONFIG_MACRO:
Douglas Gregor35b13ec2013-03-20 00:22:05 +00005694 CurrentModule->ConfigMacros.push_back(Blob.str());
5695 break;
Douglas Gregorfb912652013-03-20 21:10:35 +00005696
5697 case SUBMODULE_CONFLICT: {
Douglas Gregorfb912652013-03-20 21:10:35 +00005698 UnresolvedModuleRef Unresolved;
5699 Unresolved.File = &F;
5700 Unresolved.Mod = CurrentModule;
5701 Unresolved.ID = Record[0];
5702 Unresolved.Kind = UnresolvedModuleRef::Conflict;
5703 Unresolved.IsWildcard = false;
5704 Unresolved.String = Blob;
5705 UnresolvedModuleRefs.push_back(Unresolved);
5706 break;
5707 }
Richard Smithdc1f0422016-07-20 19:10:16 +00005708
Douglas Gregorf0b11de2017-09-14 23:38:44 +00005709 case SUBMODULE_INITIALIZERS: {
Richard Smithdbafb6c2017-06-29 23:23:46 +00005710 if (!ContextObj)
5711 break;
Richard Smithdc1f0422016-07-20 19:10:16 +00005712 SmallVector<uint32_t, 16> Inits;
5713 for (auto &ID : Record)
5714 Inits.push_back(getGlobalDeclID(F, ID));
Richard Smithdbafb6c2017-06-29 23:23:46 +00005715 ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
Richard Smithdc1f0422016-07-20 19:10:16 +00005716 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005717 }
Douglas Gregorf0b11de2017-09-14 23:38:44 +00005718
5719 case SUBMODULE_EXPORT_AS:
5720 CurrentModule->ExportAsModule = Blob.str();
Bruno Cardoso Lopesa3b5f712018-04-16 19:42:32 +00005721 ModMap.addLinkAsDependency(CurrentModule);
Douglas Gregorf0b11de2017-09-14 23:38:44 +00005722 break;
5723 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005724 }
5725}
5726
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005727/// Parse the record that corresponds to a LangOptions data
Guy Benyei11169dd2012-12-18 14:30:41 +00005728/// structure.
5729///
5730/// This routine parses the language options from the AST file and then gives
5731/// them to the AST listener if one is set.
5732///
5733/// \returns true if the listener deems the file unacceptable, false otherwise.
5734bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5735 bool Complain,
Richard Smith1e2cf0d2014-10-31 02:28:58 +00005736 ASTReaderListener &Listener,
5737 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005738 LangOptions LangOpts;
5739 unsigned Idx = 0;
5740#define LANGOPT(Name, Bits, Default, Description) \
5741 LangOpts.Name = Record[Idx++];
5742#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5743 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5744#include "clang/Basic/LangOptions.def"
Alexey Samsonovedf99a92014-11-07 22:29:38 +00005745#define SANITIZER(NAME, ID) \
5746 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
Will Dietzf54319c2013-01-18 11:30:38 +00005747#include "clang/Basic/Sanitizers.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00005748
Ben Langmuircd98cb72015-06-23 18:20:18 +00005749 for (unsigned N = Record[Idx++]; N; --N)
5750 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5751
Vedant Kumar48b4f762018-04-14 01:40:48 +00005752 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00005753 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5754 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00005755
Ben Langmuird4a667a2015-06-23 18:20:23 +00005756 LangOpts.CurrentModule = ReadString(Record, Idx);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00005757
5758 // Comment options.
5759 for (unsigned N = Record[Idx++]; N; --N) {
5760 LangOpts.CommentOpts.BlockCommandNames.push_back(
5761 ReadString(Record, Idx));
5762 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00005763 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00005764
Samuel Antaoee8fb302016-01-06 13:42:12 +00005765 // OpenMP offloading options.
5766 for (unsigned N = Record[Idx++]; N; --N) {
5767 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5768 }
5769
5770 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5771
Richard Smith1e2cf0d2014-10-31 02:28:58 +00005772 return Listener.ReadLanguageOptions(LangOpts, Complain,
5773 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00005774}
5775
Chandler Carruth0d745bc2015-03-14 04:47:43 +00005776bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5777 ASTReaderListener &Listener,
5778 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005779 unsigned Idx = 0;
5780 TargetOptions TargetOpts;
5781 TargetOpts.Triple = ReadString(Record, Idx);
5782 TargetOpts.CPU = ReadString(Record, Idx);
5783 TargetOpts.ABI = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005784 for (unsigned N = Record[Idx++]; N; --N) {
5785 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5786 }
5787 for (unsigned N = Record[Idx++]; N; --N) {
5788 TargetOpts.Features.push_back(ReadString(Record, Idx));
5789 }
5790
Chandler Carruth0d745bc2015-03-14 04:47:43 +00005791 return Listener.ReadTargetOptions(TargetOpts, Complain,
5792 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00005793}
5794
5795bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5796 ASTReaderListener &Listener) {
Ben Langmuirb92de022014-04-29 16:25:26 +00005797 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
Guy Benyei11169dd2012-12-18 14:30:41 +00005798 unsigned Idx = 0;
Ben Langmuirb92de022014-04-29 16:25:26 +00005799#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00005800#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
Ben Langmuirb92de022014-04-29 16:25:26 +00005801 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00005802#include "clang/Basic/DiagnosticOptions.def"
5803
Richard Smith3be1cb22014-08-07 00:24:21 +00005804 for (unsigned N = Record[Idx++]; N; --N)
Ben Langmuirb92de022014-04-29 16:25:26 +00005805 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
Richard Smith3be1cb22014-08-07 00:24:21 +00005806 for (unsigned N = Record[Idx++]; N; --N)
5807 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005808
5809 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5810}
5811
5812bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5813 ASTReaderListener &Listener) {
5814 FileSystemOptions FSOpts;
5815 unsigned Idx = 0;
5816 FSOpts.WorkingDir = ReadString(Record, Idx);
5817 return Listener.ReadFileSystemOptions(FSOpts, Complain);
5818}
5819
5820bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5821 bool Complain,
5822 ASTReaderListener &Listener) {
5823 HeaderSearchOptions HSOpts;
5824 unsigned Idx = 0;
5825 HSOpts.Sysroot = ReadString(Record, Idx);
5826
5827 // Include entries.
5828 for (unsigned N = Record[Idx++]; N; --N) {
5829 std::string Path = ReadString(Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00005830 frontend::IncludeDirGroup Group
5831 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00005832 bool IsFramework = Record[Idx++];
5833 bool IgnoreSysRoot = Record[Idx++];
Benjamin Kramer3204b152015-05-29 19:42:19 +00005834 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5835 IgnoreSysRoot);
Guy Benyei11169dd2012-12-18 14:30:41 +00005836 }
5837
5838 // System header prefixes.
5839 for (unsigned N = Record[Idx++]; N; --N) {
5840 std::string Prefix = ReadString(Record, Idx);
5841 bool IsSystemHeader = Record[Idx++];
Benjamin Kramer3204b152015-05-29 19:42:19 +00005842 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
Guy Benyei11169dd2012-12-18 14:30:41 +00005843 }
5844
5845 HSOpts.ResourceDir = ReadString(Record, Idx);
5846 HSOpts.ModuleCachePath = ReadString(Record, Idx);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00005847 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005848 HSOpts.DisableModuleHash = Record[Idx++];
Richard Smith18934752017-06-06 00:32:01 +00005849 HSOpts.ImplicitModuleMaps = Record[Idx++];
5850 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00005851 HSOpts.UseBuiltinIncludes = Record[Idx++];
5852 HSOpts.UseStandardSystemIncludes = Record[Idx++];
5853 HSOpts.UseStandardCXXIncludes = Record[Idx++];
5854 HSOpts.UseLibcxx = Record[Idx++];
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005855 std::string SpecificModuleCachePath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005856
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00005857 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5858 Complain);
Guy Benyei11169dd2012-12-18 14:30:41 +00005859}
5860
5861bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5862 bool Complain,
5863 ASTReaderListener &Listener,
5864 std::string &SuggestedPredefines) {
5865 PreprocessorOptions PPOpts;
5866 unsigned Idx = 0;
5867
5868 // Macro definitions/undefs
5869 for (unsigned N = Record[Idx++]; N; --N) {
5870 std::string Macro = ReadString(Record, Idx);
5871 bool IsUndef = Record[Idx++];
5872 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5873 }
5874
5875 // Includes
5876 for (unsigned N = Record[Idx++]; N; --N) {
5877 PPOpts.Includes.push_back(ReadString(Record, Idx));
5878 }
5879
5880 // Macro Includes
5881 for (unsigned N = Record[Idx++]; N; --N) {
5882 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5883 }
5884
5885 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00005886 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00005887 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005888 PPOpts.ObjCXXARCStandardLibrary =
5889 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5890 SuggestedPredefines.clear();
5891 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5892 SuggestedPredefines);
5893}
5894
5895std::pair<ModuleFile *, unsigned>
5896ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5897 GlobalPreprocessedEntityMapType::iterator
5898 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005899 assert(I != GlobalPreprocessedEntityMap.end() &&
Guy Benyei11169dd2012-12-18 14:30:41 +00005900 "Corrupted global preprocessed entity map");
5901 ModuleFile *M = I->second;
5902 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5903 return std::make_pair(M, LocalIndex);
5904}
5905
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00005906llvm::iterator_range<PreprocessingRecord::iterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00005907ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5908 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5909 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5910 Mod.NumPreprocessedEntities);
5911
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00005912 return llvm::make_range(PreprocessingRecord::iterator(),
5913 PreprocessingRecord::iterator());
Guy Benyei11169dd2012-12-18 14:30:41 +00005914}
5915
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00005916llvm::iterator_range<ASTReader::ModuleDeclIterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00005917ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00005918 return llvm::make_range(
5919 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5920 ModuleDeclIterator(this, &Mod,
5921 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
Guy Benyei11169dd2012-12-18 14:30:41 +00005922}
5923
Cameron Desrochersb60f1b62018-01-15 19:14:16 +00005924SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5925 auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5926 assert(I != GlobalSkippedRangeMap.end() &&
5927 "Corrupted global skipped range map");
5928 ModuleFile *M = I->second;
5929 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5930 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5931 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5932 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5933 TranslateSourceLocation(*M, RawRange.getEnd()));
5934 assert(Range.isValid());
5935 return Range;
5936}
5937
Guy Benyei11169dd2012-12-18 14:30:41 +00005938PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5939 PreprocessedEntityID PPID = Index+1;
5940 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5941 ModuleFile &M = *PPInfo.first;
5942 unsigned LocalIndex = PPInfo.second;
5943 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5944
Guy Benyei11169dd2012-12-18 14:30:41 +00005945 if (!PP.getPreprocessingRecord()) {
5946 Error("no preprocessing record");
Craig Toppera13603a2014-05-22 05:54:18 +00005947 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005948 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00005949
5950 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Dmitry Polukhina7afb212020-04-16 09:24:46 -07005951 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5952 M.MacroOffsetsBase + PPOffs.BitOffset)) {
JF Bastien0e828952019-06-26 19:50:12 +00005953 Error(std::move(Err));
5954 return nullptr;
5955 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005956
JF Bastien0e828952019-06-26 19:50:12 +00005957 Expected<llvm::BitstreamEntry> MaybeEntry =
5958 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5959 if (!MaybeEntry) {
5960 Error(MaybeEntry.takeError());
5961 return nullptr;
5962 }
5963 llvm::BitstreamEntry Entry = MaybeEntry.get();
5964
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005965 if (Entry.Kind != llvm::BitstreamEntry::Record)
Craig Toppera13603a2014-05-22 05:54:18 +00005966 return nullptr;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005967
Guy Benyei11169dd2012-12-18 14:30:41 +00005968 // Read the record.
Richard Smithcb34bd32016-03-27 07:28:06 +00005969 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5970 TranslateSourceLocation(M, PPOffs.getEnd()));
Guy Benyei11169dd2012-12-18 14:30:41 +00005971 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005972 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00005973 RecordData Record;
JF Bastien0e828952019-06-26 19:50:12 +00005974 Expected<unsigned> MaybeRecType =
5975 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5976 if (!MaybeRecType) {
5977 Error(MaybeRecType.takeError());
5978 return nullptr;
5979 }
5980 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005981 case PPD_MACRO_EXPANSION: {
5982 bool isBuiltin = Record[0];
Craig Toppera13603a2014-05-22 05:54:18 +00005983 IdentifierInfo *Name = nullptr;
Richard Smith66a81862015-05-04 02:25:31 +00005984 MacroDefinitionRecord *Def = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005985 if (isBuiltin)
5986 Name = getLocalIdentifier(M, Record[1]);
5987 else {
Richard Smith66a81862015-05-04 02:25:31 +00005988 PreprocessedEntityID GlobalID =
5989 getGlobalPreprocessedEntityID(M, Record[1]);
5990 Def = cast<MacroDefinitionRecord>(
5991 PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
Guy Benyei11169dd2012-12-18 14:30:41 +00005992 }
5993
5994 MacroExpansion *ME;
5995 if (isBuiltin)
5996 ME = new (PPRec) MacroExpansion(Name, Range);
5997 else
5998 ME = new (PPRec) MacroExpansion(Def, Range);
5999
6000 return ME;
6001 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006002
Guy Benyei11169dd2012-12-18 14:30:41 +00006003 case PPD_MACRO_DEFINITION: {
6004 // Decode the identifier info and then check again; if the macro is
6005 // still defined and associated with the identifier,
6006 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Vedant Kumar48b4f762018-04-14 01:40:48 +00006007 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00006008
6009 if (DeserializationListener)
6010 DeserializationListener->MacroDefinitionRead(PPID, MD);
6011
6012 return MD;
6013 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006014
Guy Benyei11169dd2012-12-18 14:30:41 +00006015 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00006016 const char *FullFileNameStart = Blob.data() + Record[0];
6017 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Craig Toppera13603a2014-05-22 05:54:18 +00006018 const FileEntry *File = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006019 if (!FullFileName.empty())
Harlan Haskins8d323d12019-08-01 21:31:56 +00006020 if (auto FE = PP.getFileManager().getFile(FullFileName))
6021 File = *FE;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006022
Guy Benyei11169dd2012-12-18 14:30:41 +00006023 // FIXME: Stable encoding
Vedant Kumar48b4f762018-04-14 01:40:48 +00006024 InclusionDirective::InclusionKind Kind
6025 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6026 InclusionDirective *ID
6027 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e6c9402013-01-20 02:38:54 +00006028 StringRef(Blob.data(), Record[0]),
Guy Benyei11169dd2012-12-18 14:30:41 +00006029 Record[1], Record[3],
6030 File,
6031 Range);
6032 return ID;
6033 }
6034 }
6035
6036 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6037}
6038
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006039/// Find the next module that contains entities and return the ID
Guy Benyei11169dd2012-12-18 14:30:41 +00006040/// of the first entry.
NAKAMURA Takumi12ab07e2017-10-12 09:42:14 +00006041///
6042/// \param SLocMapI points at a chunk of a module that contains no
6043/// preprocessed entities or the entities it contains are not the ones we are
6044/// looking for.
Guy Benyei11169dd2012-12-18 14:30:41 +00006045PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6046 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6047 ++SLocMapI;
6048 for (GlobalSLocOffsetMapType::const_iterator
6049 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6050 ModuleFile &M = *SLocMapI->second;
6051 if (M.NumPreprocessedEntities)
6052 return M.BasePreprocessedEntityID;
6053 }
6054
6055 return getTotalNumPreprocessedEntities();
6056}
6057
6058namespace {
6059
Guy Benyei11169dd2012-12-18 14:30:41 +00006060struct PPEntityComp {
6061 const ASTReader &Reader;
6062 ModuleFile &M;
6063
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006064 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
Guy Benyei11169dd2012-12-18 14:30:41 +00006065
6066 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6067 SourceLocation LHS = getLoc(L);
6068 SourceLocation RHS = getLoc(R);
6069 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6070 }
6071
6072 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6073 SourceLocation LHS = getLoc(L);
6074 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6075 }
6076
6077 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6078 SourceLocation RHS = getLoc(R);
6079 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6080 }
6081
6082 SourceLocation getLoc(const PPEntityOffset &PPE) const {
Richard Smithb22a1d12016-03-27 20:13:24 +00006083 return Reader.TranslateSourceLocation(M, PPE.getBegin());
Guy Benyei11169dd2012-12-18 14:30:41 +00006084 }
6085};
6086
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006087} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00006088
Alp Toker2e9ce4c2014-05-16 18:59:21 +00006089PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6090 bool EndsAfter) const {
6091 if (SourceMgr.isLocalSourceLocation(Loc))
Guy Benyei11169dd2012-12-18 14:30:41 +00006092 return getTotalNumPreprocessedEntities();
6093
Alp Toker2e9ce4c2014-05-16 18:59:21 +00006094 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6095 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00006096 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6097 "Corrupted global sloc offset map");
6098
6099 if (SLocMapI->second->NumPreprocessedEntities == 0)
6100 return findNextPreprocessedEntity(SLocMapI);
6101
6102 ModuleFile &M = *SLocMapI->second;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006103
6104 using pp_iterator = const PPEntityOffset *;
6105
Guy Benyei11169dd2012-12-18 14:30:41 +00006106 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6107 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6108
6109 size_t Count = M.NumPreprocessedEntities;
6110 size_t Half;
6111 pp_iterator First = pp_begin;
6112 pp_iterator PPI;
6113
Alp Toker2e9ce4c2014-05-16 18:59:21 +00006114 if (EndsAfter) {
6115 PPI = std::upper_bound(pp_begin, pp_end, Loc,
Richard Smithb22a1d12016-03-27 20:13:24 +00006116 PPEntityComp(*this, M));
Alp Toker2e9ce4c2014-05-16 18:59:21 +00006117 } else {
6118 // Do a binary search manually instead of using std::lower_bound because
6119 // The end locations of entities may be unordered (when a macro expansion
6120 // is inside another macro argument), but for this case it is not important
6121 // whether we get the first macro expansion or its containing macro.
6122 while (Count > 0) {
6123 Half = Count / 2;
6124 PPI = First;
6125 std::advance(PPI, Half);
Richard Smithb22a1d12016-03-27 20:13:24 +00006126 if (SourceMgr.isBeforeInTranslationUnit(
6127 TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
Alp Toker2e9ce4c2014-05-16 18:59:21 +00006128 First = PPI;
6129 ++First;
6130 Count = Count - Half - 1;
6131 } else
6132 Count = Half;
6133 }
Guy Benyei11169dd2012-12-18 14:30:41 +00006134 }
6135
6136 if (PPI == pp_end)
6137 return findNextPreprocessedEntity(SLocMapI);
6138
6139 return M.BasePreprocessedEntityID + (PPI - pp_begin);
6140}
6141
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006142/// Returns a pair of [Begin, End) indices of preallocated
Guy Benyei11169dd2012-12-18 14:30:41 +00006143/// preprocessed entities that \arg Range encompasses.
6144std::pair<unsigned, unsigned>
6145 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6146 if (Range.isInvalid())
6147 return std::make_pair(0,0);
6148 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6149
Alp Toker2e9ce4c2014-05-16 18:59:21 +00006150 PreprocessedEntityID BeginID =
6151 findPreprocessedEntity(Range.getBegin(), false);
6152 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
Guy Benyei11169dd2012-12-18 14:30:41 +00006153 return std::make_pair(BeginID, EndID);
6154}
6155
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006156/// Optionally returns true or false if the preallocated preprocessed
Guy Benyei11169dd2012-12-18 14:30:41 +00006157/// entity with index \arg Index came from file \arg FID.
David Blaikie05785d12013-02-20 22:23:23 +00006158Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei11169dd2012-12-18 14:30:41 +00006159 FileID FID) {
6160 if (FID.isInvalid())
6161 return false;
6162
6163 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6164 ModuleFile &M = *PPInfo.first;
6165 unsigned LocalIndex = PPInfo.second;
6166 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006167
Richard Smithcb34bd32016-03-27 07:28:06 +00006168 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
Guy Benyei11169dd2012-12-18 14:30:41 +00006169 if (Loc.isInvalid())
6170 return false;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006171
Guy Benyei11169dd2012-12-18 14:30:41 +00006172 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6173 return true;
6174 else
6175 return false;
6176}
6177
6178namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006179
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006180 /// Visitor used to search for information about a header file.
Guy Benyei11169dd2012-12-18 14:30:41 +00006181 class HeaderFileInfoVisitor {
Guy Benyei11169dd2012-12-18 14:30:41 +00006182 const FileEntry *FE;
David Blaikie05785d12013-02-20 22:23:23 +00006183 Optional<HeaderFileInfo> HFI;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006184
Guy Benyei11169dd2012-12-18 14:30:41 +00006185 public:
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006186 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00006187
6188 bool operator()(ModuleFile &M) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00006189 HeaderFileInfoLookupTable *Table
6190 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
Guy Benyei11169dd2012-12-18 14:30:41 +00006191 if (!Table)
6192 return false;
6193
6194 // Look in the on-disk hash table for an entry for this file name.
Richard Smithbdf2d932015-07-30 03:37:16 +00006195 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00006196 if (Pos == Table->end())
6197 return false;
6198
Richard Smithbdf2d932015-07-30 03:37:16 +00006199 HFI = *Pos;
Guy Benyei11169dd2012-12-18 14:30:41 +00006200 return true;
6201 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006202
David Blaikie05785d12013-02-20 22:23:23 +00006203 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei11169dd2012-12-18 14:30:41 +00006204 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006205
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006206} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00006207
6208HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00006209 HeaderFileInfoVisitor Visitor(FE);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00006210 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis1054bbf2013-05-08 23:46:55 +00006211 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei11169dd2012-12-18 14:30:41 +00006212 return *HFI;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006213
Guy Benyei11169dd2012-12-18 14:30:41 +00006214 return HeaderFileInfo();
6215}
6216
6217void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Richard Smithd230de22017-01-26 01:01:01 +00006218 using DiagState = DiagnosticsEngine::DiagState;
6219 SmallVector<DiagState *, 32> DiagStates;
6220
Vedant Kumar48b4f762018-04-14 01:40:48 +00006221 for (ModuleFile &F : ModuleMgr) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006222 unsigned Idx = 0;
Richard Smithd230de22017-01-26 01:01:01 +00006223 auto &Record = F.PragmaDiagMappings;
6224 if (Record.empty())
6225 continue;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00006226
Richard Smithd230de22017-01-26 01:01:01 +00006227 DiagStates.clear();
6228
6229 auto ReadDiagState =
6230 [&](const DiagState &BasedOn, SourceLocation Loc,
6231 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6232 unsigned BackrefID = Record[Idx++];
6233 if (BackrefID != 0)
6234 return DiagStates[BackrefID - 1];
6235
Guy Benyei11169dd2012-12-18 14:30:41 +00006236 // A new DiagState was created here.
Richard Smithd230de22017-01-26 01:01:01 +00006237 Diag.DiagStates.push_back(BasedOn);
6238 DiagState *NewState = &Diag.DiagStates.back();
Guy Benyei11169dd2012-12-18 14:30:41 +00006239 DiagStates.push_back(NewState);
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00006240 unsigned Size = Record[Idx++];
6241 assert(Idx + Size * 2 <= Record.size() &&
6242 "Invalid data, not enough diag/map pairs");
6243 while (Size--) {
Richard Smithd230de22017-01-26 01:01:01 +00006244 unsigned DiagID = Record[Idx++];
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006245 DiagnosticMapping NewMapping =
Richard Smithe37391c2017-05-03 00:28:49 +00006246 DiagnosticMapping::deserialize(Record[Idx++]);
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006247 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6248 continue;
6249
6250 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6251
6252 // If this mapping was specified as a warning but the severity was
6253 // upgraded due to diagnostic settings, simulate the current diagnostic
6254 // settings (and use a warning).
Richard Smithe37391c2017-05-03 00:28:49 +00006255 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6256 NewMapping.setSeverity(diag::Severity::Warning);
6257 NewMapping.setUpgradedFromWarning(false);
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006258 }
6259
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006260 Mapping = NewMapping;
Richard Smithd230de22017-01-26 01:01:01 +00006261 }
Richard Smithd230de22017-01-26 01:01:01 +00006262 return NewState;
6263 };
6264
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00006265 // Read the first state.
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006266 DiagState *FirstState;
6267 if (F.Kind == MK_ImplicitModule) {
6268 // Implicitly-built modules are reused with different diagnostic
6269 // settings. Use the initial diagnostic state from Diag to simulate this
6270 // compilation's diagnostic settings.
6271 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6272 DiagStates.push_back(FirstState);
6273
6274 // Skip the initial diagnostic state from the serialized module.
Richard Smithe37391c2017-05-03 00:28:49 +00006275 assert(Record[1] == 0 &&
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006276 "Invalid data, unexpected backref in initial state");
Richard Smithe37391c2017-05-03 00:28:49 +00006277 Idx = 3 + Record[2] * 2;
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006278 assert(Idx < Record.size() &&
6279 "Invalid data, not enough state change pairs in initial state");
Richard Smithe37391c2017-05-03 00:28:49 +00006280 } else if (F.isModule()) {
6281 // For an explicit module, preserve the flags from the module build
6282 // command line (-w, -Weverything, -Werror, ...) along with any explicit
6283 // -Wblah flags.
6284 unsigned Flags = Record[Idx++];
6285 DiagState Initial;
6286 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6287 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6288 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6289 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6290 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6291 Initial.ExtBehavior = (diag::Severity)Flags;
6292 FirstState = ReadDiagState(Initial, SourceLocation(), true);
Richard Smithea741482017-05-01 22:10:47 +00006293
Richard Smith6c2b5a82018-02-09 01:15:13 +00006294 assert(F.OriginalSourceFileID.isValid());
6295
Richard Smithe37391c2017-05-03 00:28:49 +00006296 // Set up the root buffer of the module to start with the initial
6297 // diagnostic state of the module itself, to cover files that contain no
6298 // explicit transitions (for which we did not serialize anything).
6299 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6300 .StateTransitions.push_back({FirstState, 0});
6301 } else {
6302 // For prefix ASTs, start with whatever the user configured on the
6303 // command line.
6304 Idx++; // Skip flags.
6305 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6306 SourceLocation(), false);
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00006307 }
Richard Smithd230de22017-01-26 01:01:01 +00006308
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00006309 // Read the state transitions.
6310 unsigned NumLocations = Record[Idx++];
6311 while (NumLocations--) {
6312 assert(Idx < Record.size() &&
6313 "Invalid data, missing pragma diagnostic states");
Richard Smithd230de22017-01-26 01:01:01 +00006314 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6315 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
Richard Smith6c2b5a82018-02-09 01:15:13 +00006316 assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
Richard Smithd230de22017-01-26 01:01:01 +00006317 assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6318 unsigned Transitions = Record[Idx++];
6319
6320 // Note that we don't need to set up Parent/ParentOffset here, because
6321 // we won't be changing the diagnostic state within imported FileIDs
6322 // (other than perhaps appending to the main source file, which has no
6323 // parent).
6324 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6325 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6326 for (unsigned I = 0; I != Transitions; ++I) {
6327 unsigned Offset = Record[Idx++];
6328 auto *State =
6329 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6330 F.StateTransitions.push_back({State, Offset});
Guy Benyei11169dd2012-12-18 14:30:41 +00006331 }
6332 }
Richard Smithd230de22017-01-26 01:01:01 +00006333
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00006334 // Read the final state.
6335 assert(Idx < Record.size() &&
6336 "Invalid data, missing final pragma diagnostic state");
6337 SourceLocation CurStateLoc =
6338 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6339 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6340
6341 if (!F.isModule()) {
6342 Diag.DiagStatesByLoc.CurDiagState = CurState;
6343 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6344
6345 // Preserve the property that the imaginary root file describes the
6346 // current state.
Simon Pilgrim22518632017-10-10 13:56:17 +00006347 FileID NullFile;
6348 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00006349 if (T.empty())
6350 T.push_back({CurState, 0});
6351 else
6352 T[0].State = CurState;
6353 }
6354
Richard Smithd230de22017-01-26 01:01:01 +00006355 // Don't try to read these mappings again.
6356 Record.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006357 }
6358}
6359
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006360/// Get the correct cursor and offset for loading a type.
Guy Benyei11169dd2012-12-18 14:30:41 +00006361ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6362 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6363 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6364 ModuleFile *M = I->second;
Dmitry Polukhina7afb212020-04-16 09:24:46 -07006365 return RecordLocation(
Daniel Grumbergbb8c7e72020-05-13 17:07:47 +01006366 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6367 M->DeclsBlockStartOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00006368}
6369
John McCalld505e572019-12-13 21:54:44 -05006370static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6371 switch (code) {
6372#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6373 case TYPE_##CODE_ID: return Type::CLASS_ID;
6374#include "clang/Serialization/TypeBitCodes.def"
6375 default: return llvm::None;
6376 }
John McCall3ce3d232019-12-13 03:37:23 -05006377}
6378
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006379/// Read and return the type with the given index..
Guy Benyei11169dd2012-12-18 14:30:41 +00006380///
6381/// The index is the type ID, shifted and minus the number of predefs. This
6382/// routine actually reads the record corresponding to the type at the given
6383/// location. It is a helper routine for GetType, which deals with reading type
6384/// IDs.
6385QualType ASTReader::readTypeRecord(unsigned Index) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00006386 assert(ContextObj && "reading type with no AST context");
6387 ASTContext &Context = *ContextObj;
Guy Benyei11169dd2012-12-18 14:30:41 +00006388 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00006389 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00006390
6391 // Keep track of where we are in the stream, then jump back there
6392 // after reading this type.
6393 SavedStreamPosition SavedPosition(DeclsCursor);
6394
6395 ReadingKindTracker ReadingKind(Read_Type, *this);
6396
6397 // Note that we are loading a type record.
6398 Deserializing AType(this);
6399
JF Bastien0e828952019-06-26 19:50:12 +00006400 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6401 Error(std::move(Err));
6402 return QualType();
6403 }
John McCall3ce3d232019-12-13 03:37:23 -05006404 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6405 if (!RawCode) {
6406 Error(RawCode.takeError());
JF Bastien0e828952019-06-26 19:50:12 +00006407 return QualType();
6408 }
JF Bastien0e828952019-06-26 19:50:12 +00006409
John McCall3ce3d232019-12-13 03:37:23 -05006410 ASTRecordReader Record(*this, *Loc.F);
6411 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6412 if (!Code) {
6413 Error(Code.takeError());
JF Bastien0e828952019-06-26 19:50:12 +00006414 return QualType();
6415 }
John McCalld505e572019-12-13 21:54:44 -05006416 if (Code.get() == TYPE_EXT_QUAL) {
6417 QualType baseType = Record.readQualType();
6418 Qualifiers quals = Record.readQualifiers();
6419 return Context.getQualifiedType(baseType, quals);
Guy Benyei11169dd2012-12-18 14:30:41 +00006420 }
6421
John McCalld505e572019-12-13 21:54:44 -05006422 auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6423 if (!maybeClass) {
6424 Error("Unexpected code for type");
6425 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006426 }
6427
John McCalld505e572019-12-13 21:54:44 -05006428 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6429 return TypeReader.read(*maybeClass);
Richard Smith564417a2014-03-20 21:47:22 +00006430}
6431
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006432namespace clang {
6433
6434class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
John McCall3ce3d232019-12-13 03:37:23 -05006435 ASTRecordReader &Reader;
Guy Benyei11169dd2012-12-18 14:30:41 +00006436
John McCall3ce3d232019-12-13 03:37:23 -05006437 SourceLocation readSourceLocation() {
6438 return Reader.readSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00006439 }
6440
6441 TypeSourceInfo *GetTypeSourceInfo() {
John McCall3ce3d232019-12-13 03:37:23 -05006442 return Reader.readTypeSourceInfo();
David L. Jonesbe1557a2016-12-21 00:17:49 +00006443 }
6444
6445 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
John McCall3ce3d232019-12-13 03:37:23 -05006446 return Reader.readNestedNameSpecifierLoc();
Guy Benyei11169dd2012-12-18 14:30:41 +00006447 }
6448
Richard Smithe43e2b32018-08-20 21:47:29 +00006449 Attr *ReadAttr() {
John McCall3ce3d232019-12-13 03:37:23 -05006450 return Reader.readAttr();
Richard Smithe43e2b32018-08-20 21:47:29 +00006451 }
6452
Guy Benyei11169dd2012-12-18 14:30:41 +00006453public:
John McCall3ce3d232019-12-13 03:37:23 -05006454 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
Guy Benyei11169dd2012-12-18 14:30:41 +00006455
6456 // We want compile-time assurance that we've enumerated all of
6457 // these, so unfortunately we have to declare them first, then
6458 // define them out-of-line.
6459#define ABSTRACT_TYPELOC(CLASS, PARENT)
6460#define TYPELOC(CLASS, PARENT) \
6461 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6462#include "clang/AST/TypeLocNodes.def"
6463
6464 void VisitFunctionTypeLoc(FunctionTypeLoc);
6465 void VisitArrayTypeLoc(ArrayTypeLoc);
6466};
6467
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006468} // namespace clang
6469
Guy Benyei11169dd2012-12-18 14:30:41 +00006470void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6471 // nothing to do
6472}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006473
Guy Benyei11169dd2012-12-18 14:30:41 +00006474void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006475 TL.setBuiltinLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006476 if (TL.needsExtraLocalData()) {
John McCall3ce3d232019-12-13 03:37:23 -05006477 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6478 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt()));
6479 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt()));
6480 TL.setModeAttr(Reader.readInt());
Guy Benyei11169dd2012-12-18 14:30:41 +00006481 }
6482}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006483
Guy Benyei11169dd2012-12-18 14:30:41 +00006484void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006485 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006486}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006487
Guy Benyei11169dd2012-12-18 14:30:41 +00006488void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006489 TL.setStarLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006490}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006491
Reid Kleckner8a365022013-06-24 17:51:48 +00006492void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6493 // nothing to do
6494}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006495
Reid Kleckner0503a872013-12-05 01:23:43 +00006496void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6497 // nothing to do
6498}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006499
Leonard Chanc72aaf62019-05-07 03:20:17 +00006500void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006501 TL.setExpansionLoc(readSourceLocation());
Leonard Chanc72aaf62019-05-07 03:20:17 +00006502}
6503
Guy Benyei11169dd2012-12-18 14:30:41 +00006504void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006505 TL.setCaretLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006506}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006507
Guy Benyei11169dd2012-12-18 14:30:41 +00006508void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006509 TL.setAmpLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006510}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006511
Guy Benyei11169dd2012-12-18 14:30:41 +00006512void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006513 TL.setAmpAmpLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006514}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006515
Guy Benyei11169dd2012-12-18 14:30:41 +00006516void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006517 TL.setStarLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006518 TL.setClassTInfo(GetTypeSourceInfo());
Guy Benyei11169dd2012-12-18 14:30:41 +00006519}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006520
Guy Benyei11169dd2012-12-18 14:30:41 +00006521void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006522 TL.setLBracketLoc(readSourceLocation());
6523 TL.setRBracketLoc(readSourceLocation());
6524 if (Reader.readBool())
6525 TL.setSizeExpr(Reader.readExpr());
Guy Benyei11169dd2012-12-18 14:30:41 +00006526 else
Craig Toppera13603a2014-05-22 05:54:18 +00006527 TL.setSizeExpr(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006528}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006529
Guy Benyei11169dd2012-12-18 14:30:41 +00006530void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6531 VisitArrayTypeLoc(TL);
6532}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006533
Guy Benyei11169dd2012-12-18 14:30:41 +00006534void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6535 VisitArrayTypeLoc(TL);
6536}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006537
Guy Benyei11169dd2012-12-18 14:30:41 +00006538void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6539 VisitArrayTypeLoc(TL);
6540}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006541
Guy Benyei11169dd2012-12-18 14:30:41 +00006542void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6543 DependentSizedArrayTypeLoc TL) {
6544 VisitArrayTypeLoc(TL);
6545}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006546
Andrew Gozillon572bbb02017-10-02 06:25:51 +00006547void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6548 DependentAddressSpaceTypeLoc TL) {
6549
John McCall3ce3d232019-12-13 03:37:23 -05006550 TL.setAttrNameLoc(readSourceLocation());
6551 TL.setAttrOperandParensRange(Reader.readSourceRange());
6552 TL.setAttrExprOperand(Reader.readExpr());
Andrew Gozillon572bbb02017-10-02 06:25:51 +00006553}
6554
Guy Benyei11169dd2012-12-18 14:30:41 +00006555void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6556 DependentSizedExtVectorTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006557 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006558}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006559
Guy Benyei11169dd2012-12-18 14:30:41 +00006560void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006561 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006562}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006563
Erich Keanef702b022018-07-13 19:46:04 +00006564void TypeLocReader::VisitDependentVectorTypeLoc(
6565 DependentVectorTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006566 TL.setNameLoc(readSourceLocation());
Erich Keanef702b022018-07-13 19:46:04 +00006567}
6568
Guy Benyei11169dd2012-12-18 14:30:41 +00006569void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006570 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006571}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006572
Florian Hahn10658692020-05-11 17:45:51 +01006573void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6574 TL.setAttrNameLoc(readSourceLocation());
6575 TL.setAttrOperandParensRange(Reader.readSourceRange());
6576 TL.setAttrRowOperand(Reader.readExpr());
6577 TL.setAttrColumnOperand(Reader.readExpr());
6578}
6579
6580void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6581 DependentSizedMatrixTypeLoc TL) {
6582 TL.setAttrNameLoc(readSourceLocation());
6583 TL.setAttrOperandParensRange(Reader.readSourceRange());
6584 TL.setAttrRowOperand(Reader.readExpr());
6585 TL.setAttrColumnOperand(Reader.readExpr());
6586}
6587
Guy Benyei11169dd2012-12-18 14:30:41 +00006588void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006589 TL.setLocalRangeBegin(readSourceLocation());
6590 TL.setLParenLoc(readSourceLocation());
6591 TL.setRParenLoc(readSourceLocation());
6592 TL.setExceptionSpecRange(Reader.readSourceRange());
6593 TL.setLocalRangeEnd(readSourceLocation());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00006594 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
John McCall3ce3d232019-12-13 03:37:23 -05006595 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
Guy Benyei11169dd2012-12-18 14:30:41 +00006596 }
6597}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006598
Guy Benyei11169dd2012-12-18 14:30:41 +00006599void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6600 VisitFunctionTypeLoc(TL);
6601}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006602
Guy Benyei11169dd2012-12-18 14:30:41 +00006603void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6604 VisitFunctionTypeLoc(TL);
6605}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006606
Guy Benyei11169dd2012-12-18 14:30:41 +00006607void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006608 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006609}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006610
Guy Benyei11169dd2012-12-18 14:30:41 +00006611void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006612 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006613}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006614
Guy Benyei11169dd2012-12-18 14:30:41 +00006615void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006616 TL.setTypeofLoc(readSourceLocation());
6617 TL.setLParenLoc(readSourceLocation());
6618 TL.setRParenLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006619}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006620
Guy Benyei11169dd2012-12-18 14:30:41 +00006621void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006622 TL.setTypeofLoc(readSourceLocation());
6623 TL.setLParenLoc(readSourceLocation());
6624 TL.setRParenLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006625 TL.setUnderlyingTInfo(GetTypeSourceInfo());
Guy Benyei11169dd2012-12-18 14:30:41 +00006626}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006627
Guy Benyei11169dd2012-12-18 14:30:41 +00006628void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006629 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006630}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006631
Guy Benyei11169dd2012-12-18 14:30:41 +00006632void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006633 TL.setKWLoc(readSourceLocation());
6634 TL.setLParenLoc(readSourceLocation());
6635 TL.setRParenLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006636 TL.setUnderlyingTInfo(GetTypeSourceInfo());
Guy Benyei11169dd2012-12-18 14:30:41 +00006637}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006638
Guy Benyei11169dd2012-12-18 14:30:41 +00006639void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006640 TL.setNameLoc(readSourceLocation());
Saar Razb481f022020-01-22 02:03:05 +02006641 if (Reader.readBool()) {
6642 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6643 TL.setTemplateKWLoc(readSourceLocation());
6644 TL.setConceptNameLoc(readSourceLocation());
6645 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6646 TL.setLAngleLoc(readSourceLocation());
6647 TL.setRAngleLoc(readSourceLocation());
6648 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6649 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6650 TL.getTypePtr()->getArg(i).getKind()));
6651 }
Guy Benyei11169dd2012-12-18 14:30:41 +00006652}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006653
Richard Smith600b5262017-01-26 20:40:47 +00006654void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6655 DeducedTemplateSpecializationTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006656 TL.setTemplateNameLoc(readSourceLocation());
Richard Smith600b5262017-01-26 20:40:47 +00006657}
6658
Guy Benyei11169dd2012-12-18 14:30:41 +00006659void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
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::VisitEnumTypeLoc(EnumTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006664 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006665}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006666
Guy Benyei11169dd2012-12-18 14:30:41 +00006667void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smithe43e2b32018-08-20 21:47:29 +00006668 TL.setAttr(ReadAttr());
Guy Benyei11169dd2012-12-18 14:30:41 +00006669}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006670
Guy Benyei11169dd2012-12-18 14:30:41 +00006671void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006672 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006673}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006674
Guy Benyei11169dd2012-12-18 14:30:41 +00006675void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6676 SubstTemplateTypeParmTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006677 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006678}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006679
Guy Benyei11169dd2012-12-18 14:30:41 +00006680void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6681 SubstTemplateTypeParmPackTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006682 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006683}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006684
Guy Benyei11169dd2012-12-18 14:30:41 +00006685void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6686 TemplateSpecializationTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006687 TL.setTemplateKeywordLoc(readSourceLocation());
6688 TL.setTemplateNameLoc(readSourceLocation());
6689 TL.setLAngleLoc(readSourceLocation());
6690 TL.setRAngleLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006691 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
David L. Jonesbe1557a2016-12-21 00:17:49 +00006692 TL.setArgLocInfo(
6693 i,
John McCall3ce3d232019-12-13 03:37:23 -05006694 Reader.readTemplateArgumentLocInfo(
6695 TL.getTypePtr()->getArg(i).getKind()));
Guy Benyei11169dd2012-12-18 14:30:41 +00006696}
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00006697
Guy Benyei11169dd2012-12-18 14:30:41 +00006698void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006699 TL.setLParenLoc(readSourceLocation());
6700 TL.setRParenLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006701}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006702
Guy Benyei11169dd2012-12-18 14:30:41 +00006703void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006704 TL.setElaboratedKeywordLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006705 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
Guy Benyei11169dd2012-12-18 14:30:41 +00006706}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006707
Guy Benyei11169dd2012-12-18 14:30:41 +00006708void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006709 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006710}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006711
Guy Benyei11169dd2012-12-18 14:30:41 +00006712void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006713 TL.setElaboratedKeywordLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006714 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
John McCall3ce3d232019-12-13 03:37:23 -05006715 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006716}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006717
Guy Benyei11169dd2012-12-18 14:30:41 +00006718void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6719 DependentTemplateSpecializationTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006720 TL.setElaboratedKeywordLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00006721 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
John McCall3ce3d232019-12-13 03:37:23 -05006722 TL.setTemplateKeywordLoc(readSourceLocation());
6723 TL.setTemplateNameLoc(readSourceLocation());
6724 TL.setLAngleLoc(readSourceLocation());
6725 TL.setRAngleLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006726 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
David L. Jonesbe1557a2016-12-21 00:17:49 +00006727 TL.setArgLocInfo(
6728 I,
John McCall3ce3d232019-12-13 03:37:23 -05006729 Reader.readTemplateArgumentLocInfo(
6730 TL.getTypePtr()->getArg(I).getKind()));
Guy Benyei11169dd2012-12-18 14:30:41 +00006731}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006732
Guy Benyei11169dd2012-12-18 14:30:41 +00006733void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006734 TL.setEllipsisLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006735}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006736
Guy Benyei11169dd2012-12-18 14:30:41 +00006737void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006738 TL.setNameLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006739}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006740
Manman Rene6be26c2016-09-13 17:25:08 +00006741void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6742 if (TL.getNumProtocols()) {
John McCall3ce3d232019-12-13 03:37:23 -05006743 TL.setProtocolLAngleLoc(readSourceLocation());
6744 TL.setProtocolRAngleLoc(readSourceLocation());
Manman Rene6be26c2016-09-13 17:25:08 +00006745 }
6746 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
John McCall3ce3d232019-12-13 03:37:23 -05006747 TL.setProtocolLoc(i, readSourceLocation());
Manman Rene6be26c2016-09-13 17:25:08 +00006748}
6749
Guy Benyei11169dd2012-12-18 14:30:41 +00006750void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006751 TL.setHasBaseTypeAsWritten(Reader.readBool());
6752 TL.setTypeArgsLAngleLoc(readSourceLocation());
6753 TL.setTypeArgsRAngleLoc(readSourceLocation());
Douglas Gregore9d95f12015-07-07 03:57:35 +00006754 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
David L. Jonesbe1557a2016-12-21 00:17:49 +00006755 TL.setTypeArgTInfo(i, GetTypeSourceInfo());
John McCall3ce3d232019-12-13 03:37:23 -05006756 TL.setProtocolLAngleLoc(readSourceLocation());
6757 TL.setProtocolRAngleLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006758 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
John McCall3ce3d232019-12-13 03:37:23 -05006759 TL.setProtocolLoc(i, readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006760}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006761
Guy Benyei11169dd2012-12-18 14:30:41 +00006762void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006763 TL.setStarLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006764}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006765
Guy Benyei11169dd2012-12-18 14:30:41 +00006766void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006767 TL.setKWLoc(readSourceLocation());
6768 TL.setLParenLoc(readSourceLocation());
6769 TL.setRParenLoc(readSourceLocation());
Guy Benyei11169dd2012-12-18 14:30:41 +00006770}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00006771
Xiuli Pan9c14e282016-01-09 12:53:17 +00006772void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
John McCall3ce3d232019-12-13 03:37:23 -05006773 TL.setKWLoc(readSourceLocation());
Xiuli Pan9c14e282016-01-09 12:53:17 +00006774}
Guy Benyei11169dd2012-12-18 14:30:41 +00006775
Erich Keane5f0903e2020-04-17 10:44:19 -07006776void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
6777 TL.setNameLoc(readSourceLocation());
6778}
6779void TypeLocReader::VisitDependentExtIntTypeLoc(
6780 clang::DependentExtIntTypeLoc TL) {
6781 TL.setNameLoc(readSourceLocation());
6782}
6783
6784
John McCall3ce3d232019-12-13 03:37:23 -05006785void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6786 TypeLocReader TLR(*this);
Richard Smithc23d7342018-06-29 20:46:25 +00006787 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6788 TLR.Visit(TL);
6789}
6790
John McCall3ce3d232019-12-13 03:37:23 -05006791TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6792 QualType InfoTy = readType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006793 if (InfoTy.isNull())
Craig Toppera13603a2014-05-22 05:54:18 +00006794 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006795
6796 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
John McCall3ce3d232019-12-13 03:37:23 -05006797 readTypeLoc(TInfo->getTypeLoc());
Guy Benyei11169dd2012-12-18 14:30:41 +00006798 return TInfo;
6799}
6800
6801QualType ASTReader::GetType(TypeID ID) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00006802 assert(ContextObj && "reading type with no AST context");
6803 ASTContext &Context = *ContextObj;
6804
Guy Benyei11169dd2012-12-18 14:30:41 +00006805 unsigned FastQuals = ID & Qualifiers::FastMask;
6806 unsigned Index = ID >> Qualifiers::FastWidth;
6807
6808 if (Index < NUM_PREDEF_TYPE_IDS) {
6809 QualType T;
6810 switch ((PredefinedTypeIDs)Index) {
Alexey Baderbdf7c842015-09-15 12:18:29 +00006811 case PREDEF_TYPE_NULL_ID:
Vedant Kumar48b4f762018-04-14 01:40:48 +00006812 return QualType();
Alexey Baderbdf7c842015-09-15 12:18:29 +00006813 case PREDEF_TYPE_VOID_ID:
6814 T = Context.VoidTy;
6815 break;
6816 case PREDEF_TYPE_BOOL_ID:
6817 T = Context.BoolTy;
6818 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00006819 case PREDEF_TYPE_CHAR_U_ID:
6820 case PREDEF_TYPE_CHAR_S_ID:
6821 // FIXME: Check that the signedness of CharTy is correct!
6822 T = Context.CharTy;
6823 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006824 case PREDEF_TYPE_UCHAR_ID:
6825 T = Context.UnsignedCharTy;
6826 break;
6827 case PREDEF_TYPE_USHORT_ID:
6828 T = Context.UnsignedShortTy;
6829 break;
6830 case PREDEF_TYPE_UINT_ID:
6831 T = Context.UnsignedIntTy;
6832 break;
6833 case PREDEF_TYPE_ULONG_ID:
6834 T = Context.UnsignedLongTy;
6835 break;
6836 case PREDEF_TYPE_ULONGLONG_ID:
6837 T = Context.UnsignedLongLongTy;
6838 break;
6839 case PREDEF_TYPE_UINT128_ID:
6840 T = Context.UnsignedInt128Ty;
6841 break;
6842 case PREDEF_TYPE_SCHAR_ID:
6843 T = Context.SignedCharTy;
6844 break;
6845 case PREDEF_TYPE_WCHAR_ID:
6846 T = Context.WCharTy;
6847 break;
6848 case PREDEF_TYPE_SHORT_ID:
6849 T = Context.ShortTy;
6850 break;
6851 case PREDEF_TYPE_INT_ID:
6852 T = Context.IntTy;
6853 break;
6854 case PREDEF_TYPE_LONG_ID:
6855 T = Context.LongTy;
6856 break;
6857 case PREDEF_TYPE_LONGLONG_ID:
6858 T = Context.LongLongTy;
6859 break;
6860 case PREDEF_TYPE_INT128_ID:
6861 T = Context.Int128Ty;
6862 break;
Ties Stuijecd682b2020-06-05 00:20:02 +01006863 case PREDEF_TYPE_BFLOAT16_ID:
6864 T = Context.BFloat16Ty;
6865 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006866 case PREDEF_TYPE_HALF_ID:
6867 T = Context.HalfTy;
6868 break;
6869 case PREDEF_TYPE_FLOAT_ID:
6870 T = Context.FloatTy;
6871 break;
6872 case PREDEF_TYPE_DOUBLE_ID:
6873 T = Context.DoubleTy;
6874 break;
6875 case PREDEF_TYPE_LONGDOUBLE_ID:
6876 T = Context.LongDoubleTy;
6877 break;
Leonard Chanf921d852018-06-04 16:07:52 +00006878 case PREDEF_TYPE_SHORT_ACCUM_ID:
6879 T = Context.ShortAccumTy;
6880 break;
6881 case PREDEF_TYPE_ACCUM_ID:
6882 T = Context.AccumTy;
6883 break;
6884 case PREDEF_TYPE_LONG_ACCUM_ID:
6885 T = Context.LongAccumTy;
6886 break;
6887 case PREDEF_TYPE_USHORT_ACCUM_ID:
6888 T = Context.UnsignedShortAccumTy;
6889 break;
6890 case PREDEF_TYPE_UACCUM_ID:
6891 T = Context.UnsignedAccumTy;
6892 break;
6893 case PREDEF_TYPE_ULONG_ACCUM_ID:
6894 T = Context.UnsignedLongAccumTy;
6895 break;
Leonard Chanab80f3c2018-06-14 14:53:51 +00006896 case PREDEF_TYPE_SHORT_FRACT_ID:
6897 T = Context.ShortFractTy;
6898 break;
6899 case PREDEF_TYPE_FRACT_ID:
6900 T = Context.FractTy;
6901 break;
6902 case PREDEF_TYPE_LONG_FRACT_ID:
6903 T = Context.LongFractTy;
6904 break;
6905 case PREDEF_TYPE_USHORT_FRACT_ID:
6906 T = Context.UnsignedShortFractTy;
6907 break;
6908 case PREDEF_TYPE_UFRACT_ID:
6909 T = Context.UnsignedFractTy;
6910 break;
6911 case PREDEF_TYPE_ULONG_FRACT_ID:
6912 T = Context.UnsignedLongFractTy;
6913 break;
6914 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6915 T = Context.SatShortAccumTy;
6916 break;
6917 case PREDEF_TYPE_SAT_ACCUM_ID:
6918 T = Context.SatAccumTy;
6919 break;
6920 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6921 T = Context.SatLongAccumTy;
6922 break;
6923 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6924 T = Context.SatUnsignedShortAccumTy;
6925 break;
6926 case PREDEF_TYPE_SAT_UACCUM_ID:
6927 T = Context.SatUnsignedAccumTy;
6928 break;
6929 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6930 T = Context.SatUnsignedLongAccumTy;
6931 break;
6932 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6933 T = Context.SatShortFractTy;
6934 break;
6935 case PREDEF_TYPE_SAT_FRACT_ID:
6936 T = Context.SatFractTy;
6937 break;
6938 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6939 T = Context.SatLongFractTy;
6940 break;
6941 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6942 T = Context.SatUnsignedShortFractTy;
6943 break;
6944 case PREDEF_TYPE_SAT_UFRACT_ID:
6945 T = Context.SatUnsignedFractTy;
6946 break;
6947 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6948 T = Context.SatUnsignedLongFractTy;
6949 break;
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00006950 case PREDEF_TYPE_FLOAT16_ID:
6951 T = Context.Float16Ty;
6952 break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00006953 case PREDEF_TYPE_FLOAT128_ID:
6954 T = Context.Float128Ty;
6955 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006956 case PREDEF_TYPE_OVERLOAD_ID:
6957 T = Context.OverloadTy;
6958 break;
6959 case PREDEF_TYPE_BOUND_MEMBER:
6960 T = Context.BoundMemberTy;
6961 break;
6962 case PREDEF_TYPE_PSEUDO_OBJECT:
6963 T = Context.PseudoObjectTy;
6964 break;
6965 case PREDEF_TYPE_DEPENDENT_ID:
6966 T = Context.DependentTy;
6967 break;
6968 case PREDEF_TYPE_UNKNOWN_ANY:
6969 T = Context.UnknownAnyTy;
6970 break;
6971 case PREDEF_TYPE_NULLPTR_ID:
6972 T = Context.NullPtrTy;
6973 break;
Richard Smith3a8244d2018-05-01 05:02:45 +00006974 case PREDEF_TYPE_CHAR8_ID:
6975 T = Context.Char8Ty;
6976 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006977 case PREDEF_TYPE_CHAR16_ID:
6978 T = Context.Char16Ty;
6979 break;
6980 case PREDEF_TYPE_CHAR32_ID:
6981 T = Context.Char32Ty;
6982 break;
6983 case PREDEF_TYPE_OBJC_ID:
6984 T = Context.ObjCBuiltinIdTy;
6985 break;
6986 case PREDEF_TYPE_OBJC_CLASS:
6987 T = Context.ObjCBuiltinClassTy;
6988 break;
6989 case PREDEF_TYPE_OBJC_SEL:
6990 T = Context.ObjCBuiltinSelTy;
6991 break;
Alexey Bader954ba212016-04-08 13:40:33 +00006992#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6993 case PREDEF_TYPE_##Id##_ID: \
6994 T = Context.SingletonId; \
Alexey Baderbdf7c842015-09-15 12:18:29 +00006995 break;
Alexey Baderb62f1442016-04-13 08:33:41 +00006996#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +00006997#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6998 case PREDEF_TYPE_##Id##_ID: \
6999 T = Context.Id##Ty; \
7000 break;
7001#include "clang/Basic/OpenCLExtensionTypes.def"
Alexey Baderbdf7c842015-09-15 12:18:29 +00007002 case PREDEF_TYPE_SAMPLER_ID:
7003 T = Context.OCLSamplerTy;
7004 break;
7005 case PREDEF_TYPE_EVENT_ID:
7006 T = Context.OCLEventTy;
7007 break;
Alexey Bader9c8453f2015-09-15 11:18:52 +00007008 case PREDEF_TYPE_CLK_EVENT_ID:
7009 T = Context.OCLClkEventTy;
7010 break;
7011 case PREDEF_TYPE_QUEUE_ID:
7012 T = Context.OCLQueueTy;
7013 break;
Alexey Bader9c8453f2015-09-15 11:18:52 +00007014 case PREDEF_TYPE_RESERVE_ID_ID:
7015 T = Context.OCLReserveIDTy;
7016 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00007017 case PREDEF_TYPE_AUTO_DEDUCT:
7018 T = Context.getAutoDeductType();
7019 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00007020 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7021 T = Context.getAutoRRefDeductType();
Guy Benyei11169dd2012-12-18 14:30:41 +00007022 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007023 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7024 T = Context.ARCUnbridgedCastTy;
7025 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007026 case PREDEF_TYPE_BUILTIN_FN:
7027 T = Context.BuiltinFnTy;
7028 break;
Florian Hahn8f3f88d2020-06-01 19:42:03 +01007029 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7030 T = Context.IncompleteMatrixIdxTy;
7031 break;
Alexey Bataev1a3320e2015-08-25 14:24:04 +00007032 case PREDEF_TYPE_OMP_ARRAY_SECTION:
7033 T = Context.OMPArraySectionTy;
7034 break;
Alexey Bataev7ac9efb2020-02-05 09:33:05 -05007035 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7036 T = Context.OMPArraySectionTy;
7037 break;
Alexey Bataev13a15042020-04-01 15:06:38 -04007038 case PREDEF_TYPE_OMP_ITERATOR:
7039 T = Context.OMPIteratorTy;
7040 break;
Richard Sandifordeb485fb2019-08-09 08:52:54 +00007041#define SVE_TYPE(Name, Id, SingletonId) \
7042 case PREDEF_TYPE_##Id##_ID: \
7043 T = Context.SingletonId; \
7044 break;
7045#include "clang/Basic/AArch64SVEACLETypes.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00007046 }
7047
7048 assert(!T.isNull() && "Unknown predefined type");
7049 return T.withFastQualifiers(FastQuals);
7050 }
7051
7052 Index -= NUM_PREDEF_TYPE_IDS;
7053 assert(Index < TypesLoaded.size() && "Type index out-of-range");
7054 if (TypesLoaded[Index].isNull()) {
7055 TypesLoaded[Index] = readTypeRecord(Index);
7056 if (TypesLoaded[Index].isNull())
Vedant Kumar48b4f762018-04-14 01:40:48 +00007057 return QualType();
Guy Benyei11169dd2012-12-18 14:30:41 +00007058
7059 TypesLoaded[Index]->setFromAST();
7060 if (DeserializationListener)
7061 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7062 TypesLoaded[Index]);
7063 }
7064
7065 return TypesLoaded[Index].withFastQualifiers(FastQuals);
7066}
7067
7068QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7069 return GetType(getGlobalTypeID(F, LocalID));
7070}
7071
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007072serialization::TypeID
Guy Benyei11169dd2012-12-18 14:30:41 +00007073ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7074 unsigned FastQuals = LocalID & Qualifiers::FastMask;
7075 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007076
Guy Benyei11169dd2012-12-18 14:30:41 +00007077 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7078 return LocalID;
7079
Richard Smith37a93df2017-02-18 00:32:02 +00007080 if (!F.ModuleOffsetMap.empty())
7081 ReadModuleOffsetMap(F);
7082
Guy Benyei11169dd2012-12-18 14:30:41 +00007083 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7084 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7085 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007086
Guy Benyei11169dd2012-12-18 14:30:41 +00007087 unsigned GlobalIndex = LocalIndex + I->second;
7088 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7089}
7090
7091TemplateArgumentLocInfo
John McCall3ce3d232019-12-13 03:37:23 -05007092ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007093 switch (Kind) {
7094 case TemplateArgument::Expression:
John McCall3ce3d232019-12-13 03:37:23 -05007095 return readExpr();
Guy Benyei11169dd2012-12-18 14:30:41 +00007096 case TemplateArgument::Type:
John McCall3ce3d232019-12-13 03:37:23 -05007097 return readTypeSourceInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +00007098 case TemplateArgument::Template: {
John McCall3ce3d232019-12-13 03:37:23 -05007099 NestedNameSpecifierLoc QualifierLoc =
7100 readNestedNameSpecifierLoc();
7101 SourceLocation TemplateNameLoc = readSourceLocation();
Guy Benyei11169dd2012-12-18 14:30:41 +00007102 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7103 SourceLocation());
7104 }
7105 case TemplateArgument::TemplateExpansion: {
John McCall3ce3d232019-12-13 03:37:23 -05007106 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7107 SourceLocation TemplateNameLoc = readSourceLocation();
7108 SourceLocation EllipsisLoc = readSourceLocation();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007109 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Guy Benyei11169dd2012-12-18 14:30:41 +00007110 EllipsisLoc);
7111 }
7112 case TemplateArgument::Null:
7113 case TemplateArgument::Integral:
7114 case TemplateArgument::Declaration:
7115 case TemplateArgument::NullPtr:
7116 case TemplateArgument::Pack:
7117 // FIXME: Is this right?
Vedant Kumar48b4f762018-04-14 01:40:48 +00007118 return TemplateArgumentLocInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +00007119 }
7120 llvm_unreachable("unexpected template argument loc");
7121}
7122
John McCall3ce3d232019-12-13 03:37:23 -05007123TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7124 TemplateArgument Arg = readTemplateArgument();
Guy Benyei11169dd2012-12-18 14:30:41 +00007125
7126 if (Arg.getKind() == TemplateArgument::Expression) {
John McCall3ce3d232019-12-13 03:37:23 -05007127 if (readBool()) // bool InfoHasSameExpr.
Guy Benyei11169dd2012-12-18 14:30:41 +00007128 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7129 }
John McCall3ce3d232019-12-13 03:37:23 -05007130 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
Guy Benyei11169dd2012-12-18 14:30:41 +00007131}
7132
John McCall3ce3d232019-12-13 03:37:23 -05007133const ASTTemplateArgumentListInfo *
7134ASTRecordReader::readASTTemplateArgumentListInfo() {
7135 SourceLocation LAngleLoc = readSourceLocation();
7136 SourceLocation RAngleLoc = readSourceLocation();
7137 unsigned NumArgsAsWritten = readInt();
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00007138 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7139 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
John McCall3ce3d232019-12-13 03:37:23 -05007140 TemplArgsInfo.addArgument(readTemplateArgumentLoc());
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00007141 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7142}
7143
Guy Benyei11169dd2012-12-18 14:30:41 +00007144Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7145 return GetDecl(ID);
7146}
7147
Richard Smith053f6c62014-05-16 23:01:30 +00007148void ASTReader::CompleteRedeclChain(const Decl *D) {
Richard Smith851072e2014-05-19 20:59:20 +00007149 if (NumCurrentElementsDeserializing) {
7150 // We arrange to not care about the complete redeclaration chain while we're
7151 // deserializing. Just remember that the AST has marked this one as complete
7152 // but that it's not actually complete yet, so we know we still need to
7153 // complete it later.
7154 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7155 return;
7156 }
7157
Richard Smith053f6c62014-05-16 23:01:30 +00007158 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7159
Richard Smith053f6c62014-05-16 23:01:30 +00007160 // If this is a named declaration, complete it by looking it up
7161 // within its context.
7162 //
Richard Smith01bdb7a2014-08-28 05:44:07 +00007163 // FIXME: Merging a function definition should merge
Richard Smith053f6c62014-05-16 23:01:30 +00007164 // all mergeable entities within it.
7165 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7166 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7167 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
Richard Smitha534a312015-07-21 23:54:07 +00007168 if (!getContext().getLangOpts().CPlusPlus &&
7169 isa<TranslationUnitDecl>(DC)) {
Richard Smith053f6c62014-05-16 23:01:30 +00007170 // Outside of C++, we don't have a lookup table for the TU, so update
Richard Smitha534a312015-07-21 23:54:07 +00007171 // the identifier instead. (For C++ modules, we don't store decls
7172 // in the serialized identifier table, so we do the lookup in the TU.)
7173 auto *II = Name.getAsIdentifierInfo();
7174 assert(II && "non-identifier name in C?");
Richard Smith053f6c62014-05-16 23:01:30 +00007175 if (II->isOutOfDate())
7176 updateOutOfDateIdentifier(*II);
7177 } else
7178 DC->lookup(Name);
Richard Smith01bdb7a2014-08-28 05:44:07 +00007179 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
Richard Smith3cb15722015-08-05 22:41:45 +00007180 // Find all declarations of this kind from the relevant context.
7181 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7182 auto *DC = cast<DeclContext>(DCDecl);
7183 SmallVector<Decl*, 8> Decls;
7184 FindExternalLexicalDecls(
7185 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7186 }
Richard Smith053f6c62014-05-16 23:01:30 +00007187 }
7188 }
Richard Smith50895422015-01-31 03:04:55 +00007189
7190 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7191 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7192 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7193 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7194 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7195 if (auto *Template = FD->getPrimaryTemplate())
7196 Template->LoadLazySpecializations();
7197 }
Richard Smith053f6c62014-05-16 23:01:30 +00007198}
7199
Richard Smithc2bb8182015-03-24 06:36:48 +00007200CXXCtorInitializer **
7201ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7202 RecordLocation Loc = getLocalBitOffset(Offset);
7203 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7204 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00007205 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7206 Error(std::move(Err));
7207 return nullptr;
7208 }
Richard Smithc2bb8182015-03-24 06:36:48 +00007209 ReadingKindTracker ReadingKind(Read_Decl, *this);
7210
JF Bastien0e828952019-06-26 19:50:12 +00007211 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7212 if (!MaybeCode) {
7213 Error(MaybeCode.takeError());
7214 return nullptr;
7215 }
7216 unsigned Code = MaybeCode.get();
7217
John McCall3ce3d232019-12-13 03:37:23 -05007218 ASTRecordReader Record(*this, *Loc.F);
7219 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
JF Bastien0e828952019-06-26 19:50:12 +00007220 if (!MaybeRecCode) {
7221 Error(MaybeRecCode.takeError());
7222 return nullptr;
7223 }
7224 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
Richard Smithc2bb8182015-03-24 06:36:48 +00007225 Error("malformed AST file: missing C++ ctor initializers");
7226 return nullptr;
7227 }
7228
John McCall3ce3d232019-12-13 03:37:23 -05007229 return Record.readCXXCtorInitializers();
Richard Smithc2bb8182015-03-24 06:36:48 +00007230}
7231
Guy Benyei11169dd2012-12-18 14:30:41 +00007232CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00007233 assert(ContextObj && "reading base specifiers with no AST context");
7234 ASTContext &Context = *ContextObj;
7235
Guy Benyei11169dd2012-12-18 14:30:41 +00007236 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007237 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00007238 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00007239 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7240 Error(std::move(Err));
7241 return nullptr;
7242 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007243 ReadingKindTracker ReadingKind(Read_Decl, *this);
JF Bastien0e828952019-06-26 19:50:12 +00007244
7245 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7246 if (!MaybeCode) {
7247 Error(MaybeCode.takeError());
7248 return nullptr;
7249 }
7250 unsigned Code = MaybeCode.get();
7251
John McCall3ce3d232019-12-13 03:37:23 -05007252 ASTRecordReader Record(*this, *Loc.F);
7253 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
JF Bastien0e828952019-06-26 19:50:12 +00007254 if (!MaybeRecCode) {
7255 Error(MaybeCode.takeError());
7256 return nullptr;
7257 }
7258 unsigned RecCode = MaybeRecCode.get();
7259
Guy Benyei11169dd2012-12-18 14:30:41 +00007260 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00007261 Error("malformed AST file: missing C++ base specifiers");
Craig Toppera13603a2014-05-22 05:54:18 +00007262 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007263 }
7264
John McCall3ce3d232019-12-13 03:37:23 -05007265 unsigned NumBases = Record.readInt();
Guy Benyei11169dd2012-12-18 14:30:41 +00007266 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Vedant Kumar48b4f762018-04-14 01:40:48 +00007267 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
Guy Benyei11169dd2012-12-18 14:30:41 +00007268 for (unsigned I = 0; I != NumBases; ++I)
John McCall3ce3d232019-12-13 03:37:23 -05007269 Bases[I] = Record.readCXXBaseSpecifier();
Guy Benyei11169dd2012-12-18 14:30:41 +00007270 return Bases;
7271}
7272
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007273serialization::DeclID
Guy Benyei11169dd2012-12-18 14:30:41 +00007274ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7275 if (LocalID < NUM_PREDEF_DECL_IDS)
7276 return LocalID;
7277
Richard Smith37a93df2017-02-18 00:32:02 +00007278 if (!F.ModuleOffsetMap.empty())
7279 ReadModuleOffsetMap(F);
7280
Guy Benyei11169dd2012-12-18 14:30:41 +00007281 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7282 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7283 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007284
Guy Benyei11169dd2012-12-18 14:30:41 +00007285 return LocalID + I->second;
7286}
7287
7288bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7289 ModuleFile &M) const {
Richard Smithfe620d22015-03-05 23:24:12 +00007290 // Predefined decls aren't from any module.
7291 if (ID < NUM_PREDEF_DECL_IDS)
7292 return false;
7293
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007294 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
Richard Smithbcda1a92015-07-12 23:51:20 +00007295 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
Guy Benyei11169dd2012-12-18 14:30:41 +00007296}
7297
Douglas Gregor9f782892013-01-21 15:25:38 +00007298ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007299 if (!D->isFromASTFile())
Craig Toppera13603a2014-05-22 05:54:18 +00007300 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007301 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7302 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7303 return I->second;
7304}
7305
7306SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7307 if (ID < NUM_PREDEF_DECL_IDS)
Vedant Kumar48b4f762018-04-14 01:40:48 +00007308 return SourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00007309
Guy Benyei11169dd2012-12-18 14:30:41 +00007310 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7311
7312 if (Index > DeclsLoaded.size()) {
7313 Error("declaration ID out-of-range for AST file");
Vedant Kumar48b4f762018-04-14 01:40:48 +00007314 return SourceLocation();
Guy Benyei11169dd2012-12-18 14:30:41 +00007315 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00007316
Guy Benyei11169dd2012-12-18 14:30:41 +00007317 if (Decl *D = DeclsLoaded[Index])
7318 return D->getLocation();
7319
Richard Smithcb34bd32016-03-27 07:28:06 +00007320 SourceLocation Loc;
7321 DeclCursorForID(ID, Loc);
7322 return Loc;
Guy Benyei11169dd2012-12-18 14:30:41 +00007323}
7324
Richard Smithfe620d22015-03-05 23:24:12 +00007325static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7326 switch (ID) {
7327 case PREDEF_DECL_NULL_ID:
7328 return nullptr;
7329
7330 case PREDEF_DECL_TRANSLATION_UNIT_ID:
7331 return Context.getTranslationUnitDecl();
7332
7333 case PREDEF_DECL_OBJC_ID_ID:
7334 return Context.getObjCIdDecl();
7335
7336 case PREDEF_DECL_OBJC_SEL_ID:
7337 return Context.getObjCSelDecl();
7338
7339 case PREDEF_DECL_OBJC_CLASS_ID:
7340 return Context.getObjCClassDecl();
7341
7342 case PREDEF_DECL_OBJC_PROTOCOL_ID:
7343 return Context.getObjCProtocolDecl();
7344
7345 case PREDEF_DECL_INT_128_ID:
7346 return Context.getInt128Decl();
7347
7348 case PREDEF_DECL_UNSIGNED_INT_128_ID:
7349 return Context.getUInt128Decl();
7350
7351 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7352 return Context.getObjCInstanceTypeDecl();
7353
7354 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7355 return Context.getBuiltinVaListDecl();
Richard Smithf19e1272015-03-07 00:04:49 +00007356
Richard Smith9b88a4c2015-07-27 05:40:23 +00007357 case PREDEF_DECL_VA_LIST_TAG:
7358 return Context.getVaListTagDecl();
7359
Charles Davisc7d5c942015-09-17 20:55:33 +00007360 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7361 return Context.getBuiltinMSVaListDecl();
7362
Richard Smithbab6df82020-04-11 22:15:29 -07007363 case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7364 return Context.getMSGuidTagDecl();
7365
Richard Smithf19e1272015-03-07 00:04:49 +00007366 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7367 return Context.getExternCContextDecl();
David Majnemerd9b1a4f2015-11-04 03:40:30 +00007368
7369 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7370 return Context.getMakeIntegerSeqDecl();
Quentin Colombet043406b2016-02-03 22:41:00 +00007371
7372 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7373 return Context.getCFConstantStringDecl();
Ben Langmuirf5416742016-02-04 00:55:24 +00007374
7375 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7376 return Context.getCFConstantStringTagDecl();
Eric Fiselier6ad68552016-07-01 01:24:09 +00007377
7378 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7379 return Context.getTypePackElementDecl();
Richard Smithfe620d22015-03-05 23:24:12 +00007380 }
Yaron Keren322bdad2015-03-06 07:49:14 +00007381 llvm_unreachable("PredefinedDeclIDs unknown enum value");
Richard Smithfe620d22015-03-05 23:24:12 +00007382}
7383
Richard Smithcd45dbc2014-04-19 03:48:30 +00007384Decl *ASTReader::GetExistingDecl(DeclID ID) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00007385 assert(ContextObj && "reading decl with no AST context");
Richard Smithcd45dbc2014-04-19 03:48:30 +00007386 if (ID < NUM_PREDEF_DECL_IDS) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00007387 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
Richard Smithfe620d22015-03-05 23:24:12 +00007388 if (D) {
7389 // Track that we have merged the declaration with ID \p ID into the
7390 // pre-existing predefined declaration \p D.
Richard Smith5fc18a92015-07-12 23:43:21 +00007391 auto &Merged = KeyDecls[D->getCanonicalDecl()];
Richard Smithfe620d22015-03-05 23:24:12 +00007392 if (Merged.empty())
7393 Merged.push_back(ID);
Guy Benyei11169dd2012-12-18 14:30:41 +00007394 }
Richard Smithfe620d22015-03-05 23:24:12 +00007395 return D;
Guy Benyei11169dd2012-12-18 14:30:41 +00007396 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00007397
Guy Benyei11169dd2012-12-18 14:30:41 +00007398 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7399
7400 if (Index >= DeclsLoaded.size()) {
7401 assert(0 && "declaration ID out-of-range for AST file");
7402 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007403 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007404 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00007405
7406 return DeclsLoaded[Index];
7407}
7408
7409Decl *ASTReader::GetDecl(DeclID ID) {
7410 if (ID < NUM_PREDEF_DECL_IDS)
7411 return GetExistingDecl(ID);
7412
7413 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7414
7415 if (Index >= DeclsLoaded.size()) {
7416 assert(0 && "declaration ID out-of-range for AST file");
7417 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007418 return nullptr;
Richard Smithcd45dbc2014-04-19 03:48:30 +00007419 }
7420
Guy Benyei11169dd2012-12-18 14:30:41 +00007421 if (!DeclsLoaded[Index]) {
7422 ReadDeclRecord(ID);
7423 if (DeserializationListener)
7424 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7425 }
7426
7427 return DeclsLoaded[Index];
7428}
7429
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007430DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
Guy Benyei11169dd2012-12-18 14:30:41 +00007431 DeclID GlobalID) {
7432 if (GlobalID < NUM_PREDEF_DECL_IDS)
7433 return GlobalID;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007434
Guy Benyei11169dd2012-12-18 14:30:41 +00007435 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7436 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7437 ModuleFile *Owner = I->second;
7438
7439 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7440 = M.GlobalToLocalDeclIDs.find(Owner);
7441 if (Pos == M.GlobalToLocalDeclIDs.end())
7442 return 0;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007443
Guy Benyei11169dd2012-12-18 14:30:41 +00007444 return GlobalID - Owner->BaseDeclID + Pos->second;
7445}
7446
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007447serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Guy Benyei11169dd2012-12-18 14:30:41 +00007448 const RecordData &Record,
7449 unsigned &Idx) {
7450 if (Idx >= Record.size()) {
7451 Error("Corrupted AST file");
7452 return 0;
7453 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007454
Guy Benyei11169dd2012-12-18 14:30:41 +00007455 return getGlobalDeclID(F, Record[Idx++]);
7456}
7457
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007458/// Resolve the offset of a statement into a statement.
Guy Benyei11169dd2012-12-18 14:30:41 +00007459///
7460/// This operation will read a new statement from the external
7461/// source each time it is called, and is meant to be used via a
7462/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7463Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7464 // Switch case IDs are per Decl.
7465 ClearSwitchCaseIDs();
7466
7467 // Offset here is a global offset across the entire chain.
7468 RecordLocation Loc = getLocalBitOffset(Offset);
JF Bastien0e828952019-06-26 19:50:12 +00007469 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7470 Error(std::move(Err));
7471 return nullptr;
7472 }
David Blaikie9fd16f82017-03-08 23:57:08 +00007473 assert(NumCurrentElementsDeserializing == 0 &&
7474 "should not be called while already deserializing");
7475 Deserializing D(this);
Guy Benyei11169dd2012-12-18 14:30:41 +00007476 return ReadStmtFromStream(*Loc.F);
7477}
7478
Richard Smith3cb15722015-08-05 22:41:45 +00007479void ASTReader::FindExternalLexicalDecls(
7480 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7481 SmallVectorImpl<Decl *> &Decls) {
Richard Smith82f8fcd2015-08-06 22:07:25 +00007482 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7483
Richard Smith9ccdd932015-08-06 22:14:12 +00007484 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
Richard Smith82f8fcd2015-08-06 22:07:25 +00007485 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7486 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7487 auto K = (Decl::Kind)+LexicalDecls[I];
7488 if (!IsKindWeWant(K))
7489 continue;
7490
7491 auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7492
7493 // Don't add predefined declarations to the lexical context more
7494 // than once.
7495 if (ID < NUM_PREDEF_DECL_IDS) {
7496 if (PredefsVisited[ID])
7497 continue;
7498
7499 PredefsVisited[ID] = true;
7500 }
7501
7502 if (Decl *D = GetLocalDecl(*M, ID)) {
Richard Smith2317a3e2015-08-11 21:21:20 +00007503 assert(D->getKind() == K && "wrong kind for lexical decl");
Richard Smith82f8fcd2015-08-06 22:07:25 +00007504 if (!DC->isDeclInLexicalTraversal(D))
7505 Decls.push_back(D);
7506 }
7507 }
7508 };
7509
7510 if (isa<TranslationUnitDecl>(DC)) {
7511 for (auto Lexical : TULexicalDecls)
7512 Visit(Lexical.first, Lexical.second);
7513 } else {
7514 auto I = LexicalDecls.find(DC);
7515 if (I != LexicalDecls.end())
Richard Smith9c9173d2015-08-11 22:00:24 +00007516 Visit(I->second.first, I->second.second);
Richard Smith82f8fcd2015-08-06 22:07:25 +00007517 }
7518
Guy Benyei11169dd2012-12-18 14:30:41 +00007519 ++NumLexicalDeclContextsRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00007520}
7521
7522namespace {
7523
7524class DeclIDComp {
7525 ASTReader &Reader;
7526 ModuleFile &Mod;
7527
7528public:
7529 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7530
7531 bool operator()(LocalDeclID L, LocalDeclID R) const {
7532 SourceLocation LHS = getLocation(L);
7533 SourceLocation RHS = getLocation(R);
7534 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7535 }
7536
7537 bool operator()(SourceLocation LHS, LocalDeclID R) const {
7538 SourceLocation RHS = getLocation(R);
7539 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7540 }
7541
7542 bool operator()(LocalDeclID L, SourceLocation RHS) const {
7543 SourceLocation LHS = getLocation(L);
7544 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7545 }
7546
7547 SourceLocation getLocation(LocalDeclID ID) const {
7548 return Reader.getSourceManager().getFileLoc(
7549 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7550 }
7551};
7552
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007553} // namespace
Guy Benyei11169dd2012-12-18 14:30:41 +00007554
7555void ASTReader::FindFileRegionDecls(FileID File,
7556 unsigned Offset, unsigned Length,
7557 SmallVectorImpl<Decl *> &Decls) {
7558 SourceManager &SM = getSourceManager();
7559
7560 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7561 if (I == FileDeclIDs.end())
7562 return;
7563
7564 FileDeclsInfo &DInfo = I->second;
7565 if (DInfo.Decls.empty())
7566 return;
7567
7568 SourceLocation
7569 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7570 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7571
7572 DeclIDComp DIDComp(*this, *DInfo.Mod);
Fangrui Song7264a472019-07-03 08:13:17 +00007573 ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7574 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
Guy Benyei11169dd2012-12-18 14:30:41 +00007575 if (BeginIt != DInfo.Decls.begin())
7576 --BeginIt;
7577
7578 // If we are pointing at a top-level decl inside an objc container, we need
7579 // to backtrack until we find it otherwise we will fail to report that the
7580 // region overlaps with an objc container.
7581 while (BeginIt != DInfo.Decls.begin() &&
7582 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7583 ->isTopLevelDeclInObjCContainer())
7584 --BeginIt;
7585
Fangrui Song7264a472019-07-03 08:13:17 +00007586 ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7587 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
Guy Benyei11169dd2012-12-18 14:30:41 +00007588 if (EndIt != DInfo.Decls.end())
7589 ++EndIt;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007590
Guy Benyei11169dd2012-12-18 14:30:41 +00007591 for (ArrayRef<serialization::LocalDeclID>::iterator
7592 DIt = BeginIt; DIt != EndIt; ++DIt)
7593 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7594}
7595
Richard Smith9ce12e32013-02-07 03:30:24 +00007596bool
Guy Benyei11169dd2012-12-18 14:30:41 +00007597ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7598 DeclarationName Name) {
Richard Smithd88a7f12015-09-01 20:35:42 +00007599 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
Guy Benyei11169dd2012-12-18 14:30:41 +00007600 "DeclContext has no visible decls in storage");
7601 if (!Name)
Richard Smith9ce12e32013-02-07 03:30:24 +00007602 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00007603
Richard Smithd88a7f12015-09-01 20:35:42 +00007604 auto It = Lookups.find(DC);
7605 if (It == Lookups.end())
7606 return false;
7607
Richard Smith8c913ec2014-08-14 02:21:01 +00007608 Deserializing LookupResults(this);
7609
Richard Smithd88a7f12015-09-01 20:35:42 +00007610 // Load the list of declarations.
Guy Benyei11169dd2012-12-18 14:30:41 +00007611 SmallVector<NamedDecl *, 64> Decls;
Vedant Kumar48b4f762018-04-14 01:40:48 +00007612 for (DeclID ID : It->second.Table.find(Name)) {
7613 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
Richard Smithd88a7f12015-09-01 20:35:42 +00007614 if (ND->getDeclName() == Name)
7615 Decls.push_back(ND);
7616 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00007617
Guy Benyei11169dd2012-12-18 14:30:41 +00007618 ++NumVisibleDeclContextsRead;
7619 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith9ce12e32013-02-07 03:30:24 +00007620 return !Decls.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +00007621}
7622
Guy Benyei11169dd2012-12-18 14:30:41 +00007623void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7624 if (!DC->hasExternalVisibleStorage())
7625 return;
Richard Smithd88a7f12015-09-01 20:35:42 +00007626
7627 auto It = Lookups.find(DC);
7628 assert(It != Lookups.end() &&
7629 "have external visible storage but no lookup tables");
7630
Craig Topper79be4cd2013-07-05 04:33:53 +00007631 DeclsMap Decls;
Guy Benyei11169dd2012-12-18 14:30:41 +00007632
Vedant Kumar48b4f762018-04-14 01:40:48 +00007633 for (DeclID ID : It->second.Table.findAll()) {
7634 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
Richard Smithd88a7f12015-09-01 20:35:42 +00007635 Decls[ND->getDeclName()].push_back(ND);
Guy Benyei11169dd2012-12-18 14:30:41 +00007636 }
7637
Guy Benyei11169dd2012-12-18 14:30:41 +00007638 ++NumVisibleDeclContextsRead;
7639
Vedant Kumar48b4f762018-04-14 01:40:48 +00007640 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7641 SetExternalVisibleDeclsForName(DC, I->first, I->second);
7642 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007643 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7644}
7645
Richard Smithd88a7f12015-09-01 20:35:42 +00007646const serialization::reader::DeclContextLookupTable *
7647ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7648 auto I = Lookups.find(Primary);
7649 return I == Lookups.end() ? nullptr : &I->second;
7650}
7651
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007652/// Under non-PCH compilation the consumer receives the objc methods
Guy Benyei11169dd2012-12-18 14:30:41 +00007653/// before receiving the implementation, and codegen depends on this.
7654/// We simulate this by deserializing and passing to consumer the methods of the
7655/// implementation before passing the deserialized implementation decl.
7656static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7657 ASTConsumer *Consumer) {
7658 assert(ImplD && Consumer);
7659
Aaron Ballmanaff18c02014-03-13 19:03:34 +00007660 for (auto *I : ImplD->methods())
7661 Consumer->HandleInterestingDecl(DeclGroupRef(I));
Guy Benyei11169dd2012-12-18 14:30:41 +00007662
7663 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7664}
7665
Guy Benyei11169dd2012-12-18 14:30:41 +00007666void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00007667 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +00007668 PassObjCImplDeclToConsumer(ImplD, Consumer);
7669 else
7670 Consumer->HandleInterestingDecl(DeclGroupRef(D));
7671}
7672
7673void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7674 this->Consumer = Consumer;
7675
Richard Smith9e2341d2015-03-23 03:25:59 +00007676 if (Consumer)
7677 PassInterestingDeclsToConsumer();
Richard Smith7f330cd2015-03-18 01:42:29 +00007678
7679 if (DeserializationListener)
7680 DeserializationListener->ReaderInitialized(this);
Guy Benyei11169dd2012-12-18 14:30:41 +00007681}
7682
7683void ASTReader::PrintStats() {
7684 std::fprintf(stderr, "*** AST File Statistics:\n");
7685
7686 unsigned NumTypesLoaded
7687 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7688 QualType());
7689 unsigned NumDeclsLoaded
7690 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00007691 (Decl *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00007692 unsigned NumIdentifiersLoaded
7693 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7694 IdentifiersLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00007695 (IdentifierInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00007696 unsigned NumMacrosLoaded
7697 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7698 MacrosLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00007699 (MacroInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00007700 unsigned NumSelectorsLoaded
7701 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7702 SelectorsLoaded.end(),
7703 Selector());
7704
7705 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7706 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
7707 NumSLocEntriesRead, TotalNumSLocEntries,
7708 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7709 if (!TypesLoaded.empty())
7710 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
7711 NumTypesLoaded, (unsigned)TypesLoaded.size(),
7712 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7713 if (!DeclsLoaded.empty())
7714 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
7715 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7716 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7717 if (!IdentifiersLoaded.empty())
7718 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
7719 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7720 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7721 if (!MacrosLoaded.empty())
7722 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
7723 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7724 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7725 if (!SelectorsLoaded.empty())
7726 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
7727 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7728 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7729 if (TotalNumStatements)
7730 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
7731 NumStatementsRead, TotalNumStatements,
7732 ((float)NumStatementsRead/TotalNumStatements * 100));
7733 if (TotalNumMacros)
7734 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
7735 NumMacrosRead, TotalNumMacros,
7736 ((float)NumMacrosRead/TotalNumMacros * 100));
7737 if (TotalLexicalDeclContexts)
7738 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
7739 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7740 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7741 * 100));
7742 if (TotalVisibleDeclContexts)
7743 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
7744 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7745 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7746 * 100));
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007747 if (TotalNumMethodPoolEntries)
Guy Benyei11169dd2012-12-18 14:30:41 +00007748 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
7749 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7750 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7751 * 100));
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007752 if (NumMethodPoolLookups)
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007753 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
7754 NumMethodPoolHits, NumMethodPoolLookups,
7755 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007756 if (NumMethodPoolTableLookups)
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007757 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
7758 NumMethodPoolTableHits, NumMethodPoolTableLookups,
7759 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7760 * 100.0));
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00007761 if (NumIdentifierLookupHits)
Douglas Gregor00a50f72013-01-25 00:38:33 +00007762 std::fprintf(stderr,
7763 " %u / %u identifier table lookups succeeded (%f%%)\n",
7764 NumIdentifierLookupHits, NumIdentifierLookups,
7765 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
Douglas Gregor00a50f72013-01-25 00:38:33 +00007766
Douglas Gregore060e572013-01-25 01:03:03 +00007767 if (GlobalIndex) {
7768 std::fprintf(stderr, "\n");
7769 GlobalIndex->printStats();
7770 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007771
Guy Benyei11169dd2012-12-18 14:30:41 +00007772 std::fprintf(stderr, "\n");
7773 dump();
7774 std::fprintf(stderr, "\n");
7775}
7776
7777template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Vassil Vassilevb2710682017-03-02 18:13:19 +00007778LLVM_DUMP_METHOD static void
Guy Benyei11169dd2012-12-18 14:30:41 +00007779dumpModuleIDMap(StringRef Name,
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007780 const ContinuousRangeMap<Key, ModuleFile *,
Guy Benyei11169dd2012-12-18 14:30:41 +00007781 InitialCapacity> &Map) {
7782 if (Map.begin() == Map.end())
7783 return;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007784
Vedant Kumar48b4f762018-04-14 01:40:48 +00007785 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7786
Guy Benyei11169dd2012-12-18 14:30:41 +00007787 llvm::errs() << Name << ":\n";
Vedant Kumar48b4f762018-04-14 01:40:48 +00007788 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7789 I != IEnd; ++I) {
7790 llvm::errs() << " " << I->first << " -> " << I->second->FileName
7791 << "\n";
7792 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007793}
7794
Yaron Kerencdae9412016-01-29 19:38:18 +00007795LLVM_DUMP_METHOD void ASTReader::dump() {
Guy Benyei11169dd2012-12-18 14:30:41 +00007796 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7797 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7798 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7799 dumpModuleIDMap("Global type map", GlobalTypeMap);
7800 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7801 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7802 dumpModuleIDMap("Global macro map", GlobalMacroMap);
7803 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7804 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007805 dumpModuleIDMap("Global preprocessed entity map",
Guy Benyei11169dd2012-12-18 14:30:41 +00007806 GlobalPreprocessedEntityMap);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00007807
Guy Benyei11169dd2012-12-18 14:30:41 +00007808 llvm::errs() << "\n*** PCH/Modules Loaded:";
Vedant Kumar48b4f762018-04-14 01:40:48 +00007809 for (ModuleFile &M : ModuleMgr)
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00007810 M.dump();
Guy Benyei11169dd2012-12-18 14:30:41 +00007811}
7812
7813/// Return the amount of memory used by memory buffers, breaking down
7814/// by heap-backed versus mmap'ed memory.
7815void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Vedant Kumar48b4f762018-04-14 01:40:48 +00007816 for (ModuleFile &I : ModuleMgr) {
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00007817 if (llvm::MemoryBuffer *buf = I.Buffer) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007818 size_t bytes = buf->getBufferSize();
7819 switch (buf->getBufferKind()) {
7820 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7821 sizes.malloc_bytes += bytes;
7822 break;
7823 case llvm::MemoryBuffer::MemoryBuffer_MMap:
7824 sizes.mmap_bytes += bytes;
7825 break;
7826 }
7827 }
7828 }
7829}
7830
7831void ASTReader::InitializeSema(Sema &S) {
7832 SemaObj = &S;
7833 S.addExternalSource(this);
7834
7835 // Makes sure any declarations that were deserialized "too early"
7836 // still get added to the identifier's declaration chains.
Vedant Kumar48b4f762018-04-14 01:40:48 +00007837 for (uint64_t ID : PreloadedDeclIDs) {
7838 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
Ben Langmuir5418f402014-09-10 21:29:41 +00007839 pushExternalDeclIntoScope(D, D->getDeclName());
Guy Benyei11169dd2012-12-18 14:30:41 +00007840 }
Ben Langmuir5418f402014-09-10 21:29:41 +00007841 PreloadedDeclIDs.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00007842
Richard Smith3d8e97e2013-10-18 06:54:39 +00007843 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00007844 if (!FPPragmaOptions.empty()) {
7845 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
Melanie Blowerdefd43a2020-06-26 08:45:12 -07007846 SemaObj->CurFPFeatures = FPOptions(FPPragmaOptions[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007847 }
7848
Yaxun Liu5b746652016-12-18 05:18:55 +00007849 SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7850 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7851 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
Richard Smith3d8e97e2013-10-18 06:54:39 +00007852
7853 UpdateSema();
7854}
7855
7856void ASTReader::UpdateSema() {
7857 assert(SemaObj && "no Sema to update");
7858
7859 // Load the offsets of the declarations that Sema references.
7860 // They will be lazily deserialized when needed.
7861 if (!SemaDeclRefs.empty()) {
Richard Smith96269c52016-09-29 22:49:46 +00007862 assert(SemaDeclRefs.size() % 3 == 0);
7863 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
Richard Smith3d8e97e2013-10-18 06:54:39 +00007864 if (!SemaObj->StdNamespace)
7865 SemaObj->StdNamespace = SemaDeclRefs[I];
7866 if (!SemaObj->StdBadAlloc)
7867 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
Richard Smith96269c52016-09-29 22:49:46 +00007868 if (!SemaObj->StdAlignValT)
7869 SemaObj->StdAlignValT = SemaDeclRefs[I+2];
Richard Smith3d8e97e2013-10-18 06:54:39 +00007870 }
7871 SemaDeclRefs.clear();
7872 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00007873
Nico Weber779355f2016-03-02 23:22:00 +00007874 // Update the state of pragmas. Use the same API as if we had encountered the
7875 // pragma in the source.
Dario Domizioli13a0a382014-05-23 12:13:25 +00007876 if(OptimizeOffPragmaLocation.isValid())
Rui Ueyama49a3ad22019-07-16 04:46:31 +00007877 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
Nico Weber779355f2016-03-02 23:22:00 +00007878 if (PragmaMSStructState != -1)
7879 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
Nico Weber42932312016-03-03 00:17:35 +00007880 if (PointersToMembersPragmaLocation.isValid()) {
7881 SemaObj->ActOnPragmaMSPointersToMembers(
7882 (LangOptions::PragmaMSPointersToMembersKind)
7883 PragmaMSPointersToMembersState,
7884 PointersToMembersPragmaLocation);
7885 }
Justin Lebar67a78a62016-10-08 22:15:58 +00007886 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00007887
7888 if (PragmaPackCurrentValue) {
7889 // The bottom of the stack might have a default value. It must be adjusted
7890 // to the current value to ensure that the packing state is preserved after
7891 // popping entries that were included/imported from a PCH/module.
7892 bool DropFirst = false;
7893 if (!PragmaPackStack.empty() &&
7894 PragmaPackStack.front().Location.isInvalid()) {
7895 assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7896 "Expected a default alignment value");
7897 SemaObj->PackStack.Stack.emplace_back(
7898 PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
Alex Lorenz45b40142017-07-28 14:41:21 +00007899 SemaObj->PackStack.CurrentPragmaLocation,
7900 PragmaPackStack.front().PushLocation);
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00007901 DropFirst = true;
7902 }
7903 for (const auto &Entry :
7904 llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7905 SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
Alex Lorenz45b40142017-07-28 14:41:21 +00007906 Entry.Location, Entry.PushLocation);
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00007907 if (PragmaPackCurrentLocation.isInvalid()) {
7908 assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7909 "Expected a default alignment value");
7910 // Keep the current values.
7911 } else {
7912 SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7913 SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7914 }
7915 }
Melanie Blowerf5360d42020-05-01 10:32:06 -07007916 if (FpPragmaCurrentValue) {
7917 // The bottom of the stack might have a default value. It must be adjusted
7918 // to the current value to ensure that fp-pragma state is preserved after
7919 // popping entries that were included/imported from a PCH/module.
7920 bool DropFirst = false;
7921 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7922 assert(FpPragmaStack.front().Value ==
7923 SemaObj->FpPragmaStack.DefaultValue &&
7924 "Expected a default pragma float_control value");
7925 SemaObj->FpPragmaStack.Stack.emplace_back(
7926 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7927 SemaObj->FpPragmaStack.CurrentPragmaLocation,
7928 FpPragmaStack.front().PushLocation);
7929 DropFirst = true;
7930 }
7931 for (const auto &Entry :
7932 llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7933 SemaObj->FpPragmaStack.Stack.emplace_back(
7934 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7935 if (FpPragmaCurrentLocation.isInvalid()) {
7936 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7937 "Expected a default pragma float_control value");
7938 // Keep the current values.
7939 } else {
7940 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7941 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7942 }
7943 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007944}
7945
Richard Smitha8d5b6a2015-07-17 19:51:03 +00007946IdentifierInfo *ASTReader::get(StringRef Name) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007947 // Note that we are loading an identifier.
7948 Deserializing AnIdentifier(this);
Douglas Gregore060e572013-01-25 01:03:03 +00007949
Douglas Gregor7211ac12013-01-25 23:32:03 +00007950 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregor00a50f72013-01-25 00:38:33 +00007951 NumIdentifierLookups,
7952 NumIdentifierLookupHits);
Richard Smith33e0f7e2015-07-22 02:08:40 +00007953
7954 // We don't need to do identifier table lookups in C++ modules (we preload
7955 // all interesting declarations, and don't need to use the scope for name
7956 // lookups). Perform the lookup in PCH files, though, since we don't build
7957 // a complete initial identifier table if we're carrying on from a PCH.
Richard Smithdbafb6c2017-06-29 23:23:46 +00007958 if (PP.getLangOpts().CPlusPlus) {
Richard Smith33e0f7e2015-07-22 02:08:40 +00007959 for (auto F : ModuleMgr.pch_modules())
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007960 if (Visitor(*F))
Richard Smith33e0f7e2015-07-22 02:08:40 +00007961 break;
7962 } else {
7963 // If there is a global index, look there first to determine which modules
7964 // provably do not have any results for this identifier.
7965 GlobalModuleIndex::HitSet Hits;
7966 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7967 if (!loadGlobalIndex()) {
7968 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7969 HitsPtr = &Hits;
7970 }
7971 }
7972
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007973 ModuleMgr.visit(Visitor, HitsPtr);
Richard Smith33e0f7e2015-07-22 02:08:40 +00007974 }
7975
Guy Benyei11169dd2012-12-18 14:30:41 +00007976 IdentifierInfo *II = Visitor.getIdentifierInfo();
7977 markIdentifierUpToDate(II);
7978 return II;
7979}
7980
7981namespace clang {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00007982
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007983 /// An identifier-lookup iterator that enumerates all of the
Guy Benyei11169dd2012-12-18 14:30:41 +00007984 /// identifiers stored within a set of AST files.
7985 class ASTIdentifierIterator : public IdentifierIterator {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007986 /// The AST reader whose identifiers are being enumerated.
Guy Benyei11169dd2012-12-18 14:30:41 +00007987 const ASTReader &Reader;
7988
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007989 /// The current index into the chain of AST files stored in
Guy Benyei11169dd2012-12-18 14:30:41 +00007990 /// the AST reader.
7991 unsigned Index;
7992
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007993 /// The current position within the identifier lookup table
Guy Benyei11169dd2012-12-18 14:30:41 +00007994 /// of the current AST file.
7995 ASTIdentifierLookupTable::key_iterator Current;
7996
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007997 /// The end position within the identifier lookup table of
Guy Benyei11169dd2012-12-18 14:30:41 +00007998 /// the current AST file.
7999 ASTIdentifierLookupTable::key_iterator End;
8000
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008001 /// Whether to skip any modules in the ASTReader.
Ben Langmuir537c5b52016-05-04 00:53:13 +00008002 bool SkipModules;
8003
Guy Benyei11169dd2012-12-18 14:30:41 +00008004 public:
Ben Langmuir537c5b52016-05-04 00:53:13 +00008005 explicit ASTIdentifierIterator(const ASTReader &Reader,
8006 bool SkipModules = false);
Guy Benyei11169dd2012-12-18 14:30:41 +00008007
Craig Topper3e89dfe2014-03-13 02:13:41 +00008008 StringRef Next() override;
Guy Benyei11169dd2012-12-18 14:30:41 +00008009 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00008010
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008011} // namespace clang
Guy Benyei11169dd2012-12-18 14:30:41 +00008012
Ben Langmuir537c5b52016-05-04 00:53:13 +00008013ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8014 bool SkipModules)
8015 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008016}
8017
8018StringRef ASTIdentifierIterator::Next() {
8019 while (Current == End) {
8020 // If we have exhausted all of our AST files, we're done.
8021 if (Index == 0)
Vedant Kumar48b4f762018-04-14 01:40:48 +00008022 return StringRef();
Guy Benyei11169dd2012-12-18 14:30:41 +00008023
8024 --Index;
Ben Langmuir537c5b52016-05-04 00:53:13 +00008025 ModuleFile &F = Reader.ModuleMgr[Index];
8026 if (SkipModules && F.isModule())
8027 continue;
8028
Vedant Kumar48b4f762018-04-14 01:40:48 +00008029 ASTIdentifierLookupTable *IdTable =
8030 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
Guy Benyei11169dd2012-12-18 14:30:41 +00008031 Current = IdTable->key_begin();
8032 End = IdTable->key_end();
8033 }
8034
8035 // We have any identifiers remaining in the current AST file; return
8036 // the next one.
Douglas Gregorbfd73d72013-01-23 18:53:14 +00008037 StringRef Result = *Current;
Guy Benyei11169dd2012-12-18 14:30:41 +00008038 ++Current;
Douglas Gregorbfd73d72013-01-23 18:53:14 +00008039 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00008040}
8041
Ben Langmuir537c5b52016-05-04 00:53:13 +00008042namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00008043
Ben Langmuir537c5b52016-05-04 00:53:13 +00008044/// A utility for appending two IdentifierIterators.
8045class ChainedIdentifierIterator : public IdentifierIterator {
8046 std::unique_ptr<IdentifierIterator> Current;
8047 std::unique_ptr<IdentifierIterator> Queued;
8048
8049public:
8050 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8051 std::unique_ptr<IdentifierIterator> Second)
8052 : Current(std::move(First)), Queued(std::move(Second)) {}
8053
8054 StringRef Next() override {
8055 if (!Current)
Vedant Kumar48b4f762018-04-14 01:40:48 +00008056 return StringRef();
Ben Langmuir537c5b52016-05-04 00:53:13 +00008057
8058 StringRef result = Current->Next();
8059 if (!result.empty())
8060 return result;
8061
8062 // Try the queued iterator, which may itself be empty.
8063 Current.reset();
8064 std::swap(Current, Queued);
8065 return Next();
8066 }
8067};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00008068
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008069} // namespace
Ben Langmuir537c5b52016-05-04 00:53:13 +00008070
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00008071IdentifierIterator *ASTReader::getIdentifiers() {
Ben Langmuir537c5b52016-05-04 00:53:13 +00008072 if (!loadGlobalIndex()) {
8073 std::unique_ptr<IdentifierIterator> ReaderIter(
8074 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8075 std::unique_ptr<IdentifierIterator> ModulesIter(
8076 GlobalIndex->createIdentifierIterator());
8077 return new ChainedIdentifierIterator(std::move(ReaderIter),
8078 std::move(ModulesIter));
8079 }
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00008080
Guy Benyei11169dd2012-12-18 14:30:41 +00008081 return new ASTIdentifierIterator(*this);
8082}
8083
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00008084namespace clang {
8085namespace serialization {
8086
Guy Benyei11169dd2012-12-18 14:30:41 +00008087 class ReadMethodPoolVisitor {
8088 ASTReader &Reader;
8089 Selector Sel;
8090 unsigned PriorGeneration;
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008091 unsigned InstanceBits = 0;
8092 unsigned FactoryBits = 0;
8093 bool InstanceHasMoreThanOneDecl = false;
8094 bool FactoryHasMoreThanOneDecl = false;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00008095 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8096 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00008097
8098 public:
Nico Weber2e0c8f72014-12-27 03:58:08 +00008099 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
Guy Benyei11169dd2012-12-18 14:30:41 +00008100 unsigned PriorGeneration)
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008101 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
Nico Weber2e0c8f72014-12-27 03:58:08 +00008102
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00008103 bool operator()(ModuleFile &M) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008104 if (!M.SelectorLookupTable)
8105 return false;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008106
Guy Benyei11169dd2012-12-18 14:30:41 +00008107 // If we've already searched this module file, skip it now.
Richard Smithbdf2d932015-07-30 03:37:16 +00008108 if (M.Generation <= PriorGeneration)
Guy Benyei11169dd2012-12-18 14:30:41 +00008109 return true;
8110
Richard Smithbdf2d932015-07-30 03:37:16 +00008111 ++Reader.NumMethodPoolTableLookups;
Vedant Kumar48b4f762018-04-14 01:40:48 +00008112 ASTSelectorLookupTable *PoolTable
8113 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
Richard Smithbdf2d932015-07-30 03:37:16 +00008114 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Guy Benyei11169dd2012-12-18 14:30:41 +00008115 if (Pos == PoolTable->end())
8116 return false;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00008117
Richard Smithbdf2d932015-07-30 03:37:16 +00008118 ++Reader.NumMethodPoolTableHits;
8119 ++Reader.NumSelectorsRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00008120 // FIXME: Not quite happy with the statistics here. We probably should
8121 // disable this tracking when called via LoadSelector.
8122 // Also, should entries without methods count as misses?
Richard Smithbdf2d932015-07-30 03:37:16 +00008123 ++Reader.NumMethodPoolEntriesRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00008124 ASTSelectorLookupTrait::data_type Data = *Pos;
Richard Smithbdf2d932015-07-30 03:37:16 +00008125 if (Reader.DeserializationListener)
8126 Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00008127
Richard Smithbdf2d932015-07-30 03:37:16 +00008128 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8129 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8130 InstanceBits = Data.InstanceBits;
8131 FactoryBits = Data.FactoryBits;
8132 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8133 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00008134 return true;
8135 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008136
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008137 /// Retrieve the instance methods found by this visitor.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008138 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8139 return InstanceMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00008140 }
8141
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008142 /// Retrieve the instance methods found by this visitor.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008143 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
Guy Benyei11169dd2012-12-18 14:30:41 +00008144 return FactoryMethods;
8145 }
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00008146
8147 unsigned getInstanceBits() const { return InstanceBits; }
8148 unsigned getFactoryBits() const { return FactoryBits; }
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008149
Nico Weberff4b35e2014-12-27 22:14:15 +00008150 bool instanceHasMoreThanOneDecl() const {
8151 return InstanceHasMoreThanOneDecl;
8152 }
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008153
Nico Weberff4b35e2014-12-27 22:14:15 +00008154 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
Guy Benyei11169dd2012-12-18 14:30:41 +00008155 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00008156
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008157} // namespace serialization
8158} // namespace clang
Guy Benyei11169dd2012-12-18 14:30:41 +00008159
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008160/// Add the given set of methods to the method list.
Guy Benyei11169dd2012-12-18 14:30:41 +00008161static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8162 ObjCMethodList &List) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008163 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8164 S.addMethodToGlobalList(&List, Methods[I]);
8165 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008166}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008167
Guy Benyei11169dd2012-12-18 14:30:41 +00008168void ASTReader::ReadMethodPool(Selector Sel) {
8169 // Get the selector generation and update it to the current generation.
8170 unsigned &Generation = SelectorGeneration[Sel];
8171 unsigned PriorGeneration = Generation;
Richard Smith053f6c62014-05-16 23:01:30 +00008172 Generation = getGeneration();
Manman Rena0f31a02016-04-29 19:04:05 +00008173 SelectorOutOfDate[Sel] = false;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008174
Guy Benyei11169dd2012-12-18 14:30:41 +00008175 // Search for methods defined with this selector.
Douglas Gregorad2f7a52013-01-28 17:54:36 +00008176 ++NumMethodPoolLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00008177 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00008178 ModuleMgr.visit(Visitor);
8179
Guy Benyei11169dd2012-12-18 14:30:41 +00008180 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregorad2f7a52013-01-28 17:54:36 +00008181 Visitor.getFactoryMethods().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00008182 return;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00008183
8184 ++NumMethodPoolHits;
8185
Guy Benyei11169dd2012-12-18 14:30:41 +00008186 if (!getSema())
8187 return;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008188
Guy Benyei11169dd2012-12-18 14:30:41 +00008189 Sema &S = *getSema();
8190 Sema::GlobalMethodPool::iterator Pos
8191 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
Ben Langmuira0c32e92015-01-12 19:27:00 +00008192
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00008193 Pos->second.first.setBits(Visitor.getInstanceBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00008194 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00008195 Pos->second.second.setBits(Visitor.getFactoryBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00008196 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
Ben Langmuira0c32e92015-01-12 19:27:00 +00008197
8198 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8199 // when building a module we keep every method individually and may need to
8200 // update hasMoreThanOneDecl as we add the methods.
8201 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8202 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Guy Benyei11169dd2012-12-18 14:30:41 +00008203}
8204
Manman Rena0f31a02016-04-29 19:04:05 +00008205void ASTReader::updateOutOfDateSelector(Selector Sel) {
8206 if (SelectorOutOfDate[Sel])
8207 ReadMethodPool(Sel);
8208}
8209
Guy Benyei11169dd2012-12-18 14:30:41 +00008210void ASTReader::ReadKnownNamespaces(
8211 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8212 Namespaces.clear();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008213
Vedant Kumar48b4f762018-04-14 01:40:48 +00008214 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8215 if (NamespaceDecl *Namespace
8216 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
Guy Benyei11169dd2012-12-18 14:30:41 +00008217 Namespaces.push_back(Namespace);
Vedant Kumar48b4f762018-04-14 01:40:48 +00008218 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008219}
8220
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00008221void ASTReader::ReadUndefinedButUsed(
Richard Smithd6a04d72016-03-25 21:49:43 +00008222 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00008223 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008224 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky8334af82013-01-26 00:35:08 +00008225 SourceLocation Loc =
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00008226 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky8334af82013-01-26 00:35:08 +00008227 Undefined.insert(std::make_pair(D, Loc));
8228 }
8229}
Nick Lewycky8334af82013-01-26 00:35:08 +00008230
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00008231void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8232 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8233 Exprs) {
8234 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008235 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00008236 uint64_t Count = DelayedDeleteExprs[Idx++];
8237 for (uint64_t C = 0; C < Count; ++C) {
8238 SourceLocation DeleteLoc =
8239 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8240 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8241 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8242 }
8243 }
8244}
8245
Guy Benyei11169dd2012-12-18 14:30:41 +00008246void ASTReader::ReadTentativeDefinitions(
8247 SmallVectorImpl<VarDecl *> &TentativeDefs) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008248 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8249 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008250 if (Var)
8251 TentativeDefs.push_back(Var);
8252 }
8253 TentativeDefinitions.clear();
8254}
8255
8256void ASTReader::ReadUnusedFileScopedDecls(
8257 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008258 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8259 DeclaratorDecl *D
8260 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008261 if (D)
8262 Decls.push_back(D);
8263 }
8264 UnusedFileScopedDecls.clear();
8265}
8266
8267void ASTReader::ReadDelegatingConstructors(
8268 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008269 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8270 CXXConstructorDecl *D
8271 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008272 if (D)
8273 Decls.push_back(D);
8274 }
8275 DelegatingCtorDecls.clear();
8276}
8277
8278void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008279 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8280 TypedefNameDecl *D
8281 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008282 if (D)
8283 Decls.push_back(D);
8284 }
8285 ExtVectorDecls.clear();
8286}
8287
Nico Weber72889432014-09-06 01:25:55 +00008288void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8289 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008290 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8291 ++I) {
8292 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8293 GetDecl(UnusedLocalTypedefNameCandidates[I]));
Nico Weber72889432014-09-06 01:25:55 +00008294 if (D)
8295 Decls.insert(D);
8296 }
8297 UnusedLocalTypedefNameCandidates.clear();
8298}
8299
Yaxun (Sam) Liub670ab72020-02-26 10:57:39 -05008300void ASTReader::ReadDeclsToCheckForDeferredDiags(
8301 llvm::SmallVector<Decl *, 4> &Decls) {
8302 for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N;
8303 ++I) {
8304 auto *D = dyn_cast_or_null<Decl>(
8305 GetDecl(DeclsToCheckForDeferredDiags[I]));
8306 if (D)
8307 Decls.push_back(D);
8308 }
8309 DeclsToCheckForDeferredDiags.clear();
8310}
8311
8312
Guy Benyei11169dd2012-12-18 14:30:41 +00008313void ASTReader::ReadReferencedSelectors(
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008314 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008315 if (ReferencedSelectorsData.empty())
8316 return;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008317
Guy Benyei11169dd2012-12-18 14:30:41 +00008318 // If there are @selector references added them to its pool. This is for
8319 // implementation of -Wselector.
8320 unsigned int DataSize = ReferencedSelectorsData.size()-1;
8321 unsigned I = 0;
8322 while (I < DataSize) {
8323 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8324 SourceLocation SelLoc
8325 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8326 Sels.push_back(std::make_pair(Sel, SelLoc));
8327 }
8328 ReferencedSelectorsData.clear();
8329}
8330
8331void ASTReader::ReadWeakUndeclaredIdentifiers(
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008332 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008333 if (WeakUndeclaredIdentifiers.empty())
8334 return;
8335
8336 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008337 IdentifierInfo *WeakId
Guy Benyei11169dd2012-12-18 14:30:41 +00008338 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008339 IdentifierInfo *AliasId
Guy Benyei11169dd2012-12-18 14:30:41 +00008340 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8341 SourceLocation Loc
8342 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8343 bool Used = WeakUndeclaredIdentifiers[I++];
8344 WeakInfo WI(AliasId, Loc);
8345 WI.setUsed(Used);
8346 WeakIDs.push_back(std::make_pair(WeakId, WI));
8347 }
8348 WeakUndeclaredIdentifiers.clear();
8349}
8350
8351void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8352 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8353 ExternalVTableUse VT;
8354 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8355 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8356 VT.DefinitionRequired = VTableUses[Idx++];
8357 VTables.push_back(VT);
8358 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008359
Guy Benyei11169dd2012-12-18 14:30:41 +00008360 VTableUses.clear();
8361}
8362
8363void ASTReader::ReadPendingInstantiations(
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00008364 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008365 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008366 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00008367 SourceLocation Loc
8368 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8369
8370 Pending.push_back(std::make_pair(D, Loc));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008371 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008372 PendingInstantiations.clear();
8373}
8374
Richard Smithe40f2ba2013-08-07 21:41:30 +00008375void ASTReader::ReadLateParsedTemplates(
Justin Lebar28f09c52016-10-10 16:26:08 +00008376 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8377 &LPTMap) {
Richard Smithe40f2ba2013-08-07 21:41:30 +00008378 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8379 /* In loop */) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00008380 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
Richard Smithe40f2ba2013-08-07 21:41:30 +00008381
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00008382 auto LT = std::make_unique<LateParsedTemplate>();
Richard Smithe40f2ba2013-08-07 21:41:30 +00008383 LT->D = GetDecl(LateParsedTemplates[Idx++]);
8384
8385 ModuleFile *F = getOwningModuleFile(LT->D);
8386 assert(F && "No module");
8387
8388 unsigned TokN = LateParsedTemplates[Idx++];
8389 LT->Toks.reserve(TokN);
8390 for (unsigned T = 0; T < TokN; ++T)
8391 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8392
Justin Lebar28f09c52016-10-10 16:26:08 +00008393 LPTMap.insert(std::make_pair(FD, std::move(LT)));
Richard Smithe40f2ba2013-08-07 21:41:30 +00008394 }
8395
8396 LateParsedTemplates.clear();
8397}
8398
Guy Benyei11169dd2012-12-18 14:30:41 +00008399void ASTReader::LoadSelector(Selector Sel) {
8400 // It would be complicated to avoid reading the methods anyway. So don't.
8401 ReadMethodPool(Sel);
8402}
8403
8404void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8405 assert(ID && "Non-zero identifier ID required");
8406 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8407 IdentifiersLoaded[ID - 1] = II;
8408 if (DeserializationListener)
8409 DeserializationListener->IdentifierRead(ID, II);
8410}
8411
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008412/// Set the globally-visible declarations associated with the given
Guy Benyei11169dd2012-12-18 14:30:41 +00008413/// identifier.
8414///
8415/// If the AST reader is currently in a state where the given declaration IDs
8416/// cannot safely be resolved, they are queued until it is safe to resolve
8417/// them.
8418///
8419/// \param II an IdentifierInfo that refers to one or more globally-visible
8420/// declarations.
8421///
8422/// \param DeclIDs the set of declaration IDs with the name @p II that are
8423/// visible at global scope.
8424///
Douglas Gregor6168bd22013-02-18 15:53:43 +00008425/// \param Decls if non-null, this vector will be populated with the set of
8426/// deserialized declarations. These declarations will not be pushed into
8427/// scope.
Guy Benyei11169dd2012-12-18 14:30:41 +00008428void
8429ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8430 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor6168bd22013-02-18 15:53:43 +00008431 SmallVectorImpl<Decl *> *Decls) {
8432 if (NumCurrentElementsDeserializing && !Decls) {
8433 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00008434 return;
8435 }
8436
Vedant Kumar48b4f762018-04-14 01:40:48 +00008437 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
Ben Langmuir5418f402014-09-10 21:29:41 +00008438 if (!SemaObj) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008439 // Queue this declaration so that it will be added to the
8440 // translation unit scope and identifier's declaration chain
8441 // once a Sema object is known.
Vedant Kumar48b4f762018-04-14 01:40:48 +00008442 PreloadedDeclIDs.push_back(DeclIDs[I]);
Ben Langmuir5418f402014-09-10 21:29:41 +00008443 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00008444 }
Ben Langmuir5418f402014-09-10 21:29:41 +00008445
Vedant Kumar48b4f762018-04-14 01:40:48 +00008446 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
Ben Langmuir5418f402014-09-10 21:29:41 +00008447
8448 // If we're simply supposed to record the declarations, do so now.
8449 if (Decls) {
8450 Decls->push_back(D);
8451 continue;
8452 }
8453
8454 // Introduce this declaration into the translation-unit scope
8455 // and add it to the declaration chain for this identifier, so
8456 // that (unqualified) name lookup will find it.
8457 pushExternalDeclIntoScope(D, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00008458 }
8459}
8460
Douglas Gregorc8a992f2013-01-21 16:52:34 +00008461IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008462 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00008463 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008464
8465 if (IdentifiersLoaded.empty()) {
8466 Error("no identifier table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00008467 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008468 }
8469
8470 ID -= 1;
8471 if (!IdentifiersLoaded[ID]) {
8472 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8473 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8474 ModuleFile *M = I->second;
8475 unsigned Index = ID - M->BaseIdentifierID;
8476 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8477
8478 // All of the strings in the AST file are preceded by a 16-bit length.
8479 // Extract that 16-bit length to avoid having to execute strlen().
8480 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8481 // unsigned integers. This is important to avoid integer overflow when
8482 // we cast them to 'unsigned'.
8483 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8484 unsigned StrLen = (((unsigned) StrLenPtr[0])
8485 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Richard Smitheb4b58f62016-02-05 01:40:54 +00008486 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8487 IdentifiersLoaded[ID] = &II;
8488 markIdentifierFromAST(*this, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00008489 if (DeserializationListener)
Richard Smitheb4b58f62016-02-05 01:40:54 +00008490 DeserializationListener->IdentifierRead(ID + 1, &II);
Guy Benyei11169dd2012-12-18 14:30:41 +00008491 }
8492
8493 return IdentifiersLoaded[ID];
8494}
8495
Douglas Gregorc8a992f2013-01-21 16:52:34 +00008496IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8497 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei11169dd2012-12-18 14:30:41 +00008498}
8499
8500IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8501 if (LocalID < NUM_PREDEF_IDENT_IDS)
8502 return LocalID;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008503
Richard Smith37a93df2017-02-18 00:32:02 +00008504 if (!M.ModuleOffsetMap.empty())
8505 ReadModuleOffsetMap(M);
8506
Guy Benyei11169dd2012-12-18 14:30:41 +00008507 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8508 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008509 assert(I != M.IdentifierRemap.end()
Guy Benyei11169dd2012-12-18 14:30:41 +00008510 && "Invalid index into identifier index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008511
Guy Benyei11169dd2012-12-18 14:30:41 +00008512 return LocalID + I->second;
8513}
8514
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008515MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008516 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00008517 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008518
8519 if (MacrosLoaded.empty()) {
8520 Error("no macro table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00008521 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008522 }
8523
8524 ID -= NUM_PREDEF_MACRO_IDS;
8525 if (!MacrosLoaded[ID]) {
8526 GlobalMacroMapType::iterator I
8527 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8528 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8529 ModuleFile *M = I->second;
8530 unsigned Index = ID - M->BaseMacroID;
Dmitry Polukhina7afb212020-04-16 09:24:46 -07008531 MacrosLoaded[ID] =
8532 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008533
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008534 if (DeserializationListener)
8535 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8536 MacrosLoaded[ID]);
Guy Benyei11169dd2012-12-18 14:30:41 +00008537 }
8538
8539 return MacrosLoaded[ID];
8540}
8541
8542MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8543 if (LocalID < NUM_PREDEF_MACRO_IDS)
8544 return LocalID;
8545
Richard Smith37a93df2017-02-18 00:32:02 +00008546 if (!M.ModuleOffsetMap.empty())
8547 ReadModuleOffsetMap(M);
8548
Guy Benyei11169dd2012-12-18 14:30:41 +00008549 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8550 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8551 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8552
8553 return LocalID + I->second;
8554}
8555
8556serialization::SubmoduleID
8557ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8558 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8559 return LocalID;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008560
Richard Smith37a93df2017-02-18 00:32:02 +00008561 if (!M.ModuleOffsetMap.empty())
8562 ReadModuleOffsetMap(M);
8563
Guy Benyei11169dd2012-12-18 14:30:41 +00008564 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8565 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008566 assert(I != M.SubmoduleRemap.end()
Guy Benyei11169dd2012-12-18 14:30:41 +00008567 && "Invalid index into submodule index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008568
Guy Benyei11169dd2012-12-18 14:30:41 +00008569 return LocalID + I->second;
8570}
8571
8572Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8573 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8574 assert(GlobalID == 0 && "Unhandled global submodule ID");
Craig Toppera13603a2014-05-22 05:54:18 +00008575 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008576 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008577
Guy Benyei11169dd2012-12-18 14:30:41 +00008578 if (GlobalID > SubmodulesLoaded.size()) {
8579 Error("submodule ID out of range in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00008580 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008581 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008582
Guy Benyei11169dd2012-12-18 14:30:41 +00008583 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8584}
Douglas Gregorc147b0b2013-01-12 01:29:50 +00008585
8586Module *ASTReader::getModule(unsigned ID) {
8587 return getSubmodule(ID);
8588}
8589
Hans Wennborg08c5a7b2018-06-25 13:23:49 +00008590bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8591 ModuleFile *MF = getOwningModuleFile(D);
8592 return MF && MF->PCHHasObjectFile;
8593}
8594
Richard Smithd88a7f12015-09-01 20:35:42 +00008595ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8596 if (ID & 1) {
8597 // It's a module, look it up by submodule ID.
8598 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8599 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8600 } else {
8601 // It's a prefix (preamble, PCH, ...). Look it up by index.
8602 unsigned IndexFromEnd = ID >> 1;
8603 assert(IndexFromEnd && "got reference to unknown module file");
8604 return getModuleManager().pch_modules().end()[-IndexFromEnd];
8605 }
8606}
8607
8608unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8609 if (!F)
8610 return 1;
8611
8612 // For a file representing a module, use the submodule ID of the top-level
8613 // module as the file ID. For any other kind of file, the number of such
8614 // files loaded beforehand will be the same on reload.
8615 // FIXME: Is this true even if we have an explicit module file and a PCH?
8616 if (F->isModule())
8617 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8618
8619 auto PCHModules = getModuleManager().pch_modules();
Fangrui Song75e74e02019-03-31 08:48:19 +00008620 auto I = llvm::find(PCHModules, F);
Richard Smithd88a7f12015-09-01 20:35:42 +00008621 assert(I != PCHModules.end() && "emitting reference to unknown file");
8622 return (I - PCHModules.end()) << 1;
8623}
8624
Reid Klecknerc915cb92020-02-27 18:13:54 -08008625llvm::Optional<ASTSourceDescriptor>
Adrian Prantl15bcf702015-06-30 17:39:43 +00008626ASTReader::getSourceDescriptor(unsigned ID) {
Adrian Prantlf4754ea2020-03-17 12:51:58 -07008627 if (Module *M = getSubmodule(ID))
Reid Klecknerc915cb92020-02-27 18:13:54 -08008628 return ASTSourceDescriptor(*M);
Adrian Prantl15bcf702015-06-30 17:39:43 +00008629
8630 // If there is only a single PCH, return it instead.
Hiroshi Inoue3170de02017-07-01 08:46:43 +00008631 // Chained PCH are not supported.
Saleem Abdulrasool97d25552017-03-02 17:37:11 +00008632 const auto &PCHChain = ModuleMgr.pch_modules();
8633 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
Adrian Prantl15bcf702015-06-30 17:39:43 +00008634 ModuleFile &MF = ModuleMgr.getPrimaryModule();
Adrian Prantl3a2d4942016-01-22 23:30:56 +00008635 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
Adrian Prantl9bc3c4f2016-04-27 17:06:22 +00008636 StringRef FileName = llvm::sys::path::filename(MF.FileName);
Reid Klecknerc915cb92020-02-27 18:13:54 -08008637 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8638 MF.Signature);
Adrian Prantl15bcf702015-06-30 17:39:43 +00008639 }
8640 return None;
8641}
8642
David Blaikie1ac9c982017-04-11 21:13:37 +00008643ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
Richard Smitha4653622017-09-06 20:01:14 +00008644 auto I = DefinitionSource.find(FD);
8645 if (I == DefinitionSource.end())
David Blaikie9ffe5a32017-01-30 05:00:26 +00008646 return EK_ReplyHazy;
David Blaikiee6b7c282017-04-11 20:46:34 +00008647 return I->second ? EK_Never : EK_Always;
David Blaikie9ffe5a32017-01-30 05:00:26 +00008648}
8649
Guy Benyei11169dd2012-12-18 14:30:41 +00008650Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8651 return DecodeSelector(getGlobalSelectorID(M, LocalID));
8652}
8653
8654Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8655 if (ID == 0)
Vedant Kumar48b4f762018-04-14 01:40:48 +00008656 return Selector();
Guy Benyei11169dd2012-12-18 14:30:41 +00008657
8658 if (ID > SelectorsLoaded.size()) {
8659 Error("selector ID out of range in AST file");
Vedant Kumar48b4f762018-04-14 01:40:48 +00008660 return Selector();
Guy Benyei11169dd2012-12-18 14:30:41 +00008661 }
8662
Craig Toppera13603a2014-05-22 05:54:18 +00008663 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008664 // Load this selector from the selector table.
8665 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8666 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8667 ModuleFile &M = *I->second;
8668 ASTSelectorLookupTrait Trait(*this, M);
8669 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8670 SelectorsLoaded[ID - 1] =
8671 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8672 if (DeserializationListener)
8673 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8674 }
8675
8676 return SelectorsLoaded[ID - 1];
8677}
8678
8679Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8680 return DecodeSelector(ID);
8681}
8682
8683uint32_t ASTReader::GetNumExternalSelectors() {
8684 // ID 0 (the null selector) is considered an external selector.
8685 return getTotalNumSelectors() + 1;
8686}
8687
8688serialization::SelectorID
8689ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8690 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8691 return LocalID;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008692
Richard Smith37a93df2017-02-18 00:32:02 +00008693 if (!M.ModuleOffsetMap.empty())
8694 ReadModuleOffsetMap(M);
8695
Guy Benyei11169dd2012-12-18 14:30:41 +00008696 ContinuousRangeMap<uint32_t, int, 2>::iterator I
8697 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008698 assert(I != M.SelectorRemap.end()
Guy Benyei11169dd2012-12-18 14:30:41 +00008699 && "Invalid index into selector index remap");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008700
Guy Benyei11169dd2012-12-18 14:30:41 +00008701 return LocalID + I->second;
8702}
8703
John McCall3ce3d232019-12-13 03:37:23 -05008704DeclarationNameLoc
8705ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8706 DeclarationNameLoc DNLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00008707 switch (Name.getNameKind()) {
8708 case DeclarationName::CXXConstructorName:
8709 case DeclarationName::CXXDestructorName:
8710 case DeclarationName::CXXConversionFunctionName:
John McCall3ce3d232019-12-13 03:37:23 -05008711 DNLoc.NamedType.TInfo = readTypeSourceInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +00008712 break;
8713
8714 case DeclarationName::CXXOperatorName:
8715 DNLoc.CXXOperatorName.BeginOpNameLoc
John McCall3ce3d232019-12-13 03:37:23 -05008716 = readSourceLocation().getRawEncoding();
Guy Benyei11169dd2012-12-18 14:30:41 +00008717 DNLoc.CXXOperatorName.EndOpNameLoc
John McCall3ce3d232019-12-13 03:37:23 -05008718 = readSourceLocation().getRawEncoding();
Guy Benyei11169dd2012-12-18 14:30:41 +00008719 break;
8720
8721 case DeclarationName::CXXLiteralOperatorName:
8722 DNLoc.CXXLiteralOperatorName.OpNameLoc
John McCall3ce3d232019-12-13 03:37:23 -05008723 = readSourceLocation().getRawEncoding();
Guy Benyei11169dd2012-12-18 14:30:41 +00008724 break;
8725
8726 case DeclarationName::Identifier:
8727 case DeclarationName::ObjCZeroArgSelector:
8728 case DeclarationName::ObjCOneArgSelector:
8729 case DeclarationName::ObjCMultiArgSelector:
8730 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00008731 case DeclarationName::CXXDeductionGuideName:
Guy Benyei11169dd2012-12-18 14:30:41 +00008732 break;
8733 }
John McCall3ce3d232019-12-13 03:37:23 -05008734 return DNLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00008735}
8736
John McCall3ce3d232019-12-13 03:37:23 -05008737DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8738 DeclarationNameInfo NameInfo;
8739 NameInfo.setName(readDeclarationName());
8740 NameInfo.setLoc(readSourceLocation());
8741 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8742 return NameInfo;
Guy Benyei11169dd2012-12-18 14:30:41 +00008743}
8744
John McCall3ce3d232019-12-13 03:37:23 -05008745void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8746 Info.QualifierLoc = readNestedNameSpecifierLoc();
8747 unsigned NumTPLists = readInt();
Guy Benyei11169dd2012-12-18 14:30:41 +00008748 Info.NumTemplParamLists = NumTPLists;
8749 if (NumTPLists) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00008750 Info.TemplParamLists =
8751 new (getContext()) TemplateParameterList *[NumTPLists];
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00008752 for (unsigned i = 0; i != NumTPLists; ++i)
John McCall3ce3d232019-12-13 03:37:23 -05008753 Info.TemplParamLists[i] = readTemplateParameterList();
Guy Benyei11169dd2012-12-18 14:30:41 +00008754 }
8755}
8756
Guy Benyei11169dd2012-12-18 14:30:41 +00008757TemplateParameterList *
John McCall3ce3d232019-12-13 03:37:23 -05008758ASTRecordReader::readTemplateParameterList() {
8759 SourceLocation TemplateLoc = readSourceLocation();
8760 SourceLocation LAngleLoc = readSourceLocation();
8761 SourceLocation RAngleLoc = readSourceLocation();
Guy Benyei11169dd2012-12-18 14:30:41 +00008762
John McCall3ce3d232019-12-13 03:37:23 -05008763 unsigned NumParams = readInt();
Guy Benyei11169dd2012-12-18 14:30:41 +00008764 SmallVector<NamedDecl *, 16> Params;
8765 Params.reserve(NumParams);
8766 while (NumParams--)
John McCall3ce3d232019-12-13 03:37:23 -05008767 Params.push_back(readDeclAs<NamedDecl>());
Guy Benyei11169dd2012-12-18 14:30:41 +00008768
John McCall3ce3d232019-12-13 03:37:23 -05008769 bool HasRequiresClause = readBool();
8770 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
Saar Raz0330fba2019-10-15 18:44:06 +00008771
Richard Smithdbafb6c2017-06-29 23:23:46 +00008772 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
Saar Raz0330fba2019-10-15 18:44:06 +00008773 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
Guy Benyei11169dd2012-12-18 14:30:41 +00008774 return TemplateParams;
8775}
8776
John McCall3ce3d232019-12-13 03:37:23 -05008777void ASTRecordReader::readTemplateArgumentList(
8778 SmallVectorImpl<TemplateArgument> &TemplArgs,
8779 bool Canonicalize) {
8780 unsigned NumTemplateArgs = readInt();
Guy Benyei11169dd2012-12-18 14:30:41 +00008781 TemplArgs.reserve(NumTemplateArgs);
8782 while (NumTemplateArgs--)
John McCall3ce3d232019-12-13 03:37:23 -05008783 TemplArgs.push_back(readTemplateArgument(Canonicalize));
Guy Benyei11169dd2012-12-18 14:30:41 +00008784}
8785
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008786/// Read a UnresolvedSet structure.
John McCall3ce3d232019-12-13 03:37:23 -05008787void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8788 unsigned NumDecls = readInt();
Richard Smithdbafb6c2017-06-29 23:23:46 +00008789 Set.reserve(getContext(), NumDecls);
Guy Benyei11169dd2012-12-18 14:30:41 +00008790 while (NumDecls--) {
John McCall3ce3d232019-12-13 03:37:23 -05008791 DeclID ID = readDeclID();
8792 AccessSpecifier AS = (AccessSpecifier) readInt();
Richard Smithdbafb6c2017-06-29 23:23:46 +00008793 Set.addLazyDecl(getContext(), ID, AS);
Guy Benyei11169dd2012-12-18 14:30:41 +00008794 }
8795}
8796
8797CXXBaseSpecifier
John McCall3ce3d232019-12-13 03:37:23 -05008798ASTRecordReader::readCXXBaseSpecifier() {
8799 bool isVirtual = readBool();
8800 bool isBaseOfClass = readBool();
8801 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8802 bool inheritConstructors = readBool();
8803 TypeSourceInfo *TInfo = readTypeSourceInfo();
8804 SourceRange Range = readSourceRange();
8805 SourceLocation EllipsisLoc = readSourceLocation();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008806 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Guy Benyei11169dd2012-12-18 14:30:41 +00008807 EllipsisLoc);
8808 Result.setInheritConstructors(inheritConstructors);
8809 return Result;
8810}
8811
Richard Smithc2bb8182015-03-24 06:36:48 +00008812CXXCtorInitializer **
John McCall3ce3d232019-12-13 03:37:23 -05008813ASTRecordReader::readCXXCtorInitializers() {
Richard Smithdbafb6c2017-06-29 23:23:46 +00008814 ASTContext &Context = getContext();
John McCall3ce3d232019-12-13 03:37:23 -05008815 unsigned NumInitializers = readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00008816 assert(NumInitializers && "wrote ctor initializers but have no inits");
Vedant Kumar48b4f762018-04-14 01:40:48 +00008817 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
Richard Smithc2bb8182015-03-24 06:36:48 +00008818 for (unsigned i = 0; i != NumInitializers; ++i) {
8819 TypeSourceInfo *TInfo = nullptr;
8820 bool IsBaseVirtual = false;
8821 FieldDecl *Member = nullptr;
8822 IndirectFieldDecl *IndirectMember = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008823
John McCall3ce3d232019-12-13 03:37:23 -05008824 CtorInitializerType Type = (CtorInitializerType) readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00008825 switch (Type) {
8826 case CTOR_INITIALIZER_BASE:
John McCall3ce3d232019-12-13 03:37:23 -05008827 TInfo = readTypeSourceInfo();
8828 IsBaseVirtual = readBool();
Richard Smithc2bb8182015-03-24 06:36:48 +00008829 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008830
Richard Smithc2bb8182015-03-24 06:36:48 +00008831 case CTOR_INITIALIZER_DELEGATING:
John McCall3ce3d232019-12-13 03:37:23 -05008832 TInfo = readTypeSourceInfo();
Richard Smithc2bb8182015-03-24 06:36:48 +00008833 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008834
Richard Smithc2bb8182015-03-24 06:36:48 +00008835 case CTOR_INITIALIZER_MEMBER:
John McCall3ce3d232019-12-13 03:37:23 -05008836 Member = readDeclAs<FieldDecl>();
Richard Smithc2bb8182015-03-24 06:36:48 +00008837 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008838
Richard Smithc2bb8182015-03-24 06:36:48 +00008839 case CTOR_INITIALIZER_INDIRECT_MEMBER:
John McCall3ce3d232019-12-13 03:37:23 -05008840 IndirectMember = readDeclAs<IndirectFieldDecl>();
Richard Smithc2bb8182015-03-24 06:36:48 +00008841 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008842 }
Richard Smithc2bb8182015-03-24 06:36:48 +00008843
John McCall3ce3d232019-12-13 03:37:23 -05008844 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8845 Expr *Init = readExpr();
8846 SourceLocation LParenLoc = readSourceLocation();
8847 SourceLocation RParenLoc = readSourceLocation();
Richard Smithc2bb8182015-03-24 06:36:48 +00008848
8849 CXXCtorInitializer *BOMInit;
Richard Smith30e304e2016-12-14 00:03:17 +00008850 if (Type == CTOR_INITIALIZER_BASE)
Richard Smithc2bb8182015-03-24 06:36:48 +00008851 BOMInit = new (Context)
8852 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8853 RParenLoc, MemberOrEllipsisLoc);
Richard Smith30e304e2016-12-14 00:03:17 +00008854 else if (Type == CTOR_INITIALIZER_DELEGATING)
Richard Smithc2bb8182015-03-24 06:36:48 +00008855 BOMInit = new (Context)
8856 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
Richard Smith30e304e2016-12-14 00:03:17 +00008857 else if (Member)
8858 BOMInit = new (Context)
8859 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8860 Init, RParenLoc);
8861 else
8862 BOMInit = new (Context)
8863 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8864 LParenLoc, Init, RParenLoc);
8865
John McCall3ce3d232019-12-13 03:37:23 -05008866 if (/*IsWritten*/readBool()) {
8867 unsigned SourceOrder = readInt();
Richard Smith30e304e2016-12-14 00:03:17 +00008868 BOMInit->setSourceOrder(SourceOrder);
Richard Smithc2bb8182015-03-24 06:36:48 +00008869 }
8870
Richard Smithc2bb8182015-03-24 06:36:48 +00008871 CtorInitializers[i] = BOMInit;
Guy Benyei11169dd2012-12-18 14:30:41 +00008872 }
8873
Richard Smithc2bb8182015-03-24 06:36:48 +00008874 return CtorInitializers;
Guy Benyei11169dd2012-12-18 14:30:41 +00008875}
8876
Guy Benyei11169dd2012-12-18 14:30:41 +00008877NestedNameSpecifierLoc
John McCall3ce3d232019-12-13 03:37:23 -05008878ASTRecordReader::readNestedNameSpecifierLoc() {
Richard Smithdbafb6c2017-06-29 23:23:46 +00008879 ASTContext &Context = getContext();
John McCall3ce3d232019-12-13 03:37:23 -05008880 unsigned N = readInt();
Guy Benyei11169dd2012-12-18 14:30:41 +00008881 NestedNameSpecifierLocBuilder Builder;
8882 for (unsigned I = 0; I != N; ++I) {
John McCalld505e572019-12-13 21:54:44 -05008883 auto Kind = readNestedNameSpecifierKind();
Guy Benyei11169dd2012-12-18 14:30:41 +00008884 switch (Kind) {
8885 case NestedNameSpecifier::Identifier: {
John McCall3ce3d232019-12-13 03:37:23 -05008886 IdentifierInfo *II = readIdentifier();
8887 SourceRange Range = readSourceRange();
Guy Benyei11169dd2012-12-18 14:30:41 +00008888 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8889 break;
8890 }
8891
8892 case NestedNameSpecifier::Namespace: {
John McCall3ce3d232019-12-13 03:37:23 -05008893 NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8894 SourceRange Range = readSourceRange();
Guy Benyei11169dd2012-12-18 14:30:41 +00008895 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8896 break;
8897 }
8898
8899 case NestedNameSpecifier::NamespaceAlias: {
John McCall3ce3d232019-12-13 03:37:23 -05008900 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8901 SourceRange Range = readSourceRange();
Guy Benyei11169dd2012-12-18 14:30:41 +00008902 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8903 break;
8904 }
8905
8906 case NestedNameSpecifier::TypeSpec:
8907 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall3ce3d232019-12-13 03:37:23 -05008908 bool Template = readBool();
8909 TypeSourceInfo *T = readTypeSourceInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +00008910 if (!T)
Vedant Kumar48b4f762018-04-14 01:40:48 +00008911 return NestedNameSpecifierLoc();
John McCall3ce3d232019-12-13 03:37:23 -05008912 SourceLocation ColonColonLoc = readSourceLocation();
Guy Benyei11169dd2012-12-18 14:30:41 +00008913
8914 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00008915 Builder.Extend(Context,
Guy Benyei11169dd2012-12-18 14:30:41 +00008916 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8917 T->getTypeLoc(), ColonColonLoc);
8918 break;
8919 }
8920
8921 case NestedNameSpecifier::Global: {
John McCall3ce3d232019-12-13 03:37:23 -05008922 SourceLocation ColonColonLoc = readSourceLocation();
Guy Benyei11169dd2012-12-18 14:30:41 +00008923 Builder.MakeGlobal(Context, ColonColonLoc);
8924 break;
8925 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008926
8927 case NestedNameSpecifier::Super: {
John McCall3ce3d232019-12-13 03:37:23 -05008928 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8929 SourceRange Range = readSourceRange();
Nikola Smiljanic67860242014-09-26 00:28:20 +00008930 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8931 break;
8932 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008933 }
8934 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008935
Guy Benyei11169dd2012-12-18 14:30:41 +00008936 return Builder.getWithLocInContext(Context);
8937}
8938
8939SourceRange
8940ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8941 unsigned &Idx) {
8942 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8943 SourceLocation end = ReadSourceLocation(F, Record, Idx);
8944 return SourceRange(beg, end);
8945}
8946
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008947static FixedPointSemantics
8948ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
8949 unsigned &Idx) {
8950 unsigned Width = Record[Idx++];
8951 unsigned Scale = Record[Idx++];
8952 uint64_t Tmp = Record[Idx++];
8953 bool IsSigned = Tmp & 0x1;
8954 bool IsSaturated = Tmp & 0x2;
8955 bool HasUnsignedPadding = Tmp & 0x4;
8956 return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
8957 HasUnsignedPadding);
8958}
8959
John McCall3ce3d232019-12-13 03:37:23 -05008960static const llvm::fltSemantics &
8961readAPFloatSemantics(ASTRecordReader &reader) {
8962 return llvm::APFloatBase::EnumToSemantics(
8963 static_cast<llvm::APFloatBase::Semantics>(reader.readInt()));
8964}
8965
8966APValue ASTRecordReader::readAPValue() {
8967 unsigned Kind = readInt();
John McCalld505e572019-12-13 21:54:44 -05008968 switch ((APValue::ValueKind) Kind) {
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008969 case APValue::None:
8970 return APValue();
8971 case APValue::Indeterminate:
8972 return APValue::IndeterminateValue();
8973 case APValue::Int:
John McCall3ce3d232019-12-13 03:37:23 -05008974 return APValue(readAPSInt());
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008975 case APValue::Float: {
John McCall3ce3d232019-12-13 03:37:23 -05008976 const llvm::fltSemantics &FloatSema = readAPFloatSemantics(*this);
8977 return APValue(readAPFloat(FloatSema));
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008978 }
8979 case APValue::FixedPoint: {
8980 FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
John McCall3ce3d232019-12-13 03:37:23 -05008981 return APValue(APFixedPoint(readAPInt(), FPSema));
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008982 }
8983 case APValue::ComplexInt: {
John McCall3ce3d232019-12-13 03:37:23 -05008984 llvm::APSInt First = readAPSInt();
8985 return APValue(std::move(First), readAPSInt());
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008986 }
8987 case APValue::ComplexFloat: {
John McCall3ce3d232019-12-13 03:37:23 -05008988 const llvm::fltSemantics &FloatSema1 = readAPFloatSemantics(*this);
8989 llvm::APFloat First = readAPFloat(FloatSema1);
8990 const llvm::fltSemantics &FloatSema2 = readAPFloatSemantics(*this);
8991 return APValue(std::move(First), readAPFloat(FloatSema2));
Gauthier Harnisch83c7b612019-06-15 10:24:47 +00008992 }
8993 case APValue::LValue:
8994 case APValue::Vector:
8995 case APValue::Array:
8996 case APValue::Struct:
8997 case APValue::Union:
8998 case APValue::MemberPointer:
8999 case APValue::AddrLabelDiff:
9000 // TODO : Handle all these APValue::ValueKind.
9001 return APValue();
9002 }
9003 llvm_unreachable("Invalid APValue::ValueKind");
9004}
9005
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009006/// Read a floating-point value
John McCall3ce3d232019-12-13 03:37:23 -05009007llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
9008 return llvm::APFloat(Sem, readAPInt());
Guy Benyei11169dd2012-12-18 14:30:41 +00009009}
9010
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009011// Read a string
Guy Benyei11169dd2012-12-18 14:30:41 +00009012std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9013 unsigned Len = Record[Idx++];
9014 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9015 Idx += Len;
9016 return Result;
9017}
9018
Richard Smith7ed1bc92014-12-05 22:42:13 +00009019std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9020 unsigned &Idx) {
9021 std::string Filename = ReadString(Record, Idx);
9022 ResolveImportedPath(F, Filename);
9023 return Filename;
9024}
9025
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +00009026std::string ASTReader::ReadPath(StringRef BaseDirectory,
9027 const RecordData &Record, unsigned &Idx) {
9028 std::string Filename = ReadString(Record, Idx);
9029 if (!BaseDirectory.empty())
9030 ResolveImportedPath(Filename, BaseDirectory);
9031 return Filename;
9032}
9033
David L. Jonesc4808b9e2016-12-15 20:53:26 +00009034VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
Guy Benyei11169dd2012-12-18 14:30:41 +00009035 unsigned &Idx) {
9036 unsigned Major = Record[Idx++];
9037 unsigned Minor = Record[Idx++];
9038 unsigned Subminor = Record[Idx++];
9039 if (Minor == 0)
9040 return VersionTuple(Major);
9041 if (Subminor == 0)
9042 return VersionTuple(Major, Minor - 1);
9043 return VersionTuple(Major, Minor - 1, Subminor - 1);
9044}
9045
David L. Jonesc4808b9e2016-12-15 20:53:26 +00009046CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Guy Benyei11169dd2012-12-18 14:30:41 +00009047 const RecordData &Record,
9048 unsigned &Idx) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00009049 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Richard Smithdbafb6c2017-06-29 23:23:46 +00009050 return CXXTemporary::Create(getContext(), Decl);
Guy Benyei11169dd2012-12-18 14:30:41 +00009051}
9052
Richard Smith37a93df2017-02-18 00:32:02 +00009053DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00009054 return Diag(CurrentImportLoc, DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00009055}
9056
Richard Smith37a93df2017-02-18 00:32:02 +00009057DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
Guy Benyei11169dd2012-12-18 14:30:41 +00009058 return Diags.Report(Loc, DiagID);
9059}
9060
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009061/// Retrieve the identifier table associated with the
Guy Benyei11169dd2012-12-18 14:30:41 +00009062/// preprocessor.
9063IdentifierTable &ASTReader::getIdentifierTable() {
9064 return PP.getIdentifierTable();
9065}
9066
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009067/// Record that the given ID maps to the given switch-case
Guy Benyei11169dd2012-12-18 14:30:41 +00009068/// statement.
9069void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00009070 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
Guy Benyei11169dd2012-12-18 14:30:41 +00009071 "Already have a SwitchCase with this ID");
9072 (*CurrSwitchCaseStmts)[ID] = SC;
9073}
9074
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009075/// Retrieve the switch-case statement with the given ID.
Guy Benyei11169dd2012-12-18 14:30:41 +00009076SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00009077 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
Guy Benyei11169dd2012-12-18 14:30:41 +00009078 return (*CurrSwitchCaseStmts)[ID];
9079}
9080
9081void ASTReader::ClearSwitchCaseIDs() {
9082 CurrSwitchCaseStmts->clear();
9083}
9084
9085void ASTReader::ReadComments() {
Richard Smithdbafb6c2017-06-29 23:23:46 +00009086 ASTContext &Context = getContext();
Guy Benyei11169dd2012-12-18 14:30:41 +00009087 std::vector<RawComment *> Comments;
Vedant Kumar48b4f762018-04-14 01:40:48 +00009088 for (SmallVectorImpl<std::pair<BitstreamCursor,
9089 serialization::ModuleFile *>>::iterator
9090 I = CommentsCursors.begin(),
9091 E = CommentsCursors.end();
9092 I != E; ++I) {
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00009093 Comments.clear();
Vedant Kumar48b4f762018-04-14 01:40:48 +00009094 BitstreamCursor &Cursor = I->first;
9095 serialization::ModuleFile &F = *I->second;
Guy Benyei11169dd2012-12-18 14:30:41 +00009096 SavedStreamPosition SavedPosition(Cursor);
9097
9098 RecordData Record;
9099 while (true) {
JF Bastien0e828952019-06-26 19:50:12 +00009100 Expected<llvm::BitstreamEntry> MaybeEntry =
9101 Cursor.advanceSkippingSubblocks(
9102 BitstreamCursor::AF_DontPopBlockAtEnd);
9103 if (!MaybeEntry) {
9104 Error(MaybeEntry.takeError());
9105 return;
9106 }
9107 llvm::BitstreamEntry Entry = MaybeEntry.get();
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00009108
Chris Lattner7fb3bef2013-01-20 00:56:42 +00009109 switch (Entry.Kind) {
9110 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9111 case llvm::BitstreamEntry::Error:
9112 Error("malformed block record in AST file");
9113 return;
9114 case llvm::BitstreamEntry::EndBlock:
9115 goto NextCursor;
9116 case llvm::BitstreamEntry::Record:
9117 // The interesting case.
Guy Benyei11169dd2012-12-18 14:30:41 +00009118 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00009119 }
9120
9121 // Read a record.
9122 Record.clear();
JF Bastien0e828952019-06-26 19:50:12 +00009123 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9124 if (!MaybeComment) {
9125 Error(MaybeComment.takeError());
9126 return;
9127 }
9128 switch ((CommentRecordTypes)MaybeComment.get()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00009129 case COMMENTS_RAW_COMMENT: {
9130 unsigned Idx = 0;
9131 SourceRange SR = ReadSourceRange(F, Record, Idx);
Vedant Kumar48b4f762018-04-14 01:40:48 +00009132 RawComment::CommentKind Kind =
9133 (RawComment::CommentKind) Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00009134 bool IsTrailingComment = Record[Idx++];
9135 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00009136 Comments.push_back(new (Context) RawComment(
David L. Jones13d5a872018-03-02 00:07:45 +00009137 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
Guy Benyei11169dd2012-12-18 14:30:41 +00009138 break;
9139 }
9140 }
9141 }
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00009142 NextCursor:
Jan Korousf31d8df2019-08-13 18:11:44 +00009143 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9144 FileToOffsetToComment;
9145 for (RawComment *C : Comments) {
9146 SourceLocation CommentLoc = C->getBeginLoc();
9147 if (CommentLoc.isValid()) {
9148 std::pair<FileID, unsigned> Loc =
9149 SourceMgr.getDecomposedLoc(CommentLoc);
9150 if (Loc.first.isValid())
9151 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9152 }
9153 }
Guy Benyei11169dd2012-12-18 14:30:41 +00009154 }
Guy Benyei11169dd2012-12-18 14:30:41 +00009155}
9156
Argyrios Kyrtzidisa38cb202017-01-30 06:05:58 +00009157void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9158 bool IncludeSystem, bool Complain,
9159 llvm::function_ref<void(const serialization::InputFile &IF,
9160 bool isSystem)> Visitor) {
9161 unsigned NumUserInputs = MF.NumUserInputFiles;
9162 unsigned NumInputs = MF.InputFilesLoaded.size();
9163 assert(NumUserInputs <= NumInputs);
9164 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9165 for (unsigned I = 0; I < N; ++I) {
9166 bool IsSystem = I >= NumUserInputs;
9167 InputFile IF = getInputFile(MF, I+1, Complain);
9168 Visitor(IF, IsSystem);
9169 }
9170}
9171
Richard Smithf3f84612017-06-29 02:19:42 +00009172void ASTReader::visitTopLevelModuleMaps(
9173 serialization::ModuleFile &MF,
9174 llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9175 unsigned NumInputs = MF.InputFilesLoaded.size();
9176 for (unsigned I = 0; I < NumInputs; ++I) {
9177 InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9178 if (IFI.TopLevelModuleMap)
9179 // FIXME: This unnecessarily re-reads the InputFileInfo.
9180 if (auto *FE = getInputFile(MF, I + 1).getFile())
9181 Visitor(FE);
9182 }
9183}
9184
Richard Smithcd45dbc2014-04-19 03:48:30 +00009185std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9186 // If we know the owning module, use it.
Richard Smith42413142015-05-15 20:05:43 +00009187 if (Module *M = D->getImportedOwningModule())
Richard Smithcd45dbc2014-04-19 03:48:30 +00009188 return M->getFullModuleName();
9189
9190 // Otherwise, use the name of the top-level module the decl is within.
9191 if (ModuleFile *M = getOwningModuleFile(D))
9192 return M->ModuleName;
9193
9194 // Not from a module.
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00009195 return {};
Richard Smithcd45dbc2014-04-19 03:48:30 +00009196}
9197
Guy Benyei11169dd2012-12-18 14:30:41 +00009198void ASTReader::finishPendingActions() {
Richard Smitha62d1982018-08-03 01:00:01 +00009199 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
Richard Smith851072e2014-05-19 20:59:20 +00009200 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
Richard Smith2b9e3e32013-10-18 06:05:18 +00009201 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
Richard Smitha0ce9c42014-07-29 23:23:27 +00009202 !PendingUpdateRecords.empty()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00009203 // If any identifiers with corresponding top-level declarations have
9204 // been loaded, load those declarations now.
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +00009205 using TopLevelDeclsMap =
9206 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
Craig Topper79be4cd2013-07-05 04:33:53 +00009207 TopLevelDeclsMap TopLevelDecls;
9208
Guy Benyei11169dd2012-12-18 14:30:41 +00009209 while (!PendingIdentifierInfos.empty()) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00009210 IdentifierInfo *II = PendingIdentifierInfos.back().first;
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00009211 SmallVector<uint32_t, 4> DeclIDs =
9212 std::move(PendingIdentifierInfos.back().second);
Douglas Gregorcb15f082013-02-19 18:26:28 +00009213 PendingIdentifierInfos.pop_back();
Douglas Gregor6168bd22013-02-18 15:53:43 +00009214
9215 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei11169dd2012-12-18 14:30:41 +00009216 }
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00009217
Richard Smitha62d1982018-08-03 01:00:01 +00009218 // Load each function type that we deferred loading because it was a
9219 // deduced type that might refer to a local type declared within itself.
9220 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9221 auto *FD = PendingFunctionTypes[I].first;
9222 FD->setType(GetType(PendingFunctionTypes[I].second));
9223
9224 // If we gave a function a deduced return type, remember that we need to
9225 // propagate that along the redeclaration chain.
9226 auto *DT = FD->getReturnType()->getContainedDeducedType();
9227 if (DT && DT->isDeduced())
9228 PendingDeducedTypeUpdates.insert(
9229 {FD->getCanonicalDecl(), FD->getReturnType()});
9230 }
9231 PendingFunctionTypes.clear();
9232
Richard Smith851072e2014-05-19 20:59:20 +00009233 // For each decl chain that we wanted to complete while deserializing, mark
9234 // it as "still needs to be completed".
Vedant Kumar48b4f762018-04-14 01:40:48 +00009235 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9236 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9237 }
Richard Smith851072e2014-05-19 20:59:20 +00009238 PendingIncompleteDeclChains.clear();
9239
Guy Benyei11169dd2012-12-18 14:30:41 +00009240 // Load pending declaration chains.
Vedant Kumar48b4f762018-04-14 01:40:48 +00009241 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
Richard Smitha62d1982018-08-03 01:00:01 +00009242 loadPendingDeclChain(PendingDeclChains[I].first,
9243 PendingDeclChains[I].second);
Guy Benyei11169dd2012-12-18 14:30:41 +00009244 PendingDeclChains.clear();
9245
Douglas Gregor6168bd22013-02-18 15:53:43 +00009246 // Make the most recent of the top-level declarations visible.
Vedant Kumar48b4f762018-04-14 01:40:48 +00009247 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9248 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9249 IdentifierInfo *II = TLD->first;
9250 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9251 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregor6168bd22013-02-18 15:53:43 +00009252 }
9253 }
9254
Guy Benyei11169dd2012-12-18 14:30:41 +00009255 // Load any pending macro definitions.
9256 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009257 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9258 SmallVector<PendingMacroInfo, 2> GlobalIDs;
9259 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9260 // Initialize the macro history from chained-PCHs ahead of module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00009261 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00009262 ++IDIdx) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009263 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Manman Ren11f2a472016-08-18 17:42:15 +00009264 if (!Info.M->isModule())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009265 resolvePendingMacro(II, Info);
9266 }
9267 // Handle module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00009268 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009269 ++IDIdx) {
9270 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Manman Ren11f2a472016-08-18 17:42:15 +00009271 if (Info.M->isModule())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00009272 resolvePendingMacro(II, Info);
Guy Benyei11169dd2012-12-18 14:30:41 +00009273 }
9274 }
9275 PendingMacroIDs.clear();
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00009276
9277 // Wire up the DeclContexts for Decls that we delayed setting until
9278 // recursive loading is completed.
9279 while (!PendingDeclContextInfos.empty()) {
9280 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9281 PendingDeclContextInfos.pop_front();
Vedant Kumar48b4f762018-04-14 01:40:48 +00009282 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9283 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00009284 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9285 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00009286
Richard Smithd1c46742014-04-30 02:24:17 +00009287 // Perform any pending declaration updates.
Richard Smithd6db68c2014-08-07 20:58:41 +00009288 while (!PendingUpdateRecords.empty()) {
Richard Smithd1c46742014-04-30 02:24:17 +00009289 auto Update = PendingUpdateRecords.pop_back_val();
9290 ReadingKindTracker ReadingKind(Read_Decl, *this);
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00009291 loadDeclUpdateRecords(Update);
Richard Smithd1c46742014-04-30 02:24:17 +00009292 }
Guy Benyei11169dd2012-12-18 14:30:41 +00009293 }
Richard Smith8a639892015-01-24 01:07:20 +00009294
9295 // At this point, all update records for loaded decls are in place, so any
9296 // fake class definitions should have become real.
9297 assert(PendingFakeDefinitionData.empty() &&
9298 "faked up a class definition but never saw the real one");
9299
Guy Benyei11169dd2012-12-18 14:30:41 +00009300 // If we deserialized any C++ or Objective-C class definitions, any
9301 // Objective-C protocol definitions, or any redeclarable templates, make sure
David L. Jonesc4808b9e2016-12-15 20:53:26 +00009302 // that all redeclarations point to the definitions. Note that this can only
Guy Benyei11169dd2012-12-18 14:30:41 +00009303 // happen now, after the redeclaration chains have been fully wired.
Vedant Kumar48b4f762018-04-14 01:40:48 +00009304 for (Decl *D : PendingDefinitions) {
9305 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9306 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00009307 // Make sure that the TagType points at the definition.
9308 const_cast<TagType*>(TagT)->decl = TD;
9309 }
Richard Smith8ce51082015-03-11 01:44:51 +00009310
Vedant Kumar48b4f762018-04-14 01:40:48 +00009311 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00009312 for (auto *R = getMostRecentExistingDecl(RD); R;
9313 R = R->getPreviousDecl()) {
9314 assert((R == D) ==
9315 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
Richard Smith2c381642014-08-27 23:11:59 +00009316 "declaration thinks it's the definition but it isn't");
Aaron Ballman86c93902014-03-06 23:45:36 +00009317 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
Richard Smith2c381642014-08-27 23:11:59 +00009318 }
Guy Benyei11169dd2012-12-18 14:30:41 +00009319 }
9320
9321 continue;
9322 }
Richard Smith8ce51082015-03-11 01:44:51 +00009323
Vedant Kumar48b4f762018-04-14 01:40:48 +00009324 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00009325 // Make sure that the ObjCInterfaceType points at the definition.
9326 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9327 ->Decl = ID;
Richard Smith8ce51082015-03-11 01:44:51 +00009328
9329 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9330 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9331
Guy Benyei11169dd2012-12-18 14:30:41 +00009332 continue;
9333 }
Richard Smith8ce51082015-03-11 01:44:51 +00009334
Vedant Kumar48b4f762018-04-14 01:40:48 +00009335 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00009336 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9337 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9338
Guy Benyei11169dd2012-12-18 14:30:41 +00009339 continue;
9340 }
Richard Smith8ce51082015-03-11 01:44:51 +00009341
Vedant Kumar48b4f762018-04-14 01:40:48 +00009342 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
Richard Smith8ce51082015-03-11 01:44:51 +00009343 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9344 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
Guy Benyei11169dd2012-12-18 14:30:41 +00009345 }
9346 PendingDefinitions.clear();
9347
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009348 // Load the bodies of any functions or methods we've encountered. We do
9349 // this now (delayed) so that we can be sure that the declaration chains
9350 // have been fully wired up (hasBody relies on this).
9351 // FIXME: We shouldn't require complete redeclaration chains here.
Vedant Kumar48b4f762018-04-14 01:40:48 +00009352 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9353 PBEnd = PendingBodies.end();
9354 PB != PBEnd; ++PB) {
9355 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
Richard Smith0b70de12018-06-28 01:07:28 +00009356 // For a function defined inline within a class template, force the
9357 // canonical definition to be the one inside the canonical definition of
9358 // the template. This ensures that we instantiate from a correct view
9359 // of the template.
9360 //
9361 // Sadly we can't do this more generally: we can't be sure that all
9362 // copies of an arbitrary class definition will have the same members
9363 // defined (eg, some member functions may not be instantiated, and some
9364 // special members may or may not have been implicitly defined).
9365 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9366 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9367 continue;
9368
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009369 // FIXME: Check for =delete/=default?
9370 // FIXME: Complain about ODR violations here?
9371 const FunctionDecl *Defn = nullptr;
9372 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
Vedant Kumar48b4f762018-04-14 01:40:48 +00009373 FD->setLazyBody(PB->second);
Richard Trieue6caa262017-12-23 00:41:01 +00009374 } else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00009375 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
Richard Trieue6caa262017-12-23 00:41:01 +00009376 mergeDefinitionVisibility(NonConstDefn, FD);
9377
9378 if (!FD->isLateTemplateParsed() &&
9379 !NonConstDefn->isLateTemplateParsed() &&
9380 FD->getODRHash() != NonConstDefn->getODRHash()) {
Richard Trieu27c1b1a2018-07-10 01:40:50 +00009381 if (!isa<CXXMethodDecl>(FD)) {
9382 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9383 } else if (FD->getLexicalParent()->isFileContext() &&
9384 NonConstDefn->getLexicalParent()->isFileContext()) {
9385 // Only diagnose out-of-line method definitions. If they are
9386 // in class definitions, then an error will be generated when
9387 // processing the class bodies.
9388 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9389 }
Richard Trieue6caa262017-12-23 00:41:01 +00009390 }
9391 }
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009392 continue;
9393 }
9394
Vedant Kumar48b4f762018-04-14 01:40:48 +00009395 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009396 if (!getContext().getLangOpts().Modules || !MD->hasBody())
Vedant Kumar48b4f762018-04-14 01:40:48 +00009397 MD->setLazyBody(PB->second);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00009398 }
9399 PendingBodies.clear();
9400
Richard Smith42413142015-05-15 20:05:43 +00009401 // Do some cleanup.
9402 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9403 getContext().deduplicateMergedDefinitonsFor(ND);
9404 PendingMergedDefinitionsToDeduplicate.clear();
Richard Smitha0ce9c42014-07-29 23:23:27 +00009405}
9406
9407void ASTReader::diagnoseOdrViolations() {
Richard Trieue6caa262017-12-23 00:41:01 +00009408 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
Richard Trieuab4d7302018-07-25 22:52:05 +00009409 PendingFunctionOdrMergeFailures.empty() &&
9410 PendingEnumOdrMergeFailures.empty())
Richard Smithbb853c72014-08-13 01:23:33 +00009411 return;
9412
Richard Smitha0ce9c42014-07-29 23:23:27 +00009413 // Trigger the import of the full definition of each class that had any
9414 // odr-merging problems, so we can produce better diagnostics for them.
Richard Smithbb853c72014-08-13 01:23:33 +00009415 // These updates may in turn find and diagnose some ODR failures, so take
9416 // ownership of the set first.
9417 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9418 PendingOdrMergeFailures.clear();
9419 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00009420 Merge.first->buildLookup();
9421 Merge.first->decls_begin();
9422 Merge.first->bases_begin();
9423 Merge.first->vbases_begin();
Richard Trieue13eabe2017-09-30 02:19:17 +00009424 for (auto &RecordPair : Merge.second) {
9425 auto *RD = RecordPair.first;
Richard Smitha0ce9c42014-07-29 23:23:27 +00009426 RD->decls_begin();
9427 RD->bases_begin();
9428 RD->vbases_begin();
9429 }
9430 }
9431
Richard Trieue6caa262017-12-23 00:41:01 +00009432 // Trigger the import of functions.
9433 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9434 PendingFunctionOdrMergeFailures.clear();
9435 for (auto &Merge : FunctionOdrMergeFailures) {
9436 Merge.first->buildLookup();
9437 Merge.first->decls_begin();
9438 Merge.first->getBody();
9439 for (auto &FD : Merge.second) {
9440 FD->buildLookup();
9441 FD->decls_begin();
9442 FD->getBody();
9443 }
9444 }
9445
Richard Trieuab4d7302018-07-25 22:52:05 +00009446 // Trigger the import of enums.
9447 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9448 PendingEnumOdrMergeFailures.clear();
9449 for (auto &Merge : EnumOdrMergeFailures) {
9450 Merge.first->decls_begin();
9451 for (auto &Enum : Merge.second) {
9452 Enum->decls_begin();
9453 }
9454 }
9455
Richard Smitha0ce9c42014-07-29 23:23:27 +00009456 // For each declaration from a merged context, check that the canonical
9457 // definition of that context also contains a declaration of the same
9458 // entity.
9459 //
9460 // Caution: this loop does things that might invalidate iterators into
9461 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9462 while (!PendingOdrMergeChecks.empty()) {
9463 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9464
9465 // FIXME: Skip over implicit declarations for now. This matters for things
9466 // like implicitly-declared special member functions. This isn't entirely
9467 // correct; we can end up with multiple unmerged declarations of the same
9468 // implicit entity.
9469 if (D->isImplicit())
9470 continue;
9471
9472 DeclContext *CanonDef = D->getDeclContext();
Richard Smitha0ce9c42014-07-29 23:23:27 +00009473
9474 bool Found = false;
9475 const Decl *DCanon = D->getCanonicalDecl();
9476
Richard Smith01bdb7a2014-08-28 05:44:07 +00009477 for (auto RI : D->redecls()) {
9478 if (RI->getLexicalDeclContext() == CanonDef) {
9479 Found = true;
9480 break;
9481 }
9482 }
9483 if (Found)
9484 continue;
9485
Richard Smith0f4e2c42015-08-06 04:23:48 +00009486 // Quick check failed, time to do the slow thing. Note, we can't just
9487 // look up the name of D in CanonDef here, because the member that is
9488 // in CanonDef might not be found by name lookup (it might have been
9489 // replaced by a more recent declaration in the lookup table), and we
9490 // can't necessarily find it in the redeclaration chain because it might
9491 // be merely mergeable, not redeclarable.
Richard Smitha0ce9c42014-07-29 23:23:27 +00009492 llvm::SmallVector<const NamedDecl*, 4> Candidates;
Richard Smith0f4e2c42015-08-06 04:23:48 +00009493 for (auto *CanonMember : CanonDef->decls()) {
9494 if (CanonMember->getCanonicalDecl() == DCanon) {
9495 // This can happen if the declaration is merely mergeable and not
9496 // actually redeclarable (we looked for redeclarations earlier).
9497 //
9498 // FIXME: We should be able to detect this more efficiently, without
9499 // pulling in all of the members of CanonDef.
9500 Found = true;
9501 break;
Richard Smitha0ce9c42014-07-29 23:23:27 +00009502 }
Richard Smith0f4e2c42015-08-06 04:23:48 +00009503 if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9504 if (ND->getDeclName() == D->getDeclName())
9505 Candidates.push_back(ND);
Richard Smitha0ce9c42014-07-29 23:23:27 +00009506 }
9507
9508 if (!Found) {
Richard Smithd08aeb62014-08-28 01:33:39 +00009509 // The AST doesn't like TagDecls becoming invalid after they've been
9510 // completed. We only really need to mark FieldDecls as invalid here.
9511 if (!isa<TagDecl>(D))
9512 D->setInvalidDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00009513
Richard Smith4ab3dbd2015-02-13 22:43:51 +00009514 // Ensure we don't accidentally recursively enter deserialization while
9515 // we're producing our diagnostic.
9516 Deserializing RecursionGuard(this);
Richard Smitha0ce9c42014-07-29 23:23:27 +00009517
9518 std::string CanonDefModule =
9519 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9520 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9521 << D << getOwningModuleNameForDiagnostic(D)
9522 << CanonDef << CanonDefModule.empty() << CanonDefModule;
9523
9524 if (Candidates.empty())
9525 Diag(cast<Decl>(CanonDef)->getLocation(),
9526 diag::note_module_odr_violation_no_possible_decls) << D;
9527 else {
Vedant Kumar48b4f762018-04-14 01:40:48 +00009528 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9529 Diag(Candidates[I]->getLocation(),
Richard Smitha0ce9c42014-07-29 23:23:27 +00009530 diag::note_module_odr_violation_possible_decl)
Vedant Kumar48b4f762018-04-14 01:40:48 +00009531 << Candidates[I];
Richard Smitha0ce9c42014-07-29 23:23:27 +00009532 }
9533
9534 DiagnosedOdrMergeFailures.insert(CanonDef);
9535 }
9536 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00009537
Richard Trieuab4d7302018-07-25 22:52:05 +00009538 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9539 EnumOdrMergeFailures.empty())
Richard Smith4ab3dbd2015-02-13 22:43:51 +00009540 return;
9541
9542 // Ensure we don't accidentally recursively enter deserialization while
9543 // we're producing our diagnostics.
9544 Deserializing RecursionGuard(this);
9545
Richard Trieue6caa262017-12-23 00:41:01 +00009546 // Common code for hashing helpers.
9547 ODRHash Hash;
9548 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9549 Hash.clear();
9550 Hash.AddQualType(Ty);
9551 return Hash.CalculateHash();
9552 };
9553
9554 auto ComputeODRHash = [&Hash](const Stmt *S) {
9555 assert(S);
9556 Hash.clear();
9557 Hash.AddStmt(S);
9558 return Hash.CalculateHash();
9559 };
9560
9561 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9562 assert(D);
9563 Hash.clear();
9564 Hash.AddSubDecl(D);
9565 return Hash.CalculateHash();
9566 };
9567
Richard Trieu7282d322018-04-25 00:31:15 +00009568 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9569 Hash.clear();
9570 Hash.AddTemplateArgument(TA);
9571 return Hash.CalculateHash();
9572 };
9573
Richard Trieu9359e8f2018-05-30 01:12:26 +00009574 auto ComputeTemplateParameterListODRHash =
9575 [&Hash](const TemplateParameterList *TPL) {
9576 assert(TPL);
9577 Hash.clear();
9578 Hash.AddTemplateParameterList(TPL);
9579 return Hash.CalculateHash();
9580 };
9581
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -08009582 // Used with err_module_odr_violation_mismatch_decl and
9583 // note_module_odr_violation_mismatch_decl
Weverythingd5f9c4a2020-06-19 18:35:36 -07009584 // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -08009585 enum ODRMismatchDecl {
9586 EndOfClass,
9587 PublicSpecifer,
9588 PrivateSpecifer,
9589 ProtectedSpecifer,
9590 StaticAssert,
9591 Field,
9592 CXXMethod,
9593 TypeAlias,
9594 TypeDef,
9595 Var,
9596 Friend,
9597 FunctionTemplate,
9598 Other
9599 };
9600
9601 // Used with err_module_odr_violation_mismatch_decl_diff and
9602 // note_module_odr_violation_mismatch_decl_diff
9603 enum ODRMismatchDeclDifference {
9604 StaticAssertCondition,
9605 StaticAssertMessage,
9606 StaticAssertOnlyMessage,
9607 FieldName,
9608 FieldTypeName,
9609 FieldSingleBitField,
9610 FieldDifferentWidthBitField,
9611 FieldSingleMutable,
9612 FieldSingleInitializer,
9613 FieldDifferentInitializers,
9614 MethodName,
9615 MethodDeleted,
9616 MethodDefaulted,
9617 MethodVirtual,
9618 MethodStatic,
9619 MethodVolatile,
9620 MethodConst,
9621 MethodInline,
9622 MethodNumberParameters,
9623 MethodParameterType,
9624 MethodParameterName,
9625 MethodParameterSingleDefaultArgument,
9626 MethodParameterDifferentDefaultArgument,
9627 MethodNoTemplateArguments,
9628 MethodDifferentNumberTemplateArguments,
9629 MethodDifferentTemplateArgument,
9630 MethodSingleBody,
9631 MethodDifferentBody,
9632 TypedefName,
9633 TypedefType,
9634 VarName,
9635 VarType,
9636 VarSingleInitializer,
9637 VarDifferentInitializer,
9638 VarConstexpr,
9639 FriendTypeFunction,
9640 FriendType,
9641 FriendFunction,
9642 FunctionTemplateDifferentNumberParameters,
9643 FunctionTemplateParameterDifferentKind,
9644 FunctionTemplateParameterName,
9645 FunctionTemplateParameterSingleDefaultArgument,
9646 FunctionTemplateParameterDifferentDefaultArgument,
9647 FunctionTemplateParameterDifferentType,
9648 FunctionTemplatePackParameter,
9649 };
9650
9651 // These lambdas have the common portions of the ODR diagnostics. This
9652 // has the same return as Diag(), so addition parameters can be passed
9653 // in with operator<<
9654 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9655 SourceLocation Loc, SourceRange Range,
9656 ODRMismatchDeclDifference DiffType) {
9657 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9658 << FirstRecord << FirstModule.empty() << FirstModule << Range
9659 << DiffType;
9660 };
9661 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9662 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9663 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9664 << SecondModule << Range << DiffType;
9665 };
9666
9667 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9668 &ComputeQualTypeODRHash, &ComputeODRHash](
9669 NamedDecl *FirstRecord, StringRef FirstModule,
9670 StringRef SecondModule, FieldDecl *FirstField,
9671 FieldDecl *SecondField) {
9672 IdentifierInfo *FirstII = FirstField->getIdentifier();
9673 IdentifierInfo *SecondII = SecondField->getIdentifier();
9674 if (FirstII->getName() != SecondII->getName()) {
9675 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9676 FirstField->getSourceRange(), FieldName)
9677 << FirstII;
9678 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9679 SecondField->getSourceRange(), FieldName)
9680 << SecondII;
9681
9682 return true;
9683 }
9684
9685 assert(getContext().hasSameType(FirstField->getType(),
9686 SecondField->getType()));
9687
9688 QualType FirstType = FirstField->getType();
9689 QualType SecondType = SecondField->getType();
9690 if (ComputeQualTypeODRHash(FirstType) !=
9691 ComputeQualTypeODRHash(SecondType)) {
9692 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9693 FirstField->getSourceRange(), FieldTypeName)
9694 << FirstII << FirstType;
9695 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9696 SecondField->getSourceRange(), FieldTypeName)
9697 << SecondII << SecondType;
9698
9699 return true;
9700 }
9701
9702 const bool IsFirstBitField = FirstField->isBitField();
9703 const bool IsSecondBitField = SecondField->isBitField();
9704 if (IsFirstBitField != IsSecondBitField) {
9705 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9706 FirstField->getSourceRange(), FieldSingleBitField)
9707 << FirstII << IsFirstBitField;
9708 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9709 SecondField->getSourceRange(), FieldSingleBitField)
9710 << SecondII << IsSecondBitField;
9711 return true;
9712 }
9713
9714 if (IsFirstBitField && IsSecondBitField) {
9715 unsigned FirstBitWidthHash =
9716 ComputeODRHash(FirstField->getBitWidth());
9717 unsigned SecondBitWidthHash =
9718 ComputeODRHash(SecondField->getBitWidth());
9719 if (FirstBitWidthHash != SecondBitWidthHash) {
9720 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9721 FirstField->getSourceRange(),
9722 FieldDifferentWidthBitField)
9723 << FirstII << FirstField->getBitWidth()->getSourceRange();
9724 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9725 SecondField->getSourceRange(),
9726 FieldDifferentWidthBitField)
9727 << SecondII << SecondField->getBitWidth()->getSourceRange();
9728 return true;
9729 }
9730 }
9731
9732 if (!PP.getLangOpts().CPlusPlus)
9733 return false;
9734
9735 const bool IsFirstMutable = FirstField->isMutable();
9736 const bool IsSecondMutable = SecondField->isMutable();
9737 if (IsFirstMutable != IsSecondMutable) {
9738 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9739 FirstField->getSourceRange(), FieldSingleMutable)
9740 << FirstII << IsFirstMutable;
9741 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9742 SecondField->getSourceRange(), FieldSingleMutable)
9743 << SecondII << IsSecondMutable;
9744 return true;
9745 }
9746
9747 const Expr *FirstInitializer = FirstField->getInClassInitializer();
9748 const Expr *SecondInitializer = SecondField->getInClassInitializer();
9749 if ((!FirstInitializer && SecondInitializer) ||
9750 (FirstInitializer && !SecondInitializer)) {
9751 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9752 FirstField->getSourceRange(), FieldSingleInitializer)
9753 << FirstII << (FirstInitializer != nullptr);
9754 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9755 SecondField->getSourceRange(), FieldSingleInitializer)
9756 << SecondII << (SecondInitializer != nullptr);
9757 return true;
9758 }
9759
9760 if (FirstInitializer && SecondInitializer) {
9761 unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9762 unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9763 if (FirstInitHash != SecondInitHash) {
9764 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9765 FirstField->getSourceRange(),
9766 FieldDifferentInitializers)
9767 << FirstII << FirstInitializer->getSourceRange();
9768 ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9769 SecondField->getSourceRange(),
9770 FieldDifferentInitializers)
9771 << SecondII << SecondInitializer->getSourceRange();
9772 return true;
9773 }
9774 }
9775
9776 return false;
9777 };
9778
9779 auto ODRDiagTypeDefOrAlias =
9780 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9781 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9782 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9783 bool IsTypeAlias) {
9784 auto FirstName = FirstTD->getDeclName();
9785 auto SecondName = SecondTD->getDeclName();
9786 if (FirstName != SecondName) {
9787 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9788 FirstTD->getSourceRange(), TypedefName)
9789 << IsTypeAlias << FirstName;
9790 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9791 SecondTD->getSourceRange(), TypedefName)
9792 << IsTypeAlias << SecondName;
9793 return true;
9794 }
9795
9796 QualType FirstType = FirstTD->getUnderlyingType();
9797 QualType SecondType = SecondTD->getUnderlyingType();
9798 if (ComputeQualTypeODRHash(FirstType) !=
9799 ComputeQualTypeODRHash(SecondType)) {
9800 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9801 FirstTD->getSourceRange(), TypedefType)
9802 << IsTypeAlias << FirstName << FirstType;
9803 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9804 SecondTD->getSourceRange(), TypedefType)
9805 << IsTypeAlias << SecondName << SecondType;
9806 return true;
9807 }
9808
9809 return false;
9810 };
9811
9812 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9813 &ComputeQualTypeODRHash, &ComputeODRHash,
9814 this](NamedDecl *FirstRecord, StringRef FirstModule,
9815 StringRef SecondModule, VarDecl *FirstVD,
9816 VarDecl *SecondVD) {
9817 auto FirstName = FirstVD->getDeclName();
9818 auto SecondName = SecondVD->getDeclName();
9819 if (FirstName != SecondName) {
9820 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9821 FirstVD->getSourceRange(), VarName)
9822 << FirstName;
9823 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9824 SecondVD->getSourceRange(), VarName)
9825 << SecondName;
9826 return true;
9827 }
9828
9829 QualType FirstType = FirstVD->getType();
9830 QualType SecondType = SecondVD->getType();
9831 if (ComputeQualTypeODRHash(FirstType) !=
9832 ComputeQualTypeODRHash(SecondType)) {
9833 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9834 FirstVD->getSourceRange(), VarType)
9835 << FirstName << FirstType;
9836 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9837 SecondVD->getSourceRange(), VarType)
9838 << SecondName << SecondType;
9839 return true;
9840 }
9841
9842 if (!PP.getLangOpts().CPlusPlus)
9843 return false;
9844
9845 const Expr *FirstInit = FirstVD->getInit();
9846 const Expr *SecondInit = SecondVD->getInit();
9847 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9848 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9849 FirstVD->getSourceRange(), VarSingleInitializer)
9850 << FirstName << (FirstInit == nullptr)
9851 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9852 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9853 SecondVD->getSourceRange(), VarSingleInitializer)
9854 << SecondName << (SecondInit == nullptr)
9855 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9856 return true;
9857 }
9858
9859 if (FirstInit && SecondInit &&
9860 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9861 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9862 FirstVD->getSourceRange(), VarDifferentInitializer)
9863 << FirstName << FirstInit->getSourceRange();
9864 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9865 SecondVD->getSourceRange(), VarDifferentInitializer)
9866 << SecondName << SecondInit->getSourceRange();
9867 return true;
9868 }
9869
9870 const bool FirstIsConstexpr = FirstVD->isConstexpr();
9871 const bool SecondIsConstexpr = SecondVD->isConstexpr();
9872 if (FirstIsConstexpr != SecondIsConstexpr) {
9873 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9874 FirstVD->getSourceRange(), VarConstexpr)
9875 << FirstName << FirstIsConstexpr;
9876 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9877 SecondVD->getSourceRange(), VarConstexpr)
9878 << SecondName << SecondIsConstexpr;
9879 return true;
9880 }
9881 return false;
9882 };
9883
9884 auto DifferenceSelector = [](Decl *D) {
9885 assert(D && "valid Decl required");
9886 switch (D->getKind()) {
9887 default:
9888 return Other;
9889 case Decl::AccessSpec:
9890 switch (D->getAccess()) {
9891 case AS_public:
9892 return PublicSpecifer;
9893 case AS_private:
9894 return PrivateSpecifer;
9895 case AS_protected:
9896 return ProtectedSpecifer;
9897 case AS_none:
9898 break;
9899 }
9900 llvm_unreachable("Invalid access specifier");
9901 case Decl::StaticAssert:
9902 return StaticAssert;
9903 case Decl::Field:
9904 return Field;
9905 case Decl::CXXMethod:
9906 case Decl::CXXConstructor:
9907 case Decl::CXXDestructor:
9908 return CXXMethod;
9909 case Decl::TypeAlias:
9910 return TypeAlias;
9911 case Decl::Typedef:
9912 return TypeDef;
9913 case Decl::Var:
9914 return Var;
9915 case Decl::Friend:
9916 return Friend;
9917 case Decl::FunctionTemplate:
9918 return FunctionTemplate;
9919 }
9920 };
9921
9922 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9923 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9924 RecordDecl *Record,
9925 const DeclContext *DC) {
9926 for (auto *D : Record->decls()) {
Weverythingd5f9c4a2020-06-19 18:35:36 -07009927 if (!ODRHash::isDeclToBeProcessed(D, DC))
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -08009928 continue;
9929 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9930 }
9931 };
9932
9933 struct DiffResult {
9934 Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9935 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9936 };
9937
9938 // If there is a diagnoseable difference, FirstDiffType and
9939 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9940 // filled in if not EndOfClass.
9941 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9942 DeclHashes &SecondHashes) {
9943 DiffResult DR;
9944 auto FirstIt = FirstHashes.begin();
9945 auto SecondIt = SecondHashes.begin();
9946 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9947 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9948 FirstIt->second == SecondIt->second) {
9949 ++FirstIt;
9950 ++SecondIt;
9951 continue;
9952 }
9953
9954 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9955 DR.SecondDecl =
9956 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9957
9958 DR.FirstDiffType =
9959 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9960 DR.SecondDiffType =
9961 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9962 return DR;
9963 }
9964 return DR;
9965 };
9966
9967 // Use this to diagnose that an unexpected Decl was encountered
9968 // or no difference was detected. This causes a generic error
9969 // message to be emitted.
9970 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9971 StringRef FirstModule,
9972 NamedDecl *SecondRecord,
9973 StringRef SecondModule) {
9974 Diag(FirstRecord->getLocation(),
9975 diag::err_module_odr_violation_different_definitions)
9976 << FirstRecord << FirstModule.empty() << FirstModule;
9977
9978 if (DR.FirstDecl) {
9979 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9980 << FirstRecord << DR.FirstDecl->getSourceRange();
9981 }
9982
9983 Diag(SecondRecord->getLocation(),
9984 diag::note_module_odr_violation_different_definitions)
9985 << SecondModule;
9986
9987 if (DR.SecondDecl) {
9988 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9989 << DR.SecondDecl->getSourceRange();
9990 }
9991 };
9992
9993 auto DiagnoseODRMismatch =
9994 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9995 NamedDecl *SecondRecord, StringRef SecondModule) {
9996 SourceLocation FirstLoc;
9997 SourceRange FirstRange;
9998 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9999 if (DR.FirstDiffType == EndOfClass && FirstTag) {
10000 FirstLoc = FirstTag->getBraceRange().getEnd();
10001 } else {
10002 FirstLoc = DR.FirstDecl->getLocation();
10003 FirstRange = DR.FirstDecl->getSourceRange();
10004 }
10005 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10006 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10007 << DR.FirstDiffType;
10008
10009 SourceLocation SecondLoc;
10010 SourceRange SecondRange;
10011 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
10012 if (DR.SecondDiffType == EndOfClass && SecondTag) {
10013 SecondLoc = SecondTag->getBraceRange().getEnd();
10014 } else {
10015 SecondLoc = DR.SecondDecl->getLocation();
10016 SecondRange = DR.SecondDecl->getSourceRange();
10017 }
10018 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10019 << SecondModule << SecondRange << DR.SecondDiffType;
10020 };
10021
Richard Smithcd45dbc2014-04-19 03:48:30 +000010022 // Issue any pending ODR-failure diagnostics.
Richard Smithbb853c72014-08-13 01:23:33 +000010023 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +000010024 // If we've already pointed out a specific problem with this class, don't
10025 // bother issuing a general "something's different" diagnostic.
David Blaikie82e95a32014-11-19 07:49:47 +000010026 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
Richard Smithcd45dbc2014-04-19 03:48:30 +000010027 continue;
10028
10029 bool Diagnosed = false;
Richard Trieue7f7ed22017-02-22 01:11:25 +000010030 CXXRecordDecl *FirstRecord = Merge.first;
10031 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
Richard Trieue13eabe2017-09-30 02:19:17 +000010032 for (auto &RecordPair : Merge.second) {
10033 CXXRecordDecl *SecondRecord = RecordPair.first;
Richard Smithcd45dbc2014-04-19 03:48:30 +000010034 // Multiple different declarations got merged together; tell the user
10035 // where they came from.
Richard Trieue7f7ed22017-02-22 01:11:25 +000010036 if (FirstRecord == SecondRecord)
10037 continue;
10038
10039 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
Richard Trieue13eabe2017-09-30 02:19:17 +000010040
10041 auto *FirstDD = FirstRecord->DefinitionData;
10042 auto *SecondDD = RecordPair.second;
10043
10044 assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10045
10046 // Diagnostics from DefinitionData are emitted here.
10047 if (FirstDD != SecondDD) {
10048 enum ODRDefinitionDataDifference {
10049 NumBases,
10050 NumVBases,
10051 BaseType,
10052 BaseVirtual,
10053 BaseAccess,
10054 };
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010055 auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10056 this](SourceLocation Loc, SourceRange Range,
10057 ODRDefinitionDataDifference DiffType) {
Richard Trieue13eabe2017-09-30 02:19:17 +000010058 return Diag(Loc, diag::err_module_odr_violation_definition_data)
10059 << FirstRecord << FirstModule.empty() << FirstModule << Range
10060 << DiffType;
10061 };
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010062 auto ODRDiagBaseNote = [&SecondModule,
10063 this](SourceLocation Loc, SourceRange Range,
10064 ODRDefinitionDataDifference DiffType) {
Richard Trieue13eabe2017-09-30 02:19:17 +000010065 return Diag(Loc, diag::note_module_odr_violation_definition_data)
10066 << SecondModule << Range << DiffType;
10067 };
10068
Richard Trieue13eabe2017-09-30 02:19:17 +000010069 unsigned FirstNumBases = FirstDD->NumBases;
10070 unsigned FirstNumVBases = FirstDD->NumVBases;
10071 unsigned SecondNumBases = SecondDD->NumBases;
10072 unsigned SecondNumVBases = SecondDD->NumVBases;
10073
10074 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10075 unsigned NumBases = DD->NumBases;
10076 if (NumBases == 0) return SourceRange();
10077 auto bases = DD->bases();
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010078 return SourceRange(bases[0].getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +000010079 bases[NumBases - 1].getEndLoc());
Richard Trieue13eabe2017-09-30 02:19:17 +000010080 };
10081
10082 if (FirstNumBases != SecondNumBases) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010083 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10084 NumBases)
Richard Trieue13eabe2017-09-30 02:19:17 +000010085 << FirstNumBases;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010086 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10087 NumBases)
Richard Trieue13eabe2017-09-30 02:19:17 +000010088 << SecondNumBases;
10089 Diagnosed = true;
10090 break;
10091 }
10092
10093 if (FirstNumVBases != SecondNumVBases) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010094 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10095 NumVBases)
Richard Trieue13eabe2017-09-30 02:19:17 +000010096 << FirstNumVBases;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010097 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10098 NumVBases)
Richard Trieue13eabe2017-09-30 02:19:17 +000010099 << SecondNumVBases;
10100 Diagnosed = true;
10101 break;
10102 }
10103
10104 auto FirstBases = FirstDD->bases();
10105 auto SecondBases = SecondDD->bases();
10106 unsigned i = 0;
10107 for (i = 0; i < FirstNumBases; ++i) {
10108 auto FirstBase = FirstBases[i];
10109 auto SecondBase = SecondBases[i];
10110 if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10111 ComputeQualTypeODRHash(SecondBase.getType())) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010112 ODRDiagBaseError(FirstRecord->getLocation(),
10113 FirstBase.getSourceRange(), BaseType)
Richard Trieue13eabe2017-09-30 02:19:17 +000010114 << (i + 1) << FirstBase.getType();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010115 ODRDiagBaseNote(SecondRecord->getLocation(),
10116 SecondBase.getSourceRange(), BaseType)
Richard Trieue13eabe2017-09-30 02:19:17 +000010117 << (i + 1) << SecondBase.getType();
10118 break;
10119 }
10120
10121 if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010122 ODRDiagBaseError(FirstRecord->getLocation(),
10123 FirstBase.getSourceRange(), BaseVirtual)
Richard Trieue13eabe2017-09-30 02:19:17 +000010124 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010125 ODRDiagBaseNote(SecondRecord->getLocation(),
10126 SecondBase.getSourceRange(), BaseVirtual)
Richard Trieue13eabe2017-09-30 02:19:17 +000010127 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10128 break;
10129 }
10130
10131 if (FirstBase.getAccessSpecifierAsWritten() !=
10132 SecondBase.getAccessSpecifierAsWritten()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010133 ODRDiagBaseError(FirstRecord->getLocation(),
10134 FirstBase.getSourceRange(), BaseAccess)
Richard Trieue13eabe2017-09-30 02:19:17 +000010135 << (i + 1) << FirstBase.getType()
10136 << (int)FirstBase.getAccessSpecifierAsWritten();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010137 ODRDiagBaseNote(SecondRecord->getLocation(),
10138 SecondBase.getSourceRange(), BaseAccess)
Richard Trieue13eabe2017-09-30 02:19:17 +000010139 << (i + 1) << SecondBase.getType()
10140 << (int)SecondBase.getAccessSpecifierAsWritten();
10141 break;
10142 }
10143 }
10144
10145 if (i != FirstNumBases) {
10146 Diagnosed = true;
10147 break;
10148 }
10149 }
10150
Richard Trieu498117b2017-08-23 02:43:59 +000010151 const ClassTemplateDecl *FirstTemplate =
10152 FirstRecord->getDescribedClassTemplate();
10153 const ClassTemplateDecl *SecondTemplate =
10154 SecondRecord->getDescribedClassTemplate();
10155
10156 assert(!FirstTemplate == !SecondTemplate &&
10157 "Both pointers should be null or non-null");
10158
10159 enum ODRTemplateDifference {
10160 ParamEmptyName,
10161 ParamName,
10162 ParamSingleDefaultArgument,
10163 ParamDifferentDefaultArgument,
10164 };
10165
10166 if (FirstTemplate && SecondTemplate) {
10167 DeclHashes FirstTemplateHashes;
10168 DeclHashes SecondTemplateHashes;
Richard Trieu498117b2017-08-23 02:43:59 +000010169
10170 auto PopulateTemplateParameterHashs =
Richard Trieue6caa262017-12-23 00:41:01 +000010171 [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10172 const ClassTemplateDecl *TD) {
Richard Trieu498117b2017-08-23 02:43:59 +000010173 for (auto *D : TD->getTemplateParameters()->asArray()) {
Richard Trieue6caa262017-12-23 00:41:01 +000010174 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
Richard Trieu498117b2017-08-23 02:43:59 +000010175 }
10176 };
10177
10178 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10179 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10180
10181 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10182 "Number of template parameters should be equal.");
10183
10184 auto FirstIt = FirstTemplateHashes.begin();
10185 auto FirstEnd = FirstTemplateHashes.end();
10186 auto SecondIt = SecondTemplateHashes.begin();
10187 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10188 if (FirstIt->second == SecondIt->second)
10189 continue;
10190
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010191 auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10192 SourceLocation Loc, SourceRange Range,
10193 ODRTemplateDifference DiffType) {
Richard Trieu498117b2017-08-23 02:43:59 +000010194 return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10195 << FirstRecord << FirstModule.empty() << FirstModule << Range
10196 << DiffType;
10197 };
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010198 auto ODRDiagTemplateNote = [&SecondModule, this](
10199 SourceLocation Loc, SourceRange Range,
10200 ODRTemplateDifference DiffType) {
Richard Trieu498117b2017-08-23 02:43:59 +000010201 return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10202 << SecondModule << Range << DiffType;
10203 };
10204
Vedant Kumar48b4f762018-04-14 01:40:48 +000010205 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10206 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
Richard Trieu498117b2017-08-23 02:43:59 +000010207
10208 assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10209 "Parameter Decl's should be the same kind.");
10210
10211 DeclarationName FirstName = FirstDecl->getDeclName();
10212 DeclarationName SecondName = SecondDecl->getDeclName();
10213
10214 if (FirstName != SecondName) {
10215 const bool FirstNameEmpty =
10216 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10217 const bool SecondNameEmpty =
10218 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10219 assert((!FirstNameEmpty || !SecondNameEmpty) &&
10220 "Both template parameters cannot be unnamed.");
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010221 ODRDiagTemplateError(FirstDecl->getLocation(),
10222 FirstDecl->getSourceRange(),
10223 FirstNameEmpty ? ParamEmptyName : ParamName)
Richard Trieu498117b2017-08-23 02:43:59 +000010224 << FirstName;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010225 ODRDiagTemplateNote(SecondDecl->getLocation(),
10226 SecondDecl->getSourceRange(),
10227 SecondNameEmpty ? ParamEmptyName : ParamName)
Richard Trieu498117b2017-08-23 02:43:59 +000010228 << SecondName;
10229 break;
10230 }
10231
10232 switch (FirstDecl->getKind()) {
10233 default:
10234 llvm_unreachable("Invalid template parameter type.");
10235 case Decl::TemplateTypeParm: {
10236 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10237 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10238 const bool HasFirstDefaultArgument =
10239 FirstParam->hasDefaultArgument() &&
10240 !FirstParam->defaultArgumentWasInherited();
10241 const bool HasSecondDefaultArgument =
10242 SecondParam->hasDefaultArgument() &&
10243 !SecondParam->defaultArgumentWasInherited();
10244
10245 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010246 ODRDiagTemplateError(FirstDecl->getLocation(),
10247 FirstDecl->getSourceRange(),
10248 ParamSingleDefaultArgument)
Richard Trieu498117b2017-08-23 02:43:59 +000010249 << HasFirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010250 ODRDiagTemplateNote(SecondDecl->getLocation(),
10251 SecondDecl->getSourceRange(),
10252 ParamSingleDefaultArgument)
Richard Trieu498117b2017-08-23 02:43:59 +000010253 << HasSecondDefaultArgument;
10254 break;
10255 }
10256
10257 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10258 "Expecting default arguments.");
10259
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010260 ODRDiagTemplateError(FirstDecl->getLocation(),
10261 FirstDecl->getSourceRange(),
10262 ParamDifferentDefaultArgument);
10263 ODRDiagTemplateNote(SecondDecl->getLocation(),
10264 SecondDecl->getSourceRange(),
10265 ParamDifferentDefaultArgument);
Richard Trieu498117b2017-08-23 02:43:59 +000010266
10267 break;
10268 }
10269 case Decl::NonTypeTemplateParm: {
10270 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10271 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10272 const bool HasFirstDefaultArgument =
10273 FirstParam->hasDefaultArgument() &&
10274 !FirstParam->defaultArgumentWasInherited();
10275 const bool HasSecondDefaultArgument =
10276 SecondParam->hasDefaultArgument() &&
10277 !SecondParam->defaultArgumentWasInherited();
10278
10279 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010280 ODRDiagTemplateError(FirstDecl->getLocation(),
10281 FirstDecl->getSourceRange(),
10282 ParamSingleDefaultArgument)
Richard Trieu498117b2017-08-23 02:43:59 +000010283 << HasFirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010284 ODRDiagTemplateNote(SecondDecl->getLocation(),
10285 SecondDecl->getSourceRange(),
10286 ParamSingleDefaultArgument)
Richard Trieu498117b2017-08-23 02:43:59 +000010287 << HasSecondDefaultArgument;
10288 break;
10289 }
10290
10291 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10292 "Expecting default arguments.");
10293
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010294 ODRDiagTemplateError(FirstDecl->getLocation(),
10295 FirstDecl->getSourceRange(),
10296 ParamDifferentDefaultArgument);
10297 ODRDiagTemplateNote(SecondDecl->getLocation(),
10298 SecondDecl->getSourceRange(),
10299 ParamDifferentDefaultArgument);
Richard Trieu498117b2017-08-23 02:43:59 +000010300
10301 break;
10302 }
10303 case Decl::TemplateTemplateParm: {
10304 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10305 const auto *SecondParam =
10306 cast<TemplateTemplateParmDecl>(SecondDecl);
10307 const bool HasFirstDefaultArgument =
10308 FirstParam->hasDefaultArgument() &&
10309 !FirstParam->defaultArgumentWasInherited();
10310 const bool HasSecondDefaultArgument =
10311 SecondParam->hasDefaultArgument() &&
10312 !SecondParam->defaultArgumentWasInherited();
10313
10314 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010315 ODRDiagTemplateError(FirstDecl->getLocation(),
10316 FirstDecl->getSourceRange(),
10317 ParamSingleDefaultArgument)
Richard Trieu498117b2017-08-23 02:43:59 +000010318 << HasFirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010319 ODRDiagTemplateNote(SecondDecl->getLocation(),
10320 SecondDecl->getSourceRange(),
10321 ParamSingleDefaultArgument)
Richard Trieu498117b2017-08-23 02:43:59 +000010322 << HasSecondDefaultArgument;
10323 break;
10324 }
10325
10326 assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10327 "Expecting default arguments.");
10328
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010329 ODRDiagTemplateError(FirstDecl->getLocation(),
10330 FirstDecl->getSourceRange(),
10331 ParamDifferentDefaultArgument);
10332 ODRDiagTemplateNote(SecondDecl->getLocation(),
10333 SecondDecl->getSourceRange(),
10334 ParamDifferentDefaultArgument);
Richard Trieu498117b2017-08-23 02:43:59 +000010335
10336 break;
10337 }
10338 }
10339
10340 break;
10341 }
10342
10343 if (FirstIt != FirstEnd) {
10344 Diagnosed = true;
10345 break;
10346 }
10347 }
10348
Richard Trieue7f7ed22017-02-22 01:11:25 +000010349 DeclHashes FirstHashes;
10350 DeclHashes SecondHashes;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010351 const DeclContext *DC = FirstRecord;
10352 PopulateHashes(FirstHashes, FirstRecord, DC);
10353 PopulateHashes(SecondHashes, SecondRecord, DC);
Richard Trieue7f7ed22017-02-22 01:11:25 +000010354
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010355 auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10356 ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10357 ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10358 Decl *FirstDecl = DR.FirstDecl;
10359 Decl *SecondDecl = DR.SecondDecl;
Richard Trieue7f7ed22017-02-22 01:11:25 +000010360
10361 if (FirstDiffType == Other || SecondDiffType == Other) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010362 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10363 SecondModule);
Richard Trieue7f7ed22017-02-22 01:11:25 +000010364 Diagnosed = true;
10365 break;
10366 }
10367
10368 if (FirstDiffType != SecondDiffType) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010369 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10370 SecondModule);
Richard Trieue7f7ed22017-02-22 01:11:25 +000010371 Diagnosed = true;
10372 break;
10373 }
10374
Richard Trieu639d7b62017-02-22 22:22:42 +000010375 assert(FirstDiffType == SecondDiffType);
10376
Richard Trieu639d7b62017-02-22 22:22:42 +000010377 switch (FirstDiffType) {
10378 case Other:
10379 case EndOfClass:
10380 case PublicSpecifer:
10381 case PrivateSpecifer:
10382 case ProtectedSpecifer:
10383 llvm_unreachable("Invalid diff type");
10384
10385 case StaticAssert: {
Vedant Kumar48b4f762018-04-14 01:40:48 +000010386 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10387 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
Richard Trieu639d7b62017-02-22 22:22:42 +000010388
10389 Expr *FirstExpr = FirstSA->getAssertExpr();
10390 Expr *SecondExpr = SecondSA->getAssertExpr();
10391 unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10392 unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10393 if (FirstODRHash != SecondODRHash) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010394 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10395 FirstExpr->getSourceRange(), StaticAssertCondition);
10396 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10397 SecondExpr->getSourceRange(), StaticAssertCondition);
Richard Trieu639d7b62017-02-22 22:22:42 +000010398 Diagnosed = true;
10399 break;
10400 }
10401
10402 StringLiteral *FirstStr = FirstSA->getMessage();
10403 StringLiteral *SecondStr = SecondSA->getMessage();
10404 assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10405 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10406 SourceLocation FirstLoc, SecondLoc;
10407 SourceRange FirstRange, SecondRange;
10408 if (FirstStr) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010409 FirstLoc = FirstStr->getBeginLoc();
Richard Trieu639d7b62017-02-22 22:22:42 +000010410 FirstRange = FirstStr->getSourceRange();
10411 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010412 FirstLoc = FirstSA->getBeginLoc();
Richard Trieu639d7b62017-02-22 22:22:42 +000010413 FirstRange = FirstSA->getSourceRange();
10414 }
10415 if (SecondStr) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010416 SecondLoc = SecondStr->getBeginLoc();
Richard Trieu639d7b62017-02-22 22:22:42 +000010417 SecondRange = SecondStr->getSourceRange();
10418 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010419 SecondLoc = SecondSA->getBeginLoc();
Richard Trieu639d7b62017-02-22 22:22:42 +000010420 SecondRange = SecondSA->getSourceRange();
10421 }
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010422 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10423 StaticAssertOnlyMessage)
Richard Trieu639d7b62017-02-22 22:22:42 +000010424 << (FirstStr == nullptr);
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010425 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10426 StaticAssertOnlyMessage)
Richard Trieu639d7b62017-02-22 22:22:42 +000010427 << (SecondStr == nullptr);
10428 Diagnosed = true;
10429 break;
10430 }
10431
10432 if (FirstStr && SecondStr &&
10433 FirstStr->getString() != SecondStr->getString()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010434 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10435 FirstStr->getSourceRange(), StaticAssertMessage);
10436 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10437 SecondStr->getSourceRange(), StaticAssertMessage);
Richard Trieu639d7b62017-02-22 22:22:42 +000010438 Diagnosed = true;
10439 break;
10440 }
10441 break;
10442 }
Richard Trieud0786092017-02-23 00:23:01 +000010443 case Field: {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010444 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10445 cast<FieldDecl>(FirstDecl),
10446 cast<FieldDecl>(SecondDecl));
Richard Trieud0786092017-02-23 00:23:01 +000010447 break;
10448 }
Richard Trieu48143742017-02-28 21:24:38 +000010449 case CXXMethod: {
Richard Trieu1c71d512017-07-15 02:55:13 +000010450 enum {
10451 DiagMethod,
10452 DiagConstructor,
10453 DiagDestructor,
10454 } FirstMethodType,
10455 SecondMethodType;
10456 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10457 if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10458 if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10459 return DiagMethod;
10460 };
Vedant Kumar48b4f762018-04-14 01:40:48 +000010461 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10462 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
Richard Trieu1c71d512017-07-15 02:55:13 +000010463 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10464 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
Richard Trieu583e2c12017-03-04 00:08:58 +000010465 auto FirstName = FirstMethod->getDeclName();
10466 auto SecondName = SecondMethod->getDeclName();
Richard Trieu1c71d512017-07-15 02:55:13 +000010467 if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010468 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10469 FirstMethod->getSourceRange(), MethodName)
Richard Trieu1c71d512017-07-15 02:55:13 +000010470 << FirstMethodType << FirstName;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010471 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10472 SecondMethod->getSourceRange(), MethodName)
Richard Trieu1c71d512017-07-15 02:55:13 +000010473 << SecondMethodType << SecondName;
Richard Trieu48143742017-02-28 21:24:38 +000010474
10475 Diagnosed = true;
10476 break;
10477 }
10478
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010479 const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10480 const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
Richard Trieu583e2c12017-03-04 00:08:58 +000010481 if (FirstDeleted != SecondDeleted) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010482 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10483 FirstMethod->getSourceRange(), MethodDeleted)
Richard Trieu1c71d512017-07-15 02:55:13 +000010484 << FirstMethodType << FirstName << FirstDeleted;
Richard Trieu583e2c12017-03-04 00:08:58 +000010485
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010486 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10487 SecondMethod->getSourceRange(), MethodDeleted)
Richard Trieu1c71d512017-07-15 02:55:13 +000010488 << SecondMethodType << SecondName << SecondDeleted;
Richard Trieu583e2c12017-03-04 00:08:58 +000010489 Diagnosed = true;
10490 break;
10491 }
10492
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010493 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10494 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10495 if (FirstDefaulted != SecondDefaulted) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010496 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10497 FirstMethod->getSourceRange(), MethodDefaulted)
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010498 << FirstMethodType << FirstName << FirstDefaulted;
10499
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010500 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10501 SecondMethod->getSourceRange(), MethodDefaulted)
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010502 << SecondMethodType << SecondName << SecondDefaulted;
10503 Diagnosed = true;
10504 break;
10505 }
10506
Richard Trieu583e2c12017-03-04 00:08:58 +000010507 const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10508 const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10509 const bool FirstPure = FirstMethod->isPure();
10510 const bool SecondPure = SecondMethod->isPure();
10511 if ((FirstVirtual || SecondVirtual) &&
10512 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010513 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10514 FirstMethod->getSourceRange(), MethodVirtual)
Richard Trieu1c71d512017-07-15 02:55:13 +000010515 << FirstMethodType << FirstName << FirstPure << FirstVirtual;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010516 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10517 SecondMethod->getSourceRange(), MethodVirtual)
Richard Trieu1c71d512017-07-15 02:55:13 +000010518 << SecondMethodType << SecondName << SecondPure << SecondVirtual;
Richard Trieu583e2c12017-03-04 00:08:58 +000010519 Diagnosed = true;
10520 break;
10521 }
10522
10523 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging,
10524 // FirstDecl is the canonical Decl of SecondDecl, so the storage
10525 // class needs to be checked instead.
10526 const auto FirstStorage = FirstMethod->getStorageClass();
10527 const auto SecondStorage = SecondMethod->getStorageClass();
10528 const bool FirstStatic = FirstStorage == SC_Static;
10529 const bool SecondStatic = SecondStorage == SC_Static;
10530 if (FirstStatic != SecondStatic) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010531 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10532 FirstMethod->getSourceRange(), MethodStatic)
Richard Trieu1c71d512017-07-15 02:55:13 +000010533 << FirstMethodType << FirstName << FirstStatic;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010534 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10535 SecondMethod->getSourceRange(), MethodStatic)
Richard Trieu1c71d512017-07-15 02:55:13 +000010536 << SecondMethodType << SecondName << SecondStatic;
Richard Trieu583e2c12017-03-04 00:08:58 +000010537 Diagnosed = true;
10538 break;
10539 }
10540
10541 const bool FirstVolatile = FirstMethod->isVolatile();
10542 const bool SecondVolatile = SecondMethod->isVolatile();
10543 if (FirstVolatile != SecondVolatile) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010544 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10545 FirstMethod->getSourceRange(), MethodVolatile)
Richard Trieu1c71d512017-07-15 02:55:13 +000010546 << FirstMethodType << FirstName << FirstVolatile;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010547 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10548 SecondMethod->getSourceRange(), MethodVolatile)
Richard Trieu1c71d512017-07-15 02:55:13 +000010549 << SecondMethodType << SecondName << SecondVolatile;
Richard Trieu583e2c12017-03-04 00:08:58 +000010550 Diagnosed = true;
10551 break;
10552 }
10553
10554 const bool FirstConst = FirstMethod->isConst();
10555 const bool SecondConst = SecondMethod->isConst();
10556 if (FirstConst != SecondConst) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010557 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10558 FirstMethod->getSourceRange(), MethodConst)
Richard Trieu1c71d512017-07-15 02:55:13 +000010559 << FirstMethodType << FirstName << FirstConst;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010560 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10561 SecondMethod->getSourceRange(), MethodConst)
Richard Trieu1c71d512017-07-15 02:55:13 +000010562 << SecondMethodType << SecondName << SecondConst;
Richard Trieu583e2c12017-03-04 00:08:58 +000010563 Diagnosed = true;
10564 break;
10565 }
10566
10567 const bool FirstInline = FirstMethod->isInlineSpecified();
10568 const bool SecondInline = SecondMethod->isInlineSpecified();
10569 if (FirstInline != SecondInline) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010570 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10571 FirstMethod->getSourceRange(), MethodInline)
Richard Trieu1c71d512017-07-15 02:55:13 +000010572 << FirstMethodType << FirstName << FirstInline;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010573 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10574 SecondMethod->getSourceRange(), MethodInline)
Richard Trieu1c71d512017-07-15 02:55:13 +000010575 << SecondMethodType << SecondName << SecondInline;
Richard Trieu583e2c12017-03-04 00:08:58 +000010576 Diagnosed = true;
10577 break;
10578 }
10579
Richard Trieu02552272017-05-02 23:58:52 +000010580 const unsigned FirstNumParameters = FirstMethod->param_size();
10581 const unsigned SecondNumParameters = SecondMethod->param_size();
10582 if (FirstNumParameters != SecondNumParameters) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010583 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10584 FirstMethod->getSourceRange(),
10585 MethodNumberParameters)
Richard Trieu1c71d512017-07-15 02:55:13 +000010586 << FirstMethodType << FirstName << FirstNumParameters;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010587 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10588 SecondMethod->getSourceRange(),
10589 MethodNumberParameters)
Richard Trieu1c71d512017-07-15 02:55:13 +000010590 << SecondMethodType << SecondName << SecondNumParameters;
Richard Trieu02552272017-05-02 23:58:52 +000010591 Diagnosed = true;
10592 break;
10593 }
10594
10595 // Need this status boolean to know when break out of the switch.
10596 bool ParameterMismatch = false;
10597 for (unsigned I = 0; I < FirstNumParameters; ++I) {
10598 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10599 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10600
10601 QualType FirstParamType = FirstParam->getType();
10602 QualType SecondParamType = SecondParam->getType();
10603 if (FirstParamType != SecondParamType &&
10604 ComputeQualTypeODRHash(FirstParamType) !=
10605 ComputeQualTypeODRHash(SecondParamType)) {
Vedant Kumar48b4f762018-04-14 01:40:48 +000010606 if (const DecayedType *ParamDecayedType =
Richard Trieu02552272017-05-02 23:58:52 +000010607 FirstParamType->getAs<DecayedType>()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010608 ODRDiagDeclError(
10609 FirstRecord, FirstModule, FirstMethod->getLocation(),
10610 FirstMethod->getSourceRange(), MethodParameterType)
Richard Trieu1c71d512017-07-15 02:55:13 +000010611 << FirstMethodType << FirstName << (I + 1) << FirstParamType
10612 << true << ParamDecayedType->getOriginalType();
Richard Trieu02552272017-05-02 23:58:52 +000010613 } else {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010614 ODRDiagDeclError(
10615 FirstRecord, FirstModule, FirstMethod->getLocation(),
10616 FirstMethod->getSourceRange(), MethodParameterType)
Richard Trieu1c71d512017-07-15 02:55:13 +000010617 << FirstMethodType << FirstName << (I + 1) << FirstParamType
10618 << false;
Richard Trieu02552272017-05-02 23:58:52 +000010619 }
10620
Vedant Kumar48b4f762018-04-14 01:40:48 +000010621 if (const DecayedType *ParamDecayedType =
Richard Trieu02552272017-05-02 23:58:52 +000010622 SecondParamType->getAs<DecayedType>()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010623 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10624 SecondMethod->getSourceRange(),
10625 MethodParameterType)
Richard Trieu1c71d512017-07-15 02:55:13 +000010626 << SecondMethodType << SecondName << (I + 1)
10627 << SecondParamType << true
Richard Trieu02552272017-05-02 23:58:52 +000010628 << ParamDecayedType->getOriginalType();
10629 } else {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010630 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10631 SecondMethod->getSourceRange(),
10632 MethodParameterType)
Richard Trieu1c71d512017-07-15 02:55:13 +000010633 << SecondMethodType << SecondName << (I + 1)
10634 << SecondParamType << false;
Richard Trieu02552272017-05-02 23:58:52 +000010635 }
10636 ParameterMismatch = true;
10637 break;
10638 }
10639
10640 DeclarationName FirstParamName = FirstParam->getDeclName();
10641 DeclarationName SecondParamName = SecondParam->getDeclName();
10642 if (FirstParamName != SecondParamName) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010643 ODRDiagDeclError(FirstRecord, FirstModule,
10644 FirstMethod->getLocation(),
10645 FirstMethod->getSourceRange(), MethodParameterName)
Richard Trieu1c71d512017-07-15 02:55:13 +000010646 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010647 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10648 SecondMethod->getSourceRange(), MethodParameterName)
Richard Trieu1c71d512017-07-15 02:55:13 +000010649 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
Richard Trieu02552272017-05-02 23:58:52 +000010650 ParameterMismatch = true;
10651 break;
10652 }
Richard Trieu6e13ff32017-06-16 02:44:29 +000010653
10654 const Expr *FirstInit = FirstParam->getInit();
10655 const Expr *SecondInit = SecondParam->getInit();
10656 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010657 ODRDiagDeclError(FirstRecord, FirstModule,
10658 FirstMethod->getLocation(),
10659 FirstMethod->getSourceRange(),
10660 MethodParameterSingleDefaultArgument)
Richard Trieu1c71d512017-07-15 02:55:13 +000010661 << FirstMethodType << FirstName << (I + 1)
10662 << (FirstInit == nullptr)
Richard Trieu6e13ff32017-06-16 02:44:29 +000010663 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010664 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10665 SecondMethod->getSourceRange(),
10666 MethodParameterSingleDefaultArgument)
Richard Trieu1c71d512017-07-15 02:55:13 +000010667 << SecondMethodType << SecondName << (I + 1)
10668 << (SecondInit == nullptr)
Richard Trieu6e13ff32017-06-16 02:44:29 +000010669 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10670 ParameterMismatch = true;
10671 break;
10672 }
10673
10674 if (FirstInit && SecondInit &&
10675 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010676 ODRDiagDeclError(FirstRecord, FirstModule,
10677 FirstMethod->getLocation(),
10678 FirstMethod->getSourceRange(),
10679 MethodParameterDifferentDefaultArgument)
Richard Trieu1c71d512017-07-15 02:55:13 +000010680 << FirstMethodType << FirstName << (I + 1)
10681 << FirstInit->getSourceRange();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010682 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10683 SecondMethod->getSourceRange(),
10684 MethodParameterDifferentDefaultArgument)
Richard Trieu1c71d512017-07-15 02:55:13 +000010685 << SecondMethodType << SecondName << (I + 1)
10686 << SecondInit->getSourceRange();
Richard Trieu6e13ff32017-06-16 02:44:29 +000010687 ParameterMismatch = true;
10688 break;
10689
10690 }
Richard Trieu02552272017-05-02 23:58:52 +000010691 }
10692
10693 if (ParameterMismatch) {
10694 Diagnosed = true;
10695 break;
10696 }
10697
Richard Trieu7282d322018-04-25 00:31:15 +000010698 const auto *FirstTemplateArgs =
10699 FirstMethod->getTemplateSpecializationArgs();
10700 const auto *SecondTemplateArgs =
10701 SecondMethod->getTemplateSpecializationArgs();
10702
10703 if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10704 (!FirstTemplateArgs && SecondTemplateArgs)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010705 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10706 FirstMethod->getSourceRange(),
10707 MethodNoTemplateArguments)
Richard Trieu7282d322018-04-25 00:31:15 +000010708 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010709 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10710 SecondMethod->getSourceRange(),
10711 MethodNoTemplateArguments)
Richard Trieu7282d322018-04-25 00:31:15 +000010712 << SecondMethodType << SecondName
10713 << (SecondTemplateArgs != nullptr);
10714
10715 Diagnosed = true;
10716 break;
10717 }
10718
10719 if (FirstTemplateArgs && SecondTemplateArgs) {
10720 // Remove pack expansions from argument list.
10721 auto ExpandTemplateArgumentList =
10722 [](const TemplateArgumentList *TAL) {
10723 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10724 for (const TemplateArgument &TA : TAL->asArray()) {
10725 if (TA.getKind() != TemplateArgument::Pack) {
10726 ExpandedList.push_back(&TA);
10727 continue;
10728 }
10729 for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10730 ExpandedList.push_back(&PackTA);
10731 }
10732 }
10733 return ExpandedList;
10734 };
10735 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10736 ExpandTemplateArgumentList(FirstTemplateArgs);
10737 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10738 ExpandTemplateArgumentList(SecondTemplateArgs);
10739
10740 if (FirstExpandedList.size() != SecondExpandedList.size()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010741 ODRDiagDeclError(FirstRecord, FirstModule,
10742 FirstMethod->getLocation(),
10743 FirstMethod->getSourceRange(),
10744 MethodDifferentNumberTemplateArguments)
Richard Trieu7282d322018-04-25 00:31:15 +000010745 << FirstMethodType << FirstName
10746 << (unsigned)FirstExpandedList.size();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010747 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10748 SecondMethod->getSourceRange(),
10749 MethodDifferentNumberTemplateArguments)
Richard Trieu7282d322018-04-25 00:31:15 +000010750 << SecondMethodType << SecondName
10751 << (unsigned)SecondExpandedList.size();
10752
10753 Diagnosed = true;
10754 break;
10755 }
10756
10757 bool TemplateArgumentMismatch = false;
10758 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10759 const TemplateArgument &FirstTA = *FirstExpandedList[i],
10760 &SecondTA = *SecondExpandedList[i];
10761 if (ComputeTemplateArgumentODRHash(FirstTA) ==
10762 ComputeTemplateArgumentODRHash(SecondTA)) {
10763 continue;
10764 }
10765
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010766 ODRDiagDeclError(
10767 FirstRecord, FirstModule, FirstMethod->getLocation(),
10768 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
Richard Trieu7282d322018-04-25 00:31:15 +000010769 << FirstMethodType << FirstName << FirstTA << i + 1;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010770 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10771 SecondMethod->getSourceRange(),
10772 MethodDifferentTemplateArgument)
Richard Trieu7282d322018-04-25 00:31:15 +000010773 << SecondMethodType << SecondName << SecondTA << i + 1;
10774
10775 TemplateArgumentMismatch = true;
10776 break;
10777 }
10778
10779 if (TemplateArgumentMismatch) {
10780 Diagnosed = true;
10781 break;
10782 }
10783 }
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010784
10785 // Compute the hash of the method as if it has no body.
10786 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10787 Hash.clear();
10788 Hash.AddFunctionDecl(D, true /*SkipBody*/);
10789 return Hash.CalculateHash();
10790 };
10791
10792 // Compare the hash generated to the hash stored. A difference means
10793 // that a body was present in the original source. Due to merging,
10794 // the stardard way of detecting a body will not work.
10795 const bool HasFirstBody =
10796 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10797 const bool HasSecondBody =
10798 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10799
10800 if (HasFirstBody != HasSecondBody) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010801 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10802 FirstMethod->getSourceRange(), MethodSingleBody)
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010803 << FirstMethodType << FirstName << HasFirstBody;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010804 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10805 SecondMethod->getSourceRange(), MethodSingleBody)
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010806 << SecondMethodType << SecondName << HasSecondBody;
10807 Diagnosed = true;
10808 break;
10809 }
10810
10811 if (HasFirstBody && HasSecondBody) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010812 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10813 FirstMethod->getSourceRange(), MethodDifferentBody)
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010814 << FirstMethodType << FirstName;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010815 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10816 SecondMethod->getSourceRange(), MethodDifferentBody)
Richard Trieu27c1b1a2018-07-10 01:40:50 +000010817 << SecondMethodType << SecondName;
10818 Diagnosed = true;
10819 break;
10820 }
10821
Richard Trieu48143742017-02-28 21:24:38 +000010822 break;
10823 }
Richard Trieu11d566a2017-06-12 21:58:22 +000010824 case TypeAlias:
10825 case TypeDef: {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010826 Diagnosed = ODRDiagTypeDefOrAlias(
10827 FirstRecord, FirstModule, SecondModule,
10828 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10829 FirstDiffType == TypeAlias);
Richard Trieu11d566a2017-06-12 21:58:22 +000010830 break;
10831 }
Richard Trieu6e13ff32017-06-16 02:44:29 +000010832 case Var: {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010833 Diagnosed =
10834 ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10835 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
Richard Trieu6e13ff32017-06-16 02:44:29 +000010836 break;
10837 }
Richard Trieuac6a1b62017-07-08 02:04:42 +000010838 case Friend: {
Vedant Kumar48b4f762018-04-14 01:40:48 +000010839 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10840 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
Richard Trieuac6a1b62017-07-08 02:04:42 +000010841
10842 NamedDecl *FirstND = FirstFriend->getFriendDecl();
10843 NamedDecl *SecondND = SecondFriend->getFriendDecl();
10844
10845 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10846 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10847
10848 if (FirstND && SecondND) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010849 ODRDiagDeclError(FirstRecord, FirstModule,
10850 FirstFriend->getFriendLoc(),
10851 FirstFriend->getSourceRange(), FriendFunction)
Richard Trieuac6a1b62017-07-08 02:04:42 +000010852 << FirstND;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010853 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10854 SecondFriend->getSourceRange(), FriendFunction)
Richard Trieuac6a1b62017-07-08 02:04:42 +000010855 << SecondND;
10856
10857 Diagnosed = true;
10858 break;
10859 }
10860
10861 if (FirstTSI && SecondTSI) {
10862 QualType FirstFriendType = FirstTSI->getType();
10863 QualType SecondFriendType = SecondTSI->getType();
10864 assert(ComputeQualTypeODRHash(FirstFriendType) !=
10865 ComputeQualTypeODRHash(SecondFriendType));
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010866 ODRDiagDeclError(FirstRecord, FirstModule,
10867 FirstFriend->getFriendLoc(),
10868 FirstFriend->getSourceRange(), FriendType)
Richard Trieuac6a1b62017-07-08 02:04:42 +000010869 << FirstFriendType;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010870 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10871 SecondFriend->getSourceRange(), FriendType)
Richard Trieuac6a1b62017-07-08 02:04:42 +000010872 << SecondFriendType;
10873 Diagnosed = true;
10874 break;
10875 }
10876
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010877 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10878 FirstFriend->getSourceRange(), FriendTypeFunction)
Richard Trieuac6a1b62017-07-08 02:04:42 +000010879 << (FirstTSI == nullptr);
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010880 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10881 SecondFriend->getSourceRange(), FriendTypeFunction)
Richard Trieuac6a1b62017-07-08 02:04:42 +000010882 << (SecondTSI == nullptr);
10883
10884 Diagnosed = true;
10885 break;
10886 }
Richard Trieu9359e8f2018-05-30 01:12:26 +000010887 case FunctionTemplate: {
10888 FunctionTemplateDecl *FirstTemplate =
10889 cast<FunctionTemplateDecl>(FirstDecl);
10890 FunctionTemplateDecl *SecondTemplate =
10891 cast<FunctionTemplateDecl>(SecondDecl);
10892
10893 TemplateParameterList *FirstTPL =
10894 FirstTemplate->getTemplateParameters();
10895 TemplateParameterList *SecondTPL =
10896 SecondTemplate->getTemplateParameters();
10897
10898 if (FirstTPL->size() != SecondTPL->size()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010899 ODRDiagDeclError(FirstRecord, FirstModule,
10900 FirstTemplate->getLocation(),
10901 FirstTemplate->getSourceRange(),
10902 FunctionTemplateDifferentNumberParameters)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010903 << FirstTemplate << FirstTPL->size();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010904 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10905 SecondTemplate->getSourceRange(),
10906 FunctionTemplateDifferentNumberParameters)
10907 << SecondTemplate << SecondTPL->size();
Richard Trieu9359e8f2018-05-30 01:12:26 +000010908
10909 Diagnosed = true;
10910 break;
10911 }
10912
10913 bool ParameterMismatch = false;
10914 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10915 NamedDecl *FirstParam = FirstTPL->getParam(i);
10916 NamedDecl *SecondParam = SecondTPL->getParam(i);
10917
10918 if (FirstParam->getKind() != SecondParam->getKind()) {
10919 enum {
10920 TemplateTypeParameter,
10921 NonTypeTemplateParameter,
10922 TemplateTemplateParameter,
10923 };
10924 auto GetParamType = [](NamedDecl *D) {
10925 switch (D->getKind()) {
10926 default:
10927 llvm_unreachable("Unexpected template parameter type");
10928 case Decl::TemplateTypeParm:
10929 return TemplateTypeParameter;
10930 case Decl::NonTypeTemplateParm:
10931 return NonTypeTemplateParameter;
10932 case Decl::TemplateTemplateParm:
10933 return TemplateTemplateParameter;
10934 }
10935 };
10936
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010937 ODRDiagDeclError(FirstRecord, FirstModule,
10938 FirstTemplate->getLocation(),
10939 FirstTemplate->getSourceRange(),
10940 FunctionTemplateParameterDifferentKind)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010941 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010942 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10943 SecondTemplate->getSourceRange(),
10944 FunctionTemplateParameterDifferentKind)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010945 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10946
10947 ParameterMismatch = true;
10948 break;
10949 }
10950
10951 if (FirstParam->getName() != SecondParam->getName()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010952 ODRDiagDeclError(
10953 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10954 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010955 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10956 << FirstParam;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010957 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10958 SecondTemplate->getSourceRange(),
10959 FunctionTemplateParameterName)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010960 << SecondTemplate << (i + 1)
10961 << (bool)SecondParam->getIdentifier() << SecondParam;
10962 ParameterMismatch = true;
10963 break;
10964 }
10965
10966 if (isa<TemplateTypeParmDecl>(FirstParam) &&
10967 isa<TemplateTypeParmDecl>(SecondParam)) {
10968 TemplateTypeParmDecl *FirstTTPD =
10969 cast<TemplateTypeParmDecl>(FirstParam);
10970 TemplateTypeParmDecl *SecondTTPD =
10971 cast<TemplateTypeParmDecl>(SecondParam);
10972 bool HasFirstDefaultArgument =
10973 FirstTTPD->hasDefaultArgument() &&
10974 !FirstTTPD->defaultArgumentWasInherited();
10975 bool HasSecondDefaultArgument =
10976 SecondTTPD->hasDefaultArgument() &&
10977 !SecondTTPD->defaultArgumentWasInherited();
10978 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010979 ODRDiagDeclError(FirstRecord, FirstModule,
10980 FirstTemplate->getLocation(),
10981 FirstTemplate->getSourceRange(),
10982 FunctionTemplateParameterSingleDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010983 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010984 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10985 SecondTemplate->getSourceRange(),
10986 FunctionTemplateParameterSingleDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000010987 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10988 ParameterMismatch = true;
10989 break;
10990 }
10991
10992 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10993 QualType FirstType = FirstTTPD->getDefaultArgument();
10994 QualType SecondType = SecondTTPD->getDefaultArgument();
10995 if (ComputeQualTypeODRHash(FirstType) !=
10996 ComputeQualTypeODRHash(SecondType)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080010997 ODRDiagDeclError(
10998 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10999 FirstTemplate->getSourceRange(),
11000 FunctionTemplateParameterDifferentDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011001 << FirstTemplate << (i + 1) << FirstType;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011002 ODRDiagDeclNote(
11003 SecondModule, SecondTemplate->getLocation(),
11004 SecondTemplate->getSourceRange(),
11005 FunctionTemplateParameterDifferentDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011006 << SecondTemplate << (i + 1) << SecondType;
11007 ParameterMismatch = true;
11008 break;
11009 }
11010 }
11011
11012 if (FirstTTPD->isParameterPack() !=
11013 SecondTTPD->isParameterPack()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011014 ODRDiagDeclError(FirstRecord, FirstModule,
11015 FirstTemplate->getLocation(),
11016 FirstTemplate->getSourceRange(),
11017 FunctionTemplatePackParameter)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011018 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011019 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11020 SecondTemplate->getSourceRange(),
11021 FunctionTemplatePackParameter)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011022 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11023 ParameterMismatch = true;
11024 break;
11025 }
11026 }
11027
11028 if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11029 isa<TemplateTemplateParmDecl>(SecondParam)) {
11030 TemplateTemplateParmDecl *FirstTTPD =
11031 cast<TemplateTemplateParmDecl>(FirstParam);
11032 TemplateTemplateParmDecl *SecondTTPD =
11033 cast<TemplateTemplateParmDecl>(SecondParam);
11034
11035 TemplateParameterList *FirstTPL =
11036 FirstTTPD->getTemplateParameters();
11037 TemplateParameterList *SecondTPL =
11038 SecondTTPD->getTemplateParameters();
11039
11040 if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11041 ComputeTemplateParameterListODRHash(SecondTPL)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011042 ODRDiagDeclError(FirstRecord, FirstModule,
11043 FirstTemplate->getLocation(),
11044 FirstTemplate->getSourceRange(),
11045 FunctionTemplateParameterDifferentType)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011046 << FirstTemplate << (i + 1);
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011047 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11048 SecondTemplate->getSourceRange(),
11049 FunctionTemplateParameterDifferentType)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011050 << SecondTemplate << (i + 1);
11051 ParameterMismatch = true;
11052 break;
11053 }
11054
11055 bool HasFirstDefaultArgument =
11056 FirstTTPD->hasDefaultArgument() &&
11057 !FirstTTPD->defaultArgumentWasInherited();
11058 bool HasSecondDefaultArgument =
11059 SecondTTPD->hasDefaultArgument() &&
11060 !SecondTTPD->defaultArgumentWasInherited();
11061 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011062 ODRDiagDeclError(FirstRecord, FirstModule,
11063 FirstTemplate->getLocation(),
11064 FirstTemplate->getSourceRange(),
11065 FunctionTemplateParameterSingleDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011066 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011067 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11068 SecondTemplate->getSourceRange(),
11069 FunctionTemplateParameterSingleDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011070 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11071 ParameterMismatch = true;
11072 break;
11073 }
11074
11075 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11076 TemplateArgument FirstTA =
11077 FirstTTPD->getDefaultArgument().getArgument();
11078 TemplateArgument SecondTA =
11079 SecondTTPD->getDefaultArgument().getArgument();
11080 if (ComputeTemplateArgumentODRHash(FirstTA) !=
11081 ComputeTemplateArgumentODRHash(SecondTA)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011082 ODRDiagDeclError(
11083 FirstRecord, FirstModule, FirstTemplate->getLocation(),
11084 FirstTemplate->getSourceRange(),
11085 FunctionTemplateParameterDifferentDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011086 << FirstTemplate << (i + 1) << FirstTA;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011087 ODRDiagDeclNote(
11088 SecondModule, SecondTemplate->getLocation(),
11089 SecondTemplate->getSourceRange(),
11090 FunctionTemplateParameterDifferentDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011091 << SecondTemplate << (i + 1) << SecondTA;
11092 ParameterMismatch = true;
11093 break;
11094 }
11095 }
11096
11097 if (FirstTTPD->isParameterPack() !=
11098 SecondTTPD->isParameterPack()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011099 ODRDiagDeclError(FirstRecord, FirstModule,
11100 FirstTemplate->getLocation(),
11101 FirstTemplate->getSourceRange(),
11102 FunctionTemplatePackParameter)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011103 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011104 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11105 SecondTemplate->getSourceRange(),
11106 FunctionTemplatePackParameter)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011107 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11108 ParameterMismatch = true;
11109 break;
11110 }
11111 }
11112
11113 if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11114 isa<NonTypeTemplateParmDecl>(SecondParam)) {
11115 NonTypeTemplateParmDecl *FirstNTTPD =
11116 cast<NonTypeTemplateParmDecl>(FirstParam);
11117 NonTypeTemplateParmDecl *SecondNTTPD =
11118 cast<NonTypeTemplateParmDecl>(SecondParam);
11119
11120 QualType FirstType = FirstNTTPD->getType();
11121 QualType SecondType = SecondNTTPD->getType();
11122 if (ComputeQualTypeODRHash(FirstType) !=
11123 ComputeQualTypeODRHash(SecondType)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011124 ODRDiagDeclError(FirstRecord, FirstModule,
11125 FirstTemplate->getLocation(),
11126 FirstTemplate->getSourceRange(),
11127 FunctionTemplateParameterDifferentType)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011128 << FirstTemplate << (i + 1);
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011129 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11130 SecondTemplate->getSourceRange(),
11131 FunctionTemplateParameterDifferentType)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011132 << SecondTemplate << (i + 1);
11133 ParameterMismatch = true;
11134 break;
11135 }
11136
11137 bool HasFirstDefaultArgument =
11138 FirstNTTPD->hasDefaultArgument() &&
11139 !FirstNTTPD->defaultArgumentWasInherited();
11140 bool HasSecondDefaultArgument =
11141 SecondNTTPD->hasDefaultArgument() &&
11142 !SecondNTTPD->defaultArgumentWasInherited();
11143 if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011144 ODRDiagDeclError(FirstRecord, FirstModule,
11145 FirstTemplate->getLocation(),
11146 FirstTemplate->getSourceRange(),
11147 FunctionTemplateParameterSingleDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011148 << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011149 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11150 SecondTemplate->getSourceRange(),
11151 FunctionTemplateParameterSingleDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011152 << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11153 ParameterMismatch = true;
11154 break;
11155 }
11156
11157 if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11158 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11159 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11160 if (ComputeODRHash(FirstDefaultArgument) !=
11161 ComputeODRHash(SecondDefaultArgument)) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011162 ODRDiagDeclError(
11163 FirstRecord, FirstModule, FirstTemplate->getLocation(),
11164 FirstTemplate->getSourceRange(),
11165 FunctionTemplateParameterDifferentDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011166 << FirstTemplate << (i + 1) << FirstDefaultArgument;
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011167 ODRDiagDeclNote(
11168 SecondModule, SecondTemplate->getLocation(),
11169 SecondTemplate->getSourceRange(),
11170 FunctionTemplateParameterDifferentDefaultArgument)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011171 << SecondTemplate << (i + 1) << SecondDefaultArgument;
11172 ParameterMismatch = true;
11173 break;
11174 }
11175 }
11176
11177 if (FirstNTTPD->isParameterPack() !=
11178 SecondNTTPD->isParameterPack()) {
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011179 ODRDiagDeclError(FirstRecord, FirstModule,
11180 FirstTemplate->getLocation(),
11181 FirstTemplate->getSourceRange(),
11182 FunctionTemplatePackParameter)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011183 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
Bruno Cardoso Lopes90f58ea2020-01-22 12:43:19 -080011184 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11185 SecondTemplate->getSourceRange(),
11186 FunctionTemplatePackParameter)
Richard Trieu9359e8f2018-05-30 01:12:26 +000011187 << SecondTemplate << (i + 1)
11188 << SecondNTTPD->isParameterPack();
11189 ParameterMismatch = true;
11190 break;
11191 }
11192 }
11193 }
11194
11195 if (ParameterMismatch) {
11196 Diagnosed = true;
11197 break;
11198 }
11199
11200 break;
11201 }
Richard Trieu639d7b62017-02-22 22:22:42 +000011202 }
11203
Eugene Zelenkob8c9e2a2017-11-08 01:03:16 +000011204 if (Diagnosed)
Richard Trieue7f7ed22017-02-22 01:11:25 +000011205 continue;
11206
Richard Trieu708859a2017-06-08 00:56:21 +000011207 Diag(FirstDecl->getLocation(),
11208 diag::err_module_odr_violation_mismatch_decl_unknown)
11209 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11210 << FirstDecl->getSourceRange();
11211 Diag(SecondDecl->getLocation(),
11212 diag::note_module_odr_violation_mismatch_decl_unknown)
11213 << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
Richard Trieue7f7ed22017-02-22 01:11:25 +000011214 Diagnosed = true;
Richard Smithcd45dbc2014-04-19 03:48:30 +000011215 }
11216
11217 if (!Diagnosed) {
11218 // All definitions are updates to the same declaration. This happens if a
11219 // module instantiates the declaration of a class template specialization
11220 // and two or more other modules instantiate its definition.
11221 //
11222 // FIXME: Indicate which modules had instantiations of this definition.
11223 // FIXME: How can this even happen?
11224 Diag(Merge.first->getLocation(),
11225 diag::err_module_odr_violation_different_instantiations)
11226 << Merge.first;
11227 }
11228 }
Richard Trieue6caa262017-12-23 00:41:01 +000011229
11230 // Issue ODR failures diagnostics for functions.
11231 for (auto &Merge : FunctionOdrMergeFailures) {
11232 enum ODRFunctionDifference {
11233 ReturnType,
11234 ParameterName,
11235 ParameterType,
11236 ParameterSingleDefaultArgument,
11237 ParameterDifferentDefaultArgument,
11238 FunctionBody,
11239 };
11240
11241 FunctionDecl *FirstFunction = Merge.first;
11242 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11243
11244 bool Diagnosed = false;
11245 for (auto &SecondFunction : Merge.second) {
Vedant Kumar48b4f762018-04-14 01:40:48 +000011246
Richard Trieue6caa262017-12-23 00:41:01 +000011247 if (FirstFunction == SecondFunction)
11248 continue;
11249
11250 std::string SecondModule =
11251 getOwningModuleNameForDiagnostic(SecondFunction);
11252
11253 auto ODRDiagError = [FirstFunction, &FirstModule,
11254 this](SourceLocation Loc, SourceRange Range,
11255 ODRFunctionDifference DiffType) {
11256 return Diag(Loc, diag::err_module_odr_violation_function)
11257 << FirstFunction << FirstModule.empty() << FirstModule << Range
11258 << DiffType;
11259 };
11260 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11261 SourceRange Range,
11262 ODRFunctionDifference DiffType) {
11263 return Diag(Loc, diag::note_module_odr_violation_function)
11264 << SecondModule << Range << DiffType;
11265 };
11266
11267 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11268 ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11269 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11270 FirstFunction->getReturnTypeSourceRange(), ReturnType)
11271 << FirstFunction->getReturnType();
11272 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11273 SecondFunction->getReturnTypeSourceRange(), ReturnType)
11274 << SecondFunction->getReturnType();
11275 Diagnosed = true;
11276 break;
11277 }
11278
11279 assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11280 "Merged functions with different number of parameters");
11281
11282 auto ParamSize = FirstFunction->param_size();
11283 bool ParameterMismatch = false;
11284 for (unsigned I = 0; I < ParamSize; ++I) {
11285 auto *FirstParam = FirstFunction->getParamDecl(I);
11286 auto *SecondParam = SecondFunction->getParamDecl(I);
11287
11288 assert(getContext().hasSameType(FirstParam->getType(),
11289 SecondParam->getType()) &&
11290 "Merged function has different parameter types.");
11291
11292 if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11293 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11294 ParameterName)
11295 << I + 1 << FirstParam->getDeclName();
11296 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11297 ParameterName)
11298 << I + 1 << SecondParam->getDeclName();
11299 ParameterMismatch = true;
11300 break;
11301 };
11302
11303 QualType FirstParamType = FirstParam->getType();
11304 QualType SecondParamType = SecondParam->getType();
11305 if (FirstParamType != SecondParamType &&
11306 ComputeQualTypeODRHash(FirstParamType) !=
11307 ComputeQualTypeODRHash(SecondParamType)) {
Vedant Kumar48b4f762018-04-14 01:40:48 +000011308 if (const DecayedType *ParamDecayedType =
Richard Trieue6caa262017-12-23 00:41:01 +000011309 FirstParamType->getAs<DecayedType>()) {
11310 ODRDiagError(FirstParam->getLocation(),
11311 FirstParam->getSourceRange(), ParameterType)
11312 << (I + 1) << FirstParamType << true
11313 << ParamDecayedType->getOriginalType();
11314 } else {
11315 ODRDiagError(FirstParam->getLocation(),
11316 FirstParam->getSourceRange(), ParameterType)
11317 << (I + 1) << FirstParamType << false;
11318 }
11319
Vedant Kumar48b4f762018-04-14 01:40:48 +000011320 if (const DecayedType *ParamDecayedType =
Richard Trieue6caa262017-12-23 00:41:01 +000011321 SecondParamType->getAs<DecayedType>()) {
11322 ODRDiagNote(SecondParam->getLocation(),
11323 SecondParam->getSourceRange(), ParameterType)
11324 << (I + 1) << SecondParamType << true
11325 << ParamDecayedType->getOriginalType();
11326 } else {
11327 ODRDiagNote(SecondParam->getLocation(),
11328 SecondParam->getSourceRange(), ParameterType)
11329 << (I + 1) << SecondParamType << false;
11330 }
11331 ParameterMismatch = true;
11332 break;
11333 }
11334
11335 const Expr *FirstInit = FirstParam->getInit();
11336 const Expr *SecondInit = SecondParam->getInit();
11337 if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11338 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11339 ParameterSingleDefaultArgument)
11340 << (I + 1) << (FirstInit == nullptr)
11341 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11342 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11343 ParameterSingleDefaultArgument)
11344 << (I + 1) << (SecondInit == nullptr)
11345 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11346 ParameterMismatch = true;
11347 break;
11348 }
11349
11350 if (FirstInit && SecondInit &&
11351 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11352 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11353 ParameterDifferentDefaultArgument)
11354 << (I + 1) << FirstInit->getSourceRange();
11355 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11356 ParameterDifferentDefaultArgument)
11357 << (I + 1) << SecondInit->getSourceRange();
11358 ParameterMismatch = true;
11359 break;
11360 }
11361
11362 assert(ComputeSubDeclODRHash(FirstParam) ==
11363 ComputeSubDeclODRHash(SecondParam) &&
11364 "Undiagnosed parameter difference.");
11365 }
11366
11367 if (ParameterMismatch) {
11368 Diagnosed = true;
11369 break;
11370 }
11371
11372 // If no error has been generated before now, assume the problem is in
11373 // the body and generate a message.
11374 ODRDiagError(FirstFunction->getLocation(),
11375 FirstFunction->getSourceRange(), FunctionBody);
11376 ODRDiagNote(SecondFunction->getLocation(),
11377 SecondFunction->getSourceRange(), FunctionBody);
11378 Diagnosed = true;
11379 break;
11380 }
Evgeny Stupachenkobf25d672018-01-05 02:22:52 +000011381 (void)Diagnosed;
Richard Trieue6caa262017-12-23 00:41:01 +000011382 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11383 }
Richard Trieuab4d7302018-07-25 22:52:05 +000011384
11385 // Issue ODR failures diagnostics for enums.
11386 for (auto &Merge : EnumOdrMergeFailures) {
11387 enum ODREnumDifference {
11388 SingleScopedEnum,
11389 EnumTagKeywordMismatch,
11390 SingleSpecifiedType,
11391 DifferentSpecifiedTypes,
11392 DifferentNumberEnumConstants,
11393 EnumConstantName,
11394 EnumConstantSingleInitilizer,
11395 EnumConstantDifferentInitilizer,
11396 };
11397
11398 // If we've already pointed out a specific problem with this enum, don't
11399 // bother issuing a general "something's different" diagnostic.
11400 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11401 continue;
11402
11403 EnumDecl *FirstEnum = Merge.first;
11404 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11405
11406 using DeclHashes =
11407 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11408 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11409 DeclHashes &Hashes, EnumDecl *Enum) {
11410 for (auto *D : Enum->decls()) {
11411 // Due to decl merging, the first EnumDecl is the parent of
11412 // Decls in both records.
Weverythingd5f9c4a2020-06-19 18:35:36 -070011413 if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
Richard Trieuab4d7302018-07-25 22:52:05 +000011414 continue;
11415 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11416 Hashes.emplace_back(cast<EnumConstantDecl>(D),
11417 ComputeSubDeclODRHash(D));
11418 }
11419 };
11420 DeclHashes FirstHashes;
11421 PopulateHashes(FirstHashes, FirstEnum);
11422 bool Diagnosed = false;
11423 for (auto &SecondEnum : Merge.second) {
11424
11425 if (FirstEnum == SecondEnum)
11426 continue;
11427
11428 std::string SecondModule =
11429 getOwningModuleNameForDiagnostic(SecondEnum);
11430
11431 auto ODRDiagError = [FirstEnum, &FirstModule,
11432 this](SourceLocation Loc, SourceRange Range,
11433 ODREnumDifference DiffType) {
11434 return Diag(Loc, diag::err_module_odr_violation_enum)
11435 << FirstEnum << FirstModule.empty() << FirstModule << Range
11436 << DiffType;
11437 };
11438 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11439 SourceRange Range,
11440 ODREnumDifference DiffType) {
11441 return Diag(Loc, diag::note_module_odr_violation_enum)
11442 << SecondModule << Range << DiffType;
11443 };
11444
11445 if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11446 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11447 SingleScopedEnum)
11448 << FirstEnum->isScoped();
11449 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11450 SingleScopedEnum)
11451 << SecondEnum->isScoped();
11452 Diagnosed = true;
11453 continue;
11454 }
11455
11456 if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11457 if (FirstEnum->isScopedUsingClassTag() !=
11458 SecondEnum->isScopedUsingClassTag()) {
11459 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11460 EnumTagKeywordMismatch)
11461 << FirstEnum->isScopedUsingClassTag();
11462 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11463 EnumTagKeywordMismatch)
11464 << SecondEnum->isScopedUsingClassTag();
11465 Diagnosed = true;
11466 continue;
11467 }
11468 }
11469
11470 QualType FirstUnderlyingType =
11471 FirstEnum->getIntegerTypeSourceInfo()
11472 ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11473 : QualType();
11474 QualType SecondUnderlyingType =
11475 SecondEnum->getIntegerTypeSourceInfo()
11476 ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11477 : QualType();
11478 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11479 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11480 SingleSpecifiedType)
11481 << !FirstUnderlyingType.isNull();
11482 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11483 SingleSpecifiedType)
11484 << !SecondUnderlyingType.isNull();
11485 Diagnosed = true;
11486 continue;
11487 }
11488
11489 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11490 if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11491 ComputeQualTypeODRHash(SecondUnderlyingType)) {
11492 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11493 DifferentSpecifiedTypes)
11494 << FirstUnderlyingType;
11495 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11496 DifferentSpecifiedTypes)
11497 << SecondUnderlyingType;
11498 Diagnosed = true;
11499 continue;
11500 }
11501 }
11502
11503 DeclHashes SecondHashes;
11504 PopulateHashes(SecondHashes, SecondEnum);
11505
11506 if (FirstHashes.size() != SecondHashes.size()) {
11507 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11508 DifferentNumberEnumConstants)
11509 << (int)FirstHashes.size();
11510 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11511 DifferentNumberEnumConstants)
11512 << (int)SecondHashes.size();
11513 Diagnosed = true;
11514 continue;
11515 }
11516
11517 for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11518 if (FirstHashes[I].second == SecondHashes[I].second)
11519 continue;
11520 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11521 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11522
11523 if (FirstEnumConstant->getDeclName() !=
11524 SecondEnumConstant->getDeclName()) {
11525
11526 ODRDiagError(FirstEnumConstant->getLocation(),
11527 FirstEnumConstant->getSourceRange(), EnumConstantName)
11528 << I + 1 << FirstEnumConstant;
11529 ODRDiagNote(SecondEnumConstant->getLocation(),
11530 SecondEnumConstant->getSourceRange(), EnumConstantName)
11531 << I + 1 << SecondEnumConstant;
11532 Diagnosed = true;
11533 break;
11534 }
11535
11536 const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11537 const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11538 if (!FirstInit && !SecondInit)
11539 continue;
11540
11541 if (!FirstInit || !SecondInit) {
11542 ODRDiagError(FirstEnumConstant->getLocation(),
11543 FirstEnumConstant->getSourceRange(),
11544 EnumConstantSingleInitilizer)
11545 << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11546 ODRDiagNote(SecondEnumConstant->getLocation(),
11547 SecondEnumConstant->getSourceRange(),
11548 EnumConstantSingleInitilizer)
11549 << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11550 Diagnosed = true;
11551 break;
11552 }
11553
11554 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11555 ODRDiagError(FirstEnumConstant->getLocation(),
11556 FirstEnumConstant->getSourceRange(),
11557 EnumConstantDifferentInitilizer)
11558 << I + 1 << FirstEnumConstant;
11559 ODRDiagNote(SecondEnumConstant->getLocation(),
11560 SecondEnumConstant->getSourceRange(),
11561 EnumConstantDifferentInitilizer)
11562 << I + 1 << SecondEnumConstant;
11563 Diagnosed = true;
11564 break;
11565 }
11566 }
11567 }
11568
11569 (void)Diagnosed;
11570 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11571 }
Guy Benyei11169dd2012-12-18 14:30:41 +000011572}
11573
Richard Smithce18a182015-07-14 00:26:00 +000011574void ASTReader::StartedDeserializing() {
David L. Jonesc4808b9e2016-12-15 20:53:26 +000011575 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
Richard Smithce18a182015-07-14 00:26:00 +000011576 ReadTimer->startTimer();
11577}
11578
Guy Benyei11169dd2012-12-18 14:30:41 +000011579void ASTReader::FinishedDeserializing() {
11580 assert(NumCurrentElementsDeserializing &&
11581 "FinishedDeserializing not paired with StartedDeserializing");
11582 if (NumCurrentElementsDeserializing == 1) {
11583 // We decrease NumCurrentElementsDeserializing only after pending actions
11584 // are finished, to avoid recursively re-calling finishPendingActions().
11585 finishPendingActions();
11586 }
11587 --NumCurrentElementsDeserializing;
11588
Richard Smitha0ce9c42014-07-29 23:23:27 +000011589 if (NumCurrentElementsDeserializing == 0) {
Richard Smitha62d1982018-08-03 01:00:01 +000011590 // Propagate exception specification and deduced type updates along
11591 // redeclaration chains.
11592 //
11593 // We do this now rather than in finishPendingActions because we want to
11594 // be able to walk the complete redeclaration chains of the updated decls.
11595 while (!PendingExceptionSpecUpdates.empty() ||
11596 !PendingDeducedTypeUpdates.empty()) {
11597 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
Richard Smith7226f2a2015-03-23 19:54:56 +000011598 PendingExceptionSpecUpdates.clear();
Richard Smitha62d1982018-08-03 01:00:01 +000011599 for (auto Update : ESUpdates) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +000011600 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Richard Smith7226f2a2015-03-23 19:54:56 +000011601 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
Richard Smith1d0f1992015-08-19 21:09:32 +000011602 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
Richard Smithdbafb6c2017-06-29 23:23:46 +000011603 if (auto *Listener = getContext().getASTMutationListener())
Richard Smithd88a7f12015-09-01 20:35:42 +000011604 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
Richard Smith1d0f1992015-08-19 21:09:32 +000011605 for (auto *Redecl : Update.second->redecls())
Richard Smithdbafb6c2017-06-29 23:23:46 +000011606 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
Richard Smith7226f2a2015-03-23 19:54:56 +000011607 }
Richard Smitha62d1982018-08-03 01:00:01 +000011608
11609 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11610 PendingDeducedTypeUpdates.clear();
11611 for (auto Update : DTUpdates) {
11612 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11613 // FIXME: If the return type is already deduced, check that it matches.
11614 getContext().adjustDeducedFunctionResultType(Update.first,
11615 Update.second);
11616 }
Richard Smith9e2341d2015-03-23 03:25:59 +000011617 }
11618
Richard Smithce18a182015-07-14 00:26:00 +000011619 if (ReadTimer)
11620 ReadTimer->stopTimer();
11621
Richard Smith0f4e2c42015-08-06 04:23:48 +000011622 diagnoseOdrViolations();
11623
Richard Smith04d05b52014-03-23 00:27:18 +000011624 // We are not in recursive loading, so it's safe to pass the "interesting"
11625 // decls to the consumer.
Richard Smitha0ce9c42014-07-29 23:23:27 +000011626 if (Consumer)
11627 PassInterestingDeclsToConsumer();
Guy Benyei11169dd2012-12-18 14:30:41 +000011628 }
11629}
11630
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +000011631void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
Richard Smith9e2341d2015-03-23 03:25:59 +000011632 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11633 // Remove any fake results before adding any real ones.
11634 auto It = PendingFakeLookupResults.find(II);
11635 if (It != PendingFakeLookupResults.end()) {
Richard Smitha534a312015-07-21 23:54:07 +000011636 for (auto *ND : It->second)
Richard Smith9e2341d2015-03-23 03:25:59 +000011637 SemaObj->IdResolver.RemoveDecl(ND);
Ben Langmuireb8bd2d2015-04-10 22:25:42 +000011638 // FIXME: this works around module+PCH performance issue.
11639 // Rather than erase the result from the map, which is O(n), just clear
11640 // the vector of NamedDecls.
11641 It->second.clear();
Richard Smith9e2341d2015-03-23 03:25:59 +000011642 }
11643 }
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +000011644
11645 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11646 SemaObj->TUScope->AddDecl(D);
11647 } else if (SemaObj->TUScope) {
11648 // Adding the decl to IdResolver may have failed because it was already in
11649 // (even though it was not added in scope). If it is already in, make sure
11650 // it gets in the scope as well.
11651 if (std::find(SemaObj->IdResolver.begin(Name),
11652 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11653 SemaObj->TUScope->AddDecl(D);
11654 }
11655}
11656
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +000011657ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11658 ASTContext *Context,
David Blaikie61137e12017-01-05 18:23:18 +000011659 const PCHContainerReader &PCHContainerRdr,
11660 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11661 StringRef isysroot, bool DisableValidation,
11662 bool AllowASTWithCompilerErrors,
11663 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +000011664 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
David Blaikie61137e12017-01-05 18:23:18 +000011665 std::unique_ptr<llvm::Timer> ReadTimer)
11666 : Listener(DisableValidation
11667 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11668 : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
David Blaikie61137e12017-01-05 18:23:18 +000011669 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
David Blaikie9d7c1ba2017-01-05 18:45:43 +000011670 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +000011671 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11672 PCHContainerRdr, PP.getHeaderSearchInfo()),
11673 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
David Blaikie61137e12017-01-05 18:23:18 +000011674 DisableValidation(DisableValidation),
Nico Weber824285e2014-05-08 04:26:47 +000011675 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11676 AllowConfigurationMismatch(AllowConfigurationMismatch),
11677 ValidateSystemInputs(ValidateSystemInputs),
Bruno Cardoso Lopes1731fc82019-10-15 14:23:55 +000011678 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
David Blaikie9d7c1ba2017-01-05 18:45:43 +000011679 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
Guy Benyei11169dd2012-12-18 14:30:41 +000011680 SourceMgr.setExternalSLocEntrySource(this);
Douglas Gregor6623e1f2015-11-03 18:33:07 +000011681
11682 for (const auto &Ext : Extensions) {
11683 auto BlockName = Ext->getExtensionMetadata().BlockName;
11684 auto Known = ModuleFileExtensions.find(BlockName);
11685 if (Known != ModuleFileExtensions.end()) {
11686 Diags.Report(diag::warn_duplicate_module_file_extension)
11687 << BlockName;
11688 continue;
11689 }
11690
11691 ModuleFileExtensions.insert({BlockName, Ext});
11692 }
Guy Benyei11169dd2012-12-18 14:30:41 +000011693}
11694
11695ASTReader::~ASTReader() {
Nico Weber824285e2014-05-08 04:26:47 +000011696 if (OwnsDeserializationListener)
11697 delete DeserializationListener;
Guy Benyei11169dd2012-12-18 14:30:41 +000011698}
Richard Smith10379092016-05-06 23:14:07 +000011699
11700IdentifierResolver &ASTReader::getIdResolver() {
11701 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11702}
David L. Jonesbe1557a2016-12-21 00:17:49 +000011703
JF Bastien0e828952019-06-26 19:50:12 +000011704Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11705 unsigned AbbrevID) {
David L. Jonesbe1557a2016-12-21 00:17:49 +000011706 Idx = 0;
11707 Record.clear();
11708 return Cursor.readRecord(AbbrevID, Record);
11709}
Kelvin Libe286f52018-09-15 13:54:15 +000011710//===----------------------------------------------------------------------===//
11711//// OMPClauseReader implementation
11712////===----------------------------------------------------------------------===//
11713
John McCallc2f18312019-12-14 03:01:28 -050011714// This has to be in namespace clang because it's friended by all
11715// of the OMP clauses.
11716namespace clang {
11717
11718class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11719 ASTRecordReader &Record;
11720 ASTContext &Context;
11721
11722public:
11723 OMPClauseReader(ASTRecordReader &Record)
11724 : Record(Record), Context(Record.getContext()) {}
11725
Johannes Doerfert419a5592020-03-30 19:58:40 -050011726#define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11727#include "llvm/Frontend/OpenMP/OMPKinds.def"
John McCallc2f18312019-12-14 03:01:28 -050011728 OMPClause *readClause();
11729 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11730 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11731};
11732
11733} // end namespace clang
11734
11735OMPClause *ASTRecordReader::readOMPClause() {
11736 return OMPClauseReader(*this).readClause();
11737}
11738
Kelvin Libe286f52018-09-15 13:54:15 +000011739OMPClause *OMPClauseReader::readClause() {
Simon Pilgrim556fbfe2019-09-15 16:05:20 +000011740 OMPClause *C = nullptr;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011741 switch (llvm::omp::Clause(Record.readInt())) {
11742 case llvm::omp::OMPC_if:
Kelvin Libe286f52018-09-15 13:54:15 +000011743 C = new (Context) OMPIfClause();
11744 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011745 case llvm::omp::OMPC_final:
Kelvin Libe286f52018-09-15 13:54:15 +000011746 C = new (Context) OMPFinalClause();
11747 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011748 case llvm::omp::OMPC_num_threads:
Kelvin Libe286f52018-09-15 13:54:15 +000011749 C = new (Context) OMPNumThreadsClause();
11750 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011751 case llvm::omp::OMPC_safelen:
Kelvin Libe286f52018-09-15 13:54:15 +000011752 C = new (Context) OMPSafelenClause();
11753 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011754 case llvm::omp::OMPC_simdlen:
Kelvin Libe286f52018-09-15 13:54:15 +000011755 C = new (Context) OMPSimdlenClause();
11756 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011757 case llvm::omp::OMPC_allocator:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000011758 C = new (Context) OMPAllocatorClause();
11759 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011760 case llvm::omp::OMPC_collapse:
Kelvin Libe286f52018-09-15 13:54:15 +000011761 C = new (Context) OMPCollapseClause();
11762 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011763 case llvm::omp::OMPC_default:
Kelvin Libe286f52018-09-15 13:54:15 +000011764 C = new (Context) OMPDefaultClause();
11765 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011766 case llvm::omp::OMPC_proc_bind:
Kelvin Libe286f52018-09-15 13:54:15 +000011767 C = new (Context) OMPProcBindClause();
11768 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011769 case llvm::omp::OMPC_schedule:
Kelvin Libe286f52018-09-15 13:54:15 +000011770 C = new (Context) OMPScheduleClause();
11771 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011772 case llvm::omp::OMPC_ordered:
Kelvin Libe286f52018-09-15 13:54:15 +000011773 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11774 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011775 case llvm::omp::OMPC_nowait:
Kelvin Libe286f52018-09-15 13:54:15 +000011776 C = new (Context) OMPNowaitClause();
11777 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011778 case llvm::omp::OMPC_untied:
Kelvin Libe286f52018-09-15 13:54:15 +000011779 C = new (Context) OMPUntiedClause();
11780 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011781 case llvm::omp::OMPC_mergeable:
Kelvin Libe286f52018-09-15 13:54:15 +000011782 C = new (Context) OMPMergeableClause();
11783 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011784 case llvm::omp::OMPC_read:
Kelvin Libe286f52018-09-15 13:54:15 +000011785 C = new (Context) OMPReadClause();
11786 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011787 case llvm::omp::OMPC_write:
Kelvin Libe286f52018-09-15 13:54:15 +000011788 C = new (Context) OMPWriteClause();
11789 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011790 case llvm::omp::OMPC_update:
Alexey Bataev82f7c202020-03-03 13:22:35 -050011791 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
Kelvin Libe286f52018-09-15 13:54:15 +000011792 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011793 case llvm::omp::OMPC_capture:
Kelvin Libe286f52018-09-15 13:54:15 +000011794 C = new (Context) OMPCaptureClause();
11795 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011796 case llvm::omp::OMPC_seq_cst:
Kelvin Libe286f52018-09-15 13:54:15 +000011797 C = new (Context) OMPSeqCstClause();
11798 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011799 case llvm::omp::OMPC_acq_rel:
Alexey Bataevea9166b2020-02-06 16:30:23 -050011800 C = new (Context) OMPAcqRelClause();
11801 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011802 case llvm::omp::OMPC_acquire:
Alexey Bataev04a830f2020-02-10 14:30:39 -050011803 C = new (Context) OMPAcquireClause();
11804 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011805 case llvm::omp::OMPC_release:
Alexey Bataev95598342020-02-10 15:49:05 -050011806 C = new (Context) OMPReleaseClause();
11807 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011808 case llvm::omp::OMPC_relaxed:
Alexey Bataev9a8defc2020-02-11 11:10:43 -050011809 C = new (Context) OMPRelaxedClause();
11810 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011811 case llvm::omp::OMPC_threads:
Kelvin Libe286f52018-09-15 13:54:15 +000011812 C = new (Context) OMPThreadsClause();
11813 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011814 case llvm::omp::OMPC_simd:
Kelvin Libe286f52018-09-15 13:54:15 +000011815 C = new (Context) OMPSIMDClause();
11816 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011817 case llvm::omp::OMPC_nogroup:
Kelvin Libe286f52018-09-15 13:54:15 +000011818 C = new (Context) OMPNogroupClause();
11819 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011820 case llvm::omp::OMPC_unified_address:
Kelvin Li1408f912018-09-26 04:28:39 +000011821 C = new (Context) OMPUnifiedAddressClause();
11822 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011823 case llvm::omp::OMPC_unified_shared_memory:
Patrick Lyster4a370b92018-10-01 13:47:43 +000011824 C = new (Context) OMPUnifiedSharedMemoryClause();
11825 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011826 case llvm::omp::OMPC_reverse_offload:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000011827 C = new (Context) OMPReverseOffloadClause();
11828 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011829 case llvm::omp::OMPC_dynamic_allocators:
Patrick Lyster3fe9e392018-10-11 14:41:10 +000011830 C = new (Context) OMPDynamicAllocatorsClause();
11831 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011832 case llvm::omp::OMPC_atomic_default_mem_order:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000011833 C = new (Context) OMPAtomicDefaultMemOrderClause();
11834 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011835 case llvm::omp::OMPC_private:
Kelvin Libe286f52018-09-15 13:54:15 +000011836 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11837 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011838 case llvm::omp::OMPC_firstprivate:
Kelvin Libe286f52018-09-15 13:54:15 +000011839 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11840 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011841 case llvm::omp::OMPC_lastprivate:
Kelvin Libe286f52018-09-15 13:54:15 +000011842 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11843 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011844 case llvm::omp::OMPC_shared:
Kelvin Libe286f52018-09-15 13:54:15 +000011845 C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11846 break;
Alexey Bataevbd1c03d2020-05-04 16:19:31 -040011847 case llvm::omp::OMPC_reduction: {
11848 unsigned N = Record.readInt();
11849 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11850 C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
Kelvin Libe286f52018-09-15 13:54:15 +000011851 break;
Alexey Bataevbd1c03d2020-05-04 16:19:31 -040011852 }
Johannes Doerfert419a5592020-03-30 19:58:40 -050011853 case llvm::omp::OMPC_task_reduction:
Kelvin Libe286f52018-09-15 13:54:15 +000011854 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11855 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011856 case llvm::omp::OMPC_in_reduction:
Kelvin Libe286f52018-09-15 13:54:15 +000011857 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11858 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011859 case llvm::omp::OMPC_linear:
Kelvin Libe286f52018-09-15 13:54:15 +000011860 C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11861 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011862 case llvm::omp::OMPC_aligned:
Kelvin Libe286f52018-09-15 13:54:15 +000011863 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11864 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011865 case llvm::omp::OMPC_copyin:
Kelvin Libe286f52018-09-15 13:54:15 +000011866 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11867 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011868 case llvm::omp::OMPC_copyprivate:
Kelvin Libe286f52018-09-15 13:54:15 +000011869 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11870 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011871 case llvm::omp::OMPC_flush:
Kelvin Libe286f52018-09-15 13:54:15 +000011872 C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11873 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011874 case llvm::omp::OMPC_depobj:
Alexey Bataevc112e942020-02-28 09:52:15 -050011875 C = OMPDepobjClause::CreateEmpty(Context);
11876 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011877 case llvm::omp::OMPC_depend: {
Kelvin Libe286f52018-09-15 13:54:15 +000011878 unsigned NumVars = Record.readInt();
11879 unsigned NumLoops = Record.readInt();
11880 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11881 break;
11882 }
Johannes Doerfert419a5592020-03-30 19:58:40 -050011883 case llvm::omp::OMPC_device:
Kelvin Libe286f52018-09-15 13:54:15 +000011884 C = new (Context) OMPDeviceClause();
11885 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011886 case llvm::omp::OMPC_map: {
Michael Kruse4304e9d2019-02-19 16:38:20 +000011887 OMPMappableExprListSizeTy Sizes;
11888 Sizes.NumVars = Record.readInt();
11889 Sizes.NumUniqueDeclarations = Record.readInt();
11890 Sizes.NumComponentLists = Record.readInt();
11891 Sizes.NumComponents = Record.readInt();
11892 C = OMPMapClause::CreateEmpty(Context, Sizes);
Kelvin Libe286f52018-09-15 13:54:15 +000011893 break;
11894 }
Johannes Doerfert419a5592020-03-30 19:58:40 -050011895 case llvm::omp::OMPC_num_teams:
Kelvin Libe286f52018-09-15 13:54:15 +000011896 C = new (Context) OMPNumTeamsClause();
11897 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011898 case llvm::omp::OMPC_thread_limit:
Kelvin Libe286f52018-09-15 13:54:15 +000011899 C = new (Context) OMPThreadLimitClause();
11900 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011901 case llvm::omp::OMPC_priority:
Kelvin Libe286f52018-09-15 13:54:15 +000011902 C = new (Context) OMPPriorityClause();
11903 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011904 case llvm::omp::OMPC_grainsize:
Kelvin Libe286f52018-09-15 13:54:15 +000011905 C = new (Context) OMPGrainsizeClause();
11906 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011907 case llvm::omp::OMPC_num_tasks:
Kelvin Libe286f52018-09-15 13:54:15 +000011908 C = new (Context) OMPNumTasksClause();
11909 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011910 case llvm::omp::OMPC_hint:
Kelvin Libe286f52018-09-15 13:54:15 +000011911 C = new (Context) OMPHintClause();
11912 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011913 case llvm::omp::OMPC_dist_schedule:
Kelvin Libe286f52018-09-15 13:54:15 +000011914 C = new (Context) OMPDistScheduleClause();
11915 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011916 case llvm::omp::OMPC_defaultmap:
Kelvin Libe286f52018-09-15 13:54:15 +000011917 C = new (Context) OMPDefaultmapClause();
11918 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011919 case llvm::omp::OMPC_to: {
Michael Kruse4304e9d2019-02-19 16:38:20 +000011920 OMPMappableExprListSizeTy Sizes;
11921 Sizes.NumVars = Record.readInt();
11922 Sizes.NumUniqueDeclarations = Record.readInt();
11923 Sizes.NumComponentLists = Record.readInt();
11924 Sizes.NumComponents = Record.readInt();
11925 C = OMPToClause::CreateEmpty(Context, Sizes);
Kelvin Libe286f52018-09-15 13:54:15 +000011926 break;
11927 }
Johannes Doerfert419a5592020-03-30 19:58:40 -050011928 case llvm::omp::OMPC_from: {
Michael Kruse4304e9d2019-02-19 16:38:20 +000011929 OMPMappableExprListSizeTy Sizes;
11930 Sizes.NumVars = Record.readInt();
11931 Sizes.NumUniqueDeclarations = Record.readInt();
11932 Sizes.NumComponentLists = Record.readInt();
11933 Sizes.NumComponents = Record.readInt();
11934 C = OMPFromClause::CreateEmpty(Context, Sizes);
Kelvin Libe286f52018-09-15 13:54:15 +000011935 break;
11936 }
Johannes Doerfert419a5592020-03-30 19:58:40 -050011937 case llvm::omp::OMPC_use_device_ptr: {
Michael Kruse4304e9d2019-02-19 16:38:20 +000011938 OMPMappableExprListSizeTy Sizes;
11939 Sizes.NumVars = Record.readInt();
11940 Sizes.NumUniqueDeclarations = Record.readInt();
11941 Sizes.NumComponentLists = Record.readInt();
11942 Sizes.NumComponents = Record.readInt();
11943 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
Kelvin Libe286f52018-09-15 13:54:15 +000011944 break;
11945 }
Alexey Bataeva888fc62020-05-21 08:30:23 -040011946 case llvm::omp::OMPC_use_device_addr: {
11947 OMPMappableExprListSizeTy Sizes;
11948 Sizes.NumVars = Record.readInt();
11949 Sizes.NumUniqueDeclarations = Record.readInt();
11950 Sizes.NumComponentLists = Record.readInt();
11951 Sizes.NumComponents = Record.readInt();
11952 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11953 break;
11954 }
Johannes Doerfert419a5592020-03-30 19:58:40 -050011955 case llvm::omp::OMPC_is_device_ptr: {
Michael Kruse4304e9d2019-02-19 16:38:20 +000011956 OMPMappableExprListSizeTy Sizes;
11957 Sizes.NumVars = Record.readInt();
11958 Sizes.NumUniqueDeclarations = Record.readInt();
11959 Sizes.NumComponentLists = Record.readInt();
11960 Sizes.NumComponents = Record.readInt();
11961 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
Kelvin Libe286f52018-09-15 13:54:15 +000011962 break;
11963 }
Johannes Doerfert419a5592020-03-30 19:58:40 -050011964 case llvm::omp::OMPC_allocate:
Alexey Bataeve04483e2019-03-27 14:14:31 +000011965 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11966 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011967 case llvm::omp::OMPC_nontemporal:
Alexey Bataevb6e70842019-12-16 15:54:17 -050011968 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11969 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011970 case llvm::omp::OMPC_inclusive:
Alexey Bataev06dea732020-03-20 09:41:22 -040011971 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11972 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011973 case llvm::omp::OMPC_exclusive:
Alexey Bataev63828a32020-03-23 10:41:08 -040011974 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11975 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011976 case llvm::omp::OMPC_order:
Alexey Bataevcb8e6912020-01-31 16:09:26 -050011977 C = new (Context) OMPOrderClause();
11978 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011979 case llvm::omp::OMPC_destroy:
Alexey Bataev375437a2020-03-02 14:21:20 -050011980 C = new (Context) OMPDestroyClause();
11981 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011982 case llvm::omp::OMPC_detach:
Alexey Bataev0f0564b2020-03-17 09:17:42 -040011983 C = new (Context) OMPDetachClause();
11984 break;
Alexey Bataevb5be1c52020-04-21 13:21:00 -040011985 case llvm::omp::OMPC_uses_allocators:
11986 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11987 break;
Alexey Bataev2e499ee2020-05-18 13:37:53 -040011988 case llvm::omp::OMPC_affinity:
11989 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
11990 break;
Johannes Doerfert419a5592020-03-30 19:58:40 -050011991#define OMP_CLAUSE_NO_CLASS(Enum, Str) \
11992 case llvm::omp::Enum: \
11993 break;
11994#include "llvm/Frontend/OpenMP/OMPKinds.def"
Kelvin Libe286f52018-09-15 13:54:15 +000011995 }
Simon Pilgrim556fbfe2019-09-15 16:05:20 +000011996 assert(C && "Unknown OMPClause type");
11997
Kelvin Libe286f52018-09-15 13:54:15 +000011998 Visit(C);
11999 C->setLocStart(Record.readSourceLocation());
12000 C->setLocEnd(Record.readSourceLocation());
12001
12002 return C;
12003}
12004
12005void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12006 C->setPreInitStmt(Record.readSubStmt(),
12007 static_cast<OpenMPDirectiveKind>(Record.readInt()));
12008}
12009
12010void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12011 VisitOMPClauseWithPreInit(C);
12012 C->setPostUpdateExpr(Record.readSubExpr());
12013}
12014
12015void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12016 VisitOMPClauseWithPreInit(C);
12017 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12018 C->setNameModifierLoc(Record.readSourceLocation());
12019 C->setColonLoc(Record.readSourceLocation());
12020 C->setCondition(Record.readSubExpr());
12021 C->setLParenLoc(Record.readSourceLocation());
12022}
12023
12024void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
Alexey Bataev3a842ec2019-10-15 19:37:05 +000012025 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +000012026 C->setCondition(Record.readSubExpr());
12027 C->setLParenLoc(Record.readSourceLocation());
12028}
12029
12030void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12031 VisitOMPClauseWithPreInit(C);
12032 C->setNumThreads(Record.readSubExpr());
12033 C->setLParenLoc(Record.readSourceLocation());
12034}
12035
12036void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12037 C->setSafelen(Record.readSubExpr());
12038 C->setLParenLoc(Record.readSourceLocation());
12039}
12040
12041void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12042 C->setSimdlen(Record.readSubExpr());
12043 C->setLParenLoc(Record.readSourceLocation());
12044}
12045
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000012046void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12047 C->setAllocator(Record.readExpr());
12048 C->setLParenLoc(Record.readSourceLocation());
12049}
12050
Kelvin Libe286f52018-09-15 13:54:15 +000012051void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12052 C->setNumForLoops(Record.readSubExpr());
12053 C->setLParenLoc(Record.readSourceLocation());
12054}
12055
12056void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
Atmn Patel577c9b02020-02-14 21:45:49 -060012057 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
Kelvin Libe286f52018-09-15 13:54:15 +000012058 C->setLParenLoc(Record.readSourceLocation());
12059 C->setDefaultKindKwLoc(Record.readSourceLocation());
12060}
12061
12062void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -060012063 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
Kelvin Libe286f52018-09-15 13:54:15 +000012064 C->setLParenLoc(Record.readSourceLocation());
12065 C->setProcBindKindKwLoc(Record.readSourceLocation());
12066}
12067
12068void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12069 VisitOMPClauseWithPreInit(C);
12070 C->setScheduleKind(
12071 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12072 C->setFirstScheduleModifier(
12073 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12074 C->setSecondScheduleModifier(
12075 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12076 C->setChunkSize(Record.readSubExpr());
12077 C->setLParenLoc(Record.readSourceLocation());
12078 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12079 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12080 C->setScheduleKindLoc(Record.readSourceLocation());
12081 C->setCommaLoc(Record.readSourceLocation());
12082}
12083
12084void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12085 C->setNumForLoops(Record.readSubExpr());
12086 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12087 C->setLoopNumIterations(I, Record.readSubExpr());
12088 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12089 C->setLoopCounter(I, Record.readSubExpr());
12090 C->setLParenLoc(Record.readSourceLocation());
12091}
12092
Alexey Bataev0f0564b2020-03-17 09:17:42 -040012093void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12094 C->setEventHandler(Record.readSubExpr());
12095 C->setLParenLoc(Record.readSourceLocation());
12096}
12097
Kelvin Libe286f52018-09-15 13:54:15 +000012098void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12099
12100void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12101
12102void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12103
12104void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12105
12106void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12107
Alexey Bataev82f7c202020-03-03 13:22:35 -050012108void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12109 if (C->isExtended()) {
12110 C->setLParenLoc(Record.readSourceLocation());
12111 C->setArgumentLoc(Record.readSourceLocation());
12112 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12113 }
12114}
Kelvin Libe286f52018-09-15 13:54:15 +000012115
12116void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12117
12118void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12119
Alexey Bataevea9166b2020-02-06 16:30:23 -050012120void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12121
Alexey Bataev04a830f2020-02-10 14:30:39 -050012122void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12123
Alexey Bataev95598342020-02-10 15:49:05 -050012124void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12125
Alexey Bataev9a8defc2020-02-11 11:10:43 -050012126void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12127
Kelvin Libe286f52018-09-15 13:54:15 +000012128void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12129
12130void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12131
12132void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12133
Alexey Bataev375437a2020-03-02 14:21:20 -050012134void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {}
12135
Kelvin Li1408f912018-09-26 04:28:39 +000012136void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12137
Patrick Lyster4a370b92018-10-01 13:47:43 +000012138void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12139 OMPUnifiedSharedMemoryClause *) {}
12140
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000012141void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12142
Patrick Lyster3fe9e392018-10-11 14:41:10 +000012143void
12144OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12145}
12146
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000012147void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12148 OMPAtomicDefaultMemOrderClause *C) {
12149 C->setAtomicDefaultMemOrderKind(
12150 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12151 C->setLParenLoc(Record.readSourceLocation());
12152 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12153}
12154
Kelvin Libe286f52018-09-15 13:54:15 +000012155void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12156 C->setLParenLoc(Record.readSourceLocation());
12157 unsigned NumVars = C->varlist_size();
12158 SmallVector<Expr *, 16> Vars;
12159 Vars.reserve(NumVars);
12160 for (unsigned i = 0; i != NumVars; ++i)
12161 Vars.push_back(Record.readSubExpr());
12162 C->setVarRefs(Vars);
12163 Vars.clear();
12164 for (unsigned i = 0; i != NumVars; ++i)
12165 Vars.push_back(Record.readSubExpr());
12166 C->setPrivateCopies(Vars);
12167}
12168
12169void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12170 VisitOMPClauseWithPreInit(C);
12171 C->setLParenLoc(Record.readSourceLocation());
12172 unsigned NumVars = C->varlist_size();
12173 SmallVector<Expr *, 16> Vars;
12174 Vars.reserve(NumVars);
12175 for (unsigned i = 0; i != NumVars; ++i)
12176 Vars.push_back(Record.readSubExpr());
12177 C->setVarRefs(Vars);
12178 Vars.clear();
12179 for (unsigned i = 0; i != NumVars; ++i)
12180 Vars.push_back(Record.readSubExpr());
12181 C->setPrivateCopies(Vars);
12182 Vars.clear();
12183 for (unsigned i = 0; i != NumVars; ++i)
12184 Vars.push_back(Record.readSubExpr());
12185 C->setInits(Vars);
12186}
12187
12188void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12189 VisitOMPClauseWithPostUpdate(C);
12190 C->setLParenLoc(Record.readSourceLocation());
Alexey Bataev93dc40d2019-12-20 11:04:57 -050012191 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12192 C->setKindLoc(Record.readSourceLocation());
12193 C->setColonLoc(Record.readSourceLocation());
Kelvin Libe286f52018-09-15 13:54:15 +000012194 unsigned NumVars = C->varlist_size();
12195 SmallVector<Expr *, 16> Vars;
12196 Vars.reserve(NumVars);
12197 for (unsigned i = 0; i != NumVars; ++i)
12198 Vars.push_back(Record.readSubExpr());
12199 C->setVarRefs(Vars);
12200 Vars.clear();
12201 for (unsigned i = 0; i != NumVars; ++i)
12202 Vars.push_back(Record.readSubExpr());
12203 C->setPrivateCopies(Vars);
12204 Vars.clear();
12205 for (unsigned i = 0; i != NumVars; ++i)
12206 Vars.push_back(Record.readSubExpr());
12207 C->setSourceExprs(Vars);
12208 Vars.clear();
12209 for (unsigned i = 0; i != NumVars; ++i)
12210 Vars.push_back(Record.readSubExpr());
12211 C->setDestinationExprs(Vars);
12212 Vars.clear();
12213 for (unsigned i = 0; i != NumVars; ++i)
12214 Vars.push_back(Record.readSubExpr());
12215 C->setAssignmentOps(Vars);
12216}
12217
12218void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12219 C->setLParenLoc(Record.readSourceLocation());
12220 unsigned NumVars = C->varlist_size();
12221 SmallVector<Expr *, 16> Vars;
12222 Vars.reserve(NumVars);
12223 for (unsigned i = 0; i != NumVars; ++i)
12224 Vars.push_back(Record.readSubExpr());
12225 C->setVarRefs(Vars);
12226}
12227
12228void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12229 VisitOMPClauseWithPostUpdate(C);
12230 C->setLParenLoc(Record.readSourceLocation());
Alexey Bataev1236eb62020-03-23 17:30:38 -040012231 C->setModifierLoc(Record.readSourceLocation());
Kelvin Libe286f52018-09-15 13:54:15 +000012232 C->setColonLoc(Record.readSourceLocation());
12233 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
John McCall3ce3d232019-12-13 03:37:23 -050012234 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
Kelvin Libe286f52018-09-15 13:54:15 +000012235 C->setQualifierLoc(NNSL);
12236 C->setNameInfo(DNI);
12237
12238 unsigned NumVars = C->varlist_size();
12239 SmallVector<Expr *, 16> Vars;
12240 Vars.reserve(NumVars);
12241 for (unsigned i = 0; i != NumVars; ++i)
12242 Vars.push_back(Record.readSubExpr());
12243 C->setVarRefs(Vars);
12244 Vars.clear();
12245 for (unsigned i = 0; i != NumVars; ++i)
12246 Vars.push_back(Record.readSubExpr());
12247 C->setPrivates(Vars);
12248 Vars.clear();
12249 for (unsigned i = 0; i != NumVars; ++i)
12250 Vars.push_back(Record.readSubExpr());
12251 C->setLHSExprs(Vars);
12252 Vars.clear();
12253 for (unsigned i = 0; i != NumVars; ++i)
12254 Vars.push_back(Record.readSubExpr());
12255 C->setRHSExprs(Vars);
12256 Vars.clear();
12257 for (unsigned i = 0; i != NumVars; ++i)
12258 Vars.push_back(Record.readSubExpr());
12259 C->setReductionOps(Vars);
Alexey Bataevbd1c03d2020-05-04 16:19:31 -040012260 if (C->getModifier() == OMPC_REDUCTION_inscan) {
12261 Vars.clear();
12262 for (unsigned i = 0; i != NumVars; ++i)
12263 Vars.push_back(Record.readSubExpr());
12264 C->setInscanCopyOps(Vars);
12265 Vars.clear();
12266 for (unsigned i = 0; i != NumVars; ++i)
12267 Vars.push_back(Record.readSubExpr());
12268 C->setInscanCopyArrayTemps(Vars);
12269 Vars.clear();
12270 for (unsigned i = 0; i != NumVars; ++i)
12271 Vars.push_back(Record.readSubExpr());
12272 C->setInscanCopyArrayElems(Vars);
12273 }
Kelvin Libe286f52018-09-15 13:54:15 +000012274}
12275
12276void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12277 VisitOMPClauseWithPostUpdate(C);
12278 C->setLParenLoc(Record.readSourceLocation());
12279 C->setColonLoc(Record.readSourceLocation());
12280 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
John McCall3ce3d232019-12-13 03:37:23 -050012281 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
Kelvin Libe286f52018-09-15 13:54:15 +000012282 C->setQualifierLoc(NNSL);
12283 C->setNameInfo(DNI);
12284
12285 unsigned NumVars = C->varlist_size();
12286 SmallVector<Expr *, 16> Vars;
12287 Vars.reserve(NumVars);
12288 for (unsigned I = 0; I != NumVars; ++I)
12289 Vars.push_back(Record.readSubExpr());
12290 C->setVarRefs(Vars);
12291 Vars.clear();
12292 for (unsigned I = 0; I != NumVars; ++I)
12293 Vars.push_back(Record.readSubExpr());
12294 C->setPrivates(Vars);
12295 Vars.clear();
12296 for (unsigned I = 0; I != NumVars; ++I)
12297 Vars.push_back(Record.readSubExpr());
12298 C->setLHSExprs(Vars);
12299 Vars.clear();
12300 for (unsigned I = 0; I != NumVars; ++I)
12301 Vars.push_back(Record.readSubExpr());
12302 C->setRHSExprs(Vars);
12303 Vars.clear();
12304 for (unsigned I = 0; I != NumVars; ++I)
12305 Vars.push_back(Record.readSubExpr());
12306 C->setReductionOps(Vars);
12307}
12308
12309void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12310 VisitOMPClauseWithPostUpdate(C);
12311 C->setLParenLoc(Record.readSourceLocation());
12312 C->setColonLoc(Record.readSourceLocation());
12313 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
John McCall3ce3d232019-12-13 03:37:23 -050012314 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
Kelvin Libe286f52018-09-15 13:54:15 +000012315 C->setQualifierLoc(NNSL);
12316 C->setNameInfo(DNI);
12317
12318 unsigned NumVars = C->varlist_size();
12319 SmallVector<Expr *, 16> Vars;
12320 Vars.reserve(NumVars);
12321 for (unsigned I = 0; I != NumVars; ++I)
12322 Vars.push_back(Record.readSubExpr());
12323 C->setVarRefs(Vars);
12324 Vars.clear();
12325 for (unsigned I = 0; I != NumVars; ++I)
12326 Vars.push_back(Record.readSubExpr());
12327 C->setPrivates(Vars);
12328 Vars.clear();
12329 for (unsigned I = 0; I != NumVars; ++I)
12330 Vars.push_back(Record.readSubExpr());
12331 C->setLHSExprs(Vars);
12332 Vars.clear();
12333 for (unsigned I = 0; I != NumVars; ++I)
12334 Vars.push_back(Record.readSubExpr());
12335 C->setRHSExprs(Vars);
12336 Vars.clear();
12337 for (unsigned I = 0; I != NumVars; ++I)
12338 Vars.push_back(Record.readSubExpr());
12339 C->setReductionOps(Vars);
12340 Vars.clear();
12341 for (unsigned I = 0; I != NumVars; ++I)
12342 Vars.push_back(Record.readSubExpr());
12343 C->setTaskgroupDescriptors(Vars);
12344}
12345
12346void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12347 VisitOMPClauseWithPostUpdate(C);
12348 C->setLParenLoc(Record.readSourceLocation());
12349 C->setColonLoc(Record.readSourceLocation());
12350 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12351 C->setModifierLoc(Record.readSourceLocation());
12352 unsigned NumVars = C->varlist_size();
12353 SmallVector<Expr *, 16> Vars;
12354 Vars.reserve(NumVars);
12355 for (unsigned i = 0; i != NumVars; ++i)
12356 Vars.push_back(Record.readSubExpr());
12357 C->setVarRefs(Vars);
12358 Vars.clear();
12359 for (unsigned i = 0; i != NumVars; ++i)
12360 Vars.push_back(Record.readSubExpr());
12361 C->setPrivates(Vars);
12362 Vars.clear();
12363 for (unsigned i = 0; i != NumVars; ++i)
12364 Vars.push_back(Record.readSubExpr());
12365 C->setInits(Vars);
12366 Vars.clear();
12367 for (unsigned i = 0; i != NumVars; ++i)
12368 Vars.push_back(Record.readSubExpr());
12369 C->setUpdates(Vars);
12370 Vars.clear();
12371 for (unsigned i = 0; i != NumVars; ++i)
12372 Vars.push_back(Record.readSubExpr());
12373 C->setFinals(Vars);
12374 C->setStep(Record.readSubExpr());
12375 C->setCalcStep(Record.readSubExpr());
Alexey Bataev195ae902019-08-08 13:42:45 +000012376 Vars.clear();
12377 for (unsigned I = 0; I != NumVars + 1; ++I)
12378 Vars.push_back(Record.readSubExpr());
12379 C->setUsedExprs(Vars);
Kelvin Libe286f52018-09-15 13:54:15 +000012380}
12381
12382void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12383 C->setLParenLoc(Record.readSourceLocation());
12384 C->setColonLoc(Record.readSourceLocation());
12385 unsigned NumVars = C->varlist_size();
12386 SmallVector<Expr *, 16> Vars;
12387 Vars.reserve(NumVars);
12388 for (unsigned i = 0; i != NumVars; ++i)
12389 Vars.push_back(Record.readSubExpr());
12390 C->setVarRefs(Vars);
12391 C->setAlignment(Record.readSubExpr());
12392}
12393
12394void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12395 C->setLParenLoc(Record.readSourceLocation());
12396 unsigned NumVars = C->varlist_size();
12397 SmallVector<Expr *, 16> Exprs;
12398 Exprs.reserve(NumVars);
12399 for (unsigned i = 0; i != NumVars; ++i)
12400 Exprs.push_back(Record.readSubExpr());
12401 C->setVarRefs(Exprs);
12402 Exprs.clear();
12403 for (unsigned i = 0; i != NumVars; ++i)
12404 Exprs.push_back(Record.readSubExpr());
12405 C->setSourceExprs(Exprs);
12406 Exprs.clear();
12407 for (unsigned i = 0; i != NumVars; ++i)
12408 Exprs.push_back(Record.readSubExpr());
12409 C->setDestinationExprs(Exprs);
12410 Exprs.clear();
12411 for (unsigned i = 0; i != NumVars; ++i)
12412 Exprs.push_back(Record.readSubExpr());
12413 C->setAssignmentOps(Exprs);
12414}
12415
12416void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12417 C->setLParenLoc(Record.readSourceLocation());
12418 unsigned NumVars = C->varlist_size();
12419 SmallVector<Expr *, 16> Exprs;
12420 Exprs.reserve(NumVars);
12421 for (unsigned i = 0; i != NumVars; ++i)
12422 Exprs.push_back(Record.readSubExpr());
12423 C->setVarRefs(Exprs);
12424 Exprs.clear();
12425 for (unsigned i = 0; i != NumVars; ++i)
12426 Exprs.push_back(Record.readSubExpr());
12427 C->setSourceExprs(Exprs);
12428 Exprs.clear();
12429 for (unsigned i = 0; i != NumVars; ++i)
12430 Exprs.push_back(Record.readSubExpr());
12431 C->setDestinationExprs(Exprs);
12432 Exprs.clear();
12433 for (unsigned i = 0; i != NumVars; ++i)
12434 Exprs.push_back(Record.readSubExpr());
12435 C->setAssignmentOps(Exprs);
12436}
12437
12438void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12439 C->setLParenLoc(Record.readSourceLocation());
12440 unsigned NumVars = C->varlist_size();
12441 SmallVector<Expr *, 16> Vars;
12442 Vars.reserve(NumVars);
12443 for (unsigned i = 0; i != NumVars; ++i)
12444 Vars.push_back(Record.readSubExpr());
12445 C->setVarRefs(Vars);
12446}
12447
Alexey Bataevc112e942020-02-28 09:52:15 -050012448void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12449 C->setDepobj(Record.readSubExpr());
12450 C->setLParenLoc(Record.readSourceLocation());
12451}
12452
Kelvin Libe286f52018-09-15 13:54:15 +000012453void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12454 C->setLParenLoc(Record.readSourceLocation());
Alexey Bataev13a15042020-04-01 15:06:38 -040012455 C->setModifier(Record.readSubExpr());
Kelvin Libe286f52018-09-15 13:54:15 +000012456 C->setDependencyKind(
12457 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12458 C->setDependencyLoc(Record.readSourceLocation());
12459 C->setColonLoc(Record.readSourceLocation());
12460 unsigned NumVars = C->varlist_size();
12461 SmallVector<Expr *, 16> Vars;
12462 Vars.reserve(NumVars);
12463 for (unsigned I = 0; I != NumVars; ++I)
12464 Vars.push_back(Record.readSubExpr());
12465 C->setVarRefs(Vars);
12466 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12467 C->setLoopData(I, Record.readSubExpr());
12468}
12469
12470void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12471 VisitOMPClauseWithPreInit(C);
Alexey Bataev2f8894a2020-03-18 15:01:15 -040012472 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
Kelvin Libe286f52018-09-15 13:54:15 +000012473 C->setDevice(Record.readSubExpr());
Alexey Bataev2f8894a2020-03-18 15:01:15 -040012474 C->setModifierLoc(Record.readSourceLocation());
Kelvin Libe286f52018-09-15 13:54:15 +000012475 C->setLParenLoc(Record.readSourceLocation());
12476}
12477
12478void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12479 C->setLParenLoc(Record.readSourceLocation());
Reid Klecknerba1ffd22020-04-03 12:35:30 -070012480 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
Kelvin Lief579432018-12-18 22:18:41 +000012481 C->setMapTypeModifier(
12482 I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12483 C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12484 }
Michael Kruse4304e9d2019-02-19 16:38:20 +000012485 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
John McCall3ce3d232019-12-13 03:37:23 -050012486 C->setMapperIdInfo(Record.readDeclarationNameInfo());
Kelvin Libe286f52018-09-15 13:54:15 +000012487 C->setMapType(
12488 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12489 C->setMapLoc(Record.readSourceLocation());
12490 C->setColonLoc(Record.readSourceLocation());
12491 auto NumVars = C->varlist_size();
12492 auto UniqueDecls = C->getUniqueDeclarationsNum();
12493 auto TotalLists = C->getTotalComponentListNum();
12494 auto TotalComponents = C->getTotalComponentsNum();
12495
12496 SmallVector<Expr *, 16> Vars;
12497 Vars.reserve(NumVars);
12498 for (unsigned i = 0; i != NumVars; ++i)
Michael Kruse251e1482019-02-01 20:25:04 +000012499 Vars.push_back(Record.readExpr());
Kelvin Libe286f52018-09-15 13:54:15 +000012500 C->setVarRefs(Vars);
12501
Michael Kruse4304e9d2019-02-19 16:38:20 +000012502 SmallVector<Expr *, 16> UDMappers;
12503 UDMappers.reserve(NumVars);
12504 for (unsigned I = 0; I < NumVars; ++I)
12505 UDMappers.push_back(Record.readExpr());
12506 C->setUDMapperRefs(UDMappers);
12507
Kelvin Libe286f52018-09-15 13:54:15 +000012508 SmallVector<ValueDecl *, 16> Decls;
12509 Decls.reserve(UniqueDecls);
12510 for (unsigned i = 0; i < UniqueDecls; ++i)
12511 Decls.push_back(Record.readDeclAs<ValueDecl>());
12512 C->setUniqueDecls(Decls);
12513
12514 SmallVector<unsigned, 16> ListsPerDecl;
12515 ListsPerDecl.reserve(UniqueDecls);
12516 for (unsigned i = 0; i < UniqueDecls; ++i)
12517 ListsPerDecl.push_back(Record.readInt());
12518 C->setDeclNumLists(ListsPerDecl);
12519
12520 SmallVector<unsigned, 32> ListSizes;
12521 ListSizes.reserve(TotalLists);
12522 for (unsigned i = 0; i < TotalLists; ++i)
12523 ListSizes.push_back(Record.readInt());
12524 C->setComponentListSizes(ListSizes);
12525
12526 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12527 Components.reserve(TotalComponents);
12528 for (unsigned i = 0; i < TotalComponents; ++i) {
Michael Kruse251e1482019-02-01 20:25:04 +000012529 Expr *AssociatedExpr = Record.readExpr();
Kelvin Libe286f52018-09-15 13:54:15 +000012530 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12531 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12532 AssociatedExpr, AssociatedDecl));
12533 }
12534 C->setComponents(Components, ListSizes);
12535}
12536
Alexey Bataeve04483e2019-03-27 14:14:31 +000012537void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12538 C->setLParenLoc(Record.readSourceLocation());
12539 C->setColonLoc(Record.readSourceLocation());
12540 C->setAllocator(Record.readSubExpr());
12541 unsigned NumVars = C->varlist_size();
12542 SmallVector<Expr *, 16> Vars;
12543 Vars.reserve(NumVars);
12544 for (unsigned i = 0; i != NumVars; ++i)
12545 Vars.push_back(Record.readSubExpr());
12546 C->setVarRefs(Vars);
12547}
12548
Kelvin Libe286f52018-09-15 13:54:15 +000012549void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12550 VisitOMPClauseWithPreInit(C);
12551 C->setNumTeams(Record.readSubExpr());
12552 C->setLParenLoc(Record.readSourceLocation());
12553}
12554
12555void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12556 VisitOMPClauseWithPreInit(C);
12557 C->setThreadLimit(Record.readSubExpr());
12558 C->setLParenLoc(Record.readSourceLocation());
12559}
12560
12561void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
Alexey Bataev31ba4762019-10-16 18:09:37 +000012562 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +000012563 C->setPriority(Record.readSubExpr());
12564 C->setLParenLoc(Record.readSourceLocation());
12565}
12566
12567void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
Alexey Bataevb9c55e22019-10-14 19:29:52 +000012568 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +000012569 C->setGrainsize(Record.readSubExpr());
12570 C->setLParenLoc(Record.readSourceLocation());
12571}
12572
12573void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
Alexey Bataevd88c7de2019-10-14 20:44:34 +000012574 VisitOMPClauseWithPreInit(C);
Kelvin Libe286f52018-09-15 13:54:15 +000012575 C->setNumTasks(Record.readSubExpr());
12576 C->setLParenLoc(Record.readSourceLocation());
12577}
12578
12579void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12580 C->setHint(Record.readSubExpr());
12581 C->setLParenLoc(Record.readSourceLocation());
12582}
12583
12584void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12585 VisitOMPClauseWithPreInit(C);
12586 C->setDistScheduleKind(
12587 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12588 C->setChunkSize(Record.readSubExpr());
12589 C->setLParenLoc(Record.readSourceLocation());
12590 C->setDistScheduleKindLoc(Record.readSourceLocation());
12591 C->setCommaLoc(Record.readSourceLocation());
12592}
12593
12594void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12595 C->setDefaultmapKind(
12596 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12597 C->setDefaultmapModifier(
12598 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12599 C->setLParenLoc(Record.readSourceLocation());
12600 C->setDefaultmapModifierLoc(Record.readSourceLocation());
12601 C->setDefaultmapKindLoc(Record.readSourceLocation());
12602}
12603
12604void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12605 C->setLParenLoc(Record.readSourceLocation());
Michael Kruse01f670d2019-02-22 22:29:42 +000012606 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
John McCall3ce3d232019-12-13 03:37:23 -050012607 C->setMapperIdInfo(Record.readDeclarationNameInfo());
Kelvin Libe286f52018-09-15 13:54:15 +000012608 auto NumVars = C->varlist_size();
12609 auto UniqueDecls = C->getUniqueDeclarationsNum();
12610 auto TotalLists = C->getTotalComponentListNum();
12611 auto TotalComponents = C->getTotalComponentsNum();
12612
12613 SmallVector<Expr *, 16> Vars;
12614 Vars.reserve(NumVars);
12615 for (unsigned i = 0; i != NumVars; ++i)
12616 Vars.push_back(Record.readSubExpr());
12617 C->setVarRefs(Vars);
12618
Michael Kruse01f670d2019-02-22 22:29:42 +000012619 SmallVector<Expr *, 16> UDMappers;
12620 UDMappers.reserve(NumVars);
12621 for (unsigned I = 0; I < NumVars; ++I)
12622 UDMappers.push_back(Record.readSubExpr());
12623 C->setUDMapperRefs(UDMappers);
12624
Kelvin Libe286f52018-09-15 13:54:15 +000012625 SmallVector<ValueDecl *, 16> Decls;
12626 Decls.reserve(UniqueDecls);
12627 for (unsigned i = 0; i < UniqueDecls; ++i)
12628 Decls.push_back(Record.readDeclAs<ValueDecl>());
12629 C->setUniqueDecls(Decls);
12630
12631 SmallVector<unsigned, 16> ListsPerDecl;
12632 ListsPerDecl.reserve(UniqueDecls);
12633 for (unsigned i = 0; i < UniqueDecls; ++i)
12634 ListsPerDecl.push_back(Record.readInt());
12635 C->setDeclNumLists(ListsPerDecl);
12636
12637 SmallVector<unsigned, 32> ListSizes;
12638 ListSizes.reserve(TotalLists);
12639 for (unsigned i = 0; i < TotalLists; ++i)
12640 ListSizes.push_back(Record.readInt());
12641 C->setComponentListSizes(ListSizes);
12642
12643 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12644 Components.reserve(TotalComponents);
12645 for (unsigned i = 0; i < TotalComponents; ++i) {
12646 Expr *AssociatedExpr = Record.readSubExpr();
12647 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12648 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12649 AssociatedExpr, AssociatedDecl));
12650 }
12651 C->setComponents(Components, ListSizes);
12652}
12653
12654void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12655 C->setLParenLoc(Record.readSourceLocation());
Michael Kruse0336c752019-02-25 20:34:15 +000012656 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
John McCall3ce3d232019-12-13 03:37:23 -050012657 C->setMapperIdInfo(Record.readDeclarationNameInfo());
Kelvin Libe286f52018-09-15 13:54:15 +000012658 auto NumVars = C->varlist_size();
12659 auto UniqueDecls = C->getUniqueDeclarationsNum();
12660 auto TotalLists = C->getTotalComponentListNum();
12661 auto TotalComponents = C->getTotalComponentsNum();
12662
12663 SmallVector<Expr *, 16> Vars;
12664 Vars.reserve(NumVars);
12665 for (unsigned i = 0; i != NumVars; ++i)
12666 Vars.push_back(Record.readSubExpr());
12667 C->setVarRefs(Vars);
12668
Michael Kruse0336c752019-02-25 20:34:15 +000012669 SmallVector<Expr *, 16> UDMappers;
12670 UDMappers.reserve(NumVars);
12671 for (unsigned I = 0; I < NumVars; ++I)
12672 UDMappers.push_back(Record.readSubExpr());
12673 C->setUDMapperRefs(UDMappers);
12674
Kelvin Libe286f52018-09-15 13:54:15 +000012675 SmallVector<ValueDecl *, 16> Decls;
12676 Decls.reserve(UniqueDecls);
12677 for (unsigned i = 0; i < UniqueDecls; ++i)
12678 Decls.push_back(Record.readDeclAs<ValueDecl>());
12679 C->setUniqueDecls(Decls);
12680
12681 SmallVector<unsigned, 16> ListsPerDecl;
12682 ListsPerDecl.reserve(UniqueDecls);
12683 for (unsigned i = 0; i < UniqueDecls; ++i)
12684 ListsPerDecl.push_back(Record.readInt());
12685 C->setDeclNumLists(ListsPerDecl);
12686
12687 SmallVector<unsigned, 32> ListSizes;
12688 ListSizes.reserve(TotalLists);
12689 for (unsigned i = 0; i < TotalLists; ++i)
12690 ListSizes.push_back(Record.readInt());
12691 C->setComponentListSizes(ListSizes);
12692
12693 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12694 Components.reserve(TotalComponents);
12695 for (unsigned i = 0; i < TotalComponents; ++i) {
12696 Expr *AssociatedExpr = Record.readSubExpr();
12697 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12698 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12699 AssociatedExpr, AssociatedDecl));
12700 }
12701 C->setComponents(Components, ListSizes);
12702}
12703
12704void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12705 C->setLParenLoc(Record.readSourceLocation());
12706 auto NumVars = C->varlist_size();
12707 auto UniqueDecls = C->getUniqueDeclarationsNum();
12708 auto TotalLists = C->getTotalComponentListNum();
12709 auto TotalComponents = C->getTotalComponentsNum();
12710
12711 SmallVector<Expr *, 16> Vars;
12712 Vars.reserve(NumVars);
12713 for (unsigned i = 0; i != NumVars; ++i)
12714 Vars.push_back(Record.readSubExpr());
12715 C->setVarRefs(Vars);
12716 Vars.clear();
12717 for (unsigned i = 0; i != NumVars; ++i)
12718 Vars.push_back(Record.readSubExpr());
12719 C->setPrivateCopies(Vars);
12720 Vars.clear();
12721 for (unsigned i = 0; i != NumVars; ++i)
12722 Vars.push_back(Record.readSubExpr());
12723 C->setInits(Vars);
12724
12725 SmallVector<ValueDecl *, 16> Decls;
12726 Decls.reserve(UniqueDecls);
12727 for (unsigned i = 0; i < UniqueDecls; ++i)
12728 Decls.push_back(Record.readDeclAs<ValueDecl>());
12729 C->setUniqueDecls(Decls);
12730
12731 SmallVector<unsigned, 16> ListsPerDecl;
12732 ListsPerDecl.reserve(UniqueDecls);
12733 for (unsigned i = 0; i < UniqueDecls; ++i)
12734 ListsPerDecl.push_back(Record.readInt());
12735 C->setDeclNumLists(ListsPerDecl);
12736
12737 SmallVector<unsigned, 32> ListSizes;
12738 ListSizes.reserve(TotalLists);
12739 for (unsigned i = 0; i < TotalLists; ++i)
12740 ListSizes.push_back(Record.readInt());
12741 C->setComponentListSizes(ListSizes);
12742
12743 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12744 Components.reserve(TotalComponents);
12745 for (unsigned i = 0; i < TotalComponents; ++i) {
12746 Expr *AssociatedExpr = Record.readSubExpr();
12747 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12748 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12749 AssociatedExpr, AssociatedDecl));
12750 }
12751 C->setComponents(Components, ListSizes);
12752}
12753
Alexey Bataeva888fc62020-05-21 08:30:23 -040012754void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12755 C->setLParenLoc(Record.readSourceLocation());
12756 auto NumVars = C->varlist_size();
12757 auto UniqueDecls = C->getUniqueDeclarationsNum();
12758 auto TotalLists = C->getTotalComponentListNum();
12759 auto TotalComponents = C->getTotalComponentsNum();
12760
12761 SmallVector<Expr *, 16> Vars;
12762 Vars.reserve(NumVars);
12763 for (unsigned i = 0; i != NumVars; ++i)
12764 Vars.push_back(Record.readSubExpr());
12765 C->setVarRefs(Vars);
12766
12767 SmallVector<ValueDecl *, 16> Decls;
12768 Decls.reserve(UniqueDecls);
12769 for (unsigned i = 0; i < UniqueDecls; ++i)
12770 Decls.push_back(Record.readDeclAs<ValueDecl>());
12771 C->setUniqueDecls(Decls);
12772
12773 SmallVector<unsigned, 16> ListsPerDecl;
12774 ListsPerDecl.reserve(UniqueDecls);
12775 for (unsigned i = 0; i < UniqueDecls; ++i)
12776 ListsPerDecl.push_back(Record.readInt());
12777 C->setDeclNumLists(ListsPerDecl);
12778
12779 SmallVector<unsigned, 32> ListSizes;
12780 ListSizes.reserve(TotalLists);
12781 for (unsigned i = 0; i < TotalLists; ++i)
12782 ListSizes.push_back(Record.readInt());
12783 C->setComponentListSizes(ListSizes);
12784
12785 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12786 Components.reserve(TotalComponents);
12787 for (unsigned i = 0; i < TotalComponents; ++i) {
12788 Expr *AssociatedExpr = Record.readSubExpr();
12789 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12790 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12791 AssociatedExpr, AssociatedDecl));
12792 }
12793 C->setComponents(Components, ListSizes);
12794}
12795
Kelvin Libe286f52018-09-15 13:54:15 +000012796void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12797 C->setLParenLoc(Record.readSourceLocation());
12798 auto NumVars = C->varlist_size();
12799 auto UniqueDecls = C->getUniqueDeclarationsNum();
12800 auto TotalLists = C->getTotalComponentListNum();
12801 auto TotalComponents = C->getTotalComponentsNum();
12802
12803 SmallVector<Expr *, 16> Vars;
12804 Vars.reserve(NumVars);
12805 for (unsigned i = 0; i != NumVars; ++i)
12806 Vars.push_back(Record.readSubExpr());
12807 C->setVarRefs(Vars);
12808 Vars.clear();
12809
12810 SmallVector<ValueDecl *, 16> Decls;
12811 Decls.reserve(UniqueDecls);
12812 for (unsigned i = 0; i < UniqueDecls; ++i)
12813 Decls.push_back(Record.readDeclAs<ValueDecl>());
12814 C->setUniqueDecls(Decls);
12815
12816 SmallVector<unsigned, 16> ListsPerDecl;
12817 ListsPerDecl.reserve(UniqueDecls);
12818 for (unsigned i = 0; i < UniqueDecls; ++i)
12819 ListsPerDecl.push_back(Record.readInt());
12820 C->setDeclNumLists(ListsPerDecl);
12821
12822 SmallVector<unsigned, 32> ListSizes;
12823 ListSizes.reserve(TotalLists);
12824 for (unsigned i = 0; i < TotalLists; ++i)
12825 ListSizes.push_back(Record.readInt());
12826 C->setComponentListSizes(ListSizes);
12827
12828 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12829 Components.reserve(TotalComponents);
12830 for (unsigned i = 0; i < TotalComponents; ++i) {
12831 Expr *AssociatedExpr = Record.readSubExpr();
12832 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12833 Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12834 AssociatedExpr, AssociatedDecl));
12835 }
12836 C->setComponents(Components, ListSizes);
12837}
Alexey Bataevb6e70842019-12-16 15:54:17 -050012838
12839void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12840 C->setLParenLoc(Record.readSourceLocation());
12841 unsigned NumVars = C->varlist_size();
12842 SmallVector<Expr *, 16> Vars;
12843 Vars.reserve(NumVars);
12844 for (unsigned i = 0; i != NumVars; ++i)
12845 Vars.push_back(Record.readSubExpr());
12846 C->setVarRefs(Vars);
Alexey Bataev0860db92019-12-19 10:01:10 -050012847 Vars.clear();
12848 Vars.reserve(NumVars);
12849 for (unsigned i = 0; i != NumVars; ++i)
12850 Vars.push_back(Record.readSubExpr());
12851 C->setPrivateRefs(Vars);
Alexey Bataevb6e70842019-12-16 15:54:17 -050012852}
Alexey Bataevcb8e6912020-01-31 16:09:26 -050012853
Alexey Bataev06dea732020-03-20 09:41:22 -040012854void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12855 C->setLParenLoc(Record.readSourceLocation());
12856 unsigned NumVars = C->varlist_size();
12857 SmallVector<Expr *, 16> Vars;
12858 Vars.reserve(NumVars);
12859 for (unsigned i = 0; i != NumVars; ++i)
12860 Vars.push_back(Record.readSubExpr());
12861 C->setVarRefs(Vars);
12862}
12863
Alexey Bataev63828a32020-03-23 10:41:08 -040012864void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12865 C->setLParenLoc(Record.readSourceLocation());
12866 unsigned NumVars = C->varlist_size();
12867 SmallVector<Expr *, 16> Vars;
12868 Vars.reserve(NumVars);
12869 for (unsigned i = 0; i != NumVars; ++i)
12870 Vars.push_back(Record.readSubExpr());
12871 C->setVarRefs(Vars);
12872}
12873
Alexey Bataevb5be1c52020-04-21 13:21:00 -040012874void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12875 C->setLParenLoc(Record.readSourceLocation());
12876 unsigned NumOfAllocators = C->getNumberOfAllocators();
12877 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12878 Data.reserve(NumOfAllocators);
12879 for (unsigned I = 0; I != NumOfAllocators; ++I) {
12880 OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12881 D.Allocator = Record.readSubExpr();
12882 D.AllocatorTraits = Record.readSubExpr();
12883 D.LParenLoc = Record.readSourceLocation();
12884 D.RParenLoc = Record.readSourceLocation();
12885 }
12886 C->setAllocatorsData(Data);
12887}
12888
Alexey Bataev2e499ee2020-05-18 13:37:53 -040012889void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12890 C->setLParenLoc(Record.readSourceLocation());
12891 C->setModifier(Record.readSubExpr());
12892 C->setColonLoc(Record.readSourceLocation());
12893 unsigned NumOfLocators = C->varlist_size();
12894 SmallVector<Expr *, 4> Locators;
12895 Locators.reserve(NumOfLocators);
12896 for (unsigned I = 0; I != NumOfLocators; ++I)
12897 Locators.push_back(Record.readSubExpr());
12898 C->setVarRefs(Locators);
12899}
12900
Alexey Bataevcb8e6912020-01-31 16:09:26 -050012901void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12902 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12903 C->setLParenLoc(Record.readSourceLocation());
12904 C->setKindKwLoc(Record.readSourceLocation());
12905}
Johannes Doerfert1228d422019-12-19 20:42:12 -060012906
Johannes Doerfert55eca282020-03-13 23:42:05 -050012907OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12908 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
Johannes Doerfertb86bf832020-02-15 18:07:42 -060012909 TI.Sets.resize(readUInt32());
12910 for (auto &Set : TI.Sets) {
Johannes Doerfert1228d422019-12-19 20:42:12 -060012911 Set.Kind = readEnum<llvm::omp::TraitSet>();
12912 Set.Selectors.resize(readUInt32());
12913 for (auto &Selector : Set.Selectors) {
12914 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12915 Selector.ScoreOrCondition = nullptr;
12916 if (readBool())
12917 Selector.ScoreOrCondition = readExprRef();
12918 Selector.Properties.resize(readUInt32());
12919 for (auto &Property : Selector.Properties)
12920 Property.Kind = readEnum<llvm::omp::TraitProperty>();
12921 }
12922 }
Johannes Doerfert55eca282020-03-13 23:42:05 -050012923 return &TI;
Johannes Doerfert1228d422019-12-19 20:42:12 -060012924}