11#include "gmock/gmock.h"
12#include "gtest/gtest.h"
14using ::testing::ElementsAre;
22TEST_F(DefineInlineTest, TriggersOnFunctionDecl) {
29 [[void [[Bar::[[b^a^z]]]]() [[{
34 [[void [[f^o^o]]() [[{
43 [[vo^id ]]foo[[()]] {[[
48 // Definition with no body.
49 class Bar { Bar() = def^ault; };
53TEST_F(DefineInlineTest, NoForwardDecl) {
54 Header = "void bar();";
59 // FIXME: Generate a decl in the header.
65TEST_F(DefineInlineTest, ReferencedDecls) {
76 Header =
"void foo(int test);";
85 Header =
"void bar();" + Header;
114 ExtraFiles[
"a.h"] =
"void bar();";
115 Header =
"void foo(int test);";
124TEST_F(DefineInlineTest, TemplateSpec) {
126 template <typename T> void foo();
127 template<> void foo<char>();
129 template<> void f^oo<int>() {
132 template <typename T> void foo();
134 template<> void f^oo<int>() {
137 template <typename T> struct Foo { void foo(); };
139 template <typename T> void Foo<T>::f^oo() {
142 template <typename T> void foo();
144 template <> void foo<int>();
146 template<> void f^oo<int>() {
151 template <typename T> void f^oo() {}
152 template void foo<int>();
156TEST_F(DefineInlineTest, CheckForCanonDecl) {
162 // This bar normally refers to the definition just above, but it is not
163 // visible from the forward declaration of foo.
177TEST_F(DefineInlineTest, UsingShadowDecls) {
179 namespace ns1 { void foo(int); }
180 namespace ns2 { void foo(int*); }
181 template <typename T>
187 template <typename T>
193TEST_F(DefineInlineTest, TransformNestedNamespaces) {
253TEST_F(DefineInlineTest, TransformUsings) {
255 namespace a { namespace b { namespace c { void aux(); } } }
266 namespace a { namespace b { namespace c { void aux(); } } }
270 using namespace a::b;
271 using namespace a::b::c;
273 namespace d = a::b::c;
279TEST_F(DefineInlineTest, TransformDecls) {
289 enum En { Zero, One };
292 enum class EnClass { Zero, One };
293 EnClass y = EnClass::Zero;
303 enum En { Zero, One };
306 enum class EnClass { Zero, One };
307 EnClass y = EnClass::Zero;
313TEST_F(DefineInlineTest, TransformTemplDecls) {
316 template <typename T> class Bar {
320 template <typename T> T bar;
321 template <typename T> void aux() {}
333 template <typename T> class Bar {
337 template <typename T> T bar;
338 template <typename T> void aux() {}
342 a::bar<a::Bar<int>>.bar();
343 a::aux<a::Bar<int>>();
351TEST_F(DefineInlineTest, TransformMembers) {
370 ExtraFiles["a.h"] = R
"cpp(
375 llvm::StringMap<std::string> EditedFiles;
384 EXPECT_EQ(apply(Test, &EditedFiles), Expected);
392 EXPECT_THAT(EditedFiles,
396TEST_F(DefineInlineTest, TransformDependentTypes) {
399 template <typename T> class Bar {};
402 template <typename T>
406 template <typename T>
413 template <typename T> class Bar {};
416 template <typename T>
428TEST_F(DefineInlineTest, TransformFunctionTempls) {
430 std::pair<llvm::StringRef, llvm::StringRef> Cases[] = {
432 template <typename T>
436 void foo<int>(int p);
439 void foo<char>(char p);
442 void fo^o<int>(int p) {
446 template <typename T>
450 void foo<int>(int p){
455 void foo<char>(char p);
460 template <typename T>
464 void foo<int>(int p);
467 void foo<char>(char p);
470 void fo^o<char>(char p) {
474 template <typename T>
478 void foo<int>(int p);
481 void foo<char>(char p){
487 template <typename T>
491 void foo<int>(int p);
493 template <typename T>
498 template <typename T>
504 void foo<int>(int p);
508 for (
const auto &Case : Cases)
509 EXPECT_EQ(apply(Case.first), Case.second) << Case.first;
512TEST_F(DefineInlineTest, TransformTypeLocs) {
515 template <typename T> class Bar {
517 template <typename Q> class Baz {};
528 a::Bar<Bar<int>>::Baz<Bar<int>> q;
532 template <typename T> class Bar {
534 template <typename Q> class Baz {};
542 a::Bar<a::Bar<int>>::Baz<a::Bar<int>> q;
550TEST_F(DefineInlineTest, TransformDeclRefs) {
553 template <typename T> class Bar {
570 Bar<Bar<int>>::bar();
579 template <typename T> class Bar {
594 a::Bar<a::Bar<int>>::bar();
596 B.x = a::Bar<int>::y;
606TEST_F(DefineInlineTest, StaticMembers) {
608 namespace ns { class X { static void foo(); void bar(); }; }
613 namespace ns { class X { static void foo(); void bar(){
620TEST_F(DefineInlineTest, TransformParamNames) {
621 std::pair<llvm::StringRef, llvm::StringRef> Cases[] = {
623 void foo(int, bool b, int T\
625 void ^foo(int f, bool x, int z) {})cpp",
627 void foo(int f, bool x, int z){}
633 void ^foo(int X) {})cpp",
634 "fail: Cant rename parameter inside macro body."},
638 #define BODY(x) 5 * (x) + 2
640 void foo(PARAM, TYPE Q, TYPE, TYPE W = BODY(P));
642 void ^foo(int Z, int b, int c, int d) {})cpp",
646 #define BODY(x) 5 * (x) + 2
648 void foo(PARAM, TYPE b, TYPE c, TYPE d = BODY(x)){}
651 for (
const auto &Case : Cases)
652 EXPECT_EQ(apply(Case.first), Case.second) << Case.first;
655TEST_F(DefineInlineTest, TransformTemplParamNames) {
659 template <class, class X,
660 template<typename> class, template<typename> class Y,
662 void foo(X, Y<X>, int W = 5 * Z + 2);
666 template <class T, class U,
667 template<typename> class V, template<typename> class W,
669 void Foo::Bar::f^oo(U, W<U>, int Q) {})cpp";
673 template <class T, class U,
674 template<typename> class V, template<typename> class W,
676 void foo(U, W<U>, int Q = 5 * Y + 2){}
684TEST_F(DefineInlineTest, TransformInlineNamespaces) {
686 namespace a { inline namespace b { namespace { struct Foo{}; } } }
690 void ^foo() {Foo foo;})cpp";
692 namespace a { inline namespace b { namespace { struct Foo{}; } } }
693 void foo(){a::Foo foo;}
700TEST_F(DefineInlineTest, TokensBeforeSemicolon) {
701 std::pair<llvm::StringRef, llvm::StringRef> Cases[] = {
703 void foo() /*Comment -_-*/ /*Com 2*/ ;
704 void fo^o() { return ; })cpp",
706 void foo() /*Comment -_-*/ /*Com 2*/ { return ; }
711 void fo^o() { return ; })cpp",
713 void foo(){ return ; }
719 void fo^o() { return ; })cpp",
720 "fail: Couldn't find semicolon for target declaration."},
722 for (
const auto &Case : Cases)
723 EXPECT_EQ(apply(Case.first), Case.second) << Case.first;
726TEST_F(DefineInlineTest, HandleMacros) {
728 #define BODY { return; }
730 void f^oo()BODY)cpp");
733 #define BODY void foo(){ return; }
737 std::pair<llvm::StringRef, llvm::StringRef> Cases[] = {
741 namespace a { class Foo{}; }
744 void f^oo(){BODY();})cpp",
747 namespace a { class Foo{}; }
756 void f^oo(){BODY})cpp",
763 #define TARGET void foo()
765 void f^oo(){ return; })cpp",
767 #define TARGET void foo()
774 void f^oo(){ return; })cpp",
777 void TARGET(){ return; }
780 for (
const auto &Case : Cases)
781 EXPECT_EQ(apply(Case.first), Case.second) << Case.first;
784TEST_F(DefineInlineTest, DropCommonNameSpecifiers) {
786 llvm::StringRef Test;
790 namespace a { namespace b { void aux(); } }
793 namespace qq { void test(); }
796 namespace ns3 { void baz(); }
801 using namespace a::b;
802 using namespace ns1::qq;
803 void ns1::ns2::ns3::b^az() {
807 ns1::ns2::ns3::baz();
812 namespace a { namespace b { void aux(); } }
815 namespace qq { void test(); }
818 namespace ns3 { void baz(){
822 ns1::ns2::ns3::baz();
830 using namespace a::b;
831 using namespace ns1::qq;
835 namespace qq { struct Foo { struct Bar {}; }; using B = Foo::Bar; }
836 namespace ns2 { void baz(); }
839 using namespace ns1::qq;
840 void ns1::ns2::b^az() { Foo f; B b; })cpp",
843 namespace qq { struct Foo { struct Bar {}; }; using B = Foo::Bar; }
844 namespace ns2 { void baz(){ qq::Foo f; qq::B b; } }
847 using namespace ns1::qq;
852 template<class T> struct Foo { template <class U> struct Bar {}; };
853 template<class T, class U>
854 using B = typename Foo<T>::template Bar<U>;
856 namespace ns2 { void baz(); }
859 using namespace ns1::qq;
860 void ns1::ns2::b^az() { B<int, bool> b; })cpp",
864 template<class T> struct Foo { template <class U> struct Bar {}; };
865 template<class T, class U>
866 using B = typename Foo<T>::template Bar<U>;
868 namespace ns2 { void baz(){ qq::B<int, bool> b; } }
871 using namespace ns1::qq;
874 for (
const auto &Case : Cases)
875 EXPECT_EQ(apply(Case.Test), Case.Expected) << Case.Test;
878TEST_F(DefineInlineTest, QualifyWithUsingDirectives) {
879 llvm::StringRef Test = R
"cpp(
882 namespace b { struct Foo{}; void aux(); }
883 namespace c { void cux(); }
897 // FIXME: The last reference to cux() in body of foo should not be
898 // qualified, since there is a using directive inside the function body.
904 namespace b { struct Foo{}; void aux(); }
905 namespace c { void cux(); }
915 // FIXME: The last reference to cux() in body of foo should not be
916 // qualified, since there is a using directive inside the function body.
923 EXPECT_EQ(apply(Test), Expected) << Test;
926TEST_F(DefineInlineTest, AddInline) {
927 llvm::StringMap<std::string> EditedFiles;
928 ExtraFiles["a.h"] =
"void foo();";
929 apply(R
"cpp(#include "a.h"
932 EXPECT_THAT(EditedFiles, testing::ElementsAre(FileWithContents(
933 testPath("a.h"),
"inline void foo(){}")));
936 ExtraFiles[
"a.h"] =
"const int foo();";
937 apply(R
"cpp(#include "a.h"
938 const int fo^o() {})cpp",
940 EXPECT_THAT(EditedFiles, testing::ElementsAre(FileWithContents(
941 testPath("a.h"),
"inline const int foo(){}")));
944 ExtraFiles[
"a.h"] =
"inline void foo();";
945 apply(R
"cpp(#include "a.h"
946 inline void fo^o() {})cpp",
948 EXPECT_THAT(EditedFiles, testing::ElementsAre(FileWithContents(
949 testPath("a.h"),
"inline void foo(){}")));
952 ExtraFiles[
"a.h"] =
"constexpr void foo();";
953 apply(R
"cpp(#include "a.h"
954 constexpr void fo^o() {})cpp",
956 EXPECT_THAT(EditedFiles, testing::ElementsAre(FileWithContents(
957 testPath("a.h"),
"constexpr void foo(){}")));
960 ExtraFiles[
"a.h"] =
"struct Foo { void foo(); };";
961 apply(R
"cpp(#include "a.h"
962 void Foo::fo^o() {})cpp",
964 EXPECT_THAT(EditedFiles,
965 testing::ElementsAre(FileWithContents(
966 testPath("a.h"),
"struct Foo { void foo(){} };")));
969 ExtraFiles[
"a.h"] =
"template <typename T> void foo();";
970 apply(R
"cpp(#include "a.h"
971 template <typename T>
974 EXPECT_THAT(EditedFiles,
975 testing::ElementsAre(FileWithContents(
976 testPath("a.h"),
"template <typename T> void foo(){}")));
979 ExtraFiles[
"a.h"] = R
"cpp(
980 template <typename T> void foo();
981 template <> void foo<int>();)cpp";
982 apply(R"cpp(#include "a.h"
984 void fo^o<int>() {})cpp",
986 EXPECT_THAT(EditedFiles,
987 testing::ElementsAre(FileWithContents(testPath("a.h"),
989 template <typename T> void foo();
990 template <> inline void foo<int>(){})cpp")));
std::vector< const char * > Expected
#define TWEAK_TEST(TweakID)
#define EXPECT_AVAILABLE(MarkedCode)
#define EXPECT_UNAVAILABLE(MarkedCode)
TEST_F(BackgroundIndexTest, NoCrashOnErrorFile)
std::string testPath(PathRef File, llvm::sys::path::Style Style)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//