15#include "clang/AST/ASTTypeTraits.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclBase.h"
19#include "clang/Basic/AttrKinds.h"
20#include "clang/Basic/SourceManager.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/Support/Casting.h"
23#include "gmock/gmock.h"
24#include "gtest/gtest.h"
32using testing::Contains;
34using testing::IsEmpty;
36TEST(GetDeducedType, KwAutoKwDecltypeExpansion) {
41 {
"^auto i = 0;",
"int"},
42 {
"^auto f(){ return 1;};",
"int"},
44 R
"cpp( // auto on struct in a namespace
45 namespace ns1 { struct S {}; }
51 R
"cpp( // decltype on struct
52 namespace ns1 { struct S {}; }
59 R
"cpp(// decltype(auto) on struct&
66 ^decltype(auto) k = j;
71 R
"cpp( // auto on template class
73 template<typename T> class Foo {};
79 R
"cpp( // auto on initializer list.
83 class [[initializer_list]] { const _E *a, *b; };
88 "std::initializer_list<int>",
91 R
"cpp( // auto in function return type with trailing return type
93 ^auto test() -> decltype(Foo()) {
100 R
"cpp( // decltype in trailing return type
102 auto test() -> ^decltype(Foo()) {
109 R
"cpp( // auto in function return type
118 R
"cpp( // auto& in function return type
128 R
"cpp( // auto* in function return type
138 R
"cpp( // const auto& in function return type
140 const ^auto& test() {
148 R
"cpp( // decltype(auto) in function return (value)
150 ^decltype(auto) test() {
157 R
"cpp( // decltype(auto) in function return (ref)
159 ^decltype(auto) test() {
167 R
"cpp( // decltype(auto) in function return (const ref)
169 ^decltype(auto) test() {
177 R
"cpp( // auto on alias
186 // Generic lambda param.
188 auto Generic = [](^auto x) { return 0; };
189 int m = Generic(Foo{});
195 // Generic lambda instantiated twice, matching deduction.
197 auto Generic = [](^auto x, auto y) { return 0; };
198 int m = Generic(Foo{}, "one");
199 int n = Generic(Foo{}, 2);
206 // Generic lambda instantiated twice, conflicting deduction.
208 auto Generic = [](^auto y) { return 0; };
209 int m = Generic("one");
216 // Generic function param.
218 int generic(^auto x) { return 0; }
219 int m = generic(Foo{});
225 // More complicated param type involving auto.
226 template <class> concept C = true;
228 int generic(C ^auto *x) { return 0; }
229 const Foo *Ptr = nullptr;
230 int m = generic(Ptr);
235 for (Test T : Tests) {
236 Annotations
File(
T.AnnotatedCode);
238 TU.ExtraArgs.push_back(
"-std=c++20");
239 auto AST = TU.build();
240 SourceManagerForFile SM(
"foo.cpp",
File.code());
242 SCOPED_TRACE(
T.AnnotatedCode);
243 EXPECT_FALSE(
File.points().empty());
244 for (Position
Pos :
File.points()) {
248 if (
T.DeducedType ==
nullptr) {
258TEST(ClangdAST, GetOnlyInstantiation) {
261 llvm::StringLiteral NodeType;
266 template <typename> class X {};
270 "template<> class X<int> {}",
274 template <typename T> T X = T{};
283 template <typename T> int X(T) { return 42; }
287 "template<> int X<const char *>(const char *)",
291 int X(auto *x) { return 42; }
295 "template<> int X<const char>(const char *x)",
299 for (
const auto &Case : Cases) {
300 SCOPED_TRACE(Case.Code);
302 TU.ExtraArgs.push_back(
"-std=c++20");
303 auto AST = TU.build();
304 PrintingPolicy PP =
AST.getASTContext().getPrintingPolicy();
305 PP.TerseOutput =
true;
308 const_cast<NamedDecl *
>(&
findDecl(
AST, [&](
const NamedDecl &D) {
309 return D.getDescribedTemplate() !=
nullptr &&
310 D.getDeclKindName() == Case.NodeType;
312 llvm::raw_string_ostream
OS(
Name);
313 Result->print(
OS, PP);
317 EXPECT_EQ(Case.Name,
Name);
319 EXPECT_THAT(
Name, IsEmpty());
323TEST(ClangdAST, GetContainedAutoParamType) {
343 TU.ExtraArgs.push_back("-std=c++20");
344 auto AST = TU.build();
346 const auto &WithAuto =
347 llvm::cast<FunctionTemplateDecl>(
findDecl(
AST,
"withAuto"));
348 auto ParamsWithAuto = WithAuto.getTemplatedDecl()->parameters();
349 auto *TemplateParamsWithAuto = WithAuto.getTemplateParameters();
350 ASSERT_EQ(ParamsWithAuto.size(), TemplateParamsWithAuto->size());
352 for (
unsigned I = 0; I < ParamsWithAuto.size(); ++I) {
353 SCOPED_TRACE(ParamsWithAuto[I]->getNameAsString());
355 ParamsWithAuto[I]->getTypeSourceInfo()->getTypeLoc());
356 ASSERT_FALSE(
Loc.isNull());
357 EXPECT_EQ(
Loc.getTypePtr()->getDecl(), TemplateParamsWithAuto->getParam(I));
360 const auto &WithoutAuto =
361 llvm::cast<FunctionDecl>(
findDecl(
AST,
"withoutAuto"));
362 for (
auto *ParamWithoutAuto : WithoutAuto.parameters()) {
364 ParamWithoutAuto->getTypeSourceInfo()->getTypeLoc())
369TEST(ClangdAST, GetQualification) {
376 llvm::StringRef Test;
377 std::vector<llvm::StringRef> Qualifications;
378 std::vector<std::string> VisibleNamespaces;
382 namespace ns1 { namespace ns2 { class Foo {}; } }
383 void insert(); // ns1::ns2::Foo
385 void insert(); // ns2::Foo
387 void insert(); // Foo
390 void insert(); // Foo
393 void insert(); // ns2::Foo
395 void insert(); // Foo
397 {"ns1::ns2::",
"ns2::",
"",
"",
"ns2::",
""},
402 namespace ns1 { namespace ns2 { class Bar { void Foo(); }; } }
403 void insert(); // ns1::ns2::Bar::Foo
405 void insert(); // ns2::Bar::Foo
407 void insert(); // Bar::Foo
410 void insert(); // Bar::Foo
413 void insert(); // ns2::Bar::Foo
415 void insert(); // Bar::Foo
417 {"ns1::ns2::Bar::",
"ns2::Bar::",
"Bar::",
"Bar::",
"ns2::Bar::",
423 namespace ns1 { namespace ns2 { void Foo(); } }
424 void insert(); // ns2::Foo
426 void insert(); // ns2::Foo
428 void insert(); // Foo
432 {"ns2::",
"ns2::",
""},
442 void insert(); // ns::Foo
448 for (
const auto &Case : Cases) {
449 Annotations Test(Case.Test);
451 ParsedAST
AST = TU.build();
452 std::vector<const Decl *> InsertionPoints;
453 const NamedDecl *TargetDecl;
455 if (ND.getNameAsString() ==
"Foo") {
460 if (ND.getNameAsString() ==
"insert")
461 InsertionPoints.push_back(&ND);
465 ASSERT_EQ(InsertionPoints.size(), Case.Qualifications.size());
466 for (
size_t I = 0,
E = InsertionPoints.size(); I !=
E; ++I) {
467 const Decl *D = InsertionPoints[I];
468 if (Case.VisibleNamespaces.empty()) {
470 D->getLexicalDeclContext(), D->getBeginLoc(),
472 Case.Qualifications[I]);
475 D->getLexicalDeclContext(), TargetDecl,
476 Case.VisibleNamespaces),
477 Case.Qualifications[I]);
483TEST(ClangdAST, PrintType) {
485 llvm::StringRef Test;
486 std::vector<llvm::StringRef> Types;
490 namespace ns1 { namespace ns2 { class Foo {}; } }
491 void insert(); // ns1::ns2::Foo
493 void insert(); // ns2::Foo
495 void insert(); // Foo
499 {"ns1::ns2::Foo",
"ns2::Foo",
"Foo"},
506 void insert(); // ns1::Foo
508 void insert(); // Foo
514 for (
const auto &Case : Cases) {
515 Annotations Test(Case.Test);
517 ParsedAST
AST = TU.build();
518 std::vector<const DeclContext *> InsertionPoints;
519 const TypeDecl *TargetDecl =
nullptr;
521 if (ND.getNameAsString() ==
"Foo") {
522 if (const auto *TD = llvm::dyn_cast<TypeDecl>(&ND)) {
526 }
else if (ND.getNameAsString() ==
"insert")
527 InsertionPoints.push_back(ND.getDeclContext());
531 ASSERT_EQ(InsertionPoints.size(), Case.Types.size());
532 for (
size_t I = 0,
E = InsertionPoints.size(); I !=
E; ++I) {
533 const auto *DC = InsertionPoints[I];
534 EXPECT_EQ(
printType(
AST.getASTContext().getTypeDeclType(TargetDecl), *DC),
540TEST(ClangdAST, IsDeeplyNested) {
551 ParsedAST AST = TU.build();
566MATCHER_P(attrKind,
K,
"") {
return arg->getKind() ==
K; }
568MATCHER(implicitAttr,
"") {
return arg->isImplicit(); }
570TEST(ClangdAST, GetAttributes) {
571 const char *Code = R
"cpp(
573 class [[nodiscard]] Y{};
574 void f(int * a, int * __attribute__((nonnull)) b);
581 auto DeclAttrs = [&](llvm::StringRef
Name) {
585 ASSERT_THAT(DeclAttrs(
"X"), Each(implicitAttr()));
586 ASSERT_THAT(DeclAttrs(
"Y"), Contains(attrKind(attr::WarnUnusedResult)));
587 ASSERT_THAT(DeclAttrs(
"f"), Each(implicitAttr()));
588 ASSERT_THAT(DeclAttrs(
"a"), Each(implicitAttr()));
589 ASSERT_THAT(DeclAttrs(
"b"), Contains(attrKind(attr::NonNull)));
591 Stmt *FooBody = cast<FunctionDecl>(
findDecl(
AST,
"foo")).getBody();
592 IfStmt *FooIf = cast<IfStmt>(cast<CompoundStmt>(FooBody)->body_front());
594 Each(implicitAttr()));
595 ASSERT_THAT(
getAttributes(DynTypedNode::create(*FooIf->getThen())),
596 Contains(attrKind(attr::Unlikely)));
599TEST(ClangdAST, HasReservedName) {
603 inline namespace __1 { class error_code; }
604 namespace __detail { int secret; }
622TEST(ClangdAST, PreferredIncludeDirective) {
623 auto ComputePreferredDirective = [](TestTU &TU) {
624 auto AST = TU.build();
626 AST.getIncludeStructure().MainFileIncludes,
627 AST.getLocalTopLevelDecls());
632 ObjCTU.Filename = "TestTU.m";
633 EXPECT_EQ(ComputePreferredDirective(ObjCTU),
639 HeaderTU.Filename = "TestTUHeader.h";
640 HeaderTU.ExtraArgs = {
"-xobjective-c++-header"};
641 EXPECT_EQ(ComputePreferredDirective(HeaderTU),
645 HeaderTU.Code = R
"cpp(
648 EXPECT_EQ(ComputePreferredDirective(HeaderTU),
651 HeaderTU.Code = R"cpp(
657 EXPECT_EQ(ComputePreferredDirective(HeaderTU),
const FunctionDecl * Decl
llvm::SmallString< 256U > Name
std::string AnnotatedCode
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
std::string printType(const QualType QT, const DeclContext &CurContext, const llvm::StringRef Placeholder)
Returns a QualType as string.
std::string getQualification(ASTContext &Context, const DeclContext *DestContext, SourceLocation InsertionPoint, const NamedDecl *ND)
Gets the nested name specifier necessary for spelling ND in DestContext, at InsertionPoint.
NamedDecl * getOnlyInstantiation(NamedDecl *TemplatedDecl)
Symbol::IncludeDirective preferredIncludeDirective(llvm::StringRef FileName, const LangOptions &LangOpts, ArrayRef< Inclusion > MainFileIncludes, ArrayRef< const Decl * > TopLevelDecls)
Infer the include directive to use for the given FileName.
TEST(BackgroundQueueTest, Priority)
bool hasReservedName(const Decl &D)
Returns true if this is a NamedDecl with a reserved name.
const NamedDecl & findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name)
std::vector< const Attr * > getAttributes(const DynTypedNode &N)
Return attributes attached directly to a node.
llvm::Expected< SourceLocation > sourceLocationInMainFile(const SourceManager &SM, Position P)
Return the file location, corresponding to P.
bool hasReservedScope(const DeclContext &DC)
Returns true if this scope would be written with a reserved name.
TemplateTypeParmTypeLoc getContainedAutoParamType(TypeLoc TL)
bool isDeeplyNested(const Decl *D, unsigned MaxDepth)
Checks whether D is more than MaxDepth away from translation unit scope.
std::optional< QualType > getDeducedType(ASTContext &ASTCtx, SourceLocation Loc)
Retrieves the deduced type at a given location (auto, decltype).
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
@ Include
#include "header.h"
@ Import
#import "header.h"
static TestTU withCode(llvm::StringRef Code)