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/SourceLocation.h"
19 #include "clang/Basic/SourceManager.h"
20 #include "llvm/ADT/StringMap.h"
25 //===----------------------------------------------------------------------===//
26 // Line Table Implementation
27 //===----------------------------------------------------------------------===//
30 /// FileOffset - The offset in this file that the line entry occurs at.
33 /// LineNo - The presumed line number of this line entry: \#line 4.
36 /// FilenameID - The ID of the filename identified by this line entry:
37 /// \#line 4 "foo.c". This is -1 if not specified.
40 /// Flags - Set the 0 if no flags, 1 if a system header,
41 SrcMgr::CharacteristicKind FileKind;
43 /// IncludeOffset - This is the offset of the virtual include stack location,
44 /// which is manipulated by GNU linemarker directives. If this is 0 then
45 /// there is no virtual \#includer.
46 unsigned IncludeOffset;
48 static LineEntry get(unsigned Offs, unsigned Line, int Filename,
49 SrcMgr::CharacteristicKind FileKind,
50 unsigned IncludeOffset) {
54 E.FilenameID = Filename;
55 E.FileKind = FileKind;
56 E.IncludeOffset = IncludeOffset;
61 // needed for FindNearestLineEntry (upper_bound of LineEntry)
62 inline bool operator<(const LineEntry &lhs, const LineEntry &rhs) {
63 // FIXME: should check the other field?
64 return lhs.FileOffset < rhs.FileOffset;
67 inline bool operator<(const LineEntry &E, unsigned Offset) {
68 return E.FileOffset < Offset;
71 inline bool operator<(unsigned Offset, const LineEntry &E) {
72 return Offset < E.FileOffset;
75 /// LineTableInfo - This class is used to hold and unique data used to
76 /// represent \#line information.
78 /// FilenameIDs - This map is used to assign unique IDs to filenames in
79 /// \#line directives. This allows us to unique the filenames that
80 /// frequently reoccur and reference them with indices. FilenameIDs holds
81 /// the mapping from string -> ID, and FilenamesByID holds the mapping of ID
83 llvm::StringMap<unsigned, llvm::BumpPtrAllocator> FilenameIDs;
84 std::vector<llvm::StringMapEntry<unsigned>*> FilenamesByID;
86 /// \brief Map from FileIDs to a list of line entries (sorted by the offset
87 /// at which they occur in the file).
88 std::map<FileID, std::vector<LineEntry> > LineEntries;
95 FilenamesByID.clear();
101 unsigned getLineTableFilenameID(StringRef Str);
102 const char *getFilename(unsigned ID) const {
103 assert(ID < FilenamesByID.size() && "Invalid FilenameID");
104 return FilenamesByID[ID]->getKeyData();
106 unsigned getNumFilenames() const { return FilenamesByID.size(); }
108 void AddLineNote(FileID FID, unsigned Offset,
109 unsigned LineNo, int FilenameID);
110 void AddLineNote(FileID FID, unsigned Offset,
111 unsigned LineNo, int FilenameID,
112 unsigned EntryExit, SrcMgr::CharacteristicKind FileKind);
115 /// FindNearestLineEntry - Find the line entry nearest to FID that is before
116 /// it. If there is no line entry before Offset in FID, return null.
117 const LineEntry *FindNearestLineEntry(FileID FID, unsigned Offset);
120 typedef std::map<FileID, std::vector<LineEntry> >::iterator iterator;
121 iterator begin() { return LineEntries.begin(); }
122 iterator end() { return LineEntries.end(); }
124 /// \brief Add a new line entry that has already been encoded into
125 /// the internal representation of the line table.
126 void AddEntry(FileID FID, const std::vector<LineEntry> &Entries);
129 } // end namespace clang