clang 20.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 void noteBottomOfStack();
31
32 /// Determine whether the stack is nearly exhausted.
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#if LLVM_ENABLE_THREADS
43 if (LLVM_UNLIKELY(isStackNearlyExhausted()))
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
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:66
void noteBottomOfStack()
Call this once on each thread, as soon after starting the thread as feasible, to note the approximate...
Definition: Stack.cpp:40
bool isStackNearlyExhausted()
Determine whether the stack is nearly exhausted.
Definition: Stack.cpp:45
constexpr size_t DesiredStackSize
The amount of stack space that Clang would like to be provided with.
Definition: Stack.h:26
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:40