blob: b0791f4cddd7c1dd181b22273c3dfe3d6a075058 [file] [log] [blame]
Chris Lattner1782da22008-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
Chris Lattner1782da22008-10-12 05:29:20 +000014#include "clang/Lex/Preprocessor.h"
Ted Kremenekcdf81492012-09-01 05:09:24 +000015#include "clang/Rewrite/Core/TokenRewriter.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000016#include "clang/Rewrite/Frontend/Rewriters.h"
Eli Friedmanfcb57d52009-05-19 01:02:07 +000017#include "llvm/Support/raw_ostream.h"
Chris Lattner1782da22008-10-12 05:29:20 +000018
Mehdi Amini9670f842016-07-18 19:02:11 +000019void clang::DoRewriteTest(Preprocessor &PP, raw_ostream *OS) {
Chris Lattner1782da22008-10-12 05:29:20 +000020 SourceManager &SM = PP.getSourceManager();
David Blaikiebbafb8a2012-03-11 07:00:24 +000021 const LangOptions &LangOpts = PP.getLangOpts();
Chris Lattner1782da22008-10-12 05:29:20 +000022
Chris Lattnerb6aa53b2008-10-12 05:44:03 +000023 TokenRewriter Rewriter(SM.getMainFileID(), SM, LangOpts);
Chris Lattnerf6a3bda2008-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 Lattner1782da22008-10-12 05:29:20 +000032 }
Mike Stump11289f42009-09-09 15:08:12 +000033
34
Chris Lattnerf6a3bda2008-10-12 06:09:52 +000035 // Print out the output.
Chris Lattnerb6aa53b2008-10-12 05:44:03 +000036 for (TokenRewriter::token_iterator I = Rewriter.token_begin(),
37 E = Rewriter.token_end(); I != E; ++I)
Eli Friedmanfcb57d52009-05-19 01:02:07 +000038 *OS << PP.getSpelling(*I);
Matthijs Kooijman1f7fac02008-10-20 08:12:48 +000039}