[PATCH] D89416: clang-{tools,unittests}: Stop using SourceManager::getBuffer, NFC
This revision was automatically updated to reflect the committed changes. Closed by commit rG006519816689: clang-{tools,unittests}: Stop using SourceManager::getBuffer, NFC (authored by dexonsmith). Herald added a project: clang. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D89416/new/ https://reviews.llvm.org/D89416 Files: clang-tools-extra/clang-tidy/bugprone/SuspiciousSemicolonCheck.cpp clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp clang-tools-extra/clangd/ParsedAST.cpp clang-tools-extra/clangd/SemanticHighlighting.cpp clang-tools-extra/clangd/SourceCode.cpp clang-tools-extra/modularize/PreprocessorTracker.cpp clang/tools/clang-diff/ClangDiff.cpp clang/tools/clang-import-test/clang-import-test.cpp clang/tools/libclang/CIndex.cpp clang/unittests/AST/ASTImporterTest.cpp Index: clang/unittests/AST/ASTImporterTest.cpp === --- clang/unittests/AST/ASTImporterTest.cpp +++ clang/unittests/AST/ASTImporterTest.cpp @@ -5695,7 +5695,8 @@ // Make sure the imported buffer has the original contents. SourceManager = ToAST->getSourceManager(); FileID ImportedID = ToSM.getFileID(ImportedLoc); - EXPECT_EQ(Source, ToSM.getBuffer(ImportedID, SourceLocation())->getBuffer()); + EXPECT_EQ(Source, +ToSM.getBufferOrFake(ImportedID, SourceLocation()).getBuffer()); } TEST_P(ImportSourceLocations, OverwrittenFileBuffer) { @@ -5729,7 +5730,7 @@ SourceManager = ToAST->getSourceManager(); FileID ImportedID = ToSM.getFileID(ImportedLoc); EXPECT_EQ(Contents, -ToSM.getBuffer(ImportedID, SourceLocation())->getBuffer()); +ToSM.getBufferOrFake(ImportedID, SourceLocation()).getBuffer()); } TEST_P(ASTImporterOptionSpecificTestBase, ImportExprOfAlignmentAttr) { Index: clang/tools/libclang/CIndex.cpp === --- clang/tools/libclang/CIndex.cpp +++ clang/tools/libclang/CIndex.cpp @@ -4362,9 +4362,8 @@ const SourceManager = cxtu::getASTUnit(TU)->getSourceManager(); FileID fid = SM.translateFile(static_cast(file)); - bool Invalid = true; - const llvm::MemoryBuffer *buf = SM.getBuffer(fid, ); - if (Invalid) { + llvm::Optional buf = SM.getBufferOrNone(fid); + if (!buf) { if (size) *size = 0; return nullptr; Index: clang/tools/clang-import-test/clang-import-test.cpp === --- clang/tools/clang-import-test/clang-import-test.cpp +++ clang/tools/clang-import-test/clang-import-test.cpp @@ -106,20 +106,19 @@ unsigned LocColumn = SM.getSpellingColumnNumber(Loc, /*Invalid=*/nullptr) - 1; FileID FID = SM.getFileID(Loc); -const llvm::MemoryBuffer *Buffer = -SM.getBuffer(FID, Loc, /*Invalid=*/nullptr); +llvm::MemoryBufferRef Buffer = SM.getBufferOrFake(FID, Loc); -assert(LocData >= Buffer->getBufferStart() && - LocData < Buffer->getBufferEnd()); +assert(LocData >= Buffer.getBufferStart() && + LocData < Buffer.getBufferEnd()); const char *LineBegin = LocData - LocColumn; -assert(LineBegin >= Buffer->getBufferStart()); +assert(LineBegin >= Buffer.getBufferStart()); const char *LineEnd = nullptr; for (LineEnd = LineBegin; *LineEnd != '\n' && *LineEnd != '\r' && - LineEnd < Buffer->getBufferEnd(); + LineEnd < Buffer.getBufferEnd(); ++LineEnd) ; Index: clang/tools/clang-diff/ClangDiff.cpp === --- clang/tools/clang-diff/ClangDiff.cpp +++ clang/tools/clang-diff/ClangDiff.cpp @@ -284,7 +284,7 @@ unsigned Begin, End; std::tie(Begin, End) = Tree.getSourceRangeOffsets(Node); const SourceManager = Tree.getASTContext().getSourceManager(); - auto Code = SrcMgr.getBuffer(SrcMgr.getMainFileID())->getBuffer(); + auto Code = SrcMgr.getBufferOrFake(SrcMgr.getMainFileID()).getBuffer(); for (; Offset < Begin; ++Offset) printHtml(OS, Code[Offset]); OS << "getBufferStart(); - const char *BufferEnd = MemBuffer->getBufferEnd(); + llvm::MemoryBufferRef MemBuffer = PP.getSourceManager().getBufferOrFake( + PP.getSourceManager().getFileID(Loc)); + const char *Buffer = MemBuffer.getBufferStart(); + const char *BufferEnd = MemBuffer.getBufferEnd(); const char *BeginPtr = PP.getSourceManager().getCharacterData(Loc); const char *EndPtr = BeginPtr; while (BeginPtr > Buffer) { @@ -338,9 +338,10 @@ // Retrieve source line from file image given a file ID and line number. static std::string getSourceLine(clang::Preprocessor , clang::FileID FileID, int Line) { - const llvm::MemoryBuffer *MemBuffer =
[PATCH] D89416: clang-{tools,unittests}: Stop using SourceManager::getBuffer, NFC
dexonsmith updated this revision to Diff 298206. dexonsmith added a comment. Removed the now-unused `Invalid` local variable from the libclang change; not sure how I missed the warning before. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D89416/new/ https://reviews.llvm.org/D89416 Files: clang-tools-extra/clang-tidy/bugprone/SuspiciousSemicolonCheck.cpp clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp clang-tools-extra/clangd/ParsedAST.cpp clang-tools-extra/clangd/SemanticHighlighting.cpp clang-tools-extra/clangd/SourceCode.cpp clang-tools-extra/modularize/PreprocessorTracker.cpp clang/tools/clang-diff/ClangDiff.cpp clang/tools/clang-import-test/clang-import-test.cpp clang/tools/libclang/CIndex.cpp clang/unittests/AST/ASTImporterTest.cpp Index: clang/unittests/AST/ASTImporterTest.cpp === --- clang/unittests/AST/ASTImporterTest.cpp +++ clang/unittests/AST/ASTImporterTest.cpp @@ -5695,7 +5695,8 @@ // Make sure the imported buffer has the original contents. SourceManager = ToAST->getSourceManager(); FileID ImportedID = ToSM.getFileID(ImportedLoc); - EXPECT_EQ(Source, ToSM.getBuffer(ImportedID, SourceLocation())->getBuffer()); + EXPECT_EQ(Source, +ToSM.getBufferOrFake(ImportedID, SourceLocation()).getBuffer()); } TEST_P(ImportSourceLocations, OverwrittenFileBuffer) { @@ -5729,7 +5730,7 @@ SourceManager = ToAST->getSourceManager(); FileID ImportedID = ToSM.getFileID(ImportedLoc); EXPECT_EQ(Contents, -ToSM.getBuffer(ImportedID, SourceLocation())->getBuffer()); +ToSM.getBufferOrFake(ImportedID, SourceLocation()).getBuffer()); } TEST_P(ASTImporterOptionSpecificTestBase, ImportExprOfAlignmentAttr) { Index: clang/tools/libclang/CIndex.cpp === --- clang/tools/libclang/CIndex.cpp +++ clang/tools/libclang/CIndex.cpp @@ -4362,9 +4362,8 @@ const SourceManager = cxtu::getASTUnit(TU)->getSourceManager(); FileID fid = SM.translateFile(static_cast(file)); - bool Invalid = true; - const llvm::MemoryBuffer *buf = SM.getBuffer(fid, ); - if (Invalid) { + llvm::Optional buf = SM.getBufferOrNone(fid); + if (!buf) { if (size) *size = 0; return nullptr; Index: clang/tools/clang-import-test/clang-import-test.cpp === --- clang/tools/clang-import-test/clang-import-test.cpp +++ clang/tools/clang-import-test/clang-import-test.cpp @@ -106,20 +106,19 @@ unsigned LocColumn = SM.getSpellingColumnNumber(Loc, /*Invalid=*/nullptr) - 1; FileID FID = SM.getFileID(Loc); -const llvm::MemoryBuffer *Buffer = -SM.getBuffer(FID, Loc, /*Invalid=*/nullptr); +llvm::MemoryBufferRef Buffer = SM.getBufferOrFake(FID, Loc); -assert(LocData >= Buffer->getBufferStart() && - LocData < Buffer->getBufferEnd()); +assert(LocData >= Buffer.getBufferStart() && + LocData < Buffer.getBufferEnd()); const char *LineBegin = LocData - LocColumn; -assert(LineBegin >= Buffer->getBufferStart()); +assert(LineBegin >= Buffer.getBufferStart()); const char *LineEnd = nullptr; for (LineEnd = LineBegin; *LineEnd != '\n' && *LineEnd != '\r' && - LineEnd < Buffer->getBufferEnd(); + LineEnd < Buffer.getBufferEnd(); ++LineEnd) ; Index: clang/tools/clang-diff/ClangDiff.cpp === --- clang/tools/clang-diff/ClangDiff.cpp +++ clang/tools/clang-diff/ClangDiff.cpp @@ -284,7 +284,7 @@ unsigned Begin, End; std::tie(Begin, End) = Tree.getSourceRangeOffsets(Node); const SourceManager = Tree.getASTContext().getSourceManager(); - auto Code = SrcMgr.getBuffer(SrcMgr.getMainFileID())->getBuffer(); + auto Code = SrcMgr.getBufferOrFake(SrcMgr.getMainFileID()).getBuffer(); for (; Offset < Begin; ++Offset) printHtml(OS, Code[Offset]); OS << "getBufferStart(); - const char *BufferEnd = MemBuffer->getBufferEnd(); + llvm::MemoryBufferRef MemBuffer = PP.getSourceManager().getBufferOrFake( + PP.getSourceManager().getFileID(Loc)); + const char *Buffer = MemBuffer.getBufferStart(); + const char *BufferEnd = MemBuffer.getBufferEnd(); const char *BeginPtr = PP.getSourceManager().getCharacterData(Loc); const char *EndPtr = BeginPtr; while (BeginPtr > Buffer) { @@ -338,9 +338,10 @@ // Retrieve source line from file image given a file ID and line number. static std::string getSourceLine(clang::Preprocessor , clang::FileID FileID, int Line) { - const llvm::MemoryBuffer *MemBuffer = PP.getSourceManager().getBuffer(FileID); - const char *Buffer =
[PATCH] D89416: clang-{tools,unittests}: Stop using SourceManager::getBuffer, NFC
dexonsmith created this revision. dexonsmith added a reviewer: arphaman. Herald added subscribers: usaxena95, ributzka, kadircet. Herald added a reviewer: shafik. dexonsmith requested review of this revision. Update clang-tools-extra, clang/tools, clang/unittests to migrate from `SourceManager::getBuffer`, which returns an always dereferenceable `MemoryBuffer*`, to `getBufferOrNone` or `getBufferOrFake`, both of which return a `MemoryBufferRef`, depending on whether the call site was checking for validity of the buffer. No functionality change intended. https://reviews.llvm.org/D89416 Files: clang-tools-extra/clang-tidy/bugprone/SuspiciousSemicolonCheck.cpp clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp clang-tools-extra/clangd/ParsedAST.cpp clang-tools-extra/clangd/SemanticHighlighting.cpp clang-tools-extra/clangd/SourceCode.cpp clang-tools-extra/modularize/PreprocessorTracker.cpp clang/tools/clang-diff/ClangDiff.cpp clang/tools/clang-import-test/clang-import-test.cpp clang/tools/libclang/CIndex.cpp clang/unittests/AST/ASTImporterTest.cpp Index: clang/unittests/AST/ASTImporterTest.cpp === --- clang/unittests/AST/ASTImporterTest.cpp +++ clang/unittests/AST/ASTImporterTest.cpp @@ -5695,7 +5695,8 @@ // Make sure the imported buffer has the original contents. SourceManager = ToAST->getSourceManager(); FileID ImportedID = ToSM.getFileID(ImportedLoc); - EXPECT_EQ(Source, ToSM.getBuffer(ImportedID, SourceLocation())->getBuffer()); + EXPECT_EQ(Source, +ToSM.getBufferOrFake(ImportedID, SourceLocation()).getBuffer()); } TEST_P(ImportSourceLocations, OverwrittenFileBuffer) { @@ -5729,7 +5730,7 @@ SourceManager = ToAST->getSourceManager(); FileID ImportedID = ToSM.getFileID(ImportedLoc); EXPECT_EQ(Contents, -ToSM.getBuffer(ImportedID, SourceLocation())->getBuffer()); +ToSM.getBufferOrFake(ImportedID, SourceLocation()).getBuffer()); } TEST_P(ASTImporterOptionSpecificTestBase, ImportExprOfAlignmentAttr) { Index: clang/tools/libclang/CIndex.cpp === --- clang/tools/libclang/CIndex.cpp +++ clang/tools/libclang/CIndex.cpp @@ -4363,8 +4363,8 @@ const SourceManager = cxtu::getASTUnit(TU)->getSourceManager(); FileID fid = SM.translateFile(static_cast(file)); bool Invalid = true; - const llvm::MemoryBuffer *buf = SM.getBuffer(fid, ); - if (Invalid) { + llvm::Optional buf = SM.getBufferOrNone(fid); + if (!buf) { if (size) *size = 0; return nullptr; Index: clang/tools/clang-import-test/clang-import-test.cpp === --- clang/tools/clang-import-test/clang-import-test.cpp +++ clang/tools/clang-import-test/clang-import-test.cpp @@ -106,20 +106,19 @@ unsigned LocColumn = SM.getSpellingColumnNumber(Loc, /*Invalid=*/nullptr) - 1; FileID FID = SM.getFileID(Loc); -const llvm::MemoryBuffer *Buffer = -SM.getBuffer(FID, Loc, /*Invalid=*/nullptr); +llvm::MemoryBufferRef Buffer = SM.getBufferOrFake(FID, Loc); -assert(LocData >= Buffer->getBufferStart() && - LocData < Buffer->getBufferEnd()); +assert(LocData >= Buffer.getBufferStart() && + LocData < Buffer.getBufferEnd()); const char *LineBegin = LocData - LocColumn; -assert(LineBegin >= Buffer->getBufferStart()); +assert(LineBegin >= Buffer.getBufferStart()); const char *LineEnd = nullptr; for (LineEnd = LineBegin; *LineEnd != '\n' && *LineEnd != '\r' && - LineEnd < Buffer->getBufferEnd(); + LineEnd < Buffer.getBufferEnd(); ++LineEnd) ; Index: clang/tools/clang-diff/ClangDiff.cpp === --- clang/tools/clang-diff/ClangDiff.cpp +++ clang/tools/clang-diff/ClangDiff.cpp @@ -284,7 +284,7 @@ unsigned Begin, End; std::tie(Begin, End) = Tree.getSourceRangeOffsets(Node); const SourceManager = Tree.getASTContext().getSourceManager(); - auto Code = SrcMgr.getBuffer(SrcMgr.getMainFileID())->getBuffer(); + auto Code = SrcMgr.getBufferOrFake(SrcMgr.getMainFileID()).getBuffer(); for (; Offset < Begin; ++Offset) printHtml(OS, Code[Offset]); OS << "getBufferStart(); - const char *BufferEnd = MemBuffer->getBufferEnd(); + llvm::MemoryBufferRef MemBuffer = PP.getSourceManager().getBufferOrFake( + PP.getSourceManager().getFileID(Loc)); + const char *Buffer = MemBuffer.getBufferStart(); + const char *BufferEnd = MemBuffer.getBufferEnd(); const char *BeginPtr = PP.getSourceManager().getCharacterData(Loc); const char *EndPtr = BeginPtr; while (BeginPtr > Buffer) { @@ -338,9 +338,10 @@ // Retrieve