clang  14.0.0git
SourceManagerInternals.h
Go to the documentation of this file.
1 //===- SourceManagerInternals.h - SourceManager Internals -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// Defines implementation details of the clang::SourceManager class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_BASIC_SOURCEMANAGERINTERNALS_H
15 #define LLVM_CLANG_BASIC_SOURCEMANAGERINTERNALS_H
16 
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/Allocator.h"
22 #include <cassert>
23 #include <map>
24 #include <vector>
25 
26 namespace clang {
27 
28 //===----------------------------------------------------------------------===//
29 // Line Table Implementation
30 //===----------------------------------------------------------------------===//
31 
32 struct LineEntry {
33  /// The offset in this file that the line entry occurs at.
34  unsigned FileOffset;
35 
36  /// The presumed line number of this line entry: \#line 4.
37  unsigned LineNo;
38 
39  /// The ID of the filename identified by this line entry:
40  /// \#line 4 "foo.c". This is -1 if not specified.
42 
43  /// Set the 0 if no flags, 1 if a system header,
45 
46  /// The offset of the virtual include stack location,
47  /// which is manipulated by GNU linemarker directives.
48  ///
49  /// If this is 0 then there is no virtual \#includer.
50  unsigned IncludeOffset;
51 
52  static LineEntry get(unsigned Offs, unsigned Line, int Filename,
54  unsigned IncludeOffset) {
55  LineEntry E;
56  E.FileOffset = Offs;
57  E.LineNo = Line;
58  E.FilenameID = Filename;
59  E.FileKind = FileKind;
61  return E;
62  }
63 };
64 
65 // needed for FindNearestLineEntry (upper_bound of LineEntry)
66 inline bool operator<(const LineEntry &lhs, const LineEntry &rhs) {
67  // FIXME: should check the other field?
68  return lhs.FileOffset < rhs.FileOffset;
69 }
70 
71 inline bool operator<(const LineEntry &E, unsigned Offset) {
72  return E.FileOffset < Offset;
73 }
74 
75 inline bool operator<(unsigned Offset, const LineEntry &E) {
76  return Offset < E.FileOffset;
77 }
78 
79 /// Used to hold and unique data used to represent \#line information.
81  /// Map used to assign unique IDs to filenames in \#line directives.
82  ///
83  /// This allows us to unique the filenames that
84  /// frequently reoccur and reference them with indices. FilenameIDs holds
85  /// the mapping from string -> ID, and FilenamesByID holds the mapping of ID
86  /// to string.
87  llvm::StringMap<unsigned, llvm::BumpPtrAllocator> FilenameIDs;
88  std::vector<llvm::StringMapEntry<unsigned>*> FilenamesByID;
89 
90  /// Map from FileIDs to a list of line entries (sorted by the offset
91  /// at which they occur in the file).
92  std::map<FileID, std::vector<LineEntry>> LineEntries;
93 
94 public:
95  void clear() {
96  FilenameIDs.clear();
97  FilenamesByID.clear();
98  LineEntries.clear();
99  }
100 
101  unsigned getLineTableFilenameID(StringRef Str);
102 
103  StringRef getFilename(unsigned ID) const {
104  assert(ID < FilenamesByID.size() && "Invalid FilenameID");
105  return FilenamesByID[ID]->getKey();
106  }
107 
108  unsigned getNumFilenames() const { return FilenamesByID.size(); }
109 
110  void AddLineNote(FileID FID, unsigned Offset,
111  unsigned LineNo, int FilenameID,
112  unsigned EntryExit, SrcMgr::CharacteristicKind FileKind);
113 
114 
115  /// Find the line entry nearest to FID that is before it.
116  ///
117  /// If there is no line entry before \p Offset in \p FID, returns null.
118  const LineEntry *FindNearestLineEntry(FileID FID, unsigned Offset);
119 
120  // Low-level access
121  using iterator = std::map<FileID, std::vector<LineEntry>>::iterator;
122 
123  iterator begin() { return LineEntries.begin(); }
124  iterator end() { return LineEntries.end(); }
125 
126  /// 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);
129 };
130 
131 } // namespace clang
132 
133 #endif // LLVM_CLANG_BASIC_SOURCEMANAGERINTERNALS_H
clang::LineEntry
Definition: SourceManagerInternals.h:32
clang::LineEntry::LineNo
unsigned LineNo
The presumed line number of this line entry: #line 4.
Definition: SourceManagerInternals.h:37
clang::LineTableInfo::AddEntry
void AddEntry(FileID FID, const std::vector< LineEntry > &Entries)
Add a new line entry that has already been encoded into the internal representation of the line table...
Definition: SourceManager.cpp:259
Filename
StringRef Filename
Definition: Format.cpp:2333
SourceManager.h
clang::SrcMgr::CharacteristicKind
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
Definition: SourceManager.h:78
clang::LineTableInfo::getFilename
StringRef getFilename(unsigned ID) const
Definition: SourceManagerInternals.h:103
Offset
unsigned Offset
Definition: Format.cpp:2335
clang::LineEntry::FileKind
SrcMgr::CharacteristicKind FileKind
Set the 0 if no flags, 1 if a system header,.
Definition: SourceManagerInternals.h:44
clang::LineTableInfo::iterator
std::map< FileID, std::vector< LineEntry > >::iterator iterator
Definition: SourceManagerInternals.h:121
clang::LineTableInfo::clear
void clear()
Definition: SourceManagerInternals.h:95
clang::LineEntry::FileOffset
unsigned FileOffset
The offset in this file that the line entry occurs at.
Definition: SourceManagerInternals.h:34
clang::LineTableInfo::getNumFilenames
unsigned getNumFilenames() const
Definition: SourceManagerInternals.h:108
clang::LineTableInfo::AddLineNote
void AddLineNote(FileID FID, unsigned Offset, unsigned LineNo, int FilenameID, unsigned EntryExit, SrcMgr::CharacteristicKind FileKind)
Add a line note to the line table that indicates that there is a #line or GNU line marker at the spec...
Definition: SourceManager.cpp:205
Line
const AnnotatedLine * Line
Definition: UsingDeclarationsSorter.cpp:68
SourceLocation.h
clang::LineTableInfo::getLineTableFilenameID
unsigned getLineTableFilenameID(StringRef Str)
Definition: SourceManager.cpp:192
clang::LineTableInfo::FindNearestLineEntry
const LineEntry * FindNearestLineEntry(FileID FID, unsigned Offset)
Find the line entry nearest to FID that is before it.
Definition: SourceManager.cpp:240
clang::LineEntry::get
static LineEntry get(unsigned Offs, unsigned Line, int Filename, SrcMgr::CharacteristicKind FileKind, unsigned IncludeOffset)
Definition: SourceManagerInternals.h:52
clang::Builtin::ID
ID
Definition: Builtins.h:48
clang
Definition: CalledOnceCheck.h:17
clang::operator<
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
Definition: DeclarationName.h:540
clang::FileID
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Definition: SourceLocation.h:40
clang::LineTableInfo::end
iterator end()
Definition: SourceManagerInternals.h:124
clang::LineTableInfo
Used to hold and unique data used to represent #line information.
Definition: SourceManagerInternals.h:80
clang::LineEntry::IncludeOffset
unsigned IncludeOffset
The offset of the virtual include stack location, which is manipulated by GNU linemarker directives.
Definition: SourceManagerInternals.h:50
clang::LineEntry::FilenameID
int FilenameID
The ID of the filename identified by this line entry: #line 4 "foo.c".
Definition: SourceManagerInternals.h:41
clang::LineTableInfo::begin
iterator begin()
Definition: SourceManagerInternals.h:123