<div dir="ltr">We're seeing some odd test failures internally caused by this patch. For whatever reason (test ordering based on hashing, I guess), we're running the tests in a different order than upstream, and TestClangASTContext crashes when TestClangASTImporter runs first.<div><br></div><div>By default, it seems like TestClangASTContext happens to run first so the failure isn't usually seen, but the failure can be reproduced with --gtest_repeat=2</div><div>$ ninja SymbolTests && tools/lldb/unittests/Symbol/SymbolTests '--gtest_filter=TestClangAST*' --gtest_repeat=2</div><div>Repeating all tests (iteration 1) . . .<br><br>Note: Google Test filter = TestClangAST*<br></div><div>[[ TestClangASTContext passes ]]</div><div>[[ TestClangASTImporter passes ]]</div><div><br></div><div>Repeating all tests (iteration 2) . . .<br><br>Note: Google Test filter = TestClangAST*<br>[==========] Running 21 tests from 2 test cases.<br>[----------] Global test environment set-up.<br>[----------] 13 tests from TestClangASTContext<br>[ RUN      ] TestClangASTContext.TestGetBasicTypeFromEnum<br>SymbolTests: /src/llvm-project/llvm/../clang/include/clang/AST/Type.h:669: const clang::ExtQualsTypeCommonBase *clang::QualType::getCommonPtr() const: Assertion `!isNull() && "Cannot retrieve a NULL type pointer"' failed.<br> #0 0x000000000215e5a7 llvm::sys::PrintStackTrace(llvm::raw_ostream&) /src/llvm-project/llvm/lib/Support/Unix/Signals.inc:548:11<br> #1 0x000000000215e749 PrintStackTraceSignalHandler(void*) /src/llvm-project/llvm/lib/Support/Unix/Signals.inc:609:1<br> #2 0x000000000215d02b llvm::sys::RunSignalHandlers() /src/llvm-project/llvm/lib/Support/Signals.cpp:67:5<br> #3 0x000000000215eec5 SignalHandler(int) /src/llvm-project/llvm/lib/Support/Unix/Signals.inc:390:1<br> #4 0x00007f819b4523a0 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x123a0)<br> #5 0x00007f819a3decfb gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x36cfb)<br> #6 0x00007f819a3c98ad abort (/lib/x86_64-linux-gnu/libc.so.6+0x218ad)<br> #7 0x00007f819a3c977f __tls_get_addr (/lib/x86_64-linux-gnu/libc.so.6+0x2177f)<br> #8 0x00007f819a3d7542 (/lib/x86_64-linux-gnu/libc.so.6+0x2f542)<br> #9 0x00000000020531d7 clang::QualType::getCommonPtr() const /src/llvm-project/llvm/../clang/include/clang/AST/Type.h:0:5<br>#10 0x00000000020529cc clang::QualType::getCanonicalType() const /src/llvm-project/llvm/../clang/include/clang/AST/Type.h:6231:20<br>#11 0x0000000002052879 clang::ASTContext::getCanonicalType(clang::QualType) const /src/llvm-project/llvm/../clang/include/clang/AST/ASTContext.h:2296:40<br>#12 0x0000000002050960 clang::ASTContext::hasSameType(clang::QualType, clang::QualType) const /src/llvm-project/llvm/../clang/include/clang/AST/ASTContext.h:2312:12<br>#13 0x0000000002047365 TestClangASTContext_TestGetBasicTypeFromEnum_Test::TestBody() /src/llvm-project/lldb/unittests/Symbol/TestClangASTContext.cpp:57:3<br></div><div><...></div><div><br></div><div>Does the failure make sense to you?</div><div>No need to revert the patch -- we already have the test disabled internally, though we would like to re-enable it</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Dec 16, 2019 at 3:44 AM Raphael Isemann via lldb-commits <<a href="mailto:lldb-commits@lists.llvm.org">lldb-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
Author: Raphael Isemann<br>
Date: 2019-12-16T12:43:55+01:00<br>
New Revision: 22caa3cfbcf5762a47acc40c425d9fe0c40da621<br>
<br>
URL: <a href="https://github.com/llvm/llvm-project/commit/22caa3cfbcf5762a47acc40c425d9fe0c40da621" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/22caa3cfbcf5762a47acc40c425d9fe0c40da621</a><br>
DIFF: <a href="https://github.com/llvm/llvm-project/commit/22caa3cfbcf5762a47acc40c425d9fe0c40da621.diff" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/22caa3cfbcf5762a47acc40c425d9fe0c40da621.diff</a><br>
<br>
LOG: [lldb] Add unit test for ClangASTImporter<br>
<br>
Added: <br>
    lldb/unittests/Symbol/TestClangASTImporter.cpp<br>
<br>
Modified: <br>
    lldb/unittests/Symbol/CMakeLists.txt<br>
<br>
Removed: <br>
<br>
<br>
<br>
################################################################################<br>
diff  --git a/lldb/unittests/Symbol/CMakeLists.txt b/lldb/unittests/Symbol/CMakeLists.txt<br>
index aa86986f4e0e..02875b8b53c1 100644<br>
--- a/lldb/unittests/Symbol/CMakeLists.txt<br>
+++ b/lldb/unittests/Symbol/CMakeLists.txt<br>
@@ -2,6 +2,7 @@ add_lldb_unittest(SymbolTests<br>
   LocateSymbolFileTest.cpp<br>
   PostfixExpressionTest.cpp<br>
   TestClangASTContext.cpp<br>
+  TestClangASTImporter.cpp<br>
   TestDWARFCallFrameInfo.cpp<br>
   TestType.cpp<br>
   TestLineEntry.cpp<br>
<br>
diff  --git a/lldb/unittests/Symbol/TestClangASTImporter.cpp b/lldb/unittests/Symbol/TestClangASTImporter.cpp<br>
new file mode 100644<br>
index 000000000000..17a0dfb6a348<br>
--- /dev/null<br>
+++ b/lldb/unittests/Symbol/TestClangASTImporter.cpp<br>
@@ -0,0 +1,220 @@<br>
+//===-- TestClangASTImporter.cpp --------------------------------*- C++ -*-===//<br>
+//<br>
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.<br>
+// See <a href="https://llvm.org/LICENSE.txt" rel="noreferrer" target="_blank">https://llvm.org/LICENSE.txt</a> for license information.<br>
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "gtest/gtest.h"<br>
+<br>
+#include "lldb/Host/FileSystem.h"<br>
+#include "lldb/Host/HostInfo.h"<br>
+#include "lldb/Symbol/ClangASTContext.h"<br>
+#include "lldb/Symbol/ClangASTImporter.h"<br>
+#include "lldb/Symbol/ClangASTMetadata.h"<br>
+#include "lldb/Symbol/ClangUtil.h"<br>
+#include "lldb/Symbol/Declaration.h"<br>
+#include "clang/AST/DeclCXX.h"<br>
+<br>
+using namespace clang;<br>
+using namespace lldb;<br>
+using namespace lldb_private;<br>
+<br>
+class TestClangASTImporter : public testing::Test {<br>
+public:<br>
+  static void SetUpTestCase() {<br>
+    FileSystem::Initialize();<br>
+    HostInfo::Initialize();<br>
+  }<br>
+<br>
+  static void TearDownTestCase() {<br>
+    HostInfo::Terminate();<br>
+    FileSystem::Terminate();<br>
+  }<br>
+<br>
+protected:<br>
+  std::unique_ptr<ClangASTContext> createAST() {<br>
+    return std::make_unique<ClangASTContext>(HostInfo::GetTargetTriple());<br>
+  }<br>
+<br>
+  CompilerType createRecord(ClangASTContext &ast, const char *name) {<br>
+    return ast.CreateRecordType(ast.getASTContext()->getTranslationUnitDecl(),<br>
+                                lldb::AccessType::eAccessPublic, name, 0,<br>
+                                lldb::LanguageType::eLanguageTypeC);<br>
+  }<br>
+};<br>
+<br>
+TEST_F(TestClangASTImporter, CanImportInvalidType) {<br>
+  ClangASTImporter importer;<br>
+  EXPECT_FALSE(importer.CanImport(CompilerType()));<br>
+}<br>
+<br>
+TEST_F(TestClangASTImporter, ImportInvalidType) {<br>
+  ClangASTImporter importer;<br>
+  EXPECT_FALSE(importer.Import(CompilerType()));<br>
+}<br>
+<br>
+TEST_F(TestClangASTImporter, CopyDeclTagDecl) {<br>
+  // Tests that the ClangASTImporter::CopyDecl can copy TagDecls.<br>
+  std::unique_ptr<ClangASTContext> source_ast = createAST();<br>
+  CompilerType source_type = createRecord(*source_ast, "Source");<br>
+  clang::TagDecl *source = ClangUtil::GetAsTagDecl(source_type);<br>
+<br>
+  std::unique_ptr<ClangASTContext> target_ast = createAST();<br>
+<br>
+  ClangASTImporter importer;<br>
+  clang::Decl *imported = importer.CopyDecl(<br>
+      target_ast->getASTContext(), source_ast->getASTContext(), source);<br>
+  ASSERT_NE(nullptr, imported);<br>
+<br>
+  // Check that we got the correct decl by just comparing their qualified name.<br>
+  clang::TagDecl *imported_tag_decl = llvm::cast<clang::TagDecl>(imported);<br>
+  EXPECT_EQ(source->getQualifiedNameAsString(),<br>
+            imported_tag_decl->getQualifiedNameAsString());<br>
+<br>
+  // Check that origin was set for the imported declaration.<br>
+  ClangASTImporter::DeclOrigin origin = importer.GetDeclOrigin(imported);<br>
+  EXPECT_TRUE(origin.Valid());<br>
+  EXPECT_EQ(origin.ctx, source_ast->getASTContext());<br>
+  EXPECT_EQ(origin.decl, source);<br>
+}<br>
+<br>
+TEST_F(TestClangASTImporter, CopyTypeTagDecl) {<br>
+  // Tests that the ClangASTImporter::CopyType can copy TagDecls types.<br>
+  std::unique_ptr<ClangASTContext> source_ast = createAST();<br>
+  CompilerType source_type = createRecord(*source_ast, "Source");<br>
+  clang::TagDecl *source = ClangUtil::GetAsTagDecl(source_type);<br>
+<br>
+  std::unique_ptr<ClangASTContext> target_ast = createAST();<br>
+<br>
+  ClangASTImporter importer;<br>
+  CompilerType imported = importer.CopyType(*target_ast, source_type);<br>
+  ASSERT_TRUE(imported.IsValid());<br>
+<br>
+  // Check that we got the correct decl by just comparing their qualified name.<br>
+  clang::TagDecl *imported_tag_decl = ClangUtil::GetAsTagDecl(imported);<br>
+  EXPECT_EQ(source->getQualifiedNameAsString(),<br>
+            imported_tag_decl->getQualifiedNameAsString());<br>
+<br>
+  // Check that origin was set for the imported declaration.<br>
+  ClangASTImporter::DeclOrigin origin =<br>
+      importer.GetDeclOrigin(imported_tag_decl);<br>
+  EXPECT_TRUE(origin.Valid());<br>
+  EXPECT_EQ(origin.ctx, source_ast->getASTContext());<br>
+  EXPECT_EQ(origin.decl, source);<br>
+}<br>
+<br>
+TEST_F(TestClangASTImporter, MetadataPropagation) {<br>
+  // Tests that AST metadata is propagated when copying declarations.<br>
+<br>
+  std::unique_ptr<ClangASTContext> source_ast = createAST();<br>
+  CompilerType source_type = createRecord(*source_ast, "Source");<br>
+  clang::TagDecl *source = ClangUtil::GetAsTagDecl(source_type);<br>
+  const lldb::user_id_t metadata = 123456;<br>
+  source_ast->SetMetadataAsUserID(source, metadata);<br>
+<br>
+  std::unique_ptr<ClangASTContext> target_ast = createAST();<br>
+<br>
+  ClangASTImporter importer;<br>
+  clang::Decl *imported = importer.CopyDecl(<br>
+      target_ast->getASTContext(), source_ast->getASTContext(), source);<br>
+  ASSERT_NE(nullptr, imported);<br>
+<br>
+  // Check that we got the same Metadata.<br>
+  ASSERT_NE(nullptr, importer.GetDeclMetadata(imported));<br>
+  EXPECT_EQ(metadata, importer.GetDeclMetadata(imported)->GetUserID());<br>
+}<br>
+<br>
+TEST_F(TestClangASTImporter, MetadataPropagationIndirectImport) {<br>
+  // Tests that AST metadata is propagated when copying declarations when<br>
+  // importing one declaration into a temporary context and then to the<br>
+  // actual destination context.<br>
+<br>
+  std::unique_ptr<ClangASTContext> source_ast = createAST();<br>
+  CompilerType source_type = createRecord(*source_ast, "Source");<br>
+  clang::TagDecl *source = ClangUtil::GetAsTagDecl(source_type);<br>
+  const lldb::user_id_t metadata = 123456;<br>
+  source_ast->SetMetadataAsUserID(source, metadata);<br>
+<br>
+  std::unique_ptr<ClangASTContext> temporary_ast = createAST();<br>
+<br>
+  ClangASTImporter importer;<br>
+  clang::Decl *temporary_imported = importer.CopyDecl(<br>
+      temporary_ast->getASTContext(), source_ast->getASTContext(), source);<br>
+  ASSERT_NE(nullptr, temporary_imported);<br>
+<br>
+  std::unique_ptr<ClangASTContext> target_ast = createAST();<br>
+  clang::Decl *imported =<br>
+      importer.CopyDecl(target_ast->getASTContext(),<br>
+                        temporary_ast->getASTContext(), temporary_imported);<br>
+  ASSERT_NE(nullptr, imported);<br>
+<br>
+  // Check that we got the same Metadata.<br>
+  ASSERT_NE(nullptr, importer.GetDeclMetadata(imported));<br>
+  EXPECT_EQ(metadata, importer.GetDeclMetadata(imported)->GetUserID());<br>
+}<br>
+<br>
+TEST_F(TestClangASTImporter, MetadataPropagationAfterCopying) {<br>
+  // Tests that AST metadata is propagated when copying declarations even<br>
+  // when the metadata was set after the declaration has already been copied.<br>
+<br>
+  std::unique_ptr<ClangASTContext> source_ast = createAST();<br>
+  CompilerType source_type = createRecord(*source_ast, "Source");<br>
+  clang::TagDecl *source = ClangUtil::GetAsTagDecl(source_type);<br>
+  const lldb::user_id_t metadata = 123456;<br>
+<br>
+  std::unique_ptr<ClangASTContext> target_ast = createAST();<br>
+<br>
+  ClangASTImporter importer;<br>
+  clang::Decl *imported = importer.CopyDecl(<br>
+      target_ast->getASTContext(), source_ast->getASTContext(), source);<br>
+  ASSERT_NE(nullptr, imported);<br>
+<br>
+  // The TagDecl has been imported. Now set the metadata of the source and<br>
+  // make sure the imported one will directly see it.<br>
+  source_ast->SetMetadataAsUserID(source, metadata);<br>
+<br>
+  // Check that we got the same Metadata.<br>
+  ASSERT_NE(nullptr, importer.GetDeclMetadata(imported));<br>
+  EXPECT_EQ(metadata, importer.GetDeclMetadata(imported)->GetUserID());<br>
+}<br>
+<br>
+TEST_F(TestClangASTImporter, RecordLayout) {<br>
+  // Test that it is possible to register RecordDecl layouts and then later<br>
+  // correctly retrieve them.<br>
+<br>
+  std::unique_ptr<ClangASTContext> source_ast = createAST();<br>
+  CompilerType source_type = createRecord(*source_ast, "Source");<br>
+  ClangASTContext::StartTagDeclarationDefinition(source_type);<br>
+  clang::FieldDecl *field = source_ast->AddFieldToRecordType(<br>
+      source_type, "a_field",<br>
+      source_ast->GetBasicType(lldb::BasicType::eBasicTypeChar),<br>
+      lldb::AccessType::eAccessPublic, 7);<br>
+  ClangASTContext::CompleteTagDeclarationDefinition(source_type);<br>
+<br>
+  clang::TagDecl *source_tag = ClangUtil::GetAsTagDecl(source_type);<br>
+  clang::RecordDecl *source_record = llvm::cast<clang::RecordDecl>(source_tag);<br>
+<br>
+  ClangASTImporter importer;<br>
+  ClangASTImporter::LayoutInfo layout_info;<br>
+  layout_info.bit_size = 15;<br>
+  layout_info.alignment = 2;<br>
+  layout_info.field_offsets[field] = 1;<br>
+  importer.InsertRecordDecl(source_record, layout_info);<br>
+<br>
+  uint64_t bit_size;<br>
+  uint64_t alignment;<br>
+  llvm::DenseMap<const clang::FieldDecl *, uint64_t> field_offsets;<br>
+  llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> base_offsets;<br>
+  llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> vbase_offsets;<br>
+  importer.LayoutRecordType(source_record, bit_size, alignment, field_offsets,<br>
+                            base_offsets, vbase_offsets);<br>
+<br>
+  EXPECT_EQ(15U, bit_size);<br>
+  EXPECT_EQ(2U, alignment);<br>
+  EXPECT_EQ(1U, field_offsets.size());<br>
+  EXPECT_EQ(1U, field_offsets[field]);<br>
+  EXPECT_EQ(0U, base_offsets.size());<br>
+  EXPECT_EQ(0U, vbase_offsets.size());<br>
+}<br>
<br>
<br>
<br>
_______________________________________________<br>
lldb-commits mailing list<br>
<a href="mailto:lldb-commits@lists.llvm.org" target="_blank">lldb-commits@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits</a><br>
</blockquote></div>