blob: 731d6fe7774caa62d844441f57c69fd81ac76f2b [file] [log] [blame]
Chris Lattner77cd2a02007-10-11 00:43:27 +00001//===--- 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 Lattner8a12c272007-10-11 18:38:32 +000015#include "clang/Rewrite/Rewriter.h"
Chris Lattner77cd2a02007-10-11 00:43:27 +000016#include "clang/AST/AST.h"
17#include "clang/AST/ASTConsumer.h"
Chris Lattner8a12c272007-10-11 18:38:32 +000018#include "clang/Basic/SourceManager.h"
Chris Lattner77cd2a02007-10-11 00:43:27 +000019using namespace clang;
20
21
22namespace {
Chris Lattner8a12c272007-10-11 18:38:32 +000023 class RewriteTest : public ASTConsumer {
Chris Lattner2c64b7b2007-10-16 21:07:07 +000024 Rewriter Rewrite;
Chris Lattner77cd2a02007-10-11 00:43:27 +000025 SourceManager *SM;
Chris Lattner8a12c272007-10-11 18:38:32 +000026 unsigned MainFileID;
Chris Lattner2c64b7b2007-10-16 21:07:07 +000027 SourceLocation LastIncLoc;
Chris Lattner77cd2a02007-10-11 00:43:27 +000028 public:
Chris Lattner8a12c272007-10-11 18:38:32 +000029 void Initialize(ASTContext &Context, unsigned mainFileID) {
Chris Lattner77cd2a02007-10-11 00:43:27 +000030 SM = &Context.SourceMgr;
Chris Lattner8a12c272007-10-11 18:38:32 +000031 MainFileID = mainFileID;
Chris Lattner2c64b7b2007-10-16 21:07:07 +000032 Rewrite.setSourceMgr(Context.SourceMgr);
Chris Lattner77cd2a02007-10-11 00:43:27 +000033 }
34
35 virtual void HandleTopLevelDecl(Decl *D);
Chris Lattner8a12c272007-10-11 18:38:32 +000036
Chris Lattner2c64b7b2007-10-16 21:07:07 +000037 void HandleDeclInMainFile(Decl *D);
38 void RewriteInclude(SourceLocation Loc);
Chris Lattner311ff022007-10-16 22:36:42 +000039
40 void RewriteFunctionBody(Stmt *S);
41 void RewriteAtEncode(ObjCEncodeExpr *Exp);
Chris Lattner2c64b7b2007-10-16 21:07:07 +000042
Chris Lattner8a12c272007-10-11 18:38:32 +000043 ~RewriteTest();
Chris Lattner77cd2a02007-10-11 00:43:27 +000044 };
45}
46
Chris Lattner8a12c272007-10-11 18:38:32 +000047ASTConsumer *clang::CreateCodeRewriterTest() { return new RewriteTest(); }
Chris Lattner77cd2a02007-10-11 00:43:27 +000048
Chris Lattner8a12c272007-10-11 18:38:32 +000049void RewriteTest::HandleTopLevelDecl(Decl *D) {
Chris Lattner2c64b7b2007-10-16 21:07:07 +000050 // 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
65void 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.
91void RewriteTest::HandleDeclInMainFile(Decl *D) {
Chris Lattner311ff022007-10-16 22:36:42 +000092 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
93 if (Stmt *Body = FD->getBody())
94 RewriteFunctionBody(Body);
Chris Lattner2c64b7b2007-10-16 21:07:07 +000095 // Nothing yet.
Chris Lattner8a12c272007-10-11 18:38:32 +000096}
97
98
Chris Lattner311ff022007-10-16 22:36:42 +000099void 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
110void 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 Lattner8a12c272007-10-11 18:38:32 +0000122RewriteTest::~RewriteTest() {
Chris Lattner8a12c272007-10-11 18:38:32 +0000123 // 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 Lattner57c337d2007-10-13 00:46:29 +0000149 Rewrite.ReplaceText(TabLoc, 1, " ", Spaces);
Chris Lattner8a12c272007-10-11 18:38:32 +0000150 }
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 Lattner7c239602007-10-13 00:11:23 +0000156 printf("Changed:\n");
157 std::string S(RewriteBuf->begin(), RewriteBuf->end());
158 printf("%s\n", S.c_str());
Chris Lattner8a12c272007-10-11 18:38:32 +0000159 } else {
160 printf("No changes\n");
161 }
Chris Lattner77cd2a02007-10-11 00:43:27 +0000162}