[lldb][NFC] Remove all ASTContext getter wrappers from ClangASTContext

Their naming is misleading as they only return the
ClangASTContext-owned variables. For ClangASTContext instances constructed
for a given clang::ASTContext they silently generated duplicated instances
(e.g., a second IdentifierTable) that were essentially unusable.

This removes all these getters as they are anyway not very useful in comparison
to just calling the clang::ASTContext getters. The initialization
code has been moved to the CreateASTContext initialization method so that all
code for making our own clang::ASTContext is in one place.
This commit is contained in:
Raphael Isemann 2019-12-21 15:26:24 +01:00
parent d269255b95
commit 49b206f958
7 changed files with 59 additions and 120 deletions

View File

@ -102,22 +102,6 @@ public:
clang::ASTContext *getASTContext();
clang::Builtin::Context *getBuiltinContext();
clang::IdentifierTable *getIdentifierTable();
clang::LangOptions *getLanguageOptions();
clang::SelectorTable *getSelectorTable();
clang::FileManager *getFileManager();
clang::SourceManager *getSourceManager();
clang::DiagnosticsEngine *getDiagnosticsEngine();
clang::DiagnosticConsumer *getDiagnosticConsumer();
clang::MangleContext *getMangleContext();
std::shared_ptr<clang::TargetOptions> &getTargetOptions();

View File

@ -56,12 +56,10 @@ ClangASTSource::ClangASTSource(const lldb::TargetSP &target,
m_ast_importer_sp = importer;
}
void ClangASTSource::InstallASTContext(ClangASTContext &clang_ast_context,
clang::FileManager &file_manager,
bool is_shared_context) {
void ClangASTSource::InstallASTContext(ClangASTContext &clang_ast_context) {
m_ast_context = clang_ast_context.getASTContext();
m_clang_ast_context = &clang_ast_context;
m_file_manager = &file_manager;
m_file_manager = &m_ast_context->getSourceManager().getFileManager();
m_ast_importer_sp->InstallMapCompleter(m_ast_context, *this);
}

View File

@ -60,9 +60,7 @@ public:
}
void MaterializeVisibleDecls(const clang::DeclContext *DC) { return; }
void InstallASTContext(ClangASTContext &ast_context,
clang::FileManager &file_manager,
bool is_shared_context = false);
void InstallASTContext(ClangASTContext &ast_context);
//
// APIs for ExternalASTSource

View File

@ -997,7 +997,7 @@ ClangExpressionParser::ParseInternal(DiagnosticManager &diagnostic_manager,
} else {
ast_context.setExternalSource(ast_source);
}
decl_map->InstallASTContext(*m_ast_context, m_compiler->getFileManager());
decl_map->InstallASTContext(*m_ast_context);
}
// Check that the ASTReader is properly attached to ASTContext and Sema.

View File

@ -571,9 +571,7 @@ lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
new ClangASTContextForExpressions(*target, fixed_arch));
ast_sp->m_scratch_ast_source_up.reset(new ClangASTSource(
target->shared_from_this(), target->GetClangASTImporter()));
lldbassert(ast_sp->getFileManager());
ast_sp->m_scratch_ast_source_up->InstallASTContext(
*ast_sp, *ast_sp->getFileManager(), true);
ast_sp->m_scratch_ast_source_up->InstallASTContext(*ast_sp);
llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
ast_sp->m_scratch_ast_source_up->CreateProxy());
ast_sp->SetExternalSource(proxy_ast_source);
@ -663,14 +661,60 @@ ASTContext *ClangASTContext::getASTContext() {
return m_ast_up.get();
}
class NullDiagnosticConsumer : public DiagnosticConsumer {
public:
NullDiagnosticConsumer() {
m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
}
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
const clang::Diagnostic &info) override {
if (m_log) {
llvm::SmallVector<char, 32> diag_str(10);
info.FormatDiagnostic(diag_str);
diag_str.push_back('\0');
LLDB_LOGF(m_log, "Compiler diagnostic: %s\n", diag_str.data());
}
}
DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
return new NullDiagnosticConsumer();
}
private:
Log *m_log;
};
void ClangASTContext::CreateASTContext() {
assert(!m_ast_up);
m_ast_owned = true;
m_ast_up.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
*getIdentifierTable(), *getSelectorTable(),
*getBuiltinContext()));
m_ast_up->getDiagnostics().setClient(getDiagnosticConsumer(), false);
m_language_options_up.reset(new LangOptions());
ParseLangArgs(*m_language_options_up, clang::Language::ObjCXX,
GetTargetTriple());
m_identifier_table_up.reset(
new IdentifierTable(*m_language_options_up, nullptr));
m_builtins_up.reset(new Builtin::Context());
m_selector_table_up.reset(new SelectorTable());
clang::FileSystemOptions file_system_options;
m_file_manager_up.reset(new clang::FileManager(
file_system_options, FileSystem::Instance().GetVirtualFileSystem()));
llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
m_diagnostics_engine_up.reset(
new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
m_source_manager_up.reset(
new clang::SourceManager(*m_diagnostics_engine_up, *m_file_manager_up));
m_ast_up.reset(new ASTContext(*m_language_options_up, *m_source_manager_up,
*m_identifier_table_up, *m_selector_table_up,
*m_builtins_up));
m_diagnostic_consumer_up.reset(new NullDiagnosticConsumer);
m_ast_up->getDiagnostics().setClient(m_diagnostic_consumer_up.get(), false);
// This can be NULL if we don't know anything about the architecture or if
// the target for an architecture isn't enabled in the llvm/clang that we
@ -699,97 +743,12 @@ ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
return clang_ast;
}
Builtin::Context *ClangASTContext::getBuiltinContext() {
if (m_builtins_up == nullptr)
m_builtins_up.reset(new Builtin::Context());
return m_builtins_up.get();
}
IdentifierTable *ClangASTContext::getIdentifierTable() {
if (m_identifier_table_up == nullptr)
m_identifier_table_up.reset(
new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
return m_identifier_table_up.get();
}
LangOptions *ClangASTContext::getLanguageOptions() {
if (m_language_options_up == nullptr) {
m_language_options_up.reset(new LangOptions());
ParseLangArgs(*m_language_options_up, clang::Language::ObjCXX,
GetTargetTriple());
// InitializeLangOptions(*m_language_options_up, Language::ObjCXX);
}
return m_language_options_up.get();
}
SelectorTable *ClangASTContext::getSelectorTable() {
if (m_selector_table_up == nullptr)
m_selector_table_up.reset(new SelectorTable());
return m_selector_table_up.get();
}
clang::FileManager *ClangASTContext::getFileManager() {
if (m_file_manager_up == nullptr) {
clang::FileSystemOptions file_system_options;
m_file_manager_up.reset(new clang::FileManager(
file_system_options, FileSystem::Instance().GetVirtualFileSystem()));
}
return m_file_manager_up.get();
}
clang::SourceManager *ClangASTContext::getSourceManager() {
if (m_source_manager_up == nullptr)
m_source_manager_up.reset(
new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
return m_source_manager_up.get();
}
clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
if (m_diagnostics_engine_up == nullptr) {
llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
m_diagnostics_engine_up.reset(
new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
}
return m_diagnostics_engine_up.get();
}
clang::MangleContext *ClangASTContext::getMangleContext() {
if (m_mangle_ctx_up == nullptr)
m_mangle_ctx_up.reset(getASTContext()->createMangleContext());
return m_mangle_ctx_up.get();
}
class NullDiagnosticConsumer : public DiagnosticConsumer {
public:
NullDiagnosticConsumer() {
m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
}
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
const clang::Diagnostic &info) override {
if (m_log) {
llvm::SmallVector<char, 32> diag_str(10);
info.FormatDiagnostic(diag_str);
diag_str.push_back('\0');
LLDB_LOGF(m_log, "Compiler diagnostic: %s\n", diag_str.data());
}
}
DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
return new NullDiagnosticConsumer();
}
private:
Log *m_log;
};
DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
if (m_diagnostic_consumer_up == nullptr)
m_diagnostic_consumer_up.reset(new NullDiagnosticConsumer);
return m_diagnostic_consumer_up.get();
}
std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
if (m_target_options_rp == nullptr && !m_target_triple.empty()) {
m_target_options_rp = std::make_shared<clang::TargetOptions>();
@ -802,8 +761,8 @@ std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
TargetInfo *ClangASTContext::getTargetInfo() {
// target_triple should be something like "x86_64-apple-macosx"
if (m_target_info_up == nullptr && !m_target_triple.empty())
m_target_info_up.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
getTargetOptions()));
m_target_info_up.reset(TargetInfo::CreateTargetInfo(
getASTContext()->getDiagnostics(), getTargetOptions()));
return m_target_info_up.get();
}

View File

@ -75,7 +75,7 @@ struct ClangExpressionDeclMapTest : public testing::Test {
importer = std::make_shared<ClangASTImporter>();
decl_map = std::make_unique<FakeClangExpressionDeclMap>(importer);
target_ast = clang_utils::createAST();
decl_map->InstallASTContext(*target_ast, *target_ast->getFileManager());
decl_map->InstallASTContext(*target_ast);
}
void TearDown() override {

View File

@ -17,7 +17,7 @@ namespace lldb_private {
namespace clang_utils {
inline clang::DeclarationName getDeclarationName(ClangASTContext &ast,
llvm::StringRef name) {
clang::IdentifierInfo &II = ast.getIdentifierTable()->get(name);
clang::IdentifierInfo &II = ast.getASTContext()->Idents.get(name);
return ast.getASTContext()->DeclarationNames.getIdentifier(&II);
}