Skip to content

[Sema][clangd] add noexcept to override functions during code completion #75937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions clang/lib/Sema/SemaCodeComplete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "clang/AST/Type.h"
#include "clang/Basic/AttributeCommonInfo.h"
#include "clang/Basic/CharInfo.h"
#include "clang/Basic/ExceptionSpecificationType.h"
#include "clang/Basic/OperatorKinds.h"
#include "clang/Basic/Specifiers.h"
#include "clang/Lex/HeaderSearch.h"
Expand Down Expand Up @@ -3427,6 +3428,25 @@ AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result,
Result.AddInformativeChunk(Result.getAllocator().CopyString(QualsStr));
}

static void
AddFunctionExceptSpecToCompletionString(std::string &NameAndSignature,
const FunctionDecl *Function) {
const auto *Proto = Function->getType()->getAs<FunctionProtoType>();
if (!Proto)
return;

auto ExceptInfo = Proto->getExceptionSpecInfo();
switch (ExceptInfo.Type) {
case EST_BasicNoexcept:
case EST_NoexceptTrue:
NameAndSignature += " noexcept";
break;

default:
break;
}
}

/// Add the name of the given declaration
static void AddTypedNameChunk(ASTContext &Context, const PrintingPolicy &Policy,
const NamedDecl *ND,
Expand Down Expand Up @@ -3642,6 +3662,13 @@ CodeCompletionResult::createCodeCompletionStringForOverride(
std::string NameAndSignature;
// For overrides all chunks go into the result, none are informative.
printOverrideString(*CCS, BeforeName, NameAndSignature);

// If the virtual function is declared with "noexcept", add it in the result
// code completion string.
const auto *VirtualFunc = dyn_cast<FunctionDecl>(Declaration);
assert(VirtualFunc && "overridden decl must be a function");
AddFunctionExceptSpecToCompletionString(NameAndSignature, VirtualFunc);

NameAndSignature += " override";

Result.AddTextChunk(Result.getAllocator().CopyString(BeforeName));
Expand Down
14 changes: 14 additions & 0 deletions clang/test/CodeCompletion/overrides.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,17 @@ void func() {
// Runs completion at empty line on line 37.
// RUN: not %clang_cc1 -fsyntax-only -code-completion-at=%s:%(line-5):1 %s -o - | FileCheck -check-prefix=CHECK-CC4 %s
// CHECK-CC4: COMPLETION: Pattern : void vfunc(bool param, int p) override{{$}}

class NoexceptBase {
public:
virtual void method() noexcept;
};

class NoexceptDerived : public NoexceptBase {
public:
met;
};

// Runs completion at met^ in the body of NoexceptDerived.
// RUN: not %clang_cc1 -fsyntax-only -code-completion-at=%s:%(line-4):6 %s -o - | FileCheck -check-prefix=CHECK-CC5 %s
// CHECK-CC5: COMPLETION: Pattern : void method() noexcept override{{$}}