diff options
Diffstat (limited to 'clang/lib/Basic')
-rw-r--r-- | clang/lib/Basic/SourceLocation.cpp | 52 | ||||
-rw-r--r-- | clang/lib/Basic/SourceManager.cpp | 29 |
2 files changed, 44 insertions, 37 deletions
diff --git a/clang/lib/Basic/SourceLocation.cpp b/clang/lib/Basic/SourceLocation.cpp index f0c8274de91..5d484721bd4 100644 --- a/clang/lib/Basic/SourceLocation.cpp +++ b/clang/lib/Basic/SourceLocation.cpp @@ -26,6 +26,29 @@ SourceLocation SourceLocation::ReadVal(llvm::Deserializer& D) { return SourceLocation::getFromRawEncoding(D.ReadInt()); } +void SourceLocation::dump(const SourceManager &SM) const { + if (!isValid()) { + fprintf(stderr, "<invalid loc>"); + return; + } + + if (isFileID()) { + PresumedLoc PLoc = SM.getPresumedLoc(*this); + + // The instantiation and spelling pos is identical for file locs. + fprintf(stderr, "%s:%d:%d", + PLoc.getFilename(), PLoc.getLine(), PLoc.getColumn()); + return; + } + + SM.getInstantiationLoc(*this).dump(SM); + + fprintf(stderr, " <Spelling="); + SM.getSpellingLoc(*this).dump(SM); + fprintf(stderr, ">"); +} + + void SourceRange::Emit(llvm::Serializer& S) const { B.Emit(S); E.Emit(S); @@ -53,11 +76,6 @@ FullSourceLoc FullSourceLoc::getSpellingLoc() const { return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr); } -FullSourceLoc FullSourceLoc::getIncludeLoc() const { - assert(isValid()); - return FullSourceLoc(SrcMgr->getIncludeLoc(*this), *SrcMgr); -} - unsigned FullSourceLoc::getLineNumber() const { assert(isValid()); return SrcMgr->getLineNumber(*this); @@ -89,11 +107,6 @@ unsigned FullSourceLoc::getSpellingColumnNumber() const { return SrcMgr->getSpellingColumnNumber(*this); } -const char* FullSourceLoc::getSourceName() const { - assert(isValid()); - return SrcMgr->getSourceName(*this); -} - bool FullSourceLoc::isInSystemHeader() const { assert(isValid()); return SrcMgr->isInSystemHeader(*this); @@ -109,22 +122,3 @@ const llvm::MemoryBuffer* FullSourceLoc::getBuffer() const { return SrcMgr->getBuffer(SrcMgr->getFileID(*this)); } -void FullSourceLoc::dump() const { - if (!isValid()) { - fprintf(stderr, "Invalid Loc\n"); - return; - } - - if (isFileID()) { - // The instantiation and spelling pos is identical for file locs. - fprintf(stderr, "File Loc from '%s': %d: %d\n", - getSourceName(), getInstantiationLineNumber(), - getInstantiationColumnNumber()); - } else { - fprintf(stderr, "Macro Loc (\n Spelling: "); - getSpellingLoc().dump(); - fprintf(stderr, " Instantiation: "); - getInstantiationLoc().dump(); - fprintf(stderr, ")\n"); - } -} diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index 9b509a56845..88292cd42bd 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -552,18 +552,31 @@ unsigned SourceManager::getLineNumber(SourceLocation Loc) const { return LineNo; } -/// getSourceName - This method returns the name of the file or buffer that -/// the SourceLocation specifies. This can be modified with #line directives, -/// etc. -const char *SourceManager::getSourceName(SourceLocation Loc) const { - if (Loc.isInvalid()) return ""; +/// getPresumedLoc - This method returns the "presumed" location of a +/// SourceLocation specifies. A "presumed location" can be modified by #line +/// or GNU line marker directives. This provides a view on the data that a +/// user should see in diagnostics, for example. +/// +/// Note that a presumed location is always given as the instantiation point +/// of an instantiation location, not at the spelling location. +PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const { + if (Loc.isInvalid()) return PresumedLoc(); + + // Presumed locations are always for instantiation points. + Loc = getInstantiationLoc(Loc); - const SrcMgr::ContentCache *C = - getSLocEntry(getFileID(getSpellingLoc(Loc))).getFile().getContentCache(); + // FIXME: Could just decompose Loc once! + const SrcMgr::FileInfo &FI = getSLocEntry(getFileID(Loc)).getFile(); + const SrcMgr::ContentCache *C = FI.getContentCache(); + // To get the source name, first consult the FileEntry (if one exists) before // the MemBuffer as this will avoid unnecessarily paging in the MemBuffer. - return C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier(); + const char *Filename = + C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier(); + + return PresumedLoc(Filename, getLineNumber(Loc), getColumnNumber(Loc), + FI.getIncludeLoc()); } //===----------------------------------------------------------------------===// |