[clang][AST] Allow DeclPrinter to suppress function name scope #243057
The proposed bit affects only DeclPrinter::VisitFunctionDecl at the point where the function name is printed. It is false per default, so existing behavior does not change.
This originates from Clangd's tweak OverridePureVirtuals where the generated overrides have to be correctly qualified. I have an ongoing PR for this tweak here: #237866
For example: Generating the override of the pure virtual function bar in
``c++
namespace A {
class Foo {};
class Base {
virtual Foo bar(Foo f) = 0;
};
}
class Derived : public A::Base {};
gives currently
c++Foo bar(Foo f) { ...}
With `PrintingPolicy::FullyQualifiedName = true` one would get
c++A::Foo A::Base::bar(A::Foo f) { ...}
Both versions will not even compile. The desired output would be a mix of both: keep qualifiers for return and parameter types, and remove qualifiers for the function name:
c++A::Foo bar(A::Foo f) { ...}
`
Giving
DeclPrinter the option to suppress nested name specifiers for the function name would solve this immediately.
The alternatives I see are
a custom printer for the whole declaration in the tweak, which duplicates a lot of code from DeclPrinter` to also capture return type, parameters, specifiers, attributes, pragmas etc.
(the current state of the tweak PR), or
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Max Winkler (max-winkler)
<details>
<summary>Changes</summary>
The DeclPrinter for function declarations allows only one printing policy for function name, return type and parameter types. In some cases, e.g. when a function declaration is printed in a class body, one wants fully qualified parameter and return types but no qualifiers on the function name.
Add another bit SuppressFunctionNameScope in PrintingPolicy which is false per default and leaves existing behavior unchanged. If set, DeclPrinter prints the name of a function declaration without nested name specifiers. Return and parameter types are not affected, in contrast to SuppressScope. This gives void foo(); instead of void ns::A::foo().
The clangd tweak OverridePureVirtuals is intended to use this when generating overrides.
---
Full diff: https://github.com/llvm/llvm-project/pull/243057.diff
3 Files Affected:
````````````diff --git a/clang/include/clang/AST/PrettyPrinter.h b/clang/include/clang/AST/PrettyPrinter.h
index bb4ee585fd8379..0cdf7cbef48b5d 100644
--- a/clang/include/clang/AST/PrettyPrinter.h
+++ b/clang/include/clang/AST/PrettyPrinter.h
@@ -95,7 +95,8 @@ struct PrintingPolicy {
CleanUglifiedParameters(false), ResolveDecltype(false),
EntireContentsOfLargeArray(true), PrettyEnums(true),
UseEnumerators(true), UseHLSLTypes(LO.HLSL),
- SuppressDeclAttributes(false), SuppressLambdaBody(false) {}
+ SuppressDeclAttributes(false), SuppressLambdaBody(false),
+ SuppressFunctionNameScope(false) {}
/// Adjust this printing policy for cases where it's known that we're
/// printing C++ code (for instance, if AST dumping reaches a C++-only
@@ -388,6 +389,14 @@ struct PrintingPolicy {
LLVM_PREFERRED_TYPE(bool)
unsigned SuppressLambdaBody : 1;
+ /// Whether to suppress the nested name specifiers of a function name. Unlike
+ /// `SuppressScope`, return and parameter types are not affected, so this can
+ /// be combined with `FullyQualifiedName`. Useful if the declaration is
+ /// printed at a place where the name is declared unqualified, e.g. in a
+ /// class body.
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned SuppressFunctionNameScope : 1;
+
/// Callbacks to use to allow the behavior of printing to be customized.
const PrintingCallbacks *Callbacks = nullptr;
};
diff --git a/clang/lib/AST/DeclPrinter.cpp b/clang/lib/AST/DeclPrinter.cpp
index 77b0c017f6ceb6..e6b2c8f6c27e64 100644
--- a/clang/lib/AST/DeclPrinter.cpp
+++ b/clang/lib/AST/DeclPrinter.cpp
@@ -724,11 +724,11 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
SubPolicy.SuppressSpecifiers = false;
std::string Proto;
- if (Policy.FullyQualifiedName) {
+ if (Policy.FullyQualifiedName && !Policy.SuppressFunctionNameScope) {
Proto += D->getQualifiedNameAsString();
} else {
llvm::raw_string_ostream OS(Proto);
- if (!Policy.SuppressScope)
+ if (!Policy.SuppressScope && !Policy.SuppressFunctionNameScope)
D->getQualifier().print(OS, Policy);
D->getNameInfo().printName(OS, Policy);
}
diff --git a/clang/unittests/AST/DeclPrinterTest.cpp b/clang/unittests/AST/DeclPrinterTest.cpp
index 4368641850c20a..163c17d759603b 100644
--- a/clang/unittests/AST/DeclPrinterTest.cpp
+++ b/clang/unittests/AST/DeclPrinterTest.cpp
@@ -454,6 +454,30 @@ TEST(DeclPrinter, TestMemberFunctionOutside_FullyQualifiedName) {
[](PrintingPolicy &Policy){ Policy.FullyQualifiedName = true; }));
}
+TEST(DeclPrinter, TestMemberFunction_SuppressFunctionNameScope) {
+ ASSERT_TRUE(PrintedDeclCXX98Matches(
+ "namespace ns { struct Foo{}; struct X { void A(Foo); }; }", "A",
+ "void A(ns::Foo)", [](PrintingPolicy &Policy) {
+ Policy.FullyQualifiedName = true;
+ Policy.SuppressFunctionNameScope = true;
+ }));
+}
+
+TEST(DeclPrinter, TestMemberFunction_SuppressFunctionNameScopeWithoutFQN) {
+ ASSERT_TRUE(PrintedDeclCXX98Matches(
+ "namespace ns { struct Foo{}; struct X { void A(Foo); }; }", "A",
+ "void A(Foo)",
+ [](PrintingPolicy &Policy) { Policy.SuppressFunctionNameScope = true; }));
+}
+
+TEST(DeclPrinter, TestMemberFunctionOutside_SuppressFunctionNameScope) {
+ ASSERT_TRUE(PrintedDeclCXX98Matches(
+ "struct X { void A(); };"
+ "void X::A() {}",
+ functionDecl(hasName("A"), isDefinition()).bind("id"), "void A()",
+ [](PrintingPolicy &Policy) { Policy.SuppressFunctionNameScope = true; }));
+}
+
TEST(DeclPrinter, TestFunctionDecl2) {
ASSERT_TRUE(PrintedDeclCXX98Matches(
"void A() {}",
</details>
Does this also work as intented with ctors/dtors ?
Yes, it works for constructors and destructors as well. I added tests for both: with FullyQualifiedName and the new bit set, they print X(ns::Foo) and ~X(), so the parameter type is still qualified while the name is not. Without the bit the destructor is printed as ns::X::~X() (also covered by a test).
<!--PREMERGE ADVISOR COMMENT: Linux-->
:penguin: Linux x64 Test Results
96025 tests passed
4379 tests skipped
:white_check_mark: The build succeeded and all tests passed.
daniel-petrovic
max-winkler
The DeclPrinter for function declarations allows only one printing policy for function name, return type and parameter types. In some cases, e.g. when a function declaration is printed in a class body, one wants fully qualified parameter and return types but no qualifiers on the function name.
Add another bit
SuppressFunctionNameScopeinPrintingPolicywhich is false per default and leaves existing behavior unchanged. If set,DeclPrinterprints the name of a function declaration without nested name specifiers. Return and parameter types are not affected, in contrast toSuppressScope. This givesvoid foo();instead ofvoid ns::A::foo().The clangd tweak OverridePureVirtuals is intended to use this when generating overrides.