1 //===--- SourceManagerInternals.h - SourceManager Internals -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the implementation details of the SourceManager
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_SOURCEMANAGER_INTERNALS_H
16 #define LLVM_CLANG_SOURCEMANAGER_INTERNALS_H
18 #include "clang/Basic/SourceManager.h"
19 #include "llvm/ADT/StringMap.h"
24 //===----------------------------------------------------------------------===//
25 // Line Table Implementation
26 //===----------------------------------------------------------------------===//
29 /// FileOffset - The offset in this file that the line entry occurs at.
32 /// LineNo - The presumed line number of this line entry: #line 4.
35 /// FilenameID - The ID of the filename identified by this line entry:
36 /// #line 4 "foo.c". This is -1 if not specified.
39 /// Flags - Set the 0 if no flags, 1 if a system header,
40 SrcMgr::CharacteristicKind FileKind;
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;
47 static LineEntry get(unsigned Offs, unsigned Line, int Filename,
48 SrcMgr::CharacteristicKind FileKind,
49 unsigned IncludeOffset) {
53 E.FilenameID = Filename;
54 E.FileKind = FileKind;
55 E.IncludeOffset = IncludeOffset;
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;
66 inline bool operator<(const LineEntry &E, unsigned Offset) {
67 return E.FileOffset < Offset;
70 inline bool operator<(unsigned Offset, const LineEntry &E) {
71 return Offset < E.FileOffset;
74 /// LineTableInfo - This class is used to hold and unique data used to
75 /// represent #line information.
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
82 llvm::StringMap<unsigned, llvm::BumpPtrAllocator> FilenameIDs;
83 std::vector<llvm::StringMapEntry<unsigned>*> FilenamesByID;
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<unsigned, std::vector<LineEntry> > LineEntries;
94 FilenamesByID.clear();
100 unsigned getLineTableFilenameID(const char *Ptr, unsigned Len);
101 const char *getFilename(unsigned ID) const {
102 assert(ID < FilenamesByID.size() && "Invalid FilenameID");
103 return FilenamesByID[ID]->getKeyData();
105 unsigned getNumFilenames() const { return FilenamesByID.size(); }
107 void AddLineNote(unsigned FID, unsigned Offset,
108 unsigned LineNo, int FilenameID);
109 void AddLineNote(unsigned FID, unsigned Offset,
110 unsigned LineNo, int FilenameID,
111 unsigned EntryExit, SrcMgr::CharacteristicKind FileKind);
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(unsigned FID, unsigned Offset);
119 typedef std::map<unsigned, std::vector<LineEntry> >::iterator iterator;
120 iterator begin() { return LineEntries.begin(); }
121 iterator end() { return LineEntries.end(); }
123 /// \brief Add a new line entry that has already been encoded into
124 /// the internal representation of the line table.
125 void AddEntry(unsigned FID, const std::vector<LineEntry> &Entries);
128 } // end namespace clang