Skip to content

Commit e34c19e

Browse files
committed
Replace std::regex in codecomplete to use llvm::regex
1 parent 508823a commit e34c19e

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

lib/Interpreter/Compatibility.h

Lines changed: 15 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,24 @@ 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+
// Regex patterns
171+
llvm::Regex removeDefinition("\\[\\#.*\\#\\]");
172+
llvm::Regex removeVariableName("(\\ |\\*)+(\\w+)(\\#\\>)");
173+
llvm::Regex removeTrailingSpace("\\ *(\\#\\>)");
174+
llvm::Regex removeTags("\\<\\#([^#>]*)\\#\\>");
166175

167176
// append cleaned results
168177
for (auto& r : results) {
169-
// remove the definition at the beginning (for example [#int#])
170-
r = std::regex_replace(r, std::regex("\\[\\#.*\\#\\]"), "");
178+
// remove the definition at the beginning (e.g., [#int#])
179+
r = removeDefinition.sub("", r, &error);
171180
// remove the variable name in <#type name#>
172-
r = std::regex_replace(r, std::regex("(\\ |\\*)+(\\w+)(\\#\\>)"), "$1$3");
181+
r = removeVariableName.sub("$1$3", r, &error);
173182
// remove unnecessary space at the end of <#type #>
174-
r = std::regex_replace(r, std::regex("\\ *(\\#\\>)"), "$1");
183+
r = removeTrailingSpace.sub("$1", r, &error);
175184
// remove <# #> to keep only the type
176-
r = std::regex_replace(r, std::regex("\\<\\#([^#>]*)\\#\\>"), "$1");
185+
r = removeTags.sub("$1", r, &error);
186+
177187
if (r.find(code) == 0)
178188
Results.push_back(r);
179189
}

0 commit comments

Comments
 (0)