Closed marxin closed 3 months ago
Looks like in https://github.com/llvm/llvm-project/pull/91393, getTypeAsWritten
was replaced with getTemplateArgsAsWritten
.
IWYU also tripped over this.
Yep, it was replaced, but I don't see how can we get to the location of the type name as getTemplateArgsAsWritten
returns something that describes the template arguments. Please let me know once you have a hint, thanks.
cc @sdkrystian
On Tue, May 28, 2024 at 4:32 AM Martin Liška @.***> wrote:
Yep, it was replaced, but I don't see how can we get to the location of the type name as getTemplateArgsAsWritten returns something that describes the template arguments. Please let me know once you have a hint, thanks.
— Reply to this email directly, view it on GitHub https://github.com/marxin/cvise/issues/142#issuecomment-2134991422, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAN5IX5XNEHTJARL5ID5BVTZERTLDAVCNFSM6AAAAABH5JPUMOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMZUHE4TCNBSGI . You are receiving this because you were mentioned.Message ID: @.***>
Looks like (maybe)
ClassTemplateSpecializationDecl::getTemplateArgsAsWritten
returns a
const ASTTemplateArgumentListInfo*
which has a getTemplateArgs
method that returns a const TemplateArgumentLoc *
which has a getTypeSourceInfo
method that finally returns a
TypeSourceInfo *
. Or are you referring to the return type of a function?
On Tue, May 28, 2024 at 8:21 PM Nick Desaulniers @.***> wrote:
cc @sdkrystian
On Tue, May 28, 2024 at 4:32 AM Martin Liška @.***> wrote:
Yep, it was replaced, but I don't see how can we get to the location of the type name as getTemplateArgsAsWritten returns something that describes the template arguments. Please let me know once you have a hint, thanks.
— Reply to this email directly, view it on GitHub https://github.com/marxin/cvise/issues/142#issuecomment-2134991422, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAN5IX5XNEHTJARL5ID5BVTZERTLDAVCNFSM6AAAAABH5JPUMOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMZUHE4TCNBSGI . You are receiving this because you were mentioned.Message ID: @.***>
@marxin @nickdesaulniers the TemplateSpecializationTypeLoc
returned by getTypeAsWritten
stores the same SourceLocation
for its TemplateName
as that returned by ClassTemplateSpecializationDecl::getLocation
(so you should change the code to use the latter).
Also, I think the existing code contains a subtile bug: it assumes TypeLoc::getBeginLoc
returns the location of the name; for explicit instantiations it will actually return the location of the template
keyword!
Thanks for the suggestions. With the following changes applied:
diff --git a/clang_delta/CommonRenameClassRewriteVisitor.h b/clang_delta/CommonRenameClassRewriteVisitor.h
index f2daeb2..ecbe4f1 100644
--- a/clang_delta/CommonRenameClassRewriteVisitor.h
+++ b/clang_delta/CommonRenameClassRewriteVisitor.h
@@ -144,7 +144,7 @@ bool CommonRenameClassRewriteVisitor<T>::
std::string Name;
if (getNewName(CXXRD, Name)) {
- const TypeSourceInfo *TyInfo = D->getTypeAsWritten();
+ const TypeSourceInfo *TyInfo = D->getTemplateArgsAsWritten()->getTemplateArgs()->getTypeSourceInfo();
if (!TyInfo)
return true;
TypeLoc TyLoc = TyInfo->getTypeLoc();
diff --git a/clang_delta/RemoveNamespace.cpp b/clang_delta/RemoveNamespace.cpp
index b31c00e..8650c49 100644
--- a/clang_delta/RemoveNamespace.cpp
+++ b/clang_delta/RemoveNamespace.cpp
@@ -439,7 +439,7 @@ bool RemoveNamespaceRewriteVisitor::VisitClassTemplatePartialSpecializationDecl(
std::string Name;
if (ConsumerInstance->getNewName(CXXRD, Name)) {
- const TypeSourceInfo *TyInfo = D->getTypeAsWritten();
+ const TypeSourceInfo *TyInfo = D->getTemplateArgsAsWritten()->getTemplateArgs()->getTypeSourceInfo();
if (!TyInfo)
return true;
TypeLoc TyLoc = TyInfo->getTypeLoc();
I've got the following failing tests:
cls = <class 'test_clang_delta.TestClangDelta'>, testcase = 'rename-class/instantiation.cpp', arguments = '--transformation=rename-class --counter=1', output_file = 'rename-class/instantiation.output'
E AssertionError: assert 'template <typename T>\nstruct A {};\n\ntemplate struct AAA<int>;\n' == 'template <typename T>\nstruct A {};\n\ntemplate struct A<int>;\n'
E template <typename T>
E struct A {};
E
E - template struct A<int>;
E + template struct AAA<int>;
E ? ++
Here it seems the renaming happens only for the original struct, not the later one.
cls = <class 'test_clang_delta.TestClangDelta'>, testcase = 'rename-class/partial_specialization.cpp', arguments = '--transformation=rename-class --counter=1', output_file = 'rename-class/partial_specialization.output'
E AssertionError: assert 'template <typename T, int N>\nstruct A {\n T value() const { return N; }\n};\n\ntemplate <typename T>\nstruct S1 <A 3> {\n T value() const { return 0; }\n};\n\n' == 'template <typename T, int N>\nstruct A {\n T value() const { return N; }\n};\n\ntemplate <typename T>\nstruct A <T, 3> {\n T value() const { return 0; }\n};\n\n'
E template <typename T, int N>
E struct A {
E T value() const { return N; }
E };
E
E template <typename T>
E - struct A <T, 3> {
E ? ----
E + struct S1 <A 3> {
E ? ++++
E T value() const { return 0; }
E };
And here it seems the location really points to the first template argument (rather than to the S1
).
cls = <class 'test_clang_delta.TestClangDelta'>, testcase = 'rename-class/specialization.cpp', arguments = '--transformation=rename-class --counter=1', output_file = 'rename-class/specialization.output'
E AssertionError: assert 'template <typename T>\nstruct A {};\n\ntemplate<> struct AAA<int> {};\n' == 'template <typename T>\nstruct A {};\n\ntemplate<> struct A<int> {};\n'
E template <typename T>
E struct A {};
E
E - template<> struct A<int> {};
E + template<> struct AAA<int> {};
E ? ++
This one is similar to the first failing test-case.
D->getTemplateArgsAsWritten()->getTemplateArgs()->getTypeSourceInfo()
returns the TypeSourceInfo
for the first template argument if it is a type, or nullptr
otherwise (and this will crash if an empty template argument list was used). You want to use Decl::getLocation
. I opened a PR that makes the correct changes here.
@sdkrystian Thanks for the PR!
The following needs to be ported to the latest LLVM tip:
Can @nickdesaulniers or @strimo378 help me, please?