1 //===--- StackExhaustionHandler.h - A utility for warning once when close to out 2 // of stack space -------*- C++ -*-===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// Defines a utilitiy for warning once when close to out of stack space. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_CLANG_BASIC_STACK_EXHAUSTION_HANDLER_H 16 #define LLVM_CLANG_BASIC_STACK_EXHAUSTION_HANDLER_H 17 18 #include "clang/Basic/Diagnostic.h" 19 20 namespace clang { 21 class StackExhaustionHandler { 22 public: StackExhaustionHandler(DiagnosticsEngine & diags)23 StackExhaustionHandler(DiagnosticsEngine &diags) : DiagsRef(diags) {} 24 25 /// Run some code with "sufficient" stack space. (Currently, at least 256K 26 /// is guaranteed). Produces a warning if we're low on stack space and 27 /// allocates more in that case. Use this in code that may recurse deeply to 28 /// avoid stack overflow. 29 void runWithSufficientStackSpace(SourceLocation Loc, 30 llvm::function_ref<void()> Fn); 31 32 /// Check to see if we're low on stack space and produce a warning if we're 33 /// low on stack space (Currently, at least 256Kis guaranteed). 34 void warnOnStackNearlyExhausted(SourceLocation Loc); 35 36 private: 37 /// Warn that the stack is nearly exhausted. 38 void warnStackExhausted(SourceLocation Loc); 39 40 DiagnosticsEngine &DiagsRef; 41 bool WarnedStackExhausted = false; 42 }; 43 } // end namespace clang 44 45 #endif // LLVM_CLANG_BASIC_STACK_EXHAUSTION_HANDLER_H 46