Skip to content

Commit 2374f60

Browse files
committed
Replace std::regex in codecomplete to use llvm::regex
1 parent 2c6c6b1 commit 2374f60

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

lib/Interpreter/Compatibility.h

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ static inline char* GetEnv(const char* Var_Name) {
7878
#include "llvm/Support/Casting.h"
7979
#include "llvm/Support/Path.h"
8080

81+
// std::regex breaks pytorch's jit: pytorch/pytorch#49460
82+
#include "llvm/Support/Regex.h"
83+
8184
#ifdef USE_CLING
8285

8386
#include "cling/Interpreter/DynamicLibraryManager.h"
@@ -163,17 +166,45 @@ inline void codeComplete(std::vector<std::string>& Results,
163166
std::vector<std::string> results;
164167
size_t column = complete_column;
165168
I.codeComplete(code, column, results);
169+
std::string error;
170+
llvm::Error Err;
171+
// Regex patterns
172+
llvm::Regex removeDefinition("\\[\\#.*\\#\\]");
173+
llvm::Regex removeVariableName("(\\ |\\*)+(\\w+)(\\#\\>)");
174+
llvm::Regex removeTrailingSpace("\\ *(\\#\\>)");
175+
llvm::Regex removeTags("\\<\\#([^#>]*)\\#\\>");
166176

167177
// append cleaned results
168178
for (auto& r : results) {
169-
// remove the definition at the beginning (for example [#int#])
170-
r = std::regex_replace(r, std::regex("\\[\\#.*\\#\\]"), "");
179+
// remove the definition at the beginning (e.g., [#int#])
180+
r = removeDefinition.sub("", r, &error);
181+
if (!error.empty()) {
182+
Err = llvm::make_error<llvm::StringError>(error, llvm::inconvertibleErrorCode());
183+
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "Invalid substitution in CodeComplete");
184+
return;
185+
}
171186
// remove the variable name in <#type name#>
172-
r = std::regex_replace(r, std::regex("(\\ |\\*)+(\\w+)(\\#\\>)"), "$1$3");
187+
r = removeVariableName.sub("$1$3", r, &error);
188+
if (!error.empty()) {
189+
Err = llvm::make_error<llvm::StringError>(error, llvm::inconvertibleErrorCode());
190+
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "Invalid substitution in CodeComplete");
191+
return;
192+
}
173193
// remove unnecessary space at the end of <#type #>
174-
r = std::regex_replace(r, std::regex("\\ *(\\#\\>)"), "$1");
194+
r = removeTrailingSpace.sub("$1", r, &error);
195+
if (!error.empty()) {
196+
Err = llvm::make_error<llvm::StringError>(error, llvm::inconvertibleErrorCode());
197+
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "Invalid substitution in CodeComplete");
198+
return;
199+
}
175200
// remove <# #> to keep only the type
176-
r = std::regex_replace(r, std::regex("\\<\\#([^#>]*)\\#\\>"), "$1");
201+
r = removeTags.sub("$1", r, &error);
202+
if (!error.empty()) {
203+
Err = llvm::make_error<llvm::StringError>(error, llvm::inconvertibleErrorCode());
204+
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "Invalid substitution in CodeComplete");
205+
return;
206+
}
207+
177208
if (r.find(code) == 0)
178209
Results.push_back(r);
179210
}

0 commit comments

Comments
 (0)