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
This commit is contained in:
Duncan P. N. Exon Smith 2020-10-14 14:06:37 -04:00
parent 159b2d8e62
commit af4fb416bd
7 changed files with 20 additions and 22 deletions

View File

@ -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(),

View File

@ -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());

View File

@ -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;

View File

@ -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 {};

View File

@ -887,12 +887,12 @@ public:
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) {

View File

@ -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?");

View File

@ -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;