Skip to content

Commit 36f970d

Browse files
committed
[clangd][Sema] add noexcept to override functions during code completion
1 parent ac7e391 commit 36f970d

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

clang/lib/Sema/SemaCodeComplete.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "clang/AST/Type.h"
2727
#include "clang/Basic/AttributeCommonInfo.h"
2828
#include "clang/Basic/CharInfo.h"
29+
#include "clang/Basic/ExceptionSpecificationType.h"
2930
#include "clang/Basic/OperatorKinds.h"
3031
#include "clang/Basic/Specifiers.h"
3132
#include "clang/Lex/HeaderSearch.h"
@@ -3427,6 +3428,25 @@ AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result,
34273428
Result.AddInformativeChunk(Result.getAllocator().CopyString(QualsStr));
34283429
}
34293430

3431+
static void
3432+
AddFunctionExceptSpecToCompletionString(std::string &NameAndSignature,
3433+
const FunctionDecl *Function) {
3434+
const auto *Proto = Function->getType()->getAs<FunctionProtoType>();
3435+
if (!Proto)
3436+
return;
3437+
3438+
auto ExceptInfo = Proto->getExceptionSpecInfo();
3439+
switch (ExceptInfo.Type) {
3440+
case EST_BasicNoexcept:
3441+
case EST_NoexceptTrue:
3442+
NameAndSignature += " noexcept";
3443+
break;
3444+
3445+
default:
3446+
break;
3447+
}
3448+
}
3449+
34303450
/// Add the name of the given declaration
34313451
static void AddTypedNameChunk(ASTContext &Context, const PrintingPolicy &Policy,
34323452
const NamedDecl *ND,
@@ -3642,6 +3662,13 @@ CodeCompletionResult::createCodeCompletionStringForOverride(
36423662
std::string NameAndSignature;
36433663
// For overrides all chunks go into the result, none are informative.
36443664
printOverrideString(*CCS, BeforeName, NameAndSignature);
3665+
3666+
// If the virtual function is declared with "noexcept", add it in the result
3667+
// code completion string.
3668+
const auto *VirtualFunc = dyn_cast<FunctionDecl>(Declaration);
3669+
assert(VirtualFunc && "overridden decl must be a function");
3670+
AddFunctionExceptSpecToCompletionString(NameAndSignature, VirtualFunc);
3671+
36453672
NameAndSignature += " override";
36463673

36473674
Result.AddTextChunk(Result.getAllocator().CopyString(BeforeName));

clang/test/CodeCompletion/overrides.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,17 @@ void func() {
4141
// Runs completion at empty line on line 37.
4242
// RUN: not %clang_cc1 -fsyntax-only -code-completion-at=%s:%(line-5):1 %s -o - | FileCheck -check-prefix=CHECK-CC4 %s
4343
// CHECK-CC4: COMPLETION: Pattern : void vfunc(bool param, int p) override{{$}}
44+
45+
class NoexceptBase {
46+
public:
47+
virtual void method() noexcept;
48+
};
49+
50+
class NoexceptDerived : public NoexceptBase {
51+
public:
52+
met;
53+
};
54+
55+
// Runs completion at met^ in the body of NoexceptDerived.
56+
// RUN: not %clang_cc1 -fsyntax-only -code-completion-at=%s:%(line-4):6 %s -o - | FileCheck -check-prefix=CHECK-CC5 %s
57+
// CHECK-CC5: COMPLETION: Pattern : void method() noexcept override{{$}}

0 commit comments

Comments
 (0)