zyounan updated this revision to Diff 554674. zyounan marked an inline comment as done. zyounan added a comment.
Address comment Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D158967/new/ https://reviews.llvm.org/D158967 Files: clang-tools-extra/clangd/support/Threading.cpp clang-tools-extra/clangd/test/infinite-instantiation.test clang-tools-extra/clangd/tool/ClangdMain.cpp clang/lib/Frontend/FrontendAction.cpp Index: clang/lib/Frontend/FrontendAction.cpp =================================================================== --- clang/lib/Frontend/FrontendAction.cpp +++ clang/lib/Frontend/FrontendAction.cpp @@ -15,6 +15,7 @@ #include "clang/Basic/FileEntry.h" #include "clang/Basic/LangStandard.h" #include "clang/Basic/Sarif.h" +#include "clang/Basic/Stack.h" #include "clang/Frontend/ASTUnit.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/FrontendDiagnostic.h" @@ -1155,6 +1156,10 @@ CompilerInstance &CI = getCompilerInstance(); if (!CI.hasPreprocessor()) return; + // This is a fallback: If the client forgets to invoke this, we mark the + // current stack as the bottom. Though not optimal, this could help prevent + // stack overflow during deep recursion. + clang::noteBottomOfStack(); // FIXME: Move the truncation aspect of this into Sema, we delayed this till // here so the source manager would be initialized. Index: clang-tools-extra/clangd/tool/ClangdMain.cpp =================================================================== --- clang-tools-extra/clangd/tool/ClangdMain.cpp +++ clang-tools-extra/clangd/tool/ClangdMain.cpp @@ -29,6 +29,7 @@ #include "support/ThreadCrashReporter.h" #include "support/ThreadsafeFS.h" #include "support/Trace.h" +#include "clang/Basic/Stack.h" #include "clang/Format/Format.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringRef.h" @@ -710,6 +711,9 @@ }; int clangdMain(int argc, char *argv[]) { + // Clang could run on the main thread. e.g., when the flag '-check' or '-sync' + // is enabled. + clang::noteBottomOfStack(); llvm::InitializeAllTargetInfos(); llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); llvm::sys::AddSignalHandler( Index: clang-tools-extra/clangd/test/infinite-instantiation.test =================================================================== --- /dev/null +++ clang-tools-extra/clangd/test/infinite-instantiation.test @@ -0,0 +1,13 @@ +// RUN: cp %s %t.cpp +// RUN: not clangd -check=%t.cpp 2>&1 | FileCheck -strict-whitespace %s + +// CHECK: [template_recursion_depth_exceeded] + +template <typename... T> +constexpr int f(T... args) { + return f(0, args...); +} + +int main() { + auto i = f(); +} Index: clang-tools-extra/clangd/support/Threading.cpp =================================================================== --- clang-tools-extra/clangd/support/Threading.cpp +++ clang-tools-extra/clangd/support/Threading.cpp @@ -8,6 +8,7 @@ #include "support/Threading.h" #include "support/Trace.h" +#include "clang/Basic/Stack.h" #include "llvm/ADT/ScopeExit.h" #include "llvm/Support/Threading.h" #include "llvm/Support/thread.h" @@ -98,6 +99,9 @@ auto Task = [Name = Name.str(), Action = std::move(Action), Cleanup = std::move(CleanupTask)]() mutable { llvm::set_thread_name(Name); + // Mark the bottom of the stack for clang to be aware of the stack usage and + // prevent stack overflow. + clang::noteBottomOfStack(); Action(); // Make sure function stored by ThreadFunc is destroyed before Cleanup runs. Action = nullptr;
Index: clang/lib/Frontend/FrontendAction.cpp =================================================================== --- clang/lib/Frontend/FrontendAction.cpp +++ clang/lib/Frontend/FrontendAction.cpp @@ -15,6 +15,7 @@ #include "clang/Basic/FileEntry.h" #include "clang/Basic/LangStandard.h" #include "clang/Basic/Sarif.h" +#include "clang/Basic/Stack.h" #include "clang/Frontend/ASTUnit.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/FrontendDiagnostic.h" @@ -1155,6 +1156,10 @@ CompilerInstance &CI = getCompilerInstance(); if (!CI.hasPreprocessor()) return; + // This is a fallback: If the client forgets to invoke this, we mark the + // current stack as the bottom. Though not optimal, this could help prevent + // stack overflow during deep recursion. + clang::noteBottomOfStack(); // FIXME: Move the truncation aspect of this into Sema, we delayed this till // here so the source manager would be initialized. Index: clang-tools-extra/clangd/tool/ClangdMain.cpp =================================================================== --- clang-tools-extra/clangd/tool/ClangdMain.cpp +++ clang-tools-extra/clangd/tool/ClangdMain.cpp @@ -29,6 +29,7 @@ #include "support/ThreadCrashReporter.h" #include "support/ThreadsafeFS.h" #include "support/Trace.h" +#include "clang/Basic/Stack.h" #include "clang/Format/Format.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringRef.h" @@ -710,6 +711,9 @@ }; int clangdMain(int argc, char *argv[]) { + // Clang could run on the main thread. e.g., when the flag '-check' or '-sync' + // is enabled. + clang::noteBottomOfStack(); llvm::InitializeAllTargetInfos(); llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); llvm::sys::AddSignalHandler( Index: clang-tools-extra/clangd/test/infinite-instantiation.test =================================================================== --- /dev/null +++ clang-tools-extra/clangd/test/infinite-instantiation.test @@ -0,0 +1,13 @@ +// RUN: cp %s %t.cpp +// RUN: not clangd -check=%t.cpp 2>&1 | FileCheck -strict-whitespace %s + +// CHECK: [template_recursion_depth_exceeded] + +template <typename... T> +constexpr int f(T... args) { + return f(0, args...); +} + +int main() { + auto i = f(); +} Index: clang-tools-extra/clangd/support/Threading.cpp =================================================================== --- clang-tools-extra/clangd/support/Threading.cpp +++ clang-tools-extra/clangd/support/Threading.cpp @@ -8,6 +8,7 @@ #include "support/Threading.h" #include "support/Trace.h" +#include "clang/Basic/Stack.h" #include "llvm/ADT/ScopeExit.h" #include "llvm/Support/Threading.h" #include "llvm/Support/thread.h" @@ -98,6 +99,9 @@ auto Task = [Name = Name.str(), Action = std::move(Action), Cleanup = std::move(CleanupTask)]() mutable { llvm::set_thread_name(Name); + // Mark the bottom of the stack for clang to be aware of the stack usage and + // prevent stack overflow. + clang::noteBottomOfStack(); Action(); // Make sure function stored by ThreadFunc is destroyed before Cleanup runs. Action = nullptr;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits