12#include "clang/AST/ASTContext.h"
13#include "clang/AST/Decl.h"
14#include "llvm/ADT/StringRef.h"
15#include "gmock/gmock.h"
16#include "gtest/gtest.h"
23using ::testing::Field;
24using ::testing::Matcher;
25using ::testing::SizeIs;
26using ::testing::UnorderedElementsAreArray;
28class ExpectedTypeConversionTest :
public ::testing::Test {
30 void build(llvm::StringRef
Code) {
31 assert(!AST &&
"AST built twice");
35 const NamedDecl *decl(llvm::StringRef
Name) {
return &
findDecl(*AST,
Name); }
37 QualType typeOf(llvm::StringRef
Name) {
38 return cast<ValueDecl>(decl(
Name))->getType().getCanonicalType();
42 std::optional<OpaqueType> fromCompletionResult(
const NamedDecl *D) {
44 astCtx(), CodeCompletionResult(D, CCP_Declaration));
49 using EquivClass = std::set<std::string>;
51 Matcher<std::map<std::string, EquivClass>>
52 classesAre(llvm::ArrayRef<EquivClass> Classes) {
53 using MapEntry = std::map<std::string, EquivClass>::value_type;
55 std::vector<Matcher<MapEntry>> Elements;
56 Elements.reserve(Classes.size());
57 for (
auto &Cls : Classes)
58 Elements.push_back(
Field(&MapEntry::second, Cls));
59 return UnorderedElementsAreArray(Elements);
64 std::map<std::string, EquivClass>
65 buildEquivClasses(llvm::ArrayRef<llvm::StringRef> DeclNames) {
66 std::map<std::string, EquivClass> Classes;
67 for (llvm::StringRef
Name : DeclNames) {
69 Classes[std::string(
Type->raw())].insert(std::string(
Name));
74 ASTContext &astCtx() {
return AST->getASTContext(); }
78 std::optional<ParsedAST> AST;
81TEST_F(ExpectedTypeConversionTest, BasicTypes) {
97 // user-defined types.
102 EXPECT_THAT(buildEquivClasses({"b",
"i",
"ui",
"ll",
"f",
"d",
"iptr",
"bptr",
112TEST_F(ExpectedTypeConversionTest, ReferencesDontMatter) {
116 const int & const_ref = noref;
120 EXPECT_THAT(buildEquivClasses({"noref",
"ref",
"const_ref",
"rv_ref"}),
124TEST_F(ExpectedTypeConversionTest, ArraysDecay) {
127 int (&arr_ref)[2] = arr;
131 EXPECT_THAT(buildEquivClasses({"arr",
"arr_ref",
"ptr"}), SizeIs(1));
134TEST_F(ExpectedTypeConversionTest, FunctionReturns) {
144 EXPECT_EQ(fromCompletionResult(decl(
"returns_int")), IntTy);
147 EXPECT_EQ(fromCompletionResult(decl(
"returns_ptr")), IntPtrTy);
150TEST_F(ExpectedTypeConversionTest, Templates) {
153int* returns_not_dependent();
155T* returns_dependent();
158int* var_not_dependent = nullptr;
160T* var_dependent = nullptr;
166 EXPECT_EQ(fromCompletionResult(decl(
"returns_not_dependent")), IntPtrTy);
167 EXPECT_EQ(fromCompletionResult(decl(
"returns_dependent")), std::nullopt);
169 EXPECT_EQ(fromCompletionResult(decl(
"var_not_dependent")), IntPtrTy);
170 EXPECT_EQ(fromCompletionResult(decl(
"var_dependent")), std::nullopt);
llvm::SmallString< 256U > Name
static std::optional< OpaqueType > fromCompletionResult(ASTContext &Ctx, const CodeCompletionResult &R)
Create a type from a code completion result.
static std::optional< OpaqueType > fromType(ASTContext &Ctx, QualType Type)
Construct an instance from a clang::QualType.
TEST_F(BackgroundIndexTest, NoCrashOnErrorFile)
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
@ Type
An inlay hint that for a type annotation.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static TestTU withCode(llvm::StringRef Code)