clang 22.0.0git
Stack.h
Go to the documentation of this file.
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 ///
31 /// \param ForceSet set to true if you know the call is near the bottom of a
32 /// new stack. Used for split stacks.
33 void noteBottomOfStack(bool ForceSet = false);
34
35 /// Determine whether the stack is nearly exhausted.
37
38 void runWithSufficientStackSpaceSlow(llvm::function_ref<void()> Diag,
39 llvm::function_ref<void()> Fn);
40
41 /// Run a given function on a stack with "sufficient" space. If stack space
42 /// is insufficient, calls Diag to emit a diagnostic before calling Fn.
43 inline void runWithSufficientStackSpace(llvm::function_ref<void()> Diag,
44 llvm::function_ref<void()> Fn) {
45#if LLVM_ENABLE_THREADS
46 if (LLVM_UNLIKELY(isStackNearlyExhausted()))
48 else
49 Fn();
50#else
51 if (LLVM_UNLIKELY(isStackNearlyExhausted()))
52 Diag();
53 Fn();
54#endif
55 }
56} // end namespace clang
57
58#endif // LLVM_CLANG_BASIC_STACK_H
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
The JSON file list parser is used to communicate input to InstallAPI.
void runWithSufficientStackSpaceSlow(llvm::function_ref< void()> Diag, llvm::function_ref< void()> Fn)
Definition: Stack.cpp:47
bool isStackNearlyExhausted()
Determine whether the stack is nearly exhausted.
Definition: Stack.cpp:25
constexpr size_t DesiredStackSize
The amount of stack space that Clang would like to be provided with.
Definition: Stack.h:26
void noteBottomOfStack(bool ForceSet=false)
Call this once on each thread, as soon after starting the thread as feasible, to note the approximate...
Definition: Stack.cpp:20
void runWithSufficientStackSpace(llvm::function_ref< void()> Diag, llvm::function_ref< void()> Fn)
Run a given function on a stack with "sufficient" space.
Definition: Stack.h:43