1 //===- unittests/Basic/SourceManagerTest.cpp ------ SourceManager tests ---===// 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/Basic/SourceManager.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/TargetInfo.h" 15 #include "clang/Basic/TargetOptions.h" 16 #include "clang/Lex/HeaderSearch.h" 17 #include "clang/Lex/HeaderSearchOptions.h" 18 #include "clang/Lex/ModuleLoader.h" 19 #include "clang/Lex/Preprocessor.h" 20 #include "clang/Lex/PreprocessorOptions.h" 21 #include "llvm/ADT/SmallString.h" 22 #include "llvm/Config/llvm-config.h" 23 #include "llvm/Support/Process.h" 24 #include "gtest/gtest.h" 25 #include <cstddef> 26 27 using namespace clang; 28 29 namespace { 30 31 // The test fixture. 32 class SourceManagerTest : public ::testing::Test { 33 protected: 34 SourceManagerTest() 35 : FileMgr(FileMgrOpts), 36 DiagID(new DiagnosticIDs()), 37 Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()), 38 SourceMgr(Diags, FileMgr), 39 TargetOpts(new TargetOptions) { 40 TargetOpts->Triple = "x86_64-apple-darwin11.1.0"; 41 Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts); 42 } 43 44 FileSystemOptions FileMgrOpts; 45 FileManager FileMgr; 46 IntrusiveRefCntPtr<DiagnosticIDs> DiagID; 47 DiagnosticsEngine Diags; 48 SourceManager SourceMgr; 49 LangOptions LangOpts; 50 std::shared_ptr<TargetOptions> TargetOpts; 51 IntrusiveRefCntPtr<TargetInfo> Target; 52 }; 53 54 TEST_F(SourceManagerTest, isBeforeInTranslationUnit) { 55 const char *source = 56 "#define M(x) [x]\n" 57 "M(foo)"; 58 std::unique_ptr<llvm::MemoryBuffer> Buf = 59 llvm::MemoryBuffer::getMemBuffer(source); 60 FileID mainFileID = SourceMgr.createFileID(std::move(Buf)); 61 SourceMgr.setMainFileID(mainFileID); 62 63 TrivialModuleLoader ModLoader; 64 HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr, 65 Diags, LangOpts, &*Target); 66 Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts, 67 SourceMgr, HeaderInfo, ModLoader, 68 /*IILookup =*/nullptr, 69 /*OwnsHeaderSearch =*/false); 70 PP.Initialize(*Target); 71 PP.EnterMainSourceFile(); 72 73 std::vector<Token> toks; 74 while (1) { 75 Token tok; 76 PP.Lex(tok); 77 if (tok.is(tok::eof)) 78 break; 79 toks.push_back(tok); 80 } 81 82 // Make sure we got the tokens that we expected. 83 ASSERT_EQ(3U, toks.size()); 84 ASSERT_EQ(tok::l_square, toks[0].getKind()); 85 ASSERT_EQ(tok::identifier, toks[1].getKind()); 86 ASSERT_EQ(tok::r_square, toks[2].getKind()); 87 88 SourceLocation lsqrLoc = toks[0].getLocation(); 89 SourceLocation idLoc = toks[1].getLocation(); 90 SourceLocation rsqrLoc = toks[2].getLocation(); 91 92 SourceLocation macroExpStartLoc = SourceMgr.translateLineCol(mainFileID, 2, 1); 93 SourceLocation macroExpEndLoc = SourceMgr.translateLineCol(mainFileID, 2, 6); 94 ASSERT_TRUE(macroExpStartLoc.isFileID()); 95 ASSERT_TRUE(macroExpEndLoc.isFileID()); 96 97 SmallString<32> str; 98 ASSERT_EQ("M", PP.getSpelling(macroExpStartLoc, str)); 99 ASSERT_EQ(")", PP.getSpelling(macroExpEndLoc, str)); 100 101 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(lsqrLoc, idLoc)); 102 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, rsqrLoc)); 103 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(macroExpStartLoc, idLoc)); 104 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, macroExpEndLoc)); 105 } 106 107 TEST_F(SourceManagerTest, getColumnNumber) { 108 const char *Source = 109 "int x;\n" 110 "int y;"; 111 112 std::unique_ptr<llvm::MemoryBuffer> Buf = 113 llvm::MemoryBuffer::getMemBuffer(Source); 114 FileID MainFileID = SourceMgr.createFileID(std::move(Buf)); 115 SourceMgr.setMainFileID(MainFileID); 116 117 bool Invalid; 118 119 Invalid = false; 120 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, &Invalid)); 121 EXPECT_TRUE(!Invalid); 122 123 Invalid = false; 124 EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 4, &Invalid)); 125 EXPECT_TRUE(!Invalid); 126 127 Invalid = false; 128 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 7, &Invalid)); 129 EXPECT_TRUE(!Invalid); 130 131 Invalid = false; 132 EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 11, &Invalid)); 133 EXPECT_TRUE(!Invalid); 134 135 Invalid = false; 136 EXPECT_EQ(7U, SourceMgr.getColumnNumber(MainFileID, strlen(Source), 137 &Invalid)); 138 EXPECT_TRUE(!Invalid); 139 140 Invalid = false; 141 SourceMgr.getColumnNumber(MainFileID, strlen(Source)+1, &Invalid); 142 EXPECT_TRUE(Invalid); 143 144 // Test invalid files 145 Invalid = false; 146 SourceMgr.getColumnNumber(FileID(), 0, &Invalid); 147 EXPECT_TRUE(Invalid); 148 149 Invalid = false; 150 SourceMgr.getColumnNumber(FileID(), 1, &Invalid); 151 EXPECT_TRUE(Invalid); 152 153 // Test with no invalid flag. 154 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, nullptr)); 155 } 156 157 TEST_F(SourceManagerTest, locationPrintTest) { 158 const char *header = "#define IDENTITY(x) x\n"; 159 160 const char *Source = "int x;\n" 161 "include \"test-header.h\"\n" 162 "IDENTITY(int y);\n" 163 "int z;"; 164 165 std::unique_ptr<llvm::MemoryBuffer> HeaderBuf = 166 llvm::MemoryBuffer::getMemBuffer(header); 167 std::unique_ptr<llvm::MemoryBuffer> Buf = 168 llvm::MemoryBuffer::getMemBuffer(Source); 169 170 const FileEntry *SourceFile = 171 FileMgr.getVirtualFile("/mainFile.cpp", Buf->getBufferSize(), 0); 172 SourceMgr.overrideFileContents(SourceFile, std::move(Buf)); 173 174 const FileEntry *HeaderFile = 175 FileMgr.getVirtualFile("/test-header.h", HeaderBuf->getBufferSize(), 0); 176 SourceMgr.overrideFileContents(HeaderFile, std::move(HeaderBuf)); 177 178 FileID MainFileID = SourceMgr.getOrCreateFileID(SourceFile, SrcMgr::C_User); 179 FileID HeaderFileID = SourceMgr.getOrCreateFileID(HeaderFile, SrcMgr::C_User); 180 SourceMgr.setMainFileID(MainFileID); 181 182 auto BeginLoc = SourceMgr.getLocForStartOfFile(MainFileID); 183 auto EndLoc = SourceMgr.getLocForEndOfFile(MainFileID); 184 185 auto BeginEOLLoc = SourceMgr.translateLineCol(MainFileID, 1, 7); 186 187 auto HeaderLoc = SourceMgr.getLocForStartOfFile(HeaderFileID); 188 189 EXPECT_EQ(BeginLoc.printToString(SourceMgr), "/mainFile.cpp:1:1"); 190 EXPECT_EQ(EndLoc.printToString(SourceMgr), "/mainFile.cpp:4:7"); 191 192 EXPECT_EQ(BeginEOLLoc.printToString(SourceMgr), "/mainFile.cpp:1:7"); 193 EXPECT_EQ(HeaderLoc.printToString(SourceMgr), "/test-header.h:1:1"); 194 195 EXPECT_EQ(SourceRange(BeginLoc, BeginLoc).printToString(SourceMgr), 196 "</mainFile.cpp:1:1>"); 197 EXPECT_EQ(SourceRange(BeginLoc, BeginEOLLoc).printToString(SourceMgr), 198 "</mainFile.cpp:1:1, col:7>"); 199 EXPECT_EQ(SourceRange(BeginLoc, EndLoc).printToString(SourceMgr), 200 "</mainFile.cpp:1:1, line:4:7>"); 201 EXPECT_EQ(SourceRange(BeginLoc, HeaderLoc).printToString(SourceMgr), 202 "</mainFile.cpp:1:1, /test-header.h:1:1>"); 203 } 204 205 TEST_F(SourceManagerTest, getInvalidBOM) { 206 ASSERT_EQ(SrcMgr::ContentCache::getInvalidBOM(""), nullptr); 207 ASSERT_EQ(SrcMgr::ContentCache::getInvalidBOM("\x00\x00\x00"), nullptr); 208 ASSERT_EQ(SrcMgr::ContentCache::getInvalidBOM("\xFF\xFF\xFF"), nullptr); 209 ASSERT_EQ(SrcMgr::ContentCache::getInvalidBOM("#include <iostream>"), 210 nullptr); 211 212 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( 213 "\xFE\xFF#include <iostream>")), 214 "UTF-16 (BE)"); 215 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( 216 "\xFF\xFE#include <iostream>")), 217 "UTF-16 (LE)"); 218 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( 219 "\x2B\x2F\x76#include <iostream>")), 220 "UTF-7"); 221 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( 222 "\xF7\x64\x4C#include <iostream>")), 223 "UTF-1"); 224 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( 225 "\xDD\x73\x66\x73#include <iostream>")), 226 "UTF-EBCDIC"); 227 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( 228 "\x0E\xFE\xFF#include <iostream>")), 229 "SCSU"); 230 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( 231 "\xFB\xEE\x28#include <iostream>")), 232 "BOCU-1"); 233 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( 234 "\x84\x31\x95\x33#include <iostream>")), 235 "GB-18030"); 236 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( 237 llvm::StringLiteral::withInnerNUL( 238 "\x00\x00\xFE\xFF#include <iostream>"))), 239 "UTF-32 (BE)"); 240 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( 241 llvm::StringLiteral::withInnerNUL( 242 "\xFF\xFE\x00\x00#include <iostream>"))), 243 "UTF-32 (LE)"); 244 } 245 246 // Regression test - there was an out of bound access for buffers not terminated by zero. 247 TEST_F(SourceManagerTest, getLineNumber) { 248 const unsigned pageSize = llvm::sys::Process::getPageSizeEstimate(); 249 std::unique_ptr<char[]> source(new char[pageSize]); 250 for(unsigned i = 0; i < pageSize; ++i) { 251 source[i] = 'a'; 252 } 253 254 std::unique_ptr<llvm::MemoryBuffer> Buf = 255 llvm::MemoryBuffer::getMemBuffer( 256 llvm::MemoryBufferRef( 257 llvm::StringRef(source.get(), 3), "whatever" 258 ), 259 false 260 ); 261 262 FileID mainFileID = SourceMgr.createFileID(std::move(Buf)); 263 SourceMgr.setMainFileID(mainFileID); 264 265 ASSERT_NO_FATAL_FAILURE(SourceMgr.getLineNumber(mainFileID, 1, nullptr)); 266 } 267 268 #if defined(LLVM_ON_UNIX) 269 270 TEST_F(SourceManagerTest, getMacroArgExpandedLocation) { 271 const char *header = 272 "#define FM(x,y) x\n"; 273 274 const char *main = 275 "#include \"/test-header.h\"\n" 276 "#define VAL 0\n" 277 "FM(VAL,0)\n" 278 "FM(0,VAL)\n" 279 "FM(FM(0,VAL),0)\n" 280 "#define CONCAT(X, Y) X##Y\n" 281 "CONCAT(1,1)\n"; 282 283 std::unique_ptr<llvm::MemoryBuffer> HeaderBuf = 284 llvm::MemoryBuffer::getMemBuffer(header); 285 std::unique_ptr<llvm::MemoryBuffer> MainBuf = 286 llvm::MemoryBuffer::getMemBuffer(main); 287 FileID mainFileID = SourceMgr.createFileID(std::move(MainBuf)); 288 SourceMgr.setMainFileID(mainFileID); 289 290 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h", 291 HeaderBuf->getBufferSize(), 0); 292 SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf)); 293 294 TrivialModuleLoader ModLoader; 295 HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr, 296 Diags, LangOpts, &*Target); 297 298 Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts, 299 SourceMgr, HeaderInfo, ModLoader, 300 /*IILookup =*/nullptr, 301 /*OwnsHeaderSearch =*/false); 302 // Ensure we can get expanded locations in presence of implicit includes. 303 // These are different than normal includes since predefines buffer doesn't 304 // have a valid insertion location. 305 PP.setPredefines("#include \"/implicit-header.h\""); 306 FileMgr.getVirtualFile("/implicit-header.h", 0, 0); 307 PP.Initialize(*Target); 308 PP.EnterMainSourceFile(); 309 310 std::vector<Token> toks; 311 while (1) { 312 Token tok; 313 PP.Lex(tok); 314 if (tok.is(tok::eof)) 315 break; 316 toks.push_back(tok); 317 } 318 319 // Make sure we got the tokens that we expected. 320 ASSERT_EQ(4U, toks.size()); 321 ASSERT_EQ(tok::numeric_constant, toks[0].getKind()); 322 ASSERT_EQ(tok::numeric_constant, toks[1].getKind()); 323 ASSERT_EQ(tok::numeric_constant, toks[2].getKind()); 324 ASSERT_EQ(tok::numeric_constant, toks[3].getKind()); 325 326 SourceLocation defLoc = SourceMgr.translateLineCol(mainFileID, 2, 13); 327 SourceLocation loc1 = SourceMgr.translateLineCol(mainFileID, 3, 8); 328 SourceLocation loc2 = SourceMgr.translateLineCol(mainFileID, 4, 4); 329 SourceLocation loc3 = SourceMgr.translateLineCol(mainFileID, 5, 7); 330 SourceLocation defLoc2 = SourceMgr.translateLineCol(mainFileID, 6, 22); 331 defLoc = SourceMgr.getMacroArgExpandedLocation(defLoc); 332 loc1 = SourceMgr.getMacroArgExpandedLocation(loc1); 333 loc2 = SourceMgr.getMacroArgExpandedLocation(loc2); 334 loc3 = SourceMgr.getMacroArgExpandedLocation(loc3); 335 defLoc2 = SourceMgr.getMacroArgExpandedLocation(defLoc2); 336 337 EXPECT_TRUE(defLoc.isFileID()); 338 EXPECT_TRUE(loc1.isFileID()); 339 EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc2)); 340 EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc3)); 341 EXPECT_EQ(loc2, toks[1].getLocation()); 342 EXPECT_EQ(loc3, toks[2].getLocation()); 343 EXPECT_TRUE(defLoc2.isFileID()); 344 } 345 346 namespace { 347 348 struct MacroAction { 349 enum Kind { kExpansion, kDefinition, kUnDefinition}; 350 351 SourceLocation Loc; 352 std::string Name; 353 unsigned MAKind : 3; 354 355 MacroAction(SourceLocation Loc, StringRef Name, unsigned K) 356 : Loc(Loc), Name(std::string(Name)), MAKind(K) {} 357 358 bool isExpansion() const { return MAKind == kExpansion; } 359 bool isDefinition() const { return MAKind & kDefinition; } 360 bool isUnDefinition() const { return MAKind & kUnDefinition; } 361 }; 362 363 class MacroTracker : public PPCallbacks { 364 std::vector<MacroAction> &Macros; 365 366 public: 367 explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { } 368 369 void MacroDefined(const Token &MacroNameTok, 370 const MacroDirective *MD) override { 371 Macros.push_back(MacroAction(MD->getLocation(), 372 MacroNameTok.getIdentifierInfo()->getName(), 373 MacroAction::kDefinition)); 374 } 375 void MacroUndefined(const Token &MacroNameTok, 376 const MacroDefinition &MD, 377 const MacroDirective *UD) override { 378 Macros.push_back( 379 MacroAction(UD ? UD->getLocation() : SourceLocation(), 380 MacroNameTok.getIdentifierInfo()->getName(), 381 UD ? MacroAction::kDefinition | MacroAction::kUnDefinition 382 : MacroAction::kUnDefinition)); 383 } 384 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, 385 SourceRange Range, const MacroArgs *Args) override { 386 Macros.push_back(MacroAction(MacroNameTok.getLocation(), 387 MacroNameTok.getIdentifierInfo()->getName(), 388 MacroAction::kExpansion)); 389 } 390 }; 391 392 } 393 394 TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) { 395 const char *header = 396 "#define MACRO_IN_INCLUDE 0\n" 397 "#define MACRO_DEFINED\n" 398 "#undef MACRO_DEFINED\n" 399 "#undef MACRO_UNDEFINED\n"; 400 401 const char *main = 402 "#define M(x) x\n" 403 "#define INC \"/test-header.h\"\n" 404 "#include M(INC)\n" 405 "#define INC2 </test-header.h>\n" 406 "#include M(INC2)\n"; 407 408 std::unique_ptr<llvm::MemoryBuffer> HeaderBuf = 409 llvm::MemoryBuffer::getMemBuffer(header); 410 std::unique_ptr<llvm::MemoryBuffer> MainBuf = 411 llvm::MemoryBuffer::getMemBuffer(main); 412 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(MainBuf))); 413 414 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h", 415 HeaderBuf->getBufferSize(), 0); 416 SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf)); 417 418 TrivialModuleLoader ModLoader; 419 HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr, 420 Diags, LangOpts, &*Target); 421 Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts, 422 SourceMgr, HeaderInfo, ModLoader, 423 /*IILookup =*/nullptr, 424 /*OwnsHeaderSearch =*/false); 425 PP.Initialize(*Target); 426 427 std::vector<MacroAction> Macros; 428 PP.addPPCallbacks(std::make_unique<MacroTracker>(Macros)); 429 430 PP.EnterMainSourceFile(); 431 432 std::vector<Token> toks; 433 while (1) { 434 Token tok; 435 PP.Lex(tok); 436 if (tok.is(tok::eof)) 437 break; 438 toks.push_back(tok); 439 } 440 441 // Make sure we got the tokens that we expected. 442 ASSERT_EQ(0U, toks.size()); 443 444 ASSERT_EQ(15U, Macros.size()); 445 // #define M(x) x 446 ASSERT_TRUE(Macros[0].isDefinition()); 447 ASSERT_EQ("M", Macros[0].Name); 448 // #define INC "/test-header.h" 449 ASSERT_TRUE(Macros[1].isDefinition()); 450 ASSERT_EQ("INC", Macros[1].Name); 451 // M expansion in #include M(INC) 452 ASSERT_FALSE(Macros[2].isDefinition()); 453 ASSERT_EQ("M", Macros[2].Name); 454 // INC expansion in #include M(INC) 455 ASSERT_TRUE(Macros[3].isExpansion()); 456 ASSERT_EQ("INC", Macros[3].Name); 457 // #define MACRO_IN_INCLUDE 0 458 ASSERT_TRUE(Macros[4].isDefinition()); 459 ASSERT_EQ("MACRO_IN_INCLUDE", Macros[4].Name); 460 // #define MACRO_DEFINED 461 ASSERT_TRUE(Macros[5].isDefinition()); 462 ASSERT_FALSE(Macros[5].isUnDefinition()); 463 ASSERT_EQ("MACRO_DEFINED", Macros[5].Name); 464 // #undef MACRO_DEFINED 465 ASSERT_TRUE(Macros[6].isDefinition()); 466 ASSERT_TRUE(Macros[6].isUnDefinition()); 467 ASSERT_EQ("MACRO_DEFINED", Macros[6].Name); 468 // #undef MACRO_UNDEFINED 469 ASSERT_FALSE(Macros[7].isDefinition()); 470 ASSERT_TRUE(Macros[7].isUnDefinition()); 471 ASSERT_EQ("MACRO_UNDEFINED", Macros[7].Name); 472 // #define INC2 </test-header.h> 473 ASSERT_TRUE(Macros[8].isDefinition()); 474 ASSERT_EQ("INC2", Macros[8].Name); 475 // M expansion in #include M(INC2) 476 ASSERT_FALSE(Macros[9].isDefinition()); 477 ASSERT_EQ("M", Macros[9].Name); 478 // INC2 expansion in #include M(INC2) 479 ASSERT_TRUE(Macros[10].isExpansion()); 480 ASSERT_EQ("INC2", Macros[10].Name); 481 // #define MACRO_IN_INCLUDE 0 482 ASSERT_TRUE(Macros[11].isDefinition()); 483 ASSERT_EQ("MACRO_IN_INCLUDE", Macros[11].Name); 484 485 // The INC expansion in #include M(INC) comes before the first 486 // MACRO_IN_INCLUDE definition of the included file. 487 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[3].Loc, Macros[4].Loc)); 488 489 // The INC2 expansion in #include M(INC2) comes before the second 490 // MACRO_IN_INCLUDE definition of the included file. 491 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[10].Loc, Macros[11].Loc)); 492 } 493 494 TEST_F(SourceManagerTest, isMainFile) { 495 const char *Source = "int x;"; 496 497 std::unique_ptr<llvm::MemoryBuffer> Buf = 498 llvm::MemoryBuffer::getMemBuffer(Source); 499 const FileEntry *SourceFile = 500 FileMgr.getVirtualFile("mainFile.cpp", Buf->getBufferSize(), 0); 501 SourceMgr.overrideFileContents(SourceFile, std::move(Buf)); 502 503 std::unique_ptr<llvm::MemoryBuffer> Buf2 = 504 llvm::MemoryBuffer::getMemBuffer(Source); 505 const FileEntry *SecondFile = 506 FileMgr.getVirtualFile("file2.cpp", Buf2->getBufferSize(), 0); 507 SourceMgr.overrideFileContents(SecondFile, std::move(Buf2)); 508 509 FileID MainFileID = SourceMgr.getOrCreateFileID(SourceFile, SrcMgr::C_User); 510 SourceMgr.setMainFileID(MainFileID); 511 512 EXPECT_TRUE(SourceMgr.isMainFile(FileEntryRef("mainFile.cpp", *SourceFile))); 513 EXPECT_TRUE( 514 SourceMgr.isMainFile(FileEntryRef("anotherName.cpp", *SourceFile))); 515 EXPECT_FALSE(SourceMgr.isMainFile(FileEntryRef("mainFile.cpp", *SecondFile))); 516 } 517 518 #endif 519 520 } // anonymous namespace 521