Factor logical line lookup better.

llvm-svn: 38594
diff --git a/clang/Basic/SourceManager.cpp b/clang/Basic/SourceManager.cpp
index 357adfd..5861744 100644
--- a/clang/Basic/SourceManager.cpp
+++ b/clang/Basic/SourceManager.cpp
@@ -115,6 +115,7 @@
 ///
 unsigned SourceManager::createFileIDForMacroExp(SourceLocation SourcePos, 
                                                 unsigned PhysicalFileID) {
+  SourcePos = getLogicalLoc(SourcePos);
   FileIDs.push_back(FileIDInfo::getMacroExpansion(SourcePos, PhysicalFileID));
   return FileIDs.size();
 }
@@ -135,7 +136,7 @@
   const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(FileID);
 
   // For Macros, the physical loc is specified by the MacroTokenFileID.
-  while (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion)
+  if (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion)
     FIDInfo = &FileIDs[FIDInfo->u.MacroTokenFileID-1];
   
   return FIDInfo->IncludeLoc;
@@ -146,17 +147,10 @@
 /// this is significantly cheaper to compute than the line number.  This returns
 /// zero if the column number isn't known.
 unsigned SourceManager::getColumnNumber(SourceLocation Loc) const {
+  Loc = getLogicalLoc(Loc);
   unsigned FileID = Loc.getFileID();
   if (FileID == 0) return 0;
   
-  // If this is a macro, we need to get the instantiation location.
-  const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(FileID);
-  while (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion) {
-    Loc = FIDInfo->IncludeLoc;
-    FileID = Loc.getFileID();
-    FIDInfo = getFIDInfo(FileID);
-  }
-  
   unsigned FilePos = getFilePos(Loc);
   const SourceBuffer *Buffer = getBuffer(FileID);
   const char *Buf = Buffer->getBufferStart();
@@ -171,17 +165,10 @@
 /// the SourceLocation specifies.  This can be modified with #line directives,
 /// etc.
 std::string SourceManager::getSourceName(SourceLocation Loc) {
+  Loc = getLogicalLoc(Loc);
   unsigned FileID = Loc.getFileID();
   if (FileID == 0) return "";
-  
-  // If this is a macro, we need to get the instantiation location.
-  const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(FileID);
-  while (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion) {
-    Loc = FIDInfo->IncludeLoc;
-    FIDInfo = getFIDInfo(Loc.getFileID());
-  }
-  
-  return getFileInfo(FIDInfo)->Buffer->getBufferIdentifier();
+  return getFileInfo(FileID)->Buffer->getBufferIdentifier();
 }
 
 
@@ -190,16 +177,8 @@
 /// line offsets for the SourceBuffer, so this is not cheap: use only when
 /// about to emit a diagnostic.
 unsigned SourceManager::getLineNumber(SourceLocation Loc) {
-  unsigned FileID = Loc.getFileID();
-  // If this is a macro, we need to get the instantiation location.
-  const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(FileID);
-  while (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion) {
-    Loc = FIDInfo->IncludeLoc;
-    FileID = Loc.getFileID();
-    FIDInfo = getFIDInfo(FileID);
-  }
-
-  FileInfo *FileInfo = getFileInfo(FileID);
+  Loc = getLogicalLoc(Loc);
+  FileInfo *FileInfo = getFileInfo(Loc.getFileID());
   
   // If this is the first use of line information for this buffer, compute the
   /// SourceLineCache for it on demand.