Home | History | Annotate | Download | only in Basic
      1 //===--- SourceManagerInternals.h - SourceManager Internals -----*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 //  This file defines the implementation details of the SourceManager
     11 //  class.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_CLANG_SOURCEMANAGER_INTERNALS_H
     16 #define LLVM_CLANG_SOURCEMANAGER_INTERNALS_H
     17 
     18 #include "clang/Basic/SourceManager.h"
     19 #include "llvm/ADT/StringMap.h"
     20 #include <map>
     21 
     22 namespace clang {
     23 
     24 //===----------------------------------------------------------------------===//
     25 // Line Table Implementation
     26 //===----------------------------------------------------------------------===//
     27 
     28 struct LineEntry {
     29   /// FileOffset - The offset in this file that the line entry occurs at.
     30   unsigned FileOffset;
     31 
     32   /// LineNo - The presumed line number of this line entry: #line 4.
     33   unsigned LineNo;
     34 
     35   /// FilenameID - The ID of the filename identified by this line entry:
     36   /// #line 4 "foo.c".  This is -1 if not specified.
     37   int FilenameID;
     38 
     39   /// Flags - Set the 0 if no flags, 1 if a system header,
     40   SrcMgr::CharacteristicKind FileKind;
     41 
     42   /// IncludeOffset - This is the offset of the virtual include stack location,
     43   /// which is manipulated by GNU linemarker directives.  If this is 0 then
     44   /// there is no virtual #includer.
     45   unsigned IncludeOffset;
     46 
     47   static LineEntry get(unsigned Offs, unsigned Line, int Filename,
     48                        SrcMgr::CharacteristicKind FileKind,
     49                        unsigned IncludeOffset) {
     50     LineEntry E;
     51     E.FileOffset = Offs;
     52     E.LineNo = Line;
     53     E.FilenameID = Filename;
     54     E.FileKind = FileKind;
     55     E.IncludeOffset = IncludeOffset;
     56     return E;
     57   }
     58 };
     59 
     60 // needed for FindNearestLineEntry (upper_bound of LineEntry)
     61 inline bool operator<(const LineEntry &lhs, const LineEntry &rhs) {
     62   // FIXME: should check the other field?
     63   return lhs.FileOffset < rhs.FileOffset;
     64 }
     65 
     66 inline bool operator<(const LineEntry &E, unsigned Offset) {
     67   return E.FileOffset < Offset;
     68 }
     69 
     70 inline bool operator<(unsigned Offset, const LineEntry &E) {
     71   return Offset < E.FileOffset;
     72 }
     73 
     74 /// LineTableInfo - This class is used to hold and unique data used to
     75 /// represent #line information.
     76 class LineTableInfo {
     77   /// FilenameIDs - This map is used to assign unique IDs to filenames in
     78   /// #line directives.  This allows us to unique the filenames that
     79   /// frequently reoccur and reference them with indices.  FilenameIDs holds
     80   /// the mapping from string -> ID, and FilenamesByID holds the mapping of ID
     81   /// to string.
     82   llvm::StringMap<unsigned, llvm::BumpPtrAllocator> FilenameIDs;
     83   std::vector<llvm::StringMapEntry<unsigned>*> FilenamesByID;
     84 
     85   /// LineEntries - This is a map from FileIDs to a list of line entries (sorted
     86   /// by the offset they occur in the file.
     87   std::map<int, std::vector<LineEntry> > LineEntries;
     88 public:
     89   LineTableInfo() {
     90   }
     91 
     92   void clear() {
     93     FilenameIDs.clear();
     94     FilenamesByID.clear();
     95     LineEntries.clear();
     96   }
     97 
     98   ~LineTableInfo() {}
     99 
    100   unsigned getLineTableFilenameID(StringRef Str);
    101   const char *getFilename(unsigned ID) const {
    102     assert(ID < FilenamesByID.size() && "Invalid FilenameID");
    103     return FilenamesByID[ID]->getKeyData();
    104   }
    105   unsigned getNumFilenames() const { return FilenamesByID.size(); }
    106 
    107   void AddLineNote(int FID, unsigned Offset,
    108                    unsigned LineNo, int FilenameID);
    109   void AddLineNote(int FID, unsigned Offset,
    110                    unsigned LineNo, int FilenameID,
    111                    unsigned EntryExit, SrcMgr::CharacteristicKind FileKind);
    112 
    113 
    114   /// FindNearestLineEntry - Find the line entry nearest to FID that is before
    115   /// it.  If there is no line entry before Offset in FID, return null.
    116   const LineEntry *FindNearestLineEntry(int FID, unsigned Offset);
    117 
    118   // Low-level access
    119   typedef std::map<int, std::vector<LineEntry> >::iterator iterator;
    120   iterator begin() { return LineEntries.begin(); }
    121   iterator end() { return LineEntries.end(); }
    122 
    123   /// \brief Add a new line entry that has already been encoded into
    124   /// the internal representation of the line table.
    125   void AddEntry(int FID, const std::vector<LineEntry> &Entries);
    126 };
    127 
    128 } // end namespace clang
    129 
    130 #endif
    131