SourceManagerInternals.h 4.4 KB

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