Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:51

0001 //===--- Stack.h - Utilities for dealing with stack space -------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// Defines utilities for dealing with stack allocation and stack space.
0011 ///
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CLANG_BASIC_STACK_H
0015 #define LLVM_CLANG_BASIC_STACK_H
0016 
0017 #include <cstddef>
0018 
0019 #include "llvm/ADT/STLExtras.h"
0020 #include "llvm/Support/Compiler.h"
0021 
0022 namespace clang {
0023   /// The amount of stack space that Clang would like to be provided with.
0024   /// If less than this much is available, we may be unable to reach our
0025   /// template instantiation depth limit and other similar limits.
0026   constexpr size_t DesiredStackSize = 8 << 20;
0027 
0028   /// Call this once on each thread, as soon after starting the thread as
0029   /// feasible, to note the approximate address of the bottom of the stack.
0030   void noteBottomOfStack();
0031 
0032   /// Determine whether the stack is nearly exhausted.
0033   bool isStackNearlyExhausted();
0034 
0035   void runWithSufficientStackSpaceSlow(llvm::function_ref<void()> Diag,
0036                                        llvm::function_ref<void()> Fn);
0037 
0038   /// Run a given function on a stack with "sufficient" space. If stack space
0039   /// is insufficient, calls Diag to emit a diagnostic before calling Fn.
0040   inline void runWithSufficientStackSpace(llvm::function_ref<void()> Diag,
0041                                           llvm::function_ref<void()> Fn) {
0042 #if LLVM_ENABLE_THREADS
0043     if (LLVM_UNLIKELY(isStackNearlyExhausted()))
0044       runWithSufficientStackSpaceSlow(Diag, Fn);
0045     else
0046       Fn();
0047 #else
0048     if (LLVM_UNLIKELY(isStackNearlyExhausted()))
0049       Diag();
0050     Fn();
0051 #endif
0052   }
0053 } // end namespace clang
0054 
0055 #endif // LLVM_CLANG_BASIC_STACK_H