From 5b99aa57f74e1432691f366d26a2ec21907a7a70 Mon Sep 17 00:00:00 2001 From: dingfei Date: Tue, 18 Jul 2023 16:29:56 +0800 Subject: [PATCH] [ASTImporter] Fix 'isVirtual()' assert failure while import overridden methods MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit CXXMethodDecl::isVirtual() count the number of overridden methods. This assertion is not true before overridden methods are fully loaded. The body of this CXXMethodDecl can introduce deps on a derived class which contains a method overriding this method, causing the assertion failure. ImportOverriddenMethods() is moved before body loading to fix this issue. Testcase is contributed by Balázs Kéri (balazske) Differential Revision: https://reviews.llvm.org/D154701 --- clang/lib/AST/ASTImporter.cpp | 10 ++++----- clang/unittests/AST/ASTImporterTest.cpp | 37 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp index 1f4e2b2..429fa58 100644 --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -3782,6 +3782,11 @@ ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { if (Error Err = ImportTemplateInformation(D, ToFunction)) return std::move(Err); + if (auto *FromCXXMethod = dyn_cast(D)) + if (Error Err = ImportOverriddenMethods(cast(ToFunction), + FromCXXMethod)) + return std::move(Err); + if (D->doesThisDeclarationHaveABody()) { Error Err = ImportFunctionDeclBody(D, ToFunction); @@ -3805,11 +3810,6 @@ ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { addDeclToContexts(D, ToFunction); - if (auto *FromCXXMethod = dyn_cast(D)) - if (Error Err = ImportOverriddenMethods(cast(ToFunction), - FromCXXMethod)) - return std::move(Err); - // Import the rest of the chain. I.e. import all subsequent declarations. for (++RedeclIt; RedeclIt != Redecls.end(); ++RedeclIt) { ExpectedDecl ToRedeclOrErr = import(*RedeclIt); diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp index ecf51d1..91b1b86 100644 --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -2332,6 +2332,43 @@ TEST_P(ImportFunctions, EXPECT_EQ(ToDFOutOfClass->getPreviousDecl(), ToDFInClass); } +TEST_P(ASTImporterOptionSpecificTestBase, + ImportVirtualOverriddenMethodOnALoopTest) { + // B::f() calls => f1() ==> C ==> C::f() + // \ + // \---- A::f() + // + // C::f()'s ImportOverriddenMethods() asserts B::isVirtual(), so B::f()'s + // ImportOverriddenMethods() should be completed before B::f()'s body + const char *Code = + R"( + void f1(); + class A { + virtual void f(){} + }; + class B: public A { + void f() override { + f1(); + } + }; + class C: public B { + void f() override {} + }; + void f1() { C c; } + )"; + Decl *FromTU = getTuDecl(Code, Lang_CXX11); + + auto *FromF = FirstDeclMatcher().match( + FromTU, cxxMethodDecl(hasName("B::f"))); + + auto *ToBF = Import(FromF, Lang_CXX11); + EXPECT_TRUE(ToBF->isVirtual()); + + auto *ToCF = FirstDeclMatcher().match( + ToBF->getTranslationUnitDecl(), cxxMethodDecl(hasName("C::f"))); + EXPECT_TRUE(ToCF->isVirtual()); +} + TEST_P(ASTImporterOptionSpecificTestBase, ImportVariableChainInC) { std::string Code = "static int v; static int v = 0;"; auto Pattern = varDecl(hasName("v")); -- 2.7.4