[clang] af4fb41 - clang/StaticAnalyzer: Stop using SourceManager::getBuffer
Duncan P. N. Exon Smith via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 14 21:34:33 PDT 2020
Author: Duncan P. N. Exon Smith
Date: 2020-10-15T00:34:24-04:00
New Revision: af4fb416bd355960ce93f271a0591f24f58d25ec
URL: https://github.com/llvm/llvm-project/commit/af4fb416bd355960ce93f271a0591f24f58d25ec
DIFF: https://github.com/llvm/llvm-project/commit/af4fb416bd355960ce93f271a0591f24f58d25ec.diff
LOG: clang/StaticAnalyzer: Stop using SourceManager::getBuffer
Update clang/lib/StaticAnalyzer to stop relying on a `MemoryBuffer*`,
using the `MemoryBufferRef` from `getBufferOrNone` or the
`Optional<MemoryBufferRef>` from `getBufferOrFake`, depending on whether
there's logic for checking validity of the buffer. The change to
clang/lib/StaticAnalyzer/Core/IssueHash.cpp is potentially a
functionality change, since the logic was wrong (it checked for
`nullptr`, which was never returned by the old API), but if that was
reachable the new behaviour should be better.
Differential Revision: https://reviews.llvm.org/D89414
Added:
Modified:
clang/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp
clang/lib/StaticAnalyzer/Core/BugReporter.cpp
clang/lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp
clang/lib/StaticAnalyzer/Core/IssueHash.cpp
clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp
clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp
clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
Removed:
################################################################################
diff --git a/clang/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp
index 252377f24bd7..28d3e058fee2 100644
--- a/clang/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp
@@ -1141,10 +1141,9 @@ void EmptyLocalizationContextChecker::MethodCrawler::VisitObjCMessageExpr(
SE = Mgr.getSourceManager().getSLocEntry(SLInfo.first);
}
- bool Invalid = false;
- const llvm::MemoryBuffer *BF =
- Mgr.getSourceManager().getBuffer(SLInfo.first, SL, &Invalid);
- if (Invalid)
+ llvm::Optional<llvm::MemoryBufferRef> BF =
+ Mgr.getSourceManager().getBufferOrNone(SLInfo.first, SL);
+ if (!BF)
return;
Lexer TheLexer(SL, LangOptions(), BF->getBufferStart(),
diff --git a/clang/lib/StaticAnalyzer/Core/BugReporter.cpp b/clang/lib/StaticAnalyzer/Core/BugReporter.cpp
index 72be4e81c83d..ebad1d1b67b4 100644
--- a/clang/lib/StaticAnalyzer/Core/BugReporter.cpp
+++ b/clang/lib/StaticAnalyzer/Core/BugReporter.cpp
@@ -1570,9 +1570,8 @@ static Optional<size_t> getLengthOnSingleLine(const SourceManager &SM,
if (FID != SM.getFileID(ExpansionRange.getEnd()))
return None;
- bool Invalid;
- const llvm::MemoryBuffer *Buffer = SM.getBuffer(FID, &Invalid);
- if (Invalid)
+ Optional<MemoryBufferRef> Buffer = SM.getBufferOrNone(FID);
+ if (!Buffer)
return None;
unsigned BeginOffset = SM.getFileOffset(ExpansionRange.getBegin());
diff --git a/clang/lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp b/clang/lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp
index bc7c41d039c4..68f1007569cd 100644
--- a/clang/lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp
+++ b/clang/lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp
@@ -786,8 +786,8 @@ void HTMLDiagnostics::HandlePiece(Rewriter &R, FileID BugFileID,
if (LPosInfo.first != BugFileID)
return;
- const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
- const char* FileStart = Buf->getBufferStart();
+ llvm::MemoryBufferRef Buf = SM.getBufferOrFake(LPosInfo.first);
+ const char *FileStart = Buf.getBufferStart();
// Compute the column number. Rewind from the current position to the start
// of the line.
@@ -797,7 +797,7 @@ void HTMLDiagnostics::HandlePiece(Rewriter &R, FileID BugFileID,
// Compute LineEnd.
const char *LineEnd = TokInstantiationPtr;
- const char* FileEnd = Buf->getBufferEnd();
+ const char *FileEnd = Buf.getBufferEnd();
while (*LineEnd != '\n' && LineEnd != FileEnd)
++LineEnd;
diff --git a/clang/lib/StaticAnalyzer/Core/IssueHash.cpp b/clang/lib/StaticAnalyzer/Core/IssueHash.cpp
index e7497f3fbdaa..2bea1593110a 100644
--- a/clang/lib/StaticAnalyzer/Core/IssueHash.cpp
+++ b/clang/lib/StaticAnalyzer/Core/IssueHash.cpp
@@ -120,7 +120,8 @@ static std::string GetEnclosingDeclContextSignature(const Decl *D) {
return "";
}
-static StringRef GetNthLineOfFile(const llvm::MemoryBuffer *Buffer, int Line) {
+static StringRef GetNthLineOfFile(llvm::Optional<llvm::MemoryBufferRef> Buffer,
+ int Line) {
if (!Buffer)
return "";
@@ -135,7 +136,7 @@ static std::string NormalizeLine(const SourceManager &SM, FullSourceLoc &L,
const LangOptions &LangOpts) {
static StringRef Whitespaces = " \t\n";
- StringRef Str = GetNthLineOfFile(SM.getBuffer(L.getFileID(), L),
+ StringRef Str = GetNthLineOfFile(SM.getBufferOrNone(L.getFileID(), L),
L.getExpansionLineNumber());
StringRef::size_type col = Str.find_first_not_of(Whitespaces);
if (col == StringRef::npos)
@@ -144,8 +145,8 @@ static std::string NormalizeLine(const SourceManager &SM, FullSourceLoc &L,
col++;
SourceLocation StartOfLine =
SM.translateLineCol(SM.getFileID(L), L.getExpansionLineNumber(), col);
- const llvm::MemoryBuffer *Buffer =
- SM.getBuffer(SM.getFileID(StartOfLine), StartOfLine);
+ Optional<llvm::MemoryBufferRef> Buffer =
+ SM.getBufferOrNone(SM.getFileID(StartOfLine), StartOfLine);
if (!Buffer)
return {};
diff --git a/clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp b/clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp
index 676d621e4978..da80f1543740 100644
--- a/clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp
+++ b/clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp
@@ -887,12 +887,12 @@ class TokenStream {
FileID File;
unsigned Offset;
std::tie(File, Offset) = SM.getDecomposedLoc(ExpanLoc);
- const llvm::MemoryBuffer *MB = SM.getBuffer(File);
- const char *MacroNameTokenPos = MB->getBufferStart() + Offset;
+ llvm::MemoryBufferRef MB = SM.getBufferOrFake(File);
+ const char *MacroNameTokenPos = MB.getBufferStart() + Offset;
RawLexer = std::make_unique<Lexer>(SM.getLocForStartOfFile(File), LangOpts,
- MB->getBufferStart(), MacroNameTokenPos,
- MB->getBufferEnd());
+ MB.getBufferStart(), MacroNameTokenPos,
+ MB.getBufferEnd());
}
void next(Token &Result) {
diff --git a/clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp b/clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp
index 8c2e85601576..6a9a13aed8d6 100644
--- a/clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp
+++ b/clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp
@@ -160,9 +160,8 @@ static unsigned int adjustColumnPos(const SourceManager &SM, SourceLocation Loc,
assert(LocInfo.second > SM.getExpansionColumnNumber(Loc) &&
"position in file is before column number?");
- bool InvalidBuffer = false;
- const MemoryBuffer *Buf = SM.getBuffer(LocInfo.first, &InvalidBuffer);
- assert(!InvalidBuffer && "got an invalid buffer for the location's file");
+ Optional<MemoryBufferRef> Buf = SM.getBufferOrNone(LocInfo.first);
+ assert(Buf && "got an invalid buffer for the location's file");
assert(Buf->getBufferSize() >= (LocInfo.second + TokenLen) &&
"token extends past end of buffer?");
diff --git a/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp b/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
index 392049e21c6e..38ed5a33a3f2 100644
--- a/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
+++ b/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
@@ -476,7 +476,7 @@ void AnalysisConsumer::HandleDeclsCallGraph(const unsigned LocalTUDeclsSize) {
static bool isBisonFile(ASTContext &C) {
const SourceManager &SM = C.getSourceManager();
FileID FID = SM.getMainFileID();
- StringRef Buffer = SM.getBuffer(FID)->getBuffer();
+ StringRef Buffer = SM.getBufferOrFake(FID).getBuffer();
if (Buffer.startswith("/* A Bison parser, made by"))
return true;
return false;
More information about the cfe-commits
mailing list