SourceLocation.cpp revision 205219
1//==--- SourceLocation.cpp - Compact identifier for Source Files -*- C++ -*-==//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines accessor methods for the FullSourceLoc class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/SourceLocation.h"
15#include "clang/Basic/PrettyStackTrace.h"
16#include "clang/Basic/SourceManager.h"
17#include "llvm/Support/MemoryBuffer.h"
18#include "llvm/Support/raw_ostream.h"
19#include <cstdio>
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// PrettyStackTraceLoc
24//===----------------------------------------------------------------------===//
25
26void PrettyStackTraceLoc::print(llvm::raw_ostream &OS) const {
27  if (Loc.isValid()) {
28    Loc.print(OS, SM);
29    OS << ": ";
30  }
31  OS << Message << '\n';
32}
33
34//===----------------------------------------------------------------------===//
35// SourceLocation
36//===----------------------------------------------------------------------===//
37
38void SourceLocation::print(llvm::raw_ostream &OS, const SourceManager &SM)const{
39  if (!isValid()) {
40    OS << "<invalid loc>";
41    return;
42  }
43
44  if (isFileID()) {
45    PresumedLoc PLoc = SM.getPresumedLoc(*this);
46    // The instantiation and spelling pos is identical for file locs.
47    OS << PLoc.getFilename() << ':' << PLoc.getLine()
48       << ':' << PLoc.getColumn();
49    return;
50  }
51
52  SM.getInstantiationLoc(*this).print(OS, SM);
53
54  OS << " <Spelling=";
55  SM.getSpellingLoc(*this).print(OS, SM);
56  OS << '>';
57}
58
59void SourceLocation::dump(const SourceManager &SM) const {
60  print(llvm::errs(), SM);
61}
62
63//===----------------------------------------------------------------------===//
64// FullSourceLoc
65//===----------------------------------------------------------------------===//
66
67FileID FullSourceLoc::getFileID() const {
68  assert(isValid());
69  return SrcMgr->getFileID(*this);
70}
71
72
73FullSourceLoc FullSourceLoc::getInstantiationLoc() const {
74  assert(isValid());
75  return FullSourceLoc(SrcMgr->getInstantiationLoc(*this), *SrcMgr);
76}
77
78FullSourceLoc FullSourceLoc::getSpellingLoc() const {
79  assert(isValid());
80  return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
81}
82
83unsigned FullSourceLoc::getInstantiationLineNumber() const {
84  assert(isValid());
85  return SrcMgr->getInstantiationLineNumber(*this);
86}
87
88unsigned FullSourceLoc::getInstantiationColumnNumber() const {
89  assert(isValid());
90  return SrcMgr->getInstantiationColumnNumber(*this);
91}
92
93unsigned FullSourceLoc::getSpellingLineNumber() const {
94  assert(isValid());
95  return SrcMgr->getSpellingLineNumber(*this);
96}
97
98unsigned FullSourceLoc::getSpellingColumnNumber() const {
99  assert(isValid());
100  return SrcMgr->getSpellingColumnNumber(*this);
101}
102
103bool FullSourceLoc::isInSystemHeader() const {
104  assert(isValid());
105  return SrcMgr->isInSystemHeader(*this);
106}
107
108const char *FullSourceLoc::getCharacterData() const {
109  assert(isValid());
110  return SrcMgr->getCharacterData(*this);
111}
112
113const llvm::MemoryBuffer* FullSourceLoc::getBuffer() const {
114  assert(isValid());
115  return SrcMgr->getBuffer(SrcMgr->getFileID(*this));
116}
117
118llvm::StringRef FullSourceLoc::getBufferData() const {
119  return getBuffer()->getBuffer();
120}
121
122std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
123  return SrcMgr->getDecomposedLoc(*this);
124}
125