[clang] 0065198 - clang-{tools,unittests}: Stop using SourceManager::getBuffer, NFC

Duncan P. N. Exon Smith via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 14 22:01:41 PDT 2020


Author: Duncan P. N. Exon Smith
Date: 2020-10-15T00:35:16-04:00
New Revision: 006519816689acef5fd971955e21b7ab17ae65d9

URL: https://github.com/llvm/llvm-project/commit/006519816689acef5fd971955e21b7ab17ae65d9
DIFF: https://github.com/llvm/llvm-project/commit/006519816689acef5fd971955e21b7ab17ae65d9.diff

LOG: clang-{tools,unittests}: Stop using SourceManager::getBuffer, NFC

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.

Differential Revision: https://reviews.llvm.org/D89416

Added: 
    

Modified: 
    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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/bugprone/SuspiciousSemicolonCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SuspiciousSemicolonCheck.cpp
index 9b34f5ab55a7..3874a52e7e20 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SuspiciousSemicolonCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SuspiciousSemicolonCheck.cpp
@@ -54,10 +54,10 @@ void SuspiciousSemicolonCheck::check(const MatchFinder::MatchResult &Result) {
 
   SourceLocation LocEnd = Semicolon->getEndLoc();
   FileID FID = SM.getFileID(LocEnd);
-  const llvm::MemoryBuffer *Buffer = SM.getBuffer(FID, LocEnd);
+  llvm::MemoryBufferRef Buffer = SM.getBufferOrFake(FID, LocEnd);
   Lexer Lexer(SM.getLocForStartOfFile(FID), Ctxt.getLangOpts(),
-              Buffer->getBufferStart(), SM.getCharacterData(LocEnd) + 1,
-              Buffer->getBufferEnd());
+              Buffer.getBufferStart(), SM.getCharacterData(LocEnd) + 1,
+              Buffer.getBufferEnd());
   if (Lexer.LexFromRawLexer(Token))
     return;
 

diff  --git a/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp b/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
index b84e4d525d8c..5d3d3c46edb4 100644
--- a/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
@@ -794,14 +794,14 @@ static bool areExprsFromDifferentMacros(const Expr *LhsExpr,
       SM.getDecomposedLoc(SM.getExpansionLoc(Lsr.getBegin()));
   std::pair<FileID, unsigned> RsrLocInfo =
       SM.getDecomposedLoc(SM.getExpansionLoc(Rsr.getBegin()));
-  const llvm::MemoryBuffer *MB = SM.getBuffer(LsrLocInfo.first);
+  llvm::MemoryBufferRef MB = SM.getBufferOrFake(LsrLocInfo.first);
 
-  const char *LTokenPos = MB->getBufferStart() + LsrLocInfo.second;
-  const char *RTokenPos = MB->getBufferStart() + RsrLocInfo.second;
+  const char *LTokenPos = MB.getBufferStart() + LsrLocInfo.second;
+  const char *RTokenPos = MB.getBufferStart() + RsrLocInfo.second;
   Lexer LRawLex(SM.getLocForStartOfFile(LsrLocInfo.first), LO,
-                MB->getBufferStart(), LTokenPos, MB->getBufferEnd());
+                MB.getBufferStart(), LTokenPos, MB.getBufferEnd());
   Lexer RRawLex(SM.getLocForStartOfFile(RsrLocInfo.first), LO,
-                MB->getBufferStart(), RTokenPos, MB->getBufferEnd());
+                MB.getBufferStart(), RTokenPos, MB.getBufferEnd());
 
   Token LTok, RTok;
   do { // Compare the expressions token-by-token.

diff  --git a/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp b/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp
index 53cebc32bb38..3e3f8dbf02ff 100644
--- a/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp
@@ -140,7 +140,8 @@ SourceLocation StaticAssertCheck::getLastParenLoc(const ASTContext *ASTCtx,
   const LangOptions &Opts = ASTCtx->getLangOpts();
   const SourceManager &SM = ASTCtx->getSourceManager();
 
-  const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getFileID(AssertLoc));
+  llvm::Optional<llvm::MemoryBufferRef> Buffer =
+      SM.getBufferOrNone(SM.getFileID(AssertLoc));
   if (!Buffer)
     return SourceLocation();
 

diff  --git a/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp b/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp
index 1cecfa31da34..18165a619adf 100644
--- a/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp
@@ -209,7 +209,7 @@ bool MagicNumbersCheck::isSyntheticValue(const SourceManager *SourceManager,
     return false;
 
   const StringRef BufferIdentifier =
-      SourceManager->getBuffer(FileOffset.first)->getBufferIdentifier();
+      SourceManager->getBufferOrFake(FileOffset.first).getBufferIdentifier();
 
   return BufferIdentifier.empty();
 }

diff  --git a/clang-tools-extra/clangd/ParsedAST.cpp b/clang-tools-extra/clangd/ParsedAST.cpp
index 23d86221e74f..c8096017b50a 100644
--- a/clang-tools-extra/clangd/ParsedAST.cpp
+++ b/clang-tools-extra/clangd/ParsedAST.cpp
@@ -164,7 +164,7 @@ class ReplayPreamble : private PPCallbacks {
                    SrcMgr::CharacteristicKind Kind, FileID PrevFID) override {
     // It'd be nice if there was a better way to identify built-in headers...
     if (Reason == FileChangeReason::ExitFile &&
-        SM.getBuffer(PrevFID)->getBufferIdentifier() == "<built-in>")
+        SM.getBufferOrFake(PrevFID).getBufferIdentifier() == "<built-in>")
       replay();
   }
 

diff  --git a/clang-tools-extra/clangd/SemanticHighlighting.cpp b/clang-tools-extra/clangd/SemanticHighlighting.cpp
index 4e66a9bb4e85..a9c885c7275e 100644
--- a/clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ b/clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -222,7 +222,7 @@ class HighlightingsBuilder {
       TokRef = TokRef.drop_front(Conflicting.size());
     }
     const auto &SM = AST.getSourceManager();
-    StringRef MainCode = SM.getBuffer(SM.getMainFileID())->getBuffer();
+    StringRef MainCode = SM.getBufferOrFake(SM.getMainFileID()).getBuffer();
 
     // Merge token stream with "inactive line" markers.
     std::vector<HighlightingToken> WithInactiveLines;

diff  --git a/clang-tools-extra/clangd/SourceCode.cpp b/clang-tools-extra/clangd/SourceCode.cpp
index c6279177eba9..bcd5758af441 100644
--- a/clang-tools-extra/clangd/SourceCode.cpp
+++ b/clang-tools-extra/clangd/SourceCode.cpp
@@ -445,9 +445,8 @@ llvm::Optional<SourceRange> toHalfOpenFileRange(const SourceManager &SM,
 
 llvm::StringRef toSourceCode(const SourceManager &SM, SourceRange R) {
   assert(isValidFileRange(SM, R));
-  bool Invalid = false;
-  auto *Buf = SM.getBuffer(SM.getFileID(R.getBegin()), &Invalid);
-  assert(!Invalid);
+  auto Buf = SM.getBufferOrNone(SM.getFileID(R.getBegin()));
+  assert(Buf);
 
   size_t BeginOffset = SM.getFileOffset(R.getBegin());
   size_t EndOffset = SM.getFileOffset(R.getEnd());
@@ -456,7 +455,7 @@ llvm::StringRef toSourceCode(const SourceManager &SM, SourceRange R) {
 
 llvm::Expected<SourceLocation> sourceLocationInMainFile(const SourceManager &SM,
                                                         Position P) {
-  llvm::StringRef Code = SM.getBuffer(SM.getMainFileID())->getBuffer();
+  llvm::StringRef Code = SM.getBufferOrFake(SM.getMainFileID()).getBuffer();
   auto Offset =
       positionToOffset(Code, P, /*AllowColumnBeyondLineLength=*/false);
   if (!Offset)

diff  --git a/clang-tools-extra/modularize/PreprocessorTracker.cpp b/clang-tools-extra/modularize/PreprocessorTracker.cpp
index f8ab2c8067c0..f472415225a1 100644
--- a/clang-tools-extra/modularize/PreprocessorTracker.cpp
+++ b/clang-tools-extra/modularize/PreprocessorTracker.cpp
@@ -312,10 +312,10 @@ static std::string getSourceString(clang::Preprocessor &PP,
 // Retrieve source line from file image given a location.
 static std::string getSourceLine(clang::Preprocessor &PP,
                                  clang::SourceLocation Loc) {
-  const llvm::MemoryBuffer *MemBuffer =
-      PP.getSourceManager().getBuffer(PP.getSourceManager().getFileID(Loc));
-  const char *Buffer = MemBuffer->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 @@ static std::string getSourceLine(clang::Preprocessor &PP,
 // Retrieve source line from file image given a file ID and line number.
 static std::string getSourceLine(clang::Preprocessor &PP, clang::FileID FileID,
                                  int Line) {
-  const llvm::MemoryBuffer *MemBuffer = PP.getSourceManager().getBuffer(FileID);
-  const char *Buffer = MemBuffer->getBufferStart();
-  const char *BufferEnd = MemBuffer->getBufferEnd();
+  llvm::MemoryBufferRef MemBuffer =
+      PP.getSourceManager().getBufferOrFake(FileID);
+  const char *Buffer = MemBuffer.getBufferStart();
+  const char *BufferEnd = MemBuffer.getBufferEnd();
   const char *BeginPtr = Buffer;
   const char *EndPtr = BufferEnd;
   int LineCounter = 1;

diff  --git a/clang/tools/clang-
diff /ClangDiff.cpp b/clang/tools/clang-
diff /ClangDiff.cpp
index 6d1f4b9a3ec8..3046e9c5fa37 100644
--- a/clang/tools/clang-
diff /ClangDiff.cpp
+++ b/clang/tools/clang-
diff /ClangDiff.cpp
@@ -284,7 +284,7 @@ static unsigned printHtmlForNode(raw_ostream &OS, const 
diff ::ASTDiff &Diff,
   unsigned Begin, End;
   std::tie(Begin, End) = Tree.getSourceRangeOffsets(Node);
   const SourceManager &SrcMgr = 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 << "<span id='" << MyTag << Id << "' "

diff  --git a/clang/tools/clang-import-test/clang-import-test.cpp b/clang/tools/clang-import-test/clang-import-test.cpp
index 0c9bd237b3ba..eca3012957a3 100644
--- a/clang/tools/clang-import-test/clang-import-test.cpp
+++ b/clang/tools/clang-import-test/clang-import-test.cpp
@@ -106,20 +106,19 @@ class TestDiagnosticConsumer : public DiagnosticConsumer {
     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)
       ;
 

diff  --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp
index 87138cd3b4a3..a64f90f8f74b 100644
--- a/clang/tools/libclang/CIndex.cpp
+++ b/clang/tools/libclang/CIndex.cpp
@@ -4362,9 +4362,8 @@ const char *clang_getFileContents(CXTranslationUnit TU, CXFile file,
 
   const SourceManager &SM = cxtu::getASTUnit(TU)->getSourceManager();
   FileID fid = SM.translateFile(static_cast<FileEntry *>(file));
-  bool Invalid = true;
-  const llvm::MemoryBuffer *buf = SM.getBuffer(fid, &Invalid);
-  if (Invalid) {
+  llvm::Optional<llvm::MemoryBufferRef> buf = SM.getBufferOrNone(fid);
+  if (!buf) {
     if (size)
       *size = 0;
     return nullptr;

diff  --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index 967dc035d11f..f869e492c90a 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -5695,7 +5695,8 @@ TEST_P(ImportSourceLocations, NormalFileBuffer) {
   // Make sure the imported buffer has the original contents.
   SourceManager &ToSM = 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 @@ TEST_P(ImportSourceLocations, OverwrittenFileBuffer) {
   SourceManager &ToSM = ToAST->getSourceManager();
   FileID ImportedID = ToSM.getFileID(ImportedLoc);
   EXPECT_EQ(Contents,
-            ToSM.getBuffer(ImportedID, SourceLocation())->getBuffer());
+            ToSM.getBufferOrFake(ImportedID, SourceLocation()).getBuffer());
 }
 
 TEST_P(ASTImporterOptionSpecificTestBase, ImportExprOfAlignmentAttr) {


        


More information about the cfe-commits mailing list