Skip to content

[clang][ASTImporter] Import source location of explicit object parameter instead of copying it #124305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions clang/lib/AST/ASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4701,9 +4701,13 @@ ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {

Error ASTNodeImporter::ImportDefaultArgOfParmVarDecl(
const ParmVarDecl *FromParam, ParmVarDecl *ToParam) {

if (auto LocOrErr = import(FromParam->getExplicitObjectParamThisLoc()))
ToParam->setExplicitObjectParameterLoc(*LocOrErr);
else
return LocOrErr.takeError();

ToParam->setHasInheritedDefaultArg(FromParam->hasInheritedDefaultArg());
ToParam->setExplicitObjectParameterLoc(
FromParam->getExplicitObjectParamThisLoc());
ToParam->setKNRPromoted(FromParam->isKNRPromoted());

if (FromParam->hasUninstantiatedDefaultArg()) {
Expand Down
20 changes: 20 additions & 0 deletions clang/unittests/AST/ASTImporterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3441,13 +3441,33 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportParmVarDecl) {
ASSERT_TRUE(FromVar);
ASSERT_TRUE(FromVar->hasUninstantiatedDefaultArg());
ASSERT_TRUE(FromVar->getUninstantiatedDefaultArg());
ASSERT_FALSE(FromVar->isExplicitObjectParameter());

const auto *ToVar = Import(FromVar, Lang_CXX11);
EXPECT_TRUE(ToVar);
EXPECT_TRUE(ToVar->hasUninstantiatedDefaultArg());
EXPECT_TRUE(ToVar->getUninstantiatedDefaultArg());
EXPECT_NE(FromVar->getUninstantiatedDefaultArg(),
ToVar->getUninstantiatedDefaultArg());
EXPECT_FALSE(ToVar->isExplicitObjectParameter());
}

TEST_P(ASTImporterOptionSpecificTestBase, ImportParmVarDecl_Explicit) {
const auto *Code = R"(
struct Wrapper {
void func(this Wrapper) {}
};
)";
Decl *FromTU = getTuDecl(Code, Lang_CXX23);
auto *FromVar = FirstDeclMatcher<ParmVarDecl>().match(FromTU, parmVarDecl());
ASSERT_TRUE(FromVar);
ASSERT_TRUE(FromVar->isExplicitObjectParameter());

const auto *ToVar = Import(FromVar, Lang_CXX23);
EXPECT_TRUE(ToVar);
EXPECT_TRUE(ToVar->isExplicitObjectParameter());
EXPECT_NE(ToVar->getExplicitObjectParamThisLoc(),
FromVar->getExplicitObjectParamThisLoc());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks not correct to copy a SourceLocation from the "from" into the "to" context. They have a different source manager instance. Function ASTNodeImporter::ImportDefaultArgOfParmVarDecl is the place where this is done. Copy of getExplicitObjectParamThisLoc should be replaced with normal import.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea good catch, i was wondering about that when reading it initially. I'll adjust it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest commit updates code to import the location.

}

TEST_P(ASTImporterOptionSpecificTestBase, ImportOfNonEquivalentField) {
Expand Down
Loading