c2c/Clang/SourceManager.cpp
/// getColumnNumber - Return the column # for the specified file position.
/// this is significantly cheaper to compute than the line number.
Return the column # for the specified file position.
this is significantly cheaper to compute than the line number.
Return the column # for the specified file position . this is significantly cheaper to compute than the line number .
unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos,
                                        bool *Invalid) const {
  bool MyInvalid = false;
  llvm::MemoryBuffer *MemBuf = getBuffer(FID, &MyInvalid);
  if (Invalid)
    *Invalid = MyInvalid;
  if (MyInvalid)
    return 1;
  if (FilePos > MemBuf->getBufferSize()) {
    if (Invalid)
      *Invalid = true;
    return 1;
  }
  const char *Buf = MemBuf->getBufferStart();
  if (LastLineNoFileIDQuery == FID &&
      LastLineNoContentCache->SourceLineCache != nullptr &&
      LastLineNoResult < LastLineNoContentCache->NumLines) {
    unsigned *SourceLineCache = LastLineNoContentCache->SourceLineCache;
    unsigned LineStart = SourceLineCache[LastLineNoResult - 1];
    unsigned LineEnd = SourceLineCache[LastLineNoResult];
    if (FilePos >= LineStart && FilePos < LineEnd) {
      if (FilePos + 1 == LineEnd && FilePos > LineStart) {
        if (Buf[FilePos - 1] == '\r' || Buf[FilePos - 1] == '\n')
          --FilePos;
      }
      return FilePos - LineStart + 1;
    }
  }
  unsigned LineStart = FilePos;
  while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
    --LineStart;
  return FilePos-LineStart+1;
}
unsigned SourceManager :: getColumnNumber ( FileID FID , unsigned FilePos , bool * Invalid ) const { bool MyInvalid = false ; llvm :: MemoryBuffer * MemBuf = getBuffer ( FID , & MyInvalid ) ; if ( Invalid ) * Invalid = MyInvalid ; if ( MyInvalid ) return 1 ; if ( FilePos > MemBuf -> getBufferSize ( ) ) { if ( Invalid ) * Invalid = true ; return 1 ; } const char * Buf = MemBuf -> getBufferStart ( ) ; if ( LastLineNoFileIDQuery == FID && LastLineNoContentCache -> SourceLineCache != nullptr && LastLineNoResult < LastLineNoContentCache -> NumLines ) { unsigned * SourceLineCache = LastLineNoContentCache -> SourceLineCache ; unsigned LineStart = SourceLineCache [ LastLineNoResult - 1 ] ; unsigned LineEnd = SourceLineCache [ LastLineNoResult ] ; if ( FilePos >= LineStart && FilePos < LineEnd ) { if ( FilePos + 1 == LineEnd && FilePos > LineStart ) { if ( Buf [ FilePos - 1 ] == ' \r ' || Buf [ FilePos - 1 ] == ' \n ' ) -- FilePos ; } return FilePos - LineStart + 1 ; } } unsigned LineStart = FilePos ; while ( LineStart && Buf [ LineStart - 1 ] != ' \n ' && Buf [ LineStart - 1 ] != ' \r ' ) -- LineStart ; return FilePos - LineStart + 1 ; }
getColumnNumber - Return the column # for the specified file position.
getColumnNumber - Return the column # for the specified file position .
// It is okay to request a position just past the end of the buffer. // See if we just calculated the line number for this FilePos and can use // that to lookup the start of the line instead of searching for it. // LineEnd is the LineStart of the next line. // A line ends with separator LF or CR+LF on Windows. // FilePos might point to the last separator, // but we need a column number at most 1 + the last column.