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_BASIC_SOURCEMANAGERINTERNALS_H
16 #define LLVM_CLANG_BASIC_SOURCEMANAGERINTERNALS_H
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Basic/SourceManager.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/Support/Allocator.h"
29 //===----------------------------------------------------------------------===//
30 // Line Table Implementation
31 //===----------------------------------------------------------------------===//
34 /// \brief The offset in this file that the line entry occurs at.
37 /// \brief The presumed line number of this line entry: \#line 4.
40 /// \brief The ID of the filename identified by this line entry:
41 /// \#line 4 "foo.c". This is -1 if not specified.
44 /// \brief Set the 0 if no flags, 1 if a system header,
45 SrcMgr::CharacteristicKind FileKind;
47 /// \brief The offset of the virtual include stack location,
48 /// which is manipulated by GNU linemarker directives.
50 /// If this is 0 then there is no virtual \#includer.
51 unsigned IncludeOffset;
53 static LineEntry get(unsigned Offs, unsigned Line, int Filename,
54 SrcMgr::CharacteristicKind FileKind,
55 unsigned IncludeOffset) {
59 E.FilenameID = Filename;
60 E.FileKind = FileKind;
61 E.IncludeOffset = IncludeOffset;
66 // needed for FindNearestLineEntry (upper_bound of LineEntry)
67 inline bool operator<(const LineEntry &lhs, const LineEntry &rhs) {
68 // FIXME: should check the other field?
69 return lhs.FileOffset < rhs.FileOffset;
72 inline bool operator<(const LineEntry &E, unsigned Offset) {
73 return E.FileOffset < Offset;
76 inline bool operator<(unsigned Offset, const LineEntry &E) {
77 return Offset < E.FileOffset;
80 /// \brief Used to hold and unique data used to represent \#line information.
82 /// \brief Map used to assign unique IDs to filenames in \#line directives.
84 /// This allows us to unique the filenames that
85 /// frequently reoccur and reference them with indices. FilenameIDs holds
86 /// the mapping from string -> ID, and FilenamesByID holds the mapping of ID
88 llvm::StringMap<unsigned, llvm::BumpPtrAllocator> FilenameIDs;
89 std::vector<llvm::StringMapEntry<unsigned>*> FilenamesByID;
91 /// \brief Map from FileIDs to a list of line entries (sorted by the offset
92 /// at which they occur in the file).
93 std::map<FileID, std::vector<LineEntry>> LineEntries;
98 FilenamesByID.clear();
102 unsigned getLineTableFilenameID(StringRef Str);
104 StringRef getFilename(unsigned ID) const {
105 assert(ID < FilenamesByID.size() && "Invalid FilenameID");
106 return FilenamesByID[ID]->getKey();
109 unsigned getNumFilenames() const { return FilenamesByID.size(); }
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 using iterator = std::map<FileID, std::vector<LineEntry>>::iterator;
124 iterator begin() { return LineEntries.begin(); }
125 iterator end() { return LineEntries.end(); }
127 /// \brief Add a new line entry that has already been encoded into
128 /// the internal representation of the line table.
129 void AddEntry(FileID FID, const std::vector<LineEntry> &Entries);
134 #endif // LLVM_CLANG_BASIC_SOURCEMANAGERINTERNALS_H