1 #include "../../lib/Format/Macros.h" 2 #include "TestLexer.h" 3 #include "clang/Basic/FileManager.h" 4 5 #include "gtest/gtest.h" 6 7 namespace clang { 8 namespace format { 9 10 namespace { 11 12 class MacroExpanderTest : public ::testing::Test { 13 public: 14 MacroExpanderTest() : Lex(Allocator, Buffers) {} 15 std::unique_ptr<MacroExpander> 16 create(const std::vector<std::string> &MacroDefinitions) { 17 return std::make_unique<MacroExpander>(MacroDefinitions, 18 Lex.SourceMgr.get(), Lex.Style, 19 Lex.Allocator, Lex.IdentTable); 20 } 21 22 std::string expand(MacroExpander &Macros, llvm::StringRef Name, 23 const std::vector<std::string> &Args = {}) { 24 EXPECT_TRUE(Macros.defined(Name)); 25 return text(Macros.expand(Lex.id(Name), lexArgs(Args))); 26 } 27 28 llvm::SmallVector<TokenList, 1> 29 lexArgs(const std::vector<std::string> &Args) { 30 llvm::SmallVector<TokenList, 1> Result; 31 for (const auto &Arg : Args) { 32 Result.push_back(uneof(Lex.lex(Arg))); 33 } 34 return Result; 35 } 36 37 struct MacroAttributes { 38 clang::tok::TokenKind Kind; 39 MacroRole Role; 40 unsigned Start; 41 unsigned End; 42 llvm::SmallVector<FormatToken *, 1> ExpandedFrom; 43 }; 44 45 void expectAttributes(const TokenList &Tokens, 46 const std::vector<MacroAttributes> &Attributes, 47 const std::string &File, unsigned Line) { 48 EXPECT_EQ(Tokens.size(), Attributes.size()) << text(Tokens); 49 for (size_t I = 0, E = Tokens.size(); I != E; ++I) { 50 if (I >= Attributes.size()) 51 continue; 52 std::string Context = 53 ("for token " + llvm::Twine(I) + ": " + Tokens[I]->Tok.getName() + 54 " / " + Tokens[I]->TokenText) 55 .str(); 56 EXPECT_TRUE(Tokens[I]->is(Attributes[I].Kind)) 57 << Context << " in " << text(Tokens) << " at " << File << ":" << Line; 58 EXPECT_EQ(Tokens[I]->MacroCtx->Role, Attributes[I].Role) 59 << Context << " in " << text(Tokens) << " at " << File << ":" << Line; 60 EXPECT_EQ(Tokens[I]->MacroCtx->StartOfExpansion, Attributes[I].Start) 61 << Context << " in " << text(Tokens) << " at " << File << ":" << Line; 62 EXPECT_EQ(Tokens[I]->MacroCtx->EndOfExpansion, Attributes[I].End) 63 << Context << " in " << text(Tokens) << " at " << File << ":" << Line; 64 EXPECT_EQ(Tokens[I]->MacroCtx->ExpandedFrom, Attributes[I].ExpandedFrom) 65 << Context << " in " << text(Tokens) << " at " << File << ":" << Line; 66 } 67 } 68 protected: 69 llvm::SpecificBumpPtrAllocator<FormatToken> Allocator; 70 std::vector<std::unique_ptr<llvm::MemoryBuffer>> Buffers; 71 TestLexer Lex; 72 }; 73 74 #define EXPECT_ATTRIBUTES(Tokens, Attributes) \ 75 expectAttributes(Tokens, Attributes, __FILE__, __LINE__) 76 77 TEST_F(MacroExpanderTest, SkipsDefinitionOnError) { 78 auto Macros = 79 create({"A(", "B(,", "C(a,", "D(a a", "E(a, a", "F(,)", "G(a;"}); 80 for (const auto *Name : {"A", "B", "C", "D", "E", "F", "G"}) { 81 EXPECT_FALSE(Macros->defined(Name)) << "for Name " << Name; 82 } 83 } 84 85 TEST_F(MacroExpanderTest, ExpandsWithoutArguments) { 86 auto Macros = create({ 87 "A", 88 "B=b", 89 "C=c + c", 90 "D()", 91 }); 92 EXPECT_TRUE(Macros->objectLike("A")); 93 EXPECT_TRUE(Macros->objectLike("B")); 94 EXPECT_TRUE(Macros->objectLike("C")); 95 EXPECT_TRUE(!Macros->objectLike("D")); 96 EXPECT_EQ("", expand(*Macros, "A")); 97 EXPECT_EQ("b", expand(*Macros, "B")); 98 EXPECT_EQ("c+c", expand(*Macros, "C")); 99 EXPECT_EQ("", expand(*Macros, "D")); 100 } 101 102 TEST_F(MacroExpanderTest, ExpandsWithArguments) { 103 auto Macros = create({ 104 "A(x)", 105 "B(x, y)=x + y", 106 }); 107 EXPECT_EQ("", expand(*Macros, "A", {"a"})); 108 EXPECT_EQ("b1+b2+b3", expand(*Macros, "B", {"b1", "b2 + b3"})); 109 EXPECT_EQ("x+", expand(*Macros, "B", {"x"})); 110 } 111 112 TEST_F(MacroExpanderTest, AttributizesTokens) { 113 auto Macros = create({ 114 "A(x, y)={ x + y; }", 115 "B(x, y)=x + 3 + y", 116 }); 117 auto *A = Lex.id("A"); 118 auto AArgs = lexArgs({"a1 * a2", "a3 * a4"}); 119 auto Result = Macros->expand(A, AArgs); 120 EXPECT_EQ(11U, Result.size()) << text(Result) << " / " << Result; 121 EXPECT_EQ("{a1*a2+a3*a4;}", text(Result)); 122 std::vector<MacroAttributes> Attributes = { 123 {tok::l_brace, MR_Hidden, 1, 0, {A}}, 124 {tok::identifier, MR_ExpandedArg, 0, 0, {A}}, 125 {tok::star, MR_ExpandedArg, 0, 0, {A}}, 126 {tok::identifier, MR_ExpandedArg, 0, 0, {A}}, 127 {tok::plus, MR_Hidden, 0, 0, {A}}, 128 {tok::identifier, MR_ExpandedArg, 0, 0, {A}}, 129 {tok::star, MR_ExpandedArg, 0, 0, {A}}, 130 {tok::identifier, MR_ExpandedArg, 0, 0, {A}}, 131 {tok::semi, MR_Hidden, 0, 0, {A}}, 132 {tok::r_brace, MR_Hidden, 0, 1, {A}}, 133 {tok::eof, MR_Hidden, 0, 0, {A}}, 134 }; 135 EXPECT_ATTRIBUTES(Result, Attributes); 136 137 auto *B = Lex.id("B"); 138 auto BArgs = lexArgs({"b1", "b2"}); 139 Result = Macros->expand(B, BArgs); 140 EXPECT_EQ(6U, Result.size()) << text(Result) << " / " << Result; 141 EXPECT_EQ("b1+3+b2", text(Result)); 142 Attributes = { 143 {tok::identifier, MR_ExpandedArg, 1, 0, {B}}, 144 {tok::plus, MR_Hidden, 0, 0, {B}}, 145 {tok::numeric_constant, MR_Hidden, 0, 0, {B}}, 146 {tok::plus, MR_Hidden, 0, 0, {B}}, 147 {tok::identifier, MR_ExpandedArg, 0, 1, {B}}, 148 {tok::eof, MR_Hidden, 0, 0, {B}}, 149 }; 150 EXPECT_ATTRIBUTES(Result, Attributes); 151 } 152 153 TEST_F(MacroExpanderTest, RecursiveExpansion) { 154 auto Macros = create({ 155 "A(x)=x", 156 "B(x)=x", 157 "C(x)=x", 158 }); 159 160 auto *A = Lex.id("A"); 161 auto *B = Lex.id("B"); 162 auto *C = Lex.id("C"); 163 164 auto Args = lexArgs({"id"}); 165 auto CResult = uneof(Macros->expand(C, Args)); 166 auto BResult = uneof(Macros->expand(B, CResult)); 167 auto AResult = uneof(Macros->expand(A, BResult)); 168 169 std::vector<MacroAttributes> Attributes = { 170 {tok::identifier, MR_ExpandedArg, 3, 3, {C, B, A}}, 171 }; 172 EXPECT_ATTRIBUTES(AResult, Attributes); 173 } 174 175 TEST_F(MacroExpanderTest, SingleExpansion) { 176 auto Macros = create({"A(x)=x+x"}); 177 auto *A = Lex.id("A"); 178 auto Args = lexArgs({"id"}); 179 auto Result = uneof(Macros->expand(A, Args)); 180 std::vector<MacroAttributes> Attributes = { 181 {tok::identifier, MR_ExpandedArg, 1, 0, {A}}, 182 {tok::plus, MR_Hidden, 0, 0, {A}}, 183 {tok::identifier, MR_Hidden, 0, 1, {A}}, 184 }; 185 EXPECT_ATTRIBUTES(Result, Attributes); 186 } 187 188 TEST_F(MacroExpanderTest, UnderstandsCppTokens) { 189 auto Macros = create({"A(T,name)=T name = 0;"}); 190 auto *A = Lex.id("A"); 191 auto Args = lexArgs({"const int", "x"}); 192 auto Result = uneof(Macros->expand(A, Args)); 193 std::vector<MacroAttributes> Attributes = { 194 {tok::kw_const, MR_ExpandedArg, 1, 0, {A}}, 195 {tok::kw_int, MR_ExpandedArg, 0, 0, {A}}, 196 {tok::identifier, MR_ExpandedArg, 0, 0, {A}}, 197 {tok::equal, MR_Hidden, 0, 0, {A}}, 198 {tok::numeric_constant, MR_Hidden, 0, 0, {A}}, 199 {tok::semi, MR_Hidden, 0, 1, {A}}, 200 }; 201 EXPECT_ATTRIBUTES(Result, Attributes); 202 } 203 204 } // namespace 205 } // namespace format 206 } // namespace clang 207