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