Skip to content

Commit 33d0381

Browse files
committed
Use llvm::regex instead of std::regex to avoid triggering torchlib segfault
1 parent 0eecb9c commit 33d0381

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

Diff for: interpreter/CppInterOp/lib/Interpreter/Compatibility.h

+14-6
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ static inline char* GetEnv(const char* Var_Name) {
7777
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
7878
#include "llvm/Support/Casting.h"
7979
#include "llvm/Support/Path.h"
80+
#include "llvm/Support/Regex.h"
8081

8182
#ifdef USE_CLING
8283

@@ -87,7 +88,7 @@ static inline char* GetEnv(const char* Var_Name) {
8788

8889
#include "cling/Utils/AST.h"
8990

90-
#include <regex>
91+
// #include <regex>
9192

9293
namespace Cpp {
9394
namespace Cpp_utils = cling::utils;
@@ -158,17 +159,24 @@ inline void codeComplete(std::vector<std::string>& Results,
158159
std::vector<std::string> results;
159160
size_t column = complete_column;
160161
I.codeComplete(code, column, results);
162+
std::string error;
163+
// Regex patterns
164+
llvm::Regex removeDefinition("\\[\\#.*\\#\\]");
165+
llvm::Regex removeVariableName("(\\ |\\*)+(\\w+)(\\#\\>)");
166+
llvm::Regex removeTrailingSpace("\\ *(\\#\\>)");
167+
llvm::Regex removeTags("\\<\\#([^#>]*)\\#\\>");
161168

162169
// append cleaned results
163170
for (auto& r : results) {
164-
// remove the definition at the beginning (for example [#int#])
165-
r = std::regex_replace(r, std::regex("\\[\\#.*\\#\\]"), "");
171+
// remove the definition at the beginning (e.g., [#int#])
172+
r = removeDefinition.sub("", r, &error);
166173
// remove the variable name in <#type name#>
167-
r = std::regex_replace(r, std::regex("(\\ |\\*)+(\\w+)(\\#\\>)"), "$1$3");
174+
r = removeVariableName.sub("$1$3", r, &error);
168175
// remove unnecessary space at the end of <#type #>
169-
r = std::regex_replace(r, std::regex("\\ *(\\#\\>)"), "$1");
176+
r = removeTrailingSpace.sub("$1", r, &error);
170177
// remove <# #> to keep only the type
171-
r = std::regex_replace(r, std::regex("\\<\\#([^#>]*)\\#\\>"), "$1");
178+
r = removeTags.sub("$1", r, &error);
179+
172180
if (r.find(code) == 0)
173181
Results.push_back(r);
174182
}

0 commit comments

Comments
 (0)