Bug Summary

File:tools/clang/lib/Frontend/CompilerInstance.cpp
Warning:line 1597, column 5
Use of memory after it is freed

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name CompilerInstance.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -relaxed-aliasing -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/clang/lib/Frontend -I /build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend -I /build/llvm-toolchain-snapshot-7~svn329677/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn329677/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/clang/lib/Frontend -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fno-common -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-04-11-031539-24776-1 -x c++ /build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp

/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp

1//===--- CompilerInstance.cpp ---------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/Frontend/CompilerInstance.h"
11#include "clang/AST/ASTConsumer.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/AST/Decl.h"
14#include "clang/Basic/CharInfo.h"
15#include "clang/Basic/Diagnostic.h"
16#include "clang/Basic/FileManager.h"
17#include "clang/Basic/MemoryBufferCache.h"
18#include "clang/Basic/SourceManager.h"
19#include "clang/Basic/TargetInfo.h"
20#include "clang/Basic/Version.h"
21#include "clang/Config/config.h"
22#include "clang/Frontend/ChainedDiagnosticConsumer.h"
23#include "clang/Frontend/FrontendAction.h"
24#include "clang/Frontend/FrontendActions.h"
25#include "clang/Frontend/FrontendDiagnostic.h"
26#include "clang/Frontend/LogDiagnosticPrinter.h"
27#include "clang/Frontend/SerializedDiagnosticPrinter.h"
28#include "clang/Frontend/TextDiagnosticPrinter.h"
29#include "clang/Frontend/Utils.h"
30#include "clang/Frontend/VerifyDiagnosticConsumer.h"
31#include "clang/Lex/HeaderSearch.h"
32#include "clang/Lex/PTHManager.h"
33#include "clang/Lex/Preprocessor.h"
34#include "clang/Lex/PreprocessorOptions.h"
35#include "clang/Sema/CodeCompleteConsumer.h"
36#include "clang/Sema/Sema.h"
37#include "clang/Serialization/ASTReader.h"
38#include "clang/Serialization/GlobalModuleIndex.h"
39#include "llvm/ADT/Statistic.h"
40#include "llvm/Support/CrashRecoveryContext.h"
41#include "llvm/Support/Errc.h"
42#include "llvm/Support/FileSystem.h"
43#include "llvm/Support/Host.h"
44#include "llvm/Support/LockFileManager.h"
45#include "llvm/Support/MemoryBuffer.h"
46#include "llvm/Support/Path.h"
47#include "llvm/Support/Program.h"
48#include "llvm/Support/Signals.h"
49#include "llvm/Support/Timer.h"
50#include "llvm/Support/raw_ostream.h"
51#include <sys/stat.h>
52#include <system_error>
53#include <time.h>
54#include <utility>
55
56using namespace clang;
57
58CompilerInstance::CompilerInstance(
59 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
60 MemoryBufferCache *SharedPCMCache)
61 : ModuleLoader(/* BuildingModule = */ SharedPCMCache),
62 Invocation(new CompilerInvocation()),
63 PCMCache(SharedPCMCache ? SharedPCMCache : new MemoryBufferCache),
64 ThePCHContainerOperations(std::move(PCHContainerOps)) {
65 // Don't allow this to invalidate buffers in use by others.
66 if (SharedPCMCache)
67 getPCMCache().finalizeCurrentBuffers();
68}
69
70CompilerInstance::~CompilerInstance() {
71 assert(OutputFiles.empty() && "Still output files in flight?")(static_cast <bool> (OutputFiles.empty() && "Still output files in flight?"
) ? void (0) : __assert_fail ("OutputFiles.empty() && \"Still output files in flight?\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 71, __extension__ __PRETTY_FUNCTION__))
;
72}
73
74void CompilerInstance::setInvocation(
75 std::shared_ptr<CompilerInvocation> Value) {
76 Invocation = std::move(Value);
77}
78
79bool CompilerInstance::shouldBuildGlobalModuleIndex() const {
80 return (BuildGlobalModuleIndex ||
81 (ModuleManager && ModuleManager->isGlobalIndexUnavailable() &&
82 getFrontendOpts().GenerateGlobalModuleIndex)) &&
83 !ModuleBuildFailed;
84}
85
86void CompilerInstance::setDiagnostics(DiagnosticsEngine *Value) {
87 Diagnostics = Value;
88}
89
90void CompilerInstance::setTarget(TargetInfo *Value) { Target = Value; }
91void CompilerInstance::setAuxTarget(TargetInfo *Value) { AuxTarget = Value; }
92
93void CompilerInstance::setFileManager(FileManager *Value) {
94 FileMgr = Value;
95 if (Value)
96 VirtualFileSystem = Value->getVirtualFileSystem();
97 else
98 VirtualFileSystem.reset();
99}
100
101void CompilerInstance::setSourceManager(SourceManager *Value) {
102 SourceMgr = Value;
103}
104
105void CompilerInstance::setPreprocessor(std::shared_ptr<Preprocessor> Value) {
106 PP = std::move(Value);
107}
108
109void CompilerInstance::setASTContext(ASTContext *Value) {
110 Context = Value;
111
112 if (Context && Consumer)
113 getASTConsumer().Initialize(getASTContext());
114}
115
116void CompilerInstance::setSema(Sema *S) {
117 TheSema.reset(S);
118}
119
120void CompilerInstance::setASTConsumer(std::unique_ptr<ASTConsumer> Value) {
121 Consumer = std::move(Value);
122
123 if (Context && Consumer)
124 getASTConsumer().Initialize(getASTContext());
125}
126
127void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) {
128 CompletionConsumer.reset(Value);
129}
130
131std::unique_ptr<Sema> CompilerInstance::takeSema() {
132 return std::move(TheSema);
133}
134
135IntrusiveRefCntPtr<ASTReader> CompilerInstance::getModuleManager() const {
136 return ModuleManager;
137}
138void CompilerInstance::setModuleManager(IntrusiveRefCntPtr<ASTReader> Reader) {
139 assert(PCMCache.get() == &Reader->getModuleManager().getPCMCache() &&(static_cast <bool> (PCMCache.get() == &Reader->
getModuleManager().getPCMCache() && "Expected ASTReader to use the same PCM cache"
) ? void (0) : __assert_fail ("PCMCache.get() == &Reader->getModuleManager().getPCMCache() && \"Expected ASTReader to use the same PCM cache\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 140, __extension__ __PRETTY_FUNCTION__))
140 "Expected ASTReader to use the same PCM cache")(static_cast <bool> (PCMCache.get() == &Reader->
getModuleManager().getPCMCache() && "Expected ASTReader to use the same PCM cache"
) ? void (0) : __assert_fail ("PCMCache.get() == &Reader->getModuleManager().getPCMCache() && \"Expected ASTReader to use the same PCM cache\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 140, __extension__ __PRETTY_FUNCTION__))
;
141 ModuleManager = std::move(Reader);
142}
143
144std::shared_ptr<ModuleDependencyCollector>
145CompilerInstance::getModuleDepCollector() const {
146 return ModuleDepCollector;
147}
148
149void CompilerInstance::setModuleDepCollector(
150 std::shared_ptr<ModuleDependencyCollector> Collector) {
151 ModuleDepCollector = std::move(Collector);
152}
153
154static void collectHeaderMaps(const HeaderSearch &HS,
155 std::shared_ptr<ModuleDependencyCollector> MDC) {
156 SmallVector<std::string, 4> HeaderMapFileNames;
157 HS.getHeaderMapFileNames(HeaderMapFileNames);
158 for (auto &Name : HeaderMapFileNames)
159 MDC->addFile(Name);
160}
161
162static void collectIncludePCH(CompilerInstance &CI,
163 std::shared_ptr<ModuleDependencyCollector> MDC) {
164 const PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
165 if (PPOpts.ImplicitPCHInclude.empty())
166 return;
167
168 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
169 FileManager &FileMgr = CI.getFileManager();
170 const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude);
171 if (!PCHDir) {
172 MDC->addFile(PCHInclude);
173 return;
174 }
175
176 std::error_code EC;
177 SmallString<128> DirNative;
178 llvm::sys::path::native(PCHDir->getName(), DirNative);
179 vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
180 SimpleASTReaderListener Validator(CI.getPreprocessor());
181 for (vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
182 Dir != DirEnd && !EC; Dir.increment(EC)) {
183 // Check whether this is an AST file. ASTReader::isAcceptableASTFile is not
184 // used here since we're not interested in validating the PCH at this time,
185 // but only to check whether this is a file containing an AST.
186 if (!ASTReader::readASTFileControlBlock(
187 Dir->getName(), FileMgr, CI.getPCHContainerReader(),
188 /*FindModuleFileExtensions=*/false, Validator,
189 /*ValidateDiagnosticOptions=*/false))
190 MDC->addFile(Dir->getName());
191 }
192}
193
194static void collectVFSEntries(CompilerInstance &CI,
195 std::shared_ptr<ModuleDependencyCollector> MDC) {
196 if (CI.getHeaderSearchOpts().VFSOverlayFiles.empty())
197 return;
198
199 // Collect all VFS found.
200 SmallVector<vfs::YAMLVFSEntry, 16> VFSEntries;
201 for (const std::string &VFSFile : CI.getHeaderSearchOpts().VFSOverlayFiles) {
202 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer =
203 llvm::MemoryBuffer::getFile(VFSFile);
204 if (!Buffer)
205 return;
206 vfs::collectVFSFromYAML(std::move(Buffer.get()), /*DiagHandler*/ nullptr,
207 VFSFile, VFSEntries);
208 }
209
210 for (auto &E : VFSEntries)
211 MDC->addFile(E.VPath, E.RPath);
212}
213
214// Diagnostics
215static void SetUpDiagnosticLog(DiagnosticOptions *DiagOpts,
216 const CodeGenOptions *CodeGenOpts,
217 DiagnosticsEngine &Diags) {
218 std::error_code EC;
219 std::unique_ptr<raw_ostream> StreamOwner;
220 raw_ostream *OS = &llvm::errs();
221 if (DiagOpts->DiagnosticLogFile != "-") {
222 // Create the output stream.
223 auto FileOS = llvm::make_unique<llvm::raw_fd_ostream>(
224 DiagOpts->DiagnosticLogFile, EC,
225 llvm::sys::fs::F_Append | llvm::sys::fs::F_Text);
226 if (EC) {
227 Diags.Report(diag::warn_fe_cc_log_diagnostics_failure)
228 << DiagOpts->DiagnosticLogFile << EC.message();
229 } else {
230 FileOS->SetUnbuffered();
231 OS = FileOS.get();
232 StreamOwner = std::move(FileOS);
233 }
234 }
235
236 // Chain in the diagnostic client which will log the diagnostics.
237 auto Logger = llvm::make_unique<LogDiagnosticPrinter>(*OS, DiagOpts,
238 std::move(StreamOwner));
239 if (CodeGenOpts)
240 Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
241 assert(Diags.ownsClient())(static_cast <bool> (Diags.ownsClient()) ? void (0) : __assert_fail
("Diags.ownsClient()", "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 241, __extension__ __PRETTY_FUNCTION__))
;
242 Diags.setClient(
243 new ChainedDiagnosticConsumer(Diags.takeClient(), std::move(Logger)));
244}
245
246static void SetupSerializedDiagnostics(DiagnosticOptions *DiagOpts,
247 DiagnosticsEngine &Diags,
248 StringRef OutputFile) {
249 auto SerializedConsumer =
250 clang::serialized_diags::create(OutputFile, DiagOpts);
251
252 if (Diags.ownsClient()) {
253 Diags.setClient(new ChainedDiagnosticConsumer(
254 Diags.takeClient(), std::move(SerializedConsumer)));
255 } else {
256 Diags.setClient(new ChainedDiagnosticConsumer(
257 Diags.getClient(), std::move(SerializedConsumer)));
258 }
259}
260
261void CompilerInstance::createDiagnostics(DiagnosticConsumer *Client,
262 bool ShouldOwnClient) {
263 Diagnostics = createDiagnostics(&getDiagnosticOpts(), Client,
264 ShouldOwnClient, &getCodeGenOpts());
265}
266
267IntrusiveRefCntPtr<DiagnosticsEngine>
268CompilerInstance::createDiagnostics(DiagnosticOptions *Opts,
269 DiagnosticConsumer *Client,
270 bool ShouldOwnClient,
271 const CodeGenOptions *CodeGenOpts) {
272 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
273 IntrusiveRefCntPtr<DiagnosticsEngine>
274 Diags(new DiagnosticsEngine(DiagID, Opts));
275
276 // Create the diagnostic client for reporting errors or for
277 // implementing -verify.
278 if (Client) {
279 Diags->setClient(Client, ShouldOwnClient);
280 } else
281 Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
282
283 // Chain in -verify checker, if requested.
284 if (Opts->VerifyDiagnostics)
285 Diags->setClient(new VerifyDiagnosticConsumer(*Diags));
286
287 // Chain in -diagnostic-log-file dumper, if requested.
288 if (!Opts->DiagnosticLogFile.empty())
289 SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags);
290
291 if (!Opts->DiagnosticSerializationFile.empty())
292 SetupSerializedDiagnostics(Opts, *Diags,
293 Opts->DiagnosticSerializationFile);
294
295 // Configure our handling of diagnostics.
296 ProcessWarningOptions(*Diags, *Opts);
297
298 return Diags;
299}
300
301// File Manager
302
303FileManager *CompilerInstance::createFileManager() {
304 if (!hasVirtualFileSystem()) {
305 IntrusiveRefCntPtr<vfs::FileSystem> VFS =
306 createVFSFromCompilerInvocation(getInvocation(), getDiagnostics());
307 setVirtualFileSystem(VFS);
308 }
309 FileMgr = new FileManager(getFileSystemOpts(), VirtualFileSystem);
310 return FileMgr.get();
311}
312
313// Source Manager
314
315void CompilerInstance::createSourceManager(FileManager &FileMgr) {
316 SourceMgr = new SourceManager(getDiagnostics(), FileMgr);
317}
318
319// Initialize the remapping of files to alternative contents, e.g.,
320// those specified through other files.
321static void InitializeFileRemapping(DiagnosticsEngine &Diags,
322 SourceManager &SourceMgr,
323 FileManager &FileMgr,
324 const PreprocessorOptions &InitOpts) {
325 // Remap files in the source manager (with buffers).
326 for (const auto &RB : InitOpts.RemappedFileBuffers) {
327 // Create the file entry for the file that we're mapping from.
328 const FileEntry *FromFile =
329 FileMgr.getVirtualFile(RB.first, RB.second->getBufferSize(), 0);
330 if (!FromFile) {
331 Diags.Report(diag::err_fe_remap_missing_from_file) << RB.first;
332 if (!InitOpts.RetainRemappedFileBuffers)
333 delete RB.second;
334 continue;
335 }
336
337 // Override the contents of the "from" file with the contents of
338 // the "to" file.
339 SourceMgr.overrideFileContents(FromFile, RB.second,
340 InitOpts.RetainRemappedFileBuffers);
341 }
342
343 // Remap files in the source manager (with other files).
344 for (const auto &RF : InitOpts.RemappedFiles) {
345 // Find the file that we're mapping to.
346 const FileEntry *ToFile = FileMgr.getFile(RF.second);
347 if (!ToFile) {
348 Diags.Report(diag::err_fe_remap_missing_to_file) << RF.first << RF.second;
349 continue;
350 }
351
352 // Create the file entry for the file that we're mapping from.
353 const FileEntry *FromFile =
354 FileMgr.getVirtualFile(RF.first, ToFile->getSize(), 0);
355 if (!FromFile) {
356 Diags.Report(diag::err_fe_remap_missing_from_file) << RF.first;
357 continue;
358 }
359
360 // Override the contents of the "from" file with the contents of
361 // the "to" file.
362 SourceMgr.overrideFileContents(FromFile, ToFile);
363 }
364
365 SourceMgr.setOverridenFilesKeepOriginalName(
366 InitOpts.RemappedFilesKeepOriginalName);
367}
368
369// Preprocessor
370
371void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) {
372 const PreprocessorOptions &PPOpts = getPreprocessorOpts();
373
374 // Create a PTH manager if we are using some form of a token cache.
375 PTHManager *PTHMgr = nullptr;
376 if (!PPOpts.TokenCache.empty())
377 PTHMgr = PTHManager::Create(PPOpts.TokenCache, getDiagnostics());
378
379 // Create the Preprocessor.
380 HeaderSearch *HeaderInfo =
381 new HeaderSearch(getHeaderSearchOptsPtr(), getSourceManager(),
382 getDiagnostics(), getLangOpts(), &getTarget());
383 PP = std::make_shared<Preprocessor>(
384 Invocation->getPreprocessorOptsPtr(), getDiagnostics(), getLangOpts(),
385 getSourceManager(), getPCMCache(), *HeaderInfo, *this, PTHMgr,
386 /*OwnsHeaderSearch=*/true, TUKind);
387 getTarget().adjust(getLangOpts());
388 PP->Initialize(getTarget(), getAuxTarget());
389
390 // Note that this is different then passing PTHMgr to Preprocessor's ctor.
391 // That argument is used as the IdentifierInfoLookup argument to
392 // IdentifierTable's ctor.
393 if (PTHMgr) {
394 PTHMgr->setPreprocessor(&*PP);
395 PP->setPTHManager(PTHMgr);
396 }
397
398 if (PPOpts.DetailedRecord)
399 PP->createPreprocessingRecord();
400
401 // Apply remappings to the source manager.
402 InitializeFileRemapping(PP->getDiagnostics(), PP->getSourceManager(),
403 PP->getFileManager(), PPOpts);
404
405 // Predefine macros and configure the preprocessor.
406 InitializePreprocessor(*PP, PPOpts, getPCHContainerReader(),
407 getFrontendOpts());
408
409 // Initialize the header search object. In CUDA compilations, we use the aux
410 // triple (the host triple) to initialize our header search, since we need to
411 // find the host headers in order to compile the CUDA code.
412 const llvm::Triple *HeaderSearchTriple = &PP->getTargetInfo().getTriple();
413 if (PP->getTargetInfo().getTriple().getOS() == llvm::Triple::CUDA &&
414 PP->getAuxTargetInfo())
415 HeaderSearchTriple = &PP->getAuxTargetInfo()->getTriple();
416
417 ApplyHeaderSearchOptions(PP->getHeaderSearchInfo(), getHeaderSearchOpts(),
418 PP->getLangOpts(), *HeaderSearchTriple);
419
420 PP->setPreprocessedOutput(getPreprocessorOutputOpts().ShowCPP);
421
422 if (PP->getLangOpts().Modules && PP->getLangOpts().ImplicitModules)
423 PP->getHeaderSearchInfo().setModuleCachePath(getSpecificModuleCachePath());
424
425 // Handle generating dependencies, if requested.
426 const DependencyOutputOptions &DepOpts = getDependencyOutputOpts();
427 if (!DepOpts.OutputFile.empty())
428 TheDependencyFileGenerator.reset(
429 DependencyFileGenerator::CreateAndAttachToPreprocessor(*PP, DepOpts));
430 if (!DepOpts.DOTOutputFile.empty())
431 AttachDependencyGraphGen(*PP, DepOpts.DOTOutputFile,
432 getHeaderSearchOpts().Sysroot);
433
434 // If we don't have a collector, but we are collecting module dependencies,
435 // then we're the top level compiler instance and need to create one.
436 if (!ModuleDepCollector && !DepOpts.ModuleDependencyOutputDir.empty()) {
437 ModuleDepCollector = std::make_shared<ModuleDependencyCollector>(
438 DepOpts.ModuleDependencyOutputDir);
439 }
440
441 // If there is a module dep collector, register with other dep collectors
442 // and also (a) collect header maps and (b) TODO: input vfs overlay files.
443 if (ModuleDepCollector) {
444 addDependencyCollector(ModuleDepCollector);
445 collectHeaderMaps(PP->getHeaderSearchInfo(), ModuleDepCollector);
446 collectIncludePCH(*this, ModuleDepCollector);
447 collectVFSEntries(*this, ModuleDepCollector);
448 }
449
450 for (auto &Listener : DependencyCollectors)
451 Listener->attachToPreprocessor(*PP);
452
453 // Handle generating header include information, if requested.
454 if (DepOpts.ShowHeaderIncludes)
455 AttachHeaderIncludeGen(*PP, DepOpts);
456 if (!DepOpts.HeaderIncludeOutputFile.empty()) {
457 StringRef OutputPath = DepOpts.HeaderIncludeOutputFile;
458 if (OutputPath == "-")
459 OutputPath = "";
460 AttachHeaderIncludeGen(*PP, DepOpts,
461 /*ShowAllHeaders=*/true, OutputPath,
462 /*ShowDepth=*/false);
463 }
464
465 if (DepOpts.PrintShowIncludes) {
466 AttachHeaderIncludeGen(*PP, DepOpts,
467 /*ShowAllHeaders=*/true, /*OutputPath=*/"",
468 /*ShowDepth=*/true, /*MSStyle=*/true);
469 }
470}
471
472std::string CompilerInstance::getSpecificModuleCachePath() {
473 // Set up the module path, including the hash for the
474 // module-creation options.
475 SmallString<256> SpecificModuleCache(getHeaderSearchOpts().ModuleCachePath);
476 if (!SpecificModuleCache.empty() && !getHeaderSearchOpts().DisableModuleHash)
477 llvm::sys::path::append(SpecificModuleCache,
478 getInvocation().getModuleHash());
479 return SpecificModuleCache.str();
480}
481
482// ASTContext
483
484void CompilerInstance::createASTContext() {
485 Preprocessor &PP = getPreprocessor();
486 auto *Context = new ASTContext(getLangOpts(), PP.getSourceManager(),
487 PP.getIdentifierTable(), PP.getSelectorTable(),
488 PP.getBuiltinInfo());
489 Context->InitBuiltinTypes(getTarget(), getAuxTarget());
490 setASTContext(Context);
491}
492
493// ExternalASTSource
494
495void CompilerInstance::createPCHExternalASTSource(
496 StringRef Path, bool DisablePCHValidation, bool AllowPCHWithCompilerErrors,
497 void *DeserializationListener, bool OwnDeserializationListener) {
498 bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
499 ModuleManager = createPCHExternalASTSource(
500 Path, getHeaderSearchOpts().Sysroot, DisablePCHValidation,
501 AllowPCHWithCompilerErrors, getPreprocessor(), getASTContext(),
502 getPCHContainerReader(),
503 getFrontendOpts().ModuleFileExtensions,
504 TheDependencyFileGenerator.get(),
505 DependencyCollectors,
506 DeserializationListener,
507 OwnDeserializationListener, Preamble,
508 getFrontendOpts().UseGlobalModuleIndex);
509}
510
511IntrusiveRefCntPtr<ASTReader> CompilerInstance::createPCHExternalASTSource(
512 StringRef Path, StringRef Sysroot, bool DisablePCHValidation,
513 bool AllowPCHWithCompilerErrors, Preprocessor &PP, ASTContext &Context,
514 const PCHContainerReader &PCHContainerRdr,
515 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
516 DependencyFileGenerator *DependencyFile,
517 ArrayRef<std::shared_ptr<DependencyCollector>> DependencyCollectors,
518 void *DeserializationListener, bool OwnDeserializationListener,
519 bool Preamble, bool UseGlobalModuleIndex) {
520 HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();
521
522 IntrusiveRefCntPtr<ASTReader> Reader(new ASTReader(
523 PP, &Context, PCHContainerRdr, Extensions,
524 Sysroot.empty() ? "" : Sysroot.data(), DisablePCHValidation,
525 AllowPCHWithCompilerErrors, /*AllowConfigurationMismatch*/ false,
526 HSOpts.ModulesValidateSystemHeaders, UseGlobalModuleIndex));
527
528 // We need the external source to be set up before we read the AST, because
529 // eagerly-deserialized declarations may use it.
530 Context.setExternalSource(Reader.get());
531
532 Reader->setDeserializationListener(
533 static_cast<ASTDeserializationListener *>(DeserializationListener),
534 /*TakeOwnership=*/OwnDeserializationListener);
535
536 if (DependencyFile)
537 DependencyFile->AttachToASTReader(*Reader);
538 for (auto &Listener : DependencyCollectors)
539 Listener->attachToASTReader(*Reader);
540
541 switch (Reader->ReadAST(Path,
542 Preamble ? serialization::MK_Preamble
543 : serialization::MK_PCH,
544 SourceLocation(),
545 ASTReader::ARR_None)) {
546 case ASTReader::Success:
547 // Set the predefines buffer as suggested by the PCH reader. Typically, the
548 // predefines buffer will be empty.
549 PP.setPredefines(Reader->getSuggestedPredefines());
550 return Reader;
551
552 case ASTReader::Failure:
553 // Unrecoverable failure: don't even try to process the input file.
554 break;
555
556 case ASTReader::Missing:
557 case ASTReader::OutOfDate:
558 case ASTReader::VersionMismatch:
559 case ASTReader::ConfigurationMismatch:
560 case ASTReader::HadErrors:
561 // No suitable PCH file could be found. Return an error.
562 break;
563 }
564
565 Context.setExternalSource(nullptr);
566 return nullptr;
567}
568
569// Code Completion
570
571static bool EnableCodeCompletion(Preprocessor &PP,
572 StringRef Filename,
573 unsigned Line,
574 unsigned Column) {
575 // Tell the source manager to chop off the given file at a specific
576 // line and column.
577 const FileEntry *Entry = PP.getFileManager().getFile(Filename);
578 if (!Entry) {
579 PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
580 << Filename;
581 return true;
582 }
583
584 // Truncate the named file at the given line/column.
585 PP.SetCodeCompletionPoint(Entry, Line, Column);
586 return false;
587}
588
589void CompilerInstance::createCodeCompletionConsumer() {
590 const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
591 if (!CompletionConsumer) {
592 setCodeCompletionConsumer(
593 createCodeCompletionConsumer(getPreprocessor(),
594 Loc.FileName, Loc.Line, Loc.Column,
595 getFrontendOpts().CodeCompleteOpts,
596 llvm::outs()));
597 if (!CompletionConsumer)
598 return;
599 } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
600 Loc.Line, Loc.Column)) {
601 setCodeCompletionConsumer(nullptr);
602 return;
603 }
604
605 if (CompletionConsumer->isOutputBinary() &&
606 llvm::sys::ChangeStdoutToBinary()) {
607 getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
608 setCodeCompletionConsumer(nullptr);
609 }
610}
611
612void CompilerInstance::createFrontendTimer() {
613 FrontendTimerGroup.reset(
614 new llvm::TimerGroup("frontend", "Clang front-end time report"));
615 FrontendTimer.reset(
616 new llvm::Timer("frontend", "Clang front-end timer",
617 *FrontendTimerGroup));
618}
619
620CodeCompleteConsumer *
621CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
622 StringRef Filename,
623 unsigned Line,
624 unsigned Column,
625 const CodeCompleteOptions &Opts,
626 raw_ostream &OS) {
627 if (EnableCodeCompletion(PP, Filename, Line, Column))
628 return nullptr;
629
630 // Set up the creation routine for code-completion.
631 return new PrintingCodeCompleteConsumer(Opts, OS);
632}
633
634void CompilerInstance::createSema(TranslationUnitKind TUKind,
635 CodeCompleteConsumer *CompletionConsumer) {
636 TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
637 TUKind, CompletionConsumer));
638 // Attach the external sema source if there is any.
639 if (ExternalSemaSrc) {
640 TheSema->addExternalSource(ExternalSemaSrc.get());
641 ExternalSemaSrc->InitializeSema(*TheSema);
642 }
643}
644
645// Output Files
646
647void CompilerInstance::addOutputFile(OutputFile &&OutFile) {
648 OutputFiles.push_back(std::move(OutFile));
649}
650
651void CompilerInstance::clearOutputFiles(bool EraseFiles) {
652 for (OutputFile &OF : OutputFiles) {
653 if (!OF.TempFilename.empty()) {
654 if (EraseFiles) {
655 llvm::sys::fs::remove(OF.TempFilename);
656 } else {
657 SmallString<128> NewOutFile(OF.Filename);
658
659 // If '-working-directory' was passed, the output filename should be
660 // relative to that.
661 FileMgr->FixupRelativePath(NewOutFile);
662 if (std::error_code ec =
663 llvm::sys::fs::rename(OF.TempFilename, NewOutFile)) {
664 getDiagnostics().Report(diag::err_unable_to_rename_temp)
665 << OF.TempFilename << OF.Filename << ec.message();
666
667 llvm::sys::fs::remove(OF.TempFilename);
668 }
669 }
670 } else if (!OF.Filename.empty() && EraseFiles)
671 llvm::sys::fs::remove(OF.Filename);
672 }
673 OutputFiles.clear();
674 if (DeleteBuiltModules) {
675 for (auto &Module : BuiltModules)
676 llvm::sys::fs::remove(Module.second);
677 BuiltModules.clear();
678 }
679 NonSeekStream.reset();
680}
681
682std::unique_ptr<raw_pwrite_stream>
683CompilerInstance::createDefaultOutputFile(bool Binary, StringRef InFile,
684 StringRef Extension) {
685 return createOutputFile(getFrontendOpts().OutputFile, Binary,
686 /*RemoveFileOnSignal=*/true, InFile, Extension,
687 /*UseTemporary=*/true);
688}
689
690std::unique_ptr<raw_pwrite_stream> CompilerInstance::createNullOutputFile() {
691 return llvm::make_unique<llvm::raw_null_ostream>();
692}
693
694std::unique_ptr<raw_pwrite_stream>
695CompilerInstance::createOutputFile(StringRef OutputPath, bool Binary,
696 bool RemoveFileOnSignal, StringRef InFile,
697 StringRef Extension, bool UseTemporary,
698 bool CreateMissingDirectories) {
699 std::string OutputPathName, TempPathName;
700 std::error_code EC;
701 std::unique_ptr<raw_pwrite_stream> OS = createOutputFile(
702 OutputPath, EC, Binary, RemoveFileOnSignal, InFile, Extension,
703 UseTemporary, CreateMissingDirectories, &OutputPathName, &TempPathName);
704 if (!OS) {
705 getDiagnostics().Report(diag::err_fe_unable_to_open_output) << OutputPath
706 << EC.message();
707 return nullptr;
708 }
709
710 // Add the output file -- but don't try to remove "-", since this means we are
711 // using stdin.
712 addOutputFile(
713 OutputFile((OutputPathName != "-") ? OutputPathName : "", TempPathName));
714
715 return OS;
716}
717
718std::unique_ptr<llvm::raw_pwrite_stream> CompilerInstance::createOutputFile(
719 StringRef OutputPath, std::error_code &Error, bool Binary,
720 bool RemoveFileOnSignal, StringRef InFile, StringRef Extension,
721 bool UseTemporary, bool CreateMissingDirectories,
722 std::string *ResultPathName, std::string *TempPathName) {
723 assert((!CreateMissingDirectories || UseTemporary) &&(static_cast <bool> ((!CreateMissingDirectories || UseTemporary
) && "CreateMissingDirectories is only allowed when using temporary files"
) ? void (0) : __assert_fail ("(!CreateMissingDirectories || UseTemporary) && \"CreateMissingDirectories is only allowed when using temporary files\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 724, __extension__ __PRETTY_FUNCTION__))
724 "CreateMissingDirectories is only allowed when using temporary files")(static_cast <bool> ((!CreateMissingDirectories || UseTemporary
) && "CreateMissingDirectories is only allowed when using temporary files"
) ? void (0) : __assert_fail ("(!CreateMissingDirectories || UseTemporary) && \"CreateMissingDirectories is only allowed when using temporary files\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 724, __extension__ __PRETTY_FUNCTION__))
;
725
726 std::string OutFile, TempFile;
727 if (!OutputPath.empty()) {
728 OutFile = OutputPath;
729 } else if (InFile == "-") {
730 OutFile = "-";
731 } else if (!Extension.empty()) {
732 SmallString<128> Path(InFile);
733 llvm::sys::path::replace_extension(Path, Extension);
734 OutFile = Path.str();
735 } else {
736 OutFile = "-";
737 }
738
739 std::unique_ptr<llvm::raw_fd_ostream> OS;
740 std::string OSFile;
741
742 if (UseTemporary) {
743 if (OutFile == "-")
744 UseTemporary = false;
745 else {
746 llvm::sys::fs::file_status Status;
747 llvm::sys::fs::status(OutputPath, Status);
748 if (llvm::sys::fs::exists(Status)) {
749 // Fail early if we can't write to the final destination.
750 if (!llvm::sys::fs::can_write(OutputPath)) {
751 Error = make_error_code(llvm::errc::operation_not_permitted);
752 return nullptr;
753 }
754
755 // Don't use a temporary if the output is a special file. This handles
756 // things like '-o /dev/null'
757 if (!llvm::sys::fs::is_regular_file(Status))
758 UseTemporary = false;
759 }
760 }
761 }
762
763 if (UseTemporary) {
764 // Create a temporary file.
765 // Insert -%%%%%%%% before the extension (if any), and because some tools
766 // (noticeable, clang's own GlobalModuleIndex.cpp) glob for build
767 // artifacts, also append .tmp.
768 StringRef OutputExtension = llvm::sys::path::extension(OutFile);
769 SmallString<128> TempPath =
770 StringRef(OutFile).drop_back(OutputExtension.size());
771 TempPath += "-%%%%%%%%";
772 TempPath += OutputExtension;
773 TempPath += ".tmp";
774 int fd;
775 std::error_code EC =
776 llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath);
777
778 if (CreateMissingDirectories &&
779 EC == llvm::errc::no_such_file_or_directory) {
780 StringRef Parent = llvm::sys::path::parent_path(OutputPath);
781 EC = llvm::sys::fs::create_directories(Parent);
782 if (!EC) {
783 EC = llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath);
784 }
785 }
786
787 if (!EC) {
788 OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
789 OSFile = TempFile = TempPath.str();
790 }
791 // If we failed to create the temporary, fallback to writing to the file
792 // directly. This handles the corner case where we cannot write to the
793 // directory, but can write to the file.
794 }
795
796 if (!OS) {
797 OSFile = OutFile;
798 OS.reset(new llvm::raw_fd_ostream(
799 OSFile, Error,
800 (Binary ? llvm::sys::fs::F_None : llvm::sys::fs::F_Text)));
801 if (Error)
802 return nullptr;
803 }
804
805 // Make sure the out stream file gets removed if we crash.
806 if (RemoveFileOnSignal)
807 llvm::sys::RemoveFileOnSignal(OSFile);
808
809 if (ResultPathName)
810 *ResultPathName = OutFile;
811 if (TempPathName)
812 *TempPathName = TempFile;
813
814 if (!Binary || OS->supportsSeeking())
815 return std::move(OS);
816
817 auto B = llvm::make_unique<llvm::buffer_ostream>(*OS);
818 assert(!NonSeekStream)(static_cast <bool> (!NonSeekStream) ? void (0) : __assert_fail
("!NonSeekStream", "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 818, __extension__ __PRETTY_FUNCTION__))
;
819 NonSeekStream = std::move(OS);
820 return std::move(B);
821}
822
823// Initialization Utilities
824
825bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input){
826 return InitializeSourceManager(
827 Input, getDiagnostics(), getFileManager(), getSourceManager(),
828 hasPreprocessor() ? &getPreprocessor().getHeaderSearchInfo() : nullptr,
829 getDependencyOutputOpts(), getFrontendOpts());
830}
831
832// static
833bool CompilerInstance::InitializeSourceManager(
834 const FrontendInputFile &Input, DiagnosticsEngine &Diags,
835 FileManager &FileMgr, SourceManager &SourceMgr, HeaderSearch *HS,
836 DependencyOutputOptions &DepOpts, const FrontendOptions &Opts) {
837 SrcMgr::CharacteristicKind Kind =
838 Input.getKind().getFormat() == InputKind::ModuleMap
839 ? Input.isSystem() ? SrcMgr::C_System_ModuleMap
840 : SrcMgr::C_User_ModuleMap
841 : Input.isSystem() ? SrcMgr::C_System : SrcMgr::C_User;
842
843 if (Input.isBuffer()) {
844 SourceMgr.setMainFileID(SourceMgr.createFileID(SourceManager::Unowned,
845 Input.getBuffer(), Kind));
846 assert(SourceMgr.getMainFileID().isValid() &&(static_cast <bool> (SourceMgr.getMainFileID().isValid(
) && "Couldn't establish MainFileID!") ? void (0) : __assert_fail
("SourceMgr.getMainFileID().isValid() && \"Couldn't establish MainFileID!\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 847, __extension__ __PRETTY_FUNCTION__))
847 "Couldn't establish MainFileID!")(static_cast <bool> (SourceMgr.getMainFileID().isValid(
) && "Couldn't establish MainFileID!") ? void (0) : __assert_fail
("SourceMgr.getMainFileID().isValid() && \"Couldn't establish MainFileID!\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 847, __extension__ __PRETTY_FUNCTION__))
;
848 return true;
849 }
850
851 StringRef InputFile = Input.getFile();
852
853 // Figure out where to get and map in the main file.
854 if (InputFile != "-") {
855 const FileEntry *File;
856 if (Opts.FindPchSource.empty()) {
857 File = FileMgr.getFile(InputFile, /*OpenFile=*/true);
858 } else {
859 // When building a pch file in clang-cl mode, the .h file is built as if
860 // it was included by a cc file. Since the driver doesn't know about
861 // all include search directories, the frontend must search the input
862 // file through HeaderSearch here, as if it had been included by the
863 // cc file at Opts.FindPchSource.
864 const FileEntry *FindFile = FileMgr.getFile(Opts.FindPchSource);
865 if (!FindFile) {
866 Diags.Report(diag::err_fe_error_reading) << Opts.FindPchSource;
867 return false;
868 }
869 const DirectoryLookup *UnusedCurDir;
870 SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 16>
871 Includers;
872 Includers.push_back(std::make_pair(FindFile, FindFile->getDir()));
873 File = HS->LookupFile(InputFile, SourceLocation(), /*isAngled=*/false,
874 /*FromDir=*/nullptr,
875 /*CurDir=*/UnusedCurDir, Includers,
876 /*SearchPath=*/nullptr,
877 /*RelativePath=*/nullptr,
878 /*RequestingModule=*/nullptr,
879 /*SuggestedModule=*/nullptr, /*IsMapped=*/nullptr,
880 /*SkipCache=*/true);
881 // Also add the header to /showIncludes output.
882 if (File)
883 DepOpts.ShowIncludesPretendHeader = File->getName();
884 }
885 if (!File) {
886 Diags.Report(diag::err_fe_error_reading) << InputFile;
887 return false;
888 }
889
890 // The natural SourceManager infrastructure can't currently handle named
891 // pipes, but we would at least like to accept them for the main
892 // file. Detect them here, read them with the volatile flag so FileMgr will
893 // pick up the correct size, and simply override their contents as we do for
894 // STDIN.
895 if (File->isNamedPipe()) {
896 auto MB = FileMgr.getBufferForFile(File, /*isVolatile=*/true);
897 if (MB) {
898 // Create a new virtual file that will have the correct size.
899 File = FileMgr.getVirtualFile(InputFile, (*MB)->getBufferSize(), 0);
900 SourceMgr.overrideFileContents(File, std::move(*MB));
901 } else {
902 Diags.Report(diag::err_cannot_open_file) << InputFile
903 << MB.getError().message();
904 return false;
905 }
906 }
907
908 SourceMgr.setMainFileID(
909 SourceMgr.createFileID(File, SourceLocation(), Kind));
910 } else {
911 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> SBOrErr =
912 llvm::MemoryBuffer::getSTDIN();
913 if (std::error_code EC = SBOrErr.getError()) {
914 Diags.Report(diag::err_fe_error_reading_stdin) << EC.message();
915 return false;
916 }
917 std::unique_ptr<llvm::MemoryBuffer> SB = std::move(SBOrErr.get());
918
919 const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(),
920 SB->getBufferSize(), 0);
921 SourceMgr.setMainFileID(
922 SourceMgr.createFileID(File, SourceLocation(), Kind));
923 SourceMgr.overrideFileContents(File, std::move(SB));
924 }
925
926 assert(SourceMgr.getMainFileID().isValid() &&(static_cast <bool> (SourceMgr.getMainFileID().isValid(
) && "Couldn't establish MainFileID!") ? void (0) : __assert_fail
("SourceMgr.getMainFileID().isValid() && \"Couldn't establish MainFileID!\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 927, __extension__ __PRETTY_FUNCTION__))
927 "Couldn't establish MainFileID!")(static_cast <bool> (SourceMgr.getMainFileID().isValid(
) && "Couldn't establish MainFileID!") ? void (0) : __assert_fail
("SourceMgr.getMainFileID().isValid() && \"Couldn't establish MainFileID!\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 927, __extension__ __PRETTY_FUNCTION__))
;
928 return true;
929}
930
931// High-Level Operations
932
933bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
934 assert(hasDiagnostics() && "Diagnostics engine is not initialized!")(static_cast <bool> (hasDiagnostics() && "Diagnostics engine is not initialized!"
) ? void (0) : __assert_fail ("hasDiagnostics() && \"Diagnostics engine is not initialized!\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 934, __extension__ __PRETTY_FUNCTION__))
;
935 assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!")(static_cast <bool> (!getFrontendOpts().ShowHelp &&
"Client must handle '-help'!") ? void (0) : __assert_fail ("!getFrontendOpts().ShowHelp && \"Client must handle '-help'!\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 935, __extension__ __PRETTY_FUNCTION__))
;
936 assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!")(static_cast <bool> (!getFrontendOpts().ShowVersion &&
"Client must handle '-version'!") ? void (0) : __assert_fail
("!getFrontendOpts().ShowVersion && \"Client must handle '-version'!\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 936, __extension__ __PRETTY_FUNCTION__))
;
937
938 // FIXME: Take this as an argument, once all the APIs we used have moved to
939 // taking it as an input instead of hard-coding llvm::errs.
940 raw_ostream &OS = llvm::errs();
941
942 // Create the target instance.
943 setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(),
944 getInvocation().TargetOpts));
945 if (!hasTarget())
946 return false;
947
948 // Create TargetInfo for the other side of CUDA and OpenMP compilation.
949 if ((getLangOpts().CUDA || getLangOpts().OpenMPIsDevice) &&
950 !getFrontendOpts().AuxTriple.empty()) {
951 auto TO = std::make_shared<TargetOptions>();
952 TO->Triple = getFrontendOpts().AuxTriple;
953 TO->HostTriple = getTarget().getTriple().str();
954 setAuxTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), TO));
955 }
956
957 // Inform the target of the language options.
958 //
959 // FIXME: We shouldn't need to do this, the target should be immutable once
960 // created. This complexity should be lifted elsewhere.
961 getTarget().adjust(getLangOpts());
962
963 // Adjust target options based on codegen options.
964 getTarget().adjustTargetOptions(getCodeGenOpts(), getTargetOpts());
965
966 // rewriter project will change target built-in bool type from its default.
967 if (getFrontendOpts().ProgramAction == frontend::RewriteObjC)
968 getTarget().noSignedCharForObjCBool();
969
970 // Validate/process some options.
971 if (getHeaderSearchOpts().Verbose)
972 OS << "clang -cc1 version " CLANG_VERSION_STRING"7.0.0"
973 << " based upon " << BACKEND_PACKAGE_STRING"LLVM 7.0.0"
974 << " default target " << llvm::sys::getDefaultTargetTriple() << "\n";
975
976 if (getFrontendOpts().ShowTimers)
977 createFrontendTimer();
978
979 if (getFrontendOpts().ShowStats || !getFrontendOpts().StatsFile.empty())
980 llvm::EnableStatistics(false);
981
982 for (const FrontendInputFile &FIF : getFrontendOpts().Inputs) {
983 // Reset the ID tables if we are reusing the SourceManager and parsing
984 // regular files.
985 if (hasSourceManager() && !Act.isModelParsingAction())
986 getSourceManager().clearIDTables();
987
988 if (Act.BeginSourceFile(*this, FIF)) {
989 Act.Execute();
990 Act.EndSourceFile();
991 }
992 }
993
994 // Notify the diagnostic client that all files were processed.
995 getDiagnostics().getClient()->finish();
996
997 if (getDiagnosticOpts().ShowCarets) {
998 // We can have multiple diagnostics sharing one diagnostic client.
999 // Get the total number of warnings/errors from the client.
1000 unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
1001 unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
1002
1003 if (NumWarnings)
1004 OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
1005 if (NumWarnings && NumErrors)
1006 OS << " and ";
1007 if (NumErrors)
1008 OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
1009 if (NumWarnings || NumErrors) {
1010 OS << " generated";
1011 if (getLangOpts().CUDA) {
1012 if (!getLangOpts().CUDAIsDevice) {
1013 OS << " when compiling for host";
1014 } else {
1015 OS << " when compiling for " << getTargetOpts().CPU;
1016 }
1017 }
1018 OS << ".\n";
1019 }
1020 }
1021
1022 if (getFrontendOpts().ShowStats) {
1023 if (hasFileManager()) {
1024 getFileManager().PrintStats();
1025 OS << '\n';
1026 }
1027 llvm::PrintStatistics(OS);
1028 }
1029 StringRef StatsFile = getFrontendOpts().StatsFile;
1030 if (!StatsFile.empty()) {
1031 std::error_code EC;
1032 auto StatS = llvm::make_unique<llvm::raw_fd_ostream>(StatsFile, EC,
1033 llvm::sys::fs::F_Text);
1034 if (EC) {
1035 getDiagnostics().Report(diag::warn_fe_unable_to_open_stats_file)
1036 << StatsFile << EC.message();
1037 } else {
1038 llvm::PrintStatisticsJSON(*StatS);
1039 }
1040 }
1041
1042 return !getDiagnostics().getClient()->getNumErrors();
1043}
1044
1045/// \brief Determine the appropriate source input kind based on language
1046/// options.
1047static InputKind::Language getLanguageFromOptions(const LangOptions &LangOpts) {
1048 if (LangOpts.OpenCL)
1049 return InputKind::OpenCL;
1050 if (LangOpts.CUDA)
1051 return InputKind::CUDA;
1052 if (LangOpts.ObjC1)
1053 return LangOpts.CPlusPlus ? InputKind::ObjCXX : InputKind::ObjC;
1054 return LangOpts.CPlusPlus ? InputKind::CXX : InputKind::C;
1055}
1056
1057/// \brief Compile a module file for the given module, using the options
1058/// provided by the importing compiler instance. Returns true if the module
1059/// was built without errors.
1060static bool
1061compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc,
1062 StringRef ModuleName, FrontendInputFile Input,
1063 StringRef OriginalModuleMapFile, StringRef ModuleFileName,
1064 llvm::function_ref<void(CompilerInstance &)> PreBuildStep =
1065 [](CompilerInstance &) {},
1066 llvm::function_ref<void(CompilerInstance &)> PostBuildStep =
1067 [](CompilerInstance &) {}) {
1068 // Construct a compiler invocation for creating this module.
1069 auto Invocation =
1070 std::make_shared<CompilerInvocation>(ImportingInstance.getInvocation());
1071
1072 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1073
1074 // For any options that aren't intended to affect how a module is built,
1075 // reset them to their default values.
1076 Invocation->getLangOpts()->resetNonModularOptions();
1077 PPOpts.resetNonModularOptions();
1078
1079 // Remove any macro definitions that are explicitly ignored by the module.
1080 // They aren't supposed to affect how the module is built anyway.
1081 HeaderSearchOptions &HSOpts = Invocation->getHeaderSearchOpts();
1082 PPOpts.Macros.erase(
1083 std::remove_if(PPOpts.Macros.begin(), PPOpts.Macros.end(),
1084 [&HSOpts](const std::pair<std::string, bool> &def) {
1085 StringRef MacroDef = def.first;
1086 return HSOpts.ModulesIgnoreMacros.count(
1087 llvm::CachedHashString(MacroDef.split('=').first)) > 0;
1088 }),
1089 PPOpts.Macros.end());
1090
1091 // If the original compiler invocation had -fmodule-name, pass it through.
1092 Invocation->getLangOpts()->ModuleName =
1093 ImportingInstance.getInvocation().getLangOpts()->ModuleName;
1094
1095 // Note the name of the module we're building.
1096 Invocation->getLangOpts()->CurrentModule = ModuleName;
1097
1098 // Make sure that the failed-module structure has been allocated in
1099 // the importing instance, and propagate the pointer to the newly-created
1100 // instance.
1101 PreprocessorOptions &ImportingPPOpts
1102 = ImportingInstance.getInvocation().getPreprocessorOpts();
1103 if (!ImportingPPOpts.FailedModules)
1104 ImportingPPOpts.FailedModules =
1105 std::make_shared<PreprocessorOptions::FailedModulesSet>();
1106 PPOpts.FailedModules = ImportingPPOpts.FailedModules;
1107
1108 // If there is a module map file, build the module using the module map.
1109 // Set up the inputs/outputs so that we build the module from its umbrella
1110 // header.
1111 FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
1112 FrontendOpts.OutputFile = ModuleFileName.str();
1113 FrontendOpts.DisableFree = false;
1114 FrontendOpts.GenerateGlobalModuleIndex = false;
1115 FrontendOpts.BuildingImplicitModule = true;
1116 FrontendOpts.OriginalModuleMap = OriginalModuleMapFile;
1117 // Force implicitly-built modules to hash the content of the module file.
1118 HSOpts.ModulesHashContent = true;
1119 FrontendOpts.Inputs = {Input};
1120
1121 // Don't free the remapped file buffers; they are owned by our caller.
1122 PPOpts.RetainRemappedFileBuffers = true;
1123
1124 Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
1125 assert(ImportingInstance.getInvocation().getModuleHash() ==(static_cast <bool> (ImportingInstance.getInvocation().
getModuleHash() == Invocation->getModuleHash() && "Module hash mismatch!"
) ? void (0) : __assert_fail ("ImportingInstance.getInvocation().getModuleHash() == Invocation->getModuleHash() && \"Module hash mismatch!\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 1126, __extension__ __PRETTY_FUNCTION__))
1126 Invocation->getModuleHash() && "Module hash mismatch!")(static_cast <bool> (ImportingInstance.getInvocation().
getModuleHash() == Invocation->getModuleHash() && "Module hash mismatch!"
) ? void (0) : __assert_fail ("ImportingInstance.getInvocation().getModuleHash() == Invocation->getModuleHash() && \"Module hash mismatch!\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 1126, __extension__ __PRETTY_FUNCTION__))
;
1127
1128 // Construct a compiler instance that will be used to actually create the
1129 // module. Since we're sharing a PCMCache,
1130 // CompilerInstance::CompilerInstance is responsible for finalizing the
1131 // buffers to prevent use-after-frees.
1132 CompilerInstance Instance(ImportingInstance.getPCHContainerOperations(),
1133 &ImportingInstance.getPreprocessor().getPCMCache());
1134 auto &Inv = *Invocation;
1135 Instance.setInvocation(std::move(Invocation));
1136
1137 Instance.createDiagnostics(new ForwardingDiagnosticConsumer(
1138 ImportingInstance.getDiagnosticClient()),
1139 /*ShouldOwnClient=*/true);
1140
1141 Instance.setVirtualFileSystem(&ImportingInstance.getVirtualFileSystem());
1142
1143 // Note that this module is part of the module build stack, so that we
1144 // can detect cycles in the module graph.
1145 Instance.setFileManager(&ImportingInstance.getFileManager());
1146 Instance.createSourceManager(Instance.getFileManager());
1147 SourceManager &SourceMgr = Instance.getSourceManager();
1148 SourceMgr.setModuleBuildStack(
1149 ImportingInstance.getSourceManager().getModuleBuildStack());
1150 SourceMgr.pushModuleBuildStack(ModuleName,
1151 FullSourceLoc(ImportLoc, ImportingInstance.getSourceManager()));
1152
1153 // If we're collecting module dependencies, we need to share a collector
1154 // between all of the module CompilerInstances. Other than that, we don't
1155 // want to produce any dependency output from the module build.
1156 Instance.setModuleDepCollector(ImportingInstance.getModuleDepCollector());
1157 Inv.getDependencyOutputOpts() = DependencyOutputOptions();
1158
1159 ImportingInstance.getDiagnostics().Report(ImportLoc,
1160 diag::remark_module_build)
1161 << ModuleName << ModuleFileName;
1162
1163 PreBuildStep(Instance);
1164
1165 // Execute the action to actually build the module in-place. Use a separate
1166 // thread so that we get a stack large enough.
1167 const unsigned ThreadStackSize = 8 << 20;
1168 llvm::CrashRecoveryContext CRC;
1169 CRC.RunSafelyOnThread(
1170 [&]() {
1171 GenerateModuleFromModuleMapAction Action;
1172 Instance.ExecuteAction(Action);
1173 },
1174 ThreadStackSize);
1175
1176 PostBuildStep(Instance);
1177
1178 ImportingInstance.getDiagnostics().Report(ImportLoc,
1179 diag::remark_module_build_done)
1180 << ModuleName;
1181
1182 // Delete the temporary module map file.
1183 // FIXME: Even though we're executing under crash protection, it would still
1184 // be nice to do this with RemoveFileOnSignal when we can. However, that
1185 // doesn't make sense for all clients, so clean this up manually.
1186 Instance.clearOutputFiles(/*EraseFiles=*/true);
1187
1188 return !Instance.getDiagnostics().hasErrorOccurred();
1189}
1190
1191/// \brief Compile a module file for the given module, using the options
1192/// provided by the importing compiler instance. Returns true if the module
1193/// was built without errors.
1194static bool compileModuleImpl(CompilerInstance &ImportingInstance,
1195 SourceLocation ImportLoc,
1196 Module *Module,
1197 StringRef ModuleFileName) {
1198 InputKind IK(getLanguageFromOptions(ImportingInstance.getLangOpts()),
1199 InputKind::ModuleMap);
1200
1201 // Get or create the module map that we'll use to build this module.
1202 ModuleMap &ModMap
1203 = ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1204 bool Result;
1205 if (const FileEntry *ModuleMapFile =
1206 ModMap.getContainingModuleMapFile(Module)) {
1207 // Use the module map where this module resides.
1208 Result = compileModuleImpl(
1209 ImportingInstance, ImportLoc, Module->getTopLevelModuleName(),
1210 FrontendInputFile(ModuleMapFile->getName(), IK, +Module->IsSystem),
1211 ModMap.getModuleMapFileForUniquing(Module)->getName(),
1212 ModuleFileName);
1213 } else {
1214 // FIXME: We only need to fake up an input file here as a way of
1215 // transporting the module's directory to the module map parser. We should
1216 // be able to do that more directly, and parse from a memory buffer without
1217 // inventing this file.
1218 SmallString<128> FakeModuleMapFile(Module->Directory->getName());
1219 llvm::sys::path::append(FakeModuleMapFile, "__inferred_module.map");
1220
1221 std::string InferredModuleMapContent;
1222 llvm::raw_string_ostream OS(InferredModuleMapContent);
1223 Module->print(OS);
1224 OS.flush();
1225
1226 Result = compileModuleImpl(
1227 ImportingInstance, ImportLoc, Module->getTopLevelModuleName(),
1228 FrontendInputFile(FakeModuleMapFile, IK, +Module->IsSystem),
1229 ModMap.getModuleMapFileForUniquing(Module)->getName(),
1230 ModuleFileName,
1231 [&](CompilerInstance &Instance) {
1232 std::unique_ptr<llvm::MemoryBuffer> ModuleMapBuffer =
1233 llvm::MemoryBuffer::getMemBuffer(InferredModuleMapContent);
1234 ModuleMapFile = Instance.getFileManager().getVirtualFile(
1235 FakeModuleMapFile, InferredModuleMapContent.size(), 0);
1236 Instance.getSourceManager().overrideFileContents(
1237 ModuleMapFile, std::move(ModuleMapBuffer));
1238 });
1239 }
1240
1241 // We've rebuilt a module. If we're allowed to generate or update the global
1242 // module index, record that fact in the importing compiler instance.
1243 if (ImportingInstance.getFrontendOpts().GenerateGlobalModuleIndex) {
1244 ImportingInstance.setBuildGlobalModuleIndex(true);
1245 }
1246
1247 return Result;
1248}
1249
1250static bool compileAndLoadModule(CompilerInstance &ImportingInstance,
1251 SourceLocation ImportLoc,
1252 SourceLocation ModuleNameLoc, Module *Module,
1253 StringRef ModuleFileName) {
1254 DiagnosticsEngine &Diags = ImportingInstance.getDiagnostics();
1255
1256 auto diagnoseBuildFailure = [&] {
1257 Diags.Report(ModuleNameLoc, diag::err_module_not_built)
1258 << Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
1259 };
1260
1261 // FIXME: have LockFileManager return an error_code so that we can
1262 // avoid the mkdir when the directory already exists.
1263 StringRef Dir = llvm::sys::path::parent_path(ModuleFileName);
1264 llvm::sys::fs::create_directories(Dir);
1265
1266 while (1) {
1267 unsigned ModuleLoadCapabilities = ASTReader::ARR_Missing;
1268 llvm::LockFileManager Locked(ModuleFileName);
1269 switch (Locked) {
1270 case llvm::LockFileManager::LFS_Error:
1271 // PCMCache takes care of correctness and locks are only necessary for
1272 // performance. Fallback to building the module in case of any lock
1273 // related errors.
1274 Diags.Report(ModuleNameLoc, diag::remark_module_lock_failure)
1275 << Module->Name << Locked.getErrorMessage();
1276 // Clear out any potential leftover.
1277 Locked.unsafeRemoveLockFile();
1278 // FALLTHROUGH
1279 case llvm::LockFileManager::LFS_Owned:
1280 // We're responsible for building the module ourselves.
1281 if (!compileModuleImpl(ImportingInstance, ModuleNameLoc, Module,
1282 ModuleFileName)) {
1283 diagnoseBuildFailure();
1284 return false;
1285 }
1286 break;
1287
1288 case llvm::LockFileManager::LFS_Shared:
1289 // Someone else is responsible for building the module. Wait for them to
1290 // finish.
1291 switch (Locked.waitForUnlock()) {
1292 case llvm::LockFileManager::Res_Success:
1293 ModuleLoadCapabilities |= ASTReader::ARR_OutOfDate;
1294 break;
1295 case llvm::LockFileManager::Res_OwnerDied:
1296 continue; // try again to get the lock.
1297 case llvm::LockFileManager::Res_Timeout:
1298 // Since PCMCache takes care of correctness, we try waiting for another
1299 // process to complete the build so clang does not do it done twice. If
1300 // case of timeout, build it ourselves.
1301 Diags.Report(ModuleNameLoc, diag::remark_module_lock_timeout)
1302 << Module->Name;
1303 // Clear the lock file so that future invocations can make progress.
1304 Locked.unsafeRemoveLockFile();
1305 continue;
1306 }
1307 break;
1308 }
1309
1310 // Try to read the module file, now that we've compiled it.
1311 ASTReader::ASTReadResult ReadResult =
1312 ImportingInstance.getModuleManager()->ReadAST(
1313 ModuleFileName, serialization::MK_ImplicitModule, ImportLoc,
1314 ModuleLoadCapabilities);
1315
1316 if (ReadResult == ASTReader::OutOfDate &&
1317 Locked == llvm::LockFileManager::LFS_Shared) {
1318 // The module may be out of date in the presence of file system races,
1319 // or if one of its imports depends on header search paths that are not
1320 // consistent with this ImportingInstance. Try again...
1321 continue;
1322 } else if (ReadResult == ASTReader::Missing) {
1323 diagnoseBuildFailure();
1324 } else if (ReadResult != ASTReader::Success &&
1325 !Diags.hasErrorOccurred()) {
1326 // The ASTReader didn't diagnose the error, so conservatively report it.
1327 diagnoseBuildFailure();
1328 }
1329 return ReadResult == ASTReader::Success;
1330 }
1331}
1332
1333/// \brief Diagnose differences between the current definition of the given
1334/// configuration macro and the definition provided on the command line.
1335static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro,
1336 Module *Mod, SourceLocation ImportLoc) {
1337 IdentifierInfo *Id = PP.getIdentifierInfo(ConfigMacro);
1338 SourceManager &SourceMgr = PP.getSourceManager();
1339
1340 // If this identifier has never had a macro definition, then it could
1341 // not have changed.
1342 if (!Id->hadMacroDefinition())
1343 return;
1344 auto *LatestLocalMD = PP.getLocalMacroDirectiveHistory(Id);
1345
1346 // Find the macro definition from the command line.
1347 MacroInfo *CmdLineDefinition = nullptr;
1348 for (auto *MD = LatestLocalMD; MD; MD = MD->getPrevious()) {
1349 // We only care about the predefines buffer.
1350 FileID FID = SourceMgr.getFileID(MD->getLocation());
1351 if (FID.isInvalid() || FID != PP.getPredefinesFileID())
1352 continue;
1353 if (auto *DMD = dyn_cast<DefMacroDirective>(MD))
1354 CmdLineDefinition = DMD->getMacroInfo();
1355 break;
1356 }
1357
1358 auto *CurrentDefinition = PP.getMacroInfo(Id);
1359 if (CurrentDefinition == CmdLineDefinition) {
1360 // Macro matches. Nothing to do.
1361 } else if (!CurrentDefinition) {
1362 // This macro was defined on the command line, then #undef'd later.
1363 // Complain.
1364 PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
1365 << true << ConfigMacro << Mod->getFullModuleName();
1366 auto LatestDef = LatestLocalMD->getDefinition();
1367 assert(LatestDef.isUndefined() &&(static_cast <bool> (LatestDef.isUndefined() &&
"predefined macro went away with no #undef?") ? void (0) : __assert_fail
("LatestDef.isUndefined() && \"predefined macro went away with no #undef?\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 1368, __extension__ __PRETTY_FUNCTION__))
1368 "predefined macro went away with no #undef?")(static_cast <bool> (LatestDef.isUndefined() &&
"predefined macro went away with no #undef?") ? void (0) : __assert_fail
("LatestDef.isUndefined() && \"predefined macro went away with no #undef?\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 1368, __extension__ __PRETTY_FUNCTION__))
;
1369 PP.Diag(LatestDef.getUndefLocation(), diag::note_module_def_undef_here)
1370 << true;
1371 return;
1372 } else if (!CmdLineDefinition) {
1373 // There was no definition for this macro in the predefines buffer,
1374 // but there was a local definition. Complain.
1375 PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
1376 << false << ConfigMacro << Mod->getFullModuleName();
1377 PP.Diag(CurrentDefinition->getDefinitionLoc(),
1378 diag::note_module_def_undef_here)
1379 << false;
1380 } else if (!CurrentDefinition->isIdenticalTo(*CmdLineDefinition, PP,
1381 /*Syntactically=*/true)) {
1382 // The macro definitions differ.
1383 PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
1384 << false << ConfigMacro << Mod->getFullModuleName();
1385 PP.Diag(CurrentDefinition->getDefinitionLoc(),
1386 diag::note_module_def_undef_here)
1387 << false;
1388 }
1389}
1390
1391/// \brief Write a new timestamp file with the given path.
1392static void writeTimestampFile(StringRef TimestampFile) {
1393 std::error_code EC;
1394 llvm::raw_fd_ostream Out(TimestampFile.str(), EC, llvm::sys::fs::F_None);
1395}
1396
1397/// \brief Prune the module cache of modules that haven't been accessed in
1398/// a long time.
1399static void pruneModuleCache(const HeaderSearchOptions &HSOpts) {
1400 struct stat StatBuf;
1401 llvm::SmallString<128> TimestampFile;
1402 TimestampFile = HSOpts.ModuleCachePath;
1403 assert(!TimestampFile.empty())(static_cast <bool> (!TimestampFile.empty()) ? void (0)
: __assert_fail ("!TimestampFile.empty()", "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 1403, __extension__ __PRETTY_FUNCTION__))
;
1404 llvm::sys::path::append(TimestampFile, "modules.timestamp");
1405
1406 // Try to stat() the timestamp file.
1407 if (::stat(TimestampFile.c_str(), &StatBuf)) {
1408 // If the timestamp file wasn't there, create one now.
1409 if (errno(*__errno_location ()) == ENOENT2) {
1410 writeTimestampFile(TimestampFile);
1411 }
1412 return;
1413 }
1414
1415 // Check whether the time stamp is older than our pruning interval.
1416 // If not, do nothing.
1417 time_t TimeStampModTime = StatBuf.st_mtimest_mtim.tv_sec;
1418 time_t CurrentTime = time(nullptr);
1419 if (CurrentTime - TimeStampModTime <= time_t(HSOpts.ModuleCachePruneInterval))
1420 return;
1421
1422 // Write a new timestamp file so that nobody else attempts to prune.
1423 // There is a benign race condition here, if two Clang instances happen to
1424 // notice at the same time that the timestamp is out-of-date.
1425 writeTimestampFile(TimestampFile);
1426
1427 // Walk the entire module cache, looking for unused module files and module
1428 // indices.
1429 std::error_code EC;
1430 SmallString<128> ModuleCachePathNative;
1431 llvm::sys::path::native(HSOpts.ModuleCachePath, ModuleCachePathNative);
1432 for (llvm::sys::fs::directory_iterator Dir(ModuleCachePathNative, EC), DirEnd;
1433 Dir != DirEnd && !EC; Dir.increment(EC)) {
1434 // If we don't have a directory, there's nothing to look into.
1435 if (!llvm::sys::fs::is_directory(Dir->path()))
1436 continue;
1437
1438 // Walk all of the files within this directory.
1439 for (llvm::sys::fs::directory_iterator File(Dir->path(), EC), FileEnd;
1440 File != FileEnd && !EC; File.increment(EC)) {
1441 // We only care about module and global module index files.
1442 StringRef Extension = llvm::sys::path::extension(File->path());
1443 if (Extension != ".pcm" && Extension != ".timestamp" &&
1444 llvm::sys::path::filename(File->path()) != "modules.idx")
1445 continue;
1446
1447 // Look at this file. If we can't stat it, there's nothing interesting
1448 // there.
1449 if (::stat(File->path().c_str(), &StatBuf))
1450 continue;
1451
1452 // If the file has been used recently enough, leave it there.
1453 time_t FileAccessTime = StatBuf.st_atimest_atim.tv_sec;
1454 if (CurrentTime - FileAccessTime <=
1455 time_t(HSOpts.ModuleCachePruneAfter)) {
1456 continue;
1457 }
1458
1459 // Remove the file.
1460 llvm::sys::fs::remove(File->path());
1461
1462 // Remove the timestamp file.
1463 std::string TimpestampFilename = File->path() + ".timestamp";
1464 llvm::sys::fs::remove(TimpestampFilename);
1465 }
1466
1467 // If we removed all of the files in the directory, remove the directory
1468 // itself.
1469 if (llvm::sys::fs::directory_iterator(Dir->path(), EC) ==
1470 llvm::sys::fs::directory_iterator() && !EC)
1471 llvm::sys::fs::remove(Dir->path());
1472 }
1473}
1474
1475void CompilerInstance::createModuleManager() {
1476 if (!ModuleManager) {
1477 if (!hasASTContext())
1478 createASTContext();
1479
1480 // If we're implicitly building modules but not currently recursively
1481 // building a module, check whether we need to prune the module cache.
1482 if (getSourceManager().getModuleBuildStack().empty() &&
1483 !getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty() &&
1484 getHeaderSearchOpts().ModuleCachePruneInterval > 0 &&
1485 getHeaderSearchOpts().ModuleCachePruneAfter > 0) {
1486 pruneModuleCache(getHeaderSearchOpts());
1487 }
1488
1489 HeaderSearchOptions &HSOpts = getHeaderSearchOpts();
1490 std::string Sysroot = HSOpts.Sysroot;
1491 const PreprocessorOptions &PPOpts = getPreprocessorOpts();
1492 std::unique_ptr<llvm::Timer> ReadTimer;
1493 if (FrontendTimerGroup)
1494 ReadTimer = llvm::make_unique<llvm::Timer>("reading_modules",
1495 "Reading modules",
1496 *FrontendTimerGroup);
1497 ModuleManager = new ASTReader(
1498 getPreprocessor(), &getASTContext(), getPCHContainerReader(),
1499 getFrontendOpts().ModuleFileExtensions,
1500 Sysroot.empty() ? "" : Sysroot.c_str(), PPOpts.DisablePCHValidation,
1501 /*AllowASTWithCompilerErrors=*/false,
1502 /*AllowConfigurationMismatch=*/false,
1503 HSOpts.ModulesValidateSystemHeaders,
1504 getFrontendOpts().UseGlobalModuleIndex,
1505 std::move(ReadTimer));
1506 if (hasASTConsumer()) {
1507 ModuleManager->setDeserializationListener(
1508 getASTConsumer().GetASTDeserializationListener());
1509 getASTContext().setASTMutationListener(
1510 getASTConsumer().GetASTMutationListener());
1511 }
1512 getASTContext().setExternalSource(ModuleManager);
1513 if (hasSema())
1514 ModuleManager->InitializeSema(getSema());
1515 if (hasASTConsumer())
1516 ModuleManager->StartTranslationUnit(&getASTConsumer());
1517
1518 if (TheDependencyFileGenerator)
1519 TheDependencyFileGenerator->AttachToASTReader(*ModuleManager);
1520 for (auto &Listener : DependencyCollectors)
1521 Listener->attachToASTReader(*ModuleManager);
1522 }
1523}
1524
1525bool CompilerInstance::loadModuleFile(StringRef FileName) {
1526 llvm::Timer Timer;
1527 if (FrontendTimerGroup)
1
Taking false branch
1528 Timer.init("preloading." + FileName.str(), "Preloading " + FileName.str(),
1529 *FrontendTimerGroup);
1530 llvm::TimeRegion TimeLoading(FrontendTimerGroup ? &Timer : nullptr);
2
'?' condition is false
1531
1532 // Helper to recursively read the module names for all modules we're adding.
1533 // We mark these as known and redirect any attempt to load that module to
1534 // the files we were handed.
1535 struct ReadModuleNames : ASTReaderListener {
1536 CompilerInstance &CI;
1537 llvm::SmallVector<IdentifierInfo*, 8> LoadedModules;
1538
1539 ReadModuleNames(CompilerInstance &CI) : CI(CI) {}
1540
1541 void ReadModuleName(StringRef ModuleName) override {
1542 LoadedModules.push_back(
1543 CI.getPreprocessor().getIdentifierInfo(ModuleName));
1544 }
1545
1546 void registerAll() {
1547 for (auto *II : LoadedModules) {
1548 CI.KnownModules[II] = CI.getPreprocessor()
1549 .getHeaderSearchInfo()
1550 .getModuleMap()
1551 .findModule(II->getName());
1552 }
1553 LoadedModules.clear();
1554 }
1555
1556 void markAllUnavailable() {
1557 for (auto *II : LoadedModules) {
1558 if (Module *M = CI.getPreprocessor()
1559 .getHeaderSearchInfo()
1560 .getModuleMap()
1561 .findModule(II->getName())) {
1562 M->HasIncompatibleModuleFile = true;
1563
1564 // Mark module as available if the only reason it was unavailable
1565 // was missing headers.
1566 SmallVector<Module *, 2> Stack;
1567 Stack.push_back(M);
1568 while (!Stack.empty()) {
1569 Module *Current = Stack.pop_back_val();
1570 if (Current->IsMissingRequirement) continue;
1571 Current->IsAvailable = true;
1572 Stack.insert(Stack.end(),
1573 Current->submodule_begin(), Current->submodule_end());
1574 }
1575 }
1576 }
1577 LoadedModules.clear();
1578 }
1579 };
1580
1581 // If we don't already have an ASTReader, create one now.
1582 if (!ModuleManager)
3
Assuming the condition is false
4
Taking false branch
1583 createModuleManager();
1584
1585 auto Listener = llvm::make_unique<ReadModuleNames>(*this);
5
Calling 'make_unique'
7
Returned allocated memory
1586 auto &ListenerRef = *Listener;
1587 ASTReader::ListenerScope ReadModuleNamesListener(*ModuleManager,
1588 std::move(Listener));
8
Calling '~unique_ptr'
13
Returning from '~unique_ptr'
1589
1590 // Try to load the module file.
1591 switch (ModuleManager->ReadAST(FileName, serialization::MK_ExplicitModule,
14
Control jumps to 'case Success:' at line 1594
1592 SourceLocation(),
1593 ASTReader::ARR_ConfigurationMismatch)) {
1594 case ASTReader::Success:
1595 // We successfully loaded the module file; remember the set of provided
1596 // modules so that we don't try to load implicit modules for them.
1597 ListenerRef.registerAll();
15
Use of memory after it is freed
1598 return true;
1599
1600 case ASTReader::ConfigurationMismatch:
1601 // Ignore unusable module files.
1602 getDiagnostics().Report(SourceLocation(), diag::warn_module_config_mismatch)
1603 << FileName;
1604 // All modules provided by any files we tried and failed to load are now
1605 // unavailable; includes of those modules should now be handled textually.
1606 ListenerRef.markAllUnavailable();
1607 return true;
1608
1609 default:
1610 return false;
1611 }
1612}
1613
1614ModuleLoadResult
1615CompilerInstance::loadModule(SourceLocation ImportLoc,
1616 ModuleIdPath Path,
1617 Module::NameVisibilityKind Visibility,
1618 bool IsInclusionDirective) {
1619 // Determine what file we're searching from.
1620 // FIXME: Should we be deciding whether this is a submodule (here and
1621 // below) based on -fmodules-ts or should we pass a flag and make the
1622 // caller decide?
1623 std::string ModuleName;
1624 if (getLangOpts().ModulesTS) {
1625 // FIXME: Same code as Sema::ActOnModuleDecl() so there is probably a
1626 // better place/way to do this.
1627 for (auto &Piece : Path) {
1628 if (!ModuleName.empty())
1629 ModuleName += ".";
1630 ModuleName += Piece.first->getName();
1631 }
1632 }
1633 else
1634 ModuleName = Path[0].first->getName();
1635
1636 SourceLocation ModuleNameLoc = Path[0].second;
1637
1638 // If we've already handled this import, just return the cached result.
1639 // This one-element cache is important to eliminate redundant diagnostics
1640 // when both the preprocessor and parser see the same import declaration.
1641 if (ImportLoc.isValid() && LastModuleImportLoc == ImportLoc) {
1642 // Make the named module visible.
1643 if (LastModuleImportResult && ModuleName != getLangOpts().CurrentModule)
1644 ModuleManager->makeModuleVisible(LastModuleImportResult, Visibility,
1645 ImportLoc);
1646 return LastModuleImportResult;
1647 }
1648
1649 clang::Module *Module = nullptr;
1650
1651 // If we don't already have information on this module, load the module now.
1652 llvm::DenseMap<const IdentifierInfo *, clang::Module *>::iterator Known
1653 = KnownModules.find(Path[0].first);
1654 if (Known != KnownModules.end()) {
1655 // Retrieve the cached top-level module.
1656 Module = Known->second;
1657 } else if (ModuleName == getLangOpts().CurrentModule) {
1658 // This is the module we're building.
1659 Module = PP->getHeaderSearchInfo().lookupModule(ModuleName);
1660 /// FIXME: perhaps we should (a) look for a module using the module name
1661 // to file map (PrebuiltModuleFiles) and (b) diagnose if still not found?
1662 //if (Module == nullptr) {
1663 // getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found)
1664 // << ModuleName;
1665 // ModuleBuildFailed = true;
1666 // return ModuleLoadResult();
1667 //}
1668 Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
1669 } else {
1670 // Search for a module with the given name.
1671 Module = PP->getHeaderSearchInfo().lookupModule(ModuleName);
1672 HeaderSearchOptions &HSOpts =
1673 PP->getHeaderSearchInfo().getHeaderSearchOpts();
1674
1675 std::string ModuleFileName;
1676 enum ModuleSource {
1677 ModuleNotFound, ModuleCache, PrebuiltModulePath, ModuleBuildPragma
1678 } Source = ModuleNotFound;
1679
1680 // Check to see if the module has been built as part of this compilation
1681 // via a module build pragma.
1682 auto BuiltModuleIt = BuiltModules.find(ModuleName);
1683 if (BuiltModuleIt != BuiltModules.end()) {
1684 ModuleFileName = BuiltModuleIt->second;
1685 Source = ModuleBuildPragma;
1686 }
1687
1688 // Try to load the module from the prebuilt module path.
1689 if (Source == ModuleNotFound && (!HSOpts.PrebuiltModuleFiles.empty() ||
1690 !HSOpts.PrebuiltModulePaths.empty())) {
1691 ModuleFileName =
1692 PP->getHeaderSearchInfo().getPrebuiltModuleFileName(ModuleName);
1693 if (!ModuleFileName.empty())
1694 Source = PrebuiltModulePath;
1695 }
1696
1697 // Try to load the module from the module cache.
1698 if (Source == ModuleNotFound && Module) {
1699 ModuleFileName = PP->getHeaderSearchInfo().getCachedModuleFileName(Module);
1700 Source = ModuleCache;
1701 }
1702
1703 if (Source == ModuleNotFound) {
1704 // We can't find a module, error out here.
1705 getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found)
1706 << ModuleName << SourceRange(ImportLoc, ModuleNameLoc);
1707 ModuleBuildFailed = true;
1708 return ModuleLoadResult();
1709 }
1710
1711 if (ModuleFileName.empty()) {
1712 if (Module && Module->HasIncompatibleModuleFile) {
1713 // We tried and failed to load a module file for this module. Fall
1714 // back to textual inclusion for its headers.
1715 return ModuleLoadResult::ConfigMismatch;
1716 }
1717
1718 getDiagnostics().Report(ModuleNameLoc, diag::err_module_build_disabled)
1719 << ModuleName;
1720 ModuleBuildFailed = true;
1721 return ModuleLoadResult();
1722 }
1723
1724 // If we don't already have an ASTReader, create one now.
1725 if (!ModuleManager)
1726 createModuleManager();
1727
1728 llvm::Timer Timer;
1729 if (FrontendTimerGroup)
1730 Timer.init("loading." + ModuleFileName, "Loading " + ModuleFileName,
1731 *FrontendTimerGroup);
1732 llvm::TimeRegion TimeLoading(FrontendTimerGroup ? &Timer : nullptr);
1733
1734 // Try to load the module file. If we are not trying to load from the
1735 // module cache, we don't know how to rebuild modules.
1736 unsigned ARRFlags = Source == ModuleCache ?
1737 ASTReader::ARR_OutOfDate | ASTReader::ARR_Missing :
1738 ASTReader::ARR_ConfigurationMismatch;
1739 switch (ModuleManager->ReadAST(ModuleFileName,
1740 Source == PrebuiltModulePath
1741 ? serialization::MK_PrebuiltModule
1742 : Source == ModuleBuildPragma
1743 ? serialization::MK_ExplicitModule
1744 : serialization::MK_ImplicitModule,
1745 ImportLoc, ARRFlags)) {
1746 case ASTReader::Success: {
1747 if (Source != ModuleCache && !Module) {
1748 Module = PP->getHeaderSearchInfo().lookupModule(ModuleName);
1749 if (!Module || !Module->getASTFile() ||
1750 FileMgr->getFile(ModuleFileName) != Module->getASTFile()) {
1751 // Error out if Module does not refer to the file in the prebuilt
1752 // module path.
1753 getDiagnostics().Report(ModuleNameLoc, diag::err_module_prebuilt)
1754 << ModuleName;
1755 ModuleBuildFailed = true;
1756 KnownModules[Path[0].first] = nullptr;
1757 return ModuleLoadResult();
1758 }
1759 }
1760 break;
1761 }
1762
1763 case ASTReader::OutOfDate:
1764 case ASTReader::Missing: {
1765 if (Source != ModuleCache) {
1766 // We don't know the desired configuration for this module and don't
1767 // necessarily even have a module map. Since ReadAST already produces
1768 // diagnostics for these two cases, we simply error out here.
1769 ModuleBuildFailed = true;
1770 KnownModules[Path[0].first] = nullptr;
1771 return ModuleLoadResult();
1772 }
1773
1774 // The module file is missing or out-of-date. Build it.
1775 assert(Module && "missing module file")(static_cast <bool> (Module && "missing module file"
) ? void (0) : __assert_fail ("Module && \"missing module file\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 1775, __extension__ __PRETTY_FUNCTION__))
;
1776 // Check whether there is a cycle in the module graph.
1777 ModuleBuildStack ModPath = getSourceManager().getModuleBuildStack();
1778 ModuleBuildStack::iterator Pos = ModPath.begin(), PosEnd = ModPath.end();
1779 for (; Pos != PosEnd; ++Pos) {
1780 if (Pos->first == ModuleName)
1781 break;
1782 }
1783
1784 if (Pos != PosEnd) {
1785 SmallString<256> CyclePath;
1786 for (; Pos != PosEnd; ++Pos) {
1787 CyclePath += Pos->first;
1788 CyclePath += " -> ";
1789 }
1790 CyclePath += ModuleName;
1791
1792 getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle)
1793 << ModuleName << CyclePath;
1794 return ModuleLoadResult();
1795 }
1796
1797 // Check whether we have already attempted to build this module (but
1798 // failed).
1799 if (getPreprocessorOpts().FailedModules &&
1800 getPreprocessorOpts().FailedModules->hasAlreadyFailed(ModuleName)) {
1801 getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built)
1802 << ModuleName
1803 << SourceRange(ImportLoc, ModuleNameLoc);
1804 ModuleBuildFailed = true;
1805 return ModuleLoadResult();
1806 }
1807
1808 // Try to compile and then load the module.
1809 if (!compileAndLoadModule(*this, ImportLoc, ModuleNameLoc, Module,
1810 ModuleFileName)) {
1811 assert(getDiagnostics().hasErrorOccurred() &&(static_cast <bool> (getDiagnostics().hasErrorOccurred(
) && "undiagnosed error in compileAndLoadModule") ? void
(0) : __assert_fail ("getDiagnostics().hasErrorOccurred() && \"undiagnosed error in compileAndLoadModule\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 1812, __extension__ __PRETTY_FUNCTION__))
1812 "undiagnosed error in compileAndLoadModule")(static_cast <bool> (getDiagnostics().hasErrorOccurred(
) && "undiagnosed error in compileAndLoadModule") ? void
(0) : __assert_fail ("getDiagnostics().hasErrorOccurred() && \"undiagnosed error in compileAndLoadModule\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Frontend/CompilerInstance.cpp"
, 1812, __extension__ __PRETTY_FUNCTION__))
;
1813 if (getPreprocessorOpts().FailedModules)
1814 getPreprocessorOpts().FailedModules->addFailed(ModuleName);
1815 KnownModules[Path[0].first] = nullptr;
1816 ModuleBuildFailed = true;
1817 return ModuleLoadResult();
1818 }
1819
1820 // Okay, we've rebuilt and now loaded the module.
1821 break;
1822 }
1823
1824 case ASTReader::ConfigurationMismatch:
1825 if (Source == PrebuiltModulePath)
1826 // FIXME: We shouldn't be setting HadFatalFailure below if we only
1827 // produce a warning here!
1828 getDiagnostics().Report(SourceLocation(),
1829 diag::warn_module_config_mismatch)
1830 << ModuleFileName;
1831 // Fall through to error out.
1832 LLVM_FALLTHROUGH[[clang::fallthrough]];
1833 case ASTReader::VersionMismatch:
1834 case ASTReader::HadErrors:
1835 ModuleLoader::HadFatalFailure = true;
1836 // FIXME: The ASTReader will already have complained, but can we shoehorn
1837 // that diagnostic information into a more useful form?
1838 KnownModules[Path[0].first] = nullptr;
1839 return ModuleLoadResult();
1840
1841 case ASTReader::Failure:
1842 ModuleLoader::HadFatalFailure = true;
1843 // Already complained, but note now that we failed.
1844 KnownModules[Path[0].first] = nullptr;
1845 ModuleBuildFailed = true;
1846 return ModuleLoadResult();
1847 }
1848
1849 // Cache the result of this top-level module lookup for later.
1850 Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
1851 }
1852
1853 // If we never found the module, fail.
1854 if (!Module)
1855 return ModuleLoadResult();
1856
1857 // Verify that the rest of the module path actually corresponds to
1858 // a submodule.
1859 bool MapPrivateSubModToTopLevel = false;
1860 if (!getLangOpts().ModulesTS && Path.size() > 1) {
1861 for (unsigned I = 1, N = Path.size(); I != N; ++I) {
1862 StringRef Name = Path[I].first->getName();
1863 clang::Module *Sub = Module->findSubmodule(Name);
1864
1865 // If the user is requesting Foo.Private and it doesn't exist, try to
1866 // match Foo_Private and emit a warning asking for the user to write
1867 // @import Foo_Private instead. FIXME: remove this when existing clients
1868 // migrate off of Foo.Private syntax.
1869 if (!Sub && PP->getLangOpts().ImplicitModules && Name == "Private" &&
1870 Module == Module->getTopLevelModule()) {
1871 SmallString<128> PrivateModule(Module->Name);
1872 PrivateModule.append("_Private");
1873
1874 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> PrivPath;
1875 auto &II = PP->getIdentifierTable().get(
1876 PrivateModule, PP->getIdentifierInfo(Module->Name)->getTokenID());
1877 PrivPath.push_back(std::make_pair(&II, Path[0].second));
1878
1879 if (PP->getHeaderSearchInfo().lookupModule(PrivateModule))
1880 Sub =
1881 loadModule(ImportLoc, PrivPath, Visibility, IsInclusionDirective);
1882 if (Sub) {
1883 MapPrivateSubModToTopLevel = true;
1884 if (!getDiagnostics().isIgnored(
1885 diag::warn_no_priv_submodule_use_toplevel, ImportLoc)) {
1886 getDiagnostics().Report(Path[I].second,
1887 diag::warn_no_priv_submodule_use_toplevel)
1888 << Path[I].first << Module->getFullModuleName() << PrivateModule
1889 << SourceRange(Path[0].second, Path[I].second)
1890 << FixItHint::CreateReplacement(SourceRange(Path[0].second),
1891 PrivateModule);
1892 getDiagnostics().Report(Sub->DefinitionLoc,
1893 diag::note_private_top_level_defined);
1894 }
1895 }
1896 }
1897
1898 if (!Sub) {
1899 // Attempt to perform typo correction to find a module name that works.
1900 SmallVector<StringRef, 2> Best;
1901 unsigned BestEditDistance = (std::numeric_limits<unsigned>::max)();
1902
1903 for (clang::Module::submodule_iterator J = Module->submodule_begin(),
1904 JEnd = Module->submodule_end();
1905 J != JEnd; ++J) {
1906 unsigned ED = Name.edit_distance((*J)->Name,
1907 /*AllowReplacements=*/true,
1908 BestEditDistance);
1909 if (ED <= BestEditDistance) {
1910 if (ED < BestEditDistance) {
1911 Best.clear();
1912 BestEditDistance = ED;
1913 }
1914
1915 Best.push_back((*J)->Name);
1916 }
1917 }
1918
1919 // If there was a clear winner, user it.
1920 if (Best.size() == 1) {
1921 getDiagnostics().Report(Path[I].second,
1922 diag::err_no_submodule_suggest)
1923 << Path[I].first << Module->getFullModuleName() << Best[0]
1924 << SourceRange(Path[0].second, Path[I-1].second)
1925 << FixItHint::CreateReplacement(SourceRange(Path[I].second),
1926 Best[0]);
1927
1928 Sub = Module->findSubmodule(Best[0]);
1929 }
1930 }
1931
1932 if (!Sub) {
1933 // No submodule by this name. Complain, and don't look for further
1934 // submodules.
1935 getDiagnostics().Report(Path[I].second, diag::err_no_submodule)
1936 << Path[I].first << Module->getFullModuleName()
1937 << SourceRange(Path[0].second, Path[I-1].second);
1938 break;
1939 }
1940
1941 Module = Sub;
1942 }
1943 }
1944
1945 // Make the named module visible, if it's not already part of the module
1946 // we are parsing.
1947 if (ModuleName != getLangOpts().CurrentModule) {
1948 if (!Module->IsFromModuleFile && !MapPrivateSubModToTopLevel) {
1949 // We have an umbrella header or directory that doesn't actually include
1950 // all of the headers within the directory it covers. Complain about
1951 // this missing submodule and recover by forgetting that we ever saw
1952 // this submodule.
1953 // FIXME: Should we detect this at module load time? It seems fairly
1954 // expensive (and rare).
1955 getDiagnostics().Report(ImportLoc, diag::warn_missing_submodule)
1956 << Module->getFullModuleName()
1957 << SourceRange(Path.front().second, Path.back().second);
1958
1959 return ModuleLoadResult::MissingExpected;
1960 }
1961
1962 // Check whether this module is available.
1963 if (Preprocessor::checkModuleIsAvailable(getLangOpts(), getTarget(),
1964 getDiagnostics(), Module)) {
1965 getDiagnostics().Report(ImportLoc, diag::note_module_import_here)
1966 << SourceRange(Path.front().second, Path.back().second);
1967 LastModuleImportLoc = ImportLoc;
1968 LastModuleImportResult = ModuleLoadResult();
1969 return ModuleLoadResult();
1970 }
1971
1972 ModuleManager->makeModuleVisible(Module, Visibility, ImportLoc);
1973 }
1974
1975 // Check for any configuration macros that have changed.
1976 clang::Module *TopModule = Module->getTopLevelModule();
1977 for (unsigned I = 0, N = TopModule->ConfigMacros.size(); I != N; ++I) {
1978 checkConfigMacro(getPreprocessor(), TopModule->ConfigMacros[I],
1979 Module, ImportLoc);
1980 }
1981
1982 LastModuleImportLoc = ImportLoc;
1983 LastModuleImportResult = ModuleLoadResult(Module);
1984 return LastModuleImportResult;
1985}
1986
1987void CompilerInstance::loadModuleFromSource(SourceLocation ImportLoc,
1988 StringRef ModuleName,
1989 StringRef Source) {
1990 // Avoid creating filenames with special characters.
1991 SmallString<128> CleanModuleName(ModuleName);
1992 for (auto &C : CleanModuleName)
1993 if (!isAlphanumeric(C))
1994 C = '_';
1995
1996 // FIXME: Using a randomized filename here means that our intermediate .pcm
1997 // output is nondeterministic (as .pcm files refer to each other by name).
1998 // Can this affect the output in any way?
1999 SmallString<128> ModuleFileName;
2000 if (std::error_code EC = llvm::sys::fs::createTemporaryFile(
2001 CleanModuleName, "pcm", ModuleFileName)) {
2002 getDiagnostics().Report(ImportLoc, diag::err_fe_unable_to_open_output)
2003 << ModuleFileName << EC.message();
2004 return;
2005 }
2006 std::string ModuleMapFileName = (CleanModuleName + ".map").str();
2007
2008 FrontendInputFile Input(
2009 ModuleMapFileName,
2010 InputKind(getLanguageFromOptions(*Invocation->getLangOpts()),
2011 InputKind::ModuleMap, /*Preprocessed*/true));
2012
2013 std::string NullTerminatedSource(Source.str());
2014
2015 auto PreBuildStep = [&](CompilerInstance &Other) {
2016 // Create a virtual file containing our desired source.
2017 // FIXME: We shouldn't need to do this.
2018 const FileEntry *ModuleMapFile = Other.getFileManager().getVirtualFile(
2019 ModuleMapFileName, NullTerminatedSource.size(), 0);
2020 Other.getSourceManager().overrideFileContents(
2021 ModuleMapFile,
2022 llvm::MemoryBuffer::getMemBuffer(NullTerminatedSource.c_str()));
2023
2024 Other.BuiltModules = std::move(BuiltModules);
2025 Other.DeleteBuiltModules = false;
2026 };
2027
2028 auto PostBuildStep = [this](CompilerInstance &Other) {
2029 BuiltModules = std::move(Other.BuiltModules);
2030 };
2031
2032 // Build the module, inheriting any modules that we've built locally.
2033 if (compileModuleImpl(*this, ImportLoc, ModuleName, Input, StringRef(),
2034 ModuleFileName, PreBuildStep, PostBuildStep)) {
2035 BuiltModules[ModuleName] = ModuleFileName.str();
2036 llvm::sys::RemoveFileOnSignal(ModuleFileName);
2037 }
2038}
2039
2040void CompilerInstance::makeModuleVisible(Module *Mod,
2041 Module::NameVisibilityKind Visibility,
2042 SourceLocation ImportLoc) {
2043 if (!ModuleManager)
2044 createModuleManager();
2045 if (!ModuleManager)
2046 return;
2047
2048 ModuleManager->makeModuleVisible(Mod, Visibility, ImportLoc);
2049}
2050
2051GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex(
2052 SourceLocation TriggerLoc) {
2053 if (getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty())
2054 return nullptr;
2055 if (!ModuleManager)
2056 createModuleManager();
2057 // Can't do anything if we don't have the module manager.
2058 if (!ModuleManager)
2059 return nullptr;
2060 // Get an existing global index. This loads it if not already
2061 // loaded.
2062 ModuleManager->loadGlobalIndex();
2063 GlobalModuleIndex *GlobalIndex = ModuleManager->getGlobalIndex();
2064 // If the global index doesn't exist, create it.
2065 if (!GlobalIndex && shouldBuildGlobalModuleIndex() && hasFileManager() &&
2066 hasPreprocessor()) {
2067 llvm::sys::fs::create_directories(
2068 getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
2069 GlobalModuleIndex::writeIndex(
2070 getFileManager(), getPCHContainerReader(),
2071 getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
2072 ModuleManager->resetForReload();
2073 ModuleManager->loadGlobalIndex();
2074 GlobalIndex = ModuleManager->getGlobalIndex();
2075 }
2076 // For finding modules needing to be imported for fixit messages,
2077 // we need to make the global index cover all modules, so we do that here.
2078 if (!HaveFullGlobalModuleIndex && GlobalIndex && !buildingModule()) {
2079 ModuleMap &MMap = getPreprocessor().getHeaderSearchInfo().getModuleMap();
2080 bool RecreateIndex = false;
2081 for (ModuleMap::module_iterator I = MMap.module_begin(),
2082 E = MMap.module_end(); I != E; ++I) {
2083 Module *TheModule = I->second;
2084 const FileEntry *Entry = TheModule->getASTFile();
2085 if (!Entry) {
2086 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
2087 Path.push_back(std::make_pair(
2088 getPreprocessor().getIdentifierInfo(TheModule->Name), TriggerLoc));
2089 std::reverse(Path.begin(), Path.end());
2090 // Load a module as hidden. This also adds it to the global index.
2091 loadModule(TheModule->DefinitionLoc, Path, Module::Hidden, false);
2092 RecreateIndex = true;
2093 }
2094 }
2095 if (RecreateIndex) {
2096 GlobalModuleIndex::writeIndex(
2097 getFileManager(), getPCHContainerReader(),
2098 getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
2099 ModuleManager->resetForReload();
2100 ModuleManager->loadGlobalIndex();
2101 GlobalIndex = ModuleManager->getGlobalIndex();
2102 }
2103 HaveFullGlobalModuleIndex = true;
2104 }
2105 return GlobalIndex;
2106}
2107
2108// Check global module index for missing imports.
2109bool
2110CompilerInstance::lookupMissingImports(StringRef Name,
2111 SourceLocation TriggerLoc) {
2112 // Look for the symbol in non-imported modules, but only if an error
2113 // actually occurred.
2114 if (!buildingModule()) {
2115 // Load global module index, or retrieve a previously loaded one.
2116 GlobalModuleIndex *GlobalIndex = loadGlobalModuleIndex(
2117 TriggerLoc);
2118
2119 // Only if we have a global index.
2120 if (GlobalIndex) {
2121 GlobalModuleIndex::HitSet FoundModules;
2122
2123 // Find the modules that reference the identifier.
2124 // Note that this only finds top-level modules.
2125 // We'll let diagnoseTypo find the actual declaration module.
2126 if (GlobalIndex->lookupIdentifier(Name, FoundModules))
2127 return true;
2128 }
2129 }
2130
2131 return false;
2132}
2133void CompilerInstance::resetAndLeakSema() { BuryPointer(takeSema()); }
2134
2135void CompilerInstance::setExternalSemaSource(
2136 IntrusiveRefCntPtr<ExternalSemaSource> ESS) {
2137 ExternalSemaSrc = std::move(ESS);
2138}

/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/ADT/STLExtras.h

1//===- llvm/ADT/STLExtras.h - Useful STL related functions ------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains some templates that are useful if you are working with the
11// STL at all.
12//
13// No library is required when using these functions.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_ADT_STLEXTRAS_H
18#define LLVM_ADT_STLEXTRAS_H
19
20#include "llvm/ADT/Optional.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/iterator.h"
23#include "llvm/ADT/iterator_range.h"
24#include "llvm/Support/ErrorHandling.h"
25#include <algorithm>
26#include <cassert>
27#include <cstddef>
28#include <cstdint>
29#include <cstdlib>
30#include <functional>
31#include <initializer_list>
32#include <iterator>
33#include <limits>
34#include <memory>
35#include <tuple>
36#include <type_traits>
37#include <utility>
38
39#ifdef EXPENSIVE_CHECKS
40#include <random> // for std::mt19937
41#endif
42
43namespace llvm {
44
45// Only used by compiler if both template types are the same. Useful when
46// using SFINAE to test for the existence of member functions.
47template <typename T, T> struct SameType;
48
49namespace detail {
50
51template <typename RangeT>
52using IterOfRange = decltype(std::begin(std::declval<RangeT &>()));
53
54template <typename RangeT>
55using ValueOfRange = typename std::remove_reference<decltype(
56 *std::begin(std::declval<RangeT &>()))>::type;
57
58} // end namespace detail
59
60//===----------------------------------------------------------------------===//
61// Extra additions to <functional>
62//===----------------------------------------------------------------------===//
63
64template <class Ty> struct identity {
65 using argument_type = Ty;
66
67 Ty &operator()(Ty &self) const {
68 return self;
69 }
70 const Ty &operator()(const Ty &self) const {
71 return self;
72 }
73};
74
75template <class Ty> struct less_ptr {
76 bool operator()(const Ty* left, const Ty* right) const {
77 return *left < *right;
78 }
79};
80
81template <class Ty> struct greater_ptr {
82 bool operator()(const Ty* left, const Ty* right) const {
83 return *right < *left;
84 }
85};
86
87/// An efficient, type-erasing, non-owning reference to a callable. This is
88/// intended for use as the type of a function parameter that is not used
89/// after the function in question returns.
90///
91/// This class does not own the callable, so it is not in general safe to store
92/// a function_ref.
93template<typename Fn> class function_ref;
94
95template<typename Ret, typename ...Params>
96class function_ref<Ret(Params...)> {
97 Ret (*callback)(intptr_t callable, Params ...params) = nullptr;
98 intptr_t callable;
99
100 template<typename Callable>
101 static Ret callback_fn(intptr_t callable, Params ...params) {
102 return (*reinterpret_cast<Callable*>(callable))(
103 std::forward<Params>(params)...);
104 }
105
106public:
107 function_ref() = default;
108 function_ref(std::nullptr_t) {}
109
110 template <typename Callable>
111 function_ref(Callable &&callable,
112 typename std::enable_if<
113 !std::is_same<typename std::remove_reference<Callable>::type,
114 function_ref>::value>::type * = nullptr)
115 : callback(callback_fn<typename std::remove_reference<Callable>::type>),
116 callable(reinterpret_cast<intptr_t>(&callable)) {}
117
118 Ret operator()(Params ...params) const {
119 return callback(callable, std::forward<Params>(params)...);
120 }
121
122 operator bool() const { return callback; }
123};
124
125// deleter - Very very very simple method that is used to invoke operator
126// delete on something. It is used like this:
127//
128// for_each(V.begin(), B.end(), deleter<Interval>);
129template <class T>
130inline void deleter(T *Ptr) {
131 delete Ptr;
132}
133
134//===----------------------------------------------------------------------===//
135// Extra additions to <iterator>
136//===----------------------------------------------------------------------===//
137
138namespace adl_detail {
139
140using std::begin;
141
142template <typename ContainerTy>
143auto adl_begin(ContainerTy &&container)
144 -> decltype(begin(std::forward<ContainerTy>(container))) {
145 return begin(std::forward<ContainerTy>(container));
146}
147
148using std::end;
149
150template <typename ContainerTy>
151auto adl_end(ContainerTy &&container)
152 -> decltype(end(std::forward<ContainerTy>(container))) {
153 return end(std::forward<ContainerTy>(container));
154}
155
156using std::swap;
157
158template <typename T>
159void adl_swap(T &&lhs, T &&rhs) noexcept(noexcept(swap(std::declval<T>(),
160 std::declval<T>()))) {
161 swap(std::forward<T>(lhs), std::forward<T>(rhs));
162}
163
164} // end namespace adl_detail
165
166template <typename ContainerTy>
167auto adl_begin(ContainerTy &&container)
168 -> decltype(adl_detail::adl_begin(std::forward<ContainerTy>(container))) {
169 return adl_detail::adl_begin(std::forward<ContainerTy>(container));
170}
171
172template <typename ContainerTy>
173auto adl_end(ContainerTy &&container)
174 -> decltype(adl_detail::adl_end(std::forward<ContainerTy>(container))) {
175 return adl_detail::adl_end(std::forward<ContainerTy>(container));
176}
177
178template <typename T>
179void adl_swap(T &&lhs, T &&rhs) noexcept(
180 noexcept(adl_detail::adl_swap(std::declval<T>(), std::declval<T>()))) {
181 adl_detail::adl_swap(std::forward<T>(lhs), std::forward<T>(rhs));
182}
183
184// mapped_iterator - This is a simple iterator adapter that causes a function to
185// be applied whenever operator* is invoked on the iterator.
186
187template <typename ItTy, typename FuncTy,
188 typename FuncReturnTy =
189 decltype(std::declval<FuncTy>()(*std::declval<ItTy>()))>
190class mapped_iterator
191 : public iterator_adaptor_base<
192 mapped_iterator<ItTy, FuncTy>, ItTy,
193 typename std::iterator_traits<ItTy>::iterator_category,
194 typename std::remove_reference<FuncReturnTy>::type> {
195public:
196 mapped_iterator(ItTy U, FuncTy F)
197 : mapped_iterator::iterator_adaptor_base(std::move(U)), F(std::move(F)) {}
198
199 ItTy getCurrent() { return this->I; }
200
201 FuncReturnTy operator*() { return F(*this->I); }
202
203private:
204 FuncTy F;
205};
206
207// map_iterator - Provide a convenient way to create mapped_iterators, just like
208// make_pair is useful for creating pairs...
209template <class ItTy, class FuncTy>
210inline mapped_iterator<ItTy, FuncTy> map_iterator(ItTy I, FuncTy F) {
211 return mapped_iterator<ItTy, FuncTy>(std::move(I), std::move(F));
212}
213
214/// Helper to determine if type T has a member called rbegin().
215template <typename Ty> class has_rbegin_impl {
216 using yes = char[1];
217 using no = char[2];
218
219 template <typename Inner>
220 static yes& test(Inner *I, decltype(I->rbegin()) * = nullptr);
221
222 template <typename>
223 static no& test(...);
224
225public:
226 static const bool value = sizeof(test<Ty>(nullptr)) == sizeof(yes);
227};
228
229/// Metafunction to determine if T& or T has a member called rbegin().
230template <typename Ty>
231struct has_rbegin : has_rbegin_impl<typename std::remove_reference<Ty>::type> {
232};
233
234// Returns an iterator_range over the given container which iterates in reverse.
235// Note that the container must have rbegin()/rend() methods for this to work.
236template <typename ContainerTy>
237auto reverse(ContainerTy &&C,
238 typename std::enable_if<has_rbegin<ContainerTy>::value>::type * =
239 nullptr) -> decltype(make_range(C.rbegin(), C.rend())) {
240 return make_range(C.rbegin(), C.rend());
241}
242
243// Returns a std::reverse_iterator wrapped around the given iterator.
244template <typename IteratorTy>
245std::reverse_iterator<IteratorTy> make_reverse_iterator(IteratorTy It) {
246 return std::reverse_iterator<IteratorTy>(It);
247}
248
249// Returns an iterator_range over the given container which iterates in reverse.
250// Note that the container must have begin()/end() methods which return
251// bidirectional iterators for this to work.
252template <typename ContainerTy>
253auto reverse(
254 ContainerTy &&C,
255 typename std::enable_if<!has_rbegin<ContainerTy>::value>::type * = nullptr)
256 -> decltype(make_range(llvm::make_reverse_iterator(std::end(C)),
257 llvm::make_reverse_iterator(std::begin(C)))) {
258 return make_range(llvm::make_reverse_iterator(std::end(C)),
259 llvm::make_reverse_iterator(std::begin(C)));
260}
261
262/// An iterator adaptor that filters the elements of given inner iterators.
263///
264/// The predicate parameter should be a callable object that accepts the wrapped
265/// iterator's reference type and returns a bool. When incrementing or
266/// decrementing the iterator, it will call the predicate on each element and
267/// skip any where it returns false.
268///
269/// \code
270/// int A[] = { 1, 2, 3, 4 };
271/// auto R = make_filter_range(A, [](int N) { return N % 2 == 1; });
272/// // R contains { 1, 3 }.
273/// \endcode
274template <typename WrappedIteratorT, typename PredicateT>
275class filter_iterator
276 : public iterator_adaptor_base<
277 filter_iterator<WrappedIteratorT, PredicateT>, WrappedIteratorT,
278 typename std::common_type<
279 std::forward_iterator_tag,
280 typename std::iterator_traits<
281 WrappedIteratorT>::iterator_category>::type> {
282 using BaseT = iterator_adaptor_base<
283 filter_iterator<WrappedIteratorT, PredicateT>, WrappedIteratorT,
284 typename std::common_type<
285 std::forward_iterator_tag,
286 typename std::iterator_traits<WrappedIteratorT>::iterator_category>::
287 type>;
288
289 struct PayloadType {
290 WrappedIteratorT End;
291 PredicateT Pred;
292 };
293
294 Optional<PayloadType> Payload;
295
296 void findNextValid() {
297 assert(Payload && "Payload should be engaged when findNextValid is called")(static_cast <bool> (Payload && "Payload should be engaged when findNextValid is called"
) ? void (0) : __assert_fail ("Payload && \"Payload should be engaged when findNextValid is called\""
, "/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/ADT/STLExtras.h"
, 297, __extension__ __PRETTY_FUNCTION__))
;
298 while (this->I != Payload->End && !Payload->Pred(*this->I))
299 BaseT::operator++();
300 }
301
302 // Construct the begin iterator. The begin iterator requires to know where end
303 // is, so that it can properly stop when it hits end.
304 filter_iterator(WrappedIteratorT Begin, WrappedIteratorT End, PredicateT Pred)
305 : BaseT(std::move(Begin)),
306 Payload(PayloadType{std::move(End), std::move(Pred)}) {
307 findNextValid();
308 }
309
310 // Construct the end iterator. It's not incrementable, so Payload doesn't
311 // have to be engaged.
312 filter_iterator(WrappedIteratorT End) : BaseT(End) {}
313
314public:
315 using BaseT::operator++;
316
317 filter_iterator &operator++() {
318 BaseT::operator++();
319 findNextValid();
320 return *this;
321 }
322
323 template <typename RT, typename PT>
324 friend iterator_range<filter_iterator<detail::IterOfRange<RT>, PT>>
325 make_filter_range(RT &&, PT);
326};
327
328/// Convenience function that takes a range of elements and a predicate,
329/// and return a new filter_iterator range.
330///
331/// FIXME: Currently if RangeT && is a rvalue reference to a temporary, the
332/// lifetime of that temporary is not kept by the returned range object, and the
333/// temporary is going to be dropped on the floor after the make_iterator_range
334/// full expression that contains this function call.
335template <typename RangeT, typename PredicateT>
336iterator_range<filter_iterator<detail::IterOfRange<RangeT>, PredicateT>>
337make_filter_range(RangeT &&Range, PredicateT Pred) {
338 using FilterIteratorT =
339 filter_iterator<detail::IterOfRange<RangeT>, PredicateT>;
340 return make_range(FilterIteratorT(std::begin(std::forward<RangeT>(Range)),
341 std::end(std::forward<RangeT>(Range)),
342 std::move(Pred)),
343 FilterIteratorT(std::end(std::forward<RangeT>(Range))));
344}
345
346// forward declarations required by zip_shortest/zip_first
347template <typename R, typename UnaryPredicate>
348bool all_of(R &&range, UnaryPredicate P);
349
350template <size_t... I> struct index_sequence;
351
352template <class... Ts> struct index_sequence_for;
353
354namespace detail {
355
356using std::declval;
357
358// We have to alias this since inlining the actual type at the usage site
359// in the parameter list of iterator_facade_base<> below ICEs MSVC 2017.
360template<typename... Iters> struct ZipTupleType {
361 using type = std::tuple<decltype(*declval<Iters>())...>;
362};
363
364template <typename ZipType, typename... Iters>
365using zip_traits = iterator_facade_base<
366 ZipType, typename std::common_type<std::bidirectional_iterator_tag,
367 typename std::iterator_traits<
368 Iters>::iterator_category...>::type,
369 // ^ TODO: Implement random access methods.
370 typename ZipTupleType<Iters...>::type,
371 typename std::iterator_traits<typename std::tuple_element<
372 0, std::tuple<Iters...>>::type>::difference_type,
373 // ^ FIXME: This follows boost::make_zip_iterator's assumption that all
374 // inner iterators have the same difference_type. It would fail if, for
375 // instance, the second field's difference_type were non-numeric while the
376 // first is.
377 typename ZipTupleType<Iters...>::type *,
378 typename ZipTupleType<Iters...>::type>;
379
380template <typename ZipType, typename... Iters>
381struct zip_common : public zip_traits<ZipType, Iters...> {
382 using Base = zip_traits<ZipType, Iters...>;
383 using value_type = typename Base::value_type;
384
385 std::tuple<Iters...> iterators;
386
387protected:
388 template <size_t... Ns> value_type deref(index_sequence<Ns...>) const {
389 return value_type(*std::get<Ns>(iterators)...);
390 }
391
392 template <size_t... Ns>
393 decltype(iterators) tup_inc(index_sequence<Ns...>) const {
394 return std::tuple<Iters...>(std::next(std::get<Ns>(iterators))...);
395 }
396
397 template <size_t... Ns>
398 decltype(iterators) tup_dec(index_sequence<Ns...>) const {
399 return std::tuple<Iters...>(std::prev(std::get<Ns>(iterators))...);
400 }
401
402public:
403 zip_common(Iters &&... ts) : iterators(std::forward<Iters>(ts)...) {}
404
405 value_type operator*() { return deref(index_sequence_for<Iters...>{}); }
406
407 const value_type operator*() const {
408 return deref(index_sequence_for<Iters...>{});
409 }
410
411 ZipType &operator++() {
412 iterators = tup_inc(index_sequence_for<Iters...>{});
413 return *reinterpret_cast<ZipType *>(this);
414 }
415
416 ZipType &operator--() {
417 static_assert(Base::IsBidirectional,
418 "All inner iterators must be at least bidirectional.");
419 iterators = tup_dec(index_sequence_for<Iters...>{});
420 return *reinterpret_cast<ZipType *>(this);
421 }
422};
423
424template <typename... Iters>
425struct zip_first : public zip_common<zip_first<Iters...>, Iters...> {
426 using Base = zip_common<zip_first<Iters...>, Iters...>;
427
428 bool operator==(const zip_first<Iters...> &other) const {
429 return std::get<0>(this->iterators) == std::get<0>(other.iterators);
430 }
431
432 zip_first(Iters &&... ts) : Base(std::forward<Iters>(ts)...) {}
433};
434
435template <typename... Iters>
436class zip_shortest : public zip_common<zip_shortest<Iters...>, Iters...> {
437 template <size_t... Ns>
438 bool test(const zip_shortest<Iters...> &other, index_sequence<Ns...>) const {
439 return all_of(std::initializer_list<bool>{std::get<Ns>(this->iterators) !=
440 std::get<Ns>(other.iterators)...},
441 identity<bool>{});
442 }
443
444public:
445 using Base = zip_common<zip_shortest<Iters...>, Iters...>;
446
447 zip_shortest(Iters &&... ts) : Base(std::forward<Iters>(ts)...) {}
448
449 bool operator==(const zip_shortest<Iters...> &other) const {
450 return !test(other, index_sequence_for<Iters...>{});
451 }
452};
453
454template <template <typename...> class ItType, typename... Args> class zippy {
455public:
456 using iterator = ItType<decltype(std::begin(std::declval<Args>()))...>;
457 using iterator_category = typename iterator::iterator_category;
458 using value_type = typename iterator::value_type;
459 using difference_type = typename iterator::difference_type;
460 using pointer = typename iterator::pointer;
461 using reference = typename iterator::reference;
462
463private:
464 std::tuple<Args...> ts;
465
466 template <size_t... Ns> iterator begin_impl(index_sequence<Ns...>) const {
467 return iterator(std::begin(std::get<Ns>(ts))...);
468 }
469 template <size_t... Ns> iterator end_impl(index_sequence<Ns...>) const {
470 return iterator(std::end(std::get<Ns>(ts))...);
471 }
472
473public:
474 zippy(Args &&... ts_) : ts(std::forward<Args>(ts_)...) {}
475
476 iterator begin() const { return begin_impl(index_sequence_for<Args...>{}); }
477 iterator end() const { return end_impl(index_sequence_for<Args...>{}); }
478};
479
480} // end namespace detail
481
482/// zip iterator for two or more iteratable types.
483template <typename T, typename U, typename... Args>
484detail::zippy<detail::zip_shortest, T, U, Args...> zip(T &&t, U &&u,
485 Args &&... args) {
486 return detail::zippy<detail::zip_shortest, T, U, Args...>(
487 std::forward<T>(t), std::forward<U>(u), std::forward<Args>(args)...);
488}
489
490/// zip iterator that, for the sake of efficiency, assumes the first iteratee to
491/// be the shortest.
492template <typename T, typename U, typename... Args>
493detail::zippy<detail::zip_first, T, U, Args...> zip_first(T &&t, U &&u,
494 Args &&... args) {
495 return detail::zippy<detail::zip_first, T, U, Args...>(
496 std::forward<T>(t), std::forward<U>(u), std::forward<Args>(args)...);
497}
498
499/// Iterator wrapper that concatenates sequences together.
500///
501/// This can concatenate different iterators, even with different types, into
502/// a single iterator provided the value types of all the concatenated
503/// iterators expose `reference` and `pointer` types that can be converted to
504/// `ValueT &` and `ValueT *` respectively. It doesn't support more
505/// interesting/customized pointer or reference types.
506///
507/// Currently this only supports forward or higher iterator categories as
508/// inputs and always exposes a forward iterator interface.
509template <typename ValueT, typename... IterTs>
510class concat_iterator
511 : public iterator_facade_base<concat_iterator<ValueT, IterTs...>,
512 std::forward_iterator_tag, ValueT> {
513 using BaseT = typename concat_iterator::iterator_facade_base;
514
515 /// We store both the current and end iterators for each concatenated
516 /// sequence in a tuple of pairs.
517 ///
518 /// Note that something like iterator_range seems nice at first here, but the
519 /// range properties are of little benefit and end up getting in the way
520 /// because we need to do mutation on the current iterators.
521 std::tuple<std::pair<IterTs, IterTs>...> IterPairs;
522
523 /// Attempts to increment a specific iterator.
524 ///
525 /// Returns true if it was able to increment the iterator. Returns false if
526 /// the iterator is already at the end iterator.
527 template <size_t Index> bool incrementHelper() {
528 auto &IterPair = std::get<Index>(IterPairs);
529 if (IterPair.first == IterPair.second)
530 return false;
531
532 ++IterPair.first;
533 return true;
534 }
535
536 /// Increments the first non-end iterator.
537 ///
538 /// It is an error to call this with all iterators at the end.
539 template <size_t... Ns> void increment(index_sequence<Ns...>) {
540 // Build a sequence of functions to increment each iterator if possible.
541 bool (concat_iterator::*IncrementHelperFns[])() = {
542 &concat_iterator::incrementHelper<Ns>...};
543
544 // Loop over them, and stop as soon as we succeed at incrementing one.
545 for (auto &IncrementHelperFn : IncrementHelperFns)
546 if ((this->*IncrementHelperFn)())
547 return;
548
549 llvm_unreachable("Attempted to increment an end concat iterator!")::llvm::llvm_unreachable_internal("Attempted to increment an end concat iterator!"
, "/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/ADT/STLExtras.h"
, 549)
;
550 }
551
552 /// Returns null if the specified iterator is at the end. Otherwise,
553 /// dereferences the iterator and returns the address of the resulting
554 /// reference.
555 template <size_t Index> ValueT *getHelper() const {
556 auto &IterPair = std::get<Index>(IterPairs);
557 if (IterPair.first == IterPair.second)
558 return nullptr;
559
560 return &*IterPair.first;
561 }
562
563 /// Finds the first non-end iterator, dereferences, and returns the resulting
564 /// reference.
565 ///
566 /// It is an error to call this with all iterators at the end.
567 template <size_t... Ns> ValueT &get(index_sequence<Ns...>) const {
568 // Build a sequence of functions to get from iterator if possible.
569 ValueT *(concat_iterator::*GetHelperFns[])() const = {
570 &concat_iterator::getHelper<Ns>...};
571
572 // Loop over them, and return the first result we find.
573 for (auto &GetHelperFn : GetHelperFns)
574 if (ValueT *P = (this->*GetHelperFn)())
575 return *P;
576
577 llvm_unreachable("Attempted to get a pointer from an end concat iterator!")::llvm::llvm_unreachable_internal("Attempted to get a pointer from an end concat iterator!"
, "/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/ADT/STLExtras.h"
, 577)
;
578 }
579
580public:
581 /// Constructs an iterator from a squence of ranges.
582 ///
583 /// We need the full range to know how to switch between each of the
584 /// iterators.
585 template <typename... RangeTs>
586 explicit concat_iterator(RangeTs &&... Ranges)
587 : IterPairs({std::begin(Ranges), std::end(Ranges)}...) {}
588
589 using BaseT::operator++;
590
591 concat_iterator &operator++() {
592 increment(index_sequence_for<IterTs...>());
593 return *this;
594 }
595
596 ValueT &operator*() const { return get(index_sequence_for<IterTs...>()); }
597
598 bool operator==(const concat_iterator &RHS) const {
599 return IterPairs == RHS.IterPairs;
600 }
601};
602
603namespace detail {
604
605/// Helper to store a sequence of ranges being concatenated and access them.
606///
607/// This is designed to facilitate providing actual storage when temporaries
608/// are passed into the constructor such that we can use it as part of range
609/// based for loops.
610template <typename ValueT, typename... RangeTs> class concat_range {
611public:
612 using iterator =
613 concat_iterator<ValueT,
614 decltype(std::begin(std::declval<RangeTs &>()))...>;
615
616private:
617 std::tuple<RangeTs...> Ranges;
618
619 template <size_t... Ns> iterator begin_impl(index_sequence<Ns...>) {
620 return iterator(std::get<Ns>(Ranges)...);
621 }
622 template <size_t... Ns> iterator end_impl(index_sequence<Ns...>) {
623 return iterator(make_range(std::end(std::get<Ns>(Ranges)),
624 std::end(std::get<Ns>(Ranges)))...);
625 }
626
627public:
628 concat_range(RangeTs &&... Ranges)
629 : Ranges(std::forward<RangeTs>(Ranges)...) {}
630
631 iterator begin() { return begin_impl(index_sequence_for<RangeTs...>{}); }
632 iterator end() { return end_impl(index_sequence_for<RangeTs...>{}); }
633};
634
635} // end namespace detail
636
637/// Concatenated range across two or more ranges.
638///
639/// The desired value type must be explicitly specified.
640template <typename ValueT, typename... RangeTs>
641detail::concat_range<ValueT, RangeTs...> concat(RangeTs &&... Ranges) {
642 static_assert(sizeof...(RangeTs) > 1,
643 "Need more than one range to concatenate!");
644 return detail::concat_range<ValueT, RangeTs...>(
645 std::forward<RangeTs>(Ranges)...);
646}
647
648//===----------------------------------------------------------------------===//
649// Extra additions to <utility>
650//===----------------------------------------------------------------------===//
651
652/// \brief Function object to check whether the first component of a std::pair
653/// compares less than the first component of another std::pair.
654struct less_first {
655 template <typename T> bool operator()(const T &lhs, const T &rhs) const {
656 return lhs.first < rhs.first;
657 }
658};
659
660/// \brief Function object to check whether the second component of a std::pair
661/// compares less than the second component of another std::pair.
662struct less_second {
663 template <typename T> bool operator()(const T &lhs, const T &rhs) const {
664 return lhs.second < rhs.second;
665 }
666};
667
668// A subset of N3658. More stuff can be added as-needed.
669
670/// \brief Represents a compile-time sequence of integers.
671template <class T, T... I> struct integer_sequence {
672 using value_type = T;
673
674 static constexpr size_t size() { return sizeof...(I); }
675};
676
677/// \brief Alias for the common case of a sequence of size_ts.
678template <size_t... I>
679struct index_sequence : integer_sequence<std::size_t, I...> {};
680
681template <std::size_t N, std::size_t... I>
682struct build_index_impl : build_index_impl<N - 1, N - 1, I...> {};
683template <std::size_t... I>
684struct build_index_impl<0, I...> : index_sequence<I...> {};
685
686/// \brief Creates a compile-time integer sequence for a parameter pack.
687template <class... Ts>
688struct index_sequence_for : build_index_impl<sizeof...(Ts)> {};
689
690/// Utility type to build an inheritance chain that makes it easy to rank
691/// overload candidates.
692template <int N> struct rank : rank<N - 1> {};
693template <> struct rank<0> {};
694
695/// \brief traits class for checking whether type T is one of any of the given
696/// types in the variadic list.
697template <typename T, typename... Ts> struct is_one_of {
698 static const bool value = false;
699};
700
701template <typename T, typename U, typename... Ts>
702struct is_one_of<T, U, Ts...> {
703 static const bool value =
704 std::is_same<T, U>::value || is_one_of<T, Ts...>::value;
705};
706
707/// \brief traits class for checking whether type T is a base class for all
708/// the given types in the variadic list.
709template <typename T, typename... Ts> struct are_base_of {
710 static const bool value = true;
711};
712
713template <typename T, typename U, typename... Ts>
714struct are_base_of<T, U, Ts...> {
715 static const bool value =
716 std::is_base_of<T, U>::value && are_base_of<T, Ts...>::value;
717};
718
719//===----------------------------------------------------------------------===//
720// Extra additions for arrays
721//===----------------------------------------------------------------------===//
722
723/// Find the length of an array.
724template <class T, std::size_t N>
725constexpr inline size_t array_lengthof(T (&)[N]) {
726 return N;
727}
728
729/// Adapt std::less<T> for array_pod_sort.
730template<typename T>
731inline int array_pod_sort_comparator(const void *P1, const void *P2) {
732 if (std::less<T>()(*reinterpret_cast<const T*>(P1),
733 *reinterpret_cast<const T*>(P2)))
734 return -1;
735 if (std::less<T>()(*reinterpret_cast<const T*>(P2),
736 *reinterpret_cast<const T*>(P1)))
737 return 1;
738 return 0;
739}
740
741/// get_array_pod_sort_comparator - This is an internal helper function used to
742/// get type deduction of T right.
743template<typename T>
744inline int (*get_array_pod_sort_comparator(const T &))
745 (const void*, const void*) {
746 return array_pod_sort_comparator<T>;
747}
748
749/// array_pod_sort - This sorts an array with the specified start and end
750/// extent. This is just like std::sort, except that it calls qsort instead of
751/// using an inlined template. qsort is slightly slower than std::sort, but
752/// most sorts are not performance critical in LLVM and std::sort has to be
753/// template instantiated for each type, leading to significant measured code
754/// bloat. This function should generally be used instead of std::sort where
755/// possible.
756///
757/// This function assumes that you have simple POD-like types that can be
758/// compared with std::less and can be moved with memcpy. If this isn't true,
759/// you should use std::sort.
760///
761/// NOTE: If qsort_r were portable, we could allow a custom comparator and
762/// default to std::less.
763template<class IteratorTy>
764inline void array_pod_sort(IteratorTy Start, IteratorTy End) {
765 // Don't inefficiently call qsort with one element or trigger undefined
766 // behavior with an empty sequence.
767 auto NElts = End - Start;
768 if (NElts <= 1) return;
769#ifdef EXPENSIVE_CHECKS
770 std::mt19937 Generator(std::random_device{}());
771 std::shuffle(Start, End, Generator);
772#endif
773 qsort(&*Start, NElts, sizeof(*Start), get_array_pod_sort_comparator(*Start));
774}
775
776template <class IteratorTy>
777inline void array_pod_sort(
778 IteratorTy Start, IteratorTy End,
779 int (*Compare)(
780 const typename std::iterator_traits<IteratorTy>::value_type *,
781 const typename std::iterator_traits<IteratorTy>::value_type *)) {
782 // Don't inefficiently call qsort with one element or trigger undefined
783 // behavior with an empty sequence.
784 auto NElts = End - Start;
785 if (NElts <= 1) return;
786#ifdef EXPENSIVE_CHECKS
787 std::mt19937 Generator(std::random_device{}());
788 std::shuffle(Start, End, Generator);
789#endif
790 qsort(&*Start, NElts, sizeof(*Start),
791 reinterpret_cast<int (*)(const void *, const void *)>(Compare));
792}
793
794// Provide wrappers to std::sort which shuffle the elements before sorting
795// to help uncover non-deterministic behavior (PR35135).
796template <typename IteratorTy>
797inline void sort(IteratorTy Start, IteratorTy End) {
798#ifdef EXPENSIVE_CHECKS
799 std::mt19937 Generator(std::random_device{}());
800 std::shuffle(Start, End, Generator);
801#endif
802 std::sort(Start, End);
803}
804
805template <typename IteratorTy, typename Compare>
806inline void sort(IteratorTy Start, IteratorTy End, Compare Comp) {
807#ifdef EXPENSIVE_CHECKS
808 std::mt19937 Generator(std::random_device{}());
809 std::shuffle(Start, End, Generator);
810#endif
811 std::sort(Start, End, Comp);
812}
813
814//===----------------------------------------------------------------------===//
815// Extra additions to <algorithm>
816//===----------------------------------------------------------------------===//
817
818/// For a container of pointers, deletes the pointers and then clears the
819/// container.
820template<typename Container>
821void DeleteContainerPointers(Container &C) {
822 for (auto V : C)
823 delete V;
824 C.clear();
825}
826
827/// In a container of pairs (usually a map) whose second element is a pointer,
828/// deletes the second elements and then clears the container.
829template<typename Container>
830void DeleteContainerSeconds(Container &C) {
831 for (auto &V : C)
832 delete V.second;
833 C.clear();
834}
835
836/// Provide wrappers to std::for_each which take ranges instead of having to
837/// pass begin/end explicitly.
838template <typename R, typename UnaryPredicate>
839UnaryPredicate for_each(R &&Range, UnaryPredicate P) {
840 return std::for_each(adl_begin(Range), adl_end(Range), P);
841}
842
843/// Provide wrappers to std::all_of which take ranges instead of having to pass
844/// begin/end explicitly.
845template <typename R, typename UnaryPredicate>
846bool all_of(R &&Range, UnaryPredicate P) {
847 return std::all_of(adl_begin(Range), adl_end(Range), P);
848}
849
850/// Provide wrappers to std::any_of which take ranges instead of having to pass
851/// begin/end explicitly.
852template <typename R, typename UnaryPredicate>
853bool any_of(R &&Range, UnaryPredicate P) {
854 return std::any_of(adl_begin(Range), adl_end(Range), P);
855}
856
857/// Provide wrappers to std::none_of which take ranges instead of having to pass
858/// begin/end explicitly.
859template <typename R, typename UnaryPredicate>
860bool none_of(R &&Range, UnaryPredicate P) {
861 return std::none_of(adl_begin(Range), adl_end(Range), P);
862}
863
864/// Provide wrappers to std::find which take ranges instead of having to pass
865/// begin/end explicitly.
866template <typename R, typename T>
867auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range)) {
868 return std::find(adl_begin(Range), adl_end(Range), Val);
869}
870
871/// Provide wrappers to std::find_if which take ranges instead of having to pass
872/// begin/end explicitly.
873template <typename R, typename UnaryPredicate>
874auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range)) {
875 return std::find_if(adl_begin(Range), adl_end(Range), P);
876}
877
878template <typename R, typename UnaryPredicate>
879auto find_if_not(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range)) {
880 return std::find_if_not(adl_begin(Range), adl_end(Range), P);
881}
882
883/// Provide wrappers to std::remove_if which take ranges instead of having to
884/// pass begin/end explicitly.
885template <typename R, typename UnaryPredicate>
886auto remove_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range)) {
887 return std::remove_if(adl_begin(Range), adl_end(Range), P);
888}
889
890/// Provide wrappers to std::copy_if which take ranges instead of having to
891/// pass begin/end explicitly.
892template <typename R, typename OutputIt, typename UnaryPredicate>
893OutputIt copy_if(R &&Range, OutputIt Out, UnaryPredicate P) {
894 return std::copy_if(adl_begin(Range), adl_end(Range), Out, P);
895}
896
897template <typename R, typename OutputIt>
898OutputIt copy(R &&Range, OutputIt Out) {
899 return std::copy(adl_begin(Range), adl_end(Range), Out);
900}
901
902/// Wrapper function around std::find to detect if an element exists
903/// in a container.
904template <typename R, typename E>
905bool is_contained(R &&Range, const E &Element) {
906 return std::find(adl_begin(Range), adl_end(Range), Element) != adl_end(Range);
907}
908
909/// Wrapper function around std::count to count the number of times an element
910/// \p Element occurs in the given range \p Range.
911template <typename R, typename E>
912auto count(R &&Range, const E &Element) ->
913 typename std::iterator_traits<decltype(adl_begin(Range))>::difference_type {
914 return std::count(adl_begin(Range), adl_end(Range), Element);
915}
916
917/// Wrapper function around std::count_if to count the number of times an
918/// element satisfying a given predicate occurs in a range.
919template <typename R, typename UnaryPredicate>
920auto count_if(R &&Range, UnaryPredicate P) ->
921 typename std::iterator_traits<decltype(adl_begin(Range))>::difference_type {
922 return std::count_if(adl_begin(Range), adl_end(Range), P);
923}
924
925/// Wrapper function around std::transform to apply a function to a range and
926/// store the result elsewhere.
927template <typename R, typename OutputIt, typename UnaryPredicate>
928OutputIt transform(R &&Range, OutputIt d_first, UnaryPredicate P) {
929 return std::transform(adl_begin(Range), adl_end(Range), d_first, P);
930}
931
932/// Provide wrappers to std::partition which take ranges instead of having to
933/// pass begin/end explicitly.
934template <typename R, typename UnaryPredicate>
935auto partition(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range)) {
936 return std::partition(adl_begin(Range), adl_end(Range), P);
937}
938
939/// Provide wrappers to std::lower_bound which take ranges instead of having to
940/// pass begin/end explicitly.
941template <typename R, typename ForwardIt>
942auto lower_bound(R &&Range, ForwardIt I) -> decltype(adl_begin(Range)) {
943 return std::lower_bound(adl_begin(Range), adl_end(Range), I);
944}
945
946/// \brief Given a range of type R, iterate the entire range and return a
947/// SmallVector with elements of the vector. This is useful, for example,
948/// when you want to iterate a range and then sort the results.
949template <unsigned Size, typename R>
950SmallVector<typename std::remove_const<detail::ValueOfRange<R>>::type, Size>
951to_vector(R &&Range) {
952 return {adl_begin(Range), adl_end(Range)};
953}
954
955/// Provide a container algorithm similar to C++ Library Fundamentals v2's
956/// `erase_if` which is equivalent to:
957///
958/// C.erase(remove_if(C, pred), C.end());
959///
960/// This version works for any container with an erase method call accepting
961/// two iterators.
962template <typename Container, typename UnaryPredicate>
963void erase_if(Container &C, UnaryPredicate P) {
964 C.erase(remove_if(C, P), C.end());
965}
966
967//===----------------------------------------------------------------------===//
968// Extra additions to <memory>
969//===----------------------------------------------------------------------===//
970
971// Implement make_unique according to N3656.
972
973/// \brief Constructs a `new T()` with the given args and returns a
974/// `unique_ptr<T>` which owns the object.
975///
976/// Example:
977///
978/// auto p = make_unique<int>();
979/// auto p = make_unique<std::tuple<int, int>>(0, 1);
980template <class T, class... Args>
981typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
982make_unique(Args &&... args) {
983 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
6
Memory is allocated
984}
985
986/// \brief Constructs a `new T[n]` with the given args and returns a
987/// `unique_ptr<T[]>` which owns the object.
988///
989/// \param n size of the new array.
990///
991/// Example:
992///
993/// auto p = make_unique<int[]>(2); // value-initializes the array with 0's.
994template <class T>
995typename std::enable_if<std::is_array<T>::value && std::extent<T>::value == 0,
996 std::unique_ptr<T>>::type
997make_unique(size_t n) {
998 return std::unique_ptr<T>(new typename std::remove_extent<T>::type[n]());
999}
1000
1001/// This function isn't used and is only here to provide better compile errors.
1002template <class T, class... Args>
1003typename std::enable_if<std::extent<T>::value != 0>::type
1004make_unique(Args &&...) = delete;
1005
1006struct FreeDeleter {
1007 void operator()(void* v) {
1008 ::free(v);
1009 }
1010};
1011
1012template<typename First, typename Second>
1013struct pair_hash {
1014 size_t operator()(const std::pair<First, Second> &P) const {
1015 return std::hash<First>()(P.first) * 31 + std::hash<Second>()(P.second);
1016 }
1017};
1018
1019/// A functor like C++14's std::less<void> in its absence.
1020struct less {
1021 template <typename A, typename B> bool operator()(A &&a, B &&b) const {
1022 return std::forward<A>(a) < std::forward<B>(b);
1023 }
1024};
1025
1026/// A functor like C++14's std::equal<void> in its absence.
1027struct equal {
1028 template <typename A, typename B> bool operator()(A &&a, B &&b) const {
1029 return std::forward<A>(a) == std::forward<B>(b);
1030 }
1031};
1032
1033/// Binary functor that adapts to any other binary functor after dereferencing
1034/// operands.
1035template <typename T> struct deref {
1036 T func;
1037
1038 // Could be further improved to cope with non-derivable functors and
1039 // non-binary functors (should be a variadic template member function
1040 // operator()).
1041 template <typename A, typename B>
1042 auto operator()(A &lhs, B &rhs) const -> decltype(func(*lhs, *rhs)) {
1043 assert(lhs)(static_cast <bool> (lhs) ? void (0) : __assert_fail ("lhs"
, "/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/ADT/STLExtras.h"
, 1043, __extension__ __PRETTY_FUNCTION__))
;
1044 assert(rhs)(static_cast <bool> (rhs) ? void (0) : __assert_fail ("rhs"
, "/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/ADT/STLExtras.h"
, 1044, __extension__ __PRETTY_FUNCTION__))
;
1045 return func(*lhs, *rhs);
1046 }
1047};
1048
1049namespace detail {
1050
1051template <typename R> class enumerator_iter;
1052
1053template <typename R> struct result_pair {
1054 friend class enumerator_iter<R>;
1055
1056 result_pair() = default;
1057 result_pair(std::size_t Index, IterOfRange<R> Iter)
1058 : Index(Index), Iter(Iter) {}
1059
1060 result_pair<R> &operator=(const result_pair<R> &Other) {
1061 Index = Other.Index;
1062 Iter = Other.Iter;
1063 return *this;
1064 }
1065
1066 std::size_t index() const { return Index; }
1067 const ValueOfRange<R> &value() const { return *Iter; }
1068 ValueOfRange<R> &value() { return *Iter; }
1069
1070private:
1071 std::size_t Index = std::numeric_limits<std::size_t>::max();
1072 IterOfRange<R> Iter;
1073};
1074
1075template <typename R>
1076class enumerator_iter
1077 : public iterator_facade_base<
1078 enumerator_iter<R>, std::forward_iterator_tag, result_pair<R>,
1079 typename std::iterator_traits<IterOfRange<R>>::difference_type,
1080 typename std::iterator_traits<IterOfRange<R>>::pointer,
1081 typename std::iterator_traits<IterOfRange<R>>::reference> {
1082 using result_type = result_pair<R>;
1083
1084public:
1085 explicit enumerator_iter(IterOfRange<R> EndIter)
1086 : Result(std::numeric_limits<size_t>::max(), EndIter) {}
1087
1088 enumerator_iter(std::size_t Index, IterOfRange<R> Iter)
1089 : Result(Index, Iter) {}
1090
1091 result_type &operator*() { return Result; }
1092 const result_type &operator*() const { return Result; }
1093
1094 enumerator_iter<R> &operator++() {
1095 assert(Result.Index != std::numeric_limits<size_t>::max())(static_cast <bool> (Result.Index != std::numeric_limits
<size_t>::max()) ? void (0) : __assert_fail ("Result.Index != std::numeric_limits<size_t>::max()"
, "/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/ADT/STLExtras.h"
, 1095, __extension__ __PRETTY_FUNCTION__))
;
1096 ++Result.Iter;
1097 ++Result.Index;
1098 return *this;
1099 }
1100
1101 bool operator==(const enumerator_iter<R> &RHS) const {
1102 // Don't compare indices here, only iterators. It's possible for an end
1103 // iterator to have different indices depending on whether it was created
1104 // by calling std::end() versus incrementing a valid iterator.
1105 return Result.Iter == RHS.Result.Iter;
1106 }
1107
1108 enumerator_iter<R> &operator=(const enumerator_iter<R> &Other) {
1109 Result = Other.Result;
1110 return *this;
1111 }
1112
1113private:
1114 result_type Result;
1115};
1116
1117template <typename R> class enumerator {
1118public:
1119 explicit enumerator(R &&Range) : TheRange(std::forward<R>(Range)) {}
1120
1121 enumerator_iter<R> begin() {
1122 return enumerator_iter<R>(0, std::begin(TheRange));
1123 }
1124
1125 enumerator_iter<R> end() {
1126 return enumerator_iter<R>(std::end(TheRange));
1127 }
1128
1129private:
1130 R TheRange;
1131};
1132
1133} // end namespace detail
1134
1135/// Given an input range, returns a new range whose values are are pair (A,B)
1136/// such that A is the 0-based index of the item in the sequence, and B is
1137/// the value from the original sequence. Example:
1138///
1139/// std::vector<char> Items = {'A', 'B', 'C', 'D'};
1140/// for (auto X : enumerate(Items)) {
1141/// printf("Item %d - %c\n", X.index(), X.value());
1142/// }
1143///
1144/// Output:
1145/// Item 0 - A
1146/// Item 1 - B
1147/// Item 2 - C
1148/// Item 3 - D
1149///
1150template <typename R> detail::enumerator<R> enumerate(R &&TheRange) {
1151 return detail::enumerator<R>(std::forward<R>(TheRange));
1152}
1153
1154namespace detail {
1155
1156template <typename F, typename Tuple, std::size_t... I>
1157auto apply_tuple_impl(F &&f, Tuple &&t, index_sequence<I...>)
1158 -> decltype(std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...)) {
1159 return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
1160}
1161
1162} // end namespace detail
1163
1164/// Given an input tuple (a1, a2, ..., an), pass the arguments of the
1165/// tuple variadically to f as if by calling f(a1, a2, ..., an) and
1166/// return the result.
1167template <typename F, typename Tuple>
1168auto apply_tuple(F &&f, Tuple &&t) -> decltype(detail::apply_tuple_impl(
1169 std::forward<F>(f), std::forward<Tuple>(t),
1170 build_index_impl<
1171 std::tuple_size<typename std::decay<Tuple>::type>::value>{})) {
1172 using Indices = build_index_impl<
1173 std::tuple_size<typename std::decay<Tuple>::type>::value>;
1174
1175 return detail::apply_tuple_impl(std::forward<F>(f), std::forward<Tuple>(t),
1176 Indices{});
1177}
1178
1179} // end namespace llvm
1180
1181#endif // LLVM_ADT_STLEXTRAS_H

/usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/bits/unique_ptr.h

1// unique_ptr implementation -*- C++ -*-
2
3// Copyright (C) 2008-2017 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file bits/unique_ptr.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{memory}
28 */
29
30#ifndef _UNIQUE_PTR_H1
31#define _UNIQUE_PTR_H1 1
32
33#include <bits/c++config.h>
34#include <debug/assertions.h>
35#include <type_traits>
36#include <utility>
37#include <tuple>
38#include <bits/stl_function.h>
39#include <bits/functional_hash.h>
40
41namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
42{
43_GLIBCXX_BEGIN_NAMESPACE_VERSION
44
45 /**
46 * @addtogroup pointer_abstractions
47 * @{
48 */
49
50#if _GLIBCXX_USE_DEPRECATED1
51 template<typename> class auto_ptr;
52#endif
53
54 /// Primary template of default_delete, used by unique_ptr
55 template<typename _Tp>
56 struct default_delete
57 {
58 /// Default constructor
59 constexpr default_delete() noexcept = default;
60
61 /** @brief Converting constructor.
62 *
63 * Allows conversion from a deleter for arrays of another type, @p _Up,
64 * only if @p _Up* is convertible to @p _Tp*.
65 */
66 template<typename _Up, typename = typename
67 enable_if<is_convertible<_Up*, _Tp*>::value>::type>
68 default_delete(const default_delete<_Up>&) noexcept { }
69
70 /// Calls @c delete @p __ptr
71 void
72 operator()(_Tp* __ptr) const
73 {
74 static_assert(!is_void<_Tp>::value,
75 "can't delete pointer to incomplete type");
76 static_assert(sizeof(_Tp)>0,
77 "can't delete pointer to incomplete type");
78 delete __ptr;
11
Memory is released
79 }
80 };
81
82 // _GLIBCXX_RESOLVE_LIB_DEFECTS
83 // DR 740 - omit specialization for array objects with a compile time length
84 /// Specialization for arrays, default_delete.
85 template<typename _Tp>
86 struct default_delete<_Tp[]>
87 {
88 public:
89 /// Default constructor
90 constexpr default_delete() noexcept = default;
91
92 /** @brief Converting constructor.
93 *
94 * Allows conversion from a deleter for arrays of another type, such as
95 * a const-qualified version of @p _Tp.
96 *
97 * Conversions from types derived from @c _Tp are not allowed because
98 * it is unsafe to @c delete[] an array of derived types through a
99 * pointer to the base type.
100 */
101 template<typename _Up, typename = typename
102 enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type>
103 default_delete(const default_delete<_Up[]>&) noexcept { }
104
105 /// Calls @c delete[] @p __ptr
106 template<typename _Up>
107 typename enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type
108 operator()(_Up* __ptr) const
109 {
110 static_assert(sizeof(_Tp)>0,
111 "can't delete pointer to incomplete type");
112 delete [] __ptr;
113 }
114 };
115
116 template <typename _Tp, typename _Dp>
117 class __uniq_ptr_impl
118 {
119 template <typename _Up, typename _Ep, typename = void>
120 struct _Ptr
121 {
122 using type = _Up*;
123 };
124
125 template <typename _Up, typename _Ep>
126 struct
127 _Ptr<_Up, _Ep, __void_t<typename remove_reference<_Ep>::type::pointer>>
128 {
129 using type = typename remove_reference<_Ep>::type::pointer;
130 };
131
132 public:
133 using _DeleterConstraint = enable_if<
134 __and_<__not_<is_pointer<_Dp>>,
135 is_default_constructible<_Dp>>::value>;
136
137 using pointer = typename _Ptr<_Tp, _Dp>::type;
138
139 __uniq_ptr_impl() = default;
140 __uniq_ptr_impl(pointer __p) : _M_t() { _M_ptr() = __p; }
141
142 template<typename _Del>
143 __uniq_ptr_impl(pointer __p, _Del&& __d)
144 : _M_t(__p, std::forward<_Del>(__d)) { }
145
146 pointer& _M_ptr() { return std::get<0>(_M_t); }
147 pointer _M_ptr() const { return std::get<0>(_M_t); }
148 _Dp& _M_deleter() { return std::get<1>(_M_t); }
149 const _Dp& _M_deleter() const { return std::get<1>(_M_t); }
150
151 private:
152 tuple<pointer, _Dp> _M_t;
153 };
154
155 /// 20.7.1.2 unique_ptr for single objects.
156 template <typename _Tp, typename _Dp = default_delete<_Tp>>
157 class unique_ptr
158 {
159 template <class _Up>
160 using _DeleterConstraint =
161 typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
162
163 __uniq_ptr_impl<_Tp, _Dp> _M_t;
164
165 public:
166 using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
167 using element_type = _Tp;
168 using deleter_type = _Dp;
169
170 // helper template for detecting a safe conversion from another
171 // unique_ptr
172 template<typename _Up, typename _Ep>
173 using __safe_conversion_up = __and_<
174 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>,
175 __not_<is_array<_Up>>,
176 __or_<__and_<is_reference<deleter_type>,
177 is_same<deleter_type, _Ep>>,
178 __and_<__not_<is_reference<deleter_type>>,
179 is_convertible<_Ep, deleter_type>>
180 >
181 >;
182
183 // Constructors.
184
185 /// Default constructor, creates a unique_ptr that owns nothing.
186 template <typename _Up = _Dp,
187 typename = _DeleterConstraint<_Up>>
188 constexpr unique_ptr() noexcept
189 : _M_t()
190 { }
191
192 /** Takes ownership of a pointer.
193 *
194 * @param __p A pointer to an object of @c element_type
195 *
196 * The deleter will be value-initialized.
197 */
198 template <typename _Up = _Dp,
199 typename = _DeleterConstraint<_Up>>
200 explicit
201 unique_ptr(pointer __p) noexcept
202 : _M_t(__p)
203 { }
204
205 /** Takes ownership of a pointer.
206 *
207 * @param __p A pointer to an object of @c element_type
208 * @param __d A reference to a deleter.
209 *
210 * The deleter will be initialized with @p __d
211 */
212 unique_ptr(pointer __p,
213 typename conditional<is_reference<deleter_type>::value,
214 deleter_type, const deleter_type&>::type __d) noexcept
215 : _M_t(__p, __d) { }
216
217 /** Takes ownership of a pointer.
218 *
219 * @param __p A pointer to an object of @c element_type
220 * @param __d An rvalue reference to a deleter.
221 *
222 * The deleter will be initialized with @p std::move(__d)
223 */
224 unique_ptr(pointer __p,
225 typename remove_reference<deleter_type>::type&& __d) noexcept
226 : _M_t(std::move(__p), std::move(__d))
227 { static_assert(!std::is_reference<deleter_type>::value,
228 "rvalue deleter bound to reference"); }
229
230 /// Creates a unique_ptr that owns nothing.
231 template <typename _Up = _Dp,
232 typename = _DeleterConstraint<_Up>>
233 constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { }
234
235 // Move constructors.
236
237 /// Move constructor.
238 unique_ptr(unique_ptr&& __u) noexcept
239 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
240
241 /** @brief Converting constructor from another type
242 *
243 * Requires that the pointer owned by @p __u is convertible to the
244 * type of pointer owned by this object, @p __u does not own an array,
245 * and @p __u has a compatible deleter type.
246 */
247 template<typename _Up, typename _Ep, typename = _Require<
248 __safe_conversion_up<_Up, _Ep>,
249 typename conditional<is_reference<_Dp>::value,
250 is_same<_Ep, _Dp>,
251 is_convertible<_Ep, _Dp>>::type>>
252 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
253 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
254 { }
255
256#if _GLIBCXX_USE_DEPRECATED1
257 /// Converting constructor from @c auto_ptr
258 template<typename _Up, typename = _Require<
259 is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>>
260 unique_ptr(auto_ptr<_Up>&& __u) noexcept;
261#endif
262
263 /// Destructor, invokes the deleter if the stored pointer is not null.
264 ~unique_ptr() noexcept
265 {
266 auto& __ptr = _M_t._M_ptr();
267 if (__ptr != nullptr)
9
Taking true branch
268 get_deleter()(__ptr);
10
Calling 'default_delete::operator()'
12
Returning; memory was released via 2nd parameter
269 __ptr = pointer();
270 }
271
272 // Assignment.
273
274 /** @brief Move assignment operator.
275 *
276 * @param __u The object to transfer ownership from.
277 *
278 * Invokes the deleter first if this object owns a pointer.
279 */
280 unique_ptr&
281 operator=(unique_ptr&& __u) noexcept
282 {
283 reset(__u.release());
284 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
285 return *this;
286 }
287
288 /** @brief Assignment from another type.
289 *
290 * @param __u The object to transfer ownership from, which owns a
291 * convertible pointer to a non-array object.
292 *
293 * Invokes the deleter first if this object owns a pointer.
294 */
295 template<typename _Up, typename _Ep>
296 typename enable_if< __and_<
297 __safe_conversion_up<_Up, _Ep>,
298 is_assignable<deleter_type&, _Ep&&>
299 >::value,
300 unique_ptr&>::type
301 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
302 {
303 reset(__u.release());
304 get_deleter() = std::forward<_Ep>(__u.get_deleter());
305 return *this;
306 }
307
308 /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
309 unique_ptr&
310 operator=(nullptr_t) noexcept
311 {
312 reset();
313 return *this;
314 }
315
316 // Observers.
317
318 /// Dereference the stored pointer.
319 typename add_lvalue_reference<element_type>::type
320 operator*() const
321 {
322 __glibcxx_assert(get() != pointer());
323 return *get();
324 }
325
326 /// Return the stored pointer.
327 pointer
328 operator->() const noexcept
329 {
330 _GLIBCXX_DEBUG_PEDASSERT(get() != pointer());
331 return get();
332 }
333
334 /// Return the stored pointer.
335 pointer
336 get() const noexcept
337 { return _M_t._M_ptr(); }
338
339 /// Return a reference to the stored deleter.
340 deleter_type&
341 get_deleter() noexcept
342 { return _M_t._M_deleter(); }
343
344 /// Return a reference to the stored deleter.
345 const deleter_type&
346 get_deleter() const noexcept
347 { return _M_t._M_deleter(); }
348
349 /// Return @c true if the stored pointer is not null.
350 explicit operator bool() const noexcept
351 { return get() == pointer() ? false : true; }
352
353 // Modifiers.
354
355 /// Release ownership of any stored pointer.
356 pointer
357 release() noexcept
358 {
359 pointer __p = get();
360 _M_t._M_ptr() = pointer();
361 return __p;
362 }
363
364 /** @brief Replace the stored pointer.
365 *
366 * @param __p The new pointer to store.
367 *
368 * The deleter will be invoked if a pointer is already owned.
369 */
370 void
371 reset(pointer __p = pointer()) noexcept
372 {
373 using std::swap;
374 swap(_M_t._M_ptr(), __p);
375 if (__p != pointer())
376 get_deleter()(__p);
377 }
378
379 /// Exchange the pointer and deleter with another object.
380 void
381 swap(unique_ptr& __u) noexcept
382 {
383 using std::swap;
384 swap(_M_t, __u._M_t);
385 }
386
387 // Disable copy from lvalue.
388 unique_ptr(const unique_ptr&) = delete;
389 unique_ptr& operator=(const unique_ptr&) = delete;
390 };
391
392 /// 20.7.1.3 unique_ptr for array objects with a runtime length
393 // [unique.ptr.runtime]
394 // _GLIBCXX_RESOLVE_LIB_DEFECTS
395 // DR 740 - omit specialization for array objects with a compile time length
396 template<typename _Tp, typename _Dp>
397 class unique_ptr<_Tp[], _Dp>
398 {
399 template <typename _Up>
400 using _DeleterConstraint =
401 typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
402
403 __uniq_ptr_impl<_Tp, _Dp> _M_t;
404
405 template<typename _Up>
406 using __remove_cv = typename remove_cv<_Up>::type;
407
408 // like is_base_of<_Tp, _Up> but false if unqualified types are the same
409 template<typename _Up>
410 using __is_derived_Tp
411 = __and_< is_base_of<_Tp, _Up>,
412 __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >;
413
414 public:
415 using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
416 using element_type = _Tp;
417 using deleter_type = _Dp;
418
419 // helper template for detecting a safe conversion from another
420 // unique_ptr
421 template<typename _Up, typename _Ep,
422 typename _Up_up = unique_ptr<_Up, _Ep>,
423 typename _Up_element_type = typename _Up_up::element_type>
424 using __safe_conversion_up = __and_<
425 is_array<_Up>,
426 is_same<pointer, element_type*>,
427 is_same<typename _Up_up::pointer, _Up_element_type*>,
428 is_convertible<_Up_element_type(*)[], element_type(*)[]>,
429 __or_<__and_<is_reference<deleter_type>, is_same<deleter_type, _Ep>>,
430 __and_<__not_<is_reference<deleter_type>>,
431 is_convertible<_Ep, deleter_type>>>
432 >;
433
434 // helper template for detecting a safe conversion from a raw pointer
435 template<typename _Up>
436 using __safe_conversion_raw = __and_<
437 __or_<__or_<is_same<_Up, pointer>,
438 is_same<_Up, nullptr_t>>,
439 __and_<is_pointer<_Up>,
440 is_same<pointer, element_type*>,
441 is_convertible<
442 typename remove_pointer<_Up>::type(*)[],
443 element_type(*)[]>
444 >
445 >
446 >;
447
448 // Constructors.
449
450 /// Default constructor, creates a unique_ptr that owns nothing.
451 template <typename _Up = _Dp,
452 typename = _DeleterConstraint<_Up>>
453 constexpr unique_ptr() noexcept
454 : _M_t()
455 { }
456
457 /** Takes ownership of a pointer.
458 *
459 * @param __p A pointer to an array of a type safely convertible
460 * to an array of @c element_type
461 *
462 * The deleter will be value-initialized.
463 */
464 template<typename _Up,
465 typename _Vp = _Dp,
466 typename = _DeleterConstraint<_Vp>,
467 typename = typename enable_if<
468 __safe_conversion_raw<_Up>::value, bool>::type>
469 explicit
470 unique_ptr(_Up __p) noexcept
471 : _M_t(__p)
472 { }
473
474 /** Takes ownership of a pointer.
475 *
476 * @param __p A pointer to an array of a type safely convertible
477 * to an array of @c element_type
478 * @param __d A reference to a deleter.
479 *
480 * The deleter will be initialized with @p __d
481 */
482 template<typename _Up,
483 typename = typename enable_if<
484 __safe_conversion_raw<_Up>::value, bool>::type>
485 unique_ptr(_Up __p,
486 typename conditional<is_reference<deleter_type>::value,
487 deleter_type, const deleter_type&>::type __d) noexcept
488 : _M_t(__p, __d) { }
489
490 /** Takes ownership of a pointer.
491 *
492 * @param __p A pointer to an array of a type safely convertible
493 * to an array of @c element_type
494 * @param __d A reference to a deleter.
495 *
496 * The deleter will be initialized with @p std::move(__d)
497 */
498 template<typename _Up,
499 typename = typename enable_if<
500 __safe_conversion_raw<_Up>::value, bool>::type>
501 unique_ptr(_Up __p, typename
502 remove_reference<deleter_type>::type&& __d) noexcept
503 : _M_t(std::move(__p), std::move(__d))
504 { static_assert(!is_reference<deleter_type>::value,
505 "rvalue deleter bound to reference"); }
506
507 /// Move constructor.
508 unique_ptr(unique_ptr&& __u) noexcept
509 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
510
511 /// Creates a unique_ptr that owns nothing.
512 template <typename _Up = _Dp,
513 typename = _DeleterConstraint<_Up>>
514 constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { }
515
516 template<typename _Up, typename _Ep,
517 typename = _Require<__safe_conversion_up<_Up, _Ep>>>
518 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
519 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
520 { }
521
522 /// Destructor, invokes the deleter if the stored pointer is not null.
523 ~unique_ptr()
524 {
525 auto& __ptr = _M_t._M_ptr();
526 if (__ptr != nullptr)
527 get_deleter()(__ptr);
528 __ptr = pointer();
529 }
530
531 // Assignment.
532
533 /** @brief Move assignment operator.
534 *
535 * @param __u The object to transfer ownership from.
536 *
537 * Invokes the deleter first if this object owns a pointer.
538 */
539 unique_ptr&
540 operator=(unique_ptr&& __u) noexcept
541 {
542 reset(__u.release());
543 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
544 return *this;
545 }
546
547 /** @brief Assignment from another type.
548 *
549 * @param __u The object to transfer ownership from, which owns a
550 * convertible pointer to an array object.
551 *
552 * Invokes the deleter first if this object owns a pointer.
553 */
554 template<typename _Up, typename _Ep>
555 typename
556 enable_if<__and_<__safe_conversion_up<_Up, _Ep>,
557 is_assignable<deleter_type&, _Ep&&>
558 >::value,
559 unique_ptr&>::type
560 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
561 {
562 reset(__u.release());
563 get_deleter() = std::forward<_Ep>(__u.get_deleter());
564 return *this;
565 }
566
567 /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
568 unique_ptr&
569 operator=(nullptr_t) noexcept
570 {
571 reset();
572 return *this;
573 }
574
575 // Observers.
576
577 /// Access an element of owned array.
578 typename std::add_lvalue_reference<element_type>::type
579 operator[](size_t __i) const
580 {
581 __glibcxx_assert(get() != pointer());
582 return get()[__i];
583 }
584
585 /// Return the stored pointer.
586 pointer
587 get() const noexcept
588 { return _M_t._M_ptr(); }
589
590 /// Return a reference to the stored deleter.
591 deleter_type&
592 get_deleter() noexcept
593 { return _M_t._M_deleter(); }
594
595 /// Return a reference to the stored deleter.
596 const deleter_type&
597 get_deleter() const noexcept
598 { return _M_t._M_deleter(); }
599
600 /// Return @c true if the stored pointer is not null.
601 explicit operator bool() const noexcept
602 { return get() == pointer() ? false : true; }
603
604 // Modifiers.
605
606 /// Release ownership of any stored pointer.
607 pointer
608 release() noexcept
609 {
610 pointer __p = get();
611 _M_t._M_ptr() = pointer();
612 return __p;
613 }
614
615 /** @brief Replace the stored pointer.
616 *
617 * @param __p The new pointer to store.
618 *
619 * The deleter will be invoked if a pointer is already owned.
620 */
621 template <typename _Up,
622 typename = _Require<
623 __or_<is_same<_Up, pointer>,
624 __and_<is_same<pointer, element_type*>,
625 is_pointer<_Up>,
626 is_convertible<
627 typename remove_pointer<_Up>::type(*)[],
628 element_type(*)[]
629 >
630 >
631 >
632 >>
633 void
634 reset(_Up __p) noexcept
635 {
636 pointer __ptr = __p;
637 using std::swap;
638 swap(_M_t._M_ptr(), __ptr);
639 if (__ptr != nullptr)
640 get_deleter()(__ptr);
641 }
642
643 void reset(nullptr_t = nullptr) noexcept
644 {
645 reset(pointer());
646 }
647
648 /// Exchange the pointer and deleter with another object.
649 void
650 swap(unique_ptr& __u) noexcept
651 {
652 using std::swap;
653 swap(_M_t, __u._M_t);
654 }
655
656 // Disable copy from lvalue.
657 unique_ptr(const unique_ptr&) = delete;
658 unique_ptr& operator=(const unique_ptr&) = delete;
659 };
660
661 template<typename _Tp, typename _Dp>
662 inline
663#if __cplusplus201103L > 201402L || !defined(__STRICT_ANSI__1) // c++1z or gnu++11
664 // Constrained free swap overload, see p0185r1
665 typename enable_if<__is_swappable<_Dp>::value>::type
666#else
667 void
668#endif
669 swap(unique_ptr<_Tp, _Dp>& __x,
670 unique_ptr<_Tp, _Dp>& __y) noexcept
671 { __x.swap(__y); }
672
673#if __cplusplus201103L > 201402L || !defined(__STRICT_ANSI__1) // c++1z or gnu++11
674 template<typename _Tp, typename _Dp>
675 typename enable_if<!__is_swappable<_Dp>::value>::type
676 swap(unique_ptr<_Tp, _Dp>&,
677 unique_ptr<_Tp, _Dp>&) = delete;
678#endif
679
680 template<typename _Tp, typename _Dp,
681 typename _Up, typename _Ep>
682 inline bool
683 operator==(const unique_ptr<_Tp, _Dp>& __x,
684 const unique_ptr<_Up, _Ep>& __y)
685 { return __x.get() == __y.get(); }
686
687 template<typename _Tp, typename _Dp>
688 inline bool
689 operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
690 { return !__x; }
691
692 template<typename _Tp, typename _Dp>
693 inline bool
694 operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
695 { return !__x; }
696
697 template<typename _Tp, typename _Dp,
698 typename _Up, typename _Ep>
699 inline bool
700 operator!=(const unique_ptr<_Tp, _Dp>& __x,
701 const unique_ptr<_Up, _Ep>& __y)
702 { return __x.get() != __y.get(); }
703
704 template<typename _Tp, typename _Dp>
705 inline bool
706 operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
707 { return (bool)__x; }
708
709 template<typename _Tp, typename _Dp>
710 inline bool
711 operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
712 { return (bool)__x; }
713
714 template<typename _Tp, typename _Dp,
715 typename _Up, typename _Ep>
716 inline bool
717 operator<(const unique_ptr<_Tp, _Dp>& __x,
718 const unique_ptr<_Up, _Ep>& __y)
719 {
720 typedef typename
721 std::common_type<typename unique_ptr<_Tp, _Dp>::pointer,
722 typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
723 return std::less<_CT>()(__x.get(), __y.get());
724 }
725
726 template<typename _Tp, typename _Dp>
727 inline bool
728 operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
729 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
730 nullptr); }
731
732 template<typename _Tp, typename _Dp>
733 inline bool
734 operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
735 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
736 __x.get()); }
737
738 template<typename _Tp, typename _Dp,
739 typename _Up, typename _Ep>
740 inline bool
741 operator<=(const unique_ptr<_Tp, _Dp>& __x,
742 const unique_ptr<_Up, _Ep>& __y)
743 { return !(__y < __x); }
744
745 template<typename _Tp, typename _Dp>
746 inline bool
747 operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
748 { return !(nullptr < __x); }
749
750 template<typename _Tp, typename _Dp>
751 inline bool
752 operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
753 { return !(__x < nullptr); }
754
755 template<typename _Tp, typename _Dp,
756 typename _Up, typename _Ep>
757 inline bool
758 operator>(const unique_ptr<_Tp, _Dp>& __x,
759 const unique_ptr<_Up, _Ep>& __y)
760 { return (__y < __x); }
761
762 template<typename _Tp, typename _Dp>
763 inline bool
764 operator>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
765 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
766 __x.get()); }
767
768 template<typename _Tp, typename _Dp>
769 inline bool
770 operator>(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
771 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
772 nullptr); }
773
774 template<typename _Tp, typename _Dp,
775 typename _Up, typename _Ep>
776 inline bool
777 operator>=(const unique_ptr<_Tp, _Dp>& __x,
778 const unique_ptr<_Up, _Ep>& __y)
779 { return !(__x < __y); }
780
781 template<typename _Tp, typename _Dp>
782 inline bool
783 operator>=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
784 { return !(__x < nullptr); }
785
786 template<typename _Tp, typename _Dp>
787 inline bool
788 operator>=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
789 { return !(nullptr < __x); }
790
791 /// std::hash specialization for unique_ptr.
792 template<typename _Tp, typename _Dp>
793 struct hash<unique_ptr<_Tp, _Dp>>
794 : public __hash_base<size_t, unique_ptr<_Tp, _Dp>>,
795 private __poison_hash<typename unique_ptr<_Tp, _Dp>::pointer>
796 {
797 size_t
798 operator()(const unique_ptr<_Tp, _Dp>& __u) const noexcept
799 {
800 typedef unique_ptr<_Tp, _Dp> _UP;
801 return std::hash<typename _UP::pointer>()(__u.get());
802 }
803 };
804
805#if __cplusplus201103L > 201103L
806
807#define __cpp_lib_make_unique 201304
808
809 template<typename _Tp>
810 struct _MakeUniq
811 { typedef unique_ptr<_Tp> __single_object; };
812
813 template<typename _Tp>
814 struct _MakeUniq<_Tp[]>
815 { typedef unique_ptr<_Tp[]> __array; };
816
817 template<typename _Tp, size_t _Bound>
818 struct _MakeUniq<_Tp[_Bound]>
819 { struct __invalid_type { }; };
820
821 /// std::make_unique for single objects
822 template<typename _Tp, typename... _Args>
823 inline typename _MakeUniq<_Tp>::__single_object
824 make_unique(_Args&&... __args)
825 { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
826
827 /// std::make_unique for arrays of unknown bound
828 template<typename _Tp>
829 inline typename _MakeUniq<_Tp>::__array
830 make_unique(size_t __num)
831 { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]()); }
832
833 /// Disable std::make_unique for arrays of known bound
834 template<typename _Tp, typename... _Args>
835 inline typename _MakeUniq<_Tp>::__invalid_type
836 make_unique(_Args&&...) = delete;
837#endif
838
839 // @} group pointer_abstractions
840
841_GLIBCXX_END_NAMESPACE_VERSION
842} // namespace
843
844#endif /* _UNIQUE_PTR_H */