SourceManagerInternals.h revision 940a273768b4c13f27e9582987604701deb6e495
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/SourceLocation.h"
19#include "clang/Basic/SourceManager.h"
20#include "llvm/ADT/StringMap.h"
21#include <map>
22
23namespace clang {
24
25//===----------------------------------------------------------------------===//
26// Line Table Implementation
27//===----------------------------------------------------------------------===//
28
29struct LineEntry {
30  /// FileOffset - The offset in this file that the line entry occurs at.
31  unsigned FileOffset;
32
33  /// LineNo - The presumed line number of this line entry: \#line 4.
34  unsigned LineNo;
35
36  /// FilenameID - The ID of the filename identified by this line entry:
37  /// \#line 4 "foo.c".  This is -1 if not specified.
38  int FilenameID;
39
40  /// Flags - Set the 0 if no flags, 1 if a system header,
41  SrcMgr::CharacteristicKind FileKind;
42
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;
47
48  static LineEntry get(unsigned Offs, unsigned Line, int Filename,
49                       SrcMgr::CharacteristicKind FileKind,
50                       unsigned IncludeOffset) {
51    LineEntry E;
52    E.FileOffset = Offs;
53    E.LineNo = Line;
54    E.FilenameID = Filename;
55    E.FileKind = FileKind;
56    E.IncludeOffset = IncludeOffset;
57    return E;
58  }
59};
60
61// needed for FindNearestLineEntry (upper_bound of LineEntry)
62inline bool operator<(const LineEntry &lhs, const LineEntry &rhs) {
63  // FIXME: should check the other field?
64  return lhs.FileOffset < rhs.FileOffset;
65}
66
67inline bool operator<(const LineEntry &E, unsigned Offset) {
68  return E.FileOffset < Offset;
69}
70
71inline bool operator<(unsigned Offset, const LineEntry &E) {
72  return Offset < E.FileOffset;
73}
74
75/// LineTableInfo - This class is used to hold and unique data used to
76/// represent \#line information.
77class LineTableInfo {
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
82  /// to string.
83  llvm::StringMap<unsigned, llvm::BumpPtrAllocator> FilenameIDs;
84  std::vector<llvm::StringMapEntry<unsigned>*> FilenamesByID;
85
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;
89public:
90  LineTableInfo() {
91  }
92
93  void clear() {
94    FilenameIDs.clear();
95    FilenamesByID.clear();
96    LineEntries.clear();
97  }
98
99  ~LineTableInfo() {}
100
101  unsigned getLineTableFilenameID(StringRef Str);
102  const char *getFilename(unsigned ID) const {
103    assert(ID < FilenamesByID.size() && "Invalid FilenameID");
104    return FilenamesByID[ID]->getKeyData();
105  }
106  unsigned getNumFilenames() const { return FilenamesByID.size(); }
107
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);
113
114
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);
118
119  // Low-level access
120  typedef std::map<FileID, std::vector<LineEntry> >::iterator iterator;
121  iterator begin() { return LineEntries.begin(); }
122  iterator end() { return LineEntries.end(); }
123
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);
127};
128
129} // end namespace clang
130
131#endif
132