]> granicus.if.org Git - clang/commitdiff
make SM::getColumnNumber take a predecomposed FileID/offset, which
authorChris Lattner <sabre@nondot.org>
Wed, 4 Feb 2009 00:55:58 +0000 (00:55 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 4 Feb 2009 00:55:58 +0000 (00:55 +0000)
makes it clear to clients that they have to pick an instantiation
or spelling location before calling it and allows optimization based
on that.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63698 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/SourceLocation.h
include/clang/Basic/SourceManager.h
lib/Analysis/GRExprEngine.cpp
lib/Basic/SourceLocation.cpp
lib/Basic/SourceManager.cpp
lib/Driver/HTMLDiagnostics.cpp
lib/Driver/TextDiagnosticPrinter.cpp

index 4a6ae1f29d0294d751940ac5c84982648150cb8a..5c7ef1ba4b81aba8d66b24ddc19aab3660762693 100644 (file)
@@ -201,7 +201,6 @@ public:
   FullSourceLoc getSpellingLoc() const;
 
   unsigned getLineNumber() const;
-  unsigned getColumnNumber() const;
   
   unsigned getInstantiationLineNumber() const;
   unsigned getInstantiationColumnNumber() const;
index dd76f7896f81441eb024e1134eb42c2f16ab02ae..340c2e562a3ba868dd9c65cc9713eedc6b7dcbec 100644 (file)
@@ -481,14 +481,9 @@ public:
   /// returns zero if the column number isn't known.  This may only be called on
   /// a file sloc, so you must choose a spelling or instantiation location
   /// before calling this method.
-  unsigned getColumnNumber(SourceLocation Loc) const;
-  
-  unsigned getSpellingColumnNumber(SourceLocation Loc) const {
-    return getColumnNumber(getSpellingLoc(Loc));
-  }
-  unsigned getInstantiationColumnNumber(SourceLocation Loc) const {
-    return getColumnNumber(getInstantiationLoc(Loc));
-  }
+  unsigned getColumnNumber(FileID FID, unsigned FilePos) const;
+  unsigned getSpellingColumnNumber(SourceLocation Loc) const;
+  unsigned getInstantiationColumnNumber(SourceLocation Loc) const;
   
   
   /// getLineNumber - Given a SourceLocation, return the spelling line number
index 5268e1324f5598e3ff516d58ae4617aca9cc6df3..616006cc8eaa686c5c885aed9366ad3df89c5183 100644 (file)
@@ -2783,8 +2783,10 @@ struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
           
           if (SLoc.isFileID()) {        
             Out << "\\lline="
-              << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
-              << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
+              << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
+              << " col="
+              << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
+              << "\\l";
           }
           
           if (GraphPrintCheckerState->isImplicitNullDeref(N))
@@ -2827,8 +2829,9 @@ struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
           
           if (SLoc.isFileID()) {
             Out << "\\lline="
-              << GraphPrintSourceManager->getLineNumber(SLoc) << " col="
-              << GraphPrintSourceManager->getColumnNumber(SLoc);
+              << GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
+              << " col="
+              << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
           }
             
           if (isa<SwitchStmt>(T)) {
index 43fa3a13a6f4b27629ca9480a2b806a00292a492..93f60fc8742c736d5401e49ff7d702400f39d012 100644 (file)
@@ -83,12 +83,6 @@ unsigned FullSourceLoc::getLineNumber() const {
   return SrcMgr->getLineNumber(*this);
 }
 
-unsigned FullSourceLoc::getColumnNumber() const {
-  assert(isValid());
-  return SrcMgr->getColumnNumber(*this);
-}
-
-
 unsigned FullSourceLoc::getInstantiationLineNumber() const {
   assert(isValid());
   return SrcMgr->getInstantiationLineNumber(*this);
index 54aed7c9f17150371299c62813d7b192513a4413..9d91debc43f926c06fabb0875cb8f87ed55a3757 100644 (file)
@@ -112,9 +112,6 @@ public:
 };
 } // namespace clang
 
-
-
-
 unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) {
   // Look up the filename in the string table, returning the pre-existing value
   // if it exists.
@@ -466,23 +463,28 @@ const char *SourceManager::getCharacterData(SourceLocation SL) const {
 
 
 /// getColumnNumber - Return the column # for the specified file position.
-/// 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 {
-  if (Loc.isInvalid()) return 0;
-  assert(Loc.isFileID() && "Don't know what part of instantiation loc to get");
+/// this is significantly cheaper to compute than the line number.
+unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos) const {
+  const char *Buf = getBuffer(FID)->getBufferStart();
   
-  std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
-  unsigned FilePos = LocInfo.second;
-  
-  const char *Buf = getBuffer(LocInfo.first)->getBufferStart();
-
   unsigned LineStart = FilePos;
   while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
     --LineStart;
   return FilePos-LineStart+1;
 }
 
+unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc) const {
+  std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
+  return getColumnNumber(LocInfo.first, LocInfo.second);
+}
+
+unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc) const {
+  std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
+  return getColumnNumber(LocInfo.first, LocInfo.second);
+}
+
+
+
 static void ComputeLineNumbers(ContentCache* FI,
                                llvm::BumpPtrAllocator &Alloc) DISABLE_INLINE;
 static void ComputeLineNumbers(ContentCache* FI, llvm::BumpPtrAllocator &Alloc){ 
@@ -634,8 +636,9 @@ PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
   if (Loc.isInvalid()) return PresumedLoc();
   
   // Presumed locations are always for instantiation points.
+  std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
   Loc = getInstantiationLoc(Loc);
-  
+
   // FIXME: Could just decompose Loc once!
   
   const SrcMgr::FileInfo &FI = getSLocEntry(getFileID(Loc)).getFile();
@@ -646,7 +649,8 @@ PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
   const char *Filename = 
     C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
   
-  return PresumedLoc(Filename, getLineNumber(Loc), getColumnNumber(Loc),
+  return PresumedLoc(Filename, getLineNumber(Loc),
+                     getColumnNumber(LocInfo.first, LocInfo.second),
                      FI.getIncludeLoc());
 }
 
index 6b60588ca56cf5f3fa403966d8e78a72f7776018..1e1190527f4d1b3be37bc3efea092c17cd03f417 100644 (file)
@@ -336,20 +336,19 @@ void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
     return;  
   
   SourceManager &SM = R.getSourceMgr();
-  FullSourceLoc LPos = Pos.getInstantiationLoc();
-  FileID FID = SM.getFileID(LPos);
-  assert(&LPos.getManager() == &SM && "SourceManagers are different!");
+  assert(&Pos.getManager() == &SM && "SourceManagers are different!");
+  std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
   
-  if (SM.getFileID(LPos) != BugFileID)
+  if (LPosInfo.first != BugFileID)
     return;
   
-  const llvm::MemoryBuffer *Buf = SM.getBuffer(FID);
+  const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
   const char* FileStart = Buf->getBufferStart();  
   
   // Compute the column number.  Rewind from the current position to the start
   // of the line.
-  unsigned ColNo = LPos.getColumnNumber();
-  const char *TokInstantiationPtr = LPos.getCharacterData();
+  unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
+  const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
   const char *LineStart = TokInstantiationPtr-ColNo;
 
   // Only compute LineEnd if we display below a line.
@@ -445,7 +444,7 @@ void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
     }
     
     SourceLocation Loc = 
-      SM.getLocForStartOfFile(FID).getFileLocWithOffset(DisplayPos);
+      SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
     R.InsertStrBefore(Loc, os.str());
   }
   
@@ -453,7 +452,7 @@ void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
   
   for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
         I != E; ++I)
-    HighlightRange(R, FID, *I);
+    HighlightRange(R, LPosInfo.first, *I);
 }
 
 void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
@@ -475,7 +474,7 @@ void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
     return;
     
   // Compute the column number of the end.
-  unsigned EndColNo = SM.getColumnNumber(InstantiationEnd);
+  unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd);
   unsigned OldEndColNo = EndColNo;
 
   if (EndColNo) {
index 27353b83443e47c2c3898a8e74264866c0ac712d..cf3733f2e9e45da21e12b6c6d47b0466c18f4dad 100644 (file)
@@ -56,7 +56,7 @@ void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
   // Compute the column number of the start.
   unsigned StartColNo = 0;
   if (StartLineNo == LineNo) {
-    StartColNo = SM.getColumnNumber(Begin);
+    StartColNo = SM.getInstantiationColumnNumber(Begin);
     if (StartColNo) --StartColNo;  // Zero base the col #.
   }
 
@@ -68,7 +68,7 @@ void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
   // Compute the column number of the end.
   unsigned EndColNo = CaretLine.size();
   if (EndLineNo == LineNo) {
-    EndColNo = SM.getColumnNumber(End);
+    EndColNo = SM.getInstantiationColumnNumber(End);
     if (EndColNo) {
       --EndColNo;  // Zero base the col #.