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

Conversation

Michael137
Copy link
Member

@Michael137 Michael137 commented Jan 24, 2025

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 ParmVarDecls. This is how Clang determines whether a parameter isExplicitObjectParamater. The LLDB expression evaluator relies on this for calling "explicit object member functions".

@Michael137 Michael137 requested a review from balazske January 24, 2025 16:34
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Jan 24, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 24, 2025

@llvm/pr-subscribers-clang

Author: Michael Buch (Michael137)

Changes

Adds test that confirms that we import the explicit object parameter location for ParmVarDecls. This is how Clang determines whether a parameter isExplicitObjectParamater. The LLDB expression evaluator relies on this for calling "explicit object member functions".


Full diff: https://github.com/llvm/llvm-project/pull/124305.diff

1 Files Affected:

  • (modified) clang/unittests/AST/ASTImporterTest.cpp (+20)
diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index 791248e7a394f1..187fe651f6f19a 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -3441,6 +3441,7 @@ 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);
@@ -3448,6 +3449,25 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportParmVarDecl) {
   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 {
+      Wrapper(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_EQ(ToVar->getExplicitObjectParamThisLoc(),
+            FromVar->getExplicitObjectParamThisLoc());
 }
 
 TEST_P(ASTImporterOptionSpecificTestBase, ImportOfNonEquivalentField) {

…ameter

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".
@Michael137 Michael137 force-pushed the clang/astimporter-explicit-object-param branch from fed71d2 to 2fbabc9 Compare January 24, 2025 16:36
EXPECT_TRUE(ToVar);
EXPECT_TRUE(ToVar->isExplicitObjectParameter());
EXPECT_EQ(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.

@llvmbot llvmbot added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Jan 27, 2025
@Michael137 Michael137 changed the title [clang][ASTImporter][test] Add test for importing explicit object parameter [clang][ASTImporter] Import source location of explicit object parameter instead of copying it Jan 27, 2025
@Michael137 Michael137 merged commit 5c5bbff into llvm:main Jan 27, 2025
8 checks passed
@Michael137 Michael137 deleted the clang/astimporter-explicit-object-param branch January 27, 2025 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants