1 //===--- TokenTest.cpp ----------------------------------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang-pseudo/Token.h" 10 #include "clang/Basic/LangOptions.h" 11 #include "clang/Basic/TokenKinds.h" 12 #include "gmock/gmock.h" 13 #include "gtest/gtest.h" 14 15 namespace clang { 16 namespace pseudo { 17 namespace { 18 19 using testing::AllOf; 20 using testing::ElementsAre; 21 using testing::ElementsAreArray; 22 using testing::Not; 23 24 MATCHER_P2(token, Text, Kind, "") { 25 return arg.Kind == Kind && arg.text() == Text; 26 } 27 28 MATCHER_P(hasFlag, Flag, "") { return arg.flag(Flag); } 29 30 MATCHER_P2(lineIndent, Line, Indent, "") { 31 return arg.Line == (unsigned)Line && arg.Indent == (unsigned)Indent; 32 } 33 34 TEST(TokenTest, Lex) { 35 LangOptions Opts; 36 std::string Code = R"cpp( 37 #include <stdio.h> 38 int main() { 39 return 42; // the answer 40 } 41 )cpp"; 42 TokenStream Raw = lex(Code, Opts); 43 ASSERT_TRUE(Raw.isFinalized()); 44 EXPECT_THAT(Raw.tokens(), 45 ElementsAreArray({ 46 // Lexing of directives is weird, especially <angled> strings. 47 token("#", tok::hash), 48 token("include", tok::raw_identifier), 49 token("<", tok::less), 50 token("stdio", tok::raw_identifier), 51 token(".", tok::period), 52 token("h", tok::raw_identifier), 53 token(">", tok::greater), 54 55 token("int", tok::raw_identifier), 56 token("main", tok::raw_identifier), 57 token("(", tok::l_paren), 58 token(")", tok::r_paren), 59 token("{", tok::l_brace), 60 token("return", tok::raw_identifier), 61 token("42", tok::numeric_constant), 62 token(";", tok::semi), 63 token("// the answer", tok::comment), 64 token("}", tok::r_brace), 65 })); 66 67 TokenStream Cooked = cook(Raw, Opts); 68 ASSERT_TRUE(Cooked.isFinalized()); 69 EXPECT_THAT(Cooked.tokens(), 70 ElementsAreArray({ 71 // Cooked identifier types in directives are not meaningful. 72 token("#", tok::hash), 73 token("include", tok::identifier), 74 token("<", tok::less), 75 token("stdio", tok::identifier), 76 token(".", tok::period), 77 token("h", tok::identifier), 78 token(">", tok::greater), 79 80 token("int", tok::kw_int), 81 token("main", tok::identifier), 82 token("(", tok::l_paren), 83 token(")", tok::r_paren), 84 token("{", tok::l_brace), 85 token("return", tok::kw_return), 86 token("42", tok::numeric_constant), 87 token(";", tok::semi), 88 token("// the answer", tok::comment), 89 token("}", tok::r_brace), 90 })); 91 // Check raw tokens point back into original source code. 92 EXPECT_EQ(Raw.tokens().front().text().begin(), &Code[Code.find('#')]); 93 } 94 95 TEST(TokenTest, LineContinuation) { 96 LangOptions Opts; 97 std::string Code = R"cpp( 98 one_\ 99 token 100 two \ 101 tokens 102 )cpp"; 103 TokenStream Raw = lex(Code, Opts); 104 EXPECT_THAT( 105 Raw.tokens(), 106 ElementsAre(AllOf(token("one_\\\ntoken", tok::raw_identifier), 107 hasFlag(LexFlags::StartsPPLine), 108 hasFlag(LexFlags::NeedsCleaning), lineIndent(1, 0)), 109 AllOf(token("two", tok::raw_identifier), 110 hasFlag(LexFlags::StartsPPLine), 111 Not(hasFlag(LexFlags::NeedsCleaning))), 112 AllOf(token("\\\ntokens", tok::raw_identifier), 113 Not(hasFlag(LexFlags::StartsPPLine)), 114 hasFlag(LexFlags::NeedsCleaning)))); 115 116 TokenStream Cooked = cook(Raw, Opts); 117 EXPECT_THAT( 118 Cooked.tokens(), 119 ElementsAre(AllOf(token("one_token", tok::identifier), lineIndent(1, 0)), 120 token("two", tok::identifier), 121 token("tokens", tok::identifier))); 122 } 123 124 TEST(TokenTest, EncodedCharacters) { 125 LangOptions Opts; 126 Opts.Trigraphs = true; 127 Opts.Digraphs = true; 128 Opts.C99 = true; // UCNs 129 Opts.CXXOperatorNames = true; 130 std::string Code = R"(and <: ??! '??=' \u00E9)"; 131 TokenStream Raw = lex(Code, Opts); 132 EXPECT_THAT( 133 Raw.tokens(), 134 ElementsAre( // and is not recognized as && until cook(). 135 AllOf(token("and", tok::raw_identifier), 136 Not(hasFlag(LexFlags::NeedsCleaning))), 137 // Digraphs are just different spellings of tokens. 138 AllOf(token("<:", tok::l_square), 139 Not(hasFlag(LexFlags::NeedsCleaning))), 140 // Trigraps are interpreted, still need text cleaning. 141 AllOf(token(R"(??!)", tok::pipe), hasFlag(LexFlags::NeedsCleaning)), 142 // Trigraphs must be substituted inside constants too. 143 AllOf(token(R"('??=')", tok::char_constant), 144 hasFlag(LexFlags::NeedsCleaning)), 145 // UCNs need substitution. 146 AllOf(token(R"(\u00E9)", tok::raw_identifier), 147 hasFlag(LexFlags::NeedsCleaning)))); 148 149 TokenStream Cooked = cook(Raw, Opts); 150 EXPECT_THAT( 151 Cooked.tokens(), 152 ElementsAre(token("and", tok::ampamp), // alternate spelling recognized 153 token("<:", tok::l_square), 154 token("|", tok::pipe), // trigraph substituted 155 token("'#'", tok::char_constant), // trigraph substituted 156 token("é", tok::identifier))); // UCN substituted 157 } 158 159 TEST(TokenTest, Indentation) { 160 LangOptions Opts; 161 std::string Code = R"cpp( hello world 162 no_indent \ 163 line_was_continued 164 )cpp"; 165 TokenStream Raw = lex(Code, Opts); 166 EXPECT_THAT(Raw.tokens(), ElementsAreArray({ 167 lineIndent(0, 3), // hello 168 lineIndent(0, 3), // world 169 lineIndent(1, 0), // no_indent 170 lineIndent(2, 2), // line_was_continued 171 })); 172 } 173 174 TEST(TokenTest, SplitGreaterGreater) { 175 LangOptions Opts; 176 std::string Code = R"cpp( 177 >> // split 178 // >> with an escaped newline in the middle, split 179 >\ 180 > 181 >>= // not split 182 )cpp"; 183 TokenStream Cook = cook(lex(Code, Opts), Opts); 184 TokenStream Split = stripComments(Cook); 185 EXPECT_THAT(Split.tokens(), ElementsAreArray({ 186 token(">", tok::greater), 187 token(">", tok::greater), 188 token(">", tok::greater), 189 token(">", tok::greater), 190 token(">>=", tok::greatergreaterequal), 191 })); 192 } 193 194 TEST(TokenTest, DropComments) { 195 LangOptions Opts; 196 std::string Code = R"cpp( 197 // comment 198 int /*abc*/; 199 )cpp"; 200 TokenStream Raw = cook(lex(Code, Opts), Opts); 201 TokenStream Stripped = stripComments(Raw); 202 EXPECT_THAT(Raw.tokens(), 203 ElementsAreArray( 204 {token("// comment", tok::comment), token("int", tok::kw_int), 205 token("/*abc*/", tok::comment), token(";", tok::semi)})); 206 207 EXPECT_THAT(Stripped.tokens(), ElementsAreArray({token("int", tok::kw_int), 208 token(";", tok::semi)})); 209 } 210 211 } // namespace 212 } // namespace pseudo 213 } // namespace clang 214