refactor SourceRange printing out of Verifier.cpp into methods on SourceRange and SourceLoc.

Swift SVN r1752
This commit is contained in:
Chris Lattner
2012-05-05 21:08:44 +00:00
parent 1357aea129
commit d77d0d3fe3
4 changed files with 74 additions and 33 deletions

59
lib/Basic/SourceLoc.cpp Normal file
View File

@@ -0,0 +1,59 @@
//===--- SourceLoc.cpp - SourceLoc and SourceRange implementations --------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Basic/SourceLoc.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/SourceMgr.h"
using namespace swift;
void SourceLoc::print(raw_ostream &OS, const llvm::SourceMgr &SM) const {
if (isInvalid()) {
OS << "<invalid loc>";
return;
}
int BufferIndex = SM.FindBufferContainingLoc(Value);
if (BufferIndex == -1) {
OS << "<malformed loc>";
return;
}
const llvm::MemoryBuffer *Buffer = SM.getMemoryBuffer((unsigned)BufferIndex);
const char *BufferStart = Buffer->getBufferStart();
OS << Buffer->getBufferIdentifier() << ':'
<< (Value.getPointer() - BufferStart);
}
void SourceLoc::dump(const llvm::SourceMgr &SM) const {
print(llvm::errs(), SM);
}
void SourceRange::print(raw_ostream &OS, const llvm::SourceMgr &SM) const {
OS << '[';
Start.print(OS, SM);
OS << " - ";
End.print(OS, SM);
OS << ']';
if (Start.isInvalid() || End.isInvalid())
return;
OS << " RangeText=\""
<< StringRef(Start.Value.getPointer(),
End.Value.getPointer() - Start.Value.getPointer())
<< '"';
}
void SourceRange::dump(const llvm::SourceMgr &SM) const {
print(llvm::errs(), SM);
}