RewriteTest.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. //===--- RewriteTest.cpp - Rewriter playground ----------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This is a testbed.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Lex/Preprocessor.h"
  13. #include "clang/Rewrite/Core/TokenRewriter.h"
  14. #include "clang/Rewrite/Frontend/Rewriters.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. void clang::DoRewriteTest(Preprocessor &PP, raw_ostream *OS) {
  17. SourceManager &SM = PP.getSourceManager();
  18. const LangOptions &LangOpts = PP.getLangOpts();
  19. TokenRewriter Rewriter(SM.getMainFileID(), SM, LangOpts);
  20. // Throw <i> </i> tags around comments.
  21. for (TokenRewriter::token_iterator I = Rewriter.token_begin(),
  22. E = Rewriter.token_end(); I != E; ++I) {
  23. if (I->isNot(tok::comment)) continue;
  24. Rewriter.AddTokenBefore(I, "<i>");
  25. Rewriter.AddTokenAfter(I, "</i>");
  26. }
  27. // Print out the output.
  28. for (TokenRewriter::token_iterator I = Rewriter.token_begin(),
  29. E = Rewriter.token_end(); I != E; ++I)
  30. *OS << PP.getSpelling(*I);
  31. }