Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 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 | |
Chris Lattner | cff9cc9 | 2008-10-12 05:44:03 +0000 | [diff] [blame^] | 24 | TokenRewriter Rewriter(SM.getMainFileID(), SM, LangOpts); |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 30 | 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 Lattner | cff9cc9 | 2008-10-12 05:44:03 +0000 | [diff] [blame^] | 46 | for (TokenRewriter::token_iterator I = Rewriter.token_begin(), |
| 47 | E = Rewriter.token_end(); I != E; ++I) |
| 48 | std::cout << PP.getSpelling(*I); |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 49 | } |