From: Chris Lattner Date: Wed, 4 Feb 2009 00:55:58 +0000 (+0000) Subject: make SM::getColumnNumber take a predecomposed FileID/offset, which X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7da5aea7669e6db3e593162b8a123aef06a04d07;p=clang make SM::getColumnNumber take a predecomposed FileID/offset, which 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 --- diff --git a/include/clang/Basic/SourceLocation.h b/include/clang/Basic/SourceLocation.h index 4a6ae1f29d..5c7ef1ba4b 100644 --- a/include/clang/Basic/SourceLocation.h +++ b/include/clang/Basic/SourceLocation.h @@ -201,7 +201,6 @@ public: FullSourceLoc getSpellingLoc() const; unsigned getLineNumber() const; - unsigned getColumnNumber() const; unsigned getInstantiationLineNumber() const; unsigned getInstantiationColumnNumber() const; diff --git a/include/clang/Basic/SourceManager.h b/include/clang/Basic/SourceManager.h index dd76f7896f..340c2e562a 100644 --- a/include/clang/Basic/SourceManager.h +++ b/include/clang/Basic/SourceManager.h @@ -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 diff --git a/lib/Analysis/GRExprEngine.cpp b/lib/Analysis/GRExprEngine.cpp index 5268e1324f..616006cc8e 100644 --- a/lib/Analysis/GRExprEngine.cpp +++ b/lib/Analysis/GRExprEngine.cpp @@ -2783,8 +2783,10 @@ struct VISIBILITY_HIDDEN DOTGraphTraits : 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 : if (SLoc.isFileID()) { Out << "\\lline=" - << GraphPrintSourceManager->getLineNumber(SLoc) << " col=" - << GraphPrintSourceManager->getColumnNumber(SLoc); + << GraphPrintSourceManager->getInstantiationLineNumber(SLoc) + << " col=" + << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc); } if (isa(T)) { diff --git a/lib/Basic/SourceLocation.cpp b/lib/Basic/SourceLocation.cpp index 43fa3a13a6..93f60fc874 100644 --- a/lib/Basic/SourceLocation.cpp +++ b/lib/Basic/SourceLocation.cpp @@ -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); diff --git a/lib/Basic/SourceManager.cpp b/lib/Basic/SourceManager.cpp index 54aed7c9f1..9d91debc43 100644 --- a/lib/Basic/SourceManager.cpp +++ b/lib/Basic/SourceManager.cpp @@ -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 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 LocInfo = getDecomposedSpellingLoc(Loc); + return getColumnNumber(LocInfo.first, LocInfo.second); +} + +unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc) const { + std::pair 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 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()); } diff --git a/lib/Driver/HTMLDiagnostics.cpp b/lib/Driver/HTMLDiagnostics.cpp index 6b60588ca5..1e1190527f 100644 --- a/lib/Driver/HTMLDiagnostics.cpp +++ b/lib/Driver/HTMLDiagnostics.cpp @@ -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 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) { diff --git a/lib/Driver/TextDiagnosticPrinter.cpp b/lib/Driver/TextDiagnosticPrinter.cpp index 27353b8344..cf3733f2e9 100644 --- a/lib/Driver/TextDiagnosticPrinter.cpp +++ b/lib/Driver/TextDiagnosticPrinter.cpp @@ -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 #.