aprantl created this revision.
aprantl added reviewers: labath, clayborg, jasonmolenda, JDevlieghere.

This patch is also motivated by the Swift branch and is effectively NFC for the 
single-TypeSystem llvm.org branch.

In multi-language projects it is extremely common to have, e.g., a Clang type 
and a similarly-named rendition of that same type in another language. When 
searching for a type It is much cheaper to pass a set of supported languages to 
the SymbolFile than having it materialize every result and then rejecting the 
materialized types that have the wrong language.


https://reviews.llvm.org/D66546

Files:
  lldb/include/lldb/Symbol/ClangASTContext.h
  lldb/include/lldb/Symbol/SymbolFile.h
  lldb/include/lldb/Symbol/TypeSystem.h
  lldb/lit/SymbolFile/DWARF/compilercontext.ll
  lldb/source/Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.cpp
  lldb/source/Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.h
  lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
  lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
  lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h
  lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp
  lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h
  lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
  lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h
  lldb/source/Symbol/ClangASTContext.cpp
  lldb/source/Symbol/SymbolFile.cpp
  lldb/tools/lldb-test/lldb-test.cpp

Index: lldb/tools/lldb-test/lldb-test.cpp
===================================================================
--- lldb/tools/lldb-test/lldb-test.cpp
+++ lldb/tools/lldb-test/lldb-test.cpp
@@ -26,6 +26,7 @@
 #include "lldb/Symbol/TypeList.h"
 #include "lldb/Symbol/TypeMap.h"
 #include "lldb/Symbol/VariableList.h"
+#include "lldb/Target/Language.h"
 #include "lldb/Target/Process.h"
 #include "lldb/Target/Target.h"
 #include "lldb/Utility/CleanUp.h"
@@ -144,6 +145,10 @@
     cl::desc("Specify a compiler context as \"kind:name,...\"."),
     cl::value_desc("context"), cl::sub(SymbolsSubcommand));
 
+static cl::opt<std::string>
+    Language("language", cl::desc("Specify a DWARF language like C99."),
+             cl::value_desc("language"), cl::sub(SymbolsSubcommand));
+
 static cl::list<FunctionNameType> FunctionNameFlags(
     "function-flags", cl::desc("Function search flags:"),
     cl::values(clEnumValN(eFunctionNameTypeAuto, "auto",
@@ -507,13 +512,17 @@
   CompilerDeclContext *ContextPtr =
       ContextOr->IsValid() ? &*ContextOr : nullptr;
 
+  SmallBitVector languages(eNumLanguageTypes);
+  if (!Language.empty())
+    languages.set(Language::GetLanguageTypeFromString(Language));
+  
   DenseSet<SymbolFile *> SearchedFiles;
   TypeMap Map;
   if (!Name.empty())
     Symfile.FindTypes(ConstString(Name), ContextPtr, true, UINT32_MAX,
                       SearchedFiles, Map);
   else
-    Symfile.FindTypes({parseCompilerContext()}, true, Map);
+    Symfile.FindTypes({parseCompilerContext()}, languages, true, Map);
 
   outs() << formatv("Found {0} types:\n", Map.GetSize());
   StreamString Stream;
Index: lldb/source/Symbol/SymbolFile.cpp
===================================================================
--- lldb/source/Symbol/SymbolFile.cpp
+++ lldb/source/Symbol/SymbolFile.cpp
@@ -150,7 +150,8 @@
 }
 
 size_t SymbolFile::FindTypes(llvm::ArrayRef<CompilerContext> pattern,
-                             bool append, TypeMap &types) {
+                             llvm::SmallBitVector languages, bool append,
+                             TypeMap &types) {
   if (!append)
     types.Clear();
   return 0;
Index: lldb/source/Symbol/ClangASTContext.cpp
===================================================================
--- lldb/source/Symbol/ClangASTContext.cpp
+++ lldb/source/Symbol/ClangASTContext.cpp
@@ -798,6 +798,28 @@
   m_pointer_byte_size = 0;
 }
 
+llvm::SmallBitVector ClangASTContext::GetSupportedLanguages() const {
+  llvm::SmallBitVector languages(64-8, 0);
+  static_assert(eNumLanguageTypes < 64-8,
+                "Languages bit vector is no longer small on 64 bit systems");
+  uint64_t mask =
+    1 << eLanguageTypeC89 |
+    1 << eLanguageTypeC |
+    1 << eLanguageTypeC_plus_plus |
+    1 << eLanguageTypeC99 |
+    1 << eLanguageTypeObjC |
+    1 << eLanguageTypeObjC_plus_plus |
+    1 << eLanguageTypeUPC |
+    1 << eLanguageTypeC_plus_plus_03 |
+    1 << eLanguageTypeC_plus_plus_11 |
+    1 << eLanguageTypeC11 |
+    1 << eLanguageTypeC_plus_plus_14;
+  uint32_t mask32[2];
+  memcpy(mask32, &mask, 8);
+  languages.setBitsInMask(mask32, 7);
+  return languages;
+}
+
 void ClangASTContext::setSema(Sema *s) {
   // Ensure that the new sema actually belongs to our ASTContext.
   assert(s == nullptr || &s->getASTContext() == m_ast_up.get());
Index: lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h
===================================================================
--- lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h
+++ lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h
@@ -133,7 +133,8 @@
             lldb_private::TypeMap &types) override;
 
   size_t FindTypes(llvm::ArrayRef<lldb_private::CompilerContext> pattern,
-                   bool append, lldb_private::TypeMap &types) override;
+                   llvm::SmallBitVector languages, bool append,
+                   lldb_private::TypeMap &types) override;
 
   void FindTypesByRegex(const lldb_private::RegularExpression &regex,
                         uint32_t max_matches, lldb_private::TypeMap &types);
Index: lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
===================================================================
--- lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
+++ lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
@@ -1585,7 +1585,8 @@
 }
 
 size_t SymbolFilePDB::FindTypes(llvm::ArrayRef<CompilerContext> pattern,
-                                bool append, lldb_private::TypeMap &types) {
+                                llvm::SmallBitVector languages, bool append,
+                                lldb_private::TypeMap &types) {
   return 0;
 }
 
Index: lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h
===================================================================
--- lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h
+++ lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h
@@ -134,7 +134,8 @@
                      llvm::DenseSet<SymbolFile *> &searched_symbol_files,
                      TypeMap &types) override;
 
-  size_t FindTypes(llvm::ArrayRef<CompilerContext> pattern, bool append,
+  size_t FindTypes(llvm::ArrayRef<CompilerContext> pattern,
+                   llvm::SmallBitVector languages, bool append,
                    TypeMap &types) override;
 
   llvm::Expected<TypeSystem &>
Index: lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp
===================================================================
--- lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp
+++ lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp
@@ -1270,6 +1270,7 @@
 }
 
 size_t SymbolFileNativePDB::FindTypes(llvm::ArrayRef<CompilerContext> pattern,
+                                      llvm::SmallBitVector languages,
                                       bool append, TypeMap &types) {
   return 0;
 }
Index: lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h
+++ lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h
@@ -185,7 +185,8 @@
             lldb_private::TypeMap &types) override;
 
   size_t FindTypes(llvm::ArrayRef<lldb_private::CompilerContext> pattern,
-                   bool append, lldb_private::TypeMap &types) override;
+                   llvm::SmallBitVector languages, bool append,
+                   lldb_private::TypeMap &types) override;
 
   size_t GetTypes(lldb_private::SymbolContextScope *sc_scope,
                   lldb::TypeClass type_mask,
Index: lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
+++ lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
@@ -2485,7 +2485,8 @@
 }
 
 size_t SymbolFileDWARF::FindTypes(llvm::ArrayRef<CompilerContext> pattern,
-                                  bool append, TypeMap &types) {
+                                  llvm::SmallBitVector languages, bool append,
+                                  TypeMap &types) {
   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
   if (!append)
     types.Clear();
@@ -2508,6 +2509,9 @@
     DWARFDIE die = GetDIE(die_ref);
 
     if (die) {
+      if (!languages[die.GetCU()->GetLanguageType()])
+        continue;
+
       llvm::SmallVector<CompilerContext, 4> die_context;
       die.GetDeclContext(die_context);
       if (!contextMatches(die_context, pattern))
Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
+++ lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
@@ -148,7 +148,10 @@
   die.GetDeclContext(decl_context);
   TypeMap dwo_types;
 
-  if (!dwo_module_sp->GetSymbolFile()->FindTypes({decl_context}, true,
+  // The type in the Clang module must have the same langage as the current CU.
+  llvm::SmallBitVector languages(eNumLanguageTypes);
+  languages.set(die.GetCU()->GetLanguageType());
+  if (!dwo_module_sp->GetSymbolFile()->FindTypes(decl_context, languages, true,
                                                  dwo_types)) {
     if (!IsClangModuleFwdDecl(die))
       return TypeSP();
@@ -159,8 +162,8 @@
     for (const auto &name_module : sym_file.getExternalTypeModules()) {
       if (!name_module.second)
         continue;
-      if (name_module.second->GetSymbolFile()->FindTypes({decl_context}, true,
-                                                         dwo_types))
+      if (name_module.second->GetSymbolFile()->FindTypes(
+              decl_context, languages, true, dwo_types))
         break;
     }
   }
Index: lldb/source/Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.h
===================================================================
--- lldb/source/Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.h
+++ lldb/source/Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.h
@@ -116,7 +116,8 @@
                      llvm::DenseSet<SymbolFile *> &searched_symbol_files,
                      TypeMap &types) override;
 
-  size_t FindTypes(llvm::ArrayRef<CompilerContext> pattern, bool append,
+  size_t FindTypes(llvm::ArrayRef<CompilerContext> pattern,
+                   llvm::SmallBitVector languages, bool append,
                    TypeMap &types) override;
 
   llvm::Expected<TypeSystem &>
Index: lldb/source/Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.cpp
===================================================================
--- lldb/source/Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.cpp
+++ lldb/source/Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.cpp
@@ -319,6 +319,7 @@
 }
 
 size_t SymbolFileBreakpad::FindTypes(llvm::ArrayRef<CompilerContext> pattern,
+                                     llvm::SmallBitVector languages,
                                      bool append, TypeMap &types) {
   if (!append)
     types.Clear();
Index: lldb/lit/SymbolFile/DWARF/compilercontext.ll
===================================================================
--- lldb/lit/SymbolFile/DWARF/compilercontext.ll
+++ lldb/lit/SymbolFile/DWARF/compilercontext.ll
@@ -1,18 +1,21 @@
 ; Test finding types by CompilerContext.
 ; RUN: llc %s -filetype=obj -o %t.o
-; RUN: lldb-test symbols %t.o -find=type \
+; RUN: lldb-test symbols %t.o -find=type --language=C99 \
 ; RUN:   -compiler-context="Module:CModule,Module:SubModule,Struct:FromSubmoduleX" \
 ; RUN:   | FileCheck %s --check-prefix=NORESULTS
-; RUN: lldb-test symbols %t.o -find=type \
+; RUN: lldb-test symbols %t.o -find=type --language=C++ \
+; RUN:   -compiler-context="Module:CModule,Module:SubModule,Struct:FromSubmodule" \
+; RUN:   | FileCheck %s --check-prefix=NORESULTS
+; RUN: lldb-test symbols %t.o -find=type --language=C99 \
 ; RUN:   -compiler-context="Module:CModule,Module:SubModule,Struct:FromSubmodule" \
 ; RUN:   | FileCheck %s
-; RUN: lldb-test symbols %t.o -find=type \
+; RUN: lldb-test symbols %t.o -find=type --language=C99 \
 ; RUN:   -compiler-context="Module:CModule,AnyModule:*,Struct:FromSubmodule" \
 ; RUN:   | FileCheck %s
-; RUN: lldb-test symbols %t.o -find=type \
+; RUN: lldb-test symbols %t.o -find=type --language=C99 \
 ; RUN:   -compiler-context="AnyModule:*,Struct:FromSubmodule" \
 ; RUN:   | FileCheck %s
-; RUN: lldb-test symbols %t.o -find=type \
+; RUN: lldb-test symbols %t.o -find=type --language=C99 \
 ; RUN:   -compiler-context="Module:CModule,Module:SubModule,AnyType:FromSubmodule" \
 ; RUN:   | FileCheck %s
 ;
Index: lldb/include/lldb/Symbol/TypeSystem.h
===================================================================
--- lldb/include/lldb/Symbol/TypeSystem.h
+++ lldb/include/lldb/Symbol/TypeSystem.h
@@ -15,6 +15,7 @@
 #include <string>
 
 #include "llvm/ADT/APSInt.h"
+#include "llvm/ADT/SmallBitVector.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/Error.h"
 
@@ -30,7 +31,7 @@
 
 namespace lldb_private {
 
-// Interface for representing the Type Systems in different languages.
+/// Interface for representing the Type Systems in different languages.
 class TypeSystem : public PluginInterface {
 public:
   // Intrusive type system that allows us to use llvm casting.
@@ -86,6 +87,12 @@
   // removing all the TypeSystems from the TypeSystemMap.
   virtual void Finalize() {}
 
+  /// Returns a bitvector of \p LanguageType entries this type system
+  /// supports. Each lldb::LanguageType is represented by the bit with
+  /// its number. The largest LanguageType is < 64, so this is very
+  /// space-efficient.
+  virtual llvm::SmallBitVector GetSupportedLanguages() const { return {}; }
+
   virtual DWARFASTParser *GetDWARFParser() { return nullptr; }
   virtual PDBASTParser *GetPDBParser() { return nullptr; }
 
Index: lldb/include/lldb/Symbol/SymbolFile.h
===================================================================
--- lldb/include/lldb/Symbol/SymbolFile.h
+++ lldb/include/lldb/Symbol/SymbolFile.h
@@ -20,6 +20,7 @@
 #include "lldb/lldb-private.h"
 
 #include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/SmallBitVector.h"
 
 #include <mutex>
 
@@ -189,7 +190,11 @@
             bool append, uint32_t max_matches,
             llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
             TypeMap &types);
-  virtual size_t FindTypes(llvm::ArrayRef<CompilerContext> pattern, bool append,
+
+  /// Find types specified by a CompilerContextPattern.
+  /// \param languages    Only return results in these languages.
+  virtual size_t FindTypes(llvm::ArrayRef<CompilerContext> pattern,
+                           llvm::SmallBitVector languages, bool append,
                            TypeMap &types);
 
   virtual void
Index: lldb/include/lldb/Symbol/ClangASTContext.h
===================================================================
--- lldb/include/lldb/Symbol/ClangASTContext.h
+++ lldb/include/lldb/Symbol/ClangASTContext.h
@@ -58,6 +58,8 @@
 
   void Finalize() override;
 
+  llvm::SmallBitVector GetSupportedLanguages() const override;
+
   // PluginInterface functions
   ConstString GetPluginName() override;
 
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to