Skip to content

Commit 5c5bbff

Browse files
authored
[clang][ASTImporter] Import source location of explicit object parameter instead of copying it (#124305)
We used to copy the `SourceLocation` instead of importing it, which isn't correct since the `SourceManager`'s of the source and target ASTContext might differ. Also adds test that confirms that we import the explicit object parameter location for `ParmVarDecl`s. This is how Clang determines whether a parameter `isExplicitObjectParamater`. The LLDB expression evaluator relies on this for calling "explicit object member functions".
1 parent 081723b commit 5c5bbff

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

clang/lib/AST/ASTImporter.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4701,9 +4701,13 @@ ExpectedDecl ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
47014701

47024702
Error ASTNodeImporter::ImportDefaultArgOfParmVarDecl(
47034703
const ParmVarDecl *FromParam, ParmVarDecl *ToParam) {
4704+
4705+
if (auto LocOrErr = import(FromParam->getExplicitObjectParamThisLoc()))
4706+
ToParam->setExplicitObjectParameterLoc(*LocOrErr);
4707+
else
4708+
return LocOrErr.takeError();
4709+
47044710
ToParam->setHasInheritedDefaultArg(FromParam->hasInheritedDefaultArg());
4705-
ToParam->setExplicitObjectParameterLoc(
4706-
FromParam->getExplicitObjectParamThisLoc());
47074711
ToParam->setKNRPromoted(FromParam->isKNRPromoted());
47084712

47094713
if (FromParam->hasUninstantiatedDefaultArg()) {

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3441,13 +3441,33 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportParmVarDecl) {
34413441
ASSERT_TRUE(FromVar);
34423442
ASSERT_TRUE(FromVar->hasUninstantiatedDefaultArg());
34433443
ASSERT_TRUE(FromVar->getUninstantiatedDefaultArg());
3444+
ASSERT_FALSE(FromVar->isExplicitObjectParameter());
34443445

34453446
const auto *ToVar = Import(FromVar, Lang_CXX11);
34463447
EXPECT_TRUE(ToVar);
34473448
EXPECT_TRUE(ToVar->hasUninstantiatedDefaultArg());
34483449
EXPECT_TRUE(ToVar->getUninstantiatedDefaultArg());
34493450
EXPECT_NE(FromVar->getUninstantiatedDefaultArg(),
34503451
ToVar->getUninstantiatedDefaultArg());
3452+
EXPECT_FALSE(ToVar->isExplicitObjectParameter());
3453+
}
3454+
3455+
TEST_P(ASTImporterOptionSpecificTestBase, ImportParmVarDecl_Explicit) {
3456+
const auto *Code = R"(
3457+
struct Wrapper {
3458+
void func(this Wrapper) {}
3459+
};
3460+
)";
3461+
Decl *FromTU = getTuDecl(Code, Lang_CXX23);
3462+
auto *FromVar = FirstDeclMatcher<ParmVarDecl>().match(FromTU, parmVarDecl());
3463+
ASSERT_TRUE(FromVar);
3464+
ASSERT_TRUE(FromVar->isExplicitObjectParameter());
3465+
3466+
const auto *ToVar = Import(FromVar, Lang_CXX23);
3467+
EXPECT_TRUE(ToVar);
3468+
EXPECT_TRUE(ToVar->isExplicitObjectParameter());
3469+
EXPECT_NE(ToVar->getExplicitObjectParamThisLoc(),
3470+
FromVar->getExplicitObjectParamThisLoc());
34513471
}
34523472

34533473
TEST_P(ASTImporterOptionSpecificTestBase, ImportOfNonEquivalentField) {

0 commit comments

Comments
 (0)