xref: /minix3/external/bsd/llvm/dist/clang/lib/Basic/SourceLocation.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //==--- SourceLocation.cpp - Compact identifier for Source Files -*- C++ -*-==//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc //  This file defines accessor methods for the FullSourceLoc class.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "clang/Basic/SourceLocation.h"
15f4a2713aSLionel Sambuc #include "clang/Basic/PrettyStackTrace.h"
16f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
17f4a2713aSLionel Sambuc #include "llvm/Support/MemoryBuffer.h"
18f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
19f4a2713aSLionel Sambuc #include <cstdio>
20f4a2713aSLionel Sambuc using namespace clang;
21f4a2713aSLionel Sambuc 
22f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
23f4a2713aSLionel Sambuc // PrettyStackTraceLoc
24f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
25f4a2713aSLionel Sambuc 
print(raw_ostream & OS) const26f4a2713aSLionel Sambuc void PrettyStackTraceLoc::print(raw_ostream &OS) const {
27f4a2713aSLionel Sambuc   if (Loc.isValid()) {
28f4a2713aSLionel Sambuc     Loc.print(OS, SM);
29f4a2713aSLionel Sambuc     OS << ": ";
30f4a2713aSLionel Sambuc   }
31f4a2713aSLionel Sambuc   OS << Message << '\n';
32f4a2713aSLionel Sambuc }
33f4a2713aSLionel Sambuc 
34f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
35f4a2713aSLionel Sambuc // SourceLocation
36f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
37f4a2713aSLionel Sambuc 
print(raw_ostream & OS,const SourceManager & SM) const38f4a2713aSLionel Sambuc void SourceLocation::print(raw_ostream &OS, const SourceManager &SM)const{
39f4a2713aSLionel Sambuc   if (!isValid()) {
40f4a2713aSLionel Sambuc     OS << "<invalid loc>";
41f4a2713aSLionel Sambuc     return;
42f4a2713aSLionel Sambuc   }
43f4a2713aSLionel Sambuc 
44f4a2713aSLionel Sambuc   if (isFileID()) {
45f4a2713aSLionel Sambuc     PresumedLoc PLoc = SM.getPresumedLoc(*this);
46f4a2713aSLionel Sambuc 
47f4a2713aSLionel Sambuc     if (PLoc.isInvalid()) {
48f4a2713aSLionel Sambuc       OS << "<invalid>";
49f4a2713aSLionel Sambuc       return;
50f4a2713aSLionel Sambuc     }
51f4a2713aSLionel Sambuc     // The macro expansion and spelling pos is identical for file locs.
52f4a2713aSLionel Sambuc     OS << PLoc.getFilename() << ':' << PLoc.getLine()
53f4a2713aSLionel Sambuc        << ':' << PLoc.getColumn();
54f4a2713aSLionel Sambuc     return;
55f4a2713aSLionel Sambuc   }
56f4a2713aSLionel Sambuc 
57f4a2713aSLionel Sambuc   SM.getExpansionLoc(*this).print(OS, SM);
58f4a2713aSLionel Sambuc 
59f4a2713aSLionel Sambuc   OS << " <Spelling=";
60f4a2713aSLionel Sambuc   SM.getSpellingLoc(*this).print(OS, SM);
61f4a2713aSLionel Sambuc   OS << '>';
62f4a2713aSLionel Sambuc }
63f4a2713aSLionel Sambuc 
64*0a6a1f1dSLionel Sambuc LLVM_DUMP_METHOD std::string
printToString(const SourceManager & SM) const65*0a6a1f1dSLionel Sambuc SourceLocation::printToString(const SourceManager &SM) const {
66f4a2713aSLionel Sambuc   std::string S;
67f4a2713aSLionel Sambuc   llvm::raw_string_ostream OS(S);
68f4a2713aSLionel Sambuc   print(OS, SM);
69f4a2713aSLionel Sambuc   return OS.str();
70f4a2713aSLionel Sambuc }
71f4a2713aSLionel Sambuc 
dump(const SourceManager & SM) const72*0a6a1f1dSLionel Sambuc LLVM_DUMP_METHOD void SourceLocation::dump(const SourceManager &SM) const {
73f4a2713aSLionel Sambuc   print(llvm::errs(), SM);
74f4a2713aSLionel Sambuc }
75f4a2713aSLionel Sambuc 
76f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
77f4a2713aSLionel Sambuc // FullSourceLoc
78f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
79f4a2713aSLionel Sambuc 
getFileID() const80f4a2713aSLionel Sambuc FileID FullSourceLoc::getFileID() const {
81f4a2713aSLionel Sambuc   assert(isValid());
82f4a2713aSLionel Sambuc   return SrcMgr->getFileID(*this);
83f4a2713aSLionel Sambuc }
84f4a2713aSLionel Sambuc 
85f4a2713aSLionel Sambuc 
getExpansionLoc() const86f4a2713aSLionel Sambuc FullSourceLoc FullSourceLoc::getExpansionLoc() const {
87f4a2713aSLionel Sambuc   assert(isValid());
88f4a2713aSLionel Sambuc   return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr);
89f4a2713aSLionel Sambuc }
90f4a2713aSLionel Sambuc 
getSpellingLoc() const91f4a2713aSLionel Sambuc FullSourceLoc FullSourceLoc::getSpellingLoc() const {
92f4a2713aSLionel Sambuc   assert(isValid());
93f4a2713aSLionel Sambuc   return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
94f4a2713aSLionel Sambuc }
95f4a2713aSLionel Sambuc 
getExpansionLineNumber(bool * Invalid) const96f4a2713aSLionel Sambuc unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const {
97f4a2713aSLionel Sambuc   assert(isValid());
98f4a2713aSLionel Sambuc   return SrcMgr->getExpansionLineNumber(*this, Invalid);
99f4a2713aSLionel Sambuc }
100f4a2713aSLionel Sambuc 
getExpansionColumnNumber(bool * Invalid) const101f4a2713aSLionel Sambuc unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const {
102f4a2713aSLionel Sambuc   assert(isValid());
103f4a2713aSLionel Sambuc   return SrcMgr->getExpansionColumnNumber(*this, Invalid);
104f4a2713aSLionel Sambuc }
105f4a2713aSLionel Sambuc 
getSpellingLineNumber(bool * Invalid) const106f4a2713aSLionel Sambuc unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const {
107f4a2713aSLionel Sambuc   assert(isValid());
108f4a2713aSLionel Sambuc   return SrcMgr->getSpellingLineNumber(*this, Invalid);
109f4a2713aSLionel Sambuc }
110f4a2713aSLionel Sambuc 
getSpellingColumnNumber(bool * Invalid) const111f4a2713aSLionel Sambuc unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const {
112f4a2713aSLionel Sambuc   assert(isValid());
113f4a2713aSLionel Sambuc   return SrcMgr->getSpellingColumnNumber(*this, Invalid);
114f4a2713aSLionel Sambuc }
115f4a2713aSLionel Sambuc 
isInSystemHeader() const116f4a2713aSLionel Sambuc bool FullSourceLoc::isInSystemHeader() const {
117f4a2713aSLionel Sambuc   assert(isValid());
118f4a2713aSLionel Sambuc   return SrcMgr->isInSystemHeader(*this);
119f4a2713aSLionel Sambuc }
120f4a2713aSLionel Sambuc 
isBeforeInTranslationUnitThan(SourceLocation Loc) const121f4a2713aSLionel Sambuc bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const {
122f4a2713aSLionel Sambuc   assert(isValid());
123f4a2713aSLionel Sambuc   return SrcMgr->isBeforeInTranslationUnit(*this, Loc);
124f4a2713aSLionel Sambuc }
125f4a2713aSLionel Sambuc 
dump() const126*0a6a1f1dSLionel Sambuc LLVM_DUMP_METHOD void FullSourceLoc::dump() const {
127f4a2713aSLionel Sambuc   SourceLocation::dump(*SrcMgr);
128f4a2713aSLionel Sambuc }
129f4a2713aSLionel Sambuc 
getCharacterData(bool * Invalid) const130f4a2713aSLionel Sambuc const char *FullSourceLoc::getCharacterData(bool *Invalid) const {
131f4a2713aSLionel Sambuc   assert(isValid());
132f4a2713aSLionel Sambuc   return SrcMgr->getCharacterData(*this, Invalid);
133f4a2713aSLionel Sambuc }
134f4a2713aSLionel Sambuc 
getBufferData(bool * Invalid) const135f4a2713aSLionel Sambuc StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
136*0a6a1f1dSLionel Sambuc   assert(isValid());
137*0a6a1f1dSLionel Sambuc   return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid)->getBuffer();;
138f4a2713aSLionel Sambuc }
139f4a2713aSLionel Sambuc 
getDecomposedLoc() const140f4a2713aSLionel Sambuc std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
141f4a2713aSLionel Sambuc   return SrcMgr->getDecomposedLoc(*this);
142f4a2713aSLionel Sambuc }
143