14 #include "clang/AST/Decl.h" 15 #include "clang/AST/DeclBase.h" 16 #include "clang/Basic/SourceManager.h" 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/Support/Casting.h" 19 #include "gmock/gmock.h" 20 #include "gtest/gtest.h" 29 TEST(GetDeducedType, KwAutoExpansion) {
34 {
"^auto i = 0;",
"int"},
35 {
"^auto f(){ return 1;};",
"int"},
37 for (Test T : Tests) {
38 Annotations
File(T.AnnotatedCode);
40 ASSERT_TRUE(
AST.getDiagnostics().empty())
41 <<
AST.getDiagnostics().begin()->Message;
42 SourceManagerForFile SM(
"foo.cpp",
File.code());
44 for (Position
Pos :
File.points()) {
48 EXPECT_EQ(DeducedType->getAsString(), T.DeducedType);
53 TEST(ClangdAST, GetQualification) {
61 std::vector<llvm::StringRef> Qualifications;
62 std::vector<std::string> VisibleNamespaces;
66 namespace ns1 { namespace ns2 { class Foo {}; } } 67 void insert(); // ns1::ns2::Foo 69 void insert(); // ns2::Foo 77 void insert(); // ns2::Foo 81 {"ns1::ns2::",
"ns2::",
"",
"",
"ns2::",
""},
86 namespace ns1 { namespace ns2 { class Bar { void Foo(); }; } } 87 void insert(); // ns1::ns2::Bar::Foo 89 void insert(); // ns2::Bar::Foo 91 void insert(); // Bar::Foo 94 void insert(); // Bar::Foo 97 void insert(); // ns2::Bar::Foo 99 void insert(); // Bar::Foo 101 {"ns1::ns2::Bar::",
"ns2::Bar::",
"Bar::",
"Bar::",
"ns2::Bar::",
107 namespace ns1 { namespace ns2 { void Foo(); } } 108 void insert(); // ns2::Foo 110 void insert(); // ns2::Foo 112 void insert(); // Foo 116 {"ns2::",
"ns2::",
""},
120 for (
const auto &Case : Cases) {
121 Annotations Test(Case.Test);
123 ParsedAST
AST = TU.build();
124 std::vector<const Decl *> InsertionPoints;
125 const NamedDecl *TargetDecl;
126 findDecl(AST, [&](
const NamedDecl &ND) {
127 if (ND.getNameAsString() ==
"Foo") {
132 if (ND.getNameAsString() ==
"insert")
133 InsertionPoints.push_back(&ND);
137 ASSERT_EQ(InsertionPoints.size(), Case.Qualifications.size());
138 for (
size_t I = 0,
E = InsertionPoints.size(); I !=
E; ++I) {
139 const Decl *D = InsertionPoints[I];
140 if (Case.VisibleNamespaces.empty()) {
142 D->getLexicalDeclContext(), D->getBeginLoc(),
144 Case.Qualifications[I]);
147 D->getLexicalDeclContext(), TargetDecl,
148 Case.VisibleNamespaces),
149 Case.Qualifications[I]);
155 TEST(ClangdAST, PrintType) {
157 llvm::StringRef Test;
158 std::vector<llvm::StringRef> Types;
162 namespace ns1 { namespace ns2 { class Foo {}; } } 163 void insert(); // ns1::ns2::Foo 165 void insert(); // ns2::Foo 167 void insert(); // Foo 171 {"ns1::ns2::Foo",
"ns2::Foo",
"Foo"},
178 void insert(); // ns1::Foo 180 void insert(); // Foo 186 for (
const auto &Case : Cases) {
187 Annotations Test(Case.Test);
189 ParsedAST AST = TU.
build();
190 std::vector<const DeclContext *> InsertionPoints;
191 const TypeDecl *TargetDecl =
nullptr;
192 findDecl(AST, [&](
const NamedDecl &ND) {
193 if (ND.getNameAsString() ==
"Foo") {
194 if (
const auto *TD = llvm::dyn_cast<TypeDecl>(&ND)) {
198 }
else if (ND.getNameAsString() ==
"insert")
199 InsertionPoints.push_back(ND.getDeclContext());
203 ASSERT_EQ(InsertionPoints.size(), Case.Types.size());
204 for (
size_t I = 0,
E = InsertionPoints.size(); I !=
E; ++I) {
205 const auto *DC = InsertionPoints[I];
206 EXPECT_EQ(
printType(AST.getASTContext().getTypeDeclType(TargetDecl), *DC),
const FunctionDecl * Decl
llvm::Optional< QualType > getDeducedType(ASTContext &ASTCtx, SourceLocation Loc)
Retrieves the deduced type at a given location (auto, decltype).
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
llvm::Expected< SourceLocation > sourceLocationInMainFile(const SourceManager &SM, Position P)
Return the file location, corresponding to P.
std::string printType(const QualType QT, const DeclContext &CurContext)
Returns a QualType as string.
TEST(BackgroundQueueTest, Priority)
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.
static TestTU withCode(llvm::StringRef Code)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::string AnnotatedCode
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)