clang API Documentation

Rewriter.h
Go to the documentation of this file.
00001 //===--- Rewriter.h - Code rewriting interface ------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the Rewriter class, which is used for code
00011 //  transformations.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CLANG_REWRITER_H
00016 #define LLVM_CLANG_REWRITER_H
00017 
00018 #include "clang/Basic/SourceLocation.h"
00019 #include "clang/Rewrite/DeltaTree.h"
00020 #include "clang/Rewrite/RewriteRope.h"
00021 #include "llvm/ADT/StringRef.h"
00022 #include <cstring>
00023 #include <map>
00024 #include <string>
00025 
00026 namespace clang {
00027   class LangOptions;
00028   class Rewriter;
00029   class SourceManager;
00030   class Stmt;
00031 
00032 /// RewriteBuffer - As code is rewritten, SourceBuffer's from the original
00033 /// input with modifications get a new RewriteBuffer associated with them.  The
00034 /// RewriteBuffer captures the modified text itself as well as information used
00035 /// to map between SourceLocation's in the original input and offsets in the
00036 /// RewriteBuffer.  For example, if text is inserted into the buffer, any
00037 /// locations after the insertion point have to be mapped.
00038 class RewriteBuffer {
00039   friend class Rewriter;
00040   /// Deltas - Keep track of all the deltas in the source code due to insertions
00041   /// and deletions.
00042   DeltaTree Deltas;
00043 
00044   /// Buffer - This is the actual buffer itself.  Note that using a vector or
00045   /// string is a horribly inefficient way to do this, we should use a rope
00046   /// instead.
00047   typedef RewriteRope BufferTy;
00048   BufferTy Buffer;
00049 public:
00050   typedef BufferTy::const_iterator iterator;
00051   iterator begin() const { return Buffer.begin(); }
00052   iterator end() const { return Buffer.end(); }
00053   unsigned size() const { return Buffer.size(); }
00054 
00055   raw_ostream &write(raw_ostream &) const;
00056 
00057   /// RemoveText - Remove the specified text.
00058   void RemoveText(unsigned OrigOffset, unsigned Size,
00059                   bool removeLineIfEmpty = false);
00060 
00061   /// InsertText - Insert some text at the specified point, where the offset in
00062   /// the buffer is specified relative to the original SourceBuffer.  The
00063   /// text is inserted after the specified location.
00064   ///
00065   void InsertText(unsigned OrigOffset, StringRef Str,
00066                   bool InsertAfter = true);
00067 
00068 
00069   /// InsertTextBefore - Insert some text before the specified point, where the
00070   /// offset in the buffer is specified relative to the original
00071   /// SourceBuffer. The text is inserted before the specified location.  This is
00072   /// method is the same as InsertText with "InsertAfter == false".
00073   void InsertTextBefore(unsigned OrigOffset, StringRef Str) {
00074     InsertText(OrigOffset, Str, false);
00075   }
00076 
00077   /// InsertTextAfter - Insert some text at the specified point, where the
00078   /// offset in the buffer is specified relative to the original SourceBuffer.
00079   /// The text is inserted after the specified location.
00080   void InsertTextAfter(unsigned OrigOffset, StringRef Str) {
00081     InsertText(OrigOffset, Str);
00082   }
00083 
00084   /// ReplaceText - This method replaces a range of characters in the input
00085   /// buffer with a new string.  This is effectively a combined "remove/insert"
00086   /// operation.
00087   void ReplaceText(unsigned OrigOffset, unsigned OrigLength,
00088                    StringRef NewStr);
00089 
00090 private:  // Methods only usable by Rewriter.
00091 
00092   /// Initialize - Start this rewrite buffer out with a copy of the unmodified
00093   /// input buffer.
00094   void Initialize(const char *BufStart, const char *BufEnd) {
00095     Buffer.assign(BufStart, BufEnd);
00096   }
00097 
00098   /// getMappedOffset - Given an offset into the original SourceBuffer that this
00099   /// RewriteBuffer is based on, map it into the offset space of the
00100   /// RewriteBuffer.  If AfterInserts is true and if the OrigOffset indicates a
00101   /// position where text is inserted, the location returned will be after any
00102   /// inserted text at the position.
00103   unsigned getMappedOffset(unsigned OrigOffset,
00104                            bool AfterInserts = false) const{
00105     return Deltas.getDeltaAt(2*OrigOffset+AfterInserts)+OrigOffset;
00106   }
00107 
00108   /// AddInsertDelta - When an insertion is made at a position, this
00109   /// method is used to record that information.
00110   void AddInsertDelta(unsigned OrigOffset, int Change) {
00111     return Deltas.AddDelta(2*OrigOffset, Change);
00112   }
00113 
00114   /// AddReplaceDelta - When a replacement/deletion is made at a position, this
00115   /// method is used to record that information.
00116   void AddReplaceDelta(unsigned OrigOffset, int Change) {
00117     return Deltas.AddDelta(2*OrigOffset+1, Change);
00118   }
00119 };
00120 
00121 
00122 /// Rewriter - This is the main interface to the rewrite buffers.  Its primary
00123 /// job is to dispatch high-level requests to the low-level RewriteBuffers that
00124 /// are involved.
00125 class Rewriter {
00126   SourceManager *SourceMgr;
00127   const LangOptions *LangOpts;
00128   std::map<FileID, RewriteBuffer> RewriteBuffers;
00129 public:
00130   struct RewriteOptions {
00131     /// \brief Given a source range, true to include previous inserts at the
00132     /// beginning of the range as part of the range itself (true by default).
00133     bool IncludeInsertsAtBeginOfRange;
00134     /// \brief Given a source range, true to include previous inserts at the
00135     /// end of the range as part of the range itself (true by default).
00136     bool IncludeInsertsAtEndOfRange;
00137     /// \brief If true and removing some text leaves a blank line
00138     /// also remove the empty line (false by default).
00139     bool RemoveLineIfEmpty;
00140 
00141     RewriteOptions()
00142       : IncludeInsertsAtBeginOfRange(true),
00143         IncludeInsertsAtEndOfRange(true),
00144         RemoveLineIfEmpty(false) { }
00145   };
00146 
00147   typedef std::map<FileID, RewriteBuffer>::iterator buffer_iterator;
00148 
00149   explicit Rewriter(SourceManager &SM, const LangOptions &LO)
00150     : SourceMgr(&SM), LangOpts(&LO) {}
00151   explicit Rewriter() : SourceMgr(0), LangOpts(0) {}
00152 
00153   void setSourceMgr(SourceManager &SM, const LangOptions &LO) {
00154     SourceMgr = &SM;
00155     LangOpts = &LO;
00156   }
00157   SourceManager &getSourceMgr() const { return *SourceMgr; }
00158   const LangOptions &getLangOpts() const { return *LangOpts; }
00159 
00160   /// isRewritable - Return true if this location is a raw file location, which
00161   /// is rewritable.  Locations from macros, etc are not rewritable.
00162   static bool isRewritable(SourceLocation Loc) {
00163     return Loc.isFileID();
00164   }
00165 
00166   /// getRangeSize - Return the size in bytes of the specified range if they
00167   /// are in the same file.  If not, this returns -1.
00168   int getRangeSize(SourceRange Range,
00169                    RewriteOptions opts = RewriteOptions()) const;
00170   int getRangeSize(const CharSourceRange &Range,
00171                    RewriteOptions opts = RewriteOptions()) const;
00172 
00173   /// getRewrittenText - Return the rewritten form of the text in the specified
00174   /// range.  If the start or end of the range was unrewritable or if they are
00175   /// in different buffers, this returns an empty string.
00176   ///
00177   /// Note that this method is not particularly efficient.
00178   ///
00179   std::string getRewrittenText(SourceRange Range) const;
00180 
00181   /// InsertText - Insert the specified string at the specified location in the
00182   /// original buffer.  This method returns true (and does nothing) if the input
00183   /// location was not rewritable, false otherwise.
00184   ///
00185   /// \param indentNewLines if true new lines in the string are indented
00186   /// using the indentation of the source line in position \arg Loc.
00187   bool InsertText(SourceLocation Loc, StringRef Str,
00188                   bool InsertAfter = true, bool indentNewLines = false);
00189 
00190   /// InsertTextAfter - Insert the specified string at the specified location in
00191   ///  the original buffer.  This method returns true (and does nothing) if
00192   ///  the input location was not rewritable, false otherwise.  Text is
00193   ///  inserted after any other text that has been previously inserted
00194   ///  at the some point (the default behavior for InsertText).
00195   bool InsertTextAfter(SourceLocation Loc, StringRef Str) {
00196     return InsertText(Loc, Str);
00197   }
00198 
00199   /// \brief Insert the specified string after the token in the
00200   /// specified location.
00201   bool InsertTextAfterToken(SourceLocation Loc, StringRef Str);
00202 
00203   /// InsertText - Insert the specified string at the specified location in the
00204   /// original buffer.  This method returns true (and does nothing) if the input
00205   /// location was not rewritable, false otherwise.  Text is
00206   /// inserted before any other text that has been previously inserted
00207   /// at the some point.
00208   bool InsertTextBefore(SourceLocation Loc, StringRef Str) {
00209     return InsertText(Loc, Str, false);
00210   }
00211 
00212   /// RemoveText - Remove the specified text region.
00213   bool RemoveText(SourceLocation Start, unsigned Length,
00214                   RewriteOptions opts = RewriteOptions());
00215 
00216   /// \brief Remove the specified text region.
00217   bool RemoveText(CharSourceRange range,
00218                   RewriteOptions opts = RewriteOptions()) {
00219     return RemoveText(range.getBegin(), getRangeSize(range, opts), opts);
00220   }
00221 
00222   /// \brief Remove the specified text region.
00223   bool RemoveText(SourceRange range, RewriteOptions opts = RewriteOptions()) {
00224     return RemoveText(range.getBegin(), getRangeSize(range, opts), opts);
00225   }
00226 
00227   /// ReplaceText - This method replaces a range of characters in the input
00228   /// buffer with a new string.  This is effectively a combined "remove/insert"
00229   /// operation.
00230   bool ReplaceText(SourceLocation Start, unsigned OrigLength,
00231                    StringRef NewStr);
00232 
00233   /// ReplaceText - This method replaces a range of characters in the input
00234   /// buffer with a new string.  This is effectively a combined "remove/insert"
00235   /// operation.
00236   bool ReplaceText(SourceRange range, StringRef NewStr) {
00237     return ReplaceText(range.getBegin(), getRangeSize(range), NewStr);
00238   }
00239 
00240   /// ReplaceText - This method replaces a range of characters in the input
00241   /// buffer with a new string.  This is effectively a combined "remove/insert"
00242   /// operation.
00243   bool ReplaceText(SourceRange range, SourceRange replacementRange);
00244 
00245   /// ReplaceStmt - This replaces a Stmt/Expr with another, using the pretty
00246   /// printer to generate the replacement code.  This returns true if the input
00247   /// could not be rewritten, or false if successful.
00248   bool ReplaceStmt(Stmt *From, Stmt *To);
00249 
00250   /// \brief Increase indentation for the lines between the given source range.
00251   /// To determine what the indentation should be, 'parentIndent' is used
00252   /// that should be at a source location with an indentation one degree
00253   /// lower than the given range.
00254   bool IncreaseIndentation(CharSourceRange range, SourceLocation parentIndent);
00255   bool IncreaseIndentation(SourceRange range, SourceLocation parentIndent) {
00256     return IncreaseIndentation(CharSourceRange::getTokenRange(range),
00257                                parentIndent);
00258   }
00259 
00260   /// ConvertToString converts statement 'From' to a string using the
00261   /// pretty printer.
00262   std::string ConvertToString(Stmt *From);
00263 
00264   /// getEditBuffer - This is like getRewriteBufferFor, but always returns a
00265   /// buffer, and allows you to write on it directly.  This is useful if you
00266   /// want efficient low-level access to apis for scribbling on one specific
00267   /// FileID's buffer.
00268   RewriteBuffer &getEditBuffer(FileID FID);
00269 
00270   /// getRewriteBufferFor - Return the rewrite buffer for the specified FileID.
00271   /// If no modification has been made to it, return null.
00272   const RewriteBuffer *getRewriteBufferFor(FileID FID) const {
00273     std::map<FileID, RewriteBuffer>::const_iterator I =
00274       RewriteBuffers.find(FID);
00275     return I == RewriteBuffers.end() ? 0 : &I->second;
00276   }
00277 
00278   // Iterators over rewrite buffers.
00279   buffer_iterator buffer_begin() { return RewriteBuffers.begin(); }
00280   buffer_iterator buffer_end() { return RewriteBuffers.end(); }
00281 
00282 private:
00283   unsigned getLocationOffsetAndFileID(SourceLocation Loc, FileID &FID) const;
00284 };
00285 
00286 } // end namespace clang
00287 
00288 #endif