blob: 6709860272bcdc5c884928fdceff1b1bc6ec9d97 [file] [log] [blame]
Chris Lattnerb13c5ee2008-10-12 05:29:20 +00001//===--- 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
19void 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
Chris Lattnercff9cc92008-10-12 05:44:03 +000024 TokenRewriter Rewriter(SM.getMainFileID(), SM, LangOpts);
25
26
27
28
29
Chris Lattnerb13c5ee2008-10-12 05:29:20 +000030 std::pair<const char*,const char*> File =SM.getBufferData(SM.getMainFileID());
31
32 // Create a lexer to lex all the tokens of the main file in raw mode. Even
33 // though it is in raw mode, it will not return comments.
34 Lexer RawLex(SourceLocation::getFileLoc(SM.getMainFileID(), 0),
35 LangOpts, File.first, File.second);
36
37 RawLex.SetKeepWhitespaceMode(true);
38
39 Token RawTok;
40 RawLex.LexFromRawLexer(RawTok);
41 while (RawTok.isNot(tok::eof)) {
42 std::cout << PP.getSpelling(RawTok);
43 RawLex.LexFromRawLexer(RawTok);
44 }
45
Chris Lattnercff9cc92008-10-12 05:44:03 +000046 for (TokenRewriter::token_iterator I = Rewriter.token_begin(),
47 E = Rewriter.token_end(); I != E; ++I)
48 std::cout << PP.getSpelling(*I);
Chris Lattnerb13c5ee2008-10-12 05:29:20 +000049}