Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:07

0001 //===- RewriteBuffer.h - Buffer rewriting interface -------------*- 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 #ifndef LLVM_ADT_REWRITEBUFFER_H
0010 #define LLVM_ADT_REWRITEBUFFER_H
0011 
0012 #include "llvm/ADT/DeltaTree.h"
0013 #include "llvm/ADT/RewriteRope.h"
0014 #include "llvm/ADT/StringRef.h"
0015 
0016 namespace clang {
0017 class Rewriter;
0018 } // namespace clang
0019 
0020 namespace llvm {
0021 
0022 class raw_ostream;
0023 
0024 /// RewriteBuffer - As code is rewritten, SourceBuffer's from the original
0025 /// input with modifications get a new RewriteBuffer associated with them.  The
0026 /// RewriteBuffer captures the modified text itself as well as information used
0027 /// to map between SourceLocation's in the original input and offsets in the
0028 /// RewriteBuffer.  For example, if text is inserted into the buffer, any
0029 /// locations after the insertion point have to be mapped.
0030 class RewriteBuffer {
0031   friend class clang::Rewriter;
0032 
0033   /// Deltas - Keep track of all the deltas in the source code due to insertions
0034   /// and deletions.
0035   DeltaTree Deltas;
0036 
0037   RewriteRope Buffer;
0038 
0039 public:
0040   using iterator = RewriteRope::const_iterator;
0041 
0042   iterator begin() const { return Buffer.begin(); }
0043   iterator end() const { return Buffer.end(); }
0044   unsigned size() const { return Buffer.size(); }
0045 
0046   /// Initialize - Start this rewrite buffer out with a copy of the unmodified
0047   /// input buffer.
0048   void Initialize(const char *BufStart, const char *BufEnd) {
0049     Buffer.assign(BufStart, BufEnd);
0050   }
0051   void Initialize(StringRef Input) { Initialize(Input.begin(), Input.end()); }
0052 
0053   /// Write to \p Stream the result of applying all changes to the
0054   /// original buffer.
0055   /// Note that it isn't safe to use this function to overwrite memory mapped
0056   /// files in-place (PR17960). Consider using a higher-level utility such as
0057   /// Rewriter::overwriteChangedFiles() instead.
0058   ///
0059   /// The original buffer is not actually changed.
0060   raw_ostream &write(raw_ostream &Stream) const;
0061 
0062   /// RemoveText - Remove the specified text.
0063   void RemoveText(unsigned OrigOffset, unsigned Size,
0064                   bool removeLineIfEmpty = false);
0065 
0066   /// InsertText - Insert some text at the specified point, where the offset in
0067   /// the buffer is specified relative to the original SourceBuffer.  The
0068   /// text is inserted after the specified location.
0069   void InsertText(unsigned OrigOffset, StringRef Str, bool InsertAfter = true);
0070 
0071   /// InsertTextBefore - Insert some text before the specified point, where the
0072   /// offset in the buffer is specified relative to the original
0073   /// SourceBuffer. The text is inserted before the specified location.  This is
0074   /// method is the same as InsertText with "InsertAfter == false".
0075   void InsertTextBefore(unsigned OrigOffset, StringRef Str) {
0076     InsertText(OrigOffset, Str, false);
0077   }
0078 
0079   /// InsertTextAfter - Insert some text at the specified point, where the
0080   /// offset in the buffer is specified relative to the original SourceBuffer.
0081   /// The text is inserted after the specified location.
0082   void InsertTextAfter(unsigned OrigOffset, StringRef Str) {
0083     InsertText(OrigOffset, Str);
0084   }
0085 
0086   /// ReplaceText - This method replaces a range of characters in the input
0087   /// buffer with a new string.  This is effectively a combined "remove/insert"
0088   /// operation.
0089   void ReplaceText(unsigned OrigOffset, unsigned OrigLength, StringRef NewStr);
0090 
0091 private:
0092   /// getMappedOffset - Given an offset into the original SourceBuffer that this
0093   /// RewriteBuffer is based on, map it into the offset space of the
0094   /// RewriteBuffer.  If AfterInserts is true and if the OrigOffset indicates a
0095   /// position where text is inserted, the location returned will be after any
0096   /// inserted text at the position.
0097   unsigned getMappedOffset(unsigned OrigOffset,
0098                            bool AfterInserts = false) const {
0099     return Deltas.getDeltaAt(2 * OrigOffset + AfterInserts) + OrigOffset;
0100   }
0101 
0102   /// AddInsertDelta - When an insertion is made at a position, this
0103   /// method is used to record that information.
0104   void AddInsertDelta(unsigned OrigOffset, int Change) {
0105     return Deltas.AddDelta(2 * OrigOffset, Change);
0106   }
0107 
0108   /// AddReplaceDelta - When a replacement/deletion is made at a position, this
0109   /// method is used to record that information.
0110   void AddReplaceDelta(unsigned OrigOffset, int Change) {
0111     return Deltas.AddDelta(2 * OrigOffset + 1, Change);
0112   }
0113 };
0114 
0115 } // namespace llvm
0116 
0117 #endif // LLVM_ADT_REWRITEBUFFER_H