From b9c3f966b103f7cfe8e5e60007c4c8b38f7298eb Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 27 Jan 2009 07:57:44 +0000 Subject: Introduce a new PresumedLoc class to represent the concept of a location as reported to the user and as manipulated by #line. This is what __FILE__, __INCLUDE_LEVEL__, diagnostics and other things should follow (but not dependency generation!). This patch also includes several cleanups along the way: - SourceLocation now has a dump method, and several other places that did similar things now use it. - I cleaned up some code in AnalysisConsumer, but it should probably be simplified further now that NamedDecl is better. - TextDiagnosticPrinter is now simplified and cleaned up a bit. This patch is a prerequisite for #line, but does not actually provide any #line functionality. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63098 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Basic/SourceManager.cpp | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'lib/Basic/SourceManager.cpp') diff --git a/lib/Basic/SourceManager.cpp b/lib/Basic/SourceManager.cpp index 9b509a5684..88292cd42b 100644 --- a/lib/Basic/SourceManager.cpp +++ b/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()); } //===----------------------------------------------------------------------===// -- cgit v1.2.3-18-g5258