Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:51

0001 //===- SourceManagerInternals.h - SourceManager Internals -------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 /// \file
0010 /// Defines implementation details of the clang::SourceManager class.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CLANG_BASIC_SOURCEMANAGERINTERNALS_H
0015 #define LLVM_CLANG_BASIC_SOURCEMANAGERINTERNALS_H
0016 
0017 #include "clang/Basic/SourceLocation.h"
0018 #include "clang/Basic/SourceManager.h"
0019 #include "llvm/ADT/StringMap.h"
0020 #include "llvm/ADT/StringRef.h"
0021 #include "llvm/Support/Allocator.h"
0022 #include <cassert>
0023 #include <map>
0024 #include <vector>
0025 
0026 namespace clang {
0027 
0028 //===----------------------------------------------------------------------===//
0029 // Line Table Implementation
0030 //===----------------------------------------------------------------------===//
0031 
0032 struct LineEntry {
0033   /// The offset in this file that the line entry occurs at.
0034   unsigned FileOffset;
0035 
0036   /// The presumed line number of this line entry: \#line 4.
0037   unsigned LineNo;
0038 
0039   /// The ID of the filename identified by this line entry:
0040   /// \#line 4 "foo.c".  This is -1 if not specified.
0041   int FilenameID;
0042 
0043   /// Set the 0 if no flags, 1 if a system header,
0044   SrcMgr::CharacteristicKind FileKind;
0045 
0046   /// The offset of the virtual include stack location,
0047   /// which is manipulated by GNU linemarker directives.
0048   ///
0049   /// If this is 0 then there is no virtual \#includer.
0050   unsigned IncludeOffset;
0051 
0052   static LineEntry get(unsigned Offs, unsigned Line, int Filename,
0053                        SrcMgr::CharacteristicKind FileKind,
0054                        unsigned IncludeOffset) {
0055     LineEntry E;
0056     E.FileOffset = Offs;
0057     E.LineNo = Line;
0058     E.FilenameID = Filename;
0059     E.FileKind = FileKind;
0060     E.IncludeOffset = IncludeOffset;
0061     return E;
0062   }
0063 };
0064 
0065 // needed for FindNearestLineEntry (upper_bound of LineEntry)
0066 inline bool operator<(const LineEntry &lhs, const LineEntry &rhs) {
0067   // FIXME: should check the other field?
0068   return lhs.FileOffset < rhs.FileOffset;
0069 }
0070 
0071 inline bool operator<(const LineEntry &E, unsigned Offset) {
0072   return E.FileOffset < Offset;
0073 }
0074 
0075 inline bool operator<(unsigned Offset, const LineEntry &E) {
0076   return Offset < E.FileOffset;
0077 }
0078 
0079 /// Used to hold and unique data used to represent \#line information.
0080 class LineTableInfo {
0081   /// Map used to assign unique IDs to filenames in \#line directives.
0082   ///
0083   /// This allows us to unique the filenames that
0084   /// frequently reoccur and reference them with indices.  FilenameIDs holds
0085   /// the mapping from string -> ID, and FilenamesByID holds the mapping of ID
0086   /// to string.
0087   llvm::StringMap<unsigned, llvm::BumpPtrAllocator> FilenameIDs;
0088   std::vector<llvm::StringMapEntry<unsigned>*> FilenamesByID;
0089 
0090   /// Map from FileIDs to a list of line entries (sorted by the offset
0091   /// at which they occur in the file).
0092   std::map<FileID, std::vector<LineEntry>> LineEntries;
0093 
0094 public:
0095   void clear() {
0096     FilenameIDs.clear();
0097     FilenamesByID.clear();
0098     LineEntries.clear();
0099   }
0100 
0101   unsigned getLineTableFilenameID(StringRef Str);
0102 
0103   StringRef getFilename(unsigned ID) const {
0104     assert(ID < FilenamesByID.size() && "Invalid FilenameID");
0105     return FilenamesByID[ID]->getKey();
0106   }
0107 
0108   unsigned getNumFilenames() const { return FilenamesByID.size(); }
0109 
0110   void AddLineNote(FileID FID, unsigned Offset,
0111                    unsigned LineNo, int FilenameID,
0112                    unsigned EntryExit, SrcMgr::CharacteristicKind FileKind);
0113 
0114 
0115   /// Find the line entry nearest to FID that is before it.
0116   ///
0117   /// If there is no line entry before \p Offset in \p FID, returns null.
0118   const LineEntry *FindNearestLineEntry(FileID FID, unsigned Offset);
0119 
0120   // Low-level access
0121   using iterator = std::map<FileID, std::vector<LineEntry>>::iterator;
0122 
0123   iterator begin() { return LineEntries.begin(); }
0124   iterator end() { return LineEntries.end(); }
0125 
0126   /// Add a new line entry that has already been encoded into
0127   /// the internal representation of the line table.
0128   void AddEntry(FileID FID, const std::vector<LineEntry> &Entries);
0129 };
0130 
0131 } // namespace clang
0132 
0133 #endif // LLVM_CLANG_BASIC_SOURCEMANAGERINTERNALS_H