blob: c4b3a7795650974ab34a4743d616d5d59469a140 [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
Ted Kremenekc2542b62009-03-31 18:58:14 +000014#include "clang-cc.h"
Chris Lattnerb13c5ee2008-10-12 05:29:20 +000015#include "clang/Lex/Preprocessor.h"
Chris Lattner99bd46c2008-10-12 06:09:52 +000016#include "clang/Rewrite/TokenRewriter.h"
Eli Friedmanf54fce82009-05-19 01:02:07 +000017#include "llvm/Support/raw_ostream.h"
Chris Lattnerb13c5ee2008-10-12 05:29:20 +000018
Eli Friedmanf54fce82009-05-19 01:02:07 +000019void clang::DoRewriteTest(Preprocessor &PP, llvm::raw_ostream* OS) {
Chris Lattnerb13c5ee2008-10-12 05:29:20 +000020 SourceManager &SM = PP.getSourceManager();
21 const LangOptions &LangOpts = PP.getLangOptions();
22
Chris Lattnercff9cc92008-10-12 05:44:03 +000023 TokenRewriter Rewriter(SM.getMainFileID(), SM, LangOpts);
Chris Lattner99bd46c2008-10-12 06:09:52 +000024
25 // Throw <i> </i> tags around comments.
26 for (TokenRewriter::token_iterator I = Rewriter.token_begin(),
27 E = Rewriter.token_end(); I != E; ++I) {
28 if (I->isNot(tok::comment)) continue;
29
30 Rewriter.AddTokenBefore(I, "<i>");
31 Rewriter.AddTokenAfter(I, "</i>");
Chris Lattnerb13c5ee2008-10-12 05:29:20 +000032 }
33
Chris Lattner99bd46c2008-10-12 06:09:52 +000034
35 // Print out the output.
Chris Lattnercff9cc92008-10-12 05:44:03 +000036 for (TokenRewriter::token_iterator I = Rewriter.token_begin(),
37 E = Rewriter.token_end(); I != E; ++I)
Eli Friedmanf54fce82009-05-19 01:02:07 +000038 *OS << PP.getSpelling(*I);
Matthijs Kooijman85b48972008-10-20 08:12:48 +000039}