Rewriter.h revision 243791
1243791Sdim//===--- Rewriter.h - Code rewriting interface ------------------*- C++ -*-===// 2243791Sdim// 3243791Sdim// The LLVM Compiler Infrastructure 4243791Sdim// 5243791Sdim// This file is distributed under the University of Illinois Open Source 6243791Sdim// License. See LICENSE.TXT for details. 7243791Sdim// 8243791Sdim//===----------------------------------------------------------------------===// 9243791Sdim// 10243791Sdim// This file defines the Rewriter class, which is used for code 11243791Sdim// transformations. 12243791Sdim// 13243791Sdim//===----------------------------------------------------------------------===// 14243791Sdim 15243791Sdim#ifndef LLVM_CLANG_REWRITER_H 16243791Sdim#define LLVM_CLANG_REWRITER_H 17243791Sdim 18243791Sdim#include "clang/Basic/SourceLocation.h" 19243791Sdim#include "clang/Rewrite/Core/DeltaTree.h" 20243791Sdim#include "clang/Rewrite/Core/RewriteRope.h" 21243791Sdim#include "llvm/ADT/StringRef.h" 22243791Sdim#include <cstring> 23243791Sdim#include <map> 24243791Sdim#include <string> 25243791Sdim 26243791Sdimnamespace clang { 27243791Sdim class LangOptions; 28243791Sdim class Rewriter; 29243791Sdim class SourceManager; 30243791Sdim class Stmt; 31243791Sdim 32243791Sdim/// RewriteBuffer - As code is rewritten, SourceBuffer's from the original 33243791Sdim/// input with modifications get a new RewriteBuffer associated with them. The 34243791Sdim/// RewriteBuffer captures the modified text itself as well as information used 35243791Sdim/// to map between SourceLocation's in the original input and offsets in the 36243791Sdim/// RewriteBuffer. For example, if text is inserted into the buffer, any 37243791Sdim/// locations after the insertion point have to be mapped. 38243791Sdimclass RewriteBuffer { 39243791Sdim friend class Rewriter; 40243791Sdim /// Deltas - Keep track of all the deltas in the source code due to insertions 41243791Sdim /// and deletions. 42243791Sdim DeltaTree Deltas; 43243791Sdim 44243791Sdim /// Buffer - This is the actual buffer itself. Note that using a vector or 45243791Sdim /// string is a horribly inefficient way to do this, we should use a rope 46243791Sdim /// instead. 47243791Sdim typedef RewriteRope BufferTy; 48243791Sdim BufferTy Buffer; 49243791Sdimpublic: 50243791Sdim typedef BufferTy::const_iterator iterator; 51243791Sdim iterator begin() const { return Buffer.begin(); } 52243791Sdim iterator end() const { return Buffer.end(); } 53243791Sdim unsigned size() const { return Buffer.size(); } 54243791Sdim 55243791Sdim raw_ostream &write(raw_ostream &) const; 56243791Sdim 57243791Sdim /// RemoveText - Remove the specified text. 58243791Sdim void RemoveText(unsigned OrigOffset, unsigned Size, 59243791Sdim bool removeLineIfEmpty = false); 60243791Sdim 61243791Sdim /// InsertText - Insert some text at the specified point, where the offset in 62243791Sdim /// the buffer is specified relative to the original SourceBuffer. The 63243791Sdim /// text is inserted after the specified location. 64243791Sdim /// 65243791Sdim void InsertText(unsigned OrigOffset, StringRef Str, 66243791Sdim bool InsertAfter = true); 67243791Sdim 68243791Sdim 69243791Sdim /// InsertTextBefore - Insert some text before the specified point, where the 70243791Sdim /// offset in the buffer is specified relative to the original 71243791Sdim /// SourceBuffer. The text is inserted before the specified location. This is 72243791Sdim /// method is the same as InsertText with "InsertAfter == false". 73243791Sdim void InsertTextBefore(unsigned OrigOffset, StringRef Str) { 74243791Sdim InsertText(OrigOffset, Str, false); 75243791Sdim } 76243791Sdim 77243791Sdim /// InsertTextAfter - Insert some text at the specified point, where the 78243791Sdim /// offset in the buffer is specified relative to the original SourceBuffer. 79243791Sdim /// The text is inserted after the specified location. 80243791Sdim void InsertTextAfter(unsigned OrigOffset, StringRef Str) { 81243791Sdim InsertText(OrigOffset, Str); 82243791Sdim } 83243791Sdim 84243791Sdim /// ReplaceText - This method replaces a range of characters in the input 85243791Sdim /// buffer with a new string. This is effectively a combined "remove/insert" 86243791Sdim /// operation. 87243791Sdim void ReplaceText(unsigned OrigOffset, unsigned OrigLength, 88243791Sdim StringRef NewStr); 89243791Sdim 90243791Sdimprivate: // Methods only usable by Rewriter. 91243791Sdim 92243791Sdim /// Initialize - Start this rewrite buffer out with a copy of the unmodified 93243791Sdim /// input buffer. 94243791Sdim void Initialize(const char *BufStart, const char *BufEnd) { 95243791Sdim Buffer.assign(BufStart, BufEnd); 96243791Sdim } 97243791Sdim 98243791Sdim /// getMappedOffset - Given an offset into the original SourceBuffer that this 99243791Sdim /// RewriteBuffer is based on, map it into the offset space of the 100243791Sdim /// RewriteBuffer. If AfterInserts is true and if the OrigOffset indicates a 101243791Sdim /// position where text is inserted, the location returned will be after any 102243791Sdim /// inserted text at the position. 103243791Sdim unsigned getMappedOffset(unsigned OrigOffset, 104243791Sdim bool AfterInserts = false) const{ 105243791Sdim return Deltas.getDeltaAt(2*OrigOffset+AfterInserts)+OrigOffset; 106243791Sdim } 107243791Sdim 108243791Sdim /// AddInsertDelta - When an insertion is made at a position, this 109243791Sdim /// method is used to record that information. 110243791Sdim void AddInsertDelta(unsigned OrigOffset, int Change) { 111243791Sdim return Deltas.AddDelta(2*OrigOffset, Change); 112243791Sdim } 113243791Sdim 114243791Sdim /// AddReplaceDelta - When a replacement/deletion is made at a position, this 115243791Sdim /// method is used to record that information. 116243791Sdim void AddReplaceDelta(unsigned OrigOffset, int Change) { 117243791Sdim return Deltas.AddDelta(2*OrigOffset+1, Change); 118243791Sdim } 119243791Sdim}; 120243791Sdim 121243791Sdim 122243791Sdim/// Rewriter - This is the main interface to the rewrite buffers. Its primary 123243791Sdim/// job is to dispatch high-level requests to the low-level RewriteBuffers that 124243791Sdim/// are involved. 125243791Sdimclass Rewriter { 126243791Sdim SourceManager *SourceMgr; 127243791Sdim const LangOptions *LangOpts; 128243791Sdim std::map<FileID, RewriteBuffer> RewriteBuffers; 129243791Sdimpublic: 130243791Sdim struct RewriteOptions { 131243791Sdim /// \brief Given a source range, true to include previous inserts at the 132243791Sdim /// beginning of the range as part of the range itself (true by default). 133243791Sdim bool IncludeInsertsAtBeginOfRange; 134243791Sdim /// \brief Given a source range, true to include previous inserts at the 135243791Sdim /// end of the range as part of the range itself (true by default). 136243791Sdim bool IncludeInsertsAtEndOfRange; 137243791Sdim /// \brief If true and removing some text leaves a blank line 138243791Sdim /// also remove the empty line (false by default). 139243791Sdim bool RemoveLineIfEmpty; 140243791Sdim 141243791Sdim RewriteOptions() 142243791Sdim : IncludeInsertsAtBeginOfRange(true), 143243791Sdim IncludeInsertsAtEndOfRange(true), 144243791Sdim RemoveLineIfEmpty(false) { } 145243791Sdim }; 146243791Sdim 147243791Sdim typedef std::map<FileID, RewriteBuffer>::iterator buffer_iterator; 148243791Sdim 149243791Sdim explicit Rewriter(SourceManager &SM, const LangOptions &LO) 150243791Sdim : SourceMgr(&SM), LangOpts(&LO) {} 151243791Sdim explicit Rewriter() : SourceMgr(0), LangOpts(0) {} 152243791Sdim 153243791Sdim void setSourceMgr(SourceManager &SM, const LangOptions &LO) { 154243791Sdim SourceMgr = &SM; 155243791Sdim LangOpts = &LO; 156243791Sdim } 157243791Sdim SourceManager &getSourceMgr() const { return *SourceMgr; } 158243791Sdim const LangOptions &getLangOpts() const { return *LangOpts; } 159243791Sdim 160243791Sdim /// isRewritable - Return true if this location is a raw file location, which 161243791Sdim /// is rewritable. Locations from macros, etc are not rewritable. 162243791Sdim static bool isRewritable(SourceLocation Loc) { 163243791Sdim return Loc.isFileID(); 164243791Sdim } 165243791Sdim 166243791Sdim /// getRangeSize - Return the size in bytes of the specified range if they 167243791Sdim /// are in the same file. If not, this returns -1. 168243791Sdim int getRangeSize(SourceRange Range, 169243791Sdim RewriteOptions opts = RewriteOptions()) const; 170243791Sdim int getRangeSize(const CharSourceRange &Range, 171243791Sdim RewriteOptions opts = RewriteOptions()) const; 172243791Sdim 173243791Sdim /// getRewrittenText - Return the rewritten form of the text in the specified 174243791Sdim /// range. If the start or end of the range was unrewritable or if they are 175243791Sdim /// in different buffers, this returns an empty string. 176243791Sdim /// 177243791Sdim /// Note that this method is not particularly efficient. 178243791Sdim /// 179243791Sdim std::string getRewrittenText(SourceRange Range) const; 180243791Sdim 181243791Sdim /// InsertText - Insert the specified string at the specified location in the 182243791Sdim /// original buffer. This method returns true (and does nothing) if the input 183243791Sdim /// location was not rewritable, false otherwise. 184243791Sdim /// 185243791Sdim /// \param indentNewLines if true new lines in the string are indented 186243791Sdim /// using the indentation of the source line in position \p Loc. 187243791Sdim bool InsertText(SourceLocation Loc, StringRef Str, 188243791Sdim bool InsertAfter = true, bool indentNewLines = false); 189243791Sdim 190243791Sdim /// InsertTextAfter - Insert the specified string at the specified location in 191243791Sdim /// the original buffer. This method returns true (and does nothing) if 192243791Sdim /// the input location was not rewritable, false otherwise. Text is 193243791Sdim /// inserted after any other text that has been previously inserted 194243791Sdim /// at the some point (the default behavior for InsertText). 195243791Sdim bool InsertTextAfter(SourceLocation Loc, StringRef Str) { 196243791Sdim return InsertText(Loc, Str); 197243791Sdim } 198243791Sdim 199243791Sdim /// \brief Insert the specified string after the token in the 200243791Sdim /// specified location. 201243791Sdim bool InsertTextAfterToken(SourceLocation Loc, StringRef Str); 202243791Sdim 203243791Sdim /// InsertText - Insert the specified string at the specified location in the 204243791Sdim /// original buffer. This method returns true (and does nothing) if the input 205243791Sdim /// location was not rewritable, false otherwise. Text is 206243791Sdim /// inserted before any other text that has been previously inserted 207243791Sdim /// at the some point. 208243791Sdim bool InsertTextBefore(SourceLocation Loc, StringRef Str) { 209243791Sdim return InsertText(Loc, Str, false); 210243791Sdim } 211243791Sdim 212243791Sdim /// RemoveText - Remove the specified text region. 213243791Sdim bool RemoveText(SourceLocation Start, unsigned Length, 214243791Sdim RewriteOptions opts = RewriteOptions()); 215243791Sdim 216243791Sdim /// \brief Remove the specified text region. 217243791Sdim bool RemoveText(CharSourceRange range, 218243791Sdim RewriteOptions opts = RewriteOptions()) { 219243791Sdim return RemoveText(range.getBegin(), getRangeSize(range, opts), opts); 220243791Sdim } 221243791Sdim 222243791Sdim /// \brief Remove the specified text region. 223243791Sdim bool RemoveText(SourceRange range, RewriteOptions opts = RewriteOptions()) { 224243791Sdim return RemoveText(range.getBegin(), getRangeSize(range, opts), opts); 225243791Sdim } 226243791Sdim 227243791Sdim /// ReplaceText - This method replaces a range of characters in the input 228243791Sdim /// buffer with a new string. This is effectively a combined "remove/insert" 229243791Sdim /// operation. 230243791Sdim bool ReplaceText(SourceLocation Start, unsigned OrigLength, 231243791Sdim StringRef NewStr); 232243791Sdim 233243791Sdim /// ReplaceText - This method replaces a range of characters in the input 234243791Sdim /// buffer with a new string. This is effectively a combined "remove/insert" 235243791Sdim /// operation. 236243791Sdim bool ReplaceText(SourceRange range, StringRef NewStr) { 237243791Sdim return ReplaceText(range.getBegin(), getRangeSize(range), NewStr); 238243791Sdim } 239243791Sdim 240243791Sdim /// ReplaceText - This method replaces a range of characters in the input 241243791Sdim /// buffer with a new string. This is effectively a combined "remove/insert" 242243791Sdim /// operation. 243243791Sdim bool ReplaceText(SourceRange range, SourceRange replacementRange); 244243791Sdim 245243791Sdim /// ReplaceStmt - This replaces a Stmt/Expr with another, using the pretty 246243791Sdim /// printer to generate the replacement code. This returns true if the input 247243791Sdim /// could not be rewritten, or false if successful. 248243791Sdim bool ReplaceStmt(Stmt *From, Stmt *To); 249243791Sdim 250243791Sdim /// \brief Increase indentation for the lines between the given source range. 251243791Sdim /// To determine what the indentation should be, 'parentIndent' is used 252243791Sdim /// that should be at a source location with an indentation one degree 253243791Sdim /// lower than the given range. 254243791Sdim bool IncreaseIndentation(CharSourceRange range, SourceLocation parentIndent); 255243791Sdim bool IncreaseIndentation(SourceRange range, SourceLocation parentIndent) { 256243791Sdim return IncreaseIndentation(CharSourceRange::getTokenRange(range), 257243791Sdim parentIndent); 258243791Sdim } 259243791Sdim 260243791Sdim /// ConvertToString converts statement 'From' to a string using the 261243791Sdim /// pretty printer. 262243791Sdim std::string ConvertToString(Stmt *From); 263243791Sdim 264243791Sdim /// getEditBuffer - This is like getRewriteBufferFor, but always returns a 265243791Sdim /// buffer, and allows you to write on it directly. This is useful if you 266243791Sdim /// want efficient low-level access to apis for scribbling on one specific 267243791Sdim /// FileID's buffer. 268243791Sdim RewriteBuffer &getEditBuffer(FileID FID); 269243791Sdim 270243791Sdim /// getRewriteBufferFor - Return the rewrite buffer for the specified FileID. 271243791Sdim /// If no modification has been made to it, return null. 272243791Sdim const RewriteBuffer *getRewriteBufferFor(FileID FID) const { 273243791Sdim std::map<FileID, RewriteBuffer>::const_iterator I = 274243791Sdim RewriteBuffers.find(FID); 275243791Sdim return I == RewriteBuffers.end() ? 0 : &I->second; 276243791Sdim } 277243791Sdim 278243791Sdim // Iterators over rewrite buffers. 279243791Sdim buffer_iterator buffer_begin() { return RewriteBuffers.begin(); } 280243791Sdim buffer_iterator buffer_end() { return RewriteBuffers.end(); } 281243791Sdim 282243791Sdim /// SaveFiles - Save all changed files to disk. 283243791Sdim /// 284243791Sdim /// Returns whether not all changes were saved successfully. 285243791Sdim /// Outputs diagnostics via the source manager's diagnostic engine 286243791Sdim /// in case of an error. 287243791Sdim bool overwriteChangedFiles(); 288243791Sdim 289243791Sdimprivate: 290243791Sdim unsigned getLocationOffsetAndFileID(SourceLocation Loc, FileID &FID) const; 291243791Sdim}; 292243791Sdim 293243791Sdim} // end namespace clang 294243791Sdim 295243791Sdim#endif 296