This is an archive of the discontinued LLVM Phabricator instance.

[AST] TypeSourceInfo for params of inherited constructor should be null
AcceptedPublic

Authored by sammccall on Aug 24 2023, 12:07 PM.

Details

Summary

This matches behavior of other synthetic members like copy constructors,
and avoids RecursiveASTVisitor traversing TypeLocs that don't correspond
to any spelling of the type they describe.

Diff Detail

Event Timeline

sammccall created this revision.Aug 24 2023, 12:07 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 24 2023, 12:07 PM
sammccall requested review of this revision.Aug 24 2023, 12:07 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 24 2023, 12:07 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
ymandel accepted this revision.Aug 24 2023, 12:28 PM
ymandel added inline comments.
clang/lib/Sema/SemaDeclCXX.cpp
13895–13896

I'm now suspicious of all TypeLocs in this code, and If I'm reading this code correctly, this typeloc is never actually used.

13908

Once you're at it, should this use actually be nullptr?

13929

nit: label with /* TypeSourceInfo=*/?

clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
2142–2144

why phrase it bottom up, instead of top-down (matching the record decl and then using hasDescendant)?

This revision is now accepted and ready to land.Aug 24 2023, 12:28 PM
sammccall marked 4 inline comments as done.
sammccall added a subscriber: aaron.ballman.

+Aaron for guidance & in case I'm making things up, TypeLocs/TSI have always confused me...

clang/lib/Sema/SemaDeclCXX.cpp
13895–13896

My understanding is TypeLoc is really just a Type* + handle into some storage, TypeSourceInfo is typically that storage.
So TInfo is storage to hold the location info for a function type, initialized to ~empty ("trivial").
Then ProtoLoc is an accessor that we use to mutate that storage (ProtoLoc.setParam below).
Meanwhile we pass TInfo in as the TypeSourceInfo for the function.

The net effect: if we ask for the function's typeloc, and then ask that loc for the param decls, we'll get them, just the same as if we ask the function for its param decls directly.
That said, see next comment.

13908

... maybe? For implicit copy-constructors, the TSI is nullptr (SemaDeclCXX.cpp:15425).
That suggests that being able to get at the params via the TypeLoc isn't actually a critical invariant.

I've done this as I agree TypeSourceInfo for implicit code is dubious, and I was able to construct a similar ASTMatcher misbehavior that this fixes.

@aaron.ballman does this seem right?

sammccall updated this revision to Diff 553234.Aug 24 2023, 1:08 PM
sammccall marked 2 inline comments as done.

address review comments, also null the TSI of the constructor itself

aaron.ballman added inline comments.Aug 25 2023, 6:09 AM
clang/lib/Sema/SemaDeclCXX.cpp
13908

Errr... somewhere in the middle between right and wrong. I can see why TypeSourceInfo for implicit code is dubious, but we've had a theoretical long-standing goal that we've never seriously worked towards of replacing QualType/Type * with TypeSourceInfo * in many of our interfaces so that location information about types is more readily available.

On balance, I think passing nullptr here is reasonable -- this is an implicit node and we do this for other implicitly created special member functions.

13929