← Back to Pull requests

[clang][AST] Allow DeclPrinter to suppress function name scope #243057

Open
max-winkler opened this pull request with 65 lines diff across 3 files
View on GitHub
max-winkler max-winkler commented on Nov 16, 2026 12:17 UTC
Author

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.

Diff: 65 lines (+62 / -3) across 3 files Reviewer: daniel-petrovic Review duration: 24753m
All Vouch review confidence checks passed
Residual risk 0.03 = Change Risk (0.31) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.31 · 65 lines diff across 3 files
0.31
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.90 · 5 substantive comment(s)
0.90
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
91%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.251
Real PR Conversation Timeline
6 items
max-winkler max-winkler commented on Nov 16, 2026 12:18 UTC
Author

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
  • removing the qualifier from the printed string afterwards, which is fragile because it has to parse the output.
  • llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Nov 16, 2026 12:18 UTC
    Bot

    <!--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:

  • (modified) clang/include/clang/AST/PrettyPrinter.h (+10-1)

  • (modified) clang/lib/AST/DeclPrinter.cpp (+2-2)

  • (modified) clang/unittests/AST/DeclPrinterTest.cpp (+24)
  • ``````

    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>

    daniel-petrovic daniel-petrovic commented on code on Nov 16, 2026 13:23 UTC
    Code Review
    clang/include/clang/AST/PrettyPrinter.h:1
    @@ -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;

    Does this also work as intented with ctors/dtors ?

    max-winkler max-winkler commented on code on Nov 16, 2026 16:56 UTC
    Code Review
    clang/include/clang/AST/PrettyPrinter.h:1
    @@ -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;

    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).

    github-actions[bot] github-actions[bot] commented on Nov 16, 2026 18:05 UTC
    Bot

    <!--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.

    Assignees
    max-winkler max-winkler
    Labels
    risk: low confidence: 91.1% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 31.0% (LOW) · Higher = larger blast radius
    31.0%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 91.1% · Lower = rushed or shallow review
    91.1%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 2.8% · Safe to merge
    2.8%