Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 1 | //===--- RewriteTest.cpp - Playground for the code rewriter ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Hacks and fun related to the code rewriter. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "ASTConsumers.h" |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 15 | #include "clang/Rewrite/Rewriter.h" |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/AST.h" |
| 17 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
| 21 | |
| 22 | namespace { |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 23 | class RewriteTest : public ASTConsumer { |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 24 | Rewriter Rewrite; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 25 | SourceManager *SM; |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 26 | unsigned MainFileID; |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 27 | SourceLocation LastIncLoc; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 28 | public: |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 29 | void Initialize(ASTContext &Context, unsigned mainFileID) { |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 30 | SM = &Context.SourceMgr; |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 31 | MainFileID = mainFileID; |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 32 | Rewrite.setSourceMgr(Context.SourceMgr); |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | virtual void HandleTopLevelDecl(Decl *D); |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 37 | void HandleDeclInMainFile(Decl *D); |
| 38 | void RewriteInclude(SourceLocation Loc); |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame^] | 39 | |
| 40 | void RewriteFunctionBody(Stmt *S); |
| 41 | void RewriteAtEncode(ObjCEncodeExpr *Exp); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 43 | ~RewriteTest(); |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 44 | }; |
| 45 | } |
| 46 | |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 47 | ASTConsumer *clang::CreateCodeRewriterTest() { return new RewriteTest(); } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 49 | void RewriteTest::HandleTopLevelDecl(Decl *D) { |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 50 | // Two cases: either the decl could be in the main file, or it could be in a |
| 51 | // #included file. If the former, rewrite it now. If the later, check to see |
| 52 | // if we rewrote the #include/#import. |
| 53 | SourceLocation Loc = D->getLocation(); |
| 54 | Loc = SM->getLogicalLoc(Loc); |
| 55 | |
| 56 | // If this is for a builtin, ignore it. |
| 57 | if (Loc.isInvalid()) return; |
| 58 | |
| 59 | if (SM->getDecomposedFileLoc(Loc).first == MainFileID) |
| 60 | return HandleDeclInMainFile(D); |
| 61 | |
| 62 | RewriteInclude(Loc); |
| 63 | } |
| 64 | |
| 65 | void RewriteTest::RewriteInclude(SourceLocation Loc) { |
| 66 | // Rip up the #include stack to the main file. |
| 67 | SourceLocation IncLoc = Loc, NextLoc = Loc; |
| 68 | do { |
| 69 | IncLoc = Loc; |
| 70 | Loc = SM->getLogicalLoc(NextLoc); |
| 71 | NextLoc = SM->getIncludeLoc(Loc); |
| 72 | } while (!NextLoc.isInvalid()); |
| 73 | |
| 74 | // Loc is now the location of the #include filename "foo" or <foo/bar.h>. |
| 75 | // IncLoc indicates the header that was included if it is useful. |
| 76 | IncLoc = SM->getLogicalLoc(IncLoc); |
| 77 | if (SM->getDecomposedFileLoc(Loc).first != MainFileID || |
| 78 | Loc == LastIncLoc) |
| 79 | return; |
| 80 | LastIncLoc = Loc; |
| 81 | |
| 82 | unsigned IncCol = SM->getColumnNumber(Loc); |
| 83 | SourceLocation LineStartLoc = Loc.getFileLocWithOffset(-IncCol+1); |
| 84 | |
| 85 | // Replace the #import with #include. |
| 86 | Rewrite.ReplaceText(LineStartLoc, IncCol-1, "#include ", strlen("#include ")); |
| 87 | } |
| 88 | |
| 89 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 90 | /// main file of the input. |
| 91 | void RewriteTest::HandleDeclInMainFile(Decl *D) { |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame^] | 92 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 93 | if (Stmt *Body = FD->getBody()) |
| 94 | RewriteFunctionBody(Body); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 95 | // Nothing yet. |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame^] | 99 | void RewriteTest::RewriteFunctionBody(Stmt *S) { |
| 100 | // Handle specific things. |
| 101 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 102 | return RewriteAtEncode(AtEncode); |
| 103 | |
| 104 | // Otherwise, just rewrite all children. |
| 105 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 106 | CI != E; ++CI) |
| 107 | RewriteFunctionBody(*CI); |
| 108 | } |
| 109 | |
| 110 | void RewriteTest::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
| 111 | #if 0 |
| 112 | int Size = Rewrite.getRangeSize(Exp->getSourceRange()); |
| 113 | if (Size == -1) { |
| 114 | printf("BLAH!"); |
| 115 | } |
| 116 | |
| 117 | Rewrite.RemoveText(Exp->getEncLoc(), Size); |
| 118 | #endif |
| 119 | } |
| 120 | |
| 121 | |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 122 | RewriteTest::~RewriteTest() { |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 123 | // Get the top-level buffer that this corresponds to. |
| 124 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 125 | const char *MainBufStart = MainBuf.first; |
| 126 | const char *MainBufEnd = MainBuf.second; |
| 127 | |
| 128 | // Loop over the whole file, looking for tabs. |
| 129 | for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) { |
| 130 | if (*BufPtr != '\t') |
| 131 | continue; |
| 132 | |
| 133 | // Okay, we found a tab. This tab will turn into at least one character, |
| 134 | // but it depends on which 'virtual column' it is in. Compute that now. |
| 135 | unsigned VCol = 0; |
| 136 | while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' && |
| 137 | BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r') |
| 138 | ++VCol; |
| 139 | |
| 140 | // Okay, now that we know the virtual column, we know how many spaces to |
| 141 | // insert. We assume 8-character tab-stops. |
| 142 | unsigned Spaces = 8-(VCol & 7); |
| 143 | |
| 144 | // Get the location of the tab. |
| 145 | SourceLocation TabLoc = |
| 146 | SourceLocation::getFileLoc(MainFileID, BufPtr-MainBufStart); |
| 147 | |
| 148 | // Rewrite the single tab character into a sequence of spaces. |
Chris Lattner | 57c337d | 2007-10-13 00:46:29 +0000 | [diff] [blame] | 149 | Rewrite.ReplaceText(TabLoc, 1, " ", Spaces); |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 153 | // we are done. |
| 154 | if (const RewriteBuffer *RewriteBuf = |
| 155 | Rewrite.getRewriteBufferFor(MainFileID)) { |
Chris Lattner | 7c23960 | 2007-10-13 00:11:23 +0000 | [diff] [blame] | 156 | printf("Changed:\n"); |
| 157 | std::string S(RewriteBuf->begin(), RewriteBuf->end()); |
| 158 | printf("%s\n", S.c_str()); |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 159 | } else { |
| 160 | printf("No changes\n"); |
| 161 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 162 | } |