54
54
#include " llvm/Support/CommandLine.h"
55
55
#include " llvm/Support/Debug.h"
56
56
#include " llvm/Support/Error.h"
57
+ #include " llvm/Support/ErrorHandling.h"
57
58
#include " llvm/Support/ManagedStatic.h"
58
59
#include " llvm/Support/Path.h"
59
60
#include " llvm/Support/raw_ostream.h"
@@ -140,10 +141,10 @@ struct InterpreterInfo {
140
141
141
142
// NOLINTBEGIN
142
143
// std::deque avoids relocations and calling the dtor of InterpreterInfo.
143
- static llvm::ManagedStatic<std::deque<std::shared_ptr <InterpreterInfo>>>
144
+ static llvm::ManagedStatic<std::deque<std::unique_ptr <InterpreterInfo>>>
144
145
sInterpreters ;
145
146
static llvm::ManagedStatic<
146
- std::unordered_map<clang::ASTContext*, std::weak_ptr< InterpreterInfo> >>
147
+ std::unordered_map<clang::ASTContext*, InterpreterInfo* >>
147
148
sInterpreterASTMap ;
148
149
static std::recursive_mutex InterpreterStackLock;
149
150
static std::recursive_mutex LLVMLock;
@@ -161,7 +162,7 @@ static InterpreterInfo& getInterpInfo(const clang::Decl* D) {
161
162
return getInterpInfo ();
162
163
if (sInterpreters ->size () == 1 )
163
164
return *sInterpreters ->back ();
164
- return *(*sInterpreterASTMap )[&D->getASTContext ()]. lock () ;
165
+ return *(*sInterpreterASTMap )[&D->getASTContext ()];
165
166
}
166
167
static InterpreterInfo& getInterpInfo (const void * D) {
167
168
std::lock_guard<std::recursive_mutex> Lock (InterpreterStackLock);
@@ -171,7 +172,7 @@ static InterpreterInfo& getInterpInfo(const void* D) {
171
172
return *sInterpreters ->back ();
172
173
for (auto & item : *sInterpreterASTMap ) {
173
174
if (item.first ->getAllocator ().identifyObject (D))
174
- return *item.second . lock () ;
175
+ return *item.second ;
175
176
}
176
177
llvm_unreachable (
177
178
" This pointer does not belong to any interpreter instance.\n " );
@@ -189,7 +190,7 @@ static compat::Interpreter& getInterp(const clang::Decl* D) {
189
190
return getInterp ();
190
191
if (sInterpreters ->size () == 1 )
191
192
return *sInterpreters ->back ()->Interpreter ;
192
- return *(*sInterpreterASTMap )[&D->getASTContext ()]. lock () ->Interpreter ;
193
+ return *(*sInterpreterASTMap )[&D->getASTContext ()]->Interpreter ;
193
194
}
194
195
static compat::Interpreter& getInterp (const void * D) {
195
196
return *getInterpInfo (D).Interpreter ;
@@ -3338,6 +3339,8 @@ static std::string MakeResourcesPath() {
3338
3339
TInterp_t CreateInterpreter (const std::vector<const char *>& Args /* ={}*/ ,
3339
3340
const std::vector<const char *>& GpuArgs /* ={}*/ ) {
3340
3341
std::lock_guard<std::recursive_mutex> Lock (InterpreterStackLock);
3342
+ assert (sInterpreters ->size () == sInterpreterASTMap ->size ());
3343
+
3341
3344
std::string MainExecutableName = sys::fs::getMainExecutable (nullptr , nullptr );
3342
3345
std::string ResourceDir = MakeResourcesPath ();
3343
3346
std::vector<const char *> ClingArgv = {" -resource-dir" , ResourceDir.c_str (),
@@ -3414,42 +3417,73 @@ TInterp_t CreateInterpreter(const std::vector<const char*>& Args /*={}*/,
3414
3417
)" );
3415
3418
3416
3419
sInterpreters ->emplace_back (
3417
- std::make_shared <InterpreterInfo>(I, /* Owned=*/ true ));
3420
+ std::make_unique <InterpreterInfo>(I, /* Owned=*/ true ));
3418
3421
sInterpreterASTMap ->insert (
3419
3422
{&sInterpreters ->back ()->Interpreter ->getSema ().getASTContext (),
3420
- sInterpreters ->back ()});
3423
+ sInterpreters ->back (). get () });
3421
3424
3425
+ assert (sInterpreters ->size () == sInterpreterASTMap ->size ());
3422
3426
return I;
3423
3427
}
3424
3428
3429
+ static inline auto find_interpreter_in_stack (TInterp_t I) {
3430
+ return std::find_if (
3431
+ sInterpreters ->begin (), sInterpreters ->end (),
3432
+ [&I](const auto & Info) { return Info->Interpreter == I; });
3433
+ }
3434
+
3435
+ static inline auto find_interpreter_in_map (InterpreterInfo* I) {
3436
+ return std::find_if (sInterpreterASTMap ->begin (), sInterpreterASTMap ->end (),
3437
+ [](const auto & Item) {
3438
+ return Item.second == sInterpreters ->back ().get ();
3439
+ });
3440
+ }
3441
+
3425
3442
bool DeleteInterpreter (TInterp_t I /* =nullptr*/ ) {
3426
3443
std::lock_guard<std::recursive_mutex> Lock (InterpreterStackLock);
3444
+ assert (sInterpreters ->size () == sInterpreterASTMap ->size ());
3427
3445
3428
3446
if (!I) {
3429
- auto foundAST =
3430
- std::find_if (sInterpreterASTMap ->begin (), sInterpreterASTMap ->end (),
3431
- [](const auto & Item) {
3432
- return Item.second .lock () == sInterpreters ->back ();
3433
- });
3447
+ auto foundAST = find_interpreter_in_map (sInterpreters ->back ().get ());
3434
3448
sInterpreterASTMap ->erase (foundAST);
3435
3449
sInterpreters ->pop_back ();
3436
3450
return true ;
3437
3451
}
3438
3452
3439
- auto found =
3440
- std::find_if (sInterpreters ->begin (), sInterpreters ->end (),
3441
- [&I](const auto & Info) { return Info->Interpreter == I; });
3453
+ auto found = find_interpreter_in_stack (I);
3442
3454
if (found == sInterpreters ->end ())
3443
3455
return false ; // failure
3444
3456
3445
- auto foundAST = std::find_if (
3446
- sInterpreterASTMap ->begin (), sInterpreterASTMap ->end (),
3447
- [&found](const auto & Item) { return Item.second .lock () == *found; });
3457
+ auto foundAST = find_interpreter_in_map ((*found).get ());
3448
3458
sInterpreterASTMap ->erase (foundAST);
3449
3459
sInterpreters ->erase (found);
3450
3460
return true ;
3451
3461
}
3452
3462
3463
+ TInterp_t TakeInterpreter (TInterp_t I /* =nullptr*/ ) {
3464
+ std::lock_guard<std::recursive_mutex> Lock (InterpreterStackLock);
3465
+ assert (sInterpreters ->size () == sInterpreterASTMap ->size ());
3466
+
3467
+ if (!I) {
3468
+ auto foundAST = find_interpreter_in_map (sInterpreters ->back ().get ());
3469
+ sInterpreterASTMap ->erase (foundAST);
3470
+ InterpreterInfo* res = sInterpreters ->back ().release ();
3471
+ sInterpreters ->pop_back ();
3472
+ return res->Interpreter ;
3473
+ }
3474
+
3475
+ auto found = find_interpreter_in_stack (I);
3476
+ if (found == sInterpreters ->end ())
3477
+ return nullptr ; // failure
3478
+
3479
+ auto foundAST = find_interpreter_in_map ((*found).get ());
3480
+ sInterpreterASTMap ->erase (foundAST);
3481
+ InterpreterInfo* res = (*found).release ();
3482
+ sInterpreters ->erase (found);
3483
+ assert (sInterpreters ->size () == sInterpreterASTMap ->size ());
3484
+ return res->Interpreter ;
3485
+ }
3486
+
3453
3487
bool ActivateInterpreter (TInterp_t I) {
3454
3488
std::lock_guard<std::recursive_mutex> Lock (InterpreterStackLock);
3455
3489
@@ -3477,10 +3511,13 @@ TInterp_t GetInterpreter() {
3477
3511
3478
3512
void UseExternalInterpreter (TInterp_t I) {
3479
3513
std::lock_guard<std::recursive_mutex> Lock (InterpreterStackLock);
3480
- assert (sInterpreters ->empty () && " sInterpreter already in use!" );
3481
3514
sInterpreters ->emplace_back (
3482
- std::make_shared <InterpreterInfo>(static_cast <compat::Interpreter*>(I),
3515
+ std::make_unique <InterpreterInfo>(static_cast <compat::Interpreter*>(I),
3483
3516
/* isOwned=*/ false ));
3517
+ sInterpreterASTMap ->insert (
3518
+ {&sInterpreters ->back ()->Interpreter ->getSema ().getASTContext (),
3519
+ sInterpreters ->back ().get ()});
3520
+ assert (sInterpreters ->size () == sInterpreterASTMap ->size ());
3484
3521
}
3485
3522
3486
3523
void AddSearchPath (const char * dir, bool isUser, bool prepend) {
0 commit comments