RewriteTest.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. #include "clang/Rewrite/Frontend/Rewriters.h"
  14. #include "clang/Lex/Preprocessor.h"
  15. #include "clang/Rewrite/Core/TokenRewriter.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. void clang::DoRewriteTest(Preprocessor &PP, raw_ostream* OS) {
  18. SourceManager &SM = PP.getSourceManager();
  19. const LangOptions &LangOpts = PP.getLangOpts();
  20. TokenRewriter Rewriter(SM.getMainFileID(), SM, LangOpts);
  21. // Throw <i> </i> tags around comments.
  22. for (TokenRewriter::token_iterator I = Rewriter.token_begin(),
  23. E = Rewriter.token_end(); I != E; ++I) {
  24. if (I->isNot(tok::comment)) continue;
  25. Rewriter.AddTokenBefore(I, "<i>");
  26. Rewriter.AddTokenAfter(I, "</i>");
  27. }
  28. // Print out the output.
  29. for (TokenRewriter::token_iterator I = Rewriter.token_begin(),
  30. E = Rewriter.token_end(); I != E; ++I)
  31. *OS << PP.getSpelling(*I);
  32. }