| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | #include "clang/Lex/Lexer.h" |
| 10 | #include "clang/Basic/Diagnostic.h" |
| 11 | #include "clang/Basic/DiagnosticOptions.h" |
| 12 | #include "clang/Basic/FileManager.h" |
| 13 | #include "clang/Basic/LangOptions.h" |
| 14 | #include "clang/Basic/SourceManager.h" |
| 15 | #include "clang/Basic/TargetInfo.h" |
| 16 | #include "clang/Basic/TargetOptions.h" |
| 17 | #include "clang/Lex/HeaderSearch.h" |
| 18 | #include "clang/Lex/HeaderSearchOptions.h" |
| 19 | #include "clang/Lex/MacroArgs.h" |
| 20 | #include "clang/Lex/MacroInfo.h" |
| 21 | #include "clang/Lex/ModuleLoader.h" |
| 22 | #include "clang/Lex/Preprocessor.h" |
| 23 | #include "clang/Lex/PreprocessorOptions.h" |
| 24 | #include "gtest/gtest.h" |
| 25 | |
| 26 | using namespace clang; |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | |
| 31 | class LexerTest : public ::testing::Test { |
| 32 | protected: |
| 33 | LexerTest() |
| 34 | : FileMgr(FileMgrOpts), |
| 35 | DiagID(new DiagnosticIDs()), |
| 36 | Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()), |
| 37 | SourceMgr(Diags, FileMgr), |
| 38 | TargetOpts(new TargetOptions) |
| 39 | { |
| 40 | TargetOpts->Triple = "x86_64-apple-darwin11.1.0"; |
| 41 | Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts); |
| 42 | } |
| 43 | |
| 44 | std::unique_ptr<Preprocessor> CreatePP(StringRef Source, |
| 45 | TrivialModuleLoader &ModLoader) { |
| 46 | std::unique_ptr<llvm::MemoryBuffer> Buf = |
| 47 | llvm::MemoryBuffer::getMemBuffer(Source); |
| 48 | SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf))); |
| 49 | |
| 50 | HeaderSearch (std::make_shared<HeaderSearchOptions>(), SourceMgr, |
| 51 | Diags, LangOpts, Target.get()); |
| 52 | std::unique_ptr<Preprocessor> PP = llvm::make_unique<Preprocessor>( |
| 53 | std::make_shared<PreprocessorOptions>(), Diags, LangOpts, SourceMgr, |
| 54 | HeaderInfo, ModLoader, |
| 55 | , |
| 56 | ); |
| 57 | PP->Initialize(*Target); |
| 58 | PP->EnterMainSourceFile(); |
| 59 | return PP; |
| 60 | } |
| 61 | |
| 62 | std::vector<Token> Lex(StringRef Source) { |
| 63 | TrivialModuleLoader ModLoader; |
| 64 | auto PP = CreatePP(Source, ModLoader); |
| 65 | |
| 66 | std::vector<Token> toks; |
| 67 | while (1) { |
| 68 | Token tok; |
| 69 | PP->Lex(tok); |
| 70 | if (tok.is(tok::eof)) |
| 71 | break; |
| 72 | toks.push_back(tok); |
| 73 | } |
| 74 | |
| 75 | return toks; |
| 76 | } |
| 77 | |
| 78 | std::vector<Token> CheckLex(StringRef Source, |
| 79 | ArrayRef<tok::TokenKind> ExpectedTokens) { |
| 80 | auto toks = Lex(Source); |
| 81 | EXPECT_EQ(ExpectedTokens.size(), toks.size()); |
| 82 | for (unsigned i = 0, e = ExpectedTokens.size(); i != e; ++i) { |
| 83 | EXPECT_EQ(ExpectedTokens[i], toks[i].getKind()); |
| 84 | } |
| 85 | |
| 86 | return toks; |
| 87 | } |
| 88 | |
| 89 | std::string getSourceText(Token Begin, Token End) { |
| 90 | bool Invalid; |
| 91 | StringRef Str = |
| 92 | Lexer::getSourceText(CharSourceRange::getTokenRange(SourceRange( |
| 93 | Begin.getLocation(), End.getLocation())), |
| 94 | SourceMgr, LangOpts, &Invalid); |
| 95 | if (Invalid) |
| 96 | return "<INVALID>"; |
| 97 | return Str; |
| 98 | } |
| 99 | |
| 100 | FileSystemOptions FileMgrOpts; |
| 101 | FileManager FileMgr; |
| 102 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID; |
| 103 | DiagnosticsEngine Diags; |
| 104 | SourceManager SourceMgr; |
| 105 | LangOptions LangOpts; |
| 106 | std::shared_ptr<TargetOptions> TargetOpts; |
| 107 | IntrusiveRefCntPtr<TargetInfo> Target; |
| 108 | }; |
| 109 | |
| 110 | TEST_F(LexerTest, GetSourceTextExpandsToMaximumInMacroArgument) { |
| 111 | std::vector<tok::TokenKind> ExpectedTokens; |
| 112 | ExpectedTokens.push_back(tok::identifier); |
| 113 | ExpectedTokens.push_back(tok::l_paren); |
| 114 | ExpectedTokens.push_back(tok::identifier); |
| 115 | ExpectedTokens.push_back(tok::r_paren); |
| 116 | |
| 117 | std::vector<Token> toks = CheckLex("#define M(x) x\n" |
| 118 | "M(f(M(i)))", |
| 119 | ExpectedTokens); |
| 120 | |
| 121 | EXPECT_EQ("M(i)", getSourceText(toks[2], toks[2])); |
| 122 | } |
| 123 | |
| 124 | TEST_F(LexerTest, GetSourceTextExpandsToMaximumInMacroArgumentForEndOfMacro) { |
| 125 | std::vector<tok::TokenKind> ExpectedTokens; |
| 126 | ExpectedTokens.push_back(tok::identifier); |
| 127 | ExpectedTokens.push_back(tok::identifier); |
| 128 | |
| 129 | std::vector<Token> toks = CheckLex("#define M(x) x\n" |
| 130 | "M(M(i) c)", |
| 131 | ExpectedTokens); |
| 132 | |
| 133 | EXPECT_EQ("M(i)", getSourceText(toks[0], toks[0])); |
| 134 | } |
| 135 | |
| 136 | TEST_F(LexerTest, GetSourceTextExpandsInMacroArgumentForBeginOfMacro) { |
| 137 | std::vector<tok::TokenKind> ExpectedTokens; |
| 138 | ExpectedTokens.push_back(tok::identifier); |
| 139 | ExpectedTokens.push_back(tok::identifier); |
| 140 | ExpectedTokens.push_back(tok::identifier); |
| 141 | |
| 142 | std::vector<Token> toks = CheckLex("#define M(x) x\n" |
| 143 | "M(c c M(i))", |
| 144 | ExpectedTokens); |
| 145 | |
| 146 | EXPECT_EQ("c M(i)", getSourceText(toks[1], toks[2])); |
| 147 | } |
| 148 | |
| 149 | TEST_F(LexerTest, GetSourceTextExpandsInMacroArgumentForEndOfMacro) { |
| 150 | std::vector<tok::TokenKind> ExpectedTokens; |
| 151 | ExpectedTokens.push_back(tok::identifier); |
| 152 | ExpectedTokens.push_back(tok::identifier); |
| 153 | ExpectedTokens.push_back(tok::identifier); |
| 154 | |
| 155 | std::vector<Token> toks = CheckLex("#define M(x) x\n" |
| 156 | "M(M(i) c c)", |
| 157 | ExpectedTokens); |
| 158 | |
| 159 | EXPECT_EQ("M(i) c", getSourceText(toks[0], toks[1])); |
| 160 | } |
| 161 | |
| 162 | TEST_F(LexerTest, GetSourceTextInSeparateFnMacros) { |
| 163 | std::vector<tok::TokenKind> ExpectedTokens; |
| 164 | ExpectedTokens.push_back(tok::identifier); |
| 165 | ExpectedTokens.push_back(tok::identifier); |
| 166 | ExpectedTokens.push_back(tok::identifier); |
| 167 | ExpectedTokens.push_back(tok::identifier); |
| 168 | |
| 169 | std::vector<Token> toks = CheckLex("#define M(x) x\n" |
| 170 | "M(c M(i)) M(M(i) c)", |
| 171 | ExpectedTokens); |
| 172 | |
| 173 | EXPECT_EQ("<INVALID>", getSourceText(toks[1], toks[2])); |
| 174 | } |
| 175 | |
| 176 | TEST_F(LexerTest, GetSourceTextWorksAcrossTokenPastes) { |
| 177 | std::vector<tok::TokenKind> ExpectedTokens; |
| 178 | ExpectedTokens.push_back(tok::identifier); |
| 179 | ExpectedTokens.push_back(tok::l_paren); |
| 180 | ExpectedTokens.push_back(tok::identifier); |
| 181 | ExpectedTokens.push_back(tok::r_paren); |
| 182 | |
| 183 | std::vector<Token> toks = CheckLex("#define M(x) x\n" |
| 184 | "#define C(x) M(x##c)\n" |
| 185 | "M(f(C(i)))", |
| 186 | ExpectedTokens); |
| 187 | |
| 188 | EXPECT_EQ("C(i)", getSourceText(toks[2], toks[2])); |
| 189 | } |
| 190 | |
| 191 | TEST_F(LexerTest, GetSourceTextExpandsAcrossMultipleMacroCalls) { |
| 192 | std::vector<tok::TokenKind> ExpectedTokens; |
| 193 | ExpectedTokens.push_back(tok::identifier); |
| 194 | ExpectedTokens.push_back(tok::l_paren); |
| 195 | ExpectedTokens.push_back(tok::identifier); |
| 196 | ExpectedTokens.push_back(tok::r_paren); |
| 197 | |
| 198 | std::vector<Token> toks = CheckLex("#define M(x) x\n" |
| 199 | "f(M(M(i)))", |
| 200 | ExpectedTokens); |
| 201 | EXPECT_EQ("M(M(i))", getSourceText(toks[2], toks[2])); |
| 202 | } |
| 203 | |
| 204 | TEST_F(LexerTest, GetSourceTextInMiddleOfMacroArgument) { |
| 205 | std::vector<tok::TokenKind> ExpectedTokens; |
| 206 | ExpectedTokens.push_back(tok::identifier); |
| 207 | ExpectedTokens.push_back(tok::l_paren); |
| 208 | ExpectedTokens.push_back(tok::identifier); |
| 209 | ExpectedTokens.push_back(tok::r_paren); |
| 210 | |
| 211 | std::vector<Token> toks = CheckLex("#define M(x) x\n" |
| 212 | "M(f(i))", |
| 213 | ExpectedTokens); |
| 214 | EXPECT_EQ("i", getSourceText(toks[2], toks[2])); |
| 215 | } |
| 216 | |
| 217 | TEST_F(LexerTest, GetSourceTextExpandsAroundDifferentMacroCalls) { |
| 218 | std::vector<tok::TokenKind> ExpectedTokens; |
| 219 | ExpectedTokens.push_back(tok::identifier); |
| 220 | ExpectedTokens.push_back(tok::l_paren); |
| 221 | ExpectedTokens.push_back(tok::identifier); |
| 222 | ExpectedTokens.push_back(tok::r_paren); |
| 223 | |
| 224 | std::vector<Token> toks = CheckLex("#define M(x) x\n" |
| 225 | "#define C(x) x\n" |
| 226 | "f(C(M(i)))", |
| 227 | ExpectedTokens); |
| 228 | EXPECT_EQ("C(M(i))", getSourceText(toks[2], toks[2])); |
| 229 | } |
| 230 | |
| 231 | TEST_F(LexerTest, GetSourceTextOnlyExpandsIfFirstTokenInMacro) { |
| 232 | std::vector<tok::TokenKind> ExpectedTokens; |
| 233 | ExpectedTokens.push_back(tok::identifier); |
| 234 | ExpectedTokens.push_back(tok::l_paren); |
| 235 | ExpectedTokens.push_back(tok::identifier); |
| 236 | ExpectedTokens.push_back(tok::identifier); |
| 237 | ExpectedTokens.push_back(tok::r_paren); |
| 238 | |
| 239 | std::vector<Token> toks = CheckLex("#define M(x) x\n" |
| 240 | "#define C(x) c x\n" |
| 241 | "f(C(M(i)))", |
| 242 | ExpectedTokens); |
| 243 | EXPECT_EQ("M(i)", getSourceText(toks[3], toks[3])); |
| 244 | } |
| 245 | |
| 246 | TEST_F(LexerTest, GetSourceTextExpandsRecursively) { |
| 247 | std::vector<tok::TokenKind> ExpectedTokens; |
| 248 | ExpectedTokens.push_back(tok::identifier); |
| 249 | ExpectedTokens.push_back(tok::identifier); |
| 250 | ExpectedTokens.push_back(tok::l_paren); |
| 251 | ExpectedTokens.push_back(tok::identifier); |
| 252 | ExpectedTokens.push_back(tok::r_paren); |
| 253 | |
| 254 | std::vector<Token> toks = CheckLex("#define M(x) x\n" |
| 255 | "#define C(x) c M(x)\n" |
| 256 | "C(f(M(i)))", |
| 257 | ExpectedTokens); |
| 258 | EXPECT_EQ("M(i)", getSourceText(toks[3], toks[3])); |
| 259 | } |
| 260 | |
| 261 | TEST_F(LexerTest, LexAPI) { |
| 262 | std::vector<tok::TokenKind> ExpectedTokens; |
| 263 | ExpectedTokens.push_back(tok::l_square); |
| 264 | ExpectedTokens.push_back(tok::identifier); |
| 265 | ExpectedTokens.push_back(tok::r_square); |
| 266 | ExpectedTokens.push_back(tok::l_square); |
| 267 | ExpectedTokens.push_back(tok::identifier); |
| 268 | ExpectedTokens.push_back(tok::r_square); |
| 269 | ExpectedTokens.push_back(tok::identifier); |
| 270 | ExpectedTokens.push_back(tok::identifier); |
| 271 | ExpectedTokens.push_back(tok::identifier); |
| 272 | ExpectedTokens.push_back(tok::identifier); |
| 273 | |
| 274 | std::vector<Token> toks = CheckLex("#define M(x) [x]\n" |
| 275 | "#define N(x) x\n" |
| 276 | "#define INN(x) x\n" |
| 277 | "#define NOF1 INN(val)\n" |
| 278 | "#define NOF2 val\n" |
| 279 | "M(foo) N([bar])\n" |
| 280 | "N(INN(val)) N(NOF1) N(NOF2) N(val)", |
| 281 | ExpectedTokens); |
| 282 | |
| 283 | SourceLocation lsqrLoc = toks[0].getLocation(); |
| 284 | SourceLocation idLoc = toks[1].getLocation(); |
| 285 | SourceLocation rsqrLoc = toks[2].getLocation(); |
| 286 | CharSourceRange macroRange = SourceMgr.getExpansionRange(lsqrLoc); |
| 287 | |
| 288 | SourceLocation Loc; |
| 289 | EXPECT_TRUE(Lexer::isAtStartOfMacroExpansion(lsqrLoc, SourceMgr, LangOpts, &Loc)); |
| 290 | EXPECT_EQ(Loc, macroRange.getBegin()); |
| 291 | EXPECT_FALSE(Lexer::isAtStartOfMacroExpansion(idLoc, SourceMgr, LangOpts)); |
| 292 | EXPECT_FALSE(Lexer::isAtEndOfMacroExpansion(idLoc, SourceMgr, LangOpts)); |
| 293 | EXPECT_TRUE(Lexer::isAtEndOfMacroExpansion(rsqrLoc, SourceMgr, LangOpts, &Loc)); |
| 294 | EXPECT_EQ(Loc, macroRange.getEnd()); |
| 295 | EXPECT_TRUE(macroRange.isTokenRange()); |
| 296 | |
| 297 | CharSourceRange range = Lexer::makeFileCharRange( |
| 298 | CharSourceRange::getTokenRange(lsqrLoc, idLoc), SourceMgr, LangOpts); |
| 299 | EXPECT_TRUE(range.isInvalid()); |
| 300 | range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(idLoc, rsqrLoc), |
| 301 | SourceMgr, LangOpts); |
| 302 | EXPECT_TRUE(range.isInvalid()); |
| 303 | range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(lsqrLoc, rsqrLoc), |
| 304 | SourceMgr, LangOpts); |
| 305 | EXPECT_TRUE(!range.isTokenRange()); |
| 306 | EXPECT_EQ(range.getAsRange(), |
| 307 | SourceRange(macroRange.getBegin(), |
| 308 | macroRange.getEnd().getLocWithOffset(1))); |
| 309 | |
| 310 | StringRef text = Lexer::getSourceText( |
| 311 | CharSourceRange::getTokenRange(lsqrLoc, rsqrLoc), |
| 312 | SourceMgr, LangOpts); |
| 313 | EXPECT_EQ(text, "M(foo)"); |
| 314 | |
| 315 | SourceLocation macroLsqrLoc = toks[3].getLocation(); |
| 316 | SourceLocation macroIdLoc = toks[4].getLocation(); |
| 317 | SourceLocation macroRsqrLoc = toks[5].getLocation(); |
| 318 | SourceLocation fileLsqrLoc = SourceMgr.getSpellingLoc(macroLsqrLoc); |
| 319 | SourceLocation fileIdLoc = SourceMgr.getSpellingLoc(macroIdLoc); |
| 320 | SourceLocation fileRsqrLoc = SourceMgr.getSpellingLoc(macroRsqrLoc); |
| 321 | |
| 322 | range = Lexer::makeFileCharRange( |
| 323 | CharSourceRange::getTokenRange(macroLsqrLoc, macroIdLoc), |
| 324 | SourceMgr, LangOpts); |
| 325 | EXPECT_EQ(SourceRange(fileLsqrLoc, fileIdLoc.getLocWithOffset(3)), |
| 326 | range.getAsRange()); |
| 327 | |
| 328 | range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(macroIdLoc, macroRsqrLoc), |
| 329 | SourceMgr, LangOpts); |
| 330 | EXPECT_EQ(SourceRange(fileIdLoc, fileRsqrLoc.getLocWithOffset(1)), |
| 331 | range.getAsRange()); |
| 332 | |
| 333 | macroRange = SourceMgr.getExpansionRange(macroLsqrLoc); |
| 334 | range = Lexer::makeFileCharRange( |
| 335 | CharSourceRange::getTokenRange(macroLsqrLoc, macroRsqrLoc), |
| 336 | SourceMgr, LangOpts); |
| 337 | EXPECT_EQ(SourceRange(macroRange.getBegin(), macroRange.getEnd().getLocWithOffset(1)), |
| 338 | range.getAsRange()); |
| 339 | |
| 340 | text = Lexer::getSourceText( |
| 341 | CharSourceRange::getTokenRange(SourceRange(macroLsqrLoc, macroIdLoc)), |
| 342 | SourceMgr, LangOpts); |
| 343 | EXPECT_EQ(text, "[bar"); |
| 344 | |
| 345 | |
| 346 | SourceLocation idLoc1 = toks[6].getLocation(); |
| 347 | SourceLocation idLoc2 = toks[7].getLocation(); |
| 348 | SourceLocation idLoc3 = toks[8].getLocation(); |
| 349 | SourceLocation idLoc4 = toks[9].getLocation(); |
| 350 | EXPECT_EQ("INN", Lexer::getImmediateMacroName(idLoc1, SourceMgr, LangOpts)); |
| 351 | EXPECT_EQ("INN", Lexer::getImmediateMacroName(idLoc2, SourceMgr, LangOpts)); |
| 352 | EXPECT_EQ("NOF2", Lexer::getImmediateMacroName(idLoc3, SourceMgr, LangOpts)); |
| 353 | EXPECT_EQ("N", Lexer::getImmediateMacroName(idLoc4, SourceMgr, LangOpts)); |
| 354 | } |
| 355 | |
| 356 | TEST_F(LexerTest, DontMergeMacroArgsFromDifferentMacroFiles) { |
| 357 | std::vector<Token> toks = |
| 358 | Lex("#define helper1 0\n" |
| 359 | "void helper2(const char *, ...);\n" |
| 360 | "#define M1(a, ...) helper2(a, ##__VA_ARGS__)\n" |
| 361 | "#define M2(a, ...) M1(a, helper1, ##__VA_ARGS__)\n" |
| 362 | "void f1() { M2(\"a\", \"b\"); }"); |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | SourceLocation helper1ArgLoc = toks[20].getLocation(); |
| 370 | EXPECT_EQ(SourceMgr.getFileIDSize(SourceMgr.getFileID(helper1ArgLoc)), 8U); |
| 371 | } |
| 372 | |
| 373 | TEST_F(LexerTest, DontOverallocateStringifyArgs) { |
| 374 | TrivialModuleLoader ModLoader; |
| 375 | auto PP = CreatePP("\"StrArg\", 5, 'C'", ModLoader); |
| 376 | |
| 377 | llvm::BumpPtrAllocator Allocator; |
| 378 | std::array<IdentifierInfo *, 3> ParamList; |
| 379 | MacroInfo *MI = PP->AllocateMacroInfo({}); |
| 380 | MI->setIsFunctionLike(); |
| 381 | MI->setParameterList(ParamList, Allocator); |
| 382 | EXPECT_EQ(3u, MI->getNumParams()); |
| 383 | EXPECT_TRUE(MI->isFunctionLike()); |
| 384 | |
| 385 | Token Eof; |
| 386 | Eof.setKind(tok::eof); |
| 387 | std::vector<Token> ArgTokens; |
| 388 | while (1) { |
| 389 | Token tok; |
| 390 | PP->Lex(tok); |
| 391 | if (tok.is(tok::eof)) { |
| 392 | ArgTokens.push_back(Eof); |
| 393 | break; |
| 394 | } |
| 395 | if (tok.is(tok::comma)) |
| 396 | ArgTokens.push_back(Eof); |
| 397 | else |
| 398 | ArgTokens.push_back(tok); |
| 399 | } |
| 400 | |
| 401 | auto MacroArgsDeleter = [&PP](MacroArgs *M) { M->destroy(*PP); }; |
| 402 | std::unique_ptr<MacroArgs, decltype(MacroArgsDeleter)> MA( |
| 403 | MacroArgs::create(MI, ArgTokens, false, *PP), MacroArgsDeleter); |
| 404 | Token Result = MA->getStringifiedArgument(0, *PP, {}, {}); |
| 405 | EXPECT_EQ(tok::string_literal, Result.getKind()); |
| 406 | EXPECT_STREQ("\"\\\"StrArg\\\"\"", Result.getLiteralData()); |
| 407 | Result = MA->getStringifiedArgument(1, *PP, {}, {}); |
| 408 | EXPECT_EQ(tok::string_literal, Result.getKind()); |
| 409 | EXPECT_STREQ("\"5\"", Result.getLiteralData()); |
| 410 | Result = MA->getStringifiedArgument(2, *PP, {}, {}); |
| 411 | EXPECT_EQ(tok::string_literal, Result.getKind()); |
| 412 | EXPECT_STREQ("\"'C'\"", Result.getLiteralData()); |
| 413 | #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST |
| 414 | EXPECT_DEATH(MA->getStringifiedArgument(3, *PP, {}, {}), |
| 415 | "Invalid argument number!"); |
| 416 | #endif |
| 417 | } |
| 418 | |
| 419 | TEST_F(LexerTest, IsNewLineEscapedValid) { |
| 420 | auto hasNewLineEscaped = [](const char *S) { |
| 421 | return Lexer::isNewLineEscaped(S, S + strlen(S) - 1); |
| 422 | }; |
| 423 | |
| 424 | EXPECT_TRUE(hasNewLineEscaped("\\\r")); |
| 425 | EXPECT_TRUE(hasNewLineEscaped("\\\n")); |
| 426 | EXPECT_TRUE(hasNewLineEscaped("\\\r\n")); |
| 427 | EXPECT_TRUE(hasNewLineEscaped("\\\n\r")); |
| 428 | EXPECT_TRUE(hasNewLineEscaped("\\ \t\v\f\r")); |
| 429 | EXPECT_TRUE(hasNewLineEscaped("\\ \t\v\f\r\n")); |
| 430 | |
| 431 | EXPECT_FALSE(hasNewLineEscaped("\\\r\r")); |
| 432 | EXPECT_FALSE(hasNewLineEscaped("\\\r\r\n")); |
| 433 | EXPECT_FALSE(hasNewLineEscaped("\\\n\n")); |
| 434 | EXPECT_FALSE(hasNewLineEscaped("\r")); |
| 435 | EXPECT_FALSE(hasNewLineEscaped("\n")); |
| 436 | EXPECT_FALSE(hasNewLineEscaped("\r\n")); |
| 437 | EXPECT_FALSE(hasNewLineEscaped("\n\r")); |
| 438 | EXPECT_FALSE(hasNewLineEscaped("\r\r")); |
| 439 | EXPECT_FALSE(hasNewLineEscaped("\n\n")); |
| 440 | } |
| 441 | |
| 442 | TEST_F(LexerTest, GetBeginningOfTokenWithEscapedNewLine) { |
| 443 | |
| 444 | |
| 445 | const unsigned IdentifierLength = 8; |
| 446 | std::string TextToLex = "rabarbar\n" |
| 447 | "foo\\\nbar\n" |
| 448 | "foo\\\rbar\n" |
| 449 | "fo\\\r\nbar\n" |
| 450 | "foo\\\n\rba\n"; |
| 451 | std::vector<tok::TokenKind> ExpectedTokens{5, tok::identifier}; |
| 452 | std::vector<Token> LexedTokens = CheckLex(TextToLex, ExpectedTokens); |
| 453 | |
| 454 | for (const Token &Tok : LexedTokens) { |
| 455 | std::pair<FileID, unsigned> OriginalLocation = |
| 456 | SourceMgr.getDecomposedLoc(Tok.getLocation()); |
| 457 | for (unsigned Offset = 0; Offset < IdentifierLength; ++Offset) { |
| 458 | SourceLocation LookupLocation = |
| 459 | Tok.getLocation().getLocWithOffset(Offset); |
| 460 | |
| 461 | std::pair<FileID, unsigned> FoundLocation = |
| 462 | SourceMgr.getDecomposedExpansionLoc( |
| 463 | Lexer::GetBeginningOfToken(LookupLocation, SourceMgr, LangOpts)); |
| 464 | |
| 465 | |
| 466 | |
| 467 | EXPECT_EQ(FoundLocation.second, OriginalLocation.second); |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | TEST_F(LexerTest, AvoidPastEndOfStringDereference) { |
| 473 | EXPECT_TRUE(Lex(" // \\\n").empty()); |
| 474 | EXPECT_TRUE(Lex("#include <\\\\").empty()); |
| 475 | EXPECT_TRUE(Lex("#include <\\\\\n").empty()); |
| 476 | } |
| 477 | |
| 478 | TEST_F(LexerTest, StringizingRasString) { |
| 479 | |
| 480 | std::string String1 = R"(foo |
| 481 | {"bar":[]} |
| 482 | baz)"; |
| 483 | |
| 484 | SmallString<128> String2; |
| 485 | String2 += String1.c_str(); |
| 486 | |
| 487 | |
| 488 | std::string String3 = R"(\ |
| 489 | \n |
| 490 | \\n |
| 491 | \\)"; |
| 492 | SmallString<128> String4; |
| 493 | String4 += String3.c_str(); |
| 494 | std::string String5 = R"(a\ |
| 495 | |
| 496 | |
| 497 | \\b)"; |
| 498 | SmallString<128> String6; |
| 499 | String6 += String5.c_str(); |
| 500 | |
| 501 | String1 = Lexer::Stringify(StringRef(String1)); |
| 502 | Lexer::Stringify(String2); |
| 503 | String3 = Lexer::Stringify(StringRef(String3)); |
| 504 | Lexer::Stringify(String4); |
| 505 | String5 = Lexer::Stringify(StringRef(String5)); |
| 506 | Lexer::Stringify(String6); |
| 507 | |
| 508 | EXPECT_EQ(String1, R"(foo\n {\"bar\":[]}\n baz)"); |
| 509 | EXPECT_EQ(String2, R"(foo\n {\"bar\":[]}\n baz)"); |
| 510 | EXPECT_EQ(String3, R"(\\\n \\n\n \\\\n\n \\\\)"); |
| 511 | EXPECT_EQ(String4, R"(\\\n \\n\n \\\\n\n \\\\)"); |
| 512 | EXPECT_EQ(String5, R"(a\\\n\n\n \\\\b)"); |
| 513 | EXPECT_EQ(String6, R"(a\\\n\n\n \\\\b)"); |
| 514 | } |
| 515 | |
| 516 | } |
| 517 | |