clang API Documentation

RewriteTest.cpp
Go to the documentation of this file.
00001 //===--- RewriteTest.cpp - Rewriter playground ----------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This is a testbed.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/Rewrite/Frontend/Rewriters.h"
00015 #include "clang/Lex/Preprocessor.h"
00016 #include "clang/Rewrite/Core/TokenRewriter.h"
00017 #include "llvm/Support/raw_ostream.h"
00018 
00019 void clang::DoRewriteTest(Preprocessor &PP, raw_ostream* OS) {
00020   SourceManager &SM = PP.getSourceManager();
00021   const LangOptions &LangOpts = PP.getLangOpts();
00022 
00023   TokenRewriter Rewriter(SM.getMainFileID(), SM, LangOpts);
00024 
00025   // Throw <i> </i> tags around comments.
00026   for (TokenRewriter::token_iterator I = Rewriter.token_begin(),
00027        E = Rewriter.token_end(); I != E; ++I) {
00028     if (I->isNot(tok::comment)) continue;
00029 
00030     Rewriter.AddTokenBefore(I, "<i>");
00031     Rewriter.AddTokenAfter(I, "</i>");
00032   }
00033 
00034 
00035   // Print out the output.
00036   for (TokenRewriter::token_iterator I = Rewriter.token_begin(),
00037        E = Rewriter.token_end(); I != E; ++I)
00038     *OS << PP.getSpelling(*I);
00039 }