Skip to content

Commit e5dd4f1

Browse files
HighCommander4llvmbot
authored andcommitted
[clangd] Guard against trivial FunctionProtoTypeLoc when creating inlay hints (llvm#143087)
Fixes llvm#142608 (cherry picked from commit 392bd57)
1 parent f653271 commit e5dd4f1

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

clang-tools-extra/clangd/InlayHints.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "llvm/ADT/StringExtras.h"
3434
#include "llvm/ADT/StringRef.h"
3535
#include "llvm/ADT/Twine.h"
36+
#include "llvm/ADT/identity.h"
3637
#include "llvm/Support/Casting.h"
3738
#include "llvm/Support/ErrorHandling.h"
3839
#include "llvm/Support/FormatVariadic.h"
@@ -368,7 +369,11 @@ static FunctionProtoTypeLoc getPrototypeLoc(Expr *Fn) {
368369
}
369370

370371
if (auto F = Target.getAs<FunctionProtoTypeLoc>()) {
371-
return F;
372+
// In some edge cases the AST can contain a "trivial" FunctionProtoTypeLoc
373+
// which has null parameters. Avoid these as they don't contain useful
374+
// information.
375+
if (llvm::all_of(F.getParams(), llvm::identity<ParmVarDecl *>()))
376+
return F;
372377
}
373378

374379
return {};

clang-tools-extra/clangd/unittests/InlayHintTests.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,11 +997,16 @@ TEST(ParameterHints, FunctionPointer) {
997997
f3_t f3;
998998
using f4_t = void(__stdcall *)(int param);
999999
f4_t f4;
1000+
__attribute__((noreturn)) f4_t f5;
10001001
void bar() {
10011002
f1($f1[[42]]);
10021003
f2($f2[[42]]);
10031004
f3($f3[[42]]);
10041005
f4($f4[[42]]);
1006+
// This one runs into an edge case in clang's type model
1007+
// and we can't extract the parameter name. But at least
1008+
// we shouldn't crash.
1009+
f5(42);
10051010
}
10061011
)cpp",
10071012
ExpectedHint{"param: ", "f1"}, ExpectedHint{"param: ", "f2"},

0 commit comments

Comments
 (0)