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 LLVM_DUMP_METHOD std::string
00065 SourceLocation::printToString(const SourceManager &SM) const {
00066   std::string S;
00067   llvm::raw_string_ostream OS(S);
00068   print(OS, SM);
00069   return OS.str();
00070 }
00071 
00072 LLVM_DUMP_METHOD void SourceLocation::dump(const SourceManager &SM) const {
00073   print(llvm::errs(), SM);
00074 }
00075 
00076 //===----------------------------------------------------------------------===//
00077 // FullSourceLoc
00078 //===----------------------------------------------------------------------===//
00079 
00080 FileID FullSourceLoc::getFileID() const {
00081   assert(isValid());
00082   return SrcMgr->getFileID(*this);
00083 }
00084 
00085 
00086 FullSourceLoc FullSourceLoc::getExpansionLoc() const {
00087   assert(isValid());
00088   return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr);
00089 }
00090 
00091 FullSourceLoc FullSourceLoc::getSpellingLoc() const {
00092   assert(isValid());
00093   return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
00094 }
00095 
00096 unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const {
00097   assert(isValid());
00098   return SrcMgr->getExpansionLineNumber(*this, Invalid);
00099 }
00100 
00101 unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const {
00102   assert(isValid());
00103   return SrcMgr->getExpansionColumnNumber(*this, Invalid);
00104 }
00105 
00106 unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const {
00107   assert(isValid());
00108   return SrcMgr->getSpellingLineNumber(*this, Invalid);
00109 }
00110 
00111 unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const {
00112   assert(isValid());
00113   return SrcMgr->getSpellingColumnNumber(*this, Invalid);
00114 }
00115 
00116 bool FullSourceLoc::isInSystemHeader() const {
00117   assert(isValid());
00118   return SrcMgr->isInSystemHeader(*this);
00119 }
00120 
00121 bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const {
00122   assert(isValid());
00123   return SrcMgr->isBeforeInTranslationUnit(*this, Loc);
00124 }
00125 
00126 LLVM_DUMP_METHOD void FullSourceLoc::dump() const {
00127   SourceLocation::dump(*SrcMgr);
00128 }
00129 
00130 const char *FullSourceLoc::getCharacterData(bool *Invalid) const {
00131   assert(isValid());
00132   return SrcMgr->getCharacterData(*this, Invalid);
00133 }
00134 
00135 StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
00136   assert(isValid());
00137   return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid)->getBuffer();;
00138 }
00139 
00140 std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
00141   return SrcMgr->getDecomposedLoc(*this);
00142 }