<commit_before>//===--- Rewriter.h - Code rewriting interface ------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  This file defines the Rewriter class, which is used for code
//  transformations.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_REWRITER_H
#define LLVM_CLANG_REWRITER_H

#include "clang/Basic/SourceLocation.h"
#include "clang/Rewrite/RewriteRope.h"
#include <map>
#include <vector>
#include <cstring>
#include <string>

namespace clang {
  class SourceManager;
  class Rewriter;
  class Stmt;
  
/// SourceDelta - As code in the original input buffer is added and deleted,
/// SourceDelta records are used to keep track of how the input SourceLocation
/// object is mapped into the output buffer.
struct SourceDelta {
  unsigned FileLoc;
  int Delta;
  
  static SourceDelta get(unsigned Loc, int D) {
    SourceDelta Delta;
    Delta.FileLoc = Loc;
    Delta.Delta = D;
    return Delta;
  }
};


/// RewriteBuffer - As code is rewritten, SourceBuffer's from the original
/// input with modifications get a new RewriteBuffer associated with them.  The
/// RewriteBuffer captures the modified text itself as well as information used
/// to map between SourceLocation's in the original input and offsets in the
/// RewriteBuffer.  For example, if text is inserted into the buffer, any
/// locations after the insertion point have to be mapped.
class RewriteBuffer {
  friend class Rewriter;
  /// Deltas - Keep track of all the deltas in the source code due to insertions
  /// and deletions.  These are kept in sorted order based on the FileLoc.
  std::vector<SourceDelta> Deltas;
  
  /// Buffer - This is the actual buffer itself.  Note that using a vector or
  /// string is a horribly inefficient way to do this, we should use a rope
  /// instead.
  typedef RewriteRope BufferTy;
  BufferTy Buffer;
public:
  typedef BufferTy::const_iterator iterator;
  iterator begin() const { return Buffer.begin(); }
  iterator end() const { return Buffer.end(); }
  
private:  // Methods only usable by Rewriter.
  
  /// Initialize - Start this rewrite buffer out with a copy of the unmodified
  /// input buffer.
  void Initialize(const char *BufStart, const char *BufEnd) {
    Buffer.assign(BufStart, BufEnd);
  }
  
  /// getMappedOffset - Given an offset into the original SourceBuffer that this
  /// RewriteBuffer is based on, map it into the offset space of the
  /// RewriteBuffer.  If AfterInserts is true and if the OrigOffset indicates a
  /// position where text is inserted, the location returned will be after any
  /// inserted text at the position.
  unsigned getMappedOffset(unsigned OrigOffset, bool AfterInserts = false)const;
  
  
  /// AddDelta - When a change is made that shifts around the text buffer, this
  /// method is used to record that info.
  void AddDelta(unsigned OrigOffset, int Change);
  
  /// RemoveText - Remove the specified text.
  void RemoveText(unsigned OrigOffset, unsigned Size);
  
  /// InsertText - Insert some text at the specified point, where the offset in
  /// the buffer is specified relative to the original SourceBuffer.  The
  /// text is inserted after the specified location.
  ///
  void InsertText(unsigned OrigOffset, const char *StrData, unsigned StrLen,
                  bool InsertAfter = true);
  

  /// InsertTextBefore - Insert some text before the specified point,
  /// where the offset in the buffer is specified relative to the original
  /// SourceBuffer.
  ///
  void InsertTextBefore(unsigned OrigOffset, const char *StrData,
                        unsigned StrLen) {
    InsertText(OrigOffset, StrData, StrLen, false);
  }
  
  /// InsertText - Insert some text at the specified point, where the offset in
  /// the buffer is specified relative to the original SourceBuffer.  The
  /// text is inserted after the specified location.  This is method is the
  /// same as InsertText with "InsertAfter == false".
  void InsertTextAfter(unsigned OrigOffset, const char *StrData,
                       unsigned StrLen) {
    InsertText(OrigOffset, StrData, StrLen);
  }
  
  /// ReplaceText - This method replaces a range of characters in the input
  /// buffer with a new string.  This is effectively a combined "remove/insert"
  /// operation.
  void ReplaceText(unsigned OrigOffset, unsigned OrigLength,
                   const char *NewStr, unsigned NewLength);
  
};
  

/// Rewriter - This is the main interface to the rewrite buffers.  Its primary
/// job is to dispatch high-level requests to the low-level RewriteBuffers that
/// are involved.
class Rewriter {
  SourceManager *SourceMgr;
  
  std::map<unsigned, RewriteBuffer> RewriteBuffers;
public:
  explicit Rewriter(SourceManager &SM) : SourceMgr(&SM) {}
  explicit Rewriter() : SourceMgr(0) {}
  
  void setSourceMgr(SourceManager &SM) { SourceMgr = &SM; }
  SourceManager& getSourceMgr() { return *SourceMgr; }
  
  /// isRewritable - Return true if this location is a raw file location, which
  /// is rewritable.  Locations from macros, etc are not rewritable.
  static bool isRewritable(SourceLocation Loc) {
    return Loc.isFileID();
  }

  /// getRangeSize - Return the size in bytes of the specified range if they
  /// are in the same file.  If not, this returns -1.
  int getRangeSize(SourceRange Range) const;
  
  /// InsertText - Insert the specified string at the specified location in the
  /// original buffer.  This method returns true (and does nothing) if the input
  /// location was not rewritable, false otherwise.
  bool InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen,
                  bool InsertAfter = true);
  
  /// InsertTextAfter - Insert the specified string at the specified location in
  ///  the original buffer.  This method returns true (and does nothing) if 
  ///  the input location was not rewritable, false otherwise.  Text is
  ///  inserted after any other text that has been previously inserted
  ///  at the some point (the default behavior for InsertText).
  bool InsertTextAfter(SourceLocation Loc, const char *StrData,
                       unsigned StrLen) {
    return InsertText(Loc, StrData, StrLen);
  }    
  
  /// InsertText - Insert the specified string at the specified location in the
  /// original buffer.  This method returns true (and does nothing) if the input
  /// location was not rewritable, false otherwise.  Text is
  /// inserted before any other text that has been previously inserted
  /// at the some point.
  bool InsertTextBefore(SourceLocation Loc, const char *StrData,
                       unsigned StrLen) {
    return InsertText(Loc, StrData, StrLen, false);
  }


  bool InsertCStrBefore(SourceLocation Loc, const char* Str) {
    return InsertTextBefore(Loc, Str, strlen(Str));
  }
  
  
  bool InsertCStrAfter(SourceLocation Loc, const char* Str) {
    return InsertTextAfter(Loc, Str, strlen(Str));
  }
  
  bool InsertStrBefore(SourceLocation Loc, const std::string& S) {
    return S.empty() ? false : InsertTextBefore(Loc, S.c_str(), S.size());
  }

  bool InsertStrAfter(SourceLocation Loc, const std::string& S) {
    return S.empty() ? false : InsertTextAfter(Loc, S.c_str(), S.size());
  }
  <commit_msg>Minor speed tweak per Chris's suggestion: use &S[0] instead of S.c_str().<commit_after>//===--- Rewriter.h - Code rewriting interface ------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  This file defines the Rewriter class, which is used for code
//  transformations.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_REWRITER_H
#define LLVM_CLANG_REWRITER_H

#include "clang/Basic/SourceLocation.h"
#include "clang/Rewrite/RewriteRope.h"
#include <map>
#include <vector>
#include <cstring>
#include <string>

namespace clang {
  class SourceManager;
  class Rewriter;
  class Stmt;
  
/// SourceDelta - As code in the original input buffer is added and deleted,
/// SourceDelta records are used to keep track of how the input SourceLocation
/// object is mapped into the output buffer.
struct SourceDelta {
  unsigned FileLoc;
  int Delta;
  
  static SourceDelta get(unsigned Loc, int D) {
    SourceDelta Delta;
    Delta.FileLoc = Loc;
    Delta.Delta = D;
    return Delta;
  }
};


/// RewriteBuffer - As code is rewritten, SourceBuffer's from the original
/// input with modifications get a new RewriteBuffer associated with them.  The
/// RewriteBuffer captures the modified text itself as well as information used
/// to map between SourceLocation's in the original input and offsets in the
/// RewriteBuffer.  For example, if text is inserted into the buffer, any
/// locations after the insertion point have to be mapped.
class RewriteBuffer {
  friend class Rewriter;
  /// Deltas - Keep track of all the deltas in the source code due to insertions
  /// and deletions.  These are kept in sorted order based on the FileLoc.
  std::vector<SourceDelta> Deltas;
  
  /// Buffer - This is the actual buffer itself.  Note that using a vector or
  /// string is a horribly inefficient way to do this, we should use a rope
  /// instead.
  typedef RewriteRope BufferTy;
  BufferTy Buffer;
public:
  typedef BufferTy::const_iterator iterator;
  iterator begin() const { return Buffer.begin(); }
  iterator end() const { return Buffer.end(); }
  
private:  // Methods only usable by Rewriter.
  
  /// Initialize - Start this rewrite buffer out with a copy of the unmodified
  /// input buffer.
  void Initialize(const char *BufStart, const char *BufEnd) {
    Buffer.assign(BufStart, BufEnd);
  }
  
  /// getMappedOffset - Given an offset into the original SourceBuffer that this
  /// RewriteBuffer is based on, map it into the offset space of the
  /// RewriteBuffer.  If AfterInserts is true and if the OrigOffset indicates a
  /// position where text is inserted, the location returned will be after any
  /// inserted text at the position.
  unsigned getMappedOffset(unsigned OrigOffset, bool AfterInserts = false)const;
  
  
  /// AddDelta - When a change is made that shifts around the text buffer, this
  /// method is used to record that info.
  void AddDelta(unsigned OrigOffset, int Change);
  
  /// RemoveText - Remove the specified text.
  void RemoveText(unsigned OrigOffset, unsigned Size);
  
  /// InsertText - Insert some text at the specified point, where the offset in
  /// the buffer is specified relative to the original SourceBuffer.  The
  /// text is inserted after the specified location.
  ///
  void InsertText(unsigned OrigOffset, const char *StrData, unsigned StrLen,
                  bool InsertAfter = true);
  

  /// InsertTextBefore - Insert some text before the specified point,
  /// where the offset in the buffer is specified relative to the original
  /// SourceBuffer.
  ///
  void InsertTextBefore(unsigned OrigOffset, const char *StrData,
                        unsigned StrLen) {
    InsertText(OrigOffset, StrData, StrLen, false);
  }
  
  /// InsertText - Insert some text at the specified point, where the offset in
  /// the buffer is specified relative to the original SourceBuffer.  The
  /// text is inserted after the specified location.  This is method is the
  /// same as InsertText with "InsertAfter == false".
  void InsertTextAfter(unsigned OrigOffset, const char *StrData,
                       unsigned StrLen) {
    InsertText(OrigOffset, StrData, StrLen);
  }
  
  /// ReplaceText - This method replaces a range of characters in the input
  /// buffer with a new string.  This is effectively a combined "remove/insert"
  /// operation.
  void ReplaceText(unsigned OrigOffset, unsigned OrigLength,
                   const char *NewStr, unsigned NewLength);
  
};
  

/// Rewriter - This is the main interface to the rewrite buffers.  Its primary
/// job is to dispatch high-level requests to the low-level RewriteBuffers that
/// are involved.
class Rewriter {
  SourceManager *SourceMgr;
  
  std::map<unsigned, RewriteBuffer> RewriteBuffers;
public:
  explicit Rewriter(SourceManager &SM) : SourceMgr(&SM) {}
  explicit Rewriter() : SourceMgr(0) {}
  
  void setSourceMgr(SourceManager &SM) { SourceMgr = &SM; }
  SourceManager& getSourceMgr() { return *SourceMgr; }
  
  /// isRewritable - Return true if this location is a raw file location, which
  /// is rewritable.  Locations from macros, etc are not rewritable.
  static bool isRewritable(SourceLocation Loc) {
    return Loc.isFileID();
  }

  /// getRangeSize - Return the size in bytes of the specified range if they
  /// are in the same file.  If not, this returns -1.
  int getRangeSize(SourceRange Range) const;
  
  /// InsertText - Insert the specified string at the specified location in the
  /// original buffer.  This method returns true (and does nothing) if the input
  /// location was not rewritable, false otherwise.
  bool InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen,
                  bool InsertAfter = true);
  
  /// InsertTextAfter - Insert the specified string at the specified location in
  ///  the original buffer.  This method returns true (and does nothing) if 
  ///  the input location was not rewritable, false otherwise.  Text is
  ///  inserted after any other text that has been previously inserted
  ///  at the some point (the default behavior for InsertText).
  bool InsertTextAfter(SourceLocation Loc, const char *StrData,
                       unsigned StrLen) {
    return InsertText(Loc, StrData, StrLen);
  }    
  
  /// InsertText - Insert the specified string at the specified location in the
  /// original buffer.  This method returns true (and does nothing) if the input
  /// location was not rewritable, false otherwise.  Text is
  /// inserted before any other text that has been previously inserted
  /// at the some point.
  bool InsertTextBefore(SourceLocation Loc, const char *StrData,
                       unsigned StrLen) {
    return InsertText(Loc, StrData, StrLen, false);
  }


  bool InsertCStrBefore(SourceLocation Loc, const char* Str) {
    return InsertTextBefore(Loc, Str, strlen(Str));
  }
  
  
  bool InsertCStrAfter(SourceLocation Loc, const char* Str) {
    return InsertTextAfter(Loc, Str, strlen(Str));
  }
  
  bool InsertStrBefore(SourceLocation Loc, const std::string& S) {
    return S.empty() ? false : InsertTextBefore(Loc, &S[0], S.size());
  }

  bool InsertStrAfter(SourceLocation Loc, const std::string& S) {
    return S.empty() ? false : InsertTextAfter(Loc, &S[0], S.size());
  }
  
{'fraction_non_alphanumeric': 0.10743306247502331, 'fraction_numerical': 0.0004662315172505661, 'mean_word_length': 2.808014202383972, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3808411', 'n_tokens_mistral': 4059, 'n_tokens_neox': 3938, 'n_words': 1688}