From 8bd12b848bc353a9d34b54144c3dd3407b064292 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 15 Sep 2007 22:21:22 +0000 Subject: [PATCH] Initial checkin of rewriter interface. It is just stubbed out for now. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@41990 91177308-0d34-0410-b5e6-96231b3b80d8 --- Driver/Makefile | 4 +- Makefile | 2 +- Rewrite/Makefile | 22 ++++++++ Rewrite/Rewriter.cpp | 26 ++++++++++ clang.xcodeproj/project.pbxproj | 24 +++++++++ include/clang/Rewrite/Rewriter.h | 88 ++++++++++++++++++++++++++++++++ 6 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 Rewrite/Makefile create mode 100644 Rewrite/Rewriter.cpp create mode 100644 include/clang/Rewrite/Rewriter.h diff --git a/Driver/Makefile b/Driver/Makefile index 3577a2bca1..86c6143412 100644 --- a/Driver/Makefile +++ b/Driver/Makefile @@ -3,6 +3,8 @@ CPPFLAGS += -I$(PROJ_SRC_DIR)/../include CXXFLAGS = -fno-rtti TOOLNAME = clang -USEDLIBS = clangCodeGen.a clangAnalysis.a clangSEMA.a clangAST.a clangParse.a clangLex.a clangBasic.a LLVMCore.a LLVMSupport.a LLVMSystem.a +USEDLIBS = clangCodeGen.a clangAnalysis.a clangRewrite.a clangSEMA.a \ + clangAST.a clangParse.a clangLex.a clangBasic.a \ + LLVMCore.a LLVMSupport.a LLVMSystem.a include $(LEVEL)/Makefile.common diff --git a/Makefile b/Makefile index db13637f64..e7b647fe43 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ LEVEL = ../.. -DIRS := Basic Lex Parse AST Sema CodeGen Analysis Driver +DIRS := Basic Lex Parse AST Sema CodeGen Analysis Rewrite Driver include $(LEVEL)/Makefile.common diff --git a/Rewrite/Makefile b/Rewrite/Makefile new file mode 100644 index 0000000000..106b61890b --- /dev/null +++ b/Rewrite/Makefile @@ -0,0 +1,22 @@ +##===- clang/Rewrite/Makefile ------------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file was developed by Chris Lattner and is distributed under +# the University of Illinois Open Source License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## +# +# This implements code transformation / rewriting facilities. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../../.. +LIBRARYNAME := clangRewrite +BUILD_ARCHIVE = 1 +CXXFLAGS = -fno-rtti + +CPPFLAGS += -I$(PROJ_SRC_DIR)/../include + +include $(LEVEL)/Makefile.common + diff --git a/Rewrite/Rewriter.cpp b/Rewrite/Rewriter.cpp new file mode 100644 index 0000000000..c4856adeea --- /dev/null +++ b/Rewrite/Rewriter.cpp @@ -0,0 +1,26 @@ +//===--- Rewriter.cpp - Code rewriting interface --------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Chris Lattner and 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. +// +//===----------------------------------------------------------------------===// + +#include "clang/Rewrite/Rewriter.h" +using namespace clang; + + +void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size) { + // FIXME: +} + +void RewriteBuffer::InsertText(unsigned OrigOffset, + const char *StrData, unsigned StrLen) { + // FIXME: +} diff --git a/clang.xcodeproj/project.pbxproj b/clang.xcodeproj/project.pbxproj index 52139a3bda..578a156700 100644 --- a/clang.xcodeproj/project.pbxproj +++ b/clang.xcodeproj/project.pbxproj @@ -131,6 +131,8 @@ DEF2EDA70C6A4252000C4259 /* StmtDumper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DEF2EDA60C6A4252000C4259 /* StmtDumper.cpp */; }; DEF2EFF30C6CDD74000C4259 /* CGExprAgg.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DEF2EFF20C6CDD74000C4259 /* CGExprAgg.cpp */; }; DEF2F0100C6CFED5000C4259 /* SemaChecking.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DEF2F00F0C6CFED5000C4259 /* SemaChecking.cpp */; }; + DEF7D9F70C9C8B1A0001F598 /* Rewriter.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DEF7D9F60C9C8B1A0001F598 /* Rewriter.h */; }; + DEF7D9F90C9C8B1D0001F598 /* Rewriter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DEF7D9F80C9C8B1D0001F598 /* Rewriter.cpp */; }; F0226FD20C18084500141F42 /* TextDiagnosticPrinter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F0226FD00C18084500141F42 /* TextDiagnosticPrinter.cpp */; }; F0226FD30C18084500141F42 /* TextDiagnosticPrinter.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = F0226FD10C18084500141F42 /* TextDiagnosticPrinter.h */; }; /* End PBXBuildFile section */ @@ -199,6 +201,7 @@ DEF2E9320C5FB9FB000C4259 /* ASTStreamers.h in CopyFiles */, DEF2E95F0C5FBD74000C4259 /* InternalsManual.html in CopyFiles */, DEC63B1C0C7B940600DBF169 /* CFG.h in CopyFiles */, + DEF7D9F70C9C8B1A0001F598 /* Rewriter.h in CopyFiles */, ); runOnlyForDeploymentPostprocessing = 1; }; @@ -339,6 +342,8 @@ DEF2EDA60C6A4252000C4259 /* StmtDumper.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = StmtDumper.cpp; path = AST/StmtDumper.cpp; sourceTree = ""; }; DEF2EFF20C6CDD74000C4259 /* CGExprAgg.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = CGExprAgg.cpp; path = CodeGen/CGExprAgg.cpp; sourceTree = ""; }; DEF2F00F0C6CFED5000C4259 /* SemaChecking.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = SemaChecking.cpp; path = Sema/SemaChecking.cpp; sourceTree = ""; }; + DEF7D9F60C9C8B1A0001F598 /* Rewriter.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = Rewriter.h; path = clang/Rewrite/Rewriter.h; sourceTree = ""; }; + DEF7D9F80C9C8B1D0001F598 /* Rewriter.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = Rewriter.cpp; path = Rewrite/Rewriter.cpp; sourceTree = ""; }; F0226FD00C18084500141F42 /* TextDiagnosticPrinter.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = TextDiagnosticPrinter.cpp; path = Driver/TextDiagnosticPrinter.cpp; sourceTree = ""; }; F0226FD10C18084500141F42 /* TextDiagnosticPrinter.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = TextDiagnosticPrinter.h; path = Driver/TextDiagnosticPrinter.h; sourceTree = ""; }; /* End PBXFileReference section */ @@ -376,6 +381,7 @@ DE67E7070C020EAB00F66BC5 /* Sema */, DE927FCC0C0557CD00231DA4 /* CodeGen */, 356EF9B30C8F7DCA006650F5 /* Analysis */, + DEF7D9F50C9C8B0C0001F598 /* Rewrite */, ); name = Source; sourceTree = ""; @@ -588,6 +594,7 @@ DE67E7260C02108300F66BC5 /* Sema */, DE928B140C05659A00231DA4 /* CodeGen */, 356EF9AF0C8F7DA4006650F5 /* Analysis */, + DEF7D9F40C9C8B020001F598 /* Rewrite */, ); path = include; sourceTree = ""; @@ -659,6 +666,22 @@ path = Lex; sourceTree = ""; }; + DEF7D9F40C9C8B020001F598 /* Rewrite */ = { + isa = PBXGroup; + children = ( + DEF7D9F60C9C8B1A0001F598 /* Rewriter.h */, + ); + name = Rewrite; + sourceTree = ""; + }; + DEF7D9F50C9C8B0C0001F598 /* Rewrite */ = { + isa = PBXGroup; + children = ( + DEF7D9F80C9C8B1D0001F598 /* Rewriter.cpp */, + ); + name = Rewrite; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -770,6 +793,7 @@ 356EF9B50C8F7DDF006650F5 /* LiveVariables.cpp in Sources */, 355CF6840C90A8D400A08AA3 /* DeadStores.cpp in Sources */, 35AE0F690C9B4CC200CC1279 /* UnintializedValues.cpp in Sources */, + DEF7D9F90C9C8B1D0001F598 /* Rewriter.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/include/clang/Rewrite/Rewriter.h b/include/clang/Rewrite/Rewriter.h new file mode 100644 index 0000000000..09a99808ed --- /dev/null +++ b/include/clang/Rewrite/Rewriter.h @@ -0,0 +1,88 @@ +//===--- Rewriter.h - Code rewriting interface ------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Chris Lattner and 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 + +namespace clang { + class SourceManager; + class SourceLocation; + +/// 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; +}; + + +/// 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 { + /// 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 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. + std::vector Buffer; +public: + + /// 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. + /// + /// TODO: Consider a bool to indicate whether the text is inserted 'before' or + /// after the atomic point: i.e. whether the atomic point is moved to after + /// the inserted text or not. + void InsertText(unsigned OrigOffset, const char *StrData, unsigned StrLen); + +}; + +class Rewriter { + SourceManager &SourceMgr; + + // FIXME: list of buffers. +public: + explicit Rewriter(SourceManager &SM) : SourceMgr(SM) {} + + /// InsertText - Insert the specified string at the specified location in the + /// original buffer. + bool InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen); + + /// RemoveText - Remove the specified text region. + bool RemoveText(SourceLocation Start, SourceLocation End); + + + // TODO: Replace Stmt/Expr with another. + + + // Write out output buffer. + +}; + +} // end namespace clang + +#endif -- 2.40.0