From b8e08860065a64963f49da64742350a8231b1727 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Thu, 30 Aug 2018 23:10:52 +0000 Subject: Add dump() method for SourceRange Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D50662 llvm-svn: 341140 --- clang/unittests/Basic/SourceManagerTest.cpp | 48 +++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'clang/unittests/Basic/SourceManagerTest.cpp') diff --git a/clang/unittests/Basic/SourceManagerTest.cpp b/clang/unittests/Basic/SourceManagerTest.cpp index 8457d3b639f..b548bf57523 100644 --- a/clang/unittests/Basic/SourceManagerTest.cpp +++ b/clang/unittests/Basic/SourceManagerTest.cpp @@ -155,6 +155,54 @@ TEST_F(SourceManagerTest, getColumnNumber) { EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, nullptr)); } +TEST_F(SourceManagerTest, locationPrintTest) { + const char *header = "#define IDENTITY(x) x\n"; + + const char *Source = "int x;\n" + "include \"test-header.h\"\n" + "IDENTITY(int y);\n" + "int z;"; + + std::unique_ptr HeaderBuf = + llvm::MemoryBuffer::getMemBuffer(header); + std::unique_ptr Buf = + llvm::MemoryBuffer::getMemBuffer(Source); + + const FileEntry *SourceFile = + FileMgr.getVirtualFile("/mainFile.cpp", Buf->getBufferSize(), 0); + SourceMgr.overrideFileContents(SourceFile, std::move(Buf)); + + const FileEntry *HeaderFile = + FileMgr.getVirtualFile("/test-header.h", HeaderBuf->getBufferSize(), 0); + SourceMgr.overrideFileContents(HeaderFile, std::move(HeaderBuf)); + + FileID MainFileID = SourceMgr.getOrCreateFileID(SourceFile, SrcMgr::C_User); + FileID HeaderFileID = SourceMgr.getOrCreateFileID(HeaderFile, SrcMgr::C_User); + SourceMgr.setMainFileID(MainFileID); + + auto BeginLoc = SourceMgr.getLocForStartOfFile(MainFileID); + auto EndLoc = SourceMgr.getLocForEndOfFile(MainFileID); + + auto BeginEOLLoc = SourceMgr.translateLineCol(MainFileID, 1, 7); + + auto HeaderLoc = SourceMgr.getLocForStartOfFile(HeaderFileID); + + EXPECT_EQ(BeginLoc.printToString(SourceMgr), "/mainFile.cpp:1:1"); + EXPECT_EQ(EndLoc.printToString(SourceMgr), "/mainFile.cpp:4:7"); + + EXPECT_EQ(BeginEOLLoc.printToString(SourceMgr), "/mainFile.cpp:1:7"); + EXPECT_EQ(HeaderLoc.printToString(SourceMgr), "/test-header.h:1:1"); + + EXPECT_EQ(SourceRange(BeginLoc, BeginLoc).printToString(SourceMgr), + ""); + EXPECT_EQ(SourceRange(BeginLoc, BeginEOLLoc).printToString(SourceMgr), + ""); + EXPECT_EQ(SourceRange(BeginLoc, EndLoc).printToString(SourceMgr), + ""); + EXPECT_EQ(SourceRange(BeginLoc, HeaderLoc).printToString(SourceMgr), + ""); +} + #if defined(LLVM_ON_UNIX) TEST_F(SourceManagerTest, getMacroArgExpandedLocation) { -- cgit v1.2.3