clang API Documentation

SourceLocation.cpp
Go to the documentation of this file.
00001 //==--- SourceLocation.cpp - Compact identifier for Source Files -*- C++ -*-==//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines accessor methods for the FullSourceLoc class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/Basic/SourceLocation.h"
00015 #include "clang/Basic/PrettyStackTrace.h"
00016 #include "clang/Basic/SourceManager.h"
00017 #include "llvm/Support/MemoryBuffer.h"
00018 #include "llvm/Support/raw_ostream.h"
00019 #include <cstdio>
00020 using namespace clang;
00021 
00022 //===----------------------------------------------------------------------===//
00023 // PrettyStackTraceLoc
00024 //===----------------------------------------------------------------------===//
00025 
00026 void PrettyStackTraceLoc::print(raw_ostream &OS) const {
00027   if (Loc.isValid()) {
00028     Loc.print(OS, SM);
00029     OS << ": ";
00030   }
00031   OS << Message << '\n';
00032 }
00033 
00034 //===----------------------------------------------------------------------===//
00035 // SourceLocation
00036 //===----------------------------------------------------------------------===//
00037 
00038 void SourceLocation::print(raw_ostream &OS, const SourceManager &SM)const{
00039   if (!isValid()) {
00040     OS << "<invalid loc>";
00041     return;
00042   }
00043 
00044   if (isFileID()) {
00045     PresumedLoc PLoc = SM.getPresumedLoc(*this);
00046     
00047     if (PLoc.isInvalid()) {
00048       OS << "<invalid>";
00049       return;
00050     }
00051     // The macro expansion and spelling pos is identical for file locs.
00052     OS << PLoc.getFilename() << ':' << PLoc.getLine()
00053        << ':' << PLoc.getColumn();
00054     return;
00055   }
00056 
00057   SM.getExpansionLoc(*this).print(OS, SM);
00058 
00059   OS << " <Spelling=";
00060   SM.getSpellingLoc(*this).print(OS, SM);
00061   OS << '>';
00062 }
00063 
00064 void SourceLocation::dump(const SourceManager &SM) const {
00065   print(llvm::errs(), SM);
00066 }
00067 
00068 //===----------------------------------------------------------------------===//
00069 // FullSourceLoc
00070 //===----------------------------------------------------------------------===//
00071 
00072 FileID FullSourceLoc::getFileID() const {
00073   assert(isValid());
00074   return SrcMgr->getFileID(*this);
00075 }
00076 
00077 
00078 FullSourceLoc FullSourceLoc::getExpansionLoc() const {
00079   assert(isValid());
00080   return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr);
00081 }
00082 
00083 FullSourceLoc FullSourceLoc::getSpellingLoc() const {
00084   assert(isValid());
00085   return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
00086 }
00087 
00088 unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const {
00089   assert(isValid());
00090   return SrcMgr->getExpansionLineNumber(*this, Invalid);
00091 }
00092 
00093 unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const {
00094   assert(isValid());
00095   return SrcMgr->getExpansionColumnNumber(*this, Invalid);
00096 }
00097 
00098 unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const {
00099   assert(isValid());
00100   return SrcMgr->getSpellingLineNumber(*this, Invalid);
00101 }
00102 
00103 unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const {
00104   assert(isValid());
00105   return SrcMgr->getSpellingColumnNumber(*this, Invalid);
00106 }
00107 
00108 bool FullSourceLoc::isInSystemHeader() const {
00109   assert(isValid());
00110   return SrcMgr->isInSystemHeader(*this);
00111 }
00112 
00113 bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const {
00114   assert(isValid());
00115   return SrcMgr->isBeforeInTranslationUnit(*this, Loc);
00116 }
00117 
00118 void FullSourceLoc::dump() const {
00119   SourceLocation::dump(*SrcMgr);
00120 }
00121 
00122 const char *FullSourceLoc::getCharacterData(bool *Invalid) const {
00123   assert(isValid());
00124   return SrcMgr->getCharacterData(*this, Invalid);
00125 }
00126 
00127 const llvm::MemoryBuffer* FullSourceLoc::getBuffer(bool *Invalid) const {
00128   assert(isValid());
00129   return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid);
00130 }
00131 
00132 StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
00133   return getBuffer(Invalid)->getBuffer();
00134 }
00135 
00136 std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
00137   return SrcMgr->getDecomposedLoc(*this);
00138 }