[Lldb-commits] [lldb] r366325 - [ASTImporter] Fix LLDB lookup in transparent ctx and with ext src

2019-10-04 Thread Gabor Marton via lldb-commits
Author: martong
Date: Wed Jul 17 06:47:46 2019
New Revision: 366325

URL: http://llvm.org/viewvc/llvm-project?rev=366325&view=rev
Log:
[ASTImporter] Fix LLDB lookup in transparent ctx and with ext src

Summary:
With LLDB we use localUncachedLookup(), however, that fails to find
Decls when a transparent context is involved and the given DC has
external lexical storage.  The solution is to use noload_lookup, which
works well with transparent contexts.  But, we cannot use only the
noload_lookup since the slow case of localUncachedLookup is still needed
in some other cases.

These other cases are handled in ASTImporterLookupTable, but we cannot
use that with LLDB since that traverses through the AST which initiates
the load of external decls again via DC::decls().

We must avoid loading external decls during the import becuase
ExternalASTSource is implemented with ASTImporter, so external loads
during import results in uncontrolled and faulty import.

Reviewers: shafik, teemperor, jingham, clayborg, a_sidorin, a.sidorin

Subscribers: rnkovacs, dkrupp, Szelethus, gamesh411, cfe-commits, lldb-commits

Tags: #clang, #lldb

Differential Revision: https://reviews.llvm.org/D61333

Modified:
lldb/trunk/packages/Python/lldbsuite/test/lang/c/modules/TestCModules.py
lldb/trunk/packages/Python/lldbsuite/test/lang/c/modules/main.c
lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp

Modified: 
lldb/trunk/packages/Python/lldbsuite/test/lang/c/modules/TestCModules.py
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/lang/c/modules/TestCModules.py?rev=366325&r1=366324&r2=366325&view=diff
==
--- lldb/trunk/packages/Python/lldbsuite/test/lang/c/modules/TestCModules.py 
(original)
+++ lldb/trunk/packages/Python/lldbsuite/test/lang/c/modules/TestCModules.py 
Wed Jul 17 06:47:46 2019
@@ -47,6 +47,10 @@ class CModulesTestCase(TestBase):
 self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
 substrs=[' resolved, hit count = 1'])
 
+# Enable logging of the imported AST.
+log_file = os.path.join(self.getBuildDir(), "lldb-ast-log.txt")
+self.runCmd("log enable lldb ast -f '%s'" % log_file)
+
 self.expect(
 "expr -l objc++ -- @import Darwin; 3",
 VARIABLES_DISPLAYED_CORRECTLY,
@@ -54,6 +58,8 @@ class CModulesTestCase(TestBase):
 "int",
 "3"])
 
+# This expr command imports __sFILE with definition
+# (FILE is a typedef to __sFILE.)
 self.expect(
 "expr *fopen(\"/dev/zero\", \"w\")",
 VARIABLES_DISPLAYED_CORRECTLY,
@@ -61,6 +67,14 @@ class CModulesTestCase(TestBase):
 "FILE",
 "_close"])
 
+# Check that the AST log contains exactly one definition of __sFILE.
+f = open(log_file)
+log_lines = f.readlines()
+f.close()
+os.remove(log_file)
+self.assertEqual(" ".join(log_lines).count("struct __sFILE 
definition"),
+ 1)
+
 self.expect("expr *myFile", VARIABLES_DISPLAYED_CORRECTLY,
 substrs=["a", "5", "b", "9"])
 

Modified: lldb/trunk/packages/Python/lldbsuite/test/lang/c/modules/main.c
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/lang/c/modules/main.c?rev=366325&r1=366324&r2=366325&view=diff
==
--- lldb/trunk/packages/Python/lldbsuite/test/lang/c/modules/main.c (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/lang/c/modules/main.c Wed Jul 17 
06:47:46 2019
@@ -5,11 +5,11 @@ int printf(const char * __restrict forma
 typedef struct {
 int a;
 int b;
-} FILE;
+} MYFILE;
 
 int main()
 {
-FILE *myFile = malloc(sizeof(FILE));
+MYFILE *myFile = malloc(sizeof(MYFILE));
 
 myFile->a = 5;
 myFile->b = 9;

Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp?rev=366325&r1=366324&r2=366325&view=diff
==
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp 
(original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp Wed Jul 
17 06:47:46 2019
@@ -612,10 +612,15 @@ void ClangASTSource::FindExternalLexical
   if (!original_decl_context)
 return;
 
+  // Indicates whether we skipped any Decls of the original DeclContext.
+  bool SkippedDecls = false;
   for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
iter != original_decl_context->decls_end(); ++iter) {
 Decl *decl = *iter;
 
+// The predicate function returns true if the passed declaration kind is
+// the one we are looking for.
+// See

[Lldb-commits] [lldb] r361362 - Add AST logging

2019-10-04 Thread Gabor Marton via lldb-commits
Author: martong
Date: Wed May 22 02:10:19 2019
New Revision: 361362

URL: http://llvm.org/viewvc/llvm-project?rev=361362&view=rev
Log:
Add AST logging

Summary:
Log the AST of the TU associated with LLDB's `expr` command, once a declaration
is completed

Reviewers: shafik

Subscribers: rnkovacs, dkrupp, Szelethus, gamesh411, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D62061

Modified:
lldb/trunk/include/lldb/Utility/Logging.h
lldb/trunk/source/Symbol/ClangASTImporter.cpp
lldb/trunk/source/Utility/Logging.cpp

Modified: lldb/trunk/include/lldb/Utility/Logging.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/Logging.h?rev=361362&r1=361361&r2=361362&view=diff
==
--- lldb/trunk/include/lldb/Utility/Logging.h (original)
+++ lldb/trunk/include/lldb/Utility/Logging.h Wed May 22 02:10:19 2019
@@ -42,6 +42,7 @@
 #define LIBLLDB_LOG_LANGUAGE (1u << 28)
 #define LIBLLDB_LOG_DATAFORMATTERS (1u << 29)
 #define LIBLLDB_LOG_DEMANGLE (1u << 30)
+#define LIBLLDB_LOG_AST (1u << 31)
 #define LIBLLDB_LOG_ALL (UINT32_MAX)
 #define LIBLLDB_LOG_DEFAULT
\
   (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD | LIBLLDB_LOG_DYNAMIC_LOADER | 
\

Modified: lldb/trunk/source/Symbol/ClangASTImporter.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Symbol/ClangASTImporter.cpp?rev=361362&r1=361361&r2=361362&view=diff
==
--- lldb/trunk/source/Symbol/ClangASTImporter.cpp (original)
+++ lldb/trunk/source/Symbol/ClangASTImporter.cpp Wed May 22 02:10:19 2019
@@ -951,6 +951,28 @@ void ClangASTImporter::ASTImporterDelega
   if (clang::TagDecl *to_tag = dyn_cast(to)) {
 if (clang::TagDecl *from_tag = dyn_cast(from)) {
   to_tag->setCompleteDefinition(from_tag->isCompleteDefinition());
+
+  if (Log *log_ast =
+  lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_AST)) {
+std::string name_string;
+if (NamedDecl *from_named_decl = dyn_cast(from)) {
+  llvm::raw_string_ostream name_stream(name_string);
+  from_named_decl->printName(name_stream);
+  name_stream.flush();
+}
+LLDB_LOG(log_ast, " [ClangASTImporter][TUDecl: {0}] Imported "
+  "({1}Decl*){2}, named {3} (from "
+  "(Decl*){4})",
+ static_cast(to->getTranslationUnitDecl()),
+ from->getDeclKindName(), static_cast(to), name_string,
+ static_cast(from));
+
+// Log the AST of the TU.
+std::string ast_string;
+llvm::raw_string_ostream ast_stream(ast_string);
+to->getTranslationUnitDecl()->dump(ast_stream);
+LLDB_LOG(log_ast, "{0}", ast_string);
+  }
 }
   }
 

Modified: lldb/trunk/source/Utility/Logging.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/Logging.cpp?rev=361362&r1=361361&r2=361362&view=diff
==
--- lldb/trunk/source/Utility/Logging.cpp (original)
+++ lldb/trunk/source/Utility/Logging.cpp Wed May 22 02:10:19 2019
@@ -17,6 +17,7 @@ using namespace lldb_private;
 
 static constexpr Log::Category g_categories[] = {
   {{"api"}, {"log API calls and return values"}, LIBLLDB_LOG_API},
+  {{"ast"}, {"log AST"}, LIBLLDB_LOG_AST},
   {{"break"}, {"log breakpoints"}, LIBLLDB_LOG_BREAKPOINTS},
   {{"commands"}, {"log command argument parsing"}, LIBLLDB_LOG_COMMANDS},
   {{"comm"}, {"log communication activities"}, LIBLLDB_LOG_COMMUNICATION},


___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] r370045 - [ASTImporter] Fix name conflict handling with different strategies

2019-10-04 Thread Gabor Marton via lldb-commits
Author: martong
Date: Tue Aug 27 04:36:10 2019
New Revision: 370045

URL: http://llvm.org/viewvc/llvm-project?rev=370045&view=rev
Log:
[ASTImporter] Fix name conflict handling with different strategies

There are numorous flaws about the name conflict handling, this patch
attempts fixes them. Changes in details:

* HandleNameConflict return with a false DeclarationName

Hitherto we effectively never returned with a NameConflict error, even
if the preceding StructuralMatch indicated a conflict.
Because we just simply returned with the parameter `Name` in
HandleNameConflict and that name is almost always `true` when converted to
`bool`.

* Add tests which indicate wrong NameConflict handling

* Add to ConflictingDecls only if decl kind is different

Note, we might not indicate an ODR error when there is an existing record decl
and a enum is imported with same name.  But there are other cases. E.g. think
about the case when we import a FunctionTemplateDecl with name f and we found a
simple FunctionDecl with name f. They overload.  Or in case of a
ClassTemplateDecl and CXXRecordDecl, the CXXRecordDecl could be the 'templated'
class, so it would be false to report error.  So I think we should report a
name conflict error only when we are 100% sure of that.  That is why I think it
should be a general pattern to report the error only if the kind is the same.

* Fix failing ctu test with EnumConstandDecl

In ctu-main.c we have the enum class 'A' which brings in the enum
constant 'x' with value 0 into the global namespace.
In ctu-other.c we had the enum class 'B' which brought in the same name
('x') as an enum constant but with a different enum value (42). This is clearly
an ODR violation in the global namespace. The solution was to rename the
second enum constant.

 * Introduce ODR handling strategies

Reviewers: a_sidorin, shafik

Differential Revision: https://reviews.llvm.org/D59692

Modified:
lldb/trunk/include/lldb/Symbol/ClangASTImporter.h

Modified: lldb/trunk/include/lldb/Symbol/ClangASTImporter.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Symbol/ClangASTImporter.h?rev=370045&r1=370044&r2=370045&view=diff
==
--- lldb/trunk/include/lldb/Symbol/ClangASTImporter.h (original)
+++ lldb/trunk/include/lldb/Symbol/ClangASTImporter.h Tue Aug 27 04:36:10 2019
@@ -252,7 +252,9 @@ private:
 : clang::ASTImporter(*target_ctx, master.m_file_manager, *source_ctx,
  master.m_file_manager, true /*minimal*/),
   m_decls_to_deport(nullptr), m_decls_already_deported(nullptr),
-  m_master(master), m_source_ctx(source_ctx) {}
+  m_master(master), m_source_ctx(source_ctx) {
+  setODRHandling(clang::ASTImporter::ODRHandlingType::Liberal);
+}
 
 /// Scope guard that attaches a CxxModuleHandler to an ASTImporterDelegate
 /// and deattaches it at the end of the scope. Supports being used multiple


___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] r360760 - [ASTImporter] Use llvm::Expected and Error in the importer API

2019-10-04 Thread Gabor Marton via lldb-commits
Author: martong
Date: Wed May 15 03:29:48 2019
New Revision: 360760

URL: http://llvm.org/viewvc/llvm-project?rev=360760&view=rev
Log:
[ASTImporter] Use llvm::Expected and Error in the importer API

Summary:
This is the final phase of the refactoring towards using llvm::Expected
and llvm::Error in the ASTImporter API.
This involves the following:
- remove old Import functions which returned with a pointer,
- use the Import_New functions (which return with Err or Expected) everywhere
  and handle their return value
- rename Import_New functions to Import
This affects both Clang and LLDB.

Reviewers: shafik, teemperor, aprantl, a_sidorin, balazske, a.sidorin

Subscribers: rnkovacs, dkrupp, Szelethus, gamesh411, cfe-commits, lldb-commits

Tags: #clang, #lldb

Differential Revision: https://reviews.llvm.org/D61438

Modified:
lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp
lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
lldb/trunk/source/Symbol/ClangASTContext.cpp
lldb/trunk/source/Symbol/ClangASTImporter.cpp
lldb/trunk/source/Symbol/CxxModuleHandler.cpp

Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp?rev=360760&r1=360759&r2=360760&view=diff
==
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp 
(original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp Wed May 
15 03:29:48 2019
@@ -1968,7 +1968,14 @@ clang::QualType ClangASTSource::CopyType
 return QualType();
   }
 
-  return merger.ImporterForOrigin(from_context).Import(type);
+  if (llvm::Expected type_or_error =
+  merger.ImporterForOrigin(from_context).Import(type)) {
+return *type_or_error;
+  } else {
+Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
+LLDB_LOG_ERROR(log, type_or_error.takeError(), "Couldn't import type: 
{0}");
+return QualType();
+  }
 }
 
 clang::Decl *ClangASTSource::CopyDecl(Decl *src_decl) {
@@ -1981,7 +1988,16 @@ clang::Decl *ClangASTSource::CopyDecl(De
   return nullptr;
 }
 
-return m_merger_up->ImporterForOrigin(from_context).Import(src_decl);
+if (llvm::Expected decl_or_error =
+m_merger_up->ImporterForOrigin(from_context).Import(src_decl)) {
+  return *decl_or_error;
+} else {
+  Log *log =
+  lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
+  LLDB_LOG_ERROR(log, decl_or_error.takeError(),
+ "Couldn't import decl: {0}");
+  return nullptr;
+}
   } else {
 lldbassert(0 && "No mechanism for copying a decl!");
 return nullptr;

Modified: 
lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp?rev=360760&r1=360759&r2=360760&view=diff
==
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp 
(original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp 
Wed May 15 03:29:48 2019
@@ -272,9 +272,15 @@ static clang::QualType ExportAllDeclared
   merger.AddSources(importer_source);
   clang::ASTImporter &exporter = merger.ImporterForOrigin(source);
   CompleteAllDeclContexts(exporter, file, root);
-  clang::QualType ret = exporter.Import(root);
+  llvm::Expected ret_or_error = exporter.Import(root);
   merger.RemoveSources(importer_source);
-  return ret;
+  if (ret_or_error) {
+return *ret_or_error;
+  } else {
+Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
+LLDB_LOG_ERROR(log, ret_or_error.takeError(), "Couldn't import type: {0}");
+return clang::QualType();
+  }
 }
 
 TypeFromUser ClangExpressionDeclMap::DeportType(ClangASTContext &target,

Modified: lldb/trunk/source/Symbol/ClangASTContext.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Symbol/ClangASTContext.cpp?rev=360760&r1=360759&r2=360760&view=diff
==
--- lldb/trunk/source/Symbol/ClangASTContext.cpp (original)
+++ lldb/trunk/source/Symbol/ClangASTContext.cpp Wed May 15 03:29:48 2019
@@ -1415,7 +1415,14 @@ clang::Decl *ClangASTContext::CopyDecl(A
   FileManager file_manager(file_system_options);
   ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
 
-  return importer.Import(source_decl);
+  if (llvm::Expected ret_or_error =
+  importer.Import(source_decl)) {
+return *ret_or_error;
+  } else {
+Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
+LLDB_LOG_ERROR(log, ret_or_error.takeError(), "Couldn't import decl: {0}");
+r

[Lldb-commits] [lldb] r347575 - [ASTImporter] Set MustBuildLookupTable on PrimaryContext

2019-10-04 Thread Gabor Marton via lldb-commits
Author: martong
Date: Mon Nov 26 09:09:50 2018
New Revision: 347575

URL: http://llvm.org/viewvc/llvm-project?rev=347575&view=rev
Log:
[ASTImporter] Set MustBuildLookupTable on PrimaryContext

Summary: SetMustBuildLookupTable() must always be called on a primary context.

Reviewers: labath, shafik, a.sidorin

Subscribers: rnkovacs, dkrupp, Szelethus, gamesh411

Differential Revision: https://reviews.llvm.org/D54863

Modified:
lldb/trunk/source/Symbol/ClangASTImporter.cpp

Modified: lldb/trunk/source/Symbol/ClangASTImporter.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Symbol/ClangASTImporter.cpp?rev=347575&r1=347574&r2=347575&view=diff
==
--- lldb/trunk/source/Symbol/ClangASTImporter.cpp (original)
+++ lldb/trunk/source/Symbol/ClangASTImporter.cpp Mon Nov 26 09:09:50 2018
@@ -1050,7 +1050,7 @@ clang::Decl *ClangASTImporter::Minion::I
 TagDecl *to_tag_decl = dyn_cast(to);
 
 to_tag_decl->setHasExternalLexicalStorage();
-to_tag_decl->setMustBuildLookupTable();
+to_tag_decl->getPrimaryContext()->setMustBuildLookupTable();
 
 if (log)
   log->Printf(


___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits