1 //===- unittest/Tooling/StandardLibrary.cpp -------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "clang/Tooling/Inclusions/StandardLibrary.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/AST/Decl.h"
12 #include "clang/AST/DeclarationName.h"
13 #include "clang/Testing/TestAST.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/Casting.h"
16 #include "llvm/Support/ScopedPrinter.h"
18 #include "gmock/gmock.h"
19 #include "gtest/gtest.h"
21 using ::testing::ElementsAre
;
27 const NamedDecl
&lookup(TestAST
&AST
, llvm::StringRef Name
) {
28 TranslationUnitDecl
*TU
= AST
.context().getTranslationUnitDecl();
29 auto Result
= TU
->lookup(DeclarationName(&AST
.context().Idents
.get(Name
)));
30 assert(!Result
.empty() && "Lookup failed");
31 assert(Result
.isSingleResult() && "Lookup returned multiple results");
32 return *Result
.front();
35 TEST(StdlibTest
, All
) {
36 auto VectorH
= stdlib::Header::named("<vector>");
38 EXPECT_EQ(llvm::to_string(*VectorH
), "<vector>");
39 EXPECT_FALSE(stdlib::Header::named("HeadersTests.cpp"));
41 auto Vector
= stdlib::Symbol::named("std::", "vector");
43 EXPECT_EQ(llvm::to_string(*Vector
), "std::vector");
44 EXPECT_FALSE(stdlib::Symbol::named("std::", "dongle"));
45 EXPECT_FALSE(stdlib::Symbol::named("clang::", "ASTContext"));
47 EXPECT_EQ(Vector
->header(), *VectorH
);
48 EXPECT_THAT(Vector
->headers(), ElementsAre(*VectorH
));
51 TEST(StdlibTest
, Recognizer
) {
54 inline namespace inl {
57 struct vector { class nested {}; };
63 inline namespace __1 {
65 inline namespace chrono_inl {
66 class system_clock {};
73 // C Standard Library structure defined in <stdlib.h>
78 std::vector<int>::nested nest;
80 std::chrono::system_clock clock;
85 auto &VectorNonstd
= lookup(AST
, "vector");
86 auto *Vec
= cast
<VarDecl
>(lookup(AST
, "vec")).getType()->getAsCXXRecordDecl();
88 cast
<VarDecl
>(lookup(AST
, "nest")).getType()->getAsCXXRecordDecl();
90 cast
<VarDecl
>(lookup(AST
, "clock")).getType()->getAsCXXRecordDecl();
91 auto *Sec
= cast
<VarDecl
>(lookup(AST
, "sec")).getType()->getAsCXXRecordDecl();
93 cast
<VarDecl
>(lookup(AST
, "div")).getType()->getAsCXXRecordDecl();
95 stdlib::Recognizer Recognizer
;
97 EXPECT_EQ(Recognizer(&VectorNonstd
), llvm::None
);
98 EXPECT_EQ(Recognizer(Vec
), stdlib::Symbol::named("std::", "vector"));
99 EXPECT_EQ(Recognizer(Nest
), stdlib::Symbol::named("std::", "vector"));
100 EXPECT_EQ(Recognizer(Clock
),
101 stdlib::Symbol::named("std::chrono::", "system_clock"));
102 EXPECT_EQ(Recognizer(CDivT
), stdlib::Symbol::named("", "div_t"));
103 EXPECT_EQ(Recognizer(Sec
), llvm::None
);
107 } // namespace tooling