[Lldb-commits] [lldb] 40bd809 - [lldb][NFC] Simplify ClangExternalASTSourceCallbacks

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Mon Dec 23 04:24:55 PST 2019


Author: Raphael Isemann
Date: 2019-12-23T13:22:29+01:00
New Revision: 40bd809b6d5cfe69ffcb567245bc521b971a80eb

URL: https://github.com/llvm/llvm-project/commit/40bd809b6d5cfe69ffcb567245bc521b971a80eb
DIFF: https://github.com/llvm/llvm-project/commit/40bd809b6d5cfe69ffcb567245bc521b971a80eb.diff

LOG: [lldb][NFC] Simplify ClangExternalASTSourceCallbacks

This class is only used by the ClangASTContext so we might as well
simplify this whole logic by just passing a ClangASTContext instead
of a list of callbacks and a void* pointer. If we ever need this
to support other classes then we can define some interface that
ClangASTContext implements but for now this isn't needed.

I also removed any code for m_callback_find_by_name as this was
always a nullptr in LLDB and removed all overriden implementations
that just redefined the default no-op implementation that the
ExternalASTSource provides.

Also removed the assert.h workarounds.

Added: 
    

Modified: 
    lldb/include/lldb/Symbol/ClangASTContext.h
    lldb/include/lldb/Symbol/ClangExternalASTSourceCallbacks.h
    lldb/source/Symbol/ClangASTContext.cpp
    lldb/source/Symbol/ClangExternalASTSourceCallbacks.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Symbol/ClangASTContext.h b/lldb/include/lldb/Symbol/ClangASTContext.h
index 255a9af17bbb..0658baa0cecc 100644
--- a/lldb/include/lldb/Symbol/ClangASTContext.h
+++ b/lldb/include/lldb/Symbol/ClangASTContext.h
@@ -402,14 +402,12 @@ class ClangASTContext : public TypeSystem {
   PDBASTParser *GetPDBParser() override;
 
   // ClangASTContext callbacks for external source lookups.
-  static void CompleteTagDecl(void *baton, clang::TagDecl *);
+  void CompleteTagDecl(clang::TagDecl *);
 
-  static void CompleteObjCInterfaceDecl(void *baton,
-                                        clang::ObjCInterfaceDecl *);
+  void CompleteObjCInterfaceDecl(clang::ObjCInterfaceDecl *);
 
-  static bool LayoutRecordType(
-      void *baton, const clang::RecordDecl *record_decl, uint64_t &size,
-      uint64_t &alignment,
+  bool LayoutRecordType(
+      const clang::RecordDecl *record_decl, uint64_t &size, uint64_t &alignment,
       llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
       llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
           &base_offsets,

diff  --git a/lldb/include/lldb/Symbol/ClangExternalASTSourceCallbacks.h b/lldb/include/lldb/Symbol/ClangExternalASTSourceCallbacks.h
index a2d4f8137a05..ed7e3b8ece7a 100644
--- a/lldb/include/lldb/Symbol/ClangExternalASTSourceCallbacks.h
+++ b/lldb/include/lldb/Symbol/ClangExternalASTSourceCallbacks.h
@@ -9,86 +9,21 @@
 #ifndef liblldb_ClangExternalASTSourceCallbacks_h_
 #define liblldb_ClangExternalASTSourceCallbacks_h_
 
-#include <stdint.h>
-
-#include "clang/AST/CharUnits.h"
-#include "llvm/ADT/DenseMap.h"
-
-#include "lldb/Core/ClangForward.h"
 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
-#include "lldb/Symbol/CompilerType.h"
-#include "lldb/lldb-enumerations.h"
 
 namespace lldb_private {
 
+class ClangASTContext;
+
 class ClangExternalASTSourceCallbacks : public ClangExternalASTSourceCommon {
 public:
-  typedef void (*CompleteTagDeclCallback)(void *baton, clang::TagDecl *);
-  typedef void (*CompleteObjCInterfaceDeclCallback)(void *baton,
-                                                    clang::ObjCInterfaceDecl *);
-  typedef void (*FindExternalVisibleDeclsByNameCallback)(
-      void *baton, const clang::DeclContext *DC, clang::DeclarationName Name,
-      llvm::SmallVectorImpl<clang::NamedDecl *> *results);
-  typedef bool (*LayoutRecordTypeCallback)(
-      void *baton, const clang::RecordDecl *Record, uint64_t &Size,
-      uint64_t &Alignment,
-      llvm::DenseMap<const clang::FieldDecl *, uint64_t> &FieldOffsets,
-      llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
-          &BaseOffsets,
-      llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
-          &VirtualBaseOffsets);
-
-  ClangExternalASTSourceCallbacks(
-      CompleteTagDeclCallback tag_decl_callback,
-      CompleteObjCInterfaceDeclCallback objc_decl_callback,
-      FindExternalVisibleDeclsByNameCallback find_by_name_callback,
-      LayoutRecordTypeCallback layout_record_type_callback,
-      void *callback_baton)
-      : m_callback_tag_decl(tag_decl_callback),
-        m_callback_objc_decl(objc_decl_callback),
-        m_callback_find_by_name(find_by_name_callback),
-        m_callback_layout_record_type(layout_record_type_callback),
-        m_callback_baton(callback_baton) {}
-
-  // clang::ExternalASTSource
-
-  clang::Decl *GetExternalDecl(uint32_t ID) override {
-    // This method only needs to be implemented if the AST source ever passes
-    // back decl sets as VisibleDeclaration objects.
-    return nullptr;
-  }
-
-  clang::Stmt *GetExternalDeclStmt(uint64_t Offset) override {
-    // This operation is meant to be used via a LazyOffsetPtr.  It only needs
-    // to be implemented if the AST source uses methods like
-    // FunctionDecl::setLazyBody when building decls.
-    return nullptr;
-  }
-
-  clang::Selector GetExternalSelector(uint32_t ID) override {
-    // This operation only needs to be implemented if the AST source returns
-    // non-zero for GetNumKnownSelectors().
-    return clang::Selector();
-  }
-
-  uint32_t GetNumExternalSelectors() override { return 0; }
-
-  clang::CXXBaseSpecifier *
-  GetExternalCXXBaseSpecifiers(uint64_t Offset) override {
-    return nullptr;
-  }
-
-  virtual void MaterializeVisibleDecls(const clang::DeclContext *decl_ctx) {}
+  ClangExternalASTSourceCallbacks(ClangASTContext &ast) : m_ast(ast) {}
 
   void FindExternalLexicalDecls(
       const clang::DeclContext *DC,
       llvm::function_ref<bool(clang::Decl::Kind)> IsKindWeWant,
       llvm::SmallVectorImpl<clang::Decl *> &Result) override;
 
-  bool
-  FindExternalVisibleDeclsByName(const clang::DeclContext *decl_ctx,
-                                 clang::DeclarationName decl_name) override;
-
   void CompleteType(clang::TagDecl *tag_decl) override;
 
   void CompleteType(clang::ObjCInterfaceDecl *objc_decl) override;
@@ -101,36 +36,8 @@ class ClangExternalASTSourceCallbacks : public ClangExternalASTSourceCommon {
       llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
           &VirtualBaseOffsets) override;
 
-  void SetExternalSourceCallbacks(
-      CompleteTagDeclCallback tag_decl_callback,
-      CompleteObjCInterfaceDeclCallback objc_decl_callback,
-      FindExternalVisibleDeclsByNameCallback find_by_name_callback,
-      LayoutRecordTypeCallback layout_record_type_callback,
-      void *callback_baton) {
-    m_callback_tag_decl = tag_decl_callback;
-    m_callback_objc_decl = objc_decl_callback;
-    m_callback_find_by_name = find_by_name_callback;
-    m_callback_layout_record_type = layout_record_type_callback;
-    m_callback_baton = callback_baton;
-  }
-
-  void RemoveExternalSourceCallbacks(void *callback_baton) {
-    if (callback_baton == m_callback_baton) {
-      m_callback_tag_decl = nullptr;
-      m_callback_objc_decl = nullptr;
-      m_callback_find_by_name = nullptr;
-      m_callback_layout_record_type = nullptr;
-    }
-  }
-
-protected:
-  // Classes that inherit from ClangExternalASTSourceCallbacks can see and
-  // modify these
-  CompleteTagDeclCallback m_callback_tag_decl;
-  CompleteObjCInterfaceDeclCallback m_callback_objc_decl;
-  FindExternalVisibleDeclsByNameCallback m_callback_find_by_name;
-  LayoutRecordTypeCallback m_callback_layout_record_type;
-  void *m_callback_baton;
+private:
+  ClangASTContext &m_ast;
 };
 
 } // namespace lldb_private

diff  --git a/lldb/source/Symbol/ClangASTContext.cpp b/lldb/source/Symbol/ClangASTContext.cpp
index ae5fe561528b..c7ab37998a98 100644
--- a/lldb/source/Symbol/ClangASTContext.cpp
+++ b/lldb/source/Symbol/ClangASTContext.cpp
@@ -729,10 +729,7 @@ void ClangASTContext::CreateASTContext() {
   GetASTMap().Insert(m_ast_up.get(), this);
 
   llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_up(
-      new ClangExternalASTSourceCallbacks(
-          ClangASTContext::CompleteTagDecl,
-          ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
-          ClangASTContext::LayoutRecordType, this));
+      new ClangExternalASTSourceCallbacks(*this));
   SetExternalSource(ast_source_up);
 }
 
@@ -8929,9 +8926,8 @@ clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
   return nullptr;
 }
 
-void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
-  ClangASTContext *ast = (ClangASTContext *)baton;
-  SymbolFile *sym_file = ast->GetSymbolFile();
+void ClangASTContext::CompleteTagDecl(clang::TagDecl *decl) {
+  SymbolFile *sym_file = GetSymbolFile();
   if (sym_file) {
     CompilerType clang_type = GetTypeForDecl(decl);
     if (clang_type)
@@ -8940,9 +8936,8 @@ void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
 }
 
 void ClangASTContext::CompleteObjCInterfaceDecl(
-    void *baton, clang::ObjCInterfaceDecl *decl) {
-  ClangASTContext *ast = (ClangASTContext *)baton;
-  SymbolFile *sym_file = ast->GetSymbolFile();
+    clang::ObjCInterfaceDecl *decl) {
+  SymbolFile *sym_file = GetSymbolFile();
   if (sym_file) {
     CompilerType clang_type = GetTypeForDecl(decl);
     if (clang_type)
@@ -8963,19 +8958,18 @@ PDBASTParser *ClangASTContext::GetPDBParser() {
 }
 
 bool ClangASTContext::LayoutRecordType(
-    void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
+    const clang::RecordDecl *record_decl, uint64_t &bit_size,
     uint64_t &alignment,
     llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
     llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
         &base_offsets,
     llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
         &vbase_offsets) {
-  ClangASTContext *ast = (ClangASTContext *)baton;
   lldb_private::ClangASTImporter *importer = nullptr;
-  if (ast->m_dwarf_ast_parser_up)
-    importer = &ast->m_dwarf_ast_parser_up->GetClangASTImporter();
-  if (!importer && ast->m_pdb_ast_parser_up)
-    importer = &ast->m_pdb_ast_parser_up->GetClangASTImporter();
+  if (m_dwarf_ast_parser_up)
+    importer = &m_dwarf_ast_parser_up->GetClangASTImporter();
+  if (!importer && m_pdb_ast_parser_up)
+    importer = &m_pdb_ast_parser_up->GetClangASTImporter();
   if (!importer)
     return false;
 

diff  --git a/lldb/source/Symbol/ClangExternalASTSourceCallbacks.cpp b/lldb/source/Symbol/ClangExternalASTSourceCallbacks.cpp
index c35fc585bfa5..008c2acd9b48 100644
--- a/lldb/source/Symbol/ClangExternalASTSourceCallbacks.cpp
+++ b/lldb/source/Symbol/ClangExternalASTSourceCallbacks.cpp
@@ -7,69 +7,19 @@
 //===----------------------------------------------------------------------===//
 
 #include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
+#include "lldb/Symbol/ClangASTContext.h"
 
-
-// Clang headers like to use NDEBUG inside of them to enable/disable debug
-// related features using "#ifndef NDEBUG" preprocessor blocks to do one thing
-// or another. This is bad because it means that if clang was built in release
-// mode, it assumes that you are building in release mode which is not always
-// the case. You can end up with functions that are defined as empty in header
-// files when NDEBUG is not defined, and this can cause link errors with the
-// clang .a files that you have since you might be missing functions in the .a
-// file. So we have to define NDEBUG when including clang headers to avoid any
-// mismatches. This is covered by rdar://problem/8691220
-
-#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
-#define LLDB_DEFINED_NDEBUG_FOR_CLANG
-#define NDEBUG
-// Need to include assert.h so it is as clang would expect it to be (disabled)
-#include <assert.h>
-#endif
-
-#include "clang/AST/DeclBase.h"
-#include "clang/AST/DeclarationName.h"
-
-#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
-#undef NDEBUG
-#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
-// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
-#include <assert.h>
-#endif
-
-#include "lldb/Utility/Log.h"
 #include "clang/AST/Decl.h"
 
-using namespace clang;
 using namespace lldb_private;
 
-bool ClangExternalASTSourceCallbacks::FindExternalVisibleDeclsByName(
-    const clang::DeclContext *decl_ctx,
-    clang::DeclarationName clang_decl_name) {
-  if (m_callback_find_by_name) {
-    llvm::SmallVector<clang::NamedDecl *, 3> results;
-
-    m_callback_find_by_name(m_callback_baton, decl_ctx, clang_decl_name,
-                            &results);
-
-    SetExternalVisibleDeclsForName(decl_ctx, clang_decl_name, results);
-
-    return (results.size() != 0);
-  }
-
-  std::string decl_name(clang_decl_name.getAsString());
-  SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
-  return false;
-}
-
-void ClangExternalASTSourceCallbacks::CompleteType(TagDecl *tag_decl) {
-  if (m_callback_tag_decl)
-    m_callback_tag_decl(m_callback_baton, tag_decl);
+void ClangExternalASTSourceCallbacks::CompleteType(clang::TagDecl *tag_decl) {
+  m_ast.CompleteTagDecl(tag_decl);
 }
 
 void ClangExternalASTSourceCallbacks::CompleteType(
-    ObjCInterfaceDecl *objc_decl) {
-  if (m_callback_objc_decl)
-    m_callback_objc_decl(m_callback_baton, objc_decl);
+    clang::ObjCInterfaceDecl *objc_decl) {
+  m_ast.CompleteObjCInterfaceDecl(objc_decl);
 }
 
 bool ClangExternalASTSourceCallbacks::layoutRecordType(
@@ -78,19 +28,15 @@ bool ClangExternalASTSourceCallbacks::layoutRecordType(
     llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &BaseOffsets,
     llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
         &VirtualBaseOffsets) {
-  if (m_callback_layout_record_type)
-    return m_callback_layout_record_type(m_callback_baton, Record, Size,
-                                         Alignment, FieldOffsets, BaseOffsets,
-                                         VirtualBaseOffsets);
-
-  return false;
+  return m_ast.LayoutRecordType(Record, Size, Alignment, FieldOffsets,
+                                BaseOffsets, VirtualBaseOffsets);
 }
 
 void ClangExternalASTSourceCallbacks::FindExternalLexicalDecls(
     const clang::DeclContext *decl_ctx,
     llvm::function_ref<bool(clang::Decl::Kind)> IsKindWeWant,
     llvm::SmallVectorImpl<clang::Decl *> &decls) {
-  if (m_callback_tag_decl && decl_ctx) {
+  if (decl_ctx) {
     clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(
         const_cast<clang::DeclContext *>(decl_ctx));
     if (tag_decl)


        


More information about the lldb-commits mailing list