Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit b13c5ee

Browse files
committed
Add a new -rewrite-test option, which is basically a
playground to experiment with some new rewriter approaches. For now it is probably the most complex version of 'cat' ever invented. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@57406 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent c106c10 commit b13c5ee

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

Driver/RewriteTest.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===--- RewriteTest.cpp - Rewriter playground ----------------------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This is a testbed.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "clang/Rewrite/TokenRewriter.h"
15+
#include "clang.h"
16+
#include "clang/Lex/Preprocessor.h"
17+
#include <iostream>
18+
19+
void clang::DoRewriteTest(Preprocessor &PP, const std::string &InFileName,
20+
const std::string &OutFileName) {
21+
SourceManager &SM = PP.getSourceManager();
22+
const LangOptions &LangOpts = PP.getLangOptions();
23+
24+
std::pair<const char*,const char*> File =SM.getBufferData(SM.getMainFileID());
25+
26+
// Create a lexer to lex all the tokens of the main file in raw mode. Even
27+
// though it is in raw mode, it will not return comments.
28+
Lexer RawLex(SourceLocation::getFileLoc(SM.getMainFileID(), 0),
29+
LangOpts, File.first, File.second);
30+
31+
RawLex.SetKeepWhitespaceMode(true);
32+
33+
Token RawTok;
34+
RawLex.LexFromRawLexer(RawTok);
35+
while (RawTok.isNot(tok::eof)) {
36+
std::cout << PP.getSpelling(RawTok);
37+
RawLex.LexFromRawLexer(RawTok);
38+
}
39+
40+
41+
}

Driver/clang.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ enum ProgActions {
6666
RewriteObjC, // ObjC->C Rewriter.
6767
RewriteBlocks, // ObjC->C Rewriter for Blocks.
6868
RewriteMacros, // Expand macros but not #includes.
69+
RewriteTest, // Rewriter playground
6970
HTMLTest, // HTML displayer testing stuff.
7071
EmitLLVM, // Emit a .ll file.
7172
EmitBC, // Emit a .bc file.
@@ -119,6 +120,8 @@ ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore,
119120
"Build ASTs then convert to LLVM, emit .bc file"),
120121
clEnumValN(SerializeAST, "serialize",
121122
"Build ASTs and emit .ast file"),
123+
clEnumValN(RewriteTest, "rewrite-test",
124+
"Rewriter playground"),
122125
clEnumValN(RewriteObjC, "rewrite-objc",
123126
"Rewrite ObjC into C (code rewriter example)"),
124127
clEnumValN(RewriteMacros, "rewrite-macros",
@@ -1173,6 +1176,11 @@ static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF,
11731176
RewriteMacrosInInput(PP, InFile, OutputFile);
11741177
ClearSourceMgr = true;
11751178
break;
1179+
1180+
case RewriteTest:
1181+
DoRewriteTest(PP, InFile, OutputFile);
1182+
ClearSourceMgr = true;
1183+
break;
11761184
}
11771185

11781186
if (Consumer) {

Driver/clang.h

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ void DoPrintPreprocessedInput(Preprocessor &PP, const std::string& OutFile);
3232
/// RewriteMacrosInInput - Implement -rewrite-macros mode.
3333
void RewriteMacrosInInput(Preprocessor &PP, const std::string &InFileName,
3434
const std::string& OutFile);
35+
36+
void DoRewriteTest(Preprocessor &PP, const std::string &InFileName,
37+
const std::string &OutFileName);
38+
3539

3640
/// CreatePrintParserActionsAction - Return the actions implementation that
3741
/// implements the -parse-print-callbacks option.

clang.xcodeproj/project.pbxproj

+6
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
DE4772FA0C10EAE5002239E8 /* CGStmt.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE4772F90C10EAE5002239E8 /* CGStmt.cpp */; };
123123
DE4772FC0C10EAEC002239E8 /* CGExpr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE4772FB0C10EAEC002239E8 /* CGExpr.cpp */; };
124124
DE47999C0D2EBE1A00706D2D /* SemaExprObjC.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE47999B0D2EBE1A00706D2D /* SemaExprObjC.cpp */; };
125+
DE4DC79E0EA1C09E00069E5A /* RewriteTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE4DC79D0EA1C09E00069E5A /* RewriteTest.cpp */; };
125126
DE5932D10AD60FF400BC794C /* clang.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE5932CD0AD60FF400BC794C /* clang.cpp */; };
126127
DE5932D20AD60FF400BC794C /* clang.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DE5932CE0AD60FF400BC794C /* clang.h */; };
127128
DE5932D30AD60FF400BC794C /* PrintParserCallbacks.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE5932CF0AD60FF400BC794C /* PrintParserCallbacks.cpp */; };
@@ -443,6 +444,8 @@
443444
DE4772F90C10EAE5002239E8 /* CGStmt.cpp */ = {isa = PBXFileReference; fileEncoding = 30; indentWidth = 2; lastKnownFileType = sourcecode.cpp.cpp; name = CGStmt.cpp; path = lib/CodeGen/CGStmt.cpp; sourceTree = "<group>"; tabWidth = 2; };
444445
DE4772FB0C10EAEC002239E8 /* CGExpr.cpp */ = {isa = PBXFileReference; fileEncoding = 30; indentWidth = 2; lastKnownFileType = sourcecode.cpp.cpp; name = CGExpr.cpp; path = lib/CodeGen/CGExpr.cpp; sourceTree = "<group>"; tabWidth = 2; };
445446
DE47999B0D2EBE1A00706D2D /* SemaExprObjC.cpp */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.cpp.cpp; name = SemaExprObjC.cpp; path = lib/Sema/SemaExprObjC.cpp; sourceTree = "<group>"; tabWidth = 2; };
447+
DE4DC7980EA1BE4400069E5A /* TokenRewriter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TokenRewriter.h; path = clang/Rewrite/TokenRewriter.h; sourceTree = "<group>"; };
448+
DE4DC79D0EA1C09E00069E5A /* RewriteTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = RewriteTest.cpp; path = Driver/RewriteTest.cpp; sourceTree = "<group>"; };
446449
DE53370B0CE2D96F00D9A028 /* RewriteRope.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RewriteRope.h; path = clang/Rewrite/RewriteRope.h; sourceTree = "<group>"; };
447450
DE5932CD0AD60FF400BC794C /* clang.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = clang.cpp; path = Driver/clang.cpp; sourceTree = "<group>"; };
448451
DE5932CE0AD60FF400BC794C /* clang.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = clang.h; path = Driver/clang.h; sourceTree = "<group>"; };
@@ -883,6 +886,7 @@
883886
9030C1090E807A9300941490 /* RewriteBlocks.cpp */,
884887
DEA0EBD90DD2D3C8007A02A9 /* RewriteMacros.cpp */,
885888
035611E10DB40C8100D2EF2A /* RewriteObjC.cpp */,
889+
DE4DC79D0EA1C09E00069E5A /* RewriteTest.cpp */,
886890
352981080CC58344008B5E84 /* SerializationTest.cpp */,
887891
);
888892
name = Driver;
@@ -1057,6 +1061,7 @@
10571061
35F2BE7B0DAC2963006E7668 /* HTMLRewrite.h */,
10581062
DEF7D9F60C9C8B1A0001F598 /* Rewriter.h */,
10591063
DE53370B0CE2D96F00D9A028 /* RewriteRope.h */,
1064+
DE4DC7980EA1BE4400069E5A /* TokenRewriter.h */,
10601065
);
10611066
name = Rewrite;
10621067
sourceTree = "<group>";
@@ -1253,6 +1258,7 @@
12531258
355106860E9A8507006A4E44 /* MemRegion.cpp in Sources */,
12541259
3551068C0E9A8546006A4E44 /* ParsePragma.cpp in Sources */,
12551260
3551068D0E9A8546006A4E44 /* ParseTentative.cpp in Sources */,
1261+
DE4DC79E0EA1C09E00069E5A /* RewriteTest.cpp in Sources */,
12561262
);
12571263
runOnlyForDeploymentPostprocessing = 0;
12581264
};

0 commit comments

Comments
 (0)