c2c/Clang/SourceManager.cpp
/// Get the source location in \arg FID for the given line:col.
/// Returns null location if \arg FID is not a file SLocEntry.
Get the source location in \arg FID for the given line:col.
Returns null location if \arg FID is not a file SLocEntry.
Get the source location in \ arg FID for the given line : col . Returns null location if \ arg FID is not a file SLocEntry .
SourceLocation SourceManager::translateLineCol(FileID FID,
                                               unsigned Line,
                                               unsigned Col) const {
  assert(Line && Col && "Line and column should start from 1!");
  if (FID.isInvalid())
    return SourceLocation();
  bool Invalid = false;
  const SLocEntry &Entry = getSLocEntry(FID, &Invalid);
  if (Invalid)
    return SourceLocation();
  if (!Entry.isFile())
    return SourceLocation();
  SourceLocation FileLoc = SourceLocation::getFileLoc(Entry.getOffset());
  if (Line == 1 && Col == 1)
    return FileLoc;
  ContentCache *Content
    = const_cast<ContentCache *>(Entry.getFile().getContentCache());
  if (!Content)
    return SourceLocation();
  if (!Content->SourceLineCache) {
    bool MyInvalid = false;
    ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid);
    if (MyInvalid)
      return SourceLocation();
  }
  if (Line > Content->NumLines) {
    unsigned Size = Content->getBuffer(Diag, *this)->getBufferSize();
    if (Size > 0)
      --Size;
    return FileLoc.getLocWithOffset(Size);
  }
  llvm::MemoryBuffer *Buffer = Content->getBuffer(Diag, *this);
  unsigned FilePos = Content->SourceLineCache[Line - 1];
  const char *Buf = Buffer->getBufferStart() + FilePos;
  unsigned BufLength = Buffer->getBufferSize() - FilePos;
  if (BufLength == 0)
    return FileLoc.getLocWithOffset(FilePos);
  unsigned i = 0;
  while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r')
    ++i;
  return FileLoc.getLocWithOffset(FilePos + i);
}
SourceLocation SourceManager :: translateLineCol ( FileID FID , unsigned Line , unsigned Col ) const { assert ( Line && Col && " " ) ; if ( FID . isInvalid ( ) ) return SourceLocation ( ) ; bool Invalid = false ; const SLocEntry & Entry = getSLocEntry ( FID , & Invalid ) ; if ( Invalid ) return SourceLocation ( ) ; if ( ! Entry . isFile ( ) ) return SourceLocation ( ) ; SourceLocation FileLoc = SourceLocation :: getFileLoc ( Entry . getOffset ( ) ) ; if ( Line == 1 && Col == 1 ) return FileLoc ; ContentCache * Content = const_cast < ContentCache * > ( Entry . getFile ( ) . getContentCache ( ) ) ; if ( ! Content ) return SourceLocation ( ) ; if ( ! Content -> SourceLineCache ) { bool MyInvalid = false ; ComputeLineNumbers ( Diag , Content , ContentCacheAlloc , * this , MyInvalid ) ; if ( MyInvalid ) return SourceLocation ( ) ; } if ( Line > Content -> NumLines ) { unsigned Size = Content -> getBuffer ( Diag , * this ) -> getBufferSize ( ) ; if ( Size > 0 ) -- Size ; return FileLoc . getLocWithOffset ( Size ) ; } llvm :: MemoryBuffer * Buffer = Content -> getBuffer ( Diag , * this ) ; unsigned FilePos = Content -> SourceLineCache [ Line - 1 ] ; const char * Buf = Buffer -> getBufferStart ( ) + FilePos ; unsigned BufLength = Buffer -> getBufferSize ( ) - FilePos ; if ( BufLength == 0 ) return FileLoc . getLocWithOffset ( FilePos ) ; unsigned i = 0 ; while ( i < BufLength - 1 && i < Col - 1 && Buf [ i ] != ' \n ' && Buf [ i ] != ' \r ' ) ++ i ; return FileLoc . getLocWithOffset ( FilePos + i ) ; }
Get the source location in \arg FID for the given line:col.
Returns null location if \arg FID is not a file SLocEntry.
Get the source location in \ arg FID for the given line : col . Returns null location if \ arg FID is not a file SLocEntry .
// Lines are used as a one-based index into a zero-based array. This assert // checks for possible buffer underruns. // If this is the first use of line information for this buffer, compute the // SourceLineCache for it on demand. // Check that the given column is valid.