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 //===----------------------------------------------------------------------===//
11 /// \brief Defines implementation details of the clang::SourceManager class.
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 /// \brief The offset in this file that the line entry occurs at.
33 /// \brief The presumed line number of this line entry: \#line 4.
36 /// \brief The ID of the filename identified by this line entry:
37 /// \#line 4 "foo.c". This is -1 if not specified.
40 /// \brief Set the 0 if no flags, 1 if a system header,
41 SrcMgr::CharacteristicKind FileKind;
43 /// \brief The offset of the virtual include stack location,
44 /// which is manipulated by GNU linemarker directives.
46 /// If this is 0 then there is no virtual \#includer.
47 unsigned IncludeOffset;
49 static LineEntry get(unsigned Offs, unsigned Line, int Filename,
50 SrcMgr::CharacteristicKind FileKind,
51 unsigned IncludeOffset) {
55 E.FilenameID = Filename;
56 E.FileKind = FileKind;
57 E.IncludeOffset = IncludeOffset;
62 // needed for FindNearestLineEntry (upper_bound of LineEntry)
63 inline bool operator<(const LineEntry &lhs, const LineEntry &rhs) {
64 // FIXME: should check the other field?
65 return lhs.FileOffset < rhs.FileOffset;
68 inline bool operator<(const LineEntry &E, unsigned Offset) {
69 return E.FileOffset < Offset;
72 inline bool operator<(unsigned Offset, const LineEntry &E) {
73 return Offset < E.FileOffset;
76 /// \brief Used to hold and unique data used to represent \#line information.
78 /// \brief Map used to assign unique IDs to filenames in \#line directives.
80 /// This allows us to unique the filenames that
81 /// frequently reoccur and reference them with indices. FilenameIDs holds
82 /// the mapping from string -> ID, and FilenamesByID holds the mapping of ID
84 llvm::StringMap<unsigned, llvm::BumpPtrAllocator> FilenameIDs;
85 std::vector<llvm::StringMapEntry<unsigned>*> FilenamesByID;
87 /// \brief Map from FileIDs to a list of line entries (sorted by the offset
88 /// at which they occur in the file).
89 std::map<FileID, std::vector<LineEntry> > LineEntries;
96 FilenamesByID.clear();
102 unsigned getLineTableFilenameID(StringRef Str);
103 const char *getFilename(unsigned ID) const {
104 assert(ID < FilenamesByID.size() && "Invalid FilenameID");
105 return FilenamesByID[ID]->getKeyData();
107 unsigned getNumFilenames() const { return FilenamesByID.size(); }
109 void AddLineNote(FileID FID, unsigned Offset,
110 unsigned LineNo, int FilenameID);
111 void AddLineNote(FileID FID, unsigned Offset,
112 unsigned LineNo, int FilenameID,
113 unsigned EntryExit, SrcMgr::CharacteristicKind FileKind);
116 /// \brief Find the line entry nearest to FID that is before it.
118 /// If there is no line entry before \p Offset in \p FID, returns null.
119 const LineEntry *FindNearestLineEntry(FileID FID, unsigned Offset);
122 typedef std::map<FileID, std::vector<LineEntry> >::iterator iterator;
123 iterator begin() { return LineEntries.begin(); }
124 iterator end() { return LineEntries.end(); }
126 /// \brief Add a new line entry that has already been encoded into
127 /// the internal representation of the line table.
128 void AddEntry(FileID FID, const std::vector<LineEntry> &Entries);
131 } // end namespace clang