This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Fix type printing in the presence of qualifiers
ClosedPublic

Authored by adamcz on Jan 7 2021, 12:51 PM.

Details

Summary

When printing QualType with qualifiers like "const", or pointing to an
elaborated type, we would print garbage like:

std::const std::vector<int>&

with the initial std:: being calculated correctly, but inserted in the
wrong place and the second std:: not removed (due to elaborated type).

This affected, among others, ExtractFunction and ExpandAuto tweaks.

This change introduces a new callback to PrintingPolicy, which allows us
to influence the printing of namespace qualifiers. In the future, the
same callback can be used to improve handling of "using namespace"
directives as well.

Fixes:

https://github.com/clangd/clangd/issues/640 (ExtractFunction)
https://github.com/clangd/clangd/issues/264 (ExpandAuto)
First point of https://github.com/clangd/clangd/issues/524

Diff Detail

Event Timeline

adamcz created this revision.Jan 7 2021, 12:51 PM
adamcz requested review of this revision.Jan 7 2021, 12:51 PM
kadircet added inline comments.Jan 8 2021, 5:07 AM
clang/include/clang/AST/PrettyPrinter.h
49

oh wow, i didn't know about these callbacks at all.

this looks pretty need, but i wonder if we should make it more generic like "shouldPrintScope" (or isScopeVisible) and pass not only namespacedecls but the declcontext directly to enable short circuiting of type scopes as well?

also it might be nice to note in comments that any parent scope won't be visited once this callback returns true (or false in the case of shouldPrintScope).

adamcz updated this revision to Diff 315363.Jan 8 2021, 5:46 AM
adamcz marked an inline comment as done.

Addressed review comment

kadircet accepted this revision.Jan 8 2021, 7:18 AM

Thanks, LGTM!

clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp
68

can you also test for shortening? e.g.

void ns::Func() { aut^o x = new ns::Class::Nested{}; }
This revision is now accepted and ready to land.Jan 8 2021, 7:18 AM
adamcz updated this revision to Diff 315398.Jan 8 2021, 7:42 AM
adamcz marked an inline comment as done.

final review comments

kadircet added inline comments.Jan 8 2021, 7:44 AM
clang/include/clang/AST/PrettyPrinter.h
51

nit: s/NS/DC/

adamcz updated this revision to Diff 315401.Jan 8 2021, 7:52 AM

s/NS/DC

adamcz marked an inline comment as done.Jan 8 2021, 7:52 AM
This revision was landed with ongoing or failed builds.Jan 8 2021, 8:06 AM
This revision was automatically updated to reflect the committed changes.