[lldb][NFC] Disallow changing the ASTContext of an ClangASTContext after construction.
We have no use case in LLDB where we actually do want to change the ASTContext after
it the ClangASTContext has been constructed. All callers of setASTContext are just setting
the ASTContext directly after construction, so we might as well make this a Constructor
instead of supporting this tricky use case.
llvm-svn: 373330
diff --git a/lldb/include/lldb/Symbol/ClangASTContext.h b/lldb/include/lldb/Symbol/ClangASTContext.h
index d0928f4..38533c0 100644
--- a/lldb/include/lldb/Symbol/ClangASTContext.h
+++ b/lldb/include/lldb/Symbol/ClangASTContext.h
@@ -54,6 +54,12 @@
// Constructors and Destructors
ClangASTContext(llvm::StringRef triple = "");
+ /// Constructs a ClangASTContext that uses an existing ASTContext internally.
+ /// Useful when having an existing ASTContext created by Clang.
+ ///
+ /// \param existing_ctxt An existing ASTContext.
+ explicit ClangASTContext(clang::ASTContext &existing_ctxt);
+
~ClangASTContext() override;
void Finalize() override;
@@ -79,8 +85,6 @@
clang::ASTContext *getASTContext();
- void setASTContext(clang::ASTContext *ast_ctx);
-
clang::Builtin::Context *getBuiltinContext();
clang::IdentifierTable *getIdentifierTable();
diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
index d632805..8801850 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
@@ -585,9 +585,7 @@
m_compiler->createASTContext();
clang::ASTContext &ast_context = m_compiler->getASTContext();
- m_ast_context.reset(
- new ClangASTContext(m_compiler->getTargetOpts().Triple.c_str()));
- m_ast_context->setASTContext(&ast_context);
+ m_ast_context.reset(new ClangASTContext(ast_context));
std::string module_name("$__lldb_module");
diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.cpp
index 887d386..f3df589 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.cpp
@@ -163,9 +163,7 @@
m_parser(std::move(parser)), m_origin_map() {
// Initialize our ClangASTContext.
- auto target_opts = m_compiler_invocation->getTargetOpts();
- m_ast_context.reset(new ClangASTContext(target_opts.Triple.c_str()));
- m_ast_context->setASTContext(&m_compiler_instance->getASTContext());
+ m_ast_context.reset(new ClangASTContext(m_compiler_instance->getASTContext()));
}
void ClangModulesDeclVendorImpl::ReportModuleExportsHelper(
diff --git a/lldb/source/Symbol/ClangASTContext.cpp b/lldb/source/Symbol/ClangASTContext.cpp
index 891410d..db962d5 100644
--- a/lldb/source/Symbol/ClangASTContext.cpp
+++ b/lldb/source/Symbol/ClangASTContext.cpp
@@ -528,6 +528,14 @@
SetTargetTriple(target_triple);
}
+ClangASTContext::ClangASTContext(ASTContext &existing_ctxt)
+ : TypeSystem(TypeSystem::eKindClang) {
+ SetTargetTriple(existing_ctxt.getTargetInfo().getTriple().str());
+
+ m_ast_up.reset(&existing_ctxt);
+ GetASTMap().Insert(&existing_ctxt, this);
+}
+
// Destructor
ClangASTContext::~ClangASTContext() { Finalize(); }
@@ -706,15 +714,6 @@
}
}
-void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
- if (!m_ast_owned) {
- m_ast_up.release();
- }
- m_ast_owned = false;
- m_ast_up.reset(ast_ctx);
- GetASTMap().Insert(ast_ctx, this);
-}
-
ASTContext *ClangASTContext::getASTContext() {
if (m_ast_up == nullptr) {
m_ast_owned = true;