1//===--- Stack.h - Utilities for dealing with stack space -------*- C++ -*-===// 2// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// 7//===----------------------------------------------------------------------===// 8/// 9/// \file 10/// Defines utilities for dealing with stack allocation and stack space. 11/// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_BASIC_STACK_H 15#define LLVM_CLANG_BASIC_STACK_H 16 17#include <cstddef> 18 19#include "llvm/ADT/STLExtras.h" 20#include "llvm/Support/Compiler.h" 21 22namespace clang { 23 /// The amount of stack space that Clang would like to be provided with. 24 /// If less than this much is available, we may be unable to reach our 25 /// template instantiation depth limit and other similar limits. 26 constexpr size_t DesiredStackSize = 8 << 20; 27 28 /// Call this once on each thread, as soon after starting the thread as 29 /// feasible, to note the approximate address of the bottom of the stack. 30 void noteBottomOfStack(); 31 32 /// Determine whether the stack is nearly exhausted. 33 bool isStackNearlyExhausted(); 34 35 void runWithSufficientStackSpaceSlow(llvm::function_ref<void()> Diag, 36 llvm::function_ref<void()> Fn); 37 38 /// Run a given function on a stack with "sufficient" space. If stack space 39 /// is insufficient, calls Diag to emit a diagnostic before calling Fn. 40 inline void runWithSufficientStackSpace(llvm::function_ref<void()> Diag, 41 llvm::function_ref<void()> Fn) { 42#ifdef LLVM_ENABLE_THREADS 43 if (LLVM_UNLIKELY(isStackNearlyExhausted())) 44 runWithSufficientStackSpaceSlow(Diag, Fn); 45 else 46 Fn(); 47#else 48 if (LLVM_UNLIKELY(isStackNearlyExhausted())) 49 Diag(); 50 Fn(); 51#endif 52 } 53} // end namespace clang 54 55#endif // LLVM_CLANG_BASIC_STACK_H 56