mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
refactor SourceRange printing out of Verifier.cpp into methods on SourceRange and SourceLoc.
Swift SVN r1752
This commit is contained in:
59
lib/Basic/SourceLoc.cpp
Normal file
59
lib/Basic/SourceLoc.cpp
Normal 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user