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

source code of clang/lib/Frontend/Rewrite/RewriteTest.cpp